Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging TLS Client connection using node.js

I'm trying to set up a client connection to a server using node.js and TLS but it's failing.

My question is how to get more information about why the connection is failing. Ideally something similar to when using openssl (which connects fine) which shows the handshaking and certificate exchange.

At the moment all I get is { [Error: socket hang up] code: 'ECONNRESET' }.

I'm using something like the code below which was based on an internet blog.

var tls = require('tls'),
 fs = require('fs');

// callback for when secure connection established
function connected(stream) {
    if (stream) {
      stream.write('Hello');
    } else {
      console.log("Connection failed");
    }
}

// needed to keep socket variable in scope
var dummy = this;

// try to connect to the server
dummy.socket = tls.connect(
{
    port: 8000,
    host: 'dest.server.com',
    ca: [
            fs.readFileSync('C:\\Tools\\Certificates\\TestRoot.pem', 'utf8')
        ],
    cert: fs.readFileSync('C:\\Tools\\Certificates\\source.server.com.cert.pem', 'utf8'),
    key:  fs.readFileSync('C:\\Tools\\Certificates\\source.server.com.key.pem', 'utf8'),
    passphrase: 'password',
}
, function() {
   // callback called only after successful socket connection
   dummy.connected = true;
   if (dummy.socket.authorized) {
      // authorization successful
      console.log('auth ok');
      dummy.socket.setEncoding('utf-8');
      connected(dummy.socket);
   } else {
      // authorization failed
      console.log('auth fail');
     console.log(dummy.socket.authorizationError);
     connected(null);
   }
});

dummy.socket.addListener('data', function(data) {
   // received data
   console.log(data);
});

dummy.socket.addListener('error', function(error) {
   if (!dummy.connected) {
     // socket was not connected, notify callback
     connected(null);
   }
   console.log("FAIL");
   console.log(error);
});

dummy.socket.addListener('close', function() {
 // do something
});
like image 449
Mike Avatar asked Sep 20 '12 08:09

Mike


1 Answers

OK after a bit of digging into the source it seems you can get some additional information by setting the NODE_DEBUG environment variable to include 'tls'. It didn't help much in my case but it might be helpful for someone else.

like image 95
Mike Avatar answered Nov 03 '22 02:11

Mike