errors.go 11 KB

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