Rodrigo Rosenfeld Rosas

Fri, 01 Dec 2017 19:12:00 +0000 (Updated at Sun, 03 Dec 2017 11:45:00 +0000)

Note: if you only care about getting the certificates, jump to the end of the article and you'll find a button to just do that. This way you don't even need Linux to generate them.

For a long time I've been testing my application locally using a certificate issued by Let's encrypt, which I must renew every few months for domains such as dev.mydomain.com. Recently, I've been considering creating a new app and I don't have a domain for it yet.

So I decided to take some time to learn how to create self-signed certificates in such a way that browsers such as Chrome and Firefox would accept it without any disclaimer with no extra step.

It took me about 2 hours to be able achieve this task, so I decided to write it down so that it would save me time in the future when I need to repeat this process.

I'll use the myapp.example.com domain for my new app, since the example.com domain is reserved.

The first step is add that domain in /etc/hosts:

1 127.0.0.1 localhost myapp.example.com

Recent browsers will require the subject alternate names extension, so the script will generate that extension using a template like this:

1 2 3 4 5 6 7 8 [SAN] subjectAltName = @alternate_names [ alternate_names ] DNS.1 = myapp.example.com IP.1 = 127.0.0.1 IP.2 = 192.168.0.10

Replace the second IP with your own fixed IP if you have one just in case you need to access it from another computer in the network, like some VM, for example. Edit the script below to change the template. You'll need to add the root CA certificate we'll generate soon to those other computers in the network in order to do so, as I'll explain in the last steps in this article. Just remove IP.2 if you don't care about it.

Then create this script to help generating the certificates in ~/.ssl/generate-certificates:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 #!/bin/bash FQDN=${1:-myapp.example.com} # Create our very own Root Certificate Authority [ -f my-root-ca.key.pem ] || \ openssl genrsa -out my-root-ca.key.pem 2048 # Self-sign our Root Certificate Authority [ -f my-root-ca.crt.pem ] || \ openssl req -x509 -new -nodes -key my-root-ca.key.pem -days 9131 \ -out my-root-ca.crt.pem \ -subj "/C=US/ST=Utah/L=Provo/O=ACME Signing Authority Inc/CN=example.net" # Create Certificate for this domain [ -f ${FQDN}.privkey.pem ] || \ openssl genrsa -out ${FQDN}.privkey.pem 2048 # Create the extfile including the SAN extension cat > extfile <<EOF [SAN] subjectAltName = @alternate_names [ alternate_names ] DNS.1 = ${FQDN} IP.1 = 127.0.0.1 IP.2 = 192.168.0.10 EOF # Create the CSR [ -f ${FQDN}.csr.pem ] || \ openssl req -new -key ${FQDN}.privkey.pem -out ${FQDN}.csr.pem \ -subj "/C=US/ST=Utah/L=Provo/O=ACME Service/CN=${FQDN}" \ -reqexts SAN -extensions SAN \ -config <(cat /etc/ssl/openssl.cnf extfile) # Sign the request from Server with your Root CA [ -f ${FQDN}.cert.pem ] || \ openssl x509 -req -in ${FQDN}.csr.pem \ -CA my-root-ca.crt.pem \ -CAkey my-root-ca.key.pem \ -CAcreateserial \ -out ${FQDN}.cert.pem \ -days 9131 \ -extensions SAN \ -extfile extfile # Update this machine to accept our own root CA as a valid one: sudo cp my-root-ca.crt.pem /usr/local/share/ca-certificates/my-root-ca.crt sudo update-ca-certificates cat <<EOF Here's a sample nginx config file: server { listen 80; listen 443 ssl; ssl_certificate ${PWD}/${FQDN}.cert.pem; ssl_certificate_key ${PWD}/${FQDN}.privkey.pem; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name ${FQDN}; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } } EOF grep -q ${FQDN} /etc/hosts || echo "Remember to add ${FQDN} to /etc/hosts"

Then run it:

1 2 3 4 5 6 cd ~/.ssl chmod +x generate-certificates ./generate-certificates # will generate the certificates for myapp.example.com # to generate for another app: ./generate-certificates otherapp.example.com

The script will output a sample nginx file demonstrating how to use the certificate and will remind you about adding the entry to /etc/hosts if it detects the domain is not present already.

That's it. Even curl should work out-of-the-box, just like browsers such as Chrome and Firefox:

1 curl -I https://myapp.example.com

If you need to install the root certificate in other computers in the network (or VMs), it's located in ~/.ssl/my-root-ca.crt.pem. If the other computers are running Linux:

1 2 3 # The .crt extension is important sudo cp my-root-ca.crt.pem /usr/local/share/ca-certificates/my-root-ca.crt sudo update-ca-certificates

I didn't research about how to install them in other OS, so please let me know in the comments if you know and I'll update the article explaining the instructions for setting up VM guests of other operating systems.

I've also created a Docker container with a simple Ruby Rack application to generate those certs. The code is simple and is available at Github.

It's also published to Docker Hub.

You can give it a try here:

I hope you'll find it useful as much as I do ;)