Skip to content

Instantly share code, notes, and snippets.

@techsolx
Last active January 20, 2022 18:04
Show Gist options
  • Save techsolx/79c8c3f64cb122682e4f54ebbbd82d1b to your computer and use it in GitHub Desktop.
Save techsolx/79c8c3f64cb122682e4f54ebbbd82d1b to your computer and use it in GitHub Desktop.
Open ssl commands I use from time to time

Shamelessly borrowed from the internets

General OpenSSL Commands

These commands allow you to generate CSRs, Certificates, Private Keys and do other miscellaneous tasks.

Generate a new private key and Certificate Signing Request openssl req -out CSR.csr -new -newkey rsa:2048 -nodes -keyout privateKey.key

Generate a self-signed certificate openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt

Generate a certificate signing request (CSR) for an existing private key openssl req -out CSR.csr -key privateKey.key -new

Generate a certificate signing request based on an existing certificate openssl x509 -x509toreq -in certificate.crt -out CSR.csr -signkey privateKey.key Remove a passphrase from a private key openssl rsa -in privateKey.pem -out newPrivateKey.pem

Checking Using OpenSSL

If you need to check the information within a Certificate, CSR or Private Key, use these commands.

Check a Certificate Signing Request (CSR) openssl req -text -noout -verify -in CSR.csr

Check a private key openssl rsa -in privateKey.key -check

Check a certificate openssl x509 -in certificate.crt -text -noout

Check a PKCS#12 file (.pfx or .p12) openssl pkcs12 -info -in keyStore.p12

Debugging Using OpenSSL

If you are receiving an error that the private doesn't match the certificate or that a certificate that you installed to a site is not trusted, try one of these commands.

openssl x509 -noout -modulus -in certificate.crt | openssl md5
openssl rsa -noout -modulus -in privateKey.key | openssl md5
openssl req -noout -modulus -in CSR.csr | openssl md5

Check an SSL connection. All the certificates (including Intermediates) should be displayed openssl s_client -connect <server>:<port>

Just checking date, use this: curl -vk https://<server>

Converting Using OpenSSL

These commands allow you to convert certificates and keys to different formats to make them compatible with specific types of servers or software. For example, you can convert a normal PEM file that would work with Apache to a PFX (PKCS#12) file and use it with Tomcat or IIS.

Convert a DER file (.crt .cer .der) to PEM openssl x509 -inform der -in certificate.cer -out certificate.pem

Convert a PEM file to DER openssl x509 -outform der -in certificate.pem -out certificate.der

Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes

You can add -nocerts to only output the private key or add -nokeys to only output the certificates. Convert PEM to CRT (.CRT file) openssl x509 -outform der -in certificate.pem -out certificate.crt

Convert a PEM certificate file and a private key to PKCS#12 (.pfx .p12) openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt

How do I verify that a private key matches a certificate?

To verify that an RSA private key matches the RSA public key in a certificate you need to i) verify the consistency of the private key and ii) compare the modulus of the public key in the certificate against the modulus of the private key.

To verify the consistency of the RSA private key and to view its modulus: openssl rsa -check -noout -modulus -in myserver.key

To view the modulus of the RSA public key in a certificate: openssl x509 -noout -modulus -in myserver.crt

via a single line with a short modulus use this:

openssl x509 -noout -modulus -in server.pem | openssl md5 ;\
openssl rsa -noout -modulus -in server.key | openssl md5

Encrypt and Decrypt with openssl:

Public key must be in .pem format, not openssl format beginning with id-rsa ... Keys can be generated with this, there are many ways: ssh-keygen -t rsa -b 4096 -C "email@domain.com" -f ~/.ssh/id_rsa

Yes use a passphrase, not a password, except in rare cases, then save the key in .pem format, this will require the passphrase created above openssl rsa -in ~/.ssh/id_rsa -pubout -out ~/.ssh/id_rsa.pub.pem

encrypt small files like this: openssl rsautl -encrypt -inkey id_rsa.pub.pem -pubin -in file.txt -out file.txt.enc

decrypt files like this: openssl rsautl -decrypt -inkey ~/.ssh/id_rsa -in file.txt.enc -out file.txt

If you need to encrypt file larger than the key size -11 bits, so 4096 - 11 = 4085 or if someone sends you a small key check with this: openssl rsa -in id_rsa.pub.pem -pubin -text -noout

then you can generate a random key with this: openssl rand -base64 128 -out random.key

and then use the smaller key to encrypt that random.key openssl rsautl -encrypt -inkey id_rsa.pub.pem -pubin -in random.key -out random.key.enc

and use the larger random.key to encrypt the file: openssl enc -aes-256-cbc -salt -in bigfile -out bigfile.enc -pass file:./random.key

Send the encrypted bigfile.enc and the encrypted random.key.enc

Decrypt the random.key.enc file like this: openssl rsautl -decrypt -inkey ~/.ssh/id_rsa -in random.key.enc -out random.key

and the bigfile.enc like this: openssl enc -d -aes-256-cbc -in bigfile.enc -out bigfile -pass file:./random.key

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