omtls
omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).
Brought by drakestapleton
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note pkv.om (ot_pk_): public keys and signature algorithms of X.509 and TLS 1.3, dispatched to the |
| 3 | note primitives of CONTRACT revision 2 (ot_rsa_key / ot_rsa_pkcs1 / ot_rsa_pss, ot_ec_key / |
| 4 | note ot_ec_verify, ot_hash, the vendored SHA-256 and ot_s5_ for SHA-384/512). Public data only. |
| 5 | note Unit order: src/der.om must come earlier in the unit (a global is declared before its first use). |
| 6 | note |
| 7 | note Supported (anything else is "unsupported", code 8 in x509.om, false in ot_pk_tls): |
| 8 | note - keys: rsaEncryption (RFC 3279 2.3.1, parameters NULL); id-RSASSA-PSS (RFC 4055 1.2, parameters |
| 9 | note absent, or RSASSA-PSS-params naming SHA-256/384/512 with the same MGF1 hash: then only that hash |
| 10 | note may be used with the key); id-ecPublicKey with namedCurve prime256v1 or secp384r1 (RFC 5480). |
| 11 | note - certificate signatures: sha256/384/512WithRSAEncryption (parameters NULL), RSASSA-PSS with |
| 12 | note explicit parameters (hash SHA-256/384/512, MGF1 with the same hash, salt from the parameters, |
| 13 | note default 20; a trailerField is refused because DER omits its only value), ecdsa-with-SHA256/384/512 |
| 14 | note (parameters absent). SHA-1 and MD5 algorithms are well-formed but unsupported. |
| 15 | note - TLS 1.3 CertificateVerify schemes (RFC 8446 4.2.3): 0x0403 ecdsa_secp256r1_sha256 (P-256 key), |
| 16 | note 0x0503 ecdsa_secp384r1_sha384 (P-384 key), 0x0804..0x0806 rsa_pss_rsae_sha256/384/512 |
| 17 | note (rsaEncryption key), 0x0809..0x080b rsa_pss_pss_sha256/384/512 (id-RSASSA-PSS key); salt length |
| 18 | note = hash length. rsa_pkcs1_* schemes are refused for CertificateVerify (RFC 8446 4.4.3). |
| 19 | note |
| 20 | note The DER readers only read the store ot_der_m, so ot_pk_tlskey copies the SPKI to the last 64 KiB |
| 21 | note of the store (ot_pk_sc); x509.om never uses that region. |
| 22 | const ot_pk_sc ulong 16711680 |
| 23 | note ot_pk_k: the last key parsed: 0 type (0 unsupported, 1 rsaEncryption, 2 RSASSA-PSS key, 3 P-256, |
| 24 | note 4 P-384), 1 n offset, 2 n length, 3 e offset or point offset, 4 e length or point length, |
| 25 | note 5 PSS key hash + 1 (0: any), 6 and 7 the hash and salt of the last RSASSA-PSS-params read |
| 26 | global ot_pk_k ulong 8 |
| 27 | note ot_pk_a: the last signature algorithm: 0 kind (0 unsupported, 1 PKCS#1 v1.5, 2 PSS, 3 ECDSA), |
| 28 | note 1 hash (0 SHA-256, 1 SHA-384, 2 SHA-512), 2 PSS salt length |
| 29 | global ot_pk_a ulong 3 |
| 30 | note ot_pk_q: one certificate signature check: 0 AlgorithmIdentifier offset, 1 its length, 2 TBS offset, |
| 31 | note 3 TBS length, 4 signature offset, 5 signature length, 6 issuer SPKI offset, 7 its length |
| 32 | global ot_pk_q ulong 8 |
| 33 | note ot_pk_st: 0 the length of the SPKI held by ot_pk_tlskey (0: none) |
| 34 | global ot_pk_st ulong 1 |
| 35 | |
| 36 | note ot_pk_null: the bytes [q, e) are exactly one NULL |
| 37 | fn ot_pk_null q ulong e ulong to bool |
| 38 | if eq ot_der_ex q e 5 false |
| 39 | ret false |
| 40 | end |
| 41 | ret and eq ot_der_vl 0 eq ot_der_nd e |
| 42 | end |
| 43 | |
| 44 | note ot_pk_hid: a hash AlgorithmIdentifier that is exactly [p, e): 0, 1, 2 for SHA-256/384/512 (RFC 5754: |
| 45 | note parameters absent or NULL), 7 well-formed but another hash, 9 malformed |
| 46 | fn ot_pk_hid p ulong e ulong to ulong |
| 47 | if eq ot_der_ex p e 48 false |
| 48 | ret 9 |
| 49 | end |
| 50 | if ne ot_der_nd e |
| 51 | ret 9 |
| 52 | end |
| 53 | if eq ot_der_ex ot_der_vo e 6 false |
| 54 | ret 9 |
| 55 | end |
| 56 | o = ot_der_vo |
| 57 | l = ot_der_vl |
| 58 | q = ot_der_nd |
| 59 | if eq ot_der_okoid o l false |
| 60 | ret 9 |
| 61 | end |
| 62 | if lt q e |
| 63 | if eq ot_pk_null q e false |
| 64 | ret 9 |
| 65 | end |
| 66 | end |
| 67 | if ot_der_oid o l 9 0x60 0x8648016503040201 |
| 68 | ret 0 |
| 69 | end |
| 70 | if ot_der_oid o l 9 0x60 0x8648016503040202 |
| 71 | ret 1 |
| 72 | end |
| 73 | if ot_der_oid o l 9 0x60 0x8648016503040203 |
| 74 | ret 2 |
| 75 | end |
| 76 | ret 7 |
| 77 | end |
| 78 | |
| 79 | note ot_pk_mgf: MaskGenAlgorithm exactly [p, e): the MGF1 hash as ot_pk_hid; 7 another MGF; 9 malformed |
| 80 | fn ot_pk_mgf p ulong e ulong to ulong |
| 81 | if eq ot_der_ex p e 48 false |
| 82 | ret 9 |
| 83 | end |
| 84 | if ne ot_der_nd e |
| 85 | ret 9 |
| 86 | end |
| 87 | if eq ot_der_ex ot_der_vo e 6 false |
| 88 | ret 9 |
| 89 | end |
| 90 | o = ot_der_vo |
| 91 | l = ot_der_vl |
| 92 | q = ot_der_nd |
| 93 | if eq ot_der_okoid o l false |
| 94 | ret 9 |
| 95 | end |
| 96 | if eq ot_der_oid o l 9 0x2a 0x864886f70d010108 false |
| 97 | ret 7 |
| 98 | end |
| 99 | ret ot_pk_hid q e |
| 100 | end |
| 101 | |
| 102 | note ot_pk_ctx: the context-tagged field t at i in [i, e) (absent: i unchanged); sets ot_pk_k 6 to the |
| 103 | note end of the field and returns its contents offset, or 0 with ot_pk_k 6 = 0 when malformed |
| 104 | fn ot_pk_ctx i ulong e ulong t ulong to ulong |
| 105 | put ot_pk_k 6 i |
| 106 | if ge i e |
| 107 | ret i |
| 108 | end |
| 109 | if ne ulong get ot_der_m i t |
| 110 | ret i |
| 111 | end |
| 112 | if eq ot_der_ex i e t false |
| 113 | put ot_pk_k 6 0 |
| 114 | ret 0 |
| 115 | end |
| 116 | put ot_pk_k 6 ot_der_nd |
| 117 | ret ot_der_vo |
| 118 | end |
| 119 | |
| 120 | note ot_pk_pssp: RSASSA-PSS-params exactly [p, e) (RFC 4055 3.1). 0 supported (ot_pk_k 6 hash, |
| 121 | note 7 salt), 7 well-formed but unsupported (SHA-1 default, another hash, MGF1 hash not the hash), |
| 122 | note 9 malformed (also a trailerField, whose only value DER omits) |
| 123 | fn ot_pk_pssp p ulong e ulong to ulong |
| 124 | if eq ot_der_ex p e 48 false |
| 125 | ret 9 |
| 126 | end |
| 127 | if ne ot_der_nd e |
| 128 | ret 9 |
| 129 | end |
| 130 | i = ot_der_vo |
| 131 | h = ulong 7 |
| 132 | m = ulong 7 |
| 133 | s = ulong 20 |
| 134 | v = ot_pk_ctx i e 160 |
| 135 | j = get ot_pk_k 6 |
| 136 | if eq j 0 |
| 137 | ret 9 |
| 138 | end |
| 139 | if ne j i |
| 140 | h = ot_pk_hid v j |
| 141 | if eq h 9 |
| 142 | ret 9 |
| 143 | end |
| 144 | i = j |
| 145 | end |
| 146 | v = ot_pk_ctx i e 161 |
| 147 | j = get ot_pk_k 6 |
| 148 | if eq j 0 |
| 149 | ret 9 |
| 150 | end |
| 151 | if ne j i |
| 152 | m = ot_pk_mgf v j |
| 153 | if eq m 9 |
| 154 | ret 9 |
| 155 | end |
| 156 | i = j |
| 157 | end |
| 158 | v = ot_pk_ctx i e 162 |
| 159 | j = get ot_pk_k 6 |
| 160 | if eq j 0 |
| 161 | ret 9 |
| 162 | end |
| 163 | if ne j i |
| 164 | if eq ot_der_ex v j 2 false |
| 165 | ret 9 |
| 166 | end |
| 167 | if ne ot_der_nd j |
| 168 | ret 9 |
| 169 | end |
| 170 | s = ot_der_small ot_der_vo ot_der_vl |
| 171 | if gt s 65535 |
| 172 | ret 9 |
| 173 | end |
| 174 | i = j |
| 175 | end |
| 176 | if ne i e |
| 177 | ret 9 |
| 178 | end |
| 179 | put ot_pk_k 6 h |
| 180 | put ot_pk_k 7 s |
| 181 | if or gt h 2 ne m h |
| 182 | ret 7 |
| 183 | end |
| 184 | ret 0 |
| 185 | end |
| 186 | |
| 187 | note ot_pk_sigalg: a signature AlgorithmIdentifier exactly [p, e) into ot_pk_a; false when malformed |
| 188 | fn ot_pk_sigalg p ulong e ulong to bool |
| 189 | put ot_pk_a 0 0 |
| 190 | put ot_pk_a 1 0 |
| 191 | put ot_pk_a 2 0 |
| 192 | if eq ot_der_ex p e 48 false |
| 193 | ret false |
| 194 | end |
| 195 | if ne ot_der_nd e |
| 196 | ret false |
| 197 | end |
| 198 | if eq ot_der_ex ot_der_vo e 6 false |
| 199 | ret false |
| 200 | end |
| 201 | o = ot_der_vo |
| 202 | l = ot_der_vl |
| 203 | q = ot_der_nd |
| 204 | if eq ot_der_okoid o l false |
| 205 | ret false |
| 206 | end |
| 207 | k = ot_pk_rsaw o l |
| 208 | if lt k 3 |
| 209 | if eq ot_pk_null q e false |
| 210 | ret false |
| 211 | end |
| 212 | put ot_pk_a 0 1 |
| 213 | put ot_pk_a 1 k |
| 214 | ret true |
| 215 | end |
| 216 | k = ot_pk_ecw o l |
| 217 | if lt k 3 |
| 218 | if ne q e |
| 219 | ret false |
| 220 | end |
| 221 | put ot_pk_a 0 3 |
| 222 | put ot_pk_a 1 k |
| 223 | ret true |
| 224 | end |
| 225 | if ot_der_oid o l 9 0x2a 0x864886f70d01010a |
| 226 | if eq q e |
| 227 | ret true |
| 228 | end |
| 229 | r = ot_pk_pssp q e |
| 230 | if eq r 9 |
| 231 | ret false |
| 232 | end |
| 233 | if eq r 0 |
| 234 | put ot_pk_a 0 2 |
| 235 | put ot_pk_a 1 get ot_pk_k 6 |
| 236 | put ot_pk_a 2 get ot_pk_k 7 |
| 237 | end |
| 238 | ret true |
| 239 | end |
| 240 | ret true |
| 241 | end |
| 242 | |
| 243 | note ot_pk_rsaw: sha256/384/512WithRSAEncryption (RFC 8017 A.2.4) as 0/1/2, else 9 |
| 244 | fn ot_pk_rsaw o ulong l ulong to ulong |
| 245 | if ot_der_oid o l 9 0x2a 0x864886f70d01010b |
| 246 | ret 0 |
| 247 | end |
| 248 | if ot_der_oid o l 9 0x2a 0x864886f70d01010c |
| 249 | ret 1 |
| 250 | end |
| 251 | if ot_der_oid o l 9 0x2a 0x864886f70d01010d |
| 252 | ret 2 |
| 253 | end |
| 254 | ret 9 |
| 255 | end |
| 256 | |
| 257 | note ot_pk_ecw: ecdsa-with-SHA256/384/512 (RFC 5758 3.2) as 0/1/2, else 9 |
| 258 | fn ot_pk_ecw o ulong l ulong to ulong |
| 259 | if ot_der_oid o l 8 0 0x2a8648ce3d040302 |
| 260 | ret 0 |
| 261 | end |
| 262 | if ot_der_oid o l 8 0 0x2a8648ce3d040303 |
| 263 | ret 1 |
| 264 | end |
| 265 | if ot_der_oid o l 8 0 0x2a8648ce3d040304 |
| 266 | ret 2 |
| 267 | end |
| 268 | ret 9 |
| 269 | end |
| 270 | |
| 271 | note ot_pk_pint: the INTEGER just read is positive and minimal; its value bytes without the leading |
| 272 | note zero go to ot_pk_k i (offset) and i + 1 (length) |
| 273 | fn ot_pk_pint i ulong to bool |
| 274 | o = ot_der_vo |
| 275 | n = ot_der_vl |
| 276 | if eq ot_der_okint o n false |
| 277 | ret false |
| 278 | end |
| 279 | if ge get ot_der_m o 128 |
| 280 | ret false |
| 281 | end |
| 282 | if eq get ot_der_m o 0 |
| 283 | o = add o 1 |
| 284 | n = sub n 1 |
| 285 | end |
| 286 | if eq n 0 |
| 287 | ret false |
| 288 | end |
| 289 | put ot_pk_k i o |
| 290 | put ot_pk_k add i 1 n |
| 291 | ret true |
| 292 | end |
| 293 | |
| 294 | note ot_pk_rsak: RSAPublicKey ::= SEQUENCE { modulus INTEGER, publicExponent INTEGER } exactly [p, e) |
| 295 | fn ot_pk_rsak p ulong e ulong kt ulong to bool |
| 296 | if eq ot_der_ex p e 48 false |
| 297 | ret false |
| 298 | end |
| 299 | if ne ot_der_nd e |
| 300 | ret false |
| 301 | end |
| 302 | if eq ot_der_ex ot_der_vo e 2 false |
| 303 | ret false |
| 304 | end |
| 305 | if eq ot_pk_pint 1 false |
| 306 | ret false |
| 307 | end |
| 308 | if eq ot_der_ex ot_der_nd e 2 false |
| 309 | ret false |
| 310 | end |
| 311 | if eq ot_pk_pint 3 false |
| 312 | ret false |
| 313 | end |
| 314 | if ne ot_der_nd e |
| 315 | ret false |
| 316 | end |
| 317 | put ot_pk_k 0 kt |
| 318 | ret true |
| 319 | end |
| 320 | |
| 321 | note ot_pk_ecp: ECParameters exactly [q, e): a namedCurve OID; another well-formed choice is unsupported |
| 322 | fn ot_pk_ecp q ulong e ulong to bool |
| 323 | if eq ot_der_rd q e false |
| 324 | ret false |
| 325 | end |
| 326 | if ne ot_der_nd e |
| 327 | ret false |
| 328 | end |
| 329 | if ne ot_der_tg 6 |
| 330 | ret true |
| 331 | end |
| 332 | o = ot_der_vo |
| 333 | l = ot_der_vl |
| 334 | if eq ot_der_okoid o l false |
| 335 | ret false |
| 336 | end |
| 337 | if ot_der_oid o l 8 0 0x2a8648ce3d030107 |
| 338 | put ot_pk_k 0 3 |
| 339 | end |
| 340 | if ot_der_oid o l 5 0 0x2b81040022 |
| 341 | put ot_pk_k 0 4 |
| 342 | end |
| 343 | ret true |
| 344 | end |
| 345 | |
| 346 | note ot_pk_spki: SubjectPublicKeyInfo exactly [p, e) into ot_pk_k; false when malformed. A well-formed |
| 347 | note key of another algorithm or curve leaves type 0. |
| 348 | fn ot_pk_spki p ulong e ulong to bool |
| 349 | put ot_pk_k 0 0 |
| 350 | put ot_pk_k 5 0 |
| 351 | if eq ot_der_ex p e 48 false |
| 352 | ret false |
| 353 | end |
| 354 | if ne ot_der_nd e |
| 355 | ret false |
| 356 | end |
| 357 | if eq ot_der_ex ot_der_vo e 48 false |
| 358 | ret false |
| 359 | end |
| 360 | ae = ot_der_nd |
| 361 | if eq ot_der_ex ot_der_vo ae 6 false |
| 362 | ret false |
| 363 | end |
| 364 | o = ot_der_vo |
| 365 | l = ot_der_vl |
| 366 | q = ot_der_nd |
| 367 | if eq ot_der_okoid o l false |
| 368 | ret false |
| 369 | end |
| 370 | if eq ot_pk_bits ae e false |
| 371 | ret false |
| 372 | end |
| 373 | if ot_der_oid o l 9 0x2a 0x864886f70d010101 |
| 374 | if eq ot_pk_null q ae false |
| 375 | ret false |
| 376 | end |
| 377 | ret ot_pk_rsak get ot_pk_k 3 add get ot_pk_k 3 get ot_pk_k 4 1 |
| 378 | end |
| 379 | if ot_der_oid o l 9 0x2a 0x864886f70d01010a |
| 380 | ret ot_pk_pssk q ae |
| 381 | end |
| 382 | if ot_der_oid o l 7 0 0x2a8648ce3d0201 |
| 383 | ret ot_pk_ecp q ae |
| 384 | end |
| 385 | ret true |
| 386 | end |
| 387 | |
| 388 | note ot_pk_bits: subjectPublicKey BIT STRING exactly [b, e) with no unused bits and at least one key |
| 389 | note byte (an empty key is malformed for every algorithm); its bytes after the unused-bits byte go to |
| 390 | note ot_pk_k 3 (offset) and 4 (length) |
| 391 | fn ot_pk_bits b ulong e ulong to bool |
| 392 | if eq ot_der_ex b e 3 false |
| 393 | ret false |
| 394 | end |
| 395 | if ne ot_der_nd e |
| 396 | ret false |
| 397 | end |
| 398 | if lt ot_der_vl 2 |
| 399 | ret false |
| 400 | end |
| 401 | if eq ot_der_okbits ot_der_vo ot_der_vl false |
| 402 | ret false |
| 403 | end |
| 404 | if ne get ot_der_m ot_der_vo 0 |
| 405 | ret false |
| 406 | end |
| 407 | put ot_pk_k 3 add ot_der_vo 1 |
| 408 | put ot_pk_k 4 sub ot_der_vl 1 |
| 409 | ret true |
| 410 | end |
| 411 | |
| 412 | note ot_pk_pssk: an id-RSASSA-PSS key; parameters [q, e) absent or RSASSA-PSS-params |
| 413 | fn ot_pk_pssk q ulong e ulong to bool |
| 414 | h = ulong 0 |
| 415 | if lt q e |
| 416 | r = ot_pk_pssp q e |
| 417 | if eq r 9 |
| 418 | ret false |
| 419 | end |
| 420 | if ne r 0 |
| 421 | ret true |
| 422 | end |
| 423 | h = add get ot_pk_k 6 1 |
| 424 | end |
| 425 | if eq ot_pk_rsak get ot_pk_k 3 add get ot_pk_k 3 get ot_pk_k 4 2 false |
| 426 | ret false |
| 427 | end |
| 428 | put ot_pk_k 5 h |
| 429 | ret true |
| 430 | end |
| 431 | |
| 432 | note ot_pk_rk: load the RSA key of ot_pk_k into the RSA module (n and e at most 2048 bytes each) |
| 433 | fn ot_pk_rk to bool |
| 434 | n = new uchar 2048 0 |
| 435 | x = new uchar 2048 0 |
| 436 | nl = get ot_pk_k 2 |
| 437 | el = get ot_pk_k 4 |
| 438 | if or gt nl 2048 gt el 2048 |
| 439 | ret false |
| 440 | end |
| 441 | o = get ot_pk_k 1 |
| 442 | for i ulong nl bound 16777216 |
| 443 | put n i get ot_der_m add o i |
| 444 | end |
| 445 | o = get ot_pk_k 3 |
| 446 | for i ulong el bound 16777216 |
| 447 | put x i get ot_der_m add o i |
| 448 | end |
| 449 | ret ot_rsa_key part n 0 nl part x 0 el |
| 450 | end |
| 451 | |
| 452 | note ot_pk_ek: load the EC key of ot_pk_k into the EC module. Only an uncompressed point has the right |
| 453 | note length (65 bytes for P-256, 97 for P-384); any other length (empty, compressed, hybrid) is refused |
| 454 | note here, before ot_ec_key reads a byte of it. |
| 455 | fn ot_pk_ek to bool |
| 456 | q = new uchar 97 0 |
| 457 | pl = get ot_pk_k 4 |
| 458 | c = ulong 1 |
| 459 | w = ulong 65 |
| 460 | if eq get ot_pk_k 0 4 |
| 461 | c = 2 |
| 462 | w = 97 |
| 463 | end |
| 464 | if ne pl w |
| 465 | ret false |
| 466 | end |
| 467 | o = get ot_pk_k 3 |
| 468 | for i ulong pl bound 97 |
| 469 | put q i get ot_der_m add o i |
| 470 | end |
| 471 | ret ot_ec_key c part q 0 pl |
| 472 | end |
| 473 | |
| 474 | note ot_pk_ld: load the key of ot_pk_k; false when unsupported or refused by the primitive |
| 475 | fn ot_pk_ld to bool |
| 476 | t = get ot_pk_k 0 |
| 477 | if or eq t 1 eq t 2 |
| 478 | ret ot_pk_rk |
| 479 | end |
| 480 | if or eq t 3 eq t 4 |
| 481 | ret ot_pk_ek |
| 482 | end |
| 483 | ret false |
| 484 | end |
| 485 | |
| 486 | note ot_pk_dig: the digest (alg 0/1/2) of store bytes [p, p + n) into o, zero after it; its length |
| 487 | fn ot_pk_dig a ulong p ulong n ulong o edit uchar 64 to ulong |
| 488 | for i ulong 64 |
| 489 | put o i 0 |
| 490 | end |
| 491 | if eq a 0 |
| 492 | ot_pk_d2 p n o |
| 493 | ret 32 |
| 494 | end |
| 495 | ot_pk_d5 eq a 1 p n o |
| 496 | if eq a 1 |
| 497 | ret 48 |
| 498 | end |
| 499 | ret 64 |
| 500 | end |
| 501 | |
| 502 | note ot_pk_cp: copy at most 4096 store bytes from p into b; the count |
| 503 | fn ot_pk_cp p ulong n ulong b edit uchar 4096 to ulong |
| 504 | k = n |
| 505 | if gt k 4096 |
| 506 | k = 4096 |
| 507 | end |
| 508 | for i ulong k bound 16777216 |
| 509 | put b i get ot_der_m add p i |
| 510 | end |
| 511 | ret k |
| 512 | end |
| 513 | |
| 514 | note ot_pk_d2: SHA-256 (vendored) of store bytes [p, p + n) into o[0..32) |
| 515 | fn ot_pk_d2 p ulong n ulong o edit uchar 64 to void |
| 516 | b = new uchar 4096 0 |
| 517 | c = new uchar 104 0 |
| 518 | d = new uchar 32 0 |
| 519 | sha256_init c |
| 520 | q = p |
| 521 | r = n |
| 522 | while gt r 0 bound 16777216 |
| 523 | k = ot_pk_cp q r b |
| 524 | sha256_update c part b 0 k |
| 525 | q = add q k |
| 526 | r = sub r k |
| 527 | end |
| 528 | sha256_final c d |
| 529 | for i ulong 32 |
| 530 | put o i get d i |
| 531 | end |
| 532 | end |
| 533 | |
| 534 | note ot_pk_d5: SHA-384 (is384) or SHA-512 of store bytes [p, p + n) into o |
| 535 | fn ot_pk_d5 is384 bool p ulong n ulong o edit uchar 64 to void |
| 536 | b = new uchar 4096 0 |
| 537 | s = new ulong 32 0 |
| 538 | ot_s5_init s is384 |
| 539 | q = p |
| 540 | r = n |
| 541 | while gt r 0 bound 16777216 |
| 542 | k = ot_pk_cp q r b |
| 543 | ot_s5_update s part b 0 k |
| 544 | q = add q k |
| 545 | r = sub r k |
| 546 | end |
| 547 | ot_s5_final s o |
| 548 | end |
| 549 | |
| 550 | note ot_pk_fit: the algorithm in ot_pk_a may be used with the key in ot_pk_k |
| 551 | fn ot_pk_fit to bool |
| 552 | a = get ot_pk_a 0 |
| 553 | t = get ot_pk_k 0 |
| 554 | if eq a 1 |
| 555 | ret eq t 1 |
| 556 | end |
| 557 | if eq a 2 |
| 558 | if eq t 1 |
| 559 | ret true |
| 560 | end |
| 561 | if eq t 2 |
| 562 | h = get ot_pk_k 5 |
| 563 | ret or eq h 0 eq h add get ot_pk_a 1 1 |
| 564 | end |
| 565 | ret false |
| 566 | end |
| 567 | if eq a 3 |
| 568 | ret or eq t 3 eq t 4 |
| 569 | end |
| 570 | ret false |
| 571 | end |
| 572 | |
| 573 | note ot_pk_csig: the certificate signature described by ot_pk_q: 0 valid, 5 bad signature (also an |
| 574 | note algorithm that does not suit the key), 8 unsupported algorithm or a key the primitive refuses |
| 575 | fn ot_pk_csig to ulong |
| 576 | s = new uchar 2048 0 |
| 577 | d = new uchar 64 0 |
| 578 | o = get ot_pk_q 0 |
| 579 | if eq ot_pk_sigalg o add o get ot_pk_q 1 false |
| 580 | ret 5 |
| 581 | end |
| 582 | if eq get ot_pk_a 0 0 |
| 583 | ret 8 |
| 584 | end |
| 585 | o = get ot_pk_q 6 |
| 586 | if eq ot_pk_spki o add o get ot_pk_q 7 false |
| 587 | ret 5 |
| 588 | end |
| 589 | if eq get ot_pk_k 0 0 |
| 590 | ret 8 |
| 591 | end |
| 592 | if eq ot_pk_fit false |
| 593 | ret 5 |
| 594 | end |
| 595 | if eq ot_pk_ld false |
| 596 | ret 8 |
| 597 | end |
| 598 | n = get ot_pk_q 5 |
| 599 | if gt n 2048 |
| 600 | ret 5 |
| 601 | end |
| 602 | o = get ot_pk_q 4 |
| 603 | for i ulong n bound 16777216 |
| 604 | put s i get ot_der_m add o i |
| 605 | end |
| 606 | h = get ot_pk_a 1 |
| 607 | k = ot_pk_dig h get ot_pk_q 2 get ot_pk_q 3 d |
| 608 | a = get ot_pk_a 0 |
| 609 | r = false |
| 610 | if eq a 1 |
| 611 | r = ot_rsa_pkcs1 part s 0 n h d |
| 612 | end |
| 613 | if eq a 2 |
| 614 | r = ot_rsa_pss part s 0 n h d get ot_pk_a 2 |
| 615 | end |
| 616 | if eq a 3 |
| 617 | r = ot_ec_verify h d part s 0 n |
| 618 | end |
| 619 | if r |
| 620 | ret 0 |
| 621 | end |
| 622 | ret 5 |
| 623 | end |
| 624 | |
| 625 | note ot_pk_kok: the SPKI at [p, p + n) holds a supported key the primitive accepts (0), else 8 |
| 626 | fn ot_pk_kok p ulong n ulong to ulong |
| 627 | if eq ot_pk_spki p add p n false |
| 628 | ret 8 |
| 629 | end |
| 630 | if eq ot_pk_ld false |
| 631 | ret 8 |
| 632 | end |
| 633 | ret 0 |
| 634 | end |
| 635 | |
| 636 | note ot_pk_tlskey: hold the server's SubjectPublicKeyInfo DER for ot_pk_tls; false when it is |
| 637 | note malformed, unsupported, longer than 64 KiB, or refused by the primitive (e.g. RSA under |
| 638 | note ot_rsa_minbits, a point not on the curve) |
| 639 | export fn ot_pk_tlskey spki view uchar * to bool |
| 640 | n = len spki |
| 641 | put ot_pk_st 0 0 |
| 642 | if or eq n 0 gt n 65536 |
| 643 | ret false |
| 644 | end |
| 645 | for i ulong n bound 16777216 |
| 646 | put ot_der_m add ot_pk_sc i get spki i |
| 647 | end |
| 648 | if eq ot_pk_kok ot_pk_sc n 8 |
| 649 | ret false |
| 650 | end |
| 651 | put ot_pk_st 0 n |
| 652 | ret true |
| 653 | end |
| 654 | |
| 655 | note ot_pk_sch: the hash of a supported CertificateVerify scheme (RFC 8446 4.2.3) and the key type it |
| 656 | note needs (hash + 16 * type), or 0 for a scheme that is not supported |
| 657 | fn ot_pk_sch s ulong to ulong |
| 658 | if eq s 0x0403 |
| 659 | ret 48 |
| 660 | end |
| 661 | if eq s 0x0503 |
| 662 | ret 65 |
| 663 | end |
| 664 | if and ge s 0x0804 le s 0x0806 |
| 665 | ret add 16 sub s 0x0804 |
| 666 | end |
| 667 | if and ge s 0x0809 le s 0x080b |
| 668 | ret add 32 sub s 0x0809 |
| 669 | end |
| 670 | ret 0 |
| 671 | end |
| 672 | |
| 673 | note ot_pk_tls: verify a TLS 1.3 CertificateVerify signature over msg (the whole content to be signed) |
| 674 | note with the key held by ot_pk_tlskey; the scheme must match the key type and curve |
| 675 | export fn ot_pk_tls scheme ulong msg view uchar * sig view uchar * to bool |
| 676 | d = new uchar 64 0 |
| 677 | n = get ot_pk_st 0 |
| 678 | if eq n 0 |
| 679 | ret false |
| 680 | end |
| 681 | w = ot_pk_sch scheme |
| 682 | if eq w 0 |
| 683 | ret false |
| 684 | end |
| 685 | h = and w 15 |
| 686 | if eq ot_pk_kok ot_pk_sc n 8 |
| 687 | ret false |
| 688 | end |
| 689 | t = get ot_pk_k 0 |
| 690 | if ne t shr w 4 |
| 691 | ret false |
| 692 | end |
| 693 | if eq t 2 |
| 694 | k = get ot_pk_k 5 |
| 695 | if and ne k 0 ne k add h 1 |
| 696 | ret false |
| 697 | end |
| 698 | end |
| 699 | l = ot_hash h msg d |
| 700 | if ge t 3 |
| 701 | ret ot_ec_verify h d sig |
| 702 | end |
| 703 | ret ot_rsa_pss sig h d l |
| 704 | end |