PeculiarVentures/x509

signature verification failing for x509 certificate with ECDSA signature algorithm

rahuledjx opened this issue · 2 comments

signature verification is failing for x509 certificate.

Current Behaviour:- isSame is coming false
Expected Behaviour:- isSame should be true

code

     import { writeFileSync, readFileSync } from 'fs';
     import { join } from 'path';
     import * as x509 from '@peculiar/x509';
     import { Crypto } from '@peculiar/webcrypto';
     
     const crypto = new Crypto();
     x509.cryptoProvider.set(crypto);
     
     const chain = new x509.X509Certificate(
      readFileSync(join('chain.pem')).toString()
    );
    const cert = new x509.X509Certificate(
      readFileSync(join('cert.pem')).toString()
    );
    const publicKey = await chain.publicKey.export();

    const isSame = await crypt.subtle.verify(
      cert.signatureAlgorithm,
      publicKey,
      cert.signature, // BufferSource
      cert.tbs
    );

cert.pem:

-----BEGIN CERTIFICATE-----
MIICgzCCAimgAwIBAgIUU3QVClDwfllZDosbQuijZEqeqN0wCgYIKoZIzj0EAwIw
fTELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRAwDgYDVQQH
EwdSYWxlaWdoMQ0wCwYDVQQKEwRFREpYMRUwEwYDVQQLEwxFREpYIFJvb3QgQ0Ex
HTAbBgNVBAMTFEVESlggSW50ZXJtZWRpYXRlIENBMB4XDTIxMDEwNzA3MTYwMFoX
DTIyMDEwNzA3MTYwMFowaTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAk5DMRAwDgYD
VQQHEwdSZWxlaWdoMQ0wCwYDVQQKEwRFREpYMRQwEgYDVQQLEwtFbmdpbmVlcmlu
ZzEWMBQGA1UEAxMNYXBpLmVkangudGVzdDBZMBMGByqGSM49AgEGCCqGSM49AwEH
A0IABKZwl6VQ9VZHpelF+ZhBkKY3N7f7qXvvIwDBIDpV18+iYs1r01Qoo2TwJh3/
j/n87sqtm4nfuOBvjL8M/RUdDRWjgZowgZcwDgYDVR0PAQH/BAQDAgWgMB0GA1Ud
JQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAMBgNVHRMBAf8EAj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yGe3CvpzKfZqMX63UeoQQ
-----END CERTIFICATE-----

@rahuledjx thank you for that issue. I found the problem and going to fix it shortly.

I fixed that problem and published the new version @peculiar/x509@1.1.0

Also I extended verify method for X509Certificate. It allows to use CryptoKey, or PublicKey, or X509Certificate like a publicKey parameter.

const ok = await leaf.verify({
  signatureOnly: true,
  publicKey: ca,
});

I added your test example to mocha tests https://github.com/PeculiarVentures/x509/blob/master/test/crypto.ts#L651