Node.js, simple TLS client/server
13,367
I solve my problem.
Solution here
Related videos on Youtube
Author by
Yoratheon
Updated on June 04, 2022Comments
-
Yoratheon almost 2 years
I want create a simple secure client/server using TLS. I've follow instruction on the official doc. But I don't know how to create self-signed certificate with openssl (does not work with me).
Here 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(); });
I don't know why use two different key-pair :
- client-key.pem
- client-cert.pem
and :
- server-key.pem
- server-cert.pem
Anyone can exmplain me ? For work in self-signed.
Sincerely,
Yoratheon