-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcerts.js
36 lines (29 loc) · 1.17 KB
/
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
const fs = require('fs')
const pem = require('pem');
const NodeRSA = require('node-rsa');
const config = require('./config');
module.exports = {
extractCertificates: function () {
const pfx = fs.readFileSync(config.Certificate.pfxPath);
pem.readPkcs12(pfx, {p12Password: config.Certificate.pfxPassword}, (err, cert) => {
// Saving Public Key
fs.writeFileSync(config.Certificate.publicCertPath, cert.cert);
// Obtaining Private Key
const RSAKey = cert.key;
const key = new NodeRSA(RSAKey);
const privateKey = key.exportKey('pkcs8');
// Saving Private Key
fs.writeFileSync(config.Certificate.privateKeyPath, privateKey);
});
},
privateKey: function () {
return fs.readFileSync(config.Certificate.privateKeyPath).toString();
},
publicCertificate: function () {
// Removing placeholder parts from certificate.pem file
return fs.readFileSync(config.Certificate.publicCertPath).toString()
.replace('-----BEGIN CERTIFICATE-----', '')
.replace('-----END CERTIFICATE-----', '')
.trim();
}
}