interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / src / tm.om

143 lines · 4085 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tm.om (ot_tm_): civil UTC date-time to Unix seconds, and the two ASN.1 time forms that
3note RFC 5280 4.1.2.5 allows in a certificate (UTCTime YYMMDDHHMMSSZ, GeneralizedTime YYYYMMDDHHMMSSZ:
4note Z only, seconds present, no fraction; UTCTime YY >= 50 is 19YY, YY < 50 is 20YY).
5note ot_tm_civil uses the days-from-civil method: shift the year to start on 1 March so the leap day
6note is the last day, count 400-year eras (146097 days each), then days within the era; 719468 is the
7note day number of 1970-01-01 in that count. Only proleptic Gregorian dates, no leap seconds.
8note wolfram: UnixTime[DateObject[{1970,1,1,0,0,0},TimeZone->0]] = 0
9note wolfram: UnixTime[DateObject[{2000,2,29,0,0,0},TimeZone->0]] = 951782400
10note wolfram: UnixTime[DateObject[{2038,1,19,3,14,8},TimeZone->0]] = 2147483648
11note wolfram: UnixTime[DateObject[{2049,12,31,23,59,59},TimeZone->0]] = 2524607999
12note wolfram: UnixTime[DateObject[{2050,1,1,0,0,0},TimeZone->0]] = 2524608000
13note wolfram: UnixTime[DateObject[{1950,1,1,0,0,0},TimeZone->0]] = -631152000
14note wolfram: UnixTime[DateObject[{9999,12,31,23,59,59},TimeZone->0]] = 253402300799
15note wolfram: LeapYearQ[{y}] for y = 1900, 2000, 2100, 2024 = False, True, False, True
16note wolfram: UnixTime[DateObject[{1,1,1,0,0,0},TimeZone->0]] = -62135596800 (proleptic Gregorian)
17note wolfram: UnixTime[DateObject[{2100,3,1,0,0,0},TimeZone->0]] = 4107542400
18note ot_tm_bad: the value every refusal returns (no certificate time can be it)
19note Unit order: src/der.om must come earlier in the unit (a global is declared before its first use).
20const ot_tm_bad long -9223372036854775808
21
22fn ot_tm_leap y long to bool
23if ne rem y 4 0
24ret false
25end
26if ne rem y 100 0
27ret true
28end
29ret eq rem y 400 0
30end
31
32fn ot_tm_mdays y long mo long to long
33if eq mo 2
34if ot_tm_leap y
35ret 29
36end
37ret 28
38end
39if or or eq mo 4 eq mo 6 or eq mo 9 eq mo 11
40ret 30
41end
42ret 31
43end
44
45note ot_tm_days: the day number of y-mo-d counted from 1970-01-01
46fn ot_tm_days y long mo long d long to long
47yy = y
48if le mo 2
49yy = sub y 1
50end
51era = div yy 400
52if lt yy 0
53era = div sub yy 399 400
54end
55yoe = sub yy mul era 400
56mp = add mo 9
57if gt mo 2
58mp = sub mo 3
59end
60x = div add mul mp 153 2 5
61x = add x sub d 1
62x = add x mul yoe 365
63x = add x div yoe 4
64x = sub x div yoe 100
65x = add x mul era 146097
66ret sub x 719468
67end
68
69note ot_tm_civil: seconds since 1970-01-01T00:00:00Z; the caller checks the fields are in range
70fn ot_tm_civil y long mo long d long h long mi long s long to long
71x = mul ot_tm_days y mo d 86400
72x = add x mul h 3600
73x = add x mul mi 60
74ret add x s
75end
76
77note ot_tm_dg: n decimal digits at store offset p, or -1 when one is not a digit
78fn ot_tm_dg p ulong n ulong to long
79v = long 0
80for i ulong n bound 16777216
81c = get ot_der_m add p i
82if or lt c 48 gt c 57
83ret -1
84end
85v = add mul v 10 long sub c 48
86end
87ret v
88end
89
90note ot_tm_fld: MMDDHHMMSSZ at store offset o for year y, or ot_tm_bad. Year 0000 is refused
91note (no certificate needs it).
92fn ot_tm_fld y long o ulong to long
93if lt y 1
94ret ot_tm_bad
95end
96if ne get ot_der_m add o 10 90
97ret ot_tm_bad
98end
99mo = ot_tm_dg o 2
100d = ot_tm_dg add o 2 2
101h = ot_tm_dg add o 4 2
102mi = ot_tm_dg add o 6 2
103s = ot_tm_dg add o 8 2
104if or lt mo 1 gt mo 12
105ret ot_tm_bad
106end
107if or lt d 1 gt d ot_tm_mdays y mo
108ret ot_tm_bad
109end
110if or or lt h 0 gt h 23 or lt mi 0 gt mi 59
111ret ot_tm_bad
112end
113if or lt s 0 gt s 59
114ret ot_tm_bad
115end
116ret ot_tm_civil y mo d h mi s
117end
118
119note ot_tm_der: the time TLV at store offset p (wholly inside [p, e)) as Unix seconds, or ot_tm_bad.
120note Tag 0x17 UTCTime with exactly 13 bytes, tag 0x18 GeneralizedTime with exactly 15 bytes.
121note Which form a certificate must use for which year is the caller's rule (x509.om).
122fn ot_tm_der p ulong e ulong to long
123if eq ot_der_rd p e false
124ret ot_tm_bad
125end
126t = ot_der_tg
127v = ot_der_vo
128n = ot_der_vl
129if and eq t 23 eq n 13
130y = ot_tm_dg v 2
131if lt y 0
132ret ot_tm_bad
133end
134if lt y 50
135ret ot_tm_fld add y 2000 add v 2
136end
137ret ot_tm_fld add y 1900 add v 2
138end
139if and eq t 24 eq n 15
140ret ot_tm_fld ot_tm_dg v 4 add v 4
141end
142ret ot_tm_bad
143end