pdf-signer icon indicating copy to clipboard operation
pdf-signer copied to clipboard

Failed to find a certificate that matches the private key.

Open GuasaPlay opened this issue 4 years ago • 5 comments

Hi everyone. I have an error when signing the pdf, although I put the certificate key correctly, I get the following error:

Error: Failed to find a certificate that matches the private key.
    at getCertificate (C:\Users\oscar\Desktop\PROYECTOS DE TRABAJOS\firma-elec\node_modules\pdf-signer\dist\signature\digital-signature.service.js:54:15)
    at Object.exports.getSignature (C:\Users\oscar\Desktop\PROYECTOS DE TRABAJOS\firma-elec\node_modules\pdf-signer\dist\signature\digital-signature.service.js:22:23)   
    at C:\Users\oscar\Desktop\PROYECTOS DE TRABAJOS\firma-elec\node_modules\pdf-signer\dist\sign.js:53:57
    at step (C:\Users\oscar\Desktop\PROYECTOS DE TRABAJOS\firma-elec\node_modules\pdf-signer\dist\sign.js:33:23)
    at Object.next (C:\Users\oscar\Desktop\PROYECTOS DE TRABAJOS\firma-elec\node_modules\pdf-signer\dist\sign.js:14:53)
    at fulfilled (C:\Users\oscar\Desktop\PROYECTOS DE TRABAJOS\firma-elec\node_modules\pdf-signer\dist\sign.js:5:58)

If anyone knows about it please help me. The code is the same as the example.

const { sign } = require("pdf-signer");
const fs = require("fs");

const p12Buffer = fs.readFileSync(`./firmas/carmen_raquel_barreto_arias.p12`);
const pdfBuffer = fs.readFileSync(`./firmas/example.pdf`);

async function getSign() {
  try {
    const signedPdf = await sign(pdfBuffer, p12Buffer, "xxxxxxx", {
  reason: '2',
  email: '[email protected]',
  location: 'Location, EC',
  signerName: 'Oscar Calle',
  annotationAppearanceOptions: {
    signatureCoordinates: { left: 0, bottom: 700, right: 190, top: 860 },
    signatureDetails: [
      {
        value: 'Signed by: Oscar Calle',
        fontSize: 7,
        transformOptions: { rotate: 0, space: 1, tilt: 0, xPos: 20, yPos: 20 },
      },
      {
        value: 'Date: 03-10-2020',
        fontSize: 7,
        transformOptions: { rotate: 0, space: 1, tilt: 0, xPos: 20, yPos: 30 },
      },
    ],
  },
});

    console.log(signedPdf);
    fs.writeFileSync("./firmas/signed.pdf", signedPdf);
  } catch (error) {
    console.log(error);
  }
}

GuasaPlay avatar Oct 03 '20 05:10 GuasaPlay

My guess is that the pkcs#12 is invalid... E.g. the public cert isn't related to the private key.

Where did you get/how did you create the .p12 file?

NoahCardoza avatar Oct 18 '20 00:10 NoahCardoza

@NoahCardoza The p12 file is an electronic signature issued by the government of my country.

GuasaPlay avatar Oct 18 '20 05:10 GuasaPlay

Interesting... I'm not really an SSL expert seeing as I just started having to work with it this week.

Maybe try openssl pkcs12 -in carmen_raquel_barreto_arias.p12 just to see what's inside.

NoahCardoza avatar Oct 18 '20 22:10 NoahCardoza

I am having the same problem. In my case, P12 certificate is working perfectly with the module "node-signpdf" but with that module It is not working to add placeholder.

inane avatar Dec 22 '20 07:12 inane

I'm having the same problem too. in my case p12 certificate file has multi level. I found and fixed it in function getCertificate. the code is.

let validCertificate: forge.pki.Certificate | undefined = getValidatedCertificate(privateKey, publicKey, rawCertificate)
if(validCertificate) {
	certificate = validCertificate;
}

tassun avatar Jan 30 '21 08:01 tassun