Best Openssl Commands Formscratch
Best Openssl Commands Formscratch
create CSRs, install your SSL/TLS certificate, and identify certificate information. We designed this
quick reference guide to help you understand the most common OpenSSL commands and how to
use them.
This guide is not meant to be comprehensive. If you're looking for a more in-depth and
comprehensive look at OpenSSL, we recommend you check out the OpenSSL Cookbook by Ivan
Ristić.
Guide Notes: Ubuntu 16.04.3 LTS was the system used to write this guide.
Some command examples use a '\' (backslash) to create a line break to make them easier to
understand.
If you don't have the time to get into the nitty-gritty of OpenSSL commands and CSR generation, or
you want to save some time, check out our OpenSSL CSR Wizard.
Use the following command to identify which version of OpenSSL you are running:
openssl version -a
The version number and version release date (OpenSSL 1.0.2g 1 Mar 2016).
The options that were built with the library (options).
The directory where certificates and private keys are stored (OPENSSLDIR).
Using the openssl version -a command, the following output was generated:
Security Note: Because of the security issues associated with using an existing private key, and
because it's very easy and entirely free to create a private key, we recommend you generate a brand
new private key whenever you create a CSR.
Key Algorithm
For the key algorithm, you need to take into account its compatibility. For this reason, we
recommend you use RSA. However, if you have a specific need to use another algorithm (such as
ECDSA), you can use that too, but be aware of the compatibility issues you might run into.
Note: This guide only covers generating keys using the RSA algorithm.
Key Size
For the key size, you need to select a bit length of at least 2048 when using RSA and 256 when
using ECDSA; these are the smallest key sizes allowed for SSL certificates. Unless you need to use
a larger key size, we recommend sticking with 2048 with RSA and 256 with ECDSA.
Note: In older versions of OpenSSL, if no key size is specified, the default key size of 512 is used.
Any key size lower than 2048 is considered unsecure and should never be used.
Passphrase
For the passphrase, you need to decide whether you want to use one. If used, the private key will be
encrypted using the specified encryption method, and it will be impossible to use without the
passphrase. Because there are pros and cons with both options, it's important you understand the
implications of using or not using a passphrase. In this guide, we will not be using a passphrase in
our examples.
Use the following command to generate your private key using the RSA algorithm:
This command generates a private key in your current directory named yourdomain.key (-out
yourdomain.key) using the RSA algorithm (genrsa) with a key length of 2048 bits (2048). The
generated key is created using the OpenSSL format called PEM.
Use the following command to view the raw, encoded contents (PEM format) of the private key:
cat yourdomain.key
Even though the contents of the file might look like a random chunk of text, it actually contains
important information about the key.
Use the following command to decode the private key and view its contents:
The -noout switch omits the output of the encoded version of the private key.
After entering the command, you will be asked series of questions. Your answers to these questions
will be embedded in the CSR. Answer the questions as described below:
Country Name (2 letter code) The two-letter country code where your company is legally located.
Locality Name (e.g., city) The city where your company is legally located.
Organizational Unit Name The name of your department within the organization. (You can leave
(e.g., section) this option blank; simply press Enter.)
Your email address. (You can leave this option blank; simply
Email Address
press Enter.)
An optional company name Leave this option blank (simply press Enter).
Some of the above CSR questions have default values that will be used if you leave the answer
blank and press Enter. These default values are pulled from the OpenSSL configuration file located
in the OPENSSLDIR (see Checking Your OpenSSL Version). If you want to leave a question blank
without using the default value, type a "." (period) and press Enter.
Use the following command to disable question prompts when generating a CSR:
This command uses your private key file (-key yourdomain.key) to create a new CSR (-out
yourdomain.csr) and disables question prompts by providing the CSR information (-subj).
Use the following command to create both the private key and CSR:
This command generates a new private key (-newkey) using the RSA algorithm with a 2048-bit key
length (rsa:2048) without using a passphrase (-nodes) and then creates the key file with a name
of yourdomain.key (-keyout yourdomain.key).
The command then generates the CSR with a filename of yourdomain.csr (-out
yourdomain.csr) and the information for the CSR is supplied (-subj).
Note: While it is possible to add a subject alternative name (SAN) to a CSR using OpenSSL, the
process is a bit complicated and involved. If you do need to add a SAN to your certificate, this can
easily be done by adding them to the order form when purchasing your DigiCert certificate.
Use the following command to view the information in your CSR before submitting it to a CA (e.g.,
DigiCert):
verify OK
Certificate Request:
Data:
Version: 0 (0x0)
Subject: C=US, ST=Utah, L=Lehi, O=Your Company, Inc., OU=IT,
CN=yourdomain.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:bb:31:71:40:81:2c:8e:fb:89:25:7c:0e:cb:76:
[...17 lines removed]
Exponent: 65537 (0x10001)
Attributes:
a0:00
Signature Algorithm: sha256WithRSAEncryption
0b:9b:23:b5:1f:8d:c9:cd:59:bf:b7:e5:11:ab:f0:e8:b9:f6:
[...14 lines removed]
On the first line of the above output, you can see that the CSR was verified (verify OK). On the
fourth line, the Subject: field contains the information you provided when you created the CSR.
Make sure this information is correct.
If any of the information is wrong, you will need to create an entirely new CSR to fix the errors. This
is because CSR files are digitally signed, meaning if even a single character is changed in the file it
will be rejected by the CA.
Use the following command to view the raw output of the CSR:
cat yourdomain.csr
You must copy the entire contents of the output (including the -----BEGIN CERTIFICATE
REQUEST----- and -----END CERTIFICATE REQUEST----- lines) and paste it into your
DigiCert order form.
Use the following commands to generate a hash of each file's public key:
Note: The above commands should be entered one by one to generate three separate outputs.
Each command will output (stdin)= followed by a string of characters. If the output of each
command matches, then the keys for each file are the same. However, if there is any mismatch,
then the keys are not the same and the certificate cannot be installed.
Key mismatch errors are typically caused by installing a certificate on a machine different from the
one used to generate the CSR. If you run into a key mismatch error, you need to do one of the
following:
Transfer the private key from the machine used to generate the CSR to the one you are
trying to install the certificate on.
Install the certificate on the machine with the private key.
Generate an entirely new key and create a new CSR on the machine that will use the
certificate.
Use the following command to convert your PEM key and certificate into the PKCS#12 format (i.e., a
single .pfx file):
Note: After you enter the command, you will be asked to provide a password to encrypt the file.
Because the PKCS#12 format is often used for system migration, we recommend encrypting the file
using a very strong password.
This command combines your private key (-inkey yourdomain.key) and your certificate (-in
yourdomain.crt) into a single .pfx file (-out yourdomain.pfx) with a friendly name (-name
"yourdomain-digicert-(expiration date)"), where the expiration date is the date that the
certificate expires.
PKCS#12 to PEM
Because the PKCS#12 format contains both the certificate and private key, you need to use two
separate commands to convert a .pfx file back into the PEM format.
Use the following command to extract the private key from a PKCS#12 (.pfx) file and convert it into a
PEM encoded private key:
Use the following command to extract the certificate from a PKCS#12 (.pfx) file and convert it into a
PEM encoded certificate:
Note: You will need to provide the password used to encrypt the .pfx file in order to convert the key
and certificate into the PEM format.
PEM to DER
The DER format uses ASN.1 encoding to store certificate or key information. Similar to the PEM
format, DER stores key and certificate information in two separate files and typically uses the same
file extensions (i.e., .key, .crt, and .csr). The file extension .der was used in the below examples for
clarity.
Use the following command to convert a PEM encoded certificate into a DER encoded certificate:
openssl x509 -inform PEM -in yourdomain.crt -outform DER -out yourdomain.der
Use the following command to convert a PEM encoded private key into a DER encoded private key:
openssl rsa -inform PEM -in yourdomain.key -outform DER -out yourdomain_key.der
DER to PEM
Use the following command to convert a DER encoded certificate into a PEM encoded certificate:
openssl x509 -inform DER -in yourdomain.der -outform PEM -out yourdomain.crt
Use the following command to convert a DER encoded private key into a PEM encoded private key:
openssl rsa -inform DER -in yourdomain_key.der -outform PEM -out yourdomain.key