forge
forge copied to clipboard
PDF signature formatting error
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 });