Giter Site home page Giter Site logo

benedridge / mutual-tls Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 701 KB

๐Ÿ” Working example of mutual TLS client-server in Node (HTTP2, WebSockets & gRPC)

JavaScript 89.11% Shell 10.38% Dockerfile 0.52%
tls nodejs mutual-authentication mutual-tls http2 ssl websockets grpc grpc-node

mutual-tls's Introduction

Mutual TLS client and server in NodeJS


Nicobon [CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0)]

A better and more complete example of Mutual TLS authentication in NodeJS for HTTPS, WebSockets and gRPC

  • Server and Clients in HTTP1/HTTP2, WebSockets and gRPC
  • Certificate generation using OpenSSL or Forge
  • Documentation for clients (Windows, Linux, OSX, Android)
  • Docker image for testing
  • No library bloat

Getting Started

Install

npm install

Generate Certificates and Private Keys

# Interactive
npm run generate:keys-openssl

# Automatic (Primarily for testing)
npm run generate:keys-forge

Connect

npm run start:server

# Connect to the above locally via HTTP2
npm run start:client2

# Connect via WebSockets
npm run start:clientwss

Docker

Docker image:

docker pull ghcr.io/benedridge/mutual-tls/mutual-tls:latest
docker run --name mutual_tls -p 127.0.0.1:8443:8443 -d ghcr.io/benedridge/mutual-tls/mutual-tls:latest

Keys and Certificates

The generator scripts will create certificates and private keys in the keys directory as follows

  • Certificates:

    • CA.crt
    • CLIENT.crt
    • SERVER.crt
  • Private Keys (DO NO EXPOSE OR USE IN PROD):

    • CA_key.pem
    • CLIENT_key.pem
    • SERVER_key.pem

These certificates and keys need to be imported and loaded into the browser/OS keychains:

Browser and OS Connectivity

Browse to: localhost:8443 and you should be requested to supply a certificate or should connect automatically if the CA and Client certificates have been imported to your browser/OS.

OSX

OSX typically requires the relevant keys to be imported into the keychain:

See security --help for additional options

security import CA.crt -k ~/Library/Keychains/login.keychain

security import CLIENT.crt -k ~/Library/Keychains/login.keychain
security import CLIENT_key.pem -k ~/Library/Keychains/login.keychain

security import SERVER.crt -k ~/Library/Keychains/login.keychain

Safari and Chrome should work once keys have been trusted and key preferences set to hostname.

Firefox has it's own keystore that doesn't like PEM formatted keys and prefers p12 format. So you will need to import the CLIENT.p12 file using the password from the generator output in the console.

See below: about:preferences#privacy then view certificates

Linux

CA import for Ubuntu

  1. Rename CA.crt and copy to /usr/local/share/ca-certificates/
  2. chmod 644 CA.cr
  3. Run sudo update-ca-certificates

Android

  • Transfer the CA.crt and CLIENT.p12 file to your device
  • Settings -> Security -> Device Administrator and Credentials -> Install from SD card etc.

Windows (WIP)

certutil -enterprise -f -v -AddStore "Root" <Cert File path>

See: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/certutil

Resources

Attributions

Social media image: Nicobon [CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0)]

mutual-tls's People

Contributors

benedridge avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.