identity: the SD-JWT form of an AERE hybrid identity is judged on its ML-DSA-65 half by default, the Ed25519 half only on explicit request, and every verdict says postQuantum (rule sdjwt.hybrid-half); pq-pki: the signature algorithm a certificate or a revocation list declares must be its issuer key's (ALG_KEY, CRL_ALG_KEY); agents: an equivocation proof requires the agent's ML-DSA-65 key; pq-gateway: the status names the path to the backend and hybrid-only refuses a plain-HTTP backend; conformance 241 rules, 631 vectors

This commit is contained in:
Aere Network committed 2026-10-19 05:19:21 +03:00
1 parent 3ff21f7758
commit 0c8f1de3fd
122 files changed
+1205 -262

No files matched your search

+120 -25
View File
@@ -57,32 +57,38 @@ test('RFC 9901: o dezvaluire scoasa din prezentare dupa semnarea KB (sd_hash) sa
});
// ---------------------------------------------------------------- jetoane emise cu chei AERE
// 2026-10-19 (AIP-25 C1, D-535): cu chei AERE (Ed25519 SI ML-DSA-65), emiterea, cnf-ul detinatorului si verificarea folosesc IMPLICIT jumatatea
// post-cuantica, ML-DSA-65; jumatatea Ed25519 numai la cerere (classicalHalf), cu verdictul postQuantum false. Probele de mai jos o masoara
// pe calea implicita; jetoanele facute de mana sunt semnate cu jumatatea ML-DSA-65 (cu cea Ed25519 singura ar cadea pe hybrid-half inaintea
// regulii pe care o masoara proba).
const NOW = Math.floor(Date.parse('2026-09-30T08:00:00Z') / 1000), LA = new Date(NOW * 1000);
const iss = I.generateKeys(), hol = I.generateKeys(), strain = I.generateKeys();
const PQ = 'ML-DSA-65', EM = iss.privat.mldsa65, HM = hol.privat.mldsa65;
const CL = { given_name: 'Ana', family_name: 'Pop', age_over_18: true, nationalities: ['RO', 'DE'], employer: 'Example Ltd' };
const emite = (o = {}) => J.issueSdJwt({ issuer: iss, holder: hol, claims: CL, disclosable: ['given_name', 'family_name', 'age_over_18'], arrayDisclosable: ['nationalities'], iss: 'https://issuer.aere.example', iat: NOW, exp: NOW + 86400, decoys: 2, ...o });
const AUD = 'https://verifier.example', NONCE = 'n-' + crypto.randomBytes(4).toString('hex');
const arata = (sd, reveal, o = {}) => J.presentSdJwt({ sdJwt: sd, reveal, holder: hol, audience: AUD, nonce: NONCE, iat: NOW, ...o });
const judeca = (p, o = {}) => J.verifySdJwt(p, { issuerKey: iss, audience: AUD, nonce: NONCE, now: LA, ...o });
const E = emite();
test('AERE -> SD-JWT (EdDSA, cheia Ed25519 a emitentului AERE): detinatorul arata age_over_18 si RO, verificatorul vede numai atat plus afirmatiile in clar', () => {
test('AERE -> SD-JWT (implicit ML-DSA-65, jumatatea post-cuantica a emitentului AERE): detinatorul arata age_over_18 si RO, verificatorul vede numai atat plus afirmatiile in clar, postQuantum true', () => {
const r = judeca(arata(E.sdJwt, ['age_over_18', { element: 'RO', in: 'nationalities' }]));
cere(r.valid, r.reason);
cere(r.payload.age_over_18 === true && !('given_name' in r.payload) && egal(r.payload.nationalities, ['RO']) && r.payload.employer === 'Example Ltd' && r.alg === 'EdDSA', JSON.stringify(r.payload));
cere(r.payload.age_over_18 === true && !('given_name' in r.payload) && egal(r.payload.nationalities, ['RO']) && r.payload.employer === 'Example Ltd' && r.alg === PQ && r.postQuantum === true, JSON.stringify(r.payload) + ' alg=' + r.alg + ' pq=' + r.postQuantum);
});
test('AERE -> SD-JWT in forma standardului: typ explicit, _sd sortat (cu momeli), _sd_alg sha-256, cnf.jwk = cheia Ed25519 a detinatorului, dezvaluiri [sare, nume, valoare]', () => {
test('AERE -> SD-JWT in forma standardului: typ explicit, _sd sortat (cu momeli), _sd_alg sha-256, cnf.jwk = cheia ML-DSA-65 (AKP, RFC 9964) a detinatorului, dezvaluiri [sare, nume, valoare]', () => {
const [h, p] = E.sdJwt.split('~')[0].split('.').slice(0, 2).map((x) => JSON.parse(Buffer.from(x, 'base64url').toString('utf8')));
cere(h.alg === 'EdDSA' && h.typ === J.TYP, JSON.stringify(h));
cere(h.alg === PQ && h.typ === J.TYP, JSON.stringify(h));
cere(Array.isArray(p._sd) && p._sd.length === 5 && egal([...p._sd].sort(), p._sd) && p._sd_alg === 'sha-256', JSON.stringify(p._sd));
cere(p.cnf.jwk.kty === 'OKP' && p.cnf.jwk.crv === 'Ed25519' && p.cnf.jwk.x === J.publicJwk(hol).x && !('d' in p.cnf.jwk), JSON.stringify(p.cnf));
cere(p.cnf.jwk.kty === 'AKP' && p.cnf.jwk.alg === PQ && p.cnf.jwk.pub === J.publicJwk(hol).pub && !('priv' in p.cnf.jwk), JSON.stringify(p.cnf).slice(0, 120));
const c = JSON.parse(Buffer.from(E.disclosures[0].disclosure, 'base64url').toString('utf8'));
cere(c.length === 3 && typeof c[0] === 'string' && c[0].length >= 22 && c[1] === 'given_name', JSON.stringify(c));
});
test('AERE -> SD-JWT post-cuantic (ML-DSA-65, cheia AKP): verifica cu cheia ML-DSA-65 a emitentului, nu cu cea Ed25519; detinatorul poate semna KB tot cu ML-DSA-65', () => {
const M = emite({ alg: 'ML-DSA-65', holderAlg: 'ML-DSA-65' });
const p = arata(M.sdJwt, ['age_over_18'], { holderAlg: 'ML-DSA-65' });
const r = judeca(p, { issuerAlg: 'ML-DSA-65' }); cere(r.valid && r.alg === 'ML-DSA-65' && r.keyBinding.alg === 'ML-DSA-65', r.reason);
cere(refuz(judeca(p, { issuerAlg: 'EdDSA' }), /does not match/), 'cheia Ed25519 a primit un jeton ML-DSA-65');
test('AERE -> SD-JWT post-cuantic numit explicit (alg ML-DSA-65): verifica cu jumatatea ML-DSA-65 a emitentului; issuerAlg EdDSA fara classicalHalf -> hybrid-half; cu classicalHalf, cheia Ed25519 nu primeste un jeton ML-DSA-65', () => {
const M = emite({ alg: PQ, holderAlg: PQ });
const p = arata(M.sdJwt, ['age_over_18'], { holderAlg: PQ });
const r = judeca(p, { issuerAlg: PQ }); cere(r.valid && r.alg === PQ && r.keyBinding.alg === PQ && r.postQuantum === true, r.reason);
const e = judeca(p, { issuerAlg: 'EdDSA' }); cere(!e.valid && e.rule === 'hybrid-half', 'issuerAlg EdDSA fara classicalHalf: ' + (e.valid ? 'VALID' : e.rule + ' ' + e.reason));
cere(refuz(judeca(p, { issuerAlg: 'EdDSA', classicalHalf: true }), /does not match/), 'cheia Ed25519 a primit un jeton ML-DSA-65');
});
// ---------------------------------------------------------------- atacuri
@@ -105,14 +111,14 @@ test('ATAC: o dezvaluire cu valoarea schimbata (alt digest, nelegata) -> refuzat
});
test('ATAC: un emitent care pune in _sd o dezvaluire cu numele "_sd", una care acopera o afirmatie in clar, sau acelasi digest de doua ori -> refuzat', () => {
const faci = (dezvaluiri, extra = {}) => { const ds = dezvaluiri.map((x) => b64u(JSON.stringify(x))); const sd = ds.map((d) => crypto.createHash('sha256').update(d).digest('base64url'));
return jwtDin({ alg: 'EdDSA', typ: J.TYP }, { _sd: extra.dublu ? [...sd, sd[0]] : sd, _sd_alg: 'sha-256', iss: 'x', ...(extra.clar || {}) }, iss.privat.ed25519) + '~' + ds.map((d) => d + '~').join(''); };
return jwtDin({ alg: PQ, typ: J.TYP }, { _sd: extra.dublu ? [...sd, sd[0]] : sd, _sd_alg: 'sha-256', iss: 'x', ...(extra.clar || {}) }, EM) + '~' + ds.map((d) => d + '~').join(''); };
cere(refuz(J.verifySdJwt(faci([['s1', '_sd', ['x']]]), { issuerKey: iss, requireKeyBinding: false, now: LA }), /may not name _sd/), '_sd a trecut');
cere(refuz(J.verifySdJwt(faci([['s1', 'role', 'admin']], { clar: { role: 'user' } }), { issuerKey: iss, requireKeyBinding: false, now: LA }), /already exists/), 'coliziunea a trecut');
cere(refuz(J.verifySdJwt(faci([['s1', 'a', 1]], { dublu: true }), { issuerKey: iss, requireKeyBinding: false, now: LA }), /more than once/), 'digestul dublu a trecut');
cere(refuz(J.verifySdJwt(faci([['s1', '__proto__', { admin: true }]]), { issuerKey: iss, requireKeyBinding: false, now: LA }), /may not name __proto__/), '__proto__ a trecut');
});
test('ATAC: o afirmatie numita __proto__ in clar sau intr-o valoare dezvaluita (prototipul payload-ului schimbat) -> refuzata', () => {
const cap = (corp) => jwtDin({ alg: 'EdDSA', typ: J.TYP }, corp, iss.privat.ed25519);
const cap = (corp) => jwtDin({ alg: PQ, typ: J.TYP }, corp, EM);
const clar = JSON.parse('{"iss":"x","_sd_alg":"sha-256","__proto__":{"admin":true}}');
cere(refuz(J.verifySdJwt(cap(clar) + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /__proto__/), 'in clar a trecut');
const d = b64u('["s1","profil",' + '{"__proto__":{"admin":true}}' + ']');
@@ -124,9 +130,9 @@ test('ATAC: KB semnat de alta cheie decat cnf.jwk, KB cu typ gresit, KB fara non
cere(refuz(judeca(alt), /key binding: the signature does not verify/), 'cheia straina a trecut');
const baza = arata(E.sdJwt, ['age_over_18']).split('~').slice(0, -1).join('~') + '~';
const sh = crypto.createHash('sha256').update(Buffer.from(baza, 'ascii')).digest('base64url');
const tip = jwtDin({ alg: 'EdDSA', typ: 'JWT' }, { iat: NOW, aud: AUD, nonce: NONCE, sd_hash: sh }, hol.privat.ed25519);
const tip = jwtDin({ alg: PQ, typ: 'JWT' }, { iat: NOW, aud: AUD, nonce: NONCE, sd_hash: sh }, HM);
cere(refuz(judeca(baza + tip), /typ/), 'typ gresit a trecut');
const faraNonce = jwtDin({ alg: 'EdDSA', typ: 'kb+jwt' }, { iat: NOW, aud: AUD, sd_hash: sh }, hol.privat.ed25519);
const faraNonce = jwtDin({ alg: PQ, typ: 'kb+jwt' }, { iat: NOW, aud: AUD, sd_hash: sh }, HM);
cere(refuz(judeca(baza + faraNonce), /required/), 'fara nonce a trecut');
});
test('timpul: exp trecut -> refuzat (fara toleranta); nbf peste 30 s -> admis (toleranta de ceas), peste 90 s -> refuzat', () => {
@@ -154,24 +160,27 @@ test('REVIZUIRE: prezentarea arata elementul NUMAI din lista numita (acelasi RO
let m = null; try { arata(D.sdJwt, [{ element: 'IT', in: 'nationalities' }]); } catch (e) { m = e.message; } cere(/nothing to reveal/.test(m || ''), 'alegerea fara corespondent a tacut');
});
test('REVIZUIRE: cheia emitentului data ca KeyObject public sau ca identitate AERE publica (keys.public) e primita; detinatorul dat ca cheie publica', () => {
const pubKO = crypto.createPublicKey(iss.privat.ed25519);
const pubKO = crypto.createPublicKey(EM);
const D = J.issueSdJwt({ issuer: iss, holder: crypto.createPublicKey(hol.privat.ed25519), claims: { age_over_18: true }, disclosable: ['age_over_18'], iss: 'x', iat: NOW });
const p = arata(D.sdJwt, ['age_over_18']);
cere(J.verifySdJwt(p, { issuerKey: pubKO, audience: AUD, nonce: NONCE, now: LA }).valid, 'KeyObject public refuzat');
cere(J.verifySdJwt(p, { issuerKey: iss.public, audience: AUD, nonce: NONCE, now: LA }).valid, 'identitatea AERE publica refuzata');
const r = J.verifySdJwt(p, { issuerKey: iss.public, audience: AUD, nonce: NONCE, now: LA });
cere(r.valid, 'identitatea AERE publica refuzata');
// detinatorul legat cu o cheie Ed25519 (KeyObject dat la emitere): Key Binding-ul e clasic, deci verdictul nu e post-cuantic (AIP-25 C1)
cere(r.keyBinding && r.keyBinding.alg === 'EdDSA' && r.postQuantum === false, 'KB Ed25519: ' + JSON.stringify(r.keyBinding) + ' pq=' + r.postQuantum);
});
test('REVIZUIRE: exp cerut implicit (un exp dezvaluibil si retinut de detinator nu mai lasa jetonul fara expirare); requireExp:false il lasa', () => {
const dExp = b64u(JSON.stringify(['s', 'exp', NOW - 10]));
const corp = { _sd: [crypto.createHash('sha256').update(dExp).digest('base64url')], _sd_alg: 'sha-256', iss: 'x' };
const jwt = jwtDin({ alg: 'EdDSA', typ: J.TYP }, corp, iss.privat.ed25519);
const jwt = jwtDin({ alg: PQ, typ: J.TYP }, corp, EM);
cere(refuz(J.verifySdJwt(jwt + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /exp is required/), 'fara exp a trecut');
cere(refuz(J.verifySdJwt(jwt + '~' + dExp + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /expired/), 'exp dezvaluit si trecut a trecut');
cere(J.verifySdJwt(jwt + '~', { issuerKey: iss, requireKeyBinding: false, requireExp: false, now: LA }).valid, 'requireExp:false a refuzat');
});
test('REVIZUIRE: tipul jetonului: un KB-JWT sau un JWT cu typ JWT dat drept jeton de emitent -> refuzat implicit; expectedIssuer altul -> refuzat', () => {
const corp = { iss: 'https://issuer.aere.example', exp: NOW + 100, _sd_alg: 'sha-256' };
cere(refuz(J.verifySdJwt(jwtDin({ alg: 'EdDSA', typ: 'kb+jwt' }, corp, iss.privat.ed25519) + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /typ/), 'kb+jwt drept emitent a trecut');
const simplu = jwtDin({ alg: 'EdDSA', typ: 'JWT' }, corp, iss.privat.ed25519) + '~';
cere(refuz(J.verifySdJwt(jwtDin({ alg: PQ, typ: 'kb+jwt' }, corp, EM) + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /typ/), 'kb+jwt drept emitent a trecut');
const simplu = jwtDin({ alg: PQ, typ: 'JWT' }, corp, EM) + '~';
cere(refuz(J.verifySdJwt(simplu, { issuerKey: iss, requireKeyBinding: false, now: LA }), /typ/), 'typ JWT a trecut implicit');
cere(J.verifySdJwt(simplu, { issuerKey: iss, requireKeyBinding: false, expectedTyp: null, now: LA }).valid, 'expectedTyp:null a refuzat un JWT simplu');
const bun = J.verifySdJwt(arata(E.sdJwt, ['age_over_18']), { issuerKey: iss, audience: AUD, nonce: NONCE, now: LA, expectedIssuer: 'https://issuer.aere.example' });
@@ -180,22 +189,24 @@ test('REVIZUIRE: tipul jetonului: un KB-JWT sau un JWT cu typ JWT dat drept jeto
});
test('REVIZUIRE: crit (orice forma), base64url necanonic al semnaturii, KB expirat sau inca nevalabil (nbf) -> refuzate', () => {
const corp = { iss: 'x', exp: NOW + 100, _sd_alg: 'sha-256', meta: { note: 1 } };
for (const crit of [[], 'b64', ['b64']]) cere(refuz(J.verifySdJwt(jwtDin({ alg: 'EdDSA', typ: J.TYP, crit, b64: false }, corp, iss.privat.ed25519) + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /crit/), 'crit ' + JSON.stringify(crit));
for (const crit of [[], 'b64', ['b64']]) cere(refuz(J.verifySdJwt(jwtDin({ alg: PQ, typ: J.TYP, crit, b64: false }, corp, EM) + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /crit/), 'crit ' + JSON.stringify(crit));
// base64url necanonic: o semnatura Ed25519 (64 de octeti) are doi biti nefolositi in ultimul caracter; una ML-DSA-65 (3309 = 3 x 1103
// octeti) nu are niciunul, deci proba ramane pe jumatatea clasica, ceruta explicit (AIP-25 C1)
const j = jwtDin({ alg: 'EdDSA', typ: J.TYP }, corp, iss.privat.ed25519);
const r = J.verifySdJwt(j + '~', { issuerKey: iss, requireKeyBinding: false, now: LA });
const r = J.verifySdJwt(j + '~', { issuerKey: iss, classicalHalf: true, requireKeyBinding: false, now: LA });
cere(r.valid && r.payload.meta.note === 1 && !('_sd_alg' in r.payload), 'martorul: ' + JSON.stringify(r.payload));
const ALF = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
const u = j[j.length - 1], alt = ALF[ALF.indexOf(u) ^ 1];
const nec = j.slice(0, -1) + alt;
cere(Buffer.from(nec.split('.')[2], 'base64url').equals(Buffer.from(j.split('.')[2], 'base64url')), 'proba: scrierea alternativa trebuie sa dea aceiasi octeti');
cere(refuz(J.verifySdJwt(nec + '~', { issuerKey: iss, requireKeyBinding: false, now: LA }), /canonical/), 'semnatura necanonica a trecut');
cere(refuz(J.verifySdJwt(nec + '~', { issuerKey: iss, classicalHalf: true, requireKeyBinding: false, now: LA }), /canonical/), 'semnatura necanonica a trecut');
const baza = arata(E.sdJwt, ['age_over_18']).split('~').slice(0, -1).join('~') + '~';
const sh = crypto.createHash('sha256').update(Buffer.from(baza, 'ascii')).digest('base64url');
const kbExp = jwtDin({ alg: 'EdDSA', typ: 'kb+jwt' }, { iat: NOW, aud: AUD, nonce: NONCE, sd_hash: sh, exp: NOW - 100 }, hol.privat.ed25519);
const kbExp = jwtDin({ alg: PQ, typ: 'kb+jwt' }, { iat: NOW, aud: AUD, nonce: NONCE, sd_hash: sh, exp: NOW - 100 }, HM);
cere(refuz(judeca(baza + kbExp), /key binding: expired/), 'KB expirat a trecut');
// 2026-10-01: nbf-ul KB (a doua jumatate a reparatiei "exp/nbf ale KB" din B-32) nu avea proba: paznicul scos lasa proba 23/23.
// Martorul aceluiasi KB facut de mana, cu nbf in toleranta de ceas, e VALID: refuzul de mai jos e al nbf-ului, nu al constructiei.
const kbNbf = (nbf) => jwtDin({ alg: 'EdDSA', typ: 'kb+jwt' }, { iat: NOW, aud: AUD, nonce: NONCE, sd_hash: sh, nbf }, hol.privat.ed25519);
const kbNbf = (nbf) => jwtDin({ alg: PQ, typ: 'kb+jwt' }, { iat: NOW, aud: AUD, nonce: NONCE, sd_hash: sh, nbf }, HM);
const rNbfBun = judeca(baza + kbNbf(NOW + 30));
cere(rNbfBun.valid, 'martorul: KB cu nbf in toleranta (30 s) trebuie sa fie VALID: ' + rNbfBun.reason);
cere(refuz(judeca(baza + kbNbf(NOW + 600)), /key binding: not valid before/), 'KB inca nevalabil (nbf peste 10 minute) a trecut');
@@ -205,7 +216,7 @@ test('REVIZUIRE: crit (orice forma), base64url necanonic al semnaturii, KB expir
// 2026-10-18, din interoperarea cu @sd-jwt/core 0.23.0 (care refuza _sd_alg imbricat): RFC 9901 s.4.1.1 "It MUST NOT be used in any object
// nested within the payload"; pana atunci il pastram ca afirmatie. Si RFC 7515 s.4.1.9: typ-ul "application/x+sd-jwt" e acelasi cu "x+sd-jwt".
test('INTEROP: _sd_alg imbricat (in clar sau intr-o valoare dezvaluita) -> refuzat (RFC 9901 s.4.1.1); o dezvaluire numita _sd_alg -> refuzata; typ cu prefixul application/ -> acelasi tip (RFC 7515 s.4.1.9)', () => {
const cap = (corp, typ = J.TYP) => jwtDin({ alg: 'EdDSA', typ }, corp, iss.privat.ed25519);
const cap = (corp, typ = J.TYP) => jwtDin({ alg: PQ, typ }, corp, EM);
const o = { issuerKey: iss, requireKeyBinding: false, now: LA };
cere(refuz(J.verifySdJwt(cap({ iss: 'x', exp: NOW + 100, _sd_alg: 'sha-256', meta: { _sd_alg: 'x', note: 1 } }) + '~', o), /_sd_alg below the top level/), 'imbricat in clar a trecut');
const dIm = b64u(JSON.stringify(['s1', 'meta', { _sd_alg: 'sha-256', note: 1 }])), dgIm = crypto.createHash('sha256').update(dIm).digest('base64url');
@@ -234,6 +245,8 @@ test(`INTEROP (fixturi @sd-jwt/core ${F2 ? F2.biblioteci['@sd-jwt/core'] : '?'}
cere(r.valid, c.nume + ': ' + r.reason);
cere(egal(r.payload, c.asteptat.payload) && egal([...r.disclosed].sort(), c.asteptat.disclosed), c.nume + ': rezultatul difera de cel inregistrat: ' + JSON.stringify(r.payload).slice(0, 200));
cere(r.payload.address && r.payload.address.locality === 'Cluj' && !('street_address' in r.payload.address) && egal(r.payload.nationalities, ['RO']) && !('given_name' in r.payload), c.nume + ': dezvaluirile imbricate/de lista nu sunt cele asteptate');
// AIP-25 C1: cheia lor e un JWK simplu (nicio jumatate de pierdut); verdictul spune daca fiecare semnatura judecata e post-cuantica
cere(r.postQuantum === (r.alg === PQ && (!r.keyBinding || r.keyBinding.alg === PQ)), c.nume + ': postQuantum=' + r.postQuantum + ' pentru ' + r.alg + '/' + (r.keyBinding && r.keyBinding.alg));
valide++;
} else { cere(refuz(r, new RegExp(c.asteptat.motiv)), c.nume + ': asteptat refuz /' + c.asteptat.motiv + '/, primit ' + (r.valid ? 'VALID' : r.reason)); refuzate++; }
}
@@ -244,6 +257,73 @@ test(`INTEROP (fixturi @sd-jwt/core ${F2 ? F2.biblioteci['@sd-jwt/core'] : '?'}
cere(faraTyp && refuz(J.verifySdJwt(faraTyp.prezentare, { issuerKey: faraTyp.issuerJwk, issuerAlg: faraTyp.alg, audience: F2.audience, nonce: F2.nonce, now: la }), /typ/), 'jetonul lor fara typ a trecut de tipul implicit');
});
// ---------------------------------------------------------------- AIP-25 C1 (D-535, 2026-10-19): jumatatea clasica a unei identitati hibride
// Cheile unei identitati AERE sunt Ed25519 SI ML-DSA-65 (identity.mjs cere amandoua semnaturile). Pana pe 19 oct verificatorul SD-JWT alegea
// IMPLICIT jumatatea Ed25519, deci cine spargea NUMAI cheia clasica a emitentului facea credentiale primite contra identitatii lui intregi.
const corpE = corpDe(E.sdJwt);
const dezvE = E.sdJwt.split('~').slice(1).join('~'); // dezvaluirile lui E, fiecare urmata de ~
// ce poate face cine are NUMAI cheia Ed25519 a emitentului: aceleasi afirmatii, semnate cu ea
const doarClasic = (alg = 'EdDSA', corp = corpE, cheie = iss.privat.ed25519) => jwtDin({ alg, typ: J.TYP }, corp, cheie) + '~' + dezvE;
const nuArunca = (f) => { try { f(); return null; } catch (e) { return String(e.message || e); } };
test('AIP-25 C1: un SD-JWT semnat NUMAI cu jumatatea Ed25519 a emitentului AERE (ce face cine a spart doar cheia clasica), judecat implicit contra cheilor AERE -> REFUZAT pe regula hybrid-half, postQuantum false; acelasi credential cinstit -> VALID, postQuantum true', () => {
const atac = judeca(arata(doarClasic(), ['age_over_18']));
cere(!atac.valid && atac.rule === 'hybrid-half' && atac.postQuantum === false, 'jetonul doar-Ed25519, cheile AERE private: ' + (atac.valid ? 'VALID' : atac.rule + ': ' + atac.reason));
const pub = judeca(arata(doarClasic(), ['age_over_18']), { issuerKey: iss.public });
cere(!pub.valid && pub.rule === 'hybrid-half' && /Ed25519 half/.test(pub.reason), 'contra identitatii AERE PUBLICE (keys.public): ' + (pub.valid ? 'VALID' : pub.rule));
const plin = judeca(arata(doarClasic('Ed25519'), ['age_over_18']));
cere(!plin.valid && plin.rule === 'hybrid-half', 'antetul Ed25519 (numele complet din RFC 9864): ' + (plin.valid ? 'VALID' : plin.rule));
const bun = judeca(arata(E.sdJwt, ['age_over_18']));
cere(bun.valid && bun.alg === PQ && bun.postQuantum === true, 'credentialul cinstit: ' + (bun.valid ? bun.alg + ' pq=' + bun.postQuantum : bun.reason));
});
test('AIP-25 C1: jumatatea clasica se judeca NUMAI la cerere: issuerAlg EdDSA fara classicalHalf -> hybrid-half; classicalHalf:true -> VALID cu postQuantum false (verdictul o spune); classicalHalf primeste si jetonul ML-DSA-65, cu postQuantum true; classicalHalf + issuerAlg ML-DSA-65 pe jetonul clasic -> hybrid-half', () => {
const p = arata(doarClasic(), ['age_over_18']);
const a = judeca(p, { issuerAlg: 'EdDSA' }); cere(!a.valid && a.rule === 'hybrid-half', 'issuerAlg EdDSA fara classicalHalf: ' + (a.valid ? 'VALID' : a.rule));
const b = judeca(p, { classicalHalf: true }); cere(b.valid && b.alg === 'EdDSA' && b.postQuantum === false, 'classicalHalf: ' + (b.valid ? 'pq=' + b.postQuantum : b.reason));
const c = judeca(p, { classicalHalf: true, issuerAlg: 'EdDSA' }); cere(c.valid && c.postQuantum === false, 'classicalHalf + issuerAlg EdDSA: ' + (c.valid ? 'pq=' + c.postQuantum : c.reason));
const d = judeca(arata(E.sdJwt, ['age_over_18']), { classicalHalf: true }); cere(d.valid && d.alg === PQ && d.postQuantum === true, 'classicalHalf pe jetonul post-cuantic: ' + (d.valid ? d.alg + ' pq=' + d.postQuantum : d.reason));
const e = judeca(p, { classicalHalf: true, issuerAlg: PQ }); cere(!e.valid && e.rule === 'hybrid-half', 'classicalHalf + issuerAlg ML-DSA-65 pe jetonul clasic: ' + (e.valid ? 'VALID' : e.rule));
});
test('AIP-25 C1: emiterea implicita cu chei AERE e post-cuantica (antet ML-DSA-65, cnf.jwk AKP ML-DSA-65, KB ML-DSA-65); EdDSA la emitere sau in cnf cere classicalHalf; publicJwk(chei AERE) = jumatatea ML-DSA-65, cea Ed25519 numai cu { classicalHalf: true }, ES256 deloc', () => {
const h = JSON.parse(Buffer.from(E.sdJwt.split('.')[0], 'base64url').toString('utf8'));
cere(h.alg === PQ && corpE.cnf.jwk.kty === 'AKP' && corpE.cnf.jwk.alg === PQ, 'emiterea implicita: ' + JSON.stringify(h) + ' cnf ' + (corpE.cnf.jwk.kty + '/' + corpE.cnf.jwk.alg));
const kb = arata(E.sdJwt, ['age_over_18']).split('~').pop(); const hk = JSON.parse(Buffer.from(kb.split('.')[0], 'base64url').toString('utf8'));
cere(hk.alg === PQ, 'Key Binding-ul implicit: ' + JSON.stringify(hk));
cere(/classicalHalf/.test(nuArunca(() => emite({ alg: 'EdDSA' })) || ''), 'emiterea EdDSA fara classicalHalf a tacut');
cere(/classicalHalf/.test(nuArunca(() => emite({ holderAlg: 'EdDSA' })) || ''), 'cnf Ed25519 fara classicalHalf a tacut');
const C = emite({ alg: 'EdDSA', holderAlg: 'EdDSA', classicalHalf: true });
const rc = judeca(arata(C.sdJwt, ['age_over_18']), { classicalHalf: true });
cere(rc.valid && rc.alg === 'EdDSA' && rc.keyBinding.alg === 'EdDSA' && rc.postQuantum === false, 'emiterea clasica ceruta: ' + (rc.valid ? 'pq=' + rc.postQuantum : rc.reason));
cere(J.publicJwk(iss).kty === 'AKP' && J.publicJwk(iss.public).kty === 'AKP' && J.publicJwk(iss.public, PQ).pub === J.publicJwk(iss).pub, 'publicJwk implicit');
cere(/classicalHalf/.test(nuArunca(() => J.publicJwk(iss, 'EdDSA')) || '') && /classicalHalf/.test(nuArunca(() => J.publicJwk(iss.public, 'Ed25519')) || ''), 'publicJwk EdDSA/Ed25519 fara classicalHalf a tacut');
cere(J.publicJwk(iss, 'EdDSA', { classicalHalf: true }).crv === 'Ed25519', 'publicJwk EdDSA cu classicalHalf');
cere(/no ES256 key/.test(nuArunca(() => J.publicJwk(iss, 'ES256')) || ''), 'publicJwk ES256 pe chei AERE a dat o cheie');
});
test('AIP-25 C1: un JWK simplu ramane cum e (nicio jumatate de pierdut), iar verdictul spune postQuantum: Ed25519 OKP -> VALID postQuantum false; AKP ML-DSA-65 -> VALID postQuantum true; ES256 al standardului -> postQuantum false; un Key Binding clasic peste un emitent ML-DSA-65 -> postQuantum false', () => {
const okp = judeca(arata(doarClasic(), ['age_over_18']), { issuerKey: J.publicJwk(iss, 'EdDSA', { classicalHalf: true }) });
cere(okp.valid && okp.alg === 'EdDSA' && okp.postQuantum === false, 'OKP simplu: ' + (okp.valid ? 'pq=' + okp.postQuantum : okp.reason));
const akp = judeca(arata(E.sdJwt, ['age_over_18']), { issuerKey: J.publicJwk(iss) });
cere(akp.valid && akp.postQuantum === true, 'AKP simplu: ' + (akp.valid ? 'pq=' + akp.postQuantum : akp.reason));
const rfc = J.verifySdJwt(F.presentation, { issuerKey: F.issuerJwk, audience: KB.aud, nonce: KB.nonce, now: LA_KB });
cere(rfc.valid && rfc.alg === 'ES256' && rfc.postQuantum === false, 'ES256 al standardului: ' + (rfc.valid ? 'pq=' + rfc.postQuantum : rfc.reason));
const K = emite({ holderAlg: 'EdDSA', classicalHalf: true }); const rk = judeca(arata(K.sdJwt, ['age_over_18']));
cere(rk.valid && rk.alg === PQ && rk.keyBinding.alg === 'EdDSA' && rk.postQuantum === false, 'KB clasic peste emitent ML-DSA-65: ' + (rk.valid ? rk.alg + '/' + rk.keyBinding.alg + ' pq=' + rk.postQuantum : rk.reason));
const refuzat = judeca(arata(doarClasic(), ['age_over_18']), { audience: 'https://alt.example' });
cere(!refuzat.valid && refuzat.postQuantum === false, 'un refuz nu spune postQuantum true: ' + refuzat.postQuantum);
});
test('AIP-25 C1 ATAC: alg none sau ES256 contra cheilor AERE (si cu classicalHalf) -> refuzate pe alg / key; chei AERE amestecate (Ed25519 al strainului langa ML-DSA-65 al emitentului) nu primesc implicit jetonul doar-Ed25519 al strainului; chei AERE fara jumatatea ML-DSA-65 -> key, nu o cadere si nu jumatatea Ed25519', () => {
const none = b64u(JSON.stringify({ alg: 'none', typ: J.TYP })) + '.' + b64u(JSON.stringify(corpE)) + '.~' + dezvE;
for (const ch of [false, true]) {
const n = J.verifySdJwt(none, { issuerKey: iss, requireKeyBinding: false, classicalHalf: ch, now: LA }); cere(!n.valid && n.rule === 'alg', 'none, classicalHalf ' + ch + ': ' + (n.valid ? 'VALID' : n.rule));
const es = J.verifySdJwt(doarClasic('ES256'), { issuerKey: iss, requireKeyBinding: false, classicalHalf: ch, now: LA }); cere(!es.valid && es.rule === 'key', 'ES256 cu cheia Ed25519, classicalHalf ' + ch + ': ' + (es.valid ? 'VALID' : es.rule));
}
const amestec = { alg: iss.public.alg, ed25519: strain.public.ed25519, mldsa65: iss.public.mldsa65 };
const s = J.verifySdJwt(doarClasic('EdDSA', corpE, strain.privat.ed25519), { issuerKey: amestec, requireKeyBinding: false, now: LA });
cere(!s.valid && s.rule === 'hybrid-half', 'chei amestecate, implicit: ' + (s.valid ? 'VALID' : s.rule));
const faraMl = { alg: iss.public.alg, ed25519: iss.public.ed25519 };
const fm = J.verifySdJwt(arata(E.sdJwt, ['age_over_18']), { issuerKey: faraMl, audience: AUD, nonce: NONCE, now: LA });
cere(!fm.valid && fm.rule === 'key', 'chei AERE fara ML-DSA-65: ' + (fm.valid ? 'VALID (pe jumatatea Ed25519)' : fm.rule + ': ' + fm.reason));
});
test('linia de comanda: verify-sdjwt pe prezentarea RFC 9901 s.5.2 -> 0 VALID la momentul ei (--at), 1 INVALID acum (KB vechi); o cheie privata -> 2', () => {
const T = fs.mkdtempSync(path.join(os.tmpdir(), 'aere-sdjwt-cli-'));
try {
@@ -255,6 +335,21 @@ test('linia de comanda: verify-sdjwt pe prezentarea RFC 9901 s.5.2 -> 0 VALID la
const c = run(); cere(c.status === 2 && /private JWK/.test(c.stderr), 'cheia privata: ' + c.status + ' ' + c.stderr);
} finally { fs.rmSync(T, { recursive: true, force: true }); }
});
test('linia de comanda AIP-25 C1: verify-sdjwt cu cheile AERE publice ale emitentului: jetonul doar-Ed25519 -> 1 INVALID (hybrid-half), si cu --issuer-alg EdDSA fara --classical-half; cu --classical-half -> 0 VALID, postQuantum false in --json si "NOT post-quantum" in text; jetonul implicit -> 0, postQuantum true', () => {
const T = fs.mkdtempSync(path.join(os.tmpdir(), 'aere-sdjwt-cli-c1-'));
try {
fs.writeFileSync(path.join(T, 'clasic.txt'), arata(doarClasic(), ['age_over_18'])); fs.writeFileSync(path.join(T, 'pq.txt'), arata(E.sdJwt, ['age_over_18']));
fs.writeFileSync(path.join(T, 'k.json'), JSON.stringify(iss.public));
const run = (f, ...a) => spawnSync(process.execPath, [path.join(AICI, 'identity-cli.mjs'), 'verify-sdjwt', '--sd-jwt', f, '--issuer-key', 'k.json', '--audience', AUD, '--nonce', NONCE, '--at', LA.toISOString(), ...a], { cwd: T, encoding: 'utf8' });
const j = (r) => { try { return JSON.parse(r.stdout); } catch { return {}; } };
const a = run('clasic.txt', '--json'); cere(a.status === 1 && j(a).rule === 'hybrid-half' && j(a).postQuantum === false, 'doar-Ed25519 implicit: ' + a.status + ' ' + (a.stdout + a.stderr).slice(0, 200));
const b = run('clasic.txt', '--issuer-alg', 'EdDSA', '--json'); cere(b.status === 1 && j(b).rule === 'hybrid-half', '--issuer-alg EdDSA fara --classical-half: ' + b.status + ' ' + (b.stdout + b.stderr).slice(0, 200));
const c = run('clasic.txt', '--classical-half', '--json'); cere(c.status === 0 && j(c).valid === true && j(c).postQuantum === false, '--classical-half: ' + c.status + ' ' + (c.stdout + c.stderr).slice(0, 200));
const d = run('clasic.txt', '--classical-half'); cere(d.status === 0 && /NOT post-quantum/.test(d.stdout), 'textul cu --classical-half: ' + d.stdout.slice(0, 200));
const e = run('pq.txt', '--json'); cere(e.status === 0 && j(e).postQuantum === true && j(e).alg === PQ, 'jetonul implicit: ' + e.status + ' ' + (e.stdout + e.stderr).slice(0, 200));
const g = run('pq.txt'); cere(g.status === 0 && /post-quantum/.test(g.stdout) && !/NOT post-quantum/.test(g.stdout), 'textul pe jetonul implicit: ' + g.stdout.slice(0, 200));
} finally { fs.rmSync(T, { recursive: true, force: true }); }
});
console.log(`\naere-sd-jwt: ${treceri}/${treceri + esecuri.length} cum trebuia`);
process.exitCode = esecuri.length ? 1 : 0;