DEV Community

Cover image for Create a self-signed certificate with your own root CA
Sergio Peris
Sergio Peris

Posted on • Edited on • Originally published at sertxu.dev

1

Create a self-signed certificate with your own root CA

Almost everyone nowadays uses SSL certificates in almost every project.

Using Let's Encrypt is the easiest way to obtain a free certificate for your project, but if you need to use an SSL certificate locally you probably won't be able to use Let's Encrypt, so you're only left option is generating a self-signed certificate.

A self-signed certificate can be used directly but it will be marked as insecure.

If you want to create a self-signed certificate and use it without being marked as insecure, you should create first your own root CA.

Create a root CA

You can create your own root CA and trust it in every device you need.

openssl genrsa -out rootCA.key 2048

openssl req -x509 -new -nodes \
    -key rootCA.key -days 1024 \
    -out rootCA.pem
Enter fullscreen mode Exit fullscreen mode

With these two commands, you will obtain your root CA public and private keys, with these files you will be able to generate all the SSL certificates you want.

Create a new certificate using our root CA

Once you have your own root CA, you can start creating our SSL certificates.

First, you need to create the private key, then you create a certificate request using the private key.

openssl genrsa -out certificate.key 2048

openssl req -new \
    -key certificate.key \
    -out certificate.req
Enter fullscreen mode Exit fullscreen mode

Usually, this certificate request is sent to the CA in order to obtain a valid certificate. In your case, you don't have to send anyone this file because you own the CA.

So you can create the certificate yourself.

openssl x509 -req \
    -in certificate.req \
    -CA rootCA.pem \
    -CAkey rootCA.key \
    -out certificate.pem \
    -days 500
Enter fullscreen mode Exit fullscreen mode

After this, you can start using this certificate in our project.

Trust your own root CA

In order to get the full potential of having your own root CA, you must trust its certificate.

With a Windows device, you can copy the file rootCA.pem and save it as rootCA.crt, doing this will let recognize it as a certificate file, so you will be able to use the Windows Certificate Manager to install it.

Also, if you're a Firefox user, you should add it to the Firefox Certificate storage, because Firefox has its own certificate collection.

In a Linux environment, you can copy the rootCA.pem into the certificates folder. Also, you need to refresh the CA certificates archive.

cp rootCA.pem /usr/local/share/ca-certificates/
update-ca-certificates --fresh
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay