interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / protocol / interplane / core / envelope.om

493 lines · 10029 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/interplane/core/envelope.om: pure-Omega port of interplane-core envelope,
3note version, identifier validation, strict untrusted-input parser, and canonical trust digest.
4note Invariant: parsing NEVER executes and NEVER grants authorization.
5note Compose after si/directives/om2.om and om/tls/sha2.om.
6
7const IP_OK ulong 0
8const IP_ERR_BAD_LEN ulong 1
9const IP_ERR_BAD_CHAR ulong 2
10const IP_ERR_BAD_VERSION ulong 3
11const IP_ERR_BAD_IDENT ulong 4
12const IP_ERR_BAD_HEX ulong 5
13const IP_ERR_DUP_KEY ulong 6
14const IP_ERR_UNKNOWN_KEY ulong 7
15const IP_ERR_MISSING_FIELD ulong 8
16const IP_ERR_BAD_LINE ulong 9
17const IP_ERR_TOO_MANY_LINES ulong 10
18const IP_ERR_BAD_TRANSPORT ulong 11
19const IP_ERR_BAD_POLICY_HINT ulong 12
20
21const IP_MAX_ENV_BYTES ulong 4096
22const IP_MAX_ENV_LINES ulong 32
23
24const TRANS_UNTRUSTED ulong 0
25const TRANS_TLS ulong 1
26const TRANS_MTLS ulong 2
27const TRANS_LOOPBACK ulong 3
28
29const POL_DENY_ALL ulong 0
30const POL_READ_ONLY ulong 1
31const POL_STANDARD ulong 2
32const POL_ELEVATED ulong 3
33
34text ip_hex_digits "0123456789abcdef"
35
36text ip_k_version "version"
37text ip_k_id "envelope_id"
38text ip_k_origin "origin_id"
39text ip_k_action "action"
40text ip_k_payload "payload_sha256"
41text ip_k_transport "transport"
42text ip_k_claimed "claimed_policy"
43
44text ip_t_untrusted "untrusted"
45text ip_t_tls "tls"
46text ip_t_mtls "mtls"
47text ip_t_loopback "loopback"
48
49note --- Basic slice, byte, and I/O helpers ---
50
51fn ip_slice_eq a view uchar * b view uchar * to bool
52na = len a
53nb = len b
54if ne na nb
55ret false
56end
57if gt na ulong 65536
58ret false
59end
60i = ulong 0
61while lt i na bound 65536
62if ne get a i get b i
63ret false
64end
65i = add i ulong 1
66end
67ret true
68end
69
70fn ip_write_all fd int buf view uchar * to bool
71n = len buf
72if eq n ulong 0
73ret true
74end
75if gt n ulong 1048576
76ret false
77end
78off = ulong 0
79while lt off n bound 1048576
80remn = sub n off
81k = write fd buf off remn
82if le k long 0
83ret false
84end
85off = add off ulong k
86end
87ret true
88end
89
90fn ip_put_u32_le dst edit uchar * pos ulong v uint to ulong
91if gt add pos ulong 4 len dst
92ret pos
93end
94put dst pos uchar and v uint 255
95put dst add pos ulong 1 uchar and shr v uint 8 uint 255
96put dst add pos ulong 2 uchar and shr v uint 16 uint 255
97put dst add pos ulong 3 uchar and shr v uint 24 uint 255
98ret add pos ulong 4
99end
100
101fn ip_put_u64_le dst edit uchar * pos ulong v ulong to ulong
102if gt add pos ulong 8 len dst
103ret pos
104end
105for i ulong 8
106sh = mul i ulong 8
107put dst add pos i uchar and shr v sh ulong 255
108end
109ret add pos ulong 8
110end
111
112fn ip_hex_nibble b uchar to int
113if and ge b uchar 48 le b uchar 57
114ret sub int b int 48
115end
116if and ge b uchar 97 le b uchar 102
117ret add sub int b int 97 int 10
118end
119ret int -1
120end
121
122fn ip_digest_to_hex d view uchar 32 dst edit uchar 64 to void
123for i ulong 32
124b = ulong get d i
125put dst mul i ulong 2 get ip_hex_digits shr b ulong 4
126put dst add mul i ulong 2 ulong 1 get ip_hex_digits and b ulong 15
127end
128end
129
130fn ip_digest_eq a view uchar 32 b view uchar 32 to bool
131for i ulong 32
132if ne get a i get b i
133ret false
134end
135end
136ret true
137end
138
139note --- Strict untrusted-input validators ---
140
141fn ip_is_lower_hex64 s view uchar * to bool
142if ne len s ulong 64
143ret false
144end
145for i ulong 64
146if lt ip_hex_nibble get s i int 0
147ret false
148end
149end
150ret true
151end
152
153note Strict Interplane identifier: 1..64 bytes of [a-z0-9._-], no leading/trailing '.' or '-', no ".."
154fn ip_valid_ident s view uchar * to ulong
155n = len s
156if eq n ulong 0
157ret IP_ERR_BAD_IDENT
158end
159if gt n ulong 64
160ret IP_ERR_BAD_IDENT
161end
162first = get s 0
163last = get s sub n ulong 1
164if or eq first uchar 46 eq first uchar 45
165ret IP_ERR_BAD_IDENT
166end
167if or eq last uchar 46 eq last uchar 45
168ret IP_ERR_BAD_IDENT
169end
170prev_dot = false
171i = ulong 0
172while lt i n bound 64
173b = get s i
174ok = false
175if and ge b uchar 97 le b uchar 122
176ok = true
177end
178if and ge b uchar 48 le b uchar 57
179ok = true
180end
181if or eq b uchar 46 or eq b uchar 95 eq b uchar 45
182ok = true
183end
184if not ok
185ret IP_ERR_BAD_IDENT
186end
187if eq b uchar 46
188if prev_dot
189ret IP_ERR_BAD_IDENT
190end
191prev_dot = true
192else
193prev_dot = false
194end
195i = add i ulong 1
196end
197ret IP_OK
198end
199
200note Strict protocol version: "major.minor", digits only, no leading zeros (except "0"), major in 1..1
201fn ip_parse_ver_part s view uchar * to long
202n = len s
203if eq n ulong 0
204ret long -1
205end
206if gt n ulong 5
207ret long -1
208end
209if gt n ulong 1
210if eq get s 0 uchar 48
211ret long -1
212end
213end
214acc = ulong 0
215i = ulong 0
216while lt i n bound 5
217b = get s i
218if or lt b uchar 48 gt b uchar 57
219ret long -1
220end
221acc = add mul acc ulong 10 sub ulong b ulong 48
222i = add i ulong 1
223end
224if gt acc ulong 65535
225ret long -1
226end
227ret long acc
228end
229
230fn ip_validate_version s view uchar * to ulong
231n = len s
232if lt n ulong 3
233ret IP_ERR_BAD_VERSION
234end
235if gt n ulong 11
236ret IP_ERR_BAD_VERSION
237end
238dot = ulong 999
239i = ulong 0
240while lt i n bound 11
241if eq get s i uchar 46
242if ne dot ulong 999
243ret IP_ERR_BAD_VERSION
244end
245dot = i
246end
247i = add i ulong 1
248end
249if eq dot ulong 999
250ret IP_ERR_BAD_VERSION
251end
252maj = ip_parse_ver_part part s 0 dot
253min = ip_parse_ver_part part s add dot ulong 1 sub sub n dot ulong 1
254if or lt maj long 0 lt min long 0
255ret IP_ERR_BAD_VERSION
256end
257if ne maj long 1
258ret IP_ERR_BAD_VERSION
259end
260ret IP_OK
261end
262
263fn ip_parse_transport s view uchar * to long
264if ip_slice_eq s ip_t_untrusted
265ret long TRANS_UNTRUSTED
266end
267if ip_slice_eq s ip_t_tls
268ret long TRANS_TLS
269end
270if ip_slice_eq s ip_t_mtls
271ret long TRANS_MTLS
272end
273if ip_slice_eq s ip_t_loopback
274ret long TRANS_LOOPBACK
275end
276ret long -1
277end
278
279fn ip_parse_policy_hint s view uchar * to long
280if ne len s ulong 1
281ret long -1
282end
283b = get s 0
284if or lt b uchar 48 gt b uchar 51
285ret long -1
286end
287ret long sub ulong b ulong 48
288end
289
290note --- Canonical length-prefixed SHA-256 trust digest helper ---
291
292fn ip_hash_lp_field ctx edit ulong 40 fld view uchar * to void
293lb = new uchar 4 0
294w = ip_put_u32_le lb ulong 0 uint len fld
295if ne w ulong 4
296trap 8
297end
298sha256_update ctx lb
299sha256_update ctx fld
300end
301
302note --- Untrusted Wire Envelope Parser ---
303note Parses key=value lines separated by '\n'. Rejects control chars, NULs, duplicate keys,
304note unknown keys, and missing required fields.
305note meta[0] = seen_mask, meta[1] = transport_class, meta[2] = claimed_policy (hint only!),
306note meta[3] = lifecycle_state (always set to 1=PARSED on IP_OK, NEVER authorized/executed).
307note Also computes canonical trust digest over the 5 core fields into digest_out[32].
308
309fn ip_check_printable_line line view uchar * to bool
310n = len line
311if gt n ulong 512
312ret false
313end
314i = ulong 0
315while lt i n bound 512
316b = get line i
317if or lt b uchar 32 ge b uchar 127
318ret false
319end
320i = add i ulong 1
321end
322ret true
323end
324
325fn ip_find_eq line view uchar * to ulong
326n = len line
327if gt n ulong 512
328ret ulong 9999
329end
330i = ulong 0
331while lt i n bound 512
332if eq get line i uchar 61
333ret i
334end
335i = add i ulong 1
336end
337ret ulong 9999
338end
339
340note Field bitmask: 1=version, 2=envelope_id, 4=origin_id, 8=action, 16=payload_sha256,
341note 32=transport, 64=claimed_policy. Required mask = 31 (bits 0..4).
342fn ip_parse_line line view uchar * meta edit ulong 4 ctx edit ulong 40 to ulong
343if not ip_check_printable_line line
344ret IP_ERR_BAD_CHAR
345end
346eqp = ip_find_eq line
347if eq eqp ulong 9999
348ret IP_ERR_BAD_LINE
349end
350if eq eqp ulong 0
351ret IP_ERR_BAD_LINE
352end
353klen = eqp
354voff = add eqp ulong 1
355vlen = sub len line voff
356seen = get meta 0
357if ip_slice_eq part line 0 klen ip_k_version
358if ne and seen ulong 1 ulong 0
359ret IP_ERR_DUP_KEY
360end
361rc = ip_validate_version part line voff vlen
362if ne rc IP_OK
363ret rc
364end
365put meta 0 or seen ulong 1
366ip_hash_lp_field ctx part line voff vlen
367ret IP_OK
368end
369if ip_slice_eq part line 0 klen ip_k_id
370if ne and seen ulong 2 ulong 0
371ret IP_ERR_DUP_KEY
372end
373rc = ip_valid_ident part line voff vlen
374if ne rc IP_OK
375ret rc
376end
377put meta 0 or seen ulong 2
378ip_hash_lp_field ctx part line voff vlen
379ret IP_OK
380end
381if ip_slice_eq part line 0 klen ip_k_origin
382if ne and seen ulong 4 ulong 0
383ret IP_ERR_DUP_KEY
384end
385rc = ip_valid_ident part line voff vlen
386if ne rc IP_OK
387ret rc
388end
389put meta 0 or seen ulong 4
390ip_hash_lp_field ctx part line voff vlen
391ret IP_OK
392end
393if ip_slice_eq part line 0 klen ip_k_action
394if ne and seen ulong 8 ulong 0
395ret IP_ERR_DUP_KEY
396end
397rc = ip_valid_ident part line voff vlen
398if ne rc IP_OK
399ret rc
400end
401put meta 0 or seen ulong 8
402ip_hash_lp_field ctx part line voff vlen
403ret IP_OK
404end
405if ip_slice_eq part line 0 klen ip_k_payload
406if ne and seen ulong 16 ulong 0
407ret IP_ERR_DUP_KEY
408end
409if not ip_is_lower_hex64 part line voff vlen
410ret IP_ERR_BAD_HEX
411end
412put meta 0 or seen ulong 16
413ip_hash_lp_field ctx part line voff vlen
414ret IP_OK
415end
416if ip_slice_eq part line 0 klen ip_k_transport
417if ne and seen ulong 32 ulong 0
418ret IP_ERR_DUP_KEY
419end
420tr = ip_parse_transport part line voff vlen
421if lt tr long 0
422ret IP_ERR_BAD_TRANSPORT
423end
424put meta 0 or seen ulong 32
425put meta 1 ulong tr
426ret IP_OK
427end
428if ip_slice_eq part line 0 klen ip_k_claimed
429if ne and seen ulong 64 ulong 0
430ret IP_ERR_DUP_KEY
431end
432ph = ip_parse_policy_hint part line voff vlen
433if lt ph long 0
434ret IP_ERR_BAD_POLICY_HINT
435end
436put meta 0 or seen ulong 64
437put meta 2 ulong ph
438ret IP_OK
439end
440ret IP_ERR_UNKNOWN_KEY
441end
442
443fn ip_parse_envelope raw view uchar * meta edit ulong 4 digest_out edit uchar 32 to ulong
444put meta 0 ulong 0
445put meta 1 TRANS_UNTRUSTED
446put meta 2 POL_DENY_ALL
447put meta 3 ulong 0
448n = len raw
449if eq n ulong 0
450ret IP_ERR_BAD_LEN
451end
452if gt n IP_MAX_ENV_BYTES
453ret IP_ERR_BAD_LEN
454end
455ctx = new ulong 40 0
456sha256_init ctx
457lines = ulong 0
458start = ulong 0
459i = ulong 0
460while le i n bound 4097
461at_nl = false
462if eq i n
463at_nl = true
464else
465if eq get raw i uchar 10
466at_nl = true
467end
468end
469if at_nl
470llen = sub i start
471if gt llen ulong 0
472lines = add lines ulong 1
473if gt lines IP_MAX_ENV_LINES
474ret IP_ERR_TOO_MANY_LINES
475end
476rc = ip_parse_line part raw start llen meta ctx
477if ne rc IP_OK
478ret rc
479end
480end
481start = add i ulong 1
482end
483i = add i ulong 1
484end
485req_mask = ulong 31
486if ne and get meta 0 req_mask req_mask
487ret IP_ERR_MISSING_FIELD
488end
489sha256_final ctx digest_out
490note Parsing NEVER authorizes or executes: lifecycle state is strictly 1 (LC_PARSED).
491put meta 3 ulong 1
492ret IP_OK
493end