How to convert a private, public PEM files and it's cer file to a fullchain and private key?

2 min read 28-10-2024
How to convert a private, public PEM files and it's cer file to a fullchain and private key?

Converting PEM (Privacy Enhanced Mail) files into a full chain certificate and private key can often feel daunting, especially if you're new to SSL/TLS management. This article will guide you through the process, ensuring that you can convert your private, public PEM files and their corresponding CER files into the desired format.

Understanding the Problem

Let's first clarify the task at hand. You might have a private key stored in a PEM file, a public certificate in another PEM file, and a CER file that needs to be combined into a single full chain certificate along with the private key. The result will be a full chain and a private key that can be used for server configurations or in services like Let's Encrypt.

Original Code/Scenario

For example, your current file setup might look something like this:

  • private_key.pem - Your private key.
  • public_cert.pem - Your public certificate.
  • intermediate_cert.cer - Your intermediate certificate.

Converting PEM and CER Files into a Full Chain Certificate and Private Key

To achieve the desired outcome, you need to use command-line tools such as OpenSSL. Below are the detailed steps to guide you through the conversion process.

Step 1: Combine Certificates

The first step is to create a full chain certificate that includes your public certificate and the intermediate certificate. You can accomplish this with the following command:

cat public_cert.pem intermediate_cert.cer > fullchain.pem

Step 2: Ensure You Have Your Private Key

Your private key should remain securely stored in its own file (private_key.pem). Make sure this file is kept confidential and not shared publicly.

Step 3: Validate Your Files

Before proceeding, it’s wise to validate that your certificates are properly formatted. Use these commands to check the contents:

openssl x509 -in public_cert.pem -text -noout
openssl x509 -in intermediate_cert.cer -text -noout
openssl rsa -in private_key.pem -check

This will display information about the certificates and confirm the private key is valid.

Step 4: Final Output Files

Now you should have the following files ready for use:

  • Full Chain Certificate: fullchain.pem
  • Private Key: private_key.pem

Practical Example

Imagine you are configuring SSL for a web server. After obtaining your certificates, you can utilize the full chain and private key in your server configuration. For example, in an Nginx server configuration, it would look like this:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/ssl/fullchain.pem;
    ssl_certificate_key /etc/ssl/private_key.pem;

    ...
}

Additional Considerations

  1. Permissions: Ensure that your private key file has the correct permissions. Typically, it should only be readable by the root user:

    chmod 600 private_key.pem
    
  2. Backup: Always keep backups of your certificates and private keys in a secure location.

  3. Renewals: Remember that SSL/TLS certificates have expiration dates. Make sure to keep track of these dates and renew your certificates when necessary.

Useful Resources

Conclusion

Converting your private, public PEM files and their corresponding CER files into a full chain certificate along with a private key is essential for secure communication. By following the steps outlined above, you can ensure that your SSL/TLS setup is both secure and efficient. Whether you are managing a small personal website or a large corporate server, understanding this process is crucial.

By keeping your certificates organized and ensuring they are properly configured, you'll create a reliable foundation for your web applications.