NGINX Modern Apps > Class 3 - NGINX Dataplane Scripting Source | Edit on
Reading subject alternative from client certificate [http/certs/subject_alternative]¶
Mutual TLS is a one of many authentication methods supported by NGINX. NGINX Javascript enables us to access arbitrary fields in a client certificate to use for business logic like routing a request.
Step 1: Use the following commands to start your NGINX container with this lab’s files:
Step 2: Now let’s use curl to test our NGINX server:
openssl x509 -noout -text -in njs/http/certs/ca/intermediate/certs/client.cert.pem | grep 'X509v3 Subject Alternative Name' -A1
X509v3 Subject Alternative Name:
IP Address:127.0.0.1, IP Address:0:0:0:0:0:0:0:1, DNS:example.com, DNS:www2.example.com
curl https://localhost/ --insecure --key njs/http/certs/ca/intermediate/private/client.key.pem --cert njs/http/certs/ca/intermediate/certs/client.cert.pem --pass secretpassword
["7f000001","00000000000000000000000000000001","example.com","www2.example.com"]
docker stop njs_example
Code Snippets¶
This config enforces Mutual TLS authentication of client requests. We use njs to extract the “Subject Alternative Name (SAN)” from the certificate presented by the client into the $san variable.
Here we import an existing module that provides processing of x509 certificates. We retrieve the client certificate from the $ssl_client_raw_cert NGINX variable and use the x509.parse_pem_cert() method to parse the raw cert into a data structure we can work with. To locate the subjectAltName field, we use x509.get_oid_value() to look it up by oid.