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
@@ -0,0 +1,418 @@
|
||||
// Catalogul de clasificare. Fiecare gasire primeste o clasa, un motiv scris si, cand e
|
||||
// vulnerabila, o recomandare de migrare. Textele pentru utilizator sunt in engleza.
|
||||
//
|
||||
// Reguli: o clasa nu se ghiceste. "unknown" inseamna ca valoarea nu se poate afla static.
|
||||
|
||||
export const CLS = Object.freeze({
|
||||
V: 'quantum-vulnerable',
|
||||
W: 'weak-now',
|
||||
S: 'quantum-safe',
|
||||
U: 'unknown',
|
||||
});
|
||||
|
||||
export const REC = Object.freeze({
|
||||
SIG: 'Migrate signatures to ML-DSA-65 (FIPS 204), or to a hybrid (composite) classical+ML-DSA signature during the transition so that both would have to be broken. For long-lived roots of trust consider SLH-DSA (FIPS 205). Keys and signatures grow (ML-DSA-65: 1,952-byte public key, 3,309-byte signature): check protocol, storage and column limits. Composite signatures for X.509 were still an IETF draft when this rule was written.',
|
||||
KEX: 'For TLS, offer the hybrid group X25519MLKEM768 in TLS 1.3 (built into Go 1.24+ and OpenSSL 3.5+). For application-level key establishment use ML-KEM-768 (FIPS 203), ideally combined with X25519 during the transition. Traffic protected by a classical key exchange can be recorded now and decrypted later, so start with data that must stay confidential for years.',
|
||||
PKE: 'Replace RSA encryption and key transport with ML-KEM-768 (FIPS 203) used as a KEM in front of an AEAD such as AES-256-GCM, ideally hybrid with X25519 during the transition. Ciphertexts recorded today become readable once a cryptographically relevant quantum computer exists; re-encrypt data that must stay confidential for years.',
|
||||
SECP256K1: 'secp256k1 ECDSA is fixed by the account and transaction format of Ethereum-style and Bitcoin-style chains, so this is not a library swap. Plan the migration at the account level: move control of funds and roles to an account whose owner is a post-quantum key (for example an ML-DSA or Falcon key checked by a smart-contract account or by the chain itself), and stop reusing addresses whose public key is already exposed on chain.',
|
||||
JWT: 'There is no final standard for post-quantum JWS/JWT yet: ML-DSA for JOSE and COSE was an IETF draft when this rule was written, and mainstream JWT libraries do not ship it by default. What you can do now: keep token lifetimes short, use HS256/HS512 where issuer and verifier can share a secret, inventory where verification keys live so they can be rotated, and plan a hybrid (classical + ML-DSA) signature once the specification is final and your library supports it.',
|
||||
JWT_NONE: 'An unsigned JWT ("none") must never be accepted: remove it from the accepted algorithms.',
|
||||
HASH_WEAK: 'Replace with SHA-256, SHA-384 or SHA3-256. If this is a non-security checksum (cache key, deduplication), document that; for passwords use a password hash (Argon2id, scrypt, bcrypt, or PBKDF2-HMAC-SHA-256 with a high iteration count).',
|
||||
CIPHER_WEAK: 'Replace with AES-256-GCM or ChaCha20-Poly1305, with a unique nonce per key.',
|
||||
ECB: 'ECB leaks plaintext patterns. Use an authenticated mode: AES-256-GCM (unique nonce per key) or ChaCha20-Poly1305.',
|
||||
TLS_OLD: 'Disable SSL, TLS 1.0 and TLS 1.1; require at least TLS 1.2 and prefer TLS 1.3 with the hybrid group X25519MLKEM768.',
|
||||
TLS12: 'TLS 1.2 has no standardized post-quantum key exchange. Prefer TLS 1.3 and offer X25519MLKEM768; keep TLS 1.2 only for clients that require it, and measure how many still negotiate it.',
|
||||
TLS13: 'Check the negotiated key-exchange groups: offer X25519MLKEM768 first. Go 1.24+ does this by default when CurvePreferences is unset, and OpenSSL 3.5+ includes it in its default groups; older runtimes negotiate a classical group.',
|
||||
AES128: 'Grover\'s algorithm gives at most a quadratic speed-up, which NIST treats as security category 1 for AES-128. Frameworks such as CNSA 2.0 require AES-256; prefer AES-256 for data that must stay confidential for decades.',
|
||||
AES_SIZE: 'Key size is set at run time by the key length. AES-128 is NIST category 1 against a quantum attacker, AES-256 category 5; prefer AES-256 for long-lived data.',
|
||||
PREPQ: 'Pre-standard variant: migrate to the final NIST standard (ML-KEM FIPS 203, ML-DSA FIPS 204, SLH-DSA FIPS 205, or FN-DSA for Falcon once published). Keys and outputs are not interoperable with the final versions.',
|
||||
BROKEN_PQ: 'Broken by classical attacks in 2022. Remove it; use ML-KEM (FIPS 203) or ML-DSA (FIPS 204).',
|
||||
UNKNOWN: 'Find where the value comes from (configuration, environment, caller) and review it there; this tool does not guess.',
|
||||
LIB_MULTI: 'The library offers both classical and post-quantum-safe primitives; no specific call was recognized in this file. Review how it is used.',
|
||||
});
|
||||
|
||||
// numele canonice ale curbelor
|
||||
const ALIAS_CURBE = [
|
||||
[/^(p-?256|prime256v1|secp256r1|nist256p|curvep256|p256)$/i, 'secp256r1'],
|
||||
[/^(p-?384|secp384r1|nist384p|curvep384|p384)$/i, 'secp384r1'],
|
||||
[/^(p-?521|secp521r1|nist521p|curvep521|p521)$/i, 'secp521r1'],
|
||||
[/^(p-?224|secp224r1|nist224p|p224)$/i, 'secp224r1'],
|
||||
[/^(p-?192|prime192v1|secp192r1|nist192p|p192)$/i, 'secp192r1'],
|
||||
[/^(secp256k1|k256|k-256)$/i, 'secp256k1'],
|
||||
[/^brainpoolp256r1$/i, 'brainpoolP256r1'],
|
||||
[/^brainpoolp384r1$/i, 'brainpoolP384r1'],
|
||||
[/^brainpoolp512r1$/i, 'brainpoolP512r1'],
|
||||
[/^(curve25519|x25519)$/i, 'Curve25519'],
|
||||
[/^(ed25519|edwards25519)$/i, 'Edwards25519'],
|
||||
[/^(curve448|x448)$/i, 'Curve448'],
|
||||
[/^(ed448|edwards448)$/i, 'Edwards448'],
|
||||
];
|
||||
const NIVEL_CURBA = {
|
||||
secp256r1: 128, secp384r1: 192, secp521r1: 256, secp224r1: 112, secp192r1: 96, secp256k1: 128,
|
||||
brainpoolP256r1: 128, brainpoolP384r1: 192, brainpoolP512r1: 256,
|
||||
};
|
||||
|
||||
export function curbaCanonica(s) {
|
||||
if (!s) return null;
|
||||
const t = String(s).trim();
|
||||
for (const [re, nume] of ALIAS_CURBE) if (re.test(t)) return nume;
|
||||
return t;
|
||||
}
|
||||
|
||||
// hash-uri: nume canonic, clasa, nivel NIST (categoria de coliziune), OID
|
||||
const HASH = {
|
||||
MD2: { n: 'MD2', c: CLS.W }, MD4: { n: 'MD4', c: CLS.W },
|
||||
MD5: { n: 'MD5', c: CLS.W, oid: '1.2.840.113549.2.5' },
|
||||
SHA1: { n: 'SHA-1', c: CLS.W, oid: '1.3.14.3.2.26' },
|
||||
RIPEMD160: { n: 'RIPEMD-160', c: CLS.W },
|
||||
SHA224: { n: 'SHA-224', c: CLS.S },
|
||||
SHA256: { n: 'SHA-256', c: CLS.S, q: 2, cl: 128, oid: '2.16.840.1.101.3.4.2.1' },
|
||||
SHA384: { n: 'SHA-384', c: CLS.S, q: 4, cl: 192, oid: '2.16.840.1.101.3.4.2.2' },
|
||||
SHA512: { n: 'SHA-512', c: CLS.S, q: 4, cl: 256, oid: '2.16.840.1.101.3.4.2.3' },
|
||||
'SHA512/256': { n: 'SHA-512/256', c: CLS.S, q: 2 },
|
||||
'SHA512/224': { n: 'SHA-512/224', c: CLS.S },
|
||||
'SHA3-224': { n: 'SHA3-224', c: CLS.S },
|
||||
'SHA3-256': { n: 'SHA3-256', c: CLS.S, q: 2 },
|
||||
'SHA3-384': { n: 'SHA3-384', c: CLS.S, q: 4 },
|
||||
'SHA3-512': { n: 'SHA3-512', c: CLS.S, q: 4 },
|
||||
SHAKE128: { n: 'SHAKE128', c: CLS.S, xof: true },
|
||||
SHAKE256: { n: 'SHAKE256', c: CLS.S, xof: true },
|
||||
BLAKE2B: { n: 'BLAKE2b', c: CLS.S }, BLAKE2S: { n: 'BLAKE2s', c: CLS.S },
|
||||
BLAKE2B512: { n: 'BLAKE2b-512', c: CLS.S }, BLAKE2S256: { n: 'BLAKE2s-256', c: CLS.S },
|
||||
SM3: { n: 'SM3', c: CLS.S },
|
||||
KECCAK256: { n: 'Keccak-256', c: CLS.S },
|
||||
};
|
||||
|
||||
export function hashCanonic(s) {
|
||||
if (!s) return null;
|
||||
let t = String(s).trim().toUpperCase().replace(/^RSA-/, '');
|
||||
t = t.replace(/_/g, '-');
|
||||
if (/^SHA-?1$|^SHA$/.test(t)) return 'SHA1';
|
||||
if (/^SHA-?(224|256|384|512)$/.test(t)) return 'SHA' + t.replace(/\D/g, '');
|
||||
if (/^SHA-?512[/-](224|256)$/.test(t)) return 'SHA512/' + t.slice(-3);
|
||||
if (/^SHA3-?(224|256|384|512)$/.test(t)) return 'SHA3-' + t.slice(-3);
|
||||
if (/^SHAKE-?(128|256)$/.test(t)) return 'SHAKE' + t.slice(-3);
|
||||
if (/^MD[245]$/.test(t)) return t;
|
||||
if (/^RIPEMD-?160$|^RMD160$/.test(t)) return 'RIPEMD160';
|
||||
if (/^BLAKE2B-?512$/.test(t)) return 'BLAKE2B512';
|
||||
if (/^BLAKE2S-?256$/.test(t)) return 'BLAKE2S256';
|
||||
if (/^BLAKE2[BS]$/.test(t)) return t;
|
||||
if (/^SM3$/.test(t)) return 'SM3';
|
||||
if (/^KECCAK-?256$/.test(t)) return 'KECCAK256';
|
||||
return null;
|
||||
}
|
||||
|
||||
function rez(o) {
|
||||
return {
|
||||
classification: o.c,
|
||||
quantumVulnerable: !!o.qv,
|
||||
reason: o.motiv,
|
||||
recommendation: o.rec || null,
|
||||
nistQuantumSecurityLevel: o.q,
|
||||
classicalSecurityLevel: o.cl,
|
||||
oid: o.oid,
|
||||
};
|
||||
}
|
||||
|
||||
const SHOR = 'Shor\'s algorithm on a cryptographically relevant quantum computer recovers the private key from the public key';
|
||||
|
||||
// intrarea: { grup, nume?, param?, curba?, hash?, mod?, primitiv?, context? }
|
||||
// iesirea: descrierea completa (nume canonic, primitiv, clasa, motiv, recomandare, niveluri)
|
||||
export function evalueaza(a) {
|
||||
const g = a.grup;
|
||||
const f = (x) => Object.assign({ grup: g, nume: a.nume, primitiv: a.primitiv || 'unknown', param: a.param, curba: a.curba, mod: a.mod, padding: a.padding }, x);
|
||||
|
||||
if (g === 'UNKNOWN') {
|
||||
return f(rez({ c: CLS.U, motiv: a.motiv || 'Algorithm could not be determined statically.', rec: REC.UNKNOWN }));
|
||||
}
|
||||
|
||||
if (g === 'RSA') {
|
||||
const bits = a.param ? Number(a.param) : null;
|
||||
const nume = a.nume || (bits ? `RSA-${bits}` : 'RSA');
|
||||
const prim = a.primitiv || 'unknown';
|
||||
const rec = prim === 'pke' ? REC.PKE : (a.context === 'jwt' ? REC.JWT : (prim === 'signature' ? REC.SIG : `${REC.SIG} If the key is used for encryption: ${REC.PKE}`));
|
||||
const hashSlab = a.hash && HASH[a.hash] && HASH[a.hash].c === CLS.W;
|
||||
if (bits && bits < 2048) {
|
||||
return f({ nume, ...rez({ c: CLS.W, qv: true, motiv: `RSA with a ${bits}-bit modulus is below the 2048-bit minimum (NIST SP 800-131A); also quantum-vulnerable: ${SHOR}.`, rec, q: 0, oid: '1.2.840.113549.1.1.1' }) });
|
||||
}
|
||||
if (hashSlab) {
|
||||
return f({ nume, ...rez({ c: CLS.W, qv: true, motiv: `RSA signature over ${HASH[a.hash].n}, which has practical collision attacks; also quantum-vulnerable: ${SHOR}.`, rec: `${REC.HASH_WEAK} ${rec}`, q: 0 }) });
|
||||
}
|
||||
const cl = bits === 2048 ? 112 : bits === 3072 ? 128 : bits === 7680 ? 192 : bits === 15360 ? 256 : undefined;
|
||||
return f({ nume, ...rez({ c: CLS.V, qv: true, motiv: `RSA: ${SHOR}.`, rec, q: 0, cl, oid: '1.2.840.113549.1.1.1' }) });
|
||||
}
|
||||
|
||||
if (g === 'DSA') {
|
||||
const bits = a.param ? Number(a.param) : null;
|
||||
const nume = a.nume || (bits ? `DSA-${bits}` : 'DSA');
|
||||
if ((bits && bits < 2048) || (a.hash && HASH[a.hash] && HASH[a.hash].c === CLS.W)) {
|
||||
return f({ nume, ...rez({ c: CLS.W, qv: true, motiv: `DSA with ${bits ? bits + '-bit parameters' : 'a weak hash'} is below current minimums; FIPS 186-5 no longer approves DSA for generating signatures; also ${SHOR}.`, rec: REC.SIG, q: 0 }) });
|
||||
}
|
||||
return f({ nume, ...rez({ c: CLS.V, qv: true, motiv: `DSA: ${SHOR} (discrete logarithm). FIPS 186-5 no longer approves DSA for generating signatures.`, rec: REC.SIG, q: 0 }) });
|
||||
}
|
||||
|
||||
if (g === 'DH') {
|
||||
const bits = a.param && /^\d+$/.test(a.param) ? Number(a.param) : null;
|
||||
const nume = a.nume || (a.param ? `DH-${a.param}` : 'DH');
|
||||
if (bits && bits < 2048) {
|
||||
return f({ nume, ...rez({ c: CLS.W, qv: true, motiv: `Finite-field Diffie-Hellman with a ${bits}-bit group is below the 2048-bit minimum; also ${SHOR} (discrete logarithm).`, rec: REC.KEX, q: 0 }) });
|
||||
}
|
||||
return f({ nume, ...rez({ c: CLS.V, qv: true, motiv: `Finite-field Diffie-Hellman: ${SHOR} (discrete logarithm), so recorded key exchanges can be decrypted later.`, rec: REC.KEX, q: 0 }) });
|
||||
}
|
||||
|
||||
if (g === 'EC' || g === 'ECDSA' || g === 'ECDH' || g === 'SECP256K1') {
|
||||
const curba = curbaCanonica(a.curba) || (g === 'SECP256K1' ? 'secp256k1' : null);
|
||||
const e256k1 = curba === 'secp256k1';
|
||||
const eticheta = g === 'SECP256K1' ? 'ECDSA' : g;
|
||||
const nume = a.nume || (curba ? `${eticheta}-${curba}` : eticheta);
|
||||
const cl = curba ? NIVEL_CURBA[curba] : undefined;
|
||||
const prim = a.primitiv || (g === 'ECDH' ? 'key-agree' : (g === 'EC' ? 'other' : 'signature'));
|
||||
let rec = g === 'ECDH' ? REC.KEX : (g === 'EC' ? `Signatures: ${REC.SIG} Key agreement: ${REC.KEX}` : REC.SIG);
|
||||
if (e256k1 && g !== 'ECDH') rec = REC.SECP256K1;
|
||||
if (a.context === 'jwt') rec = REC.JWT;
|
||||
const baza = { nume, primitiv: prim, curba: curba || undefined, grup: e256k1 ? 'SECP256K1' : g };
|
||||
if (cl !== undefined && cl < 112) {
|
||||
return f({ ...baza, ...rez({ c: CLS.W, qv: true, motiv: `Elliptic curve ${curba} gives about ${cl}-bit classical security, below the 112-bit minimum; also ${SHOR} (elliptic-curve discrete logarithm).`, rec, q: 0, cl }) });
|
||||
}
|
||||
if (a.hash && HASH[a.hash] && HASH[a.hash].c === CLS.W) {
|
||||
return f({ ...baza, ...rez({ c: CLS.W, qv: true, motiv: `ECDSA over ${HASH[a.hash].n}, which has practical collision attacks; also ${SHOR}.`, rec: `${REC.HASH_WEAK} ${rec}`, q: 0 }) });
|
||||
}
|
||||
const pe = curba ? ` on ${curba}` : '';
|
||||
const motiv = e256k1
|
||||
? `ECDSA on secp256k1: ${SHOR} (elliptic-curve discrete logarithm). On public blockchains the public key is visible on chain after the first signed transaction.`
|
||||
: `${eticheta}${pe}: ${SHOR} (elliptic-curve discrete logarithm).`;
|
||||
return f({ ...baza, ...rez({ c: CLS.V, qv: true, motiv, rec, q: 0, cl, oid: '1.2.840.10045.2.1' }) });
|
||||
}
|
||||
|
||||
if (g === 'EDDSA') {
|
||||
const nume = a.nume || 'Ed25519';
|
||||
const e448 = /448/.test(nume);
|
||||
const rec = a.context === 'jwt' ? REC.JWT : REC.SIG;
|
||||
return f({ nume, primitiv: 'signature', curba: e448 ? 'Edwards448' : 'Edwards25519', ...rez({ c: CLS.V, qv: true, motiv: `${nume}: ${SHOR} (elliptic-curve discrete logarithm).`, rec, q: 0, cl: e448 ? 224 : 128, oid: e448 ? '1.3.101.113' : '1.3.101.112' }) });
|
||||
}
|
||||
|
||||
if (g === 'XDH') {
|
||||
const nume = a.nume || 'X25519';
|
||||
const e448 = /448/.test(nume);
|
||||
return f({ nume, primitiv: 'key-agree', curba: e448 ? 'Curve448' : 'Curve25519', ...rez({ c: CLS.V, qv: true, motiv: `${nume} key agreement: ${SHOR} (elliptic-curve discrete logarithm), so recorded key exchanges can be decrypted later.`, rec: REC.KEX, q: 0, cl: e448 ? 224 : 128, oid: e448 ? '1.3.101.111' : '1.3.101.110' }) });
|
||||
}
|
||||
|
||||
if (g === 'PAIRING') {
|
||||
return f({ nume: a.nume || 'BLS12-381', primitiv: a.primitiv || 'signature', ...rez({ c: CLS.V, qv: true, motiv: `${a.nume || 'Pairing-based cryptography'}: ${SHOR} (discrete logarithm in the pairing groups).`, rec: REC.SIG, q: 0 }) });
|
||||
}
|
||||
|
||||
if (g === 'CLASSIC-SIG') {
|
||||
// semnatura clasica al carei tip de cheie nu se vede (RSA, DSA sau ECDSA)
|
||||
const h = a.hash && HASH[a.hash];
|
||||
const nume = a.nume || `signature-with-${h ? h.n : 'unknown-hash'}`;
|
||||
if (h && h.c === CLS.W) {
|
||||
return f({ nume, primitiv: 'signature', ...rez({ c: CLS.W, qv: true, motiv: `${a.motiv || 'Classical signature'} over ${h.n}, which has practical collision attacks; the key type (RSA, DSA or ECDSA) is also quantum-vulnerable.`, rec: `${REC.HASH_WEAK} ${REC.SIG}`, q: 0 }) });
|
||||
}
|
||||
return f({ nume, primitiv: 'signature', ...rez({ c: CLS.V, qv: true, motiv: `${a.motiv || 'Classical signature (RSA, DSA or ECDSA key)'}: ${SHOR}.`, rec: REC.SIG, q: 0 }) });
|
||||
}
|
||||
|
||||
if (g === 'HASH') {
|
||||
const h = HASH[a.hash];
|
||||
if (!h) return f({ nume: a.nume || String(a.hash), primitiv: 'hash', ...rez({ c: CLS.U, motiv: `Hash "${a.hash}" is not in this tool's catalog.`, rec: REC.UNKNOWN }) });
|
||||
const nota = a.nota ? ` ${a.nota}` : '';
|
||||
if (h.c === CLS.W) {
|
||||
const motiv = a.hash === 'RIPEMD160'
|
||||
? `RIPEMD-160 has a 160-bit output (about 80-bit collision resistance), below the 112-bit minimum for new designs.${nota}`
|
||||
: `${h.n} has practical collision attacks; unsafe for signatures, certificates and integrity against an adversary.${nota}`;
|
||||
return f({ nume: h.n, primitiv: 'hash', ...rez({ c: CLS.W, motiv, rec: REC.HASH_WEAK, q: 0, oid: h.oid }) });
|
||||
}
|
||||
return f({ nume: h.n, primitiv: h.xof ? 'xof' : 'hash', ...rez({ c: CLS.S, motiv: `${h.n}: quantum attacks give at most a polynomial speed-up for collisions and preimages (Grover, BHT); output size keeps it within NIST categories.${nota}`, q: h.q, cl: h.cl, oid: h.oid }) });
|
||||
}
|
||||
|
||||
if (g === 'MAC') {
|
||||
if (!a.hash) return f({ nume: 'HMAC', primitiv: 'mac', ...rez({ c: CLS.U, motiv: 'HMAC whose hash function is bound to the key object (set where the key is imported or generated), not visible at this call.', rec: REC.UNKNOWN }) });
|
||||
const h = HASH[a.hash];
|
||||
const nume = `HMAC-${h ? h.n : String(a.hash || 'unknown')}`;
|
||||
if (!h) return f({ nume, primitiv: 'mac', ...rez({ c: CLS.U, motiv: `HMAC hash "${a.hash}" is not in this tool's catalog.`, rec: REC.UNKNOWN }) });
|
||||
if (h.c === CLS.W) {
|
||||
return f({ nume, primitiv: 'mac', ...rez({ c: CLS.W, motiv: `${nume}: HMAC does not depend on collision resistance and has no practical break, but ${h.n} is deprecated for new designs and should be retired.`, rec: 'Move to HMAC-SHA-256 or HMAC-SHA-384.', q: 0 }) });
|
||||
}
|
||||
return f({ nume, primitiv: 'mac', ...rez({ c: CLS.S, motiv: `${nume}: symmetric; a quantum attacker gains at most a quadratic speed-up (Grover) on key search.` }) });
|
||||
}
|
||||
|
||||
if (g === 'KDF') {
|
||||
const h = a.hash ? HASH[a.hash] : null;
|
||||
const nume = a.nume || 'KDF';
|
||||
if (h && h.c === CLS.W) {
|
||||
return f({ nume, primitiv: 'kdf', ...rez({ c: CLS.W, motiv: `${nume} uses ${h.n}; not practically broken as a KDF, but deprecated for new designs.`, rec: 'Use PBKDF2-HMAC-SHA-256 (high iteration count), scrypt or Argon2id.', q: 0 }) });
|
||||
}
|
||||
return f({ nume, primitiv: 'kdf', ...rez({ c: CLS.S, motiv: `${nume}: symmetric key derivation; a quantum attacker gains at most a quadratic speed-up (Grover).` }) });
|
||||
}
|
||||
|
||||
if (g === 'CIPHER') return cifru(a, f);
|
||||
|
||||
if (g === 'MLKEM') {
|
||||
const p = String(a.param || '');
|
||||
const q = p === '512' ? 1 : p === '768' ? 3 : p === '1024' ? 5 : undefined;
|
||||
const oid = p === '512' ? '2.16.840.1.101.3.4.4.1' : p === '768' ? '2.16.840.1.101.3.4.4.2' : p === '1024' ? '2.16.840.1.101.3.4.4.3' : undefined;
|
||||
const nume = p ? `ML-KEM-${p}` : 'ML-KEM';
|
||||
return f({ nume, primitiv: 'kem', ...rez({ c: CLS.S, motiv: `${nume}: module-lattice KEM standardized in FIPS 203${q ? `, NIST category ${q}` : ' (parameter set not visible)'}.`, q, oid }) });
|
||||
}
|
||||
if (g === 'MLDSA') {
|
||||
const p = String(a.param || '');
|
||||
const q = p === '44' ? 2 : p === '65' ? 3 : p === '87' ? 5 : undefined;
|
||||
const oid = p === '44' ? '2.16.840.1.101.3.4.3.17' : p === '65' ? '2.16.840.1.101.3.4.3.18' : p === '87' ? '2.16.840.1.101.3.4.3.19' : undefined;
|
||||
const nume = p ? `ML-DSA-${p}` : 'ML-DSA';
|
||||
return f({ nume, primitiv: 'signature', ...rez({ c: CLS.S, motiv: `${nume}: module-lattice signature standardized in FIPS 204${q ? `, NIST category ${q}` : ' (parameter set not visible)'}.`, q, oid }) });
|
||||
}
|
||||
if (g === 'SLHDSA') {
|
||||
const p = String(a.param || '');
|
||||
const m = /(128|192|256)/.exec(p);
|
||||
const q = m ? ({ 128: 1, 192: 3, 256: 5 })[m[1]] : undefined;
|
||||
const nume = p ? `SLH-DSA-${p}` : 'SLH-DSA';
|
||||
return f({ nume, primitiv: 'signature', ...rez({ c: CLS.S, motiv: `${nume}: stateless hash-based signature standardized in FIPS 205${q ? `, NIST category ${q}` : ''}.`, q }) });
|
||||
}
|
||||
if (g === 'FALCON') {
|
||||
const p = String(a.param || '');
|
||||
const q = p === '512' ? 1 : p === '1024' ? 5 : undefined;
|
||||
const nume = p ? `Falcon-${p}` : 'Falcon';
|
||||
return f({ nume, primitiv: 'signature', ...rez({ c: CLS.S, motiv: `${nume}: NTRU-lattice signature selected by NIST (to be standardized as FN-DSA)${q ? `, NIST category ${q}` : ''}.`, q }) });
|
||||
}
|
||||
if (g === 'HASHSIG') {
|
||||
return f({ nume: a.nume || 'XMSS/LMS', primitiv: 'signature', ...rez({ c: CLS.S, motiv: `${a.nume || 'Stateful hash-based signature'}: approved in NIST SP 800-208; security depends on never reusing a one-time key state.` }) });
|
||||
}
|
||||
if (g === 'PREPQ') {
|
||||
return f({ nume: a.nume, primitiv: a.primitiv, ...rez({ c: CLS.S, motiv: `${a.nume}: pre-standard version of a NIST-selected post-quantum scheme.`, rec: REC.PREPQ }) });
|
||||
}
|
||||
if (g === 'BROKENPQ') {
|
||||
return f({ nume: a.nume, primitiv: a.primitiv, ...rez({ c: CLS.W, motiv: `${a.nume} was broken by classical attacks in 2022.`, rec: REC.BROKEN_PQ, q: 0 }) });
|
||||
}
|
||||
if (g === 'HQC') {
|
||||
return f({ nume: a.nume || 'HQC', primitiv: 'kem', ...rez({ c: CLS.S, motiv: 'HQC: code-based KEM selected by NIST in 2025 as a second KEM; final standard pending.' }) });
|
||||
}
|
||||
if (g === 'HYBRID-KEX') {
|
||||
const pq = /1024/.test(a.nume) ? 5 : 3;
|
||||
return f({ nume: a.nume, primitiv: 'kem', ...rez({ c: CLS.S, motiv: `${a.nume}: hybrid key exchange (classical ECDH + ML-KEM); stays secure if either component holds. The NIST category given is that of the ML-KEM component.`, q: pq }) });
|
||||
}
|
||||
if (g === 'TLS') return tls(a, f);
|
||||
if (g === 'JWT-HMAC') {
|
||||
const h = HASH[a.hash];
|
||||
return f({ nume: a.nume, primitiv: 'mac', ...rez({ c: CLS.S, motiv: `${a.nume} is HMAC-${h ? h.n : a.hash}: symmetric, so a quantum attacker gains at most a quadratic speed-up (Grover); the shared secret must be long and random.` }) });
|
||||
}
|
||||
if (g === 'JWT-NONE') {
|
||||
return f({ nume: 'JWS none', primitiv: 'signature', ...rez({ c: CLS.W, motiv: 'The "none" algorithm means the token is not signed at all.', rec: REC.JWT_NONE, q: 0 }) });
|
||||
}
|
||||
if (g === 'LIB-MULTI') {
|
||||
return f({ nume: a.nume, primitiv: 'unknown', ...rez({ c: CLS.U, motiv: a.motiv || `Imports ${a.nume}; no specific algorithm call recognized in this file.`, rec: REC.LIB_MULTI }) });
|
||||
}
|
||||
if (g === 'SSH') {
|
||||
return f({ nume: a.nume || 'SSH', primitiv: 'unknown', ...rez({ c: CLS.U, motiv: a.motiv || 'SSH: key exchange and host-key algorithms are negotiated at run time and not visible here.', rec: 'Check the configured key-exchange algorithms: OpenSSH 9.9+ offers mlkem768x25519-sha256 and 9.0+ sntrup761x25519-sha512; host keys remain classical (Ed25519, ECDSA, RSA).' }) });
|
||||
}
|
||||
return f(rez({ c: CLS.U, motiv: `No classification rule for group "${g}".`, rec: REC.UNKNOWN }));
|
||||
}
|
||||
|
||||
function cifru(a, f) {
|
||||
const alg = String(a.nume || '').toUpperCase();
|
||||
const mod = a.mod ? String(a.mod).toLowerCase() : undefined;
|
||||
if (/^(DES|DES-?CBC|DES-?ECB|DES-?CFB|DES-?OFB)$/.test(alg) || alg === 'DES') {
|
||||
return f({ nume: mod ? `DES-${mod.toUpperCase()}` : 'DES', primitiv: 'block-cipher', mod, ...rez({ c: CLS.W, motiv: 'DES has a 56-bit key and is brute-forceable today.', rec: REC.CIPHER_WEAK, q: 0 }) });
|
||||
}
|
||||
if (/^(3DES|DESEDE|DES-?EDE3?|TRIPLEDES|TDEA|DES3)$/.test(alg)) {
|
||||
return f({ nume: mod ? `3DES-${mod.toUpperCase()}` : '3DES', primitiv: 'block-cipher', mod, ...rez({ c: CLS.W, motiv: '3DES (TDEA) has a 64-bit block (Sweet32 birthday attacks) and was disallowed by NIST after 2023.', rec: REC.CIPHER_WEAK, q: 0 }) });
|
||||
}
|
||||
if (/^(RC4|ARC4|ARCFOUR)$/.test(alg)) {
|
||||
return f({ nume: 'RC4', primitiv: 'stream-cipher', ...rez({ c: CLS.W, motiv: 'RC4 has exploitable keystream biases and is prohibited in TLS (RFC 7465).', rec: REC.CIPHER_WEAK, q: 0 }) });
|
||||
}
|
||||
if (/^(RC2|ARC2)$/.test(alg)) {
|
||||
return f({ nume: 'RC2', primitiv: 'block-cipher', ...rez({ c: CLS.W, motiv: 'RC2 is an obsolete cipher with a 64-bit block.', rec: REC.CIPHER_WEAK, q: 0 }) });
|
||||
}
|
||||
if (/^(BLOWFISH|BF)$/.test(alg)) {
|
||||
return f({ nume: 'Blowfish', primitiv: 'block-cipher', mod, ...rez({ c: CLS.W, motiv: 'Blowfish has a 64-bit block (Sweet32 birthday attacks).', rec: REC.CIPHER_WEAK, q: 0 }) });
|
||||
}
|
||||
if (/^(CAST5|CAST|IDEA|SEED)$/.test(alg)) {
|
||||
return f({ nume: alg, primitiv: 'block-cipher', mod, ...rez({ c: CLS.W, motiv: `${alg} has a 64-bit block (Sweet32 birthday attacks) or is obsolete.`, rec: REC.CIPHER_WEAK, q: 0 }) });
|
||||
}
|
||||
if (/^CHACHA20(-POLY1305)?$|^XCHACHA20-POLY1305$|^XSALSA20-POLY1305$|^SALSA20$/.test(alg)) {
|
||||
const ae = /POLY1305/.test(alg);
|
||||
return f({ nume: a.nume, primitiv: ae ? 'ae' : 'stream-cipher', ...rez({ c: CLS.S, motiv: `${a.nume}: 256-bit key; a quantum attacker gains at most a quadratic speed-up (Grover) on key search.` }) });
|
||||
}
|
||||
if (/^AES/.test(alg) || /^CAMELLIA/.test(alg) || /^SM4/.test(alg) || /^ARIA/.test(alg)) {
|
||||
const familie = /^CAMELLIA/.test(alg) ? 'Camellia' : /^SM4/.test(alg) ? 'SM4' : /^ARIA/.test(alg) ? 'ARIA' : 'AES';
|
||||
const bits = a.param ? Number(a.param) : (familie === 'SM4' ? 128 : null);
|
||||
const numeMod = mod ? `-${mod.toUpperCase()}` : '';
|
||||
const nume = `${familie}${bits ? '-' + bits : ''}${numeMod}`;
|
||||
const ae = mod && /^(gcm|ccm|ocb|siv|gcm-siv|eax|poly1305)$/.test(mod);
|
||||
const prim = ae ? 'ae' : 'block-cipher';
|
||||
const modCdx = mod && ['cbc', 'ecb', 'ccm', 'gcm', 'cfb', 'ofb', 'ctr'].includes(mod) ? mod : (mod ? 'other' : undefined);
|
||||
if (mod === 'ecb') {
|
||||
return f({ nume, primitiv: prim, mod: modCdx, ...rez({ c: CLS.W, motiv: `${nume}: ECB mode encrypts equal blocks to equal ciphertext and leaks plaintext structure.${a.nota ? ' ' + a.nota : ''}`, rec: REC.ECB, q: 0 }) });
|
||||
}
|
||||
const q = bits === 128 ? 1 : bits === 192 ? 3 : bits === 256 ? 5 : undefined;
|
||||
const cl = bits || undefined;
|
||||
const rec = bits === 128 ? REC.AES128 : (bits ? null : REC.AES_SIZE);
|
||||
const motiv = bits === 128
|
||||
? `${nume}: symmetric; Grover's algorithm reduces key search at most quadratically, NIST category 1.`
|
||||
: bits ? `${nume}: symmetric ${bits}-bit key; Grover's algorithm reduces key search at most quadratically, NIST category ${q}.`
|
||||
: `${nume}: symmetric; key size not visible statically (AES-128 is NIST category 1, AES-256 category 5).`;
|
||||
return f({ nume, primitiv: prim, mod: modCdx, param: bits ? String(bits) : undefined, ...rez({ c: CLS.S, motiv, rec, q, cl }) });
|
||||
}
|
||||
return f({ nume: a.nume, primitiv: 'unknown', ...rez({ c: CLS.U, motiv: `Cipher "${a.nume}" is not in this tool's catalog.`, rec: REC.UNKNOWN }) });
|
||||
}
|
||||
|
||||
// a.param = versiunea ('1.0','1.1','1.2','1.3','ssl3','negotiated'); a.rol = 'min'|'max'|'only'
|
||||
function tls(a, f) {
|
||||
const v = String(a.param || '');
|
||||
const rol = a.rol || 'only';
|
||||
const numeV = v === 'ssl3' ? 'SSLv3' : v === 'ssl2' ? 'SSLv2' : (v === 'negotiated' ? 'TLS (negotiated)' : `TLSv${v}`);
|
||||
const nume = a.nume || numeV;
|
||||
const baza = { nume, primitiv: 'other', protocolType: 'tls', protocolVersion: /^1\.[0-3]$/.test(v) ? v : (v === 'ssl3' ? '3.0' : undefined), rol };
|
||||
if (v === 'ssl2' || v === 'ssl3' || v === '1.0' || v === '1.1') {
|
||||
if (rol === 'max') {
|
||||
return f({ ...baza, ...rez({ c: CLS.W, qv: true, motiv: `${numeV} as the maximum version caps the connection at a deprecated protocol (RFC 8996).`, rec: REC.TLS_OLD, q: 0 }) });
|
||||
}
|
||||
return f({ ...baza, ...rez({ c: CLS.W, qv: true, motiv: `${numeV} is deprecated (RFC 8996) and ${rol === 'min' ? 'allowed as the minimum version' : 'selected'} here.`, rec: REC.TLS_OLD, q: 0 }) });
|
||||
}
|
||||
if (v === '1.2') {
|
||||
if (rol === 'min') {
|
||||
return f({ ...baza, ...rez({ c: CLS.V, qv: true, motiv: 'TLS 1.2 is allowed; TLS 1.2 has no standardized post-quantum key exchange, so a peer that negotiates it uses classical (EC)DHE or RSA key exchange.', rec: REC.TLS12, q: 0 }) });
|
||||
}
|
||||
return f({ ...baza, ...rez({ c: CLS.V, qv: true, motiv: `TLS 1.2 ${rol === 'max' ? 'is the maximum version' : 'is selected'}; TLS 1.2 has no standardized post-quantum key exchange, so key exchange is classical (EC)DHE or RSA.`, rec: REC.TLS12, q: 0 }) });
|
||||
}
|
||||
if (v === '1.3') {
|
||||
if (rol === 'max') {
|
||||
return f({ ...baza, ...rez({ c: CLS.U, motiv: 'TLS 1.3 is the maximum version; the minimum and the key-exchange groups are not visible here.', rec: REC.TLS13 }) });
|
||||
}
|
||||
return f({ ...baza, ...rez({ c: CLS.U, motiv: 'TLS 1.3 only: whether key exchange is post-quantum depends on the negotiated group (X25519MLKEM768 or a classical group), which depends on runtime defaults and the peer.', rec: REC.TLS13 }) });
|
||||
}
|
||||
return f({ ...baza, ...rez({ c: CLS.U, motiv: 'Protocol version is negotiated at run time from the runtime defaults; not visible statically.', rec: REC.TLS13 }) });
|
||||
}
|
||||
|
||||
// grupurile de schimb de chei TLS (ecdhCurve, CurvePreferences, jdk.tls.namedGroups)
|
||||
export function grupTls(nume) {
|
||||
const t = String(nume).trim();
|
||||
if (/^(X25519MLKEM768|X25519Kyber768Draft00|SecP256r1MLKEM768|SecP384r1MLKEM1024|p256_mlkem768|p384_mlkem1024|x25519_mlkem768)$/i.test(t)) {
|
||||
if (/kyber/i.test(t)) return { grup: 'PREPQ', nume: t, primitiv: 'kem' };
|
||||
return { grup: 'HYBRID-KEX', nume: t };
|
||||
}
|
||||
if (/^(X25519|x25519)$/.test(t)) return { grup: 'XDH', nume: 'X25519' };
|
||||
if (/^(X448|x448)$/.test(t)) return { grup: 'XDH', nume: 'X448' };
|
||||
if (/^(MLKEM(512|768|1024)|mlkem(512|768|1024))$/i.test(t)) return { grup: 'MLKEM', param: t.replace(/\D/g, '') };
|
||||
if (/^(ffdhe\d+)$/i.test(t)) return { grup: 'DH', param: t.toLowerCase() };
|
||||
const c = curbaCanonica(t);
|
||||
if (c && /^secp|^brainpool/.test(c)) return { grup: 'ECDH', curba: c };
|
||||
if (/^auto$/i.test(t)) return { grup: 'UNKNOWN', motiv: 'ecdhCurve "auto" selects groups from the runtime defaults; not visible statically.' };
|
||||
return { grup: 'UNKNOWN', motiv: `Key-exchange group "${t}" is not in this tool's catalog.` };
|
||||
}
|
||||
|
||||
// algoritmii JWS (RFC 7518 + RFC 8037 + RFC 8812)
|
||||
export function jws(alg) {
|
||||
const m = /^(HS|RS|PS|ES)(256|384|512)$/.exec(alg);
|
||||
if (m) {
|
||||
const hash = 'SHA' + m[2];
|
||||
if (m[1] === 'HS') return { grup: 'JWT-HMAC', nume: `JWS ${alg}`, hash, context: 'jwt' };
|
||||
if (m[1] === 'RS') return { grup: 'RSA', nume: `JWS ${alg}`, primitiv: 'signature', padding: 'pkcs1v15', hash, context: 'jwt' };
|
||||
if (m[1] === 'PS') return { grup: 'RSA', nume: `JWS ${alg}`, primitiv: 'signature', padding: 'other', hash, context: 'jwt' };
|
||||
const curba = m[2] === '256' ? 'secp256r1' : m[2] === '384' ? 'secp384r1' : 'secp521r1';
|
||||
return { grup: 'ECDSA', nume: `JWS ${alg}`, curba, hash, context: 'jwt' };
|
||||
}
|
||||
if (alg === 'ES256K') return { grup: 'ECDSA', nume: 'JWS ES256K', curba: 'secp256k1', context: 'jwt' };
|
||||
if (alg === 'EdDSA' || alg === 'Ed25519') return { grup: 'EDDSA', nume: `JWS ${alg}`, context: 'jwt' };
|
||||
if (alg === 'none') return { grup: 'JWT-NONE' };
|
||||
return null;
|
||||
}
|
||||
export const JWS_RE = /^(?:(?:HS|RS|PS|ES)(?:256|384|512)|ES256K|EdDSA|Ed25519|none)$/;
|
||||
@@ -0,0 +1,158 @@
|
||||
// Constructia CBOM-ului CycloneDX 1.6 din rezultatul scanarii.
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
|
||||
export const ENUM = Object.freeze({
|
||||
componentType: ['application', 'framework', 'library', 'container', 'platform', 'operating-system', 'device', 'device-driver', 'firmware', 'file', 'machine-learning-model', 'data', 'cryptographic-asset'],
|
||||
assetType: ['algorithm', 'certificate', 'protocol', 'related-crypto-material'],
|
||||
primitive: ['drbg', 'mac', 'block-cipher', 'stream-cipher', 'signature', 'hash', 'pke', 'xof', 'kdf', 'key-agree', 'kem', 'ae', 'combiner', 'other', 'unknown'],
|
||||
mode: ['cbc', 'ecb', 'ccm', 'gcm', 'cfb', 'ofb', 'ctr', 'other', 'unknown'],
|
||||
padding: ['pkcs5', 'pkcs7', 'pkcs1v15', 'oaep', 'raw', 'other', 'unknown'],
|
||||
cryptoFunctions: ['generate', 'keygen', 'encrypt', 'decrypt', 'digest', 'tag', 'keyderive', 'sign', 'verify', 'encapsulate', 'decapsulate', 'other', 'unknown'],
|
||||
protocolType: ['tls', 'ssh', 'ipsec', 'ike', 'sstp', 'wpa', 'other', 'unknown'],
|
||||
materialType: ['private-key', 'public-key', 'secret-key', 'key', 'ciphertext', 'signature', 'digest', 'initialization-vector', 'nonce', 'seed', 'salt', 'shared-secret', 'tag', 'additional-data', 'password', 'credential', 'token', 'other', 'unknown'],
|
||||
});
|
||||
|
||||
const P = 'aere:crypto-inventory:';
|
||||
|
||||
function slug(s) {
|
||||
return String(s).toLowerCase().replace(/[^a-z0-9.+-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60) || 'x';
|
||||
}
|
||||
|
||||
function cheieComponenta(g) {
|
||||
return [g.assetType, g.name, g.primitive, g.classification, g.parameterSetIdentifier, g.curve, g.mode, g.padding,
|
||||
g.protocolType, g.protocolVersion, g.material && g.material.type, g.certificate && g.certificate.fingerprintSha256, g.reason].map((x) => x ?? '').join('|');
|
||||
}
|
||||
|
||||
function uuidDin(hex) {
|
||||
const h = hex.slice(0, 32).split('');
|
||||
h[12] = '5';
|
||||
h[16] = '89ab'[parseInt(h[16], 16) & 3];
|
||||
const s = h.join('');
|
||||
return `${s.slice(0, 8)}-${s.slice(8, 12)}-${s.slice(12, 16)}-${s.slice(16, 20)}-${s.slice(20, 32)}`;
|
||||
}
|
||||
|
||||
export function construiesteCbom(rez, optiuni = {}) {
|
||||
const grupuri = new Map();
|
||||
for (const g of rez.findings) {
|
||||
const k = cheieComponenta(g);
|
||||
if (!grupuri.has(k)) grupuri.set(k, []);
|
||||
grupuri.get(k).push(g);
|
||||
}
|
||||
const componente = [];
|
||||
for (const [k, lista] of grupuri) {
|
||||
const g = lista[0];
|
||||
const ref = `crypto:${g.assetType}:${slug(g.certificate ? 'x509-' + (g.certificate.subjectName || 'certificate') : g.name)}:${createHash('sha256').update(k).digest('hex').slice(0, 12)}`;
|
||||
const cp = { assetType: g.assetType };
|
||||
if (g.assetType === 'algorithm') {
|
||||
const ap = { primitive: ENUM.primitive.includes(g.primitive) ? g.primitive : 'unknown' };
|
||||
if (g.parameterSetIdentifier) ap.parameterSetIdentifier = g.parameterSetIdentifier;
|
||||
if (g.curve) ap.curve = g.curve;
|
||||
if (g.mode) ap.mode = ENUM.mode.includes(g.mode) ? g.mode : 'other';
|
||||
if (g.padding) ap.padding = ENUM.padding.includes(g.padding) ? g.padding : 'other';
|
||||
const f = [...new Set(lista.flatMap((x) => x.cryptoFunctions || []))].filter((x) => ENUM.cryptoFunctions.includes(x)).sort();
|
||||
if (f.length) ap.cryptoFunctions = f;
|
||||
if (Number.isInteger(g.classicalSecurityLevel)) ap.classicalSecurityLevel = g.classicalSecurityLevel;
|
||||
if (Number.isInteger(g.nistQuantumSecurityLevel)) ap.nistQuantumSecurityLevel = g.nistQuantumSecurityLevel;
|
||||
cp.algorithmProperties = ap;
|
||||
} else if (g.assetType === 'protocol') {
|
||||
cp.protocolProperties = { type: ENUM.protocolType.includes(g.protocolType) ? g.protocolType : 'unknown' };
|
||||
if (g.protocolVersion) cp.protocolProperties.version = g.protocolVersion;
|
||||
} else if (g.assetType === 'related-crypto-material') {
|
||||
const m = { type: ENUM.materialType.includes(g.material && g.material.type) ? g.material.type : 'unknown' };
|
||||
if (g.material && g.material.format) m.format = g.material.format;
|
||||
if (g.parameterSetIdentifier && /^\d+$/.test(g.parameterSetIdentifier)) m.size = Number(g.parameterSetIdentifier);
|
||||
cp.relatedCryptoMaterialProperties = m;
|
||||
} else if (g.assetType === 'certificate') {
|
||||
const c = g.certificate || {};
|
||||
const cert = {};
|
||||
for (const camp of ['subjectName', 'issuerName', 'notValidBefore', 'notValidAfter', 'certificateFormat', 'certificateExtension']) if (c[camp]) cert[camp] = c[camp];
|
||||
cp.certificateProperties = cert;
|
||||
}
|
||||
if (g.oid) cp.oid = g.oid;
|
||||
const props = [
|
||||
{ name: `${P}classification`, value: g.classification },
|
||||
{ name: `${P}quantum-vulnerable`, value: String(!!g.quantumVulnerable) },
|
||||
{ name: `${P}reason`, value: g.reason },
|
||||
];
|
||||
if (g.recommendation) props.push({ name: `${P}recommendation`, value: g.recommendation });
|
||||
if (g.assetType !== 'algorithm') props.push({ name: `${P}key-algorithm`, value: g.name });
|
||||
if (g.certificate && g.certificate.fingerprintSha256) props.push({ name: `${P}certificate-sha256-fingerprint`, value: g.certificate.fingerprintSha256 });
|
||||
props.push({ name: `${P}languages`, value: [...new Set(lista.map((x) => x.language))].sort().join(',') });
|
||||
props.push({ name: `${P}evidence-kinds`, value: [...new Set(lista.map((x) => x.evidenceKind))].sort().join(',') });
|
||||
const rezolvate = [...new Set(lista.filter((x) => x.resolvedFrom).map((x) => `${x.file}:${x.line} <- ${x.resolvedFrom}`))];
|
||||
if (rezolvate.length) props.push({ name: `${P}resolved-from`, value: rezolvate.join('; ') });
|
||||
const nume = g.assetType === 'certificate' ? `X.509 certificate${g.certificate && g.certificate.subjectName ? ' ' + g.certificate.subjectName : ''}`
|
||||
: g.assetType === 'related-crypto-material' ? `${g.name} ${g.material ? g.material.type : 'key material'}` : g.name;
|
||||
componente.push({
|
||||
type: 'cryptographic-asset',
|
||||
'bom-ref': ref,
|
||||
name: nume,
|
||||
cryptoProperties: cp,
|
||||
evidence: {
|
||||
occurrences: lista.map((x) => {
|
||||
const oc = { location: x.file, line: x.line, symbol: x.api };
|
||||
if (x.context) oc.additionalContext = x.context;
|
||||
return oc;
|
||||
}),
|
||||
},
|
||||
properties: props,
|
||||
});
|
||||
}
|
||||
// bibliotecile declarate, unite pe (ecosistem, nume, versiune)
|
||||
const libs = new Map();
|
||||
for (const b of rez.libraries) {
|
||||
const k = `${b.ecosystem}|${b.name}|${b.version || ''}`;
|
||||
if (!libs.has(k)) libs.set(k, []);
|
||||
libs.get(k).push(b);
|
||||
}
|
||||
for (const [k, lista] of libs) {
|
||||
const b = lista[0];
|
||||
const c = {
|
||||
type: 'library',
|
||||
'bom-ref': `library:${b.ecosystem}:${slug(b.name)}:${createHash('sha256').update(k).digest('hex').slice(0, 12)}`,
|
||||
name: b.name,
|
||||
};
|
||||
if (b.version) c.version = b.version;
|
||||
if (b.purl) c.purl = b.purl;
|
||||
c.evidence = { occurrences: lista.map((x) => ({ location: x.file, line: x.line })) };
|
||||
c.properties = [
|
||||
{ name: `${P}declared-only`, value: 'true' },
|
||||
{ name: `${P}note`, value: 'Declared in a dependency manifest. Declaration is not use: see the cryptographic-asset components for code that calls it.' },
|
||||
{ name: `${P}provides`, value: b.provides },
|
||||
];
|
||||
componente.push(c);
|
||||
}
|
||||
componente.sort((a, b) => (a['bom-ref'] < b['bom-ref'] ? -1 : a['bom-ref'] > b['bom-ref'] ? 1 : 0));
|
||||
|
||||
const s = rez.stats;
|
||||
const metaProps = [
|
||||
['files-seen', s.filesSeen], ['code-files-analyzed', s.codeFilesTotal], ['text-files-pem-only', s.textFilesPemOnly],
|
||||
['not-read-binary', s.notRead.binary], ['not-read-too-large', s.notRead.tooLarge], ['not-read-file-limit', s.notRead.fileLimit],
|
||||
['not-read-unreadable', s.notRead.unreadable], ['symlinks-not-followed', s.symlinksNotFollowed],
|
||||
['files-over-resolution-limit', s.resolutionLimitFiles], ['max-resolved-variables-per-file', rez.options.maxResolvedVariablesPerFile],
|
||||
['dirs-excluded', Object.entries(s.dirsExcluded).map(([n, c]) => `${n}:${c}`).join(',') || 'none'],
|
||||
['max-file-bytes', rez.options.maxFileBytes], ['max-files', rez.options.maxFiles],
|
||||
['method', 'static pattern analysis of source text; no code from the scanned tree is executed'],
|
||||
].map(([n, v]) => ({ name: `${P}${n}`, value: String(v) }));
|
||||
|
||||
const bom = {
|
||||
$schema: 'http://cyclonedx.org/schema/bom-1.6.schema.json',
|
||||
bomFormat: 'CycloneDX',
|
||||
specVersion: '1.6',
|
||||
serialNumber: '',
|
||||
version: 1,
|
||||
metadata: {
|
||||
tools: { components: [{ type: 'application', name: rez.tool.name, version: rez.tool.version, description: 'Static cryptographic inventory of source code (CBOM)' }] },
|
||||
component: { type: 'application', 'bom-ref': 'scanned-target', name: rez.rootName },
|
||||
properties: metaProps,
|
||||
},
|
||||
components: componente,
|
||||
};
|
||||
if (optiuni.deterministic) {
|
||||
bom.serialNumber = `urn:uuid:${uuidDin(createHash('sha256').update(JSON.stringify(componente)).digest('hex'))}`;
|
||||
} else {
|
||||
bom.metadata = { timestamp: rez.finishedAt, ...bom.metadata };
|
||||
bom.serialNumber = `urn:uuid:${randomUUID()}`;
|
||||
}
|
||||
return bom;
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
// Contextul unui fisier scanat si uneltele comune ale detectorilor.
|
||||
import { curata, inSir, inceputuriDeRand, randul } from './lexer.mjs';
|
||||
import { evalueaza } from './catalog.mjs';
|
||||
|
||||
export function escapeRe(s) {
|
||||
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
export function facContext(text, limbaj, rel) {
|
||||
const { code, siruri } = curata(text, limbaj);
|
||||
const inceputuri = inceputuriDeRand(text);
|
||||
const ctx = {
|
||||
text, code, siruri, limbaj, rel,
|
||||
gasiri: [],
|
||||
inSir: (pos) => inSir(siruri, pos),
|
||||
poz: (pos) => randul(inceputuri, pos),
|
||||
// potriviri in cod: numele API-ului (inceputul potrivirii) nu are voie sa fie intr-un sir
|
||||
potriviri(re) {
|
||||
const r = [];
|
||||
const g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g');
|
||||
for (const m of code.matchAll(g)) if (!inSir(siruri, m.index)) r.push(m);
|
||||
return r;
|
||||
},
|
||||
adauga(pos, api, fel, activ, extra = {}) {
|
||||
const ev = evalueaza(activ);
|
||||
const { line, column } = randul(inceputuri, pos);
|
||||
const bucata = extra.bucata !== undefined ? extra.bucata : '';
|
||||
ctx.gasiri.push({
|
||||
file: rel,
|
||||
line,
|
||||
column,
|
||||
language: limbaj,
|
||||
api,
|
||||
evidenceKind: fel,
|
||||
assetType: activ.assetType || (ev.protocolType ? 'protocol' : 'algorithm'),
|
||||
group: ev.grup,
|
||||
name: ev.nume || activ.nume || api,
|
||||
primitive: ev.primitiv || 'unknown',
|
||||
parameterSetIdentifier: ev.param !== undefined && ev.param !== null && ev.param !== '' ? String(ev.param) : undefined,
|
||||
curve: ev.curba || undefined,
|
||||
mode: ev.mod || undefined,
|
||||
padding: ev.padding || activ.padding || undefined,
|
||||
cryptoFunctions: activ.functii || undefined,
|
||||
classification: ev.classification,
|
||||
quantumVulnerable: ev.quantumVulnerable,
|
||||
reason: activ.nota && !ev.reason.includes(activ.nota) ? `${ev.reason} ${activ.nota}` : ev.reason,
|
||||
recommendation: ev.recommendation,
|
||||
nistQuantumSecurityLevel: ev.nistQuantumSecurityLevel,
|
||||
classicalSecurityLevel: ev.classicalSecurityLevel,
|
||||
oid: ev.oid,
|
||||
protocolType: ev.protocolType,
|
||||
protocolVersion: ev.protocolVersion,
|
||||
suppressedBy: extra.suprimaDe || undefined,
|
||||
resolvedFrom: extra.rezolvatDin || undefined,
|
||||
context: normalizeaza(bucata),
|
||||
material: activ.material || undefined,
|
||||
certificate: activ.certificat || undefined,
|
||||
});
|
||||
},
|
||||
};
|
||||
return ctx;
|
||||
}
|
||||
|
||||
function normalizeaza(s) {
|
||||
const t = String(s || '').replace(/\s+/g, ' ').trim();
|
||||
return t.length > 120 ? t.slice(0, 117) + '...' : t;
|
||||
}
|
||||
|
||||
// Argumentele unui apel: de la pozitia de dupa '(' pana la ')' echilibrata.
|
||||
// Intoarce textul argumentelor (din vederea de cod) si limitele lui.
|
||||
export function argumente(ctx, dupaParanteza) {
|
||||
const c = ctx.code;
|
||||
let adancime = 1;
|
||||
let j = dupaParanteza;
|
||||
while (j < c.length) {
|
||||
if (ctx.inSir(j)) { j++; continue; }
|
||||
const ch = c[j];
|
||||
if (ch === '(' || ch === '[' || ch === '{') adancime++;
|
||||
else if (ch === ')' || ch === ']' || ch === '}') { adancime--; if (adancime === 0) break; }
|
||||
j++;
|
||||
if (j - dupaParanteza > 4000) break;
|
||||
}
|
||||
return { text: c.slice(dupaParanteza, j), start: dupaParanteza, end: j };
|
||||
}
|
||||
|
||||
// imparte argumentele pe virgulele de nivel zero
|
||||
export function imparteArgumente(ctx, start, end) {
|
||||
const c = ctx.code;
|
||||
const r = [];
|
||||
let adancime = 0;
|
||||
let s = start;
|
||||
for (let j = start; j < end; j++) {
|
||||
if (ctx.inSir(j)) continue;
|
||||
const ch = c[j];
|
||||
if (ch === '(' || ch === '[' || ch === '{') adancime++;
|
||||
else if (ch === ')' || ch === ']' || ch === '}') adancime--;
|
||||
else if (ch === ',' && adancime === 0) { r.push({ text: c.slice(s, j), start: s }); s = j + 1; }
|
||||
}
|
||||
if (end > s) r.push({ text: c.slice(s, end), start: s });
|
||||
return r.map((a) => {
|
||||
const lead = a.text.length - a.text.trimStart().length;
|
||||
return { text: a.text.trim(), start: a.start + lead };
|
||||
}).filter((a) => a.text.length);
|
||||
}
|
||||
|
||||
// un literal de sir simplu, fara interpolare; intoarce valoarea sau null
|
||||
export function literal(s) {
|
||||
const t = String(s).trim();
|
||||
const m = /^(?:[rRbBuU]{0,2})(['"`])([^'"`\n$\\]*)\1$/.exec(t);
|
||||
return m ? m[2] : null;
|
||||
}
|
||||
|
||||
// identificator simplu (nu proprietate, nu apel)
|
||||
export function identificator(s) {
|
||||
const t = String(s).trim();
|
||||
return /^[A-Za-z_$][\w$]*$/.test(t) ? t : null;
|
||||
}
|
||||
|
||||
// Rezolvarea conservatoare a unei variabile la un literal: EXACT o atribuire in fisier,
|
||||
// sub forma unei declaratii cu literal, si numele nu apare ca parametru de functie.
|
||||
// Orice alta forma: null (gasirea iese "unknown", nu se ghiceste).
|
||||
// Rezultatul depinde numai de fisier si de nume, deci se tine minte; si cel mult REZOLVARI_PE_FISIER nume distincte se rezolva
|
||||
// intr-un fisier (2026-09-29, revizuirea adversariala): fiecare rezolvare citeste tot fisierul, deci un fisier cu mii de apeluri
|
||||
// pe variabile costa patratic. Un nume peste plafon iese "unknown", si fisierul se numara in statistici (nu se sare in tacere).
|
||||
export const REZOLVARI_PE_FISIER = 64;
|
||||
export function rezolva(ctx, nume) {
|
||||
if (!identificator(nume)) return null;
|
||||
if (!ctx.rezolvari) ctx.rezolvari = new Map();
|
||||
if (ctx.rezolvari.has(nume)) return ctx.rezolvari.get(nume);
|
||||
if (ctx.rezolvari.size >= REZOLVARI_PE_FISIER) { ctx.rezolvariPestePlafon = (ctx.rezolvariPestePlafon || 0) + 1; return null; }
|
||||
const r = rezolvaOData(ctx, nume);
|
||||
ctx.rezolvari.set(nume, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
function rezolvaOData(ctx, nume) {
|
||||
const c = ctx.code;
|
||||
const e = escapeRe(nume);
|
||||
const atribuiri = ctx.potriviri(new RegExp(`(?<![\\w$.])${e}\\s*(?:[-+*/%|&^]|\\?\\?|\\|\\||&&)?(?::=|=)(?![=>])`, 'g'));
|
||||
if (atribuiri.length !== 1) return null;
|
||||
const k = atribuiri[0].index;
|
||||
const ls0 = c.lastIndexOf('\n', k) + 1;
|
||||
const le0 = c.indexOf('\n', k) < 0 ? c.length : c.indexOf('\n', k);
|
||||
const linie = c.slice(ls0, le0);
|
||||
let parametru = false;
|
||||
if (ctx.limbaj === 'javascript') {
|
||||
// intervalele fara capat ([^(]*, [^:]*, ...) sunt marginite: pornite de la fiecare cuvant cheie, ar citi pana la capatul
|
||||
// fisierului pe un text fara paranteza (sau doua puncte) inchisa, deci un fisier facut anume ar costa patratic
|
||||
parametru = new RegExp(`(?:function\\b[^(]{0,200}\\(|\\()([^()]{0,2000}(?<![\\w$.])${e}(?![\\w$])[^()]{0,2000})\\)\\s*(?:=>|\\{)`).test(c)
|
||||
|| new RegExp(`\\bfor\\s*\\(\\s*(?:const|let|var)?\\s*${e}\\s+(?:of|in)\\b`).test(c)
|
||||
|| new RegExp(`(?<![\\w$.])${e}\\s*=>`).test(c);
|
||||
const m = new RegExp(`\\b(?:const|let|var)\\s+${e}\\s*=\\s*(['"\`])([^'"\`\\n$\\\\]*)\\1\\s*[;,]?\\s*$`).exec(linie);
|
||||
return !parametru && m ? { valoare: m[2], line: ctx.poz(k).line } : null;
|
||||
}
|
||||
if (ctx.limbaj === 'python') {
|
||||
parametru = new RegExp(`\\bdef\\s+\\w+\\s*\\([^)]{0,2000}(?<![\\w.])${e}\\b`).test(c) || new RegExp(`\\blambda\\b[^:]{0,500}\\b${e}\\b`).test(c)
|
||||
|| new RegExp(`\\bfor\\s+[^:\\n]{0,500}(?<![\\w.])${e}\\b[^:\\n]{0,500}\\bin\\b`).test(c) || new RegExp(`\\bas\\s+${e}\\b`).test(c);
|
||||
const m = new RegExp(`^[ \\t]*${e}\\s*(?::\\s*str\\s*)?=\\s*(['"])([^'"\\n\\\\]*)\\1\\s*$`, 'm').exec(c.slice(c.lastIndexOf('\n', k) + 1));
|
||||
return !parametru && m && m.index === 0 ? { valoare: m[2], line: ctx.poz(k).line } : null;
|
||||
}
|
||||
if (ctx.limbaj === 'java') {
|
||||
parametru = new RegExp(`\\(([^()]{0,2000}\\bString\\s+${e}\\b[^()]{0,2000})\\)`).test(c);
|
||||
const ls = c.lastIndexOf('\n', k) + 1;
|
||||
const le = c.indexOf('\n', k) < 0 ? c.length : c.indexOf('\n', k);
|
||||
const m = new RegExp(`\\bString\\s+${e}\\s*=\\s*"([^"\\n\\\\]*)"\\s*;`).exec(c.slice(ls, le));
|
||||
return !parametru && m ? { valoare: m[1], line: ctx.poz(k).line } : null;
|
||||
}
|
||||
if (ctx.limbaj === 'go') {
|
||||
parametru = new RegExp(`\\bfunc\\b[^{]{0,500}\\([^)]{0,2000}\\b${e}\\s+(?:\\w+\\s*,\\s*)*string\\b`).test(c) || new RegExp(`\\bfunc\\b[^{]{0,500}\\([^)]{0,2000}\\b${e}\\s+string\\b`).test(c);
|
||||
const ls = c.lastIndexOf('\n', k) + 1;
|
||||
const le = c.indexOf('\n', k) < 0 ? c.length : c.indexOf('\n', k);
|
||||
const m = new RegExp(`^\\s*(?:(?:const|var)\\s+)?${e}\\s*(?:string\\s*)?(?::=|=)\\s*"([^"\\n\\\\]*)"\\s*$`).exec(c.slice(ls, le));
|
||||
return !parametru && m ? { valoare: m[1], line: ctx.poz(k).line } : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// valoarea unui argument: literal, variabila rezolvata sau necunoscuta
|
||||
export function valoareArgument(ctx, arg) {
|
||||
if (!arg) return { fel: 'lipsa' };
|
||||
const l = literal(arg.text);
|
||||
if (l !== null) return { fel: 'literal', valoare: l };
|
||||
const id = identificator(arg.text);
|
||||
if (id) {
|
||||
const r = rezolva(ctx, id);
|
||||
if (r) return { fel: 'rezolvat', valoare: r.valoare, din: `${id} (line ${r.line})` };
|
||||
}
|
||||
return { fel: 'necunoscut', expresie: arg.text.length > 60 ? arg.text.slice(0, 57) + '...' : arg.text };
|
||||
}
|
||||
|
||||
export function necunoscut(expr, api) {
|
||||
return { grup: 'UNKNOWN', motiv: `Algorithm for ${api} is given by the expression "${expr}", which this tool does not resolve statically (it resolves only a variable with exactly one literal assignment in this file, for at most ${REZOLVARI_PE_FISIER} distinct variables per file).` };
|
||||
}
|
||||
|
||||
// Obiectul literal care contine pozitia (acolade echilibrate), pentru a citi parametri vecini.
|
||||
export function obiectulDin(ctx, pos) {
|
||||
const c = ctx.code;
|
||||
let adancime = 0;
|
||||
let s = pos;
|
||||
while (s > 0 && pos - s < 2000) {
|
||||
s--;
|
||||
if (ctx.inSir(s)) continue;
|
||||
if (c[s] === '}') adancime++;
|
||||
else if (c[s] === '{') { if (adancime === 0) break; adancime--; }
|
||||
}
|
||||
if (c[s] !== '{') return null;
|
||||
const a = argumente(ctx, s + 1);
|
||||
return { text: c.slice(s, a.end + 1), start: s, end: a.end + 1 };
|
||||
}
|
||||
|
||||
// Suprimarea: o gasire din import cade daca acelasi fisier are o gasire din apel care o acopera.
|
||||
// Pentru hash-uri si MAC se compara numele intreg, pentru cifruri si KDF familia, altfel grupul.
|
||||
function tokeni(g) {
|
||||
if (g.group === 'HASH') return [`HASH:${g.name}`];
|
||||
if (g.group === 'MAC') return g.name.startsWith('HMAC-') ? [`MAC:${g.name}`, `HASH:${g.name.slice(5)}`] : [`MAC:${g.name}`];
|
||||
if (g.group === 'CIPHER' || g.group === 'KDF') return [`${g.group}:${g.name.split('-')[0]}`];
|
||||
return [g.group];
|
||||
}
|
||||
|
||||
export function aplicaSuprimarea(gasiri) {
|
||||
const acoperite = new Set();
|
||||
for (const g of gasiri) if (g.evidenceKind !== 'import') { acoperite.add(g.group); for (const t of tokeni(g)) acoperite.add(t); }
|
||||
const r = gasiri.filter((g) => {
|
||||
if (g.evidenceKind !== 'import' || !g.suppressedBy) return true;
|
||||
const lista = g.suppressedBy === 'AUTO' ? tokeni(g) : g.suppressedBy;
|
||||
return !lista.some((x) => acoperite.has(x));
|
||||
});
|
||||
// dubluri: acelasi rand, acelasi nume, acelasi API
|
||||
const vazut = new Set();
|
||||
return r.filter((g) => {
|
||||
const k = `${g.line}|${g.name}|${g.api}|${g.classification}`;
|
||||
if (vazut.has(k)) return false;
|
||||
vazut.add(k);
|
||||
return true;
|
||||
}).map((g) => { const o = { ...g }; delete o.suppressedBy; return o; });
|
||||
}
|
||||
|
||||
// numele unui cifru in stil OpenSSL (node:crypto): aes-256-gcm, des-ede3-cbc, bf-cbc, rc4, chacha20-poly1305
|
||||
export function cifruOpenssl(s) {
|
||||
const t = String(s).toLowerCase().trim();
|
||||
let m;
|
||||
if ((m = /^(?:id-)?aes-?(128|192|256)(?:-(\w+(?:-\w+)?))?$/.exec(t))) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: m[2] || 'cbc' };
|
||||
if ((m = /^(camellia|aria|sm4)-?(128|192|256)?(?:-(\w+))?$/.exec(t))) return { grup: 'CIPHER', nume: m[1].toUpperCase(), param: m[2], mod: m[3] };
|
||||
if (/^des-ede3(-\w+)?$|^des3$|^des-ede(-\w+)?$/.test(t)) return { grup: 'CIPHER', nume: '3DES', mod: (/-(cbc|ecb|cfb|ofb)$/.exec(t) || [])[1] || (t === 'des-ede3' || t === 'des-ede' ? 'ecb' : 'cbc') };
|
||||
if ((m = /^des(?:-(\w+))?$/.exec(t))) return { grup: 'CIPHER', nume: 'DES', mod: m[1] || 'cbc' };
|
||||
if (/^rc4(-\d+)?$|^rc4-hmac-md5$/.test(t)) return { grup: 'CIPHER', nume: 'RC4' };
|
||||
if (/^rc2(-\w+)?$/.test(t)) return { grup: 'CIPHER', nume: 'RC2' };
|
||||
if ((m = /^(?:bf|blowfish)(?:-(\w+))?$/.exec(t))) return { grup: 'CIPHER', nume: 'Blowfish', mod: m[1] };
|
||||
if ((m = /^(cast5|cast|idea|seed)(?:-(\w+))?$/.exec(t))) return { grup: 'CIPHER', nume: m[1].toUpperCase(), mod: m[2] };
|
||||
if (t === 'chacha20-poly1305') return { grup: 'CIPHER', nume: 'ChaCha20-Poly1305' };
|
||||
if (t === 'chacha20') return { grup: 'CIPHER', nume: 'ChaCha20' };
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
// Detectorul Go: importurile pachetelor crypto/* (in Go un import nefolosit nu compileaza, deci
|
||||
// importul e o dovada de folosire, cu exceptia importului gol "_"), apelurile cu parametri si
|
||||
// configuratia tls.Config.
|
||||
import { argumente, imparteArgumente, escapeRe } from './context.mjs';
|
||||
import { grupTls, jws } from './catalog.mjs';
|
||||
|
||||
const PACHETE = {
|
||||
'crypto/rsa': { grup: 'RSA' },
|
||||
'crypto/dsa': { grup: 'DSA' },
|
||||
'crypto/ecdsa': { grup: 'ECDSA' },
|
||||
'crypto/elliptic': { grup: 'EC' },
|
||||
'crypto/ecdh': { grup: 'ECDH' },
|
||||
'crypto/ed25519': { grup: 'EDDSA', nume: 'Ed25519' },
|
||||
'golang.org/x/crypto/ed25519': { grup: 'EDDSA', nume: 'Ed25519' },
|
||||
'golang.org/x/crypto/curve25519': { grup: 'XDH', nume: 'X25519' },
|
||||
'crypto/md5': { grup: 'HASH', hash: 'MD5' },
|
||||
'crypto/sha1': { grup: 'HASH', hash: 'SHA1' },
|
||||
'golang.org/x/crypto/md4': { grup: 'HASH', hash: 'MD4' },
|
||||
'golang.org/x/crypto/ripemd160': { grup: 'HASH', hash: 'RIPEMD160' },
|
||||
'crypto/sha256': { grup: 'HASH', hash: 'SHA256' },
|
||||
'crypto/sha512': { grup: 'HASH', hash: 'SHA512' },
|
||||
'crypto/sha3': { grup: 'HASH', hash: 'SHA3-256' },
|
||||
'golang.org/x/crypto/sha3': { grup: 'HASH', hash: 'SHA3-256' },
|
||||
'crypto/des': { grup: 'CIPHER', nume: 'DES' },
|
||||
'crypto/rc4': { grup: 'CIPHER', nume: 'RC4' },
|
||||
'golang.org/x/crypto/blowfish': { grup: 'CIPHER', nume: 'Blowfish' },
|
||||
'crypto/aes': { grup: 'CIPHER', nume: 'AES' },
|
||||
'golang.org/x/crypto/chacha20poly1305': { grup: 'CIPHER', nume: 'ChaCha20-Poly1305' },
|
||||
'crypto/mlkem': { grup: 'MLKEM' },
|
||||
'golang.org/x/crypto/bcrypt': { grup: 'KDF', nume: 'bcrypt' },
|
||||
'golang.org/x/crypto/argon2': { grup: 'KDF', nume: 'Argon2' },
|
||||
'golang.org/x/crypto/scrypt': { grup: 'KDF', nume: 'scrypt' },
|
||||
'golang.org/x/crypto/pbkdf2': { grup: 'KDF', nume: 'PBKDF2' },
|
||||
'crypto/pbkdf2': { grup: 'KDF', nume: 'PBKDF2' },
|
||||
'crypto/hkdf': { grup: 'KDF', nume: 'HKDF' },
|
||||
'golang.org/x/crypto/ssh': { grup: 'SSH' },
|
||||
'github.com/ethereum/go-ethereum/crypto': { grup: 'SECP256K1', nota: 'go-ethereum crypto package: accounts and signatures are secp256k1 ECDSA.' },
|
||||
'github.com/ethereum/go-ethereum/crypto/secp256k1': { grup: 'SECP256K1' },
|
||||
'github.com/decred/dcrd/dcrec/secp256k1': { grup: 'SECP256K1' },
|
||||
'github.com/btcsuite/btcd/btcec': { grup: 'SECP256K1' },
|
||||
'github.com/cloudflare/circl/sign/ed25519': { grup: 'EDDSA', nume: 'Ed25519' },
|
||||
'github.com/cloudflare/circl/sign/slhdsa': { grup: 'SLHDSA' },
|
||||
'github.com/cloudflare/circl/kem/hybrid': { grup: 'HYBRID-KEX', nume: 'hybrid KEM (circl)' },
|
||||
};
|
||||
|
||||
function pachetActiv(cale) {
|
||||
if (PACHETE[cale]) return PACHETE[cale];
|
||||
let m;
|
||||
if ((m = /^github\.com\/cloudflare\/circl\/sign\/mldsa\/mldsa(44|65|87)$/.exec(cale))) return { grup: 'MLDSA', param: m[1] };
|
||||
if ((m = /^github\.com\/cloudflare\/circl\/kem\/mlkem\/mlkem(512|768|1024)$/.exec(cale))) return { grup: 'MLKEM', param: m[1] };
|
||||
if ((m = /^github\.com\/cloudflare\/circl\/sign\/dilithium\/mode(2|3|5)$/.exec(cale))) return { grup: 'PREPQ', nume: `Dilithium${m[1]}`, primitiv: 'signature' };
|
||||
if ((m = /^github\.com\/cloudflare\/circl\/kem\/kyber\/kyber(512|768|1024)$/.exec(cale))) return { grup: 'PREPQ', nume: `Kyber${m[1]}`, primitiv: 'kem' };
|
||||
if (/^github\.com\/btcsuite\/btcd\/btcec(\/v\d+)?$/.test(cale) || /^github\.com\/decred\/dcrd\/dcrec\/secp256k1(\/v\d+)?$/.test(cale)) return { grup: 'SECP256K1' };
|
||||
if (/^github\.com\/golang-jwt\/jwt(\/v\d+)?$/.test(cale)) return { grup: 'LIB-MULTI', nume: 'golang-jwt', motiv: 'Imports golang-jwt; no SigningMethod reference recognized in this file.' };
|
||||
if (cale === 'crypto/tls') return { grup: 'TLS', param: 'negotiated', motiv: 'crypto/tls with default settings' };
|
||||
return null;
|
||||
}
|
||||
|
||||
export function importuriGo(ctx) {
|
||||
const r = [];
|
||||
const c = ctx.code;
|
||||
const adauga = (alias, cale, pos) => r.push({ alias: alias || cale.split('/').filter((x) => !/^v\d+$/.test(x)).pop(), gol: alias === '_', cale, pos });
|
||||
for (const m of c.matchAll(/(^|\n)[ \t]*import[ \t]+(?:([\w.]+)[ \t]+)?("([^"\n]+)")/g)) {
|
||||
adauga(m[2], m[4], m.index + m[0].indexOf(m[3]));
|
||||
}
|
||||
for (const m of c.matchAll(/(^|\n)[ \t]*import[ \t]*\(([^)]*)\)/g)) {
|
||||
const bloc = m[2];
|
||||
const baza = m.index + m[0].indexOf('(') + 1;
|
||||
for (const x of bloc.matchAll(/(?:^|\n)[ \t]*(?:([\w.]+)[ \t]+)?("([^"\n]+)")/g)) {
|
||||
adauga(x[1], x[3], baza + x.index + x[0].indexOf(x[2]));
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
export function detecteazaGo(ctx) {
|
||||
const imp = importuriGo(ctx);
|
||||
const alias = (cale) => { const i = imp.find((x) => x.cale === cale && !x.gol); return i ? escapeRe(i.alias) : null; };
|
||||
const apel = (cale, functii) => {
|
||||
const a = alias(cale);
|
||||
if (!a) return [];
|
||||
return ctx.potriviri(new RegExp(`(?<![\\w.])${a}\\s*\\.\\s*(${functii})\\b`, 'g'));
|
||||
};
|
||||
const args = (m) => {
|
||||
const dupa = ctx.code.slice(m.index + m[0].length);
|
||||
const p = /^\s*\(/.exec(dupa);
|
||||
if (!p) return { text: '', parti: [] };
|
||||
const a = argumente(ctx, m.index + m[0].length + p[0].length);
|
||||
return { ...a, parti: imparteArgumente(ctx, a.start, a.end) };
|
||||
};
|
||||
const pachetHash = { md5: 'MD5', sha1: 'SHA1', sha256: 'SHA256', sha512: 'SHA512', sha3: 'SHA3-256' };
|
||||
|
||||
// apelurile cu parametri
|
||||
for (const m of apel('crypto/rsa', 'GenerateKey|GenerateMultiPrimeKey')) {
|
||||
const a = args(m);
|
||||
const ult = a.parti[a.parti.length - 1];
|
||||
ctx.adauga(m.index, `rsa.${m[1]}`, 'call', { grup: 'RSA', param: ult && /^\d+$/.test(ult.text) ? ult.text : undefined, functii: ['keygen'] }, { bucata: m[0] + (ult ? ult.text : '') });
|
||||
}
|
||||
for (const m of apel('crypto/rsa', 'EncryptOAEP|DecryptOAEP|EncryptPKCS1v15|DecryptPKCS1v15|SignPSS|SignPKCS1v15|VerifyPSS|VerifyPKCS1v15')) {
|
||||
const a = args(m);
|
||||
const h = /\bcrypto\s*\.\s*(MD5|SHA1|SHA224|SHA256|SHA384|SHA512)\b/.exec(a.text);
|
||||
const sig = /^(Sign|Verify)/.test(m[1]);
|
||||
ctx.adauga(m.index, `rsa.${m[1]}`, 'call', { grup: 'RSA', primitiv: sig ? 'signature' : 'pke', padding: /OAEP/.test(m[1]) ? 'oaep' : /PKCS1v15/.test(m[1]) ? 'pkcs1v15' : 'other', hash: h ? h[1] : undefined, functii: [/^Sign/.test(m[1]) ? 'sign' : /^Verify/.test(m[1]) ? 'verify' : /^Encrypt/.test(m[1]) ? 'encrypt' : 'decrypt'] }, { bucata: m[0] });
|
||||
}
|
||||
const ell = alias('crypto/elliptic');
|
||||
const consumate = new Set();
|
||||
for (const m of apel('crypto/ecdsa', 'GenerateKey|Sign|SignASN1|Verify|VerifyASN1')) {
|
||||
const a = args(m);
|
||||
let curba;
|
||||
if (ell && m[1] === 'GenerateKey') {
|
||||
const k = new RegExp(`${ell}\\s*\\.\\s*(P224|P256|P384|P521)\\s*\\(`).exec(a.text);
|
||||
if (k) { curba = k[1]; consumate.add(a.start + k.index); }
|
||||
}
|
||||
ctx.adauga(m.index, `ecdsa.${m[1]}`, 'call', { grup: 'ECDSA', curba, functii: [m[1] === 'GenerateKey' ? 'keygen' : /^Sign/.test(m[1]) ? 'sign' : 'verify'] }, { bucata: m[0] + (curba ? ` ${curba}` : '') });
|
||||
}
|
||||
for (const m of apel('crypto/elliptic', 'P224|P256|P384|P521')) {
|
||||
if (consumate.has(m.index)) continue;
|
||||
ctx.adauga(m.index, `elliptic.${m[1]}`, 'call', { grup: 'EC', curba: m[1] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of apel('crypto/ecdh', 'X25519|P256|P384|P521')) {
|
||||
ctx.adauga(m.index, `ecdh.${m[1]}`, 'call', m[1] === 'X25519' ? { grup: 'XDH', nume: 'X25519', functii: ['keygen'] } : { grup: 'ECDH', curba: m[1], functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const cale of ['crypto/ed25519', 'golang.org/x/crypto/ed25519']) {
|
||||
for (const m of apel(cale, 'GenerateKey|Sign|Verify|NewKeyFromSeed|VerifyWithOptions')) {
|
||||
ctx.adauga(m.index, `ed25519.${m[1]}`, 'call', { grup: 'EDDSA', nume: 'Ed25519', functii: [m[1] === 'Sign' ? 'sign' : /^Verify/.test(m[1]) ? 'verify' : 'keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
}
|
||||
for (const [cale, fn] of [['crypto/md5', 'New|Sum'], ['crypto/sha1', 'New|Sum'], ['crypto/sha256', 'New|New224|Sum256|Sum224'], ['crypto/sha512', 'New|New384|Sum512|Sum384|New512_256'], ['crypto/sha3', 'New256|New384|New512|Sum256|Sum384|Sum512|NewSHAKE128|NewSHAKE256'], ['golang.org/x/crypto/sha3', 'New256|New384|New512|Sum256|Sum384|Sum512|NewLegacyKeccak256']]) {
|
||||
for (const m of apel(cale, fn)) {
|
||||
const dupa = ctx.code.slice(m.index + m[0].length, m.index + m[0].length + 3);
|
||||
if (!/^\s*\(/.test(dupa)) continue; // referinta de functie (de ex. hmac.New(sha1.New, ...)) se raporteaza la hmac
|
||||
const pk = cale.split('/').pop();
|
||||
let h = pachetHash[pk];
|
||||
if (/224/.test(m[1])) h = 'SHA224';
|
||||
if (/384/.test(m[1])) h = pk === 'sha3' ? 'SHA3-384' : 'SHA384';
|
||||
if (/512_256/.test(m[1])) h = 'SHA512/256';
|
||||
if (pk === 'sha3' && /512/.test(m[1])) h = 'SHA3-512';
|
||||
if (/SHAKE128/.test(m[1])) h = 'SHAKE128';
|
||||
if (/SHAKE256/.test(m[1])) h = 'SHAKE256';
|
||||
if (/Keccak/.test(m[1])) h = 'KECCAK256';
|
||||
ctx.adauga(m.index, `${pk}.${m[1]}`, 'call', { grup: 'HASH', hash: h, functii: ['digest'] }, { bucata: m[0] });
|
||||
}
|
||||
}
|
||||
for (const m of apel('crypto/hmac', 'New')) {
|
||||
const a = args(m);
|
||||
const k = /^(\w+)\s*\.\s*New(\d*)\b/.exec(a.parti[0] ? a.parti[0].text : '');
|
||||
const pk = k ? imp.find((x) => x.alias === k[1]) : null;
|
||||
const baza = pk ? pachetHash[pk.cale.split('/').pop()] : null;
|
||||
const h = baza ? (k[2] === '384' ? 'SHA384' : k[2] === '224' ? 'SHA224' : baza) : null;
|
||||
ctx.adauga(m.index, 'hmac.New', 'call', h ? { grup: 'MAC', hash: h, functii: ['tag'] } : { grup: 'UNKNOWN', motiv: `hmac.New hash "${a.parti[0] ? a.parti[0].text : '?'}" cannot be resolved statically.` }, { bucata: m[0] + (a.parti[0] ? `(${a.parti[0].text}` : '') });
|
||||
}
|
||||
for (const m of apel('crypto/des', 'NewCipher|NewTripleDESCipher')) {
|
||||
ctx.adauga(m.index, `des.${m[1]}`, 'call', { grup: 'CIPHER', nume: m[1] === 'NewCipher' ? 'DES' : '3DES', functii: ['encrypt'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of apel('crypto/rc4', 'NewCipher')) ctx.adauga(m.index, 'rc4.NewCipher', 'call', { grup: 'CIPHER', nume: 'RC4', functii: ['encrypt'] }, { bucata: m[0] });
|
||||
for (const m of apel('crypto/aes', 'NewCipher')) ctx.adauga(m.index, 'aes.NewCipher', 'call', { grup: 'CIPHER', nume: 'AES', functii: ['encrypt'] }, { bucata: m[0] });
|
||||
for (const m of apel('crypto/mlkem', 'GenerateKey768|GenerateKey1024|NewEncapsulationKey768|NewEncapsulationKey1024|NewDecapsulationKey768|NewDecapsulationKey1024')) {
|
||||
ctx.adauga(m.index, `mlkem.${m[1]}`, 'call', { grup: 'MLKEM', param: /1024/.test(m[1]) ? '1024' : '768', functii: [/^Generate/.test(m[1]) ? 'keygen' : 'other'] }, { bucata: m[0] });
|
||||
}
|
||||
const eth = alias('github.com/ethereum/go-ethereum/crypto');
|
||||
if (eth) {
|
||||
for (const m of ctx.potriviri(new RegExp(`(?<![\\w.])${eth}\\s*\\.\\s*(Sign|GenerateKey|Ecrecover|SigToPub|VerifySignature|HexToECDSA|ToECDSA)\\s*\\(`, 'g'))) {
|
||||
ctx.adauga(m.index, `${eth}.${m[1]}`, 'call', { grup: 'SECP256K1', functii: [m[1] === 'Sign' ? 'sign' : /Verify|recover|SigToPub/i.test(m[1]) ? 'verify' : 'keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
}
|
||||
const jwtI = imp.find((x) => /^github\.com\/golang-jwt\/jwt(\/v\d+)?$/.test(x.cale));
|
||||
if (jwtI) {
|
||||
for (const m of ctx.potriviri(new RegExp(`(?<![\\w.])${escapeRe(jwtI.alias)}\\s*\\.\\s*SigningMethod(RS256|RS384|RS512|PS256|PS384|PS512|ES256|ES384|ES512|HS256|HS384|HS512|EdDSA|None)\\b`, 'g'))) {
|
||||
const alg = m[1] === 'None' ? 'none' : m[1];
|
||||
ctx.adauga(m.index, `jwt.SigningMethod${m[1]}`, 'config', { ...jws(alg), functii: ['sign', 'verify'] }, { bucata: m[0] });
|
||||
}
|
||||
}
|
||||
|
||||
// tls.Config
|
||||
const tlsA = alias('crypto/tls');
|
||||
let tlsGasit = false;
|
||||
if (tlsA) {
|
||||
const vers = { VersionSSL30: 'ssl3', VersionTLS10: '1.0', VersionTLS11: '1.1', VersionTLS12: '1.2', VersionTLS13: '1.3' };
|
||||
for (const m of ctx.potriviri(new RegExp(`(?<![\\w])(MinVersion|MaxVersion)\\s*(?::|=)\\s*${tlsA}\\s*\\.\\s*(VersionSSL30|VersionTLS1[0-3])\\b`, 'g'))) {
|
||||
tlsGasit = true;
|
||||
ctx.adauga(m.index, `tls.Config.${m[1]}`, 'config', { grup: 'TLS', param: vers[m[2]], rol: m[1] === 'MinVersion' ? 'min' : 'max' }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(new RegExp(`(?<![\\w])CurvePreferences\\s*(?::|=)\\s*\\[\\]\\s*${tlsA}\\s*\\.\\s*CurveID\\s*\\{`, 'g'))) {
|
||||
tlsGasit = true;
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
for (const x of a.text.matchAll(new RegExp(`${tlsA}\\s*\\.\\s*(\\w+)`, 'g'))) {
|
||||
const n = x[1].replace(/^Curve(P\d+)$/, '$1');
|
||||
ctx.adauga(a.start + x.index, 'tls.Config.CurvePreferences', 'config', { ...grupTls(n), functii: ['keygen'] }, { bucata: `CurvePreferences ${x[1]}` });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// importurile: dovada de folosire in Go, cazute cand fisierul are un apel al aceluiasi grup
|
||||
for (const i of imp) {
|
||||
const act = pachetActiv(i.cale);
|
||||
if (!act) continue;
|
||||
if (i.cale === 'crypto/tls') {
|
||||
if (tlsGasit) continue;
|
||||
ctx.adauga(i.pos, 'import crypto/tls', 'import', { grup: 'UNKNOWN', motiv: 'crypto/tls with default key-exchange settings: Go 1.24 and later offer X25519MLKEM768 by default when Config.CurvePreferences is unset, earlier versions do not. The Go version is not determined by this scan (see the go directive in go.mod).' }, { bucata: 'import "crypto/tls"' });
|
||||
continue;
|
||||
}
|
||||
const nota = i.gol ? `Blank import of ${i.cale}: registers the implementation; not a direct call.` : act.nota;
|
||||
const supr = act.grup === 'EC' ? ['EC', 'ECDSA', 'ECDH', 'SECP256K1', 'XDH'] : act.grup === 'ECDSA' ? ['ECDSA', 'SECP256K1'] : act.grup === 'LIB-MULTI' ? ['RSA', 'ECDSA', 'EDDSA', 'JWT-HMAC', 'JWT-NONE', 'SECP256K1'] : i.cale === 'crypto/des' ? ['CIPHER:DES', 'CIPHER:3DES'] : 'AUTO';
|
||||
ctx.adauga(i.pos, `import ${i.cale}`, 'import', { ...act, nota }, { suprimaDe: supr, bucata: `import "${i.cale}"` });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
// Detectorul Java: JCA getInstance("..."), SSLContext, protocoale activate, Bouncy Castle.
|
||||
import { argumente, imparteArgumente, valoareArgument, necunoscut } from './context.mjs';
|
||||
import { hashCanonic, grupTls, curbaCanonica } from './catalog.mjs';
|
||||
|
||||
const TLSV = { TLSv1: '1.0', 'TLSv1.1': '1.1', 'TLSv1.2': '1.2', 'TLSv1.3': '1.3', SSLv3: 'ssl3', SSLv2Hello: 'ssl2', SSL: 'negotiated', TLS: 'negotiated', Default: 'negotiated' };
|
||||
|
||||
// transforma numele JCA intr-un activ de catalog, dupa clasa care il cere
|
||||
export function activJca(clasa, val, dupa = '', variabila = null) {
|
||||
const peVar = (metoda, rest) => (variabila ? new RegExp(`(?<![\\w.])${variabila}\\s*\\.\\s*${metoda}\\s*\\(\\s*${rest}`).exec(dupa) : null);
|
||||
const v = String(val).trim();
|
||||
const u = v.toUpperCase();
|
||||
let m;
|
||||
const pq = pqNume(v);
|
||||
if (pq) return pq;
|
||||
if (clasa === 'MessageDigest') return { grup: 'HASH', hash: hashCanonic(v) || v, functii: ['digest'] };
|
||||
if (clasa === 'Mac') {
|
||||
if ((m = /^HMAC-?(\S+)$/i.exec(v))) return { grup: 'MAC', hash: hashCanonic(m[1]) || m[1], functii: ['tag'] };
|
||||
return { grup: 'UNKNOWN', motiv: `Mac "${v}" is not in this tool's catalog.` };
|
||||
}
|
||||
if (clasa === 'SecretKeyFactory') {
|
||||
if ((m = /^PBKDF2WithHmac(\w+)$/i.exec(v))) return { grup: 'KDF', nume: `PBKDF2-HMAC-${m[1].toUpperCase()}`, hash: hashCanonic(m[1]) || m[1], functii: ['keyderive'] };
|
||||
if (/^PBEWith(MD5|SHA1)AndDES/i.test(v)) return { grup: 'CIPHER', nume: 'DES', nota: `${v} derives a DES key with a weak hash.` };
|
||||
if (/^DESede$/i.test(v)) return { grup: 'CIPHER', nume: '3DES' };
|
||||
if (/^DES$/i.test(v)) return { grup: 'CIPHER', nume: 'DES' };
|
||||
return { grup: 'UNKNOWN', motiv: `SecretKeyFactory "${v}" is not in this tool's catalog.` };
|
||||
}
|
||||
if (clasa === 'KeyGenerator') {
|
||||
const init = peVar('init', '(\\d+)');
|
||||
if (u === 'AES') return { grup: 'CIPHER', nume: 'AES', param: init ? init[1] : undefined, functii: ['keygen'] };
|
||||
if ((m = /^HMAC(\w+)$/i.exec(v))) return { grup: 'MAC', hash: hashCanonic(m[1]) || m[1], functii: ['keygen'] };
|
||||
if (u === 'DES' || u === 'DESEDE' || u === 'CHACHA20' || u === 'BLOWFISH' || u === 'RC4' || u === 'ARCFOUR') return { grup: 'CIPHER', nume: u === 'DESEDE' ? '3DES' : v, functii: ['keygen'] };
|
||||
return { grup: 'UNKNOWN', motiv: `KeyGenerator "${v}" is not in this tool's catalog.` };
|
||||
}
|
||||
if (clasa === 'KeyPairGenerator' || clasa === 'KeyFactory' || clasa === 'AlgorithmParameterGenerator') {
|
||||
const init = peVar('initialize', '(\\d+)');
|
||||
const bits = clasa === 'KeyPairGenerator' && init ? init[1] : undefined;
|
||||
const f = clasa === 'KeyPairGenerator' ? ['keygen'] : undefined;
|
||||
if (u === 'RSA') return { grup: 'RSA', param: bits, functii: f };
|
||||
if (u === 'RSASSA-PSS') return { grup: 'RSA', param: bits, primitiv: 'signature', padding: 'other', functii: f };
|
||||
if (u === 'DSA') return { grup: 'DSA', param: bits, functii: f };
|
||||
if (u === 'DH' || u === 'DIFFIEHELLMAN') return { grup: 'DH', param: bits, functii: f };
|
||||
if (u === 'EC' || u === 'ECDSA' || u === 'ECDH') {
|
||||
const spec = peVar('initialize', 'new\\s+ECGenParameterSpec\\s*\\(\\s*"([^"]+)"');
|
||||
return { grup: u === 'ECDH' ? 'ECDH' : 'EC', curba: spec ? spec[1] : undefined, functii: f };
|
||||
}
|
||||
if (u === 'ED25519' || u === 'ED448' || u === 'EDDSA') return { grup: 'EDDSA', nume: u === 'ED448' ? 'Ed448' : 'Ed25519', functii: f };
|
||||
if (u === 'X25519' || u === 'X448' || u === 'XDH') return { grup: 'XDH', nume: u === 'X448' ? 'X448' : 'X25519', functii: f };
|
||||
}
|
||||
if (clasa === 'Signature') {
|
||||
if ((m = /^(\w+?)with(RSA|ECDSA|DSA|PLAIN-ECDSA|CVC-ECDSA|RSAandMGF1)(?:\/(PSS|ISO9796-2|X9\.31))?(?:inP1363Format)?$/i.exec(v))) {
|
||||
const hash = hashCanonic(m[1]) || (m[1].toUpperCase() === 'NONE' ? undefined : m[1]);
|
||||
const fam = m[2].toUpperCase();
|
||||
if (fam.startsWith('RSA')) return { grup: 'RSA', primitiv: 'signature', hash, padding: m[3] || /MGF1/i.test(fam) ? 'other' : 'pkcs1v15', functii: ['sign', 'verify'] };
|
||||
if (fam === 'DSA') return { grup: 'DSA', hash, functii: ['sign', 'verify'] };
|
||||
return { grup: 'ECDSA', hash, functii: ['sign', 'verify'] };
|
||||
}
|
||||
if (u === 'RSASSA-PSS') return { grup: 'RSA', primitiv: 'signature', padding: 'other', functii: ['sign', 'verify'] };
|
||||
if (u === 'ED25519' || u === 'ED448' || u === 'EDDSA') return { grup: 'EDDSA', nume: u === 'ED448' ? 'Ed448' : 'Ed25519', functii: ['sign', 'verify'] };
|
||||
}
|
||||
if (clasa === 'KeyAgreement') {
|
||||
if (u === 'ECDH' || u === 'ECMQV' || u === 'ECCDH') return { grup: 'ECDH', functii: ['keygen'] };
|
||||
if (u === 'DH' || u === 'DIFFIEHELLMAN') return { grup: 'DH', functii: ['keygen'] };
|
||||
if (u === 'X25519' || u === 'X448' || u === 'XDH') return { grup: 'XDH', nume: u === 'X448' ? 'X448' : 'X25519', functii: ['keygen'] };
|
||||
}
|
||||
if (clasa === 'KEM') {
|
||||
if (u === 'DHKEM') return { grup: 'ECDH', nume: 'DHKEM', primitiv: 'kem', functii: ['encapsulate', 'decapsulate'] };
|
||||
}
|
||||
if (clasa === 'Cipher') return cifruJca(v);
|
||||
return { grup: 'UNKNOWN', motiv: `${clasa} "${v}" is not in this tool's catalog.` };
|
||||
}
|
||||
|
||||
function cifruJca(v) {
|
||||
const [alg0, mod0, pad0] = v.split('/').map((x) => (x || '').trim());
|
||||
const alg = alg0.toUpperCase();
|
||||
const mod = mod0 ? mod0.toLowerCase() : undefined;
|
||||
const pad = pad0 ? pad0.toUpperCase() : '';
|
||||
let m;
|
||||
if (alg === 'RSA') {
|
||||
const padding = /OAEP/.test(pad) ? 'oaep' : /PKCS1/.test(pad) ? 'pkcs1v15' : /NOPADDING/.test(pad) ? 'raw' : (pad ? 'other' : 'pkcs1v15');
|
||||
const nota = pad ? undefined : 'Cipher "RSA" without padding means RSA/ECB/PKCS1Padding in the standard JCA providers.';
|
||||
return { grup: 'RSA', primitiv: 'pke', padding, nota, functii: ['encrypt', 'decrypt'] };
|
||||
}
|
||||
if ((m = /^AES(?:_(128|192|256))?$/.exec(alg))) {
|
||||
if (!mod) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: 'ecb', nota: 'Cipher "AES" without mode and padding means AES/ECB/PKCS5Padding in the standard JCA providers.', functii: ['encrypt', 'decrypt'] };
|
||||
return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: mod === 'nopadding' ? undefined : mod, functii: ['encrypt', 'decrypt'] };
|
||||
}
|
||||
if (alg === 'DES') return { grup: 'CIPHER', nume: 'DES', mod, functii: ['encrypt', 'decrypt'] };
|
||||
if (alg === 'DESEDE' || alg === 'TRIPLEDES') return { grup: 'CIPHER', nume: '3DES', mod, functii: ['encrypt', 'decrypt'] };
|
||||
if (alg === 'RC4' || alg === 'ARCFOUR') return { grup: 'CIPHER', nume: 'RC4', functii: ['encrypt', 'decrypt'] };
|
||||
if (alg === 'RC2') return { grup: 'CIPHER', nume: 'RC2', functii: ['encrypt', 'decrypt'] };
|
||||
if (alg === 'BLOWFISH') return { grup: 'CIPHER', nume: 'Blowfish', mod, functii: ['encrypt', 'decrypt'] };
|
||||
if (alg === 'CHACHA20-POLY1305') return { grup: 'CIPHER', nume: 'ChaCha20-Poly1305', functii: ['encrypt', 'decrypt'] };
|
||||
if (alg === 'CHACHA20') return { grup: 'CIPHER', nume: 'ChaCha20', functii: ['encrypt', 'decrypt'] };
|
||||
if (/^PBEWITH(MD5|SHA1)AND(DES|TRIPLEDES|RC2|RC4)/.test(alg)) return { grup: 'CIPHER', nume: /TRIPLEDES/.test(alg) ? '3DES' : /RC2/.test(alg) ? 'RC2' : /RC4/.test(alg) ? 'RC4' : 'DES', nota: `${v} derives the key with a weak hash.` };
|
||||
if (/^ECIES/.test(alg)) return { grup: 'ECDH', nume: 'ECIES', primitiv: 'pke', functii: ['encrypt', 'decrypt'] };
|
||||
return { grup: 'UNKNOWN', motiv: `Cipher transformation "${v}" is not in this tool's catalog.` };
|
||||
}
|
||||
|
||||
// nume post-cuantice in JCA sau Bouncy Castle
|
||||
export function pqNume(v) {
|
||||
const t = String(v).trim();
|
||||
let m;
|
||||
if ((m = /^ML-?KEM(?:-(512|768|1024))?$/i.exec(t))) return { grup: 'MLKEM', param: m[1] };
|
||||
if ((m = /^ML-?DSA(?:-(44|65|87))?$/i.exec(t))) return { grup: 'MLDSA', param: m[1] };
|
||||
if ((m = /^SLH-?DSA(?:-(SHA2|SHAKE)-(128|192|256)([sfSF]))?$/i.exec(t))) return { grup: 'SLHDSA', param: m[1] ? `${m[1].toUpperCase()}-${m[2]}${m[3].toLowerCase()}` : undefined };
|
||||
if ((m = /^(?:Falcon|FN-DSA)(?:-(512|1024))?$/i.exec(t))) return { grup: 'FALCON', param: m[1] };
|
||||
if (/^(Kyber\d*|Dilithium\d*|SPHINCSPlus|SPHINCS\+)$/i.test(t)) return { grup: 'PREPQ', nume: t, primitiv: /kyber/i.test(t) ? 'kem' : 'signature' };
|
||||
if (/^(XMSS|XMSSMT|XMSS\^MT|LMS|HSS)$/i.test(t)) return { grup: 'HASHSIG', nume: t.toUpperCase() };
|
||||
if (/^(Rainbow|SIKE)$/i.test(t)) return { grup: 'BROKENPQ', nume: t, primitiv: /rainbow/i.test(t) ? 'signature' : 'kem' };
|
||||
if (/^HQC(-\d+)?$/i.test(t)) return { grup: 'HQC', nume: t.toUpperCase() };
|
||||
return null;
|
||||
}
|
||||
|
||||
const BC_CLASA = [
|
||||
[/^(ECDSASigner|ECNRSigner|DSTU4145Signer)$/, { grup: 'ECDSA' }],
|
||||
[/^(DSASigner)$/, { grup: 'DSA' }],
|
||||
[/^(RSADigestSigner|PSSSigner|RSAEngine|RSABlindedEngine|OAEPEncoding|PKCS1Encoding|RSAKeyPairGenerator|ISO9796d2Signer)$/, { grup: 'RSA' }],
|
||||
[/^(ECKeyPairGenerator|ECNamedCurveTable|SECNamedCurves|ECDomainParameters)$/, { grup: 'EC' }],
|
||||
[/^(ECDHBasicAgreement|ECDHCBasicAgreement|ECDHUnifiedAgreement)$/, { grup: 'ECDH' }],
|
||||
[/^(DHBasicAgreement|DHAgreement|DHKeyPairGenerator)$/, { grup: 'DH' }],
|
||||
[/^(Ed25519Signer|Ed25519ctxSigner|Ed25519phSigner|Ed25519KeyPairGenerator)$/, { grup: 'EDDSA', nume: 'Ed25519' }],
|
||||
[/^(Ed448Signer|Ed448KeyPairGenerator)$/, { grup: 'EDDSA', nume: 'Ed448' }],
|
||||
[/^(X25519Agreement|X25519KeyPairGenerator)$/, { grup: 'XDH', nume: 'X25519' }],
|
||||
[/^(X448Agreement|X448KeyPairGenerator)$/, { grup: 'XDH', nume: 'X448' }],
|
||||
[/^SecP256K1Curve$/, { grup: 'SECP256K1' }],
|
||||
[/^MD5Digest$/, { grup: 'HASH', hash: 'MD5' }],
|
||||
[/^SHA1Digest$/, { grup: 'HASH', hash: 'SHA1' }],
|
||||
[/^SHA256Digest$/, { grup: 'HASH', hash: 'SHA256' }],
|
||||
[/^SHA512Digest$/, { grup: 'HASH', hash: 'SHA512' }],
|
||||
[/^(DESEngine)$/, { grup: 'CIPHER', nume: 'DES' }],
|
||||
[/^(DESedeEngine)$/, { grup: 'CIPHER', nume: '3DES' }],
|
||||
[/^(RC4Engine)$/, { grup: 'CIPHER', nume: 'RC4' }],
|
||||
[/^(BlowfishEngine)$/, { grup: 'CIPHER', nume: 'Blowfish' }],
|
||||
[/^(AESEngine|AESFastEngine|AESLightEngine)$/, { grup: 'CIPHER', nume: 'AES' }],
|
||||
];
|
||||
|
||||
function bcActiv(cale) {
|
||||
const clasa = cale.split('.').pop();
|
||||
let m;
|
||||
if ((m = /^(MLKEM|MLDSA|SLHDSA|Falcon|Kyber|Dilithium|SPHINCSPlus|XMSS|XMSSMT|LMS|HSS|Rainbow|SIKE|HQC|NTRU|NTRUPrime|BIKE|Frodo|CMCE|Picnic|Saber)/.exec(clasa))) {
|
||||
const fam = m[1];
|
||||
const map = { MLKEM: 'ML-KEM', MLDSA: 'ML-DSA', SLHDSA: 'SLH-DSA', SPHINCSPlus: 'SPHINCSPlus', XMSSMT: 'XMSSMT' };
|
||||
const pq = pqNume(map[fam] || fam);
|
||||
if (pq) return pq;
|
||||
return { grup: 'UNKNOWN', motiv: `Bouncy Castle post-quantum class ${clasa}: scheme "${fam}" is not classified by this tool (not a finalized NIST standard).` };
|
||||
}
|
||||
if (/\.pqc\./.test(cale)) {
|
||||
if (/BouncyCastlePQCProvider$/.test(clasa)) return { grup: 'UNKNOWN', motiv: 'Bouncy Castle PQC provider registered; the scheme is chosen elsewhere (not visible in this import).' };
|
||||
return { grup: 'UNKNOWN', motiv: `Bouncy Castle post-quantum package class ${clasa}; scheme not recognized by this tool.` };
|
||||
}
|
||||
for (const [re, act] of BC_CLASA) if (re.test(clasa)) return act;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Variabila din `v = <aici>` pe acelasi rand, citita INAPOI de la pozitie (ce dadea /([A-Za-z_$][\w$]*)\s*=\s*$/ pe textul randului
|
||||
// de dinainte). Forma veche taia la fiecare apel tot randul pana la pozitie, deci un rand lung cu multe apeluri costa patratic
|
||||
// (2026-09-29, revizuirea adversariala); cititul inapoi se opreste la primul caracter care nu poate face parte din tipar.
|
||||
export function variabilaAtribuita(c, pos) {
|
||||
const sp = (ch) => ch !== '\n' && /\s/.test(ch);
|
||||
let j = pos - 1;
|
||||
while (j >= 0 && sp(c[j])) j--;
|
||||
if (j < 0 || c[j] !== '=') return null;
|
||||
j--;
|
||||
while (j >= 0 && sp(c[j])) j--;
|
||||
const sf = j + 1;
|
||||
while (j >= 0 && /[\w$]/.test(c[j])) j--;
|
||||
let st = j + 1;
|
||||
while (st < sf && /[0-9]/.test(c[st])) st++;
|
||||
return st < sf ? c.slice(st, sf) : null;
|
||||
}
|
||||
|
||||
export function detecteazaJava(ctx) {
|
||||
const c = ctx.code;
|
||||
const consumate = new Set();
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(KeyPairGenerator|Signature|Cipher|KeyAgreement|MessageDigest|Mac|KeyGenerator|KeyFactory|SecretKeyFactory|KEM|SSLContext|AlgorithmParameterGenerator)\s*\.\s*getInstance\s*\(/g)) {
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
const p = imparteArgumente(ctx, a.start, a.end);
|
||||
const api = `${m[1]}.getInstance`;
|
||||
const v = valoareArgument(ctx, p[0]);
|
||||
if (v.fel === 'necunoscut') { ctx.adauga(m.index, api, 'call', necunoscut(v.expresie, api), { bucata: m[0] + v.expresie }); continue; }
|
||||
if (v.fel !== 'literal' && v.fel !== 'rezolvat') continue;
|
||||
const ex = v.fel === 'rezolvat' ? { rezolvatDin: v.din } : {};
|
||||
if (m[1] === 'SSLContext') {
|
||||
const ver = TLSV[v.valoare];
|
||||
ctx.adauga(m.index, api, 'config', ver ? { grup: 'TLS', param: ver, rol: 'only' } : { grup: 'UNKNOWN', motiv: `SSLContext protocol "${v.valoare}" is not in this tool's catalog.` }, { ...ex, bucata: `${m[0]}"${v.valoare}"` });
|
||||
continue;
|
||||
}
|
||||
const dupa = c.slice(a.end, a.end + 2000);
|
||||
const variabila = variabilaAtribuita(c, m.index);
|
||||
if (variabila && m[1] === 'KeyPairGenerator' && /^(EC|ECDSA|ECDH)$/i.test(v.valoare)) {
|
||||
const s = new RegExp(`(?<![\\w.])${variabila}\\s*\\.\\s*initialize\\s*\\(\\s*(new\\s+ECGenParameterSpec\\s*\\(\\s*")`).exec(dupa);
|
||||
if (s) consumate.add(a.end + s.index + s[0].indexOf(s[1]));
|
||||
}
|
||||
ctx.adauga(m.index, api, 'call', activJca(m[1], v.valoare, dupa, variabila), { ...ex, bucata: `${m[0]}"${v.valoare}"` });
|
||||
}
|
||||
// curbe numite in afara unui KeyPairGenerator (ECGenParameterSpec, ECNamedCurveTable)
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(?:new\s+ECGenParameterSpec|ECNamedCurveTable\s*\.\s*getParameterSpec|SECNamedCurves\s*\.\s*getByName|CustomNamedCurves\s*\.\s*getByName)\s*\(\s*"([^"]+)"/g)) {
|
||||
if (consumate.has(m.index)) continue;
|
||||
ctx.adauga(m.index, m[0].replace(/\s*\(.*$/, ''), 'call', { grup: 'EC', curba: m[1] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])NamedParameterSpec\s*\.\s*(X25519|X448|ED25519|ED448|ML_DSA_44|ML_DSA_65|ML_DSA_87|ML_KEM_512|ML_KEM_768|ML_KEM_1024)\b/g)) {
|
||||
const t = m[1].replace(/_/g, '-');
|
||||
const act = pqNume(t) || (/^X/.test(t) ? { grup: 'XDH', nume: t } : { grup: 'EDDSA', nume: t === 'ED448' ? 'Ed448' : 'Ed25519' });
|
||||
ctx.adauga(m.index, `NamedParameterSpec.${m[1]}`, 'call', act, { bucata: m[0] });
|
||||
}
|
||||
// protocoale TLS activate explicit
|
||||
for (const m of ctx.potriviri(/(?<![\w.])set(?:Enabled)?Protocols\s*\(\s*new\s+String\s*\[\s*\]\s*\{/g)) {
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
const vers = [...a.text.matchAll(/"([^"]+)"/g)];
|
||||
const lista = vers.map((x) => TLSV[x[1]]).filter(Boolean);
|
||||
for (const x of vers) {
|
||||
const ver = TLSV[x[1]];
|
||||
if (!ver) continue;
|
||||
const rol = ver === '1.2' && lista.includes('1.3') ? 'min' : 'only';
|
||||
ctx.adauga(a.start + x.index, 'setEnabledProtocols', 'config', { grup: 'TLS', param: ver, rol }, { bucata: `"${x[1]}"` });
|
||||
}
|
||||
}
|
||||
// proprietati de sistem ale JSSE
|
||||
for (const m of ctx.potriviri(/(?<![\w.])System\s*\.\s*setProperty\s*\(\s*"(jdk\.tls\.(?:client|server)\.protocols|jdk\.tls\.namedGroups|https\.protocols)"\s*,\s*"([^"]*)"/g)) {
|
||||
const off = m[0].lastIndexOf('"' + m[2] + '"') + 1;
|
||||
let k = 0;
|
||||
for (const bucata of m[2].split(',')) {
|
||||
const t = bucata.trim();
|
||||
const pos = m.index + off + k + bucata.indexOf(t);
|
||||
k += bucata.length + 1;
|
||||
if (!t) continue;
|
||||
if (/namedGroups/.test(m[1])) ctx.adauga(pos, m[1], 'config', { ...grupTls(t), functii: ['keygen'] }, { bucata: `${m[1]}=${t}` });
|
||||
else if (TLSV[t]) ctx.adauga(pos, m[1], 'config', { grup: 'TLS', param: TLSV[t], rol: 'only' }, { bucata: `${m[1]}=${t}` });
|
||||
}
|
||||
}
|
||||
// Bouncy Castle: importurile si parametrii post-cuantici
|
||||
for (const m of ctx.potriviri(/^[ \t]*import\s+(?:static\s+)?(org\.bouncycastle\.[\w.]+)\s*;/gm)) {
|
||||
const act = bcActiv(m[1]);
|
||||
if (act) ctx.adauga(m.index + m[0].indexOf('import'), `import ${m[1]}`, 'import', act, { suprimaDe: act.grup === 'EC' ? ['EC', 'ECDSA', 'ECDH', 'SECP256K1'] : 'AUTO', bucata: `import ${m[1]}` });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(MLKEM|MLDSA|SLHDSA|Falcon|Kyber|Dilithium|SPHINCSPlus)Parameter(?:s|Spec)\s*\.\s*(\w+)/g)) {
|
||||
const act = bcParam(m[1], m[2]);
|
||||
if (act) ctx.adauga(m.index, `${m[1]}Parameters.${m[2]}`, 'call', act, { bucata: m[0] });
|
||||
}
|
||||
void curbaCanonica;
|
||||
}
|
||||
|
||||
function bcParam(fam, p) {
|
||||
let m;
|
||||
if (fam === 'MLKEM' && (m = /(512|768|1024)/.exec(p))) return { grup: 'MLKEM', param: m[1] };
|
||||
if (fam === 'MLDSA' && (m = /(44|65|87)/.exec(p))) return { grup: 'MLDSA', param: m[1] };
|
||||
if (fam === 'SLHDSA' && (m = /(sha2|shake)_(128|192|256)([sf])/i.exec(p))) return { grup: 'SLHDSA', param: `${m[1].toUpperCase()}-${m[2]}${m[3].toLowerCase()}` };
|
||||
if (fam === 'Falcon' && (m = /(512|1024)/.exec(p))) return { grup: 'FALCON', param: m[1] };
|
||||
if (fam === 'Kyber' || fam === 'Dilithium' || fam === 'SPHINCSPlus') return { grup: 'PREPQ', nume: `${fam} ${p}`, primitiv: fam === 'Kyber' ? 'kem' : 'signature' };
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
// Detectorul JavaScript / TypeScript: node:crypto, WebCrypto, optiuni TLS, biblioteci din importuri.
|
||||
import { argumente, imparteArgumente, valoareArgument, necunoscut, obiectulDin, cifruOpenssl, identificator, rezolva } from './context.mjs';
|
||||
import { hashCanonic, grupTls, jws, JWS_RE } from './catalog.mjs';
|
||||
|
||||
const MODP = { modp1: 768, modp2: 1024, modp5: 1536, modp14: 2048, modp15: 3072, modp16: 4096, modp17: 6144, modp18: 8192 };
|
||||
const TLSV = { TLSv1: '1.0', 'TLSv1.0': '1.0', 'TLSv1.1': '1.1', 'TLSv1.2': '1.2', 'TLSv1.3': '1.3', SSLv3: 'ssl3' };
|
||||
|
||||
export function importuriJs(ctx) {
|
||||
const r = [];
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])import\s+((?:type\s+)?(?:[\w$*{},\s]|\bas\b)+?)\s*from\s*(['"])([^'"\n]+)\2/g)) {
|
||||
r.push(descrieImport(m[3], m.index, m[1]));
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])import\s*(['"])([^'"\n]+)\1/g)) r.push(descrieImport(m[2], m.index, ''));
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])(?:(?:const|let|var)\s+([\w$]+|\{[^}]*\})\s*=\s*(?:await\s+)?)?(?:require|import)\s*\(\s*(['"])([^'"\n]+)\2\s*\)(?:\s*\.\s*([\w$]+))?/g)) {
|
||||
const leg = m[1] || '';
|
||||
r.push(descrieImport(m[3], m.index, leg.startsWith('{') ? leg : (leg ? leg : ''), !leg.startsWith('{') && leg ? leg : null, m[4]));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
// un import printr-o cale relativa in node_modules (../x/node_modules/@noble/curves/secp256k1.js)
|
||||
// e acelasi pachet: se pastreaza ce urmeaza dupa ultimul node_modules/, fara extensie
|
||||
export function normalizeazaSpec(spec) {
|
||||
let s = String(spec);
|
||||
const k = s.lastIndexOf('node_modules/');
|
||||
if (k >= 0) s = s.slice(k + 'node_modules/'.length);
|
||||
if (/^(@[\w.-]+\/)?[\w.-]+\/.+\.(m?js|cjs)$/.test(s) && k >= 0) s = s.replace(/\.(m?js|cjs)$/, '');
|
||||
else if (/^@noble\//.test(s)) s = s.replace(/\.(m?js|cjs)$/, '');
|
||||
return s;
|
||||
}
|
||||
|
||||
function descrieImport(spec0, pos, clauza, implicitDinRequire = null, proprietate = null) {
|
||||
const spec = normalizeazaSpec(spec0);
|
||||
const importate = [];
|
||||
const locale = [];
|
||||
let implicit = implicitDinRequire;
|
||||
const acolade = /\{([^}]*)\}/.exec(clauza || '');
|
||||
if (acolade) {
|
||||
for (const bucata of acolade[1].split(',')) {
|
||||
const t = bucata.trim().replace(/^type\s+/, '');
|
||||
if (!t) continue;
|
||||
const m = /^([\w$]+)(?:\s*(?:as|:)\s*([\w$]+))?$/.exec(t);
|
||||
if (m) { importate.push(m[1]); locale.push(m[2] || m[1]); }
|
||||
}
|
||||
}
|
||||
const rest = (clauza || '').replace(/\{[^}]*\}/, '').replace(/^type\s+/, '');
|
||||
const ns = /\*\s*as\s+([\w$]+)/.exec(rest);
|
||||
if (ns) implicit = ns[1];
|
||||
const def = /^\s*([\w$]+)\s*(?:,|$)/.exec(rest);
|
||||
if (!implicit && def && def[1] !== 'type') implicit = def[1];
|
||||
if (proprietate) { importate.push(proprietate); if (implicit) locale.push(implicit); }
|
||||
return { spec, pos, importate, locale, implicit };
|
||||
}
|
||||
|
||||
export function detecteazaJs(ctx) {
|
||||
const c = ctx.code;
|
||||
const imp = importuriJs(ctx);
|
||||
const importa = (re) => imp.filter((i) => re.test(i.spec));
|
||||
const cryptoImp = importa(/^(node:)?crypto$/);
|
||||
const aliasCrypto = new Set(['crypto']);
|
||||
for (const i of cryptoImp) { if (i.implicit) aliasCrypto.add(i.implicit); }
|
||||
const numeImportate = new Set(cryptoImp.flatMap((i) => i.locale));
|
||||
const nodeCrypto = cryptoImp.length > 0 || ctx.potriviri(/(?<![\w$.])crypto\s*\.\s*(createHash|createHmac|createCipheriv|createDecipheriv|createSign|createVerify|createECDH|generateKeyPairSync|generateKeyPair|generateKeySync|publicEncrypt|privateDecrypt|getDiffieHellman|createDiffieHellman|pbkdf2Sync|pbkdf2|hkdfSync)\s*\(/g).length > 0;
|
||||
|
||||
const apeluri = (nume) => ctx.potriviri(new RegExp(`(?<![\\w$.])(?:([\\w$]+)\\s*\\.\\s*)?(${nume})\\s*\\(`, 'g'))
|
||||
.filter((m) => m[1] !== 'subtle' && (m[1] ? aliasCrypto.has(m[1]) || nodeCrypto : (numeImportate.has(m[2]) || nodeCrypto)));
|
||||
|
||||
const argsDe = (m) => {
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
return imparteArgumente(ctx, a.start, a.end);
|
||||
};
|
||||
const cuValoare = (m, arg, api, fn) => {
|
||||
const v = valoareArgument(ctx, arg);
|
||||
if (v.fel === 'literal' || v.fel === 'rezolvat') return fn(v.valoare, v.fel === 'rezolvat' ? { rezolvatDin: v.din } : {});
|
||||
if (v.fel === 'necunoscut') ctx.adauga(m.index, api, 'call', necunoscut(v.expresie, api), { bucata: m[0] + v.expresie });
|
||||
return null;
|
||||
};
|
||||
|
||||
if (nodeCrypto) {
|
||||
for (const m of apeluri('createHash|createHmac')) {
|
||||
const api = m[2];
|
||||
const p = argsDe(m);
|
||||
cuValoare(m, p[0], api, (val, ex) => {
|
||||
const h = hashCanonic(val) || val;
|
||||
if (api === 'createHash') ctx.adauga(m.index, api, 'call', { grup: 'HASH', hash: h, functii: ['digest'] }, { ...ex, bucata: `${m[0]}'${val}'` });
|
||||
else ctx.adauga(m.index, api, 'call', { grup: 'MAC', hash: h, functii: ['tag'] }, { ...ex, bucata: `${m[0]}'${val}'` });
|
||||
});
|
||||
}
|
||||
for (const m of apeluri('createCipheriv|createDecipheriv|createCipher|createDecipher')) {
|
||||
const api = m[2];
|
||||
const p = argsDe(m);
|
||||
cuValoare(m, p[0], api, (val, ex) => {
|
||||
const cf = cifruOpenssl(val) || { grup: 'CIPHER', nume: val };
|
||||
ctx.adauga(m.index, api, 'call', { ...cf, functii: [/Decipher/.test(api) ? 'decrypt' : 'encrypt'] }, { ...ex, bucata: `${m[0]}'${val}'` });
|
||||
});
|
||||
}
|
||||
for (const m of apeluri('createSign|createVerify')) {
|
||||
const api = m[2];
|
||||
const p = argsDe(m);
|
||||
cuValoare(m, p[0], api, (val, ex) => {
|
||||
const f = [api === 'createSign' ? 'sign' : 'verify'];
|
||||
const h = hashCanonic(val.replace(/^(RSA-|ecdsa-with-|DSA-|RSA-PSS-)/i, '').replace(/with(RSA|DSA)Encryption$/i, '').replace(/WithRSAEncryption$/i, ''));
|
||||
let a;
|
||||
if (/rsa/i.test(val)) a = { grup: 'RSA', primitiv: 'signature', hash: h };
|
||||
else if (/ecdsa/i.test(val)) a = { grup: 'ECDSA', hash: h };
|
||||
else if (/dsa/i.test(val)) a = { grup: 'DSA', hash: h };
|
||||
else a = { grup: 'CLASSIC-SIG', hash: h, motiv: `${api} with digest ${val} (Sign/Verify objects take RSA, RSA-PSS, DSA or EC keys; the key type comes from the key object)` };
|
||||
ctx.adauga(m.index, api, 'call', { ...a, functii: f }, { ...ex, bucata: `${m[0]}'${val}'` });
|
||||
});
|
||||
}
|
||||
for (const m of apeluri('createECDH')) {
|
||||
const p = argsDe(m);
|
||||
cuValoare(m, p[0], 'createECDH', (val, ex) => ctx.adauga(m.index, 'createECDH', 'call', { grup: 'ECDH', curba: val, functii: ['keygen'] }, { ...ex, bucata: `${m[0]}'${val}'` }));
|
||||
}
|
||||
for (const m of apeluri('getDiffieHellman|createDiffieHellmanGroup')) {
|
||||
const p = argsDe(m);
|
||||
cuValoare(m, p[0], m[2], (val, ex) => ctx.adauga(m.index, m[2], 'call', { grup: 'DH', param: MODP[val] ? String(MODP[val]) : val, functii: ['keygen'] }, { ...ex, bucata: `${m[0]}'${val}'` }));
|
||||
}
|
||||
for (const m of apeluri('createDiffieHellman')) {
|
||||
const p = argsDe(m);
|
||||
const bits = p[0] && /^\d+$/.test(p[0].text) ? p[0].text : undefined;
|
||||
ctx.adauga(m.index, 'createDiffieHellman', 'call', { grup: 'DH', param: bits, functii: ['keygen'] }, { bucata: m[0] + (bits || '') });
|
||||
}
|
||||
for (const m of apeluri('generateKeySync|generateKey')) {
|
||||
const p = argsDe(m);
|
||||
const v = valoareArgument(ctx, p[0]);
|
||||
if ((v.fel === 'literal' || v.fel === 'rezolvat') && v.valoare.toLowerCase() === 'aes') {
|
||||
const len = p[1] && /length\s*:\s*(\d+)/.exec(p[1].text);
|
||||
ctx.adauga(m.index, m[2], 'call', { grup: 'CIPHER', nume: 'AES', param: len ? len[1] : undefined, functii: ['keygen'] }, { bucata: `${m[0]}'aes'` });
|
||||
}
|
||||
}
|
||||
for (const m of apeluri('publicEncrypt|privateDecrypt|privateEncrypt|publicDecrypt')) {
|
||||
const api = m[2];
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
const t = a.text;
|
||||
const enc = api === 'publicEncrypt' || api === 'privateDecrypt';
|
||||
let padding = enc ? 'oaep' : 'pkcs1v15';
|
||||
if (/RSA_PKCS1_OAEP_PADDING|oaepHash/.test(t)) padding = 'oaep';
|
||||
else if (/RSA_PKCS1_PADDING/.test(t)) padding = 'pkcs1v15';
|
||||
else if (/RSA_NO_PADDING/.test(t)) padding = 'raw';
|
||||
ctx.adauga(m.index, api, 'call', { grup: 'RSA', primitiv: enc ? 'pke' : 'signature', padding, functii: [api === 'publicEncrypt' || api === 'privateEncrypt' ? 'encrypt' : 'decrypt'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of apeluri('pbkdf2Sync|pbkdf2')) {
|
||||
const p = argsDe(m);
|
||||
cuValoare(m, p[4], m[2], (val, ex) => {
|
||||
const h = hashCanonic(val) || val;
|
||||
ctx.adauga(m.index, m[2], 'call', { grup: 'KDF', nume: `PBKDF2-HMAC-${val.toUpperCase()}`, hash: h, functii: ['keyderive'] }, { ...ex, bucata: m[0] });
|
||||
});
|
||||
}
|
||||
for (const m of apeluri('hkdfSync|hkdf')) {
|
||||
const p = argsDe(m);
|
||||
cuValoare(m, p[0], m[2], (val, ex) => ctx.adauga(m.index, m[2], 'call', { grup: 'KDF', nume: `HKDF-${val.toUpperCase()}`, hash: hashCanonic(val) || val, functii: ['keyderive'] }, { ...ex, bucata: m[0] }));
|
||||
}
|
||||
// chei si certificate incarcate la rulare: algoritmul vine din material, nu din sursa
|
||||
for (const m of [...apeluri('createPrivateKey|createPublicKey'), ...ctx.potriviri(/(?<![\w$.])new\s+(?:([\w$]+)\s*\.\s*)?(X509Certificate)\s*\(/g).filter((x) => !x[1] || aliasCrypto.has(x[1]))]) {
|
||||
const api = m[2];
|
||||
ctx.adauga(m.index, api, 'call', { grup: 'UNKNOWN', motiv: `${api}: key material loaded at run time; its algorithm (RSA, EC, Ed25519, ML-DSA, ...) comes from the key or certificate, which is not in the scanned source. This is a place where keys enter the program: check which algorithm is deployed there.`, functii: ['other'] }, { bucata: m[0] });
|
||||
}
|
||||
// crypto.sign / crypto.verify (si importurile numite sign/verify)
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])(?:([\w$]+)\s*\.\s*)?(sign|verify)\s*\(/g)) {
|
||||
if (m[1] ? !aliasCrypto.has(m[1]) : !numeImportate.has(m[2])) continue;
|
||||
const p = argsDe(m);
|
||||
const api = `crypto.${m[2]}`;
|
||||
if (!p[0] || /^(null|undefined)$/.test(p[0].text)) {
|
||||
ctx.adauga(m.index, api, 'call', { grup: 'UNKNOWN', motiv: `${api} with a null algorithm: the algorithm is determined by the key object (for example Ed25519, Ed448, ML-DSA, RSA or ECDSA), which is not visible statically.`, functii: [m[2]] }, { bucata: `${m[0]}null` });
|
||||
continue;
|
||||
}
|
||||
cuValoare(m, p[0], api, (val, ex) => ctx.adauga(m.index, api, 'call', { grup: 'CLASSIC-SIG', hash: hashCanonic(val) || val, motiv: `${api} with digest ${val} (a digest name is used with RSA, RSA-PSS, DSA and EC keys; EdDSA and ML-DSA keys take null)`, functii: [m[2]] }, { ...ex, bucata: `${m[0]}'${val}'` }));
|
||||
}
|
||||
}
|
||||
|
||||
// generateKeyPair: node:crypto sau jose (acolo primul argument e un algoritm JWS)
|
||||
const jwtLib = importa(/^(jsonwebtoken|jose|jwt-simple|express-jwt|@fastify\/jwt|passport-jwt|koa-jwt|fast-jwt|jws)$/);
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])(?:([\w$]+)\s*\.\s*)?(generateKeyPairSync|generateKeyPair)\s*\(/g)) {
|
||||
if (!nodeCrypto && !jwtLib.length) continue;
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
const p = imparteArgumente(ctx, a.start, a.end);
|
||||
const opt = p[1] ? p[1].text : '';
|
||||
cuValoare(m, p[0], m[2], (val, ex) => {
|
||||
if (JWS_RE.test(val) && jwtLib.length) return; // se raporteaza de regula JWT
|
||||
const t = val.toLowerCase();
|
||||
const ml = /modulusLength\s*:\s*(\d+)/.exec(opt);
|
||||
const nc = /namedCurve\s*:\s*(['"])([^'"]+)\1/.exec(opt);
|
||||
let act;
|
||||
if (t === 'rsa') act = { grup: 'RSA', param: ml && ml[1] };
|
||||
else if (t === 'rsa-pss') act = { grup: 'RSA', param: ml && ml[1], primitiv: 'signature', padding: 'other' };
|
||||
else if (t === 'dsa') act = { grup: 'DSA', param: ml && ml[1] };
|
||||
else if (t === 'ec') act = { grup: 'EC', curba: nc && nc[2] };
|
||||
else if (t === 'ed25519' || t === 'ed448') act = { grup: 'EDDSA', nume: t === 'ed25519' ? 'Ed25519' : 'Ed448' };
|
||||
else if (t === 'x25519' || t === 'x448') act = { grup: 'XDH', nume: t === 'x25519' ? 'X25519' : 'X448' };
|
||||
else if (t === 'dh') {
|
||||
const pl = /primeLength\s*:\s*(\d+)/.exec(opt);
|
||||
const gr = /group\s*:\s*(['"])(modp\d+)\1/.exec(opt);
|
||||
act = { grup: 'DH', param: pl ? pl[1] : gr ? String(MODP[gr[2]] || gr[2]) : undefined };
|
||||
} else if (/^ml-dsa-(44|65|87)$/.test(t)) act = { grup: 'MLDSA', param: t.slice(7) };
|
||||
else if (/^ml-kem-(512|768|1024)$/.test(t)) act = { grup: 'MLKEM', param: t.slice(7) };
|
||||
else if (/^slh-dsa-(sha2|shake)-(128|192|256)[sf]$/.test(t)) act = { grup: 'SLHDSA', param: t.slice(8).toUpperCase().replace(/([SF])$/, (x) => x.toLowerCase()) };
|
||||
else act = { grup: 'UNKNOWN', motiv: `Key type "${val}" is not in this tool's catalog.` };
|
||||
ctx.adauga(m.index, m[2], 'call', { ...act, functii: ['keygen'] }, { ...ex, bucata: `${m[0]}'${val}'` });
|
||||
});
|
||||
}
|
||||
|
||||
detecteazaTlsJs(ctx);
|
||||
detecteazaWebCrypto(ctx);
|
||||
detecteazaBiblioteci(ctx, imp, importa, jwtLib);
|
||||
}
|
||||
|
||||
function detecteazaTlsJs(ctx) {
|
||||
for (const m of ctx.potriviri(/(?<![\w$])(minVersion|maxVersion|DEFAULT_MIN_VERSION|DEFAULT_MAX_VERSION)\s*[:=]\s*(['"])(TLSv1(?:\.[0-3])?|SSLv3)\2/g)) {
|
||||
const rol = /min/i.test(m[1]) ? 'min' : 'max';
|
||||
ctx.adauga(m.index, `tls ${m[1]}`, 'config', { grup: 'TLS', param: TLSV[m[3]], rol }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$])secureProtocol\s*:\s*(['"])(\w+)\1/g)) {
|
||||
const map = { SSLv2_method: 'ssl2', SSLv3_method: 'ssl3', TLSv1_method: '1.0', TLSv1_1_method: '1.1', TLSv1_2_method: '1.2', TLS_method: 'negotiated', SSLv23_method: 'negotiated' };
|
||||
const v = map[m[2].replace(/_(client|server)_method$/, '_method')];
|
||||
if (v) ctx.adauga(m.index, 'tls secureProtocol', 'config', { grup: 'TLS', param: v, rol: 'only' }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$])ecdhCurve\s*:\s*/g)) {
|
||||
const baza = m.index + m[0].length;
|
||||
const expr = expresiePanaLaVirgula(ctx, baza);
|
||||
const lit = /^(['"])([^'"\n]*)\1$/.exec(expr);
|
||||
if (lit) {
|
||||
// fiecare grup la coloana lui din sir
|
||||
let off = 1;
|
||||
for (const g of lit[2].split(/[:,/]/)) {
|
||||
const t = g.trim().replace(/^[*?]+/, '');
|
||||
if (t && t !== 'DEFAULT') ctx.adauga(baza + off + g.indexOf(t), 'tls ecdhCurve', 'config', { ...grupTls(t), functii: ['keygen'] }, { bucata: `ecdhCurve: ${t}` });
|
||||
off += g.length + 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const v = valoareExpresie(ctx, expr);
|
||||
if (!v) {
|
||||
ctx.adauga(m.index, 'tls ecdhCurve', 'config', necunoscut(expr || '?', 'tls ecdhCurve'), { bucata: m[0] + expr });
|
||||
continue;
|
||||
}
|
||||
for (const g of v.valoare.split(/[:,/]/)) {
|
||||
const t = g.trim().replace(/^[*?]+/, '');
|
||||
if (t && t !== 'DEFAULT') ctx.adauga(m.index, 'tls ecdhCurve', 'config', { ...grupTls(t), functii: ['keygen'] }, { bucata: `ecdhCurve: ${t}`, rezolvatDin: v.din });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// textul unei expresii pana la virgula, acolada sau paranteza de nivel zero (fara siruri), max 80 caractere
|
||||
function expresiePanaLaVirgula(ctx, start) {
|
||||
const c = ctx.code;
|
||||
let adancime = 0;
|
||||
let j = start;
|
||||
while (j < c.length && j - start < 400) {
|
||||
if (!ctx.inSir(j)) {
|
||||
const ch = c[j];
|
||||
if (ch === '(' || ch === '[' || ch === '{') adancime++;
|
||||
else if (ch === ')' || ch === ']' || ch === '}') { if (adancime === 0) break; adancime--; }
|
||||
else if ((ch === ',' || ch === '\n' || ch === ';') && adancime === 0) break;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
const t = c.slice(start, j).trim();
|
||||
return t.length > 80 ? t.slice(0, 77) + '...' : t;
|
||||
}
|
||||
|
||||
// valoarea unei expresii: identificator rezolvat, sau template ale carui ${ID} se rezolva toate; altfel null
|
||||
function valoareExpresie(ctx, expr) {
|
||||
const id = identificator(expr);
|
||||
if (id) {
|
||||
const r = rezolva(ctx, id);
|
||||
return r ? { valoare: r.valoare, din: `${id} (line ${r.line})` } : null;
|
||||
}
|
||||
const t = /^`((?:[^`$\\]|\$\{\s*[A-Za-z_$][\w$]*\s*\})*)`$/.exec(expr);
|
||||
if (!t) return null;
|
||||
const din = [];
|
||||
let ok = true;
|
||||
const val = t[1].replace(/\$\{\s*([A-Za-z_$][\w$]*)\s*\}/g, (_, n) => {
|
||||
const r = rezolva(ctx, n);
|
||||
if (!r) { ok = false; return ''; }
|
||||
din.push(`${n} (line ${r.line})`);
|
||||
return r.valoare;
|
||||
});
|
||||
return ok ? { valoare: val, din: din.join(', ') } : null;
|
||||
}
|
||||
|
||||
const WEBCRYPTO_ALG = /^(RSA-OAEP|RSASSA-PKCS1-v1_5|RSA-PSS|ECDSA|ECDH|Ed25519|Ed448|X25519|X448|AES-GCM|AES-CBC|AES-CTR|AES-KW|HMAC|HKDF|PBKDF2|ML-KEM-(?:512|768|1024)|ML-DSA-(?:44|65|87)|ChaCha20-Poly1305)$/i;
|
||||
|
||||
function activWebCrypto(nume, fereastra) {
|
||||
const n = nume.toUpperCase();
|
||||
const ml = /modulusLength\s*:\s*(\d+)/.exec(fereastra);
|
||||
const nc = /namedCurve\s*:\s*(['"])([^'"]+)\1/.exec(fereastra);
|
||||
const len = /(?<![\w$])length\s*:\s*(\d+)/.exec(fereastra);
|
||||
const hm = /hash\s*:\s*(?:\{\s*name\s*:\s*)?(['"])([^'"]+)\1/.exec(fereastra);
|
||||
const hash = hm ? hashCanonic(hm[2]) || hm[2] : undefined;
|
||||
if (n === 'RSA-OAEP') return { grup: 'RSA', primitiv: 'pke', padding: 'oaep', param: ml && ml[1] };
|
||||
if (n === 'RSASSA-PKCS1-V1_5') return { grup: 'RSA', primitiv: 'signature', padding: 'pkcs1v15', param: ml && ml[1], hash };
|
||||
if (n === 'RSA-PSS') return { grup: 'RSA', primitiv: 'signature', padding: 'other', param: ml && ml[1], hash };
|
||||
if (n === 'ECDSA') return { grup: 'ECDSA', curba: nc && nc[2], hash };
|
||||
if (n === 'ECDH') return { grup: 'ECDH', curba: nc && nc[2] };
|
||||
if (n === 'ED25519' || n === 'ED448') return { grup: 'EDDSA', nume: n === 'ED25519' ? 'Ed25519' : 'Ed448' };
|
||||
if (n === 'X25519' || n === 'X448') return { grup: 'XDH', nume: n === 'X25519' ? 'X25519' : 'X448' };
|
||||
if (/^AES-/.test(n)) return { grup: 'CIPHER', nume: 'AES', param: len && len[1], mod: n.slice(4).toLowerCase() };
|
||||
if (n === 'HMAC') return { grup: 'MAC', hash };
|
||||
if (n === 'HKDF') return { grup: 'KDF', nume: 'HKDF', hash };
|
||||
if (n === 'PBKDF2') return { grup: 'KDF', nume: 'PBKDF2', hash };
|
||||
if (/^ML-KEM-/.test(n)) return { grup: 'MLKEM', param: n.slice(7) };
|
||||
if (/^ML-DSA-/.test(n)) return { grup: 'MLDSA', param: n.slice(7) };
|
||||
if (n === 'CHACHA20-POLY1305') return { grup: 'CIPHER', nume: 'ChaCha20-Poly1305' };
|
||||
return null;
|
||||
}
|
||||
|
||||
function detecteazaWebCrypto(ctx) {
|
||||
if (!ctx.potriviri(/(?<![\w$])subtle\b/g).length) return;
|
||||
for (const m of ctx.potriviri(/(?<![\w$])name\s*:\s*(['"])([\w-]+)\1/g)) {
|
||||
if (!WEBCRYPTO_ALG.test(m[2])) continue;
|
||||
const ob = obiectulDin(ctx, m.index);
|
||||
const act = activWebCrypto(m[2], ob ? ob.text : '');
|
||||
if (act) ctx.adauga(m.index, 'WebCrypto', 'call', act, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$])subtle\s*\.\s*(generateKey|importKey|sign|verify|encrypt|decrypt|deriveBits|deriveKey|digest|encapsulateKey|encapsulateBits|decapsulateKey|decapsulateBits)\s*\(/g)) {
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
const p = imparteArgumente(ctx, a.start, a.end);
|
||||
const idx = m[1] === 'importKey' ? 2 : 0;
|
||||
const arg = p[idx];
|
||||
if (!arg) continue;
|
||||
const v = valoareArgument(ctx, arg);
|
||||
if (m[1] === 'digest') {
|
||||
if (v.fel === 'literal' || v.fel === 'rezolvat') ctx.adauga(m.index, 'subtle.digest', 'call', { grup: 'HASH', hash: hashCanonic(v.valoare) || v.valoare, functii: ['digest'] }, { bucata: `${m[0]}'${v.valoare}'`, rezolvatDin: v.din });
|
||||
else {
|
||||
const ob = /name\s*:\s*(['"])([^'"]+)\1/.exec(arg.text);
|
||||
if (ob) ctx.adauga(m.index, 'subtle.digest', 'call', { grup: 'HASH', hash: hashCanonic(ob[2]) || ob[2], functii: ['digest'] }, { bucata: `${m[0]}{name:'${ob[2]}'}` });
|
||||
else if (v.fel === 'necunoscut') ctx.adauga(m.index, 'subtle.digest', 'call', necunoscut(v.expresie, 'subtle.digest'), { bucata: m[0] });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ((v.fel === 'literal' || v.fel === 'rezolvat') && WEBCRYPTO_ALG.test(v.valoare)) {
|
||||
const act = activWebCrypto(v.valoare, '');
|
||||
if (act) ctx.adauga(arg.start, `subtle.${m[1]}`, 'call', act, { bucata: `subtle.${m[1]}('${v.valoare}')`, rezolvatDin: v.din });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ETH = /^(ethers|web3|viem|viem\/accounts|@ethersproject\/[\w-]+|ethereumjs-[\w-]+|@ethereumjs\/[\w-]+|ethereum-cryptography(\/.*)?|web3-eth-accounts)$/;
|
||||
const SECP_LIB = /^(secp256k1|tiny-secp256k1|@noble\/secp256k1|@bitcoinerlab\/secp256k1|ecpair|bitcoinjs-lib|eccrypto)$/;
|
||||
|
||||
function detecteazaBiblioteci(ctx, imp, importa, jwtLib) {
|
||||
const c = ctx.code;
|
||||
const primul = (lista) => lista[0];
|
||||
|
||||
// Ethereum: conturile si semnaturile sunt secp256k1 ECDSA
|
||||
const eth = importa(ETH);
|
||||
if (eth.length) {
|
||||
const i = primul(eth);
|
||||
ctx.adauga(i.pos, `import ${i.spec}`, 'import', { grup: 'SECP256K1', nota: `Imported library: ${i.spec}, whose accounts and transaction signatures are secp256k1 ECDSA; the import alone does not show that this file signs.` }, { suprimaDe: ['SECP256K1'], bucata: `import ${i.spec}` });
|
||||
const re1 = /(?<![\w$.])(?:new\s+(?:[\w$]+\s*\.\s*)*(?:Wallet|SigningKey|HDNodeWallet)\s*\(|(?:[\w$]+\s*\.\s*)?(?:(?:Wallet|HDNodeWallet)\s*\.\s*(?:createRandom|fromPhrase|fromMnemonic|fromEncryptedJson|fromEncryptedJsonSync|fromSeed)|privateKeyToAccount|mnemonicToAccount|generatePrivateKey|recoverAddress|recoverMessageAddress|verifyMessage|verifyTypedData|recoverPublicKey|ecrecover|ecsign)\s*\()/g;
|
||||
const re2 = /\.\s*(?:signMessage|signTransaction|signTypedData|_signTypedData|signAuthorization)\s*\(|\baccounts\s*\.\s*(?:create|sign|signTransaction|privateKeyToAccount|recover)\s*\(/g;
|
||||
for (const m of [...ctx.potriviri(re1), ...ctx.potriviri(re2)]) {
|
||||
const f = /verify|recover/i.test(m[0]) ? ['verify'] : /sign/i.test(m[0]) ? ['sign'] : ['keygen'];
|
||||
ctx.adauga(m.index, m[0].replace(/[\s(]/g, ''), 'call', { grup: 'SECP256K1', functii: f }, { bucata: m[0] });
|
||||
}
|
||||
}
|
||||
for (const i of importa(SECP_LIB)) {
|
||||
ctx.adauga(i.pos, `import ${i.spec}`, 'import', { grup: 'SECP256K1', nota: `Imported library: ${i.spec} (secp256k1 only).` }, { bucata: `import ${i.spec}` });
|
||||
}
|
||||
|
||||
// elliptic
|
||||
const ell = importa(/^elliptic$/);
|
||||
if (ell.length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])new\s+(?:[\w$]+\s*\.\s*)?(ec|EC)\s*\(\s*(['"])([\w-]+)\2/g)) {
|
||||
ctx.adauga(m.index, 'elliptic ec', 'call', { grup: 'EC', curba: m[3], functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])new\s+(?:[\w$]+\s*\.\s*)?(eddsa|EdDSA)\s*\(\s*(['"])([\w-]+)\2/g)) {
|
||||
ctx.adauga(m.index, 'elliptic eddsa', 'call', { grup: 'EDDSA', nume: /448/.test(m[3]) ? 'Ed448' : 'Ed25519' }, { bucata: m[0] });
|
||||
}
|
||||
ctx.adauga(ell[0].pos, 'import elliptic', 'import', { grup: 'LIB-MULTI', nume: 'elliptic', motiv: 'Imports elliptic (classical elliptic-curve library, quantum-vulnerable in every mode); no curve construction recognized in this file.' }, { suprimaDe: ['EC', 'ECDSA', 'ECDH', 'SECP256K1', 'EDDSA'], bucata: 'import elliptic' });
|
||||
}
|
||||
|
||||
// node-forge
|
||||
const forge = importa(/^node-forge$/);
|
||||
if (forge.length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w$])pki\s*\.\s*rsa\s*\.\s*generateKeyPair\s*\(/g)) {
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
const b = /bits\s*:\s*(\d+)/.exec(a.text) || /^\s*(\d+)/.exec(a.text);
|
||||
ctx.adauga(m.index, 'forge.pki.rsa.generateKeyPair', 'call', { grup: 'RSA', param: b && b[1], functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$])md\s*\.\s*(md5|sha1|sha256|sha384|sha512)\s*\.\s*create\s*\(/g)) {
|
||||
ctx.adauga(m.index, `forge.md.${m[1]}`, 'call', { grup: 'HASH', hash: hashCanonic(m[1]), functii: ['digest'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$])cipher\s*\.\s*create(?:De)?cipher\s*\(\s*(['"])([\w-]+)\1/g)) {
|
||||
const t = /^(AES|3DES|DES|RC2)(?:-(\w+))?$/i.exec(m[2]);
|
||||
ctx.adauga(m.index, 'forge.cipher', 'call', t ? { grup: 'CIPHER', nume: t[1].toUpperCase(), mod: t[2] } : { grup: 'CIPHER', nume: m[2] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$])ed25519\s*\.\s*(generateKeyPair|sign|verify)\s*\(/g)) {
|
||||
ctx.adauga(m.index, `forge.ed25519.${m[1]}`, 'call', { grup: 'EDDSA', nume: 'Ed25519' }, { bucata: m[0] });
|
||||
}
|
||||
ctx.adauga(forge[0].pos, 'import node-forge', 'import', { grup: 'LIB-MULTI', nume: 'node-forge' }, { suprimaDe: ['RSA', 'HASH', 'CIPHER', 'EDDSA'], bucata: 'import node-forge' });
|
||||
}
|
||||
|
||||
// JWT: literalele de algoritm JWS din fisierele care importa o biblioteca JWT
|
||||
if (jwtLib.length) {
|
||||
let gasit = false;
|
||||
for (const [s, e] of ctx.siruri) {
|
||||
if (!`'"`.includes(ctx.text[s]) || ctx.text[e - 1] !== ctx.text[s]) continue;
|
||||
const v = ctx.text.slice(s + 1, e - 1);
|
||||
if (!JWS_RE.test(v)) continue;
|
||||
if (v === 'none') {
|
||||
const inainte = ctx.code.slice(Math.max(0, s - 60), s);
|
||||
if (!/alg(?:orithms?)?\s*:\s*\[?[^\]\n]*$/.test(inainte)) continue;
|
||||
}
|
||||
gasit = true;
|
||||
ctx.adauga(s, 'JWT algorithm', 'config', { ...jws(v), functii: ['sign', 'verify'] }, { bucata: `'${v}'` });
|
||||
}
|
||||
if (!gasit) {
|
||||
ctx.adauga(jwtLib[0].pos, `import ${jwtLib[0].spec}`, 'import', { grup: 'LIB-MULTI', nume: jwtLib[0].spec, motiv: `Imports ${jwtLib[0].spec}; no JWS algorithm literal found in this file (the algorithm may come from configuration or library defaults).` }, { bucata: `import ${jwtLib[0].spec}` });
|
||||
}
|
||||
}
|
||||
|
||||
// @noble/curves
|
||||
for (const i of importa(/^@noble\/curves(\/.*)?$/)) {
|
||||
const map = {
|
||||
secp256k1: { grup: 'SECP256K1' }, schnorr: { grup: 'SECP256K1', nume: 'Schnorr-secp256k1' },
|
||||
ed25519: { grup: 'EDDSA', nume: 'Ed25519' }, ed25519ph: { grup: 'EDDSA', nume: 'Ed25519' }, ed25519ctx: { grup: 'EDDSA', nume: 'Ed25519' },
|
||||
x25519: { grup: 'XDH', nume: 'X25519' }, ed448: { grup: 'EDDSA', nume: 'Ed448' }, x448: { grup: 'XDH', nume: 'X448' },
|
||||
p256: { grup: 'EC', curba: 'secp256r1' }, secp256r1: { grup: 'EC', curba: 'secp256r1' },
|
||||
p384: { grup: 'EC', curba: 'secp384r1' }, secp384r1: { grup: 'EC', curba: 'secp384r1' },
|
||||
p521: { grup: 'EC', curba: 'secp521r1' }, secp521r1: { grup: 'EC', curba: 'secp521r1' },
|
||||
bls12_381: { grup: 'PAIRING', nume: 'BLS12-381' }, bn254: { grup: 'PAIRING', nume: 'BN254' },
|
||||
};
|
||||
const sub = (/^@noble\/curves\/([\w-]+)/.exec(i.spec) || [])[1];
|
||||
const nume = i.importate.length ? i.importate : (sub ? [sub.replace(/-/g, '_').replace('bls12_381', 'bls12_381')] : []);
|
||||
for (const n of nume) {
|
||||
const act = map[n] || (n === 'nist' ? { grup: 'EC' } : null);
|
||||
if (act) ctx.adauga(i.pos, `import ${i.spec}`, 'import', act, { bucata: `import { ${n} } from '${i.spec}'` });
|
||||
}
|
||||
}
|
||||
|
||||
// @noble/post-quantum
|
||||
for (const i of importa(/^@noble\/post-quantum(\/.*)?$/)) {
|
||||
const sub = (/^@noble\/post-quantum\/([\w-]+)/.exec(i.spec) || [])[1] || '';
|
||||
const lista = i.importate.length ? i.importate : [sub];
|
||||
for (const n of lista) {
|
||||
let m;
|
||||
let act = null;
|
||||
if ((m = /^ml_kem(512|768|1024)$/.exec(n))) act = { grup: 'MLKEM', param: m[1] };
|
||||
else if ((m = /^ml_dsa(44|65|87)$/.exec(n))) act = { grup: 'MLDSA', param: m[1] };
|
||||
else if ((m = /^slh_dsa_(sha2|shake)_(128|192|256)([sf])$/.exec(n))) act = { grup: 'SLHDSA', param: `${m[1].toUpperCase()}-${m[2]}${m[3]}` };
|
||||
else if ((m = /^ml_kem(768|1024)_(x25519|p256|p384)$/i.exec(n))) act = { grup: 'HYBRID-KEX', nume: `ML-KEM-${m[1]}+${m[2].toUpperCase()}` };
|
||||
else if (/^xwing$/i.test(n)) act = { grup: 'HYBRID-KEX', nume: 'X-Wing' };
|
||||
else if (n === 'ml-kem') act = { grup: 'MLKEM' };
|
||||
else if (n === 'ml-dsa') act = { grup: 'MLDSA' };
|
||||
else if (n === 'slh-dsa') act = { grup: 'SLHDSA' };
|
||||
if (act) ctx.adauga(i.pos, `import ${i.spec}`, 'import', act, { bucata: `import { ${n} } from '${i.spec}'` });
|
||||
}
|
||||
}
|
||||
|
||||
// @noble/hashes
|
||||
for (const i of importa(/^@noble\/hashes(\/.*)?$/)) {
|
||||
for (const n of i.importate) {
|
||||
const h = hashCanonic(n.replace(/_/g, '-')) || ({ keccak_256: 'KECCAK256', sha3_256: 'SHA3-256', sha3_512: 'SHA3-512', ripemd160: 'RIPEMD160' })[n];
|
||||
if (h) ctx.adauga(i.pos, `import ${i.spec}`, 'import', { grup: 'HASH', hash: h }, { bucata: `import { ${n} } from '${i.spec}'` });
|
||||
}
|
||||
}
|
||||
|
||||
// crypto-js
|
||||
const cjs = importa(/^crypto-js(\/.*)?$/);
|
||||
if (cjs.length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])[\w$]+\s*\.\s*(MD5|SHA1|SHA224|SHA256|SHA384|SHA512|RIPEMD160|HmacMD5|HmacSHA1|HmacSHA256|HmacSHA384|HmacSHA512)\s*\(/g)) {
|
||||
const hmac = /^Hmac/.test(m[1]);
|
||||
const h = hashCanonic(m[1].replace(/^Hmac/, ''));
|
||||
ctx.adauga(m.index, `CryptoJS.${m[1]}`, 'call', hmac ? { grup: 'MAC', hash: h, functii: ['tag'] } : { grup: 'HASH', hash: h, functii: ['digest'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])[\w$]+\s*\.\s*(AES|DES|TripleDES|RC4|RC4Drop|Rabbit|Blowfish)\s*\.\s*(encrypt|decrypt)\s*\(/g)) {
|
||||
const nume = m[1] === 'TripleDES' ? '3DES' : m[1] === 'RC4Drop' ? 'RC4' : m[1];
|
||||
ctx.adauga(m.index, `CryptoJS.${m[1]}.${m[2]}`, 'call', { grup: 'CIPHER', nume, functii: [m[2]] }, { bucata: m[0] });
|
||||
}
|
||||
ctx.adauga(cjs[0].pos, `import ${cjs[0].spec}`, 'import', { grup: 'LIB-MULTI', nume: 'crypto-js' }, { suprimaDe: ['HASH', 'MAC', 'CIPHER'], bucata: `import ${cjs[0].spec}` });
|
||||
}
|
||||
|
||||
// tweetnacl / libsodium
|
||||
const nacl = importa(/^(tweetnacl|libsodium-wrappers|libsodium-wrappers-sumo|sodium-native)$/);
|
||||
if (nacl.length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])[\w$]+\s*\.\s*(sign(?:\s*\.\s*(?:keyPair|detached))?|crypto_sign\w*)\s*\(/g)) {
|
||||
ctx.adauga(m.index, 'nacl.sign', 'call', { grup: 'EDDSA', nume: 'Ed25519', functii: ['sign'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])[\w$]+\s*\.\s*(box(?:\s*\.\s*(?:keyPair|before))?|crypto_box\w*|crypto_kx\w*|crypto_scalarmult\w*)\s*\(/g)) {
|
||||
ctx.adauga(m.index, 'nacl.box', 'call', { grup: 'XDH', nume: 'X25519', functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])[\w$]+\s*\.\s*(secretbox|crypto_secretbox\w*|crypto_aead_xchacha20poly1305\w*)\s*\(/g)) {
|
||||
ctx.adauga(m.index, 'nacl.secretbox', 'call', { grup: 'CIPHER', nume: /xchacha/.test(m[1]) ? 'XChaCha20-Poly1305' : 'XSalsa20-Poly1305', functii: ['encrypt'] }, { bucata: m[0] });
|
||||
}
|
||||
ctx.adauga(nacl[0].pos, `import ${nacl[0].spec}`, 'import', { grup: 'LIB-MULTI', nume: nacl[0].spec }, { suprimaDe: ['EDDSA', 'XDH', 'CIPHER'], bucata: `import ${nacl[0].spec}` });
|
||||
}
|
||||
|
||||
// node-rsa
|
||||
const nrsa = importa(/^node-rsa$/);
|
||||
if (nrsa.length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w$.])new\s+[\w$]+\s*\(\s*\{\s*b\s*:\s*(\d+)/g)) {
|
||||
ctx.adauga(m.index, 'new NodeRSA', 'call', { grup: 'RSA', param: m[1], functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
ctx.adauga(nrsa[0].pos, 'import node-rsa', 'import', { grup: 'RSA' }, { suprimaDe: ['RSA'], bucata: 'import node-rsa' });
|
||||
}
|
||||
|
||||
// parole: bcrypt / argon2 / scrypt
|
||||
for (const i of importa(/^(bcrypt|bcryptjs|argon2|@node-rs\/argon2|@node-rs\/bcrypt|scrypt-js)$/)) {
|
||||
const nume = /argon2/.test(i.spec) ? 'Argon2' : /scrypt/.test(i.spec) ? 'scrypt' : 'bcrypt';
|
||||
ctx.adauga(i.pos, `import ${i.spec}`, 'import', { grup: 'KDF', nume }, { bucata: `import ${i.spec}` });
|
||||
}
|
||||
|
||||
// biblioteci cu multi algoritmi, fara apel recunoscut
|
||||
for (const i of importa(/^(openpgp|jsrsasign|sshpk|@peculiar\/x509|pkijs|node-jose)$/)) {
|
||||
ctx.adauga(i.pos, `import ${i.spec}`, 'import', { grup: 'LIB-MULTI', nume: i.spec }, { bucata: `import ${i.spec}` });
|
||||
}
|
||||
void c;
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// Detectorul Python: cryptography (hazmat), PyCryptodome, hashlib, hmac, ecdsa, PyJWT/jose, oqs, ssl.
|
||||
import { argumente, imparteArgumente, valoareArgument, necunoscut } from './context.mjs';
|
||||
import { hashCanonic, jws, JWS_RE } from './catalog.mjs';
|
||||
|
||||
const CURBE_PY = {
|
||||
SECP256R1: 'secp256r1', SECP384R1: 'secp384r1', SECP521R1: 'secp521r1', SECP224R1: 'secp224r1', SECP192R1: 'secp192r1',
|
||||
SECP256K1: 'secp256k1', BrainpoolP256R1: 'brainpoolP256r1', BrainpoolP384R1: 'brainpoolP384r1', BrainpoolP512R1: 'brainpoolP512r1',
|
||||
SECT571R1: 'sect571r1', SECT409R1: 'sect409r1', SECT283R1: 'sect283r1', SECT233R1: 'sect233r1', SECT163R2: 'sect163r2',
|
||||
};
|
||||
const CURBE_ECDSA_LIB = { SECP256k1: 'secp256k1', NIST192p: 'secp192r1', NIST224p: 'secp224r1', NIST256p: 'secp256r1', NIST384p: 'secp384r1', NIST521p: 'secp521r1', BRAINPOOLP256r1: 'brainpoolP256r1', BRAINPOOLP384r1: 'brainpoolP384r1', BRAINPOOLP512r1: 'brainpoolP512r1' };
|
||||
|
||||
function importaPy(ctx, re) {
|
||||
return ctx.potriviri(/^[ \t]*(?:from\s+([\w.]+)\s+import\b|import\s+([\w.]+(?:\s*,\s*[\w.]+)*))/gm)
|
||||
.filter((m) => (m[1] ? re.test(m[1]) : m[2].split(',').some((x) => re.test(x.trim()))));
|
||||
}
|
||||
|
||||
export function detecteazaPy(ctx) {
|
||||
const argsDe = (m) => { const a = argumente(ctx, m.index + m[0].length); return { ...a, parti: imparteArgumente(ctx, a.start, a.end) }; };
|
||||
const cuValoare = (m, arg, api, fn) => {
|
||||
const v = valoareArgument(ctx, arg);
|
||||
if (v.fel === 'literal' || v.fel === 'rezolvat') return fn(v.valoare, v.fel === 'rezolvat' ? { rezolvatDin: v.din } : {});
|
||||
if (v.fel === 'necunoscut') ctx.adauga(m.index, api, 'call', necunoscut(v.expresie, api), { bucata: m[0] + v.expresie });
|
||||
return null;
|
||||
};
|
||||
const numar = (a, cheie, poz) => {
|
||||
const k = new RegExp(`\\b${cheie}\\s*=\\s*(\\d+)`).exec(a.text);
|
||||
if (k) return k[1];
|
||||
if (poz !== undefined && a.parti[poz] && /^\d+$/.test(a.parti[poz].text)) return a.parti[poz].text;
|
||||
return undefined;
|
||||
};
|
||||
|
||||
// --- cryptography.hazmat ---
|
||||
if (importaPy(ctx, /^cryptography\b/).length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(rsa|dsa|dh)\s*\.\s*(generate_private_key|generate_parameters)\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const bits = m[1] === 'rsa' ? numar(a, 'key_size', 1) : numar(a, 'key_size', m[1] === 'dh' ? 1 : 0);
|
||||
ctx.adauga(m.index, `${m[1]}.${m[2]}`, 'call', { grup: m[1].toUpperCase(), param: bits, functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])ec\s*\.\s*(generate_private_key|derive_private_key)\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const k = /\bec\s*\.\s*(\w+)\s*\(/.exec(a.text);
|
||||
ctx.adauga(m.index, `ec.${m[1]}`, 'call', { grup: 'EC', curba: k ? (CURBE_PY[k[1]] || k[1]) : undefined, functii: ['keygen'] }, { bucata: m[0] + (k ? k[0] : '') });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])ec\s*\.\s*ECDSA\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const h = /\bhashes\s*\.\s*(\w+)\s*\(/.exec(a.text);
|
||||
ctx.adauga(m.index, 'ec.ECDSA', 'call', { grup: 'ECDSA', hash: h ? hashCanonic(h[1].replace(/_/g, '-')) : undefined, functii: ['sign', 'verify'] }, { bucata: m[0] + (h ? h[0] : '') });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])ec\s*\.\s*ECDH\s*\(/g)) {
|
||||
ctx.adauga(m.index, 'ec.ECDH', 'call', { grup: 'ECDH', functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w])(?:\w+\s*\.\s*)?(Ed25519|Ed448|X25519|X448)(PrivateKey|PublicKey)\s*\.\s*(generate|from_private_bytes|from_public_bytes)\s*\(/g)) {
|
||||
const ed = /^Ed/.test(m[1]);
|
||||
ctx.adauga(m.index, `${m[1]}${m[2]}.${m[3]}`, 'call', { grup: ed ? 'EDDSA' : 'XDH', nume: m[1], functii: [m[3] === 'generate' ? 'keygen' : 'other'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])padding\s*\.\s*(OAEP|PSS|PKCS1v15)\s*\(/g)) {
|
||||
const act = m[1] === 'OAEP' ? { grup: 'RSA', primitiv: 'pke', padding: 'oaep' } : m[1] === 'PSS' ? { grup: 'RSA', primitiv: 'signature', padding: 'other' } : { grup: 'RSA', primitiv: 'unknown', padding: 'pkcs1v15' };
|
||||
ctx.adauga(m.index, `padding.${m[1]}`, 'call', act, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])hashes\s*\.\s*(MD5|SHA1|SHA224|SHA256|SHA384|SHA512|SHA512_224|SHA512_256|SHA3_224|SHA3_256|SHA3_384|SHA3_512|SHAKE128|SHAKE256|BLAKE2b|BLAKE2s|SM3)\s*\(/g)) {
|
||||
const t = m[1].replace(/^SHA512_(224|256)$/, 'SHA512/$1').replace(/_/g, '-');
|
||||
ctx.adauga(m.index, `hashes.${m[1]}`, 'call', { grup: 'HASH', hash: hashCanonic(t) || t, functii: ['digest'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])algorithms\s*\.\s*(AES|AES128|AES256|TripleDES|ARC4|Blowfish|CAST5|IDEA|SEED|ChaCha20|Camellia|SM4)\s*\(/g)) {
|
||||
const map = { AES128: ['AES', '128'], AES256: ['AES', '256'], TripleDES: ['3DES'], ARC4: ['RC4'] };
|
||||
const [nume, param] = map[m[1]] || [m[1]];
|
||||
const a = argsDe(m);
|
||||
const dupa = ctx.code.slice(a.end, a.end + 80);
|
||||
const mod = /^\s*\)\s*,\s*modes\s*\.\s*(\w+)\s*\(/.exec(dupa);
|
||||
ctx.adauga(m.index, `algorithms.${m[1]}`, 'call', { grup: 'CIPHER', nume, param, mod: mod ? mod[1].toLowerCase() : undefined, functii: ['encrypt'] }, { bucata: m[0] + (mod ? ` modes.${mod[1]}` : '') });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(AESGCM|AESCCM|AESOCB3|AESSIV|AESGCMSIV|ChaCha20Poly1305)\s*(?:\.\s*generate_key\s*)?\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const b = /generate_key/.test(m[0]) ? numar(a, 'bit_length', 0) : undefined;
|
||||
const map = { AESGCM: 'gcm', AESCCM: 'ccm', AESOCB3: 'ocb', AESSIV: 'siv', AESGCMSIV: 'gcm-siv' };
|
||||
const act = m[1] === 'ChaCha20Poly1305' ? { grup: 'CIPHER', nume: 'ChaCha20-Poly1305' } : { grup: 'CIPHER', nume: 'AES', param: b, mod: map[m[1]] };
|
||||
ctx.adauga(m.index, m[1], 'call', { ...act, functii: [/generate_key/.test(m[0]) ? 'keygen' : 'encrypt'] }, { bucata: m[0] });
|
||||
}
|
||||
}
|
||||
|
||||
// --- PyCryptodome (Crypto / Cryptodome) ---
|
||||
if (importaPy(ctx, /^(Crypto|Cryptodome)\b/).length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(RSA|DSA)\s*\.\s*generate\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
ctx.adauga(m.index, `${m[1]}.generate`, 'call', { grup: m[1], param: numar(a, 'bits', 0), functii: ['keygen'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(RSA|DSA|ECC)\s*\.\s*(import_key|importKey|construct)\s*\(/g)) {
|
||||
ctx.adauga(m.index, `${m[1]}.${m[2]}`, 'call', m[1] === 'ECC' ? { grup: 'EC' } : { grup: m[1] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])ECC\s*\.\s*generate\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const k = /\bcurve\s*=\s*(['"])([^'"]+)\1/.exec(a.text);
|
||||
const curba = k ? k[2] : undefined;
|
||||
const act = curba && /^ed(25519|448)$/i.test(curba) ? { grup: 'EDDSA', nume: /448/.test(curba) ? 'Ed448' : 'Ed25519' } : { grup: 'EC', curba };
|
||||
ctx.adauga(m.index, 'ECC.generate', 'call', { ...act, functii: ['keygen'] }, { bucata: m[0] + (k ? k[0] : '') });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(AES|DES|DES3|ARC4|ARC2|Blowfish|CAST|ChaCha20|ChaCha20_Poly1305|Salsa20)\s*\.\s*new\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const mod = /\b(?:AES|DES|DES3|ARC2|Blowfish|CAST)\s*\.\s*MODE_(\w+)/.exec(a.text);
|
||||
const map = { DES3: '3DES', ARC4: 'RC4', ARC2: 'RC2', ChaCha20_Poly1305: 'ChaCha20-Poly1305' };
|
||||
ctx.adauga(m.index, `${m[1]}.new`, 'call', { grup: 'CIPHER', nume: map[m[1]] || m[1], mod: mod ? mod[1].toLowerCase() : undefined, functii: ['encrypt'] }, { bucata: m[0] + (mod ? mod[0] : '') });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(PKCS1_OAEP|PKCS1_v1_5|pkcs1_15|pss|DSS|eddsa)\s*\.\s*new\s*\(/g)) {
|
||||
const map = {
|
||||
PKCS1_OAEP: { grup: 'RSA', primitiv: 'pke', padding: 'oaep' },
|
||||
PKCS1_v1_5: { grup: 'RSA', primitiv: 'unknown', padding: 'pkcs1v15' },
|
||||
pkcs1_15: { grup: 'RSA', primitiv: 'signature', padding: 'pkcs1v15' },
|
||||
pss: { grup: 'RSA', primitiv: 'signature', padding: 'other' },
|
||||
DSS: { grup: 'CLASSIC-SIG', nume: 'DSS (DSA or ECDSA)', motiv: 'DSS signature (DSA or ECDSA, key type from the key object)' },
|
||||
eddsa: { grup: 'EDDSA', nume: 'EdDSA' },
|
||||
};
|
||||
ctx.adauga(m.index, `${m[1]}.new`, 'call', map[m[1]], { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(MD2|MD4|MD5|SHA1|SHA224|SHA256|SHA384|SHA512|SHA3_256|SHA3_384|SHA3_512|RIPEMD160|RIPEMD|BLAKE2b|BLAKE2s)\s*\.\s*new\s*\(/g)) {
|
||||
const t = m[1].replace(/_/g, '-').replace(/^RIPEMD$/, 'RIPEMD160');
|
||||
ctx.adauga(m.index, `${m[1]}.new`, 'call', { grup: 'HASH', hash: hashCanonic(t) || t, functii: ['digest'] }, { bucata: m[0] });
|
||||
}
|
||||
}
|
||||
|
||||
// --- hashlib ---
|
||||
for (const m of ctx.potriviri(/(?<![\w.])hashlib\s*\.\s*(md5|sha1|sha224|sha256|sha384|sha512|sha3_224|sha3_256|sha3_384|sha3_512|blake2b|blake2s|shake_128|shake_256)\b(\s*\()?/g)) {
|
||||
let nota;
|
||||
if (m[2]) {
|
||||
const a = argumente(ctx, m.index + m[0].length);
|
||||
if (/usedforsecurity\s*=\s*False/.test(a.text)) nota = 'Marked usedforsecurity=False: likely a non-security use (checksum); review and document.';
|
||||
}
|
||||
const t = m[1].replace(/^shake_/, 'SHAKE').replace(/_/g, '-');
|
||||
ctx.adauga(m.index, `hashlib.${m[1]}`, 'call', { grup: 'HASH', hash: hashCanonic(t) || t, nota, functii: ['digest'] }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])hashlib\s*\.\s*new\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
cuValoare(m, a.parti[0], 'hashlib.new', (val, ex) => ctx.adauga(m.index, 'hashlib.new', 'call', { grup: 'HASH', hash: hashCanonic(val) || val, functii: ['digest'] }, { ...ex, bucata: `hashlib.new('${val}')` }));
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])hashlib\s*\.\s*pbkdf2_hmac\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
cuValoare(m, a.parti[0], 'hashlib.pbkdf2_hmac', (val, ex) => ctx.adauga(m.index, 'hashlib.pbkdf2_hmac', 'call', { grup: 'KDF', nume: `PBKDF2-HMAC-${val.toUpperCase()}`, hash: hashCanonic(val) || val, functii: ['keyderive'] }, { ...ex, bucata: m[0] }));
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])hmac\s*\.\s*(new|digest)\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const d = /\bdigestmod\s*=\s*(['"])(\w+)\1/.exec(a.text);
|
||||
if (d) ctx.adauga(m.index, `hmac.${m[1]}`, 'call', { grup: 'MAC', hash: hashCanonic(d[2]) || d[2], functii: ['tag'] }, { bucata: m[0] + d[0] });
|
||||
}
|
||||
|
||||
// --- ecdsa (python-ecdsa) ---
|
||||
if (importaPy(ctx, /^ecdsa\b/).length) {
|
||||
for (const m of ctx.potriviri(/(?<![\w.])(SigningKey|VerifyingKey)\s*\.\s*(generate|from_string|from_secret_exponent|from_pem|from_der|from_public_point)\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
const k = /\bcurve\s*=\s*(?:ecdsa\s*\.\s*)?(\w+)/.exec(a.text);
|
||||
let curba = k ? (CURBE_ECDSA_LIB[k[1]] || k[1]) : undefined;
|
||||
let nota;
|
||||
if (!k && m[2] === 'generate') { curba = 'secp192r1'; nota = 'No curve argument: python-ecdsa defaults to NIST192p.'; }
|
||||
const act = curba && /^Ed(25519|448)$/.test(curba) ? { grup: 'EDDSA', nume: curba } : { grup: 'ECDSA', curba };
|
||||
ctx.adauga(m.index, `${m[1]}.${m[2]}`, 'call', { ...act, nota, functii: [m[1] === 'SigningKey' ? 'sign' : 'verify'] }, { bucata: m[0] + (k ? k[0] : '') });
|
||||
}
|
||||
}
|
||||
|
||||
// --- JWT (PyJWT, python-jose, jwcrypto, authlib) ---
|
||||
const jwtImp = importaPy(ctx, /^(jwt|jose|jwcrypto|authlib\.jose)\b/);
|
||||
if (jwtImp.length) {
|
||||
let gasit = false;
|
||||
for (const [s, e] of ctx.siruri) {
|
||||
if (!`'"`.includes(ctx.text[s]) || ctx.text[e - 1] !== ctx.text[s]) continue;
|
||||
const v = ctx.text.slice(s + 1, e - 1);
|
||||
if (!JWS_RE.test(v)) continue;
|
||||
if (v === 'none') {
|
||||
const inainte = ctx.code.slice(Math.max(0, s - 60), s);
|
||||
if (!/alg(?:orithms?)?\s*=\s*\[?[^\]\n]*$/.test(inainte)) continue;
|
||||
}
|
||||
gasit = true;
|
||||
ctx.adauga(s, 'JWT algorithm', 'config', { ...jws(v), functii: ['sign', 'verify'] }, { bucata: `'${v}'` });
|
||||
}
|
||||
if (!gasit) ctx.adauga(jwtImp[0].index, 'import jwt', 'import', { grup: 'LIB-MULTI', nume: 'JWT library', motiv: 'Imports a JWT library; no JWS algorithm literal found in this file (the algorithm may come from configuration or library defaults).' }, { bucata: jwtImp[0][0].trim() });
|
||||
}
|
||||
|
||||
// --- liboqs-python ---
|
||||
for (const m of ctx.potriviri(/(?<![\w.])oqs\s*\.\s*(KeyEncapsulation|Signature)\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
cuValoare(m, a.parti[0], `oqs.${m[1]}`, (val, ex) => ctx.adauga(m.index, `oqs.${m[1]}`, 'call', oqsActiv(val), { ...ex, bucata: `oqs.${m[1]}('${val}')` }));
|
||||
}
|
||||
|
||||
// --- ssl ---
|
||||
for (const m of ctx.potriviri(/(?<![\w.])ssl\s*\.\s*PROTOCOL_(SSLv2|SSLv3|SSLv23|TLSv1_2|TLSv1_1|TLSv1|TLS_CLIENT|TLS_SERVER|TLS)\b/g)) {
|
||||
const map = { SSLv2: 'ssl2', SSLv3: 'ssl3', TLSv1: '1.0', TLSv1_1: '1.1', TLSv1_2: '1.2' };
|
||||
ctx.adauga(m.index, `ssl.PROTOCOL_${m[1]}`, 'config', { grup: 'TLS', param: map[m[1]] || 'negotiated', rol: 'only' }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/(?<![\w.])ssl\s*\.\s*TLSVersion\s*\.\s*(SSLv3|TLSv1_3|TLSv1_2|TLSv1_1|TLSv1)\b/g)) {
|
||||
const map = { SSLv3: 'ssl3', TLSv1: '1.0', TLSv1_1: '1.1', TLSv1_2: '1.2', TLSv1_3: '1.3' };
|
||||
const inainte = ctx.code.slice(Math.max(0, m.index - 40), m.index);
|
||||
const rol = /maximum_version\s*=\s*$/.test(inainte) ? 'max' : 'min';
|
||||
ctx.adauga(m.index, `ssl.TLSVersion.${m[1]}`, 'config', { grup: 'TLS', param: map[m[1]], rol }, { bucata: m[0] });
|
||||
}
|
||||
for (const m of ctx.potriviri(/\.\s*set_ecdh_curve\s*\(/g)) {
|
||||
const a = argsDe(m);
|
||||
cuValoare(m, a.parti[0], 'set_ecdh_curve', (val, ex) => ctx.adauga(m.index, 'set_ecdh_curve', 'config', { grup: 'ECDH', curba: val, functii: ['keygen'] }, { ...ex, bucata: m[0] }));
|
||||
}
|
||||
}
|
||||
|
||||
export function oqsActiv(val) {
|
||||
const t = String(val);
|
||||
let m;
|
||||
if ((m = /^ML-KEM-(512|768|1024)$/i.exec(t))) return { grup: 'MLKEM', param: m[1] };
|
||||
if ((m = /^ML-DSA-(44|65|87)$/i.exec(t))) return { grup: 'MLDSA', param: m[1] };
|
||||
if ((m = /^SLH[-_]DSA[-_](SHA2|SHAKE)[-_](128|192|256)([sf])/i.exec(t))) return { grup: 'SLHDSA', param: `${m[1].toUpperCase()}-${m[2]}${m[3].toLowerCase()}` };
|
||||
if ((m = /^Falcon-(512|1024)$/i.exec(t))) return { grup: 'FALCON', param: m[1] };
|
||||
if (/^(Kyber\d+|Dilithium\d|SPHINCS\+?-.*)$/i.test(t)) return { grup: 'PREPQ', nume: t, primitiv: /kyber/i.test(t) ? 'kem' : 'signature' };
|
||||
if (/^HQC-\d+$/i.test(t)) return { grup: 'HQC', nume: t };
|
||||
return { grup: 'UNKNOWN', motiv: `Post-quantum mechanism "${t}" is not in this tool's catalog.` };
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
// Lexer minimal pe limbaje: comentariile devin spatii (liniile si coloanele raman pe loc),
|
||||
// sirurile raman in text, dar li se tin minte intervalele, ca un detector sa poata cere
|
||||
// "numele API-ului e COD, nu text dintr-un sir".
|
||||
//
|
||||
// Nu e un parser: e cat trebuie ca un tipar sa nu se potriveasca in comentarii, in
|
||||
// docstring-uri Python sau in textul unui sir.
|
||||
|
||||
const KW_REGEX = new Set(['return', 'typeof', 'instanceof', 'in', 'of', 'new', 'delete', 'void',
|
||||
'throw', 'case', 'do', 'else', 'yield', 'await']);
|
||||
const PUNCT_REGEX = '(,=:[!&|?{};+-*%<>~^';
|
||||
|
||||
export function curata(text, limbaj) {
|
||||
switch (limbaj) {
|
||||
case 'javascript': return curataC(text, { template: true, regex: true });
|
||||
case 'java': return curataC(text, { textBlock: true });
|
||||
case 'go': return curataC(text, { raw: true });
|
||||
case 'python': return curataPython(text);
|
||||
default: return { code: text, siruri: [] };
|
||||
}
|
||||
}
|
||||
|
||||
function golitor(out) {
|
||||
return (a, b) => {
|
||||
for (let k = a; k < b; k++) if (out[k] !== '\n' && out[k] !== '\r') out[k] = ' ';
|
||||
};
|
||||
}
|
||||
|
||||
function curataC(text, o) {
|
||||
const n = text.length;
|
||||
const out = text.split('');
|
||||
const goleste = golitor(out);
|
||||
const siruri = [];
|
||||
const stiva = [];
|
||||
let acolade = 0;
|
||||
let prevSig = '';
|
||||
let prevWord = '';
|
||||
let i = 0;
|
||||
let esecRegex = null; // bit 1: din (pozitie, in afara clasei) nu se ajunge la sfarsitul literalului; bit 2: la fel, in clasa
|
||||
|
||||
// scaneaza un segment de template literal pornind de la j; intoarce pozitia si daca s-a deschis ${
|
||||
const scanTpl = (inceput, j) => {
|
||||
while (j < n) {
|
||||
const ch = text[j];
|
||||
if (ch === '\\') { j += 2; continue; }
|
||||
if (ch === '`') { siruri.push([inceput, j + 1]); return { pos: j + 1, deschis: false }; }
|
||||
if (ch === '$' && text[j + 1] === '{') { siruri.push([inceput, j + 2]); return { pos: j + 2, deschis: true }; }
|
||||
j++;
|
||||
}
|
||||
siruri.push([inceput, n]);
|
||||
return { pos: n, deschis: false };
|
||||
};
|
||||
|
||||
while (i < n) {
|
||||
const c = text[i];
|
||||
const d = text[i + 1];
|
||||
if (c === '/' && d === '/') {
|
||||
let j = i;
|
||||
while (j < n && text[j] !== '\n') j++;
|
||||
goleste(i, j); i = j; continue;
|
||||
}
|
||||
if (c === '/' && d === '*') {
|
||||
let j = text.indexOf('*/', i + 2);
|
||||
j = j < 0 ? n : j + 2;
|
||||
goleste(i, j); i = j; continue;
|
||||
}
|
||||
if (o.textBlock && c === '"' && text.startsWith('"""', i)) {
|
||||
let j = text.indexOf('"""', i + 3);
|
||||
j = j < 0 ? n : j + 3;
|
||||
siruri.push([i, j]); i = j; prevSig = '"'; continue;
|
||||
}
|
||||
if (c === '"' || c === "'") {
|
||||
let j = i + 1;
|
||||
while (j < n && text[j] !== c && text[j] !== '\n') { if (text[j] === '\\') j++; j++; }
|
||||
j = Math.min(n, j + 1);
|
||||
siruri.push([i, j]); i = j; prevSig = c; continue;
|
||||
}
|
||||
if (c === '`' && o.raw) {
|
||||
let j = text.indexOf('`', i + 1);
|
||||
j = j < 0 ? n : j + 1;
|
||||
siruri.push([i, j]); i = j; prevSig = '`'; continue;
|
||||
}
|
||||
if (c === '`' && o.template) {
|
||||
const r = scanTpl(i, i + 1);
|
||||
i = r.pos;
|
||||
if (r.deschis) { stiva.push(acolade); acolade++; }
|
||||
prevSig = '`'; continue;
|
||||
}
|
||||
if (c === '/' && o.regex) {
|
||||
const permis = prevSig === '' || PUNCT_REGEX.includes(prevSig) || (prevSig === 'a' && KW_REGEX.has(prevWord));
|
||||
if (permis) {
|
||||
// Cautarea sfarsitului unui literal regex e determinista din (pozitie, "in clasa"), deci o stare din care s-a ajuns o data
|
||||
// la capatul randului fara sfarsit duce acolo si a doua oara. Starile acestea se tin minte (2026-09-29, revizuirea
|
||||
// adversariala): fara ele un rand lung cu multe `/` nedeschise (de ex. `=/[` repetat) costa patratic, minute pe 1 MB.
|
||||
let j = i + 1;
|
||||
let inClasa = false;
|
||||
let bun = false;
|
||||
const vizitate = [];
|
||||
while (j < n && text[j] !== '\n') {
|
||||
if (esecRegex && esecRegex[j] & (inClasa ? 2 : 1)) break;
|
||||
vizitate.push(inClasa ? -j - 1 : j);
|
||||
const ch = text[j];
|
||||
if (ch === '\\') { j += 2; continue; }
|
||||
if (ch === '[') inClasa = true;
|
||||
else if (ch === ']') inClasa = false;
|
||||
else if (ch === '/' && !inClasa) { bun = true; break; }
|
||||
j++;
|
||||
}
|
||||
if (!bun) {
|
||||
if (!esecRegex) esecRegex = new Uint8Array(n);
|
||||
for (const v of vizitate) { if (v < 0) esecRegex[-v - 1] |= 2; else esecRegex[v] |= 1; }
|
||||
}
|
||||
if (bun) {
|
||||
j++;
|
||||
while (j < n && /[a-z]/i.test(text[j])) j++;
|
||||
siruri.push([i, j]); i = j; prevSig = '/'; continue;
|
||||
}
|
||||
}
|
||||
prevSig = '/'; i++; continue;
|
||||
}
|
||||
if (c === '{') { acolade++; prevSig = '{'; i++; continue; }
|
||||
if (c === '}') {
|
||||
acolade--;
|
||||
if (stiva.length && acolade === stiva[stiva.length - 1]) {
|
||||
stiva.pop();
|
||||
const r = scanTpl(i, i + 1);
|
||||
i = r.pos;
|
||||
if (r.deschis) { stiva.push(acolade); acolade++; }
|
||||
prevSig = '`'; continue;
|
||||
}
|
||||
prevSig = '}'; i++; continue;
|
||||
}
|
||||
if (/[A-Za-z_$]/.test(c)) {
|
||||
let j = i + 1;
|
||||
while (j < n && /[\w$]/.test(text[j])) j++;
|
||||
prevWord = text.slice(i, j); prevSig = 'a'; i = j; continue;
|
||||
}
|
||||
if (/[0-9]/.test(c)) {
|
||||
let j = i + 1;
|
||||
while (j < n && /[\w.]/.test(text[j])) j++;
|
||||
prevWord = ''; prevSig = '0'; i = j; continue;
|
||||
}
|
||||
if (!/\s/.test(c)) prevSig = c;
|
||||
i++;
|
||||
}
|
||||
return { code: out.join(''), siruri };
|
||||
}
|
||||
|
||||
function curataPython(text) {
|
||||
const n = text.length;
|
||||
const out = text.split('');
|
||||
const goleste = golitor(out);
|
||||
const siruri = [];
|
||||
let i = 0;
|
||||
// primul caracter care nu e spatiu pe randul curent (-1 = niciunul inca). Tinut din mers (2026-09-29, revizuirea adversariala):
|
||||
// forma veche cauta inapoi inceputul randului la FIECARE sir triplu, deci un rand lung cu multe siruri triple costa patratic.
|
||||
let primulPeRand = -1;
|
||||
while (i < n) {
|
||||
const c = text[i];
|
||||
if (c === '\n') { primulPeRand = -1; i++; continue; }
|
||||
if (c === '#') {
|
||||
let j = i;
|
||||
while (j < n && text[j] !== '\n') j++;
|
||||
goleste(i, j); i = j; continue;
|
||||
}
|
||||
if (c === '"' || c === "'") {
|
||||
// prefixul (r, b, f, u, rb, ...) face parte din sir
|
||||
let start = i;
|
||||
while (start > 0 && /[rRbBuUfF]/.test(text[start - 1]) && i - start < 2) start--;
|
||||
if (start < i && start > 0 && /[\w]/.test(text[start - 1])) start = i;
|
||||
const triplu = text.startsWith(c.repeat(3), i);
|
||||
let j;
|
||||
// inainte de sir, pe randul lui, sunt numai spatii (prefixul r/b/f/u face parte din sir)?
|
||||
const singurPeRand = primulPeRand < 0 || primulPeRand >= start;
|
||||
let ultimaLinieNoua = -1;
|
||||
if (triplu) {
|
||||
j = i + 3;
|
||||
while (j < n && !text.startsWith(c.repeat(3), j)) {
|
||||
if (text[j] === '\\') { if (text[j + 1] === '\n') ultimaLinieNoua = j + 1; j++; } else if (text[j] === '\n') ultimaLinieNoua = j;
|
||||
j++;
|
||||
}
|
||||
j = Math.min(n, j + 3);
|
||||
} else {
|
||||
j = i + 1;
|
||||
while (j < n && text[j] !== c && text[j] !== '\n') { if (text[j] === '\\') { if (text[j + 1] === '\n') ultimaLinieNoua = j + 1; j++; } j++; }
|
||||
if (j < n && text[j] === '\n') ultimaLinieNoua = j; // sir neterminat: intervalul lui se opreste DUPA acest rand nou
|
||||
j = Math.min(n, j + 1);
|
||||
}
|
||||
// dupa sir: daca sirul a trecut peste un rand nou, randul curent incepe in el (coada lui nu e spatiu), afara de cazul in
|
||||
// care sirul se termina chiar cu randul nou; altfel randul curent are acum cel putin sirul
|
||||
if (ultimaLinieNoua >= 0) primulPeRand = ultimaLinieNoua === j - 1 ? -1 : ultimaLinieNoua + 1;
|
||||
else if (primulPeRand < 0) primulPeRand = start;
|
||||
// docstring: sirul triplu e singur pe randul lui, ca instructiune; se trateaza ca un comentariu
|
||||
if (triplu && singurPeRand) { goleste(start, j); i = j; continue; }
|
||||
siruri.push([start, j]); i = j; continue;
|
||||
}
|
||||
if (primulPeRand < 0 && c !== ' ' && c !== '\t') primulPeRand = i;
|
||||
i++;
|
||||
}
|
||||
return { code: out.join(''), siruri };
|
||||
}
|
||||
|
||||
// cautare binara: pozitia e inauntrul unui sir?
|
||||
export function inSir(siruri, pos) {
|
||||
let lo = 0;
|
||||
let hi = siruri.length - 1;
|
||||
while (lo <= hi) {
|
||||
const mid = (lo + hi) >> 1;
|
||||
const [s, e] = siruri[mid];
|
||||
if (pos < s) hi = mid - 1;
|
||||
else if (pos >= e) lo = mid + 1;
|
||||
else return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function inceputuriDeRand(text) {
|
||||
const r = [0];
|
||||
for (let k = 0; k < text.length; k++) if (text.charCodeAt(k) === 10) r.push(k + 1);
|
||||
return r;
|
||||
}
|
||||
|
||||
export function randul(inceputuri, pos) {
|
||||
let lo = 0;
|
||||
let hi = inceputuri.length - 1;
|
||||
while (lo < hi) {
|
||||
const mid = (lo + hi + 1) >> 1;
|
||||
if (inceputuri[mid] <= pos) lo = mid; else hi = mid - 1;
|
||||
}
|
||||
return { line: lo + 1, column: pos - inceputuri[lo] + 1 };
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
// Bibliotecile criptografice DECLARATE in manifeste. Declararea nu e folosire: aceste intrari
|
||||
// ies ca componente "library", nu ca gasiri clasificate.
|
||||
import { inceputuriDeRand, randul } from './lexer.mjs';
|
||||
|
||||
// nume -> ce ofera (text scurt, in engleza, pentru utilizator)
|
||||
const NPM = {
|
||||
'node-forge': 'RSA, AES, DES, MD5, SHA-1, SHA-2, X.509 (classical)',
|
||||
elliptic: 'ECDSA/ECDH/EdDSA on classical curves including secp256k1',
|
||||
secp256k1: 'secp256k1 ECDSA', 'tiny-secp256k1': 'secp256k1 ECDSA', '@noble/secp256k1': 'secp256k1 ECDSA',
|
||||
ethers: 'secp256k1 ECDSA accounts and signatures', web3: 'secp256k1 ECDSA accounts and signatures', viem: 'secp256k1 ECDSA accounts and signatures',
|
||||
'ethereum-cryptography': 'secp256k1, keccak, AES, scrypt',
|
||||
jsonwebtoken: 'JWS HS*/RS*/PS*/ES*', jose: 'JWS/JWE (classical algorithms)', 'jwt-simple': 'JWS HS*/RS*',
|
||||
'@noble/curves': 'secp256k1, P-256/384/521, Ed25519/X25519, Ed448, BLS12-381 (classical)',
|
||||
'@noble/post-quantum': 'ML-KEM, ML-DSA, SLH-DSA (post-quantum)',
|
||||
'@noble/hashes': 'SHA-2, SHA-3, BLAKE, legacy MD5/SHA-1/RIPEMD-160',
|
||||
'@noble/ed25519': 'Ed25519',
|
||||
'crypto-js': 'AES, DES, 3DES, RC4, MD5, SHA-1, SHA-2', tweetnacl: 'Ed25519, X25519, XSalsa20-Poly1305',
|
||||
'libsodium-wrappers': 'Ed25519, X25519, XChaCha20-Poly1305, Argon2', 'node-rsa': 'RSA', jsrsasign: 'RSA, ECDSA, X.509',
|
||||
openpgp: 'OpenPGP: RSA, ECC, AES', bcrypt: 'bcrypt password hashing', bcryptjs: 'bcrypt password hashing', argon2: 'Argon2 password hashing',
|
||||
sshpk: 'SSH keys: RSA, ECDSA, Ed25519', '@peculiar/x509': 'X.509 certificates', pkijs: 'X.509/CMS',
|
||||
};
|
||||
const PYPI = {
|
||||
cryptography: 'RSA, EC, DH, Ed25519/X25519, AES, hashes (pyca/cryptography)',
|
||||
pycryptodome: 'RSA, DSA, ECC, AES, DES, hashes', pycryptodomex: 'RSA, DSA, ECC, AES, DES, hashes',
|
||||
pycrypto: 'RSA, DSA, AES, DES (unmaintained since 2013)', ecdsa: 'ECDSA on classical curves', pyjwt: 'JWS HS*/RS*/ES*/PS*/EdDSA',
|
||||
'python-jose': 'JWS/JWE (classical algorithms)', jwcrypto: 'JWS/JWE (classical algorithms)', pynacl: 'Ed25519, X25519',
|
||||
pyopenssl: 'TLS and X.509 via OpenSSL', paramiko: 'SSH: RSA, ECDSA, Ed25519, classical key exchange', 'liboqs-python': 'post-quantum KEMs and signatures (liboqs)',
|
||||
oqs: 'post-quantum KEMs and signatures (liboqs)', web3: 'secp256k1 ECDSA accounts', 'eth-account': 'secp256k1 ECDSA accounts', 'eth-keys': 'secp256k1 ECDSA',
|
||||
coincurve: 'secp256k1 ECDSA', bcrypt: 'bcrypt password hashing', 'argon2-cffi': 'Argon2 password hashing', passlib: 'password hashing', rsa: 'RSA (pure Python)',
|
||||
};
|
||||
const MAVEN_GROUP = {
|
||||
'org.bouncycastle': 'Bouncy Castle: classical and post-quantum algorithms',
|
||||
'io.jsonwebtoken': 'JJWT: JWS HS*/RS*/ES*/PS*/EdDSA', 'com.nimbusds': 'Nimbus JOSE+JWT', 'com.auth0': 'java-jwt (if artifact is java-jwt)',
|
||||
'org.web3j': 'secp256k1 ECDSA accounts (web3j)', 'com.google.crypto.tink': 'Tink: AEAD, signatures, hybrid encryption', 'org.conscrypt': 'Conscrypt TLS provider',
|
||||
};
|
||||
const GO = [
|
||||
[/^golang\.org\/x\/crypto$/, 'extended crypto: ssh, chacha20poly1305, curve25519, bcrypt, argon2'],
|
||||
[/^github\.com\/cloudflare\/circl$/, 'CIRCL: ML-KEM, ML-DSA, SLH-DSA, hybrid KEMs, classical curves'],
|
||||
[/^github\.com\/ethereum\/go-ethereum$/, 'secp256k1 ECDSA accounts and signatures'],
|
||||
[/^github\.com\/golang-jwt\/jwt(\/v\d+)?$/, 'JWS HS*/RS*/ES*/PS*/EdDSA'],
|
||||
[/^github\.com\/decred\/dcrd\/dcrec\/secp256k1(\/v\d+)?$/, 'secp256k1 ECDSA'],
|
||||
[/^github\.com\/btcsuite\/btcd\/btcec(\/v\d+)?$/, 'secp256k1 ECDSA'],
|
||||
[/^filippo\.io\/edwards25519$/, 'Edwards25519 group arithmetic'],
|
||||
[/^github\.com\/open-quantum-safe\/liboqs-go$/, 'post-quantum KEMs and signatures (liboqs)'],
|
||||
];
|
||||
|
||||
export const NUME_MANIFEST = /^(package\.json|requirements[\w.-]*\.txt|pyproject\.toml|pom\.xml|build\.gradle(\.kts)?|go\.mod)$/;
|
||||
|
||||
function lib(ecosistem, nume, versiune, ofera, pos, inceputuri, fisier) {
|
||||
const { line } = randul(inceputuri, pos);
|
||||
const purl = ecosistem === 'npm' ? `pkg:npm/${nume.startsWith('@') ? '%40' + nume.slice(1) : nume}${versiune && /^[\w.-]+$/.test(versiune) ? '@' + versiune : ''}`
|
||||
: ecosistem === 'pypi' ? `pkg:pypi/${nume}${versiune && /^[\w.-]+$/.test(versiune) ? '@' + versiune : ''}`
|
||||
: ecosistem === 'maven' ? `pkg:maven/${nume.replace(':', '/')}${versiune && /^[\w.-]+$/.test(versiune) ? '@' + versiune : ''}`
|
||||
: `pkg:golang/${nume}${versiune ? '@' + versiune : ''}`;
|
||||
return { file: fisier, line, ecosystem: ecosistem, name: nume, version: versiune || undefined, provides: ofera, purl };
|
||||
}
|
||||
|
||||
export function citesteManifest(text, numeFisier, rel) {
|
||||
const inceputuri = inceputuriDeRand(text);
|
||||
const r = [];
|
||||
if (numeFisier === 'package.json') {
|
||||
let j;
|
||||
try { j = JSON.parse(text); } catch { return { biblioteci: [], eroare: 'package.json is not valid JSON' }; }
|
||||
for (const sect of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
|
||||
const d = j && j[sect];
|
||||
if (!d || typeof d !== 'object') continue;
|
||||
const ps = text.indexOf(`"${sect}"`);
|
||||
for (const [nume, ver] of Object.entries(d)) {
|
||||
const ofera = NPM[nume] || (/^@ethersproject\//.test(nume) ? 'secp256k1 ECDSA (ethers v5 module)' : null);
|
||||
if (!ofera) continue;
|
||||
const pos = text.indexOf(`"${nume}"`, ps < 0 ? 0 : ps);
|
||||
r.push(lib('npm', nume, String(ver).replace(/^[\^~>=<\s]+/, ''), ofera, pos < 0 ? 0 : pos, inceputuri, rel));
|
||||
}
|
||||
}
|
||||
} else if (/^requirements/.test(numeFisier)) {
|
||||
let pos = 0;
|
||||
for (const linie of text.split('\n')) {
|
||||
const t = linie.replace(/#.*/, '').trim();
|
||||
const m = /^([A-Za-z0-9_.-]+)(?:\[[^\]]*\])?\s*(?:[=<>!~]=?\s*([\w.*+-]+))?/.exec(t);
|
||||
if (m) {
|
||||
const nume = m[1].toLowerCase().replace(/_/g, '-');
|
||||
if (PYPI[nume]) r.push(lib('pypi', nume, m[2], PYPI[nume], pos + linie.indexOf(m[1]), inceputuri, rel));
|
||||
}
|
||||
pos += linie.length + 1;
|
||||
}
|
||||
} else if (numeFisier === 'pyproject.toml') {
|
||||
let pos = 0;
|
||||
let sectiune = '';
|
||||
for (const linie of text.split('\n')) {
|
||||
const h = /^\s*\[([^\]]+)\]/.exec(linie);
|
||||
if (h) sectiune = h[1];
|
||||
for (const m of linie.matchAll(/["']([A-Za-z0-9_.-]+)(?:\[[^\]]*\])?\s*(?:[=<>!~]=?\s*([\w.*+-]+))?[^"']*["']/g)) {
|
||||
const nume = m[1].toLowerCase().replace(/_/g, '-');
|
||||
if (PYPI[nume]) r.push(lib('pypi', nume, m[2], PYPI[nume], pos + m.index + 1, inceputuri, rel));
|
||||
}
|
||||
const p = /^\s*([A-Za-z0-9_.-]+)\s*=\s*(?:["']([^"']*)["']|\{)/.exec(linie);
|
||||
if (p && /dependencies/.test(sectiune)) {
|
||||
const nume = p[1].toLowerCase().replace(/_/g, '-');
|
||||
if (PYPI[nume]) r.push(lib('pypi', nume, p[2] ? p[2].replace(/^[\^~>=<\s]+/, '') : undefined, PYPI[nume], pos + linie.indexOf(p[1]), inceputuri, rel));
|
||||
}
|
||||
pos += linie.length + 1;
|
||||
}
|
||||
} else if (numeFisier === 'pom.xml') {
|
||||
for (const m of text.matchAll(/<dependency>([\s\S]*?)<\/dependency>/g)) {
|
||||
const g = /<groupId>\s*([^<\s]+)\s*<\/groupId>/.exec(m[1]);
|
||||
const a = /<artifactId>\s*([^<\s]+)\s*<\/artifactId>/.exec(m[1]);
|
||||
const v = /<version>\s*([^<\s]+)\s*<\/version>/.exec(m[1]);
|
||||
if (!g || !a) continue;
|
||||
const ofera = MAVEN_GROUP[g[1]];
|
||||
if (!ofera) continue;
|
||||
r.push(lib('maven', `${g[1]}:${a[1]}`, v && !/\$\{/.test(v[1]) ? v[1] : undefined, ofera, m.index + m[0].indexOf(a[0]), inceputuri, rel));
|
||||
}
|
||||
} else if (/^build\.gradle/.test(numeFisier)) {
|
||||
for (const m of text.matchAll(/["']([\w.-]+):([\w.-]+)(?::([\w.-]+))?["']/g)) {
|
||||
const ofera = MAVEN_GROUP[m[1]];
|
||||
if (ofera) r.push(lib('maven', `${m[1]}:${m[2]}`, m[3], ofera, m.index + 1, inceputuri, rel));
|
||||
}
|
||||
} else if (numeFisier === 'go.mod') {
|
||||
let pos = 0;
|
||||
let inRequire = false;
|
||||
for (const linie of text.split('\n')) {
|
||||
const t = linie.replace(/\/\/.*/, '').trim();
|
||||
if (/^require\s*\($/.test(t)) inRequire = true;
|
||||
else if (inRequire && t === ')') inRequire = false;
|
||||
const m = inRequire ? /^([\w.\-/]+)\s+(v[\w.\-+]+)/.exec(t) : /^require\s+([\w.\-/]+)\s+(v[\w.\-+]+)/.exec(t);
|
||||
if (m) {
|
||||
const intrare = GO.find(([re]) => re.test(m[1]));
|
||||
if (intrare) r.push(lib('golang', m[1], m[2], intrare[1], pos + linie.indexOf(m[1]), inceputuri, rel));
|
||||
}
|
||||
pos += linie.length + 1;
|
||||
}
|
||||
}
|
||||
return { biblioteci: r };
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// Material criptografic in text: blocuri PEM complete (antet, corp base64, subsol).
|
||||
// Cheile private se raporteaza DOAR ca locatie si tip; valoarea nu se tipareste niciodata.
|
||||
// Tipul cheii se afla parsand blocul in memorie cu node:crypto (date, nu cod executat).
|
||||
import { createPublicKey, createPrivateKey, X509Certificate } from 'node:crypto';
|
||||
|
||||
// Cautarea blocurilor e LINIARA in marimea textului (2026-09-29, revizuirea adversariala): forma veche, un singur regex cu
|
||||
// corp lenes si referinta inapoi, relua cautarea sfarsitului de la FIECARE antet BEGIN, deci un fisier de 1 MB cu antete fara
|
||||
// sfarsit costa minute. Acum pentru fiecare antet se cere PRIMUL `-----END <tip>-----` de dupa el si ca intre ele sa nu fie niciun
|
||||
// caracter din afara alfabetului corpului, adica exact ce potrivea regexul vechi; ambele pozitii se tin minte si se cauta numai
|
||||
// inainte, deci fiecare caracter se citeste de un numar marginit de ori.
|
||||
const SURSA_BEGIN = '-----BEGIN ((?:RSA |EC |DSA |OPENSSH |ENCRYPTED )?PRIVATE KEY|PUBLIC KEY|RSA PUBLIC KEY|CERTIFICATE)-----';
|
||||
const SURSA_AFARA_DIN_CORP = '[^A-Za-z0-9+/=\\s:,\\-\\r\\n]';
|
||||
|
||||
export function* blocuriPem(text) {
|
||||
const reBegin = new RegExp(SURSA_BEGIN, 'g');
|
||||
const reRau = new RegExp(SURSA_AFARA_DIN_CORP, 'g');
|
||||
// o cautare tinuta minte e buna pentru pozitia `de` daca a pornit la sau inainte de `de` si nu a gasit ceva inainte de `de`
|
||||
const valabil = (c, de) => c && c.la <= de && (c.poz < 0 || c.poz >= de);
|
||||
const sfarsit = new Map(); // tip -> { la, poz }: primul END al tipului la pozitia >= la (poz -1 = niciunul)
|
||||
let rau = null; // { la, poz }: primul caracter din afara alfabetului corpului, la pozitia >= la
|
||||
const urmatorulSfarsit = (tip, de) => {
|
||||
let c = sfarsit.get(tip);
|
||||
if (!valabil(c, de)) { c = { la: de, poz: text.indexOf(`-----END ${tip}-----`, de) }; sfarsit.set(tip, c); }
|
||||
return c.poz;
|
||||
};
|
||||
const urmatorulRau = (de) => {
|
||||
if (!valabil(rau, de)) { reRau.lastIndex = de; const x = reRau.exec(text); rau = { la: de, poz: x ? x.index : -1 }; }
|
||||
return rau.poz;
|
||||
};
|
||||
let m;
|
||||
while ((m = reBegin.exec(text))) {
|
||||
const tip = m[1];
|
||||
const corpDe = m.index + m[0].length;
|
||||
const e = urmatorulSfarsit(tip, corpDe);
|
||||
const r = e < 0 ? -1 : urmatorulRau(corpDe);
|
||||
// fara potrivire: cautarea continua de la caracterul urmator, ca la regexul vechi (un antet poate incepe in liniutele celui de dinainte)
|
||||
if (e < 0 || (r >= 0 && r < e)) { reBegin.lastIndex = m.index + 1; continue; }
|
||||
const capat = e + `-----END ${tip}-----`.length;
|
||||
yield { index: m.index, tip, corp: text.slice(corpDe, e), bloc: text.slice(m.index, capat) };
|
||||
reBegin.lastIndex = capat;
|
||||
}
|
||||
}
|
||||
|
||||
function activCheie(ko) {
|
||||
const t = ko.asymmetricKeyType;
|
||||
const d = ko.asymmetricKeyDetails || {};
|
||||
if (t === 'rsa' || t === 'rsa-pss') return { grup: 'RSA', param: d.modulusLength ? String(d.modulusLength) : undefined };
|
||||
if (t === 'dsa') return { grup: 'DSA', param: d.modulusLength ? String(d.modulusLength) : undefined };
|
||||
if (t === 'dh') return { grup: 'DH' };
|
||||
if (t === 'ec') return { grup: 'EC', curba: d.namedCurve };
|
||||
if (t === 'ed25519' || t === 'ed448') return { grup: 'EDDSA', nume: t === 'ed25519' ? 'Ed25519' : 'Ed448' };
|
||||
if (t === 'x25519' || t === 'x448') return { grup: 'XDH', nume: t === 'x25519' ? 'X25519' : 'X448' };
|
||||
if (/^ml-dsa-(44|65|87)$/.test(t)) return { grup: 'MLDSA', param: t.slice(7) };
|
||||
if (/^ml-kem-(512|768|1024)$/.test(t)) return { grup: 'MLKEM', param: t.slice(7) };
|
||||
if (/^slh-dsa/.test(t)) return { grup: 'SLHDSA', param: t.slice(8) };
|
||||
return { grup: 'UNKNOWN', motiv: `Key type "${t}" is not in this tool's catalog.` };
|
||||
}
|
||||
|
||||
export function detecteazaPem(text, ctx) {
|
||||
for (const b of blocuriPem(text)) {
|
||||
const m = [b.bloc, b.tip, b.corp];
|
||||
m.index = b.index;
|
||||
const corp = m[2].replace(/[\s]/g, '').replace(/^Proc-Type:.*?DEK-Info:[^A-Za-z0-9+/]*/, '');
|
||||
if (corp.replace(/[^A-Za-z0-9+/=]/g, '').length < 64) continue; // un antet singur (de ex. intr-un parser) nu e material
|
||||
const tip = m[1];
|
||||
let act;
|
||||
let assetType;
|
||||
if (tip === 'CERTIFICATE') {
|
||||
assetType = 'certificate';
|
||||
try {
|
||||
const x = new X509Certificate(m[0]);
|
||||
act = { ...activCheie(x.publicKey), nota: 'Certificate public key.' };
|
||||
act.certificat = {
|
||||
subjectName: x.subject.replace(/\n/g, ', '),
|
||||
issuerName: x.issuer.replace(/\n/g, ', '),
|
||||
notValidBefore: new Date(x.validFrom).toISOString(),
|
||||
notValidAfter: new Date(x.validTo).toISOString(),
|
||||
certificateFormat: 'X.509',
|
||||
certificateExtension: 'pem',
|
||||
fingerprintSha256: x.fingerprint256,
|
||||
};
|
||||
} catch {
|
||||
act = { grup: 'UNKNOWN', motiv: 'PEM certificate block that could not be parsed.', certificat: { certificateFormat: 'X.509', certificateExtension: 'pem' } };
|
||||
}
|
||||
} else if (/PUBLIC KEY/.test(tip)) {
|
||||
assetType = 'related-crypto-material';
|
||||
try { act = activCheie(createPublicKey(m[0])); } catch { act = tip === 'RSA PUBLIC KEY' ? { grup: 'RSA' } : { grup: 'UNKNOWN', motiv: 'PEM public key block that could not be parsed.' }; }
|
||||
act.material = { type: 'public-key', format: 'PEM' };
|
||||
} else {
|
||||
assetType = 'related-crypto-material';
|
||||
if (/ENCRYPTED/.test(tip) || /Proc-Type:\s*4,ENCRYPTED/.test(m[2])) act = { grup: 'UNKNOWN', motiv: 'Encrypted private key: algorithm not visible without the passphrase.' };
|
||||
else if (tip === 'OPENSSH PRIVATE KEY') act = { grup: 'UNKNOWN', motiv: 'OpenSSH private key: algorithm not parsed by this tool.' };
|
||||
else {
|
||||
try { act = activCheie(createPrivateKey(m[0])); } catch {
|
||||
act = tip.startsWith('RSA') ? { grup: 'RSA' } : tip.startsWith('EC') ? { grup: 'EC' } : tip.startsWith('DSA') ? { grup: 'DSA' } : { grup: 'UNKNOWN', motiv: 'PEM private key block that could not be parsed.' };
|
||||
}
|
||||
}
|
||||
act.material = { type: 'private-key', format: 'PEM' };
|
||||
act.nota = 'Private key material stored in the scanned tree: if it is real, treat it as exposed and rotate it.';
|
||||
}
|
||||
ctx.adauga(m.index, `PEM ${tip}`, 'pem', { ...act, assetType }, { bucata: `-----BEGIN ${tip}-----` });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
// Rezumatul text (engleza, pentru utilizator) si regula --fail-on.
|
||||
import { CLS } from './catalog.mjs';
|
||||
|
||||
const ORDINE = [CLS.V, CLS.W, CLS.S, CLS.U];
|
||||
|
||||
export function numarPeClase(gasiri) {
|
||||
const r = Object.fromEntries(ORDINE.map((c) => [c, 0]));
|
||||
for (const g of gasiri) r[g.classification] = (r[g.classification] || 0) + 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
// --fail-on: lista separata prin virgula din vulnerable, weak, unknown, any
|
||||
// "vulnerable" = orice gasire pe care un calculator cuantic o sparge (inclusiv cele weak-now care sunt si Shor)
|
||||
export function verificaFailOn(gasiri, spec) {
|
||||
if (!spec) return { esec: false, motive: [] };
|
||||
const cer = new Set(String(spec).split(',').map((x) => x.trim()).filter(Boolean));
|
||||
const necunoscute = [...cer].filter((x) => !['vulnerable', 'weak', 'unknown', 'any'].includes(x));
|
||||
if (necunoscute.length) throw new Error(`--fail-on: unknown value(s): ${necunoscute.join(', ')} (use vulnerable, weak, unknown or any)`);
|
||||
const motive = [];
|
||||
const vuln = gasiri.filter((g) => g.classification === CLS.V || (g.classification === CLS.W && g.quantumVulnerable));
|
||||
const slabe = gasiri.filter((g) => g.classification === CLS.W);
|
||||
const nec = gasiri.filter((g) => g.classification === CLS.U);
|
||||
if ((cer.has('vulnerable') || cer.has('any')) && vuln.length) motive.push(`${vuln.length} quantum-vulnerable finding(s)`);
|
||||
if ((cer.has('weak') || cer.has('any')) && slabe.length) motive.push(`${slabe.length} weak-now finding(s)`);
|
||||
if (cer.has('unknown') && nec.length) motive.push(`${nec.length} unknown finding(s)`);
|
||||
return { esec: motive.length > 0, motive };
|
||||
}
|
||||
|
||||
export function rezumatText(rez) {
|
||||
const s = rez.stats;
|
||||
const g = rez.findings;
|
||||
const L = [];
|
||||
L.push(`Aere crypto inventory ${rez.tool.version}: ${rez.rootName}`);
|
||||
const limbaje = Object.entries(s.codeFiles).sort().map(([k, v]) => `${k} ${v}`).join(', ') || 'none';
|
||||
L.push(`Files: ${s.filesSeen} seen | ${s.codeFilesTotal} analyzed as code (${limbaje}) | ${s.textFilesPemOnly} other text files checked for PEM key/certificate blocks only`);
|
||||
const nr = s.notRead;
|
||||
L.push(`Not read: ${nr.binary} binary, ${nr.tooLarge} too large (> ${rez.options.maxFileBytes} bytes), ${nr.fileLimit} over the file limit (${rez.options.maxFiles}), ${nr.unreadable} unreadable; ${s.symlinksNotFollowed} symlinks not followed`);
|
||||
for (const k of ['binary', 'tooLarge', 'fileLimit', 'unreadable']) {
|
||||
if (s.notReadExamples[k].length) L.push(` e.g. ${k}: ${s.notReadExamples[k].join(', ')}${nr[k] > s.notReadExamples[k].length ? ', ...' : ''}`);
|
||||
}
|
||||
if (s.resolutionLimitFiles) L.push(`Variable resolution limit (${rez.options.maxResolvedVariablesPerFile} distinct variables per file) reached in ${s.resolutionLimitFiles} file(s); further variables there are reported as unknown: ${s.resolutionLimitExamples.join(', ')}${s.resolutionLimitFiles > s.resolutionLimitExamples.length ? ', ...' : ''}`);
|
||||
const ex = Object.entries(s.dirsExcluded).sort().map(([n, c]) => `${n} (${c})`).join(', ');
|
||||
L.push(`Directories not descended: ${ex || 'none'}${s.dirsUnreadable ? `; ${s.dirsUnreadable} unreadable` : ''}`);
|
||||
const pk = {};
|
||||
for (const x of g) pk[x.evidenceKind] = (pk[x.evidenceKind] || 0) + 1;
|
||||
L.push(`Findings: ${g.length} (${Object.entries(pk).sort().map(([k, v]) => `${k} ${v}`).join(', ') || 'none'})`);
|
||||
const cl = numarPeClase(g);
|
||||
const wq = g.filter((x) => x.classification === CLS.W && x.quantumVulnerable).length;
|
||||
for (const c of ORDINE) L.push(` ${c.padEnd(19)} ${String(cl[c]).padStart(5)}${c === CLS.W && wq ? ` (${wq} also quantum-vulnerable)` : ''}`);
|
||||
const pl = {};
|
||||
for (const x of g) pl[x.language] = (pl[x.language] || 0) + 1;
|
||||
L.push(`By language: ${Object.entries(pl).sort().map(([k, v]) => `${k} ${v}`).join(', ') || 'none'}`);
|
||||
const frecv = {};
|
||||
for (const x of g) { const k = `${x.name} [${x.classification}]`; frecv[k] = (frecv[k] || 0) + 1; }
|
||||
const top = Object.entries(frecv).sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1)).slice(0, 10);
|
||||
if (top.length) {
|
||||
L.push('Most frequent:');
|
||||
top.forEach(([k, v], i) => L.push(` ${String(i + 1).padStart(2)}. ${k} x${v}`));
|
||||
}
|
||||
const vf = new Map();
|
||||
for (const x of g) {
|
||||
if (!(x.classification === CLS.V || (x.classification === CLS.W && x.quantumVulnerable))) continue;
|
||||
if (!vf.has(x.file)) vf.set(x.file, []);
|
||||
vf.get(x.file).push(x);
|
||||
}
|
||||
L.push(`Files with quantum-vulnerable findings: ${vf.size}`);
|
||||
for (const [f, lista] of [...vf].sort()) {
|
||||
L.push(` ${f} (${lista.length}: ${[...new Set(lista.map((x) => x.name))].join(', ')})`);
|
||||
}
|
||||
L.push(`Declared crypto libraries (manifests; declaration is not use): ${rez.libraries.length}`);
|
||||
for (const b of rez.libraries) L.push(` ${b.file}:${b.line} ${b.name}${b.version ? ' ' + b.version : ''} (${b.provides})`);
|
||||
if (s.manifestErrors.length) L.push(`Manifest errors: ${s.manifestErrors.join('; ')}`);
|
||||
L.push('Scope: static pattern analysis of source text. Not covered: compiled binaries, transitive dependencies, cryptography chosen at run time.');
|
||||
// numele de fisiere si textele vin din arborele scanat: caracterele de control (secvente ANSI) nu ajung in terminal
|
||||
return L.map((l) => l.replace(/[\u0000-\u001f\u007f-\u009f]/g, '?')).join('\n');
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
// Parcurgerea dosarului si orchestrarea detectorilor. Nimic nu se sare in tacere: fiecare
|
||||
// fisier vazut ajunge intr-o singura categorie numarata, si suma se verifica la sfarsit.
|
||||
import { readdirSync, statSync, readFileSync, lstatSync } from 'node:fs';
|
||||
import { join, relative, sep, basename, resolve, extname } from 'node:path';
|
||||
import { facContext, aplicaSuprimarea, REZOLVARI_PE_FISIER } from './context.mjs';
|
||||
import { detecteazaJs } from './detect-js.mjs';
|
||||
import { detecteazaPy } from './detect-py.mjs';
|
||||
import { detecteazaJava } from './detect-java.mjs';
|
||||
import { detecteazaGo } from './detect-go.mjs';
|
||||
import { detecteazaPem } from './pem.mjs';
|
||||
import { citesteManifest, NUME_MANIFEST } from './manifeste.mjs';
|
||||
|
||||
export const VERSIUNE = '0.2.0';
|
||||
export const DOSARE_EXCLUSE_IMPLICIT = ['.git', '.hg', '.svn', 'node_modules', '__pycache__', '.venv', 'venv', '.tox', '.gradle', '.idea', '.mypy_cache', '.pytest_cache'];
|
||||
|
||||
const LIMBAJ = {
|
||||
'.js': 'javascript', '.mjs': 'javascript', '.cjs': 'javascript', '.jsx': 'javascript',
|
||||
'.ts': 'javascript', '.tsx': 'javascript', '.mts': 'javascript', '.cts': 'javascript',
|
||||
'.py': 'python', '.pyw': 'python',
|
||||
'.java': 'java',
|
||||
'.go': 'go',
|
||||
};
|
||||
const DETECTOR = { javascript: detecteazaJs, python: detecteazaPy, java: detecteazaJava, go: detecteazaGo };
|
||||
const EXEMPLE = 5;
|
||||
|
||||
export function limbajul(cale) {
|
||||
return LIMBAJ[extname(cale).toLowerCase()] || null;
|
||||
}
|
||||
|
||||
// binar = contine un octet NUL in primii 8192 octeti
|
||||
export function eBinar(buf) {
|
||||
const n = Math.min(buf.length, 8192);
|
||||
for (let i = 0; i < n; i++) if (buf[i] === 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
export function scaneaza(radacina, optiuni = {}) {
|
||||
const o = {
|
||||
maxFileBytes: optiuni.maxFileBytes ?? 1048576,
|
||||
maxFiles: optiuni.maxFiles ?? 50000,
|
||||
excludeDirs: new Set([...(optiuni.noDefaultExcludes ? [] : DOSARE_EXCLUSE_IMPLICIT), ...(optiuni.excludeDirs || [])]),
|
||||
};
|
||||
const root = resolve(radacina);
|
||||
const st = statSync(root);
|
||||
if (!st.isDirectory()) throw new Error(`not a directory: ${radacina}`);
|
||||
const inceput = new Date();
|
||||
const stats = {
|
||||
filesSeen: 0,
|
||||
codeFiles: {},
|
||||
textFilesPemOnly: 0,
|
||||
manifests: 0,
|
||||
manifestErrors: [],
|
||||
notRead: { binary: 0, tooLarge: 0, fileLimit: 0, unreadable: 0 },
|
||||
notReadExamples: { binary: [], tooLarge: [], fileLimit: [], unreadable: [] },
|
||||
symlinksNotFollowed: 0,
|
||||
resolutionLimitFiles: 0,
|
||||
resolutionLimitExamples: [],
|
||||
dirsExcluded: {},
|
||||
dirsUnreadable: 0,
|
||||
};
|
||||
const gasiri = [];
|
||||
const biblioteci = [];
|
||||
let cititeSauIncercate = 0;
|
||||
const noteaza = (motiv, rel, extra) => {
|
||||
stats.notRead[motiv]++;
|
||||
if (stats.notReadExamples[motiv].length < EXEMPLE) stats.notReadExamples[motiv].push(extra ? `${rel} (${extra})` : rel);
|
||||
};
|
||||
|
||||
const stiva = [root];
|
||||
while (stiva.length) {
|
||||
const dir = stiva.pop();
|
||||
let intrari;
|
||||
try { intrari = readdirSync(dir, { withFileTypes: true }); } catch { stats.dirsUnreadable++; continue; }
|
||||
intrari.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
||||
const subdosare = [];
|
||||
for (const e of intrari) {
|
||||
const abs = join(dir, e.name);
|
||||
if (e.isSymbolicLink()) { stats.symlinksNotFollowed++; continue; }
|
||||
if (e.isDirectory()) {
|
||||
if (o.excludeDirs.has(e.name)) { stats.dirsExcluded[e.name] = (stats.dirsExcluded[e.name] || 0) + 1; continue; }
|
||||
subdosare.push(abs);
|
||||
continue;
|
||||
}
|
||||
if (!e.isFile()) continue;
|
||||
stats.filesSeen++;
|
||||
const rel = relative(root, abs).split(sep).join('/');
|
||||
if (cititeSauIncercate >= o.maxFiles) { noteaza('fileLimit', rel); continue; }
|
||||
cititeSauIncercate++;
|
||||
let marime;
|
||||
try { marime = lstatSync(abs).size; } catch (err) { noteaza('unreadable', rel, err.code); continue; }
|
||||
if (marime > o.maxFileBytes) { noteaza('tooLarge', rel, `${marime} bytes`); continue; }
|
||||
let buf;
|
||||
try { buf = readFileSync(abs); } catch (err) { noteaza('unreadable', rel, err.code); continue; }
|
||||
if (eBinar(buf)) { noteaza('binary', rel); continue; }
|
||||
const text = buf.toString('utf8');
|
||||
const limbaj = limbajul(abs);
|
||||
const ctx = facContext(text, limbaj || 'text', rel);
|
||||
if (limbaj) {
|
||||
stats.codeFiles[limbaj] = (stats.codeFiles[limbaj] || 0) + 1;
|
||||
DETECTOR[limbaj](ctx);
|
||||
if (ctx.rezolvariPestePlafon) {
|
||||
stats.resolutionLimitFiles++;
|
||||
if (stats.resolutionLimitExamples.length < EXEMPLE) stats.resolutionLimitExamples.push(rel);
|
||||
}
|
||||
} else {
|
||||
stats.textFilesPemOnly++;
|
||||
}
|
||||
detecteazaPem(text, ctx);
|
||||
gasiri.push(...aplicaSuprimarea(ctx.gasiri));
|
||||
if (NUME_MANIFEST.test(basename(abs))) {
|
||||
stats.manifests++;
|
||||
const r = citesteManifest(text, basename(abs), rel);
|
||||
if (r.eroare) stats.manifestErrors.push(`${rel}: ${r.eroare}`);
|
||||
biblioteci.push(...r.biblioteci);
|
||||
}
|
||||
}
|
||||
for (let i = subdosare.length - 1; i >= 0; i--) stiva.push(subdosare[i]);
|
||||
}
|
||||
|
||||
// garda: fiecare fisier vazut e intr-o singura categorie
|
||||
const cod = Object.values(stats.codeFiles).reduce((a, b) => a + b, 0);
|
||||
const nr = stats.notRead;
|
||||
const suma = cod + stats.textFilesPemOnly + nr.binary + nr.tooLarge + nr.fileLimit + nr.unreadable;
|
||||
if (suma !== stats.filesSeen) throw new Error(`internal accounting error: ${stats.filesSeen} files seen, ${suma} accounted for`);
|
||||
stats.codeFilesTotal = cod;
|
||||
|
||||
gasiri.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : a.line - b.line || a.column - b.column));
|
||||
biblioteci.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : a.line - b.line));
|
||||
return {
|
||||
tool: { name: 'aere-crypto-inventory', version: VERSIUNE },
|
||||
root,
|
||||
rootName: basename(root),
|
||||
options: { maxFileBytes: o.maxFileBytes, maxFiles: o.maxFiles, excludeDirs: [...o.excludeDirs].sort(), maxResolvedVariablesPerFile: REZOLVARI_PE_FISIER },
|
||||
startedAt: inceput.toISOString(),
|
||||
finishedAt: new Date().toISOString(),
|
||||
findings: gasiri,
|
||||
libraries: biblioteci,
|
||||
stats,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user