Go: check the SSL certificate of a website

Go: check the SSL certificate of a website

In this article, we will explore how to check the validity of an SSL certificate using the Go programming language.

Internet security is an ever-growing concern, especially when it comes to the transmission of sensitive data. One of the most common ways to ensure the security of web communications is by using Secure Sockets Layer (SSL) certificates or their successor, Transport Layer Security (TLS) certificates. These certificates encrypt communications between your browser and the web server, protecting the data from potential eavesdropping or manipulation attacks.

However, to reap the full benefits of SSL/TLS security, it is imperative to ensure that a remote site's SSL certificate is valid and trusted. In this article, we will explore how to check the validity of an SSL certificate using the Go programming language.

Import the necessary libraries

To get started, you will need to import the following Go libraries:


import (
     "crypto/tls"
     "fmt"
     "net"
)

The crypto/tls library is essential for working with SSL/TLS certificates in Go, while the fmt and net libraries will help us in data manipulation and analysis.

Example of verifying an SSL certificate

The following is an example of Go code to check the validity of a remote website's SSL certificate:


func main() {
     // Address and port of the remote website
     remoteAddr := "example.com:443"

     // Connect to the remote website
     conn, err := net.Dial("tcp", remoteAddr)
     if err != nil {
         fmt.Println("Error connecting to the website:", err)
         return
     }
     defer conn.Close()

     // TLS client configuration
     config := &tls.Config{InsecureSkipVerify: true} // Skip certificate verification (DO NOT USE IN PRODUCTION ENVIRONMENTS)
     tlsConn := tls.Client(conn, config)

     // TLS handshake with the remote website
     if err := tlsConn.Handshake(); err != nil {
         fmt.Println("Error in TLS handshake:", err)
         return
     }

     // Get the remote website certificate
     certificate := tlsConn.ConnectionState().PeerCertificates[0]

     // Check the validity of the certificate
     if err := certificate.VerifyHostname("example.com"); err != nil {
         fmt.Println("Invalid certificate:", err)
         return
     }

     // Print the certificate information
     fmt.Println("Valid certificate:")
     fmt.Println("Subject:", certificate.Subject.CommonName)
     fmt.Println("Issuer:", certificate.Issuer.CommonName)
     fmt.Println("Expires:", certificate.NotAfter)
}

In the example above, we are checking the validity of the SSL certificate of the website "example.com" using a TLS connection. However, note that we are using InsecureSkipVerify: true in the TLS client configuration, which means we are skipping certificate verification. This option should not be used in a production environment, but is useful for learning or debugging purposes.

Conclusions

Verifying the validity of SSL certificates is a crucial aspect for ensuring the security of web communications. With Go, you can perform this verification easily and effectively using the crypto/tls library. However, it is important to remember not to use the InsecureSkipVerify option in a production environment, as this may compromise the security of your web communications. Always use an appropriate verification method to ensure that your SSL certificates are valid and trusted.