forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
192 lines (170 loc) · 5.54 KB
/
tls.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package tls
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"math"
"math/big"
"net"
"time"
"github.com/pkg/errors"
)
const (
keySize = 2048
// ValidityOneDay sets the validity of a cert to 24 hours.
ValidityOneDay = time.Hour * 24
// ValidityOneYear sets the validity of a cert to 1 year.
ValidityOneYear = ValidityOneDay * 365
// ValidityTenYears sets the validity of a cert to 10 years.
ValidityTenYears = ValidityOneYear * 10
)
// CertCfg contains all needed fields to configure a new certificate
type CertCfg struct {
DNSNames []string
ExtKeyUsages []x509.ExtKeyUsage
IPAddresses []net.IP
KeyUsages x509.KeyUsage
Subject pkix.Name
Validity time.Duration
IsCA bool
}
// rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key.
type rsaPublicKey struct {
N *big.Int
E int
}
// PrivateKey generates an RSA Private key and returns the value
func PrivateKey() (*rsa.PrivateKey, error) {
rsaKey, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return nil, errors.Wrap(err, "error generating RSA private key")
}
return rsaKey, nil
}
// SelfSignedCertificate creates a self signed certificate
func SelfSignedCertificate(cfg *CertCfg, key *rsa.PrivateKey) (*x509.Certificate, error) {
serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
cert := x509.Certificate{
BasicConstraintsValid: true,
IsCA: cfg.IsCA,
KeyUsage: cfg.KeyUsages,
NotAfter: time.Now().Add(cfg.Validity),
NotBefore: time.Now(),
SerialNumber: serial,
Subject: cfg.Subject,
}
// verifies that the CN and/or OU for the cert is set
if len(cfg.Subject.CommonName) == 0 || len(cfg.Subject.OrganizationalUnit) == 0 {
return nil, errors.Errorf("certification's subject is not set, or invalid")
}
pub := key.Public()
cert.SubjectKeyId, err = generateSubjectKeyID(pub)
if err != nil {
return nil, errors.Wrap(err, "failed to set subject key identifier")
}
certBytes, err := x509.CreateCertificate(rand.Reader, &cert, &cert, key.Public(), key)
if err != nil {
return nil, errors.Wrap(err, "failed to create certificate")
}
return x509.ParseCertificate(certBytes)
}
// SignedCertificate creates a new X.509 certificate based on a template.
func SignedCertificate(
cfg *CertCfg,
csr *x509.CertificateRequest,
key *rsa.PrivateKey,
caCert *x509.Certificate,
caKey *rsa.PrivateKey,
) (*x509.Certificate, error) {
serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
certTmpl := x509.Certificate{
DNSNames: csr.DNSNames,
ExtKeyUsage: cfg.ExtKeyUsages,
IPAddresses: csr.IPAddresses,
KeyUsage: cfg.KeyUsages,
NotAfter: time.Now().Add(cfg.Validity),
NotBefore: caCert.NotBefore,
SerialNumber: serial,
Subject: csr.Subject,
IsCA: cfg.IsCA,
Version: 3,
BasicConstraintsValid: true,
}
pub := caCert.PublicKey.(*rsa.PublicKey)
certTmpl.SubjectKeyId, err = generateSubjectKeyID(pub)
if err != nil {
return nil, errors.Wrap(err, "failed to set subject key identifier")
}
certBytes, err := x509.CreateCertificate(rand.Reader, &certTmpl, caCert, key.Public(), caKey)
if err != nil {
return nil, errors.Wrap(err, "failed to create x509 certificate")
}
return x509.ParseCertificate(certBytes)
}
// generateSubjectKeyID generates a SHA-1 hash of the subject public key.
func generateSubjectKeyID(pub crypto.PublicKey) ([]byte, error) {
var publicKeyBytes []byte
var err error
switch pub := pub.(type) {
case *rsa.PublicKey:
publicKeyBytes, err = asn1.Marshal(rsaPublicKey{N: pub.N, E: pub.E})
if err != nil {
return nil, errors.Wrap(err, "failed to Marshal ans1 public key")
}
case *ecdsa.PublicKey:
publicKeyBytes = elliptic.Marshal(pub.Curve, pub.X, pub.Y)
default:
return nil, errors.New("only RSA and ECDSA public keys supported")
}
hash := sha1.Sum(publicKeyBytes)
return hash[:], nil
}
// GenerateSignedCertificate generate a key and cert defined by CertCfg and signed by CA.
func GenerateSignedCertificate(caKey *rsa.PrivateKey, caCert *x509.Certificate,
cfg *CertCfg) (*rsa.PrivateKey, *x509.Certificate, error) {
// create a private key
key, err := PrivateKey()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to generate private key")
}
// create a CSR
csrTmpl := x509.CertificateRequest{Subject: cfg.Subject, DNSNames: cfg.DNSNames, IPAddresses: cfg.IPAddresses}
csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &csrTmpl, key)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create certificate request")
}
csr, err := x509.ParseCertificateRequest(csrBytes)
if err != nil {
return nil, nil, errors.Wrap(err, "error parsing x509 certificate request")
}
// create a cert
cert, err := SignedCertificate(cfg, csr, key, caCert, caKey)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create a signed certificate")
}
return key, cert, nil
}
// GenerateSelfSignedCertificate generates a key/cert pair defined by CertCfg.
func GenerateSelfSignedCertificate(cfg *CertCfg) (*rsa.PrivateKey, *x509.Certificate, error) {
key, err := PrivateKey()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to generate private key")
}
crt, err := SelfSignedCertificate(cfg, key)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create self-signed certificate")
}
return key, crt, nil
}