Standard library / crypto/x509
crypto/x509.Certificate
Parsed X.509 certificate or creation template. Raw fields contain DER; Subject/Issuer identify names; DNSNames/IPAddresses carry SANs; NotBefore/NotAfter bound validity; KeyUsage/ExtKeyUsage restrict purpose; BasicConstraintsValid/IsCA/MaxPathLen limit CA use. ExtraExtensions override generated fields; unhandled critical extensions must not be silently ignored. Expected stdout: 'example.invalid\n'.
Formal Syntax & Structure
go syntaxgo
type Certificate struct { Raw []byte RawTBSCertificate []byte RawSubjectPublicKeyInfo []byte RawSubject []byte RawIssuer []byte
Signature []byte SignatureAlgorithm SignatureAlgorithm
PublicKeyAlgorithm PublicKeyAlgorithm PublicKey any
Version int SerialNumber *big.Int Issuer pkix.Name Subject pkix.Name NotBefore, NotAfter time.Time KeyUsage KeyUsage
Extensions []pkix.Extension
ExtraExtensions []pkix.Extension
UnhandledCriticalExtensions []asn1.ObjectIdentifier
ExtKeyUsage []ExtKeyUsage UnknownExtKeyUsage []asn1.ObjectIdentifier
BasicConstraintsValid bool IsCA bool
MaxPathLen int MaxPathLenZero bool
SubjectKeyId []byte AuthorityKeyId []byte
OCSPServer []string IssuingCertificateURL []string
DNSNames []string EmailAddresses []string IPAddresses []net.IP URIs []*url.URL
PermittedDNSDomainsCritical bool PermittedDNSDomains []string ExcludedDNSDomains []string PermittedIPRanges []*net.IPNet ExcludedIPRanges []*net.IPNet PermittedEmailAddresses []string ExcludedEmailAddresses []string PermittedURIDomains []string ExcludedURIDomains []string
CRLDistributionPoints []string
PolicyIdentifiers []asn1.ObjectIdentifier
Policies []OID}Working Example
Example Snippetgo
package main
import ( "crypto/ed25519" "crypto/rand" "crypto/x509" "crypto/x509/pkix" "fmt" "math/big" "time")
func main() { root, leaf, key, now := fixture() _, _, _, _ = root, leaf, key, now fmt.Println(leaf.DNSNames[0])}
// Fixture: ephemeral Ed25519 keys, synthetic names and fixed verification time.func fixture() (*x509.Certificate, *x509.Certificate, ed25519.PrivateKey, time.Time) { now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) pub, key, e := ed25519.GenerateKey(rand.Reader) check(e) ca := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "Demo CA"}, NotBefore: now.Add(-time.Hour), NotAfter: now.Add(time.Hour), BasicConstraintsValid: true, IsCA: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3}} root := must(x509.ParseCertificate(must(x509.CreateCertificate(rand.Reader, ca, ca, pub, key)))) leafPub, _, e := ed25519.GenerateKey(rand.Reader) check(e) t := &x509.Certificate{SerialNumber: big.NewInt(2), DNSNames: []string{"example.invalid"}, NotBefore: ca.NotBefore, NotAfter: ca.NotAfter, KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}} leaf := must(x509.ParseCertificate(must(x509.CreateCertificate(rand.Reader, t, root, leafPub, key)))) return root, leaf, key, now}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}