Skip to content

OpenSSL

19 commands

The ubiquitous toolkit for TLS and general cryptography: inspect certificates, generate keys and CSRs, convert formats, hash, encrypt and create random secrets.

  • TLS
  • certificates
  • cryptography
  • encoding

Install OpenSSL

sudo apt install openssl

Inspecting certificates5

openssl s_client -connect <host>:<port>

Opens a TLS connection and prints the certificate chain and negotiated parameters.

Link
openssl s_client -connect <host>:443 -servername <host> </dev/null 2>/dev/null | openssl x509 -noout -text

Fetches and fully decodes a server certificate, SNI included.

Link
openssl x509 -in <file> -noout -text

Decodes a certificate file to human-readable form.

Link
openssl x509 -in <file> -noout -dates

Just the validity dates — handy for expiry checks.

Link
openssl x509 -in <file> -noout -fingerprint -sha256

The SHA-256 fingerprint of a certificate.

Keys and CSRs5

openssl genrsa -out <file> <bits>

Generates an RSA private key.

Link
openssl ecparam -genkey -name prime256v1 -out <file>

Generates an elliptic-curve private key.

Link
openssl req -new -key <key> -out <csr>

Creates a certificate signing request.

Link
openssl req -x509 -newkey rsa:4096 -keyout <key> -out <cert> -days <n> -nodes

Creates a self-signed certificate and key in one step.

Link
openssl rsa -in <key> -noout -check

Validates an RSA private key.

Link

Converting formats3

openssl pkcs12 -export -out <p12> -inkey <key> -in <cert>

Bundles a key and certificate into a PKCS#12 file.

Link
openssl pkcs12 -in <p12> -nodes -out <pem>

Extracts a key and certificate from a .p12.

Link
openssl x509 -in <der> -inform der -out <pem> -outform pem

Converts DER to PEM.

Link

Hashing, encoding and crypto6

openssl dgst -sha256 <file>

SHA-256 digest of a file.

Link
openssl rand -base64 <n>

Generates cryptographically random bytes — a quick password or token.

Link
openssl rand -hex <n>

Random bytes as hex.

Link
openssl enc -aes-256-cbc -salt -pbkdf2 -in <in> -out <out>

Encrypts a file with AES; add -d to decrypt. Always use -pbkdf2.

Link
openssl passwd -6 <password>

Generates a SHA-512 crypt hash suitable for /etc/shadow.

Link
echo -n "<text>" | openssl base64

Base64-encodes input; add -d to decode.

Link