remdra / sign-pdf-lib

10 stars 3 forks source link

Failed to sign with Chinese certificate #7

Open BQ97 opened 1 month ago

BQ97 commented 1 month ago

Hello, I am currently facing another issue with Chinese coding. Can you help me solve it?I have generated a Chinese p12 certificate, which contains the following information: image The source file of the certificate is here: chinese.zip You need to use zip to decompress this, the password for the certificate is 123456. Then I wrote a function to call

/**
* @param {Buffer} pdf
* @param {string} image
* @param {{name:string, reason:string, location:string, contactInfo:string, date:Date}} signature
* @param {{pageNumber: number, name: string, rectangle:{left:number;top:number;right:number;bottom:number}}} position
* @return {Promise<Buffer>}
*/
function sign(pdf, position, signature, image) {
    const { certificate, key, password } = signatureCert;
    const signatureComputer = { certificate: fs.readFileSync(certificate), password };
    if (key) signatureComputer.key = fs.readFileSync(key);
    const pdfSigner = new PdfSigner({
        signatureLength: 4000 - 6,
        rangePlaceHolder: 9999999,
        signatureComputer,
    });
    const info = {
        pageNumber: position.pageNumber,
        name: position.name,
        signature:{
                    name: signature.name,
                    reason: signature.reason || '我同意签署该文件,并承认其法律效力',
                    location: signature.location,
                    contactInfo: signature.contactInfo,
                   date: new Date(),
             },
        visual: {
            rectangle: position.rectangle,
            background: fs.readFileSync(image),
        },
    };
    return pdfSigner.signAsync(pdf, info);
}

After calling, I used Adobe Acrobat software to open the signed PDF file, which showed a signature error image image This pop-up box displays the following content: An error occurred during signature verification.An error occurred during verification:Error occurred during BER decoding:

Finally, I came to the conclusion that our class library failed to sign with a Chinese certificate. Could you please help me check this issue? Thank you

remdra commented 1 month ago

There is an issue with your certificate. See the output of: openssl x509 -noout -text -in chinese.p12 -passin pass:123456 -purpose

Same command (openssl x509 -noout -text -in certificate.p12 -passin pass:password -purpose) for the certificate in sign-pdf-lib/test/_assets/_common/cetificate displays the certificate information.

Check sign-pdf-lib/_scripts folder for steps to generate a certificate. Let me know if the generated certificate (with chinese chars) works for you.

BQ97 commented 3 weeks ago

It's my fault, it has been resolved. Thank you