-
Notifications
You must be signed in to change notification settings - Fork 78
/
add_certs.js
105 lines (91 loc) · 2.58 KB
/
add_certs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var https = require('https');
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var async = require('async');
var nconf = require('nconf');
const SSL_OPENSSLDIR_PATTERN = new RegExp(nconf.get('SSL_OPENSSLDIR_PATTERN'));
const SSL_CA_PATH = nconf.get('SSL_CA_PATH');
const SSL_CA_FILE = new RegExp(nconf.get('SSL_CA_FILE'), ((process.platform === 'win32') ? 'i': ''));
function addCertificates(cb){
return function(err, certificates) {
if (err) {
console.error('Custom/System CAs could not be imported', err);
return cb(err);
}
if (certificates) {
var cas = https.globalAgent.options.ca =
https.globalAgent.options.ca || [];
if (!cas.__added){
console.log('Adding', certificates.length, 'certificates');
certificates.forEach(function (cert) {
cas.push(cert.pem);
});
cas.__added = true;
}
}
cb();
}
}
function readPEM(file, cb){
fs.readFile(file, function(err,data) {
if (err) return cb(err);
cb(null, {pem: data});
});
}
function readCertficatesFromPath(certPath, cb){
console.log('Reading CA certificates from', certPath);
fs.readdir(certPath, function(err, files) {
if (err) cb (err);
files = files
.filter(function (file){ return SSL_CA_FILE.test(file);})
.map(function(file) {
return path.join(certPath, file);
});
async.map(files, readPEM, cb);
});
}
function readSystemCAs(cb) {
switch(process.platform) {
case 'win32':
console.log('Reading CA certificates from Windows Store');
const ca = require('win-ca');
const list = [];
ca({
format: ca.der2.pem,
store: ['root', 'ca', 'trustedpeople'],
ondata: list
});
cb(null, list.map(c => ({ pem: c })));
break;
case 'freebsd':
case 'linux':
console.log('Reading CA certificates from OPENSSLDIR');
exec('openssl version -d', function(err, stdout, stderr){
if (err) return cb(err);
var match = SSL_OPENSSLDIR_PATTERN.exec(stdout);
if (match && match.length > 1) {
return readCertficatesFromPath(path.join(match[1], 'certs'), cb);
}
cb();
});
break;
default:
console.warn('CA import is not implemented for platform', process.platform);
cb();
}
}
function getCAs(cb) {
if (SSL_CA_PATH) {
readCertficatesFromPath(SSL_CA_PATH, cb);
} else {
readSystemCAs(cb);
}
}
function injectHttpsCAs(cb) {
getCAs(addCertificates(cb));
}
module.exports = {
inject: injectHttpsCAs,
getSystemCAs : getCAs
};