-
Notifications
You must be signed in to change notification settings - Fork 25
/
https-helper.js
143 lines (119 loc) · 3.83 KB
/
https-helper.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const selfsigned = require('selfsigned');
const forge = require('node-forge');
const jsonfile = require('jsonfile');
const fs = require('fs');
const attrs = [{name: 'commonName', value: 'databox'}];
const config = {days: 365, keySize: 2048, algorithm: 'sha256'};
let rootPems;
const certPath = './certs/';
const devCertPath = './certs/certs.json';
const devPemCertPath = './certs/containerManager.pem';
const devCAPath = './certs/containerManager.crt';
//Generate the CM root cert at startup.
const init = function () {
return new Promise((resolve, reject) => {
fs.readFile(devPemCertPath, function (err, obj) {
//return cached certs if we have them and
if (err === null) {
rootPems = obj;
resolve({rootCAcert: rootPems.cert});
return;
}
selfsigned.generate(attrs, config, function (err, pems) {
if (err) {
reject(err);
}
rootPems = pems;
//Cash the certs in dev mode. These are new certs so display the update instructions and exit.
jsonfile.writeFileSync(devCertPath, pems);
fs.writeFileSync(devPemCertPath, pems.private + pems.public + pems.cert);
fs.writeFileSync(devCAPath, rootPems.cert);
resolve({rootCAcert: rootPems.cert});
});
});
});
};
const getRootCert = function () {
return rootPems.cert;
};
//based on code extracted from the selfsigned module Licence MIT
const createClientCert = function (commonName, ips) {
return new Promise((resolve, reject) => {
const certFullpath = certPath + commonName + ".json";
const certPemFullpath = certPath + commonName + ".pem";
fs.readFile(certPemFullpath, function (err, data) {
//return cached certs if we have them
if (err === null) {
resolve(data);
return;
}
function toPositiveHex(hexString) {
let mostSiginficativeHexAsInt = parseInt(hexString[0], 16);
if (mostSiginficativeHexAsInt < 8) {
return hexString;
}
mostSiginficativeHexAsInt -= 8;
return mostSiginficativeHexAsInt.toString() + hexString.substring(1);
}
const clientkeys = forge.pki.rsa.generateKeyPair({bits: 2048});
const clientcert = forge.pki.createCertificate();
clientcert.serialNumber = toPositiveHex(forge.util.bytesToHex(forge.random.getBytesSync(9)));
clientcert.validity.notBefore = new Date();
clientcert.validity.notAfter = new Date();
clientcert.validity.notAfter.setFullYear(clientcert.validity.notBefore.getFullYear() + 10);
const clientAttrs = [{name: 'commonName', value: commonName}];
clientcert.setSubject(clientAttrs);
// Set the issuer to the parent key
clientcert.setIssuer(attrs);
const altNames = [
{
type: 2, // DNS name
value: commonName
},
{
type: 2, // DNS name
value: 'localhost'
}
];
if (ips) {
for (const ip of ips) {
altNames.push({
type: 7,
ip: ip
})
}
}
clientcert.setExtensions([{
name: 'basicConstraints',
cA: true
}, {
name: 'keyUsage',
keyCertSign: true,
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true
}, {
name: 'subjectAltName',
altNames: altNames
}]);
clientcert.publicKey = clientkeys.publicKey;
// Sign client cert with root cert
try {
clientcert.sign(forge.pki.privateKeyFromPem(rootPems.private), forge.md.sha256.create());
} catch (e) {
reject("ERROR", e);
}
const pem = {
clientprivate: forge.pki.privateKeyToPem(clientkeys.privateKey),
clientpublic: forge.pki.publicKeyToPem(clientkeys.publicKey),
clientcert: forge.pki.certificateToPem(clientcert)
};
console.log(certFullpath, commonName);
jsonfile.writeFileSync(certFullpath, pem);
fs.writeFileSync(certPemFullpath, pem.clientprivate + pem.clientpublic + pem.clientcert);
resolve(pem);
});
});
};
module.exports = {init: init, createClientCert: createClientCert, getRootCert: getRootCert};