cert, err := x509.ParseCertificate(certBytes) if err != nil { log.Fatal(err) } if cert.IsCA { fmt.Println("This is a CA certificate.") } else { fmt.Println("This is not a CA certificate.") }
roots := x509.NewCertPool() ok := roots.AppendCertsFromPEM(rootPEM) if !ok { log.Fatal("failed to parse root certificate") } opts := x509.VerifyOptions{ Roots: roots, } cert, err := x509.ParseCertificate(certBytes) if err != nil { log.Fatal(err) } _, err = cert.Verify(opts) if err != nil { log.Fatal("failed to verify certificate") } if cert.IsCA { fmt.Println("This is a CA certificate.") } else { fmt.Println("This is not a CA certificate.") }In this example, we first create a new x509.CertPool and add a root certificate to it using the AppendCertsFromPEM function. Then, we create a x509.VerifyOptions object and set its Roots field to the CertPool we just created. Next, we parse the certificate we want to verify using x509.ParseCertificate. We then call the Verify method on the certificate, passing in the options we created earlier. If there are any errors during the verification process, we log an error and exit. Finally, we check the IsCA field of the certificate to determine if it's a CA certificate or not. Overall, these examples demonstrate how the IsCA field can be used to determine whether or not a certificate is a CA certificate. The package library is "crypto/x509".