Cert info
This article explains how to create a Go program that connects to a web server, extracts SSL certificate information, and displays key details such as the certificate's expiration date, issuer, and certificate chain. We will organize the code into reusable modules for clarity and maintainability.
Step 1: Overview of the Program
The program connects to a given HTTPS endpoint, retrieves the server's SSL certificate, and parses the details. The extracted information includes:
Certificate Expiry Date
Certificate Issuer
Certificate Chain
Step 2: Code Organization
We will divide the program into the following modules:
main.go
: The entry point of the application.certificate/certificate.go
: Handles the extraction and parsing of SSL certificates.utils/utils.go
: Provides utility functions for data formatting.
Step 3: The Code
1. main.go
This is the entry point of the program. It accepts a domain as input, fetches certificate information, and displays the results.
2. certificate/certificate.go
This module contains logic to retrieve and parse SSL certificates.
3. utils/utils.go
This module can contains utility functions for formatting certificate data. For now, we will keep it minimal.
Step 4: Running the Program
Build and Run: Save the files in the appropriate directories and build the program using
go build
.Run with Hostname:
./sslinfo example.comOutput Example:
Certificate Information for example.com: Issuer: C=US, O=Let's Encrypt, CN=R3 Expiry Date: Sun, 30 Jan 2025 15:04:05 UTC Certificate Chain: [1] CN=example.com [2] CN=R3 [3] CN=ISRG Root X1
Step 5: Explanation of Key Concepts
1. TLS Dialing:
The tls.Dial
function establishes a secure connection to the server and retrieves the certificate chain. This approach ensures we work with live certificate data.
2. Parsing Certificates:
The PeerCertificates
field from the connection state provides the full certificate chain. This allows us to display not only the leaf certificate but also intermediate and root certificates.
3. Time Formatting:
The time.RFC1123
format ensures human-readable dates are displayed.
Step 6: Possible Enhancements
Add Command-Line Flags: Support for flags like
--verbose
for detailed output.Verify Certificates: Validate certificates against system trust stores.
Error Handling: Improve error reporting for common issues like expired certificates.
With this modular approach, you can easily extend or maintain the code as needed. The program is simple yet powerful, allowing you to inspect SSL certificates with ease.