@shieldfive/crypto 1.0.0-rc.5 → 1.0.0-rc.6
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/CHANGELOG.md +15 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/vault/index.d.ts +27 -5
- package/dist/vault/index.d.ts.map +1 -1
- package/dist/vault/index.js +170 -95
- package/dist/vault/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/vault/index.ts +429 -250
package/src/vault/index.ts
CHANGED
|
@@ -27,55 +27,64 @@
|
|
|
27
27
|
* HKDF-SHA-256, SHA-256 and libsodium's Argon2id13.
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
|
-
import { sha256 } from
|
|
30
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
31
31
|
|
|
32
32
|
import {
|
|
33
33
|
base64ToBytes,
|
|
34
34
|
bytesToBase64,
|
|
35
35
|
bytesToHex,
|
|
36
36
|
concatBytes,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
import {
|
|
40
|
-
import {
|
|
41
|
-
import {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
hexToBytes,
|
|
38
|
+
} from "../internal/encoding.js";
|
|
39
|
+
import { parseHeader } from "../format/header.js";
|
|
40
|
+
import { hmacSha256 } from "../internal/hmac.js";
|
|
41
|
+
import { hkdfSha256 } from "../internal/hkdf.js";
|
|
42
|
+
import { getSubtle, randomBytes } from "../internal/runtime.js";
|
|
43
|
+
import { getSodium } from "../internal/sodium.js";
|
|
44
|
+
import { HKDF_INFO } from "../internal/types.js";
|
|
45
|
+
|
|
46
|
+
const KEY_BYTES = 32;
|
|
47
|
+
// The upload-proof frame (see buildUploadProofV3): a version byte, the cipher
|
|
48
|
+
// version, then the MAC. The 4-byte big-endian length prefix belongs to the
|
|
49
|
+
// container format, not to the proof.
|
|
50
|
+
const PROOF_VERSION_V3 = 3;
|
|
51
|
+
const CIPHER_VERSION_PQ_HYBRID = 3;
|
|
52
|
+
const LENGTH_PREFIX_BYTES = 4;
|
|
53
|
+
const IV_BYTES = 12;
|
|
54
|
+
const TAG_BYTES = 16;
|
|
55
|
+
const NAME_SALT_BYTES = 16;
|
|
56
|
+
const TEXT_ENCODER = new TextEncoder();
|
|
57
|
+
const TEXT_DECODER = new TextDecoder("utf-8", { fatal: true });
|
|
49
58
|
|
|
50
59
|
/** Raised for any envelope or wrap that does not open. Never carries key bytes. */
|
|
51
60
|
export class VaultCryptoError extends Error {
|
|
52
61
|
readonly code:
|
|
53
|
-
|
|
|
54
|
-
|
|
|
55
|
-
|
|
|
56
|
-
|
|
|
57
|
-
|
|
|
58
|
-
constructor(code: VaultCryptoError[
|
|
59
|
-
super(message)
|
|
60
|
-
this.name =
|
|
61
|
-
this.code = code
|
|
62
|
+
| "unwrap_failed"
|
|
63
|
+
| "name_decrypt_failed"
|
|
64
|
+
| "unsupported_envelope"
|
|
65
|
+
| "invalid_input"
|
|
66
|
+
| "invalid_connection_string";
|
|
67
|
+
constructor(code: VaultCryptoError["code"], message: string) {
|
|
68
|
+
super(message);
|
|
69
|
+
this.name = "VaultCryptoError";
|
|
70
|
+
this.code = code;
|
|
62
71
|
}
|
|
63
72
|
}
|
|
64
73
|
|
|
65
74
|
function assertKey(key: Uint8Array, what: string): void {
|
|
66
75
|
if (!(key instanceof Uint8Array) || key.length !== KEY_BYTES) {
|
|
67
|
-
throw new VaultCryptoError(
|
|
76
|
+
throw new VaultCryptoError("invalid_input", `${what} must be 32 bytes`);
|
|
68
77
|
}
|
|
69
78
|
}
|
|
70
79
|
|
|
71
80
|
async function importAesKey(key: Uint8Array): Promise<CryptoKey> {
|
|
72
81
|
return getSubtle().importKey(
|
|
73
|
-
|
|
82
|
+
"raw",
|
|
74
83
|
key as Uint8Array<ArrayBuffer>,
|
|
75
|
-
{ name:
|
|
84
|
+
{ name: "AES-GCM" },
|
|
76
85
|
false,
|
|
77
|
-
[
|
|
78
|
-
)
|
|
86
|
+
["encrypt", "decrypt"],
|
|
87
|
+
);
|
|
79
88
|
}
|
|
80
89
|
|
|
81
90
|
async function aesGcmSeal(
|
|
@@ -84,14 +93,17 @@ async function aesGcmSeal(
|
|
|
84
93
|
plaintext: Uint8Array,
|
|
85
94
|
aad?: Uint8Array,
|
|
86
95
|
): Promise<Uint8Array> {
|
|
87
|
-
const params: AesGcmParams = {
|
|
88
|
-
|
|
96
|
+
const params: AesGcmParams = {
|
|
97
|
+
name: "AES-GCM",
|
|
98
|
+
iv: iv as Uint8Array<ArrayBuffer>,
|
|
99
|
+
};
|
|
100
|
+
if (aad) params.additionalData = aad as Uint8Array<ArrayBuffer>;
|
|
89
101
|
const out = await getSubtle().encrypt(
|
|
90
102
|
params,
|
|
91
103
|
await importAesKey(key),
|
|
92
104
|
plaintext as Uint8Array<ArrayBuffer>,
|
|
93
|
-
)
|
|
94
|
-
return new Uint8Array(out)
|
|
105
|
+
);
|
|
106
|
+
return new Uint8Array(out);
|
|
95
107
|
}
|
|
96
108
|
|
|
97
109
|
async function aesGcmOpen(
|
|
@@ -100,17 +112,20 @@ async function aesGcmOpen(
|
|
|
100
112
|
ciphertext: Uint8Array,
|
|
101
113
|
aad?: Uint8Array,
|
|
102
114
|
): Promise<Uint8Array | null> {
|
|
103
|
-
const params: AesGcmParams = {
|
|
104
|
-
|
|
115
|
+
const params: AesGcmParams = {
|
|
116
|
+
name: "AES-GCM",
|
|
117
|
+
iv: iv as Uint8Array<ArrayBuffer>,
|
|
118
|
+
};
|
|
119
|
+
if (aad) params.additionalData = aad as Uint8Array<ArrayBuffer>;
|
|
105
120
|
try {
|
|
106
121
|
const out = await getSubtle().decrypt(
|
|
107
122
|
params,
|
|
108
123
|
await importAesKey(key),
|
|
109
124
|
ciphertext as Uint8Array<ArrayBuffer>,
|
|
110
|
-
)
|
|
111
|
-
return new Uint8Array(out)
|
|
125
|
+
);
|
|
126
|
+
return new Uint8Array(out);
|
|
112
127
|
} catch {
|
|
113
|
-
return null
|
|
128
|
+
return null;
|
|
114
129
|
}
|
|
115
130
|
}
|
|
116
131
|
|
|
@@ -120,8 +135,8 @@ async function aesGcmOpen(
|
|
|
120
135
|
|
|
121
136
|
/** A wrapped key as the database stores it: base64 ciphertext+tag and base64 IV. */
|
|
122
137
|
export interface WrappedKey {
|
|
123
|
-
wrapped: string
|
|
124
|
-
iv: string
|
|
138
|
+
wrapped: string;
|
|
139
|
+
iv: string;
|
|
125
140
|
}
|
|
126
141
|
|
|
127
142
|
/** Wrap a 32-byte key under a 32-byte key (the fk/csk/pqk chain format). */
|
|
@@ -129,11 +144,11 @@ export async function wrapChainKey(
|
|
|
129
144
|
wrappingKey: Uint8Array,
|
|
130
145
|
key: Uint8Array,
|
|
131
146
|
): Promise<WrappedKey> {
|
|
132
|
-
assertKey(wrappingKey,
|
|
133
|
-
assertKey(key,
|
|
134
|
-
const iv = randomBytes(IV_BYTES)
|
|
135
|
-
const ct = await aesGcmSeal(wrappingKey, iv, key)
|
|
136
|
-
return { wrapped: bytesToBase64(ct), iv: bytesToBase64(iv) }
|
|
147
|
+
assertKey(wrappingKey, "wrappingKey");
|
|
148
|
+
assertKey(key, "key");
|
|
149
|
+
const iv = randomBytes(IV_BYTES);
|
|
150
|
+
const ct = await aesGcmSeal(wrappingKey, iv, key);
|
|
151
|
+
return { wrapped: bytesToBase64(ct), iv: bytesToBase64(iv) };
|
|
137
152
|
}
|
|
138
153
|
|
|
139
154
|
/** Open one hop of the key chain. Throws `unwrap_failed` on a wrong key or tampering. */
|
|
@@ -141,57 +156,65 @@ export async function unwrapChainKey(
|
|
|
141
156
|
wrappingKey: Uint8Array,
|
|
142
157
|
wrapped: WrappedKey,
|
|
143
158
|
): Promise<Uint8Array> {
|
|
144
|
-
assertKey(wrappingKey,
|
|
145
|
-
const iv = base64ToBytes(wrapped.iv)
|
|
146
|
-
const ct = base64ToBytes(wrapped.wrapped)
|
|
159
|
+
assertKey(wrappingKey, "wrappingKey");
|
|
160
|
+
const iv = base64ToBytes(wrapped.iv);
|
|
161
|
+
const ct = base64ToBytes(wrapped.wrapped);
|
|
147
162
|
if (iv.length !== IV_BYTES || ct.length !== KEY_BYTES + TAG_BYTES) {
|
|
148
|
-
throw new VaultCryptoError(
|
|
163
|
+
throw new VaultCryptoError(
|
|
164
|
+
"unwrap_failed",
|
|
165
|
+
"wrapped key has the wrong shape",
|
|
166
|
+
);
|
|
149
167
|
}
|
|
150
|
-
const key = await aesGcmOpen(wrappingKey, iv, ct)
|
|
151
|
-
if (!key)
|
|
152
|
-
|
|
168
|
+
const key = await aesGcmOpen(wrappingKey, iv, ct);
|
|
169
|
+
if (!key)
|
|
170
|
+
throw new VaultCryptoError("unwrap_failed", "wrapped key did not open");
|
|
171
|
+
return key;
|
|
153
172
|
}
|
|
154
173
|
|
|
155
174
|
// ──────────────────────────────────────────────────────────────────────
|
|
156
175
|
// Name envelopes (v4, v6)
|
|
157
176
|
// ──────────────────────────────────────────────────────────────────────
|
|
158
177
|
|
|
159
|
-
export type NameKdfLevel =
|
|
178
|
+
export type NameKdfLevel = "interactive" | "moderate";
|
|
160
179
|
|
|
161
180
|
export interface NameEnvelope {
|
|
162
|
-
v: 4 | 6
|
|
163
|
-
ct: string
|
|
164
|
-
iv: string
|
|
165
|
-
tag: string
|
|
166
|
-
salt: string
|
|
167
|
-
kdf?: NameKdfLevel
|
|
181
|
+
v: 4 | 6;
|
|
182
|
+
ct: string;
|
|
183
|
+
iv: string;
|
|
184
|
+
tag: string;
|
|
185
|
+
salt: string;
|
|
186
|
+
kdf?: NameKdfLevel;
|
|
168
187
|
}
|
|
169
188
|
|
|
170
189
|
/** Parse a stored name. Returns null for anything that is not a v4/v6 envelope. */
|
|
171
|
-
export function parseNameEnvelope(
|
|
172
|
-
|
|
173
|
-
|
|
190
|
+
export function parseNameEnvelope(
|
|
191
|
+
raw: string | null | undefined,
|
|
192
|
+
): NameEnvelope | null {
|
|
193
|
+
if (typeof raw !== "string" || raw.length === 0 || raw.length > 8192)
|
|
194
|
+
return null;
|
|
195
|
+
let value: unknown;
|
|
174
196
|
try {
|
|
175
|
-
value = JSON.parse(raw)
|
|
197
|
+
value = JSON.parse(raw);
|
|
176
198
|
} catch {
|
|
177
|
-
return null
|
|
199
|
+
return null;
|
|
178
200
|
}
|
|
179
|
-
if (!value || typeof value !==
|
|
180
|
-
const p = value as Record<string, unknown
|
|
181
|
-
if (p.v !== 4 && p.v !== 6) return null
|
|
182
|
-
for (const f of [
|
|
183
|
-
if (typeof p[f] !==
|
|
201
|
+
if (!value || typeof value !== "object") return null;
|
|
202
|
+
const p = value as Record<string, unknown>;
|
|
203
|
+
if (p.v !== 4 && p.v !== 6) return null;
|
|
204
|
+
for (const f of ["ct", "iv", "tag", "salt"] as const) {
|
|
205
|
+
if (typeof p[f] !== "string") return null;
|
|
184
206
|
}
|
|
185
|
-
if (p.kdf !== undefined && p.kdf !==
|
|
207
|
+
if (p.kdf !== undefined && p.kdf !== "interactive" && p.kdf !== "moderate")
|
|
208
|
+
return null;
|
|
186
209
|
const env: NameEnvelope = {
|
|
187
210
|
v: p.v,
|
|
188
211
|
ct: p.ct as string,
|
|
189
212
|
iv: p.iv as string,
|
|
190
213
|
tag: p.tag as string,
|
|
191
214
|
salt: p.salt as string,
|
|
192
|
-
}
|
|
193
|
-
if (p.kdf !== undefined) env.kdf = p.kdf as NameKdfLevel
|
|
194
|
-
return env
|
|
215
|
+
};
|
|
216
|
+
if (p.kdf !== undefined) env.kdf = p.kdf as NameKdfLevel;
|
|
217
|
+
return env;
|
|
195
218
|
}
|
|
196
219
|
|
|
197
220
|
/**
|
|
@@ -205,19 +228,19 @@ export async function deriveNameKey(
|
|
|
205
228
|
salt: Uint8Array,
|
|
206
229
|
level: NameKdfLevel,
|
|
207
230
|
): Promise<Uint8Array> {
|
|
208
|
-
assertKey(folderKey,
|
|
231
|
+
assertKey(folderKey, "folderKey");
|
|
209
232
|
if (salt.length !== NAME_SALT_BYTES) {
|
|
210
|
-
throw new VaultCryptoError(
|
|
233
|
+
throw new VaultCryptoError("invalid_input", "name salt must be 16 bytes");
|
|
211
234
|
}
|
|
212
|
-
const sodium = await getSodium()
|
|
235
|
+
const sodium = await getSodium();
|
|
213
236
|
const ops =
|
|
214
|
-
level ===
|
|
237
|
+
level === "interactive"
|
|
215
238
|
? sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE
|
|
216
|
-
: sodium.crypto_pwhash_OPSLIMIT_MODERATE
|
|
239
|
+
: sodium.crypto_pwhash_OPSLIMIT_MODERATE;
|
|
217
240
|
const mem =
|
|
218
|
-
level ===
|
|
241
|
+
level === "interactive"
|
|
219
242
|
? sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE
|
|
220
|
-
: sodium.crypto_pwhash_MEMLIMIT_MODERATE
|
|
243
|
+
: sodium.crypto_pwhash_MEMLIMIT_MODERATE;
|
|
221
244
|
return new Uint8Array(
|
|
222
245
|
sodium.crypto_pwhash(
|
|
223
246
|
KEY_BYTES,
|
|
@@ -227,7 +250,7 @@ export async function deriveNameKey(
|
|
|
227
250
|
mem,
|
|
228
251
|
sodium.crypto_pwhash_ALG_ARGON2ID13,
|
|
229
252
|
),
|
|
230
|
-
)
|
|
253
|
+
);
|
|
231
254
|
}
|
|
232
255
|
|
|
233
256
|
/**
|
|
@@ -237,33 +260,40 @@ export async function deriveNameKey(
|
|
|
237
260
|
* web app's own fallback order for a device that cannot know the level.
|
|
238
261
|
*/
|
|
239
262
|
export async function decryptName(options: {
|
|
240
|
-
envelope: NameEnvelope
|
|
241
|
-
folderKey: Uint8Array
|
|
242
|
-
rowId?: string
|
|
263
|
+
envelope: NameEnvelope;
|
|
264
|
+
folderKey: Uint8Array;
|
|
265
|
+
rowId?: string;
|
|
243
266
|
}): Promise<string> {
|
|
244
|
-
const { envelope, folderKey, rowId } = options
|
|
267
|
+
const { envelope, folderKey, rowId } = options;
|
|
245
268
|
if (envelope.v === 6 && !rowId) {
|
|
246
|
-
throw new VaultCryptoError(
|
|
269
|
+
throw new VaultCryptoError("invalid_input", "v6 names need the row id");
|
|
247
270
|
}
|
|
248
|
-
const iv = base64ToBytes(envelope.iv)
|
|
249
|
-
const salt = base64ToBytes(envelope.salt)
|
|
250
|
-
const sealed = concatBytes([
|
|
251
|
-
|
|
271
|
+
const iv = base64ToBytes(envelope.iv);
|
|
272
|
+
const salt = base64ToBytes(envelope.salt);
|
|
273
|
+
const sealed = concatBytes([
|
|
274
|
+
base64ToBytes(envelope.ct),
|
|
275
|
+
base64ToBytes(envelope.tag),
|
|
276
|
+
]);
|
|
277
|
+
const aad =
|
|
278
|
+
envelope.v === 6 ? TEXT_ENCODER.encode(rowId as string) : undefined;
|
|
252
279
|
const levels: NameKdfLevel[] = envelope.kdf
|
|
253
280
|
? [envelope.kdf]
|
|
254
|
-
: [
|
|
281
|
+
: ["interactive", "moderate"];
|
|
255
282
|
for (const level of levels) {
|
|
256
|
-
const key = await deriveNameKey(folderKey, salt, level)
|
|
257
|
-
const pt = await aesGcmOpen(key, iv, sealed, aad)
|
|
283
|
+
const key = await deriveNameKey(folderKey, salt, level);
|
|
284
|
+
const pt = await aesGcmOpen(key, iv, sealed, aad);
|
|
258
285
|
if (pt) {
|
|
259
286
|
try {
|
|
260
|
-
return TEXT_DECODER.decode(pt)
|
|
287
|
+
return TEXT_DECODER.decode(pt);
|
|
261
288
|
} catch {
|
|
262
|
-
break
|
|
289
|
+
break;
|
|
263
290
|
}
|
|
264
291
|
}
|
|
265
292
|
}
|
|
266
|
-
throw new VaultCryptoError(
|
|
293
|
+
throw new VaultCryptoError(
|
|
294
|
+
"name_decrypt_failed",
|
|
295
|
+
"name envelope did not open",
|
|
296
|
+
);
|
|
267
297
|
}
|
|
268
298
|
|
|
269
299
|
/**
|
|
@@ -271,26 +301,33 @@ export async function decryptName(options: {
|
|
|
271
301
|
* for objects whose parent key the caller does not hold.
|
|
272
302
|
*/
|
|
273
303
|
export async function decryptNameWithKey(options: {
|
|
274
|
-
envelope: NameEnvelope
|
|
275
|
-
nameKey: Uint8Array
|
|
276
|
-
rowId?: string
|
|
304
|
+
envelope: NameEnvelope;
|
|
305
|
+
nameKey: Uint8Array;
|
|
306
|
+
rowId?: string;
|
|
277
307
|
}): Promise<string> {
|
|
278
|
-
const { envelope, nameKey, rowId } = options
|
|
279
|
-
assertKey(nameKey,
|
|
308
|
+
const { envelope, nameKey, rowId } = options;
|
|
309
|
+
assertKey(nameKey, "nameKey");
|
|
280
310
|
if (envelope.v === 6 && !rowId) {
|
|
281
|
-
throw new VaultCryptoError(
|
|
311
|
+
throw new VaultCryptoError("invalid_input", "v6 names need the row id");
|
|
282
312
|
}
|
|
283
|
-
const sealed = concatBytes([
|
|
284
|
-
|
|
285
|
-
|
|
313
|
+
const sealed = concatBytes([
|
|
314
|
+
base64ToBytes(envelope.ct),
|
|
315
|
+
base64ToBytes(envelope.tag),
|
|
316
|
+
]);
|
|
317
|
+
const aad =
|
|
318
|
+
envelope.v === 6 ? TEXT_ENCODER.encode(rowId as string) : undefined;
|
|
319
|
+
const pt = await aesGcmOpen(nameKey, base64ToBytes(envelope.iv), sealed, aad);
|
|
286
320
|
if (pt) {
|
|
287
321
|
try {
|
|
288
|
-
return TEXT_DECODER.decode(pt)
|
|
322
|
+
return TEXT_DECODER.decode(pt);
|
|
289
323
|
} catch {
|
|
290
324
|
// fall through
|
|
291
325
|
}
|
|
292
326
|
}
|
|
293
|
-
throw new VaultCryptoError(
|
|
327
|
+
throw new VaultCryptoError(
|
|
328
|
+
"name_decrypt_failed",
|
|
329
|
+
"name envelope did not open",
|
|
330
|
+
);
|
|
294
331
|
}
|
|
295
332
|
|
|
296
333
|
/**
|
|
@@ -299,57 +336,78 @@ export async function decryptNameWithKey(options: {
|
|
|
299
336
|
* it, the same fallback `decryptName` uses, so the wrapped key always works.
|
|
300
337
|
*/
|
|
301
338
|
export async function deriveNameKeyForEnvelope(options: {
|
|
302
|
-
envelope: NameEnvelope
|
|
303
|
-
parentKey: Uint8Array
|
|
304
|
-
rowId?: string
|
|
339
|
+
envelope: NameEnvelope;
|
|
340
|
+
parentKey: Uint8Array;
|
|
341
|
+
rowId?: string;
|
|
305
342
|
}): Promise<Uint8Array> {
|
|
306
|
-
const { envelope, parentKey, rowId } = options
|
|
307
|
-
const salt = base64ToBytes(envelope.salt)
|
|
308
|
-
const levels: NameKdfLevel[] = envelope.kdf
|
|
343
|
+
const { envelope, parentKey, rowId } = options;
|
|
344
|
+
const salt = base64ToBytes(envelope.salt);
|
|
345
|
+
const levels: NameKdfLevel[] = envelope.kdf
|
|
346
|
+
? [envelope.kdf]
|
|
347
|
+
: ["interactive", "moderate"];
|
|
309
348
|
for (const level of levels) {
|
|
310
|
-
const nameKey = await deriveNameKey(parentKey, salt, level)
|
|
349
|
+
const nameKey = await deriveNameKey(parentKey, salt, level);
|
|
311
350
|
try {
|
|
312
|
-
await decryptNameWithKey(
|
|
313
|
-
|
|
351
|
+
await decryptNameWithKey(
|
|
352
|
+
rowId === undefined
|
|
353
|
+
? { envelope, nameKey }
|
|
354
|
+
: { envelope, nameKey, rowId },
|
|
355
|
+
);
|
|
356
|
+
return nameKey;
|
|
314
357
|
} catch (err) {
|
|
315
|
-
if (
|
|
358
|
+
if (
|
|
359
|
+
!(err instanceof VaultCryptoError) ||
|
|
360
|
+
err.code !== "name_decrypt_failed"
|
|
361
|
+
)
|
|
362
|
+
throw err;
|
|
316
363
|
}
|
|
317
364
|
}
|
|
318
|
-
throw new VaultCryptoError(
|
|
365
|
+
throw new VaultCryptoError(
|
|
366
|
+
"name_decrypt_failed",
|
|
367
|
+
"name envelope did not open",
|
|
368
|
+
);
|
|
319
369
|
}
|
|
320
370
|
|
|
321
371
|
/** Encrypt a name as a v6 envelope bound to `rowId`, at the web app's default level. */
|
|
322
372
|
export async function encryptNameV6(options: {
|
|
323
|
-
name: string
|
|
324
|
-
folderKey: Uint8Array
|
|
325
|
-
rowId: string
|
|
373
|
+
name: string;
|
|
374
|
+
folderKey: Uint8Array;
|
|
375
|
+
rowId: string;
|
|
326
376
|
}): Promise<NameEnvelope> {
|
|
327
|
-
const { name, folderKey, rowId } = options
|
|
328
|
-
if (typeof name !==
|
|
329
|
-
throw new VaultCryptoError(
|
|
377
|
+
const { name, folderKey, rowId } = options;
|
|
378
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
379
|
+
throw new VaultCryptoError(
|
|
380
|
+
"invalid_input",
|
|
381
|
+
"name must be a non-empty string",
|
|
382
|
+
);
|
|
330
383
|
}
|
|
331
|
-
if (typeof rowId !==
|
|
332
|
-
throw new VaultCryptoError(
|
|
384
|
+
if (typeof rowId !== "string" || rowId.length === 0) {
|
|
385
|
+
throw new VaultCryptoError("invalid_input", "rowId is required");
|
|
333
386
|
}
|
|
334
|
-
const salt = randomBytes(NAME_SALT_BYTES)
|
|
335
|
-
const iv = randomBytes(IV_BYTES)
|
|
336
|
-
const key = await deriveNameKey(folderKey, salt,
|
|
337
|
-
const sealed = await aesGcmSeal(
|
|
387
|
+
const salt = randomBytes(NAME_SALT_BYTES);
|
|
388
|
+
const iv = randomBytes(IV_BYTES);
|
|
389
|
+
const key = await deriveNameKey(folderKey, salt, "interactive");
|
|
390
|
+
const sealed = await aesGcmSeal(
|
|
391
|
+
key,
|
|
392
|
+
iv,
|
|
393
|
+
TEXT_ENCODER.encode(name),
|
|
394
|
+
TEXT_ENCODER.encode(rowId),
|
|
395
|
+
);
|
|
338
396
|
return {
|
|
339
397
|
v: 6,
|
|
340
398
|
ct: bytesToBase64(sealed.slice(0, sealed.length - TAG_BYTES)),
|
|
341
399
|
iv: bytesToBase64(iv),
|
|
342
400
|
tag: bytesToBase64(sealed.slice(sealed.length - TAG_BYTES)),
|
|
343
401
|
salt: bytesToBase64(salt),
|
|
344
|
-
kdf:
|
|
345
|
-
}
|
|
402
|
+
kdf: "interactive",
|
|
403
|
+
};
|
|
346
404
|
}
|
|
347
405
|
|
|
348
406
|
// ──────────────────────────────────────────────────────────────────────
|
|
349
407
|
// Agent grants
|
|
350
408
|
// ──────────────────────────────────────────────────────────────────────
|
|
351
409
|
|
|
352
|
-
export const GRANT_CONNECTION_PREFIX =
|
|
410
|
+
export const GRANT_CONNECTION_PREFIX = "sf-grant-v1:";
|
|
353
411
|
|
|
354
412
|
/**
|
|
355
413
|
* What a grant wrap covers.
|
|
@@ -365,104 +423,195 @@ export const GRANT_CONNECTION_PREFIX = 'sf-grant-v1:'
|
|
|
365
423
|
* `file`, `file_pq` and `name` are only issued for objects whose parent key the
|
|
366
424
|
* grant does not hold; everything below a scope root is reached by the chain.
|
|
367
425
|
*/
|
|
368
|
-
export type GrantObjectKind =
|
|
369
|
-
const GRANT_KINDS: ReadonlySet<string> = new Set([
|
|
426
|
+
export type GrantObjectKind = "folder" | "file" | "file_pq" | "name";
|
|
427
|
+
const GRANT_KINDS: ReadonlySet<string> = new Set([
|
|
428
|
+
"folder",
|
|
429
|
+
"file",
|
|
430
|
+
"file_pq",
|
|
431
|
+
"name",
|
|
432
|
+
]);
|
|
370
433
|
|
|
371
434
|
export interface GrantCredential {
|
|
372
|
-
grantId: string
|
|
435
|
+
grantId: string;
|
|
373
436
|
/** Sent to the server as a bearer token. The server stores only its SHA-256. */
|
|
374
|
-
token: Uint8Array
|
|
437
|
+
token: Uint8Array;
|
|
375
438
|
/** Never leaves the client. Wraps the grant's keys. */
|
|
376
|
-
secret: Uint8Array
|
|
439
|
+
secret: Uint8Array;
|
|
377
440
|
/** SHA-256 of the owner's ML-KEM public key at grant creation, hex. */
|
|
378
|
-
publicKeyFingerprint: string
|
|
441
|
+
publicKeyFingerprint: string;
|
|
379
442
|
}
|
|
380
443
|
|
|
381
|
-
const UUID_RE =
|
|
444
|
+
const UUID_RE =
|
|
445
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
382
446
|
|
|
383
447
|
function assertUuid(value: string, what: string): void {
|
|
384
|
-
if (typeof value !==
|
|
385
|
-
throw new VaultCryptoError(
|
|
448
|
+
if (typeof value !== "string" || !UUID_RE.test(value)) {
|
|
449
|
+
throw new VaultCryptoError(
|
|
450
|
+
"invalid_input",
|
|
451
|
+
`${what} must be a lowercase UUID`,
|
|
452
|
+
);
|
|
386
453
|
}
|
|
387
454
|
}
|
|
388
455
|
|
|
389
456
|
function toBase64Url(bytes: Uint8Array): string {
|
|
390
|
-
return bytesToBase64(bytes)
|
|
457
|
+
return bytesToBase64(bytes)
|
|
458
|
+
.replace(/\+/g, "-")
|
|
459
|
+
.replace(/\//g, "_")
|
|
460
|
+
.replace(/=+$/, "");
|
|
391
461
|
}
|
|
392
462
|
|
|
393
463
|
function fromBase64Url(value: string): Uint8Array {
|
|
394
464
|
if (!/^[A-Za-z0-9_-]+$/.test(value)) {
|
|
395
|
-
throw new VaultCryptoError(
|
|
465
|
+
throw new VaultCryptoError("invalid_connection_string", "bad base64url");
|
|
396
466
|
}
|
|
397
467
|
if (value.length % 4 === 1) {
|
|
398
|
-
throw new VaultCryptoError(
|
|
468
|
+
throw new VaultCryptoError(
|
|
469
|
+
"invalid_connection_string",
|
|
470
|
+
"bad base64url length",
|
|
471
|
+
);
|
|
399
472
|
}
|
|
400
|
-
const b64 = value.replace(/-/g,
|
|
473
|
+
const b64 = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
401
474
|
try {
|
|
402
|
-
return base64ToBytes(b64 +
|
|
475
|
+
return base64ToBytes(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
|
|
403
476
|
} catch {
|
|
404
|
-
throw new VaultCryptoError(
|
|
477
|
+
throw new VaultCryptoError("invalid_connection_string", "bad base64url");
|
|
405
478
|
}
|
|
406
479
|
}
|
|
407
480
|
|
|
408
481
|
/** SHA-256 of an ML-KEM public key, hex. Pinned in the connection string. */
|
|
409
482
|
export function publicKeyFingerprint(mlKemPublicKey: Uint8Array): string {
|
|
410
|
-
return bytesToHex(sha256(mlKemPublicKey))
|
|
483
|
+
return bytesToHex(sha256(mlKemPublicKey));
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* The upload proof for a PQ-hybrid (Suite 0x03) object, in the frame the
|
|
488
|
+
* ShieldFive server verifies:
|
|
489
|
+
*
|
|
490
|
+
* base64( [0x03][0x03] || HMAC-SHA256(proofKey, [0x03][0x03] || header || chunk_0) )
|
|
491
|
+
*
|
|
492
|
+
* `chunk_0` is the first frame INCLUDING its 4-byte big-endian length prefix.
|
|
493
|
+
* The server re-reads exactly those ranges from storage and recomputes the MAC,
|
|
494
|
+
* so a matching proof says the object storage holds is the object the client
|
|
495
|
+
* encrypted — it does not, and cannot, say anything about the plaintext.
|
|
496
|
+
*
|
|
497
|
+
* It lives here because it is a keyed construction over ShieldFive's own
|
|
498
|
+
* container format, and the clients that produce it (the web app, and the MCP
|
|
499
|
+
* server's upload path) must produce byte-identical frames. A second
|
|
500
|
+
* hand-written implementation is how the two silently diverge.
|
|
501
|
+
*/
|
|
502
|
+
export async function buildUploadProofV3(options: {
|
|
503
|
+
/** The per-upload proof key the server issued, hex. */
|
|
504
|
+
proofKeyHex: string;
|
|
505
|
+
/** The complete ciphertext, or at least header + first chunk frame. */
|
|
506
|
+
ciphertext: Uint8Array;
|
|
507
|
+
}): Promise<string> {
|
|
508
|
+
const { proofKeyHex, ciphertext } = options;
|
|
509
|
+
if (!/^[0-9a-f]{64}$/i.test(proofKeyHex)) {
|
|
510
|
+
throw new VaultCryptoError(
|
|
511
|
+
"invalid_input",
|
|
512
|
+
"proofKeyHex must be 32 bytes of hex",
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
const header = parseHeader(ciphertext);
|
|
516
|
+
const lengthOffset = header.headerLength;
|
|
517
|
+
if (ciphertext.length < lengthOffset + LENGTH_PREFIX_BYTES) {
|
|
518
|
+
throw new VaultCryptoError(
|
|
519
|
+
"invalid_input",
|
|
520
|
+
"ciphertext is shorter than its header",
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
const view = new DataView(
|
|
524
|
+
ciphertext.buffer,
|
|
525
|
+
ciphertext.byteOffset,
|
|
526
|
+
ciphertext.byteLength,
|
|
527
|
+
);
|
|
528
|
+
const chunkZeroLength = view.getUint32(lengthOffset, false);
|
|
529
|
+
const end = lengthOffset + LENGTH_PREFIX_BYTES + chunkZeroLength;
|
|
530
|
+
if (chunkZeroLength === 0 || ciphertext.length < end) {
|
|
531
|
+
throw new VaultCryptoError(
|
|
532
|
+
"invalid_input",
|
|
533
|
+
"ciphertext is missing its first chunk",
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const prefix = Uint8Array.from([PROOF_VERSION_V3, CIPHER_VERSION_PQ_HYBRID]);
|
|
538
|
+
const macPayload = new Uint8Array(prefix.length + end);
|
|
539
|
+
macPayload.set(prefix, 0);
|
|
540
|
+
macPayload.set(ciphertext.subarray(0, end), prefix.length);
|
|
541
|
+
|
|
542
|
+
const mac = await hmacSha256(hexToBytes(proofKeyHex), macPayload);
|
|
543
|
+
const frame = new Uint8Array(prefix.length + mac.length);
|
|
544
|
+
frame.set(prefix, 0);
|
|
545
|
+
frame.set(mac, prefix.length);
|
|
546
|
+
return bytesToBase64(frame);
|
|
411
547
|
}
|
|
412
548
|
|
|
413
549
|
/** Fresh token + secret for a new grant. Call in the owner's unlocked client. */
|
|
414
550
|
export function createGrantCredential(options: {
|
|
415
|
-
grantId: string
|
|
416
|
-
mlKemPublicKey: Uint8Array
|
|
551
|
+
grantId: string;
|
|
552
|
+
mlKemPublicKey: Uint8Array;
|
|
417
553
|
}): GrantCredential {
|
|
418
|
-
assertUuid(options.grantId,
|
|
554
|
+
assertUuid(options.grantId, "grantId");
|
|
419
555
|
return {
|
|
420
556
|
grantId: options.grantId,
|
|
421
557
|
token: randomBytes(KEY_BYTES),
|
|
422
558
|
secret: randomBytes(KEY_BYTES),
|
|
423
559
|
publicKeyFingerprint: publicKeyFingerprint(options.mlKemPublicKey),
|
|
424
|
-
}
|
|
560
|
+
};
|
|
425
561
|
}
|
|
426
562
|
|
|
427
563
|
/** `sf-grant-v1:<grant_id>.<token>.<secret>.<pk_fingerprint>` (base64url, hex). */
|
|
428
564
|
export function formatConnectionString(c: GrantCredential): string {
|
|
429
|
-
assertUuid(c.grantId,
|
|
430
|
-
assertKey(c.token,
|
|
431
|
-
assertKey(c.secret,
|
|
565
|
+
assertUuid(c.grantId, "grantId");
|
|
566
|
+
assertKey(c.token, "token");
|
|
567
|
+
assertKey(c.secret, "secret");
|
|
432
568
|
if (!/^[0-9a-f]{64}$/.test(c.publicKeyFingerprint)) {
|
|
433
|
-
throw new VaultCryptoError(
|
|
569
|
+
throw new VaultCryptoError(
|
|
570
|
+
"invalid_input",
|
|
571
|
+
"fingerprint must be 64 hex chars",
|
|
572
|
+
);
|
|
434
573
|
}
|
|
435
|
-
return `${GRANT_CONNECTION_PREFIX}${c.grantId}.${toBase64Url(c.token)}.${toBase64Url(c.secret)}.${c.publicKeyFingerprint}
|
|
574
|
+
return `${GRANT_CONNECTION_PREFIX}${c.grantId}.${toBase64Url(c.token)}.${toBase64Url(c.secret)}.${c.publicKeyFingerprint}`;
|
|
436
575
|
}
|
|
437
576
|
|
|
438
577
|
export function parseConnectionString(value: string): GrantCredential {
|
|
439
|
-
const bad = (why: string) =>
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
578
|
+
const bad = (why: string) =>
|
|
579
|
+
new VaultCryptoError("invalid_connection_string", why);
|
|
580
|
+
if (typeof value !== "string") throw bad("not a string");
|
|
581
|
+
const trimmed = value.trim();
|
|
582
|
+
if (!trimmed.startsWith(GRANT_CONNECTION_PREFIX)) throw bad("wrong prefix");
|
|
583
|
+
const parts = trimmed.slice(GRANT_CONNECTION_PREFIX.length).split(".");
|
|
584
|
+
if (parts.length !== 4) throw bad("expected four parts");
|
|
585
|
+
const [grantId, tokenPart, secretPart, fingerprint] = parts as [
|
|
586
|
+
string,
|
|
587
|
+
string,
|
|
588
|
+
string,
|
|
589
|
+
string,
|
|
590
|
+
];
|
|
591
|
+
if (!UUID_RE.test(grantId)) throw bad("bad grant id");
|
|
592
|
+
const token = fromBase64Url(tokenPart);
|
|
593
|
+
const secret = fromBase64Url(secretPart);
|
|
594
|
+
if (token.length !== KEY_BYTES || secret.length !== KEY_BYTES)
|
|
595
|
+
throw bad("bad key length");
|
|
596
|
+
if (!/^[0-9a-f]{64}$/.test(fingerprint)) throw bad("bad fingerprint");
|
|
597
|
+
return { grantId, token, secret, publicKeyFingerprint: fingerprint };
|
|
452
598
|
}
|
|
453
599
|
|
|
454
600
|
/** The bearer value a client sends. */
|
|
455
|
-
export function grantBearerToken(c: Pick<GrantCredential,
|
|
456
|
-
return toBase64Url(c.token)
|
|
601
|
+
export function grantBearerToken(c: Pick<GrantCredential, "token">): string {
|
|
602
|
+
return toBase64Url(c.token);
|
|
457
603
|
}
|
|
458
604
|
|
|
459
605
|
/** What the server stores and looks up: SHA-256 of the bearer token's bytes, hex. */
|
|
460
606
|
export function hashGrantToken(bearer: string): string {
|
|
461
|
-
const bytes = fromBase64Url(bearer)
|
|
607
|
+
const bytes = fromBase64Url(bearer);
|
|
462
608
|
if (bytes.length !== KEY_BYTES) {
|
|
463
|
-
throw new VaultCryptoError(
|
|
609
|
+
throw new VaultCryptoError(
|
|
610
|
+
"invalid_input",
|
|
611
|
+
"bearer token has the wrong length",
|
|
612
|
+
);
|
|
464
613
|
}
|
|
465
|
-
return bytesToHex(sha256(bytes))
|
|
614
|
+
return bytesToHex(sha256(bytes));
|
|
466
615
|
}
|
|
467
616
|
|
|
468
617
|
/** GK = HKDF-SHA-256(secret, salt = utf8(grant id), info = AGENT_GRANT_WRAP). */
|
|
@@ -470,58 +619,66 @@ export async function deriveGrantWrapKey(
|
|
|
470
619
|
secret: Uint8Array,
|
|
471
620
|
grantId: string,
|
|
472
621
|
): Promise<Uint8Array> {
|
|
473
|
-
assertKey(secret,
|
|
474
|
-
assertUuid(grantId,
|
|
622
|
+
assertKey(secret, "secret");
|
|
623
|
+
assertUuid(grantId, "grantId");
|
|
475
624
|
return hkdfSha256({
|
|
476
625
|
ikm: secret,
|
|
477
626
|
salt: TEXT_ENCODER.encode(grantId),
|
|
478
627
|
info: HKDF_INFO.AGENT_GRANT_WRAP,
|
|
479
628
|
length: KEY_BYTES,
|
|
480
|
-
})
|
|
629
|
+
});
|
|
481
630
|
}
|
|
482
631
|
|
|
483
|
-
function grantAad(
|
|
632
|
+
function grantAad(
|
|
633
|
+
grantId: string,
|
|
634
|
+
kind: GrantObjectKind,
|
|
635
|
+
objectId: string,
|
|
636
|
+
): Uint8Array {
|
|
484
637
|
if (!GRANT_KINDS.has(kind)) {
|
|
485
|
-
throw new VaultCryptoError(
|
|
638
|
+
throw new VaultCryptoError("invalid_input", "unknown grant object kind");
|
|
486
639
|
}
|
|
487
|
-
assertUuid(grantId,
|
|
488
|
-
assertUuid(objectId,
|
|
489
|
-
return TEXT_ENCODER.encode(`sf-grant-v1|${grantId}|${kind}|${objectId}`)
|
|
640
|
+
assertUuid(grantId, "grantId");
|
|
641
|
+
assertUuid(objectId, "objectId");
|
|
642
|
+
return TEXT_ENCODER.encode(`sf-grant-v1|${grantId}|${kind}|${objectId}`);
|
|
490
643
|
}
|
|
491
644
|
|
|
492
645
|
/** Wrap a folder key or file content key for a grant, bound to (grant, kind, object). */
|
|
493
646
|
export async function wrapKeyForGrant(options: {
|
|
494
|
-
grantWrapKey: Uint8Array
|
|
495
|
-
grantId: string
|
|
496
|
-
kind: GrantObjectKind
|
|
497
|
-
objectId: string
|
|
498
|
-
key: Uint8Array
|
|
647
|
+
grantWrapKey: Uint8Array;
|
|
648
|
+
grantId: string;
|
|
649
|
+
kind: GrantObjectKind;
|
|
650
|
+
objectId: string;
|
|
651
|
+
key: Uint8Array;
|
|
499
652
|
}): Promise<WrappedKey> {
|
|
500
|
-
assertKey(options.grantWrapKey,
|
|
501
|
-
assertKey(options.key,
|
|
502
|
-
const aad = grantAad(options.grantId, options.kind, options.objectId)
|
|
503
|
-
const iv = randomBytes(IV_BYTES)
|
|
504
|
-
const ct = await aesGcmSeal(options.grantWrapKey, iv, options.key, aad)
|
|
505
|
-
return { wrapped: bytesToBase64(ct), iv: bytesToBase64(iv) }
|
|
653
|
+
assertKey(options.grantWrapKey, "grantWrapKey");
|
|
654
|
+
assertKey(options.key, "key");
|
|
655
|
+
const aad = grantAad(options.grantId, options.kind, options.objectId);
|
|
656
|
+
const iv = randomBytes(IV_BYTES);
|
|
657
|
+
const ct = await aesGcmSeal(options.grantWrapKey, iv, options.key, aad);
|
|
658
|
+
return { wrapped: bytesToBase64(ct), iv: bytesToBase64(iv) };
|
|
506
659
|
}
|
|
507
660
|
|
|
508
661
|
export async function unwrapKeyForGrant(options: {
|
|
509
|
-
grantWrapKey: Uint8Array
|
|
510
|
-
grantId: string
|
|
511
|
-
kind: GrantObjectKind
|
|
512
|
-
objectId: string
|
|
513
|
-
wrapped: WrappedKey
|
|
662
|
+
grantWrapKey: Uint8Array;
|
|
663
|
+
grantId: string;
|
|
664
|
+
kind: GrantObjectKind;
|
|
665
|
+
objectId: string;
|
|
666
|
+
wrapped: WrappedKey;
|
|
514
667
|
}): Promise<Uint8Array> {
|
|
515
|
-
assertKey(options.grantWrapKey,
|
|
516
|
-
const aad = grantAad(options.grantId, options.kind, options.objectId)
|
|
517
|
-
const iv = base64ToBytes(options.wrapped.iv)
|
|
518
|
-
const ct = base64ToBytes(options.wrapped.wrapped)
|
|
668
|
+
assertKey(options.grantWrapKey, "grantWrapKey");
|
|
669
|
+
const aad = grantAad(options.grantId, options.kind, options.objectId);
|
|
670
|
+
const iv = base64ToBytes(options.wrapped.iv);
|
|
671
|
+
const ct = base64ToBytes(options.wrapped.wrapped);
|
|
519
672
|
if (iv.length !== IV_BYTES || ct.length !== KEY_BYTES + TAG_BYTES) {
|
|
520
|
-
throw new VaultCryptoError(
|
|
673
|
+
throw new VaultCryptoError(
|
|
674
|
+
"unwrap_failed",
|
|
675
|
+
"grant wrap has the wrong shape",
|
|
676
|
+
);
|
|
521
677
|
}
|
|
522
|
-
const key = await aesGcmOpen(options.grantWrapKey, iv, ct, aad)
|
|
523
|
-
if (!key)
|
|
524
|
-
|
|
678
|
+
const key = await aesGcmOpen(options.grantWrapKey, iv, ct, aad);
|
|
679
|
+
if (!key)
|
|
680
|
+
throw new VaultCryptoError("unwrap_failed", "grant wrap did not open");
|
|
681
|
+
return key;
|
|
525
682
|
}
|
|
526
683
|
|
|
527
684
|
// ──────────────────────────────────────────────────────────────────────
|
|
@@ -530,65 +687,70 @@ export async function unwrapKeyForGrant(options: {
|
|
|
530
687
|
|
|
531
688
|
/** What a grant covers, as the owner decided it when creating the grant. */
|
|
532
689
|
export interface GrantScope {
|
|
533
|
-
grantId: string
|
|
534
|
-
scopeAll: boolean
|
|
535
|
-
includeMedia: boolean
|
|
690
|
+
grantId: string;
|
|
691
|
+
scopeAll: boolean;
|
|
692
|
+
includeMedia: boolean;
|
|
536
693
|
/** Scope-root folder ids (empty for a whole-vault grant). */
|
|
537
|
-
rootIds: readonly string[]
|
|
694
|
+
rootIds: readonly string[];
|
|
538
695
|
/** The grant's own trash folder, or null for a read-only grant. */
|
|
539
|
-
trashFolderId: string | null
|
|
696
|
+
trashFolderId: string | null;
|
|
540
697
|
/** Top-level folders a whole-vault grant must never cover (the Bin, and Media unless opted in). */
|
|
541
|
-
excludedIds: readonly string[]
|
|
698
|
+
excludedIds: readonly string[];
|
|
542
699
|
/**
|
|
543
700
|
* keyFingerprint() of those folders' keys. A server can relabel a row with
|
|
544
701
|
* another folder's wrapped key; it cannot change a key's fingerprint, so the
|
|
545
702
|
* owner's browser refuses to hand an excluded folder's key to the grant under
|
|
546
703
|
* any id.
|
|
547
704
|
*/
|
|
548
|
-
excludedKeyFingerprints: readonly string[]
|
|
705
|
+
excludedKeyFingerprints: readonly string[];
|
|
549
706
|
}
|
|
550
707
|
|
|
551
708
|
/** SHA-256 of a 32-byte key, hex. Reveals nothing usable about a random key. */
|
|
552
709
|
export function keyFingerprint(key: Uint8Array): string {
|
|
553
|
-
assertKey(key,
|
|
554
|
-
return bytesToHex(sha256(key))
|
|
710
|
+
assertKey(key, "key");
|
|
711
|
+
return bytesToHex(sha256(key));
|
|
555
712
|
}
|
|
556
713
|
|
|
557
714
|
/** A canonical, order-independent string for a scope. Any change to the scope changes it. */
|
|
558
715
|
export function canonicalGrantScope(scope: GrantScope): string {
|
|
559
|
-
assertUuid(scope.grantId,
|
|
716
|
+
assertUuid(scope.grantId, "grantId");
|
|
560
717
|
const ids = (list: readonly string[], what: string) => {
|
|
561
|
-
for (const id of list) assertUuid(id, what)
|
|
562
|
-
return [...new Set(list)].sort().join(
|
|
563
|
-
}
|
|
564
|
-
if (scope.trashFolderId !== null)
|
|
718
|
+
for (const id of list) assertUuid(id, what);
|
|
719
|
+
return [...new Set(list)].sort().join(",");
|
|
720
|
+
};
|
|
721
|
+
if (scope.trashFolderId !== null)
|
|
722
|
+
assertUuid(scope.trashFolderId, "trashFolderId");
|
|
565
723
|
return [
|
|
566
|
-
|
|
724
|
+
"sf-grant-scope-v1",
|
|
567
725
|
scope.grantId,
|
|
568
726
|
`all=${scope.scopeAll ? 1 : 0}`,
|
|
569
727
|
`media=${scope.includeMedia ? 1 : 0}`,
|
|
570
|
-
`roots=${ids(scope.rootIds,
|
|
571
|
-
`trash=${scope.trashFolderId ??
|
|
572
|
-
`excluded=${ids(scope.excludedIds,
|
|
728
|
+
`roots=${ids(scope.rootIds, "rootIds")}`,
|
|
729
|
+
`trash=${scope.trashFolderId ?? "-"}`,
|
|
730
|
+
`excluded=${ids(scope.excludedIds, "excludedIds")}`,
|
|
573
731
|
`excludedKeys=${[...new Set(scope.excludedKeyFingerprints)]
|
|
574
732
|
.map((f) => {
|
|
575
|
-
if (!/^[0-9a-f]{64}$/.test(f))
|
|
576
|
-
|
|
733
|
+
if (!/^[0-9a-f]{64}$/.test(f))
|
|
734
|
+
throw new VaultCryptoError("invalid_input", "bad key fingerprint");
|
|
735
|
+
return f;
|
|
577
736
|
})
|
|
578
737
|
.sort()
|
|
579
|
-
.join(
|
|
580
|
-
].join(
|
|
738
|
+
.join(",")}`,
|
|
739
|
+
].join("|");
|
|
581
740
|
}
|
|
582
741
|
|
|
583
|
-
async function grantSecretKey(
|
|
584
|
-
|
|
585
|
-
|
|
742
|
+
async function grantSecretKey(
|
|
743
|
+
rootKey: Uint8Array,
|
|
744
|
+
grantId: string,
|
|
745
|
+
): Promise<Uint8Array> {
|
|
746
|
+
assertKey(rootKey, "rootKey");
|
|
747
|
+
assertUuid(grantId, "grantId");
|
|
586
748
|
return hkdfSha256({
|
|
587
749
|
ikm: rootKey,
|
|
588
750
|
salt: TEXT_ENCODER.encode(grantId),
|
|
589
751
|
info: HKDF_INFO.AGENT_GRANT_SECRET,
|
|
590
752
|
length: KEY_BYTES,
|
|
591
|
-
})
|
|
753
|
+
});
|
|
592
754
|
}
|
|
593
755
|
|
|
594
756
|
/**
|
|
@@ -597,30 +759,47 @@ async function grantSecretKey(rootKey: Uint8Array, grantId: string): Promise<Uin
|
|
|
597
759
|
* vault root key opens it, and only for exactly this scope.
|
|
598
760
|
*/
|
|
599
761
|
export async function wrapGrantSecret(options: {
|
|
600
|
-
rootKey: Uint8Array
|
|
601
|
-
scope: GrantScope
|
|
602
|
-
secret: Uint8Array
|
|
762
|
+
rootKey: Uint8Array;
|
|
763
|
+
scope: GrantScope;
|
|
764
|
+
secret: Uint8Array;
|
|
603
765
|
}): Promise<WrappedKey> {
|
|
604
|
-
assertKey(options.secret,
|
|
605
|
-
const key = await grantSecretKey(options.rootKey, options.scope.grantId)
|
|
606
|
-
const iv = randomBytes(IV_BYTES)
|
|
607
|
-
const ct = await aesGcmSeal(
|
|
608
|
-
|
|
766
|
+
assertKey(options.secret, "secret");
|
|
767
|
+
const key = await grantSecretKey(options.rootKey, options.scope.grantId);
|
|
768
|
+
const iv = randomBytes(IV_BYTES);
|
|
769
|
+
const ct = await aesGcmSeal(
|
|
770
|
+
key,
|
|
771
|
+
iv,
|
|
772
|
+
options.secret,
|
|
773
|
+
TEXT_ENCODER.encode(canonicalGrantScope(options.scope)),
|
|
774
|
+
);
|
|
775
|
+
return { wrapped: bytesToBase64(ct), iv: bytesToBase64(iv) };
|
|
609
776
|
}
|
|
610
777
|
|
|
611
778
|
/** Open the owner's copy of a grant secret. Fails if the scope differs in any way from the one it was wrapped for. */
|
|
612
779
|
export async function unwrapGrantSecret(options: {
|
|
613
|
-
rootKey: Uint8Array
|
|
614
|
-
scope: GrantScope
|
|
615
|
-
wrapped: WrappedKey
|
|
780
|
+
rootKey: Uint8Array;
|
|
781
|
+
scope: GrantScope;
|
|
782
|
+
wrapped: WrappedKey;
|
|
616
783
|
}): Promise<Uint8Array> {
|
|
617
|
-
const key = await grantSecretKey(options.rootKey, options.scope.grantId)
|
|
618
|
-
const iv = base64ToBytes(options.wrapped.iv)
|
|
619
|
-
const ct = base64ToBytes(options.wrapped.wrapped)
|
|
784
|
+
const key = await grantSecretKey(options.rootKey, options.scope.grantId);
|
|
785
|
+
const iv = base64ToBytes(options.wrapped.iv);
|
|
786
|
+
const ct = base64ToBytes(options.wrapped.wrapped);
|
|
620
787
|
if (iv.length !== IV_BYTES || ct.length !== KEY_BYTES + TAG_BYTES) {
|
|
621
|
-
throw new VaultCryptoError(
|
|
788
|
+
throw new VaultCryptoError(
|
|
789
|
+
"unwrap_failed",
|
|
790
|
+
"grant secret wrap has the wrong shape",
|
|
791
|
+
);
|
|
622
792
|
}
|
|
623
|
-
const secret = await aesGcmOpen(
|
|
624
|
-
|
|
625
|
-
|
|
793
|
+
const secret = await aesGcmOpen(
|
|
794
|
+
key,
|
|
795
|
+
iv,
|
|
796
|
+
ct,
|
|
797
|
+
TEXT_ENCODER.encode(canonicalGrantScope(options.scope)),
|
|
798
|
+
);
|
|
799
|
+
if (!secret)
|
|
800
|
+
throw new VaultCryptoError(
|
|
801
|
+
"unwrap_failed",
|
|
802
|
+
"grant secret did not open for this scope",
|
|
803
|
+
);
|
|
804
|
+
return secret;
|
|
626
805
|
}
|