@hardkeel/benthic 0.0.0-stage → 1.0.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 +121 -0
- package/LICENSE +92 -0
- package/NOTICE +16 -0
- package/README.md +424 -2
- package/action.yml +54 -0
- package/dist/agility.js +80 -0
- package/dist/badge.js +62 -0
- package/dist/baseline.js +81 -0
- package/dist/cbom-diff.js +273 -0
- package/dist/cbom.js +418 -0
- package/dist/cli.js +834 -0
- package/dist/cloud-aws-services.js +375 -0
- package/dist/cloud-aws.js +354 -0
- package/dist/cloud-azure-services.js +73 -0
- package/dist/cloud-azure.js +144 -0
- package/dist/cloud-common.js +242 -0
- package/dist/cloud-gcp-services.js +90 -0
- package/dist/cloud-gcp.js +150 -0
- package/dist/comments.js +138 -0
- package/dist/community-rules.js +37 -0
- package/dist/community.js +30 -0
- package/dist/deps.js +406 -0
- package/dist/der.js +571 -0
- package/dist/evidence-cli.js +235 -0
- package/dist/evidence-live.js +109 -0
- package/dist/evidence.js +512 -0
- package/dist/ignore.js +90 -0
- package/dist/image.js +1224 -0
- package/dist/infra.js +635 -0
- package/dist/inventory.js +277 -0
- package/dist/map-activity.js +24 -0
- package/dist/map-areas.js +60 -0
- package/dist/map-cloud-keys.js +110 -0
- package/dist/map-cloud.js +34 -0
- package/dist/map-ct.js +107 -0
- package/dist/map-host.js +335 -0
- package/dist/map-network.js +520 -0
- package/dist/map-people.js +79 -0
- package/dist/map-rank.js +70 -0
- package/dist/map-report.js +304 -0
- package/dist/map-run.js +327 -0
- package/dist/map-sarif.js +61 -0
- package/dist/map-signin.js +247 -0
- package/dist/map-types.js +19 -0
- package/dist/map-web.js +666 -0
- package/dist/native.js +168 -0
- package/dist/policy.js +221 -0
- package/dist/pqc.js +46 -0
- package/dist/profile.js +65 -0
- package/dist/project.js +928 -0
- package/dist/prose.js +159 -0
- package/dist/protocols.js +207 -0
- package/dist/registry.js +326 -0
- package/dist/report.js +190 -0
- package/dist/rules.js +230 -0
- package/dist/sarif-common.js +30 -0
- package/dist/sarif.js +114 -0
- package/dist/scan.js +601 -0
- package/dist/score.js +124 -0
- package/dist/secrets.js +192 -0
- package/dist/share-config.js +129 -0
- package/dist/share-lists.js +19 -0
- package/dist/share.js +125 -0
- package/dist/sonar.js +83 -0
- package/dist/structure.js +218 -0
- package/dist/suites.js +402 -0
- package/dist/tls-check.js +122 -0
- package/dist/types.js +4 -0
- package/dist/walk.js +78 -0
- package/dist/yaml.js +173 -0
- package/package.json +73 -4
package/dist/der.js
ADDED
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Reads certificates, certificate requests, public keys and DH parameters committed as PEM, so the
|
|
4
|
+
// inventory shows what a key really is (RSA-2048, P-256, ML-DSA-65) instead of guessing from a label.
|
|
5
|
+
// PEM blocks are found in .pem and .crt files and inside JSON, YAML, code and config, including
|
|
6
|
+
// blocks written as one string with "\n" escapes. Private keys are left to the secret checks.
|
|
7
|
+
// Plain JavaScript only (atob, Uint8Array): this also runs in the Cloudflare Worker.
|
|
8
|
+
import { ALGORITHMS } from "./rules.js";
|
|
9
|
+
import { latticeLevelCaution } from "./agility.js";
|
|
10
|
+
function tlv(b, off) {
|
|
11
|
+
if (off + 2 > b.length)
|
|
12
|
+
return null;
|
|
13
|
+
const tag = b[off];
|
|
14
|
+
if ((tag & 0x1f) === 0x1f)
|
|
15
|
+
return null; // multi-byte tags never appear in what we read
|
|
16
|
+
let len = b[off + 1];
|
|
17
|
+
let p = off + 2;
|
|
18
|
+
if (len & 0x80) {
|
|
19
|
+
const n = len & 0x7f;
|
|
20
|
+
if (n === 0 || n > 4 || p + n > b.length)
|
|
21
|
+
return null;
|
|
22
|
+
len = 0;
|
|
23
|
+
for (let i = 0; i < n; i++)
|
|
24
|
+
len = len * 256 + b[p++];
|
|
25
|
+
}
|
|
26
|
+
if (p + len > b.length)
|
|
27
|
+
return null;
|
|
28
|
+
return { tag, start: p, end: p + len };
|
|
29
|
+
}
|
|
30
|
+
function kids(b, t) {
|
|
31
|
+
const out = [];
|
|
32
|
+
let p = t.start;
|
|
33
|
+
while (p < t.end) {
|
|
34
|
+
const c = tlv(b, p);
|
|
35
|
+
if (!c || c.end > t.end)
|
|
36
|
+
return [];
|
|
37
|
+
out.push(c);
|
|
38
|
+
p = c.end;
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
function oidOf(b, t) {
|
|
43
|
+
if (!t || t.tag !== 0x06 || t.end <= t.start)
|
|
44
|
+
return null;
|
|
45
|
+
const first = b[t.start];
|
|
46
|
+
const parts = [Math.min(2, Math.floor(first / 40)), first - 40 * Math.min(2, Math.floor(first / 40))];
|
|
47
|
+
let v = 0;
|
|
48
|
+
for (let i = t.start + 1; i < t.end; i++) {
|
|
49
|
+
v = v * 128 + (b[i] & 0x7f);
|
|
50
|
+
if (!(b[i] & 0x80)) {
|
|
51
|
+
parts.push(v);
|
|
52
|
+
v = 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return parts.join(".");
|
|
56
|
+
}
|
|
57
|
+
/** Bit length of a DER INTEGER (a modulus or a prime). */
|
|
58
|
+
function intBits(b, t) {
|
|
59
|
+
if (!t || t.tag !== 0x02)
|
|
60
|
+
return undefined;
|
|
61
|
+
let i = t.start;
|
|
62
|
+
while (i < t.end && b[i] === 0)
|
|
63
|
+
i++;
|
|
64
|
+
if (i >= t.end)
|
|
65
|
+
return 0;
|
|
66
|
+
return (t.end - i - 1) * 8 + (32 - Math.clz32(b[i]));
|
|
67
|
+
}
|
|
68
|
+
function text(b, t) {
|
|
69
|
+
const bytes = b.subarray(t.start, t.end);
|
|
70
|
+
if (t.tag === 0x1e) {
|
|
71
|
+
let s = "";
|
|
72
|
+
for (let i = 0; i + 1 < bytes.length; i += 2)
|
|
73
|
+
s += String.fromCharCode((bytes[i] << 8) | bytes[i + 1]);
|
|
74
|
+
return s;
|
|
75
|
+
}
|
|
76
|
+
return new TextDecoder("utf-8", { fatal: false }).decode(bytes);
|
|
77
|
+
}
|
|
78
|
+
const NAME_PARTS = { "2.5.4.3": "CN", "2.5.4.10": "O", "2.5.4.11": "OU", "2.5.4.6": "C" };
|
|
79
|
+
/** "CN=api.example.com, O=Example" from an X.501 Name. */
|
|
80
|
+
function nameOf(b, t) {
|
|
81
|
+
if (!t || t.tag !== 0x30)
|
|
82
|
+
return undefined;
|
|
83
|
+
const out = [];
|
|
84
|
+
for (const set of kids(b, t)) {
|
|
85
|
+
for (const atv of kids(b, set)) {
|
|
86
|
+
const [o, v] = kids(b, atv);
|
|
87
|
+
const key = NAME_PARTS[oidOf(b, o) ?? ""];
|
|
88
|
+
if (key && v)
|
|
89
|
+
out.push(`${key}=${text(b, v).slice(0, 120)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return out.length ? out.join(", ") : undefined;
|
|
93
|
+
}
|
|
94
|
+
function timeOf(b, t) {
|
|
95
|
+
if (!t || (t.tag !== 0x17 && t.tag !== 0x18))
|
|
96
|
+
return undefined;
|
|
97
|
+
const s = text(b, t);
|
|
98
|
+
const m = t.tag === 0x17 ? s.match(/^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})?Z$/) : s.match(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})?(?:\.\d+)?Z$/);
|
|
99
|
+
if (!m)
|
|
100
|
+
return undefined;
|
|
101
|
+
const year = t.tag === 0x17 ? (Number(m[1]) >= 50 ? 1900 : 2000) + Number(m[1]) : Number(m[1]);
|
|
102
|
+
return `${String(year).padStart(4, "0")}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6] ?? "00"}Z`;
|
|
103
|
+
}
|
|
104
|
+
// ---------- what an OID means ----------
|
|
105
|
+
const CURVES = {
|
|
106
|
+
"1.2.840.10045.3.1.1": "P-192",
|
|
107
|
+
"1.3.132.0.33": "P-224",
|
|
108
|
+
"1.2.840.10045.3.1.7": "P-256",
|
|
109
|
+
"1.3.132.0.34": "P-384",
|
|
110
|
+
"1.3.132.0.35": "P-521",
|
|
111
|
+
"1.3.132.0.10": "secp256k1",
|
|
112
|
+
"1.3.36.3.3.2.8.1.1.7": "brainpoolP256r1",
|
|
113
|
+
"1.3.36.3.3.2.8.1.1.11": "brainpoolP384r1",
|
|
114
|
+
"1.3.36.3.3.2.8.1.1.13": "brainpoolP512r1",
|
|
115
|
+
"1.2.156.10197.1.301": "SM2",
|
|
116
|
+
};
|
|
117
|
+
/** Classical security in bits (NIST SP 800-57 Part 1, table 2), for the CBOM. */
|
|
118
|
+
export function classicalBits(algorithm, keySize, curve) {
|
|
119
|
+
if (curve) {
|
|
120
|
+
const c = { "P-192": 96, "P-224": 112, "P-256": 128, "P-384": 192, "P-521": 256, secp256k1: 128, brainpoolP256r1: 128, brainpoolP384r1: 192, brainpoolP512r1: 256, SM2: 128 };
|
|
121
|
+
if (c[curve] !== undefined)
|
|
122
|
+
return c[curve];
|
|
123
|
+
}
|
|
124
|
+
if (/^(Ed25519|X25519)$/.test(algorithm))
|
|
125
|
+
return 128;
|
|
126
|
+
if (/^(Ed448|X448)$/.test(algorithm))
|
|
127
|
+
return 224;
|
|
128
|
+
if (keySize && /^(RSA|DH|DSA)/.test(algorithm)) {
|
|
129
|
+
if (keySize >= 15360)
|
|
130
|
+
return 256;
|
|
131
|
+
if (keySize >= 7680)
|
|
132
|
+
return 192;
|
|
133
|
+
if (keySize >= 3072)
|
|
134
|
+
return 128;
|
|
135
|
+
if (keySize >= 2048)
|
|
136
|
+
return 112;
|
|
137
|
+
if (keySize >= 1024)
|
|
138
|
+
return 80;
|
|
139
|
+
return 0;
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
const PQ_SIG = {
|
|
144
|
+
"2.16.840.1.101.3.4.3.17": ["ML-DSA-44", 2],
|
|
145
|
+
"2.16.840.1.101.3.4.3.18": ["ML-DSA-65", 3],
|
|
146
|
+
"2.16.840.1.101.3.4.3.19": ["ML-DSA-87", 5],
|
|
147
|
+
};
|
|
148
|
+
const SLH = ["SHA2-128s", "SHA2-128f", "SHA2-192s", "SHA2-192f", "SHA2-256s", "SHA2-256f", "SHAKE-128s", "SHAKE-128f", "SHAKE-192s", "SHAKE-192f", "SHAKE-256s", "SHAKE-256f"];
|
|
149
|
+
for (let i = 0; i < SLH.length; i++)
|
|
150
|
+
PQ_SIG[`2.16.840.1.101.3.4.3.${20 + i}`] = [`SLH-DSA-${SLH[i]}`, { 128: 1, 192: 3, 256: 5 }[Number(SLH[i].match(/\d{3}/)[0])]];
|
|
151
|
+
const PQ_KEM = {
|
|
152
|
+
"2.16.840.1.101.3.4.4.1": ["ML-KEM-512", 1],
|
|
153
|
+
"2.16.840.1.101.3.4.4.2": ["ML-KEM-768", 3],
|
|
154
|
+
"2.16.840.1.101.3.4.4.3": ["ML-KEM-1024", 5],
|
|
155
|
+
};
|
|
156
|
+
const STATEFUL = {
|
|
157
|
+
"1.2.840.113549.1.9.16.3.17": "HSS/LMS",
|
|
158
|
+
"1.3.6.1.5.5.7.6.34": "XMSS",
|
|
159
|
+
"1.3.6.1.5.5.7.6.35": "XMSS^MT",
|
|
160
|
+
};
|
|
161
|
+
export const SIGNATURE_OIDS = {
|
|
162
|
+
"1.2.840.113549.1.1.2": "md2WithRSAEncryption",
|
|
163
|
+
"1.2.840.113549.1.1.4": "md5WithRSAEncryption",
|
|
164
|
+
"1.2.840.113549.1.1.5": "sha1WithRSAEncryption",
|
|
165
|
+
"1.2.840.113549.1.1.10": "RSASSA-PSS",
|
|
166
|
+
"1.2.840.113549.1.1.11": "sha256WithRSAEncryption",
|
|
167
|
+
"1.2.840.113549.1.1.12": "sha384WithRSAEncryption",
|
|
168
|
+
"1.2.840.113549.1.1.13": "sha512WithRSAEncryption",
|
|
169
|
+
"1.2.840.113549.1.1.14": "sha224WithRSAEncryption",
|
|
170
|
+
"1.2.840.10045.4.1": "ecdsa-with-SHA1",
|
|
171
|
+
"1.2.840.10045.4.3.1": "ecdsa-with-SHA224",
|
|
172
|
+
"1.2.840.10045.4.3.2": "ecdsa-with-SHA256",
|
|
173
|
+
"1.2.840.10045.4.3.3": "ecdsa-with-SHA384",
|
|
174
|
+
"1.2.840.10045.4.3.4": "ecdsa-with-SHA512",
|
|
175
|
+
"1.2.840.10040.4.3": "dsa-with-sha1",
|
|
176
|
+
"2.16.840.1.101.3.4.3.2": "dsa-with-sha256",
|
|
177
|
+
"1.3.101.112": "Ed25519",
|
|
178
|
+
"1.3.101.113": "Ed448",
|
|
179
|
+
...Object.fromEntries(Object.entries(PQ_SIG).map(([o, [n]]) => [o, n])),
|
|
180
|
+
...STATEFUL,
|
|
181
|
+
};
|
|
182
|
+
function keyInfo(b, spki) {
|
|
183
|
+
const [alg, bits] = kids(b, spki);
|
|
184
|
+
if (!alg || alg.tag !== 0x30 || !bits || bits.tag !== 0x03)
|
|
185
|
+
return null;
|
|
186
|
+
const [o, params] = kids(b, alg);
|
|
187
|
+
const keyOid = oidOf(b, o);
|
|
188
|
+
if (!keyOid)
|
|
189
|
+
return null;
|
|
190
|
+
if (/^1\.2\.840\.113549\.1\.1\.(1|7|10)$/.test(keyOid)) {
|
|
191
|
+
const inner = tlv(b, bits.start + 1);
|
|
192
|
+
const [n] = inner && inner.tag === 0x30 ? kids(b, inner) : [];
|
|
193
|
+
return { algorithm: "RSA", keySize: intBits(b, n), keyOid };
|
|
194
|
+
}
|
|
195
|
+
if (keyOid === "1.2.840.10045.2.1" || keyOid === "1.3.132.1.12") {
|
|
196
|
+
const curve = params?.tag === 0x06 ? (CURVES[oidOf(b, params) ?? ""] ?? oidOf(b, params) ?? undefined) : "explicit parameters";
|
|
197
|
+
return { algorithm: keyOid === "1.3.132.1.12" ? "ECDH" : "EC", curve, keyOid };
|
|
198
|
+
}
|
|
199
|
+
const direct = { "1.3.101.110": "X25519", "1.3.101.111": "X448", "1.3.101.112": "Ed25519", "1.3.101.113": "Ed448" };
|
|
200
|
+
if (direct[keyOid])
|
|
201
|
+
return { algorithm: direct[keyOid], keyOid };
|
|
202
|
+
if (keyOid === "1.2.840.10040.4.1" || keyOid === "1.2.840.10046.2.1" || keyOid === "1.2.840.113549.1.3.1") {
|
|
203
|
+
const [p] = params?.tag === 0x30 ? kids(b, params) : [];
|
|
204
|
+
return { algorithm: keyOid === "1.2.840.10040.4.1" ? "DSA" : "DH", keySize: intBits(b, p), keyOid };
|
|
205
|
+
}
|
|
206
|
+
if (PQ_SIG[keyOid]) {
|
|
207
|
+
const [name, level] = PQ_SIG[keyOid];
|
|
208
|
+
return { algorithm: null, keyOid, pqc: { algorithm: name, family: name.startsWith("ML") ? "ML-DSA" : "SLH-DSA", primitive: "signature", functions: ["sign", "verify"], nistQuantumLevel: level, note: "Public key read from a PEM block.", ...latticeLevelCaution(name) } };
|
|
209
|
+
}
|
|
210
|
+
if (PQ_KEM[keyOid]) {
|
|
211
|
+
const [name, level] = PQ_KEM[keyOid];
|
|
212
|
+
return { algorithm: null, keyOid, pqc: { algorithm: name, family: "ML-KEM", primitive: "kem", functions: ["encapsulate", "decapsulate"], nistQuantumLevel: level, note: "Public key read from a PEM block.", ...latticeLevelCaution(name) } };
|
|
213
|
+
}
|
|
214
|
+
if (STATEFUL[keyOid]) {
|
|
215
|
+
const name = STATEFUL[keyOid];
|
|
216
|
+
return { algorithm: null, keyOid, pqc: { algorithm: name, family: name.startsWith("XMSS") ? "XMSS" : "LMS", primitive: "signature", functions: ["sign", "verify"], note: "Stateful hash-based signature key read from a PEM block. Each one-time key may sign only once." } };
|
|
217
|
+
}
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
const PEM = /-----BEGIN ([A-Z0-9. ]{3,40})-----([\s\S]{0,60000}?)-----END \1-----/g;
|
|
221
|
+
const SKIP_LABEL = /PRIVATE KEY|ENCRYPTED|PGP|SSH2|OPENSSH|ATTRIBUTE|PKCS7|CMS|CRL/;
|
|
222
|
+
function blocks(lines) {
|
|
223
|
+
if (!lines.some((l) => l.text.includes("-----BEGIN ") || l.text.includes("x5c")))
|
|
224
|
+
return [];
|
|
225
|
+
const starts = [];
|
|
226
|
+
let off = 0;
|
|
227
|
+
for (const l of lines) {
|
|
228
|
+
starts.push(off);
|
|
229
|
+
off += l.text.length + 1;
|
|
230
|
+
}
|
|
231
|
+
const all = lines.map((l) => l.text).join("\n");
|
|
232
|
+
const indexOf = (at) => {
|
|
233
|
+
let lo = 0;
|
|
234
|
+
let hi = starts.length - 1;
|
|
235
|
+
while (lo < hi) {
|
|
236
|
+
const mid = (lo + hi + 1) >> 1;
|
|
237
|
+
if (starts[mid] <= at)
|
|
238
|
+
lo = mid;
|
|
239
|
+
else
|
|
240
|
+
hi = mid - 1;
|
|
241
|
+
}
|
|
242
|
+
return lo;
|
|
243
|
+
};
|
|
244
|
+
const out = [];
|
|
245
|
+
for (const m of all.matchAll(PEM)) {
|
|
246
|
+
if (SKIP_LABEL.test(m[1]))
|
|
247
|
+
continue;
|
|
248
|
+
// Undo the ways a block is written inside a string: "\n" escapes, and "..." + "..." concatenation.
|
|
249
|
+
const body = m[2]
|
|
250
|
+
.replace(/\\[nrt]/g, "\n")
|
|
251
|
+
.replace(/["'`]\s*\+?\s*\n\s*\+?\s*["'`]/g, "")
|
|
252
|
+
.replace(/^[^\n]*:[^\n]*$/gm, "") // PEM headers (Proc-Type: ...)
|
|
253
|
+
.replace(/[^A-Za-z0-9+/=]/g, "");
|
|
254
|
+
if (body.length < 40)
|
|
255
|
+
continue;
|
|
256
|
+
let der;
|
|
257
|
+
try {
|
|
258
|
+
const raw = atob(body);
|
|
259
|
+
der = Uint8Array.from(raw, (c) => c.charCodeAt(0));
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
out.push({ label: m[1], der, index: indexOf(m.index ?? 0) });
|
|
265
|
+
}
|
|
266
|
+
// A JWKS carries each key's certificate chain in x5c as plain base64 DER, with no PEM armor.
|
|
267
|
+
for (const m of all.matchAll(/["']x5c["']\s*:\s*\[([^\]]{0,60000})\]/g)) {
|
|
268
|
+
for (const c of m[1].matchAll(/["']([A-Za-z0-9+/\s\\n]{200,}={0,2})["']/g)) {
|
|
269
|
+
try {
|
|
270
|
+
const raw = atob(c[1].replace(/\\n|\s/g, ""));
|
|
271
|
+
out.push({ label: "CERTIFICATE", der: Uint8Array.from(raw, (ch) => ch.charCodeAt(0)), index: indexOf((m.index ?? 0) + m[0].indexOf(c[0])) });
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// not base64 after all
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
function parse(block) {
|
|
281
|
+
const b = block.der;
|
|
282
|
+
const top = tlv(b, 0);
|
|
283
|
+
if (!top || top.tag !== 0x30)
|
|
284
|
+
return null;
|
|
285
|
+
const label = block.label;
|
|
286
|
+
if (/CERTIFICATE REQUEST/.test(label)) {
|
|
287
|
+
const [info, sigAlg] = kids(b, top);
|
|
288
|
+
if (!info)
|
|
289
|
+
return null;
|
|
290
|
+
const [, subject, spki] = kids(b, info);
|
|
291
|
+
const sigOid = sigAlg ? oidOf(b, kids(b, sigAlg)[0]) : null;
|
|
292
|
+
const key = spki ? keyInfo(b, spki) : null;
|
|
293
|
+
return { key, material: { type: "certificate-request", subject: nameOf(b, subject), ...(sigOid ? { signatureOid: sigOid, signatureAlgorithm: SIGNATURE_OIDS[sigOid] ?? sigOid } : {}), ...(key ? { keyOid: key.keyOid } : {}) } };
|
|
294
|
+
}
|
|
295
|
+
if (/CERTIFICATE/.test(label)) {
|
|
296
|
+
const [tbs, sigAlg] = kids(b, top);
|
|
297
|
+
if (!tbs || tbs.tag !== 0x30)
|
|
298
|
+
return null;
|
|
299
|
+
let f = kids(b, tbs);
|
|
300
|
+
if (f[0]?.tag === 0xa0)
|
|
301
|
+
f = f.slice(1); // explicit version
|
|
302
|
+
const [, , issuer, validity, subject, spki] = f;
|
|
303
|
+
const [nb, na] = validity ? kids(b, validity) : [];
|
|
304
|
+
const sigOid = sigAlg ? oidOf(b, kids(b, sigAlg)[0]) : null;
|
|
305
|
+
const key = spki ? keyInfo(b, spki) : null;
|
|
306
|
+
return {
|
|
307
|
+
key,
|
|
308
|
+
material: {
|
|
309
|
+
type: "certificate",
|
|
310
|
+
subject: nameOf(b, subject),
|
|
311
|
+
issuer: nameOf(b, issuer),
|
|
312
|
+
notBefore: timeOf(b, nb),
|
|
313
|
+
notAfter: timeOf(b, na),
|
|
314
|
+
...(sigOid ? { signatureOid: sigOid, signatureAlgorithm: SIGNATURE_OIDS[sigOid] ?? sigOid } : {}),
|
|
315
|
+
...(key ? { keyOid: key.keyOid } : {}),
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
if (label === "RSA PUBLIC KEY") {
|
|
320
|
+
const [n] = kids(b, top);
|
|
321
|
+
return { key: { algorithm: "RSA", keySize: intBits(b, n), keyOid: "1.2.840.113549.1.1.1" }, material: { type: "public-key", keyOid: "1.2.840.113549.1.1.1" } };
|
|
322
|
+
}
|
|
323
|
+
if (/PUBLIC KEY/.test(label)) {
|
|
324
|
+
const key = keyInfo(b, top);
|
|
325
|
+
return key ? { key, material: { type: "public-key", keyOid: key.keyOid } } : null;
|
|
326
|
+
}
|
|
327
|
+
if (/DH PARAMETERS|DSA PARAMETERS/.test(label)) {
|
|
328
|
+
const [p] = kids(b, top);
|
|
329
|
+
const alg = /DSA/.test(label) ? "DSA" : "DH";
|
|
330
|
+
return { key: { algorithm: alg, keySize: intBits(b, p), keyOid: alg === "DSA" ? "1.2.840.10040.4.1" : "1.2.840.10046.2.1" }, material: { type: "parameters" } };
|
|
331
|
+
}
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
/** A short name for a certificate: its CN, or its whole subject. */
|
|
335
|
+
function shortName(subject) {
|
|
336
|
+
if (!subject)
|
|
337
|
+
return "unnamed";
|
|
338
|
+
return subject.match(/CN=([^,]+)/)?.[1] ?? subject;
|
|
339
|
+
}
|
|
340
|
+
const CERT_FIX = "Plan to reissue it with an ML-DSA (FIPS 204) key once the certificate authority and the clients that check it support one.";
|
|
341
|
+
/**
|
|
342
|
+
* Every certificate, public key and parameter block in the given lines. Vulnerable keys become findings;
|
|
343
|
+
* post-quantum and hash-based keys go to the inventory.
|
|
344
|
+
*/
|
|
345
|
+
export function findPem(path, lines) {
|
|
346
|
+
const findings = [];
|
|
347
|
+
const inventory = [];
|
|
348
|
+
for (const block of blocks(lines)) {
|
|
349
|
+
const p = parse(block);
|
|
350
|
+
if (!p)
|
|
351
|
+
continue;
|
|
352
|
+
const r = materialFindings(path, lines[block.index].no, block.label, p);
|
|
353
|
+
findings.push(...r.findings);
|
|
354
|
+
inventory.push(...r.inventory);
|
|
355
|
+
}
|
|
356
|
+
return { findings, inventory };
|
|
357
|
+
}
|
|
358
|
+
/** Findings and inventory for one parsed certificate, request, key or parameter block. */
|
|
359
|
+
export function materialFindings(path, line, label, p) {
|
|
360
|
+
const findings = [];
|
|
361
|
+
const inventory = [];
|
|
362
|
+
const { material, key } = p;
|
|
363
|
+
const what = material.type === "certificate" ? `Certificate (${shortName(material.subject)})` : material.type === "certificate-request" ? `Certificate request (${shortName(material.subject)})` : material.type === "parameters" ? `${key?.algorithm} parameters` : "Public key";
|
|
364
|
+
if (key?.pqc) {
|
|
365
|
+
inventory.push({ ...key.pqc, file: path, line, note: `${what}. ${key.pqc.note ?? ""}`.trim() });
|
|
366
|
+
}
|
|
367
|
+
if (key?.algorithm) {
|
|
368
|
+
// A certificate's key signs (TLS 1.3, code signing); a bare EC key could be either.
|
|
369
|
+
let algorithm = key.algorithm;
|
|
370
|
+
if (algorithm === "RSA" && material.type !== "public-key" && material.type !== "parameters")
|
|
371
|
+
algorithm = "RSA signature";
|
|
372
|
+
if (algorithm === "EC" && material.type !== "public-key")
|
|
373
|
+
algorithm = "ECDSA";
|
|
374
|
+
const info = ALGORITHMS[algorithm];
|
|
375
|
+
if (info) {
|
|
376
|
+
const size = key.keySize ? `${key.algorithm}-${key.keySize}` : key.curve ? `${algorithm} ${key.curve}` : algorithm;
|
|
377
|
+
let risk = info.risk;
|
|
378
|
+
let severity = info.severity;
|
|
379
|
+
let fix = material.type === "certificate" ? `${CERT_FIX}${key.algorithm === "RSA" ? " Until then, keep RSA keys at 3072 bits or more." : ""}` : info.fix;
|
|
380
|
+
const uses = material.type === "parameters" ? "use" : "uses";
|
|
381
|
+
let title = `${what} ${uses} ${size}, ${risk === "weak-now" ? "weak today" : "breakable by quantum computers"}`;
|
|
382
|
+
if (key.keySize !== undefined && /^(RSA|DH|DSA)/.test(key.algorithm) && key.keySize < 2048) {
|
|
383
|
+
risk = "weak-now";
|
|
384
|
+
severity = "high";
|
|
385
|
+
title = `${what} ${uses} ${size}, weak today and breakable by quantum computers`;
|
|
386
|
+
fix = key.algorithm === "RSA" ? "Replace it with a 3072-bit key now, then plan the move to post-quantum methods." : `Use ${key.algorithm === "DH" ? "a 3072-bit group or ECDHE now, then ML-KEM" : "ML-DSA"}.`;
|
|
387
|
+
}
|
|
388
|
+
findings.push({
|
|
389
|
+
kind: "crypto",
|
|
390
|
+
ruleId: `pem-${material.type}`,
|
|
391
|
+
title,
|
|
392
|
+
algorithm,
|
|
393
|
+
primitive: info.primitive,
|
|
394
|
+
risk,
|
|
395
|
+
severity,
|
|
396
|
+
file: path,
|
|
397
|
+
line,
|
|
398
|
+
snippet: `-----BEGIN ${label}-----${material.subject ? ` ${material.subject}` : ""}${material.notAfter ? `, valid until ${material.notAfter.slice(0, 10)}` : ""}`.slice(0, 160),
|
|
399
|
+
fix,
|
|
400
|
+
...(key.keySize ? { keySize: key.keySize } : {}),
|
|
401
|
+
...(key.curve ? { curve: key.curve } : {}),
|
|
402
|
+
material,
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
// A weak hash in a certificate's own signature. A self-signed root's signature is never checked, so it is left out.
|
|
407
|
+
const weakSig = material.signatureAlgorithm?.match(/^(md2|md5|sha1)WithRSA|^ecdsa-with-SHA1$|^dsa-with-sha1$/i);
|
|
408
|
+
if (weakSig && material.type !== "parameters" && !(material.type === "certificate" && material.subject === material.issuer)) {
|
|
409
|
+
const alg = /md[25]/i.test(material.signatureAlgorithm) ? "MD5" : "SHA-1";
|
|
410
|
+
findings.push({
|
|
411
|
+
kind: "crypto",
|
|
412
|
+
ruleId: `pem-${material.type}-weak-signature`,
|
|
413
|
+
title: `${what} is signed with ${alg}, which is weak today`,
|
|
414
|
+
algorithm: alg,
|
|
415
|
+
primitive: "hash",
|
|
416
|
+
risk: "weak-now",
|
|
417
|
+
severity: "high",
|
|
418
|
+
file: path,
|
|
419
|
+
line,
|
|
420
|
+
snippet: `-----BEGIN ${label}----- signed with ${material.signatureAlgorithm}`,
|
|
421
|
+
fix: "Reissue it signed with SHA-256 or stronger.",
|
|
422
|
+
material,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
return { findings, inventory };
|
|
426
|
+
}
|
|
427
|
+
/** A certificate in DER form, as a TLS handshake gives it. */
|
|
428
|
+
export function parseCertificate(der) {
|
|
429
|
+
return parse({ label: "CERTIFICATE", der, index: 0 });
|
|
430
|
+
}
|
|
431
|
+
// ---------- OpenSSH public keys ----------
|
|
432
|
+
/** Key size or curve of an OpenSSH public key ("ssh-rsa AAAA..."), from its base64 body. */
|
|
433
|
+
export function sshKeyDetail(type, b64) {
|
|
434
|
+
if (type.startsWith("ecdsa-sha2-nistp"))
|
|
435
|
+
return { curve: `P-${type.slice(16)}` };
|
|
436
|
+
if (type !== "ssh-rsa" && type !== "ssh-dss")
|
|
437
|
+
return {};
|
|
438
|
+
let b;
|
|
439
|
+
try {
|
|
440
|
+
b = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
|
|
441
|
+
}
|
|
442
|
+
catch {
|
|
443
|
+
return {};
|
|
444
|
+
}
|
|
445
|
+
// string type, then mpint e, mpint n (RSA) or mpint p, q, g, y (DSA): each a 4-byte length, then bytes.
|
|
446
|
+
let p = 0;
|
|
447
|
+
const next = () => {
|
|
448
|
+
if (p + 4 > b.length)
|
|
449
|
+
return null;
|
|
450
|
+
const n = ((b[p] << 24) | (b[p + 1] << 16) | (b[p + 2] << 8) | b[p + 3]) >>> 0;
|
|
451
|
+
const s = p + 4;
|
|
452
|
+
p = s + n;
|
|
453
|
+
return p <= b.length ? { s, e: p } : null;
|
|
454
|
+
};
|
|
455
|
+
const t = next();
|
|
456
|
+
if (!t)
|
|
457
|
+
return {};
|
|
458
|
+
const first = type === "ssh-rsa" ? (next(), next()) : next();
|
|
459
|
+
if (!first)
|
|
460
|
+
return {};
|
|
461
|
+
let i = first.s;
|
|
462
|
+
while (i < first.e && b[i] === 0)
|
|
463
|
+
i++;
|
|
464
|
+
if (i >= first.e)
|
|
465
|
+
return {};
|
|
466
|
+
return { keySize: (first.e - i - 1) * 8 + (32 - Math.clz32(b[i])) };
|
|
467
|
+
}
|
|
468
|
+
/** The key type and size of every PEM key or certificate in a text, as "RSA 4096" or "Ed25519". */
|
|
469
|
+
export function pemKeyTypes(text) {
|
|
470
|
+
const lines = text.split(/\r?\n/).map((t, i) => ({ no: i + 1, text: t }));
|
|
471
|
+
const out = [];
|
|
472
|
+
for (const block of blocks(lines)) {
|
|
473
|
+
const k = parse(block)?.key;
|
|
474
|
+
if (k)
|
|
475
|
+
out.push(keyLabel(k.algorithm ?? "unknown", k.keySize ?? undefined, k.curve ?? undefined));
|
|
476
|
+
}
|
|
477
|
+
return out;
|
|
478
|
+
}
|
|
479
|
+
function keyLabel(algorithm, size, curve) {
|
|
480
|
+
return curve ? `${algorithm} ${curve}` : size ? `${algorithm} ${size}` : algorithm;
|
|
481
|
+
}
|
|
482
|
+
const PGP_CURVES = {
|
|
483
|
+
"2b06010401da470f01": "Ed25519",
|
|
484
|
+
"2b060104019755010501": "Curve25519",
|
|
485
|
+
"2a8648ce3d030107": "P-256",
|
|
486
|
+
"2b81040022": "P-384",
|
|
487
|
+
"2b81040023": "P-521",
|
|
488
|
+
};
|
|
489
|
+
const PGP_ALGORITHMS = { 1: "RSA", 2: "RSA", 3: "RSA", 16: "ElGamal", 17: "DSA", 18: "ECDH", 19: "ECDSA", 22: "EdDSA", 25: "X25519", 26: "X448", 27: "Ed25519", 28: "Ed448", 30: "ML-DSA-65+Ed25519", 31: "ML-DSA-87+Ed448", 32: "SLH-DSA-SHAKE-128s", 33: "SLH-DSA-SHAKE-128f", 34: "SLH-DSA-SHAKE-256s", 35: "ML-KEM-768+X25519", 36: "ML-KEM-1024+X448" };
|
|
490
|
+
/**
|
|
491
|
+
* The primary keys in an OpenPGP keyring, binary or armored, as "RSA 4096" or "EdDSA Ed25519". Only
|
|
492
|
+
* public-key packets are read; signatures and user IDs are stepped over.
|
|
493
|
+
*/
|
|
494
|
+
export function pgpKeyTypes(data) {
|
|
495
|
+
let b = data;
|
|
496
|
+
const t = new TextDecoder("latin1").decode(data.subarray(0, Math.min(data.length, 64)));
|
|
497
|
+
if (t.includes("-----BEGIN PGP")) {
|
|
498
|
+
const text = new TextDecoder("latin1").decode(data);
|
|
499
|
+
b = new Uint8Array(0);
|
|
500
|
+
const parts = [];
|
|
501
|
+
for (const m of text.matchAll(/-----BEGIN PGP PUBLIC KEY BLOCK-----\r?\n([\s\S]*?)-----END PGP PUBLIC KEY BLOCK-----/g)) {
|
|
502
|
+
const body = m[1].split(/\r?\n\r?\n/).slice(-1)[0] ?? ""; // past the armor headers
|
|
503
|
+
parts.push(Buffer.from(body.split(/\r?\n/).filter((l) => !l.startsWith("=")).join(""), "base64"));
|
|
504
|
+
}
|
|
505
|
+
b = Buffer.concat(parts);
|
|
506
|
+
}
|
|
507
|
+
const out = [];
|
|
508
|
+
let i = 0;
|
|
509
|
+
while (i < b.length) {
|
|
510
|
+
const h = b[i];
|
|
511
|
+
if ((h & 0x80) === 0)
|
|
512
|
+
break;
|
|
513
|
+
let tag, len, at;
|
|
514
|
+
if (h & 0x40) {
|
|
515
|
+
tag = h & 0x3f;
|
|
516
|
+
const l0 = b[i + 1];
|
|
517
|
+
if (l0 < 192) {
|
|
518
|
+
len = l0;
|
|
519
|
+
at = i + 2;
|
|
520
|
+
}
|
|
521
|
+
else if (l0 < 224) {
|
|
522
|
+
len = ((l0 - 192) << 8) + b[i + 2] + 192;
|
|
523
|
+
at = i + 3;
|
|
524
|
+
}
|
|
525
|
+
else if (l0 === 255) {
|
|
526
|
+
len = (b[i + 2] << 24 | b[i + 3] << 16 | b[i + 4] << 8 | b[i + 5]) >>> 0;
|
|
527
|
+
at = i + 6;
|
|
528
|
+
}
|
|
529
|
+
else
|
|
530
|
+
break; // partial lengths don't occur in keyrings
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
tag = (h >> 2) & 0x0f;
|
|
534
|
+
const lt = h & 3;
|
|
535
|
+
if (lt === 0) {
|
|
536
|
+
len = b[i + 1];
|
|
537
|
+
at = i + 2;
|
|
538
|
+
}
|
|
539
|
+
else if (lt === 1) {
|
|
540
|
+
len = b[i + 1] << 8 | b[i + 2];
|
|
541
|
+
at = i + 3;
|
|
542
|
+
}
|
|
543
|
+
else if (lt === 2) {
|
|
544
|
+
len = (b[i + 1] << 24 | b[i + 2] << 16 | b[i + 3] << 8 | b[i + 4]) >>> 0;
|
|
545
|
+
at = i + 5;
|
|
546
|
+
}
|
|
547
|
+
else
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
if (!(at + len <= b.length) || len < 0)
|
|
551
|
+
break;
|
|
552
|
+
if (tag === 6) {
|
|
553
|
+
const v = b[at];
|
|
554
|
+
const algo = b[at + (v === 4 ? 5 : v === 6 || v === 5 ? 5 : 7)];
|
|
555
|
+
const m = at + (v === 4 ? 6 : v === 6 || v === 5 ? 10 : 8); // v5 and v6 add a 4-byte length
|
|
556
|
+
const name = PGP_ALGORITHMS[algo] ?? `algorithm ${algo}`;
|
|
557
|
+
if (algo >= 1 && algo <= 3)
|
|
558
|
+
out.push(keyLabel(name, b[m] << 8 | b[m + 1]));
|
|
559
|
+
else if (algo === 18 || algo === 19 || algo === 22) {
|
|
560
|
+
const oid = Buffer.from(b.subarray(m + 1, m + 1 + b[m])).toString("hex");
|
|
561
|
+
out.push(keyLabel(name, undefined, PGP_CURVES[oid] ?? "unknown curve"));
|
|
562
|
+
}
|
|
563
|
+
else if (algo === 16 || algo === 17)
|
|
564
|
+
out.push(keyLabel(name, b[m] << 8 | b[m + 1]));
|
|
565
|
+
else
|
|
566
|
+
out.push(name);
|
|
567
|
+
}
|
|
568
|
+
i = at + len;
|
|
569
|
+
}
|
|
570
|
+
return out;
|
|
571
|
+
}
|