Using openssl to get the certificate from a server

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Using openssl to get the certificate from a server

How to Use OpenSSL to Get the Certificate from a Server πŸ“œπŸ’»

So, you want to fetch the certificate of a remote server and utilize it within your Java application? No problem! OpenSSL can come to your rescue. πŸ¦Έβ€β™‚οΈ

OpenSSL is a powerful command-line tool that is widely used for SSL/TLS-related tasks. In this guide, we will walk you through the process of using OpenSSL to obtain the certificate from a server. πŸ“

The Command to Fetch the Certificate

To get started, open your command line interface (CLI) and run the following command:

openssl s_client -connect host.host:9999

Replace host.host:9999 with the actual hostname and port number of the server you want to connect to. This command establishes a connection with the server and fetches the raw certificate. 🌐

Common Issue: Self-Signed Certificate Error 😬

When you run the above command, you might encounter the following error:

depth=1 /C=NZ/ST=Test State or Province/O=Organization Name/OU=Organizational Unit Name/CN=Test CA
verify error:num=19:self signed certificate in certificate chain
verify return:0
23177:error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure:s3_pkt.c:1086:SSL alert number 40
23177:error:140790E5:SSL routines:SSL23_WRITE:ssl handshake failure:s23_lib.c:188:

This error indicates that the certificate presented by the server is self-signed, meaning it hasn't been signed by a trusted certificate authority (CA). By default, OpenSSL tries to validate the certificate chain, which fails in the case of self-signed certificates. 😱

Easy Solution: Skipping Certificate Validation πŸ™ˆ

To overcome the self-signed certificate error, you can add the -no_ssl3 flag to the command like this:

openssl s_client -connect host.host:9999 -no_ssl3

Adding the -no_ssl3 flag tells OpenSSL to skip the SSLv3 handshake, which includes the certificate validation step. This allows you to fetch the certificate of the server without encountering the self-signed certificate error. 😌

Additional Options for More Information ℹ️

If you want to retrieve additional information about the certificate, you can make use of the -showcerts option. This option will display the entire certificate chain, including the server certificate and any intermediate certificates.

openssl s_client -connect host.host:9999 -showcerts

Furthermore, if you are running OpenSSL on Debian, you can specify the CA database path using the -CApath option. The CA database provides trusted root certificates for certificate validation. Here's an example:

openssl s_client -connect host.host:9999 -CApath /etc/ssl/certs/

Note that the above path (/etc/ssl/certs/) is the default CA database path in Debian, but you may need to adjust it depending on your system configuration. πŸ—ΊοΈ

Call-to-Action: Share Your Experience and Ask Questions! πŸ—£οΈπŸ’¬

Now that you know how to use OpenSSL to fetch a server's certificate, give it a try and let us know how it goes! If you encounter any issues or have further questions, drop a comment below. We'd love to hear about your experience and assist you in any way possible. Happy coding! πŸ˜„πŸš€

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my