Nodejs creating the TLS service by Wusheying
Server.js
var TLS = require (' TLS '); var fs = require (' FS '); var options = { Key:fs.readFileSync ('./keys/server.key '), cert: Fs.readfilesync ('./keys/server.crt '), Requestcert:true, CA: [Fs.readfilesync ('./keys/ca.crt ')]};var Server = Tls.createserver (Options,function (stream) { console.log (' Server Connected ', stream.authorized? ') Authorized ': ' Unauthorized '); Stream.Write ("welcome!\n"); Stream.setencoding (' UTF8 '); Stream.pipe (stream);}); Server.listen (8000,function () { console.log (' server Bound ');});
Client.js
var TLS = require (' TLS '); var fs = require (' FS '); var options = { Key:fs.readFileSync ('./keys/client.key '), cert: Fs.readfilesync ('./keys/client.crt '), CA: [Fs.readfilesync ('./keys/ca.crt ')]};var stream = Tls.connect (8000, Options,function () { Console.log (' Client Connected ', stream.authorized? ') Authorized ': ' Unauthorized '); Process.stdin.pipe (stream);}); Stream.setencoding (' UTF8 '); Stream.on (' Data ', function (data) { console.log (data);}); Stream.on (' End ', function () { server.close ();});
Certificate Generation:Server.key,client.keyOpenSSL genrsa-out server.key 1024x768OpenSSL genrsa-out client.key 1024x768
CA.CRTOpenSSL genrsa-out ca.key 1024x768OpenSSL req-new-key ca.key-out CA.CSROpenSSL x509-req-in ca.csr-signkey ca.key-out ca.crt
SERVER.CRTOpenSSL req-new-key server.key-out SERVER.CSROpenSSL x509-req-ca ca.crt-cakey ca.key-cacreateserial-in server.csr-out server.crt
CLIENT.CRTOpenSSL req-new-key client.key-out CLIENT.CSROpenSSL x509-req-ca ca.crt-cakey ca.key-cacreateserial-in client.csr-out client.crt
Nodejs Creating a TLS service