Savio Resende

Blog > Creating a Self-Signed SSL Certificate

Creating a Self-Signed SSL Certificate

by Savio Resende

Secure Socket Layer (SSL) is a protocol that provides a secure channel between two machines operating over the internet or an internal network. One of the primary reasons SSL is used is to keep sensitive information encrypted, ensuring that any data transmitted between users and sites or between two systems remains private and integral.

A self-signed certificate is an identity certificate signed by the same entity whose identity it certifies. This contrasts to a certificate signed by a trusted certificate authority (CA). While self-signed certificates can be used for testing purposes or within organizations, they are not recommended for production environments due to the lack of a trusted third-party Certificate Authority (CA) signature.

History of SSL

SSL was first introduced by Netscape in the 1990s as a way to secure transactions over the web. The protocol has undergone several revisions, with Transport Layer Security (TLS) being its successor. Over the years, the importance of online security has grown exponentially, leading to the widespread adoption of SSL/TLS.

I apologize for the oversight. Let's delve deeper into the process of creating a self-signed SSL certificate. While I couldn't extract the exact commands from the provided website, I can provide a general step-by-step guide with commands based on my knowledge.

Creating a Self-Signed SSL Certificate

Step 1: Install OpenSSL

Before generating an SSL certificate, you must install OpenSSL on your system.

sudo apt update
sudo apt install openssl

Step 2: Generate a Private Key

The first step in creating an SSL certificate is generating a private key.

openssl genpkey -algorithm RSA -out private_key.pem

Expected Output: A file named private_key.pem containing your private key.

Step 3: Create a Certificate Signing Request (CSR)

Once you have a private key, you can create a CSR. This request contains details about your organization and domain.

openssl req -new -key private_key.pem -out request.csr

You'll be prompted to enter details such as country, state, organization name, etc. Here, in the prompts, is where you specify the domain that will be signed.

Expected Output: A file named request.csr containing your certificate signing request.

Step 4: Generate the Self-Signed Certificate

You can now generate your self-signed certificate using the private key and CSR.

openssl x509 -req -days 365 -in request.csr -signkey private_key.pem -out certificate.pem

Expected Output: A file named certificate.pem containing your self-signed certificate.

Step 5: Install and Configure the Certificate

After generating the certificate, you'll need to install it on your server and configure the server to use it. The exact steps will vary depending on your server software (e.g., Apache, Nginx).

For Apache:

sudo cp certificate.pem /etc/ssl/certs/
sudo cp private_key.pem /etc/ssl/private/

Then, update your Apache configuration to point to these files. It consists of putting the certificate and key in place and pointing the respective configurations to them in your web server.

Proper Permissions for Certificate and Key Files

When dealing with SSL/TLS certificates and their corresponding private keys, it's crucial to set appropriate permissions to ensure the security and functionality of your encrypted connections. Improper permissions can expose sensitive data, make your system vulnerable to attacks, or prevent your server software from accessing the necessary files. Here's a breakdown of the recommended permissions:

1. Private Key File:

The private key is a sensitive piece of information. If it falls into the wrong hands, an attacker could impersonate your server or decrypt sensitive information. Therefore, the permissions for the private key should be highly restrictive.

  • Owner: The owner should be the root user or the specific user that runs the server software (e.g., www-data for Apache in some distributions).
  • Permissions: The recommended permissions for the private key file are 600 (read and write permissions for the owner only). This ensures that only the owner can read and write to the file, and no other user or group can access it.
  • chmod 600 /path/to/private_key.pem

    2. Certificate File:

    Unlike the private key, the certificate file does not contain sensitive information. The public part is shared with clients to establish encrypted connections. However, setting appropriate permissions to prevent unintended modifications is still a good practice.

  • Owner: Similar to the private key, the owner should be the root user or the specific server software user.
  • Permissions: A common permission setting for the certificate file is 644 (read and write permissions for the owner, and read permissions for others). This allows other services or users on the system to read the certificate if necessary, but only the owner can modify it.
  • chmod 644 /path/to/certificate.pem

    Adding a Self-Signed Certificate to Browsers

    When you use a self-signed certificate, browsers will typically display a warning because the certificate is not issued by a recognized Certificate Authority (CA). To avoid these warnings, you can manually add your self-signed certificate to your browser's list of trusted certificates. Here's how you can do it for some popular browsers:

    Google Chrome:

  • Open Chrome and go to the site with the self-signed certificate.
  • Click on the "Not Secure" label in the address bar.
  • Click on "Certificate" in the dropdown.
  • Drag the certificate image to your desktop or any other location.
  • Open Chrome Settings, search for "Manage Certificates", and click on it.
  • Go to the "Authorities" tab and click on "Import".
  • Choose the certificate you saved and click "Open".
  • Check the "Trust this certificate for identifying websites" box and click "OK".
  • Mozilla Firefox:

  • Open Firefox and go to the site with the self-signed certificate.
  • Click on "Advanced" and then "Add Exception…".
  • Click "View…" and then "Export" to save the certificate to your computer.
  • Open Firefox Options and search for "Certificates".
  • Click on "View Certificates" and then the "Authorities" tab.
  • Click on "Import", choose the certificate you saved, and click "Open".
  • Check the box for "Trust this CA to identify websites" and click "OK".
  • Microsoft Edge:

  • Open Edge and go to the site with the self-signed certificate.
  • Click on the "Not Secure" label in the address bar.
  • Click on "View Certificate".
  • Go to the "Details" tab and click "Copy to File…".
  • Save the certificate to your computer.
  • Open Windows Settings and search for "Manage Computer Certificates".
  • Under "Trusted Root Certification Authorities", right-click, choose "All Tasks", then "Import".
  • Follow the wizard to import the certificate you saved.
  • Safari:

  • Open Safari and go to the site with the self-signed certificate.
  • Click on the "Show Certificate" button in the warning message.
  • Drag the certificate image to your desktop or any other location.
  • Open Keychain Access (you can find it using Spotlight).
  • Drag the certificate into the "System" keychain.
  • Right-click on the certificate, choose "Get Info", expand the "Trust" section, and set "When using this certificate" to "Always Trust".
  • Note: While adding a self-signed certificate to your browser's trusted list can eliminate warnings, it's essential to understand the risks. Only add certificates that you trust entirely, as malicious entities can exploit self-signed certificates. Always ensure the certificate's authenticity before adding it to your trusted list.


    Extra: Valid Local Certificates: mkcert

    While self-signed certificates are functional, it requires some work. mkcert Is a simple, zero-config tool that creates locally-trusted development certificates. It's perfect for developers who must generate SSL/TLS certificates for local development environments, without the browser warnings that come with self-signed certificates. Here's a guide on how to use mkcert in Linux:

    Introduction to mkcert

    mkcert Automatically creates and installs a local CA in the system root store, generating locally-trusted certificates. The primary advantage is that certificates generated by mkcert are automatically trusted by your browser, eliminating the usual browser security warnings associated with self-signed certificates.

    Reference: https://github.com/FiloSottile/mkcert

    Installing mkcert on Linux:

    Here I recommend installing Brew - a tool available for Linux and OSX. If you need guidance with alternative ways of installations, visit the link above, where you'll find more about it.

  • Install certutil:
  • sudo apt update
    sudo apt install libnss3-tools -y
  • Download and install mkcert:
  • brew install mkcert

    Using mkcert:

  • Install the local CA:
  • The first time you run mkcert, it will create a new local CA and install it in the system trust store.

    mkcert -install

    Expected Output: "The local CA is now installed in the system trust store!"

  • Generate a locally-trusted certificate:
  • To generate a certificate for a specific domain (e.g., localhost or example.dev), use the following command:

    mkcert example.dev localhost 127.0.0.1

    Expected Output: This will generate two files: example.dev+2.pem (the certificate) and example.dev+2-key.pem (the private key).

  • Use the certificate in your server:
  • Configure your local web server to use the generated certificate and private key. The exact steps will vary depending on your server software (e.g., Apache, Nginx, etc.).


    Conclusion

    While self-signed SSL certificates offer a quick and easy way to secure data transmission, they come with challenges around trust. For production environments, it's always recommended to use certificates issued by a trusted CA. However, self-signed certificates can be a valuable tool for testing and development.