Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate ssl certificates with Subject Alt Names

Generate ssl certificates with Subject Alt Names on OSX

We're going to generate a key per project which includes multiple fully qualified domains. This key can be checked into the project repo as it's intended for local development but never used on production servers.

Save ssl.conf to your my_project directory.

Open ssl.conf in a text editor.

Edit the domain(s) listed under the [alt_names] section so that they match the local domain name you want to use for your project, e.g.

DNS.1   = my-project.dev

Additional FQDNs can be added if required:

DNS.1   = my-project.dev
DNS.2   = www.my-project.dev
DNS.3   = fr.my-project.dev

In terminal

`cd my_project`

Generate a private key

openssl genrsa -out private.key 4096

Generate a Certificate Signing Request

openssl req -new -sha256 \
    -out private.csr \
    -key private.key \
    -config ssl.conf 

Check the CSR. You should see X509v3 Subject Alternative Name: DNS:my-project.dev, DNS:www.my-project.dev and Signature Algorithm: sha256WithRSAEncryption

openssl req -text -noout -in private.csr

Generate the certificate

openssl x509 -req \
    -days 3650 \
    -in private.csr \
    -signkey private.key \
    -out private.crt \
    -extensions req_ext \
    -extfile ssl.conf

Add the certificate to keychain and trust it

sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain private.crt

(Alternatively, double click on the certificate file private.crt to open Keychain Access. Your project name my_project will be listed under the login keychain. Double click it and select 'Always trust' under the 'Trust' section.)

Restart apache

sudo apachectl -k restart
#!/usr/bin/env bash
openssl genrsa -out private.key 4096
openssl req -new -sha256 -out private.csr -key private.key -config ssl.conf
openssl req -text -noout -in private.csr
openssl x509 -req -days 3650 -in private.csr -signkey private.key -out private.crt -extensions req_ext -extfile ssl.conf
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain private.crt
sudo apachectl -k restart
[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
req_extensions = req_ext
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = AU
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = New South Wales
localityName = Locality Name (eg, city)
localityName_default = Newcastle
organizationName = Organization Name (eg, company)
organizationName_default = Newism
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_max = 64
commonName_default = localhost
[ req_ext ]
subjectAltName = @alt_names
[alt_names]
DNS.1 = local.newism.com.au
DNS.2 = www.newism.com.au
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.