Code Monkey home page Code Monkey logo

mtls's Introduction

SSL Mutual Authentication with Node

SSL mutual authentication example with NodeJS.

Client / Server

Test it online:

  1. Download the client certification in .p12 format
  2. Imprort the .p12 certification in your browser
  3. Visit https://server.ma.appseed.io:8090/

Local Development Instructions

Configure the domain names:

sudo echo '127.0.0.1 server.ma.appseed.io' >> /etc/hosts

Start the server:

npm run server

Query the server - curl

curl -v -s -k --key certs/cs/client-key.pem --cert certs/cs/client-crt.pem https://server.ma.appseed.io:8080

Query the server - node

npm run client

Certificates

All the certificates are create with the same pass phrase: phrase

Domains

  • ma.appseed.io
  • server.ma.appseed.io
  • ma2.appseed.io
  • client.ma2.appseed.io

Federated Nodes with a Gateway

Test it online:

Note: Access to the web interface of the nodes is intentionaly left public. API communication is protected my mTLS. Go directly to Step 3

  1. Download the client certification in .p12 format
  2. Imprort the .p12 certification in your browser
  3. Visit

Local Development Instructions

Configure the domain names:

echo '127.0.0.1 gw.ma.appseed.io' >> /etc/hosts
echo '127.0.0.1 n1.ma.appseed.io' >> /etc/hosts
echo '127.0.0.1 n2.ma.appseed.io' >> /etc/hosts

or with sudo

sudo sh -c "echo '127.0.0.1 gw.ma.appseed.io' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 n1.ma.appseed.io' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 n2.ma.appseed.io' >> /etc/hosts"

Start the server:

npm run gw

Query the gateway as node 1 and node 2 - curl

curl -v -s -k --key certs/fed/n1-key.pem --cert certs/fed/n1-crt.pem https://gw.ma.appseed.io:8080
curl -v -s -k --key certs/fed/n2-key.pem --cert certs/fed/n2-crt.pem https://gw.ma.appseed.io:8080

Query the gateway - nodejs

npm run n1
npm run n2

Create the Certificates

Gateway

Create Certification Authority (CA) - ma.appseed.io

openssl genrsa -out ca-key.pem 4096
openssl req -new -x509 -days 365 -key ca-key.pem -out ca-crt.pem

openssl x509 --in ca-crt.pem -text --noout

Create certificates - gw.ma.appseed.io

openssl genrsa -out gw-key.pem 4096
openssl req -new -sha256 -key gw-key.pem -out gw-csr.pem
openssl x509 -req -days 365 -in gw-csr.pem -CA ca-crt.pem -CAkey ca-key.pem -CAcreateserial -out gw-crt.pem

openssl x509 --in gw-crt.pem -text --noout

Verify the certificate

openssl verify -CAfile ca-crt.pem gw-crt.pem

Federated Nodes

We prepare keys/certificates for two nodes n1 and n2.

The gw would just sign the CSR and return the certificate to the user.

Create certificates

openssl genrsa -out n1-key.pem 4096
openssl req -new -sha256 -key n1-key.pem -out n1-csr.pem
openssl x509 -req -days 365 -in n1-csr.pem -CA ca-crt.pem -CAkey ca-key.pem -CAcreateserial -out n1-crt.pem

openssl genrsa -out n2-key.pem 4096
openssl req -new -sha256 -key n2-key.pem -out n2-csr.pem
openssl x509 -req -days 365 -in n2-csr.pem -CA ca-crt.pem -CAkey ca-key.pem -CAcreateserial -out n2-crt.pem

openssl x509 --in n1-crt.pem -text --noout
openssl x509 --in n2-crt.pem -text --noout

Verify the certificates

openssl verify -CAfile ca-crt.pem n1-crt.pem
openssl verify -CAfile ca-crt.pem n2-crt.pem

Export in .p12

Export a certificate in .p12 for importing in a browser, e.g. Firefox.

openssl pkcs12 -export -out client-key.p12 -inkey client-key.pem -in client-crt.pem

Check

Deploy

Start the GW and two Nodes as services:

pm2 start npm --name ma -- run gw
pm2 start npm --name ma -- run n2
pm2 start npm --name ma -- run n2

References

Mutual TLS Authentication

mtls's People

Contributors

skounis avatar vurtnec avatar

Watchers

 avatar

Forkers

takaokataoka

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.