How to create a valid self signed SSL Certificate?
Skills: Medium - Advanced
Prerequisites: none
Self-Signed Certificates
Generate CA
Generate RSA
openssl genrsa -aes256 -out ca-key.pem 4096Generate a public CA Cert
openssl req -new -x509 -sha256 -days 365 -key ca-key.pem -out ca.pemOptional Stage: View Certificate’s Content
openssl x509 -in ca.pem -text
openssl x509 -in ca.pem -purpose -noout -textGenerate Certificate
Create a RSA key
openssl genrsa -out cert-key.pem 4096Create a Certificate Signing Request (CSR)
openssl req -new -sha256 -subj "/CN=yourcn" -key cert-key.pem -out cert.csrCreate a extfile with all the alternative names
echo "subjectAltName=DNS:your-dns.record,IP:257.10.10.1" >> extfile.cnf# optional
echo extendedKeyUsage = serverAuth >> extfile.cnfCreate the certificate
openssl x509 -req -sha256 -days 365 -in cert.csr -CA ca.pem -CAkey ca-key.pem -out cert.pem -extfile extfile.cnf -CAcreateserialCertificate Formats
X.509 Certificates exist in Base64 Formats PEM (.pem, .crt, .ca-bundle), PKCS#7 (.p7b, p7s) and Binary Formats DER (.der, .cer), PKCS#12 (.pfx, p12).
Convert Certs
| COMMAND | CONVERSION |
|---|---|
openssl x509 -outform der -in cert.pem -out cert.der | PEM to DER |
openssl x509 -inform der -in cert.der -out cert.pem | DER to PEM |
openssl pkcs12 -in cert.pfx -out cert.pem -nodes | PFX to PEM |
Verify Certificates
openssl verify -CAfile ca.pem -verbose cert.pem
Install the CA Cert as a trusted root CA
On Debian & Derivatives
-
Move the CA certificate (
ca.pem) into/usr/local/share/ca-certificates/ca.crt. -
Update the Cert Store
sudo update-ca-certificatesRefer the documentation here and here.
On Fedora
-
Move the CA certificate (
ca.pem) to/etc/pki/ca-trust/source/anchors/ca.pemor/usr/share/pki/ca-trust-source/anchors/ca.pem -
Now run (with sudo if necessary)
update-ca-trustRefer the documentation here.
On Arch
System-wide – Arch(p11-kit)
(From arch wiki)
Run (As root)
trust anchor --store myCA.crt- The certificate will be written to /etc/ca-certificates/trust-source/myCA.p11-kit and the “legacy” directories automatically updated.
- If you get “no configured writable location” or a similar error, import the CA manually:
- Copy the certificate to the /etc/ca-certificates/trust-source/anchors directory.
Run (As root)
update-ca-trustwiki page here
On Windows
Assuming the path to your generated CA certificate as C:\ca.pem, run:
Import-Certificate -FilePath "C:\ca.pem" -CertStoreLocation Cert:\LocalMachine\Root- Set
-CertStoreLocationtoCert:\CurrentUser\Rootin case you want to trust certificates only for the logged in user.
OR
In Command Prompt, run:
certutil.exe -addstore root C:\ca.pemcertutil.exeis a built-in tool (classicSystem32one) and adds a system-wide trust anchor.
On Android
The exact steps vary device-to-device, but here is a generalised guide:
- Open Phone Settings
- Locate
Encryption and Credentialssection. It is generally found underSettings > Security > Encryption and Credentials - Choose
Install a certificate - Choose
CA Certificate - Locate the certificate file
ca.pemon your SD Card/Internal Storage using the file manager. - Select to load it.
- Done!