barometer: update DMA's vendoring packages
[barometer.git] / src / dma / vendor / golang.org / x / crypto / acme / autocert / autocert.go
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package autocert provides automatic access to certificates from Let's Encrypt
6 // and any other ACME-based CA.
7 //
8 // This package is a work in progress and makes no API stability promises.
9 package autocert
10
11 import (
12         "bytes"
13         "context"
14         "crypto"
15         "crypto/ecdsa"
16         "crypto/elliptic"
17         "crypto/rand"
18         "crypto/rsa"
19         "crypto/tls"
20         "crypto/x509"
21         "crypto/x509/pkix"
22         "encoding/pem"
23         "errors"
24         "fmt"
25         "io"
26         mathrand "math/rand"
27         "net"
28         "net/http"
29         "path"
30         "strings"
31         "sync"
32         "time"
33
34         "golang.org/x/crypto/acme"
35         "golang.org/x/net/idna"
36 )
37
38 // createCertRetryAfter is how much time to wait before removing a failed state
39 // entry due to an unsuccessful createCert call.
40 // This is a variable instead of a const for testing.
41 // TODO: Consider making it configurable or an exp backoff?
42 var createCertRetryAfter = time.Minute
43
44 // pseudoRand is safe for concurrent use.
45 var pseudoRand *lockedMathRand
46
47 func init() {
48         src := mathrand.NewSource(time.Now().UnixNano())
49         pseudoRand = &lockedMathRand{rnd: mathrand.New(src)}
50 }
51
52 // AcceptTOS is a Manager.Prompt function that always returns true to
53 // indicate acceptance of the CA's Terms of Service during account
54 // registration.
55 func AcceptTOS(tosURL string) bool { return true }
56
57 // HostPolicy specifies which host names the Manager is allowed to respond to.
58 // It returns a non-nil error if the host should be rejected.
59 // The returned error is accessible via tls.Conn.Handshake and its callers.
60 // See Manager's HostPolicy field and GetCertificate method docs for more details.
61 type HostPolicy func(ctx context.Context, host string) error
62
63 // HostWhitelist returns a policy where only the specified host names are allowed.
64 // Only exact matches are currently supported. Subdomains, regexp or wildcard
65 // will not match.
66 //
67 // Note that all hosts will be converted to Punycode via idna.Lookup.ToASCII so that
68 // Manager.GetCertificate can handle the Unicode IDN and mixedcase hosts correctly.
69 // Invalid hosts will be silently ignored.
70 func HostWhitelist(hosts ...string) HostPolicy {
71         whitelist := make(map[string]bool, len(hosts))
72         for _, h := range hosts {
73                 if h, err := idna.Lookup.ToASCII(h); err == nil {
74                         whitelist[h] = true
75                 }
76         }
77         return func(_ context.Context, host string) error {
78                 if !whitelist[host] {
79                         return fmt.Errorf("acme/autocert: host %q not configured in HostWhitelist", host)
80                 }
81                 return nil
82         }
83 }
84
85 // defaultHostPolicy is used when Manager.HostPolicy is not set.
86 func defaultHostPolicy(context.Context, string) error {
87         return nil
88 }
89
90 // Manager is a stateful certificate manager built on top of acme.Client.
91 // It obtains and refreshes certificates automatically using "tls-alpn-01",
92 // "tls-sni-01", "tls-sni-02" and "http-01" challenge types,
93 // as well as providing them to a TLS server via tls.Config.
94 //
95 // You must specify a cache implementation, such as DirCache,
96 // to reuse obtained certificates across program restarts.
97 // Otherwise your server is very likely to exceed the certificate
98 // issuer's request rate limits.
99 type Manager struct {
100         // Prompt specifies a callback function to conditionally accept a CA's Terms of Service (TOS).
101         // The registration may require the caller to agree to the CA's TOS.
102         // If so, Manager calls Prompt with a TOS URL provided by the CA. Prompt should report
103         // whether the caller agrees to the terms.
104         //
105         // To always accept the terms, the callers can use AcceptTOS.
106         Prompt func(tosURL string) bool
107
108         // Cache optionally stores and retrieves previously-obtained certificates
109         // and other state. If nil, certs will only be cached for the lifetime of
110         // the Manager. Multiple Managers can share the same Cache.
111         //
112         // Using a persistent Cache, such as DirCache, is strongly recommended.
113         Cache Cache
114
115         // HostPolicy controls which domains the Manager will attempt
116         // to retrieve new certificates for. It does not affect cached certs.
117         //
118         // If non-nil, HostPolicy is called before requesting a new cert.
119         // If nil, all hosts are currently allowed. This is not recommended,
120         // as it opens a potential attack where clients connect to a server
121         // by IP address and pretend to be asking for an incorrect host name.
122         // Manager will attempt to obtain a certificate for that host, incorrectly,
123         // eventually reaching the CA's rate limit for certificate requests
124         // and making it impossible to obtain actual certificates.
125         //
126         // See GetCertificate for more details.
127         HostPolicy HostPolicy
128
129         // RenewBefore optionally specifies how early certificates should
130         // be renewed before they expire.
131         //
132         // If zero, they're renewed 30 days before expiration.
133         RenewBefore time.Duration
134
135         // Client is used to perform low-level operations, such as account registration
136         // and requesting new certificates.
137         //
138         // If Client is nil, a zero-value acme.Client is used with acme.LetsEncryptURL
139         // as directory endpoint. If the Client.Key is nil, a new ECDSA P-256 key is
140         // generated and, if Cache is not nil, stored in cache.
141         //
142         // Mutating the field after the first call of GetCertificate method will have no effect.
143         Client *acme.Client
144
145         // Email optionally specifies a contact email address.
146         // This is used by CAs, such as Let's Encrypt, to notify about problems
147         // with issued certificates.
148         //
149         // If the Client's account key is already registered, Email is not used.
150         Email string
151
152         // ForceRSA used to make the Manager generate RSA certificates. It is now ignored.
153         //
154         // Deprecated: the Manager will request the correct type of certificate based
155         // on what each client supports.
156         ForceRSA bool
157
158         // ExtraExtensions are used when generating a new CSR (Certificate Request),
159         // thus allowing customization of the resulting certificate.
160         // For instance, TLS Feature Extension (RFC 7633) can be used
161         // to prevent an OCSP downgrade attack.
162         //
163         // The field value is passed to crypto/x509.CreateCertificateRequest
164         // in the template's ExtraExtensions field as is.
165         ExtraExtensions []pkix.Extension
166
167         clientMu sync.Mutex
168         client   *acme.Client // initialized by acmeClient method
169
170         stateMu sync.Mutex
171         state   map[certKey]*certState
172
173         // renewal tracks the set of domains currently running renewal timers.
174         renewalMu sync.Mutex
175         renewal   map[certKey]*domainRenewal
176
177         // tokensMu guards the rest of the fields: tryHTTP01, certTokens and httpTokens.
178         tokensMu sync.RWMutex
179         // tryHTTP01 indicates whether the Manager should try "http-01" challenge type
180         // during the authorization flow.
181         tryHTTP01 bool
182         // httpTokens contains response body values for http-01 challenges
183         // and is keyed by the URL path at which a challenge response is expected
184         // to be provisioned.
185         // The entries are stored for the duration of the authorization flow.
186         httpTokens map[string][]byte
187         // certTokens contains temporary certificates for tls-sni and tls-alpn challenges
188         // and is keyed by token domain name, which matches server name of ClientHello.
189         // Keys always have ".acme.invalid" suffix for tls-sni. Otherwise, they are domain names
190         // for tls-alpn.
191         // The entries are stored for the duration of the authorization flow.
192         certTokens map[string]*tls.Certificate
193         // nowFunc, if not nil, returns the current time. This may be set for
194         // testing purposes.
195         nowFunc func() time.Time
196 }
197
198 // certKey is the key by which certificates are tracked in state, renewal and cache.
199 type certKey struct {
200         domain  string // without trailing dot
201         isRSA   bool   // RSA cert for legacy clients (as opposed to default ECDSA)
202         isToken bool   // tls-based challenge token cert; key type is undefined regardless of isRSA
203 }
204
205 func (c certKey) String() string {
206         if c.isToken {
207                 return c.domain + "+token"
208         }
209         if c.isRSA {
210                 return c.domain + "+rsa"
211         }
212         return c.domain
213 }
214
215 // TLSConfig creates a new TLS config suitable for net/http.Server servers,
216 // supporting HTTP/2 and the tls-alpn-01 ACME challenge type.
217 func (m *Manager) TLSConfig() *tls.Config {
218         return &tls.Config{
219                 GetCertificate: m.GetCertificate,
220                 NextProtos: []string{
221                         "h2", "http/1.1", // enable HTTP/2
222                         acme.ALPNProto, // enable tls-alpn ACME challenges
223                 },
224         }
225 }
226
227 // GetCertificate implements the tls.Config.GetCertificate hook.
228 // It provides a TLS certificate for hello.ServerName host, including answering
229 // tls-alpn-01 and *.acme.invalid (tls-sni-01 and tls-sni-02) challenges.
230 // All other fields of hello are ignored.
231 //
232 // If m.HostPolicy is non-nil, GetCertificate calls the policy before requesting
233 // a new cert. A non-nil error returned from m.HostPolicy halts TLS negotiation.
234 // The error is propagated back to the caller of GetCertificate and is user-visible.
235 // This does not affect cached certs. See HostPolicy field description for more details.
236 //
237 // If GetCertificate is used directly, instead of via Manager.TLSConfig, package users will
238 // also have to add acme.ALPNProto to NextProtos for tls-alpn-01, or use HTTPHandler
239 // for http-01. (The tls-sni-* challenges have been deprecated by popular ACME providers
240 // due to security issues in the ecosystem.)
241 func (m *Manager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
242         if m.Prompt == nil {
243                 return nil, errors.New("acme/autocert: Manager.Prompt not set")
244         }
245
246         name := hello.ServerName
247         if name == "" {
248                 return nil, errors.New("acme/autocert: missing server name")
249         }
250         if !strings.Contains(strings.Trim(name, "."), ".") {
251                 return nil, errors.New("acme/autocert: server name component count invalid")
252         }
253
254         // Note that this conversion is necessary because some server names in the handshakes
255         // started by some clients (such as cURL) are not converted to Punycode, which will
256         // prevent us from obtaining certificates for them. In addition, we should also treat
257         // example.com and EXAMPLE.COM as equivalent and return the same certificate for them.
258         // Fortunately, this conversion also helped us deal with this kind of mixedcase problems.
259         //
260         // Due to the "σςΣ" problem (see https://unicode.org/faq/idn.html#22), we can't use
261         // idna.Punycode.ToASCII (or just idna.ToASCII) here.
262         name, err := idna.Lookup.ToASCII(name)
263         if err != nil {
264                 return nil, errors.New("acme/autocert: server name contains invalid character")
265         }
266
267         // In the worst-case scenario, the timeout needs to account for caching, host policy,
268         // domain ownership verification and certificate issuance.
269         ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
270         defer cancel()
271
272         // Check whether this is a token cert requested for TLS-SNI or TLS-ALPN challenge.
273         if wantsTokenCert(hello) {
274                 m.tokensMu.RLock()
275                 defer m.tokensMu.RUnlock()
276                 // It's ok to use the same token cert key for both tls-sni and tls-alpn
277                 // because there's always at most 1 token cert per on-going domain authorization.
278                 // See m.verify for details.
279                 if cert := m.certTokens[name]; cert != nil {
280                         return cert, nil
281                 }
282                 if cert, err := m.cacheGet(ctx, certKey{domain: name, isToken: true}); err == nil {
283                         return cert, nil
284                 }
285                 // TODO: cache error results?
286                 return nil, fmt.Errorf("acme/autocert: no token cert for %q", name)
287         }
288
289         // regular domain
290         ck := certKey{
291                 domain: strings.TrimSuffix(name, "."), // golang.org/issue/18114
292                 isRSA:  !supportsECDSA(hello),
293         }
294         cert, err := m.cert(ctx, ck)
295         if err == nil {
296                 return cert, nil
297         }
298         if err != ErrCacheMiss {
299                 return nil, err
300         }
301
302         // first-time
303         if err := m.hostPolicy()(ctx, name); err != nil {
304                 return nil, err
305         }
306         cert, err = m.createCert(ctx, ck)
307         if err != nil {
308                 return nil, err
309         }
310         m.cachePut(ctx, ck, cert)
311         return cert, nil
312 }
313
314 // wantsTokenCert reports whether a TLS request with SNI is made by a CA server
315 // for a challenge verification.
316 func wantsTokenCert(hello *tls.ClientHelloInfo) bool {
317         // tls-alpn-01
318         if len(hello.SupportedProtos) == 1 && hello.SupportedProtos[0] == acme.ALPNProto {
319                 return true
320         }
321         // tls-sni-xx
322         return strings.HasSuffix(hello.ServerName, ".acme.invalid")
323 }
324
325 func supportsECDSA(hello *tls.ClientHelloInfo) bool {
326         // The "signature_algorithms" extension, if present, limits the key exchange
327         // algorithms allowed by the cipher suites. See RFC 5246, section 7.4.1.4.1.
328         if hello.SignatureSchemes != nil {
329                 ecdsaOK := false
330         schemeLoop:
331                 for _, scheme := range hello.SignatureSchemes {
332                         const tlsECDSAWithSHA1 tls.SignatureScheme = 0x0203 // constant added in Go 1.10
333                         switch scheme {
334                         case tlsECDSAWithSHA1, tls.ECDSAWithP256AndSHA256,
335                                 tls.ECDSAWithP384AndSHA384, tls.ECDSAWithP521AndSHA512:
336                                 ecdsaOK = true
337                                 break schemeLoop
338                         }
339                 }
340                 if !ecdsaOK {
341                         return false
342                 }
343         }
344         if hello.SupportedCurves != nil {
345                 ecdsaOK := false
346                 for _, curve := range hello.SupportedCurves {
347                         if curve == tls.CurveP256 {
348                                 ecdsaOK = true
349                                 break
350                         }
351                 }
352                 if !ecdsaOK {
353                         return false
354                 }
355         }
356         for _, suite := range hello.CipherSuites {
357                 switch suite {
358                 case tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
359                         tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
360                         tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
361                         tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
362                         tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
363                         tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
364                         tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305:
365                         return true
366                 }
367         }
368         return false
369 }
370
371 // HTTPHandler configures the Manager to provision ACME "http-01" challenge responses.
372 // It returns an http.Handler that responds to the challenges and must be
373 // running on port 80. If it receives a request that is not an ACME challenge,
374 // it delegates the request to the optional fallback handler.
375 //
376 // If fallback is nil, the returned handler redirects all GET and HEAD requests
377 // to the default TLS port 443 with 302 Found status code, preserving the original
378 // request path and query. It responds with 400 Bad Request to all other HTTP methods.
379 // The fallback is not protected by the optional HostPolicy.
380 //
381 // Because the fallback handler is run with unencrypted port 80 requests,
382 // the fallback should not serve TLS-only requests.
383 //
384 // If HTTPHandler is never called, the Manager will only use the "tls-alpn-01"
385 // challenge for domain verification.
386 func (m *Manager) HTTPHandler(fallback http.Handler) http.Handler {
387         m.tokensMu.Lock()
388         defer m.tokensMu.Unlock()
389         m.tryHTTP01 = true
390
391         if fallback == nil {
392                 fallback = http.HandlerFunc(handleHTTPRedirect)
393         }
394         return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
395                 if !strings.HasPrefix(r.URL.Path, "/.well-known/acme-challenge/") {
396                         fallback.ServeHTTP(w, r)
397                         return
398                 }
399                 // A reasonable context timeout for cache and host policy only,
400                 // because we don't wait for a new certificate issuance here.
401                 ctx, cancel := context.WithTimeout(r.Context(), time.Minute)
402                 defer cancel()
403                 if err := m.hostPolicy()(ctx, r.Host); err != nil {
404                         http.Error(w, err.Error(), http.StatusForbidden)
405                         return
406                 }
407                 data, err := m.httpToken(ctx, r.URL.Path)
408                 if err != nil {
409                         http.Error(w, err.Error(), http.StatusNotFound)
410                         return
411                 }
412                 w.Write(data)
413         })
414 }
415
416 func handleHTTPRedirect(w http.ResponseWriter, r *http.Request) {
417         if r.Method != "GET" && r.Method != "HEAD" {
418                 http.Error(w, "Use HTTPS", http.StatusBadRequest)
419                 return
420         }
421         target := "https://" + stripPort(r.Host) + r.URL.RequestURI()
422         http.Redirect(w, r, target, http.StatusFound)
423 }
424
425 func stripPort(hostport string) string {
426         host, _, err := net.SplitHostPort(hostport)
427         if err != nil {
428                 return hostport
429         }
430         return net.JoinHostPort(host, "443")
431 }
432
433 // cert returns an existing certificate either from m.state or cache.
434 // If a certificate is found in cache but not in m.state, the latter will be filled
435 // with the cached value.
436 func (m *Manager) cert(ctx context.Context, ck certKey) (*tls.Certificate, error) {
437         m.stateMu.Lock()
438         if s, ok := m.state[ck]; ok {
439                 m.stateMu.Unlock()
440                 s.RLock()
441                 defer s.RUnlock()
442                 return s.tlscert()
443         }
444         defer m.stateMu.Unlock()
445         cert, err := m.cacheGet(ctx, ck)
446         if err != nil {
447                 return nil, err
448         }
449         signer, ok := cert.PrivateKey.(crypto.Signer)
450         if !ok {
451                 return nil, errors.New("acme/autocert: private key cannot sign")
452         }
453         if m.state == nil {
454                 m.state = make(map[certKey]*certState)
455         }
456         s := &certState{
457                 key:  signer,
458                 cert: cert.Certificate,
459                 leaf: cert.Leaf,
460         }
461         m.state[ck] = s
462         go m.renew(ck, s.key, s.leaf.NotAfter)
463         return cert, nil
464 }
465
466 // cacheGet always returns a valid certificate, or an error otherwise.
467 // If a cached certificate exists but is not valid, ErrCacheMiss is returned.
468 func (m *Manager) cacheGet(ctx context.Context, ck certKey) (*tls.Certificate, error) {
469         if m.Cache == nil {
470                 return nil, ErrCacheMiss
471         }
472         data, err := m.Cache.Get(ctx, ck.String())
473         if err != nil {
474                 return nil, err
475         }
476
477         // private
478         priv, pub := pem.Decode(data)
479         if priv == nil || !strings.Contains(priv.Type, "PRIVATE") {
480                 return nil, ErrCacheMiss
481         }
482         privKey, err := parsePrivateKey(priv.Bytes)
483         if err != nil {
484                 return nil, err
485         }
486
487         // public
488         var pubDER [][]byte
489         for len(pub) > 0 {
490                 var b *pem.Block
491                 b, pub = pem.Decode(pub)
492                 if b == nil {
493                         break
494                 }
495                 pubDER = append(pubDER, b.Bytes)
496         }
497         if len(pub) > 0 {
498                 // Leftover content not consumed by pem.Decode. Corrupt. Ignore.
499                 return nil, ErrCacheMiss
500         }
501
502         // verify and create TLS cert
503         leaf, err := validCert(ck, pubDER, privKey, m.now())
504         if err != nil {
505                 return nil, ErrCacheMiss
506         }
507         tlscert := &tls.Certificate{
508                 Certificate: pubDER,
509                 PrivateKey:  privKey,
510                 Leaf:        leaf,
511         }
512         return tlscert, nil
513 }
514
515 func (m *Manager) cachePut(ctx context.Context, ck certKey, tlscert *tls.Certificate) error {
516         if m.Cache == nil {
517                 return nil
518         }
519
520         // contains PEM-encoded data
521         var buf bytes.Buffer
522
523         // private
524         switch key := tlscert.PrivateKey.(type) {
525         case *ecdsa.PrivateKey:
526                 if err := encodeECDSAKey(&buf, key); err != nil {
527                         return err
528                 }
529         case *rsa.PrivateKey:
530                 b := x509.MarshalPKCS1PrivateKey(key)
531                 pb := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: b}
532                 if err := pem.Encode(&buf, pb); err != nil {
533                         return err
534                 }
535         default:
536                 return errors.New("acme/autocert: unknown private key type")
537         }
538
539         // public
540         for _, b := range tlscert.Certificate {
541                 pb := &pem.Block{Type: "CERTIFICATE", Bytes: b}
542                 if err := pem.Encode(&buf, pb); err != nil {
543                         return err
544                 }
545         }
546
547         return m.Cache.Put(ctx, ck.String(), buf.Bytes())
548 }
549
550 func encodeECDSAKey(w io.Writer, key *ecdsa.PrivateKey) error {
551         b, err := x509.MarshalECPrivateKey(key)
552         if err != nil {
553                 return err
554         }
555         pb := &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
556         return pem.Encode(w, pb)
557 }
558
559 // createCert starts the domain ownership verification and returns a certificate
560 // for that domain upon success.
561 //
562 // If the domain is already being verified, it waits for the existing verification to complete.
563 // Either way, createCert blocks for the duration of the whole process.
564 func (m *Manager) createCert(ctx context.Context, ck certKey) (*tls.Certificate, error) {
565         // TODO: maybe rewrite this whole piece using sync.Once
566         state, err := m.certState(ck)
567         if err != nil {
568                 return nil, err
569         }
570         // state may exist if another goroutine is already working on it
571         // in which case just wait for it to finish
572         if !state.locked {
573                 state.RLock()
574                 defer state.RUnlock()
575                 return state.tlscert()
576         }
577
578         // We are the first; state is locked.
579         // Unblock the readers when domain ownership is verified
580         // and we got the cert or the process failed.
581         defer state.Unlock()
582         state.locked = false
583
584         der, leaf, err := m.authorizedCert(ctx, state.key, ck)
585         if err != nil {
586                 // Remove the failed state after some time,
587                 // making the manager call createCert again on the following TLS hello.
588                 time.AfterFunc(createCertRetryAfter, func() {
589                         defer testDidRemoveState(ck)
590                         m.stateMu.Lock()
591                         defer m.stateMu.Unlock()
592                         // Verify the state hasn't changed and it's still invalid
593                         // before deleting.
594                         s, ok := m.state[ck]
595                         if !ok {
596                                 return
597                         }
598                         if _, err := validCert(ck, s.cert, s.key, m.now()); err == nil {
599                                 return
600                         }
601                         delete(m.state, ck)
602                 })
603                 return nil, err
604         }
605         state.cert = der
606         state.leaf = leaf
607         go m.renew(ck, state.key, state.leaf.NotAfter)
608         return state.tlscert()
609 }
610
611 // certState returns a new or existing certState.
612 // If a new certState is returned, state.exist is false and the state is locked.
613 // The returned error is non-nil only in the case where a new state could not be created.
614 func (m *Manager) certState(ck certKey) (*certState, error) {
615         m.stateMu.Lock()
616         defer m.stateMu.Unlock()
617         if m.state == nil {
618                 m.state = make(map[certKey]*certState)
619         }
620         // existing state
621         if state, ok := m.state[ck]; ok {
622                 return state, nil
623         }
624
625         // new locked state
626         var (
627                 err error
628                 key crypto.Signer
629         )
630         if ck.isRSA {
631                 key, err = rsa.GenerateKey(rand.Reader, 2048)
632         } else {
633                 key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
634         }
635         if err != nil {
636                 return nil, err
637         }
638
639         state := &certState{
640                 key:    key,
641                 locked: true,
642         }
643         state.Lock() // will be unlocked by m.certState caller
644         m.state[ck] = state
645         return state, nil
646 }
647
648 // authorizedCert starts the domain ownership verification process and requests a new cert upon success.
649 // The key argument is the certificate private key.
650 func (m *Manager) authorizedCert(ctx context.Context, key crypto.Signer, ck certKey) (der [][]byte, leaf *x509.Certificate, err error) {
651         client, err := m.acmeClient(ctx)
652         if err != nil {
653                 return nil, nil, err
654         }
655
656         if err := m.verify(ctx, client, ck.domain); err != nil {
657                 return nil, nil, err
658         }
659         csr, err := certRequest(key, ck.domain, m.ExtraExtensions)
660         if err != nil {
661                 return nil, nil, err
662         }
663         der, _, err = client.CreateCert(ctx, csr, 0, true)
664         if err != nil {
665                 return nil, nil, err
666         }
667         leaf, err = validCert(ck, der, key, m.now())
668         if err != nil {
669                 return nil, nil, err
670         }
671         return der, leaf, nil
672 }
673
674 // revokePendingAuthz revokes all authorizations idenfied by the elements of uri slice.
675 // It ignores revocation errors.
676 func (m *Manager) revokePendingAuthz(ctx context.Context, uri []string) {
677         client, err := m.acmeClient(ctx)
678         if err != nil {
679                 return
680         }
681         for _, u := range uri {
682                 client.RevokeAuthorization(ctx, u)
683         }
684 }
685
686 // verify runs the identifier (domain) authorization flow
687 // using each applicable ACME challenge type.
688 func (m *Manager) verify(ctx context.Context, client *acme.Client, domain string) error {
689         // The list of challenge types we'll try to fulfill
690         // in this specific order.
691         challengeTypes := []string{"tls-alpn-01", "tls-sni-02", "tls-sni-01"}
692         m.tokensMu.RLock()
693         if m.tryHTTP01 {
694                 challengeTypes = append(challengeTypes, "http-01")
695         }
696         m.tokensMu.RUnlock()
697
698         // Keep track of pending authzs and revoke the ones that did not validate.
699         pendingAuthzs := make(map[string]bool)
700         defer func() {
701                 var uri []string
702                 for k, pending := range pendingAuthzs {
703                         if pending {
704                                 uri = append(uri, k)
705                         }
706                 }
707                 if len(uri) > 0 {
708                         // Use "detached" background context.
709                         // The revocations need not happen in the current verification flow.
710                         go m.revokePendingAuthz(context.Background(), uri)
711                 }
712         }()
713
714         // errs accumulates challenge failure errors, printed if all fail
715         errs := make(map[*acme.Challenge]error)
716         var nextTyp int // challengeType index of the next challenge type to try
717         for {
718                 // Start domain authorization and get the challenge.
719                 authz, err := client.Authorize(ctx, domain)
720                 if err != nil {
721                         return err
722                 }
723                 // No point in accepting challenges if the authorization status
724                 // is in a final state.
725                 switch authz.Status {
726                 case acme.StatusValid:
727                         return nil // already authorized
728                 case acme.StatusInvalid:
729                         return fmt.Errorf("acme/autocert: invalid authorization %q", authz.URI)
730                 }
731
732                 pendingAuthzs[authz.URI] = true
733
734                 // Pick the next preferred challenge.
735                 var chal *acme.Challenge
736                 for chal == nil && nextTyp < len(challengeTypes) {
737                         chal = pickChallenge(challengeTypes[nextTyp], authz.Challenges)
738                         nextTyp++
739                 }
740                 if chal == nil {
741                         errorMsg := fmt.Sprintf("acme/autocert: unable to authorize %q", domain)
742                         for chal, err := range errs {
743                                 errorMsg += fmt.Sprintf("; challenge %q failed with error: %v", chal.Type, err)
744                         }
745                         return errors.New(errorMsg)
746                 }
747                 cleanup, err := m.fulfill(ctx, client, chal, domain)
748                 if err != nil {
749                         errs[chal] = err
750                         continue
751                 }
752                 defer cleanup()
753                 if _, err := client.Accept(ctx, chal); err != nil {
754                         errs[chal] = err
755                         continue
756                 }
757
758                 // A challenge is fulfilled and accepted: wait for the CA to validate.
759                 if _, err := client.WaitAuthorization(ctx, authz.URI); err != nil {
760                         errs[chal] = err
761                         continue
762                 }
763                 delete(pendingAuthzs, authz.URI)
764                 return nil
765         }
766 }
767
768 // fulfill provisions a response to the challenge chal.
769 // The cleanup is non-nil only if provisioning succeeded.
770 func (m *Manager) fulfill(ctx context.Context, client *acme.Client, chal *acme.Challenge, domain string) (cleanup func(), err error) {
771         switch chal.Type {
772         case "tls-alpn-01":
773                 cert, err := client.TLSALPN01ChallengeCert(chal.Token, domain)
774                 if err != nil {
775                         return nil, err
776                 }
777                 m.putCertToken(ctx, domain, &cert)
778                 return func() { go m.deleteCertToken(domain) }, nil
779         case "tls-sni-01":
780                 cert, name, err := client.TLSSNI01ChallengeCert(chal.Token)
781                 if err != nil {
782                         return nil, err
783                 }
784                 m.putCertToken(ctx, name, &cert)
785                 return func() { go m.deleteCertToken(name) }, nil
786         case "tls-sni-02":
787                 cert, name, err := client.TLSSNI02ChallengeCert(chal.Token)
788                 if err != nil {
789                         return nil, err
790                 }
791                 m.putCertToken(ctx, name, &cert)
792                 return func() { go m.deleteCertToken(name) }, nil
793         case "http-01":
794                 resp, err := client.HTTP01ChallengeResponse(chal.Token)
795                 if err != nil {
796                         return nil, err
797                 }
798                 p := client.HTTP01ChallengePath(chal.Token)
799                 m.putHTTPToken(ctx, p, resp)
800                 return func() { go m.deleteHTTPToken(p) }, nil
801         }
802         return nil, fmt.Errorf("acme/autocert: unknown challenge type %q", chal.Type)
803 }
804
805 func pickChallenge(typ string, chal []*acme.Challenge) *acme.Challenge {
806         for _, c := range chal {
807                 if c.Type == typ {
808                         return c
809                 }
810         }
811         return nil
812 }
813
814 // putCertToken stores the token certificate with the specified name
815 // in both m.certTokens map and m.Cache.
816 func (m *Manager) putCertToken(ctx context.Context, name string, cert *tls.Certificate) {
817         m.tokensMu.Lock()
818         defer m.tokensMu.Unlock()
819         if m.certTokens == nil {
820                 m.certTokens = make(map[string]*tls.Certificate)
821         }
822         m.certTokens[name] = cert
823         m.cachePut(ctx, certKey{domain: name, isToken: true}, cert)
824 }
825
826 // deleteCertToken removes the token certificate with the specified name
827 // from both m.certTokens map and m.Cache.
828 func (m *Manager) deleteCertToken(name string) {
829         m.tokensMu.Lock()
830         defer m.tokensMu.Unlock()
831         delete(m.certTokens, name)
832         if m.Cache != nil {
833                 ck := certKey{domain: name, isToken: true}
834                 m.Cache.Delete(context.Background(), ck.String())
835         }
836 }
837
838 // httpToken retrieves an existing http-01 token value from an in-memory map
839 // or the optional cache.
840 func (m *Manager) httpToken(ctx context.Context, tokenPath string) ([]byte, error) {
841         m.tokensMu.RLock()
842         defer m.tokensMu.RUnlock()
843         if v, ok := m.httpTokens[tokenPath]; ok {
844                 return v, nil
845         }
846         if m.Cache == nil {
847                 return nil, fmt.Errorf("acme/autocert: no token at %q", tokenPath)
848         }
849         return m.Cache.Get(ctx, httpTokenCacheKey(tokenPath))
850 }
851
852 // putHTTPToken stores an http-01 token value using tokenPath as key
853 // in both in-memory map and the optional Cache.
854 //
855 // It ignores any error returned from Cache.Put.
856 func (m *Manager) putHTTPToken(ctx context.Context, tokenPath, val string) {
857         m.tokensMu.Lock()
858         defer m.tokensMu.Unlock()
859         if m.httpTokens == nil {
860                 m.httpTokens = make(map[string][]byte)
861         }
862         b := []byte(val)
863         m.httpTokens[tokenPath] = b
864         if m.Cache != nil {
865                 m.Cache.Put(ctx, httpTokenCacheKey(tokenPath), b)
866         }
867 }
868
869 // deleteHTTPToken removes an http-01 token value from both in-memory map
870 // and the optional Cache, ignoring any error returned from the latter.
871 //
872 // If m.Cache is non-nil, it blocks until Cache.Delete returns without a timeout.
873 func (m *Manager) deleteHTTPToken(tokenPath string) {
874         m.tokensMu.Lock()
875         defer m.tokensMu.Unlock()
876         delete(m.httpTokens, tokenPath)
877         if m.Cache != nil {
878                 m.Cache.Delete(context.Background(), httpTokenCacheKey(tokenPath))
879         }
880 }
881
882 // httpTokenCacheKey returns a key at which an http-01 token value may be stored
883 // in the Manager's optional Cache.
884 func httpTokenCacheKey(tokenPath string) string {
885         return path.Base(tokenPath) + "+http-01"
886 }
887
888 // renew starts a cert renewal timer loop, one per domain.
889 //
890 // The loop is scheduled in two cases:
891 // - a cert was fetched from cache for the first time (wasn't in m.state)
892 // - a new cert was created by m.createCert
893 //
894 // The key argument is a certificate private key.
895 // The exp argument is the cert expiration time (NotAfter).
896 func (m *Manager) renew(ck certKey, key crypto.Signer, exp time.Time) {
897         m.renewalMu.Lock()
898         defer m.renewalMu.Unlock()
899         if m.renewal[ck] != nil {
900                 // another goroutine is already on it
901                 return
902         }
903         if m.renewal == nil {
904                 m.renewal = make(map[certKey]*domainRenewal)
905         }
906         dr := &domainRenewal{m: m, ck: ck, key: key}
907         m.renewal[ck] = dr
908         dr.start(exp)
909 }
910
911 // stopRenew stops all currently running cert renewal timers.
912 // The timers are not restarted during the lifetime of the Manager.
913 func (m *Manager) stopRenew() {
914         m.renewalMu.Lock()
915         defer m.renewalMu.Unlock()
916         for name, dr := range m.renewal {
917                 delete(m.renewal, name)
918                 dr.stop()
919         }
920 }
921
922 func (m *Manager) accountKey(ctx context.Context) (crypto.Signer, error) {
923         const keyName = "acme_account+key"
924
925         // Previous versions of autocert stored the value under a different key.
926         const legacyKeyName = "acme_account.key"
927
928         genKey := func() (*ecdsa.PrivateKey, error) {
929                 return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
930         }
931
932         if m.Cache == nil {
933                 return genKey()
934         }
935
936         data, err := m.Cache.Get(ctx, keyName)
937         if err == ErrCacheMiss {
938                 data, err = m.Cache.Get(ctx, legacyKeyName)
939         }
940         if err == ErrCacheMiss {
941                 key, err := genKey()
942                 if err != nil {
943                         return nil, err
944                 }
945                 var buf bytes.Buffer
946                 if err := encodeECDSAKey(&buf, key); err != nil {
947                         return nil, err
948                 }
949                 if err := m.Cache.Put(ctx, keyName, buf.Bytes()); err != nil {
950                         return nil, err
951                 }
952                 return key, nil
953         }
954         if err != nil {
955                 return nil, err
956         }
957
958         priv, _ := pem.Decode(data)
959         if priv == nil || !strings.Contains(priv.Type, "PRIVATE") {
960                 return nil, errors.New("acme/autocert: invalid account key found in cache")
961         }
962         return parsePrivateKey(priv.Bytes)
963 }
964
965 func (m *Manager) acmeClient(ctx context.Context) (*acme.Client, error) {
966         m.clientMu.Lock()
967         defer m.clientMu.Unlock()
968         if m.client != nil {
969                 return m.client, nil
970         }
971
972         client := m.Client
973         if client == nil {
974                 client = &acme.Client{DirectoryURL: acme.LetsEncryptURL}
975         }
976         if client.Key == nil {
977                 var err error
978                 client.Key, err = m.accountKey(ctx)
979                 if err != nil {
980                         return nil, err
981                 }
982         }
983         var contact []string
984         if m.Email != "" {
985                 contact = []string{"mailto:" + m.Email}
986         }
987         a := &acme.Account{Contact: contact}
988         _, err := client.Register(ctx, a, m.Prompt)
989         if ae, ok := err.(*acme.Error); err == nil || ok && ae.StatusCode == http.StatusConflict {
990                 // conflict indicates the key is already registered
991                 m.client = client
992                 err = nil
993         }
994         return m.client, err
995 }
996
997 func (m *Manager) hostPolicy() HostPolicy {
998         if m.HostPolicy != nil {
999                 return m.HostPolicy
1000         }
1001         return defaultHostPolicy
1002 }
1003
1004 func (m *Manager) renewBefore() time.Duration {
1005         if m.RenewBefore > renewJitter {
1006                 return m.RenewBefore
1007         }
1008         return 720 * time.Hour // 30 days
1009 }
1010
1011 func (m *Manager) now() time.Time {
1012         if m.nowFunc != nil {
1013                 return m.nowFunc()
1014         }
1015         return time.Now()
1016 }
1017
1018 // certState is ready when its mutex is unlocked for reading.
1019 type certState struct {
1020         sync.RWMutex
1021         locked bool              // locked for read/write
1022         key    crypto.Signer     // private key for cert
1023         cert   [][]byte          // DER encoding
1024         leaf   *x509.Certificate // parsed cert[0]; always non-nil if cert != nil
1025 }
1026
1027 // tlscert creates a tls.Certificate from s.key and s.cert.
1028 // Callers should wrap it in s.RLock() and s.RUnlock().
1029 func (s *certState) tlscert() (*tls.Certificate, error) {
1030         if s.key == nil {
1031                 return nil, errors.New("acme/autocert: missing signer")
1032         }
1033         if len(s.cert) == 0 {
1034                 return nil, errors.New("acme/autocert: missing certificate")
1035         }
1036         return &tls.Certificate{
1037                 PrivateKey:  s.key,
1038                 Certificate: s.cert,
1039                 Leaf:        s.leaf,
1040         }, nil
1041 }
1042
1043 // certRequest generates a CSR for the given common name cn and optional SANs.
1044 func certRequest(key crypto.Signer, cn string, ext []pkix.Extension, san ...string) ([]byte, error) {
1045         req := &x509.CertificateRequest{
1046                 Subject:         pkix.Name{CommonName: cn},
1047                 DNSNames:        san,
1048                 ExtraExtensions: ext,
1049         }
1050         return x509.CreateCertificateRequest(rand.Reader, req, key)
1051 }
1052
1053 // Attempt to parse the given private key DER block. OpenSSL 0.9.8 generates
1054 // PKCS#1 private keys by default, while OpenSSL 1.0.0 generates PKCS#8 keys.
1055 // OpenSSL ecparam generates SEC1 EC private keys for ECDSA. We try all three.
1056 //
1057 // Inspired by parsePrivateKey in crypto/tls/tls.go.
1058 func parsePrivateKey(der []byte) (crypto.Signer, error) {
1059         if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
1060                 return key, nil
1061         }
1062         if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
1063                 switch key := key.(type) {
1064                 case *rsa.PrivateKey:
1065                         return key, nil
1066                 case *ecdsa.PrivateKey:
1067                         return key, nil
1068                 default:
1069                         return nil, errors.New("acme/autocert: unknown private key type in PKCS#8 wrapping")
1070                 }
1071         }
1072         if key, err := x509.ParseECPrivateKey(der); err == nil {
1073                 return key, nil
1074         }
1075
1076         return nil, errors.New("acme/autocert: failed to parse private key")
1077 }
1078
1079 // validCert parses a cert chain provided as der argument and verifies the leaf and der[0]
1080 // correspond to the private key, the domain and key type match, and expiration dates
1081 // are valid. It doesn't do any revocation checking.
1082 //
1083 // The returned value is the verified leaf cert.
1084 func validCert(ck certKey, der [][]byte, key crypto.Signer, now time.Time) (leaf *x509.Certificate, err error) {
1085         // parse public part(s)
1086         var n int
1087         for _, b := range der {
1088                 n += len(b)
1089         }
1090         pub := make([]byte, n)
1091         n = 0
1092         for _, b := range der {
1093                 n += copy(pub[n:], b)
1094         }
1095         x509Cert, err := x509.ParseCertificates(pub)
1096         if err != nil || len(x509Cert) == 0 {
1097                 return nil, errors.New("acme/autocert: no public key found")
1098         }
1099         // verify the leaf is not expired and matches the domain name
1100         leaf = x509Cert[0]
1101         if now.Before(leaf.NotBefore) {
1102                 return nil, errors.New("acme/autocert: certificate is not valid yet")
1103         }
1104         if now.After(leaf.NotAfter) {
1105                 return nil, errors.New("acme/autocert: expired certificate")
1106         }
1107         if err := leaf.VerifyHostname(ck.domain); err != nil {
1108                 return nil, err
1109         }
1110         // ensure the leaf corresponds to the private key and matches the certKey type
1111         switch pub := leaf.PublicKey.(type) {
1112         case *rsa.PublicKey:
1113                 prv, ok := key.(*rsa.PrivateKey)
1114                 if !ok {
1115                         return nil, errors.New("acme/autocert: private key type does not match public key type")
1116                 }
1117                 if pub.N.Cmp(prv.N) != 0 {
1118                         return nil, errors.New("acme/autocert: private key does not match public key")
1119                 }
1120                 if !ck.isRSA && !ck.isToken {
1121                         return nil, errors.New("acme/autocert: key type does not match expected value")
1122                 }
1123         case *ecdsa.PublicKey:
1124                 prv, ok := key.(*ecdsa.PrivateKey)
1125                 if !ok {
1126                         return nil, errors.New("acme/autocert: private key type does not match public key type")
1127                 }
1128                 if pub.X.Cmp(prv.X) != 0 || pub.Y.Cmp(prv.Y) != 0 {
1129                         return nil, errors.New("acme/autocert: private key does not match public key")
1130                 }
1131                 if ck.isRSA && !ck.isToken {
1132                         return nil, errors.New("acme/autocert: key type does not match expected value")
1133                 }
1134         default:
1135                 return nil, errors.New("acme/autocert: unknown public key algorithm")
1136         }
1137         return leaf, nil
1138 }
1139
1140 type lockedMathRand struct {
1141         sync.Mutex
1142         rnd *mathrand.Rand
1143 }
1144
1145 func (r *lockedMathRand) int63n(max int64) int64 {
1146         r.Lock()
1147         n := r.rnd.Int63n(max)
1148         r.Unlock()
1149         return n
1150 }
1151
1152 // For easier testing.
1153 var (
1154         // Called when a state is removed.
1155         testDidRemoveState = func(certKey) {}
1156 )