Skip to content

Instantly share code, notes, and snippets.

@abuxton
Forked from croxton/SSL-certs-OSX.md
Last active July 22, 2022 15:07
Show Gist options
  • Save abuxton/4f43ef8731fa00768440aa9cd8dbee89 to your computer and use it in GitHub Desktop.
Save abuxton/4f43ef8731fa00768440aa9cd8dbee89 to your computer and use it in GitHub Desktop.
Generate ssl certificates with Subject Alt Names
# Created by https://www.toptal.com/developers/gitignore/api/ssl
# Edit at https://www.toptal.com/developers/gitignore?templates=ssl
*.csr
*.pem
*.key
*.crt
# End of https://www.toptal.com/developers/gitignore/api/ssl

Generate SSL certificates with Subject Alt Names

Usage

Create a suitable folder or navigate to a suitable file path and clone this gist

git clone https://gist.github.com/abuxton/4f43ef8731fa00768440aa9cd8dbee89 foldername
cd foldername

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

Create a directory for your project, e.g. my_project and save ssl.conf inside it.

Open Terminal and navigate to 'my_project':

cd my_project

Generate a private key:

openssl genrsa -out private.key 4096

Generate a Certificate Signing Request

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

(You will be asked a series of questions about your certificate. Answer however you like, but for 'Common name' enter the name of your project, e.g. my_project)

Now check the CSR:

openssl req -text -noout -in private.csr

You should see this:

X509v3 Subject Alternative Name: DNS:my-project.site and Signature Algorithm: sha256WithRSAEncryption

Generate the certificate

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

Add the certificate to keychain and trust it from command line:

    # darwin osx
    sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain private.crt
# ubuntu
$ sudo cp local-ca.crt /usr/local/share/ca-certificates
$ sudo update-ca-certificates

If you're using OSX 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.)

If you are using MAMP Pro, add (or edit) a host with the server name you listed under the [alt_names] section of your ssl.conf. On the SSL tab select the Certificate file and Certificate key that you just generated.

Save changes and restart Apache.

Links

Note on Common Name vs Subject Alternative Name

taken from https://support.dnsimple.com/articles/what-is-common-name/#:~:text=The%20Common%20Name%20(AKA%20CN,common%20name%20in%20the%20certificate.

The common name can only contain up to one entry: either a wildcard or non-wildcard name. It’s not possible to specify a list of names covered by an SSL certificate in the common name field.

The Subject Alternative Name extension (also called Subject Alternate Name or SAN) was introduced to solve this limitation. The SAN allows issuance of multi-name SSL certificates.

The ability to directly specify the content of a certificate SAN depends on the Certificate Authority and the specific product. Most certificate authorities have historically marketed multi-domain SSL certificates as a separate product. They’re generally charged at a higher rate than a standard single-name certificate.

On the technical side, the SAN extension was introduced to integrate the common name. Since HTTPS was first introduced in 2000 (and defined by the RFC 2818), the use of the commonName field has been considered deprecated, because it’s ambiguous and untyped.

The CA/Browser Forum has since mandated that the SAN would also include any value present in the common name, effectively making the SAN the only required reference for a certificate match with the server name. The notion of the common name survives mostly as a legacy of the past. There are active discussions to remove its use from most browsers and interfaces.

General OpenSSL Commands

These commands allow you to generate CSRs, Certificates, Private Keys and do other miscellaneous tasks. (branched from https://gist.github.com/davewongillies/7050080)

Generate a new private key and Certificate Signing Request

openssl req -out CSR.csr -new -newkey rsa:2048 -nodes -keyout privateKey.key

Generate a self-signed certificate (see How to Create and Install an Apache Self Signed Certificate for more info)

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt

Generate a certificate signing request (CSR) for an existing private key

openssl req -out CSR.csr -key privateKey.key -new

Generate a certificate signing request based on an existing certificate

openssl x509 -x509toreq -in certificate.crt -out CSR.csr -signkey privateKey.key

Remove a passphrase from a private key

openssl rsa -in privateKey.pem -out newPrivateKey.pem

Checking Using OpenSSL

If you need to check the information within a Certificate, CSR or Private Key, use these commands. You can also check CSRs and check certificates using our online tools.

Check a Certificate Signing Request (CSR)

openssl req -text -noout -verify -in CSR.csr

Check a private key

openssl rsa -in privateKey.key -check

Check a certificate

openssl x509 -in certificate.crt -text -noout

Check a PKCS#12 file (.pfx or .p12)

openssl pkcs12 -info -in keyStore.p12

Debugging Using OpenSSL

If you are receiving an error that the private doesn't match the certificate or that a certificate that you installed to a site is not trusted, try one of these commands. If you are trying to verify that an SSL certificate is installed correctly, be sure to check out the SSL Checker.

Check an MD5 hash of the public key to ensure that it matches with what is in a CSR or private key

openssl x509 -noout -modulus -in certificate.crt | openssl md5
openssl rsa -noout -modulus -in privateKey.key | openssl md5
openssl req -noout -modulus -in CSR.csr | openssl md5

Check an SSL connection. All the certificates (including Intermediates) should be displayed

openssl s_client -connect www.paypal.com:443

Converting Using OpenSSL

These commands allow you to convert certificates and keys to different formats to make them compatible with specific types of servers or software. For example, you can convert a normal PEM file that would work with Apache to a PFX (PKCS#12) file and use it with Tomcat or IIS. Use our SSL Converter to convert certificates without messing with OpenSSL.

Convert a DER file (.crt .cer .der) to PEM

openssl x509 -inform der -in certificate.cer -out certificate.pem

Convert a PEM file to DER

openssl x509 -outform der -in certificate.pem -out certificate.der

Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM

openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes

You can add -nocerts to only output the private key or add -nokeys to only output the certificates.

Convert a PEM certificate file and a private key to PKCS#12 (.pfx .p12)

openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt
[ req ]
default_bits = 2048
#default_bits = 4096
distinguished_name = req_distinguished_name
req_extensions = req_ext
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = GB
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = England
localityName = Locality Name (eg, city)
localityName_default = City of London
organizationName = Organization Name (eg, company)
organizationName_default = example.com
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_max = 64
commonName_default = localhost
emailAddress = Email Address
emailAddress_max = 64
[ req_ext ]
subjectAltName = @alt_names
[alt_names]
DNS.1 = local.host
DNS.2 = localhost
#!/usr/bin/bash
# lets generate a private key
openssl genrsa -out private.key 2048
# lets generate a csr
openssl req -new \
-out private.csr \
-key private.key \
-config ssl.conf
echo "to review the csr execute 'openssl req -text -noout -in private.csr' "
echo "to sign the request see ./README.md#L46"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment