Skip to content

How to Generate an SSL CSR with OpenSSL

DodaTech 2 min read

In this tutorial, you'll learn about How to Generate an SSL CSR with OpenSSL. We cover key concepts, practical examples, and best practices.

The Problem

You need an SSL certificate for your website. The certificate authority requires a Certificate Signing Request (CSR) that contains your public key and organization details. Without a CSR, you cannot obtain a signed certificate.

Quick Fix

Step 1: Generate a private key

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out example.com.key

This creates a 2048-bit RSA private key. Do not share this file. Keep it secure with chmod 600.

Step 2: Generate a CSR from the key

openssl req -new -key example.com.key -out example.com.csr

You are prompted for:

  • Common Name: example.com (the primary domain, required).
  • Organization: Your company name.
  • Organizational Unit: Department (optional).
  • City/Locality: San Francisco.
  • State/Province: California.
  • Country: US (two-letter code).

Step 3: Generate a CSR non-interactively

openssl req -new -key example.com.key -out example.com.csr \
  -subj "/C=US/ST=California/L=San Francisco/O=MyCompany/OU=IT/CN=example.com"

This is useful for automation and scripts where interactive prompts are not possible.

Step 4: Generate a CSR with SANs (Subject Alternative Names)

Create a config file san.cnf:

[req]
default_bits = 2048
prompt = no
distinguished_name = dn
req_extensions = req_ext

[dn]
C = US
ST = California
L = San Francisco
O = MyCompany
CN = example.com

[req_ext]
subjectAltName = @alt_names

[alt_names]
DNS.1 = example.com
DNS.2 = www.example.com
DNS.3 = api.example.com

Then generate:

openssl req -new -key example.com.key -out example.com.csr -config san.cnf

SANs allow a single certificate to cover multiple domain names.

Step 5: Verify the CSR

openssl req -text -noout -verify -in example.com.csr | head -20

Expected:

verify OK
Certificate Request:
    Subject: C = US, ST = California, L = San Francisco, O = MyCompany, CN = example.com
    Requested Extensions:
        X509v3 Subject Alternative Name:
            DNS:example.com, DNS:www.example.com, DNS:api.example.com

Step 6: Generate an ECC key and CSR (modern alternative)

openssl ecparam -genkey -name prime256v1 -out example.com.key
openssl req -new -key example.com.key -out example.com.csr \
  -subj "/CN=example.com"

ECC keys are smaller and faster than RSA. Most modern CAs support them.

Step 7: Submit the CSR to your CA

Copy the contents of example.com.csr into the CA's enrollment form:

cat example.com.csr

Expected output starts with:

-----BEGIN CERTIFICATE REQUEST----- ... -----END CERTIFICATE REQUEST-----


## Alternative Solutions

Use `openssl req -x509` to generate a self-signed certificate (for development only):

```bash
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout selfsigned.key -out selfsigned.crt -subj "/CN=localhost"

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Prevention

  • Always generate SANs for every domain the certificate covers.
  • Keep the private key permissions at 600 and back it up securely.
  • Use ECC (prime256v1) for better performance over RSA.
  • Verify the CSR before submitting — fixing a submitted CSR requires generating a new one.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro