@intentic/sandbox-contract 1.432.0 → 1.433.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/contracts/agent.contract.d.ts +252 -72
- package/dist/contracts/agent.contract.d.ts.map +1 -1
- package/dist/contracts/agents.contract.d.ts +67 -0
- package/dist/contracts/agents.contract.d.ts.map +1 -1
- package/dist/contracts/approvals.contract.d.ts +35 -0
- package/dist/contracts/approvals.contract.d.ts.map +1 -1
- package/dist/contracts/approvals.contract.js +13 -3
- package/dist/contracts/approvals.contract.js.map +1 -1
- package/dist/contracts/automations.contract.d.ts +1 -0
- package/dist/contracts/automations.contract.d.ts.map +1 -1
- package/dist/contracts/capabilities.contract.d.ts +3 -0
- package/dist/contracts/capabilities.contract.d.ts.map +1 -1
- package/dist/contracts/extensions.contract.d.ts +4 -0
- package/dist/contracts/extensions.contract.d.ts.map +1 -1
- package/dist/contracts/needs.contract.d.ts +198 -0
- package/dist/contracts/needs.contract.d.ts.map +1 -1
- package/dist/contracts/runner.contract.d.ts +156 -36
- package/dist/contracts/runner.contract.d.ts.map +1 -1
- package/dist/contracts/seal.contract.d.ts +5 -0
- package/dist/contracts/seal.contract.d.ts.map +1 -1
- package/dist/contracts/sessions.contract.d.ts +22 -0
- package/dist/contracts/sessions.contract.d.ts.map +1 -1
- package/dist/contracts/settings.contract.d.ts +16 -0
- package/dist/contracts/settings.contract.d.ts.map +1 -1
- package/dist/contracts/stack-grants.contract.d.ts +22 -0
- package/dist/contracts/stack-grants.contract.d.ts.map +1 -1
- package/dist/contracts/system.contract.d.ts +7 -4
- package/dist/contracts/system.contract.d.ts.map +1 -1
- package/dist/events/agent-events.d.ts +88 -0
- package/dist/events/agent-events.d.ts.map +1 -1
- package/dist/events/agent-transcript.d.ts +22 -0
- package/dist/events/agent-transcript.d.ts.map +1 -1
- package/dist/events/system-events.d.ts +4 -0
- package/dist/events/system-events.d.ts.map +1 -1
- package/dist/events/transcript.d.ts +110 -0
- package/dist/events/transcript.d.ts.map +1 -1
- package/dist/ids/conversation-ids.d.ts.map +1 -1
- package/dist/ids/conversation-ids.js.map +1 -1
- package/dist/protocol/raw/extension-protocol.d.ts +116 -0
- package/dist/protocol/raw/extension-protocol.d.ts.map +1 -1
- package/dist/protocol/raw/extension-protocol.js +17 -1
- package/dist/protocol/raw/extension-protocol.js.map +1 -1
- package/dist/protocol/raw/raw-json-routes.d.ts +2974 -0
- package/dist/protocol/raw/raw-json-routes.d.ts.map +1 -1
- package/dist/protocol/raw/raw-json-routes.js +39 -0
- package/dist/protocol/raw/raw-json-routes.js.map +1 -1
- package/dist/protocol/raw/raw-routes.d.ts +179 -0
- package/dist/protocol/raw/raw-routes.d.ts.map +1 -1
- package/dist/protocol/raw/raw-routes.js +41 -0
- package/dist/protocol/raw/raw-routes.js.map +1 -1
- package/dist/protocol/s2s/address.d.ts +53 -0
- package/dist/protocol/s2s/address.d.ts.map +1 -0
- package/dist/protocol/s2s/address.js +111 -0
- package/dist/protocol/s2s/address.js.map +1 -0
- package/dist/protocol/s2s/attestation.d.ts +41 -0
- package/dist/protocol/s2s/attestation.d.ts.map +1 -0
- package/dist/protocol/s2s/attestation.js +96 -0
- package/dist/protocol/s2s/attestation.js.map +1 -0
- package/dist/protocol/s2s/canonical.d.ts +10 -0
- package/dist/protocol/s2s/canonical.d.ts.map +1 -0
- package/dist/protocol/s2s/canonical.js +94 -0
- package/dist/protocol/s2s/canonical.js.map +1 -0
- package/dist/protocol/s2s/envelope.d.ts +40 -0
- package/dist/protocol/s2s/envelope.d.ts.map +1 -0
- package/dist/protocol/s2s/envelope.js +116 -0
- package/dist/protocol/s2s/envelope.js.map +1 -0
- package/dist/protocol/s2s/extension-sign.d.ts +21 -0
- package/dist/protocol/s2s/extension-sign.d.ts.map +1 -0
- package/dist/protocol/s2s/extension-sign.js +41 -0
- package/dist/protocol/s2s/extension-sign.js.map +1 -0
- package/dist/protocol/s2s/identity-doc.d.ts +50 -0
- package/dist/protocol/s2s/identity-doc.d.ts.map +1 -0
- package/dist/protocol/s2s/identity-doc.js +89 -0
- package/dist/protocol/s2s/identity-doc.js.map +1 -0
- package/dist/protocol/s2s/index.d.ts +12 -0
- package/dist/protocol/s2s/index.d.ts.map +1 -0
- package/dist/protocol/s2s/index.js +12 -0
- package/dist/protocol/s2s/index.js.map +1 -0
- package/dist/protocol/s2s/kem.d.ts +15 -0
- package/dist/protocol/s2s/kem.d.ts.map +1 -0
- package/dist/protocol/s2s/kem.js +34 -0
- package/dist/protocol/s2s/kem.js.map +1 -0
- package/dist/protocol/s2s/keys.d.ts +34 -0
- package/dist/protocol/s2s/keys.d.ts.map +1 -0
- package/dist/protocol/s2s/keys.js +81 -0
- package/dist/protocol/s2s/keys.js.map +1 -0
- package/dist/protocol/s2s/passkey-cosign.d.ts +44 -0
- package/dist/protocol/s2s/passkey-cosign.d.ts.map +1 -0
- package/dist/protocol/s2s/passkey-cosign.js +139 -0
- package/dist/protocol/s2s/passkey-cosign.js.map +1 -0
- package/dist/protocol/s2s/session.d.ts +61 -0
- package/dist/protocol/s2s/session.d.ts.map +1 -0
- package/dist/protocol/s2s/session.js +98 -0
- package/dist/protocol/s2s/session.js.map +1 -0
- package/dist/protocol/s2s/sign.d.ts +12 -0
- package/dist/protocol/s2s/sign.d.ts.map +1 -0
- package/dist/protocol/s2s/sign.js +33 -0
- package/dist/protocol/s2s/sign.js.map +1 -0
- package/dist/schemas/agents-list.d.ts +2 -0
- package/dist/schemas/agents-list.d.ts.map +1 -1
- package/dist/schemas/agents.d.ts +4 -0
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/approvals.d.ts +100 -0
- package/dist/schemas/approvals.d.ts.map +1 -1
- package/dist/schemas/approvals.js +21 -0
- package/dist/schemas/approvals.js.map +1 -1
- package/dist/schemas/automations.d.ts +2 -0
- package/dist/schemas/automations.d.ts.map +1 -1
- package/dist/schemas/automations.js +1 -1
- package/dist/schemas/automations.js.map +1 -1
- package/dist/schemas/capabilities.d.ts +1 -0
- package/dist/schemas/capabilities.d.ts.map +1 -1
- package/dist/schemas/command-admission.d.ts +6 -0
- package/dist/schemas/command-admission.d.ts.map +1 -1
- package/dist/schemas/command-admission.js +2 -1
- package/dist/schemas/command-admission.js.map +1 -1
- package/dist/schemas/contact-need.d.ts +50 -0
- package/dist/schemas/contact-need.d.ts.map +1 -0
- package/dist/schemas/contact-need.js +19 -0
- package/dist/schemas/contact-need.js.map +1 -0
- package/dist/schemas/contacts-settings.d.ts +5 -0
- package/dist/schemas/contacts-settings.d.ts.map +1 -0
- package/dist/schemas/contacts-settings.js +11 -0
- package/dist/schemas/contacts-settings.js.map +1 -0
- package/dist/schemas/extension-updates.d.ts +8 -0
- package/dist/schemas/extension-updates.d.ts.map +1 -1
- package/dist/schemas/issues.d.ts +2 -0
- package/dist/schemas/issues.d.ts.map +1 -1
- package/dist/schemas/issues.js +1 -0
- package/dist/schemas/issues.js.map +1 -1
- package/dist/schemas/links.d.ts +8 -0
- package/dist/schemas/links.d.ts.map +1 -1
- package/dist/schemas/links.js +4 -0
- package/dist/schemas/links.js.map +1 -1
- package/dist/schemas/need-kinds.d.ts +2 -1
- package/dist/schemas/need-kinds.d.ts.map +1 -1
- package/dist/schemas/need-kinds.js +2 -2
- package/dist/schemas/need-kinds.js.map +1 -1
- package/dist/schemas/needs.d.ts +156 -13
- package/dist/schemas/needs.d.ts.map +1 -1
- package/dist/schemas/needs.js +5 -12
- package/dist/schemas/needs.js.map +1 -1
- package/dist/schemas/redirect-need.d.ts +15 -0
- package/dist/schemas/redirect-need.d.ts.map +1 -0
- package/dist/schemas/redirect-need.js +14 -0
- package/dist/schemas/redirect-need.js.map +1 -0
- package/dist/schemas/s2s-identity.d.ts +521 -0
- package/dist/schemas/s2s-identity.d.ts.map +1 -0
- package/dist/schemas/s2s-identity.js +208 -0
- package/dist/schemas/s2s-identity.js.map +1 -0
- package/dist/schemas/s2s-views.d.ts +2127 -0
- package/dist/schemas/s2s-views.d.ts.map +1 -0
- package/dist/schemas/s2s-views.js +217 -0
- package/dist/schemas/s2s-views.js.map +1 -0
- package/dist/schemas/s2s-wire.d.ts +1616 -0
- package/dist/schemas/s2s-wire.d.ts.map +1 -0
- package/dist/schemas/s2s-wire.js +276 -0
- package/dist/schemas/s2s-wire.js.map +1 -0
- package/dist/schemas/s2s.d.ts +5 -0
- package/dist/schemas/s2s.d.ts.map +1 -0
- package/dist/schemas/s2s.js +5 -0
- package/dist/schemas/s2s.js.map +1 -0
- package/dist/schemas/settings.d.ts +16 -0
- package/dist/schemas/settings.d.ts.map +1 -1
- package/dist/schemas/settings.js +2 -0
- package/dist/schemas/settings.js.map +1 -1
- package/dist/schemas/terminal.d.ts +2 -0
- package/dist/schemas/terminal.d.ts.map +1 -1
- package/dist/schemas/terminal.js +4 -0
- package/dist/schemas/terminal.js.map +1 -1
- package/dist/schemas/vpn.d.ts +4 -0
- package/dist/schemas/vpn.d.ts.map +1 -1
- package/dist/schemas/vpn.js +9 -0
- package/dist/schemas/vpn.js.map +1 -1
- package/dist/state/auth-state.d.ts.map +1 -1
- package/dist/state/auth-state.js +6 -0
- package/dist/state/auth-state.js.map +1 -1
- package/dist/state/definition.d.ts +18 -0
- package/dist/state/definition.d.ts.map +1 -1
- package/package.json +20 -5
- package/src/contracts/approvals.contract.ts +24 -3
- package/src/ids/conversation-ids.test.ts +22 -3
- package/src/ids/conversation-ids.ts +5 -7
- package/src/protocol/ingress-contract.fixture.json +6 -1
- package/src/protocol/raw/extension-protocol.ts +45 -1
- package/src/protocol/raw/raw-json-routes.ts +85 -0
- package/src/protocol/raw/raw-routes.ts +66 -3
- package/src/protocol/s2s/address.test.ts +157 -0
- package/src/protocol/s2s/address.ts +193 -0
- package/src/protocol/s2s/attestation.test.ts +262 -0
- package/src/protocol/s2s/attestation.ts +173 -0
- package/src/protocol/s2s/canonical.test.ts +70 -0
- package/src/protocol/s2s/canonical.ts +119 -0
- package/src/protocol/s2s/envelope.test.ts +162 -0
- package/src/protocol/s2s/envelope.ts +194 -0
- package/src/protocol/s2s/extension-sign.test.ts +117 -0
- package/src/protocol/s2s/extension-sign.ts +76 -0
- package/src/protocol/s2s/identity-doc.test.ts +187 -0
- package/src/protocol/s2s/identity-doc.ts +169 -0
- package/src/protocol/s2s/index.ts +15 -0
- package/src/protocol/s2s/kem.ts +62 -0
- package/src/protocol/s2s/keys.test.ts +76 -0
- package/src/protocol/s2s/keys.ts +155 -0
- package/src/protocol/s2s/passkey-cosign.test.ts +98 -0
- package/src/protocol/s2s/passkey-cosign.testing.ts +86 -0
- package/src/protocol/s2s/passkey-cosign.ts +192 -0
- package/src/protocol/s2s/s2s.testing.ts +22 -0
- package/src/protocol/s2s/session.test.ts +133 -0
- package/src/protocol/s2s/session.ts +179 -0
- package/src/protocol/s2s/sign.test.ts +66 -0
- package/src/protocol/s2s/sign.ts +53 -0
- package/src/schemas/approvals.ts +41 -0
- package/src/schemas/automations.ts +4 -3
- package/src/schemas/command-admission.ts +4 -1
- package/src/schemas/contact-need.ts +26 -0
- package/src/schemas/contacts-settings.ts +16 -0
- package/src/schemas/issues.ts +1 -0
- package/src/schemas/links.ts +4 -0
- package/src/schemas/need-kinds.ts +2 -2
- package/src/schemas/needs.ts +5 -18
- package/src/schemas/redirect-need.ts +19 -0
- package/src/schemas/s2s-identity.ts +329 -0
- package/src/schemas/s2s-views.ts +326 -0
- package/src/schemas/s2s-wire.ts +401 -0
- package/src/schemas/s2s.test.ts +175 -0
- package/src/schemas/s2s.ts +9 -0
- package/src/schemas/settings.ts +2 -0
- package/src/schemas/terminal.ts +6 -0
- package/src/schemas/version-seam.test.ts +9 -0
- package/src/schemas/vpn.ts +13 -0
- package/src/state/auth-state.ts +10 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { S2sEnvelopeIdSchema, type S2sEnvelope, S2sEnvelopeSchema, type S2sPayload } from "../../schemas/s2s-wire.js";
|
|
2
|
+
import { canonicalBytes, fromBase64Url, toBase64Url } from "./canonical.js";
|
|
3
|
+
import { type EnvelopeAddressing, openEnvelope, openSelfSigned, sealEnvelope, uuidv7, verifyEnvelope } from "./envelope.js";
|
|
4
|
+
import { type S2sKeySet, secretKeysOf } from "./keys.js";
|
|
5
|
+
import { keySetFor, seededRandom } from "./s2s.testing.js";
|
|
6
|
+
import { hybridSign } from "./sign.js";
|
|
7
|
+
|
|
8
|
+
const NOW = 1_800_000_000_000;
|
|
9
|
+
const alice = keySetFor(`alice`);
|
|
10
|
+
const bob = keySetFor(`bob`);
|
|
11
|
+
const bobBefore = keySetFor(`bob before`);
|
|
12
|
+
const mallory = keySetFor(`mallory`);
|
|
13
|
+
|
|
14
|
+
const addressing: EnvelopeAddressing = { from: `alice.sandbox.example`, to: `bob.example`, kind: `message`, thread: `t-1`, hops: 0, sentAt: NOW };
|
|
15
|
+
const payload: S2sPayload = { kind: `message`, subject: `hello`, text: `the plan is in the file`, attachments: [{ name: `plan.md`, data: `IyBQbGFu` }] };
|
|
16
|
+
const sealed = sealEnvelope({ addressing, payload, sender: alice, recipient: bob.public, random: seededRandom(`seal`) });
|
|
17
|
+
|
|
18
|
+
// Re-signed by the sender after a change, so only the encryption can refuse it.
|
|
19
|
+
const resigned = (envelope: S2sEnvelope): S2sEnvelope => {
|
|
20
|
+
const { sig: _sig, ...unsigned } = envelope;
|
|
21
|
+
return { ...unsigned, sig: hybridSign(`envelope`, canonicalBytes(unsigned), secretKeysOf(alice.secret)) };
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const flipByte = (text: string, at: number): string => {
|
|
25
|
+
const bytes = fromBase64Url(text) ?? new Uint8Array();
|
|
26
|
+
bytes[at] = (bytes[at] ?? 0) ^ 0x01;
|
|
27
|
+
return toBase64Url(bytes);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
describe("a UUIDv7", () => {
|
|
31
|
+
it("starts with the time in milliseconds and carries version 7 and the RFC variant", () => {
|
|
32
|
+
const id = uuidv7(NOW, seededRandom(`id`));
|
|
33
|
+
expect(id.startsWith(`01a3185c-5000-7`)).toBe(true);
|
|
34
|
+
expect(S2sEnvelopeIdSchema.safeParse(id).success).toBe(true);
|
|
35
|
+
expect(uuidv7(NOW + 1, seededRandom(`id`)) > id).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("an envelope", () => {
|
|
40
|
+
it("fills the header from the addressing and the two key sets", () => {
|
|
41
|
+
expect(sealed).toMatchObject({ v: 1, from: `alice.sandbox.example`, fromFp: alice.fingerprint, to: `bob.example`, toFp: bob.fingerprint, kind: `message`, thread: `t-1`, hops: 0, sentAt: NOW });
|
|
42
|
+
expect(S2sEnvelopeIdSchema.safeParse(sealed.id).success).toBe(true);
|
|
43
|
+
expect(Object.keys(sealed)).not.toContain(`replyTo`);
|
|
44
|
+
expect(fromBase64Url(sealed.enc.nonce)?.length).toBe(12);
|
|
45
|
+
expect(fromBase64Url(sealed.enc.x25519)?.length).toBe(32);
|
|
46
|
+
expect(fromBase64Url(sealed.enc.mlkem768)?.length).toBe(1088);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("opens to the payload for the receiver, after JSON and the schema", () => {
|
|
50
|
+
const arrived = S2sEnvelopeSchema.parse(JSON.parse(JSON.stringify(sealed)));
|
|
51
|
+
const opened = openEnvelope(arrived, { sender: alice.public, keys: [bob] });
|
|
52
|
+
expect(opened).toEqual({ ok: true, header: expect.objectContaining({ id: sealed.id, kind: `message`, toFp: bob.fingerprint }), payload });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("hides the payload: no word of it is in the envelope", () => {
|
|
56
|
+
const text = JSON.stringify(sealed);
|
|
57
|
+
expect(text.includes(`the plan is in the file`)).toBe(false);
|
|
58
|
+
expect(text.includes(`plan.md`)).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("opens with a kept previous key set it was sealed to", () => {
|
|
62
|
+
const late = sealEnvelope({ addressing, payload, sender: alice, recipient: bobBefore.public });
|
|
63
|
+
expect(openEnvelope(late, { sender: alice.public, keys: [bob, bobBefore] })).toMatchObject({ ok: true, payload });
|
|
64
|
+
expect(openEnvelope(late, { sender: alice.public, keys: [bob] })).toEqual({ ok: false, reason: `recipient` });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("refuses another sender's keys, and either half of the signature from another signer", () => {
|
|
68
|
+
expect(openEnvelope(sealed, { sender: mallory.public, keys: [bob] })).toEqual({ ok: false, reason: `signature` });
|
|
69
|
+
const theirs = sealEnvelope({ addressing, payload, sender: mallory, recipient: bob.public });
|
|
70
|
+
expect(verifyEnvelope({ ...sealed, sig: { ...sealed.sig, ed25519: theirs.sig.ed25519 } }, alice.public)).toBe(false);
|
|
71
|
+
expect(verifyEnvelope({ ...sealed, sig: { ...sealed.sig, mldsa65: theirs.sig.mldsa65 } }, alice.public)).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("refuses any changed header field, as a signature that no longer verifies", () => {
|
|
75
|
+
const changes: readonly Partial<S2sEnvelope>[] = [
|
|
76
|
+
{ id: uuidv7(NOW) },
|
|
77
|
+
{ from: `mallory.example` },
|
|
78
|
+
{ to: `carol.example` },
|
|
79
|
+
{ kind: `report` },
|
|
80
|
+
{ thread: `t-2` },
|
|
81
|
+
{ as: `ada@acme.example` },
|
|
82
|
+
{ hops: 1 },
|
|
83
|
+
{ sentAt: NOW + 1 },
|
|
84
|
+
];
|
|
85
|
+
for (const change of changes) {
|
|
86
|
+
expect(openEnvelope({ ...sealed, ...change }, { sender: alice.public, keys: [bob] })).toEqual({ ok: false, reason: `signature` });
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("refuses every changed byte of the body, and changed bytes of the key agreement", () => {
|
|
91
|
+
const bodyBytes = fromBase64Url(sealed.body)?.length ?? 0;
|
|
92
|
+
expect(bodyBytes).toBeGreaterThan(16);
|
|
93
|
+
for (let at = 0; at < bodyBytes; at++) {
|
|
94
|
+
expect(openEnvelope({ ...sealed, body: flipByte(sealed.body, at) }, { sender: alice.public, keys: [bob] })).toEqual({ ok: false, reason: `signature` });
|
|
95
|
+
}
|
|
96
|
+
for (const at of [0, 11]) {
|
|
97
|
+
expect(verifyEnvelope({ ...sealed, enc: { ...sealed.enc, nonce: flipByte(sealed.enc.nonce, at) } }, alice.public)).toBe(false);
|
|
98
|
+
}
|
|
99
|
+
for (const at of [0, 31]) {
|
|
100
|
+
expect(verifyEnvelope({ ...sealed, enc: { ...sealed.enc, x25519: flipByte(sealed.enc.x25519, at) } }, alice.public)).toBe(false);
|
|
101
|
+
}
|
|
102
|
+
for (const at of [0, 544, 1087]) {
|
|
103
|
+
expect(verifyEnvelope({ ...sealed, enc: { ...sealed.enc, mlkem768: flipByte(sealed.enc.mlkem768, at) } }, alice.public)).toBe(false);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("binds the header to the body even under a fresh signature: the decryption itself refuses", () => {
|
|
108
|
+
const options = { sender: alice.public, keys: [bob] };
|
|
109
|
+
expect(openEnvelope(resigned({ ...sealed, thread: `t-2` }), options)).toEqual({ ok: false, reason: `decrypt` });
|
|
110
|
+
expect(openEnvelope(resigned({ ...sealed, hops: 2 }), options)).toEqual({ ok: false, reason: `decrypt` });
|
|
111
|
+
expect(openEnvelope(resigned({ ...sealed, body: flipByte(sealed.body, 0) }), options)).toEqual({ ok: false, reason: `decrypt` });
|
|
112
|
+
expect(openEnvelope(resigned({ ...sealed, enc: { ...sealed.enc, nonce: flipByte(sealed.enc.nonce, 3) } }), options)).toEqual({ ok: false, reason: `decrypt` });
|
|
113
|
+
expect(openEnvelope(resigned({ ...sealed, enc: { ...sealed.enc, x25519: flipByte(sealed.enc.x25519, 3) } }), options)).toEqual({ ok: false, reason: `decrypt` });
|
|
114
|
+
expect(openEnvelope(resigned({ ...sealed, enc: { ...sealed.enc, mlkem768: flipByte(sealed.enc.mlkem768, 3) } }), options)).toEqual({ ok: false, reason: `decrypt` });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("does not open under another key set, even one the envelope is relabelled to", () => {
|
|
118
|
+
expect(openEnvelope(resigned({ ...sealed, toFp: mallory.fingerprint }), { sender: alice.public, keys: [mallory] })).toEqual({ ok: false, reason: `decrypt` });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("refuses a payload that is not its header's kind when sealing, and one that is no payload when opening", () => {
|
|
122
|
+
expect(() => sealEnvelope({ addressing: { ...addressing, kind: `report` }, payload, sender: alice, recipient: bob.public })).toThrow(
|
|
123
|
+
`s2s envelope: a message payload under a report header`,
|
|
124
|
+
);
|
|
125
|
+
const oversized = sealEnvelope({ addressing, payload: { ...payload, text: `x`.repeat(60_001) }, sender: alice, recipient: bob.public });
|
|
126
|
+
expect(openEnvelope(oversized, { sender: alice.public, keys: [bob] })).toEqual({ ok: false, reason: `malformed` });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("carries a kind this build does not know in its header, for the door to refuse by name", () => {
|
|
130
|
+
expect(S2sEnvelopeSchema.safeParse({ ...sealed, kind: `poll.vote` }).success).toBe(true);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("a self-signed envelope (a stranger's request, its keys inside its body)", () => {
|
|
135
|
+
// What the request's own payload says signed it: the keys of the sandbox the message names in its subject, standing in for
|
|
136
|
+
// a request's embedded document.
|
|
137
|
+
const signerNamed = (signing: S2sPayload): S2sKeySet["public"] | undefined => {
|
|
138
|
+
if (signing.kind !== `message`) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
return signing.subject === `alice` ? alice.public : signing.subject === `mallory` ? mallory.public : undefined;
|
|
142
|
+
};
|
|
143
|
+
const asAlice = sealEnvelope({ addressing, payload: { ...payload, subject: `alice` }, sender: alice, recipient: bob.public });
|
|
144
|
+
|
|
145
|
+
it("opens when the keys its payload names signed it", () => {
|
|
146
|
+
expect(openSelfSigned(asAlice, [bob], signerNamed)).toEqual({ ok: true, header: expect.objectContaining({ id: asAlice.id }), payload: { ...payload, subject: `alice` } });
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("refuses as `signature` a body naming keys that did not sign it, or naming none", () => {
|
|
150
|
+
const claimsMallory = sealEnvelope({ addressing, payload: { ...payload, subject: `mallory` }, sender: alice, recipient: bob.public });
|
|
151
|
+
expect(openSelfSigned(claimsMallory, [bob], signerNamed)).toEqual({ ok: false, reason: `signature` });
|
|
152
|
+
expect(openSelfSigned(sealed, [bob], signerNamed)).toEqual({ ok: false, reason: `signature` });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("says `recipient` and `decrypt` before it ever reads a signer", () => {
|
|
156
|
+
const never = (): S2sKeySet["public"] => {
|
|
157
|
+
throw new Error(`read a signer for a body that does not open`);
|
|
158
|
+
};
|
|
159
|
+
expect(openSelfSigned(asAlice, [mallory], never)).toEqual({ ok: false, reason: `recipient` });
|
|
160
|
+
expect(openSelfSigned({ ...asAlice, body: flipByte(asAlice.body, 0) }, [bob], never)).toEqual({ ok: false, reason: `decrypt` });
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { gcm } from "@noble/ciphers/aes.js";
|
|
2
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
|
+
import { randomBytes } from "@noble/hashes/utils.js";
|
|
4
|
+
import { S2S_INFO, type S2sPublicKeys, type S2sSigningKeys } from "../../schemas/s2s-identity.js";
|
|
5
|
+
import {
|
|
6
|
+
S2S_BODY_MAX_BYTES,
|
|
7
|
+
type S2sEnvelope,
|
|
8
|
+
type S2sEnvelopeHeader,
|
|
9
|
+
type S2sPayload,
|
|
10
|
+
S2sPayloadSchema,
|
|
11
|
+
} from "../../schemas/s2s-wire.js";
|
|
12
|
+
import { canonicalBytes, fromBase64Url, fromUtf8, toBase64Url, utf8 } from "./canonical.js";
|
|
13
|
+
import { decapsulate, deriveKey, encapsulate } from "./kem.js";
|
|
14
|
+
import { fingerprintOf, type RandomBytes, type S2sKeySet, secretKeysOf } from "./keys.js";
|
|
15
|
+
import { hybridSign, hybridVerify } from "./sign.js";
|
|
16
|
+
|
|
17
|
+
// THE ENVELOPE (docs/architecture/messages.md, "Messages"): a payload sealed to the receiver's key set and signed by the
|
|
18
|
+
// sender's. The body is the payload's JSON under AES-256-GCM, its key HKDF-SHA256(mlkem_ss ‖ x25519_ss, salt =
|
|
19
|
+
// SHA-256(canonical {header, key agreement}), info = "intentic-s2s/1"), the canonical header its associated data; the hybrid signature is
|
|
20
|
+
// over the canonical envelope without `sig`, so the header, `enc` and `body` all. The platform in between sees who talks
|
|
21
|
+
// to whom and when, never what.
|
|
22
|
+
|
|
23
|
+
// A UUIDv7 (RFC 9562): the time in milliseconds, then random bits, so ids sort by when they were made.
|
|
24
|
+
export const uuidv7 = (now: number, random: RandomBytes = randomBytes): string => {
|
|
25
|
+
const bytes = random(16);
|
|
26
|
+
let time = Math.max(0, Math.floor(now));
|
|
27
|
+
for (let index = 5; index >= 0; index--) {
|
|
28
|
+
bytes[index] = time % 256;
|
|
29
|
+
time = Math.floor(time / 256);
|
|
30
|
+
}
|
|
31
|
+
bytes[6] = 0x70 | ((bytes[6] ?? 0) & 0x0f);
|
|
32
|
+
bytes[8] = 0x80 | ((bytes[8] ?? 0) & 0x3f);
|
|
33
|
+
const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
34
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// The header as the sender fills it; the seal adds `v`, the fingerprints, and an id when none is given.
|
|
38
|
+
export interface EnvelopeAddressing {
|
|
39
|
+
readonly id?: string;
|
|
40
|
+
readonly from: string;
|
|
41
|
+
readonly to: string;
|
|
42
|
+
readonly kind: S2sEnvelopeHeader["kind"];
|
|
43
|
+
readonly thread?: string | undefined;
|
|
44
|
+
readonly replyTo?: string | undefined;
|
|
45
|
+
readonly as?: string | undefined;
|
|
46
|
+
readonly hops: number;
|
|
47
|
+
readonly sentAt: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SealInput {
|
|
51
|
+
readonly addressing: EnvelopeAddressing;
|
|
52
|
+
readonly payload: S2sPayload;
|
|
53
|
+
// The sender's current key set, which signs.
|
|
54
|
+
readonly sender: S2sKeySet;
|
|
55
|
+
// The receiver's public keys, as its last verified document has them.
|
|
56
|
+
readonly recipient: S2sPublicKeys;
|
|
57
|
+
readonly random?: RandomBytes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const headerOf = (envelope: S2sEnvelope): S2sEnvelopeHeader => {
|
|
61
|
+
const { enc: _enc, body: _body, sig: _sig, ...header } = envelope;
|
|
62
|
+
return header;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// The key's salt: the canonical header and the key agreement both (X-Wing's lesson: bind the ephemeral key and the
|
|
66
|
+
// ciphertext into the key itself, not only through the signature), never the GCM nonce, which is not keying material.
|
|
67
|
+
const saltOf = (header: S2sEnvelopeHeader, enc: { readonly x25519: string; readonly mlkem768: string }): Uint8Array =>
|
|
68
|
+
sha256(canonicalBytes({ header, enc: { x25519: enc.x25519, mlkem768: enc.mlkem768 } }));
|
|
69
|
+
|
|
70
|
+
const unsignedOf = (envelope: S2sEnvelope): Omit<S2sEnvelope, "sig"> => {
|
|
71
|
+
const { sig: _sig, ...rest } = envelope;
|
|
72
|
+
return rest;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const headerFor = (addressing: EnvelopeAddressing, sender: S2sKeySet, recipient: S2sPublicKeys, random: RandomBytes): S2sEnvelopeHeader => {
|
|
76
|
+
const header: S2sEnvelopeHeader = {
|
|
77
|
+
v: 1,
|
|
78
|
+
id: addressing.id ?? uuidv7(addressing.sentAt, random),
|
|
79
|
+
from: addressing.from,
|
|
80
|
+
fromFp: sender.fingerprint,
|
|
81
|
+
to: addressing.to,
|
|
82
|
+
toFp: fingerprintOf(recipient),
|
|
83
|
+
kind: addressing.kind,
|
|
84
|
+
hops: addressing.hops,
|
|
85
|
+
sentAt: addressing.sentAt,
|
|
86
|
+
};
|
|
87
|
+
if (addressing.thread !== undefined) {
|
|
88
|
+
header.thread = addressing.thread;
|
|
89
|
+
}
|
|
90
|
+
if (addressing.replyTo !== undefined) {
|
|
91
|
+
header.replyTo = addressing.replyTo;
|
|
92
|
+
}
|
|
93
|
+
if (addressing.as !== undefined) {
|
|
94
|
+
header.as = addressing.as;
|
|
95
|
+
}
|
|
96
|
+
return header;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Seals and signs one payload. Seal at each attempt, so a receiver that rotated between attempts still gets one it can
|
|
100
|
+
// open. Throws when the payload is not its header's kind, or seals past the body cap.
|
|
101
|
+
export const sealEnvelope = ({ addressing, payload, sender, recipient, random = randomBytes }: SealInput): S2sEnvelope => {
|
|
102
|
+
if (payload.kind !== addressing.kind) {
|
|
103
|
+
throw new Error(`s2s envelope: a ${payload.kind} payload under a ${addressing.kind} header`);
|
|
104
|
+
}
|
|
105
|
+
const header = headerFor(addressing, sender, recipient, random);
|
|
106
|
+
const aad = canonicalBytes(header);
|
|
107
|
+
const agreement = encapsulate(recipient, random);
|
|
108
|
+
const key = deriveKey(agreement.secret, saltOf(header, agreement.enc), S2S_INFO);
|
|
109
|
+
const nonce = random(12);
|
|
110
|
+
const sealed = gcm(key, nonce, aad).encrypt(utf8(JSON.stringify(payload)));
|
|
111
|
+
if (sealed.length > S2S_BODY_MAX_BYTES) {
|
|
112
|
+
throw new Error(`s2s envelope: the body seals to ${sealed.length} bytes, past the ${S2S_BODY_MAX_BYTES} cap`);
|
|
113
|
+
}
|
|
114
|
+
const unsigned: Omit<S2sEnvelope, "sig"> = { ...header, enc: { ...agreement.enc, nonce: toBase64Url(nonce) }, body: toBase64Url(sealed) };
|
|
115
|
+
return { ...unsigned, sig: hybridSign("envelope", canonicalBytes(unsigned), secretKeysOf(sender.secret)) };
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// Whether the sender's keys signed this envelope as it stands: every header field, `enc` and `body`.
|
|
119
|
+
export const verifyEnvelope = (envelope: S2sEnvelope, sender: S2sSigningKeys): boolean =>
|
|
120
|
+
hybridVerify("envelope", canonicalBytes(unsignedOf(envelope)), envelope.sig, sender);
|
|
121
|
+
|
|
122
|
+
// `signature`: the sender's keys did not sign it. `recipient`: none of the key sets offered is the one it was sealed to.
|
|
123
|
+
// `decrypt`: it does not open under that key set (tampered, or sealed to another). `malformed`: it opens, but to no
|
|
124
|
+
// payload of its header's kind.
|
|
125
|
+
export type OpenRefusal = "signature" | "recipient" | "decrypt" | "malformed";
|
|
126
|
+
export type Opened =
|
|
127
|
+
| { readonly ok: true; readonly header: S2sEnvelopeHeader; readonly payload: S2sPayload }
|
|
128
|
+
| { readonly ok: false; readonly reason: OpenRefusal };
|
|
129
|
+
|
|
130
|
+
export interface OpenInput {
|
|
131
|
+
// The pinned sender's public signing keys.
|
|
132
|
+
readonly sender: S2sSigningKeys;
|
|
133
|
+
// Every key set this sandbox can open with: its current one and the kept previous ones.
|
|
134
|
+
readonly keys: readonly S2sKeySet[];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const decrypt = (envelope: S2sEnvelope, keySet: S2sKeySet): Uint8Array | undefined => {
|
|
138
|
+
const nonce = fromBase64Url(envelope.enc.nonce);
|
|
139
|
+
const body = fromBase64Url(envelope.body);
|
|
140
|
+
const secret = decapsulate(envelope.enc, secretKeysOf(keySet.secret));
|
|
141
|
+
if (nonce?.length !== 12 || body === undefined || secret === undefined) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const aad = canonicalBytes(headerOf(envelope));
|
|
145
|
+
try {
|
|
146
|
+
return gcm(deriveKey(secret, saltOf(headerOf(envelope), envelope.enc), S2S_INFO), nonce, aad).decrypt(body);
|
|
147
|
+
} catch {
|
|
148
|
+
// allow(silent-catch): a tag that does not verify is what a tampered or misaddressed body looks like.
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const payloadOf = (plain: Uint8Array, kind: string): S2sPayload | undefined => {
|
|
154
|
+
try {
|
|
155
|
+
const parsed = S2sPayloadSchema.safeParse(JSON.parse(fromUtf8(plain)));
|
|
156
|
+
return parsed.success && parsed.data.kind === kind ? parsed.data : undefined;
|
|
157
|
+
} catch {
|
|
158
|
+
// allow(silent-catch): a body that is not UTF-8 JSON is no payload, which the caller refuses as malformed.
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// The payload, opened with the key set the envelope names (`toFp`) and checked to be its header's kind; nothing verified.
|
|
164
|
+
const opened = (envelope: S2sEnvelope, keys: readonly S2sKeySet[]): Opened => {
|
|
165
|
+
const keySet = keys.find((candidate) => candidate.fingerprint === envelope.toFp);
|
|
166
|
+
if (keySet === undefined) {
|
|
167
|
+
return { ok: false, reason: "recipient" };
|
|
168
|
+
}
|
|
169
|
+
const plain = decrypt(envelope, keySet);
|
|
170
|
+
if (plain === undefined) {
|
|
171
|
+
return { ok: false, reason: "decrypt" };
|
|
172
|
+
}
|
|
173
|
+
const payload = payloadOf(plain, envelope.kind);
|
|
174
|
+
return payload === undefined ? { ok: false, reason: "malformed" } : { ok: true, header: headerOf(envelope), payload };
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// Verifies, then opens with the key set the envelope names (`toFp`), checking the payload is its header's kind.
|
|
178
|
+
export const openEnvelope = (envelope: S2sEnvelope, { sender, keys }: OpenInput): Opened =>
|
|
179
|
+
verifyEnvelope(envelope, sender) ? opened(envelope, keys) : { ok: false, reason: "signature" };
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Opens an envelope whose sender's keys ride inside its own body (a stranger's `contact.request`, which embeds its
|
|
183
|
+
* document), then verifies it against the keys `signer` reads off the payload: undefined there, or a signature those keys
|
|
184
|
+
* did not make, is `signature`. What a request proves this way is only that it vouches for itself; whether its address
|
|
185
|
+
* serves that key is the receiver's next check.
|
|
186
|
+
*/
|
|
187
|
+
export const openSelfSigned = (envelope: S2sEnvelope, keys: readonly S2sKeySet[], signer: (payload: S2sPayload) => S2sSigningKeys | undefined): Opened => {
|
|
188
|
+
const open = opened(envelope, keys);
|
|
189
|
+
if (!open.ok) {
|
|
190
|
+
return open;
|
|
191
|
+
}
|
|
192
|
+
const signing = signer(open.payload);
|
|
193
|
+
return signing !== undefined && verifyEnvelope(envelope, signing) ? open : { ok: false, reason: "signature" };
|
|
194
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { canonicalBytes, fromBase64Url, utf8 } from "./canonical.js";
|
|
2
|
+
import {
|
|
3
|
+
EXTENSION_SIGN_PAYLOAD_MAX_BYTES,
|
|
4
|
+
extensionSignDomain,
|
|
5
|
+
extensionSignDomainParts,
|
|
6
|
+
extensionSignProblem,
|
|
7
|
+
signExtensionPayload,
|
|
8
|
+
verifyExtensionPayload,
|
|
9
|
+
} from "./extension-sign.js";
|
|
10
|
+
import { secretKeysOf } from "./keys.js";
|
|
11
|
+
import { keySetFor } from "./s2s.testing.js";
|
|
12
|
+
import { hybridSign, hybridVerify, SIGN_DOMAINS, signedBytes } from "./sign.js";
|
|
13
|
+
|
|
14
|
+
const set = keySetFor(`ops`);
|
|
15
|
+
const other = keySetFor(`other`);
|
|
16
|
+
const secret = secretKeysOf(set.secret);
|
|
17
|
+
const record = { "@type": `Article`, name: `Hello`, at: 1_764_288_000_000, tags: [`a`, `b`] };
|
|
18
|
+
|
|
19
|
+
describe("an extension sign domain", () => {
|
|
20
|
+
it("is ext:<publisher.name>/<purpose>, and its signed bytes carry it behind the protocol prefix", () => {
|
|
21
|
+
expect<string>(extensionSignDomain(`intentic.network`, `log.entry`)).toBe(`ext:intentic.network/log.entry`);
|
|
22
|
+
expect(signedBytes(extensionSignDomain(`intentic.network`, `log.entry`), utf8(`x`))).toEqual(utf8(`intentic-s2s/1 ext:intentic.network/log.entry\nx`));
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("refuses an id that is not publisher.name and a purpose outside its pattern, naming which", () => {
|
|
26
|
+
expect(() => extensionSignDomain(`network`, `log`)).toThrow(`extension sign domain: "network" is not an extension id (publisher.name, each a lowercase word)`);
|
|
27
|
+
expect(() => extensionSignDomain(`Intentic.network`, `log`)).toThrow(TypeError);
|
|
28
|
+
expect(() => extensionSignDomain(`intentic.net/work`, `log`)).toThrow(TypeError);
|
|
29
|
+
expect(() => extensionSignDomain(`intentic.network`, `log/entry`)).toThrow(
|
|
30
|
+
`extension sign domain: "log/entry" is not a signing purpose: a lowercase word of up to 64 characters, dots and dashes inside`,
|
|
31
|
+
);
|
|
32
|
+
expect(() => extensionSignDomain(`intentic.network`, `log\nentry`)).toThrow(TypeError);
|
|
33
|
+
expect(() => extensionSignDomain(`intentic.network`, `.log`)).toThrow(TypeError);
|
|
34
|
+
expect(() => extensionSignDomain(`intentic.network`, ``)).toThrow(TypeError);
|
|
35
|
+
expect(extensionSignProblem(`intentic.network`, `a`.repeat(64))).toBeUndefined();
|
|
36
|
+
expect(extensionSignProblem(`intentic.network`, `a`.repeat(65))).toBe(
|
|
37
|
+
`"${`a`.repeat(65)}" is not a signing purpose: a lowercase word of up to 64 characters, dots and dashes inside`,
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("reads back into its halves, and nothing else does", () => {
|
|
42
|
+
expect(extensionSignDomainParts(`ext:intentic.network/log.entry`)).toEqual({ extension: `intentic.network`, purpose: `log.entry` });
|
|
43
|
+
expect(extensionSignDomainParts(`identity`)).toBeUndefined();
|
|
44
|
+
expect(extensionSignDomainParts(`ext:intentic.network/log/entry`)).toBeUndefined();
|
|
45
|
+
expect(extensionSignDomainParts(`ext:network/log`)).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("an extension's signature over a payload", () => {
|
|
50
|
+
const sig = signExtensionPayload(`intentic.network`, `log.entry`, record, secret);
|
|
51
|
+
|
|
52
|
+
it("verifies with the signer's keys for the same extension, purpose and payload, in any key order", () => {
|
|
53
|
+
expect(fromBase64Url(sig.ed25519)?.length).toBe(64);
|
|
54
|
+
expect(verifyExtensionPayload(`intentic.network`, `log.entry`, record, sig, set.public)).toBe(true);
|
|
55
|
+
const reordered = { tags: [`a`, `b`], at: 1_764_288_000_000, name: `Hello`, "@type": `Article` };
|
|
56
|
+
expect(verifyExtensionPayload(`intentic.network`, `log.entry`, reordered, sig, set.public)).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("does not verify for another purpose, another extension, another payload or another key set", () => {
|
|
60
|
+
expect(verifyExtensionPayload(`intentic.network`, `log.checkpoint`, record, sig, set.public)).toBe(false);
|
|
61
|
+
expect(verifyExtensionPayload(`acme.network`, `log.entry`, record, sig, set.public)).toBe(false);
|
|
62
|
+
expect(verifyExtensionPayload(`intentic.network`, `log.entry`, { ...record, name: `Hullo` }, sig, set.public)).toBe(false);
|
|
63
|
+
expect(verifyExtensionPayload(`intentic.network`, `log.entry`, record, sig, other.public)).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("answers false, never throws, for a bad id or purpose and a payload JSON cannot carry", () => {
|
|
67
|
+
expect(verifyExtensionPayload(`network`, `log.entry`, record, sig, set.public)).toBe(false);
|
|
68
|
+
expect(verifyExtensionPayload(`intentic.network`, `LOG`, record, sig, set.public)).toBe(false);
|
|
69
|
+
// A number JSON cannot spell: canonical JSON refuses it rather than sign `null` in its place.
|
|
70
|
+
expect(verifyExtensionPayload(`intentic.network`, `log.entry`, { n: Number.NaN }, sig, set.public)).toBe(false);
|
|
71
|
+
expect(verifyExtensionPayload(`intentic.network`, `log.entry`, record, { ...sig, ed25519: `not base64!` }, set.public)).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("signs the payload's canonical bytes, so the core's verifier agrees under the same domain", () => {
|
|
75
|
+
expect(hybridVerify(extensionSignDomain(`intentic.network`, `log.entry`), canonicalBytes(record), sig, set.public)).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("is capped at the door at 256 KiB of canonical JSON, a constant the daemon and the SDK both read", () => {
|
|
79
|
+
expect(EXTENSION_SIGN_PAYLOAD_MAX_BYTES).toBe(262_144);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("an extension's signature is never one of the core's", () => {
|
|
84
|
+
const domain = extensionSignDomain(`intentic.network`, `log.entry`);
|
|
85
|
+
const message = canonicalBytes(record);
|
|
86
|
+
const sig = hybridSign(domain, message, secret);
|
|
87
|
+
|
|
88
|
+
it("has no core domain spelled like an extension's: each is a word with no `:`, `/` or newline", () => {
|
|
89
|
+
for (const core of SIGN_DOMAINS) {
|
|
90
|
+
expect({ core, word: /^[a-z][a-z-]*$/u.test(core) }).toEqual({ core, word: true });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("covers bytes no core domain's signature covers, for any message, since the domain ends at the first newline", () => {
|
|
95
|
+
const signed = signedBytes(domain, message);
|
|
96
|
+
const head = new TextDecoder().decode(signed.slice(0, signed.indexOf(10) + 1));
|
|
97
|
+
expect(head).toBe(`intentic-s2s/1 ext:intentic.network/log.entry\n`);
|
|
98
|
+
for (const core of SIGN_DOMAINS) {
|
|
99
|
+
expect({ core, prefixed: head.startsWith(`intentic-s2s/1 ${core}\n`) }).toEqual({ core, prefixed: false });
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("does not verify under any core domain, over the payload or over the extension's whole signed bytes", () => {
|
|
104
|
+
for (const core of SIGN_DOMAINS) {
|
|
105
|
+
expect({ core, payload: hybridVerify(core, message, sig, set.public) }).toEqual({ core, payload: false });
|
|
106
|
+
// The bytes after the core's own prefix would have to be the extension's prefix and payload: they still differ.
|
|
107
|
+
expect({ core, whole: hybridVerify(core, signedBytes(domain, message), sig, set.public) }).toEqual({ core, whole: false });
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("and a core signature does not verify as an extension's", () => {
|
|
112
|
+
for (const core of SIGN_DOMAINS) {
|
|
113
|
+
const theirs = hybridSign(core, message, secret);
|
|
114
|
+
expect({ core, ext: verifyExtensionPayload(`intentic.network`, `log.entry`, record, theirs, set.public) }).toEqual({ core, ext: false });
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { HybridSig, S2sSigningKeys } from "../../schemas/s2s-identity.js";
|
|
3
|
+
import { canonicalBytes } from "./canonical.js";
|
|
4
|
+
import { hybridSign, hybridVerify, type SigningSecret } from "./sign.js";
|
|
5
|
+
|
|
6
|
+
// EXTENSION SIGN DOMAINS (2026-11-28, docs/architecture/messages.md "Identity"): an extension may have this sandbox sign
|
|
7
|
+
// a record as itself (`permissions.identity: ["sign"]`, POST /extension/identity/sign), and never holds the key. Each
|
|
8
|
+
// signature names the extension and what the record is for, `ext:<publisher.name>/<purpose>`, behind the same prefix as
|
|
9
|
+
// the core's own (`intentic-s2s/1 <domain>\n`). The core's domains are single words with no `:` or `/`, and a domain
|
|
10
|
+
// ends at the newline, so the bytes an extension's signature covers can never be the bytes of an identity document,
|
|
11
|
+
// a vouch, an envelope or a session: no extension signature verifies as one.
|
|
12
|
+
|
|
13
|
+
// An extension's id as its manifest makes it, `publisher.name`, each half as the manifest's own pattern allows.
|
|
14
|
+
export const EXTENSION_SIGN_EXTENSION = /^[a-z0-9][a-z0-9-]*\.[a-z0-9][a-z0-9-]*$/u;
|
|
15
|
+
// What the record is for, as the extension names it: a lowercase word of up to 64 characters, dots and dashes inside.
|
|
16
|
+
export const EXTENSION_SIGN_PURPOSE = /^[a-z0-9][a-z0-9.-]{0,63}$/u;
|
|
17
|
+
// The most canonical-JSON bytes one signing covers. The door refuses a larger payload (413); a record that big should
|
|
18
|
+
// sign a digest of itself instead.
|
|
19
|
+
export const EXTENSION_SIGN_PAYLOAD_MAX_BYTES = 256 * 1024;
|
|
20
|
+
|
|
21
|
+
declare const extensionSignDomainBrand: unique symbol;
|
|
22
|
+
// A domain made by extensionSignDomain, so one is never spelled by hand past its checks. The core's four (sign.ts
|
|
23
|
+
// SignDomain) stay a closed union beside it.
|
|
24
|
+
export type ExtensionSignDomain = `ext:${string}/${string}` & { readonly [extensionSignDomainBrand]: true };
|
|
25
|
+
|
|
26
|
+
// What a signed record is: any JSON value, signed as its canonical bytes.
|
|
27
|
+
export type ExtensionSignPayload = z.infer<ReturnType<typeof z.json>>;
|
|
28
|
+
|
|
29
|
+
// Why an extension id or a purpose is no part of a domain, or undefined when both are.
|
|
30
|
+
export const extensionSignProblem = (extension: string, purpose: string): string | undefined => {
|
|
31
|
+
if (!EXTENSION_SIGN_EXTENSION.test(extension)) {
|
|
32
|
+
return `"${extension}" is not an extension id (publisher.name, each a lowercase word)`;
|
|
33
|
+
}
|
|
34
|
+
if (!EXTENSION_SIGN_PURPOSE.test(purpose)) {
|
|
35
|
+
return `"${purpose}" is not a signing purpose: a lowercase word of up to 64 characters, dots and dashes inside`;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// `ext:<extension>/<purpose>`. Throws a TypeError naming what is wrong with either half.
|
|
41
|
+
export const extensionSignDomain = (extension: string, purpose: string): ExtensionSignDomain => {
|
|
42
|
+
const problem = extensionSignProblem(extension, purpose);
|
|
43
|
+
if (problem !== undefined) {
|
|
44
|
+
throw new TypeError(`extension sign domain: ${problem}`);
|
|
45
|
+
}
|
|
46
|
+
// SAFETY: both halves passed the checks above, which is what the brand stands for.
|
|
47
|
+
return `ext:${extension}/${purpose}` as ExtensionSignDomain;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// The two halves of a domain read off the wire, or undefined for anything extensionSignDomain would not have made.
|
|
51
|
+
export const extensionSignDomainParts = (domain: string): { readonly extension: string; readonly purpose: string } | undefined => {
|
|
52
|
+
const match = /^ext:([^/]+)\/(.+)$/u.exec(domain);
|
|
53
|
+
const [, extension = "", purpose = ""] = match ?? [];
|
|
54
|
+
return match !== null && extensionSignProblem(extension, purpose) === undefined ? { extension, purpose } : undefined;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// This extension's hybrid signature over the payload's canonical JSON, for this purpose. Throws on a bad id or purpose
|
|
58
|
+
// and on a payload JSON cannot carry exactly.
|
|
59
|
+
export const signExtensionPayload = (extension: string, purpose: string, payload: ExtensionSignPayload, secret: SigningSecret): HybridSig =>
|
|
60
|
+
hybridSign(extensionSignDomain(extension, purpose), canonicalBytes(payload), secret);
|
|
61
|
+
|
|
62
|
+
// True only when `sig` is these keys' signature over the payload's canonical JSON for this extension and purpose; a bad
|
|
63
|
+
// id or purpose, a payload that is not JSON and any malformed part are false.
|
|
64
|
+
export const verifyExtensionPayload = (extension: string, purpose: string, payload: ExtensionSignPayload, sig: HybridSig, keys: S2sSigningKeys): boolean => {
|
|
65
|
+
if (extensionSignProblem(extension, purpose) !== undefined) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
let bytes: Uint8Array;
|
|
69
|
+
try {
|
|
70
|
+
bytes = canonicalBytes(payload);
|
|
71
|
+
} catch {
|
|
72
|
+
// allow(silent-catch): a value JSON cannot carry was never signed, so it verifies nothing
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
return hybridVerify(extensionSignDomain(extension, purpose), bytes, sig, keys);
|
|
76
|
+
};
|