Skip to content

Instantly share code, notes, and snippets.

@mjuenema
Created November 29, 2016 20:57
Show Gist options
  • Save mjuenema/8e30efb9bc0d7a64d198b9a52536bcaf to your computer and use it in GitHub Desktop.
Save mjuenema/8e30efb9bc0d7a64d198b9a52536bcaf to your computer and use it in GitHub Desktop.

OpenSSL Commands

Displaying Server Certificates

$ echo | openssl s_client -connect www.example.com:https 2>/dev/null | openssl x509 -noout -text

$ echo | openssl s_client -connect www.example.com:https 2>/dev/null | openssl x509 -noout -dates
notBefore=Aug  4 00:00:00 2016 GMT
notAfter=Sep 19 23:59:59 2017 GMT
  
$ echo | openssl s_client -connect www.example.com:https 2>/dev/null | openssl x509 -noout -issuer
issuer= /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 High Assurance Server CA

Displaying Certificates, CSR and Keys

$ openssl x509 -in $domain.crt -noout -text
$ openssl req -in $domain.csr -noout -text
$ openssl rsa  -aes192 -in $domain.key -noout -text

Creating a certificate signing request

Generate key and CSR.

$ openssl req -newkey rsa:2048 -nodes -keyout $domain.key -out $domain.csr

Generate CSR from existing key.

$ openssl req -key $domain.key -new -out $domain.csr

Generate CSR for renewal from existing certificate and key.

$ openssl x509 -in $domain.crt -signkey $domain.key -x509toreq -out $domain.csr

Creating a self-signed certificate

Generate key and self-signed certificate.

$ openssl req -newkey rsa:2048 -nodes -keyout $domain.key -x509 -days 365 -out $domain.crt

Generate self-signed certificate from existing key.

$ openssl req -key $domain.key -new -x509 -days 365 -out $domain.crt

Generate self-signed certificate from existing key and CSR.

$ openssl x509 -signkey $domain.key -in $domain.csr -req -days 365 -out $domain.crt

Private keys

Generating an encrypted private key.

$ openssl genrsa -aes256 -out domain.key 2048

Generating an unencrypted private key.

$ openssl genrsa -nodes -out domain.key 2048

Verify key, certificate, CSR.

The modulus field of the related key, certificate and CSR will match.

$ openssl rsa  -noout -modulus -in $domain.key | openssl md5
$ openssl x509 -noout -modulus -in $domain.crt | openssl md5
$ openssl req  -noout -modulus -in $domain.csr | openssl md5

Encryption, Decryption, Digest

Omitting -in and -out will use stdin and stdout.

Encryption and decryption.

$ openssl enc -h

$ enc -e -aes-256-cbc -in file.txt -out file.aes256cbc
$ enc -d -aes-256-cbc -in file.aes256cbc -out file.txt

Digest and

$ openssl dgst -h

$ openssl dgst -sha256 file.txt
$ openssl dgst -sha256 -hmac MYKEY file.txt

Links

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