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