-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca.go
222 lines (189 loc) · 5 KB
/
ca.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package certs
import (
"crypto"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"os"
"time"
"github.com/taybart/certs/scheme"
"github.com/taybart/log"
)
// CA Certificate Authority
type CA struct {
Cert *x509.Certificate
sk crypto.PrivateKey
sch scheme.Scheme
}
// BootstrapNetwork start a new Certificate authority
func GenerateCA(sch string) (ca CA, err error) {
s, err := scheme.NewScheme(sch)
if err != nil {
return
}
sk, pk, err := s.GenerateKeys()
if err != nil {
return
}
out, err := os.Create(config.CA.Key)
if err != nil {
return
}
skPem, err := s.PrivateKeyToPem()
if err != nil {
return
}
err = pem.Encode(out, skPem)
if err != nil {
return
}
err = out.Close()
if err != nil {
return
}
_, csr, err := s.GenerateDefaultCSR(config.CA.Name)
if err != nil {
return
}
sub := config.DefaultSubject.ToPKIXName()
sub.CommonName = config.CA.Name
cert := &x509.Certificate{
SignatureAlgorithm: csr.SignatureAlgorithm,
PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
PublicKey: csr.PublicKey,
SerialNumber: big.NewInt(1),
Subject: sub,
DNSNames: []string{config.CA.Name},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
}
certbytes, err := x509.CreateCertificate(rand.Reader, cert, cert, pk, sk)
if err != nil {
return
}
out, err = os.Create(config.CA.Crt)
if err != nil {
return
}
err = pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: certbytes})
if err != nil {
return
}
err = out.Close()
if err != nil {
return
}
cert, err = x509.ParseCertificate(certbytes)
if err != nil {
return
}
ca = CA{Cert: cert, sk: sk, sch: s}
return
}
// LoadCA
func LoadCA() (ca CA, err error) {
keys, err := openPEM(config.CA.Key)
if err != nil {
return
}
keybytes := keys[0].Bytes
if x509.IsEncryptedPEMBlock(keys[0]) {
keybytes, err = x509.DecryptPEMBlock(keys[0], config.GetCAPassword())
if err != nil {
fmt.Println(log.Red, "✗", log.Rtd)
return
}
fmt.Println(log.Green, "✓", log.Rtd)
}
sk, err := x509.ParsePKCS8PrivateKey(keybytes)
if err != nil {
return
}
certblocks, err := openPEM(config.CA.Crt)
if err != nil {
return
}
cert, err := x509.ParseCertificate(certblocks[0].Bytes)
if err != nil {
return
}
sch, err := scheme.SchemeFromKey(sk, cert.PublicKey)
if err != nil {
return
}
ca = CA{Cert: cert, sk: sk, sch: sch}
return
}
// SignRequest signs x509 certificate request
func (ca *CA) SignRequest(asn1Data []byte) (cert []byte, err error) {
csr, err := x509.ParseCertificateRequest(asn1Data)
if err != nil {
return
}
if err = csr.CheckSignature(); err != nil {
return
}
// Create template for certificate creation, uses properties from the request and root certificate.
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return
}
// Client Template
template := &x509.Certificate{
SignatureAlgorithm: ca.sch.GetSignatureAlgorithm(),
PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
PublicKey: csr.PublicKey,
SerialNumber: serialNumber,
Issuer: ca.Cert.Subject,
Subject: csr.Subject,
DNSNames: csr.DNSNames,
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 365),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCRLSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
}
cert, err = x509.CreateCertificate(rand.Reader, template, ca.Cert, template.PublicKey, ca.sk)
return
}
// SignCA.Request signs x509 certificate request
func (ca *CA) SignCARequest(asn1Data []byte) (cert []byte, err error) {
csr, err := x509.ParseCertificateRequest(asn1Data)
if err != nil {
return
}
if err = csr.CheckSignature(); err != nil {
return
}
// Create template for certificate creation, uses properties from the request and root certificate.
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return
}
// Client Template
template := &x509.Certificate{
Signature: csr.Signature,
SignatureAlgorithm: csr.SignatureAlgorithm,
PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
PublicKey: csr.PublicKey,
SerialNumber: serialNumber,
Issuer: ca.Cert.Subject,
Subject: csr.Subject,
DNSNames: csr.DNSNames,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(3, 0, 0),
IsCA: true,
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageOCSPSigning},
}
cert, err = x509.CreateCertificate(rand.Reader, template, ca.Cert, template.PublicKey, ca.sk)
return
}