I have a server and client application written in NodeJS that communicate to each other via TLS, with self-signed certificates. Certificates are not validated with a CA; instead, the user has access to both interfaces at once and will manually verify the authenticity of the server and client using a verification code calculated on both applications using the TLS server write key and client write key.
Are these keys stored even after the handshake is completed? How can I access these keys from a NodeJS TLS socket on the client side? I'm using the native https library.
Server code:
const options = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt'),
enableTrace: true,
requestCert: true,
rejectUnauthorized: false,
};
const server = https.createServer(options, app);
this.initWithServer(server);
server.on('secureConnection', (soc) => {
console.log(server.getTicketKeys());
});
Client code:
const requestOptions = {
hostname: HOST,
port: PORT,
path: '/',
method: 'GET',
rejectUnauthorized: false,
requestCert: true,
key: fs.readFileSync('./client.key'),
cert: fs.readFileSync('./client.crt'),
};
requestOptions.agent = new https.Agent(requestOptions);
const req = https.request(requestOptions, (res) => {
res.setEncoding('utf8');
let data = '';
res.on('data', (resBody) => {
data += resBody;
});
res.on('end', () => {
const body = JSON.parse(data);
});
});
req.end();
req.on('socket', (socket) => {
socket.on('secureConnect', () => {
const peerCertificate = socket.getPeerCertificate(true);
console.log(peerCertificate.pubkey);
const localCertificate = socket.getCertificate(true);
console.log(localCertificate.pubkey);
console.log(socket.getSession())
});
});