DEV Community

Adarsh Singh
Adarsh Singh

Posted on

Certificate Generation using OpenSSL locally

Steps to Create a Certificate Chain

1. Create the Root Certificate Authority (CA)
Generate a private key for the Root CA:

openssl genrsa -out root.key 4096
Enter fullscreen mode Exit fullscreen mode

Generate the Root CA certificate:

openssl req -x509 -new -nodes -key root.key -sha256 -days 3650 -out root.pem -subj "/C=US/ST=State/L=City/O=RootOrg/OU=RootCA/CN=RootCA"
Enter fullscreen mode Exit fullscreen mode

2. Create the Intermediate Certificate Authority (Optional)
Generate a private key for the Intermediate CA:

openssl genrsa -out intermediate.key 4096
Enter fullscreen mode Exit fullscreen mode

Create a Certificate Signing Request (CSR) for the Intermediate CA:

openssl req -new -key intermediate.key -out intermediate.csr -subj "/C=US/ST=State/L=City/O=IntermediateOrg/OU=IntermediateCA/CN=IntermediateCA"
Enter fullscreen mode Exit fullscreen mode

Sign the Intermediate CA certificate with the Root CA:

openssl x509 -req -in intermediate.csr -CA root.pem -CAkey root.key -CAcreateserial -out intermediate.pem -days 1825 -sha256 -extfile <(echo "basicConstraints=CA:TRUE,pathlen:0")
Enter fullscreen mode Exit fullscreen mode

3. Create the Leaf Certificate

Generate a private key for the leaf certificate:

openssl genrsa -out leaf.key 2048

Enter fullscreen mode Exit fullscreen mode

Create a Certificate Signing Request (CSR) for the leaf certificate:

openssl req -new -key leaf.key -out leaf.csr -subj "/C=US/ST=State/L=City/O=LeafOrg/OU=Leaf/CN=localhost"

Enter fullscreen mode Exit fullscreen mode

Sign the leaf certificate with the Intermediate CA:

openssl x509 -req -in leaf.csr -CA intermediate.pem -CAkey intermediate.key -CAcreateserial -out leaf.pem -days 825 -sha256 -extfile <(echo "basicConstraints=CA:FALSE
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
")
Enter fullscreen mode Exit fullscreen mode

4. Combine the Certificates into a Chain
Concatenate the certificates to create a chain:

cat leaf.pem intermediate.pem root.pem > cert_chain.pem

Enter fullscreen mode Exit fullscreen mode

Now you have:

leaf.key: Private key for the leaf certificate.

cert_chain.pem: Complete certificate chain.

5. Verify the Certificate Chain
Manually verify using OpenSSL:

openssl verify -CAfile root.pem -untrusted intermediate.pem leaf.pem

Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

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 →