Accessing the client certificate in TLS/HTTPS

1,850 views
Skip to first unread message

August Lilleaas

unread,
Feb 17, 2011, 11:28:23 AM2/17/11
to nodejs
I want to use client certificates for authentication. It seems node
doesn't provide a way of accessing that. All I could find was this:

https://github.com/ry/node/blob/0474ce67908c9afddab6/src/node_crypto.cc#L968

I assume line 975 here means that the client certificate is only used
to return an error string from verifyError.

So, is there a way for TLS/HTTPS servers in node to get a hold of the
client certificate?

August Lilleaas

unread,
Feb 17, 2011, 2:11:32 PM2/17/11
to nodejs
Just found it: it is already in node

https://github.com/ry/node/blob/0474ce67908c9afddab6/lib/tls.js#L123

On Feb 17, 5:28 pm, August Lilleaas <augustlille...@gmail.com> wrote:
> I want to use client certificates for authentication. It seems node
> doesn't provide a way of accessing that. All I could find was this:
>
> https://github.com/ry/node/blob/0474ce67908c9afddab6/src/node_crypto....

Wes Widner

unread,
Feb 20, 2011, 7:25:58 AM2/20/11
to nod...@googlegroups.com
Were you able to access the client's certificate data? All I've seen so far is how to set a flag to request the certificate.

Wes Widner

unread,
Feb 20, 2011, 7:45:00 AM2/20/11
to nod...@googlegroups.com
I figured it out, my sample code is:

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

var options = {
  key: fs.readFileSync('./ssl/privatekey.pem'),
  cert: fs.readFileSync('./ssl/certificate.pem'),
  requestCert: true,
};

https.createServer(options, function (req, res) {
res.end(sys.inspect(req.socket.pair._ssl.getPeerCertificate()));
}).listen(8000);

Ryan Dahl

unread,
Feb 20, 2011, 3:24:30 PM2/20/11
to nod...@googlegroups.com, Wes Widner

req.connection.getPeerCertificate() is the preferred way.

Mike Post

unread,
Jan 7, 2015, 5:47:01 PM1/7/15
to nod...@googlegroups.com, kai52...@gmail.com, r...@tinyclouds.org
That gives you the certificate data, but it's only a representation of the certificate -- not the full certificate data.  For example, the certificate's serial number, version, and SKI are not available from getPeerCertificate as of node 0.10.35.
Reply all
Reply to author
Forward
0 new messages