Wrangling obscure OpenSSL functions to create and publish SSL certificates has always been kind of a mess. If you want(ed) to create a valid self-signed certificate for multi domains or, at least, example.com and www.example.com, you most likely were out of luck.
There is a lot of wrong or partial documentation on the subject, but is… well… wrong and/or incomplete. It is thus time for another episode of AntiFUD.
You have multiple paths of the same website to cover for, but a single CN. If you use example.com then www.example.com will result in invalid SSL certificate, and vice versa. Suppose you have the following domain names:
In such a scenario there is no real victory no matter what you choose to use as a CN: the most used wildcard CN, *.example.com, is of no use either because it matches with www.example.com and user.example.com, but not with username1.user.example.com. The only way to address all these issues is to create and sign a X.509 v3 SSL certificate, to allow SAN. The SAN extension has been introduce to resolve all of these problems, allowing the validity of multiple domains/subdomains within the same certificate.
Creating the certificate
We have to start by creating an alternative configuration file to use with OpenSSL, and list the server names we need. As mentioned below we also have to enable the usage of v3 extensions.
# mkdir certificates # cd certificates # cp /etc/ssl/openssl.cnf ./example-com.cnf
We can now edit the file and adjust as needed:
[ req ] x509_extensions = v3_ca req_extensions = v3_req [ usr_cert ] keyUsage = nonRepudiation, digitalSignature, keyEncipherment [ v3_req ] basicConstraints = CA:FALSE keyUsage = nonRepudiation, digitalSignature, keyEncipherment subjectAltName=@alt_names [ v3_ca ] subjectAltName=@alt_names [ alt_names ] DNS.1 = example.com DNS.2 = www.example.com DNS.3 = *.host.example.com
In the default file, parameters such as req_extensions and keyUsage are commented out, while subjectAltName is missing. We have to add it to v3_req and v3_ca, and create the respective section. It can be created anywhere in the file, but it is generally appended to the bottom. Since the CN is (or, at least, should be) ignored in the presence of SAN, we insert all the names in the alt_names field.
With the configuration in place we can now create the certificate:
# openssl genrsa -out example-com.key 4096 # openssl req -new -config example-com.cnf -key example-com.key -out example-com.csr # openssl x509 -req -in example-com.csr -CA rootCA.pem -CAkey rootCA.key -CAserial rootCA.srl -out example-com.crt -days 365 -extfile example-com.cnf -extensions v3_ca
The deviation from the standard procedure is the addition of the v3 during the CA sign. We do this by using -extfile example-com.cnf to use the custom configurations, and specifying -extensions v3_ca to make sure SAN are passed through and saved in the signed certificate.
To make sure it worked you can do the following:
# openssl x509 -in example-com.crt -text -noout […] X509v3 extensions: X509v3 Subject Alternative Name: DNS:example.com, DNS:www.example.com, DNS:*.user.example.com X509v3 Subject Key Identifier: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx X509v3 Authority Key Identifier: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx X509v3 Basic Constraints: CA:TRUE […]
The only thing left to do is to set up the certificates in the server, and everything will work as intended.