errors.go 12 KB

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