@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,187 @@
|
|
|
1
|
+
import { type SandboxIdentity, SandboxIdentitySchema } from "../../schemas/s2s-identity.js";
|
|
2
|
+
import { canonicalBytes } from "./canonical.js";
|
|
3
|
+
import { classifyIdentityChange, type IdentityFacts, type PinnedIdentity, signIdentity, speaksS2s, verifyIdentity, vouchFor } from "./identity-doc.js";
|
|
4
|
+
import { secretKeysOf } from "./keys.js";
|
|
5
|
+
import { keySetFor, recoveryKeyFor } from "./s2s.testing.js";
|
|
6
|
+
import { hybridSign } from "./sign.js";
|
|
7
|
+
|
|
8
|
+
const NOW = 1_800_000_000_000;
|
|
9
|
+
const HOST = `0123456789ab.sandbox.example`;
|
|
10
|
+
const DOMAIN = `ops.acme.example`;
|
|
11
|
+
const first = keySetFor(`first`);
|
|
12
|
+
const second = keySetFor(`second`);
|
|
13
|
+
const stranger = keySetFor(`stranger`);
|
|
14
|
+
const recovery = recoveryKeyFor(`ops`);
|
|
15
|
+
const facts: IdentityFacts = { sandbox: `0123456789ab`, name: `ops`, host: HOST, domains: [DOMAIN], recovery: recovery.fingerprint, issuedAt: NOW };
|
|
16
|
+
|
|
17
|
+
const byFirst = vouchFor(facts.sandbox, second, { by: `key`, keySet: first }, NOW + 1);
|
|
18
|
+
const byRecovery = vouchFor(facts.sandbox, second, { by: `recovery`, recovery }, NOW + 2);
|
|
19
|
+
const original = signIdentity(facts, first);
|
|
20
|
+
const rotated = signIdentity({ ...facts, issuedAt: NOW + 1 }, second, byFirst);
|
|
21
|
+
const recovered = signIdentity({ ...facts, issuedAt: NOW + 2 }, second, byRecovery);
|
|
22
|
+
const rekeyed = signIdentity({ ...facts, issuedAt: NOW + 3 }, second);
|
|
23
|
+
|
|
24
|
+
const pinned: PinnedIdentity = { address: HOST, fingerprint: first.fingerprint, recovery: recovery.fingerprint };
|
|
25
|
+
|
|
26
|
+
describe("an identity document", () => {
|
|
27
|
+
it("carries the facts, the key set's public keys and fingerprint, and s2s/1, and verifies", () => {
|
|
28
|
+
expect(original).toMatchObject({
|
|
29
|
+
v: 1,
|
|
30
|
+
sandbox: `0123456789ab`,
|
|
31
|
+
name: `ops`,
|
|
32
|
+
host: HOST,
|
|
33
|
+
domains: [DOMAIN],
|
|
34
|
+
keys: first.public,
|
|
35
|
+
fingerprint: first.fingerprint,
|
|
36
|
+
recovery: recovery.fingerprint,
|
|
37
|
+
protocols: [`s2s/1`],
|
|
38
|
+
issuedAt: NOW,
|
|
39
|
+
});
|
|
40
|
+
expect(Object.keys(original)).not.toContain(`prev`);
|
|
41
|
+
expect(verifyIdentity(original)).toEqual({ ok: true });
|
|
42
|
+
expect(speaksS2s(original)).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("lists its domains once each, sorted", () => {
|
|
46
|
+
expect(signIdentity({ ...facts, domains: [`b.example`, `a.example`, `b.example`] }, first).domains).toEqual([`a.example`, `b.example`]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("survives JSON and the schema whole, so what arrives over the wire still verifies", () => {
|
|
50
|
+
const arrived = SandboxIdentitySchema.parse(JSON.parse(JSON.stringify(rotated)));
|
|
51
|
+
expect(arrived).toEqual(rotated);
|
|
52
|
+
expect(verifyIdentity(arrived)).toEqual({ ok: true });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("keeps a field this build does not know, which a later build signed, and fails without it", () => {
|
|
56
|
+
const { sig: _sig, ...unsigned } = original;
|
|
57
|
+
const later = { ...unsigned, motto: `hello` };
|
|
58
|
+
const signed = { ...later, sig: hybridSign(`identity`, canonicalBytes(later), secretKeysOf(first.secret)) };
|
|
59
|
+
const arrived = SandboxIdentitySchema.parse(JSON.parse(JSON.stringify(signed)));
|
|
60
|
+
expect(verifyIdentity(arrived)).toEqual({ ok: true });
|
|
61
|
+
const { motto: _motto, ...stripped } = arrived;
|
|
62
|
+
expect(verifyIdentity(stripped)).toEqual({ ok: false, reason: `its signature does not verify` });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("fails when any fact is changed after signing", () => {
|
|
66
|
+
const changes: readonly Partial<SandboxIdentity>[] = [
|
|
67
|
+
{ name: `opz` },
|
|
68
|
+
{ host: `evil.example` },
|
|
69
|
+
{ domains: [] },
|
|
70
|
+
{ domains: [DOMAIN, `bank.example`] },
|
|
71
|
+
{ issuedAt: NOW + 1 },
|
|
72
|
+
{ protocols: [`s2s/2`] },
|
|
73
|
+
{ recovery: recoveryKeyFor(`other`).fingerprint },
|
|
74
|
+
];
|
|
75
|
+
for (const change of changes) {
|
|
76
|
+
expect(verifyIdentity({ ...original, ...change })).toEqual({ ok: false, reason: `its signature does not verify` });
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("fails when its keys are not the ones its fingerprint is over", () => {
|
|
81
|
+
expect(verifyIdentity({ ...original, keys: { ...original.keys, x25519: stranger.public.x25519 } })).toEqual({
|
|
82
|
+
ok: false,
|
|
83
|
+
reason: `its fingerprint is not over its keys`,
|
|
84
|
+
});
|
|
85
|
+
expect(verifyIdentity({ ...original, fingerprint: stranger.fingerprint })).toEqual({ ok: false, reason: `its fingerprint is not over its keys` });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("fails when either half of its signature is another's", () => {
|
|
89
|
+
const theirs = signIdentity(facts, stranger);
|
|
90
|
+
expect(verifyIdentity({ ...original, sig: { ...original.sig, ed25519: theirs.sig.ed25519 } }).ok).toBe(false);
|
|
91
|
+
expect(verifyIdentity({ ...original, sig: { ...original.sig, mldsa65: theirs.sig.mldsa65 } }).ok).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("a document's prev", () => {
|
|
96
|
+
it("names the previous key set and verifies with it", () => {
|
|
97
|
+
expect(rotated.prev).toMatchObject({ by: `key`, fingerprint: first.fingerprint, keys: first.public, at: NOW + 1 });
|
|
98
|
+
expect(verifyIdentity(rotated)).toEqual({ ok: true });
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("names the recovery key and verifies with it", () => {
|
|
102
|
+
expect(recovered.prev).toMatchObject({ by: `recovery`, fingerprint: recovery.fingerprint, keys: recovery.public, at: NOW + 2 });
|
|
103
|
+
expect(verifyIdentity(recovered)).toEqual({ ok: true });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("still verifies when the document is signed again with other facts, the voucher long gone", () => {
|
|
107
|
+
const later = signIdentity({ ...facts, name: `ops 2`, domains: [`other.example`], issuedAt: NOW + 50 }, second, byRecovery);
|
|
108
|
+
expect(verifyIdentity(later)).toEqual({ ok: true });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("fails under another successor or for another sandbox", () => {
|
|
112
|
+
expect(verifyIdentity(signIdentity(facts, stranger, byFirst))).toEqual({ ok: false, reason: `its prev signature does not verify` });
|
|
113
|
+
expect(verifyIdentity(signIdentity({ ...facts, sandbox: `fedcba987654` }, second, byFirst))).toEqual({ ok: false, reason: `its prev signature does not verify` });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("fails when it claims a key it was not signed by, or a changed time", () => {
|
|
117
|
+
const byStranger = vouchFor(facts.sandbox, second, { by: `key`, keySet: stranger }, NOW + 1);
|
|
118
|
+
const claimed = { ...byStranger, fingerprint: first.fingerprint, keys: first.public };
|
|
119
|
+
expect(verifyIdentity(signIdentity(facts, second, claimed))).toEqual({ ok: false, reason: `its prev signature does not verify` });
|
|
120
|
+
expect(verifyIdentity(signIdentity(facts, second, { ...byFirst, at: NOW + 9 }))).toEqual({ ok: false, reason: `its prev signature does not verify` });
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("fails when its keys are not the ones its fingerprint names", () => {
|
|
124
|
+
expect(verifyIdentity(signIdentity(facts, second, { ...byFirst, keys: stranger.public }))).toEqual({
|
|
125
|
+
ok: false,
|
|
126
|
+
reason: `its prev keys are not the ones prev's fingerprint names`,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("the rotation table", () => {
|
|
132
|
+
it("is unchanged for the pinned key at the pinned address", () => {
|
|
133
|
+
expect(classifyIdentityChange({ pinned, next: original, address: HOST })).toEqual({ kind: `unchanged` });
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("is rotated for a new key whose prev the pinned key signed", () => {
|
|
137
|
+
expect(classifyIdentityChange({ pinned, next: rotated, address: HOST })).toEqual({ kind: `rotated` });
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("is recovered for a new key whose prev the pinned recovery key signed, and key-changed when none was pinned", () => {
|
|
141
|
+
expect(classifyIdentityChange({ pinned, next: recovered, address: HOST })).toEqual({ kind: `recovered` });
|
|
142
|
+
expect(classifyIdentityChange({ pinned: { address: HOST, fingerprint: first.fingerprint }, next: recovered, address: HOST })).toEqual({ kind: `key-changed` });
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("is rekeyed-by-domain for a domain contact whose TXT record names the new key and host", () => {
|
|
146
|
+
const byDomain: PinnedIdentity = { ...pinned, address: DOMAIN };
|
|
147
|
+
expect(classifyIdentityChange({ pinned: byDomain, next: rekeyed, address: DOMAIN, txt: { fingerprint: second.fingerprint, host: HOST } })).toEqual({
|
|
148
|
+
kind: `rekeyed-by-domain`,
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("is key-changed when the record names the old key or another host, when there is none, or when the contact is a host", () => {
|
|
153
|
+
const byDomain: PinnedIdentity = { ...pinned, address: DOMAIN };
|
|
154
|
+
expect(classifyIdentityChange({ pinned: byDomain, next: rekeyed, address: DOMAIN, txt: { fingerprint: first.fingerprint, host: HOST } })).toEqual({ kind: `key-changed` });
|
|
155
|
+
expect(classifyIdentityChange({ pinned: byDomain, next: rekeyed, address: DOMAIN, txt: { fingerprint: second.fingerprint, host: `other.example` } })).toEqual({
|
|
156
|
+
kind: `key-changed`,
|
|
157
|
+
});
|
|
158
|
+
expect(classifyIdentityChange({ pinned: byDomain, next: rekeyed, address: DOMAIN })).toEqual({ kind: `key-changed` });
|
|
159
|
+
expect(classifyIdentityChange({ pinned, next: rekeyed, address: HOST, txt: { fingerprint: second.fingerprint, host: HOST } })).toEqual({ kind: `key-changed` });
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("is key-changed for a new key whose prev a key nobody pinned signed", () => {
|
|
163
|
+
const elsewhere = signIdentity({ ...facts, issuedAt: NOW + 4 }, second, vouchFor(facts.sandbox, second, { by: `key`, keySet: stranger }, NOW + 4));
|
|
164
|
+
expect(classifyIdentityChange({ pinned, next: elsewhere, address: HOST })).toEqual({ kind: `key-changed` });
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("is moved for a new address the pinned key, or a prev it or the recovery key signed, proves", () => {
|
|
168
|
+
const at = `fedcba987654.sandbox.example`;
|
|
169
|
+
expect(classifyIdentityChange({ pinned, next: signIdentity({ ...facts, host: at }, first), address: at })).toEqual({ kind: `moved`, via: `same-key` });
|
|
170
|
+
expect(classifyIdentityChange({ pinned, next: signIdentity({ ...facts, host: at }, second, byFirst), address: at })).toEqual({
|
|
171
|
+
kind: `moved`,
|
|
172
|
+
via: `rotated`,
|
|
173
|
+
});
|
|
174
|
+
expect(classifyIdentityChange({ pinned, next: signIdentity({ ...facts, host: at }, second, byRecovery), address: at })).toEqual({
|
|
175
|
+
kind: `moved`,
|
|
176
|
+
via: `recovered`,
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("is unrelated for a new address with no proof: a new contact, not this one", () => {
|
|
181
|
+
expect(classifyIdentityChange({ pinned, next: signIdentity({ ...facts, host: `x.example` }, stranger), address: `x.example` })).toEqual({ kind: `unrelated` });
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("is invalid for a document that does not verify, whatever else it claims", () => {
|
|
185
|
+
expect(classifyIdentityChange({ pinned, next: { ...rotated, name: `changed` }, address: HOST })).toEqual({ kind: `invalid`, reason: `its signature does not verify` });
|
|
186
|
+
});
|
|
187
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import {
|
|
2
|
+
S2S_PROTOCOL,
|
|
3
|
+
type S2sAttestation,
|
|
4
|
+
type S2sIdentityPrev,
|
|
5
|
+
type S2sTxtRecord,
|
|
6
|
+
type SandboxIdentity,
|
|
7
|
+
} from "../../schemas/s2s-identity.js";
|
|
8
|
+
import { canonicalBytes } from "./canonical.js";
|
|
9
|
+
import { fingerprintOf, recoveryFingerprintOf, type S2sKeySet, type S2sRecoveryKey, secretKeysOf } from "./keys.js";
|
|
10
|
+
import { hybridSign, hybridVerify } from "./sign.js";
|
|
11
|
+
|
|
12
|
+
// THE IDENTITY DOCUMENT (schemas/s2s-identity.ts SandboxIdentitySchema): made and signed by its own key set, carrying in
|
|
13
|
+
// `prev` the previous key set's (or the recovery key's) vouch for it when the key changed. And what a contact concludes
|
|
14
|
+
// when the document a sandbox now serves is not the one it pinned: the rotation table of docs/architecture/messages.md
|
|
15
|
+
// as one pure function.
|
|
16
|
+
|
|
17
|
+
// What a document says of its sandbox, besides its keys.
|
|
18
|
+
export interface IdentityFacts {
|
|
19
|
+
readonly sandbox: string;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly host: string;
|
|
22
|
+
readonly domains: readonly string[];
|
|
23
|
+
readonly recovery?: string | undefined;
|
|
24
|
+
readonly issuedAt: number;
|
|
25
|
+
// What other sandboxes signed about it, as it publishes them (attestation.ts); none leaves the field out, so a sandbox
|
|
26
|
+
// with no attestations signs the very document it signed before they existed.
|
|
27
|
+
readonly attestations?: readonly S2sAttestation[] | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Who vouches for a new key set: the one before it, or the recovery key.
|
|
31
|
+
export type PrevSigner = { readonly by: "key"; readonly keySet: S2sKeySet } | { readonly by: "recovery"; readonly recovery: S2sRecoveryKey };
|
|
32
|
+
|
|
33
|
+
type KeyPrev = Extract<S2sIdentityPrev, { readonly by: "key" }>;
|
|
34
|
+
type RecoveryPrev = Extract<S2sIdentityPrev, { readonly by: "recovery" }>;
|
|
35
|
+
|
|
36
|
+
// What a vouch signs: the sandbox, its new key set's fingerprint, and who vouches when.
|
|
37
|
+
const vouchBody = (sandbox: string, successor: string, prev: Pick<S2sIdentityPrev, "by" | "fingerprint" | "at">) =>
|
|
38
|
+
canonicalBytes({ sandbox, successor, by: prev.by, fingerprint: prev.fingerprint, at: prev.at });
|
|
39
|
+
|
|
40
|
+
// The previous key set's, or the recovery key's, vouch that `successor` follows it for this sandbox: made once, when the
|
|
41
|
+
// key changes, and carried as the `prev` of every document the successor signs after.
|
|
42
|
+
export const vouchFor = (sandbox: string, successor: S2sKeySet, signer: PrevSigner, at: number): S2sIdentityPrev => {
|
|
43
|
+
if (signer.by === "key") {
|
|
44
|
+
const claim: Omit<KeyPrev, "sig"> = { by: "key", fingerprint: signer.keySet.fingerprint, keys: signer.keySet.public, at };
|
|
45
|
+
return { ...claim, sig: hybridSign("identity-prev", vouchBody(sandbox, successor.fingerprint, claim), secretKeysOf(signer.keySet.secret)) };
|
|
46
|
+
}
|
|
47
|
+
const claim: Omit<RecoveryPrev, "sig"> = { by: "recovery", fingerprint: signer.recovery.fingerprint, keys: signer.recovery.public, at };
|
|
48
|
+
return { ...claim, sig: hybridSign("identity-prev", vouchBody(sandbox, successor.fingerprint, claim), signer.recovery) };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// A document for these facts under this key set, signed by it, carrying the vouch for it when it has one.
|
|
52
|
+
export const signIdentity = (facts: IdentityFacts, keySet: S2sKeySet, prev?: S2sIdentityPrev): SandboxIdentity => {
|
|
53
|
+
const unsigned: Omit<SandboxIdentity, "sig"> = {
|
|
54
|
+
v: 1,
|
|
55
|
+
sandbox: facts.sandbox,
|
|
56
|
+
name: facts.name,
|
|
57
|
+
host: facts.host,
|
|
58
|
+
domains: [...new Set(facts.domains)].toSorted(),
|
|
59
|
+
keys: keySet.public,
|
|
60
|
+
fingerprint: keySet.fingerprint,
|
|
61
|
+
protocols: [S2S_PROTOCOL],
|
|
62
|
+
issuedAt: facts.issuedAt,
|
|
63
|
+
};
|
|
64
|
+
if (facts.recovery !== undefined) {
|
|
65
|
+
unsigned.recovery = facts.recovery;
|
|
66
|
+
}
|
|
67
|
+
if (prev !== undefined) {
|
|
68
|
+
unsigned.prev = prev;
|
|
69
|
+
}
|
|
70
|
+
if (facts.attestations !== undefined && facts.attestations.length > 0) {
|
|
71
|
+
unsigned.attestations = [...facts.attestations];
|
|
72
|
+
}
|
|
73
|
+
return { ...unsigned, sig: hybridSign("identity", canonicalBytes(unsigned), secretKeysOf(keySet.secret)) };
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// The bytes `sig` is over: the document without it, `prev` included.
|
|
77
|
+
const selfBody = (doc: SandboxIdentity): Omit<SandboxIdentity, "sig"> => {
|
|
78
|
+
const { sig: _sig, ...rest } = doc;
|
|
79
|
+
return rest;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type IdentityCheck = { readonly ok: true } | { readonly ok: false; readonly reason: string };
|
|
83
|
+
|
|
84
|
+
const prevProblem = (doc: SandboxIdentity, prev: S2sIdentityPrev): string | undefined => {
|
|
85
|
+
const fingerprint = prev.by === "key" ? fingerprintOf(prev.keys) : recoveryFingerprintOf(prev.keys);
|
|
86
|
+
if (fingerprint !== prev.fingerprint) {
|
|
87
|
+
return "its prev keys are not the ones prev's fingerprint names";
|
|
88
|
+
}
|
|
89
|
+
return hybridVerify("identity-prev", vouchBody(doc.sandbox, doc.fingerprint, prev), prev.sig, prev.keys) ? undefined : "its prev signature does not verify";
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Whether a document is what it says: its fingerprint is over its keys, its own signature verifies with them, and a
|
|
93
|
+
// `prev` it carries verifies with the keys it names. Says nothing about whether those are keys anyone trusts.
|
|
94
|
+
export const verifyIdentity = (doc: SandboxIdentity): IdentityCheck => {
|
|
95
|
+
if (fingerprintOf(doc.keys) !== doc.fingerprint) {
|
|
96
|
+
return { ok: false, reason: "its fingerprint is not over its keys" };
|
|
97
|
+
}
|
|
98
|
+
if (!hybridVerify("identity", canonicalBytes(selfBody(doc)), doc.sig, doc.keys)) {
|
|
99
|
+
return { ok: false, reason: "its signature does not verify" };
|
|
100
|
+
}
|
|
101
|
+
const problem = doc.prev === undefined ? undefined : prevProblem(doc, doc.prev);
|
|
102
|
+
return problem === undefined ? { ok: true } : { ok: false, reason: problem };
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Whether the far daemon speaks this protocol, by its own document.
|
|
106
|
+
export const speaksS2s = (doc: Pick<SandboxIdentity, "protocols">): boolean => doc.protocols.includes(S2S_PROTOCOL);
|
|
107
|
+
|
|
108
|
+
// What a contact pinned: where it reached the sandbox, its key, and its recovery key's fingerprint when it had one.
|
|
109
|
+
export interface PinnedIdentity {
|
|
110
|
+
readonly address: string;
|
|
111
|
+
readonly fingerprint: string;
|
|
112
|
+
readonly recovery?: string | undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// What the document now served means for the contact. `unchanged`: the pinned key. `rotated`, `recovered`,
|
|
116
|
+
// `rekeyed-by-domain`: a new key, accepted with that notice (the last a warning). `key-changed`: a new key with no proof,
|
|
117
|
+
// held both ways until a person trusts it. `moved`: a new address, proven by the pinned key (`same-key`), or by a `prev`
|
|
118
|
+
// the pinned key or recovery key signed. `unrelated`: a new address with no such proof, so a new contact, not this one.
|
|
119
|
+
// `invalid`: the document does not verify at all.
|
|
120
|
+
export type IdentityChange =
|
|
121
|
+
| { readonly kind: "unchanged" | "rotated" | "recovered" | "rekeyed-by-domain" | "key-changed" | "unrelated" }
|
|
122
|
+
| { readonly kind: "moved"; readonly via: "same-key" | "rotated" | "recovered" }
|
|
123
|
+
| { readonly kind: "invalid"; readonly reason: string };
|
|
124
|
+
|
|
125
|
+
export interface IdentityChangeInput {
|
|
126
|
+
readonly pinned: PinnedIdentity;
|
|
127
|
+
// The document as fetched now.
|
|
128
|
+
readonly next: SandboxIdentity;
|
|
129
|
+
// The address it was resolved from.
|
|
130
|
+
readonly address: string;
|
|
131
|
+
// The address's TXT record, when it has one (address.ts resolveAddress).
|
|
132
|
+
readonly txt?: S2sTxtRecord | undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
type Continuity = "same-key" | "rotated" | "recovered" | undefined;
|
|
136
|
+
|
|
137
|
+
const continuityOf = (pinned: PinnedIdentity, next: SandboxIdentity): Continuity => {
|
|
138
|
+
if (next.fingerprint === pinned.fingerprint) {
|
|
139
|
+
return "same-key";
|
|
140
|
+
}
|
|
141
|
+
if (next.prev?.by === "key" && next.prev.fingerprint === pinned.fingerprint) {
|
|
142
|
+
return "rotated";
|
|
143
|
+
}
|
|
144
|
+
if (next.prev?.by === "recovery" && pinned.recovery !== undefined && next.prev.fingerprint === pinned.recovery) {
|
|
145
|
+
return "recovered";
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// The rotation table (docs/architecture/messages.md, "Rotation and recovery"). Pure: the caller fetched the document
|
|
151
|
+
// and the record, and only a document that verifies is weighed.
|
|
152
|
+
export const classifyIdentityChange = ({ pinned, next, address, txt }: IdentityChangeInput): IdentityChange => {
|
|
153
|
+
const check = verifyIdentity(next);
|
|
154
|
+
if (!check.ok) {
|
|
155
|
+
return { kind: "invalid", reason: check.reason };
|
|
156
|
+
}
|
|
157
|
+
const continuity = continuityOf(pinned, next);
|
|
158
|
+
if (address !== pinned.address) {
|
|
159
|
+
return continuity === undefined ? { kind: "unrelated" } : { kind: "moved", via: continuity };
|
|
160
|
+
}
|
|
161
|
+
if (continuity === "same-key") {
|
|
162
|
+
return { kind: "unchanged" };
|
|
163
|
+
}
|
|
164
|
+
if (continuity !== undefined) {
|
|
165
|
+
return { kind: continuity };
|
|
166
|
+
}
|
|
167
|
+
const isDomain = address !== next.host && next.domains.includes(address);
|
|
168
|
+
return isDomain && txt?.fingerprint === next.fingerprint && txt.host === next.host ? { kind: "rekeyed-by-domain" } : { kind: "key-changed" };
|
|
169
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The sandbox-to-sandbox protocol's primitives (docs/architecture/messages.md), imported as
|
|
2
|
+
// `@intentic/sandbox-contract/s2s`: canonical JSON and its encodings, hybrid key sets and signatures, the identity
|
|
3
|
+
// document and the rotation table, addresses and their TXT records, the message envelope, and call sessions. Their
|
|
4
|
+
// shapes are schemas/s2s.ts. Pure JavaScript on the @noble libraries, so Node, Bun and a browser run the same code.
|
|
5
|
+
export * from "./address.js";
|
|
6
|
+
export * from "./attestation.js";
|
|
7
|
+
export * from "./canonical.js";
|
|
8
|
+
export * from "./envelope.js";
|
|
9
|
+
export * from "./extension-sign.js";
|
|
10
|
+
export * from "./identity-doc.js";
|
|
11
|
+
export * from "./kem.js";
|
|
12
|
+
export * from "./keys.js";
|
|
13
|
+
export * from "./passkey-cosign.js";
|
|
14
|
+
export * from "./session.js";
|
|
15
|
+
export * from "./sign.js";
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { x25519 } from "@noble/curves/ed25519.js";
|
|
2
|
+
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
3
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
4
|
+
import { randomBytes } from "@noble/hashes/utils.js";
|
|
5
|
+
import { ml_kem768 } from "@noble/post-quantum/ml-kem.js";
|
|
6
|
+
import type { S2sPublicKeys } from "../../schemas/s2s-identity.js";
|
|
7
|
+
import { concatBytes, fromBase64Url, toBase64Url, utf8 } from "./canonical.js";
|
|
8
|
+
import type { RandomBytes, S2sSecretKeys } from "./keys.js";
|
|
9
|
+
|
|
10
|
+
// The hybrid key agreement both envelopes and call sessions use: an ephemeral X25519 exchange and an ML-KEM-768
|
|
11
|
+
// encapsulation to the receiver's key set, their shared secrets combined by HKDF-SHA256 (ML-KEM's first), so the key
|
|
12
|
+
// holds while either family does.
|
|
13
|
+
|
|
14
|
+
// What the sender sends for it: its ephemeral X25519 public key and the ML-KEM ciphertext, base64url.
|
|
15
|
+
export interface HybridEncapsulation {
|
|
16
|
+
readonly x25519: string;
|
|
17
|
+
readonly mlkem768: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// The combined input keying material: mlkem_ss ‖ x25519_ss.
|
|
21
|
+
export type SharedSecret = Uint8Array;
|
|
22
|
+
|
|
23
|
+
// A fresh agreement: what the sender sends, and the secret both sides end up holding.
|
|
24
|
+
export interface Encapsulated {
|
|
25
|
+
readonly enc: HybridEncapsulation;
|
|
26
|
+
readonly secret: SharedSecret;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// A fresh agreement to the receiver's public keys: what to send, and the secret both sides will hold.
|
|
30
|
+
export const encapsulate = (to: Pick<S2sPublicKeys, "x25519" | "mlkem768">, random: RandomBytes = randomBytes): Encapsulated => {
|
|
31
|
+
const theirX = fromBase64Url(to.x25519);
|
|
32
|
+
const theirKem = fromBase64Url(to.mlkem768);
|
|
33
|
+
if (theirX === undefined || theirKem === undefined) {
|
|
34
|
+
throw new Error("s2s: the receiver's key-agreement keys are not base64url");
|
|
35
|
+
}
|
|
36
|
+
const ephemeral = random(32);
|
|
37
|
+
const kem = ml_kem768.encapsulate(theirKem, random(32));
|
|
38
|
+
return {
|
|
39
|
+
enc: { x25519: toBase64Url(x25519.getPublicKey(ephemeral)), mlkem768: toBase64Url(kem.cipherText) },
|
|
40
|
+
secret: concatBytes(kem.sharedSecret, x25519.getSharedSecret(ephemeral, theirX)),
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// The same secret on the receiving side, or undefined when the encapsulation is malformed (a key of the wrong length, a
|
|
45
|
+
// low-order X25519 point). ML-KEM decapsulation never fails outright: a wrong ciphertext yields a wrong secret, which
|
|
46
|
+
// the AEAD then refuses.
|
|
47
|
+
export const decapsulate = (enc: HybridEncapsulation, keys: Pick<S2sSecretKeys, "x25519" | "mlkem768">): SharedSecret | undefined => {
|
|
48
|
+
const theirX = fromBase64Url(enc.x25519);
|
|
49
|
+
const cipherText = fromBase64Url(enc.mlkem768);
|
|
50
|
+
if (theirX === undefined || cipherText === undefined) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
return concatBytes(ml_kem768.decapsulate(cipherText, keys.mlkem768), x25519.getSharedSecret(keys.x25519, theirX));
|
|
55
|
+
} catch {
|
|
56
|
+
// allow(silent-catch): the library throws on a malformed key or ciphertext; it is a key nobody can derive.
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// A 32-byte key from the shared secret, salted with the hash of what it protects and bound to what it is for.
|
|
62
|
+
export const deriveKey = (secret: SharedSecret, salt: Uint8Array, info: string): Uint8Array => hkdf(sha256, secret, salt, utf8(info), 32);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { S2sFingerprintSchema, S2sPublicKeysSchema, S2sRecoveryFingerprintSchema } from "../../schemas/s2s-identity.js";
|
|
2
|
+
import { fromBase64Url } from "./canonical.js";
|
|
3
|
+
import { fingerprintOf, generateKeySet, keySetFromSeeds, recoveryFingerprintOf, recoveryKeyFromSecret } from "./keys.js";
|
|
4
|
+
import { keySetFor, recoveryKeyFor, seededRandom } from "./s2s.testing.js";
|
|
5
|
+
|
|
6
|
+
describe("a hybrid key set", () => {
|
|
7
|
+
it("holds four public keys of each algorithm's length and four seeds", () => {
|
|
8
|
+
const set = keySetFor(`ops`);
|
|
9
|
+
expect(S2sPublicKeysSchema.parse(set.public)).toEqual(set.public);
|
|
10
|
+
expect(fromBase64Url(set.public.ed25519)?.length).toBe(32);
|
|
11
|
+
expect(fromBase64Url(set.public.mldsa65)?.length).toBe(1952);
|
|
12
|
+
expect(fromBase64Url(set.public.x25519)?.length).toBe(32);
|
|
13
|
+
expect(fromBase64Url(set.public.mlkem768)?.length).toBe(1184);
|
|
14
|
+
expect(Object.fromEntries(Object.entries(set.secret).map(([name, seed]) => [name, fromBase64Url(seed)?.length]))).toEqual({
|
|
15
|
+
ed25519: 32,
|
|
16
|
+
mldsa65: 32,
|
|
17
|
+
x25519: 32,
|
|
18
|
+
mlkem768: 64,
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("is derived again, whole, from the seeds alone", () => {
|
|
23
|
+
const set = keySetFor(`ops`);
|
|
24
|
+
expect(keySetFromSeeds({ ...set.secret })).toEqual(set);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("is the same set for the same randomness and another for other randomness", () => {
|
|
28
|
+
expect(generateKeySet(seededRandom(`a`)).fingerprint).toBe(generateKeySet(seededRandom(`a`)).fingerprint);
|
|
29
|
+
expect(generateKeySet(seededRandom(`a`)).fingerprint).not.toBe(generateKeySet(seededRandom(`b`)).fingerprint);
|
|
30
|
+
expect(generateKeySet().fingerprint).not.toBe(generateKeySet().fingerprint);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("refuses a seed of the wrong length", () => {
|
|
34
|
+
const set = keySetFor(`ops`);
|
|
35
|
+
expect(() => keySetFromSeeds({ ...set.secret, mlkem768: set.secret.x25519 })).toThrow(`s2s key set: the mlkem768 seed is not 64 bytes of base64url`);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("a fingerprint", () => {
|
|
40
|
+
it("is isk1: and 52 base32 characters over the canonical public keys, whatever order they were listed in", () => {
|
|
41
|
+
const set = keySetFor(`ops`);
|
|
42
|
+
expect(S2sFingerprintSchema.safeParse(set.fingerprint).success).toBe(true);
|
|
43
|
+
const { ed25519, mldsa65, x25519, mlkem768 } = set.public;
|
|
44
|
+
expect(fingerprintOf({ mlkem768, x25519, mldsa65, ed25519 })).toBe(set.fingerprint);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("changes when any one key does", () => {
|
|
48
|
+
const set = keySetFor(`ops`);
|
|
49
|
+
const other = keySetFor(`other`);
|
|
50
|
+
for (const name of [`ed25519`, `mldsa65`, `x25519`, `mlkem768`] as const) {
|
|
51
|
+
expect(fingerprintOf({ ...set.public, [name]: other.public[name] })).not.toBe(set.fingerprint);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("a recovery key", () => {
|
|
57
|
+
it("is a signing pair whose fingerprint is isr1:, never an isk1: one", () => {
|
|
58
|
+
const recovery = recoveryKeyFor(`ops`);
|
|
59
|
+
expect(S2sRecoveryFingerprintSchema.safeParse(recovery.fingerprint).success).toBe(true);
|
|
60
|
+
expect(recovery.fingerprint.startsWith(`isr1:`)).toBe(true);
|
|
61
|
+
expect(recoveryFingerprintOf(recovery.public)).toBe(recovery.fingerprint);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("comes back whole from its pasted secret, whitespace forgiven", () => {
|
|
65
|
+
const recovery = recoveryKeyFor(`ops`);
|
|
66
|
+
expect(recovery.secret.startsWith(`isrs1.`)).toBe(true);
|
|
67
|
+
expect(recoveryKeyFromSecret(` ${recovery.secret}\n`)).toEqual(recovery);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("refuses a secret without its prefix, cut short, or carrying a stray character", () => {
|
|
71
|
+
const { secret } = recoveryKeyFor(`ops`);
|
|
72
|
+
expect(recoveryKeyFromSecret(secret.slice(`isrs1.`.length))).toBeUndefined();
|
|
73
|
+
expect(recoveryKeyFromSecret(secret.slice(0, -4))).toBeUndefined();
|
|
74
|
+
expect(recoveryKeyFromSecret(`${secret.slice(0, 10)}+${secret.slice(11)}`)).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ed25519, x25519 } from "@noble/curves/ed25519.js";
|
|
2
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
|
+
import { randomBytes } from "@noble/hashes/utils.js";
|
|
4
|
+
import { ml_dsa65 } from "@noble/post-quantum/ml-dsa.js";
|
|
5
|
+
import { ml_kem768 } from "@noble/post-quantum/ml-kem.js";
|
|
6
|
+
import {
|
|
7
|
+
S2S_FINGERPRINT_PREFIX,
|
|
8
|
+
S2S_RECOVERY_PREFIX,
|
|
9
|
+
S2S_RECOVERY_SECRET_PREFIX,
|
|
10
|
+
type S2sPublicKeys,
|
|
11
|
+
type S2sSigningKeys,
|
|
12
|
+
} from "../../schemas/s2s-identity.js";
|
|
13
|
+
import { canonicalBytes, concatBytes, fromBase64Url, toBase32, toBase64Url } from "./canonical.js";
|
|
14
|
+
|
|
15
|
+
// A sandbox's hybrid key set: Ed25519 and ML-DSA-65 to sign, X25519 and ML-KEM-768 to agree keys. The secret half is
|
|
16
|
+
// kept as the four seeds each algorithm derives its key from (32, 32, 32 and 64 bytes), which is all that has to be
|
|
17
|
+
// stored; the expanded keys are derived again on load and held in memory.
|
|
18
|
+
|
|
19
|
+
// The secret half, base64url: each algorithm's seed.
|
|
20
|
+
export interface S2sSecretSeeds {
|
|
21
|
+
readonly ed25519: string;
|
|
22
|
+
readonly mldsa65: string;
|
|
23
|
+
readonly x25519: string;
|
|
24
|
+
readonly mlkem768: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// A key set as one holds it to sign and open: the public keys and fingerprint it publishes, the seeds it stores.
|
|
28
|
+
export interface S2sKeySet {
|
|
29
|
+
readonly public: S2sPublicKeys;
|
|
30
|
+
readonly secret: S2sSecretSeeds;
|
|
31
|
+
readonly fingerprint: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The expanded secret keys, raw: what signing and decapsulation take.
|
|
35
|
+
export interface S2sSecretKeys {
|
|
36
|
+
readonly ed25519: Uint8Array;
|
|
37
|
+
readonly mldsa65: Uint8Array;
|
|
38
|
+
readonly x25519: Uint8Array;
|
|
39
|
+
readonly mlkem768: Uint8Array;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// A recovery key: a signing pair alone, which only ever signs a new key set's `prev`.
|
|
43
|
+
export interface S2sRecoveryKey {
|
|
44
|
+
readonly public: S2sSigningKeys;
|
|
45
|
+
readonly fingerprint: string;
|
|
46
|
+
// The two seeds as one string to keep off the sandbox: `isrs1.` and the base64url of ed25519 ‖ mldsa65.
|
|
47
|
+
readonly secret: string;
|
|
48
|
+
readonly ed25519: Uint8Array;
|
|
49
|
+
readonly mldsa65: Uint8Array;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The source of fresh bytes, a parameter so a test can make a key set it can name.
|
|
53
|
+
export type RandomBytes = (length: number) => Uint8Array;
|
|
54
|
+
|
|
55
|
+
const SEED_BYTES = { ed25519: 32, mldsa65: 32, x25519: 32, mlkem768: 64 } as const;
|
|
56
|
+
|
|
57
|
+
const fingerprintWith = (prefix: string, keys: S2sSigningKeys | S2sPublicKeys): string => `${prefix}${toBase32(sha256(canonicalBytes(keys)))}`;
|
|
58
|
+
|
|
59
|
+
// A key set's fingerprint: `isk1:` and the base32 of the SHA-256 over its canonical public keys.
|
|
60
|
+
export const fingerprintOf = (keys: S2sPublicKeys): string =>
|
|
61
|
+
fingerprintWith(S2S_FINGERPRINT_PREFIX, { ed25519: keys.ed25519, mldsa65: keys.mldsa65, x25519: keys.x25519, mlkem768: keys.mlkem768 });
|
|
62
|
+
|
|
63
|
+
// A recovery key's fingerprint: `isr1:` and the base32 of the SHA-256 over its canonical public signing keys.
|
|
64
|
+
export const recoveryFingerprintOf = (keys: S2sSigningKeys): string => fingerprintWith(S2S_RECOVERY_PREFIX, { ed25519: keys.ed25519, mldsa65: keys.mldsa65 });
|
|
65
|
+
|
|
66
|
+
const seedOf = (seeds: S2sSecretSeeds, algorithm: keyof typeof SEED_BYTES): Uint8Array => {
|
|
67
|
+
const seed = fromBase64Url(seeds[algorithm]);
|
|
68
|
+
if (seed?.length !== SEED_BYTES[algorithm]) {
|
|
69
|
+
throw new Error(`s2s key set: the ${algorithm} seed is not ${SEED_BYTES[algorithm]} bytes of base64url`);
|
|
70
|
+
}
|
|
71
|
+
return seed;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
interface Expanded {
|
|
75
|
+
readonly set: S2sKeySet;
|
|
76
|
+
readonly secret: S2sSecretKeys;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ML-DSA's key derivation costs milliseconds, so the few key sets a daemon holds are expanded once.
|
|
80
|
+
const EXPANDED_KEPT = 8;
|
|
81
|
+
const expanded = new Map<string, Expanded>();
|
|
82
|
+
|
|
83
|
+
const expand = (seeds: S2sSecretSeeds): Expanded => {
|
|
84
|
+
const key = `${seeds.ed25519}.${seeds.mldsa65}.${seeds.x25519}.${seeds.mlkem768}`;
|
|
85
|
+
const known = expanded.get(key);
|
|
86
|
+
if (known !== undefined) {
|
|
87
|
+
return known;
|
|
88
|
+
}
|
|
89
|
+
const edSeed = seedOf(seeds, "ed25519");
|
|
90
|
+
const dsa = ml_dsa65.keygen(seedOf(seeds, "mldsa65"));
|
|
91
|
+
const xSecret = seedOf(seeds, "x25519");
|
|
92
|
+
const kem = ml_kem768.keygen(seedOf(seeds, "mlkem768"));
|
|
93
|
+
const publicKeys: S2sPublicKeys = {
|
|
94
|
+
ed25519: toBase64Url(ed25519.getPublicKey(edSeed)),
|
|
95
|
+
mldsa65: toBase64Url(dsa.publicKey),
|
|
96
|
+
x25519: toBase64Url(x25519.getPublicKey(xSecret)),
|
|
97
|
+
mlkem768: toBase64Url(kem.publicKey),
|
|
98
|
+
};
|
|
99
|
+
const made: Expanded = {
|
|
100
|
+
set: { public: publicKeys, secret: seeds, fingerprint: fingerprintOf(publicKeys) },
|
|
101
|
+
secret: { ed25519: edSeed, mldsa65: dsa.secretKey, x25519: xSecret, mlkem768: kem.secretKey },
|
|
102
|
+
};
|
|
103
|
+
if (expanded.size >= EXPANDED_KEPT) {
|
|
104
|
+
const oldest = expanded.keys().next();
|
|
105
|
+
if (oldest.done !== true) {
|
|
106
|
+
expanded.delete(oldest.value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
expanded.set(key, made);
|
|
110
|
+
return made;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// The key set the seeds derive: its public keys and fingerprint. Throws on a seed of the wrong length.
|
|
114
|
+
export const keySetFromSeeds = (seeds: S2sSecretSeeds): S2sKeySet => expand(seeds).set;
|
|
115
|
+
|
|
116
|
+
// The expanded secret keys the seeds derive, for signing and opening.
|
|
117
|
+
export const secretKeysOf = (seeds: S2sSecretSeeds): S2sSecretKeys => expand(seeds).secret;
|
|
118
|
+
|
|
119
|
+
// A fresh key set from fresh seeds.
|
|
120
|
+
export const generateKeySet = (random: RandomBytes = randomBytes): S2sKeySet =>
|
|
121
|
+
keySetFromSeeds({
|
|
122
|
+
ed25519: toBase64Url(random(SEED_BYTES.ed25519)),
|
|
123
|
+
mldsa65: toBase64Url(random(SEED_BYTES.mldsa65)),
|
|
124
|
+
x25519: toBase64Url(random(SEED_BYTES.x25519)),
|
|
125
|
+
mlkem768: toBase64Url(random(SEED_BYTES.mlkem768)),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const recoveryFrom = (edSeed: Uint8Array, dsaSeed: Uint8Array): S2sRecoveryKey => {
|
|
129
|
+
const dsa = ml_dsa65.keygen(dsaSeed);
|
|
130
|
+
const publicKeys: S2sSigningKeys = { ed25519: toBase64Url(ed25519.getPublicKey(edSeed)), mldsa65: toBase64Url(dsa.publicKey) };
|
|
131
|
+
return {
|
|
132
|
+
public: publicKeys,
|
|
133
|
+
fingerprint: recoveryFingerprintOf(publicKeys),
|
|
134
|
+
secret: `${S2S_RECOVERY_SECRET_PREFIX}${toBase64Url(concatBytes(edSeed, dsaSeed))}`,
|
|
135
|
+
ed25519: edSeed,
|
|
136
|
+
mldsa65: dsa.secretKey,
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// A fresh recovery key. Its `secret` is shown once and kept off the sandbox; only its public half and fingerprint stay.
|
|
141
|
+
export const generateRecoveryKey = (random: RandomBytes = randomBytes): S2sRecoveryKey =>
|
|
142
|
+
recoveryFrom(random(SEED_BYTES.ed25519), random(SEED_BYTES.mldsa65));
|
|
143
|
+
|
|
144
|
+
// The recovery key a pasted secret stands for, or undefined when it is not one (surrounding whitespace forgiven).
|
|
145
|
+
export const recoveryKeyFromSecret = (secret: string): S2sRecoveryKey | undefined => {
|
|
146
|
+
const text = secret.trim();
|
|
147
|
+
if (!text.startsWith(S2S_RECOVERY_SECRET_PREFIX)) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
const seeds = fromBase64Url(text.slice(S2S_RECOVERY_SECRET_PREFIX.length));
|
|
151
|
+
if (seeds?.length !== SEED_BYTES.ed25519 + SEED_BYTES.mldsa65) {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
return recoveryFrom(seeds.slice(0, SEED_BYTES.ed25519), seeds.slice(SEED_BYTES.ed25519));
|
|
155
|
+
};
|