322 Words

Certificate Signing Request Best Practices Using OpenSSL

A Certificate Signing Request (CSR) can be generated with OpenSSL. When you create a CSR, you generate two files: a private key (.key file) and a certificate signing request (.csr file).

The best way to generate a CSR is to create a request configuration file that contains all the certificate details. In this case I call the config file request.cnf.

[ req ]
default_bits        = 4096
default_md          = sha256
attributes          = req_attributes
distinguished_name  = req_distinguished_name
prompt		    = no
req_extensions      = v3_req


[ req_distinguished_name ]
countryName                = NL
stateOrProvinceName        = Noord-Holland
localityName               = Amsterdam
organizationName           = My company Name
organizationalUnitName     = IT
commonName		   = *.domain.tld
emailAddress		   = admin@domain.tld

[v3_req]
subjectKeyIdentifier = hash
basicConstraints = critical, CA:false
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names


[ req_attributes ]

[ req_ext ]
subjectAltName = @alt_names

[alt_names]
DNS.1   = domain.tld
DNS.2   = *.domain.tld

After creating this config file, you can generate the key and the CSR. Make sure you never share the .key file: it is your private key.

openssl req -out domain_tld.csr -newkey rsa:4096 -nodes -keyout ./domain_tld.key -config ./request.cnf

With the .csr file you can request an SSL certificate from your certificate authority.

Without a config file (OpenSSL 1.1.1 and newer)

For a quick CSR you can put everything on the command line, including the SANs:

openssl req -new -newkey rsa:4096 -nodes -keyout domain_tld.key -out domain_tld.csr \
  -subj "/C=NL/ST=Noord-Holland/L=Amsterdam/O=My company Name/CN=*.domain.tld" \
  -addext "subjectAltName=DNS:domain.tld,DNS:*.domain.tld"

An ECDSA key is smaller and faster than RSA, and every browser supports it: use -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 instead of -newkey rsa:4096.

Public websites: let ACME do it

Public certificates are valid for ever shorter periods (from 2026 at most 200 days, going down to 47 days in 2029). For public websites, let an ACME client such as certbot or acme.sh request and renew Let’s Encrypt certificates automatically. A CSR by hand is still useful for internal servers and paid certificates.