First working authenticode.js.
Ylian Saint-Hilaire committed
May 27, 2022 at 12:35 UTC
33f6a71e6326ef3a582836cb6b2a681a7b1e81be
1 file changed
+18
-33
authenticode.js
+18
-33
@@ -94,32 +94,25 @@ function createAuthenticodeHandler(path) {
94
var derlen = forge.asn1.getBerValueLength(forge.util.createBuffer(pkcs7raw.slice(1, 5))) + 4;
95
if (derlen != pkcs7raw.length) { pkcs7raw = pkcs7raw.slice(0, derlen); }
96
97
- //console.log('pkcs7raw', pkcs7raw.toString('base64'));
98
-
97
// Decode the signature block
98
var pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(pkcs7raw));
101
-
102
- // To work around ForgeJS PKCS#7 limitation
103
- // Switch content type from 1.3.6.1.4.1.311.2.1.4 to forge.pki.oids.data (1.2.840.113549.1.7.1)
104
- // TODO: Find forge.asn1.oidToDer('1.3.6.1.4.1.311.2.1.4').data and switch it.
99
+
100
+ // To work around ForgeJS PKCS#7 limitation, this may break PKCS7 verify if ForjeJS adds support for it in the future
101
+ // Switch content type from "1.3.6.1.4.1.311.2.1.4" to "1.2.840.113549.1.7.1"
102
pkcs7der.value[1].value[0].value[2].value[0].value = forge.asn1.oidToDer(forge.pki.oids.data).data;
103
107
- // Convert the ASN1 content data into binary and place back
108
- var pkcs7content = forge.asn1.toDer(pkcs7der.value[1].value[0].value[2].value[1].value[0]).data;
109
- pkcs7der.value[1].value[0].value[2].value[1].value[0] = { tagClass: 0, type: 4, constructed: false, composed: false, value: pkcs7content };
110
-
111
- // DEBUG: Print out the new DER
112
- //console.log(Buffer.from(forge.asn1.toDer(pkcs7der).data, 'binary').toString('base64'));
113
-
104
// Decode the PKCS7 message
105
var pkcs7 = p7.messageFromAsn1(pkcs7der);
116
- var pkcs7content = forge.asn1.fromDer(pkcs7.rawCapture.content.value[0].value);
117
- obj.rawSignedContent = pkcs7.rawCapture.content.value[0].value;
118
-
119
- //console.log('p7content', JSON.stringify(pkcs7content));
106
+ var pkcs7content = pkcs7.rawCapture.content.value[0];
107
121
- // DEBUG: Print out the content
122
- //console.log(Buffer.from(pkcs7.rawCapture.content.value[0].value, 'binary').toString('hex'));
108
+ /*
109
+ // Verify a PKCS#7 signature
110
+ // Verify is not currently supported in node-forge, but if implemented in the future, this code could work.
111
+ var caStore = forge.pki.createCaStore();
112
+ for (var i in obj.certificates) { caStore.addCertificate(obj.certificates[i]); }
113
+ // Return is true if all signatures are valid and chain up to a provided CA
114
+ if (!pkcs7.verify(caStore)) { throw ('Executable file has an invalid signature.'); }
115
+ */
116
117
// Set the certificate chain
118
obj.certificates = pkcs7.certificates;
@@ -185,8 +178,13 @@ function createAuthenticodeHandler(path) {
178
obj.sign = function (cert, key) {
179
if ((cert == null) || (key == null)) { var c = obj.createSelfSignedCert(); cert = c.cert; key = c.key; }
180
var fileHash = getHash('sha384');
181
+
182
+ // Create the signature block
183
var p7 = forge.pkcs7.createSignedData();
189
- p7.content = forge.asn1.toDer({ "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 6, "constructed": false, "composed": false, "value": forge.asn1.oidToDer("1.3.6.1.4.1.311.2.1.15").data }, { "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 3, "constructed": false, "composed": false, "value": "\u0000", "bitStringContents": "\u0000", "original": { "tagClass": 0, "type": 3, "constructed": false, "composed": false, "value": "\u0000" } }, { "tagClass": 128, "type": 0, "constructed": true, "composed": true, "value": [{ "tagClass": 128, "type": 2, "constructed": true, "composed": true, "value": [{ "tagClass": 128, "type": 0, "constructed": false, "composed": false, "value": "" }] }] }] }] }, { "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 6, "constructed": false, "composed": false, "value": forge.asn1.oidToDer(forge.pki.oids.sha384).data }, { "tagClass": 0, "type": 5, "constructed": false, "composed": false, "value": "" }] }, { "tagClass": 0, "type": 4, "constructed": false, "composed": false, "value": fileHash.toString('binary') }] }] });
184
+ var content = { "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 6, "constructed": false, "composed": false, "value": forge.asn1.oidToDer("1.3.6.1.4.1.311.2.1.15").data }, { "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 3, "constructed": false, "composed": false, "value": "\u0000", "bitStringContents": "\u0000", "original": { "tagClass": 0, "type": 3, "constructed": false, "composed": false, "value": "\u0000" } }, { "tagClass": 128, "type": 0, "constructed": true, "composed": true, "value": [{ "tagClass": 128, "type": 2, "constructed": true, "composed": true, "value": [{ "tagClass": 128, "type": 0, "constructed": false, "composed": false, "value": "" }] }] }] }] }, { "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 16, "constructed": true, "composed": true, "value": [{ "tagClass": 0, "type": 6, "constructed": false, "composed": false, "value": forge.asn1.oidToDer(forge.pki.oids.sha384).data }, { "tagClass": 0, "type": 5, "constructed": false, "composed": false, "value": "" }] }, { "tagClass": 0, "type": 4, "constructed": false, "composed": false, "value": fileHash.toString('binary') }] }] };
185
+ p7.contentInfo = forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.SEQUENCE, true, [forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.OID, false, forge.asn1.oidToDer('1.3.6.1.4.1.311.2.1.4').getBytes())]);
186
+ p7.contentInfo.value.push(forge.asn1.create(forge.asn1.Class.CONTEXT_SPECIFIC, 0, true, [content]));
187
+ p7.content = {}; // We set .contentInfo and have .content empty to bypass node-forge limitation on the type of content it can sign.
188
p7.addCertificate(cert);
189
p7.addSigner({
190
key: key,
@@ -203,19 +201,6 @@ function createAuthenticodeHandler(path) {
201
p7.sign();
202
var p7signature = Buffer.from(forge.pkcs7.messageToPem(p7).split('-----BEGIN PKCS7-----')[1].split('-----END PKCS7-----')[0], 'base64');
203
206
- // Correct the signed data type
207
- // Decode the signature block
208
- var pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(p7signature.toString('binary')));
209
-
210
- // To work around ForgeJS PKCS#7 limitation
211
- // Switch content type from 1.2.840.113549.1.7.1 to forge.pki.oids.data (1.3.6.1.4.1.311.2.1.4)
212
- pkcs7der.value[1].value[0].value[2].value[0].value = forge.asn1.oidToDer('1.3.6.1.4.1.311.2.1.4').data;
213
-
214
- // Convert the ASN1 content data into binary and place back
215
- var pkcs7content = forge.asn1.fromDer(forge.util.createBuffer(Buffer.from(pkcs7der.value[1].value[0].value[2].value[1].value[0].value, 'binary').toString('binary')));
216
- pkcs7der.value[1].value[0].value[2].value[1].value = [ pkcs7content ];
217
- p7signature = Buffer.from(forge.asn1.toDer(pkcs7der).data, 'binary');
218
-
204
// Create the output filename
205
var outputFileName = this.path.split('.');
206
outputFileName[outputFileName.length - 2] += '-jsigned';