| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 3 | |
| 4 | package pki |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "crypto/ed25519" |
| 9 | "crypto/rand" |
| 10 | "crypto/x509" |
| 11 | "fmt" |
| 12 | "math/big" |
| 13 | "time" |
| 14 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 15 | clientv3 "go.etcd.io/etcd/client/v3" |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
| Tim Windelschmidt | 7fd64f6 | 2025-06-29 02:32:31 +0200 | [diff] [blame] | 18 | var ( |
| 19 | // From RFC 5280 Section 4.1.2.5 |
| 20 | UnknownNotAfter = time.Unix(253402300799, 0) |
| 21 | ) |
| 22 | |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 23 | // Issuer is an entity that can issue certificates. This interface is |
| 24 | // implemented by SelfSigned, which is an issuer that emits self-signed |
| 25 | // certificates, and any other Certificate that has been created with CA(), |
| 26 | // which makes this Certificate act as a CA and issue (sign) ceritficates. |
| 27 | type Issuer interface { |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 28 | // CACertificate returns the DER-encoded x509 certificate of the CA that |
| 29 | // will sign certificates when Issue is called, or nil if this is |
| 30 | // self-signing issuer. |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 31 | CACertificate(ctx context.Context, kv clientv3.KV) ([]byte, error) |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 32 | // Issue will generate a certificate signed by the Issuer. The returned |
| 33 | // certificate is x509 DER-encoded. |
| 34 | Issue(ctx context.Context, req *Certificate, kv clientv3.KV) (cert []byte, err error) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 37 | // issueCertificate is a generic low level certificate-and-key issuance |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 38 | // function. If ca is null, the certificate will be self-signed. The returned |
| 39 | // certificate is DER-encoded |
| 40 | func issueCertificate(req *Certificate, ca *x509.Certificate, caKey ed25519.PrivateKey) (cert []byte, err error) { |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 41 | serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 127) |
| 42 | serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| 43 | if err != nil { |
| 44 | err = fmt.Errorf("failed to generate serial number: %w", err) |
| 45 | return |
| 46 | } |
| 47 | |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 48 | req.Template.SerialNumber = serialNumber |
| 49 | req.Template.NotBefore = time.Now() |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 50 | req.Template.NotAfter = UnknownNotAfter |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 51 | req.Template.BasicConstraintsValid = true |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 52 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 53 | // Set the AuthorityKeyID to the SKID of the signing certificate (or self, |
| 54 | // if self-signing). |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 55 | if ca != nil { |
| Lorenz Brun | 237cf40 | 2022-07-04 12:14:37 +0000 | [diff] [blame] | 56 | req.Template.AuthorityKeyId = ca.SubjectKeyId |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 57 | } else { |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 58 | req.Template.AuthorityKeyId = req.Template.SubjectKeyId |
| 59 | ca = &req.Template |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 62 | return x509.CreateCertificate(rand.Reader, &req.Template, ca, req.PublicKey, caKey) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | type selfSigned struct{} |
| 66 | |
| 67 | var ( |
| 68 | // SelfSigned is an Issuer that generates self-signed certificates. |
| 69 | SelfSigned = &selfSigned{} |
| 70 | ) |
| 71 | |
| 72 | // Issue will generate a key and certificate that is self-signed. |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 73 | func (s *selfSigned) Issue(ctx context.Context, req *Certificate, kv clientv3.KV) (cert []byte, err error) { |
| 74 | if err := req.ensureKey(ctx, kv); err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | if req.PrivateKey == nil { |
| 78 | return nil, fmt.Errorf("cannot issue self-signed certificate without a private key") |
| 79 | } |
| 80 | return issueCertificate(req, nil, req.PrivateKey) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // CACertificate returns nil for self-signed issuers. |
| 84 | func (s *selfSigned) CACertificate(ctx context.Context, kv clientv3.KV) ([]byte, error) { |
| 85 | return nil, nil |
| 86 | } |
| 87 | |
| 88 | // Issue will generate a key and certificate that is signed by this |
| 89 | // Certificate, if the Certificate is a CA. |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 90 | func (c *Certificate) Issue(ctx context.Context, req *Certificate, kv clientv3.KV) (cert []byte, err error) { |
| 91 | if err := c.ensureKey(ctx, kv); err != nil { |
| 92 | return nil, fmt.Errorf("could not ensure CA %q key exists: %w", c.Name, err) |
| 93 | } |
| 94 | if err := req.ensureKey(ctx, kv); err != nil { |
| 95 | return nil, fmt.Errorf("could not subject %q key exists: %w", req.Name, err) |
| 96 | } |
| 97 | if c.PrivateKey == nil { |
| 98 | return nil, fmt.Errorf("cannot use certificate without private key as CA") |
| 99 | } |
| 100 | |
| 101 | caCert, err := c.ensure(ctx, kv) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 102 | if err != nil { |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 103 | return nil, fmt.Errorf("could not ensure CA %q certificate exists: %w", c.Name, err) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ca, err := x509.ParseCertificate(caCert) |
| 107 | if err != nil { |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 108 | return nil, fmt.Errorf("could not parse CA certificate: %w", err) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 109 | } |
| 110 | // Ensure only one level of CAs exist, and that they are created explicitly. |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 111 | req.Template.IsCA = false |
| 112 | return issueCertificate(req, ca, c.PrivateKey) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // CACertificate returns the DER encoded x509 form of this Certificate that |
| 116 | // will be the used to issue child certificates. |
| 117 | func (c *Certificate) CACertificate(ctx context.Context, kv clientv3.KV) ([]byte, error) { |
| Serge Bazanski | 5253884 | 2021-08-11 16:22:41 +0200 | [diff] [blame] | 118 | return c.ensure(ctx, kv) |
| Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 119 | } |