You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've encountered a weird issue where signed certificates sometimes does not verify against their CA public key. It rarely happens, only once in couple of thousands signatures, and it only happens with crypto.X509Certificate.verify function (pkijs.Certificate verify does return true). I'm not sure if the issue is somewhere in your code or webcrypto's, I was hoping you could confirm that.
Replication code (includes both the check that does work and the one that doesn't):
constpkijs=require("pkijs");constasn1js=require("asn1js");constcrypto=require("crypto");constassert=require('node:assert/strict');constCA_CERT="MIIBVjCB/aADAgECAgkAvRYDXayXsWswCgYIKoZIzj0EAwIwDzENMAsGA1UEAwwEVGVzdDAeFw0yMzExMDUxNjIwMDBaFw00ODExMDUxNjIwMDBaMA8xDTALBgNVBAMMBFRlc3QwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATgh9XNXTfcEdQ1tfZwqWiTshUsIv+j2bTLUYLSzI110WD9Pte83iN4OkXL/bK060wwXKmkEXG9I47NH8lwVX1So0IwQDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBROogLnS8YM4FqWlz0IVS+AJv3yWjAOBgNVHQ8BAf8EBAMCAQYwCgYIKoZIzj0EAwIDSAAwRQIhAJ91UC+6I70mMOwbkDWZR8lVpzZjCs0meyt3InmBe2lVAiAIJ8fXSSgzc9Fl9LOQ4a6aDKOBS49lSbXhUr+fSh2S7Q==";constCA_PRIVATE_KEY="MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQglGJ0AxhOPZlH1recNlmzqEMATJqvZInZUBob2Ij3gcOhRANCAATgh9XNXTfcEdQ1tfZwqWiTshUsIv+j2bTLUYLSzI110WD9Pte83iN4OkXL/bK060wwXKmkEXG9I47NH8lwVX1S";it('demonstrate webcrypto verify error',asyncfunction(){this.timeout(600000);constcryptoEngine=newpkijs.CryptoEngine({name: 'OpenSSL',crypto: crypto.webcrypto});constcaCert=pkijs.Certificate.fromBER(newUint8Array(Buffer.from(CA_CERT,'base64')).buffer);constcaPrivateKey=awaitcryptoEngine.importKey('pkcs8',newUint8Array(Buffer.from(CA_PRIVATE_KEY,'base64')).buffer,{name: 'ECDSA',namedCurve: 'P-256'},false,['sign']);constcaPublicKey=crypto.KeyObject.from(awaitcryptoEngine.importKey('spki',caCert.subjectPublicKeyInfo.toSchema().toBER(false),{name: 'ECDSA',namedCurve: 'P-256'},true,['verify']));const{publicKey, privateKey}=awaitcryptoEngine.generateKey({name: 'ECDSA',namedCurve: 'P-256'},true,['sign','verify']);constcertificateSigningRequest=awaitgetCertificateSigningRequest(cryptoEngine,publicKey,privateKey);constcertificate=newpkijs.Certificate();certificate.version=2;certificate.serialNumber=newasn1js.Integer({value: 1});certificate.notBefore.value=newDate();certificate.notBefore.value.setDate(certificate.notBefore.value.getDate()-1);certificate.notAfter.value=newDate();certificate.notAfter.value.setDate(certificate.notAfter.value.getDate()+7);certificate.subject=certificateSigningRequest.subject;certificate.subjectPublicKeyInfo=certificateSigningRequest.subjectPublicKeyInfo;certificate.issuer=caCert.subject;for(leti=1;i<=200000;i++){console.log(`verifying signature [${i}]`)awaitcertificate.sign(caPrivateKey,'SHA-256',cryptoEngine);constcertDER=certificate.toSchema(true).toBER(false);constx509Certificate=newcrypto.X509Certificate(Buffer.from(certDER));constverified_pkijs=awaitcertificate.verify(caCert,cryptoEngine);// this always worksassert(verified_pkijs===true)constverified_crypto=x509Certificate.verify(caPublicKey);// this sometimes failsassert(verified_crypto===true)}});asyncfunctiongetCertificateSigningRequest(cryptoEngine,publicKey,privateKey){constcsr=newpkijs.CertificationRequest();csr.version=0;csr.subject.typesAndValues.push(newpkijs.AttributeTypeAndValue({type: '2.5.4.3',// commonNamevalue: newasn1js.Utf8String({value: 'Test'})}));csr.attributes=[];awaitcsr.subjectPublicKeyInfo.importKey(publicKey,cryptoEngine);awaitcsr.sign(privateKey,'SHA-256',cryptoEngine);returncsr;}
The text was updated successfully, but these errors were encountered:
I've encountered a weird issue where signed certificates sometimes does not verify against their CA public key. It rarely happens, only once in couple of thousands signatures, and it only happens with crypto.X509Certificate.verify function (pkijs.Certificate verify does return true). I'm not sure if the issue is somewhere in your code or webcrypto's, I was hoping you could confirm that.
Replication code (includes both the check that does work and the one that doesn't):
The text was updated successfully, but these errors were encountered: