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