-
Notifications
You must be signed in to change notification settings - Fork 6
/
x509.go
180 lines (151 loc) · 4.28 KB
/
x509.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
package cert
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math"
"math/big"
"time"
)
// KeyPair stores an x509 certificate and its ECDSA private key
type KeyPair struct {
Cert *x509.Certificate
Priv *ecdsa.PrivateKey
}
// ToPEM returns the PEM encoded cert pair
func (kp *KeyPair) ToPEM() (certPEM []byte, privPEM []byte, err error) {
// PEM encode private key
privDER, err := x509.MarshalECPrivateKey(kp.Priv)
if err != nil {
return
}
privBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: privDER,
}
privPEM = pem.EncodeToMemory(privBlock)
// PEM encode cert
certBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: kp.Cert.Raw,
}
certPEM = pem.EncodeToMemory(certBlock)
return
}
// GenerateCA generates a self-signed CA cert/key pair that expires in expiresIn days
func GenerateCA(notAfter time.Time, organization string) (*KeyPair, error) {
notBefore := time.Now()
if notAfter.Before(notBefore) {
return nil, fmt.Errorf("invalid notAfter: %s before %s", notAfter.String(), notBefore.String())
}
serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
caDetails := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
Organization: []string{organization},
},
NotBefore: notBefore,
NotAfter: notAfter,
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
publicKey := &privateKey.PublicKey
certRaw, err := x509.CreateCertificate(rand.Reader, caDetails, caDetails, publicKey, privateKey)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(certRaw)
if err != nil {
return nil, err
}
ca := &KeyPair{
Cert: cert,
Priv: privateKey,
}
return ca, nil
}
// CreateSignedServingPair creates a serving cert/key pair signed by the given ca
func CreateSignedServingPair(notAfter time.Time, organization string, ca *KeyPair, hosts []string) (*KeyPair, error) {
notBefore := time.Now()
if notAfter.Before(notBefore) {
return nil, fmt.Errorf("invalid notAfter: %s before %s", notAfter.String(), notBefore.String())
}
serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
certDetails := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
Organization: []string{organization},
},
NotBefore: notBefore,
NotAfter: notAfter,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
DNSNames: hosts,
}
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
publicKey := &privateKey.PublicKey
certRaw, err := x509.CreateCertificate(rand.Reader, certDetails, ca.Cert, publicKey, ca.Priv)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(certRaw)
if err != nil {
return nil, err
}
servingCert := &KeyPair{
Cert: cert,
Priv: privateKey,
}
return servingCert, nil
}
// PEMToCert converts the PEM block of the given byte array to an x509 certificate
func PEMToCert(certPEM []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(certPEM)
if block == nil {
return nil, fmt.Errorf("cert PEM empty")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
return cert, nil
}
// VerifyCert checks that the given cert is signed and trusted by the given CA
func VerifyCert(ca, cert *x509.Certificate, host string) error {
roots := x509.NewCertPool()
roots.AddCert(ca)
opts := x509.VerifyOptions{
DNSName: host,
Roots: roots,
}
if _, err := cert.Verify(opts); err != nil {
return err
}
return nil
}
// Active checks if the given cert is within its valid time window
func Active(cert *x509.Certificate) bool {
now := time.Now()
active := now.After(cert.NotBefore) && now.Before(cert.NotAfter)
return active
}