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 / der.om

209 lines · 5000 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note der.om (ot_der_): strict DER reader (ITU-T X.690 08/2015 section 10 on top of section 8).
3note Omega never passes a global (error 19), not even a part of one, so every reader here works on
4note offsets into the one byte store ot_der_m and names it directly. x509.om owns the store layout
5note (certificate records, the roots region, the presented chain, the bundle text); test drivers may
6note use it as scratch. 16 MiB is the store; nothing else in the X.509 layer holds certificate bytes.
7note Public data only: these readers branch on the bytes they read (no secrets pass through them).
8global ot_der_m uchar 16777216
9note ot_der_r: the last header read by ot_der_rd: tag, value offset, value length, end offset
10global ot_der_r ulong 4
11const ot_der_cap ulong 16777216
12
13note ot_der_rd: one TLV wholly inside [p, e). Refused (false): p >= e; the high tag number form
14note (low five tag bits all ones, X.690 8.1.2.4: X.509 never needs it); the indefinite length 0x80
15note (10.1); more than 4 length bytes; a long form that is not minimal (10.1: first length byte 0,
16note or a length below 128 in long form); a value that runs past e.
17fn ot_der_rd p ulong e ulong to bool
18if ge p e
19ret false
20end
21t = get ot_der_m p
22if eq and t 31 31
23ret false
24end
25q = add p 1
26if ge q e
27ret false
28end
29b = get ot_der_m q
30q = add q 1
31n = ulong 0
32if lt b 128
33n = ulong b
34else
35k = ulong and b 127
36if or eq k 0 gt k 4
37ret false
38end
39if gt k sub e q
40ret false
41end
42if eq get ot_der_m q 0
43ret false
44end
45for i ulong k bound 16777216
46n = or shl n 8 ulong get ot_der_m add q i
47end
48q = add q k
49if lt n 128
50ret false
51end
52end
53if gt n sub e q
54ret false
55end
56put ot_der_r 0 ulong t
57put ot_der_r 1 q
58put ot_der_r 2 n
59put ot_der_r 3 add q n
60ret true
61end
62
63note ot_der_ex: ot_der_rd and the tag byte is exactly t
64fn ot_der_ex p ulong e ulong t ulong to bool
65if eq ot_der_rd p e false
66ret false
67end
68ret eq get ot_der_r 0 t
69end
70
71fn ot_der_tg to ulong
72ret get ot_der_r 0
73end
74fn ot_der_vo to ulong
75ret get ot_der_r 1
76end
77fn ot_der_vl to ulong
78ret get ot_der_r 2
79end
80fn ot_der_nd to ulong
81ret get ot_der_r 3
82end
83
84note ot_der_okint: INTEGER contents at v (n bytes) are present and minimal (X.690 8.3.2): the first
85note nine bits are neither all zeros nor all ones
86fn ot_der_okint v ulong n ulong to bool
87if eq n 0
88ret false
89end
90if eq n 1
91ret true
92end
93a = get ot_der_m v
94b = get ot_der_m add v 1
95if and eq a 0 lt b 128
96ret false
97end
98if and eq a 255 ge b 128
99ret false
100end
101ret true
102end
103
104note ot_der_okbool: BOOLEAN contents in DER are exactly one byte 0x00 or 0xFF (X.690 11.1)
105fn ot_der_okbool v ulong n ulong to bool
106if ne n 1
107ret false
108end
109a = get ot_der_m v
110ret or eq a 0 eq a 255
111end
112
113note ot_der_eq: the n bytes at a and at b are equal
114fn ot_der_eq a ulong b ulong n ulong to bool
115for i ulong n bound 16777216
116if ne get ot_der_m add a i get ot_der_m add b i
117ret false
118end
119end
120ret true
121end
122
123note ot_der_oid: the n content bytes at v are exactly the k-byte OID encoding packed big-endian into
124note the low k bytes of the 128-bit value hi:lo (k <= 16). Omega texts cannot hold bytes above 127,
125note so OID constants are written as two hex numbers at each call.
126fn ot_der_oid v ulong n ulong k ulong hi ulong lo ulong to bool
127if or ne n k gt k 16
128ret false
129end
130for i ulong k bound 16777216
131if ne get ot_der_m add v i ot_der_byt hi lo add i sub 16 k
132ret false
133end
134end
135ret true
136end
137
138note ot_der_byt: byte j (0 = most significant) of the 128-bit value hi:lo
139fn ot_der_byt hi ulong lo ulong j ulong to uchar
140if lt j 8
141ret uchar and shr hi mul sub 7 j 8 255
142end
143ret uchar and shr lo mul sub 15 j 8 255
144end
145
146note ot_der_okoid: OBJECT IDENTIFIER contents (X.690 8.19): non-empty, the last byte ends a
147note subidentifier (top bit clear), and no subidentifier starts with the padding byte 0x80
148fn ot_der_okoid v ulong n ulong to bool
149if eq n 0
150ret false
151end
152if ge get ot_der_m add v sub n 1 128
153ret false
154end
155st = true
156for i ulong n bound 16777216
157b = get ot_der_m add v i
158if and st eq b 128
159ret false
160end
161st = lt b 128
162end
163ret true
164end
165
166note ot_der_okbits: BIT STRING contents (X.690 8.6.2, 11.2): a first byte 0..7 giving the unused
167note bits, 0 when there are no further bytes, and the unused bits of the last byte are zero (11.2.1).
168fn ot_der_okbits v ulong n ulong to bool
169if eq n 0
170ret false
171end
172u = get ot_der_m v
173if gt u 7
174ret false
175end
176if eq n 1
177ret eq u 0
178end
179if eq u 0
180ret true
181end
182z = get ot_der_m add v sub n 1
183ret eq and z sub shl uchar 1 u 1 0
184end
185
186note ot_der_small: a minimal non-negative INTEGER of at most 8 value bytes as a ulong, or
187note 18446744073709551615 (never a valid small value here) when it is negative, too long or not minimal
188fn ot_der_small v ulong n ulong to ulong
189if eq ot_der_okint v n false
190ret 18446744073709551615
191end
192if ge get ot_der_m v 128
193ret 18446744073709551615
194end
195m = n
196o = v
197if eq get ot_der_m v 0
198m = sub n 1
199o = add v 1
200end
201if gt m 8
202ret 18446744073709551615
203end
204x = ulong 0
205for i ulong m bound 16777216
206x = or shl x 8 ulong get ot_der_m add o i
207end
208ret x
209end