http.go 9.8 KB

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