The Most Common OpenSSL Commands
One of the most versatile SSL tools is OpenSSL which is an open source implementation of the SSL protocol. There are versions of OpenSSL for nearly every platform, including Windows, Linux, and Mac OS X. OpenSSL is commonly used to create the CSR and private key for many different platforms, including Apache. However, it also has hundreds of different functions that allow you to view the details of a CSR or certificate, compare an MD5 hash of the certificate and private key (to make sure they match), verify that a certificate is installed properly on any website, and convert the certificate to a different format. A compiled version of OpenSSL for Windows can be found here.
If you don’t want to bother with OpenSSL, you can do many of the same things with our SSL Certificate Tools. Below, we have listed the most common OpenSSL commands and their usage:
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
1openssl req -out <u>CSR.csr</u> -new -newkey rsa:2048 -nodes -keyout <u>privateKey.key</u>
- Generate a self-signed certificate (see How to Create and Install an Apache Self Signed Certificate for more info)
1openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout <u>privateKey.key</u> -out <u>certificate.crt</u>
- Generate a certificate signing request (CSR) for an existing private key
1openssl req -out <u>CSR.csr</u> -key <u>privateKey.key</u> -new
- Generate a certificate signing request based on an existing certificate
1openssl x509 -x509toreq -in <u>certificate.crt</u> -out <u>CSR.csr</u> -signkey <u>privateKey.key</u>
- Remove a passphrase from a private key
1openssl rsa -in <u>privateKey.pem</u> -out <u>newPrivateKey.pem</u>
Checking Using OpenSSL
- Check a Certificate Signing Request (CSR)
1openssl req -text -noout -verify -in <u>CSR.csr</u>
- Check a private key
1openssl rsa -in <u>privateKey.key</u> -check
- Check a certificate
1openssl x509 -in <u>certificate.crt</u> -text -noout
- Check a PKCS#12 file (.pfx or .p12)
1openssl pkcs12 -info -in <u>keyStore.p12</u>
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. If you are trying to verify that an SSL certificate is installed correctly, be sure to check out the SSL Checker.
- Check an MD5 hash of the public key to ensure that it matches with what is in a CSR or private key
123openssl x509 -noout -modulus -in <u>certificate.crt</u> | openssl md5openssl rsa -noout -modulus -in <u>privateKey.key</u> | openssl md5openssl req -noout -modulus -in <u>CSR.csr</u> | openssl md5
- Check an SSL connection. All the certificates (including Intermediates) should be displayed
1openssl s_client -connect <u>www.paypal.com</u>:443
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. Use our SSL Converter to convert certificates without messing with OpenSSL.
- Convert a DER file (.crt .cer .der) to PEM
1openssl x509 -inform der -in <u>certificate.cer</u> -out <u>certificate.pem</u>
- Convert a PEM file to DER
1openssl x509 -outform der -in <u>certificate.pem</u> -out <u>certificate.der</u>
- Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM
1openssl pkcs12 -in <u>keyStore.pfx</u> -out <u>keyStore.pem</u> -nodes
You can add -nocerts to only output the private key or add -nokeys to only output the certificates.
- Convert a PEM certificate file and a private key to PKCS#12 (.pfx .p12)
1openssl pkcs12 -export -out <u>certificate.pfx</u> -inkey <u>privateKey.key</u> -in <u>certificate.crt</u> -certfile <u>CACert.crt</u>
Originally posted on Sun Jan 13, 2008
常用 openssl 命令 by 桔子小窝 is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.