devca.go90 lines, 2.8 KB
| 1 | // Package devca is a certificate authority for development and tests. It |
| 2 | // gives each of our services a certificate with the service's name in it. |
| 3 | // Its private key only ever lives in memory: once it's gone, nobody can |
| 4 | // issue another certificate that our services trust. |
| 5 | package devca |
| 6 | |
| 7 | import ( |
| 8 | "crypto/ecdsa" |
| 9 | "crypto/elliptic" |
| 10 | "crypto/rand" |
| 11 | "crypto/x509" |
| 12 | "crypto/x509/pkix" |
| 13 | "encoding/pem" |
| 14 | "errors" |
| 15 | "os" |
| 16 | "path/filepath" |
| 17 | "time" |
| 18 | ) |
| 19 | |
| 20 | // A CA issues certificates that expire when it does. |
| 21 | type CA struct { |
| 22 | cert *x509.Certificate |
| 23 | key *ecdsa.PrivateKey |
| 24 | } |
| 25 | |
| 26 | // New returns a CA that's valid from now, for validity. |
| 27 | func New(validity time.Duration) (*CA, error) { |
| 28 | key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | now := time.Now() |
| 33 | tmpl := &x509.Certificate{ |
| 34 | Subject: pkix.Name{CommonName: "payments development CA"}, |
| 35 | NotBefore: now.Add(-5 * time.Minute), // clocks drift |
| 36 | NotAfter: now.Add(validity), |
| 37 | IsCA: true, |
| 38 | BasicConstraintsValid: true, |
| 39 | MaxPathLenZero: true, // it signs services' certificates, never another CA's |
| 40 | KeyUsage: x509.KeyUsageCertSign, |
| 41 | } |
| 42 | der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, key.Public(), key) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | cert, err := x509.ParseCertificate(der) |
| 47 | return &CA{cert: cert, key: key}, err |
| 48 | } |
| 49 | |
| 50 | // Write gives the service called name a directory in dir, with the CA's |
| 51 | // certificate, ca.pem, and its own: cert.pem, and key.pem, which only its |
| 52 | // owner can read. Its name is its only name: the one servers prove, and |
| 53 | // clients are known by. |
| 54 | func (ca *CA) Write(dir, name string) error { |
| 55 | key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | tmpl := &x509.Certificate{ |
| 60 | Subject: pkix.Name{CommonName: name}, |
| 61 | DNSNames: []string{name}, |
| 62 | NotBefore: ca.cert.NotBefore, |
| 63 | NotAfter: ca.cert.NotAfter, |
| 64 | KeyUsage: x509.KeyUsageDigitalSignature, |
| 65 | // Every service is a server to some, and a client of others. |
| 66 | ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, |
| 67 | } |
| 68 | der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.cert, key.Public(), ca.key) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | pkcs8, err := x509.MarshalPKCS8PrivateKey(key) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | dir = filepath.Join(dir, name) |
| 77 | if err := os.MkdirAll(dir, 0o700); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | return errors.Join( |
| 81 | write(filepath.Join(dir, "ca.pem"), "CERTIFICATE", ca.cert.Raw, 0o644), |
| 82 | write(filepath.Join(dir, "cert.pem"), "CERTIFICATE", der, 0o644), |
| 83 | write(filepath.Join(dir, "key.pem"), "PRIVATE KEY", pkcs8, 0o600), |
| 84 | ) |
| 85 | } |
| 86 | |
| 87 | // write writes der to path, as a PEM block of type typ. |
| 88 | func write(path, typ string, der []byte, perm os.FileMode) error { |
| 89 | return os.WriteFile(path, pem.EncodeToMemory(&pem.Block{Type: typ, Bytes: der}), perm) |
| 90 | } |