This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
certificate.go
155 lines (133 loc) · 4.9 KB
/
certificate.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
package inmemorygenerator
import (
"fmt"
"time"
"code.cloudfoundry.org/quarks-secret/pkg/credsgen"
"github.com/cloudflare/cfssl/cli/genkey"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/csr"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/initca"
cfssllog "github.com/cloudflare/cfssl/log"
"github.com/cloudflare/cfssl/signer"
"github.com/cloudflare/cfssl/signer/local"
"github.com/pkg/errors"
)
// GenerateCertificate generates a certificate using Cloudflare's TLS toolkit
func (g InMemoryGenerator) GenerateCertificate(name string, request credsgen.CertificateGenerationRequest) (credsgen.Certificate, error) {
g.log.Debugf("Generating certificate %s", name)
cfssllog.Level = cfssllog.LevelWarning
var certificate credsgen.Certificate
var err error
if request.IsCA {
certificate, err = g.generateCACertificate(request)
if err != nil {
return credsgen.Certificate{}, errors.Wrap(err, "Generating CA certificate failed.")
}
} else {
certificate, err = g.generateCertificate(request)
if err != nil {
return credsgen.Certificate{}, errors.Wrap(err, "Generating certificate failed.")
}
}
return certificate, nil
}
// GenerateCertificateSigningRequest Generates a certificate signing request and private key
func (g InMemoryGenerator) GenerateCertificateSigningRequest(request credsgen.CertificateGenerationRequest) ([]byte, []byte, error) {
cfssllog.Level = cfssllog.LevelWarning
var csReq, privateKey []byte
// Generate certificate request
certReq := &csr.CertificateRequest{KeyRequest: &csr.KeyRequest{A: g.Algorithm, S: g.Bits}}
certReq.Hosts = append(certReq.Hosts, request.CommonName)
certReq.Hosts = append(certReq.Hosts, request.AlternativeNames...)
certReq.CN = certReq.Hosts[0]
sslValidator := &csr.Generator{Validator: genkey.Validator}
csReq, privateKey, err := sslValidator.ProcessRequest(certReq)
if err != nil {
return csReq, privateKey, err
}
return csReq, privateKey, nil
}
// generateCertificate Generate a local-issued certificate and private key
func (g InMemoryGenerator) generateCertificate(request credsgen.CertificateGenerationRequest) (credsgen.Certificate, error) {
if !request.CA.IsCA {
return credsgen.Certificate{}, errors.Errorf("The passed CA is not a CA")
}
cert := credsgen.Certificate{
IsCA: false,
}
// Generate certificate
signingReq, privateKey, err := g.GenerateCertificateSigningRequest(request)
if err != nil {
return credsgen.Certificate{}, err
}
// Sign certificate
signingProfile := &config.SigningProfile{
Usage: []string{"server auth", "client auth"},
Expiry: time.Duration(g.Expiry*24) * time.Hour,
ExpiryString: fmt.Sprintf("%dh", g.Expiry*24),
}
cert.Certificate, err = g.signCertificate(signingReq, signingProfile, request)
if err != nil {
return credsgen.Certificate{}, err
}
cert.PrivateKey = privateKey
return cert, nil
}
// generateCACertificate Generate self-signed root CA certificate and private key
func (g InMemoryGenerator) generateCACertificate(request credsgen.CertificateGenerationRequest) (credsgen.Certificate, error) {
req := &csr.CertificateRequest{
CA: &csr.CAConfig{Expiry: fmt.Sprintf("%dh", g.Expiry*24)},
CN: request.CommonName,
KeyRequest: &csr.KeyRequest{A: g.Algorithm, S: g.Bits},
}
ca, csr, privateKey, err := initca.New(req)
if err != nil {
return credsgen.Certificate{}, err
}
cert := credsgen.Certificate{
IsCA: true,
Certificate: ca,
PrivateKey: privateKey,
}
if request.CA.IsCA {
signingProfile := &config.SigningProfile{
Usage: []string{"cert sign", "crl sign"},
ExpiryString: "43800h",
Expiry: 5 * helpers.OneYear,
CAConstraint: config.CAConstraint{
IsCA: true,
},
}
cert.Certificate, err = g.signCertificate(csr, signingProfile, request)
if err != nil {
return credsgen.Certificate{}, err
}
}
return cert, nil
}
// Given a signing profile, csr & request with CA, the certificate is signed by the CA.
func (g InMemoryGenerator) signCertificate(csr []byte, signingProfile *config.SigningProfile, request credsgen.CertificateGenerationRequest) ([]byte, error) {
policy := &config.Signing{
Profiles: map[string]*config.SigningProfile{},
Default: signingProfile,
}
// Parse parent CA
parentCACert, err := helpers.ParseCertificatePEM([]byte(request.CA.Certificate))
if err != nil {
return []byte{}, errors.Wrap(err, "Parsing CA PEM failed.")
}
parentCAKey, err := helpers.ParsePrivateKeyPEM([]byte(request.CA.PrivateKey))
if err != nil {
return []byte{}, errors.Wrap(err, "Parsing CA private key failed.")
}
s, err := local.NewSigner(parentCAKey, parentCACert, signer.DefaultSigAlgo(parentCAKey), policy)
if err != nil {
return []byte{}, errors.Wrap(err, "Creating signer failed.")
}
certificate, err := s.Sign(signer.SignRequest{Request: string(csr)})
if err != nil {
return []byte{}, errors.Wrap(err, "Signing certificate failed.")
}
return certificate, nil
}