@adea-ai/runtime-gateway-protocol 1.12.0 → 1.14.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/dist/content-key.d.ts +85 -0
- package/dist/content-key.js +189 -0
- package/dist/device-relay.d.ts +11 -0
- package/dist/device-relay.js +29 -7
- package/package.json +5 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type AuthorizedSenderKey, type AuthorizedSenderKeyring, type PinnedPrimarySigningKey, type RelaySigningKeyPair } from './device-relay.js';
|
|
3
|
+
export declare const CONTENT_KEY_BYTES = 32;
|
|
4
|
+
export declare const CONTENT_SEAL_VERSION: 1;
|
|
5
|
+
export declare const CONTENT_KEY_WRAP_VERSION: 1;
|
|
6
|
+
export declare const MAX_CONTENT_PLAINTEXT_BYTES: number;
|
|
7
|
+
export declare const ContentKeyErrorCodeSchema: z.ZodEnum<{
|
|
8
|
+
CONTENT_DECRYPTION_FAILED: "CONTENT_DECRYPTION_FAILED";
|
|
9
|
+
CONTENT_KEY_RECIPIENT_INVALID: "CONTENT_KEY_RECIPIENT_INVALID";
|
|
10
|
+
CONTENT_KEY_VERSION_MISMATCH: "CONTENT_KEY_VERSION_MISMATCH";
|
|
11
|
+
CONTENT_KEY_WRAP_INVALID: "CONTENT_KEY_WRAP_INVALID";
|
|
12
|
+
CONTENT_SEAL_INVALID: "CONTENT_SEAL_INVALID";
|
|
13
|
+
CONTENT_TOO_LARGE: "CONTENT_TOO_LARGE";
|
|
14
|
+
}>;
|
|
15
|
+
export type ContentKeyErrorCode = z.infer<typeof ContentKeyErrorCodeSchema>;
|
|
16
|
+
export declare class ContentKeyError extends Error {
|
|
17
|
+
readonly code: ContentKeyErrorCode;
|
|
18
|
+
constructor(code: ContentKeyErrorCode);
|
|
19
|
+
}
|
|
20
|
+
export declare const ContentSealSchema: z.ZodObject<{
|
|
21
|
+
ckv: z.ZodNumber;
|
|
22
|
+
nonce: z.ZodString;
|
|
23
|
+
ciphertext: z.ZodString;
|
|
24
|
+
}, z.core.$strict>;
|
|
25
|
+
export type ContentSeal = z.infer<typeof ContentSealSchema>;
|
|
26
|
+
export interface ContentBinding {
|
|
27
|
+
readonly workspaceId: string;
|
|
28
|
+
readonly messageId: string;
|
|
29
|
+
readonly field: string;
|
|
30
|
+
}
|
|
31
|
+
export interface ContentKey {
|
|
32
|
+
readonly ckv: number;
|
|
33
|
+
readonly key: CryptoKey;
|
|
34
|
+
}
|
|
35
|
+
export declare function generateContentKey(ckv: number): Promise<{
|
|
36
|
+
readonly ckv: number;
|
|
37
|
+
readonly raw: Uint8Array;
|
|
38
|
+
}>;
|
|
39
|
+
export declare function importContentKey(ckv: number, raw: Uint8Array): Promise<ContentKey>;
|
|
40
|
+
export declare function sealContent(key: ContentKey, binding: ContentBinding, plaintext: string): Promise<ContentSeal>;
|
|
41
|
+
export declare function openContent(key: ContentKey, binding: ContentBinding, sealed: unknown): Promise<string>;
|
|
42
|
+
export declare const ContentKeyWrapBodySchema: z.ZodObject<{
|
|
43
|
+
wrapVersion: z.ZodLiteral<1>;
|
|
44
|
+
workspaceId: z.ZodString;
|
|
45
|
+
ckv: z.ZodNumber;
|
|
46
|
+
keyringRevision: z.ZodNumber;
|
|
47
|
+
recipientKeyId: z.ZodString;
|
|
48
|
+
recipientEncryptionKeyId: z.ZodString;
|
|
49
|
+
primarySigningKeyId: z.ZodString;
|
|
50
|
+
wrappedAt: z.ZodISODateTime;
|
|
51
|
+
encapsulatedKey: z.ZodString;
|
|
52
|
+
ciphertext: z.ZodString;
|
|
53
|
+
}, z.core.$strict>;
|
|
54
|
+
export declare const ContentKeyWrapSchema: z.ZodObject<{
|
|
55
|
+
wrapVersion: z.ZodLiteral<1>;
|
|
56
|
+
workspaceId: z.ZodString;
|
|
57
|
+
ckv: z.ZodNumber;
|
|
58
|
+
keyringRevision: z.ZodNumber;
|
|
59
|
+
recipientKeyId: z.ZodString;
|
|
60
|
+
recipientEncryptionKeyId: z.ZodString;
|
|
61
|
+
primarySigningKeyId: z.ZodString;
|
|
62
|
+
wrappedAt: z.ZodISODateTime;
|
|
63
|
+
encapsulatedKey: z.ZodString;
|
|
64
|
+
ciphertext: z.ZodString;
|
|
65
|
+
signature: z.ZodString;
|
|
66
|
+
}, z.core.$strict>;
|
|
67
|
+
export type ContentKeyWrap = z.infer<typeof ContentKeyWrapSchema>;
|
|
68
|
+
export declare function contentKeyRecipients(keyring: AuthorizedSenderKeyring): AuthorizedSenderKey[];
|
|
69
|
+
export declare function wrapContentKey(input: {
|
|
70
|
+
readonly contentKey: {
|
|
71
|
+
readonly ckv: number;
|
|
72
|
+
readonly raw: Uint8Array;
|
|
73
|
+
};
|
|
74
|
+
readonly keyring: AuthorizedSenderKeyring;
|
|
75
|
+
readonly recipientKeyId: string;
|
|
76
|
+
readonly primary: RelaySigningKeyPair;
|
|
77
|
+
readonly now: Date;
|
|
78
|
+
}): Promise<ContentKeyWrap>;
|
|
79
|
+
export declare function unwrapContentKey(input: {
|
|
80
|
+
readonly wrap: unknown;
|
|
81
|
+
readonly pinned: PinnedPrimarySigningKey;
|
|
82
|
+
readonly workspaceId: string;
|
|
83
|
+
readonly recipientKeyId: string;
|
|
84
|
+
readonly encryptionPrivateKey: string;
|
|
85
|
+
}): Promise<ContentKey>;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Aes128Gcm, CipherSuite, HkdfSha256 } from '@hpke/core';
|
|
2
|
+
import { DhkemX25519HkdfSha256 } from '@hpke/dhkem-x25519';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { canonicalJsonStringify } from '@adea-ai/contracts/canonical-json';
|
|
5
|
+
import { decodeBase64Url, encodeBase64Url, signRelayBytes, verifyRelayBytes, } from './device-relay.js';
|
|
6
|
+
export const CONTENT_KEY_BYTES = 32;
|
|
7
|
+
export const CONTENT_SEAL_VERSION = 1;
|
|
8
|
+
export const CONTENT_KEY_WRAP_VERSION = 1;
|
|
9
|
+
export const MAX_CONTENT_PLAINTEXT_BYTES = 1024 * 1024;
|
|
10
|
+
const CONTENT_NONCE_BYTES = 12;
|
|
11
|
+
const AEAD_TAG_BYTES = 16;
|
|
12
|
+
const WRAP_INFO = new TextEncoder().encode('adea-content-key-wrap-v1');
|
|
13
|
+
const WRAP_SIGNATURE_CONTEXT = 'adea-content-key-wrap-signature-v1';
|
|
14
|
+
const wrapSuite = new CipherSuite({
|
|
15
|
+
kem: new DhkemX25519HkdfSha256(),
|
|
16
|
+
kdf: new HkdfSha256(),
|
|
17
|
+
aead: new Aes128Gcm(),
|
|
18
|
+
});
|
|
19
|
+
export const ContentKeyErrorCodeSchema = z.enum([
|
|
20
|
+
'CONTENT_SEAL_INVALID',
|
|
21
|
+
'CONTENT_TOO_LARGE',
|
|
22
|
+
'CONTENT_KEY_VERSION_MISMATCH',
|
|
23
|
+
'CONTENT_DECRYPTION_FAILED',
|
|
24
|
+
'CONTENT_KEY_WRAP_INVALID',
|
|
25
|
+
'CONTENT_KEY_RECIPIENT_INVALID',
|
|
26
|
+
]);
|
|
27
|
+
export class ContentKeyError extends Error {
|
|
28
|
+
code;
|
|
29
|
+
constructor(code) {
|
|
30
|
+
super('Conversation content was rejected');
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.name = 'ContentKeyError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const IdentifierSchema = z
|
|
36
|
+
.string()
|
|
37
|
+
.min(1)
|
|
38
|
+
.max(128)
|
|
39
|
+
.regex(/^[A-Za-z0-9._:-]+$/);
|
|
40
|
+
const VersionSchema = z.number().int().min(1);
|
|
41
|
+
const Base64UrlSchema = z
|
|
42
|
+
.string()
|
|
43
|
+
.min(1)
|
|
44
|
+
.regex(/^[A-Za-z0-9_-]+$/);
|
|
45
|
+
export const ContentSealSchema = z
|
|
46
|
+
.object({
|
|
47
|
+
ckv: VersionSchema,
|
|
48
|
+
nonce: Base64UrlSchema.length(16),
|
|
49
|
+
ciphertext: Base64UrlSchema.max(Math.ceil(((MAX_CONTENT_PLAINTEXT_BYTES + AEAD_TAG_BYTES) * 4) / 3)),
|
|
50
|
+
})
|
|
51
|
+
.strict();
|
|
52
|
+
const ContentBindingSchema = z
|
|
53
|
+
.object({ workspaceId: IdentifierSchema, messageId: IdentifierSchema, field: IdentifierSchema })
|
|
54
|
+
.strict();
|
|
55
|
+
export async function generateContentKey(ckv) {
|
|
56
|
+
return {
|
|
57
|
+
ckv: VersionSchema.parse(ckv),
|
|
58
|
+
raw: globalThis.crypto.getRandomValues(new Uint8Array(CONTENT_KEY_BYTES)),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export async function importContentKey(ckv, raw) {
|
|
62
|
+
if (raw.byteLength !== CONTENT_KEY_BYTES)
|
|
63
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
64
|
+
const key = await globalThis.crypto.subtle.importKey('raw', Uint8Array.from(raw), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
|
65
|
+
return { ckv: VersionSchema.parse(ckv), key };
|
|
66
|
+
}
|
|
67
|
+
export async function sealContent(key, binding, plaintext) {
|
|
68
|
+
const bytes = new TextEncoder().encode(plaintext);
|
|
69
|
+
if (bytes.byteLength > MAX_CONTENT_PLAINTEXT_BYTES)
|
|
70
|
+
throw new ContentKeyError('CONTENT_TOO_LARGE');
|
|
71
|
+
const nonce = globalThis.crypto.getRandomValues(new Uint8Array(CONTENT_NONCE_BYTES));
|
|
72
|
+
const ciphertext = new Uint8Array(await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: nonce, additionalData: contentAad(binding, key.ckv) }, key.key, bytes));
|
|
73
|
+
return { ckv: key.ckv, nonce: encodeBase64Url(nonce), ciphertext: encodeBase64Url(ciphertext) };
|
|
74
|
+
}
|
|
75
|
+
export async function openContent(key, binding, sealed) {
|
|
76
|
+
const parsed = ContentSealSchema.safeParse(sealed);
|
|
77
|
+
if (!parsed.success)
|
|
78
|
+
throw new ContentKeyError('CONTENT_SEAL_INVALID');
|
|
79
|
+
if (parsed.data.ckv !== key.ckv)
|
|
80
|
+
throw new ContentKeyError('CONTENT_KEY_VERSION_MISMATCH');
|
|
81
|
+
let plaintext;
|
|
82
|
+
try {
|
|
83
|
+
plaintext = await globalThis.crypto.subtle.decrypt({
|
|
84
|
+
name: 'AES-GCM',
|
|
85
|
+
iv: Uint8Array.from(decodeBase64Url(parsed.data.nonce)),
|
|
86
|
+
additionalData: contentAad(binding, key.ckv),
|
|
87
|
+
}, key.key, Uint8Array.from(decodeBase64Url(parsed.data.ciphertext)));
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
throw new ContentKeyError('CONTENT_DECRYPTION_FAILED');
|
|
91
|
+
}
|
|
92
|
+
return new TextDecoder('utf-8', { fatal: true }).decode(plaintext);
|
|
93
|
+
}
|
|
94
|
+
export const ContentKeyWrapBodySchema = z
|
|
95
|
+
.object({
|
|
96
|
+
wrapVersion: z.literal(CONTENT_KEY_WRAP_VERSION),
|
|
97
|
+
workspaceId: IdentifierSchema,
|
|
98
|
+
ckv: VersionSchema,
|
|
99
|
+
keyringRevision: VersionSchema,
|
|
100
|
+
recipientKeyId: z.string().regex(/^(?:spk|dpk)_[a-f0-9]{32}$/),
|
|
101
|
+
recipientEncryptionKeyId: z
|
|
102
|
+
.string()
|
|
103
|
+
.min(8)
|
|
104
|
+
.max(96)
|
|
105
|
+
.regex(/^(?:hpk|rpk)_[a-f0-9]+$/),
|
|
106
|
+
primarySigningKeyId: z.string().regex(/^dpk_[a-f0-9]{32}$/),
|
|
107
|
+
wrappedAt: z.iso.datetime(),
|
|
108
|
+
encapsulatedKey: Base64UrlSchema.length(43),
|
|
109
|
+
ciphertext: Base64UrlSchema.length(64),
|
|
110
|
+
})
|
|
111
|
+
.strict();
|
|
112
|
+
export const ContentKeyWrapSchema = ContentKeyWrapBodySchema.extend({
|
|
113
|
+
signature: Base64UrlSchema.length(86),
|
|
114
|
+
}).strict();
|
|
115
|
+
export function contentKeyRecipients(keyring) {
|
|
116
|
+
return keyring.entries.filter((entry) => entry.revokedAt === undefined && entry.encryptionPublicKey !== undefined);
|
|
117
|
+
}
|
|
118
|
+
export async function wrapContentKey(input) {
|
|
119
|
+
const recipient = contentKeyRecipients(input.keyring).find((entry) => entry.senderKeyId === input.recipientKeyId);
|
|
120
|
+
if (!recipient?.encryptionPublicKey || !recipient.encryptionKeyId) {
|
|
121
|
+
throw new ContentKeyError('CONTENT_KEY_RECIPIENT_INVALID');
|
|
122
|
+
}
|
|
123
|
+
if (input.keyring.primarySigningKeyId !== input.primary.keyId) {
|
|
124
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
125
|
+
}
|
|
126
|
+
if (input.contentKey.raw.byteLength !== CONTENT_KEY_BYTES) {
|
|
127
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
128
|
+
}
|
|
129
|
+
const binding = {
|
|
130
|
+
wrapVersion: CONTENT_KEY_WRAP_VERSION,
|
|
131
|
+
workspaceId: input.keyring.workspaceId,
|
|
132
|
+
ckv: VersionSchema.parse(input.contentKey.ckv),
|
|
133
|
+
keyringRevision: input.keyring.revision,
|
|
134
|
+
recipientKeyId: recipient.senderKeyId,
|
|
135
|
+
recipientEncryptionKeyId: recipient.encryptionKeyId,
|
|
136
|
+
primarySigningKeyId: input.primary.keyId,
|
|
137
|
+
wrappedAt: input.now.toISOString(),
|
|
138
|
+
};
|
|
139
|
+
const sealed = await wrapSuite.seal({
|
|
140
|
+
recipientPublicKey: await wrapSuite.kem.deserializePublicKey(decodeBase64Url(recipient.encryptionPublicKey)),
|
|
141
|
+
info: WRAP_INFO,
|
|
142
|
+
}, Uint8Array.from(input.contentKey.raw), new TextEncoder().encode(canonicalJsonStringify(binding)));
|
|
143
|
+
const body = ContentKeyWrapBodySchema.parse({
|
|
144
|
+
...binding,
|
|
145
|
+
encapsulatedKey: encodeBase64Url(new Uint8Array(sealed.enc)),
|
|
146
|
+
ciphertext: encodeBase64Url(new Uint8Array(sealed.ct)),
|
|
147
|
+
});
|
|
148
|
+
const signature = await signRelayBytes(input.primary.privateKey, wrapSigningInput(body));
|
|
149
|
+
return ContentKeyWrapSchema.parse({ ...body, signature });
|
|
150
|
+
}
|
|
151
|
+
export async function unwrapContentKey(input) {
|
|
152
|
+
const parsed = ContentKeyWrapSchema.safeParse(input.wrap);
|
|
153
|
+
if (!parsed.success)
|
|
154
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
155
|
+
const { signature, ...body } = parsed.data;
|
|
156
|
+
if (body.workspaceId !== input.workspaceId ||
|
|
157
|
+
body.recipientKeyId !== input.recipientKeyId ||
|
|
158
|
+
body.primarySigningKeyId !== input.pinned.keyId ||
|
|
159
|
+
!(await verifyRelayBytes(input.pinned.publicKey, signature, wrapSigningInput(body)))) {
|
|
160
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
161
|
+
}
|
|
162
|
+
const { encapsulatedKey, ciphertext, ...binding } = body;
|
|
163
|
+
let raw;
|
|
164
|
+
try {
|
|
165
|
+
raw = new Uint8Array(await wrapSuite.open({
|
|
166
|
+
recipientKey: await wrapSuite.kem.deserializePrivateKey(decodeBase64Url(input.encryptionPrivateKey)),
|
|
167
|
+
enc: decodeBase64Url(encapsulatedKey),
|
|
168
|
+
info: WRAP_INFO,
|
|
169
|
+
}, decodeBase64Url(ciphertext), new TextEncoder().encode(canonicalJsonStringify(binding))));
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
return await importContentKey(body.ckv, raw);
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
raw.fill(0);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function contentAad(binding, ckv) {
|
|
182
|
+
const parsed = ContentBindingSchema.safeParse(binding);
|
|
183
|
+
if (!parsed.success)
|
|
184
|
+
throw new ContentKeyError('CONTENT_SEAL_INVALID');
|
|
185
|
+
return new TextEncoder().encode(canonicalJsonStringify({ contentVersion: CONTENT_SEAL_VERSION, ckv, ...parsed.data }));
|
|
186
|
+
}
|
|
187
|
+
function wrapSigningInput(body) {
|
|
188
|
+
return new TextEncoder().encode(`${WRAP_SIGNATURE_CONTEXT}\n${canonicalJsonStringify(body)}`);
|
|
189
|
+
}
|
package/dist/device-relay.d.ts
CHANGED
|
@@ -331,6 +331,17 @@ export declare function openAuthenticatedRelayResult(input: OpenAuthenticatedRel
|
|
|
331
331
|
readonly header: AuthenticatedRelayResultHeader;
|
|
332
332
|
readonly plaintext: Uint8Array;
|
|
333
333
|
}>;
|
|
334
|
+
export interface AttestAuthenticatedRelayResultInput {
|
|
335
|
+
readonly envelope: unknown;
|
|
336
|
+
readonly plaintext: Uint8Array;
|
|
337
|
+
readonly deviceId: string;
|
|
338
|
+
readonly workspaceId: string;
|
|
339
|
+
readonly keyring: AuthorizedSenderKeyring;
|
|
340
|
+
readonly now?: Date;
|
|
341
|
+
}
|
|
342
|
+
export declare function attestAuthenticatedRelayResult(input: AttestAuthenticatedRelayResultInput): Promise<AuthenticatedRelayResultHeader>;
|
|
343
|
+
export declare function signRelayBytes(privateKey: CryptoKey, data: Uint8Array): Promise<string>;
|
|
344
|
+
export declare function verifyRelayBytes(publicKey: string, signature: string, data: Uint8Array): Promise<boolean>;
|
|
334
345
|
export declare function relayReplayOutcome(existing: readonly (RelayReplayReservation | undefined)[], reservation: RelayReplayReservation): 'first' | 'duplicate';
|
|
335
346
|
export declare function sha256(value: Uint8Array): Promise<`sha256:${string}`>;
|
|
336
347
|
export declare function encodeBase64Url(value: Uint8Array): string;
|
package/dist/device-relay.js
CHANGED
|
@@ -200,7 +200,7 @@ export async function signAuthorizedSenderKeyring(body, primary) {
|
|
|
200
200
|
if (parsed.primarySigningKeyId !== primary.keyId) {
|
|
201
201
|
throw new AuthenticatedRelayError('RELAY_KEYRING_INVALID');
|
|
202
202
|
}
|
|
203
|
-
const signature = await
|
|
203
|
+
const signature = await signRelayBytes(primary.privateKey, keyringSigningInput(parsed));
|
|
204
204
|
return AuthorizedSenderKeyringSchema.parse({ ...parsed, signature });
|
|
205
205
|
}
|
|
206
206
|
export async function verifyAuthorizedSenderKeyring(input) {
|
|
@@ -216,7 +216,7 @@ export async function verifyAuthorizedSenderKeyring(input) {
|
|
|
216
216
|
throw new AuthenticatedRelayError('RELAY_KEYRING_INVALID');
|
|
217
217
|
}
|
|
218
218
|
const { signature, ...body } = keyring;
|
|
219
|
-
if (!(await
|
|
219
|
+
if (!(await verifyRelayBytes(input.pinned.publicKey, signature, keyringSigningInput(body)))) {
|
|
220
220
|
throw new AuthenticatedRelayError('RELAY_KEYRING_INVALID');
|
|
221
221
|
}
|
|
222
222
|
for (const entry of keyring.entries) {
|
|
@@ -285,7 +285,7 @@ export async function openAuthenticatedRelayCommand(input) {
|
|
|
285
285
|
const now = input.now ?? new Date();
|
|
286
286
|
validateLifetime(header, MAX_AUTHENTICATED_RELAY_COMMAND_LIFETIME_MS, now);
|
|
287
287
|
const sender = authorizedSender(input.keyring, header, now);
|
|
288
|
-
if (!(await
|
|
288
|
+
if (!(await verifyRelayBytes(sender.signingPublicKey, signature, await envelopeSigningInput(header, encapsulatedKey, ciphertext)))) {
|
|
289
289
|
throw new AuthenticatedRelayError('RELAY_SIGNATURE_INVALID');
|
|
290
290
|
}
|
|
291
291
|
const delivery = await input.replay.reserve({
|
|
@@ -339,7 +339,7 @@ export async function openAuthenticatedRelayResult(input) {
|
|
|
339
339
|
if (sender.kind !== 'device' || sender.deviceId !== input.command.targetDeviceId) {
|
|
340
340
|
throw new AuthenticatedRelayError('RELAY_SENDER_UNAUTHENTICATED');
|
|
341
341
|
}
|
|
342
|
-
if (!(await
|
|
342
|
+
if (!(await verifyRelayBytes(sender.signingPublicKey, signature, await envelopeSigningInput(header, encapsulatedKey, ciphertext)))) {
|
|
343
343
|
throw new AuthenticatedRelayError('RELAY_SIGNATURE_INVALID');
|
|
344
344
|
}
|
|
345
345
|
return {
|
|
@@ -347,6 +347,28 @@ export async function openAuthenticatedRelayResult(input) {
|
|
|
347
347
|
plaintext: await openSealed(header, encapsulatedKey, ciphertext, input.returnPrivateKey),
|
|
348
348
|
};
|
|
349
349
|
}
|
|
350
|
+
export async function attestAuthenticatedRelayResult(input) {
|
|
351
|
+
const parsed = AuthenticatedRelayResultEnvelopeSchema.safeParse(input.envelope);
|
|
352
|
+
if (!parsed.success)
|
|
353
|
+
throw new AuthenticatedRelayError('RELAY_ENVELOPE_INVALID');
|
|
354
|
+
const { encapsulatedKey, ciphertext, signature, ...header } = parsed.data;
|
|
355
|
+
if (header.targetDeviceId !== input.deviceId || header.workspaceId !== input.workspaceId) {
|
|
356
|
+
throw new AuthenticatedRelayError('RELAY_TARGET_MISMATCH');
|
|
357
|
+
}
|
|
358
|
+
const now = input.now ?? new Date();
|
|
359
|
+
validateLifetime(header, MAX_AUTHENTICATED_RELAY_RESULT_LIFETIME_MS, now);
|
|
360
|
+
const sender = authorizedSender(input.keyring, header, now);
|
|
361
|
+
if (sender.kind !== 'device' || sender.deviceId !== input.deviceId) {
|
|
362
|
+
throw new AuthenticatedRelayError('RELAY_SENDER_UNAUTHENTICATED');
|
|
363
|
+
}
|
|
364
|
+
if (!(await verifyRelayBytes(sender.signingPublicKey, signature, await envelopeSigningInput(header, encapsulatedKey, ciphertext)))) {
|
|
365
|
+
throw new AuthenticatedRelayError('RELAY_SIGNATURE_INVALID');
|
|
366
|
+
}
|
|
367
|
+
if ((await sha256(input.plaintext)) !== header.contentDigest) {
|
|
368
|
+
throw new AuthenticatedRelayError('RELAY_ENVELOPE_DIGEST_MISMATCH');
|
|
369
|
+
}
|
|
370
|
+
return header;
|
|
371
|
+
}
|
|
350
372
|
function authorizedSender(keyring, header, now) {
|
|
351
373
|
const sender = keyring.entries.find((entry) => entry.senderKeyId === header.senderKeyId);
|
|
352
374
|
if (sender === undefined || sender.principalId !== header.senderPrincipalId) {
|
|
@@ -376,7 +398,7 @@ async function sealAndSign(header, recipientPublicKey, plaintext, signer) {
|
|
|
376
398
|
}
|
|
377
399
|
const encapsulatedKey = encodeBase64Url(new Uint8Array(sealed.enc));
|
|
378
400
|
const ciphertext = encodeBase64Url(new Uint8Array(sealed.ct));
|
|
379
|
-
const signature = await
|
|
401
|
+
const signature = await signRelayBytes(signer.privateKey, await envelopeSigningInput(header, encapsulatedKey, ciphertext));
|
|
380
402
|
return { encapsulatedKey, ciphertext, signature };
|
|
381
403
|
}
|
|
382
404
|
async function openSealed(header, encapsulatedKey, ciphertext, privateKey) {
|
|
@@ -407,10 +429,10 @@ async function envelopeSigningInput(header, encapsulatedKey, ciphertext) {
|
|
|
407
429
|
function keyringSigningInput(body) {
|
|
408
430
|
return new TextEncoder().encode(`${KEYRING_SIGNATURE_CONTEXT}\n${canonicalJsonStringify(body)}`);
|
|
409
431
|
}
|
|
410
|
-
async function
|
|
432
|
+
export async function signRelayBytes(privateKey, data) {
|
|
411
433
|
return encodeBase64Url(new Uint8Array(await globalThis.crypto.subtle.sign('Ed25519', privateKey, Uint8Array.from(data))));
|
|
412
434
|
}
|
|
413
|
-
async function
|
|
435
|
+
export async function verifyRelayBytes(publicKey, signature, data) {
|
|
414
436
|
try {
|
|
415
437
|
const key = await globalThis.crypto.subtle.importKey('raw', Uint8Array.from(decodeBase64Url(publicKey)), { name: 'Ed25519' }, false, ['verify']);
|
|
416
438
|
return await globalThis.crypto.subtle.verify('Ed25519', key, Uint8Array.from(decodeBase64Url(signature)), Uint8Array.from(data));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adea-ai/runtime-gateway-protocol",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"description": "Versioned wire protocol between the Control Plane and external runtimes: schemas, emulator, fixtures, and authentication.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -32,6 +32,10 @@
|
|
|
32
32
|
"node": "./dist/index.js",
|
|
33
33
|
"default": "./dist/index.js"
|
|
34
34
|
},
|
|
35
|
+
"./content-key": {
|
|
36
|
+
"types": "./dist/content-key.d.ts",
|
|
37
|
+
"default": "./dist/content-key.js"
|
|
38
|
+
},
|
|
35
39
|
"./device-relay": {
|
|
36
40
|
"types": "./dist/device-relay.d.ts",
|
|
37
41
|
"default": "./dist/device-relay.js"
|