forked from thales-e-security/estclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
282 lines (225 loc) · 9.2 KB
/
client.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
// Copyright 2019 Thales eSecurity
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Package estclient is minimal EST client SDK (see RFC 7030).
package estclient
import (
"crypto"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/go-resty/resty/v2"
)
// AuthData provides the authentication data offered by the client to the server. Non-nil values will be used during
// authentication.
type AuthData struct {
// ID is a pre-shared ID used as part of HTTP basic authentication.
ID *string
// Secret is a pre-shared secret used as part of HTTP basic authentication.
Secret *string
// Key is an existing private key owned by the client. If Key is supplied, ClientCert must also be present. The
// pair are used to perform client TLS authentication.
Key crypto.PrivateKey
// ClientCert is a certificate owned by the client. If ClientCert is supplied, Key must also be present. The
// pair are used to perform client TLS authentication.
ClientCert *x509.Certificate
// Prevent construction using un-keyed fields.
_ struct{}
}
// EstClient enables clients to request certificates from an EST server.
type EstClient interface {
// CaCerts retrieves the EST CA certificate (which will sign
// the apiclient certificates)
CaCerts() (*CaCertsInfo, error)
// SimpleEnroll requests a certificate from the EST server.
SimpleEnroll(authData AuthData, req *x509.CertificateRequest) (*x509.Certificate, error)
// SimpleReenroll renews a client certificate.
SimpleReenroll(authData AuthData, req *x509.CertificateRequest) (*x509.Certificate, error)
}
// CaCertsInfo contains the data returned by the EST server when
// calling /cacerts.
type CaCertsInfo struct {
// EstTA is the trust anchor of the EST system. If the EST CA is a subordinate of
// this trust anchor, then EstChainCerts should contain the necessary certificates to
// build a chain from issued certificates through to the EstTA.
EstTA *x509.Certificate
// EstChainCerts contains the certificates necessary to construct a chain from
// the certificates issued by the EST CA through to the EstTA certificiate. This
// field will be nil or empty if the EstTA is the same certificate as the EST CA.
EstChainCerts []*x509.Certificate
// OldWithOld may be present if the EstTA has renewed its certificate. It is a copy
// of the old EstTA certificate.
OldWithOld *x509.Certificate
// OldWithNew may be present if the EstTA has renewed its certificate. It is a certificate
// containing the public key of the old certificate, signed with the new EstTA key.
OldWithNew *x509.Certificate
// NewWithOld may be present if the EstTA has renewed its certificate. It is a certificate
// containing the new public key of the EstTA, signed with the old key.
NewWithOld *x509.Certificate
}
// estHTTPClient is the default implementation of the EstClient interface.
type estHTTPClient struct {
httpClient *resty.Client
label string
host string
options ClientOptions
}
// ClientOptions contains configuration settings for building the EST apiclient.
type ClientOptions struct {
// InsecureSkipVerify, when true, causes the apiclient to accept any TLS server certificate
// presented by the EST server. As the name suggests, this is insecure and for testing
// purposes only
InsecureSkipVerify bool
// TLSTrustAnchor, if non-nil, designates an explicit trust anchor to use for the
// TLS session to the EST server.
TLSTrustAnchor *x509.Certificate
// 3.2.2. HTTP URIs for Control
// https://www.example.com/.well-known/est/cacerts
// https://www.example.com/.well-known/est/arbitraryLabel1/cacerts
// https://www.example.com/.well-known/est/arbitraryLabel2/cacerts
Label string
/* Set of HTTP headers for each request. Useful to override the "Host" header for example */
Headers map[string]string
// Override TLS server name indication and Host header
Sni string
// Enable rest http client debug
Debug bool
}
// NewEstClient creates a apiclient that communicates with the given host.
// The host string is a domain name with optional ":port" suffix.
func NewEstClient(host string) EstClient {
return NewEstClientWithOptions(host, ClientOptions{})
}
// NewEstClientWithOptions accepts additional options to configure the EST apiclient.
func NewEstClientWithOptions(host string, options ClientOptions) EstClient {
return newEstClient(host, options.Label, options)
}
// newEstClient constructs an EST client. This should be used
// by unit tests wishing to mock the server interface.
func newEstClient(host string, label string, options ClientOptions) EstClient {
rest := resty.New()
rest.SetDebug(options.Debug)
tlsCfg := &tls.Config{
InsecureSkipVerify: options.InsecureSkipVerify,
}
if options.Sni != "" {
tlsCfg.ServerName = options.Sni
rest.SetHeader("Host", options.Sni)
}
if options.TLSTrustAnchor != nil {
trustPool := x509.NewCertPool()
trustPool.AddCert(options.TLSTrustAnchor)
tlsCfg.RootCAs = trustPool
}
rest.SetTLSClientConfig(tlsCfg)
return estHTTPClient{httpClient: rest, host: host, label: label, options: options}
}
// CaCerts implements EstClient.CaCerts
func (c estHTTPClient) CaCerts() (*CaCertsInfo, error) {
var resp *resty.Response
var err error
if c.label != "" {
resp, err = c.httpClient.R().
SetHeader("Accept", "application/pkcs7-mime").
SetHeader("Content-Type", "application/json").
Get("https://" + c.host + "/.well-known/est/" + c.label + "/cacerts")
} else {
resp, err = c.httpClient.R().
SetHeader("Accept", "application/pkcs7-mime").
SetHeader("Content-Type", "application/json").
Get("https://" + c.host + "/.well-known/est/cacerts")
}
if err != nil {
return nil, err
}
if resp.StatusCode() < 200 || resp.StatusCode() > 299 {
return nil, fmt.Errorf("Http wrong response status %d", resp.StatusCode())
}
return parseCaCerts(string(resp.Body()))
}
// SimpleEnroll implements EstClient.SimpleEnroll
func (c estHTTPClient) SimpleEnroll(authData AuthData, req *x509.CertificateRequest) (*x509.Certificate, error) {
var resp *resty.Response
var err error
if err := validateAuthData(authData); err != nil {
return nil, err
}
if authData.ClientCert != nil && authData.Key != nil {
c.httpClient.SetCertificates(tls.Certificate{
PrivateKey: authData.Key,
Certificate: [][]byte{authData.ClientCert.Raw},
Leaf: authData.ClientCert,
})
}
httpReq := c.httpClient.R()
httpReq = httpReq.
SetHeader("Accept", "application/pkcs7-mime").
SetHeader("Content-Type", "application/pkcs10").
SetBody(toBase64(req.Raw))
if authData.ID != nil && authData.Secret != nil {
httpReq = httpReq.SetBasicAuth(*authData.ID, *authData.Secret)
}
if c.label != "" {
resp, err = httpReq.Post("https://" + c.host + "/.well-known/est/" + c.label + "/simpleenroll")
} else {
resp, err = httpReq.Post("https://" + c.host + "/.well-known/est/simpleenroll")
}
if err != nil {
return nil, err
}
if resp.StatusCode() < 200 && resp.StatusCode() > 299 {
return nil, fmt.Errorf("Http wrong response status %d", resp.StatusCode())
}
return readCertificate(string(resp.Body()))
}
// SimpleReenroll implements EstClient.SimpleReenroll
func (c estHTTPClient) SimpleReenroll(authData AuthData, req *x509.CertificateRequest) (*x509.Certificate, error) {
var resp *resty.Response
var err error
if err := validateAuthData(authData); err != nil {
return nil, err
}
if authData.ClientCert != nil && authData.Key != nil {
c.httpClient.SetCertificates(tls.Certificate{
PrivateKey: authData.Key,
Certificate: [][]byte{authData.ClientCert.Raw},
Leaf: authData.ClientCert,
})
}
httpReq := c.httpClient.R()
httpReq = httpReq.
SetHeader("Accept", "application/pkcs7-mime").
SetHeader("Content-Type", "application/pkcs10").
SetBody(toBase64(req.Raw))
if authData.ID != nil && authData.Secret != nil {
httpReq = httpReq.SetBasicAuth(*authData.ID, *authData.Secret)
}
if c.label != "" {
resp, err = httpReq.Post("https://" + c.host + "/.well-known/est/" + c.label + "/simplereenroll")
} else {
resp, err = httpReq.Post("https://" + c.host + "/.well-known/est/simplereenroll")
}
if err != nil {
return nil, err
}
if resp.StatusCode() < 200 && resp.StatusCode() > 299 {
return nil, fmt.Errorf("Http wrong response status %d", resp.StatusCode())
}
return readCertificate(string(resp.Body()))
}