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 x509.om (ot_x5_): certificate chain validation for the TLS 1.3 client (RFC 5280 section 6.1 subset, |
| 3 | note RFC 6125 / RFC 9525 DNS-ID matching, RFC 8446 4.4.2). State lives in ot_x5_ globals; one |
| 4 | note validation at a time per process. |
| 5 | note Unit order: src/der.om and src/pkv.om must come earlier in the unit (their globals are used here). |
| 6 | note |
| 7 | note Rules (each is checked by tests/drv/x509.om; codes are the ot_x5_check results): |
| 8 | note - Strict DER for every field read (der.om): minimal lengths, no indefinite length, minimal INTEGERs, |
| 9 | note BOOLEAN 0x00/0xFF; a DEFAULT value present (version v1, critical FALSE, cA FALSE) is not DER and is |
| 10 | note refused. version is v3 when extensions are present and v2 or v3 when unique IDs are present; |
| 11 | note serialNumber positive, at most 20 octets (4.1.2.2); tbsCertificate.signature byte-equal to |
| 12 | note signatureAlgorithm (4.1.1.2); signatureValue and subjectPublicKey with no unused bits; nothing |
| 13 | note after the last field; no extension twice (4.2: the extension OIDs of a certificate are sorted and |
| 14 | note neighbours compared, O(S log S) for a certificate of S bytes, no count cap); keyUsage with |
| 15 | note DER-minimal unused bits (X.690 11.2.2: the lowest used bit of the last byte is set); extension |
| 16 | note lists, EKU, SAN and name-constraint subtree lists non-empty; Name is SEQUENCE OF non-empty SET OF { OID, one value }; validity |
| 17 | note times per 4.1.2.5: UTCTime through 2049, GeneralizedTime from 2050 (a GeneralizedTime before 2050 |
| 18 | note is refused), Z only, seconds present. An empty subject needs a critical subjectAltName (4.2.1.6). |
| 19 | note A presented certificate that breaks any of these is code 1. Two rules are relaxed for trust-store |
| 20 | note certificates only, because widely used roots break them and neither value is relied on: serial |
| 21 | note number 0 (Go Daddy and Starfield G2, Hellenic 2015, SECOM RootCA2) and GeneralizedTime before 2050 |
| 22 | note (Certum Trusted Network CA 2). A trust-store certificate that breaks another rule is not loaded |
| 23 | note (counted in ot_x5_st 5) and so cannot anchor a path. |
| 24 | note - Codes and precedence (deterministic, not dependent on search order): 9 no certificate presented; |
| 25 | note 1 any presented certificate fails to parse; 2 the leaf does not match the host; 3 the leaf is |
| 26 | note outside its validity; 4 no path: no chain of issuer links from the leaf to a trust-store |
| 27 | note certificate. Otherwise every path is scored by its first failing check in the order 3 (validity of |
| 28 | note every certificate on it, the trust anchor included), 5 (a signature that does not verify, or an |
| 29 | note algorithm that does not suit the issuer key), 6 (constraints below), 7 (an unrecognised critical |
| 30 | note extension on any certificate of the path, or a critical nameConstraints with a form other than a |
| 31 | note plain dNSName subtree), 8 (a signature algorithm other than those of pkv.om, e.g. SHA-1 or MD5, on |
| 32 | note any non-root certificate; a key of an unsupported type or curve, or one the primitive refuses, e.g. |
| 33 | note RSA under ot_rsa_minbits, on any certificate of the path); a path with none of these is 0. The |
| 34 | note result is 0 when some path scores 0, else the lowest score of all paths. |
| 35 | note - Path building: from the leaf, candidate issuers are the presented certificates (in the order |
| 36 | note received) then the trust store (in file order) whose subject Name is byte-equal to the issuer Name |
| 37 | note (RFC 5280 7.1 normalisation is not done) and, when the child has an authorityKeyIdentifier |
| 38 | note keyIdentifier and the candidate a subjectKeyIdentifier, with equal key identifiers. Depth-first |
| 39 | note with backtracking; a certificate is used at most once per path; a path ends at the first |
| 40 | note trust-store certificate reached. No fixed depth: the search ends because certificates are not |
| 41 | note reused; a budget of 1000000 candidate examinations bounds pathological cross-certified sets (the |
| 42 | note search then stops with what it has found). The trust anchor's own signature is not checked; its |
| 43 | note validity, basicConstraints, keyUsage, nameConstraints, critical extensions and key are. |
| 44 | note - Constraints (code 6): every issuer on the path, the trust anchor included, has basicConstraints |
| 45 | note with cA TRUE; its pathLenConstraint, when present, is at least the number of certificates between it |
| 46 | note and the leaf (self-issued ones counted too: stricter than 6.1.4 (l)); its keyUsage, when present, |
| 47 | note has keyCertSign; the leaf's keyUsage, when present, has digitalSignature (TLS 1.3 signs with it); |
| 48 | note extKeyUsage, when present on the leaf or an intermediate, includes serverAuth or |
| 49 | note anyExtendedKeyUsage; nameConstraints of every issuer: each dNSName SAN of the leaf, and the host |
| 50 | note when it is a DNS name, matches no excluded dNSName subtree and, when there are permitted dNSName |
| 51 | note subtrees, matches one of them; a wildcard SAN "*.D" is also refused when an excluded subtree lies |
| 52 | note strictly below D (the wildcard would cover a name the issuer may not certify). The host check is |
| 53 | note implied by the SAN checks (the host matched a SAN) and is kept as defence in depth. A |
| 54 | note subtree "example.com" matches that name and names ending ".example.com"; ".example.com" matches |
| 55 | note only the latter; an empty one matches all. Other forms are ignored when the extension is not |
| 56 | note critical (code 7 when it is). |
| 57 | note - Host name (RFC 6125 6.4, RFC 9525): one trailing dot is removed from the host first; subjectAltName |
| 58 | note dNSName entries only (no CN fallback: a leaf without a SAN never matches); ASCII case-insensitive; a |
| 59 | note wildcard only as the whole left-most label "*." followed by at least two labels without "*", and it |
| 60 | note matches exactly one non-empty host label; "*" alone, "*.com" and partial labels "f*.x.y" never |
| 61 | note match. A host of digits and dots is an IPv4 literal (four decimal parts 0..255, no leading zeros) |
| 62 | note and a host with ":" an IPv6 literal (hex groups, at most one "::", no embedded IPv4 or zone); they |
| 63 | note match iPAddress SANs byte-exactly. An empty host, one longer than 253 bytes, one with an empty |
| 64 | note label or a "*", or a malformed literal matches nothing. |
| 65 | note - Time: notBefore <= now <= notAfter (both ends inclusive, RFC 5280 4.1.2.5). |
| 66 | note - Storage: one 16 MiB store (ot_der_m) less the 64 KiB ot_pk_ scratch at its top holds the trust |
| 67 | note store and the presented chain (and, while ot_x5_roots runs, the bundle text at the top of that |
| 68 | note region). That is the only boundary: ot_x5_roots and ot_x5_add refuse with -3 when it is full. The |
| 69 | note record index has one entry per 64 bytes of store, more than the store can hold certificates |
| 70 | note (a certificate is longer than 64 bytes: its validity alone takes 32). |
| 71 | const ot_x5_cap ulong 16711680 |
| 72 | const ot_x5_mr ulong 261120 |
| 73 | const ot_x5_none ulong 18446744073709551615 |
| 74 | note record fields (32 per certificate): 0 start, 1 end, 2 tbs offset, 3 tbs end, 4 signatureAlgorithm |
| 75 | note offset, 5 its length, 6 signature offset, 7 signature length, 8 issuer offset, 9 its length, |
| 76 | note 10 subject offset, 11 its length, 14 SPKI offset, 15 its length, 16 SAN contents offset, 17 their |
| 77 | note length, 18 SKI offset, 19 SKI length, 20 AKI keyIdentifier offset, 21 its length, 22 flags, |
| 78 | note 23 pathLenConstraint (ot_x5_none: absent), 24 keyUsage bits (first byte << 8 | second), |
| 79 | note 25 nameConstraints contents offset, 26 their length |
| 80 | note flags: 1 basicConstraints, 2 cA, 4 keyUsage, 8 extKeyUsage, 16 EKU serverAuth or any, |
| 81 | note 32 unrecognised critical extension, 64 critical nameConstraints with an unsupported form, 128 SAN, |
| 82 | note 256 nameConstraints, 512 SAN critical |
| 83 | global ot_x5_rc ulong 8355840 |
| 84 | note validity of record r: 2r notBefore, 2r + 1 notAfter (Unix seconds) |
| 85 | global ot_x5_tm long 522240 |
| 86 | note ot_x5_st: 0 trust-store records, 1 store bytes used by them, 2 presented records, 3 presented |
| 87 | note certificates refused, 4 store write position, 5 bundle certificates not loaded |
| 88 | global ot_x5_st ulong 8 |
| 89 | global ot_x5_nw long 1 |
| 90 | note ot_x5_ps: 0 the number of extension OIDs listed in ot_x5_eo, 1 the version of the certificate |
| 91 | note being parsed, 2 1 while the trust store is parsed |
| 92 | global ot_x5_ps ulong 3 |
| 93 | note ot_x5_eo: the extension OIDs of the certificate being parsed, offset << 24 | length (both below |
| 94 | note 2^24: the store is 16 MiB); ot_x5_et the merge buffer. An extension takes at least 7 bytes, so |
| 95 | note 2400000 entries hold every extension a certificate in the store can have. |
| 96 | const ot_x5_me ulong 2400000 |
| 97 | global ot_x5_eo ulong 2400000 |
| 98 | global ot_x5_et ulong 2400000 |
| 99 | global ot_x5_pth ulong 261120 |
| 100 | global ot_x5_cur ulong 261120 |
| 101 | global ot_x5_on uchar 261120 |
| 102 | note the host: ot_x5_hb its bytes (lower case), ot_x5_hs 0 length, 1 kind (0 DNS, 1 IPv4, 2 IPv6), |
| 103 | note ot_x5_ia the address bytes |
| 104 | global ot_x5_hb uchar 256 |
| 105 | global ot_x5_hs ulong 2 |
| 106 | global ot_x5_ia uchar 16 |
| 107 | |
| 108 | fn ot_x5_g r ulong f ulong to ulong |
| 109 | ret get ot_x5_rc add mul r 32 f |
| 110 | end |
| 111 | fn ot_x5_s r ulong f ulong v ulong to void |
| 112 | put ot_x5_rc add mul r 32 f v |
| 113 | end |
| 114 | fn ot_x5_fl r ulong b ulong to void |
| 115 | ot_x5_s r 22 or ot_x5_g r 22 b |
| 116 | end |
| 117 | fn ot_x5_has r ulong b ulong to bool |
| 118 | ret ne and ot_x5_g r 22 b 0 |
| 119 | end |
| 120 | |
| 121 | note ---- parsing ---- |
| 122 | |
| 123 | note ot_x5_parse: the certificate exactly [s, e) into record r; false when it breaks a rule |
| 124 | fn ot_x5_parse s ulong e ulong r ulong to bool |
| 125 | for f ulong 32 |
| 126 | ot_x5_s r f 0 |
| 127 | end |
| 128 | ot_x5_s r 23 ot_x5_none |
| 129 | ot_x5_s r 0 s |
| 130 | ot_x5_s r 1 e |
| 131 | if eq ot_der_ex s e 48 false |
| 132 | ret false |
| 133 | end |
| 134 | if ne ot_der_nd e |
| 135 | ret false |
| 136 | end |
| 137 | v = ot_der_vo |
| 138 | if eq ot_der_ex v e 48 false |
| 139 | ret false |
| 140 | end |
| 141 | ot_x5_s r 2 v |
| 142 | a = ot_der_nd |
| 143 | ot_x5_s r 3 a |
| 144 | if eq ot_der_ex a e 48 false |
| 145 | ret false |
| 146 | end |
| 147 | b = ot_der_nd |
| 148 | ot_x5_s r 4 a |
| 149 | ot_x5_s r 5 sub b a |
| 150 | if eq ot_der_ex b e 3 false |
| 151 | ret false |
| 152 | end |
| 153 | if ne ot_der_nd e |
| 154 | ret false |
| 155 | end |
| 156 | if eq ot_der_okbits ot_der_vo ot_der_vl false |
| 157 | ret false |
| 158 | end |
| 159 | if ne get ot_der_m ot_der_vo 0 |
| 160 | ret false |
| 161 | end |
| 162 | ot_x5_s r 6 add ot_der_vo 1 |
| 163 | ot_x5_s r 7 sub ot_der_vl 1 |
| 164 | if eq ot_pk_sigalg a b false |
| 165 | ret false |
| 166 | end |
| 167 | ret ot_x5_tbs r |
| 168 | end |
| 169 | |
| 170 | note ot_x5_ver: the optional [0] EXPLICIT version at i; sets ot_x5_ps 1 to 0 (v1), 1 (v2) or 2 (v3); |
| 171 | note the offset after it, or 0 when malformed |
| 172 | fn ot_x5_ver i ulong e ulong to ulong |
| 173 | put ot_x5_ps 1 0 |
| 174 | if ge i e |
| 175 | ret 0 |
| 176 | end |
| 177 | if ne get ot_der_m i 160 |
| 178 | ret i |
| 179 | end |
| 180 | if eq ot_der_ex i e 160 false |
| 181 | ret 0 |
| 182 | end |
| 183 | j = ot_der_nd |
| 184 | if eq ot_der_ex ot_der_vo j 2 false |
| 185 | ret 0 |
| 186 | end |
| 187 | if ne ot_der_nd j |
| 188 | ret 0 |
| 189 | end |
| 190 | v = ot_der_small ot_der_vo ot_der_vl |
| 191 | if or lt v 1 gt v 2 |
| 192 | ret 0 |
| 193 | end |
| 194 | put ot_x5_ps 1 v |
| 195 | ret j |
| 196 | end |
| 197 | |
| 198 | note ot_x5_serial: a positive INTEGER of at most 20 content octets just read |
| 199 | fn ot_x5_serial to bool |
| 200 | o = ot_der_vo |
| 201 | n = ot_der_vl |
| 202 | if or eq ot_der_okint o n false gt n 20 |
| 203 | ret false |
| 204 | end |
| 205 | if ge get ot_der_m o 128 |
| 206 | ret false |
| 207 | end |
| 208 | if and eq n 1 eq get ot_der_m o 0 |
| 209 | ret ne get ot_x5_ps 2 0 |
| 210 | end |
| 211 | ret true |
| 212 | end |
| 213 | |
| 214 | note ot_x5_tbs: tbsCertificate of record r (RFC 5280 4.1) |
| 215 | fn ot_x5_tbs r ulong to bool |
| 216 | e = ot_x5_g r 3 |
| 217 | if eq ot_der_ex ot_x5_g r 2 e 48 false |
| 218 | ret false |
| 219 | end |
| 220 | i = ot_x5_ver ot_der_vo e |
| 221 | if eq i 0 |
| 222 | ret false |
| 223 | end |
| 224 | if eq ot_der_ex i e 2 false |
| 225 | ret false |
| 226 | end |
| 227 | if eq ot_x5_serial false |
| 228 | ret false |
| 229 | end |
| 230 | i = ot_der_nd |
| 231 | if eq ot_der_ex i e 48 false |
| 232 | ret false |
| 233 | end |
| 234 | n = sub ot_der_nd i |
| 235 | if ne n ot_x5_g r 5 |
| 236 | ret false |
| 237 | end |
| 238 | if eq ot_der_eq i ot_x5_g r 4 n false |
| 239 | ret false |
| 240 | end |
| 241 | i = add i n |
| 242 | j = ot_x5_name i e |
| 243 | if eq j 0 |
| 244 | ret false |
| 245 | end |
| 246 | ot_x5_s r 8 i |
| 247 | ot_x5_s r 9 sub j i |
| 248 | i = ot_x5_valid j e r |
| 249 | if eq i 0 |
| 250 | ret false |
| 251 | end |
| 252 | j = ot_x5_name i e |
| 253 | if eq j 0 |
| 254 | ret false |
| 255 | end |
| 256 | ot_x5_s r 10 i |
| 257 | ot_x5_s r 11 sub j i |
| 258 | ret ot_x5_tbs2 j e r |
| 259 | end |
| 260 | |
| 261 | note ot_x5_tbs2: subjectPublicKeyInfo, unique IDs and extensions, from i to the tbs end e |
| 262 | fn ot_x5_tbs2 i ulong e ulong r ulong to bool |
| 263 | if eq ot_der_ex i e 48 false |
| 264 | ret false |
| 265 | end |
| 266 | j = ot_der_nd |
| 267 | if eq ot_pk_spki i j false |
| 268 | ret false |
| 269 | end |
| 270 | ot_x5_s r 14 i |
| 271 | ot_x5_s r 15 sub j i |
| 272 | v = get ot_x5_ps 1 |
| 273 | j = ot_x5_uid j e 129 v |
| 274 | j = ot_x5_uid j e 130 v |
| 275 | if eq j 0 |
| 276 | ret false |
| 277 | end |
| 278 | if lt j e |
| 279 | if ne v 2 |
| 280 | ret false |
| 281 | end |
| 282 | if eq ot_der_ex j e 163 false |
| 283 | ret false |
| 284 | end |
| 285 | k = ot_der_nd |
| 286 | if eq ot_x5_exts ot_der_vo k r false |
| 287 | ret false |
| 288 | end |
| 289 | j = k |
| 290 | end |
| 291 | if ne j e |
| 292 | ret false |
| 293 | end |
| 294 | if eq ot_x5_g r 11 2 |
| 295 | if eq ot_x5_has r 512 false |
| 296 | ret false |
| 297 | end |
| 298 | end |
| 299 | ret true |
| 300 | end |
| 301 | |
| 302 | note ot_x5_uid: the optional unique ID with tag t at i (version v2 or v3 only); next offset, 0 malformed |
| 303 | fn ot_x5_uid i ulong e ulong t ulong v ulong to ulong |
| 304 | if or eq i 0 ge i e |
| 305 | ret i |
| 306 | end |
| 307 | if ne ulong get ot_der_m i t |
| 308 | ret i |
| 309 | end |
| 310 | if eq v 0 |
| 311 | ret 0 |
| 312 | end |
| 313 | if eq ot_der_ex i e t false |
| 314 | ret 0 |
| 315 | end |
| 316 | if eq ot_der_okbits ot_der_vo ot_der_vl false |
| 317 | ret 0 |
| 318 | end |
| 319 | ret ot_der_nd |
| 320 | end |
| 321 | |
| 322 | note ot_x5_name: a Name at i inside [i, e); its end, or 0 when malformed |
| 323 | fn ot_x5_name i ulong e ulong to ulong |
| 324 | if eq ot_der_ex i e 48 false |
| 325 | ret 0 |
| 326 | end |
| 327 | j = ot_der_nd |
| 328 | k = ot_der_vo |
| 329 | while lt k j bound 16777216 |
| 330 | if eq ot_der_ex k j 49 false |
| 331 | ret 0 |
| 332 | end |
| 333 | m = ot_der_nd |
| 334 | if eq ot_der_vl 0 |
| 335 | ret 0 |
| 336 | end |
| 337 | if eq ot_x5_rdn ot_der_vo m false |
| 338 | ret 0 |
| 339 | end |
| 340 | k = m |
| 341 | end |
| 342 | ret j |
| 343 | end |
| 344 | |
| 345 | note ot_x5_rdn: SET OF AttributeTypeAndValue contents [a, m) |
| 346 | fn ot_x5_rdn a ulong m ulong to bool |
| 347 | k = a |
| 348 | while lt k m bound 16777216 |
| 349 | if eq ot_der_ex k m 48 false |
| 350 | ret false |
| 351 | end |
| 352 | b = ot_der_nd |
| 353 | if eq ot_der_ex ot_der_vo b 6 false |
| 354 | ret false |
| 355 | end |
| 356 | if eq ot_der_okoid ot_der_vo ot_der_vl false |
| 357 | ret false |
| 358 | end |
| 359 | if eq ot_der_rd ot_der_nd b false |
| 360 | ret false |
| 361 | end |
| 362 | if ne ot_der_nd b |
| 363 | ret false |
| 364 | end |
| 365 | k = b |
| 366 | end |
| 367 | ret true |
| 368 | end |
| 369 | |
| 370 | note ot_x5_time: the time TLV exactly [a, b): seconds or ot_tm_bad (GeneralizedTime before 2050 refused) |
| 371 | fn ot_x5_time a ulong b ulong to long |
| 372 | v = ot_tm_der a b |
| 373 | if eq v ot_tm_bad |
| 374 | ret v |
| 375 | end |
| 376 | if ne ot_der_nd b |
| 377 | ret ot_tm_bad |
| 378 | end |
| 379 | if and eq ot_der_tg 24 lt v 2524608000 |
| 380 | if eq get ot_x5_ps 2 0 |
| 381 | ret ot_tm_bad |
| 382 | end |
| 383 | end |
| 384 | ret v |
| 385 | end |
| 386 | |
| 387 | note ot_x5_valid: Validity at i; its end, or 0 |
| 388 | fn ot_x5_valid i ulong e ulong r ulong to ulong |
| 389 | if eq ot_der_ex i e 48 false |
| 390 | ret 0 |
| 391 | end |
| 392 | j = ot_der_nd |
| 393 | a = ot_der_vo |
| 394 | if eq ot_der_rd a j false |
| 395 | ret 0 |
| 396 | end |
| 397 | b = ot_der_nd |
| 398 | t = ot_x5_time a b |
| 399 | if eq t ot_tm_bad |
| 400 | ret 0 |
| 401 | end |
| 402 | put ot_x5_tm mul r 2 t |
| 403 | if eq ot_der_rd b j false |
| 404 | ret 0 |
| 405 | end |
| 406 | t = ot_x5_time b ot_der_nd |
| 407 | if eq t ot_tm_bad |
| 408 | ret 0 |
| 409 | end |
| 410 | if ne ot_der_nd j |
| 411 | ret 0 |
| 412 | end |
| 413 | put ot_x5_tm add mul r 2 1 t |
| 414 | ret j |
| 415 | end |
| 416 | |
| 417 | note ---- extensions ---- |
| 418 | |
| 419 | note ot_x5_exts: Extensions SEQUENCE exactly [p, e) |
| 420 | fn ot_x5_exts p ulong e ulong r ulong to bool |
| 421 | if eq ot_der_ex p e 48 false |
| 422 | ret false |
| 423 | end |
| 424 | if or ne ot_der_nd e eq ot_der_vl 0 |
| 425 | ret false |
| 426 | end |
| 427 | k = ot_der_vo |
| 428 | put ot_x5_ps 0 0 |
| 429 | while lt k e bound 16777216 |
| 430 | if eq ot_der_ex k e 48 false |
| 431 | ret false |
| 432 | end |
| 433 | m = ot_der_nd |
| 434 | if eq ot_x5_ext ot_der_vo m r false |
| 435 | ret false |
| 436 | end |
| 437 | k = m |
| 438 | end |
| 439 | ret ot_x5_nodup |
| 440 | end |
| 441 | |
| 442 | note ot_x5_eadd: list the extension OID [o, o + l) for the duplicate check |
| 443 | fn ot_x5_eadd o ulong l ulong to bool |
| 444 | n = get ot_x5_ps 0 |
| 445 | if ge n ot_x5_me |
| 446 | ret false |
| 447 | end |
| 448 | put ot_x5_eo n or shl o 24 l |
| 449 | put ot_x5_ps 0 add n 1 |
| 450 | ret true |
| 451 | end |
| 452 | |
| 453 | note ot_x5_ocmp: order of two listed OIDs: 0 a first, 1 equal, 2 b first (length, then bytes) |
| 454 | fn ot_x5_ocmp a ulong b ulong to ulong |
| 455 | la = and a 16777215 |
| 456 | lb = and b 16777215 |
| 457 | if lt la lb |
| 458 | ret 0 |
| 459 | end |
| 460 | if gt la lb |
| 461 | ret 2 |
| 462 | end |
| 463 | oa = shr a 24 |
| 464 | ob = shr b 24 |
| 465 | for i ulong la bound 16777216 |
| 466 | x = get ot_der_m add oa i |
| 467 | y = get ot_der_m add ob i |
| 468 | if lt x y |
| 469 | ret 0 |
| 470 | end |
| 471 | if gt x y |
| 472 | ret 2 |
| 473 | end |
| 474 | end |
| 475 | ret 1 |
| 476 | end |
| 477 | |
| 478 | note ot_x5_mrg: merge the sorted runs ot_x5_eo [lo, md) and [md, hi) into ot_x5_et [lo, hi) |
| 479 | fn ot_x5_mrg lo ulong md ulong hi ulong to void |
| 480 | i = lo |
| 481 | j = md |
| 482 | k = lo |
| 483 | while lt k hi bound 16777216 |
| 484 | tk = false |
| 485 | if lt i md |
| 486 | if ge j hi |
| 487 | tk = true |
| 488 | else |
| 489 | if ne ot_x5_ocmp get ot_x5_eo i get ot_x5_eo j 2 |
| 490 | tk = true |
| 491 | end |
| 492 | end |
| 493 | end |
| 494 | if tk |
| 495 | put ot_x5_et k get ot_x5_eo i |
| 496 | i = add i 1 |
| 497 | else |
| 498 | put ot_x5_et k get ot_x5_eo j |
| 499 | j = add j 1 |
| 500 | end |
| 501 | k = add k 1 |
| 502 | end |
| 503 | end |
| 504 | |
| 505 | note ot_x5_pass: one bottom-up merge pass with runs of w over n entries, then back into ot_x5_eo |
| 506 | fn ot_x5_pass w ulong n ulong to void |
| 507 | lo = ulong 0 |
| 508 | while lt lo n bound 16777216 |
| 509 | md = add lo w |
| 510 | if gt md n |
| 511 | md = n |
| 512 | end |
| 513 | hi = add md w |
| 514 | if gt hi n |
| 515 | hi = n |
| 516 | end |
| 517 | ot_x5_mrg lo md hi |
| 518 | lo = hi |
| 519 | end |
| 520 | for i ulong n bound 16777216 |
| 521 | put ot_x5_eo i get ot_x5_et i |
| 522 | end |
| 523 | end |
| 524 | |
| 525 | note ot_x5_nodup: sort the listed extension OIDs (bottom-up merge sort) and refuse two equal ones |
| 526 | fn ot_x5_nodup to bool |
| 527 | n = get ot_x5_ps 0 |
| 528 | w = ulong 1 |
| 529 | while lt w n bound 64 |
| 530 | ot_x5_pass w n |
| 531 | w = mul w 2 |
| 532 | end |
| 533 | for i ulong n bound 16777216 |
| 534 | if gt i 0 |
| 535 | if eq ot_x5_ocmp get ot_x5_eo sub i 1 get ot_x5_eo i 1 |
| 536 | ret false |
| 537 | end |
| 538 | end |
| 539 | end |
| 540 | ret true |
| 541 | end |
| 542 | |
| 543 | note ot_x5_ext: one Extension's contents [v, m) |
| 544 | fn ot_x5_ext v ulong m ulong r ulong to bool |
| 545 | if eq ot_der_ex v m 6 false |
| 546 | ret false |
| 547 | end |
| 548 | o = ot_der_vo |
| 549 | l = ot_der_vl |
| 550 | i = ot_der_nd |
| 551 | if eq ot_der_okoid o l false |
| 552 | ret false |
| 553 | end |
| 554 | if eq ot_x5_eadd o l false |
| 555 | ret false |
| 556 | end |
| 557 | c = false |
| 558 | if lt i m |
| 559 | if eq get ot_der_m i 1 |
| 560 | if eq ot_der_ex i m 1 false |
| 561 | ret false |
| 562 | end |
| 563 | if or ne ot_der_vl 1 ne get ot_der_m ot_der_vo 255 |
| 564 | ret false |
| 565 | end |
| 566 | c = true |
| 567 | i = ot_der_nd |
| 568 | end |
| 569 | end |
| 570 | if eq ot_der_ex i m 4 false |
| 571 | ret false |
| 572 | end |
| 573 | if ne ot_der_nd m |
| 574 | ret false |
| 575 | end |
| 576 | ret ot_x5_extv o l ot_der_vo r c |
| 577 | end |
| 578 | |
| 579 | note ot_x5_extv: extnValue contents from v (to the end the last header read gave) by OID [o, o+l) |
| 580 | fn ot_x5_extv o ulong l ulong v ulong r ulong c bool to bool |
| 581 | e = ot_der_nd |
| 582 | if ot_der_oid o l 3 0 0x551d13 |
| 583 | ret ot_x5_bc v e r |
| 584 | end |
| 585 | if ot_der_oid o l 3 0 0x551d0f |
| 586 | ret ot_x5_ku v e r |
| 587 | end |
| 588 | if ot_der_oid o l 3 0 0x551d25 |
| 589 | ret ot_x5_eku v e r |
| 590 | end |
| 591 | if ot_der_oid o l 3 0 0x551d11 |
| 592 | if c |
| 593 | ot_x5_fl r 512 |
| 594 | end |
| 595 | ret ot_x5_san v e r |
| 596 | end |
| 597 | if ot_der_oid o l 3 0 0x551d1e |
| 598 | ret ot_x5_nc v e r c |
| 599 | end |
| 600 | if ot_der_oid o l 3 0 0x551d0e |
| 601 | ret ot_x5_ski v e r |
| 602 | end |
| 603 | if ot_der_oid o l 3 0 0x551d23 |
| 604 | ret ot_x5_aki v e r |
| 605 | end |
| 606 | if c |
| 607 | ot_x5_fl r 32 |
| 608 | end |
| 609 | ret true |
| 610 | end |
| 611 | |
| 612 | note ot_x5_bc: BasicConstraints ::= SEQUENCE { cA BOOLEAN DEFAULT FALSE, pathLenConstraint INTEGER (0..MAX) OPTIONAL } |
| 613 | fn ot_x5_bc v ulong e ulong r ulong to bool |
| 614 | if eq ot_der_ex v e 48 false |
| 615 | ret false |
| 616 | end |
| 617 | if ne ot_der_nd e |
| 618 | ret false |
| 619 | end |
| 620 | i = ot_der_vo |
| 621 | ot_x5_fl r 1 |
| 622 | if lt i e |
| 623 | if eq get ot_der_m i 1 |
| 624 | if eq ot_der_ex i e 1 false |
| 625 | ret false |
| 626 | end |
| 627 | if or ne ot_der_vl 1 ne get ot_der_m ot_der_vo 255 |
| 628 | ret false |
| 629 | end |
| 630 | ot_x5_fl r 2 |
| 631 | i = ot_der_nd |
| 632 | end |
| 633 | end |
| 634 | if lt i e |
| 635 | if eq ot_der_ex i e 2 false |
| 636 | ret false |
| 637 | end |
| 638 | n = ot_der_small ot_der_vo ot_der_vl |
| 639 | if eq n ot_x5_none |
| 640 | ret false |
| 641 | end |
| 642 | ot_x5_s r 23 n |
| 643 | i = ot_der_nd |
| 644 | end |
| 645 | ret eq i e |
| 646 | end |
| 647 | |
| 648 | note ot_x5_ku: KeyUsage BIT STRING (DER, X.690 11.2.2: trailing zero bits removed, so the lowest used |
| 649 | note bit of the last byte is set and at least one bit is; only decipherOnly may be set in the second byte) |
| 650 | fn ot_x5_ku v ulong e ulong r ulong to bool |
| 651 | if eq ot_der_ex v e 3 false |
| 652 | ret false |
| 653 | end |
| 654 | if ne ot_der_nd e |
| 655 | ret false |
| 656 | end |
| 657 | o = ot_der_vo |
| 658 | n = ot_der_vl |
| 659 | if or lt n 2 gt n 3 |
| 660 | ret false |
| 661 | end |
| 662 | if eq ot_der_okbits o n false |
| 663 | ret false |
| 664 | end |
| 665 | u = get ot_der_m o |
| 666 | if eq and shr get ot_der_m add o sub n 1 u 1 0 |
| 667 | ret false |
| 668 | end |
| 669 | x = shl ulong get ot_der_m add o 1 8 |
| 670 | if eq n 3 |
| 671 | if ne and get ot_der_m add o 2 127 0 |
| 672 | ret false |
| 673 | end |
| 674 | x = or x ulong get ot_der_m add o 2 |
| 675 | end |
| 676 | ot_x5_fl r 4 |
| 677 | ot_x5_s r 24 x |
| 678 | ret true |
| 679 | end |
| 680 | |
| 681 | note ot_x5_eku: ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId |
| 682 | fn ot_x5_eku v ulong e ulong r ulong to bool |
| 683 | if eq ot_der_ex v e 48 false |
| 684 | ret false |
| 685 | end |
| 686 | if or ne ot_der_nd e eq ot_der_vl 0 |
| 687 | ret false |
| 688 | end |
| 689 | k = ot_der_vo |
| 690 | ot_x5_fl r 8 |
| 691 | while lt k e bound 16777216 |
| 692 | if eq ot_der_ex k e 6 false |
| 693 | ret false |
| 694 | end |
| 695 | o = ot_der_vo |
| 696 | l = ot_der_vl |
| 697 | k = ot_der_nd |
| 698 | if eq ot_der_okoid o l false |
| 699 | ret false |
| 700 | end |
| 701 | if or ot_der_oid o l 8 0 0x2b06010505070301 ot_der_oid o l 4 0 0x551d2500 |
| 702 | ot_x5_fl r 16 |
| 703 | end |
| 704 | end |
| 705 | ret true |
| 706 | end |
| 707 | |
| 708 | note ot_x5_gn: one GeneralName at k inside [k, e); nc: inside nameConstraints (iPAddress is then an |
| 709 | note address and a mask, 8 or 32 bytes) |
| 710 | fn ot_x5_gn k ulong e ulong nc bool to bool |
| 711 | if eq ot_der_rd k e false |
| 712 | ret false |
| 713 | end |
| 714 | t = ot_der_tg |
| 715 | if eq t 130 |
| 716 | ret ot_x5_ia5 ot_der_vo ot_der_vl |
| 717 | end |
| 718 | if eq t 135 |
| 719 | n = ot_der_vl |
| 720 | if nc |
| 721 | ret or eq n 8 eq n 32 |
| 722 | end |
| 723 | ret or eq n 4 eq n 16 |
| 724 | end |
| 725 | if or or eq t 160 eq t 129 or eq t 163 eq t 164 |
| 726 | ret true |
| 727 | end |
| 728 | ret or or eq t 165 eq t 134 eq t 136 |
| 729 | end |
| 730 | |
| 731 | fn ot_x5_ia5 o ulong n ulong to bool |
| 732 | for i ulong n bound 16777216 |
| 733 | if ge get ot_der_m add o i 128 |
| 734 | ret false |
| 735 | end |
| 736 | end |
| 737 | ret true |
| 738 | end |
| 739 | |
| 740 | note ot_x5_san: GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName |
| 741 | fn ot_x5_san v ulong e ulong r ulong to bool |
| 742 | if eq ot_der_ex v e 48 false |
| 743 | ret false |
| 744 | end |
| 745 | if or ne ot_der_nd e eq ot_der_vl 0 |
| 746 | ret false |
| 747 | end |
| 748 | a = ot_der_vo |
| 749 | k = a |
| 750 | while lt k e bound 16777216 |
| 751 | if eq ot_x5_gn k e false false |
| 752 | ret false |
| 753 | end |
| 754 | k = ot_der_nd |
| 755 | end |
| 756 | ot_x5_s r 16 a |
| 757 | ot_x5_s r 17 sub e a |
| 758 | ot_x5_fl r 128 |
| 759 | ret true |
| 760 | end |
| 761 | |
| 762 | note ot_x5_ski: SubjectKeyIdentifier ::= OCTET STRING (non-empty) |
| 763 | fn ot_x5_ski v ulong e ulong r ulong to bool |
| 764 | if eq ot_der_ex v e 4 false |
| 765 | ret false |
| 766 | end |
| 767 | if or ne ot_der_nd e eq ot_der_vl 0 |
| 768 | ret false |
| 769 | end |
| 770 | ot_x5_s r 18 ot_der_vo |
| 771 | ot_x5_s r 19 ot_der_vl |
| 772 | ret true |
| 773 | end |
| 774 | |
| 775 | note ot_x5_aki: AuthorityKeyIdentifier ::= SEQUENCE { [0] keyIdentifier, [1] authorityCertIssuer, |
| 776 | note [2] authorityCertSerialNumber } (all optional, in order) |
| 777 | fn ot_x5_aki v ulong e ulong r ulong to bool |
| 778 | if eq ot_der_ex v e 48 false |
| 779 | ret false |
| 780 | end |
| 781 | if ne ot_der_nd e |
| 782 | ret false |
| 783 | end |
| 784 | i = ot_der_vo |
| 785 | if lt i e |
| 786 | if eq get ot_der_m i 128 |
| 787 | if eq ot_der_ex i e 128 false |
| 788 | ret false |
| 789 | end |
| 790 | if eq ot_der_vl 0 |
| 791 | ret false |
| 792 | end |
| 793 | ot_x5_s r 20 ot_der_vo |
| 794 | ot_x5_s r 21 ot_der_vl |
| 795 | i = ot_der_nd |
| 796 | end |
| 797 | end |
| 798 | if lt i e |
| 799 | if eq get ot_der_m i 161 |
| 800 | if eq ot_der_ex i e 161 false |
| 801 | ret false |
| 802 | end |
| 803 | i = ot_der_nd |
| 804 | end |
| 805 | end |
| 806 | if lt i e |
| 807 | if eq get ot_der_m i 130 |
| 808 | if eq ot_der_ex i e 130 false |
| 809 | ret false |
| 810 | end |
| 811 | i = ot_der_nd |
| 812 | end |
| 813 | end |
| 814 | ret eq i e |
| 815 | end |
| 816 | |
| 817 | note ot_x5_nc: NameConstraints ::= SEQUENCE { permittedSubtrees [0], excludedSubtrees [1] } (not both absent) |
| 818 | fn ot_x5_nc v ulong e ulong r ulong c bool to bool |
| 819 | if eq ot_der_ex v e 48 false |
| 820 | ret false |
| 821 | end |
| 822 | if or ne ot_der_nd e eq ot_der_vl 0 |
| 823 | ret false |
| 824 | end |
| 825 | i = ot_der_vo |
| 826 | ot_x5_s r 25 i |
| 827 | ot_x5_s r 26 sub e i |
| 828 | ot_x5_fl r 256 |
| 829 | if lt i e |
| 830 | if eq get ot_der_m i 160 |
| 831 | if eq ot_der_ex i e 160 false |
| 832 | ret false |
| 833 | end |
| 834 | j = ot_der_nd |
| 835 | if eq ot_x5_gst ot_der_vo j r c false |
| 836 | ret false |
| 837 | end |
| 838 | i = j |
| 839 | end |
| 840 | end |
| 841 | if lt i e |
| 842 | if eq ot_der_ex i e 161 false |
| 843 | ret false |
| 844 | end |
| 845 | j = ot_der_nd |
| 846 | if eq ot_x5_gst ot_der_vo j r c false |
| 847 | ret false |
| 848 | end |
| 849 | i = j |
| 850 | end |
| 851 | ret eq i e |
| 852 | end |
| 853 | |
| 854 | note ot_x5_gst: GeneralSubtrees contents [a, j): SIZE (1..MAX) OF SEQUENCE { base GeneralName, |
| 855 | note minimum [0] DEFAULT 0, maximum [1] OPTIONAL }. A subtree other than a dNSName base alone is an |
| 856 | note unsupported form (flag 64 when the extension is critical). |
| 857 | fn ot_x5_gst a ulong j ulong r ulong c bool to bool |
| 858 | if ge a j |
| 859 | ret false |
| 860 | end |
| 861 | k = a |
| 862 | while lt k j bound 16777216 |
| 863 | if eq ot_der_ex k j 48 false |
| 864 | ret false |
| 865 | end |
| 866 | m = ot_der_nd |
| 867 | if eq ot_x5_gn ot_der_vo m true false |
| 868 | ret false |
| 869 | end |
| 870 | if or ne ot_der_tg 130 ne ot_der_nd m |
| 871 | if c |
| 872 | ot_x5_fl r 64 |
| 873 | end |
| 874 | if ne ot_der_nd m |
| 875 | if eq ot_x5_mm ot_der_nd m false |
| 876 | ret false |
| 877 | end |
| 878 | end |
| 879 | end |
| 880 | k = m |
| 881 | end |
| 882 | ret true |
| 883 | end |
| 884 | |
| 885 | note ot_x5_mm: the minimum [0] and maximum [1] of a GeneralSubtree, exactly [i, m) |
| 886 | fn ot_x5_mm i ulong m ulong to bool |
| 887 | k = i |
| 888 | if lt k m |
| 889 | if eq get ot_der_m k 128 |
| 890 | if eq ot_der_ex k m 128 false |
| 891 | ret false |
| 892 | end |
| 893 | k = ot_der_nd |
| 894 | end |
| 895 | end |
| 896 | if lt k m |
| 897 | if eq ot_der_ex k m 129 false |
| 898 | ret false |
| 899 | end |
| 900 | k = ot_der_nd |
| 901 | end |
| 902 | ret eq k m |
| 903 | end |
| 904 | |
| 905 | note ---- trust store and presented chain ---- |
| 906 | |
| 907 | fn ot_x5_clear to void |
| 908 | for i ulong 8 |
| 909 | put ot_x5_st i 0 |
| 910 | end |
| 911 | end |
| 912 | |
| 913 | note ot_x5_roots: load a PEM bundle as the trust store (replacing any earlier one and the presented |
| 914 | note chain). The number of certificates loaded, or: -1 the file cannot be opened or read, -2 a malformed |
| 915 | note PEM block, -3 the file or its certificates do not fit the store (the one storage boundary). A |
| 916 | note well-formed block whose certificate breaks a parsing rule is not loaded (ot_x5_st 5 counts them). |
| 917 | export fn ot_x5_roots path view uchar * to long |
| 918 | sb = new uchar 128 0 |
| 919 | ot_x5_clear |
| 920 | n = len path |
| 921 | if or eq n 0 gt n 4096 |
| 922 | ret -1 |
| 923 | end |
| 924 | fd = fopen path 0 n 0 0 |
| 925 | if lt fd 0 |
| 926 | ret -1 |
| 927 | end |
| 928 | st = fstat int fd sb 0 128 |
| 929 | if lt st 0 |
| 930 | c = fclose int fd |
| 931 | ret -1 |
| 932 | end |
| 933 | z = ulong 0 |
| 934 | for i ulong 8 |
| 935 | z = or z shl ulong get sb add 48 i mul i 8 |
| 936 | end |
| 937 | if gt z ot_x5_cap |
| 938 | c = fclose int fd |
| 939 | ret -3 |
| 940 | end |
| 941 | b = sub ot_x5_cap z |
| 942 | t = ulong 0 |
| 943 | while lt t z bound 16777216 |
| 944 | k = read int fd ot_der_m add b t sub z t |
| 945 | if lt k 0 |
| 946 | c = fclose int fd |
| 947 | ret -1 |
| 948 | end |
| 949 | if eq k 0 |
| 950 | break |
| 951 | end |
| 952 | t = add t ulong k |
| 953 | end |
| 954 | c = fclose int fd |
| 955 | ret ot_x5_pemld b add b t |
| 956 | end |
| 957 | |
| 958 | note ot_x5_pemld: decode every CERTIFICATE block of store [i, e) to the store start and index them. |
| 959 | note The write position never passes the read position (four characters give at most three bytes). |
| 960 | fn ot_x5_pemld i ulong e ulong to long |
| 961 | w = ulong 0 |
| 962 | p = i |
| 963 | put ot_x5_ps 2 1 |
| 964 | while true bound 16777216 |
| 965 | r = ot_pem_next p e |
| 966 | if lt r 0 |
| 967 | ot_x5_clear |
| 968 | put ot_x5_ps 2 0 |
| 969 | ret -2 |
| 970 | end |
| 971 | if eq r 0 |
| 972 | break |
| 973 | end |
| 974 | p = get ot_pem_r 2 |
| 975 | k = ot_b64_dec get ot_pem_r 0 get ot_pem_r 1 w |
| 976 | if lt k 0 |
| 977 | ot_x5_clear |
| 978 | put ot_x5_ps 2 0 |
| 979 | ret -2 |
| 980 | end |
| 981 | n = get ot_x5_st 0 |
| 982 | if ge n ot_x5_mr |
| 983 | ot_x5_clear |
| 984 | put ot_x5_ps 2 0 |
| 985 | ret -3 |
| 986 | end |
| 987 | if ot_x5_parse w add w ulong k n |
| 988 | put ot_x5_st 0 add n 1 |
| 989 | w = add w ulong k |
| 990 | else |
| 991 | put ot_x5_st 5 add get ot_x5_st 5 1 |
| 992 | end |
| 993 | end |
| 994 | put ot_x5_ps 2 0 |
| 995 | put ot_x5_st 1 w |
| 996 | put ot_x5_st 4 w |
| 997 | ret long get ot_x5_st 0 |
| 998 | end |
| 999 | |
| 1000 | note ot_x5_reset: forget the presented chain (the trust store stays) |
| 1001 | export fn ot_x5_reset to void |
| 1002 | put ot_x5_st 2 0 |
| 1003 | put ot_x5_st 3 0 |
| 1004 | put ot_x5_st 4 get ot_x5_st 1 |
| 1005 | end |
| 1006 | |
| 1007 | note ot_x5_add: the next certificate of the server's Certificate message (leaf first). The number of |
| 1008 | note certificates held so far, or -1 when it breaks a parsing rule (ot_x5_check then gives 1), -3 when |
| 1009 | note the store is full (also then 1). |
| 1010 | export fn ot_x5_add der view uchar * to long |
| 1011 | n = len der |
| 1012 | w = get ot_x5_st 4 |
| 1013 | r = add get ot_x5_st 0 get ot_x5_st 2 |
| 1014 | if or gt n sub ot_x5_cap w ge r ot_x5_mr |
| 1015 | put ot_x5_st 3 add get ot_x5_st 3 1 |
| 1016 | ret -3 |
| 1017 | end |
| 1018 | for i ulong n bound 16777216 |
| 1019 | put ot_der_m add w i get der i |
| 1020 | end |
| 1021 | if eq ot_x5_parse w add w n r false |
| 1022 | put ot_x5_st 3 add get ot_x5_st 3 1 |
| 1023 | ret -1 |
| 1024 | end |
| 1025 | put ot_x5_st 2 add get ot_x5_st 2 1 |
| 1026 | put ot_x5_st 4 add w n |
| 1027 | ret long get ot_x5_st 2 |
| 1028 | end |
| 1029 | |
| 1030 | note ot_x5_leafspki: copy the leaf's SubjectPublicKeyInfo DER into o; its length, or -1 (no leaf, or o |
| 1031 | note too short) |
| 1032 | export fn ot_x5_leafspki o edit uchar * to long |
| 1033 | if eq get ot_x5_st 2 0 |
| 1034 | ret -1 |
| 1035 | end |
| 1036 | r = get ot_x5_st 0 |
| 1037 | n = ot_x5_g r 15 |
| 1038 | if gt n len o |
| 1039 | ret -1 |
| 1040 | end |
| 1041 | p = ot_x5_g r 14 |
| 1042 | for i ulong n bound 16777216 |
| 1043 | put o i get ot_der_m add p i |
| 1044 | end |
| 1045 | ret long n |
| 1046 | end |
| 1047 | |
| 1048 | note ---- host name ---- |
| 1049 | |
| 1050 | fn ot_x5_low c uchar to uchar |
| 1051 | if and ge c 65 le c 90 |
| 1052 | ret add c 32 |
| 1053 | end |
| 1054 | ret c |
| 1055 | end |
| 1056 | |
| 1057 | note ot_x5_host: take the host into ot_x5_hb / ot_x5_hs; false when it can match nothing |
| 1058 | fn ot_x5_host h view uchar * to bool |
| 1059 | n = len h |
| 1060 | if gt n 0 |
| 1061 | if eq get h sub n 1 46 |
| 1062 | n = sub n 1 |
| 1063 | end |
| 1064 | end |
| 1065 | if or eq n 0 gt n 253 |
| 1066 | ret false |
| 1067 | end |
| 1068 | dg = true |
| 1069 | cl = false |
| 1070 | for i ulong n bound 16777216 |
| 1071 | c = ot_x5_low get h i |
| 1072 | put ot_x5_hb i c |
| 1073 | if and ne c 46 or lt c 48 gt c 57 |
| 1074 | dg = false |
| 1075 | end |
| 1076 | if eq c 58 |
| 1077 | cl = true |
| 1078 | end |
| 1079 | end |
| 1080 | put ot_x5_hs 0 n |
| 1081 | if dg |
| 1082 | put ot_x5_hs 1 1 |
| 1083 | ret ot_x5_ip4 n |
| 1084 | end |
| 1085 | if cl |
| 1086 | put ot_x5_hs 1 2 |
| 1087 | ret ot_x5_ip6 n |
| 1088 | end |
| 1089 | put ot_x5_hs 1 0 |
| 1090 | ret ot_x5_dnsok 0 n |
| 1091 | end |
| 1092 | |
| 1093 | note ot_x5_dnsok: ot_x5_hb [o, o + n) is a name of labels: no empty label, no "*" |
| 1094 | fn ot_x5_dnsok o ulong n ulong to bool |
| 1095 | if eq n 0 |
| 1096 | ret false |
| 1097 | end |
| 1098 | if or eq get ot_x5_hb o 46 eq get ot_x5_hb add o sub n 1 46 |
| 1099 | ret false |
| 1100 | end |
| 1101 | for i ulong n bound 16777216 |
| 1102 | c = get ot_x5_hb add o i |
| 1103 | if eq c 42 |
| 1104 | ret false |
| 1105 | end |
| 1106 | if and eq c 46 gt i 0 |
| 1107 | if eq get ot_x5_hb add o sub i 1 46 |
| 1108 | ret false |
| 1109 | end |
| 1110 | end |
| 1111 | end |
| 1112 | ret true |
| 1113 | end |
| 1114 | |
| 1115 | note ot_x5_ip4: four decimal parts 0..255 without leading zeros into ot_x5_ia |
| 1116 | fn ot_x5_ip4 n ulong to bool |
| 1117 | k = ulong 0 |
| 1118 | v = ulong 0 |
| 1119 | d = ulong 0 |
| 1120 | for i ulong add n 1 bound 16777216 |
| 1121 | c = uchar 46 |
| 1122 | if lt i n |
| 1123 | c = get ot_x5_hb i |
| 1124 | end |
| 1125 | if eq c 46 |
| 1126 | if or or eq d 0 ge k 4 gt v 255 |
| 1127 | ret false |
| 1128 | end |
| 1129 | put ot_x5_ia k uchar v |
| 1130 | k = add k 1 |
| 1131 | v = 0 |
| 1132 | d = 0 |
| 1133 | else |
| 1134 | if and eq d 1 eq v 0 |
| 1135 | ret false |
| 1136 | end |
| 1137 | v = add mul v 10 ulong sub c 48 |
| 1138 | d = add d 1 |
| 1139 | if gt d 3 |
| 1140 | ret false |
| 1141 | end |
| 1142 | end |
| 1143 | end |
| 1144 | ret eq k 4 |
| 1145 | end |
| 1146 | |
| 1147 | note ot_x5_hexv: a lower-case hex digit's value, 16 otherwise |
| 1148 | fn ot_x5_hexv c uchar to ulong |
| 1149 | if and ge c 48 le c 57 |
| 1150 | ret ulong sub c 48 |
| 1151 | end |
| 1152 | if and ge c 97 le c 102 |
| 1153 | ret ulong add sub c 97 10 |
| 1154 | end |
| 1155 | ret 16 |
| 1156 | end |
| 1157 | |
| 1158 | note ot_x5_ip6: RFC 4291 2.2 forms 1 and 2 (no embedded IPv4) into ot_x5_ia |
| 1159 | fn ot_x5_ip6 n ulong to bool |
| 1160 | g = new ulong 8 0 |
| 1161 | k = ulong 0 |
| 1162 | dc = ulong 8 |
| 1163 | i = ulong 0 |
| 1164 | if and gt n 1 eq get ot_x5_hb 0 58 |
| 1165 | if ne get ot_x5_hb 1 58 |
| 1166 | ret false |
| 1167 | end |
| 1168 | dc = 0 |
| 1169 | i = 2 |
| 1170 | end |
| 1171 | while lt i n bound 256 |
| 1172 | v = ulong 0 |
| 1173 | d = ulong 0 |
| 1174 | while lt i n bound 256 |
| 1175 | x = ot_x5_hexv get ot_x5_hb i |
| 1176 | if eq x 16 |
| 1177 | break |
| 1178 | end |
| 1179 | v = add mul v 16 x |
| 1180 | d = add d 1 |
| 1181 | i = add i 1 |
| 1182 | end |
| 1183 | if or or eq d 0 gt d 4 ge k 8 |
| 1184 | ret false |
| 1185 | end |
| 1186 | put g k v |
| 1187 | k = add k 1 |
| 1188 | if lt i n |
| 1189 | if ne get ot_x5_hb i 58 |
| 1190 | ret false |
| 1191 | end |
| 1192 | i = add i 1 |
| 1193 | if ge i n |
| 1194 | ret false |
| 1195 | end |
| 1196 | if eq get ot_x5_hb i 58 |
| 1197 | if ne dc 8 |
| 1198 | ret false |
| 1199 | end |
| 1200 | dc = k |
| 1201 | i = add i 1 |
| 1202 | end |
| 1203 | end |
| 1204 | end |
| 1205 | ret ot_x5_ip6x g k dc |
| 1206 | end |
| 1207 | |
| 1208 | note ot_x5_ip6x: expand k groups with "::" before group dc (8: none) into ot_x5_ia |
| 1209 | fn ot_x5_ip6x g view ulong 8 k ulong dc ulong to bool |
| 1210 | if eq dc 8 |
| 1211 | if ne k 8 |
| 1212 | ret false |
| 1213 | end |
| 1214 | else |
| 1215 | if gt k 7 |
| 1216 | ret false |
| 1217 | end |
| 1218 | end |
| 1219 | z = ulong 0 |
| 1220 | if ne dc 8 |
| 1221 | z = sub 8 k |
| 1222 | end |
| 1223 | for j ulong 8 |
| 1224 | v = ulong 0 |
| 1225 | if lt j dc |
| 1226 | v = get g j |
| 1227 | else |
| 1228 | if ge j add dc z |
| 1229 | v = get g sub j z |
| 1230 | end |
| 1231 | end |
| 1232 | put ot_x5_ia mul j 2 uchar shr v 8 |
| 1233 | put ot_x5_ia add mul j 2 1 uchar and v 255 |
| 1234 | end |
| 1235 | ret true |
| 1236 | end |
| 1237 | |
| 1238 | note ot_x5_ceq: store [v, v + n) equals ot_x5_hb [o, o + n), ASCII case-insensitive |
| 1239 | fn ot_x5_ceq v ulong o ulong n ulong to bool |
| 1240 | for i ulong n bound 16777216 |
| 1241 | if ne ot_x5_low get ot_der_m add v i get ot_x5_hb add o i |
| 1242 | ret false |
| 1243 | end |
| 1244 | end |
| 1245 | ret true |
| 1246 | end |
| 1247 | |
| 1248 | note ot_x5_lbls: store [v, v + n) is at least two labels, none empty, no "*" |
| 1249 | fn ot_x5_lbls v ulong n ulong to bool |
| 1250 | if eq n 0 |
| 1251 | ret false |
| 1252 | end |
| 1253 | if or eq get ot_der_m v 46 eq get ot_der_m add v sub n 1 46 |
| 1254 | ret false |
| 1255 | end |
| 1256 | dots = ulong 0 |
| 1257 | for i ulong n bound 16777216 |
| 1258 | c = get ot_der_m add v i |
| 1259 | if eq c 42 |
| 1260 | ret false |
| 1261 | end |
| 1262 | if eq c 46 |
| 1263 | dots = add dots 1 |
| 1264 | if eq get ot_der_m add v sub i 1 46 |
| 1265 | ret false |
| 1266 | end |
| 1267 | end |
| 1268 | end |
| 1269 | ret gt dots 0 |
| 1270 | end |
| 1271 | |
| 1272 | note ot_x5_dnsm: the dNSName [v, v + l) matches the host |
| 1273 | fn ot_x5_dnsm v ulong l ulong to bool |
| 1274 | hn = get ot_x5_hs 0 |
| 1275 | if eq l 0 |
| 1276 | ret false |
| 1277 | end |
| 1278 | if eq get ot_der_m v 42 |
| 1279 | if lt l 3 |
| 1280 | ret false |
| 1281 | end |
| 1282 | if ne get ot_der_m add v 1 46 |
| 1283 | ret false |
| 1284 | end |
| 1285 | if eq ot_x5_lbls add v 2 sub l 2 false |
| 1286 | ret false |
| 1287 | end |
| 1288 | f = ulong 0 |
| 1289 | while lt f hn bound 256 |
| 1290 | if eq get ot_x5_hb f 46 |
| 1291 | break |
| 1292 | end |
| 1293 | f = add f 1 |
| 1294 | end |
| 1295 | if or eq f 0 ge f hn |
| 1296 | ret false |
| 1297 | end |
| 1298 | if ne sub hn add f 1 sub l 2 |
| 1299 | ret false |
| 1300 | end |
| 1301 | ret ot_x5_ceq add v 2 add f 1 sub l 2 |
| 1302 | end |
| 1303 | if ne l hn |
| 1304 | ret false |
| 1305 | end |
| 1306 | ret ot_x5_ceq v 0 l |
| 1307 | end |
| 1308 | |
| 1309 | note ot_x5_ipm: the iPAddress [v, v + l) equals the host address |
| 1310 | fn ot_x5_ipm v ulong l ulong to bool |
| 1311 | n = ulong 4 |
| 1312 | if eq get ot_x5_hs 1 2 |
| 1313 | n = 16 |
| 1314 | end |
| 1315 | if ne l n |
| 1316 | ret false |
| 1317 | end |
| 1318 | for i ulong n bound 16777216 |
| 1319 | if ne get ot_der_m add v i get ot_x5_ia i |
| 1320 | ret false |
| 1321 | end |
| 1322 | end |
| 1323 | ret true |
| 1324 | end |
| 1325 | |
| 1326 | note ot_x5_nmok: a SAN entry of record r matches the host |
| 1327 | fn ot_x5_nmok r ulong to bool |
| 1328 | if eq ot_x5_has r 128 false |
| 1329 | ret false |
| 1330 | end |
| 1331 | k = ot_x5_g r 16 |
| 1332 | e = add k ot_x5_g r 17 |
| 1333 | w = get ot_x5_hs 1 |
| 1334 | while lt k e bound 16777216 |
| 1335 | if eq ot_der_rd k e false |
| 1336 | ret false |
| 1337 | end |
| 1338 | t = ot_der_tg |
| 1339 | m = ot_der_nd |
| 1340 | if and eq w 0 eq t 130 |
| 1341 | if ot_x5_dnsm ot_der_vo ot_der_vl |
| 1342 | ret true |
| 1343 | end |
| 1344 | end |
| 1345 | if and ne w 0 eq t 135 |
| 1346 | if ot_x5_ipm ot_der_vo ot_der_vl |
| 1347 | ret true |
| 1348 | end |
| 1349 | end |
| 1350 | k = m |
| 1351 | end |
| 1352 | ret false |
| 1353 | end |
| 1354 | |
| 1355 | note ---- path checks ---- |
| 1356 | |
| 1357 | note ot_x5_tok: record r is valid at now |
| 1358 | fn ot_x5_tok r ulong to bool |
| 1359 | t = get ot_x5_nw 0 |
| 1360 | if lt t get ot_x5_tm mul r 2 |
| 1361 | ret false |
| 1362 | end |
| 1363 | ret le t get ot_x5_tm add mul r 2 1 |
| 1364 | end |
| 1365 | |
| 1366 | note ot_x5_iss: record x can have issued record c |
| 1367 | fn ot_x5_iss x ulong c ulong to bool |
| 1368 | n = ot_x5_g c 9 |
| 1369 | if ne n ot_x5_g x 11 |
| 1370 | ret false |
| 1371 | end |
| 1372 | if eq ot_der_eq ot_x5_g c 8 ot_x5_g x 10 n false |
| 1373 | ret false |
| 1374 | end |
| 1375 | a = ot_x5_g c 21 |
| 1376 | b = ot_x5_g x 19 |
| 1377 | if or eq a 0 eq b 0 |
| 1378 | ret true |
| 1379 | end |
| 1380 | if ne a b |
| 1381 | ret false |
| 1382 | end |
| 1383 | ret ot_der_eq ot_x5_g c 20 ot_x5_g x 18 a |
| 1384 | end |
| 1385 | |
| 1386 | note ot_x5_sig: the signature of record c under the key of record x: 0, 5 or 8 (ot_pk_csig) |
| 1387 | fn ot_x5_sig c ulong x ulong to ulong |
| 1388 | put ot_pk_q 0 ot_x5_g c 4 |
| 1389 | put ot_pk_q 1 ot_x5_g c 5 |
| 1390 | put ot_pk_q 2 ot_x5_g c 2 |
| 1391 | put ot_pk_q 3 sub ot_x5_g c 3 ot_x5_g c 2 |
| 1392 | put ot_pk_q 4 ot_x5_g c 6 |
| 1393 | put ot_pk_q 5 ot_x5_g c 7 |
| 1394 | put ot_pk_q 6 ot_x5_g x 14 |
| 1395 | put ot_pk_q 7 ot_x5_g x 15 |
| 1396 | ret ot_pk_csig |
| 1397 | end |
| 1398 | |
| 1399 | note ot_x5_dcm: the dNSName constraint [c, c + cl) matches the name [n, n + nl) (store, case-insensitive) |
| 1400 | fn ot_x5_dcm c ulong cl ulong n ulong nl ulong to bool |
| 1401 | if eq cl 0 |
| 1402 | ret true |
| 1403 | end |
| 1404 | if lt nl cl |
| 1405 | ret false |
| 1406 | end |
| 1407 | o = add n sub nl cl |
| 1408 | for i ulong cl bound 16777216 |
| 1409 | if ne ot_x5_low get ot_der_m add c i ot_x5_low get ot_der_m add o i |
| 1410 | ret false |
| 1411 | end |
| 1412 | end |
| 1413 | if eq get ot_der_m c 46 |
| 1414 | ret gt nl cl |
| 1415 | end |
| 1416 | if eq nl cl |
| 1417 | ret true |
| 1418 | end |
| 1419 | ret eq get ot_der_m sub o 1 46 |
| 1420 | end |
| 1421 | |
| 1422 | note ot_x5_ncany: the subtree list [a, j) has a plain dNSName subtree; with n, nl: one matching that name |
| 1423 | fn ot_x5_ncany a ulong j ulong n ulong nl ulong any bool to bool |
| 1424 | k = a |
| 1425 | while lt k j bound 16777216 |
| 1426 | if eq ot_der_ex k j 48 false |
| 1427 | ret false |
| 1428 | end |
| 1429 | m = ot_der_nd |
| 1430 | if ot_der_rd ot_der_vo m |
| 1431 | if and eq ot_der_tg 130 eq ot_der_nd m |
| 1432 | if any |
| 1433 | ret true |
| 1434 | end |
| 1435 | if ot_x5_dcm ot_der_vo ot_der_vl n nl |
| 1436 | ret true |
| 1437 | end |
| 1438 | end |
| 1439 | end |
| 1440 | k = m |
| 1441 | end |
| 1442 | ret false |
| 1443 | end |
| 1444 | |
| 1445 | note ot_x5_ncl: the leaf lf and the host against one subtree list [a, j); ex: excluded, else permitted |
| 1446 | fn ot_x5_ncl a ulong j ulong lf ulong ex bool to bool |
| 1447 | if eq ex false |
| 1448 | if eq ot_x5_ncany a j 0 0 true false |
| 1449 | ret true |
| 1450 | end |
| 1451 | end |
| 1452 | if eq ot_x5_nchok a j ex false |
| 1453 | ret false |
| 1454 | end |
| 1455 | k = ot_x5_g lf 16 |
| 1456 | e = add k ot_x5_g lf 17 |
| 1457 | while lt k e bound 16777216 |
| 1458 | if eq ot_der_rd k e false |
| 1459 | ret false |
| 1460 | end |
| 1461 | m = ot_der_nd |
| 1462 | if eq ot_der_tg 130 |
| 1463 | v = ot_der_vo |
| 1464 | l = ot_der_vl |
| 1465 | h = ot_x5_ncany a j v l false |
| 1466 | if eq h ex |
| 1467 | ret false |
| 1468 | end |
| 1469 | if ex |
| 1470 | if ot_x5_ncwb a j v l |
| 1471 | ret false |
| 1472 | end |
| 1473 | end |
| 1474 | end |
| 1475 | k = m |
| 1476 | end |
| 1477 | ret true |
| 1478 | end |
| 1479 | |
| 1480 | note ot_x5_dch: the dNSName constraint [c, c + cl) (store) matches the host (ot_x5_hb, lower case) |
| 1481 | fn ot_x5_dch c ulong cl ulong to bool |
| 1482 | hn = get ot_x5_hs 0 |
| 1483 | if eq cl 0 |
| 1484 | ret true |
| 1485 | end |
| 1486 | if lt hn cl |
| 1487 | ret false |
| 1488 | end |
| 1489 | o = sub hn cl |
| 1490 | for i ulong cl bound 16777216 |
| 1491 | if ne ot_x5_low get ot_der_m add c i get ot_x5_hb add o i |
| 1492 | ret false |
| 1493 | end |
| 1494 | end |
| 1495 | if eq get ot_der_m c 46 |
| 1496 | ret gt hn cl |
| 1497 | end |
| 1498 | if eq hn cl |
| 1499 | ret true |
| 1500 | end |
| 1501 | ret eq get ot_x5_hb sub o 1 46 |
| 1502 | end |
| 1503 | |
| 1504 | note ot_x5_nchst: a plain dNSName subtree of the list [a, j) matches the host |
| 1505 | fn ot_x5_nchst a ulong j ulong to bool |
| 1506 | k = a |
| 1507 | while lt k j bound 16777216 |
| 1508 | if eq ot_der_ex k j 48 false |
| 1509 | ret false |
| 1510 | end |
| 1511 | m = ot_der_nd |
| 1512 | if ot_der_rd ot_der_vo m |
| 1513 | if and eq ot_der_tg 130 eq ot_der_nd m |
| 1514 | if ot_x5_dch ot_der_vo ot_der_vl |
| 1515 | ret true |
| 1516 | end |
| 1517 | end |
| 1518 | end |
| 1519 | k = m |
| 1520 | end |
| 1521 | ret false |
| 1522 | end |
| 1523 | |
| 1524 | note ot_x5_nchok: a DNS host satisfies the subtree list [a, j) (excluded: matches none; permitted: the |
| 1525 | note caller has found a dNSName subtree, the host matches one); an IP host is not a dNSName |
| 1526 | fn ot_x5_nchok a ulong j ulong ex bool to bool |
| 1527 | if ne get ot_x5_hs 1 0 |
| 1528 | ret true |
| 1529 | end |
| 1530 | h = ot_x5_nchst a j |
| 1531 | if ex |
| 1532 | ret eq h false |
| 1533 | end |
| 1534 | ret h |
| 1535 | end |
| 1536 | |
| 1537 | note ot_x5_below: the constraint [c, c + cl) without a leading dot is a name strictly below the name |
| 1538 | note [d, d + dl) (store, case-insensitive) |
| 1539 | fn ot_x5_below c ulong cl ulong d ulong dl ulong to bool |
| 1540 | x = c |
| 1541 | n = cl |
| 1542 | if gt n 0 |
| 1543 | if eq get ot_der_m x 46 |
| 1544 | x = add x 1 |
| 1545 | n = sub n 1 |
| 1546 | end |
| 1547 | end |
| 1548 | if le n dl |
| 1549 | ret false |
| 1550 | end |
| 1551 | o = add x sub n dl |
| 1552 | if ne get ot_der_m sub o 1 46 |
| 1553 | ret false |
| 1554 | end |
| 1555 | for i ulong dl bound 16777216 |
| 1556 | if ne ot_x5_low get ot_der_m add o i ot_x5_low get ot_der_m add d i |
| 1557 | ret false |
| 1558 | end |
| 1559 | end |
| 1560 | ret true |
| 1561 | end |
| 1562 | |
| 1563 | note ot_x5_ncwb: the SAN [v, v + l) is a wildcard "*.D" and an excluded plain dNSName subtree of the |
| 1564 | note list [a, j) lies strictly below D |
| 1565 | fn ot_x5_ncwb a ulong j ulong v ulong l ulong to bool |
| 1566 | if lt l 3 |
| 1567 | ret false |
| 1568 | end |
| 1569 | if or ne get ot_der_m v 42 ne get ot_der_m add v 1 46 |
| 1570 | ret false |
| 1571 | end |
| 1572 | k = a |
| 1573 | while lt k j bound 16777216 |
| 1574 | if eq ot_der_ex k j 48 false |
| 1575 | ret false |
| 1576 | end |
| 1577 | m = ot_der_nd |
| 1578 | if ot_der_rd ot_der_vo m |
| 1579 | if and eq ot_der_tg 130 eq ot_der_nd m |
| 1580 | if ot_x5_below ot_der_vo ot_der_vl add v 2 sub l 2 |
| 1581 | ret true |
| 1582 | end |
| 1583 | end |
| 1584 | end |
| 1585 | k = m |
| 1586 | end |
| 1587 | ret false |
| 1588 | end |
| 1589 | |
| 1590 | note ot_x5_ncok: the leaf's dNSName SANs satisfy the nameConstraints of record r |
| 1591 | fn ot_x5_ncok r ulong lf ulong to bool |
| 1592 | i = ot_x5_g r 25 |
| 1593 | e = add i ot_x5_g r 26 |
| 1594 | while lt i e bound 16777216 |
| 1595 | if eq ot_der_rd i e false |
| 1596 | ret false |
| 1597 | end |
| 1598 | j = ot_der_nd |
| 1599 | if eq ot_x5_ncl ot_der_vo j lf eq ot_der_tg 161 false |
| 1600 | ret false |
| 1601 | end |
| 1602 | i = j |
| 1603 | end |
| 1604 | ret true |
| 1605 | end |
| 1606 | |
| 1607 | note ot_x5_ca: issuer record r at path position i (1 = issues the leaf) meets its constraints |
| 1608 | fn ot_x5_ca r ulong i ulong lf ulong to bool |
| 1609 | if or eq ot_x5_has r 1 false eq ot_x5_has r 2 false |
| 1610 | ret false |
| 1611 | end |
| 1612 | if gt sub i 1 ot_x5_g r 23 |
| 1613 | ret false |
| 1614 | end |
| 1615 | if ot_x5_has r 4 |
| 1616 | if eq and ot_x5_g r 24 1024 0 |
| 1617 | ret false |
| 1618 | end |
| 1619 | end |
| 1620 | if ot_x5_has r 256 |
| 1621 | if eq ot_x5_ncok r lf false |
| 1622 | ret false |
| 1623 | end |
| 1624 | end |
| 1625 | ret true |
| 1626 | end |
| 1627 | |
| 1628 | note ot_x5_cons: constraints (code 6) of the path ot_x5_pth [0 .. m] |
| 1629 | fn ot_x5_cons m ulong to bool |
| 1630 | lf = get ot_x5_pth 0 |
| 1631 | if ot_x5_has lf 4 |
| 1632 | if eq and ot_x5_g lf 24 32768 0 |
| 1633 | ret false |
| 1634 | end |
| 1635 | end |
| 1636 | for i ulong m bound 16777216 |
| 1637 | r = get ot_x5_pth i |
| 1638 | if ot_x5_has r 8 |
| 1639 | if eq ot_x5_has r 16 false |
| 1640 | ret false |
| 1641 | end |
| 1642 | end |
| 1643 | if eq ot_x5_ca get ot_x5_pth add i 1 add i 1 lf false |
| 1644 | ret false |
| 1645 | end |
| 1646 | end |
| 1647 | ret true |
| 1648 | end |
| 1649 | |
| 1650 | note ot_x5_pev: the score of the path ot_x5_pth [0 .. m] (m: the trust anchor) |
| 1651 | fn ot_x5_pev m ulong to ulong |
| 1652 | for i ulong add m 1 bound 16777216 |
| 1653 | if eq ot_x5_tok get ot_x5_pth i false |
| 1654 | ret 3 |
| 1655 | end |
| 1656 | end |
| 1657 | c = ulong 0 |
| 1658 | for i ulong m bound 16777216 |
| 1659 | s = ot_x5_sig get ot_x5_pth i get ot_x5_pth add i 1 |
| 1660 | if eq s 5 |
| 1661 | ret 5 |
| 1662 | end |
| 1663 | if ne s 0 |
| 1664 | c = s |
| 1665 | end |
| 1666 | end |
| 1667 | if eq ot_x5_cons m false |
| 1668 | ret 6 |
| 1669 | end |
| 1670 | for i ulong add m 1 bound 16777216 |
| 1671 | if ne and ot_x5_g get ot_x5_pth i 22 96 0 |
| 1672 | ret 7 |
| 1673 | end |
| 1674 | end |
| 1675 | for i ulong add m 1 bound 16777216 |
| 1676 | r = get ot_x5_pth i |
| 1677 | if ne ot_pk_kok ot_x5_g r 14 ot_x5_g r 15 0 |
| 1678 | ret 8 |
| 1679 | end |
| 1680 | end |
| 1681 | ret c |
| 1682 | end |
| 1683 | |
| 1684 | note ot_x5_cand: the t-th candidate issuer: presented certificates first, then the trust store |
| 1685 | fn ot_x5_cand t ulong to ulong |
| 1686 | nr = get ot_x5_st 0 |
| 1687 | np = get ot_x5_st 2 |
| 1688 | if lt t np |
| 1689 | ret add nr t |
| 1690 | end |
| 1691 | ret sub t np |
| 1692 | end |
| 1693 | |
| 1694 | note ot_x5_dfs: depth-first search from the leaf; the best path score, 4 when there is no path |
| 1695 | fn ot_x5_dfs lf ulong to ulong |
| 1696 | na = add get ot_x5_st 0 get ot_x5_st 2 |
| 1697 | best = ulong 99 |
| 1698 | d = ulong 0 |
| 1699 | n = ulong 0 |
| 1700 | put ot_x5_pth 0 lf |
| 1701 | put ot_x5_cur 0 0 |
| 1702 | put ot_x5_on lf 1 |
| 1703 | while true bound 4611686018427387904 |
| 1704 | c = get ot_x5_pth d |
| 1705 | t = get ot_x5_cur d |
| 1706 | if ge t na |
| 1707 | put ot_x5_on c 0 |
| 1708 | if eq d 0 |
| 1709 | break |
| 1710 | end |
| 1711 | d = sub d 1 |
| 1712 | else |
| 1713 | put ot_x5_cur d add t 1 |
| 1714 | n = add n 1 |
| 1715 | if gt n 1000000 |
| 1716 | break |
| 1717 | end |
| 1718 | x = ot_x5_cand t |
| 1719 | if eq get ot_x5_on x 0 |
| 1720 | if ot_x5_iss x c |
| 1721 | if lt x get ot_x5_st 0 |
| 1722 | put ot_x5_pth add d 1 x |
| 1723 | s = ot_x5_pev add d 1 |
| 1724 | if lt s best |
| 1725 | best = s |
| 1726 | end |
| 1727 | if eq best 0 |
| 1728 | break |
| 1729 | end |
| 1730 | else |
| 1731 | d = add d 1 |
| 1732 | put ot_x5_pth d x |
| 1733 | put ot_x5_cur d 0 |
| 1734 | put ot_x5_on x 1 |
| 1735 | end |
| 1736 | end |
| 1737 | end |
| 1738 | end |
| 1739 | end |
| 1740 | for i ulong add d 1 bound 16777216 |
| 1741 | put ot_x5_on get ot_x5_pth i 0 |
| 1742 | end |
| 1743 | if eq best 99 |
| 1744 | ret 4 |
| 1745 | end |
| 1746 | ret best |
| 1747 | end |
| 1748 | |
| 1749 | note ot_x5_check: validate the presented chain for host at now (Unix seconds); 0 or the code |
| 1750 | export fn ot_x5_check host view uchar * now long to int |
| 1751 | if eq add get ot_x5_st 2 get ot_x5_st 3 0 |
| 1752 | ret 9 |
| 1753 | end |
| 1754 | if gt get ot_x5_st 3 0 |
| 1755 | ret 1 |
| 1756 | end |
| 1757 | put ot_x5_nw 0 now |
| 1758 | lf = get ot_x5_st 0 |
| 1759 | if eq ot_x5_host host false |
| 1760 | ret 2 |
| 1761 | end |
| 1762 | if eq ot_x5_nmok lf false |
| 1763 | ret 2 |
| 1764 | end |
| 1765 | if eq ot_x5_tok lf false |
| 1766 | ret 3 |
| 1767 | end |
| 1768 | ret int ot_x5_dfs lf |
| 1769 | end |