forge icon indicating copy to clipboard operation
forge copied to clipboard

PDF signature formatting error

Open tomasantunes opened this issue 3 years ago • 0 comments

When I sign a PDF using node-forge my signature is invalid and displays the following message: "There are errors in the formatting or information contained in this signature."

Here is the code I used:

    const p7 = _nodeForge.default.pkcs7.createSignedData();
    p7.content = _nodeForge.default.util.createBuffer(pdf.toString('binary'));
    console.log(externalCertificate);
    const certPem = ['-----BEGIN CERTIFICATE-----', externalCertificate, '-----END CERTIFICATE-----'].join('\n');
    const cert = _nodeForge.default.pki.certificateFromPem(certPem);
    p7.addCertificate(cert);

    p7.addSigner({
        key: {
          sign: (md) => {
            const sig = externalSignature;
            return sig;
          }
        },
        certificate: cert,
        digestAlgorithm: _nodeForge.default.pki.oids.sha256,
        authenticatedAttributes: [
          {
            type: _nodeForge.default.pki.oids.contentType,
            value: _nodeForge.default.pki.oids.data,
          }, {
            type: _nodeForge.default.pki.oids.messageDigest,
          }, {
            type: _nodeForge.default.pki.oids.signingTime,
            value: new Date(),
          },
        ]
    });

    p7.sign({ detached: true });

tomasantunes avatar Sep 02 '22 15:09 tomasantunes