OpenSSL

Intro

One of the most popular commands in SSL to create, convert, manage the SSL Certificates is OpenSSL. There are many cases nowadays where OpenSSL have to be used in various ways. Below are the most common commnads used for managing Certficates, ciphers, encryption, et all. OpenSSL includes tonnes of features covering a broad range of use cases, and it’s difficult to remember its syntax for all of them and quite easy to get lost. Helppages are not so helpful here, so often we just Google “openssl how to [use case here]” or look for openssl cheatsheet to recall the usage of a command and see examples.
For indept review and usercases of OpenSSL see http://fvck.in/ssl-basics/

Some of the abbreviations related to certificates.

  • SSL – Secure Socket Layer
  • CSR – Certificate Signing Request
  • TLS – Transport Layer Security
  • PEM – Privacy Enhanced Mail
  • DER – Distinguished Encoding Rules
  • SHA – Secure Hash Algorithm
  • PKCS – Public-Key Cryptography Standards

Create new Private Key and Certificate Signing Request

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

Above command will generate CSR and 2048-bit RSA key file. If you intend to use this certificate in Apache or Nginx, then you need to send this CSR file to certificate issuer authority, and they will give you signed certificate mostly in der or pem format which you need to configure in Apache or Nginx web server.

Create a Self-Signed Certificate (default is 30 days)

openssl req -x509 -sha256 -nodes -newkey rsa:2048 -keyout domain_selfsigned.key -out domain_cert.pem

Above command will generate a self-signed certificate and key file with 2048-bit RSA. I have also included sha256 as it’s considered most secure at the moment.

Create a Self-Signed Certificate (custom, 2 years)

Tip: by default, it will generate self-signed certificate valid for only one month so you may consider defining –days parameter to extend the validity.

openssl req -x509 -sha256 -nodes -days 750 -newkey rsa:2048 -keyout  domain_selfsigned.key  -out domain_cert.pem

Verify CSR file

openssl req -noout -text -in domain.csr

Verification is essential to ensure you are sending CSR to issuer authority with required details.

Create RSA Private Key

openssl genrsa -out private.key 2048

If you just need to generate RSA private key, you can use the above command. I have included 2048 for stronger encryption.

Remove Passphrase from Key

openssl rsa -in certkey.key -out nopassphrase.key

If you are using passphrase in key file and using Apache then every time you start, you have to enter the password. If you are annoyed with entering a password, then you can use above openssl rsa -in domain.key -check to remove the passphrase key from an existing key.

Verify Private Key

openssl rsa -in certkey.key –check

If you doubt on your key file, you can use the above command to check.

Verify Certificate File

openssl x509 -in certfile.pem -text –noout

If you would like to validate certificate data like CN, OU, etc. then you can use an above command which will give you certificate details.

Verify the Certificate Signer Authority

openssl x509 -in certfile.pem -noout -issuer -issuer_hash

Certificate issuer authority signs every certificate and in case you need to check them.

Check Hash Value of A Certificate

openssl x509 -noout -hash -in domain.pem

Convert DER to PEM format

openssl x509 –inform der –in sslcert.der –out sslcert.pem

Usually, certificate authority will give you SSL cert in .der format, and if you need to use them in apache or .pem format then the above command will help you.

Convert PEM to DER format

openssl x509 –outform der –in sslcert.pem –out sslcert.der

In case you need to change .pem format to .der

Convert Certificate and Private Key to PKCS#12 format

openssl pkcs12 –export –out sslcert.pfx –inkey key.pem –in sslcert.pem

If you need to use a cert with the java application or with any other who accept only PKCS#12 format, you can use the above command, which will generate single pfx containing certificate & key file. You can also include chain certificate by passing –chain as below.

openssl pkcs12 –export –out sslcert.pfx –inkey key.pem –in sslcert.pem -chain cacert.pem

Create CSR using an existing private key

openssl req –out certificate.csr –key existing.key –new

If you don’t want to create a new private key instead using an existing one, you can go with the above command.

Check contents of PKCS12 format cert

openssl pkcs12 –info –nodes –in cert.p12

PKCS12 is binary format so you won’t be able to view the content in notepad or another editor. The above command will help you to see the contents of PKCS12 file.

Convert PKCS12 format to PEM certificate

openssl pkcs12 –in cert.p12 –out cert.pem

If you wish to use existing pkcs12 format with Apache or just in pem format, this will be useful.

Test SSL certificate of particular URL

openssl s_client -connect domain.com:443 –showcerts

I use this quite often to validate the SSL certificate of particular URL from the server. This is very handy to validate the protocol, cipher, and cert details.

Find out OpenSSL version

openssl version

If you are responsible for ensuring OpenSSL is secure then probably one of the first things you got to do is to verify the version.

18Check PEM File Certificate Expiration Date

openssl x509 -noout -in certificate.pem -dates

Useful if you are planning to put some monitoring to check the validity. It will show you date in notBefore and notAfter syntax. notAfter is one you will have to verify to confirm if a certificate is expired or still valid.

Example

[root@Chandan opt]# openssl x509 -noout -in bestflare.pem -dates
notBefore=Jul 4 14:02:45 2015 GMT
notAfter=Aug 4 09:46:42 2015 GMT

Check Certificate Expiration Date of SSL URL

openssl s_client -connect domain.com:443 2>/dev/null | openssl x509 -noout –enddate

Another useful if you are planning to monitor SSL cert expiration date remotely or particular URL.

Example:

[root@Chandan opt]# openssl s_client -connect google.com:443 2>/dev/null | openssl x509 -noout -enddate

notAfter=Dec 8 00:00:00 2020 GMT

Check if SSL V2 or V3 is accepted on URL

To check SSL V2

openssl s_client -connect domain.com:443 -ssl2

To Check SSL V3

openssl s_client -connect domain.com:443 –ssl3

To Check TLS 1.0

openssl s_client -connect domain.com:443 –tls1

To Check TLS 1.1

openssl s_client -connect domain.com:443 –tls1_1

To Check TLS 1.2

openssl s_client -connect domain.com:443 –tls1_2

If you are securing web server and need to validate if SSL V2/V3 is enabled or not, you can use the above command. If activated, you will get “CONNECTED” else “handshake failure.” For all risks associated with SSL vulnerabilities please visit http://fvck.in/vulerabilities/

Verify if the particular cipher is accepted on URL

openssl s_client -cipher 'ECDHE-ECDSA-AES256-SHA' -connect domiain.com:443

If you are working on security findings and pen test results show some of the weak ciphers is accepted then to validate, you can use the above command.

Of course, you will have to change the cipher and URL, which you want to test against. If the mentioned cipher is accepted, then you will get “CONNECTED” else “handshake failure.”