@johnhenry/browsermesh-core 0.0.1 → 0.1.1
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/package.json +2 -2
- package/src/capabilities.mjs +177 -26
- package/src/group-keys.mjs +60 -4
- package/src/handshake.mjs +267 -20
- package/src/identity-tools.mjs +20 -6
- package/src/identity-wallet.mjs +5 -1
- package/src/identity.mjs +217 -106
- package/src/keyring.mjs +94 -16
package/src/identity.mjs
CHANGED
|
@@ -30,6 +30,122 @@ export { PodIdentity, derivePodId, encodeBase64url, decodeBase64url };
|
|
|
30
30
|
const VAULT_PBKDF2_ITERATIONS = 310_000;
|
|
31
31
|
const VAULT_SALT_BYTES = 16;
|
|
32
32
|
const VAULT_IV_BYTES = 12;
|
|
33
|
+
const VAULT_KDF = 'PBKDF2-SHA256';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Reject a passphrase that would not actually protect anything.
|
|
37
|
+
*
|
|
38
|
+
* PBKDF2 happily accepts an empty key, and `TextEncoder` turns `undefined`
|
|
39
|
+
* into the five bytes `undefined`, so a missing passphrase produces a
|
|
40
|
+
* well-formed ciphertext that anyone can derive the key for. Both cases have
|
|
41
|
+
* to be refused rather than encrypted.
|
|
42
|
+
*
|
|
43
|
+
* @param {*} passphrase
|
|
44
|
+
* @param {string} source - Where the passphrase came from, for the message
|
|
45
|
+
* @returns {string}
|
|
46
|
+
* @throws {TypeError}
|
|
47
|
+
*/
|
|
48
|
+
function requirePassphrase(passphrase, source) {
|
|
49
|
+
if (typeof passphrase !== 'string' || passphrase.length === 0) {
|
|
50
|
+
throw new TypeError(
|
|
51
|
+
`${source} must return a non-empty string passphrase, got ` +
|
|
52
|
+
`${passphrase === null ? 'null' : typeof passphrase}` +
|
|
53
|
+
`${typeof passphrase === 'string' ? ' of length 0' : ''}`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return passphrase;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* PBKDF2(SHA-256) → AES-256-GCM over a UTF-8 string.
|
|
61
|
+
*
|
|
62
|
+
* The KDF name and iteration count travel with the ciphertext so a future
|
|
63
|
+
* change of parameters does not orphan envelopes written under the old ones.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} plaintext
|
|
66
|
+
* @param {string} passphrase
|
|
67
|
+
* @returns {Promise<{ kdf: string, iterations: number, salt: string, iv: string, ciphertext: string }>}
|
|
68
|
+
*/
|
|
69
|
+
async function encryptWithPassphrase(plaintext, passphrase) {
|
|
70
|
+
const enc = new TextEncoder();
|
|
71
|
+
const salt = crypto.getRandomValues(new Uint8Array(VAULT_SALT_BYTES));
|
|
72
|
+
const iv = crypto.getRandomValues(new Uint8Array(VAULT_IV_BYTES));
|
|
73
|
+
|
|
74
|
+
const baseKey = await crypto.subtle.importKey(
|
|
75
|
+
'raw',
|
|
76
|
+
enc.encode(passphrase),
|
|
77
|
+
'PBKDF2',
|
|
78
|
+
false,
|
|
79
|
+
['deriveKey']
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const aesKey = await crypto.subtle.deriveKey(
|
|
83
|
+
{ name: 'PBKDF2', salt, iterations: VAULT_PBKDF2_ITERATIONS, hash: 'SHA-256' },
|
|
84
|
+
baseKey,
|
|
85
|
+
{ name: 'AES-GCM', length: 256 },
|
|
86
|
+
false,
|
|
87
|
+
['encrypt']
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const ciphertext = new Uint8Array(
|
|
91
|
+
await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, aesKey, enc.encode(plaintext))
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
kdf: VAULT_KDF,
|
|
96
|
+
iterations: VAULT_PBKDF2_ITERATIONS,
|
|
97
|
+
salt: encodeBase64url(salt),
|
|
98
|
+
iv: encodeBase64url(iv),
|
|
99
|
+
ciphertext: encodeBase64url(ciphertext),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Inverse of {@link encryptWithPassphrase}.
|
|
105
|
+
*
|
|
106
|
+
* `iterations` is read from the envelope so blobs written before the count
|
|
107
|
+
* was recorded (and before it was raised to match the documented 310k) still
|
|
108
|
+
* open.
|
|
109
|
+
*
|
|
110
|
+
* @param {{ kdf?: string, iterations?: number, salt: string, iv: string, ciphertext: string }} envelope
|
|
111
|
+
* @param {string} passphrase
|
|
112
|
+
* @returns {Promise<string>}
|
|
113
|
+
*/
|
|
114
|
+
async function decryptWithPassphrase(envelope, passphrase) {
|
|
115
|
+
const { kdf, iterations, salt, iv, ciphertext } = envelope;
|
|
116
|
+
if (kdf !== undefined && kdf !== VAULT_KDF) {
|
|
117
|
+
throw new Error(`Unsupported key derivation function: ${kdf}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const baseKey = await crypto.subtle.importKey(
|
|
121
|
+
'raw',
|
|
122
|
+
new TextEncoder().encode(passphrase),
|
|
123
|
+
'PBKDF2',
|
|
124
|
+
false,
|
|
125
|
+
['deriveKey']
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const aesKey = await crypto.subtle.deriveKey(
|
|
129
|
+
{
|
|
130
|
+
name: 'PBKDF2',
|
|
131
|
+
salt: decodeBase64url(salt),
|
|
132
|
+
iterations: iterations ?? VAULT_PBKDF2_ITERATIONS,
|
|
133
|
+
hash: 'SHA-256',
|
|
134
|
+
},
|
|
135
|
+
baseKey,
|
|
136
|
+
{ name: 'AES-GCM', length: 256 },
|
|
137
|
+
false,
|
|
138
|
+
['decrypt']
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const plaintext = await crypto.subtle.decrypt(
|
|
142
|
+
{ name: 'AES-GCM', iv: decodeBase64url(iv) },
|
|
143
|
+
aesKey,
|
|
144
|
+
decodeBase64url(ciphertext)
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
return new TextDecoder().decode(plaintext);
|
|
148
|
+
}
|
|
33
149
|
|
|
34
150
|
// ---------------------------------------------------------------------------
|
|
35
151
|
// base58btc encoding (Bitcoin alphabet) — used for W3C did:key
|
|
@@ -216,22 +332,72 @@ export class MeshIdentityManager {
|
|
|
216
332
|
}
|
|
217
333
|
|
|
218
334
|
/**
|
|
219
|
-
* Import an identity from a JWK private key.
|
|
335
|
+
* Import an identity from a JWK private key, or from an encrypted export.
|
|
336
|
+
*
|
|
337
|
+
* Accepts either shape {@link MeshIdentityManager#export} produces: a raw
|
|
338
|
+
* JWK, or the `{ encrypted: true, ... }` envelope. Before this took the
|
|
339
|
+
* envelope, an encrypted export was write-only -- the safe thing to do with
|
|
340
|
+
* a backup produced a backup this library could not restore.
|
|
220
341
|
*
|
|
221
|
-
*
|
|
342
|
+
* The passphrase travels in `opts` rather than as a fourth positional,
|
|
343
|
+
* because `opts` is already the third parameter and `import(jwk, label,
|
|
344
|
+
* undefined, pw)` is not a signature anyone should have to write.
|
|
345
|
+
*
|
|
346
|
+
* @param {object} keyData - An Ed25519 private key JWK, or an encrypted
|
|
347
|
+
* envelope from `export(podId, passphrase)`
|
|
222
348
|
* @param {string} label - Human-readable name
|
|
223
349
|
* @param {object} [opts]
|
|
350
|
+
* @param {string} [opts.passphrase] - Required when `keyData` is encrypted
|
|
224
351
|
* @param {object} [opts.metadata]
|
|
225
352
|
* @returns {Promise<IdentitySummary>}
|
|
353
|
+
* @throws {TypeError} If an encrypted envelope is given without a passphrase
|
|
354
|
+
* @throws {Error} If the passphrase is wrong or the envelope is damaged
|
|
226
355
|
*/
|
|
227
|
-
async import(
|
|
356
|
+
async import(keyData, label, opts = {}) {
|
|
228
357
|
if (!label || typeof label !== 'string') {
|
|
229
358
|
throw new Error('Label is required and must be a non-empty string');
|
|
230
359
|
}
|
|
231
|
-
if (!
|
|
360
|
+
if (!keyData || typeof keyData !== 'object') {
|
|
232
361
|
throw new Error('privateKeyJwk must be a valid JWK object');
|
|
233
362
|
}
|
|
234
363
|
|
|
364
|
+
let privateKeyJwk = keyData;
|
|
365
|
+
if (keyData.encrypted) {
|
|
366
|
+
const passphrase = requirePassphrase(opts?.passphrase, 'opts.passphrase');
|
|
367
|
+
// One message for a wrong passphrase and for a damaged envelope alike.
|
|
368
|
+
// AES-GCM cannot tell them apart anyway, and reporting "decrypted but
|
|
369
|
+
// the contents were not a key" would confirm to someone guessing that
|
|
370
|
+
// a passphrase was correct.
|
|
371
|
+
const unreadable = (err) => new Error(
|
|
372
|
+
'Could not read the encrypted identity export: the passphrase is ' +
|
|
373
|
+
'wrong, or the export is damaged.',
|
|
374
|
+
{ cause: err }
|
|
375
|
+
);
|
|
376
|
+
let plaintext;
|
|
377
|
+
try {
|
|
378
|
+
plaintext = await decryptWithPassphrase(keyData, passphrase);
|
|
379
|
+
} catch (err) {
|
|
380
|
+
throw unreadable(err);
|
|
381
|
+
}
|
|
382
|
+
try {
|
|
383
|
+
privateKeyJwk = JSON.parse(plaintext);
|
|
384
|
+
} catch (err) {
|
|
385
|
+
throw unreadable(err);
|
|
386
|
+
}
|
|
387
|
+
if (!privateKeyJwk || typeof privateKeyJwk !== 'object') {
|
|
388
|
+
throw unreadable(new Error('decrypted payload was not a JWK object'));
|
|
389
|
+
}
|
|
390
|
+
} else if (opts?.passphrase !== undefined && opts?.passphrase !== null) {
|
|
391
|
+
// A caller who supplied a passphrase believes this blob is encrypted.
|
|
392
|
+
// Importing it quietly would hand them an identity restored from a
|
|
393
|
+
// plaintext private key while they believed it had been protected --
|
|
394
|
+
// the same confusion #18 was about, arriving from the other direction.
|
|
395
|
+
throw new Error(
|
|
396
|
+
'A passphrase was supplied but this export is not encrypted. ' +
|
|
397
|
+
'Import it without a passphrase, or check you have the right file.'
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
235
401
|
// Import the private key
|
|
236
402
|
const privateKey = await crypto.subtle.importKey(
|
|
237
403
|
'jwk',
|
|
@@ -286,62 +452,54 @@ export class MeshIdentityManager {
|
|
|
286
452
|
/**
|
|
287
453
|
* Export an identity as a JWK.
|
|
288
454
|
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
455
|
+
* Called with no passphrase, this returns the **raw private key** — that
|
|
456
|
+
* is the documented plaintext-export path, and the caller is responsible
|
|
457
|
+
* for what happens to it next.
|
|
458
|
+
*
|
|
459
|
+
* Called with a passphrase, it returns an encrypted envelope
|
|
460
|
+
* (`{ encrypted: true, kdf, iterations, salt, iv, ciphertext }`) or it
|
|
461
|
+
* throws. It never quietly hands back the plaintext key instead: a caller
|
|
462
|
+
* who asked for encryption and got an unlabelled JWK would have no way to
|
|
463
|
+
* tell, and would go on to write, paste or QR-encode a private key it
|
|
464
|
+
* believed was protected.
|
|
292
465
|
*
|
|
293
466
|
* @param {string} podId
|
|
294
|
-
* @param {string} [passphrase] -
|
|
295
|
-
*
|
|
467
|
+
* @param {string} [passphrase] - Encryption passphrase; must be a non-empty
|
|
468
|
+
* string when given. Omit it to export in the clear.
|
|
469
|
+
* @returns {Promise<object>} The raw JWK, or an encrypted envelope
|
|
470
|
+
* @throws {TypeError} If `passphrase` is given but is not a non-empty string
|
|
471
|
+
* @throws {Error} If encryption was requested and could not be performed
|
|
296
472
|
*/
|
|
297
473
|
async export(podId, passphrase) {
|
|
298
474
|
const entry = this.#identities.get(podId);
|
|
299
475
|
if (!entry) throw new Error(`Unknown identity: ${podId}`);
|
|
300
476
|
|
|
477
|
+
if (passphrase !== undefined && passphrase !== null) {
|
|
478
|
+
if (typeof passphrase !== 'string' || passphrase.length === 0) {
|
|
479
|
+
throw new TypeError(
|
|
480
|
+
'passphrase must be a non-empty string; omit it to export the key in the clear'
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
301
485
|
const jwk = await crypto.subtle.exportKey('jwk', entry.identity.keyPair.privateKey);
|
|
302
486
|
|
|
303
|
-
if (
|
|
487
|
+
if (passphrase === undefined || passphrase === null) return jwk;
|
|
304
488
|
|
|
305
|
-
|
|
489
|
+
let envelope;
|
|
306
490
|
try {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
false,
|
|
316
|
-
['deriveKey']
|
|
317
|
-
);
|
|
318
|
-
|
|
319
|
-
const aesKey = await crypto.subtle.deriveKey(
|
|
320
|
-
{ name: 'PBKDF2', salt, iterations: 100_000, hash: 'SHA-256' },
|
|
321
|
-
baseKey,
|
|
322
|
-
{ name: 'AES-GCM', length: 256 },
|
|
323
|
-
false,
|
|
324
|
-
['encrypt']
|
|
491
|
+
envelope = await encryptWithPassphrase(JSON.stringify(jwk), passphrase);
|
|
492
|
+
} catch (err) {
|
|
493
|
+
// Deliberately not falling back to the plaintext JWK. Encryption was
|
|
494
|
+
// asked for; if it cannot be done, the caller has to know.
|
|
495
|
+
throw new Error(
|
|
496
|
+
`Encrypted export failed for ${podId}: ${err?.message || err}. ` +
|
|
497
|
+
'The private key was not exported.',
|
|
498
|
+
{ cause: err }
|
|
325
499
|
);
|
|
326
|
-
|
|
327
|
-
const ciphertext = new Uint8Array(
|
|
328
|
-
await crypto.subtle.encrypt(
|
|
329
|
-
{ name: 'AES-GCM', iv },
|
|
330
|
-
aesKey,
|
|
331
|
-
enc.encode(JSON.stringify(jwk))
|
|
332
|
-
)
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
return {
|
|
336
|
-
encrypted: true,
|
|
337
|
-
salt: encodeBase64url(salt),
|
|
338
|
-
iv: encodeBase64url(iv),
|
|
339
|
-
ciphertext: encodeBase64url(ciphertext),
|
|
340
|
-
};
|
|
341
|
-
} catch {
|
|
342
|
-
// PBKDF2 or AES-GCM not available -- return raw JWK
|
|
343
|
-
return jwk;
|
|
344
500
|
}
|
|
501
|
+
|
|
502
|
+
return { encrypted: true, ...envelope };
|
|
345
503
|
}
|
|
346
504
|
|
|
347
505
|
// -- Lookup & Enumeration -----------------------------------------------
|
|
@@ -781,6 +939,11 @@ export class IndexedDBIdentityStorage {
|
|
|
781
939
|
* Wraps another identity storage with passphrase-based encryption.
|
|
782
940
|
* Only the privateKey field is encrypted; publicKey + metadata stored cleartext.
|
|
783
941
|
* Uses PBKDF2(310k, SHA-256) → AES-256-GCM.
|
|
942
|
+
*
|
|
943
|
+
* `getPassphrase` must resolve to a non-empty string. An empty string or a
|
|
944
|
+
* missing value is refused rather than used: PBKDF2 accepts an empty key and
|
|
945
|
+
* `TextEncoder` renders `undefined` as the bytes `undefined`, either of which
|
|
946
|
+
* would write a record that looks encrypted and is trivially readable.
|
|
784
947
|
*/
|
|
785
948
|
export class VaultIdentityStorage {
|
|
786
949
|
/** @type {InMemoryIdentityStorage|IndexedDBIdentityStorage} */
|
|
@@ -813,44 +976,15 @@ export class VaultIdentityStorage {
|
|
|
813
976
|
return this.#inner.save(podId, data);
|
|
814
977
|
}
|
|
815
978
|
|
|
816
|
-
const passphrase = await this.#getPassphrase();
|
|
817
979
|
const record = { ...data };
|
|
818
980
|
|
|
819
981
|
if (record.privateKeyJwk) {
|
|
820
|
-
const
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
const baseKey = await crypto.subtle.importKey(
|
|
825
|
-
'raw',
|
|
826
|
-
enc.encode(passphrase),
|
|
827
|
-
'PBKDF2',
|
|
828
|
-
false,
|
|
829
|
-
['deriveKey']
|
|
830
|
-
);
|
|
831
|
-
|
|
832
|
-
const aesKey = await crypto.subtle.deriveKey(
|
|
833
|
-
{ name: 'PBKDF2', salt, iterations: VAULT_PBKDF2_ITERATIONS, hash: 'SHA-256' },
|
|
834
|
-
baseKey,
|
|
835
|
-
{ name: 'AES-GCM', length: 256 },
|
|
836
|
-
false,
|
|
837
|
-
['encrypt']
|
|
838
|
-
);
|
|
839
|
-
|
|
840
|
-
const ciphertext = new Uint8Array(
|
|
841
|
-
await crypto.subtle.encrypt(
|
|
842
|
-
{ name: 'AES-GCM', iv },
|
|
843
|
-
aesKey,
|
|
844
|
-
enc.encode(JSON.stringify(record.privateKeyJwk))
|
|
845
|
-
)
|
|
982
|
+
const passphrase = requirePassphrase(await this.#getPassphrase(), 'getPassphrase');
|
|
983
|
+
record.encryptedPrivateKey = await encryptWithPassphrase(
|
|
984
|
+
JSON.stringify(record.privateKeyJwk),
|
|
985
|
+
passphrase
|
|
846
986
|
);
|
|
847
|
-
|
|
848
987
|
record.privateKeyJwk = null;
|
|
849
|
-
record.encryptedPrivateKey = {
|
|
850
|
-
salt: encodeBase64url(salt),
|
|
851
|
-
iv: encodeBase64url(iv),
|
|
852
|
-
ciphertext: encodeBase64url(ciphertext),
|
|
853
|
-
};
|
|
854
988
|
}
|
|
855
989
|
|
|
856
990
|
return this.#inner.save(podId, record);
|
|
@@ -869,33 +1003,10 @@ export class VaultIdentityStorage {
|
|
|
869
1003
|
if (podId === '__meta__') return record;
|
|
870
1004
|
|
|
871
1005
|
if (record.encryptedPrivateKey) {
|
|
872
|
-
const passphrase = await this.#getPassphrase();
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
const baseKey = await crypto.subtle.importKey(
|
|
877
|
-
'raw',
|
|
878
|
-
enc.encode(passphrase),
|
|
879
|
-
'PBKDF2',
|
|
880
|
-
false,
|
|
881
|
-
['deriveKey']
|
|
882
|
-
);
|
|
883
|
-
|
|
884
|
-
const aesKey = await crypto.subtle.deriveKey(
|
|
885
|
-
{ name: 'PBKDF2', salt: decodeBase64url(salt), iterations: VAULT_PBKDF2_ITERATIONS, hash: 'SHA-256' },
|
|
886
|
-
baseKey,
|
|
887
|
-
{ name: 'AES-GCM', length: 256 },
|
|
888
|
-
false,
|
|
889
|
-
['decrypt']
|
|
1006
|
+
const passphrase = requirePassphrase(await this.#getPassphrase(), 'getPassphrase');
|
|
1007
|
+
record.privateKeyJwk = JSON.parse(
|
|
1008
|
+
await decryptWithPassphrase(record.encryptedPrivateKey, passphrase)
|
|
890
1009
|
);
|
|
891
|
-
|
|
892
|
-
const plaintext = await crypto.subtle.decrypt(
|
|
893
|
-
{ name: 'AES-GCM', iv: decodeBase64url(iv) },
|
|
894
|
-
aesKey,
|
|
895
|
-
decodeBase64url(ciphertext)
|
|
896
|
-
);
|
|
897
|
-
|
|
898
|
-
record.privateKeyJwk = JSON.parse(new TextDecoder().decode(plaintext));
|
|
899
1010
|
delete record.encryptedPrivateKey;
|
|
900
1011
|
}
|
|
901
1012
|
|
package/src/keyring.mjs
CHANGED
|
@@ -106,7 +106,11 @@ export class KeyLink {
|
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
108
|
* A KeyLink with cryptographic Ed25519 signatures from both parent and child.
|
|
109
|
-
*
|
|
109
|
+
*
|
|
110
|
+
* The signed payload is canonical JSON over every asserted field —
|
|
111
|
+
* `parent`, `child`, `relation`, `scope`, `expires` and `created` — so a
|
|
112
|
+
* signature that verifies vouches for the delegation's limits, not just for
|
|
113
|
+
* who linked to whom. See {@link SignedKeyLink#signedPayload}.
|
|
110
114
|
*/
|
|
111
115
|
export class SignedKeyLink extends KeyLink {
|
|
112
116
|
/** @type {Uint8Array|null} */
|
|
@@ -128,13 +132,83 @@ export class SignedKeyLink extends KeyLink {
|
|
|
128
132
|
|
|
129
133
|
/**
|
|
130
134
|
* Get the canonical signed payload bytes.
|
|
135
|
+
*
|
|
136
|
+
* Version 2. Covers **every** field the link asserts, including `scope`
|
|
137
|
+
* and `expires`. Version 1 (`parent|child|relation|created`) left those
|
|
138
|
+
* two out, which meant a holder of a signed link could widen its scope
|
|
139
|
+
* and push out its expiry without invalidating either signature — see
|
|
140
|
+
* {@link legacySignedPayload}.
|
|
141
|
+
*
|
|
142
|
+
* JSON is used rather than a delimiter-joined string because `scope`
|
|
143
|
+
* holds caller-supplied strings: `['a|b']` and `['a', 'b']` must not
|
|
144
|
+
* canonicalise to the same bytes. Key order is fixed by the literal, so
|
|
145
|
+
* the encoding is deterministic.
|
|
146
|
+
*
|
|
131
147
|
* @returns {Uint8Array}
|
|
132
148
|
*/
|
|
133
149
|
get signedPayload() {
|
|
150
|
+
return new TextEncoder().encode(JSON.stringify({
|
|
151
|
+
v: 2,
|
|
152
|
+
parent: this.parent,
|
|
153
|
+
child: this.child,
|
|
154
|
+
relation: this.relation,
|
|
155
|
+
scope: this.scope,
|
|
156
|
+
expires: this.expires,
|
|
157
|
+
created: this.created,
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The version-1 payload bytes, kept only so links signed before `scope`
|
|
163
|
+
* and `expires` were covered still verify.
|
|
164
|
+
*
|
|
165
|
+
* A v1 signature is accepted **only** when the link carries no scope and
|
|
166
|
+
* no expiry — i.e. when there is nothing v1 failed to cover. A link that
|
|
167
|
+
* has either field must carry a v2 signature over it.
|
|
168
|
+
*
|
|
169
|
+
* @returns {Uint8Array}
|
|
170
|
+
*/
|
|
171
|
+
get legacySignedPayload() {
|
|
134
172
|
const str = `${this.parent}|${this.child}|${this.relation}|${this.created}`;
|
|
135
173
|
return new TextEncoder().encode(str);
|
|
136
174
|
}
|
|
137
175
|
|
|
176
|
+
/**
|
|
177
|
+
* True when a v1 signature may stand in for a v2 one: the link asserts
|
|
178
|
+
* nothing that the v1 payload left unsigned.
|
|
179
|
+
*
|
|
180
|
+
* @returns {boolean}
|
|
181
|
+
*/
|
|
182
|
+
get #legacyAcceptable() {
|
|
183
|
+
return this.scope === null && this.expires === null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Verify one signature against the canonical payload, falling back to the
|
|
188
|
+
* legacy payload only for links with nothing outside its coverage.
|
|
189
|
+
*
|
|
190
|
+
* @param {CryptoKey} publicKey
|
|
191
|
+
* @param {Uint8Array|null} signature
|
|
192
|
+
* @returns {Promise<boolean>}
|
|
193
|
+
*/
|
|
194
|
+
async #verifyOne(publicKey, signature) {
|
|
195
|
+
if (!signature) return false;
|
|
196
|
+
const current = await crypto.subtle.verify(
|
|
197
|
+
'Ed25519',
|
|
198
|
+
publicKey,
|
|
199
|
+
signature,
|
|
200
|
+
this.signedPayload
|
|
201
|
+
);
|
|
202
|
+
if (current) return true;
|
|
203
|
+
if (!this.#legacyAcceptable) return false;
|
|
204
|
+
return crypto.subtle.verify(
|
|
205
|
+
'Ed25519',
|
|
206
|
+
publicKey,
|
|
207
|
+
signature,
|
|
208
|
+
this.legacySignedPayload
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
138
212
|
/**
|
|
139
213
|
* Create a signed link between two identities.
|
|
140
214
|
* Both parent and child sign the canonical payload.
|
|
@@ -171,13 +245,7 @@ export class SignedKeyLink extends KeyLink {
|
|
|
171
245
|
* @returns {Promise<boolean>}
|
|
172
246
|
*/
|
|
173
247
|
async verifyParent(publicKey) {
|
|
174
|
-
|
|
175
|
-
return crypto.subtle.verify(
|
|
176
|
-
'Ed25519',
|
|
177
|
-
publicKey,
|
|
178
|
-
this.parentSignature,
|
|
179
|
-
this.signedPayload
|
|
180
|
-
);
|
|
248
|
+
return this.#verifyOne(publicKey, this.parentSignature);
|
|
181
249
|
}
|
|
182
250
|
|
|
183
251
|
/**
|
|
@@ -186,13 +254,7 @@ export class SignedKeyLink extends KeyLink {
|
|
|
186
254
|
* @returns {Promise<boolean>}
|
|
187
255
|
*/
|
|
188
256
|
async verifyChild(publicKey) {
|
|
189
|
-
|
|
190
|
-
return crypto.subtle.verify(
|
|
191
|
-
'Ed25519',
|
|
192
|
-
publicKey,
|
|
193
|
-
this.childSignature,
|
|
194
|
-
this.signedPayload
|
|
195
|
-
);
|
|
257
|
+
return this.#verifyOne(publicKey, this.childSignature);
|
|
196
258
|
}
|
|
197
259
|
|
|
198
260
|
/**
|
|
@@ -734,7 +796,23 @@ export class MeshKeyring {
|
|
|
734
796
|
static fromJSON(data) {
|
|
735
797
|
const kr = new MeshKeyring();
|
|
736
798
|
for (const d of data) {
|
|
737
|
-
|
|
799
|
+
/*
|
|
800
|
+
* Dispatch on the serialised shape. Reviving everything through
|
|
801
|
+
* `KeyLink.fromJSON` returned a plain KeyLink for signed links too, and
|
|
802
|
+
* `verifyCryptoChain` only checks a signature `if (link instanceof
|
|
803
|
+
* SignedKeyLink)` -- so every restored keyring reported `valid: true`
|
|
804
|
+
* having called `getPublicKey` zero times.
|
|
805
|
+
*
|
|
806
|
+
* The signatures were never lost; they survive serialisation intact.
|
|
807
|
+
* Only the CLASS was, and the class is what the check keys on. So a
|
|
808
|
+
* keyring that had been persisted, sent to a peer, or reloaded from
|
|
809
|
+
* disk verified without any cryptography at all.
|
|
810
|
+
*/
|
|
811
|
+
kr.#links.push(
|
|
812
|
+
d && (d.parentSignature || d.childSignature || d.signed)
|
|
813
|
+
? SignedKeyLink.fromJSON(d)
|
|
814
|
+
: KeyLink.fromJSON(d)
|
|
815
|
+
);
|
|
738
816
|
}
|
|
739
817
|
return kr;
|
|
740
818
|
}
|