This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
/
keys.go
158 lines (142 loc) · 3.88 KB
/
keys.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
package keys
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
cryptorand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"math"
"math/big"
"time"
certutil "k8s.io/client-go/util/cert"
"k8s.io/client-go/util/keyutil"
)
const duration365d = time.Hour * 24 * 365
// SignerWithPEM is a signer implementation that has its PEM encoded representation attached
type SignerWithPEM interface {
crypto.Signer
PEM() []byte
}
type signerWithPEM struct {
crypto.Signer
pemData []byte
}
func (s signerWithPEM) PEM() []byte {
return s.pemData
}
// CA is a certificate authority usable to generated signed certificates
type CA interface {
PrivateKey() SignerWithPEM
Cert() *x509.Certificate
NewSignedCert(cfg certutil.Config, publicKey crypto.PublicKey) (*x509.Certificate, error)
}
type authority struct {
pvk SignerWithPEM
cert *x509.Certificate
}
func (ca *authority) PrivateKey() SignerWithPEM {
return ca.pvk
}
func (ca *authority) Cert() *x509.Certificate {
return ca.cert
}
func (ca *authority) NewSignedCert(cfg certutil.Config, publicKey crypto.PublicKey) (*x509.Certificate, error) {
serial, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
if len(cfg.CommonName) == 0 {
return nil, errors.New("must specify a CommonName")
}
if len(cfg.Usages) == 0 {
return nil, errors.New("must specify at least one ExtKeyUsage")
}
certTmpl := x509.Certificate{
Subject: pkix.Name{
CommonName: cfg.CommonName,
Organization: cfg.Organization,
},
DNSNames: cfg.AltNames.DNSNames,
IPAddresses: cfg.AltNames.IPs,
SerialNumber: serial,
NotBefore: ca.cert.NotBefore,
NotAfter: time.Now().Add(time.Hour * 24 * 365).UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: cfg.Usages,
}
certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &certTmpl, ca.cert, publicKey, ca.pvk)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(certDERBytes)
if err != nil {
return nil, err
}
return cert, nil
}
// NewSelfSignedCA creates a CA and return it with its private key
func NewSelfSignedCA(commonName string, organization []string) (CA, error) {
pvk, err := ecdsa.GenerateKey(elliptic.P256(), cryptorand.Reader)
if err != nil {
return nil, err
}
derBytes, err := x509.MarshalECPrivateKey(pvk)
if err != nil {
return nil, err
}
keyPemBlock := &pem.Block{
Type: keyutil.ECPrivateKeyBlockType,
Bytes: derBytes,
}
keyData := pem.EncodeToMemory(keyPemBlock)
key := signerWithPEM{Signer: pvk, pemData: keyData}
now := time.Now()
tmpl := x509.Certificate{
SerialNumber: new(big.Int).SetInt64(0),
Subject: pkix.Name{
CommonName: commonName,
Organization: organization,
},
NotBefore: now.UTC(),
NotAfter: now.Add(duration365d * 10).UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
}
certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &tmpl, &tmpl, key.Public(), key)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(certDERBytes)
if err != nil {
return nil, err
}
return &authority{
cert: cert,
pvk: key,
}, nil
}
// NewRSASigner generates a private key suitable for a TLS cert (client or server)
func NewRSASigner() (SignerWithPEM, error) {
key, err := rsa.GenerateKey(cryptorand.Reader, 2048)
if err != nil {
return nil, err
}
pemData, err := keyutil.MarshalPrivateKeyToPEM(key)
if err != nil {
return nil, err
}
return signerWithPEM{Signer: key, pemData: pemData}, nil
}
// EncodeCertPEM embed a certificate in a PEM block
func EncodeCertPEM(cert *x509.Certificate) []byte {
block := pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
}
return pem.EncodeToMemory(&block)
}