Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom authenticatedAttributes #991

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,10 @@ p7.addSigner({
type: forge.pki.oids.signingTime,
// value can also be auto-populated at signing time
value: new Date()
// }, {
// // attributes other than contentType/messageDigest/signingTime need an ASN.1 value
// type: '1.2.840.113549.1.9.25.3',
// ans1Value: forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.OCTETSTRING, false, forge.random.getBytesSync(32))
}]
});
p7.sign();
Expand Down
2 changes: 2 additions & 0 deletions lib/pkcs7.js
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,8 @@ function _attributeToAsn1(attr) {
asn1.Class.UNIVERSAL, asn1.Type.GENERALIZEDTIME, false,
asn1.dateToGeneralizedTime(date));
}
} else if(attr.asn1Value) {
value = attr.asn1Value;
}

// TODO: expose as common API call
Expand Down
74 changes: 74 additions & 0 deletions tests/unit/pkcs7.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,44 @@ var UTIL = require('../../lib/util');
'6m/90OGUus11leuRlpgs1hR1TU/ScfPIAAfemPigk18hox9vMMAdRs7JBGdKDDQr\r\n' +
'c6mfUV75ZWEKFZM7y5bgX0IrolPexuMrOgeJzkzYtoMGwXA5fudHT3Nk53D3tLj3\r\n' +
'x4KOfz69nJA=\r\n' +
'-----END PKCS7-----\r\n',
signedDataWithCustomAuthenticatedAttribute:
'-----BEGIN PKCS7-----\r\n' +
'MIIGXgYJKoZIhvcNAQcCoIIGTzCCBksCAQExD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+
'Oxa43GI7+uw9T8LZ4zg//6NzFZHNaOzn5A62LsGEGVYAC+Bz/lGAUH4k5AZ4eZJk\r\n' +
'KOCJcGT4f9DT22bQ03QCg4ryCL0nyFQ/N4nj3Gx5COEQYhBw1Eq8Fg9YWDwup14f\r\n' +
'O0qEcgH+DGQC9xUl0Z/6O+BJILDiNQfyQOmJS28As5V7/L4Ap34D3oxbqhAPAmgX\r\n' +
'FmiJtgEpAj/6QoUryJSZgid6FTDxjnxcEgKLuZ5uhNkpUp5zcZpwVzApBNzlxtyM\r\n' +
'ry8=\r\n' +
'-----END PKCS7-----\r\n'
};

Expand Down Expand Up @@ -788,5 +826,41 @@ var UTIL = require('../../lib/util');
ASSERT.equal(pem, _pem.signedDataWithAttrs1949GeneralizedTime);
});

it('should create PKCS#7 SignedData with custom authenticatedAttribute', function() {
// verify with:
// openssl smime -verify -in p7.pem -signer certificate.pem \
// -out signedtext.txt -inform PEM -CAfile certificate.pem
var p7 = PKCS7.createSignedData();
p7.content = UTIL.createBuffer('To be signed.', 'utf8');
p7.addCertificate(_pem.certificate);
p7.addSigner({
key: PKI.privateKeyFromPem(_pem.privateKey),
certificate: _pem.certificate,
digestAlgorithm: PKI.oids.sha256,
authenticatedAttributes: [{
type: forge.pki.oids.contentType,
value: forge.pki.oids.data
}, {
type: forge.pki.oids.messageDigest
// value will be auto-populated at signing time
}, {
type: forge.pki.oids.signingTime,
// will be encoded as UTC time because it's >= 2050
value: new Date('2050-01-01T00:00:00Z')
}, {
//Add randomNonce attribute
type: '1.2.840.113549.1.9.25.3',
asn1Value: forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.OCTETSTRING, false, forge.random.getBytesSync(32))
}
]
});
p7.sign();
var pem = PKCS7.messageToPem(p7);
var fs = require('fs');
fs.writeFileSync('/home/dettoni/p7.pem', pem);
fs.writeFileSync('/home/dettoni/certificate.pem', _pem.certificate);
ASSERT.equal(pem, _pem.signedDataWithCustomAuthenticatedAttribute);
});

});
})();