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

186 lines · 6143 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/interplane/core/policy.om: pure-Omega trust-boundary policy evaluator,
3note conformance lifecycle state machine, and hash-chained provenance ledger.
4note Enforces all 5 Interplane security invariants:
5note 1. Parsing never executes (LC_PARSED cannot jump to LC_EXECUTED)
6note 2. Transport never grants (mTLS/loopback never bypasses runtime policy)
7note 3. Valid envelope != authorization (only LC_POLICY_AUTHORIZED can execute)
8note 4. Signature proves origin != authority (ORIG_SIG_VALID never grants ops)
9note 5. Runtime policy decides and requests cannot upgrade it (claimed_policy > runtime_policy refused)
10note Compose after si/directives/om2.om, om/tls/sha2.om, and protocol/interplane/core/envelope.om.
11
12const ORIG_UNVERIFIED ulong 0
13const ORIG_SIG_VALID ulong 1
14const ORIG_REVOKED ulong 2
15
16const OP_READ ulong 1
17const OP_WRITE ulong 2
18const OP_EXEC ulong 4
19const OP_ADMIN ulong 8
20const OP_POLICY_MUTATE ulong 16
21const OP_ALL_MASK ulong 31
22
23const LC_RECEIVED ulong 0
24const LC_PARSED ulong 1
25const LC_ORIGIN_CHECKED ulong 2
26const LC_POLICY_AUTHORIZED ulong 3
27const LC_EXECUTED ulong 4
28const LC_DENIED ulong 5
29
30const POL_OK ulong 0
31const POL_ERR_NOT_PARSED ulong 1
32const POL_ERR_ORIGIN_UNVERIFIED ulong 2
33const POL_ERR_ORIGIN_REVOKED ulong 3
34const POL_ERR_POLICY_DENIED ulong 4
35const POL_ERR_OP_NOT_ALLOWED ulong 5
36const POL_ERR_ESCALATION ulong 6
37const POL_ERR_BAD_OP ulong 7
38const POL_ERR_UNAUTH_TRANSITION ulong 8
39const POL_ERR_BAD_STATE ulong 9
40const POL_ERR_LEDGER_TAMPER ulong 10
41
42note --- Conformance Lifecycle State Machine ---
43note Allowed transitions:
44note LC_RECEIVED -> LC_PARSED | LC_DENIED
45note LC_PARSED -> LC_ORIGIN_CHECKED | LC_DENIED
46note LC_ORIGIN_CHECKED -> LC_POLICY_AUTHORIZED | LC_DENIED
47note LC_POLICY_AUTHORIZED -> LC_EXECUTED | LC_DENIED
48note LC_EXECUTED, LC_DENIED -> terminal (no further transitions)
49
50fn ip_lc_transition cur ulong nxt ulong to ulong
51if or gt cur LC_DENIED gt nxt LC_DENIED
52ret POL_ERR_BAD_STATE
53end
54if eq cur LC_RECEIVED
55if or eq nxt LC_PARSED eq nxt LC_DENIED
56ret POL_OK
57end
58ret POL_ERR_UNAUTH_TRANSITION
59end
60if eq cur LC_PARSED
61if or eq nxt LC_ORIGIN_CHECKED eq nxt LC_DENIED
62ret POL_OK
63end
64ret POL_ERR_UNAUTH_TRANSITION
65end
66if eq cur LC_ORIGIN_CHECKED
67if or eq nxt LC_POLICY_AUTHORIZED eq nxt LC_DENIED
68ret POL_OK
69end
70ret POL_ERR_UNAUTH_TRANSITION
71end
72if eq cur LC_POLICY_AUTHORIZED
73if or eq nxt LC_EXECUTED eq nxt LC_DENIED
74ret POL_OK
75end
76ret POL_ERR_UNAUTH_TRANSITION
77end
78ret POL_ERR_UNAUTH_TRANSITION
79end
80
81note Invariant 1 & 3: only an explicitly policy-authorized envelope may execute.
82fn ip_can_execute lc_state ulong to bool
83ret eq lc_state LC_POLICY_AUTHORIZED
84end
85
86note Required minimum runtime policy level for an operation mask.
87fn ip_op_min_policy req_op ulong to ulong
88if ne and req_op OP_POLICY_MUTATE ulong 0
89ret POL_ELEVATED
90end
91if ne and req_op OP_ADMIN ulong 0
92ret POL_ELEVATED
93end
94if ne and req_op or OP_WRITE OP_EXEC ulong 0
95ret POL_STANDARD
96end
97if ne and req_op OP_READ ulong 0
98ret POL_READ_ONLY
99end
100ret POL_ELEVATED
101end
102
103note --- Trust-Boundary Policy Evaluator ---
104note Inputs:
105note runtime_pol: authoritative runtime policy level (POL_DENY_ALL..POL_ELEVATED)
106note allowed_ops: authoritative runtime allowed-ops bitmask
107note origin_status: ORIG_UNVERIFIED | ORIG_SIG_VALID | ORIG_REVOKED
108note transport: TRANS_UNTRUSTED..TRANS_LOOPBACK (recorded only; NEVER grants authority)
109note req_op: requested operation bitmask (must be non-zero subset of OP_ALL_MASK)
110note claimed_pol: untrusted policy level claimed by the request (MUST NOT exceed runtime_pol)
111
112fn ip_eval_policy runtime_pol ulong allowed_ops ulong origin_status ulong transport ulong req_op ulong claimed_pol ulong to ulong
113note Transport is intentionally NOT used to grant or elevate authority (Invariant 2).
114if gt transport TRANS_LOOPBACK
115ret POL_ERR_POLICY_DENIED
116end
117note Reject unknown or empty op masks.
118if eq req_op ulong 0
119ret POL_ERR_BAD_OP
120end
121if ne and req_op not OP_ALL_MASK ulong 0
122ret POL_ERR_BAD_OP
123end
124note Invariant 5: a request claiming a higher policy level than runtime_pol is an escalation attempt.
125if gt claimed_pol runtime_pol
126ret POL_ERR_ESCALATION
127end
128note Invariant 4: origin verification is required, and revoked origins are always denied.
129if eq origin_status ORIG_REVOKED
130ret POL_ERR_ORIGIN_REVOKED
131end
132if ne origin_status ORIG_SIG_VALID
133ret POL_ERR_ORIGIN_UNVERIFIED
134end
135note Runtime policy DENY_ALL blocks everything, regardless of valid signature or loopback transport.
136if eq runtime_pol POL_DENY_ALL
137ret POL_ERR_POLICY_DENIED
138end
139note Runtime policy level must meet operation minimum.
140min_pol = ip_op_min_policy req_op
141if lt runtime_pol min_pol
142ret POL_ERR_POLICY_DENIED
143end
144note Runtime allowed_ops mask must explicitly include every requested op bit.
145if ne and allowed_ops req_op req_op
146ret POL_ERR_OP_NOT_ALLOWED
147end
148ret POL_OK
149end
150
151note End-to-end checked authorization step: requires lc_state == LC_ORIGIN_CHECKED.
152fn ip_authorize_step lc_state ulong runtime_pol ulong allowed_ops ulong origin_status ulong req_op ulong claimed_pol ulong to ulong
153if ne lc_state LC_ORIGIN_CHECKED
154ret POL_ERR_UNAUTH_TRANSITION
155end
156ret ip_eval_policy runtime_pol allowed_ops origin_status TRANS_UNTRUSTED req_op claimed_pol
157end
158
159note --- Hash-Chained Provenance Ledger ---
160note entry_digest = SHA-256(prev_digest[32] || seq_u64_le[8] || env_digest[32] || verdict_u32_le[4])
161
162fn ip_ledger_step prev_dig view uchar 32 seq ulong env_dig view uchar 32 verdict ulong out_dig edit uchar 32 to void
163ctx = new ulong 40 0
164sb = new uchar 8 0
165vb = new uchar 4 0
166w1 = ip_put_u64_le sb ulong 0 seq
167w2 = ip_put_u32_le vb ulong 0 uint verdict
168if or ne w1 ulong 8 ne w2 ulong 4
169trap 8
170end
171sha256_init ctx
172sha256_update ctx prev_dig
173sha256_update ctx sb
174sha256_update ctx env_dig
175sha256_update ctx vb
176sha256_final ctx out_dig
177end
178
179fn ip_ledger_verify prev_dig view uchar 32 seq ulong env_dig view uchar 32 verdict ulong expected view uchar 32 to ulong
180calc = new uchar 32 0
181ip_ledger_step prev_dig seq env_dig verdict calc
182if not ip_digest_eq calc expected
183ret POL_ERR_LEDGER_TAMPER
184end
185ret POL_OK
186end