vizicsaba89 / pdf-signer

sign pdf documents
MIT License
38 stars 33 forks source link

Failed to find a certificate that matches the private key. #15

Open GuasaPlay opened 4 years ago

GuasaPlay commented 4 years ago

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: 'oscar@email.com',
  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);
  }
}
NoahCardoza commented 4 years ago

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?

GuasaPlay commented 4 years ago

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

NoahCardoza commented 4 years ago

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.

inane commented 3 years ago

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.

tassun commented 3 years ago

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;
}