omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / apps / aienos-site / om / agora / tls / x509.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note om/tls/x509.om (module X5): strict DER reader, X.509 v3 certificate parser, PEM trust store, |
| 3 | note TLS 1.3 server chain validation and the CertificateVerify signature check. Library unit for the |
| 4 | note OM-2 TLS client; compose after sha2.om, hkdf.om, ec.om, rsa.om. No mutable globals. |
| 5 | note |
| 6 | note Reject codes (ulong, 0 = valid; x509_errname gives the ASCII name): |
| 7 | note 1 MALFORMED DER/structure/framing error (any input that does not parse) |
| 8 | note 2 EXPIRED now >= notAfter of a non-anchor certificate in the path (at notAfter |
| 9 | note itself already expired, as OpenSSL; stricter than RFC 5280 4.1.2.5) |
| 10 | note 3 NOT_YET_VALID now < notBefore of a non-anchor certificate in the path |
| 11 | note 4 NAME_MISMATCH no SAN dNSName matches the host, or the host is not a valid LDH name |
| 12 | note 5 UNKNOWN_ISSUER no trust anchor or presented certificate has the issuer's name (only |
| 13 | note self-issued store entries are trust anchors, see chain_anchor) |
| 14 | note 6 BAD_SIGNATURE issuer found by name but no candidate key verifies the signature |
| 15 | note 7 NOT_CA issuer lacks basicConstraints cA TRUE |
| 16 | note 8 PATH_LENGTH pathLenConstraint of an issuer exceeded |
| 17 | note 9 KEY_USAGE issuer keyUsage lacks keyCertSign, or leaf keyUsage lacks digitalSignature |
| 18 | note 10 EKU leaf EKU lacks serverAuth, or issuer EKU lacks serverAuth and anyExtendedKeyUsage |
| 19 | note 11 UNSUPPORTED_ALG signature algorithm or key type not on the allowed list, or key/alg mismatch |
| 20 | note 12 WEAK_KEY RSA key out of policy: modulus below 2048 bits (leaf key or any key that |
| 21 | note verifies a signature); for presented keys (leaf and chain) also a modulus |
| 22 | note above 4096 bits or a public exponent that is even, below 3 or above 2^32-1 |
| 23 | note 13 CRITICAL_EXT critical extension we do not implement |
| 24 | note 14 TOO_DEEP more than 8 non-anchor certificates would be needed |
| 25 | note 15 LEAF_IS_CA leaf has basicConstraints cA TRUE |
| 26 | note 16 NO_SAN leaf has no subjectAltName (no CN fallback) |
| 27 | note 17 IP_HOST host is an IP literal (refused) |
| 28 | note 18 ALG_MISMATCH tbsCertificate.signature differs from signatureAlgorithm |
| 29 | note 19 SCHEME CertificateVerify scheme refused or does not match the leaf key |
| 30 | note 20 NAME_CONSTRAINTS a nameConstraints extension (critical or not) on any certificate: name |
| 31 | note constraints are not implemented, so such a certificate is always refused |
| 32 | note |
| 33 | note Constant time: nothing here handles secrets. Certificates, names, signatures and keys are public; |
| 34 | note every function is variable time by design (branches on public data). Hashing and signature checks |
| 35 | note are done by sha2.om, ec.om and rsa.om (see their notes). |
| 36 | |
| 37 | const X5RSZ ulong 32 |
| 38 | const X5RTBO ulong 0 |
| 39 | const X5RTBL ulong 1 |
| 40 | const X5RALG ulong 2 |
| 41 | const X5RSGO ulong 3 |
| 42 | const X5RSGL ulong 4 |
| 43 | const X5RISO ulong 5 |
| 44 | const X5RISL ulong 6 |
| 45 | const X5RSBO ulong 7 |
| 46 | const X5RSBL ulong 8 |
| 47 | const X5RNB ulong 9 |
| 48 | const X5RNA ulong 10 |
| 49 | const X5RKT ulong 11 |
| 50 | const X5RKO ulong 12 |
| 51 | const X5RKL ulong 13 |
| 52 | const X5RBITS ulong 14 |
| 53 | const X5RBC ulong 15 |
| 54 | const X5RCA ulong 16 |
| 55 | const X5RPL ulong 17 |
| 56 | const X5RKU ulong 18 |
| 57 | const X5RKUB ulong 19 |
| 58 | const X5REKU ulong 20 |
| 59 | const X5RSRV ulong 21 |
| 60 | const X5RANY ulong 22 |
| 61 | const X5RSNO ulong 23 |
| 62 | const X5RSNL ulong 24 |
| 63 | const X5RSAN ulong 25 |
| 64 | const X5RVER ulong 26 |
| 65 | const X5RSEEN ulong 27 |
| 66 | const X5RAO ulong 28 |
| 67 | const X5RAL ulong 29 |
| 68 | const X5RTAO ulong 30 |
| 69 | const X5RTAL ulong 31 |
| 70 | |
| 71 | const X5WNOW ulong 64 |
| 72 | const X5WN ulong 65 |
| 73 | const X5WUSED ulong 66 |
| 74 | const X5WDEP ulong 67 |
| 75 | const X5WCUR ulong 68 |
| 76 | const X5WSEL ulong 69 |
| 77 | const X5WIX ulong 96 |
| 78 | const X5WSZ ulong 128 |
| 79 | const X5MAXC ulong 16 |
| 80 | const X5MAXD ulong 8 |
| 81 | const X5KBUF ulong 16384 |
| 82 | note largest presented leaf (bytes, DER), equal to TLLFSZ in tls.om; a larger one is X5EMAL |
| 83 | const X5LMAX ulong 16896 |
| 84 | note element cap of the SEQUENCE OF loops (EKU, SAN, Extensions, SAN host walk): X5EMAL beyond it |
| 85 | const X5NMAX ulong 9999 |
| 86 | note added to the RSA bit count in X5RBITS when the public exponent is out of policy (x509_rsaexp) |
| 87 | const X5RBADE ulong 4294967296 |
| 88 | const X5BIG ulong 0xffffffffffffffff |
| 89 | |
| 90 | const X5EMAL ulong 1 |
| 91 | const X5EEXP ulong 2 |
| 92 | const X5ENYV ulong 3 |
| 93 | const X5ENAME ulong 4 |
| 94 | const X5EUNK ulong 5 |
| 95 | const X5ESIG ulong 6 |
| 96 | const X5ENCA ulong 7 |
| 97 | const X5EPATH ulong 8 |
| 98 | const X5EKU ulong 9 |
| 99 | const X5EEKU ulong 10 |
| 100 | const X5EALG ulong 11 |
| 101 | const X5EWEAK ulong 12 |
| 102 | const X5ECRIT ulong 13 |
| 103 | const X5EDEEP ulong 14 |
| 104 | const X5ELCA ulong 15 |
| 105 | const X5ENSAN ulong 16 |
| 106 | const X5EIP ulong 17 |
| 107 | const X5EAMIS ulong 18 |
| 108 | const X5ESCH ulong 19 |
| 109 | const X5ENC ulong 20 |
| 110 | |
| 111 | note OID content bytes (hex), space separated; der_oidix returns the 1-based position. |
| 112 | note 1-3 sha256/384/512WithRSAEncryption (RFC 4055), 4 RSASSA-PSS, 5-7 ecdsa-with-SHA256/384/512 |
| 113 | note (RFC 5758), 8 rsaEncryption, 9 id-ecPublicKey, 10 prime256v1, 11 secp384r1 (RFC 5480), |
| 114 | note 12-14 sha256/384/512 (NIST CSOR), 15 id-mgf1, 16 basicConstraints, 17 keyUsage, 18 extKeyUsage, |
| 115 | note 19 subjectAltName, 20 nameConstraints (RFC 5280 4.2.1), 21 id-kp-serverAuth, 22 anyExtendedKeyUsage |
| 116 | text X5OIDS "2a864886f70d01010b 2a864886f70d01010c 2a864886f70d01010d 2a864886f70d01010a 2a8648ce3d040302 2a8648ce3d040303 2a8648ce3d040304 2a864886f70d010101 2a8648ce3d0201 2a8648ce3d030107 2b81040022 608648016503040201 608648016503040202 608648016503040203 2a864886f70d010108 551d13 551d0f 551d25 551d11 551d1e 2b06010505070301 551d2500 " |
| 117 | text X5NAMES "X509_OK X509_ERR_MALFORMED X509_ERR_EXPIRED X509_ERR_NOT_YET_VALID X509_ERR_NAME_MISMATCH X509_ERR_UNKNOWN_ISSUER X509_ERR_BAD_SIGNATURE X509_ERR_NOT_CA X509_ERR_PATH_LENGTH X509_ERR_KEY_USAGE X509_ERR_EKU X509_ERR_UNSUPPORTED_ALG X509_ERR_WEAK_KEY X509_ERR_CRITICAL_EXT X509_ERR_TOO_DEEP X509_ERR_LEAF_IS_CA X509_ERR_NO_SAN X509_ERR_IP_HOST X509_ERR_ALG_MISMATCH X509_ERR_SCHEME X509_ERR_NAME_CONSTRAINTS " |
| 118 | text X5PATH "/etc/ssl/certs/ca-certificates.crt" |
| 119 | text X5BEG "-----BEGIN CERTIFICATE-----" |
| 120 | text X5END "-----END CERTIFICATE-----" |
| 121 | |
| 122 | note ---------------------------------------------------------------- DER reader |
| 123 | |
| 124 | note der_hdr: the TLV header at d[p], whose content must end by e (e <= len d). Single-byte tags |
| 125 | note only; definite lengths of at most 3 length bytes, minimal (short form below 128, no leading 0). |
| 126 | note h[0] tag, h[1] content start, h[2] content end. 0 ok, 1 malformed. Never traps for len h >= 3. |
| 127 | fn der_hdr d view uchar * p ulong e ulong h edit ulong * to ulong |
| 128 | if gt e len d |
| 129 | ret 1 |
| 130 | end |
| 131 | if ge p e |
| 132 | ret 1 |
| 133 | end |
| 134 | if lt sub e p 2 |
| 135 | ret 1 |
| 136 | end |
| 137 | t = ulong get d p |
| 138 | if eq and t 31 31 |
| 139 | ret 1 |
| 140 | end |
| 141 | b = ulong get d add p 1 |
| 142 | q = add p 2 |
| 143 | n = b |
| 144 | if ge b 128 |
| 145 | k = and b 127 |
| 146 | if or eq k 0 gt k 3 |
| 147 | ret 1 |
| 148 | end |
| 149 | if gt k sub e q |
| 150 | ret 1 |
| 151 | end |
| 152 | if eq get d q 0 |
| 153 | ret 1 |
| 154 | end |
| 155 | n = 0 |
| 156 | for i ulong k |
| 157 | n = or shl n 8 ulong get d add q i |
| 158 | end |
| 159 | q = add q k |
| 160 | if lt n 128 |
| 161 | ret 1 |
| 162 | end |
| 163 | end |
| 164 | if gt n sub e q |
| 165 | ret 1 |
| 166 | end |
| 167 | put h 0 t |
| 168 | put h 1 q |
| 169 | put h 2 add q n |
| 170 | ret 0 |
| 171 | end |
| 172 | |
| 173 | note der_same: byte equality of two spans (lengths must match) |
| 174 | fn der_same a view uchar * b view uchar * to bool |
| 175 | if ne len a len b |
| 176 | ret false |
| 177 | end |
| 178 | for i ulong len a |
| 179 | if ne get a i get b i |
| 180 | ret false |
| 181 | end |
| 182 | end |
| 183 | ret true |
| 184 | end |
| 185 | |
| 186 | note der_eqat: d[a .. a+n) equals d[b .. b+n); false when either span leaves d |
| 187 | fn der_eqat d view uchar * a ulong b ulong n ulong to bool |
| 188 | if gt n len d |
| 189 | ret false |
| 190 | end |
| 191 | if or gt a sub len d n gt b sub len d n |
| 192 | ret false |
| 193 | end |
| 194 | for i ulong n |
| 195 | if ne get d add a i get d add b i |
| 196 | ret false |
| 197 | end |
| 198 | end |
| 199 | ret true |
| 200 | end |
| 201 | |
| 202 | note der_hexv: value of a lowercase hex digit (16 when not one) |
| 203 | fn der_hexv c ulong to ulong |
| 204 | if and ge c 48 le c 57 |
| 205 | ret sub c 48 |
| 206 | end |
| 207 | if and ge c 97 le c 102 |
| 208 | ret sub c 87 |
| 209 | end |
| 210 | ret 16 |
| 211 | end |
| 212 | |
| 213 | note der_hexeq: d equals the bytes written in hex by t (len t = 2 len d) |
| 214 | fn der_hexeq d view uchar * t view uchar * to bool |
| 215 | if ne len t mul len d 2 |
| 216 | ret false |
| 217 | end |
| 218 | for i ulong len d |
| 219 | hi = der_hexv ulong get t mul i 2 |
| 220 | lo = der_hexv ulong get t add mul i 2 1 |
| 221 | if ne ulong get d i or shl hi 4 lo |
| 222 | ret false |
| 223 | end |
| 224 | end |
| 225 | ret true |
| 226 | end |
| 227 | |
| 228 | note der_oidix: position (1-based) of the OID content d in X5OIDS, 0 when absent |
| 229 | fn der_oidix d view uchar * to ulong |
| 230 | s = ulong 0 |
| 231 | k = ulong 1 |
| 232 | n = len X5OIDS |
| 233 | while lt s n bound 1000 |
| 234 | j = s |
| 235 | while lt j n bound 1000 |
| 236 | if eq get X5OIDS j 32 |
| 237 | break |
| 238 | end |
| 239 | j = add j 1 |
| 240 | end |
| 241 | if der_hexeq d part X5OIDS s sub j s |
| 242 | ret k |
| 243 | end |
| 244 | s = add j 1 |
| 245 | k = add k 1 |
| 246 | end |
| 247 | ret 0 |
| 248 | end |
| 249 | |
| 250 | note der_small: value of the INTEGER TLV that is exactly a; nonnegative, minimal, at most 7 content |
| 251 | note bytes. X5BIG when it is not one. |
| 252 | fn der_small a view uchar * to ulong |
| 253 | h = new ulong 3 0 |
| 254 | if ne der_hdr a 0 len a h 0 |
| 255 | ret X5BIG |
| 256 | end |
| 257 | if or ne get h 0 2 ne get h 2 len a |
| 258 | ret X5BIG |
| 259 | end |
| 260 | s = get h 1 |
| 261 | n = sub len a s |
| 262 | if or eq n 0 gt n 7 |
| 263 | ret X5BIG |
| 264 | end |
| 265 | b = ulong get a s |
| 266 | if ge b 128 |
| 267 | ret X5BIG |
| 268 | end |
| 269 | if and gt n 1 eq b 0 |
| 270 | if lt ulong get a add s 1 128 |
| 271 | ret X5BIG |
| 272 | end |
| 273 | end |
| 274 | v = ulong 0 |
| 275 | for i ulong n |
| 276 | v = or shl v 8 ulong get a add s i |
| 277 | end |
| 278 | ret v |
| 279 | end |
| 280 | |
| 281 | note der_null: c[q .. e) is exactly a NULL (05 00) |
| 282 | fn der_null c view uchar * q ulong e ulong to bool |
| 283 | if ne sub e q 2 |
| 284 | ret false |
| 285 | end |
| 286 | if or ne get c q 5 ne get c add q 1 0 |
| 287 | ret false |
| 288 | end |
| 289 | ret true |
| 290 | end |
| 291 | |
| 292 | note der_u24: big-endian 24-bit value at a[p] (caller checks p + 3 <= len a) |
| 293 | fn der_u24 a view uchar * p ulong to ulong |
| 294 | v = shl ulong get a p 16 |
| 295 | v = or v shl ulong get a add p 1 8 |
| 296 | ret or v ulong get a add p 2 |
| 297 | end |
| 298 | |
| 299 | note der_d2: two decimal digits at t[i], 100 when either is not a digit |
| 300 | fn der_d2 t view uchar * i ulong to ulong |
| 301 | a = ulong get t i |
| 302 | b = ulong get t add i 1 |
| 303 | if or lt a 48 gt a 57 |
| 304 | ret 100 |
| 305 | end |
| 306 | if or lt b 48 gt b 57 |
| 307 | ret 100 |
| 308 | end |
| 309 | ret add mul sub a 48 10 sub b 48 |
| 310 | end |
| 311 | |
| 312 | note x509_days: days from 0000-03-01 to y-m-d (proleptic Gregorian, H. Hinnant's days_from_civil |
| 313 | note without the 1970 shift; y >= 1, m 1..12) |
| 314 | fn x509_days y ulong m ulong d ulong to ulong |
| 315 | yy = y |
| 316 | if le m 2 |
| 317 | yy = sub y 1 |
| 318 | end |
| 319 | era = div yy 400 |
| 320 | yoe = sub yy mul era 400 |
| 321 | mp = add m 9 |
| 322 | if gt m 2 |
| 323 | mp = sub m 3 |
| 324 | end |
| 325 | doy = div add mul mp 153 2 5 |
| 326 | doy = add doy sub d 1 |
| 327 | doe = add mul yoe 365 div yoe 4 |
| 328 | doe = add sub doe div yoe 100 doy |
| 329 | ret add mul era 146097 doe |
| 330 | end |
| 331 | |
| 332 | note x509_dim: days in month m of year y |
| 333 | fn x509_dim y ulong m ulong to ulong |
| 334 | if eq m 2 |
| 335 | if and eq rem y 4 0 or ne rem y 100 0 eq rem y 400 0 |
| 336 | ret 29 |
| 337 | end |
| 338 | ret 28 |
| 339 | end |
| 340 | if or or eq m 4 eq m 6 or eq m 9 eq m 11 |
| 341 | ret 30 |
| 342 | end |
| 343 | ret 31 |
| 344 | end |
| 345 | |
| 346 | note der_time: the Time TLV that is exactly t, as seconds since 0000-03-01T00:00:00Z, X5BIG when bad. |
| 347 | note UTCTime YYMMDDHHMMSSZ (RFC 5280 4.1.2.5.1: YY >= 50 is 19YY, else 20YY); |
| 348 | note GeneralizedTime YYYYMMDDHHMMSSZ (4.1.2.5.2). No fractions, no offsets, seconds 00..59. |
| 349 | fn der_time t view uchar * to ulong |
| 350 | h = new ulong 3 0 |
| 351 | if ne der_hdr t 0 len t h 0 |
| 352 | ret X5BIG |
| 353 | end |
| 354 | g = get h 0 |
| 355 | s = get h 1 |
| 356 | n = sub get h 2 s |
| 357 | if ne get h 2 len t |
| 358 | ret X5BIG |
| 359 | end |
| 360 | y = ulong 0 |
| 361 | if eq g 23 |
| 362 | if ne n 13 |
| 363 | ret X5BIG |
| 364 | end |
| 365 | y = der_d2 t s |
| 366 | if gt y 99 |
| 367 | ret X5BIG |
| 368 | end |
| 369 | y = add y 1900 |
| 370 | if lt y 1950 |
| 371 | y = add y 100 |
| 372 | end |
| 373 | s = add s 2 |
| 374 | else |
| 375 | if or ne g 24 ne n 15 |
| 376 | ret X5BIG |
| 377 | end |
| 378 | a = der_d2 t s |
| 379 | b = der_d2 t add s 2 |
| 380 | if or gt a 99 gt b 99 |
| 381 | ret X5BIG |
| 382 | end |
| 383 | y = add mul a 100 b |
| 384 | if eq y 0 |
| 385 | ret X5BIG |
| 386 | end |
| 387 | s = add s 4 |
| 388 | end |
| 389 | if ne get t add s 10 90 |
| 390 | ret X5BIG |
| 391 | end |
| 392 | ret x509_hms t s y |
| 393 | end |
| 394 | |
| 395 | note x509_hms: MMDDHHMMSS at t[s] of year y, as seconds (see der_time) |
| 396 | fn x509_hms t view uchar * s ulong y ulong to ulong |
| 397 | hh = der_d2 t add s 4 |
| 398 | mi = der_d2 t add s 6 |
| 399 | se = der_d2 t add s 8 |
| 400 | if or gt hh 23 or gt mi 59 gt se 59 |
| 401 | ret X5BIG |
| 402 | end |
| 403 | sec = add add mul hh 3600 mul mi 60 se |
| 404 | mo = der_d2 t s |
| 405 | dd = der_d2 t add s 2 |
| 406 | if or lt mo 1 gt mo 12 |
| 407 | ret X5BIG |
| 408 | end |
| 409 | if or lt dd 1 gt dd x509_dim y mo |
| 410 | ret X5BIG |
| 411 | end |
| 412 | dy = x509_days y mo dd |
| 413 | ret add mul dy 86400 sec |
| 414 | end |
| 415 | |
| 416 | note ---------------------------------------------------------------- algorithm identifiers |
| 417 | |
| 418 | note x509_hashix: the AlgorithmIdentifier TLV that is exactly a, for sha256/384/512 with absent or NULL |
| 419 | note parameters: 256, 384 or 512; else 0 |
| 420 | fn x509_hashix a view uchar * to ulong |
| 421 | h = new ulong 3 0 |
| 422 | if ne der_hdr a 0 len a h 0 |
| 423 | ret 0 |
| 424 | end |
| 425 | if or ne get h 0 0x30 ne get h 2 len a |
| 426 | ret 0 |
| 427 | end |
| 428 | e = get h 2 |
| 429 | if ne der_hdr a get h 1 e h 0 |
| 430 | ret 0 |
| 431 | end |
| 432 | if ne get h 0 6 |
| 433 | ret 0 |
| 434 | end |
| 435 | o = der_oidix part a get h 1 sub get h 2 get h 1 |
| 436 | q = get h 2 |
| 437 | if ne q e |
| 438 | if not der_null a q e |
| 439 | ret 0 |
| 440 | end |
| 441 | end |
| 442 | if or lt o 12 gt o 14 |
| 443 | ret 0 |
| 444 | end |
| 445 | ret add 256 mul sub o 12 128 |
| 446 | end |
| 447 | |
| 448 | note x509_mgf: the [1] content of RSASSA-PSS-params: AlgorithmIdentifier {id-mgf1, hash}; the hash |
| 449 | note (256/384/512) or 0 |
| 450 | fn x509_mgf a view uchar * to ulong |
| 451 | h = new ulong 3 0 |
| 452 | if ne der_hdr a 0 len a h 0 |
| 453 | ret 0 |
| 454 | end |
| 455 | if or ne get h 0 0x30 ne get h 2 len a |
| 456 | ret 0 |
| 457 | end |
| 458 | e = get h 2 |
| 459 | if ne der_hdr a get h 1 e h 0 |
| 460 | ret 0 |
| 461 | end |
| 462 | if ne get h 0 6 |
| 463 | ret 0 |
| 464 | end |
| 465 | if ne der_oidix part a get h 1 sub get h 2 get h 1 15 |
| 466 | ret 0 |
| 467 | end |
| 468 | q = get h 2 |
| 469 | ret x509_hashix part a q sub e q |
| 470 | end |
| 471 | |
| 472 | note x509_pss: RSASSA-PSS-params TLV that is exactly p (RFC 4055 3.1). Allowed: hashAlgorithm |
| 473 | note sha256/384/512 present, maskGenAlgorithm MGF1 with the same hash, saltLength = hash length, |
| 474 | note trailerField absent. Returns sigalg 4/5/6, else 0 (unsupported). |
| 475 | fn x509_pss p view uchar * to ulong |
| 476 | h = new ulong 3 0 |
| 477 | if ne der_hdr p 0 len p h 0 |
| 478 | ret 0 |
| 479 | end |
| 480 | if or ne get h 0 0x30 ne get h 2 len p |
| 481 | ret 0 |
| 482 | end |
| 483 | e = get h 2 |
| 484 | if ne der_hdr p get h 1 e h 0 |
| 485 | ret 0 |
| 486 | end |
| 487 | if ne get h 0 0xa0 |
| 488 | ret 0 |
| 489 | end |
| 490 | hb = x509_hashix part p get h 1 sub get h 2 get h 1 |
| 491 | if ne der_hdr p get h 2 e h 0 |
| 492 | ret 0 |
| 493 | end |
| 494 | if ne get h 0 0xa1 |
| 495 | ret 0 |
| 496 | end |
| 497 | mg = x509_mgf part p get h 1 sub get h 2 get h 1 |
| 498 | if ne der_hdr p get h 2 e h 0 |
| 499 | ret 0 |
| 500 | end |
| 501 | if ne get h 0 0xa2 |
| 502 | ret 0 |
| 503 | end |
| 504 | sl = der_small part p get h 1 sub get h 2 get h 1 |
| 505 | if ne get h 2 e |
| 506 | ret 0 |
| 507 | end |
| 508 | if or eq hb 0 ne mg hb |
| 509 | ret 0 |
| 510 | end |
| 511 | if ne sl div hb 8 |
| 512 | ret 0 |
| 513 | end |
| 514 | ret add 4 div sub hb 256 128 |
| 515 | end |
| 516 | |
| 517 | note x509_algid: signature AlgorithmIdentifier TLV that is exactly a -> sigalg code: |
| 518 | note 1-3 sha256/384/512WithRSAEncryption (parameters NULL or absent, RFC 4055 5), 4-6 RSASSA-PSS |
| 519 | note SHA-256/384/512 (x509_pss), 7-9 ecdsa-with-SHA256/384/512 (parameters absent, RFC 5758 3.2); |
| 520 | note 0 for anything else (SHA-1, MD5, Ed25519, malformed parameters...) |
| 521 | fn x509_algid a view uchar * to ulong |
| 522 | h = new ulong 3 0 |
| 523 | if ne der_hdr a 0 len a h 0 |
| 524 | ret 0 |
| 525 | end |
| 526 | if or ne get h 0 0x30 ne get h 2 len a |
| 527 | ret 0 |
| 528 | end |
| 529 | e = get h 2 |
| 530 | if ne der_hdr a get h 1 e h 0 |
| 531 | ret 0 |
| 532 | end |
| 533 | if ne get h 0 6 |
| 534 | ret 0 |
| 535 | end |
| 536 | o = der_oidix part a get h 1 sub get h 2 get h 1 |
| 537 | q = get h 2 |
| 538 | if and ge o 1 le o 3 |
| 539 | if eq q e |
| 540 | ret o |
| 541 | end |
| 542 | if der_null a q e |
| 543 | ret o |
| 544 | end |
| 545 | ret 0 |
| 546 | end |
| 547 | if eq o 4 |
| 548 | if eq q e |
| 549 | ret 0 |
| 550 | end |
| 551 | ret x509_pss part a q sub e q |
| 552 | end |
| 553 | if and ge o 5 le o 7 |
| 554 | if eq q e |
| 555 | ret add o 2 |
| 556 | end |
| 557 | end |
| 558 | ret 0 |
| 559 | end |
| 560 | |
| 561 | note ---------------------------------------------------------------- certificate parser |
| 562 | |
| 563 | note x509_rsabits: RSAPublicKey TLV that is exactly k (RFC 8017 A.1.1): modulus bit count, plus |
| 564 | note X5RBADE when the public exponent is out of policy (x509_rsaexp); 0 when malformed |
| 565 | note (non-minimal or non-positive integers, trailing bytes) |
| 566 | fn x509_rsabits k view uchar * to ulong |
| 567 | h = new ulong 3 0 |
| 568 | if ne der_hdr k 0 len k h 0 |
| 569 | ret 0 |
| 570 | end |
| 571 | if or ne get h 0 0x30 ne get h 2 len k |
| 572 | ret 0 |
| 573 | end |
| 574 | e = get h 2 |
| 575 | if ne der_hdr k get h 1 e h 0 |
| 576 | ret 0 |
| 577 | end |
| 578 | if ne get h 0 2 |
| 579 | ret 0 |
| 580 | end |
| 581 | s = get h 1 |
| 582 | n = sub get h 2 s |
| 583 | if ne x509_posint part k s n 0 |
| 584 | ret 0 |
| 585 | end |
| 586 | if ne der_hdr k get h 2 e h 0 |
| 587 | ret 0 |
| 588 | end |
| 589 | if or ne get h 0 2 ne get h 2 e |
| 590 | ret 0 |
| 591 | end |
| 592 | if ne x509_posint part k get h 1 sub e get h 1 0 |
| 593 | ret 0 |
| 594 | end |
| 595 | x = x509_rsaexp part k get h 1 sub e get h 1 |
| 596 | if eq get k s 0 |
| 597 | s = add s 1 |
| 598 | n = sub n 1 |
| 599 | end |
| 600 | b = ulong get k s |
| 601 | bits = mul sub n 1 8 |
| 602 | while ne b 0 bound 9 |
| 603 | bits = add bits 1 |
| 604 | b = shr b 1 |
| 605 | end |
| 606 | if eq x 0 |
| 607 | bits = add bits X5RBADE |
| 608 | end |
| 609 | ret bits |
| 610 | end |
| 611 | |
| 612 | note x509_rsaexp: 1 when the public exponent content x (minimal, positive) is odd and in 3 .. 2^32-1 |
| 613 | fn x509_rsaexp x view uchar * to ulong |
| 614 | n = len x |
| 615 | if eq n 0 |
| 616 | ret 0 |
| 617 | end |
| 618 | s = ulong 0 |
| 619 | if eq get x 0 0 |
| 620 | s = 1 |
| 621 | end |
| 622 | if gt sub n s 4 |
| 623 | ret 0 |
| 624 | end |
| 625 | v = ulong 0 |
| 626 | for i ulong sub n s |
| 627 | v = or shl v 8 ulong get x add s i |
| 628 | end |
| 629 | if or lt v 3 eq and v 1 0 |
| 630 | ret 0 |
| 631 | end |
| 632 | ret 1 |
| 633 | end |
| 634 | |
| 635 | note x509_rsaok: 1 when the RSA bit count b (X5RBITS) is in 2048 .. 4096 and the exponent passed |
| 636 | note (policy for keys presented by the server; anchors only need >= 2048 bits, chain_sig) |
| 637 | fn x509_rsaok b ulong to ulong |
| 638 | if or lt b 2048 gt b 4096 |
| 639 | ret 0 |
| 640 | end |
| 641 | ret 1 |
| 642 | end |
| 643 | |
| 644 | note x509_posint: INTEGER content c is minimal and positive (0 ok, 1 not) |
| 645 | fn x509_posint c view uchar * to ulong |
| 646 | n = len c |
| 647 | if eq n 0 |
| 648 | ret 1 |
| 649 | end |
| 650 | b = ulong get c 0 |
| 651 | if ge b 128 |
| 652 | ret 1 |
| 653 | end |
| 654 | if eq b 0 |
| 655 | if eq n 1 |
| 656 | ret 1 |
| 657 | end |
| 658 | if lt ulong get c 1 128 |
| 659 | ret 1 |
| 660 | end |
| 661 | end |
| 662 | ret 0 |
| 663 | end |
| 664 | |
| 665 | note x509_spki: SubjectPublicKeyInfo TLV that is exactly c[a .. b). Key types: 1 rsaEncryption |
| 666 | note (parameters NULL; key = RSAPublicKey DER, bits recorded), 2 id-ecPublicKey namedCurve |
| 667 | note prime256v1 (uncompressed point, 65 bytes), 3 secp384r1 (97 bytes); 0 = any other key (refused |
| 668 | note when used). 0 ok, 1 malformed. |
| 669 | fn x509_spki c view uchar * a ulong b ulong r edit ulong * to ulong |
| 670 | h = new ulong 3 0 |
| 671 | if ne der_hdr c a b h 0 |
| 672 | ret 1 |
| 673 | end |
| 674 | if or ne get h 0 0x30 ne get h 2 b |
| 675 | ret 1 |
| 676 | end |
| 677 | if ne der_hdr c get h 1 b h 0 |
| 678 | ret 1 |
| 679 | end |
| 680 | if ne get h 0 0x30 |
| 681 | ret 1 |
| 682 | end |
| 683 | s = get h 2 |
| 684 | kt = x509_keyalg part c get h 1 sub get h 2 get h 1 |
| 685 | if eq kt 9 |
| 686 | ret 1 |
| 687 | end |
| 688 | if ne der_hdr c s b h 0 |
| 689 | ret 1 |
| 690 | end |
| 691 | if or ne get h 0 3 ne get h 2 b |
| 692 | ret 1 |
| 693 | end |
| 694 | q = get h 1 |
| 695 | if or ge q b ne get c q 0 |
| 696 | ret 1 |
| 697 | end |
| 698 | q = add q 1 |
| 699 | put r X5RKO q |
| 700 | put r X5RKL sub b q |
| 701 | put r X5RKT kt |
| 702 | ret x509_keyset c q b r |
| 703 | end |
| 704 | |
| 705 | note x509_keyalg: SPKI AlgorithmIdentifier content a -> key type 1 (rsaEncryption, NULL), 2 (EC |
| 706 | note prime256v1), 3 (EC secp384r1), 0 other, 9 malformed |
| 707 | fn x509_keyalg a view uchar * to ulong |
| 708 | h = new ulong 3 0 |
| 709 | e = len a |
| 710 | if ne der_hdr a 0 e h 0 |
| 711 | ret 9 |
| 712 | end |
| 713 | if ne get h 0 6 |
| 714 | ret 9 |
| 715 | end |
| 716 | o = der_oidix part a get h 1 sub get h 2 get h 1 |
| 717 | q = get h 2 |
| 718 | if eq o 8 |
| 719 | if der_null a q e |
| 720 | ret 1 |
| 721 | end |
| 722 | ret 0 |
| 723 | end |
| 724 | if ne o 9 |
| 725 | ret 0 |
| 726 | end |
| 727 | if ne der_hdr a q e h 0 |
| 728 | ret 9 |
| 729 | end |
| 730 | if or ne get h 0 6 ne get h 2 e |
| 731 | ret 0 |
| 732 | end |
| 733 | p = der_oidix part a get h 1 sub get h 2 get h 1 |
| 734 | if eq p 10 |
| 735 | ret 2 |
| 736 | end |
| 737 | if eq p 11 |
| 738 | ret 3 |
| 739 | end |
| 740 | ret 0 |
| 741 | end |
| 742 | |
| 743 | note x509_keyset: check the key bytes c[q .. b) for the key type in r[X5RKT] (see x509_spki); a |
| 744 | note bad EC point becomes key type 0; 0 ok, 1 malformed RSAPublicKey |
| 745 | fn x509_keyset c view uchar * q ulong b ulong r edit ulong * to ulong |
| 746 | kt = get r X5RKT |
| 747 | n = sub b q |
| 748 | if eq kt 1 |
| 749 | bits = x509_rsabits part c q n |
| 750 | if eq bits 0 |
| 751 | ret 1 |
| 752 | end |
| 753 | put r X5RBITS bits |
| 754 | end |
| 755 | if eq kt 2 |
| 756 | if or ne n 65 ne get c q 4 |
| 757 | put r X5RKT 0 |
| 758 | ret 0 |
| 759 | end |
| 760 | end |
| 761 | if eq kt 3 |
| 762 | if or ne n 97 ne get c q 4 |
| 763 | put r X5RKT 0 |
| 764 | ret 0 |
| 765 | end |
| 766 | end |
| 767 | put r X5RKT kt |
| 768 | ret 0 |
| 769 | end |
| 770 | |
| 771 | note x509_bc: basicConstraints extnValue content c[a .. b): SEQUENCE {cA BOOLEAN DEFAULT FALSE, |
| 772 | note pathLenConstraint INTEGER (0..MAX) OPTIONAL}. An explicit FALSE is tolerated (BER default). |
| 773 | fn x509_bc c view uchar * a ulong b ulong r edit ulong * to ulong |
| 774 | h = new ulong 3 0 |
| 775 | if ne der_hdr c a b h 0 |
| 776 | ret X5EMAL |
| 777 | end |
| 778 | if or ne get h 0 0x30 ne get h 2 b |
| 779 | ret X5EMAL |
| 780 | end |
| 781 | s = get h 1 |
| 782 | put r X5RBC 1 |
| 783 | if lt s b |
| 784 | if ne der_hdr c s b h 0 |
| 785 | ret X5EMAL |
| 786 | end |
| 787 | if eq get h 0 1 |
| 788 | if ne sub get h 2 get h 1 1 |
| 789 | ret X5EMAL |
| 790 | end |
| 791 | v = ulong get c get h 1 |
| 792 | if eq v 255 |
| 793 | put r X5RCA 1 |
| 794 | else |
| 795 | if ne v 0 |
| 796 | ret X5EMAL |
| 797 | end |
| 798 | end |
| 799 | s = get h 2 |
| 800 | end |
| 801 | end |
| 802 | if lt s b |
| 803 | if ne der_hdr c s b h 0 |
| 804 | ret X5EMAL |
| 805 | end |
| 806 | pl = der_small part c s sub get h 2 s |
| 807 | if eq pl X5BIG |
| 808 | ret X5EMAL |
| 809 | end |
| 810 | put r X5RPL pl |
| 811 | s = get h 2 |
| 812 | end |
| 813 | if ne s b |
| 814 | ret X5EMAL |
| 815 | end |
| 816 | ret 0 |
| 817 | end |
| 818 | |
| 819 | note x509_ku: keyUsage extnValue content c[a .. b): BIT STRING, at least one content byte, unused |
| 820 | note bits 0..7 and zero. The first content byte is recorded (0x80 digitalSignature, 0x04 keyCertSign). |
| 821 | fn x509_ku c view uchar * a ulong b ulong r edit ulong * to ulong |
| 822 | h = new ulong 3 0 |
| 823 | if ne der_hdr c a b h 0 |
| 824 | ret X5EMAL |
| 825 | end |
| 826 | if or ne get h 0 3 ne get h 2 b |
| 827 | ret X5EMAL |
| 828 | end |
| 829 | s = get h 1 |
| 830 | if lt sub b s 2 |
| 831 | ret X5EMAL |
| 832 | end |
| 833 | u = ulong get c s |
| 834 | if gt u 7 |
| 835 | ret X5EMAL |
| 836 | end |
| 837 | z = ulong get c sub b 1 |
| 838 | if ne and z sub shl ulong 1 u 1 0 |
| 839 | ret X5EMAL |
| 840 | end |
| 841 | put r X5RKU 1 |
| 842 | put r X5RKUB ulong get c add s 1 |
| 843 | ret 0 |
| 844 | end |
| 845 | |
| 846 | note x509_eku: extKeyUsage extnValue content c[a .. b): SEQUENCE SIZE (1..MAX) OF OBJECT IDENTIFIER |
| 847 | fn x509_eku c view uchar * a ulong b ulong r edit ulong * to ulong |
| 848 | h = new ulong 3 0 |
| 849 | if ne der_hdr c a b h 0 |
| 850 | ret X5EMAL |
| 851 | end |
| 852 | if or ne get h 0 0x30 ne get h 2 b |
| 853 | ret X5EMAL |
| 854 | end |
| 855 | s = get h 1 |
| 856 | if eq s b |
| 857 | ret X5EMAL |
| 858 | end |
| 859 | n = ulong 0 |
| 860 | while lt s b bound 10000 |
| 861 | n = add n 1 |
| 862 | if gt n X5NMAX |
| 863 | ret X5EMAL |
| 864 | end |
| 865 | if ne der_hdr c s b h 0 |
| 866 | ret X5EMAL |
| 867 | end |
| 868 | if ne get h 0 6 |
| 869 | ret X5EMAL |
| 870 | end |
| 871 | o = der_oidix part c get h 1 sub get h 2 get h 1 |
| 872 | if eq o 21 |
| 873 | put r X5RSRV 1 |
| 874 | end |
| 875 | if eq o 22 |
| 876 | put r X5RANY 1 |
| 877 | end |
| 878 | s = get h 2 |
| 879 | end |
| 880 | put r X5REKU 1 |
| 881 | ret 0 |
| 882 | end |
| 883 | |
| 884 | note x509_san: subjectAltName extnValue content c[a .. b): SEQUENCE SIZE (1..MAX) OF GeneralName, |
| 885 | note each a context-specific TLV; the GeneralNames content span is recorded |
| 886 | fn x509_san c view uchar * a ulong b ulong r edit ulong * to ulong |
| 887 | h = new ulong 3 0 |
| 888 | if ne der_hdr c a b h 0 |
| 889 | ret X5EMAL |
| 890 | end |
| 891 | if or ne get h 0 0x30 ne get h 2 b |
| 892 | ret X5EMAL |
| 893 | end |
| 894 | s = get h 1 |
| 895 | if eq s b |
| 896 | ret X5EMAL |
| 897 | end |
| 898 | put r X5RSNO s |
| 899 | put r X5RSNL sub b s |
| 900 | n = ulong 0 |
| 901 | while lt s b bound 10000 |
| 902 | n = add n 1 |
| 903 | if gt n X5NMAX |
| 904 | ret X5EMAL |
| 905 | end |
| 906 | if ne der_hdr c s b h 0 |
| 907 | ret X5EMAL |
| 908 | end |
| 909 | if ne and get h 0 0xc0 0x80 |
| 910 | ret X5EMAL |
| 911 | end |
| 912 | s = get h 2 |
| 913 | end |
| 914 | put r X5RSAN 1 |
| 915 | ret 0 |
| 916 | end |
| 917 | |
| 918 | note x509_ext: one Extension, content c[a .. b): {extnID, critical BOOLEAN DEFAULT FALSE, extnValue}. |
| 919 | note Known: basicConstraints, keyUsage, extKeyUsage, subjectAltName (each at most once). A |
| 920 | note nameConstraints extension, critical or not, is X5ENC; any other unknown critical one X5ECRIT. |
| 921 | fn x509_ext c view uchar * a ulong b ulong r edit ulong * to ulong |
| 922 | h = new ulong 3 0 |
| 923 | if ne der_hdr c a b h 0 |
| 924 | ret X5EMAL |
| 925 | end |
| 926 | if ne get h 0 6 |
| 927 | ret X5EMAL |
| 928 | end |
| 929 | o = der_oidix part c get h 1 sub get h 2 get h 1 |
| 930 | if ne der_hdr c get h 2 b h 0 |
| 931 | ret X5EMAL |
| 932 | end |
| 933 | cr = ulong 0 |
| 934 | if eq get h 0 1 |
| 935 | if ne sub get h 2 get h 1 1 |
| 936 | ret X5EMAL |
| 937 | end |
| 938 | v = ulong get c get h 1 |
| 939 | if eq v 255 |
| 940 | cr = 1 |
| 941 | else |
| 942 | if ne v 0 |
| 943 | ret X5EMAL |
| 944 | end |
| 945 | end |
| 946 | if ne der_hdr c get h 2 b h 0 |
| 947 | ret X5EMAL |
| 948 | end |
| 949 | end |
| 950 | if or ne get h 0 4 ne get h 2 b |
| 951 | ret X5EMAL |
| 952 | end |
| 953 | if eq o 20 |
| 954 | ret X5ENC |
| 955 | end |
| 956 | if or lt o 16 gt o 19 |
| 957 | if ne cr 0 |
| 958 | ret X5ECRIT |
| 959 | end |
| 960 | ret 0 |
| 961 | end |
| 962 | m = shl ulong 1 sub o 16 |
| 963 | if ne and get r X5RSEEN m 0 |
| 964 | ret X5EMAL |
| 965 | end |
| 966 | put r X5RSEEN or get r X5RSEEN m |
| 967 | s = get h 1 |
| 968 | if eq o 16 |
| 969 | ret x509_bc c s b r |
| 970 | end |
| 971 | if eq o 17 |
| 972 | ret x509_ku c s b r |
| 973 | end |
| 974 | if eq o 18 |
| 975 | ret x509_eku c s b r |
| 976 | end |
| 977 | ret x509_san c s b r |
| 978 | end |
| 979 | |
| 980 | note x509_exts: [3] content c[a .. b): Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 981 | fn x509_exts c view uchar * a ulong b ulong r edit ulong * to ulong |
| 982 | h = new ulong 3 0 |
| 983 | if ne der_hdr c a b h 0 |
| 984 | ret X5EMAL |
| 985 | end |
| 986 | if or ne get h 0 0x30 ne get h 2 b |
| 987 | ret X5EMAL |
| 988 | end |
| 989 | s = get h 1 |
| 990 | if eq s b |
| 991 | ret X5EMAL |
| 992 | end |
| 993 | n = ulong 0 |
| 994 | while lt s b bound 10000 |
| 995 | n = add n 1 |
| 996 | if gt n X5NMAX |
| 997 | ret X5EMAL |
| 998 | end |
| 999 | if ne der_hdr c s b h 0 |
| 1000 | ret X5EMAL |
| 1001 | end |
| 1002 | if ne get h 0 0x30 |
| 1003 | ret X5EMAL |
| 1004 | end |
| 1005 | v = x509_ext c get h 1 get h 2 r |
| 1006 | if ne v 0 |
| 1007 | ret v |
| 1008 | end |
| 1009 | s = get h 2 |
| 1010 | end |
| 1011 | ret 0 |
| 1012 | end |
| 1013 | |
| 1014 | note x509_valid: Validity TLV that is exactly c[a .. b): notBefore, notAfter |
| 1015 | fn x509_valid c view uchar * a ulong b ulong r edit ulong * to ulong |
| 1016 | h = new ulong 3 0 |
| 1017 | if ne der_hdr c a b h 0 |
| 1018 | ret 1 |
| 1019 | end |
| 1020 | if or ne get h 0 0x30 ne get h 2 b |
| 1021 | ret 1 |
| 1022 | end |
| 1023 | s = get h 1 |
| 1024 | if ne der_hdr c s b h 0 |
| 1025 | ret 1 |
| 1026 | end |
| 1027 | nb = der_time part c s sub get h 2 s |
| 1028 | s = get h 2 |
| 1029 | if ne der_hdr c s b h 0 |
| 1030 | ret 1 |
| 1031 | end |
| 1032 | if ne get h 2 b |
| 1033 | ret 1 |
| 1034 | end |
| 1035 | na = der_time part c s sub b s |
| 1036 | if or eq nb X5BIG eq na X5BIG |
| 1037 | ret 1 |
| 1038 | end |
| 1039 | put r X5RNB nb |
| 1040 | put r X5RNA na |
| 1041 | ret 0 |
| 1042 | end |
| 1043 | |
| 1044 | note x509_tbs2: tbsCertificate from validity on, c[s .. b): validity, subject, SPKI, optional unique |
| 1045 | note IDs (v2, v3: at most one issuerUniqueID [1], then at most one subjectUniqueID [2]) and |
| 1046 | note extensions (v3 only) |
| 1047 | fn x509_tbs2 c view uchar * s ulong b ulong r edit ulong * to ulong |
| 1048 | h = new ulong 3 0 |
| 1049 | if ne der_hdr c s b h 0 |
| 1050 | ret X5EMAL |
| 1051 | end |
| 1052 | if ne x509_valid c s get h 2 r 0 |
| 1053 | ret X5EMAL |
| 1054 | end |
| 1055 | s = get h 2 |
| 1056 | if ne der_hdr c s b h 0 |
| 1057 | ret X5EMAL |
| 1058 | end |
| 1059 | if ne get h 0 0x30 |
| 1060 | ret X5EMAL |
| 1061 | end |
| 1062 | put r X5RSBO s |
| 1063 | put r X5RSBL sub get h 2 s |
| 1064 | s = get h 2 |
| 1065 | if ne der_hdr c s b h 0 |
| 1066 | ret X5EMAL |
| 1067 | end |
| 1068 | if ne x509_spki c s get h 2 r 0 |
| 1069 | ret X5EMAL |
| 1070 | end |
| 1071 | s = get h 2 |
| 1072 | v = get r X5RVER |
| 1073 | k = ulong 0x80 |
| 1074 | while lt s b bound 4 |
| 1075 | if ne der_hdr c s b h 0 |
| 1076 | ret X5EMAL |
| 1077 | end |
| 1078 | g = get h 0 |
| 1079 | if or eq g 0x81 eq g 0x82 |
| 1080 | if or eq v 0 le g k |
| 1081 | ret X5EMAL |
| 1082 | end |
| 1083 | k = g |
| 1084 | s = get h 2 |
| 1085 | continue |
| 1086 | end |
| 1087 | if or ne g 0xa3 ne v 2 |
| 1088 | ret X5EMAL |
| 1089 | end |
| 1090 | if ne get h 2 b |
| 1091 | ret X5EMAL |
| 1092 | end |
| 1093 | ret x509_exts c get h 1 b r |
| 1094 | end |
| 1095 | if ne s b |
| 1096 | ret X5EMAL |
| 1097 | end |
| 1098 | ret 0 |
| 1099 | end |
| 1100 | |
| 1101 | note x509_tbs: tbsCertificate content c[a .. b): version (explicit [0], 1 or 2 when present), |
| 1102 | note serialNumber (1..21 content bytes), signature AlgorithmIdentifier, issuer Name, then x509_tbs2 |
| 1103 | fn x509_tbs c view uchar * a ulong b ulong r edit ulong * to ulong |
| 1104 | h = new ulong 3 0 |
| 1105 | s = a |
| 1106 | if ne der_hdr c s b h 0 |
| 1107 | ret X5EMAL |
| 1108 | end |
| 1109 | if eq get h 0 0xa0 |
| 1110 | v = der_small part c get h 1 sub get h 2 get h 1 |
| 1111 | if or eq v 0 gt v 2 |
| 1112 | ret X5EMAL |
| 1113 | end |
| 1114 | put r X5RVER v |
| 1115 | s = get h 2 |
| 1116 | if ne der_hdr c s b h 0 |
| 1117 | ret X5EMAL |
| 1118 | end |
| 1119 | end |
| 1120 | n = sub get h 2 get h 1 |
| 1121 | if or ne get h 0 2 or eq n 0 gt n 21 |
| 1122 | ret X5EMAL |
| 1123 | end |
| 1124 | s = get h 2 |
| 1125 | if ne der_hdr c s b h 0 |
| 1126 | ret X5EMAL |
| 1127 | end |
| 1128 | if ne get h 0 0x30 |
| 1129 | ret X5EMAL |
| 1130 | end |
| 1131 | put r X5RTAO s |
| 1132 | put r X5RTAL sub get h 2 s |
| 1133 | s = get h 2 |
| 1134 | if ne der_hdr c s b h 0 |
| 1135 | ret X5EMAL |
| 1136 | end |
| 1137 | if ne get h 0 0x30 |
| 1138 | ret X5EMAL |
| 1139 | end |
| 1140 | put r X5RISO s |
| 1141 | put r X5RISL sub get h 2 s |
| 1142 | ret x509_tbs2 c get h 2 b r |
| 1143 | end |
| 1144 | |
| 1145 | note x509_parse: the Certificate that is exactly c (RFC 5280 4.1) into the record r (len r >= X5RSZ, |
| 1146 | note offsets relative to c). 0 ok, else X5EMAL, X5ECRIT or X5EAMIS. An unsupported signature |
| 1147 | note algorithm or key type is recorded (sigalg 0, key type 0), not refused here. |
| 1148 | fn x509_parse c view uchar * r edit ulong * to ulong |
| 1149 | h = new ulong 3 0 |
| 1150 | for i ulong X5RSZ |
| 1151 | put r i 0 |
| 1152 | end |
| 1153 | put r X5RPL X5BIG |
| 1154 | if ne der_hdr c 0 len c h 0 |
| 1155 | ret X5EMAL |
| 1156 | end |
| 1157 | if or ne get h 0 0x30 ne get h 2 len c |
| 1158 | ret X5EMAL |
| 1159 | end |
| 1160 | e = len c |
| 1161 | s = get h 1 |
| 1162 | if ne der_hdr c s e h 0 |
| 1163 | ret X5EMAL |
| 1164 | end |
| 1165 | if ne get h 0 0x30 |
| 1166 | ret X5EMAL |
| 1167 | end |
| 1168 | put r X5RTBO s |
| 1169 | put r X5RTBL sub get h 2 s |
| 1170 | tc = get h 1 |
| 1171 | s = get h 2 |
| 1172 | if ne der_hdr c s e h 0 |
| 1173 | ret X5EMAL |
| 1174 | end |
| 1175 | if ne get h 0 0x30 |
| 1176 | ret X5EMAL |
| 1177 | end |
| 1178 | put r X5RAO s |
| 1179 | put r X5RAL sub get h 2 s |
| 1180 | put r X5RALG x509_algid part c s sub get h 2 s |
| 1181 | s = get h 2 |
| 1182 | if ne der_hdr c s e h 0 |
| 1183 | ret X5EMAL |
| 1184 | end |
| 1185 | if or ne get h 0 3 ne get h 2 e |
| 1186 | ret X5EMAL |
| 1187 | end |
| 1188 | q = get h 1 |
| 1189 | if or ge q e ne get c q 0 |
| 1190 | ret X5EMAL |
| 1191 | end |
| 1192 | put r X5RSGO add q 1 |
| 1193 | put r X5RSGL sub sub e q 1 |
| 1194 | v = x509_tbs c tc add get r X5RTBO get r X5RTBL r |
| 1195 | if ne v 0 |
| 1196 | ret v |
| 1197 | end |
| 1198 | if ne get r X5RAL get r X5RTAL |
| 1199 | ret X5EAMIS |
| 1200 | end |
| 1201 | if not der_eqat c get r X5RAO get r X5RTAO get r X5RAL |
| 1202 | ret X5EAMIS |
| 1203 | end |
| 1204 | ret 0 |
| 1205 | end |
| 1206 | |
| 1207 | note ---------------------------------------------------------------- PEM trust store |
| 1208 | |
| 1209 | note b64_val: value of a base64 character (RFC 4648 4), 64 for '=', 99 for anything else |
| 1210 | fn b64_val c ulong to ulong |
| 1211 | if and ge c 65 le c 90 |
| 1212 | ret sub c 65 |
| 1213 | end |
| 1214 | if and ge c 97 le c 122 |
| 1215 | ret sub c 71 |
| 1216 | end |
| 1217 | if and ge c 48 le c 57 |
| 1218 | ret add c 4 |
| 1219 | end |
| 1220 | if eq c 43 |
| 1221 | ret 62 |
| 1222 | end |
| 1223 | if eq c 47 |
| 1224 | ret 63 |
| 1225 | end |
| 1226 | if eq c 61 |
| 1227 | ret 64 |
| 1228 | end |
| 1229 | ret 99 |
| 1230 | end |
| 1231 | |
| 1232 | note b64_push: one base64 character c of a certificate block. s: 0 write position, 3 accumulator, |
| 1233 | note 4 characters in the quartet, 5 '=' seen, 6 block finished by padding. Strict: no character |
| 1234 | note after padding, '=' only as the last one or two of a quartet, unused bits zero. 0 or -4098. |
| 1235 | fn b64_push st edit uchar * c ulong s edit ulong * to long |
| 1236 | v = b64_val c |
| 1237 | if or eq v 99 eq get s 6 1 |
| 1238 | ret -4098 |
| 1239 | end |
| 1240 | acc = get s 3 |
| 1241 | if eq v 64 |
| 1242 | if lt get s 4 2 |
| 1243 | ret -4098 |
| 1244 | end |
| 1245 | put s 5 add get s 5 1 |
| 1246 | acc = shl acc 6 |
| 1247 | else |
| 1248 | if gt get s 5 0 |
| 1249 | ret -4098 |
| 1250 | end |
| 1251 | acc = or shl acc 6 v |
| 1252 | end |
| 1253 | put s 3 acc |
| 1254 | put s 4 add get s 4 1 |
| 1255 | if lt get s 4 4 |
| 1256 | ret 0 |
| 1257 | end |
| 1258 | pad = get s 5 |
| 1259 | w = get s 0 |
| 1260 | if and eq pad 1 ne and acc 255 0 |
| 1261 | ret -4098 |
| 1262 | end |
| 1263 | if and eq pad 2 ne and acc 65535 0 |
| 1264 | ret -4098 |
| 1265 | end |
| 1266 | put st w uchar shr acc 16 |
| 1267 | if lt pad 2 |
| 1268 | put st add w 1 uchar and shr acc 8 255 |
| 1269 | end |
| 1270 | if lt pad 1 |
| 1271 | put st add w 2 uchar and acc 255 |
| 1272 | end |
| 1273 | put s 0 add w sub 3 pad |
| 1274 | put s 3 0 |
| 1275 | put s 4 0 |
| 1276 | if gt pad 0 |
| 1277 | put s 6 1 |
| 1278 | end |
| 1279 | ret 0 |
| 1280 | end |
| 1281 | |
| 1282 | note pem_end: close the block: complete quartets, one SEQUENCE TLV of exactly the decoded length, |
| 1283 | note then the 3-byte big-endian length is written in front of it |
| 1284 | fn pem_end st edit uchar * s edit ulong * to long |
| 1285 | h = new ulong 3 0 |
| 1286 | if ne get s 4 0 |
| 1287 | ret -4098 |
| 1288 | end |
| 1289 | o = add get s 2 3 |
| 1290 | n = sub get s 0 o |
| 1291 | if or eq n 0 ge n 16777216 |
| 1292 | ret -4099 |
| 1293 | end |
| 1294 | if ne der_hdr view part st o n 0 n h 0 |
| 1295 | ret -4099 |
| 1296 | end |
| 1297 | if or ne get h 0 0x30 ne get h 2 n |
| 1298 | ret -4099 |
| 1299 | end |
| 1300 | w = get s 2 |
| 1301 | put st w uchar shr n 16 |
| 1302 | put st add w 1 uchar and shr n 8 255 |
| 1303 | put st add w 2 uchar and n 255 |
| 1304 | put s 1 0 |
| 1305 | ret 0 |
| 1306 | end |
| 1307 | |
| 1308 | note pem_line: one line st[a .. b) (no line end). Outside a block only the exact BEGIN CERTIFICATE |
| 1309 | note line matters (other lines are ignored); inside, the exact END line or base64 characters only. |
| 1310 | fn pem_line st edit uchar * a ulong b ulong s edit ulong * to long |
| 1311 | if eq get s 1 0 |
| 1312 | if der_same view part st a sub b a X5BEG |
| 1313 | put s 1 1 |
| 1314 | put s 2 get s 0 |
| 1315 | put s 0 add get s 0 3 |
| 1316 | put s 3 0 |
| 1317 | put s 4 0 |
| 1318 | put s 5 0 |
| 1319 | put s 6 0 |
| 1320 | end |
| 1321 | ret 0 |
| 1322 | end |
| 1323 | if der_same view part st a sub b a X5END |
| 1324 | ret pem_end st s |
| 1325 | end |
| 1326 | for i ulong sub b a |
| 1327 | r = b64_push st ulong get st add a i s |
| 1328 | if ne r 0 |
| 1329 | ret r |
| 1330 | end |
| 1331 | end |
| 1332 | ret 0 |
| 1333 | end |
| 1334 | |
| 1335 | note pem_decode: decode the PEM text st[0 .. n) in place into the store format (repeated 3-byte length |
| 1336 | note || certificate DER; a zero length or the array end terminates; 3 zero bytes are written after |
| 1337 | note the last entry when they fit), return the |
| 1338 | note bytes used. In place is safe: the write position stays below the read position (each block |
| 1339 | note consumes its 28-byte BEGIN line before 3 header bytes are written, then 4 characters per <= 3 |
| 1340 | note bytes). Codes: -4098 bad base64 or framing, -4099 a block that is not one DER SEQUENCE, -4100 |
| 1341 | note n > len st. Lines end with LF; one CR before the LF is dropped. |
| 1342 | fn pem_decode st edit uchar * n ulong to long |
| 1343 | s = new ulong 8 0 |
| 1344 | if gt n len st |
| 1345 | ret -4100 |
| 1346 | end |
| 1347 | a = ulong 0 |
| 1348 | while lt a n bound 999999 |
| 1349 | e = a |
| 1350 | while lt e n bound 999999 |
| 1351 | if eq get st e 10 |
| 1352 | break |
| 1353 | end |
| 1354 | e = add e 1 |
| 1355 | end |
| 1356 | b = e |
| 1357 | if gt b a |
| 1358 | if eq get st sub b 1 13 |
| 1359 | b = sub b 1 |
| 1360 | end |
| 1361 | end |
| 1362 | r = pem_line st a b s |
| 1363 | if ne r 0 |
| 1364 | ret r |
| 1365 | end |
| 1366 | a = add e 1 |
| 1367 | end |
| 1368 | if ne get s 1 0 |
| 1369 | ret -4098 |
| 1370 | end |
| 1371 | w = get s 0 |
| 1372 | if le add w 3 len st |
| 1373 | put st w 0 |
| 1374 | put st add w 1 0 |
| 1375 | put st add w 2 0 |
| 1376 | end |
| 1377 | ret long w |
| 1378 | end |
| 1379 | |
| 1380 | note x509_store_loadf: read the PEM bundle at path into st and decode it (pem_decode). Returns the |
| 1381 | note bytes used, -errno for open/read errors, -4097 when the file does not fit in st, or a |
| 1382 | note pem_decode code. |
| 1383 | fn x509_store_loadf st edit uchar * path view uchar * to long |
| 1384 | one = new uchar 1 0 |
| 1385 | fd = fopen path 0 len path 524288 0 |
| 1386 | if lt fd 0 |
| 1387 | ret fd |
| 1388 | end |
| 1389 | n = ulong 0 |
| 1390 | z = long 0 |
| 1391 | while lt n len st bound 999999 |
| 1392 | r = read int fd st n sub len st n |
| 1393 | if lt r 0 |
| 1394 | z = fclose int fd |
| 1395 | ret r |
| 1396 | end |
| 1397 | if eq r 0 |
| 1398 | break |
| 1399 | end |
| 1400 | n = add n ulong r |
| 1401 | end |
| 1402 | if eq n len st |
| 1403 | r = read int fd one 0 1 |
| 1404 | if ne r 0 |
| 1405 | z = fclose int fd |
| 1406 | ret -4097 |
| 1407 | end |
| 1408 | end |
| 1409 | z = fclose int fd |
| 1410 | ret pem_decode st n |
| 1411 | end |
| 1412 | |
| 1413 | note x509_store_load: the system bundle /etc/ssl/certs/ca-certificates.crt (see x509_store_loadf) |
| 1414 | fn x509_store_load st edit uchar * to long |
| 1415 | ret x509_store_loadf st X5PATH |
| 1416 | end |
| 1417 | |
| 1418 | note x509_errname: ASCII name of a reject code into o (truncated to len o); its length, 0 if unknown |
| 1419 | fn x509_errname code ulong o edit uchar * to ulong |
| 1420 | s = ulong 0 |
| 1421 | k = ulong 0 |
| 1422 | n = len X5NAMES |
| 1423 | while lt s n bound 100 |
| 1424 | j = s |
| 1425 | while lt j n bound 100 |
| 1426 | if eq get X5NAMES j 32 |
| 1427 | break |
| 1428 | end |
| 1429 | j = add j 1 |
| 1430 | end |
| 1431 | if eq k code |
| 1432 | m = sub j s |
| 1433 | for i ulong m |
| 1434 | if lt i len o |
| 1435 | put o i get X5NAMES add s i |
| 1436 | end |
| 1437 | end |
| 1438 | ret m |
| 1439 | end |
| 1440 | s = add j 1 |
| 1441 | k = add k 1 |
| 1442 | end |
| 1443 | ret 0 |
| 1444 | end |
| 1445 | |
| 1446 | note ---------------------------------------------------------------- chain validation |
| 1447 | |
| 1448 | note chain_index: the TLS 1.3 certificate_list contents cl (RFC 8446 4.4.2: uint24 length, DER, |
| 1449 | note uint16 extensions length, extensions) into ix (offset, length pairs). Entry count, or 0 when the |
| 1450 | note framing is bad, an entry is empty, or there are more than len ix / 2 entries. |
| 1451 | fn chain_index cl view uchar * ix edit ulong * to ulong |
| 1452 | p = ulong 0 |
| 1453 | n = ulong 0 |
| 1454 | e = len cl |
| 1455 | while lt p e bound 100000 |
| 1456 | if lt sub e p 3 |
| 1457 | ret 0 |
| 1458 | end |
| 1459 | l = der_u24 cl p |
| 1460 | p = add p 3 |
| 1461 | if or eq l 0 gt l sub e p |
| 1462 | ret 0 |
| 1463 | end |
| 1464 | if ge n div len ix 2 |
| 1465 | ret 0 |
| 1466 | end |
| 1467 | put ix mul n 2 p |
| 1468 | put ix add mul n 2 1 l |
| 1469 | p = add p l |
| 1470 | if lt sub e p 2 |
| 1471 | ret 0 |
| 1472 | end |
| 1473 | x = or shl ulong get cl p 8 ulong get cl add p 1 |
| 1474 | p = add p 2 |
| 1475 | if gt x sub e p |
| 1476 | ret 0 |
| 1477 | end |
| 1478 | p = add p x |
| 1479 | n = add n 1 |
| 1480 | end |
| 1481 | ret n |
| 1482 | end |
| 1483 | |
| 1484 | note chain_hostok: host must be an LDH name: 1..253 bytes, labels 1..63 of letters, digits and '-', |
| 1485 | note no empty label, no trailing dot. All digits and dots, or any ':', is an IP literal (X5EIP). |
| 1486 | fn chain_hostok h view uchar * to ulong |
| 1487 | n = len h |
| 1488 | if or eq n 0 gt n 253 |
| 1489 | ret X5ENAME |
| 1490 | end |
| 1491 | dig = ulong 1 |
| 1492 | lab = ulong 0 |
| 1493 | for i ulong n |
| 1494 | c = ulong get h i |
| 1495 | if eq c 58 |
| 1496 | ret X5EIP |
| 1497 | end |
| 1498 | if eq c 46 |
| 1499 | if eq lab 0 |
| 1500 | ret X5ENAME |
| 1501 | end |
| 1502 | lab = 0 |
| 1503 | continue |
| 1504 | end |
| 1505 | if or lt c 48 gt c 57 |
| 1506 | dig = 0 |
| 1507 | lc = chain_low c |
| 1508 | if and or lt lc 97 gt lc 122 ne c 45 |
| 1509 | ret X5ENAME |
| 1510 | end |
| 1511 | end |
| 1512 | lab = add lab 1 |
| 1513 | if gt lab 63 |
| 1514 | ret X5ENAME |
| 1515 | end |
| 1516 | end |
| 1517 | if eq dig 1 |
| 1518 | ret X5EIP |
| 1519 | end |
| 1520 | if eq lab 0 |
| 1521 | ret X5ENAME |
| 1522 | end |
| 1523 | ret 0 |
| 1524 | end |
| 1525 | |
| 1526 | note chain_low: ASCII lower case |
| 1527 | fn chain_low c ulong to ulong |
| 1528 | if and ge c 65 le c 90 |
| 1529 | ret add c 32 |
| 1530 | end |
| 1531 | ret c |
| 1532 | end |
| 1533 | |
| 1534 | note chain_ieq: equal spans under ASCII case folding |
| 1535 | fn chain_ieq a view uchar * b view uchar * to bool |
| 1536 | if ne len a len b |
| 1537 | ret false |
| 1538 | end |
| 1539 | for i ulong len a |
| 1540 | if ne chain_low ulong get a i chain_low ulong get b i |
| 1541 | ret false |
| 1542 | end |
| 1543 | end |
| 1544 | ret true |
| 1545 | end |
| 1546 | |
| 1547 | note chain_match: SAN dNSName pattern p against host h (RFC 6125 6.4). Exact match ignoring ASCII |
| 1548 | note case; or p = "*." rest where rest has >= 2 non-empty labels and no '*': the '*' stands for the |
| 1549 | note whole leftmost label of h (one non-empty label, never several, never the bare parent). |
| 1550 | note Partial wildcards ("f*.example.com") never match. |
| 1551 | fn chain_match p view uchar * h view uchar * to bool |
| 1552 | np = len p |
| 1553 | nh = len h |
| 1554 | if eq np 0 |
| 1555 | ret false |
| 1556 | end |
| 1557 | if ne get p 0 42 |
| 1558 | ret chain_ieq p h |
| 1559 | end |
| 1560 | if or lt np 5 ne get p 1 46 |
| 1561 | ret false |
| 1562 | end |
| 1563 | dots = ulong 0 |
| 1564 | lab = ulong 0 |
| 1565 | for i ulong sub np 2 |
| 1566 | c = get p add i 2 |
| 1567 | if eq c 42 |
| 1568 | ret false |
| 1569 | end |
| 1570 | if eq c 46 |
| 1571 | if eq lab 0 |
| 1572 | ret false |
| 1573 | end |
| 1574 | dots = add dots 1 |
| 1575 | lab = 0 |
| 1576 | else |
| 1577 | lab = add lab 1 |
| 1578 | end |
| 1579 | end |
| 1580 | if or eq lab 0 eq dots 0 |
| 1581 | ret false |
| 1582 | end |
| 1583 | k = ulong 0 |
| 1584 | while lt k nh bound 300 |
| 1585 | if eq get h k 46 |
| 1586 | break |
| 1587 | end |
| 1588 | k = add k 1 |
| 1589 | end |
| 1590 | if or eq k 0 ge k nh |
| 1591 | ret false |
| 1592 | end |
| 1593 | ret chain_ieq part p 1 sub np 1 part h k sub nh k |
| 1594 | end |
| 1595 | |
| 1596 | note chain_host: host h against the SAN dNSNames of the certificate c (record r). No SAN: X5ENSAN |
| 1597 | note (no subject CN fallback); no matching dNSName: X5ENAME. |
| 1598 | fn chain_host c view uchar * r view ulong * h view uchar * to ulong |
| 1599 | hd = new ulong 3 0 |
| 1600 | if eq get r X5RSAN 0 |
| 1601 | ret X5ENSAN |
| 1602 | end |
| 1603 | s = get r X5RSNO |
| 1604 | e = add s get r X5RSNL |
| 1605 | n = ulong 0 |
| 1606 | while lt s e bound 10000 |
| 1607 | n = add n 1 |
| 1608 | if gt n X5NMAX |
| 1609 | ret X5EMAL |
| 1610 | end |
| 1611 | if ne der_hdr c s e hd 0 |
| 1612 | ret X5EMAL |
| 1613 | end |
| 1614 | if eq get hd 0 0x82 |
| 1615 | if chain_match part c get hd 1 sub get hd 2 get hd 1 h |
| 1616 | ret 0 |
| 1617 | end |
| 1618 | end |
| 1619 | s = get hd 2 |
| 1620 | end |
| 1621 | ret X5ENAME |
| 1622 | end |
| 1623 | |
| 1624 | note chain_time: validity of record r at nw (seconds since 0000-03-01, as der_time): valid iff |
| 1625 | note notBefore <= nw < notAfter. RFC 5280 4.1.2.5 includes notAfter; OpenSSL does not, and the |
| 1626 | note stricter bound is kept so that this client never accepts what OpenSSL refuses. |
| 1627 | fn chain_time r view ulong * nw ulong to ulong |
| 1628 | if gt get r X5RNB nw |
| 1629 | ret X5ENYV |
| 1630 | end |
| 1631 | if le get r X5RNA nw |
| 1632 | ret X5EEXP |
| 1633 | end |
| 1634 | ret 0 |
| 1635 | end |
| 1636 | |
| 1637 | note chain_hash: the hash (256/384/512) of sigalg a (1..9) |
| 1638 | fn chain_hash a ulong to ulong |
| 1639 | ret add 256 mul rem sub a 1 3 128 |
| 1640 | end |
| 1641 | |
| 1642 | note chain_sig: verify the signature of the certificate cd (record rr[0 .. 32)) with the key of the |
| 1643 | note certificate kd (record rr[32 .. 64)). Allowed: sigalg 1-6 with an rsaEncryption key of >= 2048 |
| 1644 | note bits, sigalg 7-9 with a P-256 or P-384 key. 0, X5EALG, X5EWEAK or X5ESIG. |
| 1645 | fn chain_sig cd view uchar * kd view uchar * rr view ulong * to ulong |
| 1646 | p = new ulong 10 0 |
| 1647 | a = get rr X5RALG |
| 1648 | kt = get rr add X5RSZ X5RKT |
| 1649 | if or eq a 0 gt a 9 |
| 1650 | ret X5EALG |
| 1651 | end |
| 1652 | if le a 6 |
| 1653 | if ne kt 1 |
| 1654 | ret X5EALG |
| 1655 | end |
| 1656 | if lt get rr add X5RSZ X5RBITS 2048 |
| 1657 | ret X5EWEAK |
| 1658 | end |
| 1659 | else |
| 1660 | if or lt kt 2 gt kt 3 |
| 1661 | ret X5EALG |
| 1662 | end |
| 1663 | end |
| 1664 | put p 0 add div sub a 1 3 1 |
| 1665 | put p 1 chain_hash a |
| 1666 | put p 2 get rr add X5RSZ X5RKO |
| 1667 | put p 3 get rr add X5RSZ X5RKL |
| 1668 | put p 4 kt |
| 1669 | put p 6 get rr X5RTBO |
| 1670 | put p 7 get rr X5RTBL |
| 1671 | put p 8 get rr X5RSGO |
| 1672 | put p 9 get rr X5RSGL |
| 1673 | ret chain_hsig cd kd edit p |
| 1674 | end |
| 1675 | |
| 1676 | note chain_hsig: digest of cd[p[6] .. +p[7]) with hash p[1], then chain_vfy of digest || signature |
| 1677 | note cd[p[8] .. +p[9]) (at most 1024 bytes, else X5ESIG) with the key in kd |
| 1678 | fn chain_hsig cd view uchar * kd view uchar * p edit ulong * to ulong |
| 1679 | hs = new uchar 1100 0 |
| 1680 | sl = get p 9 |
| 1681 | if or eq sl 0 gt sl 1024 |
| 1682 | ret X5ESIG |
| 1683 | end |
| 1684 | put p 5 sha2_digest get p 1 part cd get p 6 get p 7 edit hs |
| 1685 | dl = get p 5 |
| 1686 | so = get p 8 |
| 1687 | for i ulong sl |
| 1688 | put hs add dl i get cd add so i |
| 1689 | end |
| 1690 | dl = add dl sl |
| 1691 | so = get p 2 |
| 1692 | sl = get p 3 |
| 1693 | ret chain_vfy part kd so sl view part hs 0 dl view p |
| 1694 | end |
| 1695 | |
| 1696 | note chain_vfy: hs = digest (p[5] bytes) || signature, key ky (RSAPublicKey DER or SEC1 point); |
| 1697 | note p[0] 1 PKCS#1 v1.5, 2 PSS, 3 ECDSA; p[1] hash, p[4] key type. 0 or X5ESIG. |
| 1698 | fn chain_vfy ky view uchar * hs view uchar * p view ulong * to ulong |
| 1699 | k = get p 0 |
| 1700 | hb = get p 1 |
| 1701 | dl = get p 5 |
| 1702 | v = ulong 1 |
| 1703 | if eq k 1 |
| 1704 | v = rsa_verify_pkcs1 hb ky hs dl |
| 1705 | end |
| 1706 | if eq k 2 |
| 1707 | v = rsa_verify_pss chain_ctl hb ky hs dl |
| 1708 | end |
| 1709 | if eq k 3 |
| 1710 | v = ecdsa_verify chain_crv get p 4 ky hs dl |
| 1711 | end |
| 1712 | if ne v 0 |
| 1713 | ret X5ESIG |
| 1714 | end |
| 1715 | ret 0 |
| 1716 | end |
| 1717 | |
| 1718 | note chain_ctl: rsa.om PSS control word for hash hb: hash | MGF1 hash << 16 | salt (= hash bytes) << 32 |
| 1719 | fn chain_ctl hb ulong to ulong |
| 1720 | ret or or hb shl hb 16 shl div hb 8 32 |
| 1721 | end |
| 1722 | |
| 1723 | note chain_crv: ec.om curve for key type kt (2 P-256, 3 P-384) |
| 1724 | fn chain_crv kt ulong to ulong |
| 1725 | if eq kt 2 |
| 1726 | ret 256 |
| 1727 | end |
| 1728 | ret 384 |
| 1729 | end |
| 1730 | |
| 1731 | note chain_link: is kd (record w[32 .. 64), already parsed) the issuer of cd (record w[0 .. 32))? |
| 1732 | note subject(kd) must equal issuer(cd) byte for byte (DER equality instead of the RFC 5280 7.1 |
| 1733 | note comparison rules), then chain_sig. X5EUNK when the names differ. |
| 1734 | fn chain_link cd view uchar * kd view uchar * w view ulong * to ulong |
| 1735 | io = get w X5RISO |
| 1736 | il = get w X5RISL |
| 1737 | so = get w add X5RSZ X5RSBO |
| 1738 | sn = get w add X5RSZ X5RSBL |
| 1739 | if not der_same part cd io il part kd so sn |
| 1740 | ret X5EUNK |
| 1741 | end |
| 1742 | ret chain_sig cd kd part w 0 mul X5RSZ 2 |
| 1743 | end |
| 1744 | |
| 1745 | note chain_self: 1 when the certificate c (record w[32 .. 64)) is self-issued: its issuer and subject |
| 1746 | note are the same DER bytes; else 0 |
| 1747 | fn chain_self c view uchar * w view ulong * to ulong |
| 1748 | io = get w add X5RSZ X5RISO |
| 1749 | il = get w add X5RSZ X5RISL |
| 1750 | so = get w add X5RSZ X5RSBO |
| 1751 | if ne il get w add X5RSZ X5RSBL |
| 1752 | ret 0 |
| 1753 | end |
| 1754 | for i ulong il |
| 1755 | if ne get c add io i get c add so i |
| 1756 | ret 0 |
| 1757 | end |
| 1758 | end |
| 1759 | ret 1 |
| 1760 | end |
| 1761 | |
| 1762 | note chain_alink: chain_link for a store entry kd, which must be self-issued (chain_self) to be an |
| 1763 | note anchor; X5EUNK otherwise |
| 1764 | fn chain_alink cd view uchar * kd view uchar * w view ulong * to ulong |
| 1765 | if eq chain_self kd w 0 |
| 1766 | ret X5EUNK |
| 1767 | end |
| 1768 | ret chain_link cd kd w |
| 1769 | end |
| 1770 | |
| 1771 | note chain_anchor: try every trust anchor in st (store format) whose subject is the issuer of the |
| 1772 | note current certificate cd. 0 when one verifies it; else the code of the last failed candidate, |
| 1773 | note X5EUNK when none has the name. Anchors that do not parse are skipped, and so are store entries |
| 1774 | note that are not self-issued (subject DER == issuer DER, chain_self): an intermediate placed in the |
| 1775 | note store is not a trust anchor (OpenSSL without -partial_chain; stricter than RFC 5280 6.1.1 d). |
| 1776 | note Anchors are not checked for validity period or CA constraints (RFC 5280 6.1: a name and a key). |
| 1777 | fn chain_anchor cd view uchar * st view uchar * w edit ulong * to ulong |
| 1778 | p = ulong 0 |
| 1779 | res = X5EUNK |
| 1780 | while lt p len st bound 100000 |
| 1781 | if lt sub len st p 3 |
| 1782 | break |
| 1783 | end |
| 1784 | l = der_u24 st p |
| 1785 | p = add p 3 |
| 1786 | if or eq l 0 gt l sub len st p |
| 1787 | break |
| 1788 | end |
| 1789 | if eq x509_parse part st p l part w X5RSZ X5RSZ 0 |
| 1790 | v = chain_alink cd part st p l view w |
| 1791 | if eq v 0 |
| 1792 | ret 0 |
| 1793 | end |
| 1794 | if ne v X5EUNK |
| 1795 | res = v |
| 1796 | end |
| 1797 | end |
| 1798 | p = add p l |
| 1799 | end |
| 1800 | ret res |
| 1801 | end |
| 1802 | |
| 1803 | note chain_pres: try the presented certificates of cl not yet in the path as issuer of the current |
| 1804 | note one (the first that verifies is taken; its index goes to w[X5WSEL]). 0, or the code of the |
| 1805 | note first failed candidate, replaced by any later code other than X5EUNK and X5EMAL. A presented |
| 1806 | note certificate that does not parse (X5EMAL, X5ECRIT) fails the chain when no candidate verifies |
| 1807 | note (fail closed: its subject cannot be known). X5EUNK when nothing is left to try. |
| 1808 | fn chain_pres cl view uchar * w edit ulong * to ulong |
| 1809 | kb = new uchar 16384 0 |
| 1810 | res = X5EUNK |
| 1811 | for j ulong get w X5WN |
| 1812 | if ne and get w X5WUSED shl ulong 1 j 0 |
| 1813 | continue |
| 1814 | end |
| 1815 | put w X5WSEL j |
| 1816 | v = chain_try cl w kb |
| 1817 | if eq v 0 |
| 1818 | ret 0 |
| 1819 | end |
| 1820 | if or eq res X5EUNK and ne v X5EUNK ne v X5EMAL |
| 1821 | res = v |
| 1822 | end |
| 1823 | end |
| 1824 | ret res |
| 1825 | end |
| 1826 | |
| 1827 | note chain_try: candidate issuer w[X5WSEL] of cl for the current certificate w[X5WCUR]; the |
| 1828 | note candidate is copied into kb (the two certificates of one call must be distinct arrays) |
| 1829 | fn chain_try cl view uchar * w edit ulong * kb edit uchar * to ulong |
| 1830 | j = get w X5WSEL |
| 1831 | o = get w add X5WIX mul j 2 |
| 1832 | l = get w add X5WIX add mul j 2 1 |
| 1833 | if gt l len kb |
| 1834 | ret X5EMAL |
| 1835 | end |
| 1836 | for i ulong l |
| 1837 | put kb i get cl add o i |
| 1838 | end |
| 1839 | v = x509_parse view part kb 0 l part w X5RSZ X5RSZ |
| 1840 | if ne v 0 |
| 1841 | ret v |
| 1842 | end |
| 1843 | if ne chain_rsapol part w X5RSZ X5RSZ 0 |
| 1844 | ret X5EWEAK |
| 1845 | end |
| 1846 | j = get w X5WCUR |
| 1847 | o = get w add X5WIX mul j 2 |
| 1848 | i = get w add X5WIX add mul j 2 1 |
| 1849 | ret chain_link part cl o i view part kb 0 l view w |
| 1850 | end |
| 1851 | |
| 1852 | note chain_ca: constraints of the chosen issuer (record w[32 .. 64)), a non-anchor CA certificate: |
| 1853 | note validity at now, basicConstraints cA TRUE, pathLenConstraint >= the number of intermediates |
| 1854 | note below it (w[X5WDEP] - 1), keyUsage (if present) keyCertSign, EKU (if present) serverAuth or |
| 1855 | note anyExtendedKeyUsage |
| 1856 | fn chain_ca w view ulong * to ulong |
| 1857 | v = chain_time part w X5RSZ X5RSZ get w X5WNOW |
| 1858 | if ne v 0 |
| 1859 | ret v |
| 1860 | end |
| 1861 | if eq get w add X5RSZ X5RCA 0 |
| 1862 | ret X5ENCA |
| 1863 | end |
| 1864 | if gt sub get w X5WDEP 1 get w add X5RSZ X5RPL |
| 1865 | ret X5EPATH |
| 1866 | end |
| 1867 | if eq get w add X5RSZ X5RKU 1 |
| 1868 | if eq and get w add X5RSZ X5RKUB 4 0 |
| 1869 | ret X5EKU |
| 1870 | end |
| 1871 | end |
| 1872 | if eq get w add X5RSZ X5REKU 1 |
| 1873 | if and eq get w add X5RSZ X5RSRV 0 eq get w add X5RSZ X5RANY 0 |
| 1874 | ret X5EEKU |
| 1875 | end |
| 1876 | end |
| 1877 | ret 0 |
| 1878 | end |
| 1879 | |
| 1880 | note chain_rsapol: X5EWEAK when the certificate record r has an RSA key outside x509_rsaok, else 0 |
| 1881 | fn chain_rsapol r view ulong * to ulong |
| 1882 | if and eq get r X5RKT 1 eq x509_rsaok get r X5RBITS 0 |
| 1883 | ret X5EWEAK |
| 1884 | end |
| 1885 | ret 0 |
| 1886 | end |
| 1887 | |
| 1888 | note chain_leaf: the leaf (entry 0): parse, validity at now, host name, not a CA, keyUsage (if |
| 1889 | note present) digitalSignature, EKU (if present) serverAuth, key type supported, RSA key policy |
| 1890 | note (x509_rsaok: 2048 .. 4096 bits, exponent odd in 3 .. 2^32-1) |
| 1891 | fn chain_leaf cl view uchar * hq view uchar * w edit ulong * to ulong |
| 1892 | lo = get w X5WIX |
| 1893 | ll = get w add X5WIX 1 |
| 1894 | v = x509_parse part cl lo ll part w 0 X5RSZ |
| 1895 | if ne v 0 |
| 1896 | ret v |
| 1897 | end |
| 1898 | v = chain_time part w 0 X5RSZ get w X5WNOW |
| 1899 | if ne v 0 |
| 1900 | ret v |
| 1901 | end |
| 1902 | v = chain_hostok part hq 8 sub len hq 8 |
| 1903 | if ne v 0 |
| 1904 | ret v |
| 1905 | end |
| 1906 | v = chain_host part cl lo ll view part w 0 X5RSZ part hq 8 sub len hq 8 |
| 1907 | if ne v 0 |
| 1908 | ret v |
| 1909 | end |
| 1910 | if eq get w X5RCA 1 |
| 1911 | ret X5ELCA |
| 1912 | end |
| 1913 | if eq get w X5RKU 1 |
| 1914 | if eq and get w X5RKUB 128 0 |
| 1915 | ret X5EKU |
| 1916 | end |
| 1917 | end |
| 1918 | if eq get w X5REKU 1 |
| 1919 | if eq get w X5RSRV 0 |
| 1920 | ret X5EEKU |
| 1921 | end |
| 1922 | end |
| 1923 | kt = get w X5RKT |
| 1924 | if or eq kt 0 gt kt 3 |
| 1925 | ret X5EALG |
| 1926 | end |
| 1927 | if and eq kt 1 eq x509_rsaok get w X5RBITS 0 |
| 1928 | ret X5EWEAK |
| 1929 | end |
| 1930 | ret 0 |
| 1931 | end |
| 1932 | |
| 1933 | note chain_path: build the path upward from the leaf. At each step a trust anchor is preferred |
| 1934 | note (chain_anchor); else a presented certificate (chain_pres), which must then pass chain_ca. Each |
| 1935 | note presented certificate is used at most once (no loops); at most X5MAXD non-anchor certificates. |
| 1936 | fn chain_path cl view uchar * st view uchar * w edit ulong * to ulong |
| 1937 | put w X5WUSED 1 |
| 1938 | put w X5WDEP 1 |
| 1939 | put w X5WCUR 0 |
| 1940 | for k ulong X5MAXD |
| 1941 | cu = get w X5WCUR |
| 1942 | co = get w add X5WIX mul cu 2 |
| 1943 | cn = get w add X5WIX add mul cu 2 1 |
| 1944 | a = chain_anchor part cl co cn st w |
| 1945 | if eq a 0 |
| 1946 | ret 0 |
| 1947 | end |
| 1948 | v = chain_pres cl w |
| 1949 | if ne v 0 |
| 1950 | if eq v X5EUNK |
| 1951 | ret a |
| 1952 | end |
| 1953 | ret v |
| 1954 | end |
| 1955 | if ge get w X5WDEP X5MAXD |
| 1956 | ret X5EDEEP |
| 1957 | end |
| 1958 | v = chain_ca w |
| 1959 | if ne v 0 |
| 1960 | ret v |
| 1961 | end |
| 1962 | chain_up w |
| 1963 | end |
| 1964 | ret X5EDEEP |
| 1965 | end |
| 1966 | |
| 1967 | note chain_up: the chosen presented issuer becomes the current certificate |
| 1968 | fn chain_up w edit ulong * to void |
| 1969 | for i ulong X5RSZ |
| 1970 | put w i get w add X5RSZ i |
| 1971 | end |
| 1972 | j = get w X5WSEL |
| 1973 | put w X5WCUR j |
| 1974 | put w X5WUSED or get w X5WUSED shl ulong 1 j |
| 1975 | put w X5WDEP add get w X5WDEP 1 |
| 1976 | end |
| 1977 | |
| 1978 | note x509_chain_verify: validate the server chain. cl = TLS 1.3 certificate_list contents, leaf |
| 1979 | note first; st = store from x509_store_load(f); hq = 8-byte big-endian unix time || host name. |
| 1980 | note 0 valid, else a reject code (see the table at the top). Limits (X5EMAL beyond them): at most |
| 1981 | note X5MAXC (16) entries in cl, presented issuer certificates at most X5KBUF (16384) bytes, signatures |
| 1982 | note at most 1024 bytes (X5ESIG), now at most 253402300799 (9999-12-31T23:59:59Z). |
| 1983 | note Rules, all fail closed: leaf = entry 0 (chain_leaf); issuers by path building (chain_path); |
| 1984 | note trust anchors are the self-issued store entries, name + key only (no validity, CA or usage |
| 1985 | note checks on anchors, RFC 5280 6.1.1 d); |
| 1986 | note names are compared as DER bytes, not with the RFC 5280 7.1 / 4.1.2.4 matching rules (case and |
| 1987 | note whitespace folding of PrintableString/UTF8String are NOT applied: an issuer whose encoding of |
| 1988 | note the same name differs from the subject encoding is reported X5EUNK). |
| 1989 | fn x509_chain_verify cl view uchar * st view uchar * hq view uchar * to ulong |
| 1990 | w = new ulong 128 0 |
| 1991 | if lt len hq 8 |
| 1992 | ret X5EMAL |
| 1993 | end |
| 1994 | nw = ulong 0 |
| 1995 | for i ulong 8 |
| 1996 | nw = or shl nw 8 ulong get hq i |
| 1997 | end |
| 1998 | if gt nw 253402300799 |
| 1999 | ret X5EMAL |
| 2000 | end |
| 2001 | put w X5WNOW add nw mul x509_days 1970 1 1 86400 |
| 2002 | n = chain_index cl part w X5WIX mul X5MAXC 2 |
| 2003 | if eq n 0 |
| 2004 | ret X5EMAL |
| 2005 | end |
| 2006 | if gt get w add X5WIX 1 X5LMAX |
| 2007 | ret X5EMAL |
| 2008 | end |
| 2009 | put w X5WN n |
| 2010 | v = chain_leaf cl hq w |
| 2011 | if ne v 0 |
| 2012 | ret v |
| 2013 | end |
| 2014 | ret chain_path cl st w |
| 2015 | end |
| 2016 | |
| 2017 | note chain_cvsig: CertificateVerify check with the leaf kd (record r; r[30] hash, r[31] content |
| 2018 | note length): ms = content || signature; RSA keys use PSS, EC keys ECDSA (chain_hsig) |
| 2019 | fn chain_cvsig kd view uchar * r view ulong * ms view uchar * to ulong |
| 2020 | p = new ulong 10 0 |
| 2021 | kt = get r X5RKT |
| 2022 | put p 0 3 |
| 2023 | if eq kt 1 |
| 2024 | put p 0 2 |
| 2025 | end |
| 2026 | put p 1 get r 30 |
| 2027 | put p 2 get r X5RKO |
| 2028 | put p 3 get r X5RKL |
| 2029 | put p 4 kt |
| 2030 | put p 7 get r 31 |
| 2031 | put p 8 get r 31 |
| 2032 | put p 9 sub len ms get r 31 |
| 2033 | ret chain_hsig ms kd edit p |
| 2034 | end |
| 2035 | |
| 2036 | note x509_cv_verify: TLS 1.3 CertificateVerify (RFC 8446 4.4.3) against the leaf of cl. ms = signed |
| 2037 | note content (mlen bytes) || signature. Schemes: 0x0403 ecdsa_secp256r1_sha256 (P-256 key), |
| 2038 | note 0x0503 ecdsa_secp384r1_sha384 (P-384 key), 0x0804/0x0805/0x0806 rsa_pss_rsae_sha256/384/512 |
| 2039 | note (rsaEncryption key within x509_rsaok, else X5EWEAK). Anything else, rsa_pkcs1_* included, is |
| 2040 | note X5ESCH. |
| 2041 | fn x509_cv_verify cl view uchar * scheme ulong ms view uchar * mlen ulong to ulong |
| 2042 | r = new ulong 32 0 |
| 2043 | if lt len cl 3 |
| 2044 | ret X5EMAL |
| 2045 | end |
| 2046 | l = der_u24 cl 0 |
| 2047 | if or eq l 0 gt l sub len cl 3 |
| 2048 | ret X5EMAL |
| 2049 | end |
| 2050 | v = x509_parse part cl ulong 3 l edit r |
| 2051 | if ne v 0 |
| 2052 | ret v |
| 2053 | end |
| 2054 | if gt mlen len ms |
| 2055 | ret X5EMAL |
| 2056 | end |
| 2057 | kt = get r X5RKT |
| 2058 | hb = ulong 0 |
| 2059 | if and eq scheme 0x0403 eq kt 2 |
| 2060 | hb = 256 |
| 2061 | end |
| 2062 | if and eq scheme 0x0503 eq kt 3 |
| 2063 | hb = 384 |
| 2064 | end |
| 2065 | if and and ge scheme 0x0804 le scheme 0x0806 eq kt 1 |
| 2066 | hb = add 256 mul sub scheme 0x0804 128 |
| 2067 | if eq x509_rsaok get r X5RBITS 0 |
| 2068 | ret X5EWEAK |
| 2069 | end |
| 2070 | end |
| 2071 | if eq hb 0 |
| 2072 | ret X5ESCH |
| 2073 | end |
| 2074 | put r 30 hb |
| 2075 | put r 31 mlen |
| 2076 | ret chain_cvsig part cl ulong 3 l r ms |
| 2077 | end |