@intellectual-frontiers/grc 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/assess.d.ts +247 -0
- package/dist/assess.js +289 -0
- package/dist/attest.d.ts +164 -0
- package/dist/attest.js +213 -0
- package/dist/evidence.d.ts +212 -0
- package/dist/evidence.js +453 -0
- package/dist/grc.d.ts +356 -0
- package/dist/grc.js +487 -0
- package/dist/host/grc.d.ts +159 -0
- package/dist/host/grc.js +358 -0
- package/dist/host/mod.d.ts +14 -0
- package/dist/host/mod.js +15 -0
- package/dist/host/sign.d.ts +141 -0
- package/dist/host/sign.js +282 -0
- package/dist/mod.d.ts +19 -0
- package/dist/mod.js +20 -0
- package/dist/ocsf-types.auto.d.ts +4374 -0
- package/dist/ocsf-types.auto.js +24 -0
- package/dist/ocsf.d.ts +160 -0
- package/dist/ocsf.js +257 -0
- package/dist/oscal.d.ts +356 -0
- package/dist/oscal.js +513 -0
- package/package.json +87 -4
- package/src/assess.ts +472 -0
- package/src/attest.ts +354 -0
- package/src/evidence.ts +551 -0
- package/src/grc.ts +771 -0
- package/src/host/grc.ts +435 -0
- package/src/host/mod.ts +15 -0
- package/src/host/sign.ts +364 -0
- package/src/mod.ts +20 -0
- package/src/ocsf-types.auto.ts +4680 -0
- package/src/ocsf.ts +471 -0
- package/src/oscal.ts +850 -0
package/src/host/sign.ts
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
// Spec: 0001-grc FR-020
|
|
2
|
+
/**
|
|
3
|
+
* Signing and verifying behind one interface (commons 0001-grc FR-020): a `Signer` turns bytes into a signature and the id of the key that
|
|
4
|
+
* made it, a `Verifier` says whether a signature over bytes holds. Two kinds stand behind it. `sshSigner` and `sshVerifier` run OpenSSH's
|
|
5
|
+
* `ssh-keygen -Y sign` and `ssh-keygen -Y verify` by their argument vector (the message on standard input, the signature an armored
|
|
6
|
+
* SSHSIG), with the namespace given, the private key a file the caller names and the allowed signers a file the caller names; nothing else
|
|
7
|
+
* is read, and the signature's temporary file is removed after use. `webCryptoSigner` and `webCryptoVerifier` sign with Web Crypto keys
|
|
8
|
+
* (Ed25519, ECDSA with DER signatures, RSA-PSS, RSASSA-PKCS1-v1_5) in the same form as `crypto/dsse`, so an envelope signed here
|
|
9
|
+
* verifies there and the other way round. `signEnvelopeWith` and `verifyEnvelopeWith` make and check DSSE envelopes with any of them, and
|
|
10
|
+
* `toSign` and `toVerifySignature` give the shapes `evidence.ts` takes for manifests and bundles.
|
|
11
|
+
*
|
|
12
|
+
* Failures expected in use (no `ssh-keygen`, a key that cannot sign) are `Result`s; a signature that does not hold is `ok(false)`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { sshSigner, sshVerifier } from "./sign.ts";
|
|
17
|
+
* const message = new TextEncoder().encode("release 1.2.3");
|
|
18
|
+
* const signed = await sshSigner({ keyFile: "ci_ed25519", namespace: "file" }).sign(message);
|
|
19
|
+
* if (signed.kind === "ok") {
|
|
20
|
+
* const v = sshVerifier({ allowedSignersFile: "allowed_signers", identity: "ci@example.com", namespace: "file" });
|
|
21
|
+
* await v.verify(message, signed.value.sig); // { kind: "ok", value: true }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @module
|
|
26
|
+
*/
|
|
27
|
+
import { absurd, err, ok, type Result } from "@intellectual-frontiers/core";
|
|
28
|
+
import { fromBase64, toBase64 } from "@intellectual-frontiers/formats/base64";
|
|
29
|
+
import { children, integer, readTlv, sequence, TAG } from "@intellectual-frontiers/formats/der";
|
|
30
|
+
import { type DsseAlgorithm, type DsseEnvelope, pae, signatureAlgorithm } from "@intellectual-frontiers/crypto/dsse";
|
|
31
|
+
import type { Sign, VerifySignature } from "../evidence.ts";
|
|
32
|
+
import { fromHex } from "@intellectual-frontiers/formats/hex";
|
|
33
|
+
import { sha256 } from "@intellectual-frontiers/crypto/sha256";
|
|
34
|
+
import { NotFound } from "@intellectual-frontiers/system/errors";
|
|
35
|
+
import { exists, withTempDir, writeFile } from "@intellectual-frontiers/system/fs";
|
|
36
|
+
import { join } from "@intellectual-frontiers/system/path";
|
|
37
|
+
import { output } from "@intellectual-frontiers/system/proc";
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------------------------------------------------------- the interface
|
|
40
|
+
|
|
41
|
+
/** Why signing or verifying could not be done (as opposed to a signature that does not hold). */
|
|
42
|
+
export type SignError =
|
|
43
|
+
| { readonly kind: "missing-program"; readonly program: string }
|
|
44
|
+
| { readonly kind: "key"; readonly reason: string }
|
|
45
|
+
| { readonly kind: "failed"; readonly reason: string };
|
|
46
|
+
|
|
47
|
+
/** A signature, and the id of the key that made it. */
|
|
48
|
+
export type Signature = { readonly keyid: string; readonly sig: Uint8Array };
|
|
49
|
+
|
|
50
|
+
/** A signer: bytes to a signature with the id of its key. */
|
|
51
|
+
export type Signer = {
|
|
52
|
+
/** How it signs. */
|
|
53
|
+
readonly kind: "ssh" | "web-crypto";
|
|
54
|
+
/** The signature over `message`, or why there is none. */
|
|
55
|
+
readonly sign: (message: Uint8Array) => Promise<Result<Signature, SignError>>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** A verifier: whether a signature over bytes holds (`ok(false)` where it does not), or why it could not be checked. */
|
|
59
|
+
export type Verifier = {
|
|
60
|
+
/** How it verifies. */
|
|
61
|
+
readonly kind: "ssh" | "web-crypto";
|
|
62
|
+
/** The id of the key it verifies with, where it has one: an envelope's signature naming another id is not tried with it. */
|
|
63
|
+
readonly keyid?: string;
|
|
64
|
+
/** Whether `sig` is a signature over `message`. */
|
|
65
|
+
readonly verify: (message: Uint8Array, sig: Uint8Array) => Promise<Result<boolean, SignError>>;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** A sign error as one sentence. */
|
|
69
|
+
export function formatSignError(e: SignError): string {
|
|
70
|
+
switch (e.kind) {
|
|
71
|
+
case "missing-program":
|
|
72
|
+
return `${e.program} is not installed (or not on PATH)`;
|
|
73
|
+
case "key":
|
|
74
|
+
return `the key cannot be used: ${e.reason}`;
|
|
75
|
+
case "failed":
|
|
76
|
+
return `signing failed: ${e.reason}`;
|
|
77
|
+
default:
|
|
78
|
+
return absurd(e);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A signer as the `Sign` that `evidence.ts` takes; that shape has no error, so a failure rejects with an `Error` naming it. */
|
|
83
|
+
export function toSign(signer: Signer): Sign {
|
|
84
|
+
return async (message) => {
|
|
85
|
+
const r = await signer.sign(message);
|
|
86
|
+
if (r.kind === "err") throw new Error(formatSignError(r.error));
|
|
87
|
+
return r.value.sig;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A verifier as the `VerifySignature` that `evidence.ts` takes; a check that cannot be done rejects, which it reports as a bad signature with the reason. */
|
|
92
|
+
export function toVerifySignature(verifier: Verifier): VerifySignature {
|
|
93
|
+
return async (message, signature) => {
|
|
94
|
+
const r = await verifier.verify(message, signature);
|
|
95
|
+
if (r.kind === "err") throw new Error(formatSignError(r.error));
|
|
96
|
+
return r.value;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------------------------------------------------------- DSSE
|
|
101
|
+
|
|
102
|
+
/** Why an envelope could not be signed: the place of the signer in the list given, and its error. */
|
|
103
|
+
export type EnvelopeSignError = { readonly kind: "signer"; readonly index: number; readonly error: SignError };
|
|
104
|
+
|
|
105
|
+
/** The payload of the type signed by every signer given, over DSSE's pre-authentication encoding, in an envelope with each key's id. */
|
|
106
|
+
export async function signEnvelopeWith(
|
|
107
|
+
payloadType: string,
|
|
108
|
+
payload: Uint8Array,
|
|
109
|
+
signers: readonly Signer[],
|
|
110
|
+
): Promise<Result<DsseEnvelope, EnvelopeSignError>> {
|
|
111
|
+
const message = pae(payloadType, payload);
|
|
112
|
+
const signatures: { readonly keyid: string; readonly sig: string }[] = [];
|
|
113
|
+
for (const [index, s] of signers.entries()) {
|
|
114
|
+
const r = await s.sign(message);
|
|
115
|
+
if (r.kind === "err") return err({ kind: "signer", index, error: r.error });
|
|
116
|
+
signatures.push({ keyid: r.value.keyid, sig: toBase64(r.value.sig) });
|
|
117
|
+
}
|
|
118
|
+
return ok({ payload: toBase64(payload), payloadType, signatures });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Why an envelope was refused. */
|
|
122
|
+
export type EnvelopeVerifyError =
|
|
123
|
+
| { readonly kind: "malformed"; readonly reason: string }
|
|
124
|
+
| { readonly kind: "payload-type"; readonly expected: string; readonly found: string }
|
|
125
|
+
| { readonly kind: "verifier"; readonly index: number; readonly error: SignError }
|
|
126
|
+
| { readonly kind: "no-valid-signature"; readonly signatures: number; readonly verified: number; readonly threshold: number };
|
|
127
|
+
|
|
128
|
+
/** What a verified envelope carries: its payload type, its payload, and the places of the verifiers that held a signature. */
|
|
129
|
+
export type VerifiedEnvelope = { readonly payloadType: string; readonly payload: Uint8Array; readonly verifiers: readonly number[] };
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The payload of an envelope of type `payloadType` of which at least `threshold` (1 by default) of the verifiers given each hold a
|
|
133
|
+
* signature. A signature naming a key id is tried only with verifiers of that id or of none.
|
|
134
|
+
*/
|
|
135
|
+
export async function verifyEnvelopeWith(
|
|
136
|
+
envelope: DsseEnvelope,
|
|
137
|
+
payloadType: string,
|
|
138
|
+
verifiers: readonly Verifier[],
|
|
139
|
+
threshold = 1,
|
|
140
|
+
): Promise<Result<VerifiedEnvelope, EnvelopeVerifyError>> {
|
|
141
|
+
if (envelope.payloadType !== payloadType) return err({ kind: "payload-type", expected: payloadType, found: envelope.payloadType });
|
|
142
|
+
const payload = fromBase64(envelope.payload);
|
|
143
|
+
if (payload.kind === "err") return err({ kind: "malformed", reason: `the payload: ${payload.error}` });
|
|
144
|
+
const sigs: Uint8Array[] = [];
|
|
145
|
+
for (const [i, s] of envelope.signatures.entries()) {
|
|
146
|
+
const b = fromBase64(s.sig);
|
|
147
|
+
if (b.kind === "err") return err({ kind: "malformed", reason: `signature ${i}: ${b.error}` });
|
|
148
|
+
sigs.push(b.value);
|
|
149
|
+
}
|
|
150
|
+
const message = pae(envelope.payloadType, payload.value);
|
|
151
|
+
const held: number[] = [];
|
|
152
|
+
for (const [index, v] of verifiers.entries()) {
|
|
153
|
+
for (const [i, s] of envelope.signatures.entries()) {
|
|
154
|
+
const bytes = sigs[i];
|
|
155
|
+
if (bytes === undefined || (s.keyid !== undefined && v.keyid !== undefined && s.keyid !== v.keyid)) continue;
|
|
156
|
+
const r = await v.verify(message, bytes);
|
|
157
|
+
if (r.kind === "err") return err({ kind: "verifier", index, error: r.error });
|
|
158
|
+
if (r.value) {
|
|
159
|
+
held.push(index);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const needed = Math.max(1, threshold);
|
|
165
|
+
if (held.length < needed) {
|
|
166
|
+
return err({ kind: "no-valid-signature", signatures: envelope.signatures.length, verified: held.length, threshold: needed });
|
|
167
|
+
}
|
|
168
|
+
return ok({ payloadType: envelope.payloadType, payload: payload.value, verifiers: held });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ---------------------------------------------------------------------------------------------------------------------------- ssh-keygen -Y
|
|
172
|
+
|
|
173
|
+
/** How `sshSigner` signs: the private key's file, the namespace (`file`, `git`, or one of the caller's own), and optionally the key id and the program. */
|
|
174
|
+
export type SshSignerOptions = {
|
|
175
|
+
/** The private key's file (unencrypted, or held by an agent for its public key); the only key file read. */
|
|
176
|
+
readonly keyFile: string;
|
|
177
|
+
/** The namespace the signature is bound to; a verifier must name the same one. */
|
|
178
|
+
readonly namespace: string;
|
|
179
|
+
/** The id a signature carries; by default the key's SHA-256 fingerprint (`SHA256:...`) as `ssh-keygen -l` shows it, read from the signature. */
|
|
180
|
+
readonly keyid?: string;
|
|
181
|
+
/** The program to run (`ssh-keygen` on PATH by default). */
|
|
182
|
+
readonly program?: string;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/** How `sshVerifier` verifies: the allowed signers file, the identity expected in it, the namespace, and optionally the program. */
|
|
186
|
+
export type SshVerifierOptions = {
|
|
187
|
+
/** The allowed signers file (`ssh-keygen(1)`'s ALLOWED SIGNERS format); the only key file read. */
|
|
188
|
+
readonly allowedSignersFile: string;
|
|
189
|
+
/** The principal the signature must be by, as the allowed signers file names it. */
|
|
190
|
+
readonly identity: string;
|
|
191
|
+
/** The namespace the signature must be bound to. */
|
|
192
|
+
readonly namespace: string;
|
|
193
|
+
/** The id of the key, where the caller knows it, to match an envelope's signatures by. */
|
|
194
|
+
readonly keyid?: string;
|
|
195
|
+
/** The program to run (`ssh-keygen` on PATH by default). */
|
|
196
|
+
readonly program?: string;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const SSHSIG_MAGIC = "SSHSIG";
|
|
200
|
+
|
|
201
|
+
/** The bytes inside an armored SSH signature (`-----BEGIN SSH SIGNATURE-----`), or undefined where it is not one. */
|
|
202
|
+
function sshsigBlob(armored: string): Uint8Array | undefined {
|
|
203
|
+
const m = /-----BEGIN SSH SIGNATURE-----([\s\S]*?)-----END SSH SIGNATURE-----/.exec(armored);
|
|
204
|
+
if (m === null) return undefined;
|
|
205
|
+
const b = fromBase64(m[1] ?? "");
|
|
206
|
+
return b.kind === "ok" ? b.value : undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The public key blob of an SSHSIG (magic, version, then the key as an SSH string), or undefined where it is not one. */
|
|
210
|
+
function sshsigPublicKey(blob: Uint8Array): Uint8Array | undefined {
|
|
211
|
+
const magic = new TextDecoder().decode(blob.subarray(0, SSHSIG_MAGIC.length));
|
|
212
|
+
if (magic !== SSHSIG_MAGIC || blob.length < 14) return undefined;
|
|
213
|
+
const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength);
|
|
214
|
+
const len = view.getUint32(10);
|
|
215
|
+
if (14 + len > blob.length) return undefined;
|
|
216
|
+
return blob.subarray(14, 14 + len);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The SHA-256 fingerprint of an SSH public key blob, as `ssh-keygen -l` writes it: `SHA256:` and unpadded base64. */
|
|
220
|
+
export async function sshFingerprint(publicKeyBlob: Uint8Array): Promise<string> {
|
|
221
|
+
const digest = fromHex(await sha256(publicKeyBlob)) ?? new Uint8Array(0);
|
|
222
|
+
return `SHA256:${toBase64(digest).replace(/=+$/, "")}`;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** What ssh-keygen said, trimmed, or its exit status where it said nothing. */
|
|
226
|
+
const said = (stderr: Uint8Array, code: number): string => new TextDecoder().decode(stderr).trim() || `exit status ${code}`;
|
|
227
|
+
|
|
228
|
+
/** A program run by its argument vector, or `missing-program` where it is not installed. */
|
|
229
|
+
async function runProgram(
|
|
230
|
+
argv: readonly [string, ...string[]],
|
|
231
|
+
input: Uint8Array,
|
|
232
|
+
): Promise<Result<{ readonly code: number; readonly stdout: Uint8Array; readonly stderr: Uint8Array }, SignError>> {
|
|
233
|
+
try {
|
|
234
|
+
const r = await output(argv, { input, env: { LC_ALL: "C" } });
|
|
235
|
+
return ok(r);
|
|
236
|
+
} catch (e) {
|
|
237
|
+
if (e instanceof NotFound) return err({ kind: "missing-program", program: argv[0] });
|
|
238
|
+
return err({ kind: "failed", reason: e instanceof Error ? e.message : String(e) });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A signer by `ssh-keygen -Y sign -f KEY -n NAMESPACE`, the message on standard input; its signatures are armored SSHSIG text as bytes. */
|
|
243
|
+
export function sshSigner(options: SshSignerOptions): Signer {
|
|
244
|
+
const program = options.program ?? "ssh-keygen";
|
|
245
|
+
return {
|
|
246
|
+
kind: "ssh",
|
|
247
|
+
async sign(message) {
|
|
248
|
+
if (!(await exists(options.keyFile))) return err({ kind: "key", reason: `no key file ${options.keyFile}` });
|
|
249
|
+
const r = await runProgram([program, "-Y", "sign", "-f", options.keyFile, "-n", options.namespace], message);
|
|
250
|
+
if (r.kind === "err") return r;
|
|
251
|
+
if (r.value.code !== 0) return err({ kind: "failed", reason: said(r.value.stderr, r.value.code) });
|
|
252
|
+
const armored = new TextDecoder().decode(r.value.stdout);
|
|
253
|
+
const blob = sshsigBlob(armored);
|
|
254
|
+
const key = blob === undefined ? undefined : sshsigPublicKey(blob);
|
|
255
|
+
if (key === undefined) return err({ kind: "failed", reason: `${program} wrote no SSH signature` });
|
|
256
|
+
return ok({ keyid: options.keyid ?? (await sshFingerprint(key)), sig: r.value.stdout });
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* A verifier by `ssh-keygen -Y verify -f ALLOWED -I IDENTITY -n NAMESPACE -s SIGFILE`, the message on standard input; the signature is
|
|
263
|
+
* written to a private temporary folder removed afterwards. A signature that does not hold (or is not by that identity, or not in that
|
|
264
|
+
* namespace) is `ok(false)`; an allowed signers file that is not there is a `key` error.
|
|
265
|
+
*/
|
|
266
|
+
export function sshVerifier(options: SshVerifierOptions): Verifier {
|
|
267
|
+
const program = options.program ?? "ssh-keygen";
|
|
268
|
+
const verify = async (message: Uint8Array, sig: Uint8Array): Promise<Result<boolean, SignError>> => {
|
|
269
|
+
if (!(await exists(options.allowedSignersFile)))
|
|
270
|
+
return err({ kind: "key", reason: `no allowed signers file ${options.allowedSignersFile}` });
|
|
271
|
+
if (sshsigBlob(new TextDecoder().decode(sig)) === undefined) return ok(false);
|
|
272
|
+
return withTempDir(
|
|
273
|
+
async (dir) => {
|
|
274
|
+
const sigFile = join(dir, "message.sig");
|
|
275
|
+
await writeFile(sigFile, sig);
|
|
276
|
+
const f = options.allowedSignersFile;
|
|
277
|
+
const r = await runProgram(
|
|
278
|
+
[program, "-Y", "verify", "-f", f, "-I", options.identity, "-n", options.namespace, "-s", sigFile],
|
|
279
|
+
message,
|
|
280
|
+
);
|
|
281
|
+
if (r.kind === "err") return r;
|
|
282
|
+
return ok(r.value.code === 0);
|
|
283
|
+
},
|
|
284
|
+
{ prefix: "ifc-sshsig-" },
|
|
285
|
+
);
|
|
286
|
+
};
|
|
287
|
+
return options.keyid === undefined ? { kind: "ssh", verify } : { kind: "ssh", keyid: options.keyid, verify };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ---------------------------------------------------------------------------------------------------------------------------- Web Crypto
|
|
291
|
+
|
|
292
|
+
const CURVE_BYTES = { "P-256": 32, "P-384": 48, "P-521": 66 } as const;
|
|
293
|
+
const CURVE_HASH = { "P-256": "SHA-256", "P-384": "SHA-384", "P-521": "SHA-512" } as const;
|
|
294
|
+
const HASH_BYTES = { "SHA-256": 32, "SHA-384": 48, "SHA-512": 64 } as const;
|
|
295
|
+
|
|
296
|
+
/** Web Crypto's parameters for an algorithm, as `crypto/dsse` signs with them. */
|
|
297
|
+
function params(a: DsseAlgorithm): AlgorithmIdentifier | EcdsaParams | RsaPssParams {
|
|
298
|
+
switch (a.kind) {
|
|
299
|
+
case "ed25519":
|
|
300
|
+
return { name: "Ed25519" };
|
|
301
|
+
case "ecdsa":
|
|
302
|
+
return { name: "ECDSA", hash: CURVE_HASH[a.curve] };
|
|
303
|
+
case "rsa-pss":
|
|
304
|
+
return { name: "RSA-PSS", saltLength: HASH_BYTES[a.hash] };
|
|
305
|
+
case "rsassa-pkcs1-v1_5":
|
|
306
|
+
return { name: "RSASSA-PKCS1-v1_5" };
|
|
307
|
+
default:
|
|
308
|
+
return absurd(a);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** An ASN.1 DER ECDSA signature as Web Crypto's r and s of `n` bytes each, or undefined where it is not one. */
|
|
313
|
+
function ecdsaFromDer(der: Uint8Array, n: number): Uint8Array<ArrayBuffer> | undefined {
|
|
314
|
+
const t = readTlv(der);
|
|
315
|
+
if (t.kind === "err" || t.value.tag !== TAG.sequence || t.value.raw.length !== der.length) return undefined;
|
|
316
|
+
const parts = children(t.value.content);
|
|
317
|
+
if (parts.kind === "err" || parts.value.length !== 2) return undefined;
|
|
318
|
+
const out = new Uint8Array(2 * n);
|
|
319
|
+
for (const [i, part] of parts.value.entries()) {
|
|
320
|
+
if (part.tag !== TAG.integer || ((part.content[0] ?? 0x80) & 0x80) !== 0) return undefined;
|
|
321
|
+
const magnitude = part.content[0] === 0 ? part.content.subarray(1) : part.content;
|
|
322
|
+
if (magnitude.length > n) return undefined;
|
|
323
|
+
out.set(magnitude, (i + 1) * n - magnitude.length);
|
|
324
|
+
}
|
|
325
|
+
return out;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const reason = (e: unknown): string => (e instanceof Error ? e.message : String(e));
|
|
329
|
+
|
|
330
|
+
/** A signer with a Web Crypto private key, its signatures in the form `crypto/dsse` writes (ECDSA as DER), each named by `keyid`. */
|
|
331
|
+
export function webCryptoSigner(privateKey: CryptoKey, keyid: string): Signer {
|
|
332
|
+
return {
|
|
333
|
+
kind: "web-crypto",
|
|
334
|
+
async sign(message) {
|
|
335
|
+
const a = signatureAlgorithm(privateKey);
|
|
336
|
+
if (a.kind === "err") return err({ kind: "key", reason: a.error });
|
|
337
|
+
let raw: Uint8Array;
|
|
338
|
+
try {
|
|
339
|
+
raw = new Uint8Array(await crypto.subtle.sign(params(a.value), privateKey, new Uint8Array(message)));
|
|
340
|
+
} catch (e) {
|
|
341
|
+
return err({ kind: "key", reason: reason(e) });
|
|
342
|
+
}
|
|
343
|
+
if (a.value.kind !== "ecdsa") return ok({ keyid, sig: raw });
|
|
344
|
+
const n = CURVE_BYTES[a.value.curve];
|
|
345
|
+
return ok({ keyid, sig: sequence(integer(raw.subarray(0, n)), integer(raw.subarray(n, 2 * n))) });
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** A verifier with a Web Crypto public key, of signatures in the form `crypto/dsse` writes; with `keyid`, matched by it in envelopes. */
|
|
351
|
+
export function webCryptoVerifier(publicKey: CryptoKey, keyid?: string): Verifier {
|
|
352
|
+
const verify = async (message: Uint8Array, sig: Uint8Array): Promise<Result<boolean, SignError>> => {
|
|
353
|
+
const a = signatureAlgorithm(publicKey);
|
|
354
|
+
if (a.kind === "err") return err({ kind: "key", reason: a.error });
|
|
355
|
+
const bytes = a.value.kind === "ecdsa" ? ecdsaFromDer(sig, CURVE_BYTES[a.value.curve]) : new Uint8Array(sig);
|
|
356
|
+
if (bytes === undefined) return ok(false);
|
|
357
|
+
try {
|
|
358
|
+
return ok(await crypto.subtle.verify(params(a.value), publicKey, bytes, new Uint8Array(message)));
|
|
359
|
+
} catch (e) {
|
|
360
|
+
return err({ kind: "key", reason: reason(e) });
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
return keyid === undefined ? { kind: "web-crypto", verify } : { kind: "web-crypto", keyid, verify };
|
|
364
|
+
}
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Spec: 0001-grc FR-001
|
|
2
|
+
/**
|
|
3
|
+
* GRC engineering as one typed library (commons 0007-grc, 0032-lifting-libraries): the pure modules as namespaces. `grc` gives catalogs, controls, verdicts, findings and waivers; `evidence` gives the canonical form of
|
|
4
|
+
* JSON, the hash-chained ledger and the manifests and bundles; `assess` gives coverage, freshness, drift, the change-impact gate and the risk register; `ocsf`, `oscal` and `attest` write the findings, the assessments and
|
|
5
|
+
* the attestations as the standards define them. The host (`@intellectual-frontiers/grc/host`) loads the settings of packs and runs them as signals, and signs.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { grc } from "@intellectual-frontiers/grc";
|
|
10
|
+
* grc.join("fail", "pass"); // "fail"
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
export * as assess from "./assess.ts";
|
|
16
|
+
export * as attest from "./attest.ts";
|
|
17
|
+
export * as evidence from "./evidence.ts";
|
|
18
|
+
export * as grc from "./grc.ts";
|
|
19
|
+
export * as ocsf from "./ocsf.ts";
|
|
20
|
+
export * as oscal from "./oscal.ts";
|