1.2.6 Creating Certificates Valid for Multiple Hostnames
By default, certificates produced by OpenSSL have only one common name and are valid for only one hostname. Because of this, even if you have related web sites, you are forced to use a separate certificate for each site. In this situation, using a single multidomain certificate makes much more sense. Further, even when you’re running a single web site, you need to ensure that the certificate is valid for all possible paths that end users can take to reach it. In practice, this means using at least two names, one with the www
prefix and one without (e.g., www.feistyduck.com
and feistyduck.com
).
There are two mechanisms for supporting multiple hostnames in a certificate. The first is to list all desired hostnames using an X.509 extension called Subject Alternative Name (SAN). The second is to use wildcards. You can also use a combination of the two approaches when it’s more convenient. In practice, for most sites, you can specify a bare domain name and a wildcard to cover all the subdomains (e.g., feistyduck.com
and *.feistyduck.com
).
When a certificate contains alternative names, all common names are ignored. Newer certificates produced by CAs may not even include any common names. For that reason, include all desired hostnames on the alternative names list.
First, place the extension information in a separate text file. I’m going to call it fd.ext
. In the file, specify the name of the extension (subjectAltName
) and list the desired hostnames, as in the following example:
subjectAltName = DNS:*.feistyduck.com, DNS:feistyduck.com
Then, when using the x509
command to issue a certificate, refer to the file using the -extfile
switch:
$ openssl x509 -req -days 365 \
-in fd.csr -signkey fd.key -out fd.crt \
-extfile fd.ext
The rest of the process is no different from before. But when you examine the generated certificate afterward (see the next section), you’ll find that it contains the SAN extension:
X509v3 extensions:
X509v3 Subject Alternative Name:
DNS:*.feistyduck.com, DNS:feistyduck.com