/
utils.go
167 lines (141 loc) · 4.28 KB
/
utils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package certutils
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"time"
)
const (
// PKCS1 certificate format
PKCS1 = iota
// PKCS8 certificate format
PKCS8
)
const (
// CACert indicates that the certificate should be a certificate authority.
CACert string = "ca"
// ServerCert indicates that the certificate should have the ExtKeyUsageServerAuth usage.
ServerCert string = "server"
// ClientCert indicates that the certificate should have the ExtKeyUsageClientAuth usage.
ClientCert string = "client"
// ServerClientCert indicates that the certificate should have both the ExtKeyUsageServerAuth and ExtKeyUsageClientAuth usage.
ServerClientCert string = "both"
)
type CertConfig struct {
Name string
CommonName string
Organization []string
DNSNames []string
IPAddresses []net.IP
CertType string
Validity *time.Duration
SigningCA *Certificate
PKCS int
}
type Certificate struct {
Name string
CA *Certificate
PrivateKey *rsa.PrivateKey
PrivateKeyPEM []byte
Certificate *x509.Certificate
CertificatePEM []byte
}
func (s *CertConfig) GenerateCertificate() (*Certificate, error) {
certificateObj := &Certificate{
Name: s.Name,
CA: s.SigningCA,
}
// If no cert type is given then we only return a certificate object that contains the CA.
if s.CertType != "" {
privateKey, err := generateRSAPrivateKey(2048)
if err != nil {
return nil, err
}
var (
certificate = s.generateCertificateTemplate()
certificateSigner = certificate
privateKeySigner = privateKey
)
if s.SigningCA != nil {
certificateSigner = s.SigningCA.Certificate
privateKeySigner = s.SigningCA.PrivateKey
}
certificatePEM, err := signCertificate(certificate, privateKey, certificateSigner, privateKeySigner)
if err != nil {
return nil, err
}
var pk []byte
if s.PKCS == PKCS1 {
pk = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
})
} else if s.PKCS == PKCS8 {
bytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, err
}
pk = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: bytes,
})
}
certificateObj.PrivateKey = privateKey
certificateObj.PrivateKeyPEM = pk
certificateObj.Certificate = certificate
certificateObj.CertificatePEM = certificatePEM
}
return certificateObj, nil
}
// generateRSAPrivateKey generates a RSA private for the given number of <bits>.
func generateRSAPrivateKey(bits int) (*rsa.PrivateKey, error) {
return rsa.GenerateKey(rand.Reader, bits)
}
func (s *CertConfig) generateCertificateTemplate() *x509.Certificate {
var (
serialNumber, _ = rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
isCA = s.CertType == CACert
template = &x509.Certificate{
BasicConstraintsValid: true,
IsCA: isCA,
SerialNumber: serialNumber,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(0, 0, 2),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
Subject: pkix.Name{
CommonName: s.CommonName,
Organization: s.Organization,
},
DNSNames: s.DNSNames,
IPAddresses: s.IPAddresses,
}
)
switch s.CertType {
case CACert:
template.KeyUsage |= x509.KeyUsageCertSign | x509.KeyUsageCRLSign
case ServerCert:
template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
case ClientCert:
template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
case ServerClientCert:
template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}
}
return template
}
func signCertificate(certificateTemplate *x509.Certificate, privateKey *rsa.PrivateKey, certificateTemplateSigner *x509.Certificate, privateKeySigner *rsa.PrivateKey) ([]byte, error) {
certificate, err := x509.CreateCertificate(rand.Reader, certificateTemplate, certificateTemplateSigner, &privateKey.PublicKey, privateKeySigner)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: certificate,
}), nil
}