http.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Copyright 2018 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 acme
  5. import (
  6. "bytes"
  7. "context"
  8. "crypto"
  9. "crypto/rand"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "math/big"
  15. "net/http"
  16. "runtime/debug"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // retryTimer encapsulates common logic for retrying unsuccessful requests.
  22. // It is not safe for concurrent use.
  23. type retryTimer struct {
  24. // backoffFn provides backoff delay sequence for retries.
  25. // See Client.RetryBackoff doc comment.
  26. backoffFn func(n int, r *http.Request, res *http.Response) time.Duration
  27. // n is the current retry attempt.
  28. n int
  29. }
  30. func (t *retryTimer) inc() {
  31. t.n++
  32. }
  33. // backoff pauses the current goroutine as described in Client.RetryBackoff.
  34. func (t *retryTimer) backoff(ctx context.Context, r *http.Request, res *http.Response) error {
  35. d := t.backoffFn(t.n, r, res)
  36. if d <= 0 {
  37. return fmt.Errorf("acme: no more retries for %s; tried %d time(s)", r.URL, t.n)
  38. }
  39. wakeup := time.NewTimer(d)
  40. defer wakeup.Stop()
  41. select {
  42. case <-ctx.Done():
  43. return ctx.Err()
  44. case <-wakeup.C:
  45. return nil
  46. }
  47. }
  48. func (c *Client) retryTimer() *retryTimer {
  49. f := c.RetryBackoff
  50. if f == nil {
  51. f = defaultBackoff
  52. }
  53. return &retryTimer{backoffFn: f}
  54. }
  55. // defaultBackoff provides default Client.RetryBackoff implementation
  56. // using a truncated exponential backoff algorithm,
  57. // as described in Client.RetryBackoff.
  58. //
  59. // The n argument is always bounded between 1 and 30.
  60. // The returned value is always greater than 0.
  61. func defaultBackoff(n int, r *http.Request, res *http.Response) time.Duration {
  62. const maxVal = 10 * time.Second
  63. var jitter time.Duration
  64. if x, err := rand.Int(rand.Reader, big.NewInt(1000)); err == nil {
  65. // Set the minimum to 1ms to avoid a case where
  66. // an invalid Retry-After value is parsed into 0 below,
  67. // resulting in the 0 returned value which would unintentionally
  68. // stop the retries.
  69. jitter = (1 + time.Duration(x.Int64())) * time.Millisecond
  70. }
  71. if v, ok := res.Header["Retry-After"]; ok {
  72. return retryAfter(v[0]) + jitter
  73. }
  74. if n < 1 {
  75. n = 1
  76. }
  77. if n > 30 {
  78. n = 30
  79. }
  80. d := time.Duration(1<<uint(n-1))*time.Second + jitter
  81. return min(d, maxVal)
  82. }
  83. // retryAfter parses a Retry-After HTTP header value,
  84. // trying to convert v into an int (seconds) or use http.ParseTime otherwise.
  85. // It returns zero value if v cannot be parsed.
  86. func retryAfter(v string) time.Duration {
  87. if i, err := strconv.Atoi(v); err == nil {
  88. return time.Duration(i) * time.Second
  89. }
  90. t, err := http.ParseTime(v)
  91. if err != nil {
  92. return 0
  93. }
  94. return t.Sub(timeNow())
  95. }
  96. // resOkay is a function that reports whether the provided response is okay.
  97. // It is expected to keep the response body unread.
  98. type resOkay func(*http.Response) bool
  99. // wantStatus returns a function which reports whether the code
  100. // matches the status code of a response.
  101. func wantStatus(codes ...int) resOkay {
  102. return func(res *http.Response) bool {
  103. for _, code := range codes {
  104. if code == res.StatusCode {
  105. return true
  106. }
  107. }
  108. return false
  109. }
  110. }
  111. // get issues an unsigned GET request to the specified URL.
  112. // It returns a non-error value only when ok reports true.
  113. //
  114. // get retries unsuccessful attempts according to c.RetryBackoff
  115. // until the context is done or a non-retriable error is received.
  116. func (c *Client) get(ctx context.Context, url string, ok resOkay) (*http.Response, error) {
  117. retry := c.retryTimer()
  118. for {
  119. req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
  120. if err != nil {
  121. return nil, err
  122. }
  123. res, err := c.doNoRetry(ctx, req)
  124. switch {
  125. case err != nil:
  126. return nil, err
  127. case ok(res):
  128. return res, nil
  129. case isRetriable(res.StatusCode):
  130. retry.inc()
  131. resErr := responseError(res)
  132. res.Body.Close()
  133. // Ignore the error value from retry.backoff
  134. // and return the one from last retry, as received from the CA.
  135. if retry.backoff(ctx, req, res) != nil {
  136. return nil, resErr
  137. }
  138. default:
  139. defer res.Body.Close()
  140. return nil, responseError(res)
  141. }
  142. }
  143. }
  144. // postAsGet is POST-as-GET, a replacement for GET in RFC 8555
  145. // as described in https://tools.ietf.org/html/rfc8555#section-6.3.
  146. // It makes a POST request in KID form with zero JWS payload.
  147. // See nopayload doc comments in jws.go.
  148. func (c *Client) postAsGet(ctx context.Context, url string, ok resOkay) (*http.Response, error) {
  149. return c.post(ctx, nil, url, noPayload, ok)
  150. }
  151. // post issues a signed POST request in JWS format using the provided key
  152. // to the specified URL. If key is nil, c.Key is used instead.
  153. // It returns a non-error value only when ok reports true.
  154. //
  155. // post retries unsuccessful attempts according to c.RetryBackoff
  156. // until the context is done or a non-retriable error is received.
  157. // It uses postNoRetry to make individual requests.
  158. func (c *Client) post(ctx context.Context, key crypto.Signer, url string, body interface{}, ok resOkay) (*http.Response, error) {
  159. retry := c.retryTimer()
  160. for {
  161. res, req, err := c.postNoRetry(ctx, key, url, body)
  162. if err != nil {
  163. return nil, err
  164. }
  165. if ok(res) {
  166. return res, nil
  167. }
  168. resErr := responseError(res)
  169. res.Body.Close()
  170. switch {
  171. // Check for bad nonce before isRetriable because it may have been returned
  172. // with an unretriable response code such as 400 Bad Request.
  173. case isBadNonce(resErr):
  174. // Consider any previously stored nonce values to be invalid.
  175. c.clearNonces()
  176. case !isRetriable(res.StatusCode):
  177. return nil, resErr
  178. }
  179. retry.inc()
  180. // Ignore the error value from retry.backoff
  181. // and return the one from last retry, as received from the CA.
  182. if err := retry.backoff(ctx, req, res); err != nil {
  183. return nil, resErr
  184. }
  185. }
  186. }
  187. // postNoRetry signs the body with the given key and POSTs it to the provided url.
  188. // It is used by c.post to retry unsuccessful attempts.
  189. // The body argument must be JSON-serializable.
  190. //
  191. // If key argument is nil, c.Key is used to sign the request.
  192. // If key argument is nil and c.accountKID returns a non-zero keyID,
  193. // the request is sent in KID form. Otherwise, JWK form is used.
  194. //
  195. // In practice, when interfacing with RFC-compliant CAs most requests are sent in KID form
  196. // and JWK is used only when KID is unavailable: new account endpoint and certificate
  197. // revocation requests authenticated by a cert key.
  198. // See jwsEncodeJSON for other details.
  199. func (c *Client) postNoRetry(ctx context.Context, key crypto.Signer, url string, body interface{}) (*http.Response, *http.Request, error) {
  200. kid := noKeyID
  201. if key == nil {
  202. if c.Key == nil {
  203. return nil, nil, errors.New("acme: Client.Key must be populated to make POST requests")
  204. }
  205. key = c.Key
  206. kid = c.accountKID(ctx)
  207. }
  208. nonce, err := c.popNonce(ctx, url)
  209. if err != nil {
  210. return nil, nil, err
  211. }
  212. b, err := jwsEncodeJSON(body, key, kid, nonce, url)
  213. if err != nil {
  214. return nil, nil, err
  215. }
  216. req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(b))
  217. if err != nil {
  218. return nil, nil, err
  219. }
  220. req.Header.Set("Content-Type", "application/jose+json")
  221. res, err := c.doNoRetry(ctx, req)
  222. if err != nil {
  223. return nil, nil, err
  224. }
  225. c.addNonce(res.Header)
  226. return res, req, nil
  227. }
  228. // doNoRetry issues a request req, replacing its context (if any) with ctx.
  229. func (c *Client) doNoRetry(ctx context.Context, req *http.Request) (*http.Response, error) {
  230. req.Header.Set("User-Agent", c.userAgent())
  231. res, err := c.httpClient().Do(req.WithContext(ctx))
  232. if err != nil {
  233. select {
  234. case <-ctx.Done():
  235. // Prefer the unadorned context error.
  236. // (The acme package had tests assuming this, previously from ctxhttp's
  237. // behavior, predating net/http supporting contexts natively)
  238. // TODO(bradfitz): reconsider this in the future. But for now this
  239. // requires no test updates.
  240. return nil, ctx.Err()
  241. default:
  242. return nil, err
  243. }
  244. }
  245. return res, nil
  246. }
  247. func (c *Client) httpClient() *http.Client {
  248. if c.HTTPClient != nil {
  249. return c.HTTPClient
  250. }
  251. return http.DefaultClient
  252. }
  253. // packageVersion is the version of the module that contains this package, for
  254. // sending as part of the User-Agent header.
  255. var packageVersion string
  256. func init() {
  257. // Set packageVersion if the binary was built in modules mode and x/crypto
  258. // was not replaced with a different module.
  259. info, ok := debug.ReadBuildInfo()
  260. if !ok {
  261. return
  262. }
  263. for _, m := range info.Deps {
  264. if m.Path != "golang.org/x/crypto" {
  265. continue
  266. }
  267. if m.Replace == nil {
  268. packageVersion = m.Version
  269. }
  270. break
  271. }
  272. }
  273. // userAgent returns the User-Agent header value. It includes the package name,
  274. // the module version (if available), and the c.UserAgent value (if set).
  275. func (c *Client) userAgent() string {
  276. ua := "golang.org/x/crypto/acme"
  277. if packageVersion != "" {
  278. ua += "@" + packageVersion
  279. }
  280. if c.UserAgent != "" {
  281. ua = c.UserAgent + " " + ua
  282. }
  283. return ua
  284. }
  285. // isBadNonce reports whether err is an ACME "badnonce" error.
  286. func isBadNonce(err error) bool {
  287. // According to the spec badNonce is urn:ietf:params:acme:error:badNonce.
  288. // However, ACME servers in the wild return their versions of the error.
  289. // See https://tools.ietf.org/html/draft-ietf-acme-acme-02#section-5.4
  290. // and https://github.com/letsencrypt/boulder/blob/0e07eacb/docs/acme-divergences.md#section-66.
  291. ae, ok := err.(*Error)
  292. return ok && strings.HasSuffix(strings.ToLower(ae.ProblemType), ":badnonce")
  293. }
  294. // isRetriable reports whether a request can be retried
  295. // based on the response status code.
  296. //
  297. // Note that a "bad nonce" error is returned with a non-retriable 400 Bad Request code.
  298. // Callers should parse the response and check with isBadNonce.
  299. func isRetriable(code int) bool {
  300. return code <= 399 || code >= 500 || code == http.StatusTooManyRequests
  301. }
  302. // responseError creates an error of Error type from resp.
  303. func responseError(resp *http.Response) error {
  304. // don't care if ReadAll returns an error:
  305. // json.Unmarshal will fail in that case anyway
  306. b, _ := io.ReadAll(resp.Body)
  307. e := &wireError{Status: resp.StatusCode}
  308. if err := json.Unmarshal(b, e); err != nil {
  309. // this is not a regular error response:
  310. // populate detail with anything we received,
  311. // e.Status will already contain HTTP response code value
  312. e.Detail = string(b)
  313. if e.Detail == "" {
  314. e.Detail = resp.Status
  315. }
  316. }
  317. return e.error(resp.Header)
  318. }