mergeseal 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/CONTRIBUTING.md +64 -2
- package/README.md +29 -6
- package/dist/cli-contract.d.ts +2 -0
- package/dist/cli-contract.js +8 -0
- package/dist/cli.js +24 -0
- package/dist/coding/git.js +5 -0
- package/dist/intoto.d.ts +34 -1
- package/dist/intoto.js +316 -13
- package/dist/keyless/asn1.d.ts +26 -0
- package/dist/keyless/asn1.js +269 -0
- package/dist/keyless/bundle.d.ts +52 -0
- package/dist/keyless/bundle.js +163 -0
- package/dist/keyless/cli.d.ts +11 -0
- package/dist/keyless/cli.js +182 -0
- package/dist/keyless/errors.d.ts +17 -0
- package/dist/keyless/errors.js +29 -0
- package/dist/keyless/fulcio.d.ts +13 -0
- package/dist/keyless/fulcio.js +104 -0
- package/dist/keyless/index.d.ts +16 -0
- package/dist/keyless/index.js +12 -0
- package/dist/keyless/oidc.d.ts +29 -0
- package/dist/keyless/oidc.js +130 -0
- package/dist/keyless/x509.d.ts +91 -0
- package/dist/keyless/x509.js +670 -0
- package/dist/portable/index.d.ts +5 -2
- package/dist/portable/index.js +4 -1
- package/docs/aaif-sandbox-thesis.md +51 -0
- package/docs/cli-contract.md +27 -2
- package/docs/intoto-predicate-v1.md +155 -0
- package/docs/ucan-biscuit.md +3 -1
- package/package.json +5 -1
- package/spec/intoto/mergeseal_receipt_v1.proto +47 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 code-signing certificates for a Fulcio-shaped keyless seal.
|
|
3
|
+
* The leaf SAN is the GitHub Actions workflow URI. The OIDC issuer is
|
|
4
|
+
* extension 1.3.6.1.4.1.57264.1.8, a DER UTF8String.
|
|
5
|
+
*
|
|
6
|
+
* Copyright 2026 MergeSeal
|
|
7
|
+
* MergeSeal <team@mergeseal.dev>
|
|
8
|
+
*/
|
|
9
|
+
import { X509Certificate } from "node:crypto";
|
|
10
|
+
import { bytesEqual, fromHex, utf8 } from "../v1_1/bytes.js";
|
|
11
|
+
import { sha256 } from "../v1_1/domain.js";
|
|
12
|
+
import { signMessage } from "../v1_1/ed25519.js";
|
|
13
|
+
import { derBitString, derBoolean, derInteger, derOid, derUtf8, encodePem, oidContent, parseDer, sequence, tlv, } from "./asn1.js";
|
|
14
|
+
import { KeylessUsageError, KeylessVerifyError } from "./errors.js";
|
|
15
|
+
const OID_ED25519 = "1.3.101.112";
|
|
16
|
+
const OID_CN = "2.5.4.3";
|
|
17
|
+
const OID_BASIC = "2.5.29.19";
|
|
18
|
+
const OID_KEY_USAGE = "2.5.29.15";
|
|
19
|
+
const OID_SAN = "2.5.29.17";
|
|
20
|
+
const OID_EKU = "2.5.29.37";
|
|
21
|
+
const OID_CODE_SIGNING = "1.3.6.1.5.5.7.3.3";
|
|
22
|
+
const OID_ISSUER_V1 = "1.3.6.1.4.1.57264.1.1";
|
|
23
|
+
const OID_ISSUER_V2 = "1.3.6.1.4.1.57264.1.8";
|
|
24
|
+
const OID_TOKEN_SUBJECT = "1.3.6.1.4.1.57264.1.24";
|
|
25
|
+
const ED_ALG = sequence([derOid(OID_ED25519)]);
|
|
26
|
+
export const CERTIFICATE_LIFETIME_MS = 10 * 60 * 1000;
|
|
27
|
+
export function certificateWindow(issuedAtSeconds) {
|
|
28
|
+
if (!Number.isSafeInteger(issuedAtSeconds) || issuedAtSeconds < 0) {
|
|
29
|
+
throw new KeylessUsageError("oidc iat is not a time");
|
|
30
|
+
}
|
|
31
|
+
const notBeforeMs = issuedAtSeconds * 1000;
|
|
32
|
+
return {
|
|
33
|
+
notBeforeMs,
|
|
34
|
+
notAfterMs: notBeforeMs + CERTIFICATE_LIFETIME_MS,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function createCertificateAuthority(input) {
|
|
38
|
+
assertTimes(input.notBeforeMs, input.notAfterMs);
|
|
39
|
+
const subject = directoryName(input.commonName);
|
|
40
|
+
const tbs = certificateBody({
|
|
41
|
+
serial: serialBytes("ca", input.publicKey, input.notBeforeMs, input.commonName),
|
|
42
|
+
issuer: subject,
|
|
43
|
+
subject,
|
|
44
|
+
notBeforeMs: input.notBeforeMs,
|
|
45
|
+
notAfterMs: input.notAfterMs,
|
|
46
|
+
publicKey: input.publicKey,
|
|
47
|
+
extensions: [
|
|
48
|
+
keyUsage(0x06, 1),
|
|
49
|
+
basicConstraints(true, input.pathLenConstraint),
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
const der = signCertificate(tbs, input.privateKey);
|
|
53
|
+
const pem = encodePem("CERTIFICATE", der);
|
|
54
|
+
assertIssued(pem, pem);
|
|
55
|
+
return {
|
|
56
|
+
commonName: input.commonName,
|
|
57
|
+
privateKey: input.privateKey,
|
|
58
|
+
certificateDer: der,
|
|
59
|
+
certificatePem: pem,
|
|
60
|
+
subjectDer: subject,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function issueCodeSigningCertificate(input) {
|
|
64
|
+
const issued = issueCodeSigningCertificateSignedBy({
|
|
65
|
+
issuerPrivateKey: input.issuer.privateKey,
|
|
66
|
+
issuerName: input.issuer.subjectDer,
|
|
67
|
+
issuerCertificatePem: input.issuer.certificatePem,
|
|
68
|
+
publicKey: input.publicKey,
|
|
69
|
+
certificateIdentity: input.certificateIdentity,
|
|
70
|
+
oidcIssuer: input.oidcIssuer,
|
|
71
|
+
tokenSubject: input.tokenSubject,
|
|
72
|
+
notBeforeMs: input.notBeforeMs,
|
|
73
|
+
notAfterMs: input.notAfterMs,
|
|
74
|
+
});
|
|
75
|
+
assertIssued(issued.pem, input.issuer.certificatePem);
|
|
76
|
+
return issued;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Subject name of a code-signing leaf. A forged child uses this as its
|
|
80
|
+
* issuer name when the signer is that leaf.
|
|
81
|
+
*/
|
|
82
|
+
export function codeSigningSubjectDer() {
|
|
83
|
+
return directoryName("keyless");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Signs a code-signing certificate. This does not decide that the issuer
|
|
87
|
+
* is a CA. Verification does.
|
|
88
|
+
*/
|
|
89
|
+
export function issueCodeSigningCertificateSignedBy(input) {
|
|
90
|
+
assertTimes(input.notBeforeMs, input.notAfterMs);
|
|
91
|
+
assertAscii(input.certificateIdentity, "certificate identity");
|
|
92
|
+
assertAscii(input.oidcIssuer, "oidc issuer");
|
|
93
|
+
const tbs = certificateBody({
|
|
94
|
+
serial: serialBytes("leaf", input.publicKey, input.notBeforeMs, input.certificateIdentity),
|
|
95
|
+
issuer: input.issuerName,
|
|
96
|
+
subject: codeSigningSubjectDer(),
|
|
97
|
+
notBeforeMs: input.notBeforeMs,
|
|
98
|
+
notAfterMs: input.notAfterMs,
|
|
99
|
+
publicKey: input.publicKey,
|
|
100
|
+
extensions: [
|
|
101
|
+
keyUsage(input.keyCertSign === true ? 0x84 : 0x80, input.keyCertSign === true ? 2 : 7),
|
|
102
|
+
basicConstraints(false, undefined),
|
|
103
|
+
extendedKeyUsage(),
|
|
104
|
+
subjectAltName(input.certificateIdentity),
|
|
105
|
+
utf8Extension(OID_ISSUER_V2, input.oidcIssuer),
|
|
106
|
+
utf8Extension(OID_TOKEN_SUBJECT, input.tokenSubject),
|
|
107
|
+
],
|
|
108
|
+
});
|
|
109
|
+
const der = signCertificate(tbs, input.issuerPrivateKey);
|
|
110
|
+
const pem = encodePem("CERTIFICATE", der);
|
|
111
|
+
return {
|
|
112
|
+
der,
|
|
113
|
+
pem,
|
|
114
|
+
notBeforeMs: input.notBeforeMs,
|
|
115
|
+
notAfterMs: input.notAfterMs,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** A CA certificate signed by another CA. The new key is `privateKey`. */
|
|
119
|
+
export function issueCaCertificate(input) {
|
|
120
|
+
assertTimes(input.notBeforeMs, input.notAfterMs);
|
|
121
|
+
const subject = directoryName(input.commonName);
|
|
122
|
+
const tbs = certificateBody({
|
|
123
|
+
serial: serialBytes("ica", input.publicKey, input.notBeforeMs, input.commonName),
|
|
124
|
+
issuer: input.issuer.subjectDer,
|
|
125
|
+
subject,
|
|
126
|
+
notBeforeMs: input.notBeforeMs,
|
|
127
|
+
notAfterMs: input.notAfterMs,
|
|
128
|
+
publicKey: input.publicKey,
|
|
129
|
+
extensions: [
|
|
130
|
+
keyUsage(0x06, 1),
|
|
131
|
+
basicConstraints(true, input.pathLenConstraint),
|
|
132
|
+
],
|
|
133
|
+
});
|
|
134
|
+
const der = signCertificate(tbs, input.issuer.privateKey);
|
|
135
|
+
const pem = encodePem("CERTIFICATE", der);
|
|
136
|
+
assertIssued(pem, input.issuer.certificatePem);
|
|
137
|
+
return {
|
|
138
|
+
commonName: input.commonName,
|
|
139
|
+
privateKey: input.privateKey,
|
|
140
|
+
certificateDer: der,
|
|
141
|
+
certificatePem: pem,
|
|
142
|
+
subjectDer: subject,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
export function publicKeyPem(publicKey) {
|
|
146
|
+
return encodePem("PUBLIC KEY", subjectPublicKey(publicKey));
|
|
147
|
+
}
|
|
148
|
+
export function splitCertificates(pem) {
|
|
149
|
+
const matches = pem.match(/-----BEGIN CERTIFICATE-----[\s\S]*?-----END CERTIFICATE-----/gu);
|
|
150
|
+
return matches ?? [];
|
|
151
|
+
}
|
|
152
|
+
/** The chain links, and the leaf was signed by the trusted root. */
|
|
153
|
+
export function chainTrusts(chain, rootPem, nowMs) {
|
|
154
|
+
if (!Number.isSafeInteger(nowMs) || nowMs < 0) {
|
|
155
|
+
throw new KeylessUsageError("now must be a millisecond timestamp");
|
|
156
|
+
}
|
|
157
|
+
const linked = linkChain(chain, nowMs);
|
|
158
|
+
const trusted = oneCertificate(rootPem);
|
|
159
|
+
if (!signedBy(linked.current, trusted)) {
|
|
160
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
161
|
+
}
|
|
162
|
+
if (!insideWindow(trusted, nowMs)) {
|
|
163
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
164
|
+
}
|
|
165
|
+
if (!sameCertificate(linked.current, trusted)) {
|
|
166
|
+
assertIssuer(trusted, linked.length - 1);
|
|
167
|
+
}
|
|
168
|
+
else if (linked.length === 1) {
|
|
169
|
+
assertIssuer(trusted, 0);
|
|
170
|
+
}
|
|
171
|
+
return linked.identity;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Leaf and parents form one chain. No external root. Time is checked
|
|
175
|
+
* only when `nowMs` is set, so a seal can attach a certificate whose
|
|
176
|
+
* window is fixed by the test token rather than the wall clock.
|
|
177
|
+
*/
|
|
178
|
+
export function linkedChain(chain, nowMs) {
|
|
179
|
+
return linkChain(chain, nowMs).identity;
|
|
180
|
+
}
|
|
181
|
+
function linkChain(chain, nowMs) {
|
|
182
|
+
if (chain.length === 0) {
|
|
183
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
184
|
+
}
|
|
185
|
+
const links = chain.map((pem) => oneCertificate(pem));
|
|
186
|
+
const leaf = links[0];
|
|
187
|
+
if (leaf === undefined) {
|
|
188
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
189
|
+
}
|
|
190
|
+
assertEndEntity(leaf);
|
|
191
|
+
let current = leaf;
|
|
192
|
+
for (let index = 1; index < links.length; index += 1) {
|
|
193
|
+
const parent = links[index];
|
|
194
|
+
if (parent === undefined || !signedBy(current, parent)) {
|
|
195
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
196
|
+
}
|
|
197
|
+
assertIssuer(parent, index - 1);
|
|
198
|
+
if (nowMs !== undefined && !insideWindow(parent, nowMs)) {
|
|
199
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
200
|
+
}
|
|
201
|
+
current = parent;
|
|
202
|
+
}
|
|
203
|
+
if (links.length === 1 && !signedBy(current, current)) {
|
|
204
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
205
|
+
}
|
|
206
|
+
const identity = readLeaf(leaf);
|
|
207
|
+
if (nowMs !== undefined &&
|
|
208
|
+
(nowMs < identity.notBeforeMs || nowMs >= identity.notAfterMs)) {
|
|
209
|
+
throw new KeylessVerifyError("expired");
|
|
210
|
+
}
|
|
211
|
+
return { identity, current, length: links.length };
|
|
212
|
+
}
|
|
213
|
+
function readLeaf(cert) {
|
|
214
|
+
let der;
|
|
215
|
+
try {
|
|
216
|
+
der = new Uint8Array(cert.raw);
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
220
|
+
}
|
|
221
|
+
const parsed = parseCertificate(der);
|
|
222
|
+
if (!parsed.digitalSignature || !parsed.codeSigning) {
|
|
223
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
224
|
+
}
|
|
225
|
+
if (parsed.issuer === undefined || parsed.names.length === 0) {
|
|
226
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
227
|
+
}
|
|
228
|
+
const publicKey = ed25519PublicKey(cert);
|
|
229
|
+
return {
|
|
230
|
+
names: parsed.names,
|
|
231
|
+
issuer: parsed.issuer,
|
|
232
|
+
notBeforeMs: parsed.notBeforeMs,
|
|
233
|
+
notAfterMs: parsed.notAfterMs,
|
|
234
|
+
publicKey,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function parseCertificate(der) {
|
|
238
|
+
const cert = parseDer(der);
|
|
239
|
+
if (cert.tag !== 0x30 || cert.children.length < 1) {
|
|
240
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
241
|
+
}
|
|
242
|
+
const body = cert.children[0];
|
|
243
|
+
if (body === undefined || body.tag !== 0x30) {
|
|
244
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
245
|
+
}
|
|
246
|
+
const validity = body.children[4];
|
|
247
|
+
const extensions = body.children.find((child) => child.tag === 0xa3);
|
|
248
|
+
if (validity === undefined ||
|
|
249
|
+
validity.children.length !== 2 ||
|
|
250
|
+
extensions === undefined ||
|
|
251
|
+
extensions.children.length !== 1) {
|
|
252
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
253
|
+
}
|
|
254
|
+
const notBefore = validity.children[0];
|
|
255
|
+
const notAfter = validity.children[1];
|
|
256
|
+
const extensionList = extensions.children[0];
|
|
257
|
+
if (notBefore === undefined ||
|
|
258
|
+
notAfter === undefined ||
|
|
259
|
+
extensionList === undefined) {
|
|
260
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
261
|
+
}
|
|
262
|
+
const fields = readExtensions(extensionList);
|
|
263
|
+
return {
|
|
264
|
+
names: fields.names,
|
|
265
|
+
issuer: fields.issuer,
|
|
266
|
+
notBeforeMs: readTime(notBefore),
|
|
267
|
+
notAfterMs: readTime(notAfter),
|
|
268
|
+
digitalSignature: fields.digitalSignature,
|
|
269
|
+
codeSigning: fields.codeSigning,
|
|
270
|
+
constraints: fields.constraints,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function readExtensions(list) {
|
|
274
|
+
if (list.tag !== 0x30) {
|
|
275
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
276
|
+
}
|
|
277
|
+
const names = [];
|
|
278
|
+
let issuerV1;
|
|
279
|
+
let issuerV2;
|
|
280
|
+
let digitalSignature = false;
|
|
281
|
+
let codeSigning = false;
|
|
282
|
+
let sawBasic = false;
|
|
283
|
+
let sawKeyUsage = false;
|
|
284
|
+
let ca = false;
|
|
285
|
+
let pathLen;
|
|
286
|
+
let keyCertSign = false;
|
|
287
|
+
for (const extension of list.children) {
|
|
288
|
+
const oid = extension.children[0];
|
|
289
|
+
if (extension.tag !== 0x30 || oid === undefined || oid.tag !== 0x06) {
|
|
290
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
291
|
+
}
|
|
292
|
+
const valueNode = extension.children[extension.children.length - 1];
|
|
293
|
+
if (valueNode === undefined || valueNode.tag !== 0x04) {
|
|
294
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
295
|
+
}
|
|
296
|
+
const value = valueNode.value;
|
|
297
|
+
if (oidEquals(oid, OID_SAN)) {
|
|
298
|
+
names.push(...readGeneralNames(value));
|
|
299
|
+
}
|
|
300
|
+
else if (oidEquals(oid, OID_ISSUER_V2)) {
|
|
301
|
+
issuerV2 = readUtf8String(value);
|
|
302
|
+
}
|
|
303
|
+
else if (oidEquals(oid, OID_ISSUER_V1)) {
|
|
304
|
+
issuerV1 = decodeUtf8(value);
|
|
305
|
+
}
|
|
306
|
+
else if (oidEquals(oid, OID_BASIC)) {
|
|
307
|
+
if (sawBasic) {
|
|
308
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
309
|
+
}
|
|
310
|
+
sawBasic = true;
|
|
311
|
+
const basic = readBasicConstraints(value);
|
|
312
|
+
ca = basic.ca;
|
|
313
|
+
pathLen = basic.pathLen;
|
|
314
|
+
}
|
|
315
|
+
else if (oidEquals(oid, OID_KEY_USAGE)) {
|
|
316
|
+
if (sawKeyUsage) {
|
|
317
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
318
|
+
}
|
|
319
|
+
sawKeyUsage = true;
|
|
320
|
+
digitalSignature = keyUsageBit(value, 0);
|
|
321
|
+
keyCertSign = keyUsageBit(value, 5);
|
|
322
|
+
}
|
|
323
|
+
else if (oidEquals(oid, OID_EKU)) {
|
|
324
|
+
codeSigning = ekuHasCodeSigning(value);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (issuerV1 !== undefined &&
|
|
328
|
+
issuerV2 !== undefined &&
|
|
329
|
+
issuerV1 !== issuerV2) {
|
|
330
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
names,
|
|
334
|
+
issuer: issuerV2 ?? issuerV1,
|
|
335
|
+
digitalSignature,
|
|
336
|
+
codeSigning,
|
|
337
|
+
constraints: {
|
|
338
|
+
ca,
|
|
339
|
+
pathLen,
|
|
340
|
+
hasKeyUsage: sawKeyUsage,
|
|
341
|
+
keyCertSign,
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function readBasicConstraints(value) {
|
|
346
|
+
const body = parseDer(value);
|
|
347
|
+
if (body.tag !== 0x30) {
|
|
348
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
349
|
+
}
|
|
350
|
+
let ca = false;
|
|
351
|
+
let index = 0;
|
|
352
|
+
const first = body.children[0];
|
|
353
|
+
if (first !== undefined && first.tag === 0x01) {
|
|
354
|
+
if (first.value.length !== 1) {
|
|
355
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
356
|
+
}
|
|
357
|
+
const bit = first.value[0] ?? 0;
|
|
358
|
+
if (bit !== 0x00 && bit !== 0xff) {
|
|
359
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
360
|
+
}
|
|
361
|
+
ca = bit === 0xff;
|
|
362
|
+
index = 1;
|
|
363
|
+
}
|
|
364
|
+
const next = body.children[index];
|
|
365
|
+
if (next === undefined) {
|
|
366
|
+
return { ca, pathLen: undefined };
|
|
367
|
+
}
|
|
368
|
+
if (!ca || next.tag !== 0x02 || body.children.length !== index + 1) {
|
|
369
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
370
|
+
}
|
|
371
|
+
return { ca, pathLen: readDerInteger(next.value) };
|
|
372
|
+
}
|
|
373
|
+
function readDerInteger(bytes) {
|
|
374
|
+
if (bytes.length < 1 || bytes.length > 4 || ((bytes[0] ?? 0) & 0x80) !== 0) {
|
|
375
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
376
|
+
}
|
|
377
|
+
let value = 0;
|
|
378
|
+
for (const byte of bytes) {
|
|
379
|
+
value = value * 256 + byte;
|
|
380
|
+
}
|
|
381
|
+
if (!Number.isSafeInteger(value)) {
|
|
382
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
383
|
+
}
|
|
384
|
+
return value;
|
|
385
|
+
}
|
|
386
|
+
function readGeneralNames(value) {
|
|
387
|
+
const names = parseDer(value);
|
|
388
|
+
if (names.tag !== 0x30) {
|
|
389
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
390
|
+
}
|
|
391
|
+
const found = [];
|
|
392
|
+
for (const name of names.children) {
|
|
393
|
+
if (name.tag === 0x86 || name.tag === 0x81) {
|
|
394
|
+
found.push(decodeAscii(name.value));
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return found;
|
|
398
|
+
}
|
|
399
|
+
function readUtf8String(value) {
|
|
400
|
+
const text = parseDer(value);
|
|
401
|
+
if (text.tag !== 0x0c) {
|
|
402
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
403
|
+
}
|
|
404
|
+
return decodeUtf8(text.value);
|
|
405
|
+
}
|
|
406
|
+
function keyUsageBit(value, bit) {
|
|
407
|
+
const bits = parseDer(value);
|
|
408
|
+
if (bits.tag !== 0x03 || bits.value.length < 2) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
const unused = bits.value[0] ?? 0;
|
|
412
|
+
if (unused > 7) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
const data = bits.value.subarray(1);
|
|
416
|
+
const totalBits = data.length * 8 - unused;
|
|
417
|
+
if (bit >= totalBits) {
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
const byte = data[bit >> 3] ?? 0;
|
|
421
|
+
const mask = 0x80 >> (bit & 7);
|
|
422
|
+
return (byte & mask) !== 0;
|
|
423
|
+
}
|
|
424
|
+
function constraintsOf(cert) {
|
|
425
|
+
return parseCertificate(new Uint8Array(cert.raw)).constraints;
|
|
426
|
+
}
|
|
427
|
+
/** An end-entity certificate must not be a CA. */
|
|
428
|
+
function assertEndEntity(cert) {
|
|
429
|
+
if (constraintsOf(cert).ca) {
|
|
430
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* RFC 5280: an issuer has cA true, keyCertSign, and room under
|
|
435
|
+
* pathLenConstraint for the intermediates below it.
|
|
436
|
+
*/
|
|
437
|
+
function assertIssuer(cert, intermediatesBelow) {
|
|
438
|
+
const constraints = constraintsOf(cert);
|
|
439
|
+
if (!constraints.ca || !constraints.hasKeyUsage || !constraints.keyCertSign) {
|
|
440
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
441
|
+
}
|
|
442
|
+
if (constraints.pathLen !== undefined &&
|
|
443
|
+
intermediatesBelow > constraints.pathLen) {
|
|
444
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function sameCertificate(left, right) {
|
|
448
|
+
return bytesEqual(new Uint8Array(left.raw), new Uint8Array(right.raw));
|
|
449
|
+
}
|
|
450
|
+
function ekuHasCodeSigning(value) {
|
|
451
|
+
const list = parseDer(value);
|
|
452
|
+
if (list.tag !== 0x30) {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
const want = oidContent(OID_CODE_SIGNING);
|
|
456
|
+
return list.children.some((child) => child.tag === 0x06 && bytesEqual(child.value, want));
|
|
457
|
+
}
|
|
458
|
+
function readTime(node) {
|
|
459
|
+
const text = decodeAscii(node.value);
|
|
460
|
+
let year = 0;
|
|
461
|
+
let rest = "";
|
|
462
|
+
if (node.tag === 0x17 && /^\d{12}Z$/u.test(text)) {
|
|
463
|
+
const yy = Number(text.slice(0, 2));
|
|
464
|
+
year = yy >= 50 ? 1900 + yy : 2000 + yy;
|
|
465
|
+
rest = text.slice(2);
|
|
466
|
+
}
|
|
467
|
+
else if (node.tag === 0x18 && /^\d{14}Z$/u.test(text)) {
|
|
468
|
+
year = Number(text.slice(0, 4));
|
|
469
|
+
rest = text.slice(4);
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
473
|
+
}
|
|
474
|
+
const month = Number(rest.slice(0, 2));
|
|
475
|
+
const day = Number(rest.slice(2, 4));
|
|
476
|
+
const hour = Number(rest.slice(4, 6));
|
|
477
|
+
const minute = Number(rest.slice(6, 8));
|
|
478
|
+
const second = Number(rest.slice(8, 10));
|
|
479
|
+
if (month < 1 ||
|
|
480
|
+
month > 12 ||
|
|
481
|
+
day < 1 ||
|
|
482
|
+
hour > 23 ||
|
|
483
|
+
minute > 59 ||
|
|
484
|
+
second > 60) {
|
|
485
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
486
|
+
}
|
|
487
|
+
const ms = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
488
|
+
const back = new Date(ms);
|
|
489
|
+
if (back.getUTCFullYear() !== year ||
|
|
490
|
+
back.getUTCMonth() !== month - 1 ||
|
|
491
|
+
back.getUTCDate() !== day) {
|
|
492
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
493
|
+
}
|
|
494
|
+
return ms;
|
|
495
|
+
}
|
|
496
|
+
function ed25519PublicKey(cert) {
|
|
497
|
+
const exported = cert.publicKey.export({ format: "der", type: "spki" });
|
|
498
|
+
const der = new Uint8Array(exported);
|
|
499
|
+
const prefix = fromHex("302a300506032b6570032100");
|
|
500
|
+
const head = der.subarray(0, prefix.length);
|
|
501
|
+
if (der.length !== prefix.length + 32 || !bytesEqual(head, prefix)) {
|
|
502
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
503
|
+
}
|
|
504
|
+
return der.subarray(prefix.length);
|
|
505
|
+
}
|
|
506
|
+
function oidEquals(node, dotted) {
|
|
507
|
+
return bytesEqual(node.value, oidContent(dotted));
|
|
508
|
+
}
|
|
509
|
+
function insideWindow(cert, nowMs) {
|
|
510
|
+
const from = Date.parse(cert.validFrom);
|
|
511
|
+
const to = Date.parse(cert.validTo);
|
|
512
|
+
if (!Number.isFinite(from) || !Number.isFinite(to)) {
|
|
513
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
514
|
+
}
|
|
515
|
+
return nowMs >= from && nowMs < to;
|
|
516
|
+
}
|
|
517
|
+
function oneCertificate(pem) {
|
|
518
|
+
const parts = splitCertificates(pem);
|
|
519
|
+
if (parts.length !== 1) {
|
|
520
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
521
|
+
}
|
|
522
|
+
const encoded = parts[0];
|
|
523
|
+
if (encoded === undefined) {
|
|
524
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
return new X509Certificate(encoded);
|
|
528
|
+
}
|
|
529
|
+
catch {
|
|
530
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
function assertIssued(childPem, parentPem) {
|
|
534
|
+
const child = new X509Certificate(childPem);
|
|
535
|
+
const parent = new X509Certificate(parentPem);
|
|
536
|
+
if (!signedBy(child, parent)) {
|
|
537
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
function signedBy(child, parent) {
|
|
541
|
+
try {
|
|
542
|
+
return child.checkIssued(parent) && child.verify(parent.publicKey);
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
function certificateBody(input) {
|
|
549
|
+
const version = tlv(0xa0, derInteger(Uint8Array.of(2)));
|
|
550
|
+
return sequence([
|
|
551
|
+
version,
|
|
552
|
+
derInteger(input.serial),
|
|
553
|
+
ED_ALG,
|
|
554
|
+
input.issuer,
|
|
555
|
+
sequence([derTime(input.notBeforeMs), derTime(input.notAfterMs)]),
|
|
556
|
+
input.subject,
|
|
557
|
+
subjectPublicKey(input.publicKey),
|
|
558
|
+
tlv(0xa3, sequence(input.extensions)),
|
|
559
|
+
]);
|
|
560
|
+
}
|
|
561
|
+
function signCertificate(body, privateKey) {
|
|
562
|
+
const signature = signMessage(privateKey, body);
|
|
563
|
+
if (signature.length !== 64) {
|
|
564
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
565
|
+
}
|
|
566
|
+
return sequence([body, ED_ALG, derBitString(signature)]);
|
|
567
|
+
}
|
|
568
|
+
function subjectPublicKey(publicKey) {
|
|
569
|
+
if (publicKey.length !== 32) {
|
|
570
|
+
throw new KeylessUsageError("ephemeral public key must be 32 bytes");
|
|
571
|
+
}
|
|
572
|
+
return sequence([ED_ALG, derBitString(publicKey)]);
|
|
573
|
+
}
|
|
574
|
+
function directoryName(commonName) {
|
|
575
|
+
const attribute = sequence([derOid(OID_CN), derUtf8(commonName)]);
|
|
576
|
+
return sequence([tlv(0x31, attribute)]);
|
|
577
|
+
}
|
|
578
|
+
function keyUsage(bits, unused) {
|
|
579
|
+
const bitString = tlv(0x03, Uint8Array.of(unused, bits));
|
|
580
|
+
return extension(OID_KEY_USAGE, true, bitString);
|
|
581
|
+
}
|
|
582
|
+
function basicConstraints(ca, pathLen) {
|
|
583
|
+
const parts = [];
|
|
584
|
+
if (ca) {
|
|
585
|
+
parts.push(derBoolean(true));
|
|
586
|
+
}
|
|
587
|
+
if (pathLen !== undefined) {
|
|
588
|
+
if (!ca || !Number.isSafeInteger(pathLen) || pathLen < 0 || pathLen > 255) {
|
|
589
|
+
throw new KeylessUsageError("pathLenConstraint requires a CA");
|
|
590
|
+
}
|
|
591
|
+
parts.push(derInteger(Uint8Array.of(pathLen)));
|
|
592
|
+
}
|
|
593
|
+
return extension(OID_BASIC, true, sequence(parts));
|
|
594
|
+
}
|
|
595
|
+
function extendedKeyUsage() {
|
|
596
|
+
return extension(OID_EKU, false, sequence([derOid(OID_CODE_SIGNING)]));
|
|
597
|
+
}
|
|
598
|
+
function subjectAltName(uri) {
|
|
599
|
+
return extension(OID_SAN, true, sequence([tlv(0x86, utf8(uri))]));
|
|
600
|
+
}
|
|
601
|
+
function utf8Extension(oid, text) {
|
|
602
|
+
return extension(oid, false, derUtf8(text));
|
|
603
|
+
}
|
|
604
|
+
function extension(oid, critical, value) {
|
|
605
|
+
const parts = [derOid(oid)];
|
|
606
|
+
if (critical) {
|
|
607
|
+
parts.push(derBoolean(true));
|
|
608
|
+
}
|
|
609
|
+
parts.push(tlv(0x04, value));
|
|
610
|
+
return sequence(parts);
|
|
611
|
+
}
|
|
612
|
+
function derTime(ms) {
|
|
613
|
+
const date = new Date(ms);
|
|
614
|
+
const year = date.getUTCFullYear();
|
|
615
|
+
const stamp = `${pad(date.getUTCMonth() + 1)}${pad(date.getUTCDate())}${pad(date.getUTCHours())}${pad(date.getUTCMinutes())}${pad(date.getUTCSeconds())}Z`;
|
|
616
|
+
if (year >= 1950 && year < 2050) {
|
|
617
|
+
return tlv(0x17, utf8(`${String(year).slice(2)}${stamp}`));
|
|
618
|
+
}
|
|
619
|
+
return tlv(0x18, utf8(`${String(year)}${stamp}`));
|
|
620
|
+
}
|
|
621
|
+
function serialBytes(label, publicKey, notBeforeMs, name) {
|
|
622
|
+
const digest = sha256([
|
|
623
|
+
utf8(label),
|
|
624
|
+
publicKey,
|
|
625
|
+
utf8(String(notBeforeMs)),
|
|
626
|
+
utf8(name),
|
|
627
|
+
]);
|
|
628
|
+
const serial = new Uint8Array(digest.subarray(0, 16));
|
|
629
|
+
serial[0] = (serial[0] ?? 0) & 0x7f;
|
|
630
|
+
if (serial.every((byte) => byte === 0)) {
|
|
631
|
+
serial[15] = 1;
|
|
632
|
+
}
|
|
633
|
+
return serial;
|
|
634
|
+
}
|
|
635
|
+
function assertTimes(notBeforeMs, notAfterMs) {
|
|
636
|
+
if (!Number.isSafeInteger(notBeforeMs) ||
|
|
637
|
+
!Number.isSafeInteger(notAfterMs) ||
|
|
638
|
+
notBeforeMs < 0 ||
|
|
639
|
+
notAfterMs <= notBeforeMs ||
|
|
640
|
+
notBeforeMs % 1000 !== 0 ||
|
|
641
|
+
notAfterMs % 1000 !== 0) {
|
|
642
|
+
throw new KeylessUsageError("certificate window is not whole seconds");
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
function assertAscii(text, label) {
|
|
646
|
+
if (text.length < 1 || text.length > 512 || /[^\x20-\x7e]/u.test(text)) {
|
|
647
|
+
throw new KeylessUsageError(`${label} is not a printable token`);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function pad(value) {
|
|
651
|
+
return String(value).padStart(2, "0");
|
|
652
|
+
}
|
|
653
|
+
function decodeUtf8(bytes) {
|
|
654
|
+
try {
|
|
655
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
656
|
+
}
|
|
657
|
+
catch {
|
|
658
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
function decodeAscii(bytes) {
|
|
662
|
+
let out = "";
|
|
663
|
+
for (const byte of bytes) {
|
|
664
|
+
if (byte < 0x20 || byte > 0x7e) {
|
|
665
|
+
throw new KeylessVerifyError("invalid_certificate");
|
|
666
|
+
}
|
|
667
|
+
out += String.fromCharCode(byte);
|
|
668
|
+
}
|
|
669
|
+
return out;
|
|
670
|
+
}
|
package/dist/portable/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Portable certificates: git trailer, host shims, header guard, and receipt.
|
|
3
3
|
* v1, v1.1, the coding gate, and the native registry are unchanged.
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2026 MergeSeal
|
|
6
|
+
* MergeSeal <team@mergeseal.dev>
|
|
4
7
|
*/
|
|
5
|
-
export type { InTotoStatement, VerifiedReceipt } from "../intoto.js";
|
|
6
|
-
export { IN_TOTO_STATEMENT_TYPE, MERGESEAL_PREDICATE_TYPE, toInTotoStatement, } from "../intoto.js";
|
|
8
|
+
export type { DsseEnvelope, DsseSignature, DsseSigner, DsseVerifier, InTotoStatement, VerifiedReceipt, } from "../intoto.js";
|
|
9
|
+
export { DSSE_PAYLOAD_TYPE, dssePreAuthEncoding, IN_TOTO_STATEMENT_TYPE, MERGESEAL_PREDICATE_TYPE, signDsseEnvelope, toInTotoStatement, verifyDsseEnvelope, } from "../intoto.js";
|
|
7
10
|
export { readCompact } from "./compact.js";
|
|
8
11
|
export type { AgentHost } from "./domain.js";
|
|
9
12
|
export { PortableError, PortableUsageError, RECEIPT_KIND, TRAILER_KEY, } from "./domain.js";
|
package/dist/portable/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Portable certificates: git trailer, host shims, header guard, and receipt.
|
|
3
3
|
* v1, v1.1, the coding gate, and the native registry are unchanged.
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2026 MergeSeal
|
|
6
|
+
* MergeSeal <team@mergeseal.dev>
|
|
4
7
|
*/
|
|
5
|
-
export { IN_TOTO_STATEMENT_TYPE, MERGESEAL_PREDICATE_TYPE, toInTotoStatement, } from "../intoto.js";
|
|
8
|
+
export { DSSE_PAYLOAD_TYPE, dssePreAuthEncoding, IN_TOTO_STATEMENT_TYPE, MERGESEAL_PREDICATE_TYPE, signDsseEnvelope, toInTotoStatement, verifyDsseEnvelope, } from "../intoto.js";
|
|
6
9
|
export { readCompact } from "./compact.js";
|
|
7
10
|
export { PortableError, PortableUsageError, RECEIPT_KIND, TRAILER_KEY, } from "./domain.js";
|
|
8
11
|
export { emitCertificate, fromClaudeCode, fromCli, fromCodex, fromCursor, fromWindsurf, normalizedSubject, } from "./hook.js";
|