Mastering OpenSSL: From Creating Self-Signed Certs to ‘Lightning-Fast’ TLS Debugging

Security tutorial - IT technology blog
Security tutorial - IT technology blog

The 2 AM Phone Call and the Power of OpenSSL

My phone buzzed incessantly at 2 AM. The monitoring dashboard was glowing red with 502 Bad Gateway errors on an Nginx cluster handling over 500 transactions per second. Checking the server logs, the line SSL routines:ssl3_get_record:wrong version number immediately caught my eye. It turned out a colleague had just updated the Cipher Suite but forgot to check compatibility with older clients.

At times like this, there’s no time for GUI tools. The only thing that can save your sleep is precise OpenSSL command-line skills. After auditing over 10 real-world projects, I’ve noticed a clear pattern: most serious security issues stem from misconfigured certificates or a lack of basic TLS debugging skills.

Which Tool Should You Use for Certificate Management?

Before we dive into commands, let’s look at the common methods a SysAdmin typically uses:

  • Certbot (Let’s Encrypt): The #1 choice for public websites thanks to 100% automation. However, it’s nearly useless in Intranet environments or backend services without public domains.
  • UI Managers (like KeyStore Explorer): Intuitive, but requires a Desktop environment. You can’t install it on a minimalist Ubuntu Server with only 512MB of RAM.
  • OpenSSL CLI: The “Swiss Army Knife” available on every Linux distribution. It allows you to manipulate every byte of a certificate, simulate connections, and encrypt files extremely fast.

Why is OpenSSL CLI Still ‘King’?

Its greatest strength lies in speed and flexibility. It takes less than 2 seconds to verify if a certificate matches its private key. Although the syntax can be dry and hard to remember, once mastered, you gain full control over your system’s security layer.

Practical Implementation: From Creation to Debugging

1. Creating a Self-Signed Certificate in 30 Seconds

For Lab environments or internal Docker Registries, buying a commercial SSL is a waste. I usually use the following command to quickly generate a Key and Certificate pair valid for 1 year:

openssl req -x509 -newkey rsa:4096 -keyout server.key -out server.crt -days 365 -nodes -subj "/C=VN/ST=Hanoi/L=Hanoi/O=ITFromZero/CN=internal.dev"

Note the important parameters:

  • -x509: Directly output a self-signed certificate instead of a Certificate Signing Request (CSR).
  • -nodes: Do not set a password for the private key. This allows Nginx or Apache to restart automatically without requiring manual password entry.
  • -rsa:4096: 4096-bit key length. RSA 2048 is no longer considered secure enough against modern supercomputing systems. While 4096-bit makes the handshake about 15-20% slower, the security is far superior.

2. ‘Inspecting’ TLS Handshake Errors with s_client

This is the skill that has saved me most during on-call shifts. When an application reports a connection failure to a third-party API, don’t rush to blame the code. Use s_client to witness the SSL handshake process firsthand:

openssl s_client -connect api.example.com:443 -servername api.example.com

This command displays the entire Certificate Chain. If you see the line Verify return code: 21 (unable to verify the first certificate), the server is definitely missing an Intermediate Certificate. This is a classic mistake when configuring SSL for Nginx that many beginners make.

Pro tip: Want to check if a server still supports the outdated TLS 1.0 protocol? Add the flag -tls1. If the connection succeeds, you should disable it immediately to avoid vulnerabilities like POODLE.

3. Encrypting Sensitive Files with AES-256

Sending database backup files via Telegram or Email is an extremely dangerous habit. Instead of sending raw files, I always encrypt them with the AES-256 algorithm first:

# Encrypt file (using PBKDF2 for increased security)
openssl enc -aes-256-cbc -salt -pbkdf2 -in backup.sql -out backup.sql.enc

# Decrypt file
openssl enc -aes-256-cbc -d -pbkdf2 -in backup.sql.enc -out backup.sql

The system will prompt for a password. Only those with the password can read the file content. This is the fastest way to protect data without installing bulky software.

The Secret to Verifying Key and Cert Matching

Imagine you have 5 .key files and 5 .crt files mixed up in the /etc/ssl directory. How do you know which pair goes together? If misconfigured, Nginx will refuse to start.

The fastest way is to compare the MD5 hash of the modulus. If the two results are identical, they are a perfect match:

openssl x509 -noout -modulus -in server.crt | openssl md5
openssl rsa -noout -modulus -in server.key | openssl md5

Lessons Learned from the Trenches

After years of working with systems, I’ve realized that OpenSSL isn’t just a tool—it’s a standard. Mastering these commands has saved me hours of aimless searching through log files.

Don’t be intimidated by OpenSSL’s dry commands. Start by creating self-signed certs for personal projects. Then, use s_client to “peek” at what cipher suites major websites are using. That’s the shortest path to turning security theory into high-level practical skills.

If a server encounters an SSL error, prioritize using openssl s_client before searching StackOverflow. The returned output will usually point you exactly to the problem.

Share: