Skip to content

Instantly share code, notes, and snippets.

@kkroesch
Last active April 2, 2025 06:59
Show Gist options
  • Save kkroesch/915a21fb69761d5e6a9168300a4426c9 to your computer and use it in GitHub Desktop.
Save kkroesch/915a21fb69761d5e6a9168300a4426c9 to your computer and use it in GitHub Desktop.
Certificate creation with OpenSSL for devices with Subject Alternative Names (SAN)
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
prompt = no
[req_distinguished_name]
CN = mydevice.example.com
[v3_req]
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = mydevice.example.com
DNS.2 = otherdevice.example.com
DNS.3 = foo.example.com
DNS.4 = bar.example.com
pkey:
# Create private key.
# Most certification authorities only support RSA alogrithm.
openssl genpkey -algorithm RSA -out device.key
pkeyec:
# Create private key with elliptic curve algorithm.
openssl ecparam -name prime256v1 -genkey -noout -out device.key
csr:
# Create certificate request (CSR) from private key and info from device.conf.
just pkey
openssl req -new -key device.key -out device.csr -config device.cnf
sign:
# Self-sign the CSR.
openssl x509 -req -in device.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out device.crt -days 365 -sha256 -extfile device.cnf -extensions v3_req
p12:
# Create a PKCS12 keystore from certificate chain.
[ -f fullchain.crt ] || cat device.crt intermediate.crt ca.crt > fullchain.crt
openssl pkcs12 -export -out device.p12 -inkey device.key -in fullchain.crt -nodes -name "Device Certificate"
clean:
# Remove generated files.
rm device.crt device.key device.csr
export:
# Export keys and certificates from keystore.
openssl pkcs12 -in device.p12 -passin pass: -nocerts -nodes -out exported_device.key
openssl pkcs12 -in device.p12 -passin pass: -nokeys -out exported_device.crt
openssl pkcs12 -in device.p12 -passin pass: -cacerts -nokeys -out exported_ca.crt
@kkroesch
Copy link
Author

kkroesch commented Mar 7, 2025

SANCert

Install OpenSSL and just before using it and adapt the device.cnf for your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment