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

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 -sha256 \
    -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 \
    -sha256 \
    -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.)

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.

[ req ]
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 = Brighton
organizationName = Organization Name (eg, company)
organizationName_default = Hallmarkdesign
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 = your-website.dev
DNS.2 = another-website.dev
@johnclary
Copy link

johnclary commented Feb 21, 2018

This was tremendously helpful--thanks!

@benjibee
Copy link

benjibee commented Mar 2, 2018

This has been working great for my local development setup until a recent PHP-built project. It uses file_get_contents() and I've started getting this PHP error which seems to have 100+ fixes, but I have a feeling it's something to do with these certs not being properly registered:


file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in: [path to PHP file]

😢

@friendscottn
Copy link

friendscottn commented Mar 26, 2018

THANK YOU!

@michael-pratt
Copy link

michael-pratt commented May 11, 2018

Many thanks! I couldn't figure out why my SANs weren't carrying over from the CSR to the final cert. This was a big help!

@mcolacino
Copy link

mcolacino commented Jul 10, 2018

THANKS A LOT MAN!

@Zextremo
Copy link

Zextremo commented Aug 21, 2018

THANKS

@pserrano
Copy link

pserrano commented Sep 18, 2018

i would also add to ssl.conf

organizationalUnitName          = Organizational Unit Name (eg, section)
organizationalUnitName_default    = IT

@jorgejesus
Copy link

jorgejesus commented Jan 31, 2019

Your ssl.conf help me alot !!!

@boh717
Copy link

boh717 commented Mar 8, 2019

Thanks, it worked like a charm!

Would you please add -sha256 option to the signing process?

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

Without that option, certificate will be signed with SHA1 (which is deprecated).

@shikaku
Copy link

shikaku commented Mar 13, 2019

Thanks, very helpful!

@carlosyague
Copy link

carlosyague commented Jun 3, 2019

Thank you so much @croxton!!

@alexwitedja
Copy link

alexwitedja commented Jun 30, 2019

I'm getting error
NET::ERR_CERT_AUTHORITY_INVALID

How do i do this sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain private.crt in windows?

@Trollazzo
Copy link

Trollazzo commented Jul 30, 2019

Thank you for this marvelous work

@amnkh
Copy link

amnkh commented Sep 4, 2019

i would also add to ssl.conf

organizationalUnitName          = Organizational Unit Name (eg, section)
organizationalUnitName_default    = IT

Thanks @croxton and @pserrano,
I added organizationalUnitName, emailAddress and different SAN examples to make Wildcard usage more clear.

[ req ]
default_bits       = 4096
distinguished_name = req_distinguished_name
req_extensions     = req_extensions_section
prompt = yes

[ req_distinguished_name ]
countryName                     = Country Name (2 letter code)
countryName_min                 = 2
countryName_max                 = 2
countryName_default             = GB
stateOrProvinceName             = State or Province Name (full name)
stateOrProvinceName_default     = England
localityName                    = Locality Name (eg, city)
localityName_default            = Brighton
organizationName                = Organization Name (eg, company)
organizationName_default        = Hallmarkdesign
organizationalUnitName          = Organizational Unit Name (eg, section)
organizationalUnitName_default  = IT
commonName                      = Common Name (eg, server FQDN or YOUR name)
commonName_max                  = 64
commonName_default              = example.com
emailAddress                    = Email Address (eg, admin@example.com)
emailAddress_max                = 64
emailAddress_default            = info@example.com

[ req_extensions_section ]
subjectAltName = @subject_alternative_name_section

[ subject_alternative_name_section ]
DNS.1   = subdomain.example.com
DNS.2   = *.example.com
DNS.3   = www.example.org
DNS.4   = *.example.org

Result:

Subject: C = GB, ST = England, L = Brighton, O = Hallmarkdesign, OU = IT, CN = example.com, emailAddress = info@example.com
Requested Extensions:
            X509v3 Subject Alternative Name:
                DNS:subdomain.example.com, DNS:*.example.com, DNS:www.example.org, DNS:*.example.org

@HighSoftWare96
Copy link

HighSoftWare96 commented Sep 13, 2019

SUPER-useful!

@metajiji
Copy link

metajiji commented Dec 3, 2019

As of OpenSSL 1.1.1, providing subjectAltName directly on command line becomes much easier:

openssl req -x509 \
    -nodes \
    -subj "/CN=$1" \
    -newkey rsa:2048 \
    -keyout key.pem \
    -out cert.pem \
    -addext "subjectAltName=DNS:$1" \
    -days 365

More info here: https://security.stackexchange.com/questions/74345/provide-subjectaltname-to-openssl-directly-on-the-command-line

Also python universal script:

import sys
import ssl

LISTEN_ADDR = 'localhost'
LISTEN_PORT = 4443

if sys.version_info.major == 2:
    import BaseHTTPServer, SimpleHTTPServer
    httpd = BaseHTTPServer.HTTPServer((LISTEN_ADDR, LISTEN_PORT), SimpleHTTPServer.SimpleHTTPRequestHandler)
else:
    import http.server
    httpd = http.server.HTTPServer((LISTEN_ADDR, LISTEN_PORT), http.server.SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./cert.pem', keyfile='./key.pem', server_side=True)
httpd.serve_forever()

@dspencerr
Copy link

dspencerr commented Dec 4, 2019

Thank you. This was incredibly helpful after a very long wrestle!

@ertyagi
Copy link

ertyagi commented Dec 6, 2019

This was so helpful!!

@paullacour
Copy link

paullacour commented Jan 14, 2020

Thanks!!

@KseniiaPelykh
Copy link

KseniiaPelykh commented Mar 25, 2020

Thanks, it was really helpful!

@neil40m
Copy link

neil40m commented Apr 12, 2020

@eriksalhus
Copy link

eriksalhus commented Apr 24, 2020

Thank you so much! You are a life saver!
excited

@daareiza
Copy link

daareiza commented May 8, 2020

wow man, you saved my life, thank you so much.

@ddfault
Copy link

ddfault commented May 27, 2020

Thank you for this post!!!! But had to dig further to get all the functionality I wanted. Here's the ssl.conf I ended up with.

[req]
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        = Brighton
organizationName            = Organization Name (eg, company)
organizationName_default    = Hallmarkdesign
commonName                  = Common Name (e.g. server FQDN or YOUR name)
commonName_max              = 64
commonName_default          = localhost

[req_ext]
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names

[alt_names]
DNS.1   = domainexample.com
DNS.2   = sub.domainexample.com
IP.1    = 169.254.124.124

Here was my commandline
"openssl.exe" x509 -req -days 730 -in request.req -CA ca.crt -CAkey ca.key -set_serial 02 -extensions req_ext -extfile ssl.conf -out request.crt

This got me a cert with key usage, extended key usage, and the subject alternative names I was looking for!

These were the other pages that helped me.
http://apetec.com/support/GenerateSAN-CSR.htm
https://www.openssl.org/docs/manmaster/man5/x509v3_config.html

This extra stuff was all in the request, but was ignored and not added to the output cert. Not sure how to pull from the request, but hand coding into the ssl.conf got me the one-off certificate I needed with all the stuff.

@BullwinkleDev
Copy link

BullwinkleDev commented Aug 31, 2020

It works like magic!
Thank you so much!!!

@v-andrius
Copy link

v-andrius commented Feb 5, 2021

one liner to generate self-signed certificate with subjectAltName for testing:

openssl req -new -newkey rsa:2048 -nodes \
  -subj "/C=GB/ST=England/L=Brighton/O=Example/CN=*.example.com/emailAddress=info@example.com" \
  -reqexts SAN \
  -config <(cat /etc/ssl/openssl.cnf \
        <(printf "\n[SAN]\nsubjectAltName=DNS:example.com,DNS:www.example.com")) \
  -keyout example_com.key \
  -x509 -days 3650 -extensions SAN -out example_com.crt

Verify certificate:

openssl x509 -in /tmp/example.com.crt -noout -text

@wiwan-wijoyo
Copy link

wiwan-wijoyo commented Feb 27, 2021

Thanks, it worked..

@kevinsulatra
Copy link

kevinsulatra commented Mar 25, 2021

Thank you very much. It worked.

@masayyed
Copy link

masayyed commented Dec 22, 2021

Thanks Thanks a lot man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment