Skip to content

Client/Server app - problem with certificate #253

@Heziode

Description

@Heziode

Hi all,

I would like create client/server app with Node.js (in TCP). I use TLS. I've followed instruction on the official doc but I can't create a self-signed certificat for client and server who work…

Here it's code :

Server.js :

const tls = require('tls');
const fs = require('fs');
 
const options = {
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem'),
 
  // This is necessary only if using the client certificate authentication.
  requestCert: true,
 
  // This is necessary only if the client uses the self-signed certificate.
  ca: [ fs.readFileSync('client-cert.pem') ]
};
 
const server = tls.createServer(options, (socket) => {
  console.log('server connected',
              socket.authorized ? 'authorized' : 'unauthorized');
  socket.write('welcome!\n');
  socket.setEncoding('utf8');
  socket.pipe(socket);
});
server.listen(8000, () => {
  console.log('server bound');
});

client.js :

const tls = require('tls');
const fs = require('fs');
 
const options = {
  // Necessary only if using the client certificate authentication
  key: fs.readFileSync('client-key.pem'),
  cert: fs.readFileSync('client-cert.pem'),
 
  // Necessary only if the server uses the self-signed certificate
  ca: [ fs.readFileSync('server-cert.pem') ]
};
 
const socket = tls.connect(8000, options, () => {
  console.log('client connected',
              socket.authorized ? 'authorized' : 'unauthorized');
  process.stdin.pipe(socket);
  process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
  console.log(data);
});
socket.on('end', () => {
  server.close();
});

The error who has returned is :

events.js:141
      throw er; // Unhandled 'error' event
      ^
 
Error: Hostname/IP doesn't match certificate's altnames: "Host: localhost. is not cert's CN: CA"
    at Object.checkServerIdentity (tls.js:186:15)
    at TLSSocket.<anonymous> (_tls_wrap.js:1023:29)
    at emitNone (events.js:67:13)
    at TLSSocket.emit (events.js:166:7)
    at TLSSocket._init.ssl.onclienthello.ssl.oncertcb.TLSSocket._finishInit (_tls_wrap.js:582:8)
    at TLSWrap.ssl.onclienthello.ssl.oncertcb.ssl.onnewsession.ssl.onhandshakedone (_tls_wrap.js:424:38)

Server traceback

server bound
server connected authorized

Would anyone have a solution?

Respectfully,

Heziode

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions