Published May 21, 2025 ⦁ 9 min read
How to generate a valid self-signed SSL Certificate?

How to generate a valid self-signed SSL Certificate?

A self-signed SSL certificate is a cost-free option to secure your local development environment, internal servers, or staging setups. Unlike certificates from trusted Certificate Authorities (CA), these are created and signed by you, making them ideal for testing purposes.

Key Highlights:

  • Cost: Free
  • Use Cases: Local development, internal testing, and staging
  • Downside: Browsers flag them as untrusted (can be bypassed).

Steps to Create:

  1. Prepare macOS Environment: Update OpenSSL via Homebrew and set up your working directory.
  2. Generate Key & CSR: Use OpenSSL to create a private key and certificate signing request.
  3. Add SANs (Subject Alternative Names): Modern browsers require SANs for compatibility.
  4. Sign Certificate: Create a self-signed certificate valid for 365 days.
  5. Install on macOS: Add the certificate to Keychain and set it to "Always Trust."

Quick Comparison:

Feature Self-Signed Certificate CA-Issued Certificate
Cost Free $8.78–$300/year
Trust Level Local only Universal browser trust
Identity Validation None Extensive validation
Use Cases Development, testing Production environments

This guide walks you through the entire process, from setup to installation, ensuring your local HTTPS environment is secure and functional.

Setting Up macOS Requirements

Before generating a self-signed SSL certificate, you’ll need to prepare your macOS environment. Here's how to get everything set up.

OpenSSL Setup Guide

OpenSSL

macOS comes with an older version of OpenSSL, which might not meet your needs. To get the latest version, you’ll use Homebrew.

  • Check Your Current OpenSSL Version
    Open Terminal and run the following command:
    openssl version
    
    If the output mentions LibreSSL or an outdated OpenSSL version, you’ll need to update it.
  • Install the Latest OpenSSL
    First, update Homebrew and then install OpenSSL 3:
    brew update
    brew install openssl@3
    
    Once installed, create a symbolic link to make the new version easily accessible:
    ln -s /usr/local/opt/openssl/bin/openssl /usr/local/bin/openssl
    

Now your system is ready for certificate generation.

Terminal Setup

A quick Terminal setup ensures smooth certificate creation.

  • File Permissions and Working Directory: Make sure you have read/write access to the folder where certificates will be stored.
  • Admin Access: You’ll need sudo privileges to perform system-level operations.

Start by creating a dedicated directory for your SSL certificates:

mkdir ~/ssl-certificates
cd ~/ssl-certificates

Once this is done, you’re ready to move on to integrating your certificates with macOS Keychain.

macOS Keychain Basics

The macOS Keychain is your system’s built-in tool for managing certificates and ensuring they work seamlessly with browsers and other services. To get started, open Keychain Access (found under Applications > Utilities).

For proper integration:

  • Add your certificates to the System Keychain.
  • Adjust trust settings as needed.
  • Make sure to include the subjectAltName property (details on this will come during certificate creation).

With these steps complete, your macOS environment is fully prepared for generating and managing SSL certificates.

Creating Your Self-Signed Certificate

To set up a self-signed SSL certificate using OpenSSL, follow these steps to ensure it's secure and browser-compatible.

With your macOS environment ready, you can dive into creating your certificate.

Generate Key and CSR

Start by navigating to your desired directory:

cd ~/ssl-certificates

Next, create a 2048-bit RSA private key and a Certificate Signing Request (CSR):

openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr

You'll be prompted to input some details. Here's what you'll need to provide:

  • Country Name: US
  • State: Your state (e.g., California)
  • Locality: Your city (e.g., San Francisco)
  • Organization Name: Your company name
  • Organizational Unit: Department name (e.g., IT)
  • Common Name: Your domain name

Configure Subject Alternative Names

Modern browsers demand Subject Alternative Names (SANs) for certificate validation. To include SANs, create a configuration file called certificate.conf:

cat > certificate.conf << EOL
[req]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn

[dn]
C = US
ST = California
L = San Francisco
O = MyOrganization
OU = IT Department
CN = example.com

[req_ext]
subjectAltName = @alt_names

[alt_names]
DNS.1 = example.com
DNS.2 = www.example.com
DNS.3 = localhost
IP.1 = 127.0.0.1
EOL

This file defines the necessary SANs and ensures compatibility with modern browsers.

Sign Your Certificate

Now, generate a self-signed certificate that includes SANs:

openssl req -x509 -nodes \
  -sha256 \
  -newkey rsa:4096 \
  -keyout domain.key \
  -out domain.crt \
  -days 365 \
  -config certificate.conf \
  -extensions req_ext

This command will produce:

  • A 4096-bit private key (domain.key)
  • A self-signed certificate (domain.crt) valid for 365 days
  • SHA-256 encryption for improved security
  • SANs configured for browser compatibility

To confirm your certificate's details, run:

openssl x509 -in domain.crt -text -noout

Look for the "X509v3 extensions" section in the output to verify that your SANs are correctly listed. This ensures your certificate will meet the requirements of modern web browsers.

The next step is to import your certificate into macOS Keychain for use.

Installing Certificates on macOS

To use a self-signed certificate on macOS, you’ll need to add it to the Keychain and adjust its trust settings.

Add to Keychain

  1. Open Keychain Access by pressing Command + Space, typing "Keychain Access", and hitting Enter.
  2. Drag your domain.crt file into the System keychain (preferred) or the login keychain.
  3. Enter your admin password if prompted.

"Here is a quick trick you can use to make sure your browsers accept self-generated SSL certificates on OS X." - Toby Osbourn

Once the certificate is added, you’ll need to change its trust settings to ensure browsers recognize it.

Set Trust Settings

  1. Find the certificate you just added, double-click it, and expand the Trust section.
  2. Set When using this certificate to Always Trust, and provide your admin password if prompted.
  3. Restart your browsers and visit your local site to confirm the certificate is working.

For proper browser recognition, make sure your certificate includes a subjectAltName. This step is critical for avoiding browser warnings.

Security and Problem Solving

Keeping your self-signed certificate and private key secure is essential for maintaining HTTPS functionality and protecting your data.

Private Key Security

"The private key is a critical component of your SSL certificate and data protection"

Here are some key practices to safeguard your private key:

  • Generate private keys directly on the server where they’ll be used to minimize exposure.
  • Use a robust key management system (KMS) to store and manage keys securely.
  • Mark imported keys as non-exportable to prevent unauthorized access.
  • Keep encrypted backups off-site, using AES encryption paired with strong passphrases for added security.

Fix Common Certificate Errors

Once your keys are secure, the next step is to tackle common certificate-related issues. Here’s a quick guide:

Problem Solution
Browser Trust Warnings Add the certificate to the Keychain's Trusted Root Authority.
Invalid Certificate Format Regenerate the certificate with the correct SAN (Subject Alternative Name) records.
Time-based Validation Errors Set macOS date and time to automatic to ensure proper validation.
Cached Certificate Issues Clear your browser cache and reload the page.

"If Apple has indeed changed the requirements for self-signed certificates, this should be explicitly communicated to users, in my opinion."

  • NullGazonk, Apple Developer Forums User

Development Certificate Setup

Self-signed certificates can be helpful in testing environments, but they need to be handled carefully to avoid potential security risks.

"Self-signed certificates may be useful in testing and sandbox environments, but they pose significant security risks when they make it into production environments"

Follow these best practices to manage certificates in development environments:

  • Automate the certificate lifecycle for issuance, renewal, and expiration to avoid manual errors.
  • Include SAN entries for all development domains to ensure compatibility.
  • Install the CA certificate in your development trust store to establish trust.
  • Use intermediate certificates to build a proper trust chain.
  • If a private key is compromised, revoke the certificate immediately and generate a new one.
  • Regularly monitor access logs to detect and address any security issues.

Summary

Creating a self-signed SSL certificate for local development involves following proper security practices and ensuring correct configuration. Here's a quick breakdown of the process:

Phase Key Considerations Best Practice
Generation Private Key Security Generate the private key directly on the target server for added security.
Configuration Domain Matching Set the Common Name (CN) to match the domain or IP address of your server.
Installation Trust Settings Add the certificate to your system or browser trust store to avoid warnings.
Management Certificate Lifecycle Monitor expiration dates and track access logs to maintain security.

For example, a Synology NAS user on Reddit, Iamgalavanter, shared a helpful tip: exporting the private key as a .pem file instead of .p12 resolved "Invalid private key" errors during DSM imports. Additionally, they found that mapping the NAS's fixed IP to 127.0.0.1 in the hosts file ensured the certificate worked as expected.

If you're looking for tools to simplify this process, mkcert is a great option for creating locally trusted certificates. However, it’s important to remember that self-signed certificates only provide encryption - they don’t verify the identity of the server.

"If Apple has indeed changed the requirements for self-signed certificates, this should be explicitly communicated to users, in my opinion."

  • NullGazonk, Apple Developer Forums User

When troubleshooting certificate issues, focus on these key areas:

  • Verify that the complete certificate chain is installed correctly.
  • Ensure the root CA is properly trusted by your system or browser.
  • Keep private keys stored securely to avoid unauthorized access.

FAQs

Why do browsers show warnings for self-signed SSL certificates, and how can I safely bypass them?

Browsers show warnings for self-signed SSL certificates because these certificates aren't issued by a trusted Certificate Authority (CA). Instead, they're generated and signed by the same entity, meaning there's no independent verification to confirm their legitimacy. This system is designed to alert users to potential security risks since self-signed certificates can sometimes be exploited for malicious purposes.

For testing or development, you can bypass these warnings in most browsers. Here's how:

  • Chrome: On the warning page, simply type thisisunsafe.
  • Firefox: Click Advanced and then select Accept the Risk and Continue.
  • Safari: Click Show Details and then choose Visit Website.

It's important to remember that bypassing these warnings should only be done in secure, controlled environments, such as local development setups, to avoid exposing yourself to security threats.

What are Subject Alternative Names (SANs), and why do modern browsers require them?

Subject Alternative Names (SANs)

Subject Alternative Names (SANs) are a feature in SSL/TLS certificates that let you secure multiple domain names, subdomains, or even IP addresses under a single certificate. This is crucial for modern web security because browsers depend on the SAN field to confirm that the domain you're visiting matches one of the names listed in the certificate. This verification process helps ensure a secure connection and reinforces user confidence.

These days, SANs are more important than ever. Relying only on the Common Name (CN) field is outdated and no longer considered secure. Current best practices emphasize using SANs to meet browser standards and maintain compatibility with secure HTTPS protocols.

How can I keep my private key secure when using a self-signed SSL certificate?

To keep your private key safe when using a self-signed SSL certificate, here are some essential practices to follow:

  • Store it securely: Always generate and store your private key directly on the server where it will be used. Avoid sharing it or saving it in locations that aren't secure. For an extra layer of protection, consider using hardware security modules (HSMs) or encrypted external storage devices.
  • Encrypt the key: Never leave your private key in an unencrypted state. Use strong encryption methods, such as the PKCS#12 format, to protect the file and minimize the risk of unauthorized access.
  • Restrict access: Make sure only those who absolutely need to access the private key can do so. Regularly review and update permissions to maintain tight security.

By sticking to these steps, you can reduce the chances of your private key being compromised and ensure the integrity of your self-signed SSL certificate remains intact.

Related posts