# P-256 key and a self-signed TLS server certificate, valid 365 daysopenssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -noenc \ -keyout key.pem -out cert.pem -days 365 \ -subj "/CN=localhost" \ -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" \ -addext "basicConstraints=critical,CA:FALSE" \ -addext "keyUsage=critical,digitalSignature" \ -addext "extendedKeyUsage=serverAuth"openssl x509 -in cert.pem -noout -subject -dates -ext subjectAltNameHow it works
req -x509makes a self-signed certificate instead of a CSR;-newkey ec -pkeyopt ec_paramgen_curve:P-256generates the key, and-noencwrites key.pem unencrypted.-subj "/CN=localhost"becomes both the subject and the issuer;-days 365counts from the current time, which is the notBefore.subjectAltName=DNS:localhost,IP:127.0.0.1lists the names clients match:DNS:entries for host names,IP:entries for addresses.basicConstraints=critical,CA:FALSE,keyUsage=critical,digitalSignatureandextendedKeyUsage=serverAuthmake it a leaf certificate for TLS servers; inspect.sh prints single fields with-datesand-ext subjectAltName.
Gotchas
- Without
-addext "basicConstraints=…",req -x509applies thev3_casection of the default openssl.cnf, and the certificate comes out as a CA —basicConstraints: critical, CA:TRUE— with no key usage at all. - Without
-days, the certificate is valid for 30 days only. DNS:127.0.0.1is not an IP SAN: clients that connect by address reject it, andopenssl verify -verify_ip 127.0.0.1fails witherror 64 at 0 depth lookup: IP address mismatch. WriteIP:127.0.0.1.- Clients still have to be told to trust the file:
openssl verify cert.pemfails witherror 18 at 0 depth lookup: self-signed certificateand curl withcurl: (60) SSL certificate problem: self-signed certificateuntil you pass-CAfile cert.pemor--cacert cert.pem.
Related recipes
- How to generate a CSR for an EC P-256 key with SANs with OpenSSL
- How to decode an X.509 certificate with OpenSSL
- How to generate an ECDSA key pair with OpenSSL
- How to create a self-signed EC P-256 certificate with SANs in Python
- How to create a self-signed EC P-256 certificate with SANs in Go
- How to create a self-signed EC P-256 certificate with SANs in Node.js