2021-01-14 18:42:12 -07:00
|
|
|
// Package certificate provides utility functions for TLS certificates.
|
|
|
|
package certificate
|
2020-09-27 11:50:48 -06:00
|
|
|
|
|
|
|
import (
|
2020-10-13 15:33:14 -06:00
|
|
|
"crypto"
|
2020-11-03 17:43:04 -07:00
|
|
|
"crypto/ecdsa"
|
2020-09-27 11:50:48 -06:00
|
|
|
"crypto/ed25519"
|
2020-11-03 17:43:04 -07:00
|
|
|
"crypto/elliptic"
|
2020-09-27 11:50:48 -06:00
|
|
|
"crypto/rand"
|
2020-09-27 21:49:41 -06:00
|
|
|
"crypto/tls"
|
2020-09-27 11:50:48 -06:00
|
|
|
"crypto/x509"
|
2020-11-02 21:11:46 -07:00
|
|
|
"crypto/x509/pkix"
|
2020-10-31 14:33:56 -06:00
|
|
|
"encoding/pem"
|
2020-09-27 11:50:48 -06:00
|
|
|
"math/big"
|
|
|
|
"net"
|
2020-10-31 14:33:56 -06:00
|
|
|
"os"
|
2020-09-27 11:50:48 -06:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-01-14 18:42:12 -07:00
|
|
|
// CreateOptions configures the creation of a TLS certificate.
|
|
|
|
type CreateOptions struct {
|
2020-11-04 22:04:58 -07:00
|
|
|
// Subject Alternate Name values.
|
|
|
|
// Should contain the DNS names that this certificate is valid for.
|
|
|
|
// E.g. example.com, *.example.com
|
|
|
|
DNSNames []string
|
|
|
|
|
2021-01-14 18:42:12 -07:00
|
|
|
// Subject Alternate Name values.
|
|
|
|
// Should contain the IP addresses that the certificate is valid for.
|
|
|
|
IPAddresses []net.IP
|
|
|
|
|
2020-11-04 22:04:58 -07:00
|
|
|
// Subject specifies the certificate Subject.
|
|
|
|
//
|
|
|
|
// Subject.CommonName can contain the DNS name that this certificate
|
|
|
|
// is valid for. Server certificates should specify both a Subject
|
|
|
|
// and a Subject Alternate Name.
|
|
|
|
Subject pkix.Name
|
|
|
|
|
|
|
|
// Duration specifies the amount of time that the certificate is valid for.
|
|
|
|
Duration time.Duration
|
|
|
|
|
|
|
|
// Ed25519 specifies whether to generate an Ed25519 key pair.
|
|
|
|
// If false, an ECDSA key will be generated instead.
|
|
|
|
// Ed25519 is not as widely supported as ECDSA.
|
|
|
|
Ed25519 bool
|
2020-10-27 21:34:06 -06:00
|
|
|
}
|
|
|
|
|
2021-01-14 18:42:12 -07:00
|
|
|
// Create creates a new TLS certificate.
|
|
|
|
func Create(options CreateOptions) (tls.Certificate, error) {
|
2020-10-28 11:40:25 -06:00
|
|
|
crt, priv, err := newX509KeyPair(options)
|
2020-09-27 21:49:41 -06:00
|
|
|
if err != nil {
|
|
|
|
return tls.Certificate{}, err
|
|
|
|
}
|
2020-10-13 15:33:14 -06:00
|
|
|
var cert tls.Certificate
|
|
|
|
cert.Leaf = crt
|
|
|
|
cert.Certificate = append(cert.Certificate, crt.Raw)
|
|
|
|
cert.PrivateKey = priv
|
|
|
|
return cert, nil
|
2020-09-27 11:50:48 -06:00
|
|
|
}
|
|
|
|
|
2020-10-13 15:33:14 -06:00
|
|
|
// newX509KeyPair creates and returns a new certificate and private key.
|
2021-01-14 18:42:12 -07:00
|
|
|
func newX509KeyPair(options CreateOptions) (*x509.Certificate, crypto.PrivateKey, error) {
|
2020-11-03 17:43:04 -07:00
|
|
|
var pub crypto.PublicKey
|
|
|
|
var priv crypto.PrivateKey
|
2020-11-04 22:04:58 -07:00
|
|
|
if options.Ed25519 {
|
|
|
|
// Generate an Ed25519 private key
|
2020-11-03 17:43:04 -07:00
|
|
|
var err error
|
|
|
|
pub, priv, err = ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Generate an ECDSA private key
|
|
|
|
private, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
priv = private
|
|
|
|
pub = &private.PublicKey
|
2020-09-27 11:50:48 -06:00
|
|
|
}
|
|
|
|
|
2020-11-04 22:04:58 -07:00
|
|
|
// ECDSA and Ed25519 keys should have the DigitalSignature KeyUsage bits
|
2020-11-03 17:43:04 -07:00
|
|
|
// set in the x509.Certificate template
|
2020-09-27 11:50:48 -06:00
|
|
|
keyUsage := x509.KeyUsageDigitalSignature
|
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:33:14 -06:00
|
|
|
notBefore := time.Now()
|
2020-10-28 11:40:25 -06:00
|
|
|
notAfter := notBefore.Add(options.Duration)
|
2020-10-13 15:33:14 -06:00
|
|
|
|
2020-09-27 11:50:48 -06:00
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
KeyUsage: keyUsage,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
2020-10-28 11:40:25 -06:00
|
|
|
IPAddresses: options.IPAddresses,
|
|
|
|
DNSNames: options.DNSNames,
|
2020-11-02 21:11:46 -07:00
|
|
|
Subject: options.Subject,
|
2020-09-27 11:50:48 -06:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:43:04 -07:00
|
|
|
crt, err := x509.CreateCertificate(rand.Reader, &template, &template, pub, priv)
|
2020-09-27 11:50:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-10-13 15:33:14 -06:00
|
|
|
cert, err := x509.ParseCertificate(crt)
|
2020-09-27 11:50:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-10-13 15:33:14 -06:00
|
|
|
return cert, priv, nil
|
2020-09-27 11:50:48 -06:00
|
|
|
}
|
2020-10-31 14:33:56 -06:00
|
|
|
|
2021-01-14 18:42:12 -07:00
|
|
|
// Write writes the provided certificate and its private key
|
2020-10-31 14:33:56 -06:00
|
|
|
// to certPath and keyPath respectively.
|
2021-01-14 18:42:12 -07:00
|
|
|
func Write(cert tls.Certificate, certPath, keyPath string) error {
|
2020-10-31 14:33:56 -06:00
|
|
|
certOut, err := os.OpenFile(certPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer certOut.Close()
|
|
|
|
if err := pem.Encode(certOut, &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: cert.Leaf.Raw,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer keyOut.Close()
|
|
|
|
privBytes, err := x509.MarshalPKCS8PrivateKey(cert.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
|
|
|
|
}
|