4
0

errors.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package acme
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "runtime/debug"
  8. "github.com/pkg/errors"
  9. "github.com/smallstep/certificates/api/render"
  10. )
  11. // ProblemType is the type of the ACME problem.
  12. type ProblemType int
  13. const (
  14. // ErrorAccountDoesNotExistType request specified an account that does not exist
  15. ErrorAccountDoesNotExistType ProblemType = iota
  16. // ErrorAlreadyRevokedType request specified a certificate to be revoked that has already been revoked
  17. ErrorAlreadyRevokedType
  18. // ErrorBadAttestation WebAuthn attestation statement could not be verified
  19. ErrorBadAttestationStatement
  20. // ErrorBadCSRType CSR is unacceptable (e.g., due to a short key)
  21. ErrorBadCSRType
  22. // ErrorBadNonceType client sent an unacceptable anti-replay nonce
  23. ErrorBadNonceType
  24. // ErrorBadPublicKeyType JWS was signed by a public key the server does not support
  25. ErrorBadPublicKeyType
  26. // ErrorBadRevocationReasonType revocation reason provided is not allowed by the server
  27. ErrorBadRevocationReasonType
  28. // ErrorBadSignatureAlgorithmType JWS was signed with an algorithm the server does not support
  29. ErrorBadSignatureAlgorithmType
  30. // ErrorCaaType Authority Authorization (CAA) records forbid the CA from issuing a certificate
  31. ErrorCaaType
  32. // ErrorCompoundType error conditions are indicated in the “subproblems” array.
  33. ErrorCompoundType
  34. // ErrorConnectionType server could not connect to validation target
  35. ErrorConnectionType
  36. // ErrorDNSType was a problem with a DNS query during identifier validation
  37. ErrorDNSType
  38. // ErrorExternalAccountRequiredType request must include a value for the “externalAccountBinding” field
  39. ErrorExternalAccountRequiredType
  40. // ErrorIncorrectResponseType received didn’t match the challenge’s requirements
  41. ErrorIncorrectResponseType
  42. // ErrorInvalidContactType URL for an account was invalid
  43. ErrorInvalidContactType
  44. // ErrorMalformedType request message was malformed
  45. ErrorMalformedType
  46. // ErrorOrderNotReadyType request attempted to finalize an order that is not ready to be finalized
  47. ErrorOrderNotReadyType
  48. // ErrorRateLimitedType request exceeds a rate limit
  49. ErrorRateLimitedType
  50. // ErrorRejectedIdentifierType server will not issue certificates for the identifier
  51. ErrorRejectedIdentifierType
  52. // ErrorServerInternalType server experienced an internal error
  53. ErrorServerInternalType
  54. // ErrorTLSType server received a TLS error during validation
  55. ErrorTLSType
  56. // ErrorUnauthorizedType client lacks sufficient authorization
  57. ErrorUnauthorizedType
  58. // ErrorUnsupportedContactType URL for an account used an unsupported protocol scheme
  59. ErrorUnsupportedContactType
  60. // ErrorUnsupportedIdentifierType identifier is of an unsupported type
  61. ErrorUnsupportedIdentifierType
  62. // ErrorUserActionRequiredType the “instance” URL and take actions specified there
  63. ErrorUserActionRequiredType
  64. // ErrorNotImplementedType operation is not implemented
  65. ErrorNotImplementedType
  66. )
  67. // String returns the string representation of the acme problem type,
  68. // fulfilling the Stringer interface.
  69. func (ap ProblemType) String() string {
  70. switch ap {
  71. case ErrorAccountDoesNotExistType:
  72. return "accountDoesNotExist"
  73. case ErrorAlreadyRevokedType:
  74. return "alreadyRevoked"
  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. ErrorCaaType: {
  173. typ: officialACMEPrefix + ErrorCaaType.String(),
  174. details: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
  175. status: 400,
  176. },
  177. ErrorCompoundType: {
  178. typ: officialACMEPrefix + ErrorCompoundType.String(),
  179. details: "Specific error conditions are indicated in the “subproblems” array",
  180. status: 400,
  181. },
  182. ErrorConnectionType: {
  183. typ: officialACMEPrefix + ErrorConnectionType.String(),
  184. details: "The server could not connect to validation target",
  185. status: 400,
  186. },
  187. ErrorDNSType: {
  188. typ: officialACMEPrefix + ErrorDNSType.String(),
  189. details: "There was a problem with a DNS query during identifier validation",
  190. status: 400,
  191. },
  192. ErrorExternalAccountRequiredType: {
  193. typ: officialACMEPrefix + ErrorExternalAccountRequiredType.String(),
  194. details: "The request must include a value for the \"externalAccountBinding\" field",
  195. status: 400,
  196. },
  197. ErrorIncorrectResponseType: {
  198. typ: officialACMEPrefix + ErrorIncorrectResponseType.String(),
  199. details: "Response received didn't match the challenge's requirements",
  200. status: 400,
  201. },
  202. ErrorInvalidContactType: {
  203. typ: officialACMEPrefix + ErrorInvalidContactType.String(),
  204. details: "A contact URL for an account was invalid",
  205. status: 400,
  206. },
  207. ErrorMalformedType: {
  208. typ: officialACMEPrefix + ErrorMalformedType.String(),
  209. details: "The request message was malformed",
  210. status: 400,
  211. },
  212. ErrorOrderNotReadyType: {
  213. typ: officialACMEPrefix + ErrorOrderNotReadyType.String(),
  214. details: "The request attempted to finalize an order that is not ready to be finalized",
  215. status: 400,
  216. },
  217. ErrorRateLimitedType: {
  218. typ: officialACMEPrefix + ErrorRateLimitedType.String(),
  219. details: "The request exceeds a rate limit",
  220. status: 400,
  221. },
  222. ErrorRejectedIdentifierType: {
  223. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  224. details: "The server will not issue certificates for the identifier",
  225. status: 400,
  226. },
  227. ErrorNotImplementedType: {
  228. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  229. details: "The requested operation is not implemented",
  230. status: 501,
  231. },
  232. ErrorTLSType: {
  233. typ: officialACMEPrefix + ErrorTLSType.String(),
  234. details: "The server received a TLS error during validation",
  235. status: 400,
  236. },
  237. ErrorUnauthorizedType: {
  238. typ: officialACMEPrefix + ErrorUnauthorizedType.String(),
  239. details: "The client lacks sufficient authorization",
  240. status: 401,
  241. },
  242. ErrorUnsupportedContactType: {
  243. typ: officialACMEPrefix + ErrorUnsupportedContactType.String(),
  244. details: "A contact URL for an account used an unsupported protocol scheme",
  245. status: 400,
  246. },
  247. ErrorUnsupportedIdentifierType: {
  248. typ: officialACMEPrefix + ErrorUnsupportedIdentifierType.String(),
  249. details: "An identifier is of an unsupported type",
  250. status: 400,
  251. },
  252. ErrorUserActionRequiredType: {
  253. typ: officialACMEPrefix + ErrorUserActionRequiredType.String(),
  254. details: "Visit the “instance” URL and take actions specified there",
  255. status: 400,
  256. },
  257. ErrorServerInternalType: errorServerInternalMetadata,
  258. }
  259. )
  260. // Error represents an ACME
  261. type Error struct {
  262. Type string `json:"type"`
  263. Detail string `json:"detail"`
  264. Subproblems []interface{} `json:"subproblems,omitempty"`
  265. Identifier interface{} `json:"identifier,omitempty"`
  266. Err error `json:"-"`
  267. Status int `json:"-"`
  268. }
  269. // NewError creates a new Error type.
  270. func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
  271. fmt.Fprintf(os.Stderr, msg+"\n", args...)
  272. debug.PrintStack()
  273. return newError(pt, errors.Errorf(msg, args...))
  274. }
  275. func newError(pt ProblemType, err error) *Error {
  276. meta, ok := errorMap[pt]
  277. if !ok {
  278. meta = errorServerInternalMetadata
  279. return &Error{
  280. Type: meta.typ,
  281. Detail: meta.details,
  282. Status: meta.status,
  283. Err: err,
  284. }
  285. }
  286. return &Error{
  287. Type: meta.typ,
  288. Detail: meta.details,
  289. Status: meta.status,
  290. Err: err,
  291. }
  292. }
  293. // NewErrorISE creates a new ErrorServerInternalType Error.
  294. func NewErrorISE(msg string, args ...interface{}) *Error {
  295. fmt.Fprintf(os.Stderr, msg+"\n", args...)
  296. debug.PrintStack()
  297. return NewError(ErrorServerInternalType, msg, args...)
  298. }
  299. // WrapError attempts to wrap the internal error.
  300. func WrapError(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  301. switch e := err.(type) {
  302. case nil:
  303. return nil
  304. case *Error:
  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. fmt.Fprintf(os.Stderr, msg+"\n", args...)
  318. debug.PrintStack()
  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 allows AError to implement the error interface.
  326. func (e *Error) Error() string {
  327. return e.Detail
  328. }
  329. // Cause returns the internal error and implements the Causer interface.
  330. func (e *Error) Cause() error {
  331. if e.Err == nil {
  332. return errors.New(e.Detail)
  333. }
  334. return e.Err
  335. }
  336. // ToLog implements the EnableLogger interface.
  337. func (e *Error) ToLog() (interface{}, error) {
  338. b, err := json.Marshal(e)
  339. if err != nil {
  340. return nil, WrapErrorISE(err, "error marshaling acme.Error for logging")
  341. }
  342. return string(b), nil
  343. }
  344. // Render implements render.RenderableError for Error.
  345. func (e *Error) Render(w http.ResponseWriter) {
  346. w.Header().Set("Content-Type", "application/problem+json")
  347. render.JSONStatus(w, e, e.StatusCode())
  348. }