/
client.go
407 lines (345 loc) · 10.4 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package acme
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
"github.com/tommie/acme-go/protocol"
"gopkg.in/square/go-jose.v2"
)
var (
ErrUnsupported = errors.New("unsupported operation")
)
// ClientAccount represents a client for connecting to an ACME
// account. Instances are not concurrency-safe.
type ClientAccount struct {
// URI is the registration URI of the account.
URI string
Key crypto.PublicKey
dirURI string
http getPoster
// d is a cache with URIs to well-known endpoints.
d *protocol.Directory
// reg is a cache used to get the authz and cert enumeration URIs.
reg *protocol.Registration
}
type getPoster interface {
protocol.Getter
protocol.Poster
}
// NewClientAccount creates a new account client by supplying the
// directory URI, account registration URI and the account key.
func NewClientAccount(dirURI, regURI string, accountKey crypto.PrivateKey) (*ClientAccount, error) {
type hasPublic interface {
Public() crypto.PublicKey
}
var pub crypto.PublicKey
if hp, ok := accountKey.(hasPublic); ok {
pub = hp.Public()
}
s, err := jose.NewSigner(
jose.SigningKey{Algorithm: signatureAlgo(accountKey), Key: accountKey},
&jose.SignerOptions{NonceSource: &protocol.NonceStack{}, EmbedJWK: true})
if err != nil {
return nil, err
}
hc := protocol.NewHTTPClient(nil, s)
// Get an initial nonce and validate the URI.
if _, err := hc.Head(dirURI); err != nil {
return nil, err
}
return &ClientAccount{
URI: regURI,
Key: pub,
dirURI: dirURI,
http: hc,
}, nil
}
// UpdateRegistration allows changing one or more aspects of the
// registration. Takes the same options as RegisterAccount.
func (a *ClientAccount) UpdateRegistration(opts ...RegistrationOpt) (*Registration, error) {
if len(opts) == 0 {
return nil, fmt.Errorf("nothing to update")
}
ret, err := doRegistration(a.http, a.URI, &protocol.Registration{Resource: protocol.ResourceReg}, opts...)
return ret, err
}
// Registration fetches the current registration resource. If the
// account registration is not complete, this returns ErrPending.
func (a *ClientAccount) Registration() (*Registration, error) {
ret, err := doRegistration(a.http, a.URI, &protocol.Registration{Resource: protocol.ResourceReg})
return ret, err
}
// AuthorizeIdentity starts an authorization flow for the given
// identifier. The returned *Authorization may be in pending state and
// require further action through solving returned challenges.
func (a *ClientAccount) AuthorizeIdentity(id Identifier) (*Authorization, error) {
req := &protocol.Authorization{
Resource: protocol.ResourceNewAuthz,
Identifier: *id.Protocol(),
}
d, err := a.directory()
if err != nil {
return nil, err
}
authz, resp, err := protocol.PostAuthorization(a.http, d.NewAuthz, req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("authorize identity: unexpected HTTP status: %s", resp.Status)
}
return newAuthorization(authz, resp)
}
// Authorization returns information about an existing
// authorization. It is up to the server to decide what authorizations
// are available to fetch (pending/valid/invalid).
func (a *ClientAccount) Authorization(uri string) (*Authorization, error) {
authz, resp, err := protocol.GetAuthorization(a.http, uri)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusAccepted:
case http.StatusOK:
break
default:
return nil, fmt.Errorf("get authorization: unexpected HTTP status: %s", resp.Status)
}
return newAuthorization(authz, resp)
}
// AuthorizationURIs returns the list of pending and/or valid
// authorizations, depending on the ACME server implementation. A
// returned URI can be used in a call to Authorization to get more
// information.
func (a *ClientAccount) AuthorizationURIs() ([]string, error) {
reg, err := a.registration()
if err != nil {
return nil, err
}
if reg.AuthorizationsURI == "" {
return nil, ErrUnsupported
}
uris, resp, err := protocol.GetAuthorizationURIs(a.http, reg.AuthorizationsURI)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("get authorization URIs: unexpected HTTP status: %s", resp.Status)
}
return uris.Authorizations, nil
}
// ValidateChallenge notifies the ACME server that a challenge is
// ready to be validated. The ACME client should keep the challenge
// solver running until the associated Authorization stops being
// pending.
func (a *ClientAccount) ValidateChallenge(uri string, req protocol.Response) (protocol.Challenge, error) {
chal, resp, err := protocol.PostResponse(a.http, uri, req)
if err != nil {
return nil, err
}
// ACME spec., Sec. 6.5 says OK is returned.
// Boulder returns Accepted.
switch resp.StatusCode {
case http.StatusAccepted:
case http.StatusOK:
break
default:
return nil, fmt.Errorf("validate challenge: unexpected HTTP status: %s", resp.Status)
}
return chal, nil
}
// IssueCertificate signs a certificate signing request if
// authorized. This function will block until the requst is completed
// by the ACME server.
func (a *ClientAccount) IssueCertificate(csr []byte) (*Certificate, error) {
d, err := a.directory()
if err != nil {
return nil, err
}
cbs, resp, err := protocol.PostCertificateIssuance(a.http, d.NewCert, &protocol.CertificateIssuance{
Resource: protocol.ResourceNewCert,
CSR: protocol.DERData(csr),
})
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("issue certificate: unexpected HTTP status: %s", resp.Status)
}
uri, err := contentLocation(resp)
// ACME Spec Sec. 6.6 says servers SHOULD provide Content-Location.
// Boulder does not, so fall back to Location (which is not stable).
if err == http.ErrNoLocation {
uri, err = resp.Location()
}
if err != nil {
return nil, err
}
if len(cbs) != 0 {
return &Certificate{
Bytes: cbs,
URI: uri.String(),
IssuerURIs: links(resp, "up"),
}, err
}
ra, _ := retryAfter(resp.Header, 1*time.Second)
time.Sleep(ra)
return a.Certificate(uri.String())
}
// Certificate returns an existing certificate. This blocks while the
// certificate is pending.
func (a *ClientAccount) Certificate(uri string) (*Certificate, error) {
for {
cbs, resp, err := protocol.GetCertificate(a.http, uri)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
return &Certificate{
Bytes: cbs,
URI: uri,
IssuerURIs: links(resp, "up"),
}, nil
case http.StatusAccepted:
break
default:
return nil, fmt.Errorf("get certificate: unexpected HTTP status: %s", resp.Status)
}
d, _ := retryAfter(resp.Header, 1*time.Second)
time.Sleep(d)
}
}
// CertificateURIs returns the list of certificates known for this
// account by the ACME server. Returned URIs can be used in calls to
// Certificate to get more information.
func (a *ClientAccount) CertificateURIs() ([]string, error) {
reg, err := a.registration()
if err != nil {
return nil, err
}
if reg.CertificatesURI == "" {
return nil, ErrUnsupported
}
uris, resp, err := protocol.GetCertificateURIs(a.http, reg.CertificatesURI)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("get certificate URIs: unexpected HTTP status: %s", resp.Status)
}
return uris.Certificates, nil
}
// RevokeCertificate requests a revocation. The given cert should be
// exactly the same as returned by IssueCertificate.
func (a *ClientAccount) RevokeCertificate(cert []byte) error {
d, err := a.directory()
if err != nil {
return err
}
req := &protocol.Certificate{
Resource: protocol.ResourceRevokeCert,
Certificate: protocol.DERData(cert),
}
resp, err := protocol.PostCertificateRevocation(a.http, d.RevokeCert, req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("revoke certificate: unexpected HTTP status: %s", resp.Status)
}
return nil
}
// directory returns the ACME server directory, and caches it.
func (a *ClientAccount) directory() (*protocol.Directory, error) {
if a.d == nil {
d, _, err := protocol.GetDirectory(a.http, a.dirURI)
if err != nil {
return nil, err
}
a.d = d
}
return a.d, nil
}
// registration returns the current ACME account registration, and caches it.
func (a *ClientAccount) registration() (*protocol.Registration, error) {
if a.reg == nil {
reg, _, err := protocol.PostRegistration(a.http, a.URI, &protocol.Registration{
Resource: protocol.ResourceReg,
})
if err != nil {
return nil, err
}
a.reg = reg
}
return a.reg, nil
}
// contentLocation returns a resolved Content-Location header. Returns
// http.ErrNoLocation if the header is missing. See also http.Response.Location.
func contentLocation(r *http.Response) (*url.URL, error) {
s := r.Header.Get("Content-Location")
if s == "" {
return nil, http.ErrNoLocation
}
if r.Request == nil || r.Request.URL == nil {
return url.Parse(s)
}
return r.Request.URL.Parse(s)
}
// retryAfter returns the Retry-After header, or def.
func retryAfter(hdr http.Header, def time.Duration) (time.Duration, error) {
n, err := strconv.Atoi(hdr.Get(protocol.RetryAfter))
if err != nil {
return def, err
}
return time.Duration(n) * time.Second, nil
}
var linkRE = regexp.MustCompile(`^<([^>]+)>(?:;[^=]+=(?:[^;"]+|"[^"]*"))*;rel="([^"]+)"(?:;.*)?$`)
// links returns the specified type of Link headers.
func links(r *http.Response, rel string) []string {
base := &url.URL{}
if r.Request != nil && r.Request.URL != nil {
base = r.Request.URL
}
var ret []string
for _, s := range r.Header[protocol.Link] {
ss := linkRE.FindStringSubmatch(s)
if ss == nil || ss[2] != rel {
continue
}
u, err := url.Parse(ss[1])
if err != nil {
ret = append(ret, ss[1])
continue
}
ret = append(ret, base.ResolveReference(u).String())
}
return ret
}
// signatureAlgo returns a suggested JWS algorithm based on the
// private key. Returns a zero value if none exists.
func signatureAlgo(key crypto.PrivateKey) jose.SignatureAlgorithm {
switch k := key.(type) {
case *ecdsa.PrivateKey:
// This is a generalization of what the ECDH algorithm
// uses in the ACME spec.
if k.Curve.Params().BitSize < 256 {
return jose.ES256
} else if k.Curve.Params().BitSize < 521 {
return jose.ES384
} else {
return jose.ES512
}
case *rsa.PrivateKey:
// RS256 is the default in the letsencrypt client.
return jose.RS256
}
return ""
}