|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package testca provides helpers to create a self-signed CA certificate, and the ability to generate |
| 18 | +// signed certificates from it. |
| 19 | +// PLEASE NOTE THIS IS NOT A PRODUCTION SAFE NOR VERIFIED WAY TO MANAGE CERTIFICATES FOR SERVERS. |
| 20 | +package testca |
| 21 | + |
| 22 | +import ( |
| 23 | + "crypto/rand" |
| 24 | + "crypto/rsa" |
| 25 | + "crypto/x509" |
| 26 | + "crypto/x509/pkix" |
| 27 | + "encoding/pem" |
| 28 | + "io" |
| 29 | + "math/big" |
| 30 | + "net" |
| 31 | + "time" |
| 32 | + |
| 33 | + "github.com/containerd/nerdctl/mod/tigron/internal/assertive" |
| 34 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 35 | + "github.com/containerd/nerdctl/mod/tigron/tig" |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + keyLength = 4096 |
| 40 | + caRoot = "ca" |
| 41 | + certsRoot = "certs" |
| 42 | + organization = "tigron volatile testing organization" |
| 43 | + lifetime = 24 * time.Hour |
| 44 | + serialSize = 60 |
| 45 | +) |
| 46 | + |
| 47 | +// NewX509 creates a new, self-signed, signing certificate under data.Temp()/ca |
| 48 | +// From that Cert as a CA, you can then generate signed certificates. |
| 49 | +// Note that the common name of the cert will be set to the test name. |
| 50 | +func NewX509(data test.Data, helpers test.Helpers) *Cert { |
| 51 | + template := &x509.Certificate{ |
| 52 | + Subject: pkix.Name{ |
| 53 | + Organization: []string{organization}, |
| 54 | + CommonName: helpers.T().Name(), |
| 55 | + }, |
| 56 | + NotBefore: time.Now(), |
| 57 | + NotAfter: time.Now().Add(lifetime), |
| 58 | + IsCA: true, |
| 59 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, |
| 60 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 61 | + BasicConstraintsValid: true, |
| 62 | + } |
| 63 | + |
| 64 | + return (&Cert{}).GenerateCustomX509(data, helpers, caRoot, template) |
| 65 | +} |
| 66 | + |
| 67 | +// Cert allows the consumer to retrieve the cert and key path, to be used by other processes, like servers for example. |
| 68 | +type Cert struct { |
| 69 | + KeyPath string |
| 70 | + CertPath string |
| 71 | + key *rsa.PrivateKey |
| 72 | + cert *x509.Certificate |
| 73 | +} |
| 74 | + |
| 75 | +// GenerateServerX509 produces a certificate usable by a server. |
| 76 | +// additional can be used to provide additional ips to be added to the certificate. |
| 77 | +func (ca *Cert) GenerateServerX509(data test.Data, helpers test.Helpers, host string, additional ...string) *Cert { |
| 78 | + template := &x509.Certificate{ |
| 79 | + Subject: pkix.Name{ |
| 80 | + Organization: []string{organization}, |
| 81 | + CommonName: host, |
| 82 | + }, |
| 83 | + NotBefore: time.Now(), |
| 84 | + NotAfter: time.Now().Add(lifetime), |
| 85 | + KeyUsage: x509.KeyUsageCRLSign, |
| 86 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 87 | + DNSNames: additional, |
| 88 | + } |
| 89 | + |
| 90 | + additional = append([]string{host}, additional...) |
| 91 | + for _, h := range additional { |
| 92 | + if ip := net.ParseIP(h); ip != nil { |
| 93 | + template.IPAddresses = append(template.IPAddresses, ip) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return ca.GenerateCustomX509(data, helpers, certsRoot, template) |
| 98 | +} |
| 99 | + |
| 100 | +// GenerateCustomX509 signs a random x509 certificate template. |
| 101 | +// Note that if SerialNumber is specified, it must be safe to use on the filesystem as this will be used in the name |
| 102 | +// of the certificate file. |
| 103 | +func (ca *Cert) GenerateCustomX509( |
| 104 | + data test.Data, |
| 105 | + helpers test.Helpers, |
| 106 | + underDirectory string, |
| 107 | + template *x509.Certificate, |
| 108 | +) *Cert { |
| 109 | + silentT := assertive.WithSilentSuccess(helpers.T()) |
| 110 | + key, certPath, keyPath := createCert(silentT, data, underDirectory, template, ca.cert, ca.key) |
| 111 | + |
| 112 | + return &Cert{ |
| 113 | + CertPath: certPath, |
| 114 | + KeyPath: keyPath, |
| 115 | + key: key, |
| 116 | + cert: template, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func createCert( |
| 121 | + testing tig.T, |
| 122 | + data test.Data, |
| 123 | + dir string, |
| 124 | + template, caCert *x509.Certificate, |
| 125 | + caKey *rsa.PrivateKey, |
| 126 | +) (key *rsa.PrivateKey, certPath, keyPath string) { |
| 127 | + if caCert == nil { |
| 128 | + caCert = template |
| 129 | + } |
| 130 | + |
| 131 | + if caKey == nil { |
| 132 | + caKey = key |
| 133 | + } |
| 134 | + |
| 135 | + key, err := rsa.GenerateKey(rand.Reader, keyLength) |
| 136 | + assertive.ErrorIsNil(testing, err, "key generation should succeed") |
| 137 | + |
| 138 | + signedCert, err := x509.CreateCertificate(rand.Reader, template, caCert, &key.PublicKey, caKey) |
| 139 | + assertive.ErrorIsNil(testing, err, "certificate creation should succeed") |
| 140 | + |
| 141 | + serial := template.SerialNumber |
| 142 | + if serial == nil { |
| 143 | + serial = serialNumber() |
| 144 | + } |
| 145 | + |
| 146 | + data.Temp().Dir(dir) |
| 147 | + certPath = data.Temp().Path(dir, serial.String()+".cert") |
| 148 | + keyPath = data.Temp().Path(dir, serial.String()+".key") |
| 149 | + |
| 150 | + data.Temp().SaveToWriter(func(writer io.Writer) error { |
| 151 | + return pem.Encode(writer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}) |
| 152 | + }, keyPath) |
| 153 | + |
| 154 | + data.Temp().SaveToWriter(func(writer io.Writer) error { |
| 155 | + return pem.Encode(writer, &pem.Block{Type: "CERTIFICATE", Bytes: signedCert}) |
| 156 | + }, keyPath) |
| 157 | + |
| 158 | + return key, certPath, keyPath |
| 159 | +} |
| 160 | + |
| 161 | +func serialNumber() *big.Int { |
| 162 | + serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), serialSize) |
| 163 | + |
| 164 | + serial, err := rand.Int(rand.Reader, serialNumberLimit) |
| 165 | + if err != nil { |
| 166 | + panic(err) |
| 167 | + } |
| 168 | + |
| 169 | + return serial |
| 170 | +} |
0 commit comments