Self-signed certificates: what they are and how to create one

13 min readUpdated

A self-signed certificate is an SSL certificate signed with its own private key instead of by a certificate authority. Its issuer and subject are the same name, and its signature checks out against the public key it carries [1]. The encryption it provides is the same as any other certificate's; what it lacks is anyone independent confirming who holds the key [5] [10]. That is why browsers stop at a warning on public sites, and why it is still the right tool for development and internal systems.

Issuer
the same name as the subject [1]
Signed with
its own private key [1]
Created with
one openssl req -x509 command [2]
Chrome shows
NET::ERR_CERT_AUTHORITY_INVALID [10]
Firefox shows
MOZILLA_PKIX_ERROR_SELF_SIGNED_CERT [10]
Protects against
passive eavesdropping, not an attacker in the middle [5]

What makes a certificate self-signed

Certificates work through delegation of trust. A certificate authority your browser already trusts signs a certificate, and by doing so states that it verified control of the name; see certificate authorities.

A self-signed certificate skips that step: its own private key signs it. RFC 5280 calls a certificate self-issued when issuer and subject are the same, and self-signed when the public key bound into it verifies its own signature [1]. The certificate is well formed and its signature is sound. OpenSSL shows both halves of that on the certificate that openssl req -x509 creates below: openssl verify cert.pem fails with error 18 at 0 depth lookup: self-signed certificate, and openssl verify -CAfile cert.pem cert.pem prints cert.pem: OK once you tell it to trust the file [10]. To anyone who does not already know the site, it is an assertion without a witness.

Why the browser warns instead of loading the site

A browser shows a full-page warning for a self-signed certificate instead of loading the site, because anyone can sign a certificate for any name: the same OpenSSL command can create a certificate with example.com in place of localhost and the result names example.com as both subject and issuer [10]. If browsers accepted such certificates, an attacker in the middle could present one, and certificate verification would stop protecting anything while the padlock stayed in place. A comment in Chromium's source puts the trade-off in one line: a self-signed certificate foils passive attackers and provides no defense against active ones [5].

How to create a self-signed certificate

OpenSSL is the certificate generator here. To create a certificate and its private key together, run one command [2]:

$ openssl req -x509 -newkey rsa:2048 -noenc -keyout key.pem -out cert.pem \
    -days 365 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"
  • -x509 makes openssl req output a certificate instead of a certificate signing request [2].

  • -newkey rsa:2048 generates a new 2048-bit RSA key, written to key.pem [2].

  • -noenc leaves the key unencrypted, which suits a development server and nothing else. It replaces -nodes, deprecated since OpenSSL 3.0. On OpenSSL 1.1.1, use -nodes [2].

  • -days 365 sets how many days the certificate is valid; without it, OpenSSL uses 30 days [2].

  • -addext "subjectAltName=DNS:localhost" adds the names the certificate covers [2].

The subjectAltName extension is not optional in practice. RFC 9525 says the common name must not be used to identify a service [3], and Chrome stopped matching names against the common name in Chrome 58 [4]. Chrome rejects a certificate with a common name and no subjectAltName for a name mismatch, even once you trust it. List every name the server will answer for.

Generate the key and the certificate separately

The OpenSSL certificate generator creates a new key every time the one-command form runs. To keep the key, generate it first and point openssl req at it with -key [2]:

$ openssl genrsa -out key.pem 2048
$ openssl req -x509 -key key.pem -out cert.pem -days <N> \
    -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"

openssl genrsa writes a 2048-bit RSA key to key.pem [11]. -key supplies the private key that signs the certificate, and the matching public key goes into it, so the result is self-signed [2]. Replace <N> with the number of days you want the certificate to be valid; the choice is yours. Because the key stays the same, you can create a new certificate on it later, for example with more names, without generating another key. Keep key.pem as you would any private key.

Without -subj, OpenSSL 3.0.13 asked for the subject fields one by one in the lab run, Common Name among them; with -subj it asked nothing [13].

Put several names in one certificate

One certificate can carry several names. Put them all in a single subjectAltName value, separated by commas: DNS: entries hold host names and IP: entries hold IP addresses, as described in the manual page for certificate extensions [11].

$ openssl req -x509 -key key.pem -out cert.pem -days <N> \
    -subj "/CN=localhost" \
    -addext "subjectAltName=DNS:localhost,DNS:dev.example.com,IP:127.0.0.1"

Check the names with openssl x509:

$ openssl x509 -in cert.pem -noout -ext subjectAltName
X509v3 Subject Alternative Name:
    DNS:localhost, DNS:dev.example.com, IP Address:127.0.0.1

Every name a client will connect to has to be in this list. The next section shows what happens when one is missing.

Check the new certificate

To check the new certificate with OpenSSL:

$ openssl x509 -in cert.pem -noout -subject -issuer -dates -ext subjectAltName
subject=CN = localhost
issuer=CN = localhost
notBefore=<creation time>
notAfter=<365 days later>
X509v3 Subject Alternative Name:
    DNS:localhost

Subject and issuer are identical, which is what self-signed means [10].

Try the certificate on a local test server

openssl s_server serves the certificate without installing a web server. Start it in one terminal; -www makes it answer a client with an HTML status page [11]:

$ openssl s_server -accept 8443 -cert cert.pem -key key.pem -www

With a second terminal, ask for the page with curl:

$ curl https://localhost:8443/
curl: (60) SSL certificate problem: self-signed certificate
$ curl --cacert cert.pem https://localhost:8443/

The first command fails because nothing trusts the certificate. The second tells curl to trust the file and gets the page [13]. Stop the server when you are done.

Check the names with openssl s_client

With the server still running, -CAfile cert.pem trusts the certificate and -verify_hostname checks a name against the names inside it [11]:

$ openssl s_client -connect localhost:8443 -CAfile cert.pem -verify_hostname localhost </dev/null 2>&1 | grep Verification
Verification: OK
$ openssl s_client -connect localhost:8443 -CAfile cert.pem -verify_hostname other.example.com </dev/null 2>&1 | grep Verification
Verification error: hostname mismatch

The second run fails because other.example.com is not in the certificate's subjectAltName list [13].

Create a self-signed certificate on Windows

On Windows, PowerShell's New-SelfSignedCertificate creates the certificate straight into a certificate store, without OpenSSL. Following Microsoft's first example:

New-SelfSignedCertificate -DnsName 'www.fabrikam.com', 'www.contoso.com' -CertStoreLocation 'Cert:\LocalMachine\My'

-DnsName puts every name into the subject alternative name list, and the first one also becomes the subject and the issuer. Without -NotAfter the certificate expires one year after it is created, and the default type is an SSL server authentication certificate [14]. The result sits in the store, not in a file; a server that reads files needs it exported, as in exporting and importing a certificate with its key.

When self-signed is the right choice

  • Local development and testing. A laptop, a build server or a staging system reachable only by the people who set it up. Nobody is asked to trust an unfamiliar site.

  • Internal services between known machines. Where both ends are configured by the same team and can pin the certificate or trust it explicitly.

  • Device management interfaces. If a device's admin page presents a certificate the device signed itself, install your own certificate where the device lets you; where it does not, restrict who can reach the interface.

What these cases share: the people connecting can verify what they are connecting to by other means. Where that is not true, a self-signed certificate is the wrong tool, however small the audience seems.

When it is not

Any site a stranger might visit: public websites, customer-facing applications, anything reachable from the internet by someone who cannot verify it independently. The old reasons for self-signing a public site, cost and effort, are gone: free authorities issue domain-validated certificates automatically through ACME. The two ways to obtain one are covered in how to get a free SSL certificate. Buying from a paid authority is the third route; all three are compared in how to make an SSL certificate browsers accept.

A self-signed certificate put on a public launch "for now" teaches every visitor who meets the warning that security warnings on your site are normal.

What browsers and command-line tools show

We served three setups from openssl s_server on this page's test machine and opened each in five clients [10]:

What browsers and command-line tools show
What the server sendsChromeFirefoxcurl and Pythonopenssl s_client
a self-signed certificateNET::ERR_CERT_AUTHORITY_INVALIDMOZILLA_PKIX_ERROR_SELF_SIGNED_CERTself-signed certificate18 (self-signed certificate)
a certificate and the self-signed root of an untrusted authorityNET::ERR_CERT_AUTHORITY_INVALIDSEC_ERROR_UNKNOWN_ISSUERself-signed certificate in certificate chain19 (self-signed certificate in certificate chain)
a certificate from an untrusted authority, without its rootNET::ERR_CERT_AUTHORITY_INVALIDSEC_ERROR_UNKNOWN_ISSUERunable to get local issuer certificate21 (unable to verify the first certificate)

Chrome reports one code for all three, so the code alone does not say which case you have. Its page reads "Your connection is not private"; under Advanced it names the code and offers "Proceed to localhost (unsafe)" [10]. Firefox separates the first case: its code means "the certificate is not trusted because it is self-signed" [6]. SEC_ERROR_UNKNOWN_ISSUER means Firefox does not recognise the issuer [6], which needs a different fix, covered on ERR_CERT_AUTHORITY_INVALID.

Command-line tools separate all three. OpenSSL 1.1.1 and earlier spell the first two messages self signed certificate and self signed certificate in certificate chain, without the hyphen [7].

Trusting it on your own machine, or running your own authority

For one developer, add the certificate to the operating system's trust store. Firefox keeps its own store: on Windows and macOS, Firefox 120 and later also import certificates a user added to the system store, a setting under Settings, Privacy & Security, Certificates [8]; on Linux, import the certificate in Firefox as well. Command-line tools can take the file directly: curl --cacert cert.pem connects without an error [10]. Either way, the warning goes away on that machine only.

If you have many internal servers, trusting one root through a private CA is less work than trusting each self-signed certificate.

Clicking through the warning is the option to avoid. The warning page looks the same whether the server is yours or an attacker's; on a shared network, telling the two apart is exactly the job the certificate failed to do. Every internal system that shows the warning trains its users to dismiss it, and the habit does not stay inside the office.

A private certificate authority for a team

For a team, a private certificate authority scales better. You create one root, install it on the devices you manage, and issue certificates from it; every service then works without warnings on those devices and on nothing else, which is exactly the intent. Tools such as mkcert automate this for local development: mkcert creates a local authority and installs it in the system trust store. Its documentation warns that the authority's private key gives complete power to intercept secure requests from your machine, so keep that key to yourself [9].

FAQ

What is a self-signed certificate?

A self-signed certificate is signed with its own private key instead of by a certificate authority. Anyone can create one with a single command; the encryption is ordinary, but nobody independent has confirmed who holds the key.

How do I create a self-signed certificate?

With OpenSSL: openssl req -x509 -newkey rsa:2048 -noenc -keyout key.pem -out cert.pem -days 365 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost". On OpenSSL 1.1.1, use -nodes instead of -noenc. Include every hostname in subjectAltName, because clients identify the server by those names, not by the common name.

Is a self-signed certificate secure?

The encryption is no weaker than with any other certificate, and it stops passive eavesdropping. What is missing is verification: a visitor cannot tell the real site from an attacker in the middle presenting its own self-signed certificate, which is what the browser warns about.

When should I use a self-signed certificate?

For local development, testing and internal services where the people or machines connecting already know what they are connecting to. Not for a public site.

How do I get rid of the self-signed certificate warning?

On a public site, replace it with a certificate from a trusted authority and automate renewal. On your own machine, add the certificate to the trust store. For a team, issue certificates from a private authority installed on managed devices.

Why does Firefox still warn after I trusted the certificate in my operating system?

Firefox keeps its own certificate store. On Windows and macOS, Firefox 120 and later also import certificates a user added to the system store, unless that setting is turned off under Settings, Privacy & Security, Certificates. On Linux, Firefox does not import them, so import the certificate or your private authority in Firefox's certificate settings as well.

What does it mean for a certificate to be signed?

A certificate carries a digital signature computed over its contents, and its issuer field names the entity that signed and issued it. In a self-signed certificate the public key bound into the certificate verifies that signature.

Can I create a certificate chain with OpenSSL?

openssl req -x509 creates one self-signed certificate. Its issuer is itself, so it has no separate issuing certificate. A certificate issued by someone else names that issuer in its issuer field; how a certificate chain works has its own page.

Sources

  1. RFC 5280, Internet X.509 Public Key Infrastructure Certificate and CRL Profile, section 3.2.

  2. OpenSSL 3.0 manual: openssl-req; OpenSSL 1.1.1 manual: req.

  3. RFC 9525, Service Identity in TLS, section 2.

  4. Chrome for Developers, Deprecations and removals in Chrome 58: remove support for commonName matching in certificates.

  5. Chromium source, net/base/net_error_list.h: CERT_AUTHORITY_INVALID.

  6. Mozilla source: mozpkix pkixnss.cpp (MOZILLA_PKIX_ERROR_SELF_SIGNED_CERT) and NSS SECerrs.h (SEC_ERROR_UNKNOWN_ISSUER).

  7. OpenSSL source, crypto/x509/x509_txt.c, branches OpenSSL_1_1_1-stable and openssl-3.0.

  8. Firefox 120 release notes; Firefox enterprise policies: Certificates, ImportEnterpriseRoots.

  9. mkcert README.

  10. Lab runs for this page: OpenSSL 3.0.13, curl 8.5.0, Python 3.14.4, Chrome for Testing 156, Firefox 147 on Linux Mint 22.1; test names localhost and example.com, test authority Example Test CA.

  11. OpenSSL 3.0 manual: genrsa, x509v3_config, s_server, s_client, verification-options.

  12. RFC 5280, sections 4.1.1.3 (signatureValue) and 4.1.2.4 (issuer).

  13. Lab runs for the added sections: OpenSSL 3.0.13, curl 8.5.0; test names localhost, dev.example.com, other.example.com.

  14. Microsoft Learn, New-SelfSignedCertificate: Example 1, -DnsName, -NotAfter, -Type.