A self-signed certificate can provide TLS encryption for a test or internal service when its clients are configured to trust that certificate. This guide explains how to create a wildcard certificate with Subject Alternative Names using OpenSSL.
Before you start
Use OpenSSL 1.1.1 or later for these command options, on a supported system. Replace example.com with your own domain. A wildcard such as *.example.com covers site.example.com, but not a.b.example.com; the example also includes the main domain separately.
A self-signed certificate is not automatically trusted by browsers or other clients. Establish trust through your organisation’s approved process. For a public website, obtain a certificate from a trusted certificate authority rather than asking visitors to bypass warnings.
Use a new private working directory and filenames that do not already exist. The examples create an unencrypted private key, so restrict access to it and never place it in a public website directory.
Create a certificate with a configuration file
-
Set restrictive permissions for newly created files in your current shell:
umask 077 -
Save the following as
san.cnf, changing the domain names:[req] prompt = no distinguished_name = dn req_extensions = req_ext [dn] CN = *.example.com [req_ext] subjectAltName = @alt_names [alt_names] DNS.1 = *.example.com DNS.2 = example.com -
Generate the private key and signing request:
openssl req -new -newkey rsa:2048 -nodes -sha256 -keyout wildcard.key -out wildcard.csr -config san.cnf -
Sign the certificate using that key and include the SAN extension:
openssl x509 -req -sha256 -days 365 -in wildcard.csr -signkey wildcard.key -extfile san.cnf -extensions req_ext -out wildcard.crt
Create the certificate in one command
As an alternative to the configuration-file method, run the following in a private directory after umask 077. It also includes SANs:
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -days 365 -keyout wildcard.key -out wildcard.crt -subj '/CN=*.example.com' -addext 'subjectAltName=DNS:*.example.com,DNS:example.com'-nodes keeps the private key unencrypted. OpenSSL 3 also supports -noenc for this purpose.
Check and install the certificate
Inspect the certificate before configuring the service:
openssl x509 -in wildcard.crt -noout -textCheck the validity dates and X509v3 Subject Alternative Name entries. Configure the service with wildcard.crt and wildcard.key, using the correct paths and access permissions. For Apache or NGINX, test the configuration before reloading the service; service names and configuration locations vary by system.
For an application that needs a password-protected PFX file, export one and enter a strong export password when prompted:
openssl pkcs12 -export -out wildcard.pfx -inkey wildcard.key -in wildcard.crtKeep the key and PFX private. Install the public certificate in client trust stores only where you are authorised to do so, and test the connection without disabling certificate verification.