Node.js
Node.js
Get a Received Client Certificate (Server Side)
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetRcvdClientCert, which copies a client certificate presented during the TLS handshake of an accepted server-side connection into a Cert object.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. Use
NumReceivedClientCerts to determine the valid index range. Client certificates are supplied only when the server requests them for mutual TLS.Chilkat Node.js Downloads
NODEJS_PRELUDE
function chilkatExample() {
var success = false;
var socket = new chilkat.Socket();
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// The PFX password should come from a secure source rather than being hard-coded.
var pfxPassword = "myPfxPassword";
var cert = new chilkat.Cert();
success = cert.LoadPfxFile("qa_data/server.pfx",pfxPassword);
if (success == false) {
console.log(cert.LastErrorText);
return;
}
success = socket.InitSslServer(cert);
if (success == false) {
console.log(socket.LastErrorText);
return;
}
success = socket.BindAndListen(5000,25);
if (success == false) {
console.log(socket.LastErrorText);
return;
}
// Accept a client connection. The accepted connection carries any client certificate presented
// during the TLS handshake.
var connectedSock = new chilkat.Socket();
success = socket.AcceptNext(20000,connectedSock);
if (success == false) {
console.log(socket.LastErrorText);
return;
}
// Copy each received client certificate and inspect it.
var numClientCerts = connectedSock.NumReceivedClientCerts;
var clientCert = new chilkat.Cert();
var i;
for (i = 0; i <= numClientCerts - 1; i++) {
success = connectedSock.GetRcvdClientCert(i,clientCert);
if (success == false) {
console.log(connectedSock.LastErrorText);
return;
}
console.log("Client certificate subject: " + clientCert.SubjectCN);
}
}
chilkatExample();