forked from cloudflare/cfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfssl_provider.go
294 lines (245 loc) · 6.99 KB
/
cfssl_provider.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package ca
import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"net"
"path/filepath"
"github.com/cloudflare/cfssl/api/client"
"github.com/cloudflare/cfssl/auth"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/info"
"github.com/cloudflare/cfssl/signer"
"github.com/cloudflare/cfssl/transport/core"
)
type authError struct {
authType string
}
func (err *authError) Error() string {
return fmt.Sprintf("transport: unsupported CFSSL authentication method %s", err.authType)
}
// This approach allows us to quickly add other providers later, such
// as the TPM.
var authTypes = map[string]func(config.AuthKey, []byte) (auth.Provider, error){
"standard": newStandardProvider,
}
// Create a standard provider without providing any additional data.
func newStandardProvider(ak config.AuthKey, ad []byte) (auth.Provider, error) {
return auth.New(ak.Key, ad)
}
// Create a new provider from an authentication key and possibly
// additional data.
func newProvider(ak config.AuthKey, ad []byte) (auth.Provider, error) {
// If no auth key was provided, use unauthenticated
// requests. This is useful when a local CFSSL is being used.
if ak.Type == "" && ak.Key == "" {
return nil, nil
}
f, ok := authTypes[ak.Type]
if !ok {
return nil, &authError{authType: ak.Type}
}
return f(ak, ad)
}
// ErrNoAuth is returned when a client is talking to a CFSSL remote
// that is not on a loopback address and doesn't have an
// authentication provider set.
var ErrNoAuth = errors.New("transport: authentication is required for non-local remotes")
var v4Loopback = net.IPNet{
IP: net.IP{127, 0, 0, 0},
Mask: net.IPv4Mask(255, 0, 0, 0),
}
func ipIsLocal(ip net.IP) bool {
if ip.To4() == nil {
return ip.Equal(net.IPv6loopback)
}
return v4Loopback.Contains(ip)
}
// The only time a client should be doing unauthenticated requests is
// when a local CFSSL is being used.
func (cap *CFSSL) validateAuth() error {
// The client is using some form of authentication, and the best way
// to figure out that the auth is invalid is when it's used. Therefore,
// we'll delay checking the credentials until that time.
if cap.provider != nil {
return nil
}
hosts := cap.remote.Hosts()
for i := range hosts {
ips, err := net.LookupIP(hosts[i])
if err != nil {
return err
}
for _, ip := range ips {
if !ipIsLocal(ip) {
return ErrNoAuth
}
}
}
return nil
}
var cfsslConfigDirs = []string{
"/usr/local/cfssl",
"/etc/cfssl",
"/state/etc/cfssl",
}
// The CFSSL standard is to have a configuration file for a label as
// <config>.json.
func findLabel(label string) *config.Config {
for _, dir := range cfsslConfigDirs {
cfgFile := filepath.Join(dir, label+".json")
cfg, err := config.LoadFile(cfgFile)
if err == nil {
return cfg
}
}
return nil
}
func getProfile(cfg *config.Config, profileName string) (*config.SigningProfile, bool) {
if cfg == nil || cfg.Signing == nil || cfg.Signing.Default == nil {
return nil, false
}
var ok bool
profile := cfg.Signing.Default
if profileName != "" {
if cfg.Signing.Profiles == nil {
return nil, false
}
profile, ok = cfg.Signing.Profiles[profileName]
if !ok {
return nil, false
}
}
return profile, true
}
// loadAuth loads an authentication provider from the client config's
// explicitly set auth key.
func (cap *CFSSL) loadAuth() error {
var err error
cap.provider, err = newProvider(cap.DefaultAuth, nil)
return err
}
func getRemote(cfg *config.Config, profile *config.SigningProfile) (string, bool) {
// NB: Loading the config will validate that the remote is
// present in the config's remote section.
if profile.RemoteServer != "" {
return profile.RemoteServer, true
}
return "", false
}
func (cap *CFSSL) setRemoteAndAuth() error {
if cap.Label != "" {
cfsslConfig := findLabel(cap.Label)
profile, ok := getProfile(cfsslConfig, cap.Profile)
if ok {
remote, ok := getRemote(cfsslConfig, profile)
if ok {
cap.remote = client.NewServerTLS(remote, helpers.CreateTLSConfig(profile.RemoteCAs, profile.ClientCert))
cap.provider = profile.Provider
return nil
}
// The profile may not have a remote set, but
// it may have an authentication provider.
cap.provider = profile.Provider
}
}
cap.remote = cap.DefaultRemote
if cap.provider != nil {
return nil
}
return cap.loadAuth()
}
// CFSSL provides support for signing certificates via
// CFSSL.
type CFSSL struct {
remote client.Remote
provider auth.Provider
Profile string
Label string
DefaultRemote client.Remote
DefaultAuth config.AuthKey
}
// SignCSR requests a certificate from a CFSSL signer.
func (cap *CFSSL) SignCSR(csrPEM []byte) (cert []byte, err error) {
p, _ := pem.Decode(csrPEM)
if p == nil || p.Type != "CERTIFICATE REQUEST" {
return nil, errors.New("transport: invalid PEM-encoded certificate signing request")
}
csr, err := x509.ParseCertificateRequest(p.Bytes)
if err != nil {
return nil, err
}
hosts := make([]string, len(csr.DNSNames), len(csr.DNSNames)+len(csr.IPAddresses))
copy(hosts, csr.DNSNames)
for i := range csr.IPAddresses {
hosts = append(hosts, csr.IPAddresses[i].String())
}
sreq := &signer.SignRequest{
Hosts: hosts,
Request: string(csrPEM),
Profile: cap.Profile,
Label: cap.Label,
}
out, err := json.Marshal(sreq)
if err != nil {
return nil, err
}
if cap.provider != nil {
return cap.remote.AuthSign(out, nil, cap.provider)
}
return cap.remote.Sign(out)
}
// CACertificate returns the certificate for a CFSSL CA.
func (cap *CFSSL) CACertificate() ([]byte, error) {
req := &info.Req{
Label: cap.Label,
Profile: cap.Profile,
}
out, err := json.Marshal(req)
if err != nil {
return nil, err
}
resp, err := cap.remote.Info(out)
if err != nil {
return nil, err
}
return []byte(resp.Certificate), nil
}
// NewCFSSLProvider takes the configuration information from an
// Identity (and an optional default remote), returning a CFSSL
// instance. There should be a profile in id called "cfssl", which
// should contain label and profile fields as needed.
func NewCFSSLProvider(id *core.Identity, defaultRemote client.Remote) (*CFSSL, error) {
if id == nil {
return nil, errors.New("transport: the identity hasn't been initialised. Has it been loaded from disk?")
}
cap := &CFSSL{
DefaultRemote: defaultRemote,
}
cfssl := id.Profiles["cfssl"]
if cfssl != nil {
cap.Label = cfssl["label"]
cap.Profile = cfssl["profile"]
if cap.DefaultRemote == nil {
cert, err := helpers.LoadClientCertificate(cfssl["mutual-tls-cert"], cfssl["mutual-tls-key"])
if err != nil {
return nil, err
}
remoteCAs, err := helpers.LoadPEMCertPool(cfssl["tls-remote-ca"])
if err != nil {
return nil, err
}
cap.DefaultRemote = client.NewServerTLS(cfssl["remote"], helpers.CreateTLSConfig(remoteCAs, cert))
}
cap.DefaultAuth.Type = cfssl["auth-type"]
cap.DefaultAuth.Key = cfssl["auth-key"]
}
err := cap.setRemoteAndAuth()
if err != nil {
return nil, err
}
return cap, nil
}