π SSL/TLS
A comprehensive reference for working with SSL/TLS, certificates, and OpenSSL.
π Core Concepts
- SSL vs TLS
- SSL (Secure Sockets Layer) β Deprecated
-
TLS (Transport Layer Security) β Modern, secure replacement (use TLS 1.2 or 1.3)
-
Encryption Types
- Asymmetric (Public/Private Key): Used in the handshake for authentication.
-
Symmetric (Shared Key): Used for fast bulk data encryption after handshake.
-
Digital Certificates
- Prove ownership of a domain/organization.
- Issued and validated by Certificate Authorities (CAs).
-
Contain public key + metadata (subject, issuer, validity period, etc.).
-
Chain of Trust
- Root CA β Intermediate CA(s) β Server Certificate
- Clients must trust the Root CA.
π§ OpenSSL Essentials
π Key Management
Generate a 2048-bit private key:
openssl genrsa -out private.key 2048
````
Generate a 4096-bit RSA key (stronger):
```bash
openssl genpkey -algorithm RSA -out private.key -pkeyopt rsa_keygen_bits:4096
Generate an EC (Elliptic Curve) key:
π Certificates
Generate a Certificate Signing Request (CSR):
Create a Self-Signed Certificate (valid 365 days):
View Certificate Details:
Check Expiry Date:
Verify CSR contents:
π Validation
Check if private key and certificate match:
openssl rsa -noout -modulus -in private.key | openssl md5
openssl x509 -noout -modulus -in cert.pem | openssl md5
Test SSL/TLS handshake with server:
Test specific protocol:
Check OCSP status (revocation):
π File Formats
Extension | Description |
---|---|
.key |
Private key |
.csr |
Certificate Signing Request |
.crt / .cer |
Certificate (X.509) |
.pem |
Base64-encoded key/cert (common on Linux) |
.der |
Binary format certificate (common on Windows/Java) |
.pfx / .p12 |
PKCS#12 bundle (certificate + private key, often for Windows/IIS) |
.jks |
Java Keystore |
π Cipher Suites & Protocols
- Prefer TLS 1.3 (or at least TLS 1.2).
-
Disable weak/obsolete protocols:
-
SSLv2, SSLv3, TLS 1.0, TLS 1.1
-
Disable weak ciphers:
-
RC4, DES, 3DES, EXPORT, NULL
-
Strong ciphers (examples):
-
ECDHE-ECDSA-AES256-GCM-SHA384
ECDHE-RSA-AES128-GCM-SHA256
Check available ciphers:
π οΈ Server Config Snippets
Apache (httpd.conf / ssl.conf)
SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite HIGH:!aNULL:!MD5:!3DES:!RC4
SSLHonorCipherOrder on
Nginx (nginx.conf)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers on;
π‘οΈ Best Practices
- Always use TLS 1.2 or TLS 1.3.
- Use 2048+ bit RSA or Elliptic Curve (P-256, P-384) keys.
- Enable Perfect Forward Secrecy (PFS) (via ECDHE).
- Automate renewal with Letβs Encrypt / Certbot.
- Rotate certificates before they expire.
-
Regularly audit SSL/TLS config with:
testssl.sh
π Useful Tools
- OpenSSL β Core SSL/TLS toolkit
- Certbot β Free automated TLS certs from Letβs Encrypt
- mkcert β Local development trusted certificates
- sslscan / testssl.sh β Scan SSL/TLS configurations
- keytool β Manage Java Keystores
- cfssl β PKI/TLS toolkit by Cloudflare
π Typical Workflow for Production
- Generate private key
- Commercial CA (DigiCert, GlobalSign, etc.)
- Free CA (Letβs Encrypt via Certbot)
-
Receive signed certificate
-
cert.pem
(your cert) chain.pem
(intermediate certs)fullchain.pem
(cert + intermediates)- Deploy to server with private key + cert + chain.
- Test setup:
π TLS Handshake (Simplified)
sequenceDiagram
participant Client
participant Server
Client->>Server: ClientHello (supported ciphers, TLS version, random)
Server->>Client: ServerHello (chosen cipher, cert, random)
Client->>Server: Verify cert, send pre-master secret (encrypted with serverβs public key)
Server->>Client: Decrypts with private key, derives session key
Note over Client,Server: Both sides now have the same symmetric key
Client->>Server: Finished (encrypted)
Server->>Client: Finished (encrypted)
Note over Client,Server: Secure symmetric encryption begins