node-forge: Add test for tls

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg
2019-01-30 12:55:34 -08:00
parent 8d693d6025
commit 5b070eaaa8
+119 -2
View File
@@ -207,7 +207,7 @@ if (forge.util.fillString('1', 5) !== '11111') throw Error('forge.util.fillStrin
{
const emptyStore = forge.pki.createCaStore();
const certificate = forge.pki.createCertificate();
const certificate = cert;
const pem = forge.pki.certificateToPem(certificate);
const caStore = forge.pki.createCaStore();
@@ -225,4 +225,121 @@ if (forge.util.fillString('1', 5) !== '11111') throw Error('forge.util.fillStrin
{
const key: string = forge.pkcs5.pbkdf2("password", "salt", 1000, 32);
}
}
{
// Based on node-forge/examples/tls.js
let success = false;
const client = forge.tls.createConnection({
server: false,
caStore: [cert],
sessionCache: {},
// supported cipher suites in order of preference
cipherSuites: [
forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
virtualHost: 'server',
verify: function(c, verified, depth, certs) {
console.log(
'TLS Client verifying certificate w/CN: \"' +
certs[0].subject.getField('CN').value +
'\", verified: ' + verified + '...');
return verified;
},
connected: function(c) {
console.log('Client connected...');
// send message to server
setTimeout(function() {
c.prepareHeartbeatRequest('heartbeat');
c.prepare('Hello Server');
}, 1);
},
getCertificate: function(c, hint) {
console.log('Client getting certificate ...');
return forge.pki.certificateToPem(cert);
},
getPrivateKey: function(c, cert) {
return privateKeyPem;
},
tlsDataReady: function(c) {
// send TLS data to server
server.process(c.tlsData.getBytes());
},
dataReady: function(c) {
var response = c.data.getBytes();
console.log('Client received \"' + response + '\"');
success = (response === 'Hello Client');
c.close();
},
heartbeatReceived: function(c, payload) {
console.log('Client received heartbeat: ' + payload.getBytes());
},
closed: function(c) {
console.log('Client disconnected.');
if(success) {
console.log('PASS');
} else {
console.log('FAIL');
}
},
error: function(c, error) {
console.log('Client error: ' + error.message);
}
});
// create TLS server
const server = forge.tls.createConnection({
server: true,
caStore: [cert],
sessionCache: {},
// supported cipher suites in order of preference
cipherSuites: [
forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
connected: function(c) {
console.log('Server connected');
c.prepareHeartbeatRequest('heartbeat');
},
verifyClient: true,
verify: function(c, verified, depth, certs) {
console.log(
'Server verifying certificate w/CN: \"' +
certs[0].subject.getField('CN').value +
'\", verified: ' + verified + '...');
return verified;
},
getCertificate: function(c, hint) {
console.log('Server getting certificate for \"' + (hint as string[])[0] + '\"...');
return forge.pki.certificateToPem(cert);
},
getPrivateKey: function(c, cert) {
return privateKeyPem;
},
tlsDataReady: function(c) {
// send TLS data to client
client.process(c.tlsData.getBytes());
},
dataReady: function(c) {
console.log('Server received \"' + c.data.getBytes() + '\"');
// send response
c.prepare('Hello Client');
c.close();
},
heartbeatReceived: function(c, payload) {
console.log('Server received heartbeat: ' + payload.getBytes());
},
closed: function(c) {
console.log('Server disconnected.');
},
error: function(c, error) {
console.log('Server error: ' + error.message);
}
});
console.log('created TLS client and server, doing handshake...');
client.handshake();
}