@kumiai/mls 0.10.0 → 0.10.2
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/anchor.d.ts +10 -4
- package/lib/anchor.js +26 -2
- package/lib/authentication.d.ts +24 -12
- package/lib/authentication.js +192 -83
- package/lib/capability.d.ts +37 -0
- package/lib/capability.js +141 -0
- package/lib/credential.d.ts +7 -7
- package/lib/credential.js +15 -10
- package/lib/device-proof.d.ts +2 -0
- package/lib/device-proof.js +8 -12
- package/lib/envelope-fold.d.ts +16 -1
- package/lib/envelope-fold.js +49 -9
- package/lib/errors.d.ts +18 -0
- package/lib/errors.js +33 -0
- package/lib/group-commit.d.ts +24 -35
- package/lib/group-commit.js +139 -110
- package/lib/group-context.d.ts +6 -2
- package/lib/group-context.js +11 -4
- package/lib/group-create.js +56 -11
- package/lib/group-credential.d.ts +3 -1
- package/lib/group-credential.js +17 -3
- package/lib/group-device.js +34 -21
- package/lib/group-handle.d.ts +31 -8
- package/lib/group-handle.js +275 -58
- package/lib/group-lifecycle.d.ts +49 -0
- package/lib/group-lifecycle.js +341 -0
- package/lib/group-membership.js +7 -3
- package/lib/group-welcome.d.ts +11 -2
- package/lib/group-welcome.js +122 -22
- package/lib/group.d.ts +2 -1
- package/lib/group.js +1 -0
- package/lib/history.d.ts +6 -0
- package/lib/history.js +26 -0
- package/lib/index.d.ts +12 -5
- package/lib/index.js +10 -3
- package/lib/lifecycle-proof.d.ts +33 -0
- package/lib/lifecycle-proof.js +218 -0
- package/lib/lifecycle.d.ts +46 -0
- package/lib/lifecycle.js +410 -0
- package/lib/policy.d.ts +20 -0
- package/lib/policy.js +8 -5
- package/lib/recovery-verdict.d.ts +43 -0
- package/lib/recovery-verdict.js +135 -0
- package/lib/recovery.d.ts +70 -0
- package/lib/recovery.js +110 -36
- package/lib/registry.d.ts +34 -7
- package/lib/registry.js +113 -15
- package/lib/roster.d.ts +1 -1
- package/lib/roster.js +2 -2
- package/lib/secret-tree.d.ts +3 -0
- package/lib/secret-tree.js +60 -0
- package/lib/types.d.ts +13 -8
- package/package.json +5 -5
package/lib/anchor.d.ts
CHANGED
|
@@ -20,12 +20,18 @@ export declare const LEDGER_HEAD_EXTENSION_TYPE = 61697;
|
|
|
20
20
|
*/
|
|
21
21
|
export declare const RESERVED_EXTENSION_TYPE = 61698;
|
|
22
22
|
/**
|
|
23
|
-
* Genesis anchor baked into the MLS GroupContext at creation: the creator DID is
|
|
24
|
-
* admin. Survives every epoch, authenticated by the GroupInfo signature, treated as immutable.
|
|
23
|
+
* Genesis anchor baked into the MLS GroupContext at creation: the controller or creator DID is
|
|
24
|
+
* the epoch-0 admin. Survives every epoch, authenticated by the GroupInfo signature, treated as immutable.
|
|
25
25
|
*/
|
|
26
26
|
export type GroupAnchor = {
|
|
27
27
|
creatorDID: string;
|
|
28
28
|
version: number;
|
|
29
|
+
/** Immutable lifecycle controller. All peers must understand the same release's lifecycle rules. */
|
|
30
|
+
controller?: string;
|
|
31
|
+
/** Seconds, default 86,400, maximum 604,800. */
|
|
32
|
+
leafLifetime?: number;
|
|
33
|
+
/** Seconds, default 2,592,000, maximum 31,536,000. */
|
|
34
|
+
trustedGrantLifetime?: number;
|
|
29
35
|
/**
|
|
30
36
|
* Opaque consumer payload written once at creation; `@kumiai/mls` never reads or interprets
|
|
31
37
|
* it. A consumer holding raw bytes must JSON-safe-encode them (e.g. base64) itself. Kubun
|
|
@@ -44,8 +50,8 @@ export declare function encodeGroupAnchor(anchor: GroupAnchor): Uint8Array;
|
|
|
44
50
|
* this build has never seen, and a v1 consumer reading it as v1 (kubun keeps its recovery seed in
|
|
45
51
|
* `app`) cannot tell. `version` is preserved so a consumer distinguishes "future version, app
|
|
46
52
|
* withheld" from "genuinely no app". Contract this rests on: a `version` bump means `app` semantics
|
|
47
|
-
* changed and nothing else
|
|
48
|
-
*
|
|
53
|
+
* changed and nothing else. Lifecycle fields are validated regardless of version, and lifecycle
|
|
54
|
+
* peers must run a release that understands them.
|
|
49
55
|
*/
|
|
50
56
|
export declare function decodeGroupAnchor(bytes: Uint8Array): GroupAnchor | null;
|
|
51
57
|
export declare function buildGroupAnchorExtension(anchor: GroupAnchor): GroupContextExtension;
|
package/lib/anchor.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { defaultCapabilities, makeCustomExtension } from 'ts-mls';
|
|
2
|
+
import { MAX_LEAF_LIFETIME, MAX_TRUSTED_GRANT_LIFETIME } from './capability.js';
|
|
2
3
|
/**
|
|
3
4
|
* MLS GroupContext extension type for the genesis anchor. A uint16 outside the MLS default
|
|
4
5
|
* range (1–5) and clear of GREASE values, so it never collides with a built-in or a probe.
|
|
@@ -31,8 +32,8 @@ export function encodeGroupAnchor(anchor) {
|
|
|
31
32
|
* this build has never seen, and a v1 consumer reading it as v1 (kubun keeps its recovery seed in
|
|
32
33
|
* `app`) cannot tell. `version` is preserved so a consumer distinguishes "future version, app
|
|
33
34
|
* withheld" from "genuinely no app". Contract this rests on: a `version` bump means `app` semantics
|
|
34
|
-
* changed and nothing else
|
|
35
|
-
*
|
|
35
|
+
* changed and nothing else. Lifecycle fields are validated regardless of version, and lifecycle
|
|
36
|
+
* peers must run a release that understands them.
|
|
36
37
|
*/ export function decodeGroupAnchor(bytes) {
|
|
37
38
|
let parsed;
|
|
38
39
|
try {
|
|
@@ -47,10 +48,33 @@ export function encodeGroupAnchor(anchor) {
|
|
|
47
48
|
if (typeof record.creatorDID !== 'string' || typeof record.version !== 'number') {
|
|
48
49
|
return null;
|
|
49
50
|
}
|
|
51
|
+
if ('controller' in record && (typeof record.controller !== 'string' || !record.controller.startsWith('did:kokuin:'))) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
for (const [field, maximum] of [
|
|
55
|
+
[
|
|
56
|
+
'leafLifetime',
|
|
57
|
+
MAX_LEAF_LIFETIME
|
|
58
|
+
],
|
|
59
|
+
[
|
|
60
|
+
'trustedGrantLifetime',
|
|
61
|
+
MAX_TRUSTED_GRANT_LIFETIME
|
|
62
|
+
]
|
|
63
|
+
]){
|
|
64
|
+
if (field in record) {
|
|
65
|
+
const value = record[field];
|
|
66
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value < 1 || value > maximum) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
50
71
|
const anchor = {
|
|
51
72
|
creatorDID: record.creatorDID,
|
|
52
73
|
version: record.version
|
|
53
74
|
};
|
|
75
|
+
if (typeof record.controller === 'string') anchor.controller = record.controller;
|
|
76
|
+
if (typeof record.leafLifetime === 'number') anchor.leafLifetime = record.leafLifetime;
|
|
77
|
+
if (typeof record.trustedGrantLifetime === 'number') anchor.trustedGrantLifetime = record.trustedGrantLifetime;
|
|
54
78
|
// Withhold the app payload from a future build's anchor: a version this build
|
|
55
79
|
// has never seen may carry a payload with v2 semantics, and handing it to a
|
|
56
80
|
// consumer under v1 expectations is exactly the silent misread this guards.
|
package/lib/authentication.d.ts
CHANGED
|
@@ -1,19 +1,33 @@
|
|
|
1
1
|
import { type SignedEvent } from '@kokuin/controller';
|
|
2
|
-
import type { AuthenticationService } from 'ts-mls';
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
/** The resource half of that grant — kumiai-namespaced, group-independent. */
|
|
6
|
-
export declare const MLS_LEAF_RES = "kumiai/mls-leaf";
|
|
2
|
+
import type { AuthenticationService, Credential } from 'ts-mls';
|
|
3
|
+
import { type MLSCredentialIdentity } from './credential.js';
|
|
4
|
+
export { MLS_LEAF_ACT, MLS_LEAF_RES } from './capability.js';
|
|
7
5
|
/** The action a management capability must grant to mutate a profile's device registry. */
|
|
8
6
|
export declare const MLS_DEVICES_ACT = "manage";
|
|
9
7
|
/** The resource half — kumiai-namespaced, group-independent, per the kokuin management tier. */
|
|
10
8
|
export declare const MLS_DEVICES_RES = "kumiai/devices";
|
|
9
|
+
export type DIDAuthenticationDependencies = {
|
|
10
|
+
deviceDenySet?: () => ReadonlySet<string>;
|
|
11
|
+
leafLifetime?: () => number | undefined;
|
|
12
|
+
trustedGrantLifetime?: () => number | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Whether this leaf may keep an issuer the deny set names, on its holder evidence. The caller
|
|
15
|
+
* answers for the group (its anchor names a controller) and for the leaf (it is the credential
|
|
16
|
+
* already in the tree at that index, or it arrives in a Welcome tree). Absent: never.
|
|
17
|
+
*/
|
|
18
|
+
mayKeepDeniedIssuer?: (credential: Credential, signaturePublicKey: Uint8Array) => boolean;
|
|
19
|
+
};
|
|
20
|
+
/** Structural evidence gate also used before MLS invokes its boolean authentication adapter. */
|
|
21
|
+
export declare function checkHolderEvidence(parsed: MLSCredentialIdentity, signaturePublicKey: Uint8Array, trustedGrantLifetime?: number): string | undefined;
|
|
11
22
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* issuer's own bound leaf), never fetched. Returns false on any failure.
|
|
23
|
+
* A chained leaf carrying its holder's own valid controller grant, for a holder the deny set does
|
|
24
|
+
* not name. Such a leaf names a device the controller bound itself, so denying the device that
|
|
25
|
+
* issued its capability does not take that binding away.
|
|
16
26
|
*/
|
|
27
|
+
export declare function hasHolderEvidence(parsed: MLSCredentialIdentity, signaturePublicKey: Uint8Array, denied: ReadonlySet<string>, trustedGrantLifetime?: number): boolean;
|
|
28
|
+
/** Internal throwing boundary for entry gates; ts-mls consumes the boolean adapter below. */
|
|
29
|
+
export declare function verifyLeafCredential(credential: Credential, signaturePublicKey: Uint8Array, deps?: DIDAuthenticationDependencies): Promise<void>;
|
|
30
|
+
/** Management grants are direct controller grants, verified at their issuance time. */
|
|
17
31
|
export declare function verifyManagementCapability(params: {
|
|
18
32
|
capability?: string;
|
|
19
33
|
prefix: Array<SignedEvent>;
|
|
@@ -21,6 +35,4 @@ export declare function verifyManagementCapability(params: {
|
|
|
21
35
|
audience: string;
|
|
22
36
|
leafKey: Uint8Array;
|
|
23
37
|
}): Promise<boolean>;
|
|
24
|
-
export declare function createDIDAuthenticationService(deps?:
|
|
25
|
-
deviceDenySet?: () => ReadonlySet<string>;
|
|
26
|
-
}): AuthenticationService;
|
|
38
|
+
export declare function createDIDAuthenticationService(deps?: DIDAuthenticationDependencies): AuthenticationService;
|
package/lib/authentication.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { assertCapabilityToken,
|
|
2
|
-
import {
|
|
3
|
-
import { decodeMultibase, decodePeer4, getAlgorithmAndPublicKey, getSignatureInfo, isPeer4, normalizeDID, verifyToken } from '@kokuin/token';
|
|
1
|
+
import { assertCapabilityToken, assertValidDelegation, checkCapability, hasPermission } from '@kokuin/capability';
|
|
2
|
+
import { foldLog } from '@kokuin/controller';
|
|
3
|
+
import { decodeMultibase, decodePeer4, getAlgorithmAndPublicKey, getSignatureInfo, getVerifier, isPeer4, normalizeDID, verifyToken } from '@kokuin/token';
|
|
4
|
+
import { fromB64U, fromUTF } from '@sozai/codec';
|
|
4
5
|
import { defaultCredentialTypes } from 'ts-mls';
|
|
6
|
+
import { assertCapabilityLifetime, assertControllerGrant, capabilityKey, MAX_LEAF_LIFETIME, MAX_TRUSTED_GRANT_LIFETIME, MLS_LEAF_ACT, MLS_LEAF_RES, readCapability } from './capability.js';
|
|
5
7
|
import { parseMLSCredentialIdentity } from './credential.js';
|
|
6
8
|
import { createEmbeddedControllerResolver } from './embedded-resolver.js';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
import { LeafBindingError } from './errors.js';
|
|
10
|
+
export { MLS_LEAF_ACT, MLS_LEAF_RES } from './capability.js';
|
|
9
11
|
/** The action a management capability must grant to mutate a profile's device registry. */ export const MLS_DEVICES_ACT = 'manage';
|
|
10
12
|
/** The resource half — kumiai-namespaced, group-independent, per the kokuin management tier. */ export const MLS_DEVICES_RES = 'kumiai/devices';
|
|
11
13
|
const EMPTY_DENY = new Set();
|
|
@@ -65,109 +67,216 @@ function constantTimeEqual(a, b) {
|
|
|
65
67
|
return false;
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
function isDenied(denied, did) {
|
|
71
|
+
return denied.has(did) || denied.has(normalizeDID(did));
|
|
72
|
+
}
|
|
73
|
+
/** Authenticate only embedded proof, using the token's issuance as its reference time. */ async function verifyPinnedCapability(params) {
|
|
74
|
+
if (!params.controllerID.startsWith('did:kokuin:') || !foldLog(params.controllerID, params.prefix).ok) {
|
|
75
|
+
throw new LeafBindingError('controller-mismatch');
|
|
76
|
+
}
|
|
77
|
+
const token = readCapability(params.capability);
|
|
78
|
+
const payload = token.payload;
|
|
79
|
+
const ceiling = params.trustedGrant ? MAX_TRUSTED_GRANT_LIFETIME : MAX_LEAF_LIFETIME;
|
|
80
|
+
const lifetime = params.trustedGrant ? params.trustedGrantLifetime : params.leafLifetime;
|
|
81
|
+
assertCapabilityLifetime(payload, Math.min(lifetime ?? ceiling, ceiling));
|
|
82
|
+
if (normalizeDID(payload.sub) !== normalizeDID(params.controllerID)) {
|
|
83
|
+
throw new LeafBindingError('subject-mismatch');
|
|
84
|
+
}
|
|
85
|
+
if (normalizeDID(payload.aud) !== normalizeDID(params.audience)) {
|
|
86
|
+
throw new LeafBindingError('audience-mismatch');
|
|
87
|
+
}
|
|
88
|
+
if (!hasPermission(params.permission, payload)) throw new LeafBindingError('permission-denied');
|
|
89
|
+
const leafKey = capabilityKey(payload);
|
|
90
|
+
if (!constantTimeEqual(leafKey.publicKey, params.leafKey)) {
|
|
91
|
+
throw new LeafBindingError('confirmation-invalid');
|
|
92
|
+
}
|
|
93
|
+
const denySet = params.denySet ?? EMPTY_DENY;
|
|
75
94
|
const resolver = createEmbeddedControllerResolver({
|
|
76
95
|
controllerID: params.controllerID,
|
|
77
|
-
prefix: params.prefix
|
|
96
|
+
prefix: params.prefix,
|
|
97
|
+
denySet
|
|
78
98
|
});
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
verified = await verifyToken(params.capability, {
|
|
99
|
+
if (payload.cap === undefined) {
|
|
100
|
+
assertControllerGrant(payload, params.controllerID);
|
|
101
|
+
const verified = await verifyToken(params.capability, {
|
|
82
102
|
methods: [
|
|
83
103
|
resolver
|
|
84
104
|
],
|
|
85
105
|
historic: true,
|
|
106
|
+
atTime: payload.iat,
|
|
86
107
|
allowUnsigned: false
|
|
87
108
|
});
|
|
88
|
-
} catch {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
try {
|
|
92
109
|
assertCapabilityToken(verified);
|
|
93
|
-
|
|
94
|
-
if (normalizeDID(verified.payload.aud) !== normalizeDID(params.audience)) return false;
|
|
95
|
-
if (!hasPermission(params.permission, verified.payload)) return false;
|
|
96
|
-
assertValidIssuedAt(verified.payload);
|
|
97
|
-
assertDeviceCapabilityPolicy(verified.payload);
|
|
98
|
-
} catch {
|
|
99
|
-
return false;
|
|
110
|
+
return;
|
|
100
111
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
112
|
+
if (params.directOnly) throw new LeafBindingError('chain-depth');
|
|
113
|
+
const parents = Array.isArray(payload.cap) ? payload.cap : [
|
|
114
|
+
payload.cap
|
|
115
|
+
];
|
|
116
|
+
const parentRaw = parents[0];
|
|
117
|
+
if (parents.length !== 1 || parentRaw == null) throw new LeafBindingError('chain-depth');
|
|
118
|
+
const parent = readCapability(parentRaw);
|
|
119
|
+
if (parent.payload.cap !== undefined) throw new LeafBindingError('chain-depth');
|
|
120
|
+
assertControllerGrant(parent.payload, params.controllerID);
|
|
121
|
+
assertCapabilityLifetime(parent.payload, Math.min(params.trustedGrantLifetime ?? MAX_TRUSTED_GRANT_LIFETIME, MAX_TRUSTED_GRANT_LIFETIME));
|
|
122
|
+
if (normalizeDID(payload.iss) !== normalizeDID(parent.payload.aud)) {
|
|
123
|
+
throw new LeafBindingError('issuer-mismatch');
|
|
124
|
+
}
|
|
125
|
+
if (normalizeDID(payload.aud) === normalizeDID(payload.iss)) {
|
|
126
|
+
throw new LeafBindingError('self-issued');
|
|
127
|
+
}
|
|
128
|
+
if (payload.exp > parent.payload.exp) throw new LeafBindingError('child-outlives-parent');
|
|
129
|
+
if (isDenied(denySet, payload.iss)) throw new LeafBindingError('denied-issuer');
|
|
130
|
+
const issuerKey = capabilityKey(parent.payload);
|
|
131
|
+
if (token.header.alg !== issuerKey.alg) throw new LeafBindingError('signature-invalid');
|
|
132
|
+
if (!await getVerifier(token.header.alg)(fromB64U(token.signature), fromUTF(token.data), issuerKey.publicKey)) {
|
|
133
|
+
throw new LeafBindingError('signature-invalid');
|
|
134
|
+
}
|
|
135
|
+
// checkCapability treats iss === sub as a root grant, so it does not walk that parent.
|
|
136
|
+
if (normalizeDID(payload.iss) === normalizeDID(payload.sub)) {
|
|
137
|
+
const verifiedParent = await verifyToken(parentRaw, {
|
|
138
|
+
atTime: payload.iat,
|
|
139
|
+
methods: [
|
|
140
|
+
resolver
|
|
141
|
+
],
|
|
142
|
+
historic: true,
|
|
143
|
+
allowUnsigned: false
|
|
144
|
+
});
|
|
145
|
+
assertCapabilityToken(verifiedParent);
|
|
146
|
+
assertValidDelegation(verifiedParent.payload, payload, payload.iat);
|
|
147
|
+
}
|
|
148
|
+
await checkCapability(params.permission, payload, {
|
|
149
|
+
atTime: payload.iat,
|
|
150
|
+
methods: [
|
|
151
|
+
resolver
|
|
152
|
+
],
|
|
153
|
+
maxDepth: 1
|
|
154
|
+
});
|
|
105
155
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
permission: {
|
|
156
|
+
function withoutIssuer(denied, capability) {
|
|
157
|
+
const issuer = normalizeDID(readCapability(capability).payload.iss);
|
|
158
|
+
return new Set([
|
|
159
|
+
...denied
|
|
160
|
+
].filter((did)=>normalizeDID(did) !== issuer));
|
|
161
|
+
}
|
|
162
|
+
/** Structural evidence gate also used before MLS invokes its boolean authentication adapter. */ export function checkHolderEvidence(parsed, signaturePublicKey, trustedGrantLifetime = MAX_TRUSTED_GRANT_LIFETIME) {
|
|
163
|
+
const binding = parsed.controller;
|
|
164
|
+
if (binding == null) return;
|
|
165
|
+
const leafPayload = readCapability(binding.capability).payload;
|
|
166
|
+
if (leafPayload.cap === undefined) return;
|
|
167
|
+
if (binding.holderGrant == null) throw new LeafBindingError('missing-holder-evidence');
|
|
168
|
+
try {
|
|
169
|
+
const grant = readCapability(binding.holderGrant).payload;
|
|
170
|
+
assertControllerGrant(grant, binding.id);
|
|
171
|
+
assertCapabilityLifetime(grant, Math.min(trustedGrantLifetime, MAX_TRUSTED_GRANT_LIFETIME));
|
|
172
|
+
if (grant.cap !== undefined || grant.iat > leafPayload.iat || grant.exp <= leafPayload.iat || grant.nbf != null && grant.nbf > leafPayload.iat || normalizeDID(grant.aud) !== normalizeDID(parsed.id) || !constantTimeEqual(capabilityKey(grant).publicKey, signaturePublicKey) || !hasPermission({
|
|
124
173
|
act: MLS_LEAF_ACT,
|
|
125
174
|
res: MLS_LEAF_RES
|
|
126
|
-
},
|
|
127
|
-
|
|
128
|
-
|
|
175
|
+
}, grant)) throw new LeafBindingError('holder-evidence-mismatch');
|
|
176
|
+
} catch (cause) {
|
|
177
|
+
const error = new LeafBindingError('holder-evidence-mismatch');
|
|
178
|
+
error.cause = cause;
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
return binding.holderGrant;
|
|
129
182
|
}
|
|
130
183
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
|
|
135
|
-
|
|
184
|
+
* A chained leaf carrying its holder's own valid controller grant, for a holder the deny set does
|
|
185
|
+
* not name. Such a leaf names a device the controller bound itself, so denying the device that
|
|
186
|
+
* issued its capability does not take that binding away.
|
|
187
|
+
*/ export function hasHolderEvidence(parsed, signaturePublicKey, denied, trustedGrantLifetime) {
|
|
188
|
+
const binding = parsed.controller;
|
|
189
|
+
if (binding == null || isDenied(denied, parsed.id)) return false;
|
|
190
|
+
try {
|
|
191
|
+
if (readCapability(binding.capability).payload.cap === undefined) return false;
|
|
192
|
+
return checkHolderEvidence(parsed, signaturePublicKey, trustedGrantLifetime) != null;
|
|
193
|
+
} catch {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/** Internal throwing boundary for entry gates; ts-mls consumes the boolean adapter below. */ export async function verifyLeafCredential(credential, signaturePublicKey, deps = {}) {
|
|
198
|
+
if (credential.credentialType !== defaultCredentialTypes.basic) {
|
|
199
|
+
throw new Error('Unsupported MLS credential type');
|
|
200
|
+
}
|
|
201
|
+
const parsed = parseMLSCredentialIdentity(credential.identity);
|
|
202
|
+
if (!matchesLeafKey(parsed, signaturePublicKey)) throw new LeafBindingError('identity-change');
|
|
203
|
+
const denySet = deps.deviceDenySet?.() ?? EMPTY_DENY;
|
|
204
|
+
if (isDenied(denySet, parsed.id)) throw new LeafBindingError('denied-id');
|
|
205
|
+
if (parsed.controller == null) return;
|
|
206
|
+
try {
|
|
207
|
+
// The holder grant below is still verified against the full deny set.
|
|
208
|
+
const leafDenySet = deps.mayKeepDeniedIssuer?.(credential, signaturePublicKey) && hasHolderEvidence(parsed, signaturePublicKey, denySet, deps.trustedGrantLifetime?.()) ? withoutIssuer(denySet, parsed.controller.capability) : denySet;
|
|
209
|
+
await verifyPinnedCapability({
|
|
210
|
+
capability: parsed.controller.capability,
|
|
211
|
+
prefix: parsed.controller.prefix,
|
|
212
|
+
controllerID: parsed.controller.id,
|
|
213
|
+
audience: parsed.id,
|
|
214
|
+
permission: {
|
|
215
|
+
act: MLS_LEAF_ACT,
|
|
216
|
+
res: MLS_LEAF_RES
|
|
217
|
+
},
|
|
218
|
+
leafKey: signaturePublicKey,
|
|
219
|
+
denySet: leafDenySet,
|
|
220
|
+
leafLifetime: deps.leafLifetime?.(),
|
|
221
|
+
trustedGrantLifetime: deps.trustedGrantLifetime?.()
|
|
222
|
+
});
|
|
223
|
+
const holderGrant = checkHolderEvidence(parsed, signaturePublicKey, deps.trustedGrantLifetime?.());
|
|
224
|
+
if (holderGrant != null) {
|
|
225
|
+
try {
|
|
226
|
+
await verifyPinnedCapability({
|
|
227
|
+
capability: holderGrant,
|
|
228
|
+
prefix: parsed.controller.prefix,
|
|
229
|
+
controllerID: parsed.controller.id,
|
|
230
|
+
audience: parsed.id,
|
|
231
|
+
permission: {
|
|
232
|
+
act: MLS_LEAF_ACT,
|
|
233
|
+
res: MLS_LEAF_RES
|
|
234
|
+
},
|
|
235
|
+
leafKey: signaturePublicKey,
|
|
236
|
+
denySet,
|
|
237
|
+
trustedGrantLifetime: deps.trustedGrantLifetime?.(),
|
|
238
|
+
directOnly: true,
|
|
239
|
+
trustedGrant: true
|
|
240
|
+
});
|
|
241
|
+
} catch (cause) {
|
|
242
|
+
const error = new LeafBindingError('holder-evidence-mismatch');
|
|
243
|
+
error.cause = cause;
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (error instanceof LeafBindingError) throw error;
|
|
249
|
+
const rejection = new LeafBindingError('signature-invalid');
|
|
250
|
+
rejection.cause = error;
|
|
251
|
+
throw rejection;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Management grants are direct controller grants, verified at their issuance time. */ export async function verifyManagementCapability(params) {
|
|
136
255
|
if (params.capability == null) return false;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
256
|
+
try {
|
|
257
|
+
await verifyPinnedCapability({
|
|
258
|
+
...params,
|
|
259
|
+
capability: params.capability,
|
|
260
|
+
permission: {
|
|
261
|
+
act: MLS_DEVICES_ACT,
|
|
262
|
+
res: MLS_DEVICES_RES
|
|
263
|
+
},
|
|
264
|
+
directOnly: true
|
|
265
|
+
});
|
|
266
|
+
return true;
|
|
267
|
+
} catch {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
149
270
|
}
|
|
150
271
|
export function createDIDAuthenticationService(deps = {}) {
|
|
151
|
-
const deviceDenySet = deps.deviceDenySet ?? (()=>EMPTY_DENY);
|
|
152
272
|
return {
|
|
153
273
|
async validateCredential (credential, signaturePublicKey) {
|
|
154
|
-
if (credential.credentialType !== defaultCredentialTypes.basic) {
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
let parsed;
|
|
158
274
|
try {
|
|
159
|
-
|
|
275
|
+
await verifyLeafCredential(credential, signaturePublicKey, deps);
|
|
276
|
+
return true;
|
|
160
277
|
} catch {
|
|
161
278
|
return false;
|
|
162
279
|
}
|
|
163
|
-
if (parsed.controller != null) {
|
|
164
|
-
try {
|
|
165
|
-
return await validateBoundLeaf(parsed, signaturePublicKey, deviceDenySet);
|
|
166
|
-
} catch {
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
return matchesLeafKey(parsed, signaturePublicKey);
|
|
171
280
|
}
|
|
172
281
|
};
|
|
173
282
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type CapabilityPayload } from '@kokuin/capability';
|
|
2
|
+
import { type SignedToken, type SigningIdentity } from '@kokuin/token';
|
|
3
|
+
/** The action a device capability must grant for a leaf to authenticate as a kumiai MLS leaf. */
|
|
4
|
+
export declare const MLS_LEAF_ACT = "authenticate";
|
|
5
|
+
/** The resource half of that grant — kumiai-namespaced, group-independent. */
|
|
6
|
+
export declare const MLS_LEAF_RES = "kumiai/mls-leaf";
|
|
7
|
+
export declare const MAX_LEAF_LIFETIME = 604800;
|
|
8
|
+
export declare const MAX_TRUSTED_GRANT_LIFETIME = 31536000;
|
|
9
|
+
type LeafCapabilityPayload = CapabilityPayload & {
|
|
10
|
+
iat: number;
|
|
11
|
+
exp: number;
|
|
12
|
+
nbf?: number;
|
|
13
|
+
cap?: string | Array<string>;
|
|
14
|
+
};
|
|
15
|
+
/** Decode only; callers must authenticate the signed bytes before trusting these claims. */
|
|
16
|
+
export declare function readCapability(raw: string): SignedToken<LeafCapabilityPayload>;
|
|
17
|
+
/** Unvalidated time claims, as read from a decoded token. */
|
|
18
|
+
export type CapabilityTimeClaims = {
|
|
19
|
+
iat?: unknown;
|
|
20
|
+
exp?: unknown;
|
|
21
|
+
nbf?: unknown;
|
|
22
|
+
};
|
|
23
|
+
export declare function assertCapabilityLifetime(payload: CapabilityTimeClaims, maximum: number): void;
|
|
24
|
+
export declare function capabilityKey(payload: CapabilityPayload): import("@kokuin/controller").TaggedKey;
|
|
25
|
+
export declare function assertControllerGrant(payload: CapabilityPayload, controllerID: string): void;
|
|
26
|
+
export type MintLeafCapabilityParams = {
|
|
27
|
+
signer: SigningIdentity;
|
|
28
|
+
controllerID: string;
|
|
29
|
+
audience: string;
|
|
30
|
+
leafKey: Uint8Array;
|
|
31
|
+
exp: number;
|
|
32
|
+
parent?: string;
|
|
33
|
+
};
|
|
34
|
+
export type MintTrustedGrantParams = Omit<MintLeafCapabilityParams, 'parent'>;
|
|
35
|
+
export declare function mintLeafCapability(params: MintLeafCapabilityParams): Promise<string>;
|
|
36
|
+
export declare function mintTrustedGrant(params: MintTrustedGrantParams): Promise<string>;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { audienceConfirmation, hasPermission } from '@kokuin/capability';
|
|
2
|
+
import { tryDecodeKey } from '@kokuin/controller';
|
|
3
|
+
import { isSignedToken, normalizeDID, now, stringifyToken } from '@kokuin/token';
|
|
4
|
+
import { b64uToJSON } from '@sozai/codec';
|
|
5
|
+
import { LeafBindingError } from './errors.js';
|
|
6
|
+
/** The action a device capability must grant for a leaf to authenticate as a kumiai MLS leaf. */ export const MLS_LEAF_ACT = 'authenticate';
|
|
7
|
+
/** The resource half of that grant — kumiai-namespaced, group-independent. */ export const MLS_LEAF_RES = 'kumiai/mls-leaf';
|
|
8
|
+
export const MAX_LEAF_LIFETIME = 604800;
|
|
9
|
+
export const MAX_TRUSTED_GRANT_LIFETIME = 31536000;
|
|
10
|
+
/** Decode only; callers must authenticate the signed bytes before trusting these claims. */ export function readCapability(raw) {
|
|
11
|
+
const parts = raw.split('.');
|
|
12
|
+
const [header, payload, signature] = parts;
|
|
13
|
+
if (parts.length !== 3 || !header || !payload || !signature) {
|
|
14
|
+
throw new Error('Invalid capability encoding');
|
|
15
|
+
}
|
|
16
|
+
const token = {
|
|
17
|
+
header: b64uToJSON(header),
|
|
18
|
+
payload: b64uToJSON(payload),
|
|
19
|
+
signature,
|
|
20
|
+
data: `${header}.${payload}`
|
|
21
|
+
};
|
|
22
|
+
if (!isSignedToken(token)) throw new Error('Invalid signed capability');
|
|
23
|
+
const claims = token.payload;
|
|
24
|
+
if (typeof claims.sub !== 'string' || typeof claims.aud !== 'string') {
|
|
25
|
+
throw new Error('Invalid capability subject or audience');
|
|
26
|
+
}
|
|
27
|
+
for (const part of [
|
|
28
|
+
claims.act,
|
|
29
|
+
claims.res
|
|
30
|
+
]){
|
|
31
|
+
if (typeof part !== 'string' && !(Array.isArray(part) && part.every((value)=>typeof value === 'string'))) {
|
|
32
|
+
throw new Error('Invalid capability permission');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return token;
|
|
36
|
+
}
|
|
37
|
+
export function assertCapabilityLifetime(payload, maximum) {
|
|
38
|
+
const { iat, exp, nbf } = payload;
|
|
39
|
+
if (typeof iat !== 'number' || !Number.isFinite(iat) || typeof exp !== 'number' || !Number.isFinite(exp) || exp <= iat || exp - iat > maximum || !Number.isFinite(maximum) || maximum <= 0) {
|
|
40
|
+
throw new LeafBindingError('lifetime-cap');
|
|
41
|
+
}
|
|
42
|
+
if (nbf !== undefined && (typeof nbf !== 'number' || !Number.isFinite(nbf))) {
|
|
43
|
+
throw new Error('Invalid capability not-before claim');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function capabilityKey(payload) {
|
|
47
|
+
const cnf = payload.cnf;
|
|
48
|
+
if (cnf == null || typeof cnf !== 'object' || Object.keys(cnf).length !== 1 || typeof cnf.kid !== 'string') {
|
|
49
|
+
throw new LeafBindingError('confirmation-invalid');
|
|
50
|
+
}
|
|
51
|
+
const key = tryDecodeKey(cnf.kid);
|
|
52
|
+
if (key == null || key.alg !== 'EdDSA') throw new Error('Invalid capability confirmation key');
|
|
53
|
+
return key;
|
|
54
|
+
}
|
|
55
|
+
export function assertControllerGrant(payload, controllerID) {
|
|
56
|
+
if (normalizeDID(payload.iss) !== normalizeDID(controllerID)) {
|
|
57
|
+
throw new LeafBindingError('issuer-mismatch');
|
|
58
|
+
}
|
|
59
|
+
if (normalizeDID(payload.sub) !== normalizeDID(controllerID)) {
|
|
60
|
+
throw new LeafBindingError('subject-mismatch');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function issuingPayload(params, maximum) {
|
|
64
|
+
if (!params.controllerID.startsWith('did:kokuin:')) {
|
|
65
|
+
throw new LeafBindingError('controller-mismatch');
|
|
66
|
+
}
|
|
67
|
+
const iat = now();
|
|
68
|
+
assertCapabilityLifetime({
|
|
69
|
+
iat,
|
|
70
|
+
exp: params.exp
|
|
71
|
+
}, maximum);
|
|
72
|
+
const cnf = audienceConfirmation({
|
|
73
|
+
alg: 'EdDSA',
|
|
74
|
+
publicKey: params.leafKey
|
|
75
|
+
});
|
|
76
|
+
capabilityKey({
|
|
77
|
+
iss: params.signer.id,
|
|
78
|
+
sub: params.controllerID,
|
|
79
|
+
aud: params.audience,
|
|
80
|
+
act: MLS_LEAF_ACT,
|
|
81
|
+
res: MLS_LEAF_RES,
|
|
82
|
+
cnf
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
sub: params.controllerID,
|
|
86
|
+
aud: params.audience,
|
|
87
|
+
act: MLS_LEAF_ACT,
|
|
88
|
+
res: MLS_LEAF_RES,
|
|
89
|
+
cnf,
|
|
90
|
+
iat,
|
|
91
|
+
exp: params.exp
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export async function mintLeafCapability(params) {
|
|
95
|
+
const payload = issuingPayload(params, MAX_LEAF_LIFETIME);
|
|
96
|
+
if (normalizeDID(params.audience) === normalizeDID(params.signer.id)) {
|
|
97
|
+
throw new LeafBindingError('self-issued');
|
|
98
|
+
}
|
|
99
|
+
if (params.parent == null) {
|
|
100
|
+
assertControllerGrant({
|
|
101
|
+
...payload,
|
|
102
|
+
iss: params.signer.id
|
|
103
|
+
}, params.controllerID);
|
|
104
|
+
} else {
|
|
105
|
+
// Issuing has no controller prefix; receivers authenticate the embedded parent.
|
|
106
|
+
const parent = readCapability(params.parent);
|
|
107
|
+
if (parent.payload.cap !== undefined) throw new LeafBindingError('chain-depth');
|
|
108
|
+
assertControllerGrant(parent.payload, params.controllerID);
|
|
109
|
+
if (!hasPermission({
|
|
110
|
+
act: MLS_LEAF_ACT,
|
|
111
|
+
res: MLS_LEAF_RES
|
|
112
|
+
}, parent.payload)) {
|
|
113
|
+
throw new Error('Parent does not grant leaf authentication');
|
|
114
|
+
}
|
|
115
|
+
assertCapabilityLifetime(parent.payload, MAX_TRUSTED_GRANT_LIFETIME);
|
|
116
|
+
if (normalizeDID(parent.payload.aud) !== normalizeDID(params.signer.id)) {
|
|
117
|
+
throw new LeafBindingError('issuer-mismatch');
|
|
118
|
+
}
|
|
119
|
+
const key = capabilityKey(parent.payload);
|
|
120
|
+
if (!key.publicKey.every((byte, index)=>byte === params.signer.publicKey[index]) || key.publicKey.length !== params.signer.publicKey.length) {
|
|
121
|
+
throw new LeafBindingError('issuer-mismatch');
|
|
122
|
+
}
|
|
123
|
+
if (payload.exp > parent.payload.exp) throw new LeafBindingError('child-outlives-parent');
|
|
124
|
+
if (parent.payload.iat > payload.iat || parent.payload.exp < payload.iat || parent.payload.nbf != null && parent.payload.nbf > payload.iat) {
|
|
125
|
+
throw new Error('Parent capability is not valid at issuance');
|
|
126
|
+
}
|
|
127
|
+
return stringifyToken(await params.signer.signToken({
|
|
128
|
+
...payload,
|
|
129
|
+
cap: params.parent
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
return stringifyToken(await params.signer.signToken(payload));
|
|
133
|
+
}
|
|
134
|
+
export async function mintTrustedGrant(params) {
|
|
135
|
+
const payload = issuingPayload(params, MAX_TRUSTED_GRANT_LIFETIME);
|
|
136
|
+
assertControllerGrant({
|
|
137
|
+
...payload,
|
|
138
|
+
iss: params.signer.id
|
|
139
|
+
}, params.controllerID);
|
|
140
|
+
return stringifyToken(await params.signer.signToken(payload));
|
|
141
|
+
}
|