1. Overview

OpenSSL is a powerful toolkit widely used for implementing cryptographic functions and securing communications over computer networks. It provides a comprehensive suite of tools for managing certificates, encrypting data, and verifying secure connections.

In this tutorial, we’ll explore essential OpenSSL commands with practical examples to help us understand how to use them effectively. By following the examples in this guide, we’ll gain hands-on experience in generating private keys, creating certificate signing requests (CSR), encrypting files, and more.

2. Installation

Before we can use OpenSSL, we need to install it. In this section, we’ll demonstrate how to install OpenSSL on different Linux distributions and verify the installation.

To install OpenSSL on Ubuntu or Debian, we use the apt command:

$ sudo apt-get install openssl

For CentOS or RHEL, we’ll use yum to install OpenSSL:

$ sudo yum install openssl

After installing OpenSSL, we can verify the installation and check the version:

$ openssl version

This command outputs the version of OpenSSL installed on our system, ensuring that the installation was successful and we have the correct version.

3. Generating Private Keys

With OpenSSL, we can easily generate private keys for various applications. Let’s look at the command to create a private key:

$ openssl genpkey -algorithm RSA -out private_key.pem -aes256

In this example, we use the openssl genpkey command to generate a private key. Specifically, the -algorithm RSA option specifies the RSA algorithm for generating private keys. Additionally, the -out private_key.pem option indicates the file where the private key will be saved, and the -aes256 option applies AES-256 encryption to the private key for added security.

Consequently, this command creates a secure RSA private key that can be used for various cryptographic tasks, such as creating certificate signing requests or decrypting encrypted data.

4. Creating Certificate Signing Requests (CSRs)

Generating a Certificate Signing Request (CSR) is a crucial step in obtaining an SSL certificate from a Certificate Authority (CA). The CSR contains information about the organization and includes the public key that the CA uses to create a valid SSL certificate for securing communications. Let’s see how to create a CSR with openssl:

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

In this command, we use openssl req to initiate the process of creating a CSR. The -new flag indicates that we’re creating a new CSR. The -key option specifies the private key file, in this case, private_key.pem, which will be used to sign the CSR. The -out option specifies the output file for the CSR, which we’ve named domain.csr.

During the execution of this command, we have to enter information such as our country, state, organization name, and common name (usually the domain name). The CA includes this information in the CSR to verify our identity and issue the certificate. By carefully following these steps, we can successfully generate a CSR that meets the requirements for obtaining an SSL certificate.

It’s crucial to ensure all necessary information is accurate and complete. Therefore, after creating the CSR, it’s important to verify its content to confirm its correctness. To verify the content of the CSR, we can use the following command:

$ openssl req -text -in domain.csr -noout -verify

Similarly to the previous command, openssl req initiates the process of handling a certificate request. The -text option outputs the contents of the CSR in a human-readable format. Additionally, the -noout option omits the encoded version of the file from the output, while the -verify flag checks the signature to ensure it hasn’t been modified.

5. Self-Signed Certificates

Self-signed certificates are useful for testing purposes or internal use within an organization. They allow us to create a certificate without the need for a trusted certificate authority (CA).

To generate a self-signed certificate, we need to create a private key and then generate the certificate using that key. Here’s an example command:

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout private_key.pem -out certificate.pem

In this command, openssl req initiates a certificate request. Additionally, the -x509 option specifies that we’re creating a self-signed certificate. The -nodes option means no DES encryption on the private key. The -days 365 option sets the certificate validity period to 365 days. Moreover, the -newkey rsa:2048 option generates a new RSA key of 2048 bits. The -keyout private_key.pem option specifies the output file for the private key. Finally, the -out certificate.pem option specifies the output file for the certificate.

The command creates a private key file named private_key.pem and a certificate file named certificate.pem. These files can be used to secure communications on a server, ensuring data transmitted between the server and clients is encrypted. This process provides a simple and effective way to secure internal services without relying on a trusted CA.

6. Encrypting and Decrypting Files

Encrypting and decrypting files is a fundamental aspect of data security, ensuring that sensitive information remains protected from unauthorized access. OpenSSL provides robust tools for both symmetric and asymmetric encryption.

To encrypt a file using OpenSSL, we can run openssl enc:

$ openssl enc -aes-256-cbc -salt -in file.txt -out file.enc

In this example, we’re encrypting file.txt using the AES-256-CBC encryption algorithm. The -salt option adds a layer of security by introducing a random salt value, and -out file.enc outputs the encrypted file as file.enc.

Next, we’ll run another command to decrypt the file that we’ve encrypted:

$ openssl enc -aes-256-cbc -d -in file.enc -out file_dec.txt

Here, the -d option indicates decryption, and we specify file.enc as the input encrypted file and file_dec.txt as the output decrypted file. This command will prompt us for the password used during encryption to ensure that only authorized users can decrypt the data.

7. Converting Certificate Formats

Converting certificate formats is a common task when dealing with various applications and servers that require specific certificate types. OpenSSL makes it easy to convert certificates between different formats like PEM, DER, and PFX.

7.1. PEM to DER

We can convert a PEM file to a DER file using the -outform der flag with the openssl x509 command:

$ openssl x509 -outform der -in certificate.pem -out certificate.der

In this example, the command reads the PEM-formatted certificate from the certificate.pem and converts it to DER format, saving the result as certificate.der.

7.2. DER to PEM

Similar to PEM to DER conversion, we can convert a DER file to a PEM file using the -inform der flag instead of -outform der:

$ openssl x509 -inform der -in certificate.der -out certificate.pem

This command takes the DER-formatted certificate from the certificate.der and converts it to PEM format, outputting the result to certificate.pem.

7.3. PEM to PFX

In beginning, let’s create the certfile using the -signkey flag with the openssl x509 command:

$ openssl x509 -req -days 365 -in domain.csr -signkey private_key.pem -out CA.pem

Then, to convert a PEM file to a PFX file, we can execute the openssl pcks12 command with the -export flag:

$ openssl pkcs12 -export -out certificate.pfx -inkey private_key.pem -in certificate.pem -certfile CA.pem

This command takes the PEM-formatted certificate and private key, along with the CA certificate, and packages them into a PFX file named certificate.pfx.

7.4. PFX to PEM

For converting a PFX (PKCS#12) file to a PEM file, we can use the -nodes flag openssl pkcs12 command:

$ openssl pkcs12 -in certificate.pfx -out certificate.pem -nodes

This command extracts the certificates and private keys from the PFX file certificate.pfx and converts them to PEM format, and saves the result as certificate.pem.

Converting certificates between different formats ensures compatibility with various applications and systems. OpenSSL simplifies this process, making it easy to switch between formats as needed. By using the commands provided, we can efficiently manage our certificates in the required formats.

8. Checking Certificate Information

Checking certificate information is essential for verifying the details and validity of SSL/TLS certificates. By using OpenSSL, we can easily view and analyze these certificate details. Let’s examine various attributes such as the issuer, subject, and validity period of the certificate using the openssl x509 command:

$ openssl x509 -in certificate.pem -text -noout

In this command, openssl x509 is used to work with x509 certificates. The -in option specifies the input file, certificate.pem, which is the certificate we want to inspect. The -text option tells OpenSSL to display the certificate details in a human-readable format.  Lastly, the -noout prevents the output of the encoded version of the certificate.

This command will output various details about the certificate, including the issuer, subject, validity period, public key information, and more. By reviewing these details, we can ensure the certificate is correctly configured and valid for its intended use. This command is particularly useful for system administrators and security professionals who need to verify and troubleshoot SSL/TLS certificates on their Linux systems.

9. Conclusion

In this article, we’ve covered a range of essential OpenSSL commands that are crucial for securing communications and managing cryptographic operations on Linux systems. From generating private keys to verifying certificate chains, each example has demonstrated practical use cases that we can apply in real-world scenarios.

All in all, by mastering these OpenSSL commands, we can enhance our ability to implement robust security measures and efficiently manage certificates. As we continue to explore OpenSSL’s vast array of features, we’ll be better equipped to handle various security tasks and protect our data more effectively.