errors.go 11 KB

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