github.com/annwntech/go-micro/v2@v2.9.5/util/pki/certoptions.go (about)

     1  package pki
     2  
     3  import (
     4  	"crypto/ed25519"
     5  	"crypto/x509"
     6  	"crypto/x509/pkix"
     7  	"math/big"
     8  	"net"
     9  	"time"
    10  )
    11  
    12  // CertOptions are passed to cert options
    13  type CertOptions struct {
    14  	IsCA         bool
    15  	Subject      pkix.Name
    16  	DNSNames     []string
    17  	IPAddresses  []net.IP
    18  	SerialNumber *big.Int
    19  	NotBefore    time.Time
    20  	NotAfter     time.Time
    21  
    22  	Parent *x509.Certificate
    23  	Pub    ed25519.PublicKey
    24  	Priv   ed25519.PrivateKey
    25  }
    26  
    27  // CertOption sets CertOptions
    28  type CertOption func(c *CertOptions)
    29  
    30  // Subject sets the Subject field
    31  func Subject(subject pkix.Name) CertOption {
    32  	return func(c *CertOptions) {
    33  		c.Subject = subject
    34  	}
    35  }
    36  
    37  // IsCA states the cert is a CA
    38  func IsCA() CertOption {
    39  	return func(c *CertOptions) {
    40  		c.IsCA = true
    41  	}
    42  }
    43  
    44  // DNSNames is a list of hosts to sign in to the certificate
    45  func DNSNames(names ...string) CertOption {
    46  	return func(c *CertOptions) {
    47  		c.DNSNames = names
    48  	}
    49  }
    50  
    51  // IPAddresses is a list of IPs to sign in to the certificate
    52  func IPAddresses(ips ...net.IP) CertOption {
    53  	return func(c *CertOptions) {
    54  		c.IPAddresses = ips
    55  	}
    56  }
    57  
    58  // KeyPair is the key pair to sign the certificate with
    59  func KeyPair(pub ed25519.PublicKey, priv ed25519.PrivateKey) CertOption {
    60  	return func(c *CertOptions) {
    61  		c.Pub = pub
    62  		c.Priv = priv
    63  	}
    64  }
    65  
    66  // SerialNumber is the Certificate Serial number
    67  func SerialNumber(serial *big.Int) CertOption {
    68  	return func(c *CertOptions) {
    69  		c.SerialNumber = serial
    70  	}
    71  }
    72  
    73  // NotBefore is the time the certificate is not valid before
    74  func NotBefore(time time.Time) CertOption {
    75  	return func(c *CertOptions) {
    76  		c.NotBefore = time
    77  	}
    78  }
    79  
    80  // NotAfter is the time the certificate is not valid after
    81  func NotAfter(time time.Time) CertOption {
    82  	return func(c *CertOptions) {
    83  		c.NotAfter = time
    84  	}
    85  }