|
| 1 | +package autocert |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ecdsa" |
| 5 | + "crypto/elliptic" |
| 6 | + "crypto/tls" |
| 7 | + "crypto/x509" |
| 8 | + "crypto/x509/pkix" |
| 9 | + "encoding/pem" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "math/big" |
| 13 | + "net" |
| 14 | + "net/url" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +type AutoCertOption func(*x509.Certificate) |
| 19 | + |
| 20 | +// AutoCert generates a self-signed cert using the specified keyReader for a source for private key generation |
| 21 | +func New(keyReader io.Reader, opts ...AutoCertOption) (tls.Certificate, error) { |
| 22 | + |
| 23 | + // Generate the key |
| 24 | + privKey, err := ecdsa.GenerateKey(elliptic.P256(), keyReader) |
| 25 | + if err != nil { |
| 26 | + return tls.Certificate{}, fmt.Errorf("Could not generate private key: %v\n", err) |
| 27 | + } |
| 28 | + |
| 29 | + // Build Cert |
| 30 | + cert := x509.Certificate{ |
| 31 | + SerialNumber: big.NewInt(0), |
| 32 | + Subject: pkix.Name{ |
| 33 | + CommonName: "localhost", |
| 34 | + }, |
| 35 | + |
| 36 | + // Starting jan 1, 2010 for 100 years |
| 37 | + NotBefore: time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC), |
| 38 | + NotAfter: time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Add(100 * 365 * 24 * time.Hour), |
| 39 | + |
| 40 | + IsCA: true, |
| 41 | + |
| 42 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 43 | + KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 44 | + |
| 45 | + BasicConstraintsValid: true, |
| 46 | + } |
| 47 | + |
| 48 | + // Apply the options |
| 49 | + for _, f := range opts { |
| 50 | + f(&cert) |
| 51 | + } |
| 52 | + |
| 53 | + // Create Cert |
| 54 | + derBytes, err := x509.CreateCertificate(keyReader, &cert, &cert, &privKey.PublicKey, privKey) |
| 55 | + if err != nil { |
| 56 | + return tls.Certificate{}, fmt.Errorf("Failed to create certificate: %s", err) |
| 57 | + } |
| 58 | + |
| 59 | + pKeyBytes, err := x509.MarshalECPrivateKey(privKey) |
| 60 | + if err != nil { |
| 61 | + return tls.Certificate{}, fmt.Errorf("Failed to marshal private key: %s", err) |
| 62 | + } |
| 63 | + |
| 64 | + certBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) |
| 65 | + privBytes := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: pKeyBytes}) |
| 66 | + |
| 67 | + tlsCert, err := tls.X509KeyPair(certBytes, privBytes) |
| 68 | + if err != nil { |
| 69 | + return tls.Certificate{}, fmt.Errorf("Failed to load key-pair: %s", err) |
| 70 | + } |
| 71 | + |
| 72 | + return tlsCert, nil |
| 73 | +} |
| 74 | + |
| 75 | +// SerialNummber sets the serial number for the cert |
| 76 | +func SerialNumber(sn int64) AutoCertOption { |
| 77 | + return func(c *x509.Certificate) { |
| 78 | + c.SerialNumber = big.NewInt(sn) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// CommonName sets the commont name of the certificate |
| 83 | +func CommonName(cn string) AutoCertOption { |
| 84 | + return func(c *x509.Certificate) { |
| 85 | + c.Subject = pkix.Name{ |
| 86 | + CommonName: cn, |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Organization sets the Organization(s) of the cert subject |
| 92 | +func Organization(o []string) AutoCertOption { |
| 93 | + return func(c *x509.Certificate) { |
| 94 | + c.Subject.Organization = o |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// OrganizationalUnit sets the OrganizationalUnit(s) of the cert subject |
| 99 | +func OrganizationalUnit(ou []string) AutoCertOption { |
| 100 | + return func(c *x509.Certificate) { |
| 101 | + c.Subject.OrganizationalUnit = ou |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// DNSNames sets the DNS names of the cert |
| 106 | +func DNSNames(dnsNames []string) AutoCertOption { |
| 107 | + return func(c *x509.Certificate) { |
| 108 | + c.DNSNames = dnsNames |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// URIs sets the URIs of the cert |
| 113 | +func URIs(uris []*url.URL) AutoCertOption { |
| 114 | + return func(c *x509.Certificate) { |
| 115 | + c.URIs = uris |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// IPAddresses sets the IPAddresses of the cert |
| 120 | +func IPAddresses(ips []net.IP) AutoCertOption { |
| 121 | + return func(c *x509.Certificate) { |
| 122 | + c.IPAddresses = ips |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// ValidTimes sets the times in which this cert is valid |
| 127 | +func ValidTimes(notBefore time.Time, notAfter time.Time) AutoCertOption { |
| 128 | + return func(c *x509.Certificate) { |
| 129 | + c.NotBefore = notBefore |
| 130 | + c.NotAfter = notAfter |
| 131 | + } |
| 132 | +} |
0 commit comments