autocert.go 30 KB

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