errors.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. // ErrorBadAttestationStatementType WebAuthn attestation statement could not be verified
  17. ErrorBadAttestationStatementType
  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. ErrorBadAttestationStatementType: {
  171. typ: officialACMEPrefix + ErrorBadAttestationStatementType.String(),
  172. details: "Attestation statement cannot be verified",
  173. status: 400,
  174. },
  175. ErrorCaaType: {
  176. typ: officialACMEPrefix + ErrorCaaType.String(),
  177. details: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
  178. status: 400,
  179. },
  180. ErrorCompoundType: {
  181. typ: officialACMEPrefix + ErrorCompoundType.String(),
  182. details: "Specific error conditions are indicated in the “subproblems” array",
  183. status: 400,
  184. },
  185. ErrorConnectionType: {
  186. typ: officialACMEPrefix + ErrorConnectionType.String(),
  187. details: "The server could not connect to validation target",
  188. status: 400,
  189. },
  190. ErrorDNSType: {
  191. typ: officialACMEPrefix + ErrorDNSType.String(),
  192. details: "There was a problem with a DNS query during identifier validation",
  193. status: 400,
  194. },
  195. ErrorExternalAccountRequiredType: {
  196. typ: officialACMEPrefix + ErrorExternalAccountRequiredType.String(),
  197. details: "The request must include a value for the \"externalAccountBinding\" field",
  198. status: 400,
  199. },
  200. ErrorIncorrectResponseType: {
  201. typ: officialACMEPrefix + ErrorIncorrectResponseType.String(),
  202. details: "Response received didn't match the challenge's requirements",
  203. status: 400,
  204. },
  205. ErrorInvalidContactType: {
  206. typ: officialACMEPrefix + ErrorInvalidContactType.String(),
  207. details: "A contact URL for an account was invalid",
  208. status: 400,
  209. },
  210. ErrorMalformedType: {
  211. typ: officialACMEPrefix + ErrorMalformedType.String(),
  212. details: "The request message was malformed",
  213. status: 400,
  214. },
  215. ErrorOrderNotReadyType: {
  216. typ: officialACMEPrefix + ErrorOrderNotReadyType.String(),
  217. details: "The request attempted to finalize an order that is not ready to be finalized",
  218. status: 400,
  219. },
  220. ErrorRateLimitedType: {
  221. typ: officialACMEPrefix + ErrorRateLimitedType.String(),
  222. details: "The request exceeds a rate limit",
  223. status: 400,
  224. },
  225. ErrorRejectedIdentifierType: {
  226. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  227. details: "The server will not issue certificates for the identifier",
  228. status: 400,
  229. },
  230. ErrorNotImplementedType: {
  231. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  232. details: "The requested operation is not implemented",
  233. status: 501,
  234. },
  235. ErrorTLSType: {
  236. typ: officialACMEPrefix + ErrorTLSType.String(),
  237. details: "The server received a TLS error during validation",
  238. status: 400,
  239. },
  240. ErrorUnauthorizedType: {
  241. typ: officialACMEPrefix + ErrorUnauthorizedType.String(),
  242. details: "The client lacks sufficient authorization",
  243. status: 401,
  244. },
  245. ErrorUnsupportedContactType: {
  246. typ: officialACMEPrefix + ErrorUnsupportedContactType.String(),
  247. details: "A contact URL for an account used an unsupported protocol scheme",
  248. status: 400,
  249. },
  250. ErrorUnsupportedIdentifierType: {
  251. typ: officialACMEPrefix + ErrorUnsupportedIdentifierType.String(),
  252. details: "An identifier is of an unsupported type",
  253. status: 400,
  254. },
  255. ErrorUserActionRequiredType: {
  256. typ: officialACMEPrefix + ErrorUserActionRequiredType.String(),
  257. details: "Visit the “instance” URL and take actions specified there",
  258. status: 400,
  259. },
  260. ErrorServerInternalType: errorServerInternalMetadata,
  261. }
  262. )
  263. // Error represents an ACME
  264. type Error struct {
  265. Type string `json:"type"`
  266. Detail string `json:"detail"`
  267. Subproblems []interface{} `json:"subproblems,omitempty"`
  268. Identifier interface{} `json:"identifier,omitempty"`
  269. Err error `json:"-"`
  270. Status int `json:"-"`
  271. }
  272. // NewError creates a new Error type.
  273. func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
  274. return newError(pt, errors.Errorf(msg, args...))
  275. }
  276. func newError(pt ProblemType, err error) *Error {
  277. meta, ok := errorMap[pt]
  278. if !ok {
  279. meta = errorServerInternalMetadata
  280. return &Error{
  281. Type: meta.typ,
  282. Detail: meta.details,
  283. Status: meta.status,
  284. Err: err,
  285. }
  286. }
  287. return &Error{
  288. Type: meta.typ,
  289. Detail: meta.details,
  290. Status: meta.status,
  291. Err: err,
  292. }
  293. }
  294. // NewErrorISE creates a new ErrorServerInternalType Error.
  295. func NewErrorISE(msg string, args ...interface{}) *Error {
  296. return NewError(ErrorServerInternalType, msg, args...)
  297. }
  298. // WrapError attempts to wrap the internal error.
  299. func WrapError(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  300. var e *Error
  301. switch {
  302. case err == nil:
  303. return nil
  304. case errors.As(err, &e):
  305. if e.Err == nil {
  306. e.Err = errors.Errorf(msg+"; "+e.Detail, args...)
  307. } else {
  308. e.Err = errors.Wrapf(e.Err, msg, args...)
  309. }
  310. return e
  311. default:
  312. return newError(typ, errors.Wrapf(err, msg, args...))
  313. }
  314. }
  315. // WrapErrorISE shortcut to wrap an internal server error type.
  316. func WrapErrorISE(err error, msg string, args ...interface{}) *Error {
  317. return WrapError(ErrorServerInternalType, err, msg, args...)
  318. }
  319. // StatusCode returns the status code and implements the StatusCoder interface.
  320. func (e *Error) StatusCode() int {
  321. return e.Status
  322. }
  323. // Error implements the error interface.
  324. func (e *Error) Error() string {
  325. if e.Err == nil {
  326. return e.Detail
  327. }
  328. return e.Err.Error()
  329. }
  330. // Cause returns the internal error and implements the Causer interface.
  331. func (e *Error) Cause() error {
  332. if e.Err == nil {
  333. return errors.New(e.Detail)
  334. }
  335. return e.Err
  336. }
  337. // ToLog implements the EnableLogger interface.
  338. func (e *Error) ToLog() (interface{}, error) {
  339. b, err := json.Marshal(e)
  340. if err != nil {
  341. return nil, WrapErrorISE(err, "error marshaling acme.Error for logging")
  342. }
  343. return string(b), nil
  344. }
  345. // Render implements render.RenderableError for Error.
  346. func (e *Error) Render(w http.ResponseWriter) {
  347. w.Header().Set("Content-Type", "application/problem+json")
  348. render.JSONStatus(w, e, e.StatusCode())
  349. }