-
Notifications
You must be signed in to change notification settings - Fork 582
/
PeerCertificate.js
134 lines (120 loc) · 3.78 KB
/
PeerCertificate.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
const https = require('https');
const forge = require('node-forge');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
// Generate CA certificate
const ca = generateCACertificate();
const caCertPem = pemEncodeCert(ca.cert);
// Generate server certificate signed by CA
const serverCert = generateCertificate(ca, 'localhost');
const serverCertPem = pemEncodeCert(serverCert.cert);
const serverKeyPem = pemEncodeKey(serverCert.privateKey);
// Generate client certificate signed by CA
const clientCert = generateCertificate(ca, 'client');
const clientCertPem = pemEncodeCert(clientCert.cert);
const clientKeyPem = pemEncodeKey(clientCert.privateKey);
fs.writeFileSync(path.join(__dirname, "server.ca"), caCertPem);
fs.writeFileSync(path.join(__dirname, "server.key"), serverKeyPem);
fs.writeFileSync(path.join(__dirname, "server.cert"), serverCertPem);
const uWS = require('../dist/uws');
const port = 8086;
const app = uWS.SSLApp({
cert_file_name: path.join(__dirname, "server.cert"),
key_file_name: path.join(__dirname, "server.key"),
ca_file_name: path.join(__dirname, "server.ca")
}).get('/*', (res, req) => {
const clientCert = res.getX509Certificate();
const x509 = new crypto.X509Certificate(clientCert);
if (x509.verify(crypto.createPublicKey(caCertPem))) {
res.end(`Hello World! your certificate is valid!`);
}
else
res.end('Hello World!');
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
sendClientRequest();
} else {
console.log('Failed to listen to port ' + port);
}
});
function generateCACertificate() {
const keys = forge.pki.rsa.generateKeyPair(2048);
const cert = forge.pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
const attrs = [
{ name: 'commonName', value: 'example.org' },
{ name: 'countryName', value: 'US' },
{ shortName: 'ST', value: 'California' },
{ name: 'localityName', value: 'San Francisco' },
{ name: 'organizationName', value: 'example.org' },
{ shortName: 'OU', value: 'Test' }
];
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions([{
name: 'basicConstraints',
cA: true
}]);
cert.sign(keys.privateKey, forge.md.sha256.create());
return {
privateKey: keys.privateKey,
publicKey: keys.publicKey,
cert: cert
};
}
function generateCertificate(ca, commonName) {
const keys = forge.pki.rsa.generateKeyPair(2048);
const cert = forge.pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = '02';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
const attrs = [
{ name: 'commonName', value: commonName }
];
cert.setSubject(attrs);
cert.setIssuer(ca.cert.subject.attributes);
cert.sign(ca.privateKey, forge.md.sha256.create());
return {
privateKey: keys.privateKey,
cert: cert
};
}
function pemEncodeCert(cert) {
return forge.pki.certificateToPem(cert);
}
function pemEncodeKey(key) {
return forge.pki.privateKeyToPem(key);
}
function sendClientRequest() {
const clientOptions = {
hostname: 'localhost',
port: port,
path: '/',
method: 'GET',
key: clientKeyPem,
cert: clientCertPem,
ca: [caCertPem],
rejectUnauthorized: false
};
const req = https.request(clientOptions, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Response from server:', data);
});
});
req.on('error', (e) => {
console.error('errp', e);
});
req.end();
}