Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

autocert.go 34KB

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