interplane.Get Omega
Read in your language

open-humanity

Privacy-first peer assistance network for sovereign autonomous agents

Brought by drakestapleton

branch main Branches Tags History

open-humanity / om / lib.om

1416 lines · 29923 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/open-humanity/lib.om: pure-Omega opt-in, privacy-first peer assistance protocol.
3note Derived from open-humanity (beacon-core wire/schema/firewall + beacon relay queue/audit).
4note Enforces explicit opt-in consent, strict zero-authority boundary (never grants local exec,
5note secret access, identity mutation, or admin rights), deterministic Personal Data Firewall
6note with obfuscation decoding and path scrubbing, TTL/replay guards, bounded peer assistance
7note queue, and append-only resolution audit trail. Library unit: no om2 directive, no fn main.
8
9const OH_MAGIC_0 ulong 0x4f
10const OH_MAGIC_1 ulong 0x48
11const OH_VERSION ulong 1
12const OH_HDR_LEN ulong 48
13const OH_MAX_PAYLOAD ulong 512
14const OH_MAX_WIRE ulong 560
15
16const OH_CAT_RUST ulong 0
17const OH_CAT_SIMD ulong 1
18const OH_CAT_SERVING ulong 2
19const OH_CAT_HW ulong 3
20const OH_CAT_SEC ulong 4
21const OH_CAT_PROTO ulong 5
22const OH_CAT_AGENT ulong 6
23const OH_CAT_MAX ulong 6
24
25const OH_MAX_TTL_MS ulong 604800000
26const OH_MAX_FUTURE_DRIFT_MS ulong 300000
27
28const OH_CAP_NONE ulong 0
29const OH_CAP_LOCAL_EXEC ulong 1
30const OH_CAP_SECRET_ACCESS ulong 2
31const OH_CAP_IDENTITY_MUTATE ulong 4
32const OH_CAP_ADMIN_RIGHTS ulong 8
33
34const OH_OK ulong 0
35const OH_ERR_BAD_MAGIC ulong 1
36const OH_ERR_BAD_VERSION ulong 2
37const OH_ERR_TRUNCATED ulong 3
38const OH_ERR_BAD_DIGEST ulong 4
39const OH_ERR_BAD_PEER ulong 5
40const OH_ERR_BAD_CATEGORY ulong 6
41const OH_ERR_NODE_OPT_OUT ulong 10
42const OH_ERR_NO_CONSENT ulong 11
43const OH_ERR_TTL_INVALID ulong 20
44const OH_ERR_TTL_EXPIRED ulong 21
45const OH_ERR_FUTURE_DRIFT ulong 22
46const OH_ERR_AUTH_EXEC ulong 30
47const OH_ERR_AUTH_SECRET ulong 31
48const OH_ERR_AUTH_IDENTITY ulong 32
49const OH_ERR_AUTH_ADMIN ulong 33
50const OH_ERR_AUTH_UNKNOWN ulong 34
51const OH_ERR_AUTH_PAYLOAD_ESC ulong 35
52const OH_ERR_PRIV_AWS_KEY ulong 40
53const OH_ERR_PRIV_GITHUB_TOKEN ulong 41
54const OH_ERR_PRIV_ANTHROPIC_KEY ulong 42
55const OH_ERR_PRIV_OPENAI_KEY ulong 43
56const OH_ERR_PRIV_GOOGLE_KEY ulong 44
57const OH_ERR_PRIV_STRIPE_KEY ulong 45
58const OH_ERR_PRIV_HF_TOKEN ulong 46
59const OH_ERR_PRIV_PEM_KEY ulong 47
60const OH_ERR_PRIV_GENERIC_SECRET ulong 48
61const OH_ERR_PRIV_USER_PATH ulong 49
62const OH_ERR_PRIV_TRAVERSAL ulong 50
63const OH_ERR_REPLAY ulong 60
64const OH_ERR_QUEUE_FULL ulong 61
65const OH_ERR_REQ_NOT_FOUND ulong 62
66const OH_ERR_REQ_ALREADY_RESOLVED ulong 63
67const OH_ERR_SELF_RESOLVE ulong 64
68const OH_ERR_EMPTY_PAYLOAD ulong 65
69
70const OH_QUEUE_CAP ulong 32
71const OH_AUDIT_CAP ulong 64
72const OH_SEEN_CAP ulong 64
73
74const OH_Q_EMPTY ulong 0
75const OH_Q_PENDING ulong 1
76const OH_Q_RESOLVED ulong 2
77const OH_Q_EXPIRED ulong 3
78
79const OH_AUD_ENQUEUED ulong 1
80const OH_AUD_RESOLVED ulong 2
81const OH_AUD_REFUSED ulong 3
82const OH_AUD_EXPIRED ulong 4
83
84struct OhEnv version ulong peer_id ulong consent ulong category ulong created_ms ulong ttl_ms ulong cap_flags ulong digest ulong payload_len ulong end
85struct OhPolicy opt_in ulong now_ms ulong next_req_id ulong audit_seq ulong seen_head ulong end
86
87global oh_q_status ulong 32
88global oh_q_req_id ulong 32
89global oh_q_peer_id ulong 32
90global oh_q_cat ulong 32
91global oh_q_created ulong 32
92global oh_q_ttl ulong 32
93global oh_q_digest ulong 32
94global oh_q_helper ulong 32
95global oh_q_plen ulong 32
96global oh_q_payload uchar 16384
97
98global oh_seen_peer ulong 64
99global oh_seen_dig ulong 64
100
101global oh_aud_seq ulong 64
102global oh_aud_kind ulong 64
103global oh_aud_peer ulong 64
104global oh_aud_req ulong 64
105global oh_aud_cat ulong 64
106global oh_aud_code ulong 64
107global oh_aud_ts ulong 64
108global oh_aud_dig ulong 64
109global oh_aud_cnt ulong 1
110
111text oh_hex_chars "0123456789abcdef"
112text oh_t_pem_beg "-----BEGIN "
113text oh_t_pem_end "PRIVATE KEY-----"
114text oh_t_akia "AKIA"
115text oh_t_sk_ant "sk-ant-"
116text oh_t_sk "sk-"
117text oh_t_aiza "AIza"
118text oh_t_hf "hf_"
119text oh_t_live "live_"
120text oh_t_test "test_"
121text oh_t_home "/home/"
122text oh_t_users "/Users/"
123text oh_t_wusers ":\\Users\\"
124text oh_t_wusers2 ":/Users/"
125text oh_t_ak_api "api_key"
126text oh_t_ak_apikey "apikey"
127text oh_t_ak_sec "secret_key"
128text oh_t_ak_priv "private_key"
129text oh_t_ak_auth "auth_token"
130text oh_t_ak_bear "bearer"
131text oh_t_esc_exec "exec:"
132text oh_t_esc_sudo "sudo "
133text oh_t_esc_chmod "chmod "
134text oh_t_esc_chown "chown "
135text oh_t_esc_ident "open_humanity_identity"
136text oh_t_esc_seed "identity_seed="
137text oh_t_esc_admin "grant_admin"
138
139text oh_r_ok "ok"
140text oh_r_magic "bad_magic"
141text oh_r_ver "bad_version"
142text oh_r_trunc "truncated"
143text oh_r_dig "bad_digest"
144text oh_r_peer "bad_peer_id"
145text oh_r_cat "bad_category"
146text oh_r_optout "node_opt_out"
147text oh_r_noconsent "missing_consent"
148text oh_r_ttlinv "ttl_invalid"
149text oh_r_ttlexp "ttl_expired"
150text oh_r_drift "future_timestamp_drift"
151text oh_r_aexec "authority_refused_local_exec"
152text oh_r_asec "authority_refused_secret_access"
153text oh_r_aid "authority_refused_identity_mutate"
154text oh_r_aadm "authority_refused_admin_rights"
155text oh_r_aunk "authority_refused_unknown_cap"
156text oh_r_aesc "authority_refused_payload_escalation"
157text oh_r_paws "privacy_refused_aws_key"
158text oh_r_pgh "privacy_refused_github_token"
159text oh_r_pant "privacy_refused_anthropic_key"
160text oh_r_poai "privacy_refused_openai_key"
161text oh_r_pgoo "privacy_refused_google_key"
162text oh_r_pstr "privacy_refused_stripe_key"
163text oh_r_phf "privacy_refused_hf_token"
164text oh_r_ppem "privacy_refused_private_key_block"
165text oh_r_pgen "privacy_refused_generic_secret"
166text oh_r_ppath "privacy_refused_private_user_path"
167text oh_r_ptrav "privacy_refused_path_traversal"
168text oh_r_rep "replay_detected"
169text oh_r_qfull "queue_full"
170text oh_r_nf "request_not_found"
171text oh_r_alr "request_already_resolved"
172text oh_r_self "self_resolution_refused"
173text oh_r_empty "empty_payload"
174text oh_r_unk "unknown_error"
175
176fn oh_write_all fd int buf view uchar * to bool
177n = len buf
178pos = ulong 0
179while lt pos n bound 65536
180k = write fd buf pos sub n pos
181if le k 0
182ret false
183end
184pos = add pos ulong k
185end
186ret true
187end
188
189fn oh_write_u64 fd int v ulong to void
190digits = new uchar 24 0
191if eq v 0
192put digits 0 uchar 48
193ok0 = oh_write_all fd part digits 0 1
194if ok0
195ret
196end
197ret
198end
199tmp = new uchar 24 0
200cnt = ulong 0
201x = v
202while gt x 0 bound 24
203d = rem x 10
204put tmp cnt uchar add 48 d
205cnt = add cnt 1
206x = div x 10
207end
208i = ulong 0
209while lt i cnt bound 24
210put digits i get tmp sub sub cnt 1 i
211i = add i 1
212end
213ok1 = oh_write_all fd part digits 0 cnt
214if ok1
215ret
216end
217end
218
219fn oh_write_hex64 fd int v ulong to void
220buf = new uchar 16 48
221i = ulong 0
222while lt i 16 bound 16
223sh = mul sub 15 i 4
224nib = and shr v sh 0xf
225put buf i get oh_hex_chars nib
226i = add i 1
227end
228ok = oh_write_all fd buf
229if ok
230ret
231end
232end
233
234fn oh_write_reason fd int code ulong to void
235ok = false
236if eq code OH_OK
237ok = oh_write_all fd oh_r_ok
238else
239if eq code OH_ERR_BAD_MAGIC
240ok = oh_write_all fd oh_r_magic
241else
242if eq code OH_ERR_BAD_VERSION
243ok = oh_write_all fd oh_r_ver
244else
245if eq code OH_ERR_TRUNCATED
246ok = oh_write_all fd oh_r_trunc
247else
248if eq code OH_ERR_BAD_DIGEST
249ok = oh_write_all fd oh_r_dig
250else
251if eq code OH_ERR_BAD_PEER
252ok = oh_write_all fd oh_r_peer
253else
254if eq code OH_ERR_BAD_CATEGORY
255ok = oh_write_all fd oh_r_cat
256else
257if eq code OH_ERR_NODE_OPT_OUT
258ok = oh_write_all fd oh_r_optout
259else
260if eq code OH_ERR_NO_CONSENT
261ok = oh_write_all fd oh_r_noconsent
262else
263if eq code OH_ERR_TTL_INVALID
264ok = oh_write_all fd oh_r_ttlinv
265else
266if eq code OH_ERR_TTL_EXPIRED
267ok = oh_write_all fd oh_r_ttlexp
268else
269if eq code OH_ERR_FUTURE_DRIFT
270ok = oh_write_all fd oh_r_drift
271else
272ok = oh_write_reason_hi fd code
273end
274end
275end
276end
277end
278end
279end
280end
281end
282end
283end
284end
285if ok
286ret
287end
288end
289
290fn oh_write_reason_hi fd int code ulong to bool
291if eq code OH_ERR_AUTH_EXEC
292ret oh_write_all fd oh_r_aexec
293end
294if eq code OH_ERR_AUTH_SECRET
295ret oh_write_all fd oh_r_asec
296end
297if eq code OH_ERR_AUTH_IDENTITY
298ret oh_write_all fd oh_r_aid
299end
300if eq code OH_ERR_AUTH_ADMIN
301ret oh_write_all fd oh_r_aadm
302end
303if eq code OH_ERR_AUTH_UNKNOWN
304ret oh_write_all fd oh_r_aunk
305end
306if eq code OH_ERR_AUTH_PAYLOAD_ESC
307ret oh_write_all fd oh_r_aesc
308end
309if eq code OH_ERR_PRIV_AWS_KEY
310ret oh_write_all fd oh_r_paws
311end
312if eq code OH_ERR_PRIV_GITHUB_TOKEN
313ret oh_write_all fd oh_r_pgh
314end
315if eq code OH_ERR_PRIV_ANTHROPIC_KEY
316ret oh_write_all fd oh_r_pant
317end
318if eq code OH_ERR_PRIV_OPENAI_KEY
319ret oh_write_all fd oh_r_poai
320end
321if eq code OH_ERR_PRIV_GOOGLE_KEY
322ret oh_write_all fd oh_r_pgoo
323end
324if eq code OH_ERR_PRIV_STRIPE_KEY
325ret oh_write_all fd oh_r_pstr
326end
327if eq code OH_ERR_PRIV_HF_TOKEN
328ret oh_write_all fd oh_r_phf
329end
330if eq code OH_ERR_PRIV_PEM_KEY
331ret oh_write_all fd oh_r_ppem
332end
333if eq code OH_ERR_PRIV_GENERIC_SECRET
334ret oh_write_all fd oh_r_pgen
335end
336if eq code OH_ERR_PRIV_USER_PATH
337ret oh_write_all fd oh_r_ppath
338end
339if eq code OH_ERR_PRIV_TRAVERSAL
340ret oh_write_all fd oh_r_ptrav
341end
342if eq code OH_ERR_REPLAY
343ret oh_write_all fd oh_r_rep
344end
345if eq code OH_ERR_QUEUE_FULL
346ret oh_write_all fd oh_r_qfull
347end
348if eq code OH_ERR_REQ_NOT_FOUND
349ret oh_write_all fd oh_r_nf
350end
351if eq code OH_ERR_REQ_ALREADY_RESOLVED
352ret oh_write_all fd oh_r_alr
353end
354if eq code OH_ERR_SELF_RESOLVE
355ret oh_write_all fd oh_r_self
356end
357if eq code OH_ERR_EMPTY_PAYLOAD
358ret oh_write_all fd oh_r_empty
359end
360ret oh_write_all fd oh_r_unk
361end
362
363fn oh_reset_state pol edit OhPolicy to void
364put pol opt_in ulong 0
365put pol now_ms ulong 0
366put pol next_req_id ulong 1
367put pol audit_seq ulong 0
368put pol seen_head ulong 0
369i = ulong 0
370while lt i OH_QUEUE_CAP bound 64
371put oh_q_status i OH_Q_EMPTY
372put oh_q_req_id i ulong 0
373put oh_q_peer_id i ulong 0
374put oh_q_cat i ulong 0
375put oh_q_created i ulong 0
376put oh_q_ttl i ulong 0
377put oh_q_digest i ulong 0
378put oh_q_helper i ulong 0
379put oh_q_plen i ulong 0
380i = add i 1
381end
382j = ulong 0
383while lt j OH_SEEN_CAP bound 64
384put oh_seen_peer j ulong 0
385put oh_seen_dig j ulong 0
386j = add j 1
387end
388k = ulong 0
389while lt k OH_AUDIT_CAP bound 64
390put oh_aud_seq k ulong 0
391put oh_aud_kind k ulong 0
392put oh_aud_peer k ulong 0
393put oh_aud_req k ulong 0
394put oh_aud_cat k ulong 0
395put oh_aud_code k ulong 0
396put oh_aud_ts k ulong 0
397put oh_aud_dig k ulong 0
398k = add k 1
399end
400put oh_aud_cnt 0 ulong 0
401end
402
403fn oh_fnv1a_byte h ulong b uchar to ulong
404x = xor h ulong b
405ret mul x ulong 0x100000001b3
406end
407
408fn oh_fnv1a_u64 h ulong v ulong to ulong
409acc = h
410i = ulong 0
411while lt i 8 bound 8
412b = uchar and shr v mul i 8 0xff
413acc = oh_fnv1a_byte acc b
414i = add i 1
415end
416ret acc
417end
418
419fn oh_compute_digest env view OhEnv payload view uchar * to ulong
420h = ulong 0xcbf29ce484222325
421h = oh_fnv1a_byte h uchar OH_MAGIC_0
422h = oh_fnv1a_byte h uchar OH_MAGIC_1
423h = oh_fnv1a_byte h uchar and get env version 0xff
424h = oh_fnv1a_byte h uchar and get env consent 0xff
425h = oh_fnv1a_byte h uchar and get env category 0xff
426h = oh_fnv1a_byte h uchar 0
427h = oh_fnv1a_u64 h get env peer_id
428h = oh_fnv1a_u64 h get env created_ms
429h = oh_fnv1a_u64 h get env ttl_ms
430h = oh_fnv1a_u64 h get env cap_flags
431n = len payload
432h = oh_fnv1a_u64 h n
433i = ulong 0
434while lt i n bound 1024
435h = oh_fnv1a_byte h get payload i
436i = add i 1
437end
438ret h
439end
440
441fn oh_put_u64_le dst edit uchar * off ulong v ulong to void
442i = ulong 0
443while lt i 8 bound 8
444put dst add off i uchar and shr v mul i 8 0xff
445i = add i 1
446end
447end
448
449fn oh_get_u64_le src view uchar * off ulong to ulong
450acc = ulong 0
451i = ulong 0
452while lt i 8 bound 8
453b = ulong get src add off i
454acc = or acc shl b mul i 8
455i = add i 1
456end
457ret acc
458end
459
460fn oh_serialize_envelope env view OhEnv payload view uchar * dst edit uchar * to ulong
461plen = len payload
462if gt plen OH_MAX_PAYLOAD
463ret ulong 0
464end
465total = add OH_HDR_LEN plen
466if gt total len dst
467ret ulong 0
468end
469dig = oh_compute_digest env payload
470put dst 0 uchar OH_MAGIC_0
471put dst 1 uchar OH_MAGIC_1
472put dst 2 uchar and get env version 0xff
473put dst 3 uchar and get env consent 0xff
474put dst 4 uchar and get env category 0xff
475put dst 5 uchar 0
476oh_put_u64_le dst 6 get env peer_id
477oh_put_u64_le dst 14 get env created_ms
478oh_put_u64_le dst 22 get env ttl_ms
479oh_put_u64_le dst 30 get env cap_flags
480oh_put_u64_le dst 38 dig
481put dst 46 uchar and plen 0xff
482put dst 47 uchar and shr plen 8 0xff
483i = ulong 0
484while lt i plen bound 512
485put dst add OH_HDR_LEN i get payload i
486i = add i 1
487end
488ret total
489end
490
491fn oh_parse_envelope wire view uchar * env edit OhEnv dst_payload edit uchar * to ulong
492n = len wire
493if lt n OH_HDR_LEN
494ret OH_ERR_TRUNCATED
495end
496if or ne ulong get wire 0 OH_MAGIC_0 ne ulong get wire 1 OH_MAGIC_1
497ret OH_ERR_BAD_MAGIC
498end
499ver = ulong get wire 2
500if ne ver OH_VERSION
501ret OH_ERR_BAD_VERSION
502end
503plen = or ulong get wire 46 shl ulong get wire 47 8
504if gt plen OH_MAX_PAYLOAD
505ret OH_ERR_TRUNCATED
506end
507if ne add OH_HDR_LEN plen n
508ret OH_ERR_TRUNCATED
509end
510if gt plen len dst_payload
511ret OH_ERR_TRUNCATED
512end
513put env version ver
514put env consent ulong get wire 3
515put env category ulong get wire 4
516put env peer_id oh_get_u64_le wire 6
517put env created_ms oh_get_u64_le wire 14
518put env ttl_ms oh_get_u64_le wire 22
519put env cap_flags oh_get_u64_le wire 30
520wire_dig = oh_get_u64_le wire 38
521put env digest wire_dig
522put env payload_len plen
523i = ulong 0
524while lt i plen bound 512
525put dst_payload i get wire add OH_HDR_LEN i
526i = add i 1
527end
528want_dig = oh_compute_digest env part dst_payload 0 plen
529if ne wire_dig want_dig
530ret OH_ERR_BAD_DIGEST
531end
532ret OH_OK
533end
534
535note --- Character & token classification helpers for Personal Data Firewall ---
536
537fn oh_is_digit c uchar to bool
538ret and ge c 48 le c 57
539end
540
541fn oh_is_upper c uchar to bool
542ret and ge c 65 le c 90
543end
544
545fn oh_is_lower c uchar to bool
546ret and ge c 97 le c 122
547end
548
549fn oh_is_alpha c uchar to bool
550ret or oh_is_upper c oh_is_lower c
551end
552
553fn oh_is_alnum c uchar to bool
554ret or oh_is_alpha c oh_is_digit c
555end
556
557fn oh_is_tok_char c uchar to bool
558ret or oh_is_alnum c or eq c 95 eq c 45
559end
560
561fn oh_is_path_char c uchar to bool
562ret or oh_is_alnum c or eq c 46 or eq c 95 eq c 45
563end
564
565fn oh_to_lower c uchar to uchar
566if oh_is_upper c
567ret add c 32
568end
569ret c
570end
571
572fn oh_hex_val c uchar to ulong
573if and ge c 48 le c 57
574ret ulong sub c 48
575end
576if and ge c 65 le c 70
577ret add ulong sub c 65 10
578end
579if and ge c 97 le c 102
580ret add ulong sub c 97 10
581end
582ret ulong 255
583end
584
585fn oh_match_at buf view uchar * pos ulong pat view uchar * to bool
586pn = len pat
587if gt add pos pn len buf
588ret false
589end
590i = ulong 0
591while lt i pn bound 64
592if ne get buf add pos i get pat i
593ret false
594end
595i = add i 1
596end
597ret true
598end
599
600fn oh_match_ci_at buf view uchar * pos ulong pat view uchar * to bool
601pn = len pat
602if gt add pos pn len buf
603ret false
604end
605i = ulong 0
606while lt i pn bound 64
607if ne oh_to_lower get buf add pos i oh_to_lower get pat i
608ret false
609end
610i = add i 1
611end
612ret true
613end
614
615fn oh_count_upper_alnum buf view uchar * pos ulong to ulong
616n = len buf
617i = pos
618cnt = ulong 0
619while lt i n bound 512
620c = get buf i
621if or oh_is_digit c oh_is_upper c
622cnt = add cnt 1
623i = add i 1
624else
625break
626end
627end
628ret cnt
629end
630
631fn oh_count_alnum_u buf view uchar * pos ulong to ulong
632n = len buf
633i = pos
634cnt = ulong 0
635while lt i n bound 512
636c = get buf i
637if or oh_is_alnum c eq c 95
638cnt = add cnt 1
639i = add i 1
640else
641break
642end
643end
644ret cnt
645end
646
647fn oh_count_tok buf view uchar * pos ulong to ulong
648n = len buf
649i = pos
650cnt = ulong 0
651while lt i n bound 512
652if oh_is_tok_char get buf i
653cnt = add cnt 1
654i = add i 1
655else
656break
657end
658end
659ret cnt
660end
661
662fn oh_count_alnum buf view uchar * pos ulong to ulong
663n = len buf
664i = pos
665cnt = ulong 0
666while lt i n bound 512
667if oh_is_alnum get buf i
668cnt = add cnt 1
669i = add i 1
670else
671break
672end
673end
674ret cnt
675end
676
677fn oh_is_zw_at buf view uchar * pos ulong to ulong
678n = len buf
679b0 = get buf pos
680if and eq b0 0xc2 lt add pos 1 n
681if eq get buf add pos 1 0xad
682ret ulong 2
683end
684end
685if lt add pos 2 n
686b1 = get buf add pos 1
687b2 = get buf add pos 2
688if and eq b0 0xe2 eq b1 0x80
689if or eq b2 0x8b or eq b2 0x8c eq b2 0x8d
690ret ulong 3
691end
692end
693if and eq b0 0xe2 and eq b1 0x81 eq b2 0xa0
694ret ulong 3
695end
696if and eq b0 0xef and eq b1 0xbb eq b2 0xbf
697ret ulong 3
698end
699end
700ret ulong 0
701end
702
703fn oh_normalize_obfuscation src view uchar * dst edit uchar * to ulong
704n = len src
705cap = len dst
706i = ulong 0
707out_n = ulong 0
708while lt i n bound 1024
709zw = oh_is_zw_at src i
710if gt zw 0
711i = add i zw
712continue
713end
714b = get src i
715if and eq b 37 lt add i 2 n
716hi = oh_hex_val get src add i 1
717lo = oh_hex_val get src add i 2
718if and lt hi 16 lt lo 16
719if lt out_n cap
720put dst out_n uchar or shl hi 4 lo
721out_n = add out_n 1
722end
723i = add i 3
724continue
725end
726end
727if lt out_n cap
728put dst out_n b
729out_n = add out_n 1
730end
731i = add i 1
732end
733ret out_n
734end
735
736fn oh_leet_char c uchar to uchar
737if eq c 48
738ret uchar 111
739end
740if or eq c 49 eq c 33
741ret uchar 105
742end
743if eq c 51
744ret uchar 101
745end
746if or eq c 52 eq c 64
747ret uchar 97
748end
749if or eq c 53 eq c 36
750ret uchar 115
751end
752if eq c 55
753ret uchar 116
754end
755ret c
756end
757
758fn oh_normalize_leet src view uchar * dst edit uchar * to ulong
759n = len src
760cap = len dst
761if gt n cap
762n = cap
763end
764i = ulong 0
765while lt i n bound 1024
766put dst i oh_leet_char get src i
767i = add i 1
768end
769ret n
770end
771
772fn oh_check_gh_at buf view uchar * pos ulong to bool
773if gt add pos 40 len buf
774ret false
775end
776if ne get buf pos 103
777ret false
778end
779if ne get buf add pos 1 104
780ret false
781end
782c2 = get buf add pos 2
783ok2 = or eq c2 112 or eq c2 111 or eq c2 117 or eq c2 115 eq c2 114
784if not ok2
785ret false
786end
787if ne get buf add pos 3 95
788ret false
789end
790ret ge oh_count_alnum_u buf add pos 4 36
791end
792
793fn oh_check_stripe_at buf view uchar * pos ulong to bool
794if gt add pos 32 len buf
795ret false
796end
797c0 = get buf pos
798if not or eq c0 115 eq c0 114
799ret false
800end
801if ne get buf add pos 1 107
802ret false
803end
804if ne get buf add pos 2 95
805ret false
806end
807ok_kind = oh_match_at buf add pos 3 oh_t_live
808if not ok_kind
809ok_kind = oh_match_at buf add pos 3 oh_t_test
810end
811if not ok_kind
812ret false
813end
814ret ge oh_count_alnum buf add pos 8 24
815end
816
817fn oh_is_b64_tok c uchar to bool
818if oh_is_tok_char c
819ret true
820end
821if eq c 43
822ret true
823end
824if eq c 61
825ret true
826end
827ret eq c 47
828end
829
830fn oh_skip_hws buf view uchar * pos ulong to ulong
831n = len buf
832p = pos
833while lt p n bound 64
834c = get buf p
835if or eq c 32 eq c 9
836p = add p 1
837else
838break
839end
840end
841ret p
842end
843
844fn oh_check_generic_at buf view uchar * pos ulong keylen ulong to bool
845n = len buf
846p = oh_skip_hws buf add pos keylen
847if ge p n
848ret false
849end
850sep = get buf p
851if not or eq sep 61 eq sep 58
852ret false
853end
854p = oh_skip_hws buf add p 1
855if ge p n
856ret false
857end
858q = get buf p
859if not or eq q 34 eq q 39
860ret false
861end
862p = add p 1
863cnt = ulong 0
864while lt p n bound 256
865c = get buf p
866if eq c q
867ret ge cnt 16
868end
869if not oh_is_b64_tok c
870ret false
871end
872cnt = add cnt 1
873p = add p 1
874end
875ret false
876end
877
878fn oh_scan_generic_secret buf view uchar * to bool
879n = len buf
880i = ulong 0
881while lt i n bound 1024
882if oh_match_ci_at buf i oh_t_ak_api
883if oh_check_generic_at buf i len oh_t_ak_api
884ret true
885end
886end
887if oh_match_ci_at buf i oh_t_ak_apikey
888if oh_check_generic_at buf i len oh_t_ak_apikey
889ret true
890end
891end
892if oh_match_ci_at buf i oh_t_ak_sec
893if oh_check_generic_at buf i len oh_t_ak_sec
894ret true
895end
896end
897if oh_match_ci_at buf i oh_t_ak_priv
898if oh_check_generic_at buf i len oh_t_ak_priv
899ret true
900end
901end
902if oh_match_ci_at buf i oh_t_ak_auth
903if oh_check_generic_at buf i len oh_t_ak_auth
904ret true
905end
906end
907if oh_match_ci_at buf i oh_t_ak_bear
908if oh_check_generic_at buf i len oh_t_ak_bear
909ret true
910end
911end
912i = add i 1
913end
914ret false
915end
916
917fn oh_user_path_len buf view uchar * pos ulong to ulong
918n = len buf
919plen = ulong 0
920sep = uchar 47
921if oh_match_at buf pos oh_t_home
922plen = len oh_t_home
923else
924if oh_match_at buf pos oh_t_users
925plen = len oh_t_users
926else
927if and oh_is_alpha get buf pos lt add pos 9 n
928if oh_match_ci_at buf add pos 1 oh_t_wusers
929plen = ulong 9
930sep = uchar 92
931else
932if oh_match_ci_at buf add pos 1 oh_t_wusers2
933plen = ulong 9
934sep = uchar 47
935end
936end
937end
938end
939end
940if eq plen 0
941ret ulong 0
942end
943p = add pos plen
944ulen = ulong 0
945while lt p n bound 128
946c = get buf p
947if oh_is_path_char c
948ulen = add ulen 1
949p = add p 1
950else
951break
952end
953end
954if and gt ulen 0 lt p n
955if eq get buf p sep
956ret sub add p 1 pos
957end
958end
959ret ulong 0
960end
961
962fn oh_scan_secrets_raw buf view uchar * to ulong
963n = len buf
964i = ulong 0
965while lt i n bound 1024
966c = get buf i
967note AWS Access Key: AKIA[0-9A-Z]{16}
968if eq c 65
969if oh_match_at buf i oh_t_akia
970if ge oh_count_upper_alnum buf add i 4 16
971ret OH_ERR_PRIV_AWS_KEY
972end
973end
974if oh_match_at buf i oh_t_aiza
975if ge oh_count_tok buf add i 4 30
976ret OH_ERR_PRIV_GOOGLE_KEY
977end
978end
979end
980note GitHub PAT
981if eq c 103
982if oh_check_gh_at buf i
983ret OH_ERR_PRIV_GITHUB_TOKEN
984end
985end
986note Anthropic / OpenAI / Stripe keys
987if or eq c 115 eq c 114
988if oh_check_stripe_at buf i
989ret OH_ERR_PRIV_STRIPE_KEY
990end
991if oh_match_at buf i oh_t_sk_ant
992if ge oh_count_tok buf add i 7 20
993ret OH_ERR_PRIV_ANTHROPIC_KEY
994end
995end
996if oh_match_at buf i oh_t_sk
997if ge oh_count_tok buf add i 3 20
998ret OH_ERR_PRIV_OPENAI_KEY
999end
1000end
1001end
1002note HuggingFace token: hf_[A-Za-z0-9]{34,}
1003if eq c 104
1004if oh_match_at buf i oh_t_hf
1005if ge oh_count_alnum buf add i 3 34
1006ret OH_ERR_PRIV_HF_TOKEN
1007end
1008end
1009end
1010note PEM private key block
1011if eq c 45
1012if oh_match_at buf i oh_t_pem_beg
1013j = add i len oh_t_pem_beg
1014while lt j n bound 128
1015if oh_match_at buf j oh_t_pem_end
1016ret OH_ERR_PRIV_PEM_KEY
1017end
1018j = add j 1
1019end
1020end
1021end
1022i = add i 1
1023end
1024if oh_scan_generic_secret buf
1025ret OH_ERR_PRIV_GENERIC_SECRET
1026end
1027ret OH_OK
1028end
1029
1030fn oh_scan_paths_raw buf view uchar * to ulong
1031n = len buf
1032i = ulong 0
1033while lt i n bound 1024
1034if gt oh_user_path_len buf i 0
1035ret OH_ERR_PRIV_USER_PATH
1036end
1037if and eq get buf i 46 lt add i 2 n
1038if eq get buf add i 1 46
1039c2 = get buf add i 2
1040if or eq c2 47 eq c2 92
1041ret OH_ERR_PRIV_TRAVERSAL
1042end
1043end
1044end
1045i = add i 1
1046end
1047ret OH_OK
1048end
1049
1050fn oh_privacy_check payload view uchar * to ulong
1051if eq len payload 0
1052ret OH_ERR_EMPTY_PAYLOAD
1053end
1054rc = oh_scan_secrets_raw payload
1055if ne rc OH_OK
1056ret rc
1057end
1058rc = oh_scan_paths_raw payload
1059if ne rc OH_OK
1060ret rc
1061end
1062norm = new uchar 512 0
1063nn = oh_normalize_obfuscation payload norm
1064rc = oh_scan_secrets_raw part norm 0 nn
1065if ne rc OH_OK
1066ret rc
1067end
1068rc = oh_scan_paths_raw part norm 0 nn
1069if ne rc OH_OK
1070ret rc
1071end
1072leet = new uchar 512 0
1073ln = oh_normalize_leet part norm 0 nn leet
1074if oh_scan_generic_secret part leet 0 ln
1075ret OH_ERR_PRIV_GENERIC_SECRET
1076end
1077ret OH_OK
1078end
1079
1080fn oh_scrub_paths src view uchar * dst edit uchar * to ulong
1081n = len src
1082cap = len dst
1083i = ulong 0
1084out_n = ulong 0
1085while lt i n bound 1024
1086plen = oh_user_path_len src i
1087if gt plen 0
1088if lt add out_n 1 cap
1089put dst out_n uchar 126
1090put dst add out_n 1 uchar 47
1091out_n = add out_n 2
1092end
1093i = add i plen
1094continue
1095end
1096if lt out_n cap
1097put dst out_n get src i
1098out_n = add out_n 1
1099end
1100i = add i 1
1101end
1102ret out_n
1103end
1104
1105note --- Strict Authority Boundary Enforcement ---
1106
1107fn oh_scan_escalation_raw buf view uchar * to bool
1108n = len buf
1109i = ulong 0
1110while lt i n bound 1024
1111if oh_match_ci_at buf i oh_t_esc_exec
1112ret true
1113end
1114if oh_match_ci_at buf i oh_t_esc_sudo
1115ret true
1116end
1117if oh_match_ci_at buf i oh_t_esc_chmod
1118ret true
1119end
1120if oh_match_ci_at buf i oh_t_esc_chown
1121ret true
1122end
1123if oh_match_ci_at buf i oh_t_esc_ident
1124ret true
1125end
1126if oh_match_ci_at buf i oh_t_esc_seed
1127ret true
1128end
1129if oh_match_ci_at buf i oh_t_esc_admin
1130ret true
1131end
1132i = add i 1
1133end
1134ret false
1135end
1136
1137fn oh_check_authority cap_flags ulong payload view uchar * to ulong
1138if ne and cap_flags OH_CAP_LOCAL_EXEC 0
1139ret OH_ERR_AUTH_EXEC
1140end
1141if ne and cap_flags OH_CAP_SECRET_ACCESS 0
1142ret OH_ERR_AUTH_SECRET
1143end
1144if ne and cap_flags OH_CAP_IDENTITY_MUTATE 0
1145ret OH_ERR_AUTH_IDENTITY
1146end
1147if ne and cap_flags OH_CAP_ADMIN_RIGHTS 0
1148ret OH_ERR_AUTH_ADMIN
1149end
1150if ne cap_flags 0
1151ret OH_ERR_AUTH_UNKNOWN
1152end
1153if oh_scan_escalation_raw payload
1154ret OH_ERR_AUTH_PAYLOAD_ESC
1155end
1156norm = new uchar 512 0
1157nn = oh_normalize_obfuscation payload norm
1158if oh_scan_escalation_raw part norm 0 nn
1159ret OH_ERR_AUTH_PAYLOAD_ESC
1160end
1161ret OH_OK
1162end
1163
1164note --- Consent, TTL, Replay, Audit Trail, and Bounded Peer Assistance Queue ---
1165
1166fn oh_check_consent pol view OhPolicy env_consent ulong to ulong
1167if eq get pol opt_in 0
1168ret OH_ERR_NODE_OPT_OUT
1169end
1170if ne env_consent 1
1171ret OH_ERR_NO_CONSENT
1172end
1173ret OH_OK
1174end
1175
1176fn oh_check_ttl now_ms ulong created_ms ulong ttl_ms ulong to ulong
1177if or eq ttl_ms 0 gt ttl_ms OH_MAX_TTL_MS
1178ret OH_ERR_TTL_INVALID
1179end
1180if gt created_ms add now_ms OH_MAX_FUTURE_DRIFT_MS
1181ret OH_ERR_FUTURE_DRIFT
1182end
1183if ge now_ms add created_ms ttl_ms
1184ret OH_ERR_TTL_EXPIRED
1185end
1186ret OH_OK
1187end
1188
1189fn oh_audit_record pol edit OhPolicy kind ulong peer_id ulong req_id ulong cat ulong code ulong to void
1190seq = add get pol audit_seq 1
1191put pol audit_seq seq
1192idx = rem sub seq 1 OH_AUDIT_CAP
1193put oh_aud_seq idx seq
1194put oh_aud_kind idx kind
1195put oh_aud_peer idx peer_id
1196put oh_aud_req idx req_id
1197put oh_aud_cat idx cat
1198put oh_aud_code idx code
1199put oh_aud_ts idx get pol now_ms
1200put oh_aud_dig idx ulong 0
1201cnt = get oh_aud_cnt 0
1202if lt cnt OH_AUDIT_CAP
1203put oh_aud_cnt 0 add cnt 1
1204end
1205end
1206
1207fn oh_audit_set_last_digest pol view OhPolicy dig ulong to void
1208seq = get pol audit_seq
1209if eq seq 0
1210ret
1211end
1212idx = rem sub seq 1 OH_AUDIT_CAP
1213put oh_aud_dig idx dig
1214end
1215
1216fn oh_audit_count to ulong
1217ret get oh_aud_cnt 0
1218end
1219
1220fn oh_seen_check_and_record pol edit OhPolicy peer_id ulong dig ulong to bool
1221i = ulong 0
1222while lt i OH_SEEN_CAP bound 64
1223if and eq get oh_seen_peer i peer_id eq get oh_seen_dig i dig
1224ret false
1225end
1226i = add i 1
1227end
1228head = get pol seen_head
1229put oh_seen_peer head peer_id
1230put oh_seen_dig head dig
1231put pol seen_head rem add head 1 OH_SEEN_CAP
1232ret true
1233end
1234
1235fn oh_queue_sweep_expired pol edit OhPolicy to ulong
1236now = get pol now_ms
1237swept = ulong 0
1238i = ulong 0
1239while lt i OH_QUEUE_CAP bound 64
1240if eq get oh_q_status i OH_Q_PENDING
1241exp = add get oh_q_created i get oh_q_ttl i
1242if ge now exp
1243put oh_q_status i OH_Q_EXPIRED
1244oh_audit_record pol OH_AUD_EXPIRED get oh_q_peer_id i get oh_q_req_id i get oh_q_cat i OH_ERR_TTL_EXPIRED
1245oh_audit_set_last_digest pol get oh_q_digest i
1246swept = add swept 1
1247end
1248end
1249i = add i 1
1250end
1251ret swept
1252end
1253
1254fn oh_queue_pending_count to ulong
1255cnt = ulong 0
1256i = ulong 0
1257while lt i OH_QUEUE_CAP bound 64
1258if eq get oh_q_status i OH_Q_PENDING
1259cnt = add cnt 1
1260end
1261i = add i 1
1262end
1263ret cnt
1264end
1265
1266fn oh_queue_find_free to ulong
1267i = ulong 0
1268while lt i OH_QUEUE_CAP bound 64
1269st = get oh_q_status i
1270if or eq st OH_Q_EMPTY eq st OH_Q_EXPIRED
1271ret i
1272end
1273i = add i 1
1274end
1275ret OH_QUEUE_CAP
1276end
1277
1278fn oh_queue_store_slot slot ulong rid ulong env view OhEnv payload view uchar * dig ulong to void
1279put oh_q_status slot OH_Q_PENDING
1280put oh_q_req_id slot rid
1281put oh_q_peer_id slot get env peer_id
1282put oh_q_cat slot get env category
1283put oh_q_created slot get env created_ms
1284put oh_q_ttl slot get env ttl_ms
1285put oh_q_digest slot dig
1286put oh_q_helper slot ulong 0
1287plen = len payload
1288put oh_q_plen slot plen
1289base = mul slot OH_MAX_PAYLOAD
1290i = ulong 0
1291while lt i plen bound 512
1292put oh_q_payload add base i get payload i
1293i = add i 1
1294end
1295end
1296
1297fn oh_validate_distress pol edit OhPolicy env view OhEnv payload view uchar * to ulong
1298rc = oh_check_consent pol get env consent
1299if ne rc OH_OK
1300ret rc
1301end
1302if ne get env version OH_VERSION
1303ret OH_ERR_BAD_VERSION
1304end
1305if eq get env peer_id 0
1306ret OH_ERR_BAD_PEER
1307end
1308if gt get env category OH_CAT_MAX
1309ret OH_ERR_BAD_CATEGORY
1310end
1311rc = oh_check_authority get env cap_flags payload
1312if ne rc OH_OK
1313ret rc
1314end
1315rc = oh_check_ttl get pol now_ms get env created_ms get env ttl_ms
1316if ne rc OH_OK
1317ret rc
1318end
1319ret oh_privacy_check payload
1320end
1321
1322fn oh_submit_distress pol edit OhPolicy env view OhEnv payload view uchar * out_req edit ulong 1 to ulong
1323put out_req 0 ulong 0
1324swept = oh_queue_sweep_expired pol
1325if gt swept OH_QUEUE_CAP
1326ret OH_ERR_QUEUE_FULL
1327end
1328rc = oh_validate_distress pol env payload
1329if ne rc OH_OK
1330oh_audit_record pol OH_AUD_REFUSED get env peer_id 0 get env category rc
1331oh_audit_set_last_digest pol get env digest
1332ret rc
1333end
1334dig = oh_compute_digest env payload
1335if not oh_seen_check_and_record pol get env peer_id dig
1336oh_audit_record pol OH_AUD_REFUSED get env peer_id 0 get env category OH_ERR_REPLAY
1337oh_audit_set_last_digest pol dig
1338ret OH_ERR_REPLAY
1339end
1340slot = oh_queue_find_free
1341if ge slot OH_QUEUE_CAP
1342oh_audit_record pol OH_AUD_REFUSED get env peer_id 0 get env category OH_ERR_QUEUE_FULL
1343oh_audit_set_last_digest pol dig
1344ret OH_ERR_QUEUE_FULL
1345end
1346rid = get pol next_req_id
1347put pol next_req_id add rid 1
1348oh_queue_store_slot slot rid env payload dig
1349put out_req 0 rid
1350oh_audit_record pol OH_AUD_ENQUEUED get env peer_id rid get env category OH_OK
1351oh_audit_set_last_digest pol dig
1352ret OH_OK
1353end
1354
1355fn oh_find_req_slot req_id ulong to ulong
1356if eq req_id 0
1357ret OH_QUEUE_CAP
1358end
1359i = ulong 0
1360while lt i OH_QUEUE_CAP bound 64
1361if and ne get oh_q_status i OH_Q_EMPTY eq get oh_q_req_id i req_id
1362ret i
1363end
1364i = add i 1
1365end
1366ret OH_QUEUE_CAP
1367end
1368
1369fn oh_resolve_distress pol edit OhPolicy req_id ulong helper view OhEnv solution view uchar * to ulong
1370swept = oh_queue_sweep_expired pol
1371if gt swept OH_QUEUE_CAP
1372ret OH_ERR_QUEUE_FULL
1373end
1374rc = oh_check_consent pol get helper consent
1375if ne rc OH_OK
1376oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 rc
1377ret rc
1378end
1379if eq get helper peer_id 0
1380oh_audit_record pol OH_AUD_REFUSED 0 req_id 0 OH_ERR_BAD_PEER
1381ret OH_ERR_BAD_PEER
1382end
1383rc = oh_check_authority get helper cap_flags solution
1384if ne rc OH_OK
1385oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 rc
1386ret rc
1387end
1388rc = oh_privacy_check solution
1389if ne rc OH_OK
1390oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 rc
1391ret rc
1392end
1393slot = oh_find_req_slot req_id
1394if ge slot OH_QUEUE_CAP
1395oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 OH_ERR_REQ_NOT_FOUND
1396ret OH_ERR_REQ_NOT_FOUND
1397end
1398st = get oh_q_status slot
1399if eq st OH_Q_EXPIRED
1400oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id get oh_q_cat slot OH_ERR_TTL_EXPIRED
1401ret OH_ERR_TTL_EXPIRED
1402end
1403if eq st OH_Q_RESOLVED
1404oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id get oh_q_cat slot OH_ERR_REQ_ALREADY_RESOLVED
1405ret OH_ERR_REQ_ALREADY_RESOLVED
1406end
1407if eq get oh_q_peer_id slot get helper peer_id
1408oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id get oh_q_cat slot OH_ERR_SELF_RESOLVE
1409ret OH_ERR_SELF_RESOLVE
1410end
1411put oh_q_status slot OH_Q_RESOLVED
1412put oh_q_helper slot get helper peer_id
1413oh_audit_record pol OH_AUD_RESOLVED get helper peer_id req_id get oh_q_cat slot OH_OK
1414oh_audit_set_last_digest pol get oh_q_digest slot
1415ret OH_OK
1416end