/
cfssl.go
241 lines (207 loc) · 6.23 KB
/
cfssl.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
package csrprov
import (
"crypto"
"crypto/x509"
"crypto/x509/pkix"
"io/ioutil"
"strings"
"time"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/csr"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/initca"
"github.com/cloudflare/cfssl/signer"
"github.com/cloudflare/cfssl/signer/local"
"github.com/go-phorce/dolly/xpki/cryptoprov"
"github.com/pkg/errors"
)
// A CertificateRequest encapsulates the API interface to the
// certificate request functionality.
type CertificateRequest struct {
CN string
Names []X509Name `json:"names" yaml:"names"`
Hosts []string `json:"hosts" yaml:"hosts"`
KeyRequest KeyRequest `json:"key,omitempty" yaml:"key,omitempty"`
CA *CAConfig `json:"ca,omitempty" yaml:"ca,omitempty"`
SerialNumber string `json:"serialnumber,omitempty" yaml:"serialnumber,omitempty"`
}
// X509Name contains the SubjectInfo fields.
type X509Name struct {
C string // Country
ST string // State
L string // Locality
O string // OrganisationName
OU string // OrganisationalUnitName
SerialNumber string
}
// Name returns the PKIX name for the request.
func (cr *CertificateRequest) Name() pkix.Name {
var name pkix.Name
name.CommonName = cr.CN
for _, n := range cr.Names {
appendIf(n.C, &name.Country)
appendIf(n.ST, &name.Province)
appendIf(n.L, &name.Locality)
appendIf(n.O, &name.Organization)
appendIf(n.OU, &name.OrganizationalUnit)
}
name.SerialNumber = cr.SerialNumber
return name
}
// appendIf appends to a if s is not an empty string.
func appendIf(s string, a *[]string) {
if s != "" {
*a = append(*a, s)
}
}
// ConvertToCFSSL converts to CFSSL type
func (c *X509Name) ConvertToCFSSL() *csr.Name {
return &csr.Name{
C: c.C,
ST: c.ST,
L: c.L,
O: c.O,
OU: c.OU,
}
}
// CopyToCFSSL copies to CFSSL type
func (c *X509Name) CopyToCFSSL(d *csr.Name) {
d.C = c.C
d.ST = c.ST
d.L = c.L
d.O = c.O
d.OU = c.OU
}
// CAConfig is a section used in the requests initialising a new CA.
type CAConfig struct {
PathLength int `json:"pathlen" yaml:"pathlen"`
PathLenZero bool `json:"pathlenzero" yaml:"pathlenzero"`
Expiry string `json:"expiry" yaml:"expiry"`
Backdate string `json:"backdate" yaml:"backdate"`
}
// ConvertToCFSSL converts to CFSSL type
func (c *CAConfig) ConvertToCFSSL() *csr.CAConfig {
return &csr.CAConfig{
PathLength: c.PathLength,
PathLenZero: c.PathLenZero,
Expiry: c.Expiry,
Backdate: c.Backdate,
}
}
// CopyToCFSSL copies to CFSSL type
func (c *CAConfig) CopyToCFSSL(d *csr.CAConfig) {
d.PathLength = c.PathLength
d.PathLenZero = c.PathLenZero
d.Expiry = c.Expiry
d.Backdate = c.Backdate
}
// ValidateCSR contains the default validation logic for certificate
// authority certificates. The only requirement here is that the
// certificate have a non-empty subject field.
func ValidateCSR(req *CertificateRequest) error {
if req.CN != "" {
return nil
}
if len(req.Names) == 0 {
return errors.New("missing subject information")
}
for _, n := range req.Names {
if isNameEmpty(n) {
return errors.New("empty name")
}
}
return nil
}
// isNameEmpty returns true if the name has no identifying information in it.
func isNameEmpty(n X509Name) bool {
empty := func(s string) bool { return strings.TrimSpace(s) == "" }
if empty(n.C) && empty(n.ST) && empty(n.L) && empty(n.O) && empty(n.OU) {
return true
}
return false
}
// MakeCAPolicy make CA policy from the given certificate request
func MakeCAPolicy(req *CertificateRequest) (*config.Signing, error) {
var err error
policy := initca.CAPolicy()
if req.CA == nil {
return policy, nil
}
if req.CA.Expiry != "" {
policy.Default.ExpiryString = req.CA.Expiry
policy.Default.Expiry, err = time.ParseDuration(req.CA.Expiry)
if err != nil {
return nil, errors.WithStack(err)
}
}
if req.CA.Backdate != "" {
policy.Default.Backdate, err = time.ParseDuration(req.CA.Backdate)
if err != nil {
return nil, errors.WithStack(err)
}
}
policy.Default.CAConstraint.MaxPathLen = req.CA.PathLength
if req.CA.PathLength != 0 && req.CA.PathLenZero {
logger.Infof("ignore invalid 'pathlenzero' value")
} else {
policy.Default.CAConstraint.MaxPathLenZero = req.CA.PathLenZero
}
return policy, nil
}
// ParseCaFiles parses CA files
func ParseCaFiles(caFile, caKeyFile string) (cakey []byte, parsedCa *x509.Certificate, err error) {
ca, err := ioutil.ReadFile(caFile)
if err != nil {
err = errors.WithMessagef(err, "load ca file")
return
}
cakey, err = ioutil.ReadFile(caKeyFile)
if err != nil {
err = errors.WithMessagef(err, "load ca-key file")
return
}
parsedCa, err = helpers.ParseCertificatePEM(ca)
if err != nil {
err = errors.WithMessagef(err, "parse ca file")
return
}
return
}
// NewLocalCASignerFromFile generates a new local signer from a caFile
// and a caKey file, both PEM encoded or caKey contains PKCS#11 Uri
func NewLocalCASignerFromFile(c *cryptoprov.Crypto, caFile, caKeyFile string, policy *config.Signing) (*local.Signer, crypto.Signer, error) {
ca, err := ioutil.ReadFile(caFile)
if err != nil {
return nil, nil, errors.WithMessagef(err, "load ca file")
}
cakey, err := ioutil.ReadFile(caKeyFile)
if err != nil {
return nil, nil, errors.WithMessagef(err, "load ca-key file")
}
return NewLocalCASignerFromPEM(c, ca, cakey, policy)
}
// NewLocalCASignerFromPEM generates a new local signer from PEM encoded blocks,
// or caKey contains PKCS#11 Uri
func NewLocalCASignerFromPEM(c *cryptoprov.Crypto, ca, caKey []byte, policy *config.Signing) (*local.Signer, crypto.Signer, error) {
if policy == nil {
return nil, nil, errors.New("invalid parameter: policy")
}
_, pvk, err := c.LoadPrivateKey(caKey)
if err != nil {
return nil, nil, errors.WithStack(err)
}
sign, supported := pvk.(crypto.Signer)
if !supported {
return nil, nil, errors.Errorf("loaded key of %T type does not support crypto.Signer", pvk)
}
parsedCa, err := helpers.ParseCertificatePEM(ca)
if err != nil {
return nil, nil, errors.WithStack(err)
}
signPolicy := config.Signing(*policy)
localSigner, err := local.NewSigner(sign, parsedCa, signer.DefaultSigAlgo(sign), &signPolicy)
if err != nil {
return nil, nil, errors.WithStack(err)
}
return localSigner, sign, nil
}