certData := []byte("-----BEGIN CERTIFICATE-----\n\n-----END CERTIFICATE-----") cert, err := x509.ParseCertificate(certData) if err != nil { log.Fatal("Failed to parse certificate:", err) } fmt.Println("Parsed certificate:", cert.Subject.CommonName)
certChain := []byte("-----BEGIN CERTIFICATE-----\nIn this example, we are parsing a certificate chain and a root CA certificate from PEM-encoded byte arrays. We then create a `CertPool` containing the root CA certificate, and use `VerifyOptions` to configure the root certificates and intermediate certificates that can be used to verify the chain. Finally, we use the `Verify` method on the first certificate in the chain to verify the entire chain, and print a message indicating whether the chain is valid or not. Package Library: go.crypto.x509\n-----END CERTIFICATE-----") rootCACert := []byte("-----BEGIN CERTIFICATE-----\n \n-----END CERTIFICATE-----") // Parse the certificates certs, err := x509.ParseCertificates(certChain) if err != nil { log.Fatal("Failed to parse certificate chain:", err) } rootCA, err := x509.ParseCertificate(rootCACert) if err != nil { log.Fatal("Failed to parse root CA certificate:", err) } // Verify the certificate chain roots := x509.NewCertPool() roots.AddCert(rootCA) verifyOpts := x509.VerifyOptions{ Roots: roots, Intermediates: x509.NewCertPool(), } if _, err := certs[0].Verify(verifyOpts); err != nil { log.Fatal("Failed to verify certificate chain:", err) } fmt.Println("Certificate chain is valid!")