Last active
May 28, 2024 01:30
-
-
Save narenaryan/5279f3921c9aea2b46c4d80c3d62215d to your computer and use it in GitHub Desktop.
A certificate pool in Go to verify server certificates from clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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