Node.js Tutorial
- Node.js Tutorial
- Install Node.js on Windows
- Install Node.js on Linux/Ubuntu/CentOS
- Node.js First Example
- Node.js Console
- Node.js REPL
- Node.js Package Manager
- Node.js Command Line Options
- Node.js Global Objects
- Node.js OS
- Node.js Timer
- Node.js Errors
- Node.js DNS
- Node.js Net
- Node.js Crypto
- Node.js TLS/SSL
- Node.js Debugger
- Node.js Process
- Node.js Child Process
- Node.js Buffers
- Node.js Streams
- Node.js File System (FS)
- Node.js Path
- Node.js StringDecoder
- Node.js Query String
- Node.js ZLIB
- Node.js Assertion Testing
- Node.js V8
- Node.js Callbacks
- Node.js Events
- Node.js Punycode
- Node.js TTY
- Node.js Web Module
- NestJS
Node.js MySQL
Node.js MongoDB
Nodejs Difference
Node.js MCQ
Node.js Express
Nodejs Interview Questions
Node.js TLS/SSL
What is TLS/SSL
TLS stands for Transport Layer Security. It is the successor to Secure Sockets Layer (SSL). TLS along with SSL is used for cryptographic protocols to secure communication over the web.
TLS uses public-key cryptography to encrypt messages. It encrypts communication generally on the TCP layer.
What is public-key cryptography
In public-key cryptography, each client and each server has two keys: public key and private key. Public key is shared with everyone and private key is secured. To encrypt a message, a computer requires its private key and the recipient?s public key. On the other hand, to decrypt the message, the recipient requires its own
You have to use require('tls') to access this module.
Syntax:
- var tls = require('tls');
The tls module uses OpenSSL to attain Transport Layer Security and Secure Socket Layer. TLS/SSL is a public/private key infrastructure. Each client and each server must have a private key.
A private key can be created like this:
- openssl genrsa -out ryans-key.pem 1024
All severs and some clients need to have a certificate. Certificates are public keys signed by a Certificate Authority or self-signed. To get certificate, you have to create a "Certificate Signing Request" (CSR) file.
A certificate can be created like this:
- openssl req -new -key ryans-key.pem -out ryans-csr.pem
To create a self-signed certificate with the CSR:
- openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem
Node.js TLS client example
File: tls_client.js
- tls = require('tls');
- function connected(stream) {
- if (stream) {
- // socket connected
- stream.write("GET / HTTP/1.0\n\rHost: encrypted.google.com:443\n\r\n\r");
- } else {
- console.log("Connection failed");
- }
- }
- // needed to keep socket variable in scope
- var dummy = this;
- // try to connect to the server
- dummy.socket = tls.connect(443, 'encrypted.google.com', function() {
- // callback called only after successful socket connection
- dummy.connected = true;
- if (dummy.socket.authorized) {
- // authorization successful
- dummy.socket.setEncoding('utf-8');
- connected(dummy.socket);
- } else {
- // authorization failed
- console.log(dummy.socket.authorizationError);
- connected(null);
- }
- });
- dummy.socket.addListener('data', function(data) {
- // received data
- console.log(data);
- });
- dummy.socket.addListener('error', function(error) {
- if (!dummy.connected) {
- // socket was not connected, notify callback
- connected(null);
- }
- console.log("FAIL");
- console.log(error);
- });
- dummy.socket.addListener('close', function() {
- // do something
- });
Output: