simpleloanpayment.com → best internet → server proxy dns → get server certificate openssl
Posted on by Jaiden Wolfe

get server certificate openssl

Terminal window showing openssl command output for fetching a server certificate

The get server certificate openssl command retrieves the public certificate from a TLS-enabled server using OpenSSL's command-line tools. This process connects to a server's port 443 (HTTPS) or 465 (SMTPS), pulls the certificate chain, and displays it in PEM format.

OpenSSL is a free, open-source toolkit implementing the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It includes utilities for generating keys, creating certificates, and inspecting existing ones. The openssl s_client command is used to fetch the server certificate during a handshake.

For example, running openssl s_client -connect example.com:443 -servername example.com initiates a TLS connection and outputs the server's certificate chain. The output begins with the server's certificate, followed by intermediate and root CA certificates if available. This data is essential for validating trust, diagnosing misconfigurations, or verifying certificate validity.

How It Works

When you run openssl s_client -connect hostname:port, OpenSSL establishes a TCP connection to the specified host and port. It then performs a TLS handshake. During this handshake, the server sends its certificate chain to the client.

OpenSSL captures the entire chain and prints it to standard output. The chain starts with the server's certificate, followed by any intermediate certificates issued by a trusted Certificate Authority (CA), ending with a root CA certificate. These are displayed in PEM format - base64-encoded DER structures wrapped in -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- blocks.

To stop the session after retrieving the certificate, press Ctrl+C. The output remains in the terminal until closed. You can redirect it to a file using shell redirection, such as openssl s_client -connect example.com:443 > cert.pem.

Key Components and Common Options

The most frequently used options include:

For example, to retrieve the certificate from Google's secure site:

openssl s_client -connect google.com:443 -servername google.com -showcerts </dev/null

This command connects to Google's HTTPS server, sends SNI data, shows all certificates in the chain, and discards input (to avoid waiting for user input).

Types of Certificates and What They Reveal

Server certificates come in different forms based on their purpose and validation level. The most common are:

Type Validation Level Typical Use Example Output
Domain Validated (DV) Checks domain ownership only Basic websites, internal services Issued to www.example.com, signed by Let's Encrypt
Organization Validated (OV) Verifies organization identity Corporate sites, APIs Issued to company.org, verified via business registration
Extended Validation (EV) Full legal entity verification Banks, e-commerce platforms Green address bar in browsers, issued to bankname.com

When you run get server certificate openssl, the output reveals the certificate's subject (common name), issuer, validity period, public key algorithm, and signature hash. For example:

Subject: CN=www.example.com
Issuer: C=US, O=Let's Encrypt, CN=R3
Not Before: Jan 15 12:00:00 2024 GMT
Not After : Apr 15 12:00:00 2024 GMT
Signature Algorithm: sha256WithRSAEncryption

This shows the certificate is valid for 90 days, issued by Let's Encrypt's R3 intermediate CA, and uses RSA with SHA-256 signing.

Common Mistakes and Fixes

A frequent error is omitting the -servername flag when connecting to a virtual host. Without it, some servers return a default certificate instead of the correct one. For instance, a server hosting both site1.com and site2.com may serve the wrong certificate if SNI is not sent.

Another issue is missing the </dev/null redirection. Without it, the command waits for input after the handshake completes. This causes the terminal to hang until manually interrupted. Always pipe input or use -quiet to suppress prompts.

Some users expect the command to save the certificate automatically. It does not. You must redirect output to a file. For example:

openssl s_client -connect example.com:443 -servername example.com -showcerts > server_cert.pem

After saving, you can inspect the file with openssl x509 -in server_cert.pem -text -noout to parse details without needing the full chain.

Real-World Example: Diagnosing a Misconfigured Site

In 2023, a web application hosted on AWS returned a certificate for api.example.net instead of app.example.net. Users saw browser warnings due to mismatched domain names.

To diagnose, I ran:

openssl s_client -connect app.example.net:443 -servername app.example.net -showcerts </dev/null | grep "Subject:"

The output showed:

Subject: CN=api.example.net

This confirmed the server was configured with the wrong certificate. The fix involved updating the SSL/TLS configuration in the load balancer to bind the correct certificate to the app.example.net host header.

Using get server certificate openssl helped isolate the issue quickly. Without it, troubleshooting would have required deeper network analysis or access to the server's configuration files.

What It's Not

Do not confuse get server certificate openssl with certificate generation. The command only retrieves an existing certificate from a live server. It cannot create a private key, generate a Certificate Signing Request (CSR), or sign a certificate.

Also, it does not verify whether the certificate is trusted. A successful retrieval does not mean the chain is valid. You must validate the certificate chain using openssl verify or a tool like curl --cacert to check trust.

For example, a self-signed certificate will be retrieved successfully but will fail verification. To test trust, run:

openssl verify -CAfile ca-bundle.crt server_cert.pem

If the output says "OK", the certificate is trusted. If it says "error" or "unable to get local issuer certificate", the chain is broken or untrusted.

Best Practices and Tools

Always use -servername when querying a specific domain. This ensures the correct certificate is returned, especially in shared hosting environments.

Save the output to a file for later inspection. Use openssl x509 -in cert.pem -text -noout to examine fields like key usage, extended key usage, and subject alternative names.

For automated checks, combine the command with scripting. For example, a Bash function to check multiple domains:

check_ssl() {
  local domain=$1
  openssl s_client -connect $domain:443 -servername $domain -showcerts </dev/null | \
    openssl x509 -noout -subject -issuer -dates
}

This function returns the subject, issuer, and validity dates for each domain.


Related Posts