# Sign CSR with CAopensslx509-req-inrequest.csr-CAca.crt-CAkeyca.key-CAcreateserial\-outcert.crt-days365# Sign with specific serial numberopensslx509-req-inrequest.csr-CAca.crt-CAkeyca.key-set_serial01\-outcert.crt-days365# Sign with SHA-256opensslx509-req-inrequest.csr-CAca.crt-CAkeyca.key-CAcreateserial\-outcert.crt-days365-sha256
# Sign with extensionsopensslx509-req-inrequest.csr-CAca.crt-CAkeyca.key-CAcreateserial\-outcert.crt-days365-extensionsv3_req-extfile<(cat<<EOF[v3_req]basicConstraints = CA:FALSEkeyUsage = digitalSignature, keyEnciphermentextendedKeyUsage = serverAuthsubjectAltName = @alt_names[alt_names]DNS.1 = example.comDNS.2 = www.example.comEOF)
# View certificate detailsopensslx509-incert.crt-text-noout
# View certificate subjectopensslx509-incert.crt-subject-noout
# View certificate issueropensslx509-incert.crt-issuer-noout
# View certificate datesopensslx509-incert.crt-dates-noout
# View certificate serial numberopensslx509-incert.crt-serial-noout
# View certificate fingerprint (SHA-256)opensslx509-incert.crt-fingerprint-sha256-noout
# View certificate fingerprint (SHA-1)opensslx509-incert.crt-fingerprint-sha1-noout
# View certificate in different formatsopensslx509-incert.crt-text# Textopensslx509-incert.crt-purpose# Purposeopensslx509-incert.crt-subject_hash# Subject hashopensslx509-incert.crt-issuer_hash# Issuer hash# Extract all SANsopensslx509-incert.crt-text-noout|grep"DNS:"# Check certificate expirationopensslx509-incert.crt-noout-enddate
# Check if certificate expires in 30 daysopensslx509-incert.crt-noout-checkend2592000
Verify Certificates
# Verify certificate against CAopensslverify-CAfileca.crtcert.crt
# Verify certificate chainopensslverify-CAfileca.crt-untrustedintermediate.crtcert.crt
# Verify certificate with CRLopensslverify-CAfileca.crt-CRLfilecrl.pem-crl_checkcert.crt
# Check if private key matches certificateopensslx509-incert.crt-noout-modulus|opensslmd5
opensslrsa-inprivate.key-noout-modulus|opensslmd5
# Check if CSR matches private keyopensslreq-inrequest.csr-noout-modulus|opensslmd5
opensslrsa-inprivate.key-noout-modulus|opensslmd5
Format Conversions
PEM Conversions
# PEM to DERopensslx509-incert.pem-outformDER-outcert.der
opensslrsa-inkey.pem-outformDER-outkey.der
# DER to PEMopensslx509-incert.der-informDER-outcert.pem
opensslrsa-inkey.der-informDER-outkey.pem
# PEM to PKCS#7opensslcrl2pkcs7-nocrl-certfilecert.pem-outcert.p7b
# PKCS#7 to PEMopensslpkcs7-incert.p7b-print_certs-outcert.pem
# PEM to PKCS#12 (with key and cert)opensslpkcs12-export-outcert.p12-inkeyprivate.key-incert.crt-certfileca.crt
# PKCS#12 to PEMopensslpkcs12-incert.p12-outcert.pem-nodes
# Extract only certificate from PKCS#12opensslpkcs12-incert.p12-clcerts-nokeys-outcert.pem
# Extract only key from PKCS#12opensslpkcs12-incert.p12-nocerts-nodes-outprivate.key
# Extract CA certificates from PKCS#12opensslpkcs12-incert.p12-cacerts-nokeys-outca.pem
Java Keystore Conversions
# Convert PKCS#12 to JKS (use keytool)keytool-importkeystore-srckeystorecert.p12-srcstoretypePKCS12\-destkeystorekeystore.jks-deststoretypeJKS
# Export from JKS to PEM (via PKCS#12)keytool-importkeystore-srckeystorekeystore.jks-destkeystorecert.p12\-deststoretypePKCS12
opensslpkcs12-incert.p12-outcert.pem-nodes
# Import PEM to JKSopensslpkcs12-export-incert.pem-inkeyprivate.key-outcert.p12
keytool-importkeystore-srckeystorecert.p12-srcstoretypePKCS12\-destkeystorekeystore.jks
SSL/TLS Testing
Test SSL Connection
# Connect to HTTPS serveropenssls_client-connectexample.com:443
# Show certificate chainopenssls_client-connectexample.com:443-showcerts
# Test with SNI (Server Name Indication)openssls_client-connectexample.com:443-servernameexample.com
# Test with specific protocolopenssls_client-connectexample.com:443-tls1_2
openssls_client-connectexample.com:443-tls1_3
# Test STARTTLS (SMTP)openssls_client-connectmail.example.com:25-starttlssmtp
# Test STARTTLS (IMAP)openssls_client-connectmail.example.com:143-starttlsimap
# Test STARTTLS (POP3)openssls_client-connectmail.example.com:110-starttlspop3
# Test STARTTLS (FTP)openssls_client-connectftp.example.com:21-starttlsftp
# Show certificate onlyopenssls_client-connectexample.com:443</dev/null2>/dev/null|\opensslx509-text-noout
# Extract certificate from serveropenssls_client-connectexample.com:443</dev/null2>/dev/null|\opensslx509-outformPEM-outserver-cert.pem
# Verify certificate from serveropenssls_client-connectexample.com:443-CAfileca.crt
# Test cipher suiteopenssls_client-connectexample.com:443-cipher'ECDHE-RSA-AES256-GCM-SHA384'# List supported ciphersopensslciphers-v'ALL'opensslciphers-v'HIGH:!aNULL'# Check which ciphers server supportsforcipherin$(opensslciphers'ALL:eNULL'|tr':'' ');doecho-n"Testing $cipher..."result=$(openssls_client-cipher"$cipher"-connectexample.com:443</dev/null2>&1)ifecho"$result"|grep-q"Cipher is ${cipher}";thenecho" supported"elseecho" not supported"fidone
Certificate Chain Validation
# Get certificate chainopenssls_client-connectexample.com:443-showcerts</dev/null2>/dev/null
# Verify server certificate with system CAopenssls_client-connectexample.com:443-CApath/etc/ssl/certs/
# Check certificate expiration remotelyecho|openssls_client-connectexample.com:443-servernameexample.com2>/dev/null|\opensslx509-noout-dates
# Get certificate expiration in daysecho$((($(date-d"$(echo|openssls_client-connectexample.com:443\-servernameexample.com2>/dev/null|opensslx509-noout-enddate|\cut-d=-f2)"+%s)-$(date+%s))/86400))
Security Testing
# Test for Heartbleedopenssls_client-connectexample.com:443-tlsextdebug2>&1|grep"heartbeat"# Check OCSP staplingopenssls_client-connectexample.com:443-status-tlsextdebug
# Test session resumptionopenssls_client-connectexample.com:443-reconnect-no_ticket
# Test renegotiationopenssls_client-connectexample.com:443-state-no_tls1_3<<<"R"# Check supported SSL/TLS versionsforversioninssl3tls1tls1_1tls1_2tls1_3;doecho-n"Testing $version: "ifopenssls_client-connectexample.com:443-$version</dev/null2>/dev/null|\grep-q"Protocol";thenecho"supported"elseecho"not supported"fidone
Encryption & Decryption
Symmetric Encryption
# Encrypt file with AES-256-CBCopensslenc-aes-256-cbc-salt-infile.txt-outfile.txt.enc
# Decrypt fileopensslenc-aes-256-cbc-d-infile.txt.enc-outfile.txt
# Encrypt with password from command line (not recommended)opensslenc-aes-256-cbc-salt-infile.txt-outfile.txt.enc-k"password"# Encrypt with key fileopensslenc-aes-256-cbc-salt-infile.txt-outfile.txt.enc-kfilekey.txt
# Base64 encode encrypted fileopensslenc-aes-256-cbc-salt-infile.txt-outfile.txt.enc-a
# List available ciphersopensslenc-list
Asymmetric Encryption
# Encrypt with public keyopensslrsautl-encrypt-pubin-inkeypublic.key-infile.txt-outfile.enc
# Decrypt with private keyopensslrsautl-decrypt-inkeyprivate.key-infile.enc-outfile.txt
# Encrypt large file (hybrid encryption)# 1. Generate random keyopensslrand-base6432>key.bin
# 2. Encrypt file with symmetric keyopensslenc-aes-256-cbc-salt-infile.txt-outfile.enc-passfile:key.bin
# 3. Encrypt symmetric key with public keyopensslrsautl-encrypt-pubin-inkeypublic.key-inkey.bin-outkey.enc
# 4. To decrypt: decrypt key first, then fileopensslrsautl-decrypt-inkeyprivate.key-inkey.enc-outkey.bin
opensslenc-d-aes-256-cbc-infile.enc-outfile.txt-passfile:key.bin
# Generate random bytesopensslrand-hex16# 16 bytes as hexopensslrand-base6432# 32 bytes as base64opensslrand128>random.bin# 128 bytes to file# Generate random passwordopensslrand-base6412|cut-c1-16
# Generate strong passwordopensslrand-base6432|tr-d'/+='|cut-c1-20
OCSP (Online Certificate Status Protocol)
# Get OCSP URL from certificateopensslx509-incert.crt-noout-ocsp_uri
# Make OCSP requestopensslocsp-issuerca.crt-certcert.crt-urlhttp://ocsp.example.com\-CAfileca.crt
# OCSP with nonceopensslocsp-issuerca.crt-certcert.crt-urlhttp://ocsp.example.com\-CAfileca.crt-header"HOST""ocsp.example.com"# Save OCSP responseopensslocsp-issuerca.crt-certcert.crt-urlhttp://ocsp.example.com\-respoutocsp-response.der
Certificate Revocation Lists (CRL)
# Download CRLwget-Ocrl.derhttp://crl.example.com/crl.crl
# Convert CRL to PEMopensslcrl-informDER-incrl.der-outformPEM-outcrl.pem
# View CRLopensslcrl-incrl.pem-text-noout
# Verify certificate against CRLopensslverify-CAfileca.crt-CRLfilecrl.pem-crl_checkcert.crt
# Check if certificate is revokedopensslcrl-incrl.pem-noout-text|grep-A1$(opensslx509-incert.crt-serial-noout|cut-d=-f2)
# "unable to load certificate"# Fix: Check file format (PEM vs DER)opensslx509-incert.crt-text-noout
# "bad decrypt"# Fix: Wrong password or corrupted keyopensslrsa-inprivate.key-check
# "certificate verify failed"# Fix: Missing intermediate certificatesopensslverify-CAfileca.crt-untrustedintermediate.crtcert.crt
# "unable to get local issuer certificate"# Fix: CA certificate not trustedopensslverify-CApath/etc/ssl/certs/cert.crt
Useful One-Liners
# Check certificate expiry in daysecho$((($(date-d"$(opensslx509-incert.crt-noout-enddate|cut-d=-f2)"+%s)-$(date+%s))/86400))# Find all certificates expiring in 30 daysfind/etc/ssl-name"*.crt"-execsh-c'openssl x509 -in "$1" -noout -checkend 2592000 && echo "$1"'_{}\;# Extract domain names from certificateopensslx509-incert.crt-noout-text|grep-oP'(?<=DNS:)[^,]+'# Generate CSR from existing certificateopensslx509-x509toreq-incert.crt-signkeyprivate.key-outrequest.csr
# Compare two certificatesdiff<(opensslx509-incert1.crt-noout-text)<(opensslx509-incert2.crt-noout-text)