forked from micro/go-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pki.go
164 lines (154 loc) · 4.82 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
// Package pki provides PKI all the PKI functions necessary to run micro over an untrusted network
// including a CA
package pki
import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"github.com/pkg/errors"
)
// GenerateKey returns an ed25519 key
func GenerateKey() (ed25519.PublicKey, ed25519.PrivateKey, error) {
return ed25519.GenerateKey(rand.Reader)
}
// CA generates a self signed CA and returns cert, key in PEM format
func CA(opts ...CertOption) ([]byte, []byte, error) {
opts = append(opts, IsCA())
options := CertOptions{}
for _, o := range opts {
o(&options)
}
template := &x509.Certificate{
SignatureAlgorithm: x509.PureEd25519,
Subject: options.Subject,
DNSNames: options.DNSNames,
IPAddresses: options.IPAddresses,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
NotBefore: options.NotBefore,
NotAfter: options.NotAfter,
SerialNumber: options.SerialNumber,
BasicConstraintsValid: true,
}
if options.IsCA {
template.IsCA = true
template.KeyUsage |= x509.KeyUsageCertSign
}
x509Cert, err := x509.CreateCertificate(rand.Reader, template, template, options.Pub, options.Priv)
if err != nil {
return nil, nil, err
}
cert, key := &bytes.Buffer{}, &bytes.Buffer{}
if err := pem.Encode(cert, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
return nil, nil, err
}
x509Key, err := x509.MarshalPKCS8PrivateKey(options.Priv)
if err != nil {
return nil, nil, err
}
if err := pem.Encode(key, &pem.Block{Type: "PRIVATE KEY", Bytes: x509Key}); err != nil {
return nil, nil, err
}
return cert.Bytes(), key.Bytes(), nil
}
// CSR generates a certificate request in PEM format
func CSR(opts ...CertOption) ([]byte, error) {
options := CertOptions{}
for _, o := range opts {
o(&options)
}
csrTemplate := &x509.CertificateRequest{
Subject: options.Subject,
SignatureAlgorithm: x509.PureEd25519,
DNSNames: options.DNSNames,
IPAddresses: options.IPAddresses,
}
out := &bytes.Buffer{}
csr, err := x509.CreateCertificateRequest(rand.Reader, csrTemplate, options.Priv)
if err != nil {
return nil, err
}
if err := pem.Encode(out, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csr}); err != nil {
return nil, err
}
return out.Bytes(), nil
}
// Sign decodes a CSR and signs it with the CA
func Sign(CACrt, CAKey, CSR []byte, opts ...CertOption) ([]byte, error) {
options := CertOptions{}
for _, o := range opts {
o(&options)
}
asn1CACrt, err := decodePEM(CACrt)
if err != nil {
return nil, errors.Wrap(err, "failed to decode CA Crt PEM")
}
if len(asn1CACrt) != 1 {
return nil, errors.Errorf("expected 1 CA Crt, got %d", len(asn1CACrt))
}
caCrt, err := x509.ParseCertificate(asn1CACrt[0].Bytes)
if err != nil {
return nil, errors.Wrap(err, "ca is not a valid certificate")
}
asn1CAKey, err := decodePEM(CAKey)
if err != nil {
return nil, errors.Wrap(err, "failed to decode CA Key PEM")
}
if len(asn1CAKey) != 1 {
return nil, errors.Errorf("expected 1 CA Key, got %d", len(asn1CACrt))
}
caKey, err := x509.ParsePKCS8PrivateKey(asn1CAKey[0].Bytes)
if err != nil {
return nil, errors.Wrap(err, "ca key is not a valid private key")
}
asn1CSR, err := decodePEM(CSR)
if err != nil {
return nil, errors.Wrap(err, "failed to decode CSR PEM")
}
if len(asn1CSR) != 1 {
return nil, errors.Errorf("expected 1 CSR, got %d", len(asn1CSR))
}
csr, err := x509.ParseCertificateRequest(asn1CSR[0].Bytes)
if err != nil {
return nil, errors.Wrap(err, "csr is invalid")
}
template := &x509.Certificate{
SignatureAlgorithm: x509.PureEd25519,
Subject: csr.Subject,
DNSNames: csr.DNSNames,
IPAddresses: csr.IPAddresses,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
NotBefore: options.NotBefore,
NotAfter: options.NotAfter,
SerialNumber: options.SerialNumber,
BasicConstraintsValid: true,
}
x509Cert, err := x509.CreateCertificate(rand.Reader, template, caCrt, caCrt.PublicKey, caKey)
if err != nil {
return nil, errors.Wrap(err, "Couldn't sign certificate")
}
out := &bytes.Buffer{}
if err := pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
return nil, errors.Wrap(err, "couldn't encode cert")
}
return out.Bytes(), nil
}
func decodePEM(PEM []byte) ([]*pem.Block, error) {
var blocks []*pem.Block
var asn1 *pem.Block
var rest []byte
for {
asn1, rest = pem.Decode(PEM)
if asn1 == nil {
return nil, errors.New("PEM is not valid")
}
blocks = append(blocks, asn1)
if len(rest) == 0 {
break
}
}
return blocks, nil
}