This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
generated from knative-extensions/sample-controller
-
Notifications
You must be signed in to change notification settings - Fork 8
/
certs.go
150 lines (133 loc) · 4.72 KB
/
certs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Copyright 2022 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package resources
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"math/big"
"time"
"go.uber.org/zap"
"knative.dev/pkg/logging"
)
// The logic below here is adapted from:
// https://github.com/knative/pkg/blob/main/webhook/certificates/resources/certs.go
func createCertTemplate(commonName string, notAfter time.Time) (*x509.Certificate, error) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, errors.New("failed to generate serial number: " + err.Error())
}
tmpl := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Chainguard, Inc."},
CommonName: commonName,
},
SignatureAlgorithm: x509.ECDSAWithSHA256,
NotBefore: time.Now(),
NotAfter: notAfter,
BasicConstraintsValid: true,
DNSNames: []string{commonName},
}
return &tmpl, nil
}
// Create cert template suitable for CA and hence signing
func createCACertTemplate(commonName string, notAfter time.Time) (*x509.Certificate, error) {
rootCert, err := createCertTemplate(commonName, notAfter)
if err != nil {
return nil, err
}
// Make it into a CA cert and change it so we can use it to sign certs
rootCert.IsCA = true
rootCert.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature
rootCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}
return rootCert, nil
}
// Create cert template that we can use on the server for TLS
func createServerCertTemplate(commonName string, notAfter time.Time) (*x509.Certificate, error) {
serverCert, err := createCertTemplate(commonName, notAfter)
if err != nil {
return nil, err
}
serverCert.KeyUsage = x509.KeyUsageDigitalSignature
serverCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
return serverCert, err
}
// Actually sign the cert and return things in a form that we can use later on
func createCert(template, parent *x509.Certificate, pub, parentPriv interface{}) (
cert *x509.Certificate, certPEM []byte, err error) {
certDER, err := x509.CreateCertificate(rand.Reader, template, parent, pub, parentPriv)
if err != nil {
return
}
cert, err = x509.ParseCertificate(certDER)
if err != nil {
return
}
b := pem.Block{Type: "CERTIFICATE", Bytes: certDER}
certPEM = pem.EncodeToMemory(&b)
return
}
func createCA(ctx context.Context, commonName string, notAfter time.Time) (*ecdsa.PrivateKey, *x509.Certificate, []byte, error) {
logger := logging.FromContext(ctx)
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
logger.Errorw("error generating random key", zap.Error(err))
return nil, nil, nil, err
}
publicKey := privateKey.Public()
rootCertTmpl, err := createCACertTemplate(commonName, notAfter)
if err != nil {
logger.Errorw("error generating CA cert", zap.Error(err))
return nil, nil, nil, err
}
rootCert, rootCertPEM, err := createCert(rootCertTmpl, rootCertTmpl, publicKey, privateKey)
if err != nil {
logger.Errorw("error signing the CA cert", zap.Error(err))
return nil, nil, nil, err
}
return privateKey, rootCert, rootCertPEM, nil
}
func CreateCerts(ctx context.Context, commonName string, notAfter time.Time) (serverKey, serverCert, caCert []byte, err error) {
logger := logging.FromContext(ctx)
// First create a CA certificate and private key
caKey, caCertificate, caCertificatePEM, err := createCA(ctx, commonName, notAfter)
if err != nil {
return nil, nil, nil, err
}
// Then create the private key for the serving cert
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
logger.Errorw("error generating random key", zap.Error(err))
return nil, nil, nil, err
}
publicKey := privateKey.Public()
servCertTemplate, err := createServerCertTemplate(commonName, notAfter)
if err != nil {
logger.Errorw("failed to create the server certificate template", zap.Error(err))
return nil, nil, nil, err
}
// create a certificate which wraps the server's public key, sign it with the CA private key
_, servCertPEM, err := createCert(servCertTemplate, caCertificate, publicKey, caKey)
if err != nil {
logger.Errorw("error signing server certificate template", zap.Error(err))
return nil, nil, nil, err
}
privKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
logger.Errorw("error marshaling private key", zap.Error(err))
return nil, nil, nil, err
}
servKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY", Bytes: privKeyBytes,
})
return servKeyPEM, servCertPEM, caCertificatePEM, nil
}