Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Last active May 28, 2024 01:30
Show Gist options
  • Save narenaryan/5279f3921c9aea2b46c4d80c3d62215d to your computer and use it in GitHub Desktop.
Save narenaryan/5279f3921c9aea2b46c4d80c3d62215d to your computer and use it in GitHub Desktop.
A certificate pool in Go to verify server certificates from clients
func getCertPool(fileName string) *x509.CertPool {
// Load the server certificate from a file
certBytes, err := os.ReadFile(fileName)
if err != nil {
fmt.Println("Error reading certificate file:", err)
return nil
}
// Decode the PEM-encoded certificate
pemBlock, _ := pem.Decode(certBytes)
if pemBlock == nil {
fmt.Println("Failed to decode PEM block")
return nil
}
// Parse the certificate data
cert, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
fmt.Println("Error parsing certificate:", err)
return nil
}
// Create a certificate pool and add the server certificate to it
pool := x509.NewCertPool()
pool.AddCert(cert)
return pool
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment