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