errors.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 Error
  266. type Error struct {
  267. Type string `json:"type"`
  268. Detail string `json:"detail"`
  269. Subproblems []Subproblem `json:"subproblems,omitempty"`
  270. Err error `json:"-"`
  271. Status int `json:"-"`
  272. }
  273. // Subproblem represents an ACME subproblem. It's fairly
  274. // similar to an ACME error, but differs in that it can't
  275. // include subproblems itself, the error is reflected
  276. // in the Detail property and doesn't have a Status.
  277. type Subproblem struct {
  278. Type string `json:"type"`
  279. Detail string `json:"detail"`
  280. // The "identifier" field MUST NOT be present at the top level in ACME
  281. // problem documents. It can only be present in subproblems.
  282. // Subproblems need not all have the same type, and they do not need to
  283. // match the top level type.
  284. Identifier *Identifier `json:"identifier,omitempty"`
  285. }
  286. // NewError creates a new Error.
  287. func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
  288. return newError(pt, errors.Errorf(msg, args...))
  289. }
  290. // NewDetailedError creates a new Error that includes the error
  291. // message in the details, providing more information to the
  292. // ACME client.
  293. func NewDetailedError(pt ProblemType, msg string, args ...interface{}) *Error {
  294. return NewError(pt, msg, args...).withDetail()
  295. }
  296. func (e *Error) withDetail() *Error {
  297. if e == nil || e.Status >= 500 || e.Err == nil {
  298. return e
  299. }
  300. e.Detail = fmt.Sprintf("%s: %s", e.Detail, e.Err)
  301. return e
  302. }
  303. // AddSubproblems adds the Subproblems to Error. It
  304. // returns the Error, allowing for fluent addition.
  305. func (e *Error) AddSubproblems(subproblems ...Subproblem) *Error {
  306. e.Subproblems = append(e.Subproblems, subproblems...)
  307. return e
  308. }
  309. // NewSubproblem creates a new Subproblem. The msg and args
  310. // are used to create a new error, which is set as the Detail, allowing
  311. // for more detailed error messages to be returned to the ACME client.
  312. func NewSubproblem(pt ProblemType, msg string, args ...interface{}) Subproblem {
  313. e := newError(pt, fmt.Errorf(msg, args...))
  314. s := Subproblem{
  315. Type: e.Type,
  316. Detail: e.Err.Error(),
  317. }
  318. return s
  319. }
  320. // NewSubproblemWithIdentifier creates a new Subproblem with a specific ACME
  321. // Identifier. It calls NewSubproblem and sets the Identifier.
  322. func NewSubproblemWithIdentifier(pt ProblemType, identifier Identifier, msg string, args ...interface{}) Subproblem {
  323. s := NewSubproblem(pt, msg, args...)
  324. s.Identifier = &identifier
  325. return s
  326. }
  327. func newError(pt ProblemType, err error) *Error {
  328. meta, ok := errorMap[pt]
  329. if !ok {
  330. meta = errorServerInternalMetadata
  331. return &Error{
  332. Type: meta.typ,
  333. Detail: meta.details,
  334. Status: meta.status,
  335. Err: err,
  336. }
  337. }
  338. return &Error{
  339. Type: meta.typ,
  340. Detail: meta.details,
  341. Status: meta.status,
  342. Err: err,
  343. }
  344. }
  345. // NewErrorISE creates a new ErrorServerInternalType Error.
  346. func NewErrorISE(msg string, args ...interface{}) *Error {
  347. return NewError(ErrorServerInternalType, msg, args...)
  348. }
  349. // WrapError attempts to wrap the internal error.
  350. func WrapError(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  351. var e *Error
  352. switch {
  353. case err == nil:
  354. return nil
  355. case errors.As(err, &e):
  356. if e.Err == nil {
  357. e.Err = errors.Errorf(msg+"; "+e.Detail, args...)
  358. } else {
  359. e.Err = errors.Wrapf(e.Err, msg, args...)
  360. }
  361. return e
  362. default:
  363. return newError(typ, errors.Wrapf(err, msg, args...))
  364. }
  365. }
  366. func WrapDetailedError(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  367. return WrapError(typ, err, msg, args...).withDetail()
  368. }
  369. // WrapErrorISE shortcut to wrap an internal server error type.
  370. func WrapErrorISE(err error, msg string, args ...interface{}) *Error {
  371. return WrapError(ErrorServerInternalType, err, msg, args...)
  372. }
  373. // StatusCode returns the status code and implements the StatusCoder interface.
  374. func (e *Error) StatusCode() int {
  375. return e.Status
  376. }
  377. // Error implements the error interface.
  378. func (e *Error) Error() string {
  379. if e.Err == nil {
  380. return e.Detail
  381. }
  382. return e.Err.Error()
  383. }
  384. // Cause returns the internal error and implements the Causer interface.
  385. func (e *Error) Cause() error {
  386. if e.Err == nil {
  387. return errors.New(e.Detail)
  388. }
  389. return e.Err
  390. }
  391. // ToLog implements the EnableLogger interface.
  392. func (e *Error) ToLog() (interface{}, error) {
  393. b, err := json.Marshal(e)
  394. if err != nil {
  395. return nil, WrapErrorISE(err, "error marshaling acme.Error for logging")
  396. }
  397. return string(b), nil
  398. }
  399. // Render implements render.RenderableError for Error.
  400. func (e *Error) Render(w http.ResponseWriter, r *http.Request) {
  401. w.Header().Set("Content-Type", "application/problem+json")
  402. render.JSONStatus(w, r, e, e.StatusCode())
  403. }