autocert.go 37 KB

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