-
Notifications
You must be signed in to change notification settings - Fork 98
/
createRootCA.js
71 lines (64 loc) · 1.86 KB
/
createRootCA.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
/**
* 生成根证书
*/
console.log('生成根证书\n');
const forge = require('node-forge');
const pki = forge.pki;
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
var keys = pki.rsa.generateKeyPair(1024);
var cert = pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = (new Date()).getTime() + '';
cert.validity.notBefore = new Date();
cert.validity.notBefore.setFullYear(cert.validity.notBefore.getFullYear() - 5);
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 20);
var attrs = [{
name: 'commonName',
value: 'https-mitm-proxy-handbook'
}, {
name: 'countryName',
value: 'CN'
}, {
shortName: 'ST',
value: 'GuangDong'
}, {
name: 'localityName',
value: 'ShenZhen'
}, {
name: 'organizationName',
value: 'https-mitm-proxy-handbook'
}, {
shortName: 'OU',
value: 'https://github.com/wuchangming/https-mitm-proxy-handbook'
}];
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions([{
name: 'basicConstraints',
critical: true,
cA: true
}, {
name: 'keyUsage',
critical: true,
keyCertSign: true
}, {
name: 'subjectKeyIdentifier'
}]);
// self-sign certificate
cert.sign(keys.privateKey, forge.md.sha256.create());
var certPem = pki.certificateToPem(cert);
var keyPem = pki.privateKeyToPem(keys.privateKey);
var certPath = path.join(__dirname, '../../rootCA/rootCA.crt');
var keyPath = path.join(__dirname, '../../rootCA/rootCA.key.pem');
console.log('公钥内容:\n');
console.log(certPem);
console.log('私钥内容:\n');
console.log(keyPem);
console.log(`公钥存放路径:\n ${certPath}\n`);
console.log(`私钥存放路径:\n ${keyPath}\n`);
mkdirp.sync(path.join(__dirname, '../../rootCA'));
fs.writeFileSync(certPath, certPem);
fs.writeFileSync(keyPath, keyPem);