OpenSSL Cheat Sheet

From Carini MediaWiki
Jump to navigation Jump to search

OpenSSL Cheat Sheet

Comandi più comuni con OpenSSL

Verifica da file

  • Chiave privata: openssl rsa -check -noout -in ./private.key → Restituisce RSA key ok oppure RSA key error: [...]
  • Certificato X.509: openssl x509 -subject -ext subjectAltName -dates -noout -in ./public.pem → Restituisce il subject e la validità
  • CSR: openssl req -text -noout -verify -in ./request.csr → Restituisce OK
  • Coerenza tra certificato e chiave privata: openssl x509 -noout -modulus -in ./public.pem | openssl md5 deve coincidere con openssl rsa -noout -modulus -in ./private.key | openssl md5

Operazioni file PKCS#12

I file .pfx / .p12 (PKCS#12) contengono chiavi private, certificati e catene CA in un unico archivio protetto da password.

  • Chiave privata: openssl pkcs12 -legacy[1] -nocerts -nodes -in ./store.pfx -out ./private.key[pkcs12 1]
  • Certificato: openssl pkcs12 -legacy[1] -clcerts -nokeys -in ./store.pfx -out ./public.pem
  • CA: openssl pkcs12 -legacy[1] -cacerts -chain -nokeys -in ./store.pfx -out ./CA.pem

Conversione formati

  • PEM → DER: openssl x509 -in ./cert.pem -outform der -out ./cert.der
  • DER → PEM: openssl x509 -in ./cert.der -inform der -out ./cert.pem
  • PEM → PKCS#12 (.pfx): openssl pkcs12 -export -inkey ./private.key -in ./cert.pem -certfile ./CA.pem -out ./bundle.pfx
  • PKCS#12 (.pfx) → PEM (chiave + cert): openssl pkcs12 -in ./bundle.pfx -out ./bundle.pem -nodes

Generazione chiavi e CSR

  • Chiave RSA 2048 bit: openssl genrsa -out ./private.key 2048
  • Chiave ECC (P-256): openssl ecparam -name prime256v1 -genkey -noout -out ./private.key
  • CSR da chiave esistente: openssl req -new -key ./private.key -out ./request.csr
  • CSR + chiave in un colpo: openssl req -new -newkey rsa:2048 -nodes -keyout ./private.key -out ./request.csr

Certificati self-signed (test/dev)

  • Autocertificazione 365 giorni (RSA): openssl req -x509 -newkey rsa:2048 -nodes -keyout ./private.key -out ./selfsigned.pem -days 365
  • Autocertificazione ECC (P-256): openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -keyout ./private.key -out ./selfsigned.pem -days 365

Verifica da sito (online)

  • Certificato X.509: openssl s_client -connect <FQDN>:443 -servername <FQDN> -showcerts </dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName → Restituisce subject, issuer, SAN e validità.

Note e riferimenti

  1. Opzioni principali PKCS#12: -nocerts (solo chiave), -clcerts (solo certificato client), -cacerts (solo CA), -nodes (senza cifratura)
  1. 1.0 1.1 1.2 Necessario se si incontra l'errore inner_evp_generic_fetch:unsupported, dovuto al fatto che in OpenSSL 3.x alcuni algoritmi, come RC2, sono stati deprecati