interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omtls / src / pkv.om

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