crypto-inventory 0.4.0: Swift and Kotlin detectors (CryptoKit, CommonCrypto, Security.framework, Android Keystore, Tink, OkHttp, the libsignal Swift and Android APIs), Apple and Android manifests; remediation recipes for Apache and HAProxy measured on real servers, HAProxy precondition fixed

This commit is contained in:
Aere Network committed 2026-10-18 08:53:13 +03:00
1 parent e453c0c42d
commit b6ed4d2b71
37 files changed
+1623 -82

No files matched your search

+185 -22
View File
@@ -1,5 +1,9 @@
// Detectorul Java: JCA getInstance("..."), SSLContext, protocoale activate, Bouncy Castle.
import { argumente, imparteArgumente, valoareArgument, necunoscut } from './context.mjs';
// 0.4.0 (2026-10-18): acelasi cod serveste si Kotlin (`detecteazaJvm(ctx, true)`, chemat din detect-kotlin.mjs): JCA e acelasi API,
// fara `new`, fara `;`, cu `val`/`var` si `arrayOf(...)`; si regulile noi comune JVM-ului: Android Keystore (KeyGenParameterSpec),
// Tink (sabloanele de chei), OkHttp (ConnectionSpec, TlsVersion, CipherSuite, CertificatePinner) si verificarea lanturilor de
// certificate (CertPathValidator "PKIX", TrustManagerFactory, X509TrustManager, checkServerTrusted), raportata ca autentificare.
import { argumente, imparteArgumente, valoareArgument, necunoscut, escapeRe } 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' };
@@ -40,7 +44,7 @@ export function activJca(clasa, val, dupa = '', variabila = null) {
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*"([^"]+)"');
const spec = peVar('initialize', '(?:new\\s+)?ECGenParameterSpec\\s*\\(\\s*"([^"]+)"'); // Kotlin: fara new
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 };
@@ -169,14 +173,94 @@ export function variabilaAtribuita(c, pos) {
return st < sf ? c.slice(st, sf) : null;
}
export function detecteazaJava(ctx) {
// KeyProperties.KEY_ALGORITHM_* (Android Keystore) -> numele JCA pe care il primeste getInstance
const KEY_ALGORITHM = { EC: 'EC', RSA: 'RSA', AES: 'AES', '3DES': 'DESede', HMAC_SHA1: 'HmacSHA1', HMAC_SHA224: 'HmacSHA224', HMAC_SHA256: 'HmacSHA256', HMAC_SHA384: 'HmacSHA384', HMAC_SHA512: 'HmacSHA512', XDH: 'XDH' };
const NUME_PKIX = 'X.509 chain validation (PKIX)';
// Lantul unui KeyGenParameterSpec.Builder(...)....build(): marimea cheii, curba, hash-urile, modurile, umpluturile
function citesteSpecAndroid(lant) {
const t = lant.slice(0, 3000);
const r = {};
let m;
if ((m = /setKeySize\s*\(\s*(\d+)\s*\)/.exec(t))) r.bits = m[1];
if ((m = /(?:new\s+)?ECGenParameterSpec\s*\(\s*"([^"]+)"/.exec(t))) { r.curba = m[1]; r.pozCurba = m.index; } // pozitia, ca sa nu se numere si separat
if ((m = /BLOCK_MODE_(GCM|CBC|ECB|CTR)\b/.exec(t))) r.mod = m[1].toLowerCase();
if ((m = /DIGEST_(SHA1|SHA224|SHA256|SHA384|SHA512|MD5)\b/.exec(t))) r.hash = hashCanonic(m[1]) || m[1];
if ((m = /(ENCRYPTION_PADDING_RSA_OAEP|ENCRYPTION_PADDING_RSA_PKCS1|SIGNATURE_PADDING_RSA_PSS|SIGNATURE_PADDING_RSA_PKCS1)\b/.exec(t))) r.padding = /OAEP/.test(m[1]) ? 'oaep' : /PSS/.test(m[1]) ? 'other' : 'pkcs1v15';
return r;
}
// Specificatia Android Keystore data generatorului `variabila` prin .initialize(spec) / .init(spec): inline in apel sau printr-o variabila
// definita in fisier cu KeyGenParameterSpec.Builder; intoarce { spec, pozitiiBuilder } (pozitiile builderelor consumate)
function specAndroidPentru(ctx, dupaStart, dupa, variabila) {
if (!variabila) return null;
const s = new RegExp(`(?<![\\w.])${escapeRe(variabila)}\\s*\\.\\s*(?:initialize|init)\\s*\\(`).exec(dupa);
if (!s) return null;
const a = argumente(ctx, dupaStart + s.index + s[0].length);
const RE_B = /(?<![\w.])KeyGenParameterSpec\s*\.\s*Builder\s*\(/g;
const cu = (baza, text) => { const spec = citesteSpecAndroid(text); const pozitii = [baza]; if (spec.pozCurba !== undefined) pozitii.push(baza + spec.pozCurba); return { spec, pozitii }; };
let b = RE_B.exec(a.text);
if (b) return cu(a.start + b.index, a.text.slice(b.index));
const id = /^\s*([A-Za-z_]\w*)\s*$/.exec(a.text);
if (!id) return null;
const def = new RegExp(`(?<![\\w.])${escapeRe(id[1])}\\s*(?::\\s*KeyGenParameterSpec\\s*)?=\\s*KeyGenParameterSpec\\s*\\.\\s*Builder\\s*\\(`).exec(ctx.code);
if (!def) return null;
const inceput = def.index + def[0].lastIndexOf('KeyGenParameterSpec');
return cu(inceput, ctx.code.slice(inceput));
}
// Sabloanele de chei Tink (KeyTemplates.get("..."), AeadKeyTemplates.X, PredefinedAeadParameters.X): numele spune algoritmul
export function sablonTink(nume) {
const t = String(nume).trim().replace(/_RAW$/, '');
let m;
const pq = pqNume(t.replace(/_/g, '-'));
if (pq) return pq;
if ((m = /^AES(128|256)_GCM(_SIV)?$/.exec(t))) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: m[2] ? 'gcm-siv' : 'gcm', functii: ['encrypt', 'decrypt'] };
if ((m = /^AES(128|256)_EAX$/.exec(t))) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: 'eax', functii: ['encrypt', 'decrypt'] };
if ((m = /^AES(128|256)_CTR_HMAC_SHA256$/.exec(t))) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: 'ctr', functii: ['encrypt', 'decrypt'], nota: 'Tink AES-CTR with HMAC-SHA-256 (encrypt-then-MAC).' };
if ((m = /^AES(128|256)_GCM_HKDF_(\d+KB|\d+MB)$/.exec(t))) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: 'gcm', functii: ['encrypt', 'decrypt'], nota: 'Tink streaming AEAD (segments keyed with HKDF).' };
if (t === 'AES256_SIV') return { grup: 'CIPHER', nume: 'AES', param: '256', mod: 'siv', functii: ['encrypt', 'decrypt'] };
if (t === 'XCHACHA20_POLY1305') return { grup: 'CIPHER', nume: 'XChaCha20-Poly1305', functii: ['encrypt', 'decrypt'] };
if (t === 'CHACHA20_POLY1305') return { grup: 'CIPHER', nume: 'ChaCha20-Poly1305', functii: ['encrypt', 'decrypt'] };
if (t === 'AES_CMAC') return { grup: 'MAC', nume: 'AES-CMAC', functii: ['tag'] };
if ((m = /^HMAC_SHA(256|512)_(\d+)BITTAG$/.exec(t))) return { grup: 'MAC', hash: `SHA${m[1]}`, functii: ['tag'] };
if ((m = /^HMAC_SHA(256|512)_PRF$/.exec(t)) || (m = /^HKDF_SHA(256)$/.exec(t))) return { grup: 'KDF', nume: /^HKDF/.test(t) ? 'HKDF' : 'HMAC-PRF', hash: `SHA${m[1]}`, functii: ['keyderive'] };
if ((m = /^ECDSA_P(256|384|521)(?:_IEEE_P1363|_RAW)?$/.exec(t))) return { grup: 'ECDSA', curba: `secp${m[1]}r1`, functii: ['sign', 'verify'] };
if (t === 'ED25519') return { grup: 'EDDSA', nume: 'Ed25519', functii: ['sign', 'verify'] };
if ((m = /^RSA_SSA_(PKCS1|PSS)_(\d{4})_SHA(\d{3})(?:_SHA\d{3}_\d+)?_F4$/.exec(t))) return { grup: 'RSA', param: m[2], primitiv: 'signature', padding: m[1] === 'PSS' ? 'other' : 'pkcs1v15', hash: `SHA${m[3]}`, functii: ['sign', 'verify'] };
if ((m = /^ECIES_P(256|384|521)(?:_COMPRESSED)?_HKDF_HMAC_SHA\d+_AES\d+_(?:GCM|CTR_HMAC_SHA\d+)$/.exec(t))) return { grup: 'ECDH', curba: `secp${m[1]}r1`, primitiv: 'pke', functii: ['encrypt', 'decrypt'], nota: `Tink ECIES (${t}).` };
if ((m = /^DHKEM_(X25519|P256|P384|P521)_HKDF_SHA\d+_HKDF_SHA\d+_(AES_128_GCM|AES_256_GCM|CHACHA20_POLY1305)$/.exec(t))) {
const nota = `Tink HPKE (RFC 9180) with DHKEM ${m[1]}: the key encapsulation is classical.`;
return m[1] === 'X25519' ? { grup: 'XDH', nume: 'X25519', functii: ['encapsulate', 'decapsulate'], nota } : { grup: 'ECDH', curba: `secp${m[1].slice(1)}r1`, primitiv: 'pke', functii: ['encapsulate', 'decapsulate'], nota };
}
return { grup: 'UNKNOWN', nume: `Tink template ${nume}`, motiv: `Tink key template "${nume}" is not in this tool's catalog.` };
}
// Suita de cifruri TLS (OkHttp CipherSuite.TLS_...): schimbul de cheie din nume, sau cifrul pentru suitele TLS 1.3
function suitaTls(nume) {
let m;
if ((m = /^TLS_(ECDHE|ECDH)_/.exec(nume))) return { grup: 'ECDH', nume, functii: ['keygen'], nota: 'TLS cipher suite with (EC)DHE key exchange; the curve is negotiated separately.' };
if ((m = /^TLS_(DHE|DH)_/.exec(nume))) return { grup: 'DH', nume, functii: ['keygen'] };
if (/^TLS_RSA_/.test(nume)) return { grup: 'RSA', nume, primitiv: 'pke', padding: 'pkcs1v15', functii: ['encrypt'], nota: 'TLS cipher suite with RSA key transport (no forward secrecy).' };
if ((m = /^TLS_AES_(128|256)_GCM_SHA\d+$/.exec(nume))) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: 'gcm', functii: ['encrypt'], nota: 'TLS 1.3 cipher suite: the key exchange group is negotiated separately.' };
if (/^TLS_CHACHA20_POLY1305_SHA256$/.test(nume)) return { grup: 'CIPHER', nume: 'ChaCha20-Poly1305', functii: ['encrypt'], nota: 'TLS 1.3 cipher suite: the key exchange group is negotiated separately.' };
if ((m = /^TLS_AES_(128|256)_CCM/.exec(nume))) return { grup: 'CIPHER', nume: 'AES', param: m[1], mod: 'ccm', functii: ['encrypt'] };
return { grup: 'UNKNOWN', nume, motiv: `TLS cipher suite "${nume}" is not in this tool's catalog.` };
}
export function detecteazaJava(ctx) { detecteazaJvm(ctx, false); }
export function detecteazaJvm(ctx, kotlin) {
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]);
let v = valoareArgument(ctx, p[0]);
// Android Keystore: KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore")
const kp = v.fel === 'necunoscut' && /^KeyProperties\s*\.\s*KEY_ALGORITHM_(\w+)$/.exec(v.expresie);
if (kp && KEY_ALGORITHM[kp[1]]) v = { fel: 'rezolvat', valoare: KEY_ALGORITHM[kp[1]], din: `KeyProperties.KEY_ALGORITHM_${kp[1]}` };
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 } : {};
@@ -188,13 +272,39 @@ export function detecteazaJava(ctx) {
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);
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}"` });
const act = activJca(m[1], v.valoare, dupa, variabila);
// Android Keystore: marimea, curba, modul si umplutura vin din KeyGenParameterSpec.Builder, nu din initialize(bits)
if (/^(KeyPairGenerator|KeyGenerator)$/.test(m[1]) && act.grup !== 'UNKNOWN') {
const sp = specAndroidPentru(ctx, a.end, dupa, variabila);
if (sp) {
for (const pz of sp.pozitii) consumate.add(pz);
if (sp.spec.bits && !act.param) act.param = sp.spec.bits;
if (sp.spec.curba && !act.curba && /^(EC|ECDH)$/.test(act.grup)) act.curba = sp.spec.curba;
if (sp.spec.mod && act.grup === 'CIPHER') act.mod = sp.spec.mod;
if (sp.spec.padding && act.grup === 'RSA') { act.padding = sp.spec.padding; act.primitiv = sp.spec.padding === 'oaep' ? 'pke' : 'signature'; }
if (sp.spec.hash && act.grup === 'RSA' && !act.hash) act.hash = sp.spec.hash;
}
}
ctx.adauga(m.index, api, 'call', act, { ...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)) {
// Android Keystore: un KeyGenParameterSpec.Builder neconsumat de un getInstance din acelasi fisier (generatorul e in alta parte);
// inaintea curbelor numite, fiindca ECGenParameterSpec-ul din lantul lui e al lui, nu o a doua curba
for (const m of ctx.potriviri(/(?<![\w.])KeyGenParameterSpec\s*\.\s*Builder\s*\(/g)) {
if (consumate.has(m.index)) continue;
const sp = citesteSpecAndroid(c.slice(m.index));
if (sp.pozCurba !== undefined) consumate.add(m.index + sp.pozCurba);
const act = sp.curba ? { grup: 'EC', curba: sp.curba, functii: ['keygen'] }
: sp.mod ? { grup: 'CIPHER', nume: sp.bits === '168' ? '3DES' : 'AES', param: sp.bits, mod: sp.mod, functii: ['keygen'] }
: sp.padding || (sp.bits && Number(sp.bits) >= 1024) ? { grup: 'RSA', param: sp.bits, padding: sp.padding, primitiv: sp.padding === 'oaep' ? 'pke' : (sp.padding ? 'signature' : undefined), hash: sp.hash, functii: ['keygen'] }
: sp.bits && /^(224|256|384|521)$/.test(sp.bits) ? { grup: 'EC', curba: `secp${sp.bits}r1`, functii: ['keygen'] }
: { grup: 'UNKNOWN', nume: 'KeyGenParameterSpec', motiv: 'Android Keystore key specification: the algorithm is given to the generator (KeyProperties.KEY_ALGORITHM_*), which is not in this file; the spec alone does not say it.' };
ctx.adauga(m.index, 'KeyGenParameterSpec.Builder', 'config', act, { bucata: m[0] });
}
// curbe numite in afara unui KeyPairGenerator (ECGenParameterSpec, ECNamedCurveTable); Kotlin: fara new
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] });
}
@@ -203,8 +313,8 @@ export function detecteazaJava(ctx) {
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)) {
// protocoale TLS activate explicit: Java new String[]{...}, Kotlin arrayOf(...) / listOf(...)
for (const m of ctx.potriviri(/(?<![\w.])set(?:Enabled)?Protocols\s*\(\s*(?:new\s+String\s*\[\s*\]\s*\{|arrayOf\s*\(|listOf\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);
@@ -215,6 +325,39 @@ export function detecteazaJava(ctx) {
ctx.adauga(a.start + x.index, 'setEnabledProtocols', 'config', { grup: 'TLS', param: ver, rol }, { bucata: `"${x[1]}"` });
}
}
// OkHttp (ancorat pe importul okhttp3): ConnectionSpec, versiunile si suitele cerute, pinning
if (/^[ \t]*import\s+okhttp3\./m.test(c)) {
for (const m of ctx.potriviri(/(?<![\w.])ConnectionSpec\s*\.\s*Builder\s*\(\s*ConnectionSpec\s*\.\s*(MODERN_TLS|COMPATIBLE_TLS|RESTRICTED_TLS|CLEARTEXT)\s*\)/g)) {
if (m[1] === 'CLEARTEXT') { ctx.adauga(m.index, 'ConnectionSpec.CLEARTEXT', 'config', { grup: 'UNKNOWN', nume: 'OkHttp CLEARTEXT', motiv: 'OkHttp ConnectionSpec.CLEARTEXT: plain HTTP, no TLS at all on this connection spec.' }, { bucata: m[0] }); continue; }
ctx.adauga(m.index, `ConnectionSpec.${m[1]}`, 'config', { grup: 'TLS', param: 'negotiated', nume: `TLS (negotiated, OkHttp ${m[1]})` }, { bucata: m[0] });
}
for (const m of ctx.potriviri(/\.\s*tlsVersions\s*\(/g)) {
const a = argumente(ctx, m.index + m[0].length);
const vers = [...a.text.matchAll(/TlsVersion\s*\.\s*(TLS_1_0|TLS_1_1|TLS_1_2|TLS_1_3|SSL_3_0)\b/g)];
const map = { TLS_1_0: '1.0', TLS_1_1: '1.1', TLS_1_2: '1.2', TLS_1_3: '1.3', SSL_3_0: 'ssl3' };
const lista = vers.map((x) => map[x[1]]);
for (const x of vers) ctx.adauga(a.start + x.index, 'ConnectionSpec.tlsVersions', 'config', { grup: 'TLS', param: map[x[1]], rol: map[x[1]] === '1.2' && lista.includes('1.3') ? 'min' : 'only' }, { bucata: x[0] });
}
for (const m of ctx.potriviri(/\.\s*cipherSuites\s*\(/g)) {
const a = argumente(ctx, m.index + m[0].length);
for (const x of a.text.matchAll(/CipherSuite\s*\.\s*(TLS_[A-Z0-9_]+)\b/g)) ctx.adauga(a.start + x.index, 'ConnectionSpec.cipherSuites', 'config', suitaTls(x[1]), { bucata: x[0] });
}
for (const m of ctx.potriviri(/(?<![\w.])CertificatePinner\s*\.\s*Builder\s*\(/g)) ctx.adauga(m.index, 'CertificatePinner.Builder', 'config', { grup: 'PKI-VERIFY', nume: 'certificate pinning (SPKI)', nota: 'OkHttp CertificatePinner: the pinned SPKI hashes name classical (RSA or EC) keys.' }, { bucata: m[0] });
}
// verificarea lanturilor de certificate (lantul de incredere TLS al clientului): autentificare, nu schimb de cheie
for (const m of ctx.potriviri(/(?<![\w.])(CertPathValidator|TrustManagerFactory)\s*\.\s*getInstance\s*\(/g)) ctx.adauga(m.index, `${m[1]}.getInstance`, 'call', { grup: 'PKI-VERIFY', nume: NUME_PKIX, functii: ['verify'] }, { bucata: m[0] });
for (const m of ctx.potriviri(/(?<![\w.])KeyStore\s*\.\s*getInstance\s*\(\s*"AndroidCAStore"\s*\)/g)) ctx.adauga(m.index, 'KeyStore.getInstance("AndroidCAStore")', 'call', { grup: 'PKI-VERIFY', nume: NUME_PKIX, nota: 'The Android system CA store (the client\'s TLS trust anchors).' }, { bucata: m[0] });
for (const m of ctx.potriviri(/(?<![\w.])X509TrustManager\b(?!\s*\.)/g)) ctx.adauga(m.index, 'X509TrustManager', 'config', { grup: 'PKI-VERIFY', nume: NUME_PKIX, functii: ['verify'] }, { bucata: c.slice(Math.max(0, m.index - 12), m.index + 16) });
for (const m of ctx.potriviri(/\.\s*checkServerTrusted\s*\(/g)) ctx.adauga(m.index, 'checkServerTrusted', 'call', { grup: 'PKI-VERIFY', nume: NUME_PKIX, functii: ['verify'] }, { bucata: m[0].trim() });
// Tink (ancorat pe importul com.google.crypto.tink): sablonul de cheie spune algoritmul; inregistrarea primitivelor e dovada slaba
const tink = ctx.potriviri(/^[ \t]*import\s+com\.google\.crypto\.tink\.[\w.*]+/gm);
if (tink.length) {
const TINK_SUPRIMA = ['CIPHER', 'MAC', 'KDF', 'ECDSA', 'EDDSA', 'RSA', 'XDH', 'ECDH', 'MLDSA', 'MLKEM', 'SLHDSA', 'UNKNOWN'];
for (const m of ctx.potriviri(/(?<![\w.])KeyTemplates\s*\.\s*get\s*\(\s*"([^"]+)"\s*\)/g)) ctx.adauga(m.index, 'KeyTemplates.get', 'call', sablonTink(m[1]), { bucata: m[0] });
for (const m of ctx.potriviri(/(?<![\w.])(?:(?:Aead|Mac|Signature|Hybrid|Daead|StreamingAead|Prf)KeyTemplates|Predefined(?:Aead|Mac|Signature|Hybrid|Daead|StreamingAead|Prf)Parameters)\s*\.\s*([A-Z][A-Z0-9_]+)\b/g)) ctx.adauga(m.index, m[0].replace(/\s+/g, ''), 'call', sablonTink(m[1]), { bucata: m[0] });
for (const m of ctx.potriviri(/(?<![\w.])(Aead|Mac|Signature|Hybrid|Daead|StreamingAead|Prf|Tink)Config\s*\.\s*register\s*\(/g)) ctx.adauga(m.index, `${m[1]}Config.register`, 'import', { grup: 'LIB-MULTI', nume: `Tink ${m[1]} primitives`, motiv: `Tink ${m[1]}Config.register(): registers the primitive family; the algorithm is chosen by the key template, not visible in this file.` }, { suprimaDe: TINK_SUPRIMA, bucata: m[0] });
ctx.adauga(tink[0].index + tink[0][0].indexOf('import'), 'import com.google.crypto.tink', 'import', { grup: 'LIB-MULTI', nume: 'Tink (com.google.crypto.tink)', motiv: 'Imports Tink (AEAD, MAC, signatures, hybrid encryption, HPKE); no key template recognized in this file.' }, { suprimaDe: [...TINK_SUPRIMA, 'LIB-MULTI'], bucata: tink[0][0].trim() });
}
// 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;
@@ -228,8 +371,8 @@ export function detecteazaJava(ctx) {
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)) {
// Bouncy Castle: importurile (Java cu `;`, Kotlin fara) 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]}` });
}
@@ -237,7 +380,7 @@ export function detecteazaJava(ctx) {
const act = bcParam(m[1], m[2]);
if (act) ctx.adauga(m.index, `${m[1]}Parameters.${m[2]}`, 'call', act, { bucata: m[0] });
}
detecteazaLibsignalJava(ctx);
detecteazaLibsignalJvm(ctx, kotlin);
void curbaCanonica;
}
@@ -253,23 +396,43 @@ export function kemLibsignal(tip) {
if ((k = /^ML_?KEM_?(768|1024)$/i.exec(tip || ''))) return { grup: 'MLKEM', param: k[1], functii: ['keygen'] };
return { grup: 'UNKNOWN', nume: 'libsignal KEM key pair', motiv: 'libsignal KEMKeyPair: the key type (Kyber-1024 or ML-KEM) is the KEMKeyType argument, not visible on this line.' };
}
function detecteazaLibsignalJava(ctx) {
export const LIBSIGNAL_NOTA_KYBER_PK = 'libsignal KEMPublicKey: a Kyber-1024 public key (a PQXDH prekey), built from bytes or from a native handle.';
export const LIBSIGNAL_NOTA_SEALED = 'libsignal sealed sender: the envelope key is derived with X25519 (an ephemeral key against the recipient\'s identity key) and HKDF-SHA-256, then AES-256-CTR with HMAC-SHA-256 (v1) or AES-256-GCM-SIV (v2); the session inside is X3DH/PQXDH.';
// 0.4.0 (2026-10-18): Kotlin are aceleasi clase fara `new` (Aes256GcmSiv(key), PreKeyBundle(...)); forma fara `new` nu se admite in Java,
// unde ar prinde declaratii. In Kotlin, `class X(` / `fun X(` / `constructor(` sunt declaratii, nu apeluri. Si puntea nativa a bibliotecii
// insesi (Native.ECPrivateKey_Generate(), Native.KyberKeyPair_Generate(), Native.HKDF_DeriveSecrets(...)): asa isi fac cheile sursele
// Kotlin si Java din org.signal.libsignal (ECKeyPair.kt, KEMKeyPair.kt, HKDF.kt), unde ancora e pachetul.
function detecteazaLibsignalJvm(ctx, kotlin) {
const ancore = ctx.potriviri(/(?<![\w.])(?:import\s+(?:static\s+)?org\.signal\.libsignal\.(?:protocol|crypto|metadata|zkgroup|svr|keytrans)\b|package\s+org\.signal\.libsignal\b)/g);
if (!ancore.length) return;
for (const m of ctx.potriviri(/(?<![\w.])(?:(ECKeyPair|IdentityKeyPair|ECPrivateKey)\s*\.\s*generate|(Curve)\s*\.\s*generateKeyPair)\s*\(/g)) {
ctx.adauga(m.index, `${m[1] || m[2]}.${m[1] ? 'generate' : 'generateKeyPair'}`, 'call', { grup: 'XDH', nume: 'X25519', functii: ['keygen'], nota: LIBSIGNAL_NOTA_EC }, { bucata: m[0] });
const ctor = kotlin ? '(?<![\\w.])(?<!class\\s)(?<!fun\\s)(?<!constructor\\s)(?<!object\\s)' : '(?<![\\w.])new\\s+';
const api = (nume) => (kotlin ? nume : `new ${nume}`);
for (const m of ctx.potriviri(/(?<![\w.])(?:(ECKeyPair|IdentityKeyPair|ECPrivateKey)\s*\.\s*generate|(Curve)\s*\.\s*generateKeyPair|Native\s*\.\s*(ECPrivateKey_Generate|IdentityKeyPair_Generate))\s*\(/g)) {
const nume = m[3] ? `Native.${m[3]}` : `${m[1] || m[2]}.${m[1] ? 'generate' : 'generateKeyPair'}`;
ctx.adauga(m.index, nume, 'call', { grup: 'XDH', nume: 'X25519', functii: ['keygen'], nota: LIBSIGNAL_NOTA_EC }, { bucata: m[0] });
}
for (const m of ctx.potriviri(/(?<![\w.])KEMKeyPair\s*\.\s*generate\s*\(\s*(?:KEMKeyType\s*\.\s*([A-Za-z_0-9]+))?/g)) {
ctx.adauga(m.index, 'KEMKeyPair.generate', 'call', kemLibsignal(m[1]), { bucata: m[0].trim() });
}
for (const m of ctx.potriviri(/(?<![\w.])HKDF\s*\.\s*deriveSecrets\s*\(/g)) {
ctx.adauga(m.index, 'HKDF.deriveSecrets', 'call', { grup: 'KDF', nume: 'HKDF', hash: 'SHA256', functii: ['keyderive'], nota: 'libsignal HKDF is HKDF-SHA-256.' }, { bucata: m[0] });
for (const m of ctx.potriviri(/(?<![\w.])Native\s*\.\s*KyberKeyPair_Generate\s*\(/g)) ctx.adauga(m.index, 'Native.KyberKeyPair_Generate', 'call', kemLibsignal('KYBER_1024'), { bucata: m[0] });
// puntea cheii Curve25519 (ECPrivateKey.kt / ECPrivateKey.java): acordul X25519, semnatura XEdDSA pe aceeasi cheie, HPKE (RFC 9180, DHKEM X25519)
for (const m of ctx.potriviri(/(?<![\w.])Native\s*\.\s*(ECPrivateKey_Agree|ECPrivateKey_Sign|ECPublicKey_Verify|ECPrivateKey_HpkeOpen|ECPublicKey_HpkeSeal)\s*\(/g)) {
const act = m[1] === 'ECPrivateKey_Agree' ? { grup: 'XDH', nume: 'X25519', functii: ['other'], nota: 'libsignal Diffie-Hellman agreement over Curve25519.' }
: /Hpke/.test(m[1]) ? { grup: 'XDH', nume: 'X25519', primitiv: 'pke', functii: [m[1] === 'ECPublicKey_HpkeSeal' ? 'encrypt' : 'decrypt'], nota: 'libsignal HPKE (RFC 9180) with DHKEM X25519: the key encapsulation is classical.' }
: { grup: 'EDDSA', nume: 'XEdDSA (Curve25519)', functii: [m[1] === 'ECPublicKey_Verify' ? 'verify' : 'sign'], nota: 'libsignal XEdDSA: an Ed25519-style signature made with the Curve25519 (X25519) identity key.' };
ctx.adauga(m.index, `Native.${m[1]}`, 'call', act, { bucata: m[0] });
}
for (const m of ctx.potriviri(/(?<![\w.])new\s+(Aes256GcmSiv|Aes256GcmEncryption|Aes256GcmDecryption|Aes256Ctr32)\s*\(/g)) {
const mod = m[1] === 'Aes256GcmSiv' ? 'gcm-siv' : m[1] === 'Aes256Ctr32' ? 'ctr' : 'gcm';
ctx.adauga(m.index, `new ${m[1]}`, 'call', { grup: 'CIPHER', nume: 'AES', param: '256', mod, functii: [m[1] === 'Aes256GcmDecryption' ? 'decrypt' : 'encrypt'] }, { bucata: m[0] });
for (const m of ctx.potriviri(new RegExp(`${ctor}KEMPublicKey\\s*\\(`, 'g'))) ctx.adauga(m.index, api('KEMPublicKey'), 'call', { grup: 'PREPQ', nume: 'Kyber-1024', primitiv: 'kem', functii: ['other'], nota: LIBSIGNAL_NOTA_KYBER_PK }, { bucata: m[0] });
for (const m of ctx.potriviri(/(?<![\w.])(?:HKDF\s*\.\s*deriveSecrets|Native\s*\.\s*HKDF_DeriveSecrets)\s*\(/g)) {
ctx.adauga(m.index, /Native/.test(m[0]) ? 'Native.HKDF_DeriveSecrets' : 'HKDF.deriveSecrets', 'call', { grup: 'KDF', nume: 'HKDF', hash: 'SHA256', functii: ['keyderive'], nota: 'libsignal HKDF is HKDF-SHA-256.' }, { bucata: m[0] });
}
for (const m of ctx.potriviri(/(?<![\w.])new\s+PreKeyBundle\s*\(/g)) ctx.adauga(m.index, 'new PreKeyBundle', 'call', LIBSIGNAL_BUNDLE, { bucata: m[0] });
for (const m of ctx.potriviri(new RegExp(`${ctor}(Aes256GcmSiv|Aes256GcmEncryption|Aes256GcmDecryption|Aes256Ctr32)\\s*\\(|(?<![\\w.])Native\\s*\\.\\s*(Aes256GcmSiv|Aes256GcmEncryption|Aes256GcmDecryption|Aes256Ctr32)_New\\s*\\(`, 'g'))) {
const tip = m[1] || m[2];
const mod = tip === 'Aes256GcmSiv' ? 'gcm-siv' : tip === 'Aes256Ctr32' ? 'ctr' : 'gcm';
ctx.adauga(m.index, m[2] ? `Native.${m[2]}_New` : api(tip), 'call', { grup: 'CIPHER', nume: 'AES', param: '256', mod, functii: [tip === 'Aes256GcmDecryption' ? 'decrypt' : 'encrypt'] }, { bucata: m[0] });
}
for (const m of ctx.potriviri(new RegExp(`${ctor}PreKeyBundle\\s*\\(|(?<![\\w.])Native\\s*\\.\\s*(?:PreKeyBundle_New|SessionBuilder_ProcessPreKeyBundle)\\s*\\(`, 'g'))) ctx.adauga(m.index, /Native/.test(m[0]) ? m[0].replace(/\s+|\($/g, '') : api('PreKeyBundle'), 'call', LIBSIGNAL_BUNDLE, { bucata: m[0] });
for (const m of ctx.potriviri(new RegExp(`${ctor}SealedSessionCipher\\s*\\(`, 'g'))) ctx.adauga(m.index, api('SealedSessionCipher'), 'call', { grup: 'XDH', nume: 'X25519', functii: ['keygen'], nota: LIBSIGNAL_NOTA_SEALED }, { bucata: m[0] });
// importul: dovada slaba, o singura data pe fisier, suprimata de orice apel recunoscut de mai sus
const imp = ancore.find((a) => /^import/.test(a[0]));
if (imp) ctx.adauga(imp.index, 'import org.signal.libsignal', 'import', { grup: 'LIB-MULTI', nume: 'libsignal (org.signal.libsignal)', motiv: 'Imports the Signal protocol library (Curve25519 identity and prekeys, Kyber prekeys (PQXDH), Double Ratchet, SPQR, sealed sender); no specific call recognized in this file.' }, { suprimaDe: ['XDH', 'PREPQ', 'MLKEM', 'KDF', 'CIPHER', 'LIB-MULTI'], bucata: imp[0].trim() });