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) };
|
||||
}
|
||||
Reference in new issue
Block a user