Skip to content
ecdsa.com

Generate a CSR · OpenSSL

How to generate a CSR for an EC P-256 key with SANs with OpenSSL

One openssl req command generates a P-256 key and a CSR (PKCS#10 certificate signing request) with subject alternative names: -newkey ec with the P-256 curve creates the key, -subj sets the subject, and -addext adds the subjectAltName extension that CAs and browsers check. The run decodes the result with openssl req -text.

Tested with OpenSSL 3.5.7 (Debian 13).

csr.sh — new P-256 key and CSR with two SANs
# New P-256 key (key.pem, unencrypted) and a CSR (csr.pem) with two SANs, in one command
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -noenc \
-keyout key.pem -out csr.pem \
-subj "/C=US/O=Example Corp/CN=api.example.com" \
-addext "subjectAltName=DNS:api.example.com,DNS:www.example.com"
a CSR for an existing key, for example on renewal
# For a key you already have: -key instead of -newkey
openssl req -new -key key.pem -out renew.csr \
-subj "/C=US/O=Example Corp/CN=api.example.com" \
-addext "subjectAltName=DNS:api.example.com,DNS:www.example.com"
openssl req -in renew.csr -noout -text | grep -A1 'Subject Alternative Name'

How it works

  1. -newkey ec -pkeyopt ec_paramgen_curve:P-256 generates the key; -keyout key.pem writes it as unencrypted PKCS#8 because of -noenc.
  2. -subj "/C=US/O=Example Corp/CN=api.example.com" sets the subject without prompting; the order of the components is the order in the CSR.
  3. -addext "subjectAltName=DNS:…,DNS:…" puts the SAN extension into the request's extensionRequest attribute — every host name the certificate must cover goes here, including the one in the CN.
  4. -key key.pem instead of -newkey makes a request for a key you already have, which is how renewals keep the same key.

Gotchas

  • Without -noenc, OpenSSL asks for a pass phrase to encrypt the key; in a script without a terminal that fails with UI routines:UI_process:processing error … problems getting password. -nodes still works but is deprecated since OpenSSL 3.0.
  • Browsers and most TLS clients ignore the CN when matching host names: a CSR without SANs leads to a certificate that fails with name-mismatch errors. List every name in subjectAltName.
  • -addext needs OpenSSL 1.1.1 or later. Older versions take extensions only from a config file section named by -reqexts.
  • Many CAs ignore everything in a CSR except the public key and the names; key usage, validity and other extensions you request may be replaced by the CA's own profile.

Related recipes