@adea-ai/runtime-gateway-protocol 1.14.0 → 1.16.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.
@@ -31,13 +31,20 @@ export interface ContentBinding {
31
31
  export interface ContentKey {
32
32
  readonly ckv: number;
33
33
  readonly key: CryptoKey;
34
+ readonly nonceKey?: CryptoKey;
34
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
+ }>;
35
40
  export declare function generateContentKey(ckv: number): Promise<{
36
41
  readonly ckv: number;
37
42
  readonly raw: Uint8Array;
38
43
  }>;
39
44
  export declare function importContentKey(ckv: number, raw: Uint8Array): Promise<ContentKey>;
40
- export declare function sealContent(key: ContentKey, binding: ContentBinding, plaintext: string): Promise<ContentSeal>;
45
+ export declare function sealContent(key: ContentKey, binding: ContentBinding, plaintext: string, options?: {
46
+ readonly deterministic?: boolean;
47
+ }): Promise<ContentSeal>;
41
48
  export declare function openContent(key: ContentKey, binding: ContentBinding, sealed: unknown): Promise<string>;
42
49
  export declare const ContentKeyWrapBodySchema: z.ZodObject<{
43
50
  wrapVersion: z.ZodLiteral<1>;
@@ -52,6 +52,23 @@ export const ContentSealSchema = z
52
52
  const ContentBindingSchema = z
53
53
  .object({ workspaceId: IdentifierSchema, messageId: IdentifierSchema, field: IdentifierSchema })
54
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
+ }
55
72
  export async function generateContentKey(ckv) {
56
73
  return {
57
74
  ckv: VersionSchema.parse(ckv),
@@ -64,12 +81,24 @@ export async function importContentKey(ckv, raw) {
64
81
  const key = await globalThis.crypto.subtle.importKey('raw', Uint8Array.from(raw), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
65
82
  return { ckv: VersionSchema.parse(ckv), key };
66
83
  }
67
- export async function sealContent(key, binding, plaintext) {
84
+ export async function sealContent(key, binding, plaintext, options = {}) {
68
85
  const bytes = new TextEncoder().encode(plaintext);
69
86
  if (bytes.byteLength > MAX_CONTENT_PLAINTEXT_BYTES)
70
87
  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));
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));
73
102
  return { ckv: key.ckv, nonce: encodeBase64Url(nonce), ciphertext: encodeBase64Url(ciphertext) };
74
103
  }
75
104
  export async function openContent(key, binding, sealed) {
@@ -28,6 +28,7 @@ export declare const RelayRejectionCodeSchema: z.ZodEnum<{
28
28
  RELAY_ENVELOPE_REPLAYED: "RELAY_ENVELOPE_REPLAYED";
29
29
  RELAY_ENVELOPE_TOO_LARGE: "RELAY_ENVELOPE_TOO_LARGE";
30
30
  RELAY_KEYRING_INVALID: "RELAY_KEYRING_INVALID";
31
+ RELAY_SENDER_CONTENT_ONLY: "RELAY_SENDER_CONTENT_ONLY";
31
32
  RELAY_SENDER_REVOKED: "RELAY_SENDER_REVOKED";
32
33
  RELAY_SENDER_UNAUTHENTICATED: "RELAY_SENDER_UNAUTHENTICATED";
33
34
  RELAY_SIGNATURE_INVALID: "RELAY_SIGNATURE_INVALID";
@@ -155,6 +156,7 @@ export declare const AuthorizedSenderKeySchema: z.ZodObject<{
155
156
  encryptionPublicKey: z.ZodOptional<z.ZodString>;
156
157
  approvedAt: z.ZodISODateTime;
157
158
  revokedAt: z.ZodOptional<z.ZodISODateTime>;
159
+ scope: z.ZodOptional<z.ZodLiteral<"content">>;
158
160
  }, z.core.$strict>;
159
161
  export declare const AuthorizedSenderKeyringBodySchema: z.ZodObject<{
160
162
  keyringVersion: z.ZodLiteral<1>;
@@ -176,6 +178,7 @@ export declare const AuthorizedSenderKeyringBodySchema: z.ZodObject<{
176
178
  encryptionPublicKey: z.ZodOptional<z.ZodString>;
177
179
  approvedAt: z.ZodISODateTime;
178
180
  revokedAt: z.ZodOptional<z.ZodISODateTime>;
181
+ scope: z.ZodOptional<z.ZodLiteral<"content">>;
179
182
  }, z.core.$strict>>;
180
183
  }, z.core.$strict>;
181
184
  export declare const AuthorizedSenderKeyringSchema: z.ZodObject<{
@@ -198,6 +201,7 @@ export declare const AuthorizedSenderKeyringSchema: z.ZodObject<{
198
201
  encryptionPublicKey: z.ZodOptional<z.ZodString>;
199
202
  approvedAt: z.ZodISODateTime;
200
203
  revokedAt: z.ZodOptional<z.ZodISODateTime>;
204
+ scope: z.ZodOptional<z.ZodLiteral<"content">>;
201
205
  }, z.core.$strict>>;
202
206
  signature: z.ZodString;
203
207
  }, z.core.$strict>;
@@ -62,6 +62,7 @@ export const RelayRejectionCodeSchema = z.enum([
62
62
  'RELAY_ENVELOPE_REPLAYED',
63
63
  'RELAY_SENDER_UNAUTHENTICATED',
64
64
  'RELAY_SENDER_REVOKED',
65
+ 'RELAY_SENDER_CONTENT_ONLY',
65
66
  'RELAY_SIGNATURE_INVALID',
66
67
  'RELAY_TARGET_MISMATCH',
67
68
  'RELAY_KEYRING_INVALID',
@@ -125,12 +126,16 @@ export const AuthorizedSenderKeySchema = z
125
126
  encryptionPublicKey: PublicKeySchema.optional(),
126
127
  approvedAt: z.iso.datetime(),
127
128
  revokedAt: z.iso.datetime().optional(),
129
+ scope: z.literal('content').optional(),
128
130
  })
129
131
  .strict()
130
132
  .superRefine((value, context) => {
131
133
  if ((value.kind === 'device') !== (value.deviceId !== undefined)) {
132
134
  context.addIssue({ code: 'custom', message: 'a device key names its device' });
133
135
  }
136
+ if (value.scope === 'content' && value.kind !== 'session') {
137
+ context.addIssue({ code: 'custom', message: 'only a session key is limited to content' });
138
+ }
134
139
  if ((value.encryptionKeyId === undefined) !== (value.encryptionPublicKey === undefined)) {
135
140
  context.addIssue({ code: 'custom', message: 'encryption key id and key go together' });
136
141
  }
@@ -288,6 +293,8 @@ export async function openAuthenticatedRelayCommand(input) {
288
293
  if (!(await verifyRelayBytes(sender.signingPublicKey, signature, await envelopeSigningInput(header, encapsulatedKey, ciphertext)))) {
289
294
  throw new AuthenticatedRelayError('RELAY_SIGNATURE_INVALID');
290
295
  }
296
+ if (sender.scope === 'content')
297
+ throw new AuthenticatedRelayError('RELAY_SENDER_CONTENT_ONLY');
291
298
  const delivery = await input.replay.reserve({
292
299
  senderKeyId: header.senderKeyId,
293
300
  nonce: header.nonce,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adea-ai/runtime-gateway-protocol",
3
- "version": "1.14.0",
3
+ "version": "1.16.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",
@@ -56,7 +56,7 @@
56
56
  "test": "bun scripts/schema.ts --check && bun test src"
57
57
  },
58
58
  "dependencies": {
59
- "@adea-ai/contracts": "^2.1.0",
59
+ "@adea-ai/contracts": "^2.2.0",
60
60
  "@adea-ai/runtime-sdk": "^1.22.0",
61
61
  "@hpke/core": "1.9.0",
62
62
  "@hpke/dhkem-x25519": "1.8.0",