autocert.go 37 KB

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