@kumiai/mls 0.3.0 → 0.3.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/lib/group-handle.d.ts +19 -0
- package/lib/group-handle.js +78 -0
- package/lib/sender-data.d.ts +24 -0
- package/lib/sender-data.js +99 -0
- package/package.json +1 -1
package/lib/group-handle.d.ts
CHANGED
|
@@ -153,6 +153,25 @@ export declare class GroupHandle {
|
|
|
153
153
|
* `newGroup` and encrypt on that.
|
|
154
154
|
*/
|
|
155
155
|
encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
|
|
156
|
+
/**
|
|
157
|
+
* Read a Commit's MLS-authenticated committer against this handle, WITHOUT advancing
|
|
158
|
+
* state. `null` for bytes that are not a Commit.
|
|
159
|
+
*
|
|
160
|
+
* A member commit (PrivateMessage) has its committer encrypted under the epoch's
|
|
161
|
+
* sender-data secret: decrypt it to the sender leaf index and resolve that against this
|
|
162
|
+
* handle's ratchet tree — the same leaf->DID the commit policy sees as
|
|
163
|
+
* `didOfLeaf(senderLeafIndex)`. An external commit's committer rides its UpdatePath leaf
|
|
164
|
+
* instead (see {@link readExternalCommit}) — no tree lookup, no sender-data decrypt.
|
|
165
|
+
* `null` for anything that is neither.
|
|
166
|
+
*
|
|
167
|
+
* Runs on the handle mutex so the epoch secret, tree, and epoch are one snapshot against a
|
|
168
|
+
* concurrent processMessage. Non-mutating: sender-data decrypt is epoch-level and consumes
|
|
169
|
+
* no per-message key. Async only because the KDF and AEAD are.
|
|
170
|
+
*/
|
|
171
|
+
readCommitHeader(commit: Uint8Array): Promise<{
|
|
172
|
+
epoch: bigint;
|
|
173
|
+
committerDID: string;
|
|
174
|
+
} | null>;
|
|
156
175
|
/**
|
|
157
176
|
* Process a received MLS message (Commit, Proposal, or application). Accepts
|
|
158
177
|
* wire-form bytes (preferred, e.g. from commitInvite/removeMember) or a pre-decoded
|
package/lib/group-handle.js
CHANGED
|
@@ -4,11 +4,13 @@ import { readGroupAnchor } from './anchor.js';
|
|
|
4
4
|
import { parseMLSCredentialIdentity } from './credential.js';
|
|
5
5
|
import { decodeControlEnvelope } from './envelope.js';
|
|
6
6
|
import { foldEnvelope } from './envelope-fold.js';
|
|
7
|
+
import { readMessageEpoch } from './group-info.js';
|
|
7
8
|
import { assertHeadMatches, computeHead, decodeLedgerHead, encodeLedgerHead, extendHead, headsMatch, readLedgerHead, readLedgerHeadExtension } from './head.js';
|
|
8
9
|
import { ledgerEntryDigest, verifyLedgerEntry } from './ledger.js';
|
|
9
10
|
import { createMutex } from './mutex.js';
|
|
10
11
|
import { defaultCommitPolicy, MissingLedgerEntriesError } from './policy.js';
|
|
11
12
|
import { foldRoster } from './roster.js';
|
|
13
|
+
import { readSenderLeafIndex } from './sender-data.js';
|
|
12
14
|
/** One serializer per live handle, so its state-mutating operations run one at a
|
|
13
15
|
* time in issue order. Keyed weakly: the entry is collected with the handle, and
|
|
14
16
|
* the handle carries no reference back to it. */ const MUTEXES = new WeakMap();
|
|
@@ -74,6 +76,26 @@ export function mutexFor(handle) {
|
|
|
74
76
|
authenticatedData: data instanceof Uint8Array ? data : new Uint8Array()
|
|
75
77
|
};
|
|
76
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Narrow a decoded frame to the PrivateMessage-commit fields sender-data decrypt reads.
|
|
81
|
+
* Returns undefined for anything that is not a PrivateMessage of contentType commit.
|
|
82
|
+
*/ function readPrivateCommitFrame(decoded) {
|
|
83
|
+
if (decoded == null || typeof decoded !== 'object') return undefined;
|
|
84
|
+
const frame = decoded;
|
|
85
|
+
if (frame.wireformat !== wireformats.mls_private_message) return undefined;
|
|
86
|
+
const pm = frame.privateMessage;
|
|
87
|
+
if (pm == null || pm.contentType !== contentTypes.commit) return undefined;
|
|
88
|
+
if (!(pm.groupId instanceof Uint8Array) || typeof pm.epoch !== 'bigint' || !(pm.encryptedSenderData instanceof Uint8Array) || !(pm.ciphertext instanceof Uint8Array)) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
groupId: pm.groupId,
|
|
93
|
+
epoch: pm.epoch,
|
|
94
|
+
contentType: pm.contentType,
|
|
95
|
+
encryptedSenderData: pm.encryptedSenderData,
|
|
96
|
+
ciphertext: pm.ciphertext
|
|
97
|
+
};
|
|
98
|
+
}
|
|
77
99
|
/**
|
|
78
100
|
* Read the DID an external-join commit proves control of. An external join is a
|
|
79
101
|
* PublicMessage from a joining non-member (senderType new_member_commit) carrying a
|
|
@@ -540,6 +562,62 @@ export function mutexFor(handle) {
|
|
|
540
562
|
applyOnAccept
|
|
541
563
|
};
|
|
542
564
|
}
|
|
565
|
+
/** The DID at a ratchet-tree leaf index, or undefined if that leaf is empty/unparsable. */ #didOfLeaf(leafIndex) {
|
|
566
|
+
for (const member of this.#iterateMembers()){
|
|
567
|
+
if (member.leafIndex === leafIndex) return member.id;
|
|
568
|
+
}
|
|
569
|
+
return undefined;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Read a Commit's MLS-authenticated committer against this handle, WITHOUT advancing
|
|
573
|
+
* state. `null` for bytes that are not a Commit.
|
|
574
|
+
*
|
|
575
|
+
* A member commit (PrivateMessage) has its committer encrypted under the epoch's
|
|
576
|
+
* sender-data secret: decrypt it to the sender leaf index and resolve that against this
|
|
577
|
+
* handle's ratchet tree — the same leaf->DID the commit policy sees as
|
|
578
|
+
* `didOfLeaf(senderLeafIndex)`. An external commit's committer rides its UpdatePath leaf
|
|
579
|
+
* instead (see {@link readExternalCommit}) — no tree lookup, no sender-data decrypt.
|
|
580
|
+
* `null` for anything that is neither.
|
|
581
|
+
*
|
|
582
|
+
* Runs on the handle mutex so the epoch secret, tree, and epoch are one snapshot against a
|
|
583
|
+
* concurrent processMessage. Non-mutating: sender-data decrypt is epoch-level and consumes
|
|
584
|
+
* no per-message key. Async only because the KDF and AEAD are.
|
|
585
|
+
*/ async readCommitHeader(commit) {
|
|
586
|
+
return mutexFor(this).run(async ()=>{
|
|
587
|
+
let decoded;
|
|
588
|
+
try {
|
|
589
|
+
decoded = decode(mlsMessageDecoder, commit);
|
|
590
|
+
} catch {
|
|
591
|
+
// ts-mls `decode` throws CodecError on malformed/truncated frames; the contract is
|
|
592
|
+
// "null for bytes that are not a Commit", and the lane treats null as poison (advance
|
|
593
|
+
// the cursor, never retry). An unguarded throw would instead wedge the commit lane on
|
|
594
|
+
// this frame forever. Mirrors readMessageEpoch, which guards this same decode.
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
if (decoded == null) return null;
|
|
598
|
+
const epoch = readMessageEpoch(commit);
|
|
599
|
+
if (epoch == null) return null;
|
|
600
|
+
// External-join commit: the committer holds no pre-commit leaf, so its DID rides the
|
|
601
|
+
// commit's own UpdatePath leaf. No tree lookup, no sender-data decrypt.
|
|
602
|
+
const external = readExternalCommit(decoded);
|
|
603
|
+
if (external != null) {
|
|
604
|
+
return external.did == null ? null : {
|
|
605
|
+
epoch,
|
|
606
|
+
committerDID: external.did
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
const pm = readPrivateCommitFrame(decoded);
|
|
610
|
+
if (pm == null) return null // non-commit frame
|
|
611
|
+
;
|
|
612
|
+
const leafIndex = await readSenderLeafIndex(this.#context, this.#state.keySchedule.senderDataSecret, pm);
|
|
613
|
+
if (leafIndex == null) return null;
|
|
614
|
+
const committerDID = this.#didOfLeaf(leafIndex);
|
|
615
|
+
return committerDID == null ? null : {
|
|
616
|
+
epoch,
|
|
617
|
+
committerDID
|
|
618
|
+
};
|
|
619
|
+
});
|
|
620
|
+
}
|
|
543
621
|
/**
|
|
544
622
|
* Process a received MLS message (Commit, Proposal, or application). Accepts
|
|
545
623
|
* wire-form bytes (preferred, e.g. from commitInvite/removeMember) or a pre-decoded
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { MlsContext } from 'ts-mls';
|
|
2
|
+
/**
|
|
3
|
+
* PrivateMessage sender-data decrypt, reimplemented from RFC 9420 §6.3.2.
|
|
4
|
+
*
|
|
5
|
+
* ts-mls ships `decryptSenderData` but does not re-export it (its `exports` map exposes
|
|
6
|
+
* only `.`), so this package reproduces the derivation from primitives the live
|
|
7
|
+
* `CiphersuiteImpl` does expose. Frozen wire format — see the backlog note
|
|
8
|
+
* `ts-mls-v2-stable-upgrade.md`: delete this module and delegate to ts-mls once stable
|
|
9
|
+
* re-exports its own.
|
|
10
|
+
*/
|
|
11
|
+
/** The PrivateMessage fields sender-data decrypt reads, narrowed off a decoded frame. */
|
|
12
|
+
export type PrivateCommitFrame = {
|
|
13
|
+
groupId: Uint8Array;
|
|
14
|
+
epoch: bigint;
|
|
15
|
+
contentType: number;
|
|
16
|
+
encryptedSenderData: Uint8Array;
|
|
17
|
+
ciphertext: Uint8Array;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Decrypt a PrivateMessage's sender-data and return the committer's leaf index, or `null`
|
|
21
|
+
* if the AEAD refuses the bytes or the plaintext is malformed. Non-mutating: the
|
|
22
|
+
* sender-data secret is epoch-level and consumes no per-message ratchet key.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readSenderLeafIndex(context: MlsContext, senderDataSecret: Uint8Array, pm: PrivateCommitFrame): Promise<number | null>;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const LABEL_PREFIX = new TextEncoder().encode('MLS 1.0 ');
|
|
2
|
+
function concat(parts) {
|
|
3
|
+
let length = 0;
|
|
4
|
+
for (const part of parts)length += part.length;
|
|
5
|
+
const out = new Uint8Array(length);
|
|
6
|
+
let offset = 0;
|
|
7
|
+
for (const part of parts){
|
|
8
|
+
out.set(part, offset);
|
|
9
|
+
offset += part.length;
|
|
10
|
+
}
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
function uint16(n) {
|
|
14
|
+
return new Uint8Array([
|
|
15
|
+
n >> 8 & 0xff,
|
|
16
|
+
n & 0xff
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
function uint64(n) {
|
|
20
|
+
const out = new Uint8Array(8);
|
|
21
|
+
let value = n;
|
|
22
|
+
for(let i = 7; i >= 0; i--){
|
|
23
|
+
out[i] = Number(value & 0xffn);
|
|
24
|
+
value >>= 8n;
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
/** RFC 9420 opaque<V>: QUIC-style variable-length prefix (RFC 9000 §16), then the bytes. */ function varLen(data) {
|
|
29
|
+
const len = data.length;
|
|
30
|
+
let prefix;
|
|
31
|
+
if (len < 64) {
|
|
32
|
+
prefix = new Uint8Array([
|
|
33
|
+
len & 0x3f
|
|
34
|
+
]);
|
|
35
|
+
} else if (len < 16384) {
|
|
36
|
+
prefix = new Uint8Array([
|
|
37
|
+
len >> 8 & 0x3f | 0x40,
|
|
38
|
+
len & 0xff
|
|
39
|
+
]);
|
|
40
|
+
} else if (len < 0x40000000) {
|
|
41
|
+
prefix = new Uint8Array([
|
|
42
|
+
len >> 24 & 0x3f | 0x80,
|
|
43
|
+
len >> 16 & 0xff,
|
|
44
|
+
len >> 8 & 0xff,
|
|
45
|
+
len & 0xff
|
|
46
|
+
]);
|
|
47
|
+
} else {
|
|
48
|
+
throw new Error('sender-data: length too large to encode');
|
|
49
|
+
}
|
|
50
|
+
return concat([
|
|
51
|
+
prefix,
|
|
52
|
+
data
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* MLS `ExpandWithLabel(Secret, Label, Context, Length)`: `KDF.Expand` over the KDFLabel
|
|
57
|
+
* struct `{ uint16 length; opaque label<V> = "MLS 1.0 " + Label; opaque context<V> }`.
|
|
58
|
+
*/ function expandWithLabel(context, secret, label, labelContext, length) {
|
|
59
|
+
const kdfLabel = concat([
|
|
60
|
+
uint16(length),
|
|
61
|
+
varLen(concat([
|
|
62
|
+
LABEL_PREFIX,
|
|
63
|
+
new TextEncoder().encode(label)
|
|
64
|
+
])),
|
|
65
|
+
varLen(labelContext)
|
|
66
|
+
]);
|
|
67
|
+
return context.cipherSuite.kdf.expand(secret, kdfLabel, length);
|
|
68
|
+
}
|
|
69
|
+
/** The ciphertext sample the sender-data key/nonce derive from: the first `KDF.Nh` bytes. */ function sample(context, ciphertext) {
|
|
70
|
+
const size = context.cipherSuite.kdf.size;
|
|
71
|
+
return ciphertext.length < size ? ciphertext : ciphertext.subarray(0, size);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Decrypt a PrivateMessage's sender-data and return the committer's leaf index, or `null`
|
|
75
|
+
* if the AEAD refuses the bytes or the plaintext is malformed. Non-mutating: the
|
|
76
|
+
* sender-data secret is epoch-level and consumes no per-message ratchet key.
|
|
77
|
+
*/ export async function readSenderLeafIndex(context, senderDataSecret, pm) {
|
|
78
|
+
const { hpke } = context.cipherSuite;
|
|
79
|
+
const sampled = sample(context, pm.ciphertext);
|
|
80
|
+
const key = await expandWithLabel(context, senderDataSecret, 'key', sampled, hpke.keyLength);
|
|
81
|
+
const nonce = await expandWithLabel(context, senderDataSecret, 'nonce', sampled, hpke.nonceLength);
|
|
82
|
+
// SenderDataAAD = { opaque group_id<V>; uint64 epoch; ContentType content_type (uint8) }.
|
|
83
|
+
const aad = concat([
|
|
84
|
+
varLen(pm.groupId),
|
|
85
|
+
uint64(pm.epoch),
|
|
86
|
+
new Uint8Array([
|
|
87
|
+
pm.contentType & 0xff
|
|
88
|
+
])
|
|
89
|
+
]);
|
|
90
|
+
let plaintext;
|
|
91
|
+
try {
|
|
92
|
+
plaintext = await hpke.decryptAead(key, nonce, aad, pm.encryptedSenderData);
|
|
93
|
+
} catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
// SenderData = { uint32 leaf_index; uint32 generation; opaque reuse_guard[4] }.
|
|
97
|
+
if (plaintext.length < 4) return null;
|
|
98
|
+
return new DataView(plaintext.buffer, plaintext.byteOffset, plaintext.byteLength).getUint32(0);
|
|
99
|
+
}
|