Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tlsSocket.renegotiate(options, callback) in Node.js 0.11.8 and higher

I'm new to node.js and I have a simple https server running. Now when a user requests a certain context path the server should initiate a SSL renegotiation and ask for a client certificate authentication. I saw that this is supported in node.js 0.11.8 and higher.

I tried this so far, but a renegotiation is not happening. Not even an error is thrown.

var https = require('https');
var fs = require('fs');

var optSsl = {
   key: fs.readFileSync('ssl/server/keys/server.key'),
   cert: fs.readFileSync('ssl/server/certs/server.crt'),
   ca: fs.readFileSync('ssl/ca/ca.crt'),
   requestCert: false,
   rejectUnauthorized: true,
   ciphers: 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS',
   honorCipherOrder: true
};

var optClientAuth = {
    requestCert: true,
    rejectUnauthorized: true
};

var server = https.createServer(optSsl, function(req, res){
    res.writeHead(200);
    res.end("Hello World\n");
});

server.on('request', function(req, res){
    console.log('request emitted on ' + req.url);
    if (req.url == '/secure') {
        try {
            var socket = req.connection;
            socket.renegotiate(optClientAuth, function(err){
                if (!err) {
                    console.log(req.connection.getPeerCertificate());
                } else {
                    console.log(err.message);
                }
            });
        } catch (err) {
            console.log(err);
        }
    };
});

server.on('secureConnection', function(socket) {
    console.log('Secure connection established');
});

server.listen(8443);

Thank you for your support.

like image 951
Thomas Avatar asked Oct 31 '22 17:10

Thomas


1 Answers

This is the code which works for me.

var https = require('https');
var fs = require('fs');
var constants = require('constants');

var optSsl = {
    key: fs.readFileSync('./server.key'),
    cert: fs.readFileSync('./server.crt'),
    ca: fs.readFileSync('./ca.crt'),
    passphrase: "very_secret",
    agent: false,
    requestCert: false,
    rejectUnauthorized: false,
    ciphers: 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS',
    honorCipherOrder: true,
    secureOptions: constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_SSLv2
};

var optClientAuth = {
    requestCert: true,
    rejectUnauthorized: true
};

var server = https.createServer(optSsl);

server.on('request', function(req, res){
    console.log('request emitted on ' + req.url);
    var socket = req.connection;
    if (req.url == '/secure') {
        var result = socket.renegotiate(optClientAuth, function(err){
            if (!err) {
                console.log(req.connection.getPeerCertificate());

                res.writeHead(200);
                res.end("Authenticated Hello World\n");
            } else {
                console.log(err.message);
            }
        });
    } else {
        res.writeHead(200);
        res.end("Secured Hello World\n");
    };
});

server.listen(8443);
like image 153
Thomas Avatar answered Nov 09 '22 11:11

Thomas