@tik-choco/mistsign 0.0.0-stage → 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/LICENSE +373 -0
- package/README.ja.md +125 -0
- package/README.md +129 -2
- package/README.zh.md +122 -0
- package/dist/agreement/bundle.d.ts +23 -0
- package/dist/agreement/bundle.js +139 -0
- package/dist/agreement/decision.d.ts +7 -0
- package/dist/agreement/decision.js +77 -0
- package/dist/agreement/errors.d.ts +9 -0
- package/dist/agreement/errors.js +19 -0
- package/dist/agreement/evaluate.d.ts +5 -0
- package/dist/agreement/evaluate.js +112 -0
- package/dist/agreement/index.d.ts +9 -0
- package/dist/agreement/index.js +6 -0
- package/dist/agreement/policy.d.ts +31 -0
- package/dist/agreement/policy.js +113 -0
- package/dist/agreement/proposal.d.ts +4 -0
- package/dist/agreement/proposal.js +107 -0
- package/dist/agreement/schema.d.ts +16 -0
- package/dist/agreement/schema.js +93 -0
- package/dist/agreement/strict.d.ts +5 -0
- package/dist/agreement/strict.js +167 -0
- package/dist/agreement/types.d.ts +214 -0
- package/dist/agreement/types.js +3 -0
- package/dist/agreement/verify.d.ts +4 -0
- package/dist/agreement/verify.js +151 -0
- package/dist/bytes.d.ts +6 -0
- package/dist/bytes.js +19 -0
- package/dist/content.d.ts +10 -0
- package/dist/content.js +58 -0
- package/dist/didKey.d.ts +4 -0
- package/dist/didKey.js +48 -0
- package/dist/elements/index.d.ts +1 -0
- package/dist/elements/index.js +2 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +8 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +6 -0
- package/dist/json.d.ts +20 -0
- package/dist/json.js +170 -0
- package/dist/preact/index.d.ts +1 -0
- package/dist/preact/index.js +2 -0
- package/dist/preact/ui.css +0 -0
- package/dist/sha256.d.ts +11 -0
- package/dist/sha256.js +103 -0
- package/dist/sign.d.ts +33 -0
- package/dist/sign.js +61 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.js +27 -0
- package/dist/time.d.ts +9 -0
- package/dist/time.js +36 -0
- package/dist/timestamp/asnTime.d.ts +2 -0
- package/dist/timestamp/asnTime.js +17 -0
- package/dist/timestamp/cms.d.ts +30 -0
- package/dist/timestamp/cms.js +126 -0
- package/dist/timestamp/constraints.d.ts +17 -0
- package/dist/timestamp/constraints.js +159 -0
- package/dist/timestamp/crypto.d.ts +6 -0
- package/dist/timestamp/crypto.js +101 -0
- package/dist/timestamp/der.d.ts +24 -0
- package/dist/timestamp/der.js +118 -0
- package/dist/timestamp/index.d.ts +2 -0
- package/dist/timestamp/index.js +1 -0
- package/dist/timestamp/name.d.ts +2 -0
- package/dist/timestamp/name.js +55 -0
- package/dist/timestamp/policy.d.ts +9 -0
- package/dist/timestamp/policy.js +56 -0
- package/dist/timestamp/verify.d.ts +33 -0
- package/dist/timestamp/verify.js +74 -0
- package/dist/timestamp/x509.d.ts +35 -0
- package/dist/timestamp/x509.js +232 -0
- package/dist/transport/index.d.ts +1 -0
- package/dist/transport/index.js +2 -0
- package/dist/trust/delegation.d.ts +15 -0
- package/dist/trust/delegation.js +40 -0
- package/dist/trust/index.d.ts +15 -0
- package/dist/trust/index.js +3 -0
- package/dist/trust/revocation.d.ts +33 -0
- package/dist/trust/revocation.js +78 -0
- package/dist/trust/signers.d.ts +31 -0
- package/dist/trust/signers.js +166 -0
- package/dist/verify.d.ts +88 -0
- package/dist/verify.js +195 -0
- package/dist/wire.d.ts +6 -0
- package/dist/wire.js +38 -0
- package/package.json +89 -4
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { subjectAltNames } from './constraints.js';
|
|
2
|
+
import type { NameConstraints } from './constraints.js';
|
|
3
|
+
import type { Algorithm, Der } from './der.js';
|
|
4
|
+
import type { PolicyExtensions } from './policy.js';
|
|
5
|
+
export declare const timestampEKU = "1.3.6.1.5.5.7.3.8";
|
|
6
|
+
export interface Certificate extends PolicyExtensions {
|
|
7
|
+
raw: Uint8Array<ArrayBuffer>;
|
|
8
|
+
tbs: Uint8Array<ArrayBuffer>;
|
|
9
|
+
issuer: Uint8Array;
|
|
10
|
+
subject: Uint8Array;
|
|
11
|
+
name: string;
|
|
12
|
+
serial: Uint8Array;
|
|
13
|
+
version: number;
|
|
14
|
+
spki: Der;
|
|
15
|
+
signatureAlgorithm: Algorithm;
|
|
16
|
+
signature: Uint8Array<ArrayBuffer>;
|
|
17
|
+
notBefore: bigint;
|
|
18
|
+
notAfter: bigint;
|
|
19
|
+
ski?: Uint8Array;
|
|
20
|
+
aki?: Uint8Array;
|
|
21
|
+
ca?: boolean;
|
|
22
|
+
pathLen?: number;
|
|
23
|
+
certSign?: boolean;
|
|
24
|
+
eku?: string[];
|
|
25
|
+
ekuCritical?: boolean;
|
|
26
|
+
san?: Uint8Array;
|
|
27
|
+
names?: ReturnType<typeof subjectAltNames>;
|
|
28
|
+
constraints?: NameConstraints;
|
|
29
|
+
unsupportedConstraint: boolean;
|
|
30
|
+
}
|
|
31
|
+
export declare function certificate(der: Der): Certificate;
|
|
32
|
+
export type CertificateInput = string | Uint8Array;
|
|
33
|
+
export declare function parseRoots(inputs: readonly CertificateInput[]): Certificate[];
|
|
34
|
+
/** Every candidate must lead to an exact certificate in the caller's pool. */
|
|
35
|
+
export declare function verifyChain(leaf: Certificate, embedded: Certificate[], roots: Certificate[], at: bigint): Promise<boolean>;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { asnTime } from './asnTime.js';
|
|
2
|
+
import { nameConstraints, permitsNames, subjectAltNames } from './constraints.js';
|
|
3
|
+
import { algorithm, bitString, bool, check, decode, equal, fields, intBytes, oid, only, tagged, uint } from './der.js';
|
|
4
|
+
import { verifySignature } from './crypto.js';
|
|
5
|
+
import { subjectName } from './name.js';
|
|
6
|
+
import { validPolicies } from './policy.js';
|
|
7
|
+
export const timestampEKU = '1.3.6.1.5.5.7.3.8';
|
|
8
|
+
function extensions(cert, value) {
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
for (const e of fields(only(value, 0xa3))) {
|
|
11
|
+
const members = fields(e, 0x30, 2, 3);
|
|
12
|
+
const id = oid(members[0]);
|
|
13
|
+
check(!seen.has(id));
|
|
14
|
+
seen.add(id);
|
|
15
|
+
const critical = members.length === 3 && bool(members[1]);
|
|
16
|
+
const content = tagged(members[members.length - 1], 4).body;
|
|
17
|
+
switch (id) {
|
|
18
|
+
case '2.5.29.19': {
|
|
19
|
+
const [first, second] = fields(decode(content), 0x30, 0, 2);
|
|
20
|
+
cert.ca = first?.tag === 1 && bool(first);
|
|
21
|
+
const limit = first?.tag === 1 ? second : first;
|
|
22
|
+
if (limit)
|
|
23
|
+
cert.pathLen = uint(limit);
|
|
24
|
+
check(!second || first.tag === 1);
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case '2.5.29.15': {
|
|
28
|
+
const usage = bitString(decode(content), false);
|
|
29
|
+
// CheckSignatureFrom applies keyCertSign to issuing keys. The leaf
|
|
30
|
+
// is authorized for timestamping by its exclusive critical EKU.
|
|
31
|
+
if (usage.some(b => b !== 0))
|
|
32
|
+
cert.certSign = (usage[0] & 4) !== 0;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case '2.5.29.37':
|
|
36
|
+
cert.eku = fields(decode(content)).map(oid);
|
|
37
|
+
cert.ekuCritical = critical;
|
|
38
|
+
break;
|
|
39
|
+
case '2.5.29.14':
|
|
40
|
+
check(!critical);
|
|
41
|
+
cert.ski = tagged(decode(content), 4).body;
|
|
42
|
+
break;
|
|
43
|
+
case '2.5.29.35': {
|
|
44
|
+
check(!critical);
|
|
45
|
+
const [keyId] = fields(decode(content));
|
|
46
|
+
if (keyId?.tag === 0x80)
|
|
47
|
+
cert.aki = keyId.body;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case '2.5.29.32': {
|
|
51
|
+
const policies = new Set();
|
|
52
|
+
for (const policy of fields(decode(content))) {
|
|
53
|
+
const [id, qualifiers] = fields(policy, 0x30, 1, 2);
|
|
54
|
+
const policyId = oid(id);
|
|
55
|
+
check(!policies.has(policyId));
|
|
56
|
+
policies.add(policyId);
|
|
57
|
+
if (qualifiers)
|
|
58
|
+
fields(qualifiers);
|
|
59
|
+
}
|
|
60
|
+
cert.policies = policies;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case '2.5.29.33':
|
|
64
|
+
cert.mappings = new Map();
|
|
65
|
+
for (const pair of fields(decode(content))) {
|
|
66
|
+
const [issuer, subject] = fields(pair, 0x30, 2, 2).map(oid);
|
|
67
|
+
const subjects = cert.mappings.get(issuer) ?? new Set();
|
|
68
|
+
subjects.add(subject);
|
|
69
|
+
cert.mappings.set(issuer, subjects);
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case '2.5.29.36': {
|
|
73
|
+
const parts = fields(decode(content), 0x30, 1, 2);
|
|
74
|
+
let i = 0;
|
|
75
|
+
if (parts[i].tag === 0x80)
|
|
76
|
+
cert.requireExplicit = uint(parts[i++], 0x80);
|
|
77
|
+
if (parts[i]?.tag === 0x81)
|
|
78
|
+
cert.inhibitMapping = uint(parts[i++], 0x81);
|
|
79
|
+
check(i === parts.length);
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case '2.5.29.54': // inhibit anyPolicy
|
|
83
|
+
cert.inhibitAny = uint(decode(content));
|
|
84
|
+
break;
|
|
85
|
+
case '2.5.29.17':
|
|
86
|
+
cert.san = content;
|
|
87
|
+
cert.names = subjectAltNames(decode(content));
|
|
88
|
+
if (critical && !cert.names.length)
|
|
89
|
+
cert.unsupportedConstraint = true;
|
|
90
|
+
break;
|
|
91
|
+
case '2.5.29.30':
|
|
92
|
+
cert.constraints = nameConstraints(decode(content));
|
|
93
|
+
if (critical && cert.constraints.unhandled)
|
|
94
|
+
cert.unsupportedConstraint = true;
|
|
95
|
+
break;
|
|
96
|
+
case '1.3.6.1.5.5.7.1.1': // Authority Information Access
|
|
97
|
+
check(!critical);
|
|
98
|
+
for (const item of fields(decode(content))) {
|
|
99
|
+
const [method] = fields(item, 0x30, 2, 2);
|
|
100
|
+
oid(method);
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
case '2.5.29.31': // CRLDistributionPoints. No network access.
|
|
104
|
+
for (const dp of fields(decode(content))) {
|
|
105
|
+
const [point] = fields(dp);
|
|
106
|
+
if (point?.tag === 0xa0)
|
|
107
|
+
tagged(only(point, 0xa0), 0xa0);
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
default:
|
|
111
|
+
if (critical)
|
|
112
|
+
cert.unsupportedConstraint = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export function certificate(der) {
|
|
117
|
+
const [tbs, outerAlg, sig] = fields(der, 0x30, 3, 3);
|
|
118
|
+
const values = fields(tbs, 0x30, 6, 10);
|
|
119
|
+
let i = 0;
|
|
120
|
+
const version = values[i].tag === 0xa0 ? uint(only(values[i++], 0xa0)) + 1 : 1;
|
|
121
|
+
check(version <= 3);
|
|
122
|
+
const serial = intBytes(values[i++]);
|
|
123
|
+
check(serial[0] < 128);
|
|
124
|
+
check(equal(values[i++].raw, outerAlg.raw));
|
|
125
|
+
const issuer = values[i++];
|
|
126
|
+
subjectName(issuer);
|
|
127
|
+
const [start, end] = fields(values[i++], 0x30, 2, 2);
|
|
128
|
+
const subject = values[i++];
|
|
129
|
+
const spki = values[i++];
|
|
130
|
+
const [keyAlg, key] = fields(spki, 0x30, 2, 2);
|
|
131
|
+
algorithm(keyAlg);
|
|
132
|
+
bitString(key);
|
|
133
|
+
const cert = {
|
|
134
|
+
raw: der.raw, tbs: tbs.raw, version, serial, issuer: issuer.raw, subject: subject.raw,
|
|
135
|
+
name: subjectName(subject), spki, signatureAlgorithm: algorithm(outerAlg), signature: bitString(sig),
|
|
136
|
+
notBefore: asnTime(start), notAfter: asnTime(end), unsupportedConstraint: false,
|
|
137
|
+
};
|
|
138
|
+
if (version >= 2 && values[i]?.tag === 0x81)
|
|
139
|
+
i++;
|
|
140
|
+
if (version >= 2 && values[i]?.tag === 0x82)
|
|
141
|
+
i++;
|
|
142
|
+
if (version === 3 && values[i])
|
|
143
|
+
extensions(cert, values[i++]);
|
|
144
|
+
check(i === values.length);
|
|
145
|
+
return cert;
|
|
146
|
+
}
|
|
147
|
+
const maxRootSize = 1024 * 1024;
|
|
148
|
+
export function parseRoots(inputs) {
|
|
149
|
+
// An explicitly empty pool trusts zero anchors. Only omission skips chaining.
|
|
150
|
+
check(inputs.length <= 1024);
|
|
151
|
+
const roots = [];
|
|
152
|
+
let total = 0;
|
|
153
|
+
function add(bytes) {
|
|
154
|
+
check(roots.length < 4096);
|
|
155
|
+
roots.push(certificate(decode(bytes)));
|
|
156
|
+
}
|
|
157
|
+
for (const input of inputs) {
|
|
158
|
+
check(input.length > 0 && input.length <= maxRootSize);
|
|
159
|
+
total += input.length;
|
|
160
|
+
check(total <= 16 * maxRootSize);
|
|
161
|
+
if (typeof input !== 'string' && input[0] === 0x30) {
|
|
162
|
+
add(new Uint8Array(input));
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const pem = typeof input === 'string' ? input : new TextDecoder().decode(input);
|
|
166
|
+
const before = roots.length;
|
|
167
|
+
let block;
|
|
168
|
+
for (const line of pem.split(/\r?\n/)) {
|
|
169
|
+
const s = line.trimEnd();
|
|
170
|
+
if (s === '-----BEGIN CERTIFICATE-----') {
|
|
171
|
+
block = [];
|
|
172
|
+
}
|
|
173
|
+
else if (s === '-----END CERTIFICATE-----' && block) {
|
|
174
|
+
const body = block.join('').replace(/[ \t]/g, '');
|
|
175
|
+
check(body.length % 4 === 0 && /^[A-Za-z0-9+/]+={0,2}$/.test(body));
|
|
176
|
+
add(Uint8Array.from(atob(body), c => c.charCodeAt(0)));
|
|
177
|
+
block = undefined;
|
|
178
|
+
}
|
|
179
|
+
else if (block)
|
|
180
|
+
block.push(s);
|
|
181
|
+
}
|
|
182
|
+
check(roots.length > before);
|
|
183
|
+
}
|
|
184
|
+
return roots;
|
|
185
|
+
}
|
|
186
|
+
/** Every candidate must lead to an exact certificate in the caller's pool. */
|
|
187
|
+
export async function verifyChain(leaf, embedded, roots, at) {
|
|
188
|
+
const valid = (c) => !c.unsupportedConstraint && c.notBefore <= at && at <= c.notAfter &&
|
|
189
|
+
(!c.eku?.length || c.eku.includes(timestampEKU) || c.eku.includes('2.5.29.37.0'));
|
|
190
|
+
if (!valid(leaf))
|
|
191
|
+
return false;
|
|
192
|
+
if (roots.some(c => equal(c.raw, leaf.raw)))
|
|
193
|
+
return true;
|
|
194
|
+
const anchors = new Set(roots);
|
|
195
|
+
const issuers = [...roots, ...embedded];
|
|
196
|
+
let attempts = 0;
|
|
197
|
+
const budget = { remaining: 250000 };
|
|
198
|
+
async function walk(child, path) {
|
|
199
|
+
if (path.length > 32)
|
|
200
|
+
return false;
|
|
201
|
+
const parents = issuers.filter(p => equal(p.subject, child.issuer));
|
|
202
|
+
// An AKI/SKI match is a hint, never an alternative to the signature.
|
|
203
|
+
const priority = (p) => !child.aki || !p.ski ? 1 : equal(child.aki, p.ski) ? 0 : 2;
|
|
204
|
+
parents.sort((a, b) => priority(a) - priority(b));
|
|
205
|
+
for (const p of parents) {
|
|
206
|
+
if (!valid(p) || path.some(c => equal(c.subject, p.subject) && equal(c.spki.raw, p.spki.raw) &&
|
|
207
|
+
(c.san && p.san ? equal(c.san, p.san) : c.san === p.san)))
|
|
208
|
+
continue;
|
|
209
|
+
const anchor = anchors.has(p);
|
|
210
|
+
if (!p.ca && !(anchor && p.version < 3 && p.ca === undefined))
|
|
211
|
+
continue;
|
|
212
|
+
if (p.certSign === false || (p.pathLen !== undefined && path.length - 1 > p.pathLen))
|
|
213
|
+
continue;
|
|
214
|
+
check(++attempts <= 100);
|
|
215
|
+
try {
|
|
216
|
+
if (!await verifySignature(p.spki, child.signatureAlgorithm, child.tbs, child.signature))
|
|
217
|
+
continue;
|
|
218
|
+
const nc = p.constraints;
|
|
219
|
+
if (nc && path.some(c => c.names && !permitsNames(nc, c.names, budget)))
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const next = [...path, p];
|
|
226
|
+
if (anchor ? validPolicies(next) : await walk(p, next))
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
return walk(leaf, [leaf]);
|
|
232
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { JsonInput } from '../json.js';
|
|
2
|
+
import type { Instant, TimeInput } from '../time.js';
|
|
3
|
+
import type { DelegationV1 } from '@tik-choco/mistseed/identity/core';
|
|
4
|
+
export type { DelegationV1 } from '@tik-choco/mistseed/identity/core';
|
|
5
|
+
export interface DelegationOptions {
|
|
6
|
+
now?: TimeInput;
|
|
7
|
+
leaf?: string;
|
|
8
|
+
}
|
|
9
|
+
/** Keep only the six DelegationV1 fields; unknown fields are not root-signed. */
|
|
10
|
+
export declare function delegationFields(value: unknown): DelegationV1;
|
|
11
|
+
/** Parse a standalone delegation (up to 64 KiB); does not establish trust. */
|
|
12
|
+
export declare function parseDelegation(input: JsonInput): DelegationV1;
|
|
13
|
+
export declare function verifyDelegationAt(value: unknown, at: Instant, expectedLeaf?: string): Promise<boolean>;
|
|
14
|
+
/** Verify the delegation's root signature, attribution and validity period. */
|
|
15
|
+
export declare function verifyDelegation(value: unknown, options?: DelegationOptions): Promise<boolean>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isEd25519DidKey } from '../didKey.js';
|
|
2
|
+
import { MistsignError } from '../errors.js';
|
|
3
|
+
import { canonicalJson, decodeText, isObject, parseStrictJson } from '../json.js';
|
|
4
|
+
import { utf8 } from '../bytes.js';
|
|
5
|
+
import { CLOCK_SKEW, MAX_DELEGATION_DURATION, nowInstant, parseTime } from '../time.js';
|
|
6
|
+
import { verifyEd25519 } from '../wire.js';
|
|
7
|
+
/** Keep only the six DelegationV1 fields; unknown fields are not root-signed. */
|
|
8
|
+
export function delegationFields(value) {
|
|
9
|
+
if (!isObject(value) || value.v !== 1 ||
|
|
10
|
+
typeof value.root !== 'string' || typeof value.leaf !== 'string' ||
|
|
11
|
+
typeof value.iat !== 'string' || typeof value.exp !== 'string' || typeof value.sig !== 'string') {
|
|
12
|
+
throw new MistsignError('invalid-delegation');
|
|
13
|
+
}
|
|
14
|
+
return { v: 1, root: value.root, leaf: value.leaf, iat: value.iat, exp: value.exp, sig: value.sig };
|
|
15
|
+
}
|
|
16
|
+
/** Parse a standalone delegation (up to 64 KiB); does not establish trust. */
|
|
17
|
+
export function parseDelegation(input) {
|
|
18
|
+
return delegationFields(parseStrictJson(decodeText(input, 64 * 1024)));
|
|
19
|
+
}
|
|
20
|
+
export async function verifyDelegationAt(value, at, expectedLeaf = '') {
|
|
21
|
+
try {
|
|
22
|
+
const { sig, ...unsigned } = delegationFields(value);
|
|
23
|
+
const { root, leaf, iat, exp } = unsigned;
|
|
24
|
+
if (!isEd25519DidKey(root) || !isEd25519DidKey(leaf) || root === leaf || (expectedLeaf !== '' && leaf !== expectedLeaf)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const start = parseTime(iat), end = parseTime(exp);
|
|
28
|
+
if (start >= end || end - start > MAX_DELEGATION_DURATION ||
|
|
29
|
+
at < start - CLOCK_SKEW || at > end + CLOCK_SKEW)
|
|
30
|
+
return false;
|
|
31
|
+
return verifyEd25519(root, utf8.encode(canonicalJson(unsigned)), sig);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Verify the delegation's root signature, attribution and validity period. */
|
|
38
|
+
export async function verifyDelegation(value, options = {}) {
|
|
39
|
+
return verifyDelegationAt(value, nowInstant(options.now), options.leaf);
|
|
40
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { JsonInput } from '../json.js';
|
|
2
|
+
export { parseDelegation, verifyDelegation } from './delegation.js';
|
|
3
|
+
export type { DelegationV1, DelegationOptions } from './delegation.js';
|
|
4
|
+
export { parseRevocation, verifyRevocation, evaluateRevocations } from './revocation.js';
|
|
5
|
+
export type { RevocationEntry, RevocationDocument, RevocationNotice, RevocationResult, RevocationEvaluation } from './revocation.js';
|
|
6
|
+
export { parseAllowedSigners, matchAllowedSigner } from './signers.js';
|
|
7
|
+
export type { AllowedSigner, SignerMatch, SignerMatchCode, SignerMatchOptions } from './signers.js';
|
|
8
|
+
/** Verification policy supplied by the verifier, not by the signer. */
|
|
9
|
+
export interface TrustPolicy {
|
|
10
|
+
expect?: string;
|
|
11
|
+
signers?: JsonInput;
|
|
12
|
+
signerName?: string;
|
|
13
|
+
revocations?: readonly JsonInput[];
|
|
14
|
+
requireTimestamp?: boolean;
|
|
15
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { JsonInput } from '../json.js';
|
|
2
|
+
export interface RevocationEntry {
|
|
3
|
+
leaf: string;
|
|
4
|
+
revokedAt: string;
|
|
5
|
+
reason?: string;
|
|
6
|
+
}
|
|
7
|
+
/** A successfully parsed and cryptographically verified revocation document. */
|
|
8
|
+
export interface RevocationDocument {
|
|
9
|
+
issuer: string;
|
|
10
|
+
issuedAt: string;
|
|
11
|
+
revoked: RevocationEntry[];
|
|
12
|
+
}
|
|
13
|
+
export interface RevocationNotice {
|
|
14
|
+
issuer: string;
|
|
15
|
+
revokedAt: string;
|
|
16
|
+
reason?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface RevocationResult extends RevocationNotice {
|
|
19
|
+
beforeRevocation: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface RevocationEvaluation {
|
|
22
|
+
revocation?: RevocationResult;
|
|
23
|
+
revocationNotices: RevocationNotice[];
|
|
24
|
+
}
|
|
25
|
+
/** Validate all fields and the issuer signature, including unknown wire fields. */
|
|
26
|
+
export declare function parseRevocation(input: JsonInput): Promise<RevocationDocument>;
|
|
27
|
+
export declare function verifyRevocation(input: JsonInput): Promise<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* Apply the earliest revocation from the claimed root (even for an invalid
|
|
30
|
+
* delegation). Other issuers produce notices only. Phase 1 cannot establish
|
|
31
|
+
* a trusted time before revocation, so beforeRevocation is always false.
|
|
32
|
+
*/
|
|
33
|
+
export declare function evaluateRevocations(documents: readonly RevocationDocument[], root: string, leaf: string): RevocationEvaluation;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isEd25519DidKey } from '../didKey.js';
|
|
2
|
+
import { MistsignError } from '../errors.js';
|
|
3
|
+
import { isObject, parseStrictJson } from '../json.js';
|
|
4
|
+
import { CLOCK_SKEW, formatTime, parseTime } from '../time.js';
|
|
5
|
+
import { verifyWire } from '../wire.js';
|
|
6
|
+
function revocationTime(value) {
|
|
7
|
+
const at = parseTime(value);
|
|
8
|
+
if (formatTime(at) !== value)
|
|
9
|
+
throw new MistsignError('invalid-revocation');
|
|
10
|
+
return at;
|
|
11
|
+
}
|
|
12
|
+
/** Validate all fields and the issuer signature, including unknown wire fields. */
|
|
13
|
+
export async function parseRevocation(input) {
|
|
14
|
+
try {
|
|
15
|
+
const wire = parseStrictJson(input, 'signature');
|
|
16
|
+
if (!isObject(wire) || wire.type !== 'tc-sign:revocation' || wire.v !== 1 ||
|
|
17
|
+
!isEd25519DidKey(wire.fromId) || !await verifyWire(wire))
|
|
18
|
+
throw new MistsignError('invalid-revocation');
|
|
19
|
+
const issuer = wire.fromId;
|
|
20
|
+
const issuedAt = revocationTime(wire.issuedAt);
|
|
21
|
+
if (!Array.isArray(wire.revoked) || wire.revoked.length < 1 || wire.revoked.length > 10000) {
|
|
22
|
+
throw new MistsignError('invalid-revocation');
|
|
23
|
+
}
|
|
24
|
+
const seen = new Set();
|
|
25
|
+
const revoked = [];
|
|
26
|
+
for (const item of wire.revoked) {
|
|
27
|
+
if (!isObject(item) || !isEd25519DidKey(item.leaf) || item.leaf === issuer || seen.has(item.leaf)) {
|
|
28
|
+
throw new MistsignError('invalid-revocation');
|
|
29
|
+
}
|
|
30
|
+
seen.add(item.leaf);
|
|
31
|
+
const at = revocationTime(item.revokedAt);
|
|
32
|
+
if (at > issuedAt + CLOCK_SKEW)
|
|
33
|
+
throw new MistsignError('invalid-revocation');
|
|
34
|
+
const entry = { leaf: item.leaf, revokedAt: formatTime(at) };
|
|
35
|
+
if (Object.hasOwn(item, 'reason')) {
|
|
36
|
+
if (typeof item.reason !== 'string' || [...item.reason].length > 1000)
|
|
37
|
+
throw new MistsignError('invalid-revocation');
|
|
38
|
+
entry.reason = item.reason;
|
|
39
|
+
}
|
|
40
|
+
revoked.push(entry);
|
|
41
|
+
}
|
|
42
|
+
return { issuer, issuedAt: formatTime(issuedAt), revoked };
|
|
43
|
+
}
|
|
44
|
+
catch (cause) {
|
|
45
|
+
throw new MistsignError('invalid-revocation', { cause });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export async function verifyRevocation(input) {
|
|
49
|
+
try {
|
|
50
|
+
await parseRevocation(input);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Apply the earliest revocation from the claimed root (even for an invalid
|
|
59
|
+
* delegation). Other issuers produce notices only. Phase 1 cannot establish
|
|
60
|
+
* a trusted time before revocation, so beforeRevocation is always false.
|
|
61
|
+
*/
|
|
62
|
+
export function evaluateRevocations(documents, root, leaf) {
|
|
63
|
+
const result = { revocationNotices: [] };
|
|
64
|
+
for (const doc of documents) {
|
|
65
|
+
const item = doc.revoked.find(entry => entry.leaf === leaf);
|
|
66
|
+
if (!item)
|
|
67
|
+
continue;
|
|
68
|
+
const notice = { issuer: doc.issuer, revokedAt: item.revokedAt };
|
|
69
|
+
if (item.reason !== undefined)
|
|
70
|
+
notice.reason = item.reason;
|
|
71
|
+
if (doc.issuer !== root)
|
|
72
|
+
result.revocationNotices.push(notice);
|
|
73
|
+
else if (!result.revocation || parseTime(item.revokedAt) < parseTime(result.revocation.revokedAt)) {
|
|
74
|
+
result.revocation = { ...notice, beforeRevocation: false };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ErrorCode } from '../errors.js';
|
|
2
|
+
import type { JsonInput } from '../json.js';
|
|
3
|
+
import type { Instant, TimeInput } from '../time.js';
|
|
4
|
+
export interface AllowedSigner {
|
|
5
|
+
name: string;
|
|
6
|
+
did: string;
|
|
7
|
+
root: boolean;
|
|
8
|
+
validAfter?: string;
|
|
9
|
+
validBefore?: string;
|
|
10
|
+
types?: string[];
|
|
11
|
+
}
|
|
12
|
+
export type SignerMatchCode = Extract<ErrorCode, 'signer-name-mismatch' | 'signer-root-required' | 'signer-type-mismatch' | 'signer-outside-period' | 'signer-unlisted'>;
|
|
13
|
+
export interface SignerMatch {
|
|
14
|
+
matched?: AllowedSigner;
|
|
15
|
+
periodChecked: boolean;
|
|
16
|
+
reasons: SignerMatchCode[];
|
|
17
|
+
}
|
|
18
|
+
export interface SignerMatchOptions {
|
|
19
|
+
leaf: string;
|
|
20
|
+
/** Must be the root of a verified delegation, not a claimed root. */
|
|
21
|
+
root?: string;
|
|
22
|
+
type?: string;
|
|
23
|
+
name?: string;
|
|
24
|
+
/** Date, milliseconds, or RFC3339 signedAt (including sub-ms precision). */
|
|
25
|
+
at: TimeInput | string;
|
|
26
|
+
}
|
|
27
|
+
/** Parse an allowed signers list (1 MiB / 10,000 entries maximum). */
|
|
28
|
+
export declare function parseAllowedSigners(input: JsonInput): AllowedSigner[];
|
|
29
|
+
export declare function matchSignerAt(entries: readonly AllowedSigner[], leaf: string, root: string, kind: string, name: string, at: Instant): SignerMatch;
|
|
30
|
+
/** Match the first authorized signer, with inclusive date/time boundaries. */
|
|
31
|
+
export declare function matchAllowedSigner(entries: readonly AllowedSigner[], options: SignerMatchOptions): SignerMatch;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { isEd25519DidKey } from '../didKey.js';
|
|
2
|
+
import { MistsignError } from '../errors.js';
|
|
3
|
+
import { decodeText, MAX_SIGNATURE_SIZE } from '../json.js';
|
|
4
|
+
import { nowInstant, parseTime } from '../time.js';
|
|
5
|
+
// Go unicode.IsSpace / strings.TrimSpace (not JS's BOM-inclusive trim).
|
|
6
|
+
const SPACE = /[\u0009-\u000d\u0020\u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]/;
|
|
7
|
+
const leftSpace = new RegExp('^' + SPACE.source + '+');
|
|
8
|
+
const rightSpace = new RegExp(SPACE.source + '+$');
|
|
9
|
+
const trim = (s) => s.replace(leftSpace, '').replace(rightSpace, '');
|
|
10
|
+
const invalid = () => { throw new MistsignError('invalid-signers'); };
|
|
11
|
+
function cutName(line) {
|
|
12
|
+
if (!line.startsWith('"')) {
|
|
13
|
+
const split = line.search(SPACE);
|
|
14
|
+
const name = split < 0 ? line : line.slice(0, split);
|
|
15
|
+
if (name.includes('"'))
|
|
16
|
+
return invalid();
|
|
17
|
+
return [name, split < 0 ? '' : line.slice(split).replace(leftSpace, '')];
|
|
18
|
+
}
|
|
19
|
+
let name = '';
|
|
20
|
+
for (let i = 1; i < line.length; i++) {
|
|
21
|
+
const c = line[i];
|
|
22
|
+
if (c === '"') {
|
|
23
|
+
const tail = line.slice(i + 1);
|
|
24
|
+
if (tail && !SPACE.test(tail[0]))
|
|
25
|
+
return invalid();
|
|
26
|
+
return [name, tail.replace(leftSpace, '')];
|
|
27
|
+
}
|
|
28
|
+
if (c === '\\') {
|
|
29
|
+
const next = line[++i];
|
|
30
|
+
if (next !== '"' && next !== '\\')
|
|
31
|
+
return invalid();
|
|
32
|
+
name += next;
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
name += c;
|
|
36
|
+
}
|
|
37
|
+
return invalid();
|
|
38
|
+
}
|
|
39
|
+
function cutValue(value) {
|
|
40
|
+
if (value.startsWith('"')) {
|
|
41
|
+
const close = value.indexOf('"', 1);
|
|
42
|
+
if (close < 0)
|
|
43
|
+
return invalid();
|
|
44
|
+
const tail = value.slice(close + 1);
|
|
45
|
+
if (tail && !tail.startsWith(','))
|
|
46
|
+
return invalid();
|
|
47
|
+
return [value.slice(1, close), tail, true];
|
|
48
|
+
}
|
|
49
|
+
const comma = value.indexOf(',');
|
|
50
|
+
const token = comma < 0 ? value : value.slice(0, comma);
|
|
51
|
+
if (token.includes('"') || SPACE.test(token))
|
|
52
|
+
return invalid();
|
|
53
|
+
return [token, comma < 0 ? '' : value.slice(comma), false];
|
|
54
|
+
}
|
|
55
|
+
function signerTime(value) {
|
|
56
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(value))
|
|
57
|
+
value += 'T00:00:00Z';
|
|
58
|
+
else if (!/^\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:[Zz]|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/.test(value)) {
|
|
59
|
+
return invalid();
|
|
60
|
+
}
|
|
61
|
+
value = value.toUpperCase();
|
|
62
|
+
parseTime(value);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
function parseEntry(line) {
|
|
66
|
+
const [name, rest] = cutName(line);
|
|
67
|
+
if (!name || [...name].length > 200 || /\p{Cc}/u.test(name))
|
|
68
|
+
return invalid();
|
|
69
|
+
let split = rest.length - 1;
|
|
70
|
+
while (split >= 0 && !SPACE.test(rest[split]))
|
|
71
|
+
split--;
|
|
72
|
+
const did = trim(rest.slice(split + 1));
|
|
73
|
+
if (!isEd25519DidKey(did))
|
|
74
|
+
return invalid();
|
|
75
|
+
let options = trim(rest.slice(0, split + 1));
|
|
76
|
+
const entry = { name, did, root: false };
|
|
77
|
+
const seen = new Set();
|
|
78
|
+
while (options) {
|
|
79
|
+
const split = options.search(/[=,]/);
|
|
80
|
+
const key = split < 0 ? options : options.slice(0, split);
|
|
81
|
+
let tail = split < 0 ? '' : options.slice(split);
|
|
82
|
+
const hasValue = tail.startsWith('=');
|
|
83
|
+
let value = '', quoted = false;
|
|
84
|
+
if (hasValue)
|
|
85
|
+
[value, tail, quoted] = cutValue(tail.slice(1));
|
|
86
|
+
if (seen.has(key))
|
|
87
|
+
return invalid();
|
|
88
|
+
seen.add(key);
|
|
89
|
+
switch (key) {
|
|
90
|
+
case 'root':
|
|
91
|
+
if (hasValue)
|
|
92
|
+
return invalid();
|
|
93
|
+
entry.root = true;
|
|
94
|
+
break;
|
|
95
|
+
case 'valid-after':
|
|
96
|
+
entry.validAfter = signerTime(value);
|
|
97
|
+
break;
|
|
98
|
+
case 'valid-before':
|
|
99
|
+
entry.validBefore = signerTime(value);
|
|
100
|
+
break;
|
|
101
|
+
case 'types':
|
|
102
|
+
entry.types = value.split(',');
|
|
103
|
+
if (!quoted || entry.types.includes(''))
|
|
104
|
+
return invalid();
|
|
105
|
+
break;
|
|
106
|
+
default: return invalid();
|
|
107
|
+
}
|
|
108
|
+
if (tail === ',')
|
|
109
|
+
return invalid();
|
|
110
|
+
options = tail.slice(1);
|
|
111
|
+
}
|
|
112
|
+
return entry;
|
|
113
|
+
}
|
|
114
|
+
/** Parse an allowed signers list (1 MiB / 10,000 entries maximum). */
|
|
115
|
+
export function parseAllowedSigners(input) {
|
|
116
|
+
try {
|
|
117
|
+
const entries = [];
|
|
118
|
+
for (const raw of decodeText(input, MAX_SIGNATURE_SIZE).split('\n')) {
|
|
119
|
+
const line = trim(raw);
|
|
120
|
+
if (!line || line.startsWith('#'))
|
|
121
|
+
continue;
|
|
122
|
+
if (entries.length >= 10000)
|
|
123
|
+
return invalid();
|
|
124
|
+
entries.push(parseEntry(line));
|
|
125
|
+
}
|
|
126
|
+
return entries;
|
|
127
|
+
}
|
|
128
|
+
catch (cause) {
|
|
129
|
+
throw new MistsignError('invalid-signers', { cause });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export function matchSignerAt(entries, leaf, root, kind, name, at) {
|
|
133
|
+
const reasons = new Set();
|
|
134
|
+
let periodChecked = false;
|
|
135
|
+
for (const entry of entries) {
|
|
136
|
+
if (entry.did !== leaf && entry.did !== root)
|
|
137
|
+
continue;
|
|
138
|
+
if (name && entry.name !== name) {
|
|
139
|
+
reasons.add('signer-name-mismatch');
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (entry.did !== leaf && !entry.root) {
|
|
143
|
+
reasons.add('signer-root-required');
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (!(entry.types ?? ['tc-sign:signature']).includes(kind)) {
|
|
147
|
+
reasons.add('signer-type-mismatch');
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
periodChecked ||= entry.validAfter !== undefined || entry.validBefore !== undefined;
|
|
151
|
+
if ((entry.validAfter !== undefined && at < parseTime(entry.validAfter)) ||
|
|
152
|
+
(entry.validBefore !== undefined && at > parseTime(entry.validBefore))) {
|
|
153
|
+
reasons.add('signer-outside-period');
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
return { matched: entry, periodChecked, reasons: [] };
|
|
157
|
+
}
|
|
158
|
+
if (!reasons.size)
|
|
159
|
+
reasons.add('signer-unlisted');
|
|
160
|
+
return { periodChecked, reasons: [...reasons] };
|
|
161
|
+
}
|
|
162
|
+
/** Match the first authorized signer, with inclusive date/time boundaries. */
|
|
163
|
+
export function matchAllowedSigner(entries, options) {
|
|
164
|
+
const at = typeof options.at === 'string' ? parseTime(options.at) : nowInstant(options.at);
|
|
165
|
+
return matchSignerAt(entries, options.leaf, options.root ?? '', options.type ?? 'tc-sign:signature', options.name ?? '', at);
|
|
166
|
+
}
|