4
0

errors.go 12 KB

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