autocert.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. // revokePending revokes all provided authorizations (passed as a map of URIs)
  503. func (m *Manager) revokePending(ctx context.Context, pendingAuthzURIs map[string]bool) {
  504. f := func(ctx context.Context) {
  505. for uri := range pendingAuthzURIs {
  506. m.Client.RevokeAuthorization(ctx, uri)
  507. }
  508. }
  509. if waitForRevocations {
  510. f(ctx)
  511. } else {
  512. go f(context.Background())
  513. }
  514. }
  515. // verify runs the identifier (domain) authorization flow
  516. // using each applicable ACME challenge type.
  517. func (m *Manager) verify(ctx context.Context, client *acme.Client, domain string) error {
  518. // The list of challenge types we'll try to fulfill
  519. // in this specific order.
  520. challengeTypes := []string{"tls-sni-02", "tls-sni-01"}
  521. m.tokensMu.RLock()
  522. if m.tryHTTP01 {
  523. challengeTypes = append(challengeTypes, "http-01")
  524. }
  525. m.tokensMu.RUnlock()
  526. // we keep track of pending authzs and revoke the ones that did not validate.
  527. pendingAuthzs := make(map[string]bool)
  528. defer m.revokePending(ctx, pendingAuthzs)
  529. var nextTyp int // challengeType index of the next challenge type to try
  530. for {
  531. // Start domain authorization and get the challenge.
  532. authz, err := client.Authorize(ctx, domain)
  533. if err != nil {
  534. return err
  535. }
  536. // No point in accepting challenges if the authorization status
  537. // is in a final state.
  538. switch authz.Status {
  539. case acme.StatusValid:
  540. return nil // already authorized
  541. case acme.StatusInvalid:
  542. return fmt.Errorf("acme/autocert: invalid authorization %q", authz.URI)
  543. }
  544. pendingAuthzs[authz.URI] = true
  545. // Pick the next preferred challenge.
  546. var chal *acme.Challenge
  547. for chal == nil && nextTyp < len(challengeTypes) {
  548. chal = pickChallenge(challengeTypes[nextTyp], authz.Challenges)
  549. nextTyp++
  550. }
  551. if chal == nil {
  552. return fmt.Errorf("acme/autocert: unable to authorize %q; tried %q", domain, challengeTypes)
  553. }
  554. cleanup, err := m.fulfill(ctx, client, chal)
  555. if err != nil {
  556. continue
  557. }
  558. defer cleanup()
  559. if _, err := client.Accept(ctx, chal); err != nil {
  560. continue
  561. }
  562. // A challenge is fulfilled and accepted: wait for the CA to validate.
  563. if _, err := client.WaitAuthorization(ctx, authz.URI); err == nil {
  564. delete(pendingAuthzs, authz.URI)
  565. return nil
  566. }
  567. }
  568. }
  569. // fulfill provisions a response to the challenge chal.
  570. // The cleanup is non-nil only if provisioning succeeded.
  571. func (m *Manager) fulfill(ctx context.Context, client *acme.Client, chal *acme.Challenge) (cleanup func(), err error) {
  572. switch chal.Type {
  573. case "tls-sni-01":
  574. cert, name, err := client.TLSSNI01ChallengeCert(chal.Token)
  575. if err != nil {
  576. return nil, err
  577. }
  578. m.putCertToken(ctx, name, &cert)
  579. return func() { go m.deleteCertToken(name) }, nil
  580. case "tls-sni-02":
  581. cert, name, err := client.TLSSNI02ChallengeCert(chal.Token)
  582. if err != nil {
  583. return nil, err
  584. }
  585. m.putCertToken(ctx, name, &cert)
  586. return func() { go m.deleteCertToken(name) }, nil
  587. case "http-01":
  588. resp, err := client.HTTP01ChallengeResponse(chal.Token)
  589. if err != nil {
  590. return nil, err
  591. }
  592. p := client.HTTP01ChallengePath(chal.Token)
  593. m.putHTTPToken(ctx, p, resp)
  594. return func() { go m.deleteHTTPToken(p) }, nil
  595. }
  596. return nil, fmt.Errorf("acme/autocert: unknown challenge type %q", chal.Type)
  597. }
  598. func pickChallenge(typ string, chal []*acme.Challenge) *acme.Challenge {
  599. for _, c := range chal {
  600. if c.Type == typ {
  601. return c
  602. }
  603. }
  604. return nil
  605. }
  606. // putCertToken stores the cert under the named key in both m.certTokens map
  607. // and m.Cache.
  608. func (m *Manager) putCertToken(ctx context.Context, name string, cert *tls.Certificate) {
  609. m.tokensMu.Lock()
  610. defer m.tokensMu.Unlock()
  611. if m.certTokens == nil {
  612. m.certTokens = make(map[string]*tls.Certificate)
  613. }
  614. m.certTokens[name] = cert
  615. m.cachePut(ctx, name, cert)
  616. }
  617. // deleteCertToken removes the token certificate for the specified domain name
  618. // from both m.certTokens map and m.Cache.
  619. func (m *Manager) deleteCertToken(name string) {
  620. m.tokensMu.Lock()
  621. defer m.tokensMu.Unlock()
  622. delete(m.certTokens, name)
  623. if m.Cache != nil {
  624. m.Cache.Delete(context.Background(), name)
  625. }
  626. }
  627. // httpToken retrieves an existing http-01 token value from an in-memory map
  628. // or the optional cache.
  629. func (m *Manager) httpToken(ctx context.Context, tokenPath string) ([]byte, error) {
  630. m.tokensMu.RLock()
  631. defer m.tokensMu.RUnlock()
  632. if v, ok := m.httpTokens[tokenPath]; ok {
  633. return v, nil
  634. }
  635. if m.Cache == nil {
  636. return nil, fmt.Errorf("acme/autocert: no token at %q", tokenPath)
  637. }
  638. return m.Cache.Get(ctx, httpTokenCacheKey(tokenPath))
  639. }
  640. // putHTTPToken stores an http-01 token value using tokenPath as key
  641. // in both in-memory map and the optional Cache.
  642. //
  643. // It ignores any error returned from Cache.Put.
  644. func (m *Manager) putHTTPToken(ctx context.Context, tokenPath, val string) {
  645. m.tokensMu.Lock()
  646. defer m.tokensMu.Unlock()
  647. if m.httpTokens == nil {
  648. m.httpTokens = make(map[string][]byte)
  649. }
  650. b := []byte(val)
  651. m.httpTokens[tokenPath] = b
  652. if m.Cache != nil {
  653. m.Cache.Put(ctx, httpTokenCacheKey(tokenPath), b)
  654. }
  655. }
  656. // deleteHTTPToken removes an http-01 token value from both in-memory map
  657. // and the optional Cache, ignoring any error returned from the latter.
  658. //
  659. // If m.Cache is non-nil, it blocks until Cache.Delete returns without a timeout.
  660. func (m *Manager) deleteHTTPToken(tokenPath string) {
  661. m.tokensMu.Lock()
  662. defer m.tokensMu.Unlock()
  663. delete(m.httpTokens, tokenPath)
  664. if m.Cache != nil {
  665. m.Cache.Delete(context.Background(), httpTokenCacheKey(tokenPath))
  666. }
  667. }
  668. // httpTokenCacheKey returns a key at which an http-01 token value may be stored
  669. // in the Manager's optional Cache.
  670. func httpTokenCacheKey(tokenPath string) string {
  671. return "http-01-" + path.Base(tokenPath)
  672. }
  673. // renew starts a cert renewal timer loop, one per domain.
  674. //
  675. // The loop is scheduled in two cases:
  676. // - a cert was fetched from cache for the first time (wasn't in m.state)
  677. // - a new cert was created by m.createCert
  678. //
  679. // The key argument is a certificate private key.
  680. // The exp argument is the cert expiration time (NotAfter).
  681. func (m *Manager) renew(domain string, key crypto.Signer, exp time.Time) {
  682. m.renewalMu.Lock()
  683. defer m.renewalMu.Unlock()
  684. if m.renewal[domain] != nil {
  685. // another goroutine is already on it
  686. return
  687. }
  688. if m.renewal == nil {
  689. m.renewal = make(map[string]*domainRenewal)
  690. }
  691. dr := &domainRenewal{m: m, domain: domain, key: key}
  692. m.renewal[domain] = dr
  693. dr.start(exp)
  694. }
  695. // stopRenew stops all currently running cert renewal timers.
  696. // The timers are not restarted during the lifetime of the Manager.
  697. func (m *Manager) stopRenew() {
  698. m.renewalMu.Lock()
  699. defer m.renewalMu.Unlock()
  700. for name, dr := range m.renewal {
  701. delete(m.renewal, name)
  702. dr.stop()
  703. }
  704. }
  705. func (m *Manager) accountKey(ctx context.Context) (crypto.Signer, error) {
  706. const keyName = "acme_account.key"
  707. genKey := func() (*ecdsa.PrivateKey, error) {
  708. return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  709. }
  710. if m.Cache == nil {
  711. return genKey()
  712. }
  713. data, err := m.Cache.Get(ctx, keyName)
  714. if err == ErrCacheMiss {
  715. key, err := genKey()
  716. if err != nil {
  717. return nil, err
  718. }
  719. var buf bytes.Buffer
  720. if err := encodeECDSAKey(&buf, key); err != nil {
  721. return nil, err
  722. }
  723. if err := m.Cache.Put(ctx, keyName, buf.Bytes()); err != nil {
  724. return nil, err
  725. }
  726. return key, nil
  727. }
  728. if err != nil {
  729. return nil, err
  730. }
  731. priv, _ := pem.Decode(data)
  732. if priv == nil || !strings.Contains(priv.Type, "PRIVATE") {
  733. return nil, errors.New("acme/autocert: invalid account key found in cache")
  734. }
  735. return parsePrivateKey(priv.Bytes)
  736. }
  737. func (m *Manager) acmeClient(ctx context.Context) (*acme.Client, error) {
  738. m.clientMu.Lock()
  739. defer m.clientMu.Unlock()
  740. if m.client != nil {
  741. return m.client, nil
  742. }
  743. client := m.Client
  744. if client == nil {
  745. client = &acme.Client{DirectoryURL: acme.LetsEncryptURL}
  746. }
  747. if client.Key == nil {
  748. var err error
  749. client.Key, err = m.accountKey(ctx)
  750. if err != nil {
  751. return nil, err
  752. }
  753. }
  754. var contact []string
  755. if m.Email != "" {
  756. contact = []string{"mailto:" + m.Email}
  757. }
  758. a := &acme.Account{Contact: contact}
  759. _, err := client.Register(ctx, a, m.Prompt)
  760. if ae, ok := err.(*acme.Error); err == nil || ok && ae.StatusCode == http.StatusConflict {
  761. // conflict indicates the key is already registered
  762. m.client = client
  763. err = nil
  764. }
  765. return m.client, err
  766. }
  767. func (m *Manager) hostPolicy() HostPolicy {
  768. if m.HostPolicy != nil {
  769. return m.HostPolicy
  770. }
  771. return defaultHostPolicy
  772. }
  773. func (m *Manager) renewBefore() time.Duration {
  774. if m.RenewBefore > renewJitter {
  775. return m.RenewBefore
  776. }
  777. return 720 * time.Hour // 30 days
  778. }
  779. // certState is ready when its mutex is unlocked for reading.
  780. type certState struct {
  781. sync.RWMutex
  782. locked bool // locked for read/write
  783. key crypto.Signer // private key for cert
  784. cert [][]byte // DER encoding
  785. leaf *x509.Certificate // parsed cert[0]; always non-nil if cert != nil
  786. }
  787. // tlscert creates a tls.Certificate from s.key and s.cert.
  788. // Callers should wrap it in s.RLock() and s.RUnlock().
  789. func (s *certState) tlscert() (*tls.Certificate, error) {
  790. if s.key == nil {
  791. return nil, errors.New("acme/autocert: missing signer")
  792. }
  793. if len(s.cert) == 0 {
  794. return nil, errors.New("acme/autocert: missing certificate")
  795. }
  796. return &tls.Certificate{
  797. PrivateKey: s.key,
  798. Certificate: s.cert,
  799. Leaf: s.leaf,
  800. }, nil
  801. }
  802. // certRequest generates a CSR for the given common name cn and optional SANs.
  803. func certRequest(key crypto.Signer, cn string, ext []pkix.Extension, san ...string) ([]byte, error) {
  804. req := &x509.CertificateRequest{
  805. Subject: pkix.Name{CommonName: cn},
  806. DNSNames: san,
  807. ExtraExtensions: ext,
  808. }
  809. return x509.CreateCertificateRequest(rand.Reader, req, key)
  810. }
  811. // Attempt to parse the given private key DER block. OpenSSL 0.9.8 generates
  812. // PKCS#1 private keys by default, while OpenSSL 1.0.0 generates PKCS#8 keys.
  813. // OpenSSL ecparam generates SEC1 EC private keys for ECDSA. We try all three.
  814. //
  815. // Inspired by parsePrivateKey in crypto/tls/tls.go.
  816. func parsePrivateKey(der []byte) (crypto.Signer, error) {
  817. if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
  818. return key, nil
  819. }
  820. if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
  821. switch key := key.(type) {
  822. case *rsa.PrivateKey:
  823. return key, nil
  824. case *ecdsa.PrivateKey:
  825. return key, nil
  826. default:
  827. return nil, errors.New("acme/autocert: unknown private key type in PKCS#8 wrapping")
  828. }
  829. }
  830. if key, err := x509.ParseECPrivateKey(der); err == nil {
  831. return key, nil
  832. }
  833. return nil, errors.New("acme/autocert: failed to parse private key")
  834. }
  835. // validCert parses a cert chain provided as der argument and verifies the leaf, der[0],
  836. // corresponds to the private key, as well as the domain match and expiration dates.
  837. // It doesn't do any revocation checking.
  838. //
  839. // The returned value is the verified leaf cert.
  840. func validCert(domain string, der [][]byte, key crypto.Signer) (leaf *x509.Certificate, err error) {
  841. // parse public part(s)
  842. var n int
  843. for _, b := range der {
  844. n += len(b)
  845. }
  846. pub := make([]byte, n)
  847. n = 0
  848. for _, b := range der {
  849. n += copy(pub[n:], b)
  850. }
  851. x509Cert, err := x509.ParseCertificates(pub)
  852. if len(x509Cert) == 0 {
  853. return nil, errors.New("acme/autocert: no public key found")
  854. }
  855. // verify the leaf is not expired and matches the domain name
  856. leaf = x509Cert[0]
  857. now := timeNow()
  858. if now.Before(leaf.NotBefore) {
  859. return nil, errors.New("acme/autocert: certificate is not valid yet")
  860. }
  861. if now.After(leaf.NotAfter) {
  862. return nil, errors.New("acme/autocert: expired certificate")
  863. }
  864. if err := leaf.VerifyHostname(domain); err != nil {
  865. return nil, err
  866. }
  867. // ensure the leaf corresponds to the private key
  868. switch pub := leaf.PublicKey.(type) {
  869. case *rsa.PublicKey:
  870. prv, ok := key.(*rsa.PrivateKey)
  871. if !ok {
  872. return nil, errors.New("acme/autocert: private key type does not match public key type")
  873. }
  874. if pub.N.Cmp(prv.N) != 0 {
  875. return nil, errors.New("acme/autocert: private key does not match public key")
  876. }
  877. case *ecdsa.PublicKey:
  878. prv, ok := key.(*ecdsa.PrivateKey)
  879. if !ok {
  880. return nil, errors.New("acme/autocert: private key type does not match public key type")
  881. }
  882. if pub.X.Cmp(prv.X) != 0 || pub.Y.Cmp(prv.Y) != 0 {
  883. return nil, errors.New("acme/autocert: private key does not match public key")
  884. }
  885. default:
  886. return nil, errors.New("acme/autocert: unknown public key algorithm")
  887. }
  888. return leaf, nil
  889. }
  890. type lockedMathRand struct {
  891. sync.Mutex
  892. rnd *mathrand.Rand
  893. }
  894. func (r *lockedMathRand) int63n(max int64) int64 {
  895. r.Lock()
  896. n := r.rnd.Int63n(max)
  897. r.Unlock()
  898. return n
  899. }
  900. // For easier testing.
  901. var (
  902. timeNow = time.Now
  903. // Called when a state is removed.
  904. testDidRemoveState = func(domain string) {}
  905. // make testing of revokePending synchronous
  906. waitForRevocations = false
  907. )