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
omega-mv / protocol / aien-protocols / ext / discovery_capsule.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/aien-protocols/ext/discovery_capsule.om: pure-Omega port of aien-discovery-capsule |
| 3 | note Implements recipient URI validation, capsule lifecycle state machine, |
| 4 | note hop-limit & TTL routing enforcement, and canonical capsule digest verification. |
| 5 | note Compose after si/directives/om2.om, om/tls/sha2.om, and protocol/aien-protocols/core/types.om. |
| 6 | |
| 7 | const DC_OK ulong 0 |
| 8 | const DC_ERR_RECIPIENT ulong 1 |
| 9 | const DC_ERR_TTL ulong 2 |
| 10 | const DC_ERR_HOPS ulong 3 |
| 11 | const DC_ERR_STATE ulong 4 |
| 12 | const DC_ERR_DIGEST ulong 5 |
| 13 | const DC_ERR_ID ulong 6 |
| 14 | |
| 15 | const DC_STATE_DRAFT ulong 1 |
| 16 | const DC_STATE_SEALED ulong 2 |
| 17 | const DC_STATE_ROUTED ulong 3 |
| 18 | const DC_STATE_DELIVERED ulong 4 |
| 19 | const DC_STATE_EXPIRED ulong 5 |
| 20 | const DC_STATE_REVOKED ulong 6 |
| 21 | |
| 22 | const DC_MAX_HOPS ulong 16 |
| 23 | const DC_MAX_TTL_S ulong 86400 |
| 24 | |
| 25 | text dc_dom_cap "aien-dc:v1|" |
| 26 | text dc_sep "|" |
| 27 | text dc_pfx_did "did:" |
| 28 | text dc_pfx_rad "rad:" |
| 29 | text dc_pfx_node "node:" |
| 30 | |
| 31 | fn dc_state_valid s ulong to bool |
| 32 | ret and ge s DC_STATE_DRAFT le s DC_STATE_REVOKED |
| 33 | end |
| 34 | |
| 35 | fn dc_transition_check cur ulong nxt ulong to ulong |
| 36 | if not dc_state_valid cur |
| 37 | ret DC_ERR_STATE |
| 38 | end |
| 39 | if not dc_state_valid nxt |
| 40 | ret DC_ERR_STATE |
| 41 | end |
| 42 | if eq cur DC_STATE_DRAFT |
| 43 | if or eq nxt DC_STATE_SEALED eq nxt DC_STATE_REVOKED |
| 44 | ret DC_OK |
| 45 | end |
| 46 | ret DC_ERR_STATE |
| 47 | end |
| 48 | if eq cur DC_STATE_SEALED |
| 49 | if or eq nxt DC_STATE_ROUTED or eq nxt DC_STATE_EXPIRED eq nxt DC_STATE_REVOKED |
| 50 | ret DC_OK |
| 51 | end |
| 52 | ret DC_ERR_STATE |
| 53 | end |
| 54 | if eq cur DC_STATE_ROUTED |
| 55 | if or eq nxt DC_STATE_ROUTED or eq nxt DC_STATE_DELIVERED or eq nxt DC_STATE_EXPIRED eq nxt DC_STATE_REVOKED |
| 56 | ret DC_OK |
| 57 | end |
| 58 | ret DC_ERR_STATE |
| 59 | end |
| 60 | ret DC_ERR_STATE |
| 61 | end |
| 62 | |
| 63 | fn dc_validate_recipient recip view uchar * to ulong |
| 64 | n = len recip |
| 65 | if or lt n ulong 8 gt n ulong 128 |
| 66 | ret DC_ERR_RECIPIENT |
| 67 | end |
| 68 | i = ulong 0 |
| 69 | while lt i n bound 128 |
| 70 | b = get recip i |
| 71 | if or lt b uchar 33 gt b uchar 126 |
| 72 | ret DC_ERR_RECIPIENT |
| 73 | end |
| 74 | i = add i ulong 1 |
| 75 | end |
| 76 | has_pfx = false |
| 77 | if pt_slice_eq part recip ulong 0 ulong 4 dc_pfx_did |
| 78 | has_pfx = true |
| 79 | end |
| 80 | if pt_slice_eq part recip ulong 0 ulong 4 dc_pfx_rad |
| 81 | has_pfx = true |
| 82 | end |
| 83 | if pt_slice_eq part recip ulong 0 ulong 5 dc_pfx_node |
| 84 | has_pfx = true |
| 85 | end |
| 86 | if not has_pfx |
| 87 | ret DC_ERR_RECIPIENT |
| 88 | end |
| 89 | ret DC_OK |
| 90 | end |
| 91 | |
| 92 | fn dc_check_routing hops ulong max_hops ulong issued_s ulong ttl_s ulong now_s ulong to ulong |
| 93 | if or eq max_hops ulong 0 gt max_hops DC_MAX_HOPS |
| 94 | ret DC_ERR_HOPS |
| 95 | end |
| 96 | if gt hops max_hops |
| 97 | ret DC_ERR_HOPS |
| 98 | end |
| 99 | if or eq ttl_s ulong 0 gt ttl_s DC_MAX_TTL_S |
| 100 | ret DC_ERR_TTL |
| 101 | end |
| 102 | if lt now_s issued_s |
| 103 | ret DC_ERR_TTL |
| 104 | end |
| 105 | elapsed = sub now_s issued_s |
| 106 | if gt elapsed ttl_s |
| 107 | ret DC_ERR_TTL |
| 108 | end |
| 109 | ret DC_OK |
| 110 | end |
| 111 | |
| 112 | fn dc_hash_head ctx edit ulong 40 cap_id view uchar 16 recip view uchar * payload_dig view uchar 32 to void |
| 113 | sha256_init ctx |
| 114 | sha256_update ctx dc_dom_cap |
| 115 | sha256_update ctx cap_id |
| 116 | sha256_update ctx dc_sep |
| 117 | sha256_update ctx recip |
| 118 | sha256_update ctx dc_sep |
| 119 | sha256_update ctx payload_dig |
| 120 | end |
| 121 | |
| 122 | fn dc_hash_finish ctx edit ulong 40 ttl_s ulong max_hops ulong dst edit uchar 32 to void |
| 123 | buf = new uchar 16 0 |
| 124 | p0 = pt_put_u64_le buf ulong 0 ttl_s |
| 125 | p1 = pt_put_u64_le buf p0 max_hops |
| 126 | sha256_update ctx part buf ulong 0 p1 |
| 127 | sha256_final ctx dst |
| 128 | end |
| 129 | |
| 130 | fn dc_verify_capsule cap_id view uchar 16 recip view uchar * payload_dig view uchar 32 ttl_s ulong max_hops ulong to ulong |
| 131 | if uuid_is_nil cap_id |
| 132 | ret DC_ERR_ID |
| 133 | end |
| 134 | if ne dc_validate_recipient recip DC_OK |
| 135 | ret DC_ERR_RECIPIENT |
| 136 | end |
| 137 | if digest_is_zero payload_dig |
| 138 | ret DC_ERR_DIGEST |
| 139 | end |
| 140 | if or eq ttl_s ulong 0 gt ttl_s DC_MAX_TTL_S |
| 141 | ret DC_ERR_TTL |
| 142 | end |
| 143 | if or eq max_hops ulong 0 gt max_hops DC_MAX_HOPS |
| 144 | ret DC_ERR_HOPS |
| 145 | end |
| 146 | ret DC_OK |
| 147 | end |
| 148 | |
| 149 | fn dc_verify_digest ctx edit ulong 40 ttl_s ulong max_hops ulong claimed view uchar 32 to ulong |
| 150 | exp = new uchar 32 0 |
| 151 | dc_hash_finish ctx ttl_s max_hops exp |
| 152 | if not digest_eq exp claimed |
| 153 | ret DC_ERR_DIGEST |
| 154 | end |
| 155 | ret DC_OK |
| 156 | end |