Issues (18)

bin/gentruststore.js (4 issues)

1
var fs = require('fs');
2
var pkijs = require('pkijs');
3
var asn1js = require('asn1js');
4
var Validation = require('../lib/x509/validation.pkijs');
5
var Certificate = pkijs.Certificate;
6
7
var path = "/etc/ssl/certs/";
8
9
fs.readdir(path, function(err, items) {
10
    var trustFile = {};
11
    for (var i = 0; i < items.length; i++) {
12
        if (items[i].slice(-4) === ".pem") {
13
            var pem = fs.readFileSync(path + items[i]).toString();
14
            pem = pem.replace(/-----BEGIN CERTIFICATE-----/g, '');
15
            pem = pem.replace(/-----END CERTIFICATE-----/g, '');
16
            pem = pem.replace(/\s+/g, '');
17
18
            try {
19
                var der = Validation.stringToArrayBuffer(Buffer.from(pem, 'base64'));
0 ignored issues
show
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
20
                var asn = asn1js.fromBER(der);
21
22
            } catch (e) {
23
                throw new Error("Failed to decode certificate")
24
            }
25
26
            if (asn.offset === -1) {
27
                throw new Error("Failed to decode certificate")
28
            }
29
30
            try {
31
                var cert = new Certificate({schema: asn.result});
32
            } catch (e) {
33
                throw new Error("Failed to decode certificate")
34
            }
35
36
            var subject;
37
            cert.subject.typesAndValues.map(function(typeAndValue) {
38
                if (typeAndValue.type.toLowerCase() === "2.5.4.3") {
39
                    subject = typeAndValue.value.valueBlock.value;
40
                    trustFile[""+subject] = pem;
0 ignored issues
show
The variable pem is changed as part of the for loop for example by pem.replace(\s+, "") on line 16. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
The variable subject is changed as part of the for loop for example by typeAndValue.value.valueBlock.value on line 39. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
41
                }
42
            });
43
        }
44
    }
45
46
    console.log(JSON.stringify(trustFile, null, 2));
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
47
});
48