errors.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package acme
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "github.com/pkg/errors"
  9. "github.com/smallstep/certificates/errs"
  10. "github.com/smallstep/certificates/logging"
  11. )
  12. // ProblemType is the type of the ACME problem.
  13. type ProblemType int
  14. const (
  15. // ErrorAccountDoesNotExistType request specified an account that does not exist
  16. ErrorAccountDoesNotExistType ProblemType = iota
  17. // ErrorAlreadyRevokedType request specified a certificate to be revoked that has already been revoked
  18. ErrorAlreadyRevokedType
  19. // ErrorBadCSRType CSR is unacceptable (e.g., due to a short key)
  20. ErrorBadCSRType
  21. // ErrorBadNonceType client sent an unacceptable anti-replay nonce
  22. ErrorBadNonceType
  23. // ErrorBadPublicKeyType JWS was signed by a public key the server does not support
  24. ErrorBadPublicKeyType
  25. // ErrorBadRevocationReasonType revocation reason provided is not allowed by the server
  26. ErrorBadRevocationReasonType
  27. // ErrorBadSignatureAlgorithmType JWS was signed with an algorithm the server does not support
  28. ErrorBadSignatureAlgorithmType
  29. // ErrorCaaType Authority Authorization (CAA) records forbid the CA from issuing a certificate
  30. ErrorCaaType
  31. // ErrorCompoundType error conditions are indicated in the “subproblems” array.
  32. ErrorCompoundType
  33. // ErrorConnectionType server could not connect to validation target
  34. ErrorConnectionType
  35. // ErrorDNSType was a problem with a DNS query during identifier validation
  36. ErrorDNSType
  37. // ErrorExternalAccountRequiredType request must include a value for the “externalAccountBinding” field
  38. ErrorExternalAccountRequiredType
  39. // ErrorIncorrectResponseType received didn’t match the challenge’s requirements
  40. ErrorIncorrectResponseType
  41. // ErrorInvalidContactType URL for an account was invalid
  42. ErrorInvalidContactType
  43. // ErrorMalformedType request message was malformed
  44. ErrorMalformedType
  45. // ErrorOrderNotReadyType request attempted to finalize an order that is not ready to be finalized
  46. ErrorOrderNotReadyType
  47. // ErrorRateLimitedType request exceeds a rate limit
  48. ErrorRateLimitedType
  49. // ErrorRejectedIdentifierType server will not issue certificates for the identifier
  50. ErrorRejectedIdentifierType
  51. // ErrorServerInternalType server experienced an internal error
  52. ErrorServerInternalType
  53. // ErrorTLSType server received a TLS error during validation
  54. ErrorTLSType
  55. // ErrorUnauthorizedType client lacks sufficient authorization
  56. ErrorUnauthorizedType
  57. // ErrorUnsupportedContactType URL for an account used an unsupported protocol scheme
  58. ErrorUnsupportedContactType
  59. // ErrorUnsupportedIdentifierType identifier is of an unsupported type
  60. ErrorUnsupportedIdentifierType
  61. // ErrorUserActionRequiredType the “instance” URL and take actions specified there
  62. ErrorUserActionRequiredType
  63. // ErrorNotImplementedType operation is not implemented
  64. ErrorNotImplementedType
  65. )
  66. // String returns the string representation of the acme problem type,
  67. // fulfilling the Stringer interface.
  68. func (ap ProblemType) String() string {
  69. switch ap {
  70. case ErrorAccountDoesNotExistType:
  71. return "accountDoesNotExist"
  72. case ErrorAlreadyRevokedType:
  73. return "alreadyRevoked"
  74. case ErrorBadCSRType:
  75. return "badCSR"
  76. case ErrorBadNonceType:
  77. return "badNonce"
  78. case ErrorBadPublicKeyType:
  79. return "badPublicKey"
  80. case ErrorBadRevocationReasonType:
  81. return "badRevocationReason"
  82. case ErrorBadSignatureAlgorithmType:
  83. return "badSignatureAlgorithm"
  84. case ErrorCaaType:
  85. return "caa"
  86. case ErrorCompoundType:
  87. return "compound"
  88. case ErrorConnectionType:
  89. return "connection"
  90. case ErrorDNSType:
  91. return "dns"
  92. case ErrorExternalAccountRequiredType:
  93. return "externalAccountRequired"
  94. case ErrorInvalidContactType:
  95. return "incorrectResponse"
  96. case ErrorMalformedType:
  97. return "malformed"
  98. case ErrorOrderNotReadyType:
  99. return "orderNotReady"
  100. case ErrorRateLimitedType:
  101. return "rateLimited"
  102. case ErrorRejectedIdentifierType:
  103. return "rejectedIdentifier"
  104. case ErrorServerInternalType:
  105. return "serverInternal"
  106. case ErrorTLSType:
  107. return "tls"
  108. case ErrorUnauthorizedType:
  109. return "unauthorized"
  110. case ErrorUnsupportedContactType:
  111. return "unsupportedContact"
  112. case ErrorUnsupportedIdentifierType:
  113. return "unsupportedIdentifier"
  114. case ErrorUserActionRequiredType:
  115. return "userActionRequired"
  116. case ErrorNotImplementedType:
  117. return "notImplemented"
  118. default:
  119. return fmt.Sprintf("unsupported type ACME error type '%d'", int(ap))
  120. }
  121. }
  122. type errorMetadata struct {
  123. details string
  124. status int
  125. typ string
  126. String string
  127. }
  128. var (
  129. officialACMEPrefix = "urn:ietf:params:acme:error:"
  130. errorServerInternalMetadata = errorMetadata{
  131. typ: officialACMEPrefix + ErrorServerInternalType.String(),
  132. details: "The server experienced an internal error",
  133. status: 500,
  134. }
  135. errorMap = map[ProblemType]errorMetadata{
  136. ErrorAccountDoesNotExistType: {
  137. typ: officialACMEPrefix + ErrorAccountDoesNotExistType.String(),
  138. details: "Account does not exist",
  139. status: 400,
  140. },
  141. ErrorAlreadyRevokedType: {
  142. typ: officialACMEPrefix + ErrorAlreadyRevokedType.String(),
  143. details: "Certificate already Revoked",
  144. status: 400,
  145. },
  146. ErrorBadCSRType: {
  147. typ: officialACMEPrefix + ErrorBadCSRType.String(),
  148. details: "The CSR is unacceptable",
  149. status: 400,
  150. },
  151. ErrorBadNonceType: {
  152. typ: officialACMEPrefix + ErrorBadNonceType.String(),
  153. details: "Unacceptable anti-replay nonce",
  154. status: 400,
  155. },
  156. ErrorBadPublicKeyType: {
  157. typ: officialACMEPrefix + ErrorBadPublicKeyType.String(),
  158. details: "The jws was signed by a public key the server does not support",
  159. status: 400,
  160. },
  161. ErrorBadRevocationReasonType: {
  162. typ: officialACMEPrefix + ErrorBadRevocationReasonType.String(),
  163. details: "The revocation reason provided is not allowed by the server",
  164. status: 400,
  165. },
  166. ErrorBadSignatureAlgorithmType: {
  167. typ: officialACMEPrefix + ErrorBadSignatureAlgorithmType.String(),
  168. details: "The JWS was signed with an algorithm the server does not support",
  169. status: 400,
  170. },
  171. ErrorCaaType: {
  172. typ: officialACMEPrefix + ErrorCaaType.String(),
  173. details: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
  174. status: 400,
  175. },
  176. ErrorCompoundType: {
  177. typ: officialACMEPrefix + ErrorCompoundType.String(),
  178. details: "Specific error conditions are indicated in the “subproblems” array",
  179. status: 400,
  180. },
  181. ErrorConnectionType: {
  182. typ: officialACMEPrefix + ErrorConnectionType.String(),
  183. details: "The server could not connect to validation target",
  184. status: 400,
  185. },
  186. ErrorDNSType: {
  187. typ: officialACMEPrefix + ErrorDNSType.String(),
  188. details: "There was a problem with a DNS query during identifier validation",
  189. status: 400,
  190. },
  191. ErrorExternalAccountRequiredType: {
  192. typ: officialACMEPrefix + ErrorExternalAccountRequiredType.String(),
  193. details: "The request must include a value for the \"externalAccountBinding\" field",
  194. status: 400,
  195. },
  196. ErrorIncorrectResponseType: {
  197. typ: officialACMEPrefix + ErrorIncorrectResponseType.String(),
  198. details: "Response received didn't match the challenge's requirements",
  199. status: 400,
  200. },
  201. ErrorInvalidContactType: {
  202. typ: officialACMEPrefix + ErrorInvalidContactType.String(),
  203. details: "A contact URL for an account was invalid",
  204. status: 400,
  205. },
  206. ErrorMalformedType: {
  207. typ: officialACMEPrefix + ErrorMalformedType.String(),
  208. details: "The request message was malformed",
  209. status: 400,
  210. },
  211. ErrorOrderNotReadyType: {
  212. typ: officialACMEPrefix + ErrorOrderNotReadyType.String(),
  213. details: "The request attempted to finalize an order that is not ready to be finalized",
  214. status: 400,
  215. },
  216. ErrorRateLimitedType: {
  217. typ: officialACMEPrefix + ErrorRateLimitedType.String(),
  218. details: "The request exceeds a rate limit",
  219. status: 400,
  220. },
  221. ErrorRejectedIdentifierType: {
  222. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  223. details: "The server will not issue certificates for the identifier",
  224. status: 400,
  225. },
  226. ErrorNotImplementedType: {
  227. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  228. details: "The requested operation is not implemented",
  229. status: 501,
  230. },
  231. ErrorTLSType: {
  232. typ: officialACMEPrefix + ErrorTLSType.String(),
  233. details: "The server received a TLS error during validation",
  234. status: 400,
  235. },
  236. ErrorUnauthorizedType: {
  237. typ: officialACMEPrefix + ErrorUnauthorizedType.String(),
  238. details: "The client lacks sufficient authorization",
  239. status: 401,
  240. },
  241. ErrorUnsupportedContactType: {
  242. typ: officialACMEPrefix + ErrorUnsupportedContactType.String(),
  243. details: "A contact URL for an account used an unsupported protocol scheme",
  244. status: 400,
  245. },
  246. ErrorUnsupportedIdentifierType: {
  247. typ: officialACMEPrefix + ErrorUnsupportedIdentifierType.String(),
  248. details: "An identifier is of an unsupported type",
  249. status: 400,
  250. },
  251. ErrorUserActionRequiredType: {
  252. typ: officialACMEPrefix + ErrorUserActionRequiredType.String(),
  253. details: "Visit the “instance” URL and take actions specified there",
  254. status: 400,
  255. },
  256. ErrorServerInternalType: errorServerInternalMetadata,
  257. }
  258. )
  259. // Error represents an ACME
  260. type Error struct {
  261. Type string `json:"type"`
  262. Detail string `json:"detail"`
  263. Subproblems []interface{} `json:"subproblems,omitempty"`
  264. Identifier interface{} `json:"identifier,omitempty"`
  265. Err error `json:"-"`
  266. Status int `json:"-"`
  267. }
  268. // NewError creates a new Error type.
  269. func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
  270. return newError(pt, errors.Errorf(msg, args...))
  271. }
  272. func newError(pt ProblemType, err error) *Error {
  273. meta, ok := errorMap[pt]
  274. if !ok {
  275. meta = errorServerInternalMetadata
  276. return &Error{
  277. Type: meta.typ,
  278. Detail: meta.details,
  279. Status: meta.status,
  280. Err: err,
  281. }
  282. }
  283. return &Error{
  284. Type: meta.typ,
  285. Detail: meta.details,
  286. Status: meta.status,
  287. Err: err,
  288. }
  289. }
  290. // NewErrorISE creates a new ErrorServerInternalType Error.
  291. func NewErrorISE(msg string, args ...interface{}) *Error {
  292. return NewError(ErrorServerInternalType, msg, args...)
  293. }
  294. // WrapError attempts to wrap the internal error.
  295. func WrapError(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  296. switch e := err.(type) {
  297. case nil:
  298. return nil
  299. case *Error:
  300. if e.Err == nil {
  301. e.Err = errors.Errorf(msg+"; "+e.Detail, args...)
  302. } else {
  303. e.Err = errors.Wrapf(e.Err, msg, args...)
  304. }
  305. return e
  306. default:
  307. return newError(typ, errors.Wrapf(err, msg, args...))
  308. }
  309. }
  310. // WrapErrorISE shortcut to wrap an internal server error type.
  311. func WrapErrorISE(err error, msg string, args ...interface{}) *Error {
  312. return WrapError(ErrorServerInternalType, err, msg, args...)
  313. }
  314. // StatusCode returns the status code and implements the StatusCoder interface.
  315. func (e *Error) StatusCode() int {
  316. return e.Status
  317. }
  318. // Error allows AError to implement the error interface.
  319. func (e *Error) Error() string {
  320. return e.Detail
  321. }
  322. // Cause returns the internal error and implements the Causer interface.
  323. func (e *Error) Cause() error {
  324. if e.Err == nil {
  325. return errors.New(e.Detail)
  326. }
  327. return e.Err
  328. }
  329. // ToLog implements the EnableLogger interface.
  330. func (e *Error) ToLog() (interface{}, error) {
  331. b, err := json.Marshal(e)
  332. if err != nil {
  333. return nil, WrapErrorISE(err, "error marshaling acme.Error for logging")
  334. }
  335. return string(b), nil
  336. }
  337. // WriteError writes to w a JSON representation of the given error.
  338. func WriteError(w http.ResponseWriter, err *Error) {
  339. w.Header().Set("Content-Type", "application/problem+json")
  340. w.WriteHeader(err.StatusCode())
  341. // Write errors in the response writer
  342. if rl, ok := w.(logging.ResponseLogger); ok {
  343. rl.WithFields(map[string]interface{}{
  344. "error": err.Err,
  345. })
  346. if os.Getenv("STEPDEBUG") == "1" {
  347. if e, ok := err.Err.(errs.StackTracer); ok {
  348. rl.WithFields(map[string]interface{}{
  349. "stack-trace": fmt.Sprintf("%+v", e),
  350. })
  351. }
  352. }
  353. }
  354. if err := json.NewEncoder(w).Encode(err); err != nil {
  355. log.Println(err)
  356. }
  357. }