Add crypto-inventory: a cryptographic inventory of source code as a CycloneDX 1.6 CBOM, with linear cost on hostile input; PKI DER messages in English
This commit is contained in:
1 parent
7d814d1bb8
commit
0f081f0847
49 files changed
+4342
-33
No files matched your search
+22
-22
@@ -34,7 +34,7 @@ export function int(v) {
|
||||
if (Buffer.isBuffer(v)) b = v;
|
||||
else {
|
||||
let x = BigInt(v);
|
||||
if (x < 0n) throw new Error('DER: numai intregi nenegativi');
|
||||
if (x < 0n) throw new Error('DER: only non-negative integers');
|
||||
const a = [];
|
||||
do { a.unshift(Number(x & 0xffn)); x >>= 8n; } while (x > 0n);
|
||||
b = Buffer.from(a);
|
||||
@@ -54,9 +54,9 @@ function base128(x) {
|
||||
}
|
||||
|
||||
export function oid(dotted) {
|
||||
const p = String(dotted).split('.').map((x) => { if (!/^\d+$/.test(x)) throw new Error('DER: OID cu componenta nevalida: ' + dotted); return BigInt(x); });
|
||||
if (p.length < 2) throw new Error('DER: OID prea scurt');
|
||||
if (p[0] > 2n || (p[0] < 2n && p[1] > 39n)) throw new Error('DER: OID cu arc nevalid: ' + dotted);
|
||||
const p = String(dotted).split('.').map((x) => { if (!/^\d+$/.test(x)) throw new Error('DER: OID with an invalid component: ' + dotted); return BigInt(x); });
|
||||
if (p.length < 2) throw new Error('DER: OID too short');
|
||||
if (p[0] > 2n || (p[0] < 2n && p[1] > 39n)) throw new Error('DER: OID with an invalid arc: ' + dotted);
|
||||
// [A10] primul subidentificator poarta impreuna arcul si al doilea numar (40*arc + al doilea) si, ca oricare altul, se scrie in baza 128
|
||||
// pe mai multi octeti cand trece de 127: 2.999 e 88 37, nu un singur octet trunchiat. Pe arcul 2 al doilea numar nu are limita.
|
||||
const out = base128(p[0] * 40n + p[1]);
|
||||
@@ -77,23 +77,23 @@ export function time(d) {
|
||||
|
||||
/** Un element: { tag, start, hdr, len, end, raw (tot TLV-ul), content } peste acelasi Buffer. */
|
||||
export function read(buf, off = 0) {
|
||||
if (off + 2 > buf.length) throw new Error('DER: trunchiat la antet');
|
||||
if (off + 2 > buf.length) throw new Error('DER: truncated in the header');
|
||||
const tag = buf[off];
|
||||
if ((tag & 0x1f) === 0x1f) throw new Error('DER: eticheta cu forma lunga nesuportata');
|
||||
if ((tag & 0x1f) === 0x1f) throw new Error('DER: long-form tag not supported');
|
||||
let l = buf[off + 1], hdr = 2;
|
||||
if (l === 0x80) throw new Error('DER: lungime nedefinita (BER), refuzata');
|
||||
if (l === 0x80) throw new Error('DER: indefinite length (BER) refused');
|
||||
if (l & 0x80) {
|
||||
const n = l & 0x7f;
|
||||
if (n === 0 || n > 4) throw new Error('DER: lungime pe ' + n + ' octeti');
|
||||
if (off + 2 + n > buf.length) throw new Error('DER: trunchiat la lungime');
|
||||
if (buf[off + 2] === 0) throw new Error('DER: lungime cu zero initial (necanonica)');
|
||||
if (n === 0 || n > 4) throw new Error('DER: length in ' + n + ' bytes');
|
||||
if (off + 2 + n > buf.length) throw new Error('DER: truncated in the length');
|
||||
if (buf[off + 2] === 0) throw new Error('DER: length with a leading zero (non-canonical)');
|
||||
l = 0;
|
||||
for (let i = 0; i < n; i++) l = l * 256 + buf[off + 2 + i];
|
||||
if (l < 0x80) throw new Error('DER: lungime scurta scrisa in forma lunga (necanonica)');
|
||||
if (l < 0x80) throw new Error('DER: short length written in long form (non-canonical)');
|
||||
hdr = 2 + n;
|
||||
}
|
||||
const end = off + hdr + l;
|
||||
if (end > buf.length) throw new Error('DER: continut trunchiat');
|
||||
if (end > buf.length) throw new Error('DER: truncated content');
|
||||
return { tag, start: off, hdr, len: l, end, raw: buf.subarray(off, end), content: buf.subarray(off + hdr, end) };
|
||||
}
|
||||
|
||||
@@ -108,26 +108,26 @@ export function children(el) {
|
||||
/** Decodeaza un document DER intreg si refuza octetii in plus. */
|
||||
export function parse(buf) {
|
||||
const el = read(buf, 0);
|
||||
if (el.end !== buf.length) throw new Error('DER: ' + (buf.length - el.end) + ' octeti dupa structura');
|
||||
if (el.end !== buf.length) throw new Error('DER: ' + (buf.length - el.end) + ' bytes after the structure');
|
||||
return el;
|
||||
}
|
||||
|
||||
export function expect(el, tag, what) {
|
||||
if (el.tag !== tag) throw new Error(`DER: ${what}: eticheta 0x${el.tag.toString(16)}, asteptat 0x${tag.toString(16)}`);
|
||||
if (el.tag !== tag) throw new Error(`DER: ${what}: tag 0x${el.tag.toString(16)}, expected 0x${tag.toString(16)}`);
|
||||
return el;
|
||||
}
|
||||
|
||||
export function oidToString(el) {
|
||||
expect(el, TAG.OID, 'OID');
|
||||
const b = el.content;
|
||||
if (b.length === 0) throw new Error('DER: OID gol');
|
||||
if (b[b.length - 1] & 0x80) throw new Error('DER: OID trunchiat');
|
||||
if (b.length === 0) throw new Error('DER: empty OID');
|
||||
if (b[b.length - 1] & 0x80) throw new Error('DER: truncated OID');
|
||||
// [A10] intai se citesc TOTI subidentificatorii in baza 128 (si primul poate avea mai multi octeti), abia apoi primul se desparte in
|
||||
// arc si al doilea numar: sub 80 arcul e catul impartirii la 40, de la 80 in sus arcul e 2 (X.690 8.19.4). Un octet 0x80 la inceputul
|
||||
// unui subidentificator e o codificare necanonica si se refuza.
|
||||
const subs = []; let v = 0n, inceput = true;
|
||||
for (let i = 0; i < b.length; i++) {
|
||||
if (inceput && b[i] === 0x80) throw new Error('DER: OID necanonic');
|
||||
if (inceput && b[i] === 0x80) throw new Error('DER: non-canonical OID');
|
||||
v = (v << 7n) | BigInt(b[i] & 0x7f); inceput = false;
|
||||
if ((b[i] & 0x80) === 0) { subs.push(v); v = 0n; inceput = true; }
|
||||
}
|
||||
@@ -139,9 +139,9 @@ export function oidToString(el) {
|
||||
export function intToBigInt(el) {
|
||||
expect(el, TAG.INTEGER, 'INTEGER');
|
||||
const b = el.content;
|
||||
if (b.length === 0) throw new Error('DER: INTEGER gol');
|
||||
if (b.length > 1 && ((b[0] === 0 && (b[1] & 0x80) === 0) || (b[0] === 0xff && (b[1] & 0x80)))) throw new Error('DER: INTEGER necanonic');
|
||||
if (b[0] & 0x80) throw new Error('DER: INTEGER negativ');
|
||||
if (b.length === 0) throw new Error('DER: empty INTEGER');
|
||||
if (b.length > 1 && ((b[0] === 0 && (b[1] & 0x80) === 0) || (b[0] === 0xff && (b[1] & 0x80)))) throw new Error('DER: non-canonical INTEGER');
|
||||
if (b[0] & 0x80) throw new Error('DER: negative INTEGER');
|
||||
return BigInt('0x' + (b.toString('hex') || '0'));
|
||||
}
|
||||
|
||||
@@ -155,11 +155,11 @@ export function timeToDate(el) {
|
||||
if (el.tag === TAG.GEN_TIME && (m = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(s))) {
|
||||
return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]));
|
||||
}
|
||||
throw new Error('DER: timp in forma nepermisa de RFC 5280: ' + s);
|
||||
throw new Error('DER: time in a form RFC 5280 does not allow: ' + s);
|
||||
}
|
||||
|
||||
export function bitString(el) {
|
||||
expect(el, TAG.BIT_STRING, 'BIT STRING');
|
||||
if (el.content.length === 0) throw new Error('DER: BIT STRING gol');
|
||||
if (el.content.length === 0) throw new Error('DER: empty BIT STRING');
|
||||
return { unused: el.content[0], bytes: el.content.subarray(1) };
|
||||
}
|
||||
+1
-1
@@ -243,7 +243,7 @@ function parseExtensions(seqEl, what) {
|
||||
|
||||
export function parseCert(der) {
|
||||
const top = D.parse(der);
|
||||
D.expect(top, D.TAG.SEQUENCE, 'certificat');
|
||||
D.expect(top, D.TAG.SEQUENCE, 'certificate');
|
||||
const [tbsEl, algEl, sigEl, ...rest] = D.children(top);
|
||||
if (rest.length || !sigEl) throw new PkiError('DER', 'a certificate has exactly three parts');
|
||||
const t = D.children(D.expect(tbsEl, D.TAG.SEQUENCE, 'tbsCertificate'));
|
||||
|
||||
@@ -32,7 +32,7 @@ const PLANTARI = [
|
||||
['extensia critica necunoscuta ignorata', 'pki.mjs', ' for (const [id, x] of exts) if (x.critical && !EXT_STIUTE.has(id))', " for (const [id, x] of exts) if (x.critical && !EXT_STIUTE.has(id) && Boolean(Number('0')))", 'extensie critica necunoscuta'],
|
||||
['scopul (EKU) pe frunza ignorat', 'pki.mjs', ' if (L.eku && !L.eku.has(OID[purpose]))', " if (L.eku && !L.eku.has(OID[purpose]) && Boolean(Number('0')))", 'scopul'],
|
||||
['cheia privata scrisa in clar (PKCS#8)', 'pki.mjs', ' return pem(KEY_LABEL, D.seq(header, D.octets(tag), D.octets(ct)));', " return key.export({ type: 'pkcs8', format: 'pem' });", 'linia de comanda'],
|
||||
['DER necanonic acceptat', 'der.mjs', " if (l < 0x80) throw new Error('DER: lungime scurta scrisa in forma lunga (necanonica)');", " if (l < 0x80 && Boolean(Number('0'))) throw new Error('DER: lungime scurta scrisa in forma lunga (necanonica)');", 'forma'],
|
||||
['DER necanonic acceptat', 'der.mjs', " if (l < 0x80) throw new Error('DER: short length written in long form (non-canonical)');", " if (l < 0x80 && Boolean(Number('0'))) throw new Error('DER: short length written in long form (non-canonical)');", 'forma'],
|
||||
// revizuirea din 2026-09-25
|
||||
['[A1] scrypt coborat la N=2^10', 'pki.mjs', 'N: 2 ** 17,', 'N: 2 ** 10,', 'sigilarea cheii'],
|
||||
['[A1] politica de parola redusa la 12 caractere (accepta aaaaaaaaaaaa)', 'pki.mjs', 'export function checkPassphrase(p) {', "export function checkPassphrase(p) { if (typeof p === 'string' && p.length >= 12 && Boolean(Number('1'))) return p;", 'politica de parola'],
|
||||
|
||||
@@ -265,8 +265,8 @@ await test('forma: algoritm exterior diferit de cel interior (ALG_MISMATCH), par
|
||||
const n = Buffer.concat([Buffer.from([0x30, 0x82]), Buffer.alloc(2), leaf.subarray(4, j), withNull, leaf.subarray(j + src.length)]);
|
||||
n.writeUInt16BE(lung, 2);
|
||||
e = null; try { P.parseCert(n); } catch (x) { e = x; } eq(e && e.code, 'ALG_PARAMS', 'NULL pe ML-DSA');
|
||||
e = null; try { P.parseCert(Buffer.concat([leaf, Buffer.from([0])])); } catch (x) { e = x; } has(e && e.message, 'octeti dupa structura', 'coada');
|
||||
const nc = Buffer.from([0x30, 0x81, 0x03, 0x02, 0x01, 0x05]); e = null; try { P.parseCert(nc); } catch (x) { e = x; } has(e && e.message, 'necanonica', 'lungime lunga pentru una scurta');
|
||||
e = null; try { P.parseCert(Buffer.concat([leaf, Buffer.from([0])])); } catch (x) { e = x; } has(e && e.message, 'bytes after the structure', 'coada');
|
||||
const nc = Buffer.from([0x30, 0x81, 0x03, 0x02, 0x01, 0x05]); e = null; try { P.parseCert(nc); } catch (x) { e = x; } has(e && e.message, 'non-canonical', 'lungime lunga pentru una scurta');
|
||||
});
|
||||
// [A5] SPKI
|
||||
const cuSpkiRau = (der) => { const c = P.parseCert(der); const [algEl] = Dm.children(Dm.parse(c.spki)); const s = Dm.seq(algEl.raw, Dm.bits(Buffer.alloc(10, 7))); return { c, tbs: Dm.seq(...Dm.children(Dm.parse(c.tbs)).map((k) => (k.raw.equals(c.spki) ? s : k.raw))) }; };
|
||||
@@ -306,8 +306,8 @@ await test('OID [A10]: 2.999 se codifica 06 02 88 37 si se decodifica inapoi; 2.
|
||||
const r = ossl(['asn1parse', '-genstr', 'OID:' + o, '-noout', '-out', f('oid.der')]); eq(r.code, 0, 'openssl genstr ' + o + ' ' + r.out);
|
||||
eq(fs.readFileSync(f('oid.der')).toString('hex'), noi.toString('hex'), 'octetii OpenSSL pentru ' + o);
|
||||
}
|
||||
let e = null; try { Dm.oidToString(Dm.parse(Buffer.from('06028001', 'hex'))); } catch (x) { e = x; } has(e && e.message, 'necanonic', '0x80 initial');
|
||||
e = null; try { Dm.oid('1.40'); } catch (x) { e = x; } has(e && e.message, 'arc nevalid', '1.40');
|
||||
let e = null; try { Dm.oidToString(Dm.parse(Buffer.from('06028001', 'hex'))); } catch (x) { e = x; } has(e && e.message, 'non-canonical', '0x80 initial');
|
||||
e = null; try { Dm.oid('1.40'); } catch (x) { e = x; } has(e && e.message, 'invalid arc', '1.40');
|
||||
const o = ossl(['req', '-x509', '-newkey', 'mldsa44', '-keyout', f('c9.key'), '-out', f('c9.pem'), '-days', '2', '-nodes', '-subj', '/CN=crit999',
|
||||
'-addext', 'basicConstraints=critical,CA:TRUE', '-addext', 'keyUsage=critical,keyCertSign,cRLSign,digitalSignature', '-addext', '2.999.1=critical,ASN1:UTF8String:x']);
|
||||
eq(o.code, 0, 'req ' + o.out);
|
||||
|
||||
Reference in new issue
Block a user