To get an SSL server certificate from a server, you use OpenSSL or your browser to connect to the server’s port 443 and retrieve the certificate it presents during the TLS handshake. This gives you the public certificate, which you can inspect, save, or use to troubleshoot issues.
Why You Might Need the Server’s Certificate
There are several common reasons to fetch a certificate directly from a live server:
- Verifying expiration: Check if the certificate is still valid before it causes a service outage.
- Checking the certificate chain: See if the server sends the full chain or is missing intermediate certificates.
- Debugging TLS errors: Understand why a client is rejecting the certificate (e.g., hostname mismatch, self-signed, expired).
- Testing after renewal: Confirm the new certificate is being served correctly.
- Security auditing: Inspect the key size, signature algorithm, and subject alternative names.
Using OpenSSL to Get the Certificate
The most common method is with OpenSSL’s s_client command. The basic syntax is:
openssl s_client -connect example.com:443 -showcerts
This connects to example.com on port 443 (HTTPS) and prints the certificate chain. The -showcerts flag displays all certificates in the chain, not just the server certificate. The output includes the server certificate in PEM format (between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----), followed by any intermediate certificates.
To save the certificate to a file, you can redirect the output and extract the PEM block:
openssl s_client -connect example.com:443 -showcerts < /dev/null | sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p' > cert.pem
The < /dev/null prevents the command from hanging waiting for input. The sed command extracts the first certificate block. If you want only the server certificate (not the intermediates), use -showcerts and take the first block, or omit -showcerts – but note that without it, OpenSSL still prints the server certificate, just not the intermediates.
Using a Web Browser to View the Certificate
If you prefer a graphical interface, most browsers let you view the certificate details:
- Chrome/Edge: Click the padlock icon in the address bar, then click “Connection is secure” and “Certificate is valid”.
- Firefox: Click the padlock, then “Connection secure” and “More information”.
- Safari: Click the padlock and select “Show Certificate”.
This shows the certificate’s validity period, issuer, and subject, but not the full chain in some browsers. For a detailed view, use OpenSSL.
Understanding the Output
The OpenSSL output contains several important fields:
- subject: The entity the certificate is issued to (e.g.,
CN=example.com). - issuer: The certificate authority that issued it.
- Not Before / Not After: Validity period.
- Public Key Algorithm: Usually RSA or ECDSA.
- Signature Algorithm: e.g., SHA256withRSA.
- Subject Alternative Names: Lists all domains the certificate is valid for.
To see these details in a readable format, save the certificate to a file and run:
openssl x509 -in cert.pem -text -noout
This prints the full certificate details, including extensions like SANs and key usage.
Common Problems and How to Fix Them
When fetching a certificate, you might encounter issues:
- Connection refused/timeout: The server may not be listening on port 443, or a firewall is blocking. Try
telnet example.com 443to test connectivity. - Self-signed certificate: OpenSSL will still show it, but you’ll see a verification error. Use
-verify_return_errorto see the error code. - Incomplete chain: If the server doesn’t send intermediate certificates, clients may fail to validate. The
-showcertsoutput will show only the server certificate. - Hostname mismatch: The certificate’s CN/SANs don’t include the hostname you connected to. Check the SANs with
-text.
Alternatives to OpenSSL
If OpenSSL isn’t available, you can use:
- curl:
curl -vI https://example.comshows the certificate details, but not the full PEM. To get the PEM, usecurl --cert-statusor combine withopenssl. - Nmap: The
ssl-certscript retrieves and displays the certificate:nmap --script ssl-cert -p 443 example.com. - Online tools: Websites like SSL Labs or “SSL Checker” let you enter a domain and see the certificate chain, but they don’t give you the raw PEM file.
Frequently Asked Questions
Can I get the private key too?
No. The TLS handshake only exposes the public certificate. The private key never leaves the server. You can only retrieve the public certificate.
Why does the certificate chain matter?
Clients need the full chain to validate the server certificate. If the server sends only its own certificate, the client must fetch missing intermediates from the internet, which can fail in some environments. Checking the chain helps diagnose such issues.
What if the server uses a non-standard port?
Use the -connect option with the correct port, e.g., openssl s_client -connect example.com:8443 for HTTPS on port 8443.
How do I check if the certificate is expired?
Look at the “Not After” date in the output. You can also use openssl x509 -in cert.pem -noout -enddate after saving the certificate.
Can I use this method for other protocols like SMTP or IMAP?
Yes. Use the appropriate port and starttls option. For example, for SMTP on port 587, use openssl s_client -starttls smtp -connect example.com:587.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Microsoft Surface Duo: The Foldable Phone Story
- Samsung SM-T813 Hard Reset: Step-by-Step Guide
- Mafia Clash Codes: Real Meanings & Usage
- Remove Programs from Mac: Full Uninstall Guide
- Project Sentinel Morning Digest: Data Stack Failure Alerts
- Best KVM VPS Hosting: Top Picks & Guide