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

111 lines · 2801 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note pem.om (ot_pem_): RFC 7468 CERTIFICATE blocks in the store ot_der_m.
3note A block is a line that starts "-----BEGIN CERTIFICATE-----" (then optional spaces or tabs, then
4note the line end), a base64 body (ot_b64_dec rules: whitespace and line breaks allowed, nothing
5note else, so RFC 1421 headers are refused), and a line that starts "-----END CERTIFICATE-----".
6note Text between blocks is skipped (RFC 7468 section 2 explanatory text) unless a line starts with
7note "-----BEGIN " and another label: such a block is a refusal, as is a body without its END line.
8note Unit order: src/der.om must come earlier in the unit (a global is declared before its first use).
9text ot_pem_tb "-----BEGIN "
10text ot_pem_te "-----END "
11text ot_pem_tc "CERTIFICATE-----"
12note ot_pem_r: the last block found: 0 body start, 1 body end, 2 offset after the END line
13global ot_pem_r ulong 3
14
15note ot_pem_is: the store bytes at p (inside [p, e)) start with t
16fn ot_pem_is p ulong e ulong t view uchar * to bool
17n = len t
18if gt n sub e p
19ret false
20end
21for i ulong n bound 16777216
22if ne get ot_der_m add p i get t i
23ret false
24end
25end
26ret true
27end
28
29note ot_pem_eol: offset after the line end that follows p, allowing spaces and tabs before it;
30note e when the input ends first; 18446744073709551615 when another byte comes first
31fn ot_pem_eol p ulong e ulong to ulong
32i = p
33while lt i e bound 16777216
34c = get ot_der_m i
35if eq c 10
36ret add i 1
37end
38if eq c 13
39if lt add i 1 e
40if eq get ot_der_m add i 1 10
41ret add i 2
42end
43end
44ret 18446744073709551615
45end
46if and ne c 32 ne c 9
47ret 18446744073709551615
48end
49i = add i 1
50end
51ret e
52end
53
54note ot_pem_nl: offset of the start of the next line after p, or e
55fn ot_pem_nl p ulong e ulong to ulong
56i = p
57while lt i e bound 16777216
58if eq get ot_der_m i 10
59ret add i 1
60end
61i = add i 1
62end
63ret e
64end
65
66note ot_pem_next: from line start p, find the next block in [p, e). 1 found (ot_pem_r set),
67note 0 no further block, -1 malformed (other label, bad BEGIN or END line, or no END line)
68fn ot_pem_next p ulong e ulong to long
69i = p
70while lt i e bound 16777216
71if ot_pem_is i e ot_pem_tb
72j = add i len ot_pem_tb
73if eq ot_pem_is j e ot_pem_tc false
74ret -1
75end
76b = ot_pem_eol add j len ot_pem_tc e
77if eq b 18446744073709551615
78ret -1
79end
80ret ot_pem_body b e
81end
82i = ot_pem_nl i e
83end
84ret 0
85end
86
87note ot_pem_body: body from b; the END line must start a line before e
88fn ot_pem_body b ulong e ulong to long
89i = b
90while lt i e bound 16777216
91if ot_pem_is i e ot_pem_te
92j = add i len ot_pem_te
93if eq ot_pem_is j e ot_pem_tc false
94ret -1
95end
96f = ot_pem_eol add j len ot_pem_tc e
97if eq f 18446744073709551615
98ret -1
99end
100put ot_pem_r 0 b
101put ot_pem_r 1 i
102put ot_pem_r 2 f
103ret 1
104end
105if ot_pem_is i e ot_pem_tb
106ret -1
107end
108i = ot_pem_nl i e
109end
110ret -1
111end