@adea-ai/runtime-gateway-protocol 1.13.0 → 1.15.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 +92 -0
- package/dist/content-key.js +218 -0
- package/dist/device-relay.d.ts +2 -0
- package/dist/device-relay.js +8 -8
- package/package.json +6 -2
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
readonly nonceKey?: CryptoKey;
|
|
35
|
+
}
|
|
36
|
+
export declare const CONTENT_KEY_SEED_BYTES = 32;
|
|
37
|
+
export declare function deriveContentKey(seed: Uint8Array, workspaceId: string, ckv: number): Promise<ContentKey & {
|
|
38
|
+
readonly raw: Uint8Array;
|
|
39
|
+
}>;
|
|
40
|
+
export declare function generateContentKey(ckv: number): Promise<{
|
|
41
|
+
readonly ckv: number;
|
|
42
|
+
readonly raw: Uint8Array;
|
|
43
|
+
}>;
|
|
44
|
+
export declare function importContentKey(ckv: number, raw: Uint8Array): Promise<ContentKey>;
|
|
45
|
+
export declare function sealContent(key: ContentKey, binding: ContentBinding, plaintext: string, options?: {
|
|
46
|
+
readonly deterministic?: boolean;
|
|
47
|
+
}): Promise<ContentSeal>;
|
|
48
|
+
export declare function openContent(key: ContentKey, binding: ContentBinding, sealed: unknown): Promise<string>;
|
|
49
|
+
export declare const ContentKeyWrapBodySchema: z.ZodObject<{
|
|
50
|
+
wrapVersion: z.ZodLiteral<1>;
|
|
51
|
+
workspaceId: z.ZodString;
|
|
52
|
+
ckv: z.ZodNumber;
|
|
53
|
+
keyringRevision: z.ZodNumber;
|
|
54
|
+
recipientKeyId: z.ZodString;
|
|
55
|
+
recipientEncryptionKeyId: z.ZodString;
|
|
56
|
+
primarySigningKeyId: z.ZodString;
|
|
57
|
+
wrappedAt: z.ZodISODateTime;
|
|
58
|
+
encapsulatedKey: z.ZodString;
|
|
59
|
+
ciphertext: z.ZodString;
|
|
60
|
+
}, z.core.$strict>;
|
|
61
|
+
export declare const ContentKeyWrapSchema: z.ZodObject<{
|
|
62
|
+
wrapVersion: z.ZodLiteral<1>;
|
|
63
|
+
workspaceId: z.ZodString;
|
|
64
|
+
ckv: z.ZodNumber;
|
|
65
|
+
keyringRevision: z.ZodNumber;
|
|
66
|
+
recipientKeyId: z.ZodString;
|
|
67
|
+
recipientEncryptionKeyId: z.ZodString;
|
|
68
|
+
primarySigningKeyId: z.ZodString;
|
|
69
|
+
wrappedAt: z.ZodISODateTime;
|
|
70
|
+
encapsulatedKey: z.ZodString;
|
|
71
|
+
ciphertext: z.ZodString;
|
|
72
|
+
signature: z.ZodString;
|
|
73
|
+
}, z.core.$strict>;
|
|
74
|
+
export type ContentKeyWrap = z.infer<typeof ContentKeyWrapSchema>;
|
|
75
|
+
export declare function contentKeyRecipients(keyring: AuthorizedSenderKeyring): AuthorizedSenderKey[];
|
|
76
|
+
export declare function wrapContentKey(input: {
|
|
77
|
+
readonly contentKey: {
|
|
78
|
+
readonly ckv: number;
|
|
79
|
+
readonly raw: Uint8Array;
|
|
80
|
+
};
|
|
81
|
+
readonly keyring: AuthorizedSenderKeyring;
|
|
82
|
+
readonly recipientKeyId: string;
|
|
83
|
+
readonly primary: RelaySigningKeyPair;
|
|
84
|
+
readonly now: Date;
|
|
85
|
+
}): Promise<ContentKeyWrap>;
|
|
86
|
+
export declare function unwrapContentKey(input: {
|
|
87
|
+
readonly wrap: unknown;
|
|
88
|
+
readonly pinned: PinnedPrimarySigningKey;
|
|
89
|
+
readonly workspaceId: string;
|
|
90
|
+
readonly recipientKeyId: string;
|
|
91
|
+
readonly encryptionPrivateKey: string;
|
|
92
|
+
}): Promise<ContentKey>;
|
|
@@ -0,0 +1,218 @@
|
|
|
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 const CONTENT_KEY_SEED_BYTES = 32;
|
|
56
|
+
export async function deriveContentKey(seed, workspaceId, ckv) {
|
|
57
|
+
if (seed.byteLength !== CONTENT_KEY_SEED_BYTES)
|
|
58
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
59
|
+
const version = VersionSchema.parse(ckv);
|
|
60
|
+
const root = await globalThis.crypto.subtle.importKey('raw', Uint8Array.from(seed), 'HKDF', false, ['deriveBits']);
|
|
61
|
+
const expand = async (info) => new Uint8Array(await globalThis.crypto.subtle.deriveBits({
|
|
62
|
+
name: 'HKDF',
|
|
63
|
+
hash: 'SHA-256',
|
|
64
|
+
salt: new Uint8Array(0),
|
|
65
|
+
info: new TextEncoder().encode(info),
|
|
66
|
+
}, root, CONTENT_KEY_BYTES * 8));
|
|
67
|
+
const info = `adea-content-key:${IdentifierSchema.parse(workspaceId)}:${version}`;
|
|
68
|
+
const raw = await expand(info);
|
|
69
|
+
const nonceKey = await globalThis.crypto.subtle.importKey('raw', await expand(`${info}:nonce`), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
70
|
+
return { ...(await importContentKey(version, raw)), nonceKey, raw };
|
|
71
|
+
}
|
|
72
|
+
export async function generateContentKey(ckv) {
|
|
73
|
+
return {
|
|
74
|
+
ckv: VersionSchema.parse(ckv),
|
|
75
|
+
raw: globalThis.crypto.getRandomValues(new Uint8Array(CONTENT_KEY_BYTES)),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export async function importContentKey(ckv, raw) {
|
|
79
|
+
if (raw.byteLength !== CONTENT_KEY_BYTES)
|
|
80
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
81
|
+
const key = await globalThis.crypto.subtle.importKey('raw', Uint8Array.from(raw), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
|
82
|
+
return { ckv: VersionSchema.parse(ckv), key };
|
|
83
|
+
}
|
|
84
|
+
export async function sealContent(key, binding, plaintext, options = {}) {
|
|
85
|
+
const bytes = new TextEncoder().encode(plaintext);
|
|
86
|
+
if (bytes.byteLength > MAX_CONTENT_PLAINTEXT_BYTES)
|
|
87
|
+
throw new ContentKeyError('CONTENT_TOO_LARGE');
|
|
88
|
+
const aad = contentAad(binding, key.ckv);
|
|
89
|
+
let nonce;
|
|
90
|
+
if (options.deterministic) {
|
|
91
|
+
if (!key.nonceKey)
|
|
92
|
+
throw new ContentKeyError('CONTENT_SEAL_INVALID');
|
|
93
|
+
const input = new Uint8Array(4 + aad.byteLength + bytes.byteLength);
|
|
94
|
+
new DataView(input.buffer).setUint32(0, aad.byteLength);
|
|
95
|
+
input.set(aad, 4);
|
|
96
|
+
input.set(bytes, 4 + aad.byteLength);
|
|
97
|
+
nonce = new Uint8Array(await globalThis.crypto.subtle.sign('HMAC', key.nonceKey, input)).slice(0, CONTENT_NONCE_BYTES);
|
|
98
|
+
}
|
|
99
|
+
else
|
|
100
|
+
nonce = globalThis.crypto.getRandomValues(new Uint8Array(CONTENT_NONCE_BYTES));
|
|
101
|
+
const ciphertext = new Uint8Array(await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: nonce, additionalData: aad }, key.key, bytes));
|
|
102
|
+
return { ckv: key.ckv, nonce: encodeBase64Url(nonce), ciphertext: encodeBase64Url(ciphertext) };
|
|
103
|
+
}
|
|
104
|
+
export async function openContent(key, binding, sealed) {
|
|
105
|
+
const parsed = ContentSealSchema.safeParse(sealed);
|
|
106
|
+
if (!parsed.success)
|
|
107
|
+
throw new ContentKeyError('CONTENT_SEAL_INVALID');
|
|
108
|
+
if (parsed.data.ckv !== key.ckv)
|
|
109
|
+
throw new ContentKeyError('CONTENT_KEY_VERSION_MISMATCH');
|
|
110
|
+
let plaintext;
|
|
111
|
+
try {
|
|
112
|
+
plaintext = await globalThis.crypto.subtle.decrypt({
|
|
113
|
+
name: 'AES-GCM',
|
|
114
|
+
iv: Uint8Array.from(decodeBase64Url(parsed.data.nonce)),
|
|
115
|
+
additionalData: contentAad(binding, key.ckv),
|
|
116
|
+
}, key.key, Uint8Array.from(decodeBase64Url(parsed.data.ciphertext)));
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
throw new ContentKeyError('CONTENT_DECRYPTION_FAILED');
|
|
120
|
+
}
|
|
121
|
+
return new TextDecoder('utf-8', { fatal: true }).decode(plaintext);
|
|
122
|
+
}
|
|
123
|
+
export const ContentKeyWrapBodySchema = z
|
|
124
|
+
.object({
|
|
125
|
+
wrapVersion: z.literal(CONTENT_KEY_WRAP_VERSION),
|
|
126
|
+
workspaceId: IdentifierSchema,
|
|
127
|
+
ckv: VersionSchema,
|
|
128
|
+
keyringRevision: VersionSchema,
|
|
129
|
+
recipientKeyId: z.string().regex(/^(?:spk|dpk)_[a-f0-9]{32}$/),
|
|
130
|
+
recipientEncryptionKeyId: z
|
|
131
|
+
.string()
|
|
132
|
+
.min(8)
|
|
133
|
+
.max(96)
|
|
134
|
+
.regex(/^(?:hpk|rpk)_[a-f0-9]+$/),
|
|
135
|
+
primarySigningKeyId: z.string().regex(/^dpk_[a-f0-9]{32}$/),
|
|
136
|
+
wrappedAt: z.iso.datetime(),
|
|
137
|
+
encapsulatedKey: Base64UrlSchema.length(43),
|
|
138
|
+
ciphertext: Base64UrlSchema.length(64),
|
|
139
|
+
})
|
|
140
|
+
.strict();
|
|
141
|
+
export const ContentKeyWrapSchema = ContentKeyWrapBodySchema.extend({
|
|
142
|
+
signature: Base64UrlSchema.length(86),
|
|
143
|
+
}).strict();
|
|
144
|
+
export function contentKeyRecipients(keyring) {
|
|
145
|
+
return keyring.entries.filter((entry) => entry.revokedAt === undefined && entry.encryptionPublicKey !== undefined);
|
|
146
|
+
}
|
|
147
|
+
export async function wrapContentKey(input) {
|
|
148
|
+
const recipient = contentKeyRecipients(input.keyring).find((entry) => entry.senderKeyId === input.recipientKeyId);
|
|
149
|
+
if (!recipient?.encryptionPublicKey || !recipient.encryptionKeyId) {
|
|
150
|
+
throw new ContentKeyError('CONTENT_KEY_RECIPIENT_INVALID');
|
|
151
|
+
}
|
|
152
|
+
if (input.keyring.primarySigningKeyId !== input.primary.keyId) {
|
|
153
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
154
|
+
}
|
|
155
|
+
if (input.contentKey.raw.byteLength !== CONTENT_KEY_BYTES) {
|
|
156
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
157
|
+
}
|
|
158
|
+
const binding = {
|
|
159
|
+
wrapVersion: CONTENT_KEY_WRAP_VERSION,
|
|
160
|
+
workspaceId: input.keyring.workspaceId,
|
|
161
|
+
ckv: VersionSchema.parse(input.contentKey.ckv),
|
|
162
|
+
keyringRevision: input.keyring.revision,
|
|
163
|
+
recipientKeyId: recipient.senderKeyId,
|
|
164
|
+
recipientEncryptionKeyId: recipient.encryptionKeyId,
|
|
165
|
+
primarySigningKeyId: input.primary.keyId,
|
|
166
|
+
wrappedAt: input.now.toISOString(),
|
|
167
|
+
};
|
|
168
|
+
const sealed = await wrapSuite.seal({
|
|
169
|
+
recipientPublicKey: await wrapSuite.kem.deserializePublicKey(decodeBase64Url(recipient.encryptionPublicKey)),
|
|
170
|
+
info: WRAP_INFO,
|
|
171
|
+
}, Uint8Array.from(input.contentKey.raw), new TextEncoder().encode(canonicalJsonStringify(binding)));
|
|
172
|
+
const body = ContentKeyWrapBodySchema.parse({
|
|
173
|
+
...binding,
|
|
174
|
+
encapsulatedKey: encodeBase64Url(new Uint8Array(sealed.enc)),
|
|
175
|
+
ciphertext: encodeBase64Url(new Uint8Array(sealed.ct)),
|
|
176
|
+
});
|
|
177
|
+
const signature = await signRelayBytes(input.primary.privateKey, wrapSigningInput(body));
|
|
178
|
+
return ContentKeyWrapSchema.parse({ ...body, signature });
|
|
179
|
+
}
|
|
180
|
+
export async function unwrapContentKey(input) {
|
|
181
|
+
const parsed = ContentKeyWrapSchema.safeParse(input.wrap);
|
|
182
|
+
if (!parsed.success)
|
|
183
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
184
|
+
const { signature, ...body } = parsed.data;
|
|
185
|
+
if (body.workspaceId !== input.workspaceId ||
|
|
186
|
+
body.recipientKeyId !== input.recipientKeyId ||
|
|
187
|
+
body.primarySigningKeyId !== input.pinned.keyId ||
|
|
188
|
+
!(await verifyRelayBytes(input.pinned.publicKey, signature, wrapSigningInput(body)))) {
|
|
189
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
190
|
+
}
|
|
191
|
+
const { encapsulatedKey, ciphertext, ...binding } = body;
|
|
192
|
+
let raw;
|
|
193
|
+
try {
|
|
194
|
+
raw = new Uint8Array(await wrapSuite.open({
|
|
195
|
+
recipientKey: await wrapSuite.kem.deserializePrivateKey(decodeBase64Url(input.encryptionPrivateKey)),
|
|
196
|
+
enc: decodeBase64Url(encapsulatedKey),
|
|
197
|
+
info: WRAP_INFO,
|
|
198
|
+
}, decodeBase64Url(ciphertext), new TextEncoder().encode(canonicalJsonStringify(binding))));
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
throw new ContentKeyError('CONTENT_KEY_WRAP_INVALID');
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
return await importContentKey(body.ckv, raw);
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
raw.fill(0);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function contentAad(binding, ckv) {
|
|
211
|
+
const parsed = ContentBindingSchema.safeParse(binding);
|
|
212
|
+
if (!parsed.success)
|
|
213
|
+
throw new ContentKeyError('CONTENT_SEAL_INVALID');
|
|
214
|
+
return new TextEncoder().encode(canonicalJsonStringify({ contentVersion: CONTENT_SEAL_VERSION, ckv, ...parsed.data }));
|
|
215
|
+
}
|
|
216
|
+
function wrapSigningInput(body) {
|
|
217
|
+
return new TextEncoder().encode(`${WRAP_SIGNATURE_CONTEXT}\n${canonicalJsonStringify(body)}`);
|
|
218
|
+
}
|
package/dist/device-relay.d.ts
CHANGED
|
@@ -340,6 +340,8 @@ export interface AttestAuthenticatedRelayResultInput {
|
|
|
340
340
|
readonly now?: Date;
|
|
341
341
|
}
|
|
342
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>;
|
|
343
345
|
export declare function relayReplayOutcome(existing: readonly (RelayReplayReservation | undefined)[], reservation: RelayReplayReservation): 'first' | 'duplicate';
|
|
344
346
|
export declare function sha256(value: Uint8Array): Promise<`sha256:${string}`>;
|
|
345
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 {
|
|
@@ -361,7 +361,7 @@ export async function attestAuthenticatedRelayResult(input) {
|
|
|
361
361
|
if (sender.kind !== 'device' || sender.deviceId !== input.deviceId) {
|
|
362
362
|
throw new AuthenticatedRelayError('RELAY_SENDER_UNAUTHENTICATED');
|
|
363
363
|
}
|
|
364
|
-
if (!(await
|
|
364
|
+
if (!(await verifyRelayBytes(sender.signingPublicKey, signature, await envelopeSigningInput(header, encapsulatedKey, ciphertext)))) {
|
|
365
365
|
throw new AuthenticatedRelayError('RELAY_SIGNATURE_INVALID');
|
|
366
366
|
}
|
|
367
367
|
if ((await sha256(input.plaintext)) !== header.contentDigest) {
|
|
@@ -398,7 +398,7 @@ async function sealAndSign(header, recipientPublicKey, plaintext, signer) {
|
|
|
398
398
|
}
|
|
399
399
|
const encapsulatedKey = encodeBase64Url(new Uint8Array(sealed.enc));
|
|
400
400
|
const ciphertext = encodeBase64Url(new Uint8Array(sealed.ct));
|
|
401
|
-
const signature = await
|
|
401
|
+
const signature = await signRelayBytes(signer.privateKey, await envelopeSigningInput(header, encapsulatedKey, ciphertext));
|
|
402
402
|
return { encapsulatedKey, ciphertext, signature };
|
|
403
403
|
}
|
|
404
404
|
async function openSealed(header, encapsulatedKey, ciphertext, privateKey) {
|
|
@@ -429,10 +429,10 @@ async function envelopeSigningInput(header, encapsulatedKey, ciphertext) {
|
|
|
429
429
|
function keyringSigningInput(body) {
|
|
430
430
|
return new TextEncoder().encode(`${KEYRING_SIGNATURE_CONTEXT}\n${canonicalJsonStringify(body)}`);
|
|
431
431
|
}
|
|
432
|
-
async function
|
|
432
|
+
export async function signRelayBytes(privateKey, data) {
|
|
433
433
|
return encodeBase64Url(new Uint8Array(await globalThis.crypto.subtle.sign('Ed25519', privateKey, Uint8Array.from(data))));
|
|
434
434
|
}
|
|
435
|
-
async function
|
|
435
|
+
export async function verifyRelayBytes(publicKey, signature, data) {
|
|
436
436
|
try {
|
|
437
437
|
const key = await globalThis.crypto.subtle.importKey('raw', Uint8Array.from(decodeBase64Url(publicKey)), { name: 'Ed25519' }, false, ['verify']);
|
|
438
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.15.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"
|
|
@@ -52,7 +56,7 @@
|
|
|
52
56
|
"test": "bun scripts/schema.ts --check && bun test src"
|
|
53
57
|
},
|
|
54
58
|
"dependencies": {
|
|
55
|
-
"@adea-ai/contracts": "^2.
|
|
59
|
+
"@adea-ai/contracts": "^2.2.0",
|
|
56
60
|
"@adea-ai/runtime-sdk": "^1.22.0",
|
|
57
61
|
"@hpke/core": "1.9.0",
|
|
58
62
|
"@hpke/dhkem-x25519": "1.8.0",
|