You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

httphandler.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 Matthew Holt
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package certmagic
  15. import (
  16. "net/http"
  17. "strings"
  18. "github.com/mholt/acmez/acme"
  19. "go.uber.org/zap"
  20. )
  21. // HTTPChallengeHandler wraps h in a handler that can solve the ACME
  22. // HTTP challenge. cfg is required, and it must have a certificate
  23. // cache backed by a functional storage facility, since that is where
  24. // the challenge state is stored between initiation and solution.
  25. //
  26. // If a request is not an ACME HTTP challenge, h will be invoked.
  27. func (am *ACMEManager) HTTPChallengeHandler(h http.Handler) http.Handler {
  28. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  29. if am.HandleHTTPChallenge(w, r) {
  30. return
  31. }
  32. h.ServeHTTP(w, r)
  33. })
  34. }
  35. // HandleHTTPChallenge uses am to solve challenge requests from an ACME
  36. // server that were initiated by this instance or any other instance in
  37. // this cluster (being, any instances using the same storage am does).
  38. //
  39. // If the HTTP challenge is disabled, this function is a no-op.
  40. //
  41. // If am is nil or if am does not have a certificate cache backed by
  42. // usable storage, solving the HTTP challenge will fail.
  43. //
  44. // It returns true if it handled the request; if so, the response has
  45. // already been written. If false is returned, this call was a no-op and
  46. // the request has not been handled.
  47. func (am *ACMEManager) HandleHTTPChallenge(w http.ResponseWriter, r *http.Request) bool {
  48. if am == nil {
  49. return false
  50. }
  51. if am.DisableHTTPChallenge {
  52. return false
  53. }
  54. if !LooksLikeHTTPChallenge(r) {
  55. return false
  56. }
  57. return am.distributedHTTPChallengeSolver(w, r)
  58. }
  59. // distributedHTTPChallengeSolver checks to see if this challenge
  60. // request was initiated by this or another instance which uses the
  61. // same storage as am does, and attempts to complete the challenge for
  62. // it. It returns true if the request was handled; false otherwise.
  63. func (am *ACMEManager) distributedHTTPChallengeSolver(w http.ResponseWriter, r *http.Request) bool {
  64. if am == nil {
  65. return false
  66. }
  67. host := hostOnly(r.Host)
  68. chalInfo, distributed, err := am.config.getChallengeInfo(host)
  69. if err != nil {
  70. if am.Logger != nil {
  71. am.Logger.Error("looking up info for HTTP challenge",
  72. zap.String("host", host),
  73. zap.Error(err))
  74. }
  75. return false
  76. }
  77. return solveHTTPChallenge(am.Logger, w, r, chalInfo.Challenge, distributed)
  78. }
  79. // solveHTTPChallenge solves the HTTP challenge using the given challenge information.
  80. // If the challenge is being solved in a distributed fahsion, set distributed to true for logging purposes.
  81. // It returns true the properties of the request check out in relation to the HTTP challenge.
  82. // Most of this code borrowed from xenolf's built-in HTTP-01 challenge solver in March 2018.
  83. func solveHTTPChallenge(logger *zap.Logger, w http.ResponseWriter, r *http.Request, challenge acme.Challenge, distributed bool) bool {
  84. challengeReqPath := challenge.HTTP01ResourcePath()
  85. if r.URL.Path == challengeReqPath &&
  86. strings.EqualFold(hostOnly(r.Host), challenge.Identifier.Value) && // mitigate DNS rebinding attacks
  87. r.Method == "GET" {
  88. w.Header().Add("Content-Type", "text/plain")
  89. w.Write([]byte(challenge.KeyAuthorization))
  90. r.Close = true
  91. if logger != nil {
  92. logger.Info("served key authentication",
  93. zap.String("identifier", challenge.Identifier.Value),
  94. zap.String("challenge", "http-01"),
  95. zap.String("remote", r.RemoteAddr),
  96. zap.Bool("distributed", distributed))
  97. }
  98. return true
  99. }
  100. return false
  101. }
  102. // SolveHTTPChallenge solves the HTTP challenge. It should be used only on HTTP requests that are
  103. // from ACME servers trying to validate an identifier (i.e. LooksLikeHTTPChallenge() == true). It
  104. // returns true if the request criteria check out and it answered with key authentication, in which
  105. // case no further handling of the request is necessary.
  106. func SolveHTTPChallenge(logger *zap.Logger, w http.ResponseWriter, r *http.Request, challenge acme.Challenge) bool {
  107. return solveHTTPChallenge(logger, w, r, challenge, false)
  108. }
  109. // LooksLikeHTTPChallenge returns true if r looks like an ACME
  110. // HTTP challenge request from an ACME server.
  111. func LooksLikeHTTPChallenge(r *http.Request) bool {
  112. return r.Method == "GET" && strings.HasPrefix(r.URL.Path, challengeBasePath)
  113. }
  114. const challengeBasePath = "/.well-known/acme-challenge"