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 der.om (ot_der_): strict DER reader (ITU-T X.690 08/2015 section 10 on top of section 8). |
| 3 | note Omega never passes a global (error 19), not even a part of one, so every reader here works on |
| 4 | note offsets into the one byte store ot_der_m and names it directly. x509.om owns the store layout |
| 5 | note (certificate records, the roots region, the presented chain, the bundle text); test drivers may |
| 6 | note use it as scratch. 16 MiB is the store; nothing else in the X.509 layer holds certificate bytes. |
| 7 | note Public data only: these readers branch on the bytes they read (no secrets pass through them). |
| 8 | global ot_der_m uchar 16777216 |
| 9 | note ot_der_r: the last header read by ot_der_rd: tag, value offset, value length, end offset |
| 10 | global ot_der_r ulong 4 |
| 11 | const ot_der_cap ulong 16777216 |
| 12 | |
| 13 | note ot_der_rd: one TLV wholly inside [p, e). Refused (false): p >= e; the high tag number form |
| 14 | note (low five tag bits all ones, X.690 8.1.2.4: X.509 never needs it); the indefinite length 0x80 |
| 15 | note (10.1); more than 4 length bytes; a long form that is not minimal (10.1: first length byte 0, |
| 16 | note or a length below 128 in long form); a value that runs past e. |
| 17 | fn ot_der_rd p ulong e ulong to bool |
| 18 | if ge p e |
| 19 | ret false |
| 20 | end |
| 21 | t = get ot_der_m p |
| 22 | if eq and t 31 31 |
| 23 | ret false |
| 24 | end |
| 25 | q = add p 1 |
| 26 | if ge q e |
| 27 | ret false |
| 28 | end |
| 29 | b = get ot_der_m q |
| 30 | q = add q 1 |
| 31 | n = ulong 0 |
| 32 | if lt b 128 |
| 33 | n = ulong b |
| 34 | else |
| 35 | k = ulong and b 127 |
| 36 | if or eq k 0 gt k 4 |
| 37 | ret false |
| 38 | end |
| 39 | if gt k sub e q |
| 40 | ret false |
| 41 | end |
| 42 | if eq get ot_der_m q 0 |
| 43 | ret false |
| 44 | end |
| 45 | for i ulong k bound 16777216 |
| 46 | n = or shl n 8 ulong get ot_der_m add q i |
| 47 | end |
| 48 | q = add q k |
| 49 | if lt n 128 |
| 50 | ret false |
| 51 | end |
| 52 | end |
| 53 | if gt n sub e q |
| 54 | ret false |
| 55 | end |
| 56 | put ot_der_r 0 ulong t |
| 57 | put ot_der_r 1 q |
| 58 | put ot_der_r 2 n |
| 59 | put ot_der_r 3 add q n |
| 60 | ret true |
| 61 | end |
| 62 | |
| 63 | note ot_der_ex: ot_der_rd and the tag byte is exactly t |
| 64 | fn ot_der_ex p ulong e ulong t ulong to bool |
| 65 | if eq ot_der_rd p e false |
| 66 | ret false |
| 67 | end |
| 68 | ret eq get ot_der_r 0 t |
| 69 | end |
| 70 | |
| 71 | fn ot_der_tg to ulong |
| 72 | ret get ot_der_r 0 |
| 73 | end |
| 74 | fn ot_der_vo to ulong |
| 75 | ret get ot_der_r 1 |
| 76 | end |
| 77 | fn ot_der_vl to ulong |
| 78 | ret get ot_der_r 2 |
| 79 | end |
| 80 | fn ot_der_nd to ulong |
| 81 | ret get ot_der_r 3 |
| 82 | end |
| 83 | |
| 84 | note ot_der_okint: INTEGER contents at v (n bytes) are present and minimal (X.690 8.3.2): the first |
| 85 | note nine bits are neither all zeros nor all ones |
| 86 | fn ot_der_okint v ulong n ulong to bool |
| 87 | if eq n 0 |
| 88 | ret false |
| 89 | end |
| 90 | if eq n 1 |
| 91 | ret true |
| 92 | end |
| 93 | a = get ot_der_m v |
| 94 | b = get ot_der_m add v 1 |
| 95 | if and eq a 0 lt b 128 |
| 96 | ret false |
| 97 | end |
| 98 | if and eq a 255 ge b 128 |
| 99 | ret false |
| 100 | end |
| 101 | ret true |
| 102 | end |
| 103 | |
| 104 | note ot_der_okbool: BOOLEAN contents in DER are exactly one byte 0x00 or 0xFF (X.690 11.1) |
| 105 | fn ot_der_okbool v ulong n ulong to bool |
| 106 | if ne n 1 |
| 107 | ret false |
| 108 | end |
| 109 | a = get ot_der_m v |
| 110 | ret or eq a 0 eq a 255 |
| 111 | end |
| 112 | |
| 113 | note ot_der_eq: the n bytes at a and at b are equal |
| 114 | fn ot_der_eq a ulong b ulong n ulong to bool |
| 115 | for i ulong n bound 16777216 |
| 116 | if ne get ot_der_m add a i get ot_der_m add b i |
| 117 | ret false |
| 118 | end |
| 119 | end |
| 120 | ret true |
| 121 | end |
| 122 | |
| 123 | note ot_der_oid: the n content bytes at v are exactly the k-byte OID encoding packed big-endian into |
| 124 | note the low k bytes of the 128-bit value hi:lo (k <= 16). Omega texts cannot hold bytes above 127, |
| 125 | note so OID constants are written as two hex numbers at each call. |
| 126 | fn ot_der_oid v ulong n ulong k ulong hi ulong lo ulong to bool |
| 127 | if or ne n k gt k 16 |
| 128 | ret false |
| 129 | end |
| 130 | for i ulong k bound 16777216 |
| 131 | if ne get ot_der_m add v i ot_der_byt hi lo add i sub 16 k |
| 132 | ret false |
| 133 | end |
| 134 | end |
| 135 | ret true |
| 136 | end |
| 137 | |
| 138 | note ot_der_byt: byte j (0 = most significant) of the 128-bit value hi:lo |
| 139 | fn ot_der_byt hi ulong lo ulong j ulong to uchar |
| 140 | if lt j 8 |
| 141 | ret uchar and shr hi mul sub 7 j 8 255 |
| 142 | end |
| 143 | ret uchar and shr lo mul sub 15 j 8 255 |
| 144 | end |
| 145 | |
| 146 | note ot_der_okoid: OBJECT IDENTIFIER contents (X.690 8.19): non-empty, the last byte ends a |
| 147 | note subidentifier (top bit clear), and no subidentifier starts with the padding byte 0x80 |
| 148 | fn ot_der_okoid v ulong n ulong to bool |
| 149 | if eq n 0 |
| 150 | ret false |
| 151 | end |
| 152 | if ge get ot_der_m add v sub n 1 128 |
| 153 | ret false |
| 154 | end |
| 155 | st = true |
| 156 | for i ulong n bound 16777216 |
| 157 | b = get ot_der_m add v i |
| 158 | if and st eq b 128 |
| 159 | ret false |
| 160 | end |
| 161 | st = lt b 128 |
| 162 | end |
| 163 | ret true |
| 164 | end |
| 165 | |
| 166 | note ot_der_okbits: BIT STRING contents (X.690 8.6.2, 11.2): a first byte 0..7 giving the unused |
| 167 | note bits, 0 when there are no further bytes, and the unused bits of the last byte are zero (11.2.1). |
| 168 | fn ot_der_okbits v ulong n ulong to bool |
| 169 | if eq n 0 |
| 170 | ret false |
| 171 | end |
| 172 | u = get ot_der_m v |
| 173 | if gt u 7 |
| 174 | ret false |
| 175 | end |
| 176 | if eq n 1 |
| 177 | ret eq u 0 |
| 178 | end |
| 179 | if eq u 0 |
| 180 | ret true |
| 181 | end |
| 182 | z = get ot_der_m add v sub n 1 |
| 183 | ret eq and z sub shl uchar 1 u 1 0 |
| 184 | end |
| 185 | |
| 186 | note ot_der_small: a minimal non-negative INTEGER of at most 8 value bytes as a ulong, or |
| 187 | note 18446744073709551615 (never a valid small value here) when it is negative, too long or not minimal |
| 188 | fn ot_der_small v ulong n ulong to ulong |
| 189 | if eq ot_der_okint v n false |
| 190 | ret 18446744073709551615 |
| 191 | end |
| 192 | if ge get ot_der_m v 128 |
| 193 | ret 18446744073709551615 |
| 194 | end |
| 195 | m = n |
| 196 | o = v |
| 197 | if eq get ot_der_m v 0 |
| 198 | m = sub n 1 |
| 199 | o = add v 1 |
| 200 | end |
| 201 | if gt m 8 |
| 202 | ret 18446744073709551615 |
| 203 | end |
| 204 | x = ulong 0 |
| 205 | for i ulong m bound 16777216 |
| 206 | x = or shl x 8 ulong get ot_der_m add o i |
| 207 | end |
| 208 | ret x |
| 209 | end |