| 9 | "encoding/pem" |
| 10 | "errors" |
| 11 | "fmt" |
| 12 | "math" |
| 13 | "math/big" |
| 14 | "net" |
| 15 | "time" |
| 16 |
| 17 | "k8s.io/client-go/util/cert" |
| 18 | "k8s.io/client-go/util/keyutil" |
| 19 | ) |
| 20 |
| 21 | type SelfSignedCertGenerator struct { |
| 22 | caKey []byte |
| 23 | caCert []byte |
| 24 | } |
| 25 |
| 26 | var _ Generator = &SelfSignedCertGenerator{} |
| 27 |
| 28 | // SetCA sets the PEM-encoded CA private key and CA cert for signing the generated serving cert. |
| 29 | func (cp *SelfSignedCertGenerator) SetCA(caKey, caCert []byte) { |
| 30 | cp.caKey = caKey |
| 31 | cp.caCert = caCert |
| 32 | } |
| 33 |
| 34 | // Generate creates and returns a CA certificate, certificate and |
| 35 | // key for the server or client. Key and Cert are used by the server or client |
| 36 | // to establish trust for others, CA certificate is used by the |
| 37 | // client or server to verify the other's authentication chain. |
| 38 | // The cert will be valid for 365 days. |
| 39 | func (cp *SelfSignedCertGenerator) Generate(commonName string) (*Artifacts, error) { |
| 40 | var signingKey *rsa.PrivateKey |
| 41 | var signingCert *x509.Certificate |
| 42 | var valid bool |
| 43 | var err error |
| 44 |
| 45 | valid, signingKey, signingCert = cp.validCACert() |
| 46 | if !valid { |
| 47 | signingKey, err = NewPrivateKey() |
| 48 | if err != nil { |
| 49 | return nil, fmt.Errorf("failed to create the CA private key: %v", err) |
| 50 | } |
| 51 | signingCert, err = cert.NewSelfSignedCACert(cert.Config{CommonName: commonName}, signingKey) |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("failed to create the CA cert: %v", err) |
| 54 | } |
| 55 | } |
| 56 |
| 57 | hostIP := net.ParseIP(commonName) |
| 58 | var altIPs []net.IP |
| 59 | DNSNames := []string{"localhost"} |
| 60 | if hostIP.To4() != nil { |
| 61 | altIPs = append(altIPs, hostIP.To4()) |
| 62 | } else { |
| 63 | DNSNames = append(DNSNames, commonName) |
| 64 | } |
| 65 |
| 66 | key, err := NewPrivateKey() |
| 67 | if err != nil { |
| 68 | return nil, fmt.Errorf("failed to create the private key: %v", err) |
| 69 | } |
| 70 | signedCert, err := NewSignedCert( |
| 71 | cert.Config{ |
| 72 | CommonName: commonName, |
| 73 | Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, |
| 74 | AltNames: cert.AltNames{IPs: altIPs, DNSNames: DNSNames}, |
| 75 | }, |
| 76 | key, signingCert, signingKey, |
| 77 | ) |
| 78 | if err != nil { |
| 79 | return nil, fmt.Errorf("failed to create the cert: %v", err) |
| 80 | } |
| 81 | return &Artifacts{ |
| 82 | Key: EncodePrivateKeyPEM(key), |
| 83 | Cert: EncodeCertPEM(signedCert), |
| 84 | CAKey: EncodePrivateKeyPEM(signingKey), |
| 85 | CACert: EncodeCertPEM(signingCert), |
| 86 | }, nil |
| 87 | } |
| 88 |
| 89 | func (cp *SelfSignedCertGenerator) validCACert() (bool, *rsa.PrivateKey, *x509.Certificate) { |
| 90 | if !ValidCACert(cp.caKey, cp.caCert, cp.caCert, "", |
| 91 | time.Now().AddDate(1, 0, 0)) { |
| 92 | return false, nil, nil |
| 93 | } |
| 94 |
| 95 | var ok bool |
| 96 | key, err := keyutil.ParsePrivateKeyPEM(cp.caKey) |
| 97 | if err != nil { |
| 98 | return false, nil, nil |
| 99 | } |
| 100 | privateKey, ok := key.(*rsa.PrivateKey) |
| 101 | if !ok { |
| 102 | return false, nil, nil |
| 103 | } |
| 104 |
| 105 | certs, err := cert.ParseCertsPEM(cp.caCert) |
| 106 | if err != nil { |
| 107 | return false, nil, nil |
| 108 | } |
| 109 | if len(certs) != 1 { |
| 110 | return false, nil, nil |
| 111 | } |
| 112 | return true, privateKey, certs[0] |
| 113 | } |
| 114 |
| 115 | // NewPrivateKey creates an RSA private key |
| 116 | func NewPrivateKey() (*rsa.PrivateKey, error) { |
| 117 | return rsa.GenerateKey(rand.Reader, 2048) |
| 118 | } |
| 119 |
| 120 | // NewSignedCert creates a signed certificate using the given CA certificate and key |
| 121 | func NewSignedCert(cfg cert.Config, key crypto.Signer, caCert *x509.Certificate, caKey crypto.Signer) (*x509.Certificate, error) { |
| 122 | serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64)) |
| 123 | if err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | if len(cfg.CommonName) == 0 { |
| 127 | return nil, errors.New("must specify a CommonName") |
| 128 | } |
| 129 | if len(cfg.Usages) == 0 { |
| 130 | return nil, errors.New("must specify at least one ExtKeyUsage") |
| 131 | } |
| 132 |
| 133 | certTmpl := x509.Certificate{ |
| 134 | Subject: pkix.Name{ |
| 135 | CommonName: cfg.CommonName, |
| 136 | Organization: cfg.Organization, |
| 137 | }, |
| 138 | DNSNames: cfg.AltNames.DNSNames, |
| 139 | IPAddresses: cfg.AltNames.IPs, |
| 140 | SerialNumber: serial, |
| 141 | NotBefore: caCert.NotBefore, |
| 142 | NotAfter: time.Now().Add(time.Hour * 24 * 365 * 10).UTC(), |
| 143 | KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| 144 | ExtKeyUsage: cfg.Usages, |
| 145 | } |
| 146 | certDERBytes, err := x509.CreateCertificate(rand.Reader, &certTmpl, caCert, key.Public(), caKey) |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | return x509.ParseCertificate(certDERBytes) |
| 151 | } |
| 152 |
| 153 | // EncodePrivateKeyPEM returns PEM-encoded private key data |
| 154 | func EncodePrivateKeyPEM(key *rsa.PrivateKey) []byte { |
| 155 | block := pem.Block{ |
| 156 | Type: keyutil.RSAPrivateKeyBlockType, |
| 157 | Bytes: x509.MarshalPKCS1PrivateKey(key), |
| 158 | } |
| 159 | return pem.EncodeToMemory(&block) |
| 160 | } |
| 161 |
| 162 | // EncodeCertPEM returns PEM-encoded certificate data |
| 163 | func EncodeCertPEM(ct *x509.Certificate) []byte { |
| 164 | block := pem.Block{ |
| 165 | Type: cert.CertificateBlockType, |
| 166 | Bytes: ct.Raw, |
| 167 | } |
| 168 | return pem.EncodeToMemory(&block) |
| 169 | } |