-
Notifications
You must be signed in to change notification settings - Fork 77
/
pki.go
261 lines (224 loc) · 7.43 KB
/
pki.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Package pki contains bits and pieces to work with OpenVPN PKI related operations.
package pki
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
//"github.com/Sirupsen/logrus"
"bytes"
"encoding/pem"
"fmt"
"math/big"
"time"
)
const (
_CrtExpireYears = 10
_CrtKeyLength = 2024
)
// CertHolder encapsulates a public certificate and the corresponding private key.
type CertHolder struct {
Cert string // PEM Encoded Certificate
Key string // PEM Encoded Private Key
}
// CA is a special type of CertHolder that also has a CSR in it.
type CA struct {
CertHolder
CSR string
}
// NewCA returns a newly generated CA.
//
// This will generate a public/private RSA keypair and a authority certificate signed by itself.
func NewCA() (*CA, error) {
type basicConstraints struct {
IsCA bool `asn1:"optional"`
MaxPathLen int `asn1:"optional,default:-1"`
}
key, err := rsa.GenerateKey(rand.Reader, _CrtKeyLength)
if err != nil {
return nil, fmt.Errorf("private key cannot be created: %s", err)
}
val, err := asn1.Marshal(basicConstraints{true, 0})
if err != nil {
return nil, fmt.Errorf("can not marshal basic constraints: %s", err)
}
names := pkix.Name{CommonName: "CA"}
var csrTemplate = x509.CertificateRequest{
Subject: names,
SignatureAlgorithm: x509.SHA512WithRSA,
ExtraExtensions: []pkix.Extension{
{
Id: asn1.ObjectIdentifier{2, 5, 29, 19},
Value: val,
Critical: true,
},
},
}
csrCertificate, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, key)
if err != nil {
return nil, fmt.Errorf("can not create certificate request: %s", err)
}
csr := pem.EncodeToMemory(&pem.Block{
Type: PEMCSRBlockType, Bytes: csrCertificate,
})
// Serial number
serial, err := rand.Int(rand.Reader, (&big.Int{}).Exp(big.NewInt(2), big.NewInt(159), nil))
if err != nil {
return nil, err
}
now := time.Now()
// Create the request template
template := x509.Certificate{
SerialNumber: serial,
Subject: names,
NotBefore: now.Add(-10 * time.Minute).UTC(),
NotAfter: now.Add(time.Duration(24*365*_CrtExpireYears) * time.Hour).UTC(),
BasicConstraintsValid: true,
IsCA: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
//ExtKeyUsage: []x509.ExtKeyUsage{x509.KeyUsageCertSign, x509.ExtKeyUsageClientAuth},
}
// Sign the certificate authority
certificate, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
return nil, fmt.Errorf("failed to generate certificate error: %s", err)
}
var request bytes.Buffer
var privateKey bytes.Buffer
if err := pem.Encode(&request, &pem.Block{Type: PEMCertificateBlockType, Bytes: certificate}); err != nil {
return nil, err
}
if err := pem.Encode(&privateKey, &pem.Block{Type: PEMRSAPrivateKeyBlockType, Bytes: x509.MarshalPKCS1PrivateKey(key)}); err != nil {
return nil, err
}
return &CA{
CertHolder: CertHolder{
Key: privateKey.String(),
Cert: request.String(),
},
CSR: string(csr),
}, nil
}
// NewServerCertHolder generates a RSA key-pair and a x509 certificate signed by the CA for the server.
func NewServerCertHolder(ca *CA) (*CertHolder, error) {
return newCert(ca, true, "localhost")
}
// NewClientCertHolder generates a RSA key-pair and a x509 certificate signed by the CA for the client.
func NewClientCertHolder(ca *CA, username string) (*CertHolder, error) {
return newCert(ca, false, username)
}
// newCert generates a RSA key-pair and a x509 certificate signed by the CA.
func newCert(ca *CA, server bool, cn string) (*CertHolder, error) {
// Get CA private key
block, _ := pem.Decode([]byte(ca.Key))
if block == nil {
return nil, fmt.Errorf("failed to parse ca private key")
}
caKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse ca private key: %s", err)
}
caCert, err := ReadCertFromPEM(ca.Cert)
if err != nil {
return nil, fmt.Errorf("failed to parse ca cert: %v", err)
}
// Create new cert's key
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("private key cannot be created: %s", err)
}
serial, err := rand.Int(rand.Reader, (&big.Int{}).Exp(big.NewInt(2), big.NewInt(159), nil))
if err != nil {
return nil, err
}
val, err := asn1.Marshal(asn1.BitString{Bytes: []byte{0x80}, BitLength: 2}) // setting nsCertType to Client Type
if err != nil {
return nil, fmt.Errorf("can not marshal nsCertType: %v", err)
}
now := time.Now()
tml := x509.Certificate{
NotBefore: now.Add(-10 * time.Minute).UTC(),
NotAfter: now.Add(time.Duration(24*365*_CrtExpireYears) * time.Hour).UTC(),
SerialNumber: serial,
Subject: pkix.Name{
CommonName: cn,
Organization: []string{"OVPM"},
},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyAgreement,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
ExtraExtensions: []pkix.Extension{
{
Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 1, 1},
Value: val,
},
},
}
if server {
tml.KeyUsage = x509.KeyUsageDigitalSignature | x509.KeyUsageKeyAgreement | x509.KeyUsageKeyEncipherment
tml.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
val, err := asn1.Marshal(asn1.BitString{Bytes: []byte{0x40}, BitLength: 2}) // setting nsCertType to Server Type
if err != nil {
return nil, fmt.Errorf("can not marshal nsCertType: %v", err)
}
tml.ExtraExtensions[0].Id = asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 1, 1}
tml.ExtraExtensions[0].Value = val
}
// Sign with CA's private key
cert, err := x509.CreateCertificate(rand.Reader, &tml, caCert, &key.PublicKey, caKey)
if err != nil {
return nil, fmt.Errorf("certificate cannot be created: %s", err)
}
priKeyPem := pem.EncodeToMemory(&pem.Block{
Type: PEMRSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
certPem := pem.EncodeToMemory(&pem.Block{
Type: PEMCertificateBlockType,
Bytes: cert,
})
return &CertHolder{
Key: string(priKeyPem[:]),
Cert: string(certPem[:]),
}, nil
}
// NewCRL takes in a list of certificate serial numbers to-be-revoked and a CA then makes a PEM encoded CRL and returns it as a string.
func NewCRL(ca *CA, serials ...*big.Int) (string, error) {
caCrt, err := ReadCertFromPEM(ca.Cert)
if err != nil {
return "", err
}
block, _ := pem.Decode([]byte(ca.Key))
if block == nil {
return "", fmt.Errorf("failed to parse ca private key")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", fmt.Errorf("failed to parse ca private key: %s", err)
}
var revokedCertList []pkix.RevokedCertificate
for _, serial := range serials {
revokedCert := pkix.RevokedCertificate{
SerialNumber: serial,
RevocationTime: time.Now().UTC(),
}
revokedCertList = append(revokedCertList, revokedCert)
}
crl, err := caCrt.CreateCRL(rand.Reader, priv, revokedCertList, time.Now().UTC(), time.Now().Add(365*24*60*time.Minute).UTC())
if err != nil {
return "", err
}
crlPem := pem.EncodeToMemory(&pem.Block{
Type: PEMx509CRLBlockType,
Bytes: crl,
})
return string(crlPem[:]), nil
}
// ReadCertFromPEM decodes a PEM encoded string into a x509.Certificate.
func ReadCertFromPEM(s string) (*x509.Certificate, error) {
block, _ := pem.Decode([]byte(s))
var cert *x509.Certificate
cert, _ = x509.ParseCertificate(block.Bytes)
return cert, nil
}