forked from latentflip/dev-cert-authority
/
sign-cert.js
35 lines (27 loc) · 1.13 KB
/
sign-cert.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
const UserHome = require('user-home');
const Mkdirp = require('mkdirp');
const Path = require('path');
const CP = require('child_process');
const Fs = require('fs');
const AppPaths = require('./paths');
const Log = require('./log');
module.exports = function (host) {
Mkdirp(AppPaths.hostsDir);
const certPaths = AppPaths.makeCertPaths(host);
const subj = `/C=US/ST=AK/L=Anchorage/O=npm-dev-cert-authority/OU=npm-module/CN=${host}`;
try {
Fs.statSync(certPaths.key);
Log.ok(`Cert for ${host} already exists`);
} catch (err) {
Log.wait(`Generating certificates for ${host}`);
CP.execSync(`openssl genrsa -out ${certPaths.key} 2048 2>/dev/null`);
CP.execSync(`openssl req -new -key ${certPaths.key} -out ${certPaths.csr} -subj "${subj}" 2>/dev/null`);
CP.execSync(`openssl x509 -req -in ${certPaths.csr} -CA ${AppPaths.caPemPath} -CAkey ${AppPaths.caKeyPath} -CAcreateserial -out ${certPaths.crt} -days 500 -sha256 2>/dev/null`);
CP.execSync(`rm ${certPaths.csr}`);
Log.success(`Cert for ${host} created!`);
}
return {
key: Fs.readFileSync(certPaths.key),
cert: Fs.readFileSync(certPaths.crt)
};
};