# New P-256 key (key.pem, unencrypted) and a CSR (csr.pem) with two SANs, in one commandopenssl 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"# For a key you already have: -key instead of -newkeyopenssl 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
-newkey ec -pkeyopt ec_paramgen_curve:P-256generates the key;-keyout key.pemwrites it as unencrypted PKCS#8 because of-noenc.-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.-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.-key key.peminstead of-newkeymakes 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 withUI routines:UI_process:processing error … problems getting password.-nodesstill 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. -addextneeds 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
- How to create a self-signed EC P-256 certificate with SANs with OpenSSL
- How to generate an ECDSA key pair with OpenSSL
- How to decode an X.509 certificate with OpenSSL
- How to generate a CSR for an EC P-256 key with SANs in Python
- How to generate a CSR for an EC P-256 key with SANs in Go
- How to generate a CSR for an EC P-256 key with SANs in Node.js