OpenSSL for Hacker
π‘οΈ Mastering OpenSSL
OpenSSL is a robust, full featured open source toolkit for Transport Layer Security (TLS) and Secure Sockets Layer (SSL). This cheat sheet walks you through generating, inspecting, and converting cryptographic keys and certificates.
β οΈ Learn how to use OpenSSL for generating RSA, DSA, and EC keys, creating CSRs and self signed certificates, and converting between PEM, DER, and PFX formats.
π Generate Public and Private Keys
π RSA Keys
Generate a 2048 bit RSA private key
1openssl genrsa -out KEY1.pem 2048Generate a 4096 bit RSA private key encrypted with AES 128
1openssl genrsa -out KEY2.pem -aes128 4096You can also use -aes192, -aes256, -des3, or -des for different encryption.
π DSA Keys
Generate DSA parameter file
1openssl dsaparam -out DSA-PARAM.pem 1024Generate DSA key using parameters
1openssl gendsa -out DSA-KEY.pem DSA-PARAM.pGenerate parameters and key in one step
1openssl dsaparam -genkey -out DSA-PARAM-KEY.pem 2048π EC (Elliptic Curve) Keys
Generate EC parameters
1openssl genpkey -genparam -algorithm EC -pkeyopt ec_paramgen_curve:secp384r1 -out EC-PARAM.pemGenerate EC key using parameters
1openssl genpkey -paramfile EC-PARAM.pem -out EC-KEY.pemGenerate EC key directly
1openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-384 -out EC-KEY.pemList all supported EC curves
1openssl ecparam -list_curvesπ Generate Certificate Signing Requests (CSRs) and Self-Signed Certificates
π Certificate Signing Requests (CSR)
Generate CSR from existing private key
1openssl req -new -key KEY.pem -out CSR.pemGenerate new private key and CSR
1openssl req -new -newkey rsa:2048 -nodes -keyout KEY.pem -out CSR.pemπ§Ύ Generate Self Signed Certificates
Generate Certificate with existing Private Key file
1openssl req -x509 -key KEY.pem -out CERT.pemGenerate Certificate and new Private Key file
1openssl req -x509 -newkey -nodes -out CERT.pemπ Inspect Keys
π RSA Keys
Convert an RSA Private Key into text
1openssl rsa -in KEY.pem -noout -textRemove encryption from an RSA key file
1openssl rsa -in ENCRYPTED-KEY.pem -out KEY.pemEncrypt an RSA Key File
1openssl rsa -in KEY.pem -aes128 -out ENCRYPTED-KEY.pemπ DSA Keys
Inspect DSA Parameters file
1openssl dsaparam -in DSA-PARAM.pem -text -nooutInspect DSA Private Key file
1openssl dsa -in DSA-KEY.pem -text -nooutπ EC Keys
Inspect Elliptic Curve (EC) Parameters file
1openssl ecparam -in EC-PARAM.pem -text -nooutInspect Elliptic Curve (EC) Private Key file
1openssl ec -in EC-KEY.pem -text -nooutπ¦ Inspect any Key file using pkey utility
Convert any Private Key file into text (RSA, DSA, or EC)
1openssl pkey -in KEY.pem -noout -textExtract only Public Key as text from any Key file
1openssl pkey -in KEY.pem -noout -text_pubExtract only Public Key in PEM format
1openssl pkey -in KEY.pem -puboutπ Check if RSA Key matches a CSR or Cert
Compare Modulus values to see if files match each other
1openssl rsa -in KEY.pem -noout -modulus
2openssl req -in CSR.pem -noout -modulus
3openssl x509 -in CERT.pem -noout -modulusπ Check if EC Key matches a CSR or Cert
Compare Public Key values to see if files match each other
1openssl req -in EC-CSR.pem -noout -pubkey
2openssl x509 -in EC-CERT.pem -noout -pubkey
3openssl ec -in EC-KEY.pem -puboutπ Inspect Certificate Signing Requests (CSRs) and Certificates
π View contents of Certs and CSRs
View x509 Certificate as human readable Text
1openssl x509 -in CERT.pem -noout -textView Certificate Signing Request (CSR) contents as Text
1openssl req -in CSR.pem -noout -textπ Extract Specific Info from Certificates
Extract specific pieces of information from x509 Certificates
1openssl x509 -in CERT.pem -noout -dates
2openssl x509 -in CERT.pem -noout -issuer βsubjectπ Extract x509 Certificate Extensions
Extract specific Extension(s) from a certificate
1openssl x509 -in CERT.pem -noout -ext subjectAltName
2openssl x509 -in CERT.pem -noout -ext authorityInfoAccess,crlDistributionPointsExtract all Extensions from a certificate
1openssl x509 -in CERT.pem -noout -text | sed '/X509v3 extensions/,/Signature Algorithm:/!d'π File Formats and Converting between formats (PEM, DER, PFX)
π Check if file is PEM, DER, or PFX
Check if file is PEM format
1openssl x509 -in FILECheck if file is DER format
1openssl x509 -in FILE -inform DERCheck if file is PFX format
1openssl pkcs12 -in FILE -nodesπ Convert Between Formats
Convert PEM Certificate file to DER
1openssl x509 -in CERT.pem -outform DER -out CERT.derConvert DER Certificate file to PEM
1openssl x509 -in CERT.der -inform der -out CERT.pemConvert PEM Certificate(s) to PFX
1openssl pkcs12 -in CERTS.pem -nokeys -export -out CERTS.pfxExtract everything within a PFX file as a PEM file
1openssl pkcs12 -in FILE.pfx -out EVERYTHING.pem -nodesExtract only the Private Key from a PFX file as PEM
1openssl pkcs12 -in FILE.pfx -out KEY.pem -nodes -nocertsπ§© Final Thoughts
OpenSSL is an indispensable tool in any security conscious developer or DevOps engineer's toolkit. Whether youβre issuing CSRs, managing certificates, or debugging TLS issues, this guide should have you covered.