TLS/SSL module methods in node.js

The tutorial provides the details on the node.js TLS /SSL module where TLS (Transport Layer Security) and SSL (Secure Socket Layer) protocols uses the public/private key infrastructure (PKI) for data communication on the TCP Protocol.. The node.js module for TLS/SSL can be added using the below command:

const tls = require('tls');

The TLS /SSL Protocols are built on top of the OpenSSL and for each secure communication and each Client and Server should have a private key which can be generated in many ways.

OpenSSL command-line interface to generate a 1024-bit RSA private key:
openssl genrsa -out ryans-key.pem 1024

All server communicating using TLS/SSL protocol should have a certificate which are are digitally signed (Certificate Authority) . Each Certificate contains public-key that correspond to a private key. There are also self-signed certificates signed by the owner of the private key . The certificates can be created using Certificate Signing Request (CSR) file.

OpenSSL command-line interface can be used to generate a CSR for a private key
openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem

The generated CSR File requires to be sent to Certificate Authority or get the self-signed certificate. OpenSSL command-line interface for genrating self-signed certificate:
openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem

The generated certificate is to be converted to .pfx or .p12 file using the below OpenSSL command-line-interface:

openssl pkcs12 -export -in ryans-cert.pem -inkey ryans-key.pem \
-certfile ca-cert.pem -out ryans.pfx

Perfect Forward Secrecy

Perfect Forward Secrecy is the feature of key-exchange methods where if the server’s private key is compromised , then communication can be decrypted only by  eavesdroppers in case key-pair is obtained through the hacking process. Perfect Forward Secrecy generated the key-pair randomly for key-exchange for the TLS/SSL handshake. The below given methods are defined to achieve Perfect Forward Secrecy is called ‘ephemeral’.

  • DHE: An ephemeral version of the Diffie Hellman key-agreement protocol.
  • ECDHE: An ephemeral version of the Elliptic Curve Diffie Hellman key-agreement protocol

The DHE method of Perfect Forward Secrecy need to generate Diffie-Hellman parameters and specify with the dhparam option to tls.createSecureContext(). The OpenSSL command-line interface to generate DHE parameters:
openssl dhparam -outform PEM -out dhparam.pem 1024

ALPN and SNI

ALPN (Application-Layer Protocol Negotiation Extension) and SNI (Server Name Indication) are TLS handshake extensions:

  • ALPN: permits 1 TLS server for multiple protocols (HTTP, HTTP/2)
  • SNI: permits 1 TLS server for multiple hostnames with different SSL certificates

Session Resumption

TLS Session requires to save the session state to ensure the reuse of it for TLS handshake between client and server communications. The below given are the mechanism which are used frequently

Session Identifier: When Client creates a new connection in the session, server sends a unique Id to client which is saved by the client. Client send the same unique id for another connection within the same session and if the server has unique Id for the same session state , sever agrees to use it , else server creates a new session with a new unique Id and sends it back to client.

Below steps are performed between node.js and server communicating session Ids:

  • Node.js client waits for the ‘session’ event to get the session data
  • Node.js calls tls.connect() to resue the session state
  • Servers consuming session state should have implementation of ‘newSession’ and ‘resumeSession’ events to save and restore the session Ids
  • In case of clustered servers, servers must use shared session cache in the session handlers.

Resumption using the session identifier is supported by all browsers running HTTPS requests.

Session Tickets: The Session Tickets is the process where server encrypts the session and send it back to client as ‘Ticket’. When client reconnects with the server , it sends the shared ticket to the server. If the ticket is not decrypted by the server , then server will create a new ticket and sends it back to client , else it will consume the received ticket and perform the decryption to process the request.

Below steps are performed between node.js and server communicating session tickets:

  • Node.js consumes the same APIs for session tickets and session identifiers
  • tls.TLSSocket.getTLSTicket() returns the value
  • The session contains the ticket or it contains the client-side session state

Session ticket keys are cryptographic keys and must be stored securely. If session tickets are compromised , then all sessions using the session tickets can be decrypted. Thus session tickets should be generated frequently and not be stored on disk.

Resumption using the session tickets is supported by all browsers running HTTPS requests.