@intentius/chant 0.124.85 → 0.124.87
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/dist/bundle-check.d.ts +102 -0
- package/dist/bundle-check.d.ts.map +1 -0
- package/dist/bundle-check.js +310 -0
- package/dist/chant-install.d.ts +100 -0
- package/dist/chant-install.d.ts.map +1 -0
- package/dist/chant-install.js +158 -0
- package/dist/ci-commit.d.ts +19 -6
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +23 -4
- package/dist/ci-evidence-jobs.d.ts +78 -0
- package/dist/ci-evidence-jobs.d.ts.map +1 -0
- package/dist/ci-evidence-jobs.js +85 -0
- package/dist/ci-image.d.ts +274 -0
- package/dist/ci-image.d.ts.map +1 -0
- package/dist/ci-image.js +635 -0
- package/dist/ci-login.d.ts +150 -0
- package/dist/ci-login.d.ts.map +1 -0
- package/dist/ci-login.js +240 -0
- package/dist/ci-product.d.ts +16 -0
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +17 -0
- package/dist/cli/commands/import-index.js +2 -2
- package/dist/cli/handlers/build.d.ts +8 -0
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +22 -4
- package/dist/cli/handlers/ci-check.d.ts.map +1 -1
- package/dist/cli/handlers/ci-check.js +61 -2
- package/dist/cli/handlers/ci-evidence.d.ts +3 -0
- package/dist/cli/handlers/ci-evidence.d.ts.map +1 -0
- package/dist/cli/handlers/ci-evidence.js +174 -0
- package/dist/cli/handlers/components.js +3 -3
- package/dist/cli/handlers/dead-guards.d.ts +3 -0
- package/dist/cli/handlers/dead-guards.d.ts.map +1 -0
- package/dist/cli/handlers/dead-guards.js +35 -0
- package/dist/cli/handlers/graph.js +4 -4
- package/dist/cli/handlers/lifecycle.d.ts +2 -1
- package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
- package/dist/cli/handlers/lifecycle.js +5 -4
- package/dist/cli/handlers/local-chant.d.ts +16 -0
- package/dist/cli/handlers/local-chant.d.ts.map +1 -0
- package/dist/cli/handlers/local-chant.js +109 -0
- package/dist/cli/handlers/operator.js +2 -2
- package/dist/cli/handlers/search.js +5 -5
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +38 -0
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +28 -0
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/check-hooks.d.ts +49 -0
- package/dist/components/check-hooks.d.ts.map +1 -1
- package/dist/components/check-hooks.js +64 -4
- package/dist/components/check-steps.d.ts +17 -0
- package/dist/components/check-steps.d.ts.map +1 -1
- package/dist/components/check-steps.js +37 -5
- package/dist/components/product-pipeline.d.ts +23 -0
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +6 -2
- package/dist/components/product-plan.d.ts +143 -0
- package/dist/components/product-plan.d.ts.map +1 -0
- package/dist/components/product-plan.js +145 -0
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +2 -2
- package/dist/config.d.ts +39 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +35 -3
- package/dist/dead-guards.d.ts +82 -0
- package/dist/dead-guards.d.ts.map +1 -0
- package/dist/dead-guards.js +436 -0
- package/dist/discovery/files.d.ts +3 -1
- package/dist/discovery/files.d.ts.map +1 -1
- package/dist/discovery/files.js +5 -3
- package/dist/drift-issue.d.ts +152 -0
- package/dist/drift-issue.d.ts.map +1 -0
- package/dist/drift-issue.js +167 -0
- package/dist/floor-table.d.ts +30 -0
- package/dist/floor-table.d.ts.map +1 -0
- package/dist/floor-table.data.d.ts +3 -0
- package/dist/floor-table.data.d.ts.map +1 -0
- package/dist/floor-table.data.js +179 -0
- package/dist/floor-table.js +56 -0
- package/dist/forge-flags.d.ts +90 -0
- package/dist/forge-flags.d.ts.map +1 -0
- package/dist/forge-flags.js +184 -0
- package/dist/lexicon.d.ts +69 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.d.ts +7 -1
- package/dist/lifecycle/gated-op.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.js +7 -4
- package/dist/local-chant.d.ts +98 -0
- package/dist/local-chant.d.ts.map +1 -0
- package/dist/local-chant.js +307 -0
- package/dist/op/activities/predict-behaviour.js +2 -2
- package/dist/op/op-waves-evidence-spec.d.ts +13 -0
- package/dist/op/op-waves-evidence-spec.d.ts.map +1 -0
- package/dist/op/op-waves-evidence-spec.js +21 -0
- package/dist/op/op-waves-evidence.d.ts +40 -0
- package/dist/op/op-waves-evidence.d.ts.map +1 -0
- package/dist/op/op-waves-evidence.js +58 -0
- package/dist/op/op-waves-run.d.ts +4 -0
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +24 -1
- package/dist/op/op-waves.d.ts +17 -1
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +23 -0
- package/dist/op/schedule-overdue.d.ts +41 -9
- package/dist/op/schedule-overdue.d.ts.map +1 -1
- package/dist/op/schedule-overdue.js +154 -25
- package/dist/plan-summary.d.ts +25 -0
- package/dist/plan-summary.d.ts.map +1 -1
- package/dist/plan-summary.js +31 -0
- package/dist/pr-plan-note.d.ts +35 -0
- package/dist/pr-plan-note.d.ts.map +1 -1
- package/dist/pr-plan-note.js +10 -0
- package/dist/pr-post.d.ts +23 -0
- package/dist/pr-post.d.ts.map +1 -1
- package/dist/pr-post.js +125 -3
- package/dist/release-floors.d.ts +33 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +33 -1
- package/dist/state-migration.d.ts +24 -0
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +100 -5
- package/dist/wave-runtime.mjs +309 -302
- package/dist/workspace/ci-green.d.ts.map +1 -1
- package/dist/workspace/ci-green.js +4 -3
- package/dist/workspace/lexicon-outputs.d.ts.map +1 -1
- package/dist/workspace/lexicon-outputs.js +21 -7
- package/dist/workspace/reason-codes.d.ts +3 -0
- package/dist/workspace/reason-codes.d.ts.map +1 -1
- package/dist/workspace/reason-codes.js +4 -0
- package/dist/workspace/ref-gate.schema.json +85 -0
- package/dist/workspace/search-contract.d.ts +1 -1
- package/dist/workspace/search-contract.d.ts.map +1 -1
- package/dist/workspace/trust/check-evidence-report.d.ts +58 -0
- package/dist/workspace/trust/check-evidence-report.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence-report.js +109 -0
- package/dist/workspace/trust/check-evidence.d.ts +160 -0
- package/dist/workspace/trust/check-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence.js +309 -0
- package/dist/workspace/trust/ci-tag-statement.d.ts +2 -2
- package/dist/workspace/trust/ci-tag-statement.d.ts.map +1 -1
- package/dist/workspace/trust/dsse.d.ts +40 -2
- package/dist/workspace/trust/dsse.d.ts.map +1 -1
- package/dist/workspace/trust/dsse.js +34 -2
- package/dist/workspace/trust/evidence.d.ts +2 -2
- package/dist/workspace/trust/evidence.d.ts.map +1 -1
- package/dist/workspace/trust/keyless-fixture.d.ts +41 -0
- package/dist/workspace/trust/keyless-fixture.d.ts.map +1 -0
- package/dist/workspace/trust/keyless-fixture.js +161 -0
- package/dist/workspace/trust/keyless.d.ts +202 -0
- package/dist/workspace/trust/keyless.d.ts.map +1 -0
- package/dist/workspace/trust/keyless.js +488 -0
- package/dist/workspace/trust/local-evidence.d.ts +123 -0
- package/dist/workspace/trust/local-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/local-evidence.js +259 -0
- package/dist/workspace/trust/op-run-statement.d.ts +2 -2
- package/dist/workspace/trust/op-run-statement.d.ts.map +1 -1
- package/dist/workspace/trust/policy.d.ts +140 -1
- package/dist/workspace/trust/policy.d.ts.map +1 -1
- package/dist/workspace/trust/policy.js +197 -3
- package/dist/workspace/trust/ref-gate-cli.d.ts +33 -0
- package/dist/workspace/trust/ref-gate-cli.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-cli.js +234 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts +38 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-fixture.js +97 -0
- package/dist/workspace/trust/ref-gate.d.ts +187 -0
- package/dist/workspace/trust/ref-gate.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate.js +432 -0
- package/dist/workspace/trust/run-statement.d.ts +3 -3
- package/dist/workspace/trust/run-statement.d.ts.map +1 -1
- package/dist/workspace/trust/runner-identity.d.ts +56 -0
- package/dist/workspace/trust/runner-identity.d.ts.map +1 -0
- package/dist/workspace/trust/runner-identity.js +65 -0
- package/dist/workspace/trust/trust-files.d.ts.map +1 -1
- package/dist/workspace/trust/trust-files.js +25 -1
- package/package.json +1 -1
- package/src/bundle-check.test.ts +140 -0
- package/src/bundle-check.ts +352 -0
- package/src/chant-install.test.ts +112 -0
- package/src/chant-install.ts +179 -0
- package/src/ci-commit.test.ts +45 -0
- package/src/ci-commit.ts +38 -4
- package/src/ci-evidence-jobs.ts +105 -0
- package/src/ci-image.test.ts +506 -0
- package/src/ci-image.ts +822 -0
- package/src/ci-login.test.ts +235 -0
- package/src/ci-login.ts +307 -0
- package/src/ci-product.ts +18 -0
- package/src/cli/commands/__fixtures__/source-dir-include/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-root/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.test.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/app.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/infra/net.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/import-index.ts +2 -2
- package/src/cli/handlers/build-source-dir.test.ts +64 -0
- package/src/cli/handlers/build.ts +21 -4
- package/src/cli/handlers/ci-check.ts +60 -3
- package/src/cli/handlers/ci-evidence.ts +167 -0
- package/src/cli/handlers/components.ts +3 -3
- package/src/cli/handlers/dead-guards.ts +37 -0
- package/src/cli/handlers/graph.ts +4 -4
- package/src/cli/handlers/lifecycle.ts +5 -4
- package/src/cli/handlers/local-chant.ts +106 -0
- package/src/cli/handlers/operator.ts +2 -2
- package/src/cli/handlers/search.ts +5 -5
- package/src/cli/main.test.ts +7 -0
- package/src/cli/main.ts +38 -0
- package/src/cli/parse-args.ts +21 -0
- package/src/cli/registry.ts +16 -0
- package/src/components/check-evidence-stage.test.ts +80 -0
- package/src/components/check-hooks.ts +84 -4
- package/src/components/check-local-evidence-stage.test.ts +141 -0
- package/src/components/check-steps.ts +56 -6
- package/src/components/product-pipeline.ts +28 -3
- package/src/components/product-plan.test.ts +87 -0
- package/src/components/product-plan.ts +230 -0
- package/src/components/release-compile.ts +2 -2
- package/src/config.ts +44 -3
- package/src/dead-guards.test.ts +206 -0
- package/src/dead-guards.ts +446 -0
- package/src/discovery/files.ts +5 -3
- package/src/drift-issue-bundle.test.ts +31 -0
- package/src/drift-issue.test.ts +206 -0
- package/src/drift-issue.ts +274 -0
- package/src/fit-note.test.ts +44 -0
- package/src/floor-table.data.ts +181 -0
- package/src/floor-table.ts +77 -0
- package/src/forge-flags.test.ts +110 -0
- package/src/forge-flags.ts +208 -0
- package/src/gate-decision-bundle.test.ts +1 -0
- package/src/lexicon.ts +68 -0
- package/src/lifecycle/gated-op.ts +10 -5
- package/src/local-chant.e2e.test.ts +94 -0
- package/src/local-chant.test.ts +162 -0
- package/src/local-chant.ts +341 -0
- package/src/op/activities/predict-behaviour.ts +2 -2
- package/src/op/op-waves-evidence-spec.ts +24 -0
- package/src/op/op-waves-evidence.test.ts +130 -0
- package/src/op/op-waves-evidence.ts +58 -0
- package/src/op/op-waves-run.ts +25 -1
- package/src/op/op-waves.ts +31 -1
- package/src/op/schedule-overdue.test.ts +85 -0
- package/src/op/schedule-overdue.ts +162 -26
- package/src/plan-summary.ts +40 -0
- package/src/pr-plan-note.ts +34 -0
- package/src/pr-post-plan-note.test.ts +146 -0
- package/src/pr-post.ts +127 -3
- package/src/release-floors.ts +47 -1
- package/src/stage-hooks-bundle.test.ts +1 -0
- package/src/state-migration.test.ts +52 -1
- package/src/state-migration.ts +107 -5
- package/src/workspace/ci-green.ts +4 -3
- package/src/workspace/lexicon-outputs.ts +23 -6
- package/src/workspace/reason-codes.test.ts +3 -0
- package/src/workspace/reason-codes.ts +4 -0
- package/src/workspace/ref-gate.schema.json +85 -0
- package/src/workspace/trust/check-evidence-report.ts +115 -0
- package/src/workspace/trust/check-evidence.test.ts +133 -0
- package/src/workspace/trust/check-evidence.ts +327 -0
- package/src/workspace/trust/ci-tag-statement.ts +2 -2
- package/src/workspace/trust/dsse.ts +48 -4
- package/src/workspace/trust/evidence.ts +2 -2
- package/src/workspace/trust/keyless-fixture.ts +214 -0
- package/src/workspace/trust/keyless.test.ts +220 -0
- package/src/workspace/trust/keyless.ts +557 -0
- package/src/workspace/trust/local-evidence.test.ts +127 -0
- package/src/workspace/trust/local-evidence.ts +268 -0
- package/src/workspace/trust/op-run-statement.ts +2 -2
- package/src/workspace/trust/policy.ts +263 -3
- package/src/workspace/trust/ref-gate-cli.ts +244 -0
- package/src/workspace/trust/ref-gate-fixture.ts +125 -0
- package/src/workspace/trust/ref-gate.e2e.test.ts +76 -0
- package/src/workspace/trust/ref-gate.test.ts +247 -0
- package/src/workspace/trust/ref-gate.ts +539 -0
- package/src/workspace/trust/run-statement.ts +3 -3
- package/src/workspace/trust/runner-identity.ts +92 -0
- package/src/workspace/trust/trust-files.test.ts +22 -0
- package/src/workspace/trust/trust-files.ts +54 -1
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runner statements signed keyless from the CI job's OIDC identity (#4869).
|
|
3
|
+
*
|
|
4
|
+
* A runner key is a secret a project stores and rotates, and a leaked one
|
|
5
|
+
* signs anything until someone does. A keyless runner holds no key: the job
|
|
6
|
+
* trades its OIDC token for a short-lived certificate from a Fulcio
|
|
7
|
+
* certificate authority (sigstore's, or a private one), signs the DSSE
|
|
8
|
+
* envelope with a key it made for this run, and has the signature
|
|
9
|
+
* timestamped by an RFC 3161 timestamp authority. The envelope's signature
|
|
10
|
+
* then carries the certificate chain and the timestamp token:
|
|
11
|
+
*
|
|
12
|
+
* { keyid, sig, certificates: [<base64 DER>, ...], timestamp: <base64 DER> }
|
|
13
|
+
*
|
|
14
|
+
* `.chant/trust.json` at base lists which identities may sign
|
|
15
|
+
* (`keyless.identities`: an OIDC issuer and a subject pattern over the
|
|
16
|
+
* certificate's subject alternative name, which names the repository, the
|
|
17
|
+
* workflow file and the ref), and names the trusted root, a file in
|
|
18
|
+
* sigstore's `trusted_root.json` format holding the certificate
|
|
19
|
+
* authorities' and timestamp authorities' certificates. So verifying is
|
|
20
|
+
* offline: the chain is checked against the trusted root, the timestamp
|
|
21
|
+
* against the timestamp authorities, and the certificate's validity at the
|
|
22
|
+
* timestamp's time, never at the time of reading. A statement still
|
|
23
|
+
* verifies after its certificate expired.
|
|
24
|
+
*
|
|
25
|
+
* Nothing is sent to a transparency log.
|
|
26
|
+
*
|
|
27
|
+
* Every network call goes through an injected `fetch`, so the tests use a
|
|
28
|
+
* fake token, a fake certificate authority and a fake timestamp authority
|
|
29
|
+
* (./keyless-fixture.ts).
|
|
30
|
+
*/
|
|
31
|
+
import { createHash, createPublicKey, generateKeyPairSync, sign, verify, X509Certificate } from "node:crypto";
|
|
32
|
+
import { SIGNING_ID_TOKEN_VAR } from "./check-evidence-report.js";
|
|
33
|
+
/** The element at `at` in `buf`. Throws on a truncated or indefinite-length element. */
|
|
34
|
+
export function derRead(buf, at = 0) {
|
|
35
|
+
if (at + 2 > buf.length)
|
|
36
|
+
throw new Error("truncated DER");
|
|
37
|
+
const tag = buf[at];
|
|
38
|
+
let len = buf[at + 1];
|
|
39
|
+
let head = 2;
|
|
40
|
+
if (len & 0x80) {
|
|
41
|
+
const n = len & 0x7f;
|
|
42
|
+
if (n === 0 || n > 4 || at + 2 + n > buf.length)
|
|
43
|
+
throw new Error("unsupported DER length");
|
|
44
|
+
len = 0;
|
|
45
|
+
for (let i = 0; i < n; i++)
|
|
46
|
+
len = len * 256 + buf[at + 2 + i];
|
|
47
|
+
head = 2 + n;
|
|
48
|
+
}
|
|
49
|
+
const end = at + head + len;
|
|
50
|
+
if (end > buf.length)
|
|
51
|
+
throw new Error("truncated DER");
|
|
52
|
+
return { tag, raw: buf.subarray(at, end), value: buf.subarray(at + head, end), end };
|
|
53
|
+
}
|
|
54
|
+
/** The elements inside a constructed element. */
|
|
55
|
+
export function derChildren(node) {
|
|
56
|
+
const out = [];
|
|
57
|
+
let at = 0;
|
|
58
|
+
while (at < node.value.length) {
|
|
59
|
+
const c = derRead(node.value, at);
|
|
60
|
+
out.push(c);
|
|
61
|
+
at = c.end;
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
/** A DER element of `tag` holding `parts`. */
|
|
66
|
+
export function der(tag, ...parts) {
|
|
67
|
+
const body = Buffer.concat(parts);
|
|
68
|
+
const n = body.length;
|
|
69
|
+
const len = n < 0x80 ? Buffer.from([n]) : n < 0x100 ? Buffer.from([0x81, n]) : n < 0x10000 ? Buffer.from([0x82, n >> 8, n & 0xff]) : Buffer.from([0x83, n >> 16, (n >> 8) & 0xff, n & 0xff]);
|
|
70
|
+
return Buffer.concat([Buffer.from([tag]), len, body]);
|
|
71
|
+
}
|
|
72
|
+
export const DER = { SEQUENCE: 0x30, SET: 0x31, INTEGER: 0x02, BIT_STRING: 0x03, OCTET_STRING: 0x04, NULL: 0x05, OID: 0x06, UTF8: 0x0c, BOOLEAN: 0x01, UTC_TIME: 0x17, GENERALIZED_TIME: 0x18 };
|
|
73
|
+
/** An OBJECT IDENTIFIER's encoding. */
|
|
74
|
+
export function derOid(oid) {
|
|
75
|
+
const parts = oid.split(".").map(Number);
|
|
76
|
+
const bytes = [parts[0] * 40 + parts[1]];
|
|
77
|
+
for (const p of parts.slice(2)) {
|
|
78
|
+
const seven = [];
|
|
79
|
+
let v = p;
|
|
80
|
+
do {
|
|
81
|
+
seven.unshift(v & 0x7f);
|
|
82
|
+
v = Math.floor(v / 128);
|
|
83
|
+
} while (v > 0);
|
|
84
|
+
for (let i = 0; i < seven.length - 1; i++)
|
|
85
|
+
seven[i] |= 0x80;
|
|
86
|
+
bytes.push(...seven);
|
|
87
|
+
}
|
|
88
|
+
return der(DER.OID, Buffer.from(bytes));
|
|
89
|
+
}
|
|
90
|
+
/** The dotted form of an OBJECT IDENTIFIER's contents. */
|
|
91
|
+
export function oidOf(value) {
|
|
92
|
+
const out = [Math.floor(value[0] / 40), value[0] % 40];
|
|
93
|
+
let v = 0;
|
|
94
|
+
for (const b of value.subarray(1)) {
|
|
95
|
+
v = v * 128 + (b & 0x7f);
|
|
96
|
+
if (!(b & 0x80)) {
|
|
97
|
+
out.push(v);
|
|
98
|
+
v = 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return out.join(".");
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A non-negative INTEGER, in DER's minimal form: a Buffer's leading zero
|
|
105
|
+
* bytes are dropped (OpenSSL refuses `00 3a ...` as a bad INTEGER), and one
|
|
106
|
+
* zero byte is put back only when the first byte left has its top bit set.
|
|
107
|
+
*/
|
|
108
|
+
export function derInt(n) {
|
|
109
|
+
let hex = Buffer.isBuffer(n) ? "" : BigInt(n).toString(16);
|
|
110
|
+
if (hex.length % 2)
|
|
111
|
+
hex = `0${hex}`;
|
|
112
|
+
let b = Buffer.isBuffer(n) ? n : Buffer.from(hex, "hex");
|
|
113
|
+
let start = 0;
|
|
114
|
+
while (start < b.length - 1 && b[start] === 0)
|
|
115
|
+
start++;
|
|
116
|
+
b = b.subarray(start);
|
|
117
|
+
if (b.length === 0)
|
|
118
|
+
b = Buffer.from([0]);
|
|
119
|
+
if (b[0] & 0x80)
|
|
120
|
+
b = Buffer.concat([Buffer.from([0]), b]);
|
|
121
|
+
return der(DER.INTEGER, b);
|
|
122
|
+
}
|
|
123
|
+
export const OID = {
|
|
124
|
+
sha256: "2.16.840.1.101.3.4.2.1",
|
|
125
|
+
sha384: "2.16.840.1.101.3.4.2.2",
|
|
126
|
+
sha512: "2.16.840.1.101.3.4.2.3",
|
|
127
|
+
signedData: "1.2.840.113549.1.7.2",
|
|
128
|
+
tstInfo: "1.2.840.113549.1.9.16.1.4",
|
|
129
|
+
contentType: "1.2.840.113549.1.9.3",
|
|
130
|
+
messageDigest: "1.2.840.113549.1.9.4",
|
|
131
|
+
/** Fulcio's OIDC issuer extension, v1 (raw string) and v2 (DER UTF8String). */
|
|
132
|
+
fulcioIssuerV1: "1.3.6.1.4.1.57264.1.1",
|
|
133
|
+
fulcioIssuerV2: "1.3.6.1.4.1.57264.1.8",
|
|
134
|
+
codeSigning: "1.3.6.1.5.5.7.3.3",
|
|
135
|
+
timeStamping: "1.3.6.1.5.5.7.3.8",
|
|
136
|
+
};
|
|
137
|
+
const HASH_OF = { [OID.sha256]: "sha256", [OID.sha384]: "sha384", [OID.sha512]: "sha512" };
|
|
138
|
+
function authorities(list, name) {
|
|
139
|
+
if (list === undefined)
|
|
140
|
+
return [];
|
|
141
|
+
if (!Array.isArray(list))
|
|
142
|
+
throw new Error(`${name} is not a list`);
|
|
143
|
+
return list.map((a, i) => {
|
|
144
|
+
const raw = a?.certChain?.certificates;
|
|
145
|
+
if (!Array.isArray(raw) || raw.length === 0)
|
|
146
|
+
throw new Error(`${name}[${i}] has no certChain.certificates`);
|
|
147
|
+
const certs = raw.map((c, j) => {
|
|
148
|
+
if (typeof c?.rawBytes !== "string")
|
|
149
|
+
throw new Error(`${name}[${i}].certChain.certificates[${j}] has no rawBytes`);
|
|
150
|
+
return new X509Certificate(Buffer.from(c.rawBytes, "base64"));
|
|
151
|
+
});
|
|
152
|
+
const valid = a.validFor;
|
|
153
|
+
return {
|
|
154
|
+
certs,
|
|
155
|
+
...(typeof valid?.start === "string" ? { start: new Date(valid.start) } : {}),
|
|
156
|
+
...(typeof valid?.end === "string" ? { end: new Date(valid.end) } : {}),
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The certificate and timestamp authorities of a `trusted_root.json`
|
|
162
|
+
* (`certificateAuthorities` and `timestampAuthorities`, each a `certChain`
|
|
163
|
+
* of base64 DER certificates and an optional `validFor`). Throws when the
|
|
164
|
+
* file is not one, or names no certificate authority.
|
|
165
|
+
*/
|
|
166
|
+
export function parseTrustedRoot(text) {
|
|
167
|
+
const doc = JSON.parse(text);
|
|
168
|
+
const ca = authorities(doc.certificateAuthorities, "certificateAuthorities");
|
|
169
|
+
const tsa = authorities(doc.timestampAuthorities, "timestampAuthorities");
|
|
170
|
+
if (ca.length === 0)
|
|
171
|
+
throw new Error("it lists no certificateAuthorities");
|
|
172
|
+
if (tsa.length === 0)
|
|
173
|
+
throw new Error("it lists no timestampAuthorities, so no signature's time can be trusted");
|
|
174
|
+
return { ca, tsa };
|
|
175
|
+
}
|
|
176
|
+
/** A subject pattern as a regular expression: `**` any text, `*` any text without `/`. */
|
|
177
|
+
export function subjectPattern(pattern) {
|
|
178
|
+
const body = pattern
|
|
179
|
+
.split("**")
|
|
180
|
+
.map((part) => part.split("*").map((s) => s.replace(/[.+?^${}()|[\]\\]/g, "\\$&")).join("[^/]*"))
|
|
181
|
+
.join(".*");
|
|
182
|
+
return new RegExp(`^${body}$`);
|
|
183
|
+
}
|
|
184
|
+
/** The URIs and emails of a certificate's subject alternative name. */
|
|
185
|
+
export function certificateSubjects(cert) {
|
|
186
|
+
const san = cert.subjectAltName ?? "";
|
|
187
|
+
return san
|
|
188
|
+
.split(/, (?=[A-Za-z ]+:)/)
|
|
189
|
+
.map((s) => s.match(/^(?:URI|email):(.*)$/)?.[1])
|
|
190
|
+
.filter((s) => typeof s === "string" && s.length > 0)
|
|
191
|
+
.map((s) => (s.startsWith('"') && s.endsWith('"') ? JSON.parse(s) : s));
|
|
192
|
+
}
|
|
193
|
+
/** The extensions of a certificate, by OID: each one's OCTET STRING contents. */
|
|
194
|
+
function extensions(cert) {
|
|
195
|
+
const out = new Map();
|
|
196
|
+
const tbs = derChildren(derRead(cert.raw))[0];
|
|
197
|
+
const wrapper = derChildren(tbs).find((c) => c.tag === 0xa3);
|
|
198
|
+
if (!wrapper)
|
|
199
|
+
return out;
|
|
200
|
+
for (const ext of derChildren(derChildren(wrapper)[0])) {
|
|
201
|
+
const parts = derChildren(ext);
|
|
202
|
+
const value = parts[parts.length - 1];
|
|
203
|
+
out.set(oidOf(parts[0].value), value.value);
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
/** The OIDC issuer Fulcio wrote into the certificate, or undefined. */
|
|
208
|
+
export function certificateIssuer(cert) {
|
|
209
|
+
const ext = extensions(cert);
|
|
210
|
+
const v2 = ext.get(OID.fulcioIssuerV2);
|
|
211
|
+
if (v2)
|
|
212
|
+
return derRead(v2).value.toString("utf8");
|
|
213
|
+
return ext.get(OID.fulcioIssuerV1)?.toString("utf8");
|
|
214
|
+
}
|
|
215
|
+
// ── Chains and timestamps ────────────────────────────────────────────────────
|
|
216
|
+
const within = (cert, at) => new Date(cert.validFrom) <= at && at <= new Date(cert.validTo);
|
|
217
|
+
/**
|
|
218
|
+
* Whether `leaf` chains, through `given` and the authorities' own
|
|
219
|
+
* certificates, to one of `authorities` whose `validFor` covers `at`, with
|
|
220
|
+
* every certificate on the way valid at `at`. Every certificate of an
|
|
221
|
+
* authority's chain is trusted, as sigstore's clients treat them.
|
|
222
|
+
*/
|
|
223
|
+
export function chainsTo(leaf, given, list, at) {
|
|
224
|
+
for (const a of list) {
|
|
225
|
+
if ((a.start && at < a.start) || (a.end && at > a.end))
|
|
226
|
+
continue;
|
|
227
|
+
const anchors = new Set(a.certs.map((c) => c.fingerprint256));
|
|
228
|
+
const pool = [...given, ...a.certs];
|
|
229
|
+
let cur = leaf;
|
|
230
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
231
|
+
if (!within(cur, at))
|
|
232
|
+
break;
|
|
233
|
+
if (anchors.has(cur.fingerprint256))
|
|
234
|
+
return true;
|
|
235
|
+
const issuer = pool.find((p) => p.fingerprint256 !== cur.fingerprint256 && cur.checkIssued(p) && cur.verify(p.publicKey));
|
|
236
|
+
if (!issuer)
|
|
237
|
+
break;
|
|
238
|
+
cur = issuer;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
/** A GeneralizedTime's contents as a Date: `YYYYMMDDHHMMSS[.fff]Z`. */
|
|
244
|
+
function generalizedTime(value) {
|
|
245
|
+
const m = value.toString("ascii").match(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\.\d+)?Z$/);
|
|
246
|
+
if (!m)
|
|
247
|
+
throw new Error("the timestamp's time is not a GeneralizedTime in UTC");
|
|
248
|
+
return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6], m[7] ? Math.round(Number(m[7]) * 1000) : 0));
|
|
249
|
+
}
|
|
250
|
+
/** Read an RFC 3161 TimeStampToken (a CMS ContentInfo), or the TimeStampResp around one. */
|
|
251
|
+
function readTimestamp(bytes) {
|
|
252
|
+
let top = derRead(bytes);
|
|
253
|
+
let parts = derChildren(top);
|
|
254
|
+
// A TimeStampResp: status, then the token.
|
|
255
|
+
if (parts[0]?.tag === DER.SEQUENCE && parts[1]?.tag === DER.SEQUENCE) {
|
|
256
|
+
const status = derChildren(parts[0])[0];
|
|
257
|
+
if (!status || status.value.readUIntBE(0, status.value.length) > 1)
|
|
258
|
+
throw new Error("the timestamp authority did not grant the timestamp");
|
|
259
|
+
top = parts[1];
|
|
260
|
+
parts = derChildren(top);
|
|
261
|
+
}
|
|
262
|
+
if (parts[0]?.tag !== DER.OID || oidOf(parts[0].value) !== OID.signedData)
|
|
263
|
+
throw new Error("the timestamp is not CMS SignedData");
|
|
264
|
+
const signed = derChildren(derChildren(parts[1])[0]);
|
|
265
|
+
const encap = derChildren(signed[2]);
|
|
266
|
+
if (oidOf(encap[0].value) !== OID.tstInfo)
|
|
267
|
+
throw new Error("the timestamp's content is not a TSTInfo");
|
|
268
|
+
const eContent = derChildren(encap[1])[0].value;
|
|
269
|
+
const tst = derChildren(derRead(eContent));
|
|
270
|
+
const imprint = derChildren(tst[2]);
|
|
271
|
+
const imprintHash = HASH_OF[oidOf(derChildren(imprint[0])[0].value)];
|
|
272
|
+
if (!imprintHash)
|
|
273
|
+
throw new Error("the timestamp's message imprint uses a hash chant does not read");
|
|
274
|
+
const time = generalizedTime(tst[4].value);
|
|
275
|
+
const certField = signed.find((c) => c.tag === 0xa0);
|
|
276
|
+
const certs = certField ? derChildren(certField).filter((c) => c.tag === DER.SEQUENCE).map((c) => new X509Certificate(c.raw)) : [];
|
|
277
|
+
const signerInfos = signed[signed.length - 1];
|
|
278
|
+
if (signerInfos.tag !== DER.SET)
|
|
279
|
+
throw new Error("the timestamp has no signer");
|
|
280
|
+
const si = derChildren(derChildren(signerInfos)[0]);
|
|
281
|
+
const digest = HASH_OF[oidOf(derChildren(si[2])[0].value)];
|
|
282
|
+
const attrs = si.find((c) => c.tag === 0xa0);
|
|
283
|
+
const signature = si.find((c, i) => i > 3 && c.tag === DER.OCTET_STRING);
|
|
284
|
+
if (!digest || !attrs || !signature)
|
|
285
|
+
throw new Error("the timestamp's signer has no signed attributes or signature");
|
|
286
|
+
// The signature covers the attributes encoded as a SET, not as the [0] they are stored as.
|
|
287
|
+
const signedAttrs = Buffer.concat([Buffer.from([DER.SET]), attrs.raw.subarray(1)]);
|
|
288
|
+
const md = derChildren(attrs).find((a) => oidOf(derChildren(a)[0].value) === OID.messageDigest);
|
|
289
|
+
if (!md || !derChildren(derChildren(md)[1])[0].value.equals(createHash(digest).update(eContent).digest())) {
|
|
290
|
+
throw new Error("the timestamp's signed attributes do not match its TSTInfo");
|
|
291
|
+
}
|
|
292
|
+
return { time, imprintHash, imprint: imprint[1].value, eContent, digest, signedAttrs, signature: signature.value, certs };
|
|
293
|
+
}
|
|
294
|
+
const usableFor = (cert, eku) => !cert.keyUsage || cert.keyUsage.includes(eku);
|
|
295
|
+
/**
|
|
296
|
+
* The time a timestamp token vouches `signature` existed at, once the
|
|
297
|
+
* token's imprint is the signature's hash and a timestamp authority of
|
|
298
|
+
* `roots` signed it. Throws why not.
|
|
299
|
+
*/
|
|
300
|
+
export function verifyTimestamp(token, signature, roots) {
|
|
301
|
+
const t = readTimestamp(token);
|
|
302
|
+
if (!t.imprint.equals(createHash(t.imprintHash).update(signature).digest()))
|
|
303
|
+
throw new Error("the timestamp is for another signature");
|
|
304
|
+
const candidates = [...t.certs, ...roots.tsa.flatMap((a) => a.certs)];
|
|
305
|
+
const signer = candidates.find((c) => {
|
|
306
|
+
try {
|
|
307
|
+
return verify(c.publicKey.asymmetricKeyType === "ed25519" ? null : t.digest, t.signedAttrs, c.publicKey, t.signature);
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
if (!signer)
|
|
314
|
+
throw new Error("no timestamp authority's certificate verifies the timestamp's signature");
|
|
315
|
+
if (!usableFor(signer, OID.timeStamping))
|
|
316
|
+
throw new Error("the timestamp was signed by a certificate not issued for timestamping");
|
|
317
|
+
if (!chainsTo(signer, t.certs, roots.tsa, t.time))
|
|
318
|
+
throw new Error("the timestamp's signer does not chain to a timestamp authority of the trusted root");
|
|
319
|
+
return t.time;
|
|
320
|
+
}
|
|
321
|
+
export function isKeylessSignature(s) {
|
|
322
|
+
const k = s;
|
|
323
|
+
return (k !== null &&
|
|
324
|
+
typeof k === "object" &&
|
|
325
|
+
typeof k.sig === "string" &&
|
|
326
|
+
typeof k.timestamp === "string" &&
|
|
327
|
+
Array.isArray(k.certificates) &&
|
|
328
|
+
k.certificates.length > 0 &&
|
|
329
|
+
k.certificates.every((c) => typeof c === "string"));
|
|
330
|
+
}
|
|
331
|
+
/** The key id a keyless signature is given: its certificate's SHA-256 fingerprint. */
|
|
332
|
+
export function certificateKeyid(cert) {
|
|
333
|
+
return `x509:SHA256:${createHash("sha256").update(cert.raw).digest("base64").replace(/=+$/, "")}`;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Verify one keyless signature over `message` (the envelope's PAE) against
|
|
337
|
+
* `identities`. The certificate must chain to the trusted root and be valid
|
|
338
|
+
* at the timestamp's time, the timestamp must verify, and the certificate's
|
|
339
|
+
* issuer and subject must match one identity, whose principal is returned.
|
|
340
|
+
*/
|
|
341
|
+
export function verifyKeylessSignature(message, s, identities) {
|
|
342
|
+
if (identities.length === 0)
|
|
343
|
+
return { ok: false, named: false, reason: "the policy at base lists no keyless identity" };
|
|
344
|
+
let leaf;
|
|
345
|
+
let chain;
|
|
346
|
+
try {
|
|
347
|
+
[leaf, ...chain] = s.certificates.map((c) => new X509Certificate(Buffer.from(c, "base64")));
|
|
348
|
+
}
|
|
349
|
+
catch (err) {
|
|
350
|
+
return { ok: false, named: false, reason: `the signature's certificates do not parse: ${err instanceof Error ? err.message : String(err)}` };
|
|
351
|
+
}
|
|
352
|
+
const sig = Buffer.from(s.sig, "base64");
|
|
353
|
+
const pub = leaf.publicKey;
|
|
354
|
+
let good = false;
|
|
355
|
+
try {
|
|
356
|
+
good = verify(pub.asymmetricKeyType === "ed25519" ? null : "sha256", message, pub, sig);
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
good = false;
|
|
360
|
+
}
|
|
361
|
+
if (!good)
|
|
362
|
+
return { ok: false, named: false, reason: "the signature does not verify against its certificate's key" };
|
|
363
|
+
if (!usableFor(leaf, OID.codeSigning))
|
|
364
|
+
return { ok: false, named: false, reason: "the certificate was not issued for code signing" };
|
|
365
|
+
const issuer = certificateIssuer(leaf);
|
|
366
|
+
const subjects = certificateSubjects(leaf);
|
|
367
|
+
const said = `${issuer ?? "no issuer"} ${subjects.join(" ") || "no subject"}`;
|
|
368
|
+
const named = identities.filter((i) => i.issuer === issuer && subjects.some((s2) => subjectPattern(i.subject).test(s2)));
|
|
369
|
+
if (named.length === 0)
|
|
370
|
+
return { ok: false, named: false, reason: `the certificate's identity (${said}) is not one the policy at base lists under keyless.identities` };
|
|
371
|
+
const problems = [];
|
|
372
|
+
for (const id of named) {
|
|
373
|
+
let at;
|
|
374
|
+
try {
|
|
375
|
+
at = verifyTimestamp(Buffer.from(s.timestamp, "base64"), sig, id.roots);
|
|
376
|
+
}
|
|
377
|
+
catch (err) {
|
|
378
|
+
problems.push(err instanceof Error ? err.message : String(err));
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
if (!chainsTo(leaf, chain, id.roots.ca, at)) {
|
|
382
|
+
problems.push(`the certificate does not chain to a certificate authority of the trusted root, valid at ${at.toISOString()}`);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const subject = subjects.find((s2) => subjectPattern(id.subject).test(s2));
|
|
386
|
+
return { ok: true, principal: id.principal, keyid: certificateKeyid(leaf), issuer: issuer, subject, signedAt: at.toISOString() };
|
|
387
|
+
}
|
|
388
|
+
return { ok: false, named: true, reason: `the keyless signature by ${said} does not verify: ${[...new Set(problems)].join("; ")}` };
|
|
389
|
+
}
|
|
390
|
+
/** The verifier a policy's identity carries (./dsse.ts's `TrustedIdentity.verifyKeyless`). */
|
|
391
|
+
export function keylessVerifier(identity) {
|
|
392
|
+
return (message, signature) => (isKeylessSignature(signature) ? verifyKeylessSignature(message, signature, [identity]) : { ok: false, named: false, reason: "the signature is not keyless" });
|
|
393
|
+
}
|
|
394
|
+
// ── Sign ─────────────────────────────────────────────────────────────────────
|
|
395
|
+
/** The default certificate authority and timestamp authority: sigstore's public-good instances. */
|
|
396
|
+
export const DEFAULT_FULCIO_URL = "https://fulcio.sigstore.dev";
|
|
397
|
+
export const DEFAULT_TIMESTAMP_URL = "https://timestamp.sigstore.dev/api/v1/timestamp";
|
|
398
|
+
/** The audience the job's token is requested for, as Fulcio expects. */
|
|
399
|
+
export const DEFAULT_SIGNING_AUDIENCE = "sigstore";
|
|
400
|
+
export { SIGNING_ID_TOKEN_VAR };
|
|
401
|
+
/**
|
|
402
|
+
* The job's OIDC token for `audience`: GitLab's {@link SIGNING_ID_TOKEN_VAR},
|
|
403
|
+
* else the token request URL GitHub serves a job with `id-token: write`
|
|
404
|
+
* (and Forgejo 15 a job with `enable-openid-connect: true`), as the cloud
|
|
405
|
+
* login of ../../ci-login.ts takes it.
|
|
406
|
+
*/
|
|
407
|
+
export async function signingToken(env, audience, fetchFn) {
|
|
408
|
+
const given = env[SIGNING_ID_TOKEN_VAR];
|
|
409
|
+
if (given)
|
|
410
|
+
return given.trim();
|
|
411
|
+
const url = env.ACTIONS_ID_TOKEN_REQUEST_URL;
|
|
412
|
+
const bearer = env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
|
|
413
|
+
if (!url || !bearer) {
|
|
414
|
+
throw new Error(`the job has no OIDC token to sign with: GitLab needs id_tokens: ${SIGNING_ID_TOKEN_VAR}: aud: ${audience}; GitHub needs permissions: id-token: write; Forgejo 15 needs enable-openid-connect: true`);
|
|
415
|
+
}
|
|
416
|
+
const r = await fetchFn(`${url}${url.includes("?") ? "&" : "?"}audience=${encodeURIComponent(audience)}`, { headers: { authorization: `bearer ${bearer}`, accept: "application/json" } });
|
|
417
|
+
if (!r.ok)
|
|
418
|
+
throw new Error(`the OIDC token request answered ${r.status}`);
|
|
419
|
+
const value = (await r.json()).value;
|
|
420
|
+
if (typeof value !== "string" || !value)
|
|
421
|
+
throw new Error("the OIDC token request answered no token");
|
|
422
|
+
return value;
|
|
423
|
+
}
|
|
424
|
+
/** A JWT's claims, read without verifying: the certificate authority verifies the token. */
|
|
425
|
+
export function tokenClaims(token) {
|
|
426
|
+
const part = token.split(".")[1];
|
|
427
|
+
if (!part)
|
|
428
|
+
throw new Error("the OIDC token is not a JWT");
|
|
429
|
+
return JSON.parse(Buffer.from(part, "base64url").toString("utf8"));
|
|
430
|
+
}
|
|
431
|
+
const pemToDer = (pem) => Buffer.from(pem.replace(/-----(BEGIN|END) CERTIFICATE-----/g, "").replace(/\s+/g, ""), "base64");
|
|
432
|
+
/**
|
|
433
|
+
* Trade `token` for a certificate over a P-256 key made for this run, at
|
|
434
|
+
* Fulcio's `POST /api/v2/signingCert`. The proof of possession is the
|
|
435
|
+
* token's subject signed with that key.
|
|
436
|
+
*/
|
|
437
|
+
export async function requestCertificate(token, fulcioUrl, fetchFn) {
|
|
438
|
+
const sub = tokenClaims(token).sub;
|
|
439
|
+
if (typeof sub !== "string" || !sub)
|
|
440
|
+
throw new Error("the OIDC token has no subject");
|
|
441
|
+
const { privateKey, publicKey } = generateKeyPairSync("ec", { namedCurve: "P-256" });
|
|
442
|
+
const body = {
|
|
443
|
+
credentials: { oidcIdentityToken: token },
|
|
444
|
+
publicKeyRequest: {
|
|
445
|
+
publicKey: { algorithm: "ECDSA", content: publicKey.export({ format: "pem", type: "spki" }).toString() },
|
|
446
|
+
proofOfPossession: sign("sha256", Buffer.from(sub, "utf8"), privateKey).toString("base64"),
|
|
447
|
+
},
|
|
448
|
+
};
|
|
449
|
+
const r = await fetchFn(`${fulcioUrl.replace(/\/+$/, "")}/api/v2/signingCert`, { method: "POST", headers: { "content-type": "application/json", accept: "application/json" }, body: JSON.stringify(body) });
|
|
450
|
+
if (!r.ok)
|
|
451
|
+
throw new Error(`the certificate authority at ${fulcioUrl} answered ${r.status}: ${(await r.text()).slice(0, 200)}`);
|
|
452
|
+
const doc = (await r.json());
|
|
453
|
+
const pems = (doc.signedCertificateEmbeddedSct ?? doc.signedCertificateDetachedSct)?.chain?.certificates;
|
|
454
|
+
if (!Array.isArray(pems) || pems.length === 0 || !pems.every((p) => typeof p === "string"))
|
|
455
|
+
throw new Error(`the certificate authority at ${fulcioUrl} answered no certificate chain`);
|
|
456
|
+
const certificates = pems.map((p) => pemToDer(p).toString("base64"));
|
|
457
|
+
const leaf = new X509Certificate(Buffer.from(certificates[0], "base64"));
|
|
458
|
+
const spki = (k) => k.export({ format: "der", type: "spki" });
|
|
459
|
+
if (!spki(leaf.publicKey).equals(spki(createPublicKey(privateKey))))
|
|
460
|
+
throw new Error("the certificate authority certified another key");
|
|
461
|
+
return { key: privateKey, certificates };
|
|
462
|
+
}
|
|
463
|
+
/** An RFC 3161 TimeStampReq for the SHA-256 of `data`, asking for the authority's certificate. */
|
|
464
|
+
export function timestampRequest(data) {
|
|
465
|
+
const digest = createHash("sha256").update(data).digest();
|
|
466
|
+
return der(DER.SEQUENCE, derInt(1), der(DER.SEQUENCE, der(DER.SEQUENCE, derOid(OID.sha256), der(DER.NULL)), der(DER.OCTET_STRING, digest)), derInt(BigInt(`0x${createHash("sha256").update(digest).update(String(Date.now())).digest("hex").slice(0, 15)}`)), der(DER.BOOLEAN, Buffer.from([0xff])));
|
|
467
|
+
}
|
|
468
|
+
/** The TimeStampToken from a TimeStampResp. */
|
|
469
|
+
function tokenOfResponse(resp) {
|
|
470
|
+
const parts = derChildren(derRead(resp));
|
|
471
|
+
const status = parts[0] ? derChildren(parts[0])[0] : undefined;
|
|
472
|
+
if (!status || status.value.readUIntBE(0, status.value.length) > 1 || !parts[1])
|
|
473
|
+
throw new Error("the timestamp authority did not grant the timestamp");
|
|
474
|
+
return Buffer.from(parts[1].raw);
|
|
475
|
+
}
|
|
476
|
+
/** A timestamp token over `signature` from the authority at `url`. */
|
|
477
|
+
export async function requestTimestamp(signature, url, fetchFn) {
|
|
478
|
+
const r = await fetchFn(url, { method: "POST", headers: { "content-type": "application/timestamp-query", accept: "application/timestamp-reply" }, body: new Uint8Array(timestampRequest(signature)) });
|
|
479
|
+
if (!r.ok)
|
|
480
|
+
throw new Error(`the timestamp authority at ${url} answered ${r.status}`);
|
|
481
|
+
return tokenOfResponse(Buffer.from(await r.arrayBuffer()));
|
|
482
|
+
}
|
|
483
|
+
/** Sign `message` (an envelope's PAE) with `cert`'s key and have the signature timestamped. */
|
|
484
|
+
export async function keylessSignature(message, cert, timestampUrl, fetchFn) {
|
|
485
|
+
const sig = sign("sha256", message, cert.key);
|
|
486
|
+
const timestamp = await requestTimestamp(sig, timestampUrl, fetchFn);
|
|
487
|
+
return { keyid: certificateKeyid(new X509Certificate(Buffer.from(cert.certificates[0], "base64"))), sig: sig.toString("base64"), certificates: cert.certificates, timestamp: timestamp.toString("base64") };
|
|
488
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local evidence (#4868): a check proven on a developer's or an agent's
|
|
3
|
+
* machine, which CI accepts in place of running the check again when the
|
|
4
|
+
* policy at base says so.
|
|
5
|
+
*
|
|
6
|
+
* - The statement is the check statement of ./check-evidence.ts with its own
|
|
7
|
+
* predicate type ({@link LOCAL_EVIDENCE_PREDICATE}) and the signer's class
|
|
8
|
+
* (`person` or `agent`) in place of a runner. It binds the commit and its
|
|
9
|
+
* tree, and counts for any commit with the same tree, so a rebase or an
|
|
10
|
+
* amend that keeps the tree keeps it current.
|
|
11
|
+
* - It is signed by a key `local.keys` of `.chant/trust.json` lists at base,
|
|
12
|
+
* for a person or an agent session, and kept under its own notes ref,
|
|
13
|
+
* {@link LOCAL_EVIDENCE_NOTES_REF}. The ref gate (#4867) and an Op waves
|
|
14
|
+
* spec's `evidence.requires` read only `refs/notes/chant/evidence` and only
|
|
15
|
+
* runner keys, so local evidence never counts as runner evidence.
|
|
16
|
+
* - `local.checks` at base names each check it stands in for, and from whom
|
|
17
|
+
* (`from`, default people only). A check it does not name accepts none.
|
|
18
|
+
* The checks that gate an apply, a release or a gate
|
|
19
|
+
* (`LOCAL_EVIDENCE_NEVER`: plan, policy, apply, drift, ...) can't be named:
|
|
20
|
+
* the policy that names one does not parse, and nothing is accepted.
|
|
21
|
+
*
|
|
22
|
+
* The check runner (`chant ci check`, ../../components/check-steps.ts) reads
|
|
23
|
+
* the evidence per hook with {@link acceptedLocalEvidence} and skips a hook
|
|
24
|
+
* whose evidence is current and accepted, so a job that runs several hooks
|
|
25
|
+
* runs the rest and still passes.
|
|
26
|
+
*/
|
|
27
|
+
import { z } from "zod";
|
|
28
|
+
import { type DsseEnvelope } from "./dsse.js";
|
|
29
|
+
import { type EvidenceSigner, type SignedCheck } from "./check-evidence.js";
|
|
30
|
+
import { type LocalSignerClass } from "./policy.js";
|
|
31
|
+
/** The predicate type of a local check statement: never the runner evidence one. */
|
|
32
|
+
export declare const LOCAL_EVIDENCE_PREDICATE = "https://intentius.io/chant/local-evidence/v1";
|
|
33
|
+
/** Where local evidence is kept: a notes ref the ref gate never reads. */
|
|
34
|
+
export declare const LOCAL_EVIDENCE_NOTES_REF = "refs/notes/chant/local-evidence";
|
|
35
|
+
export { LOCAL_EVIDENCE_KEY_ENV } from "./check-evidence-report.js";
|
|
36
|
+
/** The statement a local key signs. Strict, as the runner statement is. */
|
|
37
|
+
export declare const localStatementSchema: z.ZodObject<{
|
|
38
|
+
_type: z.ZodLiteral<"https://in-toto.io/Statement/v1">;
|
|
39
|
+
subject: z.ZodArray<z.ZodNever>;
|
|
40
|
+
predicateType: z.ZodLiteral<"https://intentius.io/chant/local-evidence/v1">;
|
|
41
|
+
predicate: z.ZodObject<{
|
|
42
|
+
signer: z.ZodString;
|
|
43
|
+
class: z.ZodEnum<{
|
|
44
|
+
agent: "agent";
|
|
45
|
+
person: "person";
|
|
46
|
+
}>;
|
|
47
|
+
commit: z.ZodString;
|
|
48
|
+
tree: z.ZodString;
|
|
49
|
+
check: z.ZodString;
|
|
50
|
+
claim: z.ZodOptional<z.ZodObject<{
|
|
51
|
+
sha256: z.ZodString;
|
|
52
|
+
}, z.core.$strict>>;
|
|
53
|
+
}, z.core.$strict>;
|
|
54
|
+
}, z.core.$strict>;
|
|
55
|
+
export type LocalStatement = z.infer<typeof localStatementSchema>;
|
|
56
|
+
/** A local signer: the key's principal and class as `.chant/trust.json` at base lists them. */
|
|
57
|
+
export interface LocalEvidenceSigner extends EvidenceSigner {
|
|
58
|
+
class: LocalSignerClass;
|
|
59
|
+
/** A local key signs on the spot; only a keyless runner (#4869) waits on a service. */
|
|
60
|
+
sign(payloadType: string, payload: Buffer): DsseEnvelope;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The signer a local key makes, from `local.keys` of `.chant/trust.json` at
|
|
64
|
+
* `base`. Throws when the key is not an Ed25519 PEM key or is not listed
|
|
65
|
+
* there (a runner's key never is), so nobody keeps evidence CI would ignore.
|
|
66
|
+
*/
|
|
67
|
+
export declare function localKeyEvidenceSigner(repo: string, base: string, pem: string): LocalEvidenceSigner;
|
|
68
|
+
/** The local statement for one check over `commit`. */
|
|
69
|
+
export declare function localStatement(input: {
|
|
70
|
+
commit: string;
|
|
71
|
+
tree: string;
|
|
72
|
+
check: string;
|
|
73
|
+
signer: string;
|
|
74
|
+
class: LocalSignerClass;
|
|
75
|
+
claim?: Buffer | string;
|
|
76
|
+
}): LocalStatement;
|
|
77
|
+
/** Sign each of `checks` over `at` as local evidence. Writes nothing. */
|
|
78
|
+
export declare function signLocalChecks(repo: string, at: string, checks: readonly {
|
|
79
|
+
check: string;
|
|
80
|
+
claim?: Buffer | string;
|
|
81
|
+
}[], signer: LocalEvidenceSigner): SignedCheck[];
|
|
82
|
+
/** Attach local envelopes under {@link LOCAL_EVIDENCE_NOTES_REF} and push that ref, as `recordChecks` does for runner evidence. */
|
|
83
|
+
export declare function recordLocalChecks(repo: string, signed: readonly SignedCheck[], opts?: {
|
|
84
|
+
push?: boolean;
|
|
85
|
+
remote?: string;
|
|
86
|
+
}): Promise<{
|
|
87
|
+
pushed: boolean;
|
|
88
|
+
pushWarning?: string;
|
|
89
|
+
}>;
|
|
90
|
+
/**
|
|
91
|
+
* Fetch {@link LOCAL_EVIDENCE_NOTES_REF} from `remote` into the checkout, so
|
|
92
|
+
* a CI job reads what developers pushed. A checkout without the remote, or a
|
|
93
|
+
* remote without the ref, keeps what it has; returns whether the fetch landed.
|
|
94
|
+
*/
|
|
95
|
+
export declare function fetchLocalEvidence(repo: string, remote?: string): boolean;
|
|
96
|
+
/** What the local evidence comes to for one check at a commit's tree. */
|
|
97
|
+
export type LocalEvidenceVerdict = {
|
|
98
|
+
check: string;
|
|
99
|
+
status: "accepted";
|
|
100
|
+
signer: string;
|
|
101
|
+
class: LocalSignerClass;
|
|
102
|
+
commit: string;
|
|
103
|
+
} | {
|
|
104
|
+
check: string;
|
|
105
|
+
status: "not-accepted" | "missing" | "untrusted" | "refused";
|
|
106
|
+
reason: string;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Whether CI may skip each of `checks` at `at` on local evidence, judged by
|
|
110
|
+
* the policy at `base` (#4868):
|
|
111
|
+
*
|
|
112
|
+
* - `refused`: the check is one local evidence never stands in for, or the policy at base cannot be read.
|
|
113
|
+
* - `not-accepted`: `local.checks` at base does not name it (the default).
|
|
114
|
+
* - `missing`: nothing current is kept for the tree.
|
|
115
|
+
* - `untrusted`: an envelope is kept for it, and none verifies against a
|
|
116
|
+
* local key at base whose class the check accepts (a key the change adds
|
|
117
|
+
* itself, or an agent's key where the check takes people only).
|
|
118
|
+
* - `accepted`: an envelope for the check over `at`'s tree verifies, from a class the check takes.
|
|
119
|
+
*/
|
|
120
|
+
export declare function acceptedLocalEvidence(repo: string, at: string, base: string, checks: readonly string[]): LocalEvidenceVerdict[];
|
|
121
|
+
/** Whether the policy at `base` takes local evidence for any check, so a job knows whether to fetch the notes at all. */
|
|
122
|
+
export declare function acceptsLocalEvidence(repo: string, base: string): boolean;
|
|
123
|
+
//# sourceMappingURL=local-evidence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-evidence.d.ts","sourceRoot":"","sources":["../../../src/workspace/trust/local-evidence.ts"],"names":[],"mappings":"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"}
|