errors.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package acme
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. )
  7. // ProblemType is the type of the ACME problem.
  8. type ProblemType int
  9. const (
  10. // ErrorAccountDoesNotExistType request specified an account that does not exist
  11. ErrorAccountDoesNotExistType ProblemType = iota
  12. // ErrorAlreadyRevokedType request specified a certificate to be revoked that has already been revoked
  13. ErrorAlreadyRevokedType
  14. // ErrorBadCSRType CSR is unacceptable (e.g., due to a short key)
  15. ErrorBadCSRType
  16. // ErrorBadNonceType client sent an unacceptable anti-replay nonce
  17. ErrorBadNonceType
  18. // ErrorBadPublicKeyType JWS was signed by a public key the server does not support
  19. ErrorBadPublicKeyType
  20. // ErrorBadRevocationReasonType revocation reason provided is not allowed by the server
  21. ErrorBadRevocationReasonType
  22. // ErrorBadSignatureAlgorithmType JWS was signed with an algorithm the server does not support
  23. ErrorBadSignatureAlgorithmType
  24. // ErrorCaaType Authority Authorization (CAA) records forbid the CA from issuing a certificate
  25. ErrorCaaType
  26. // ErrorCompoundType error conditions are indicated in the “subproblems” array.
  27. ErrorCompoundType
  28. // ErrorConnectionType server could not connect to validation target
  29. ErrorConnectionType
  30. // ErrorDNSType was a problem with a DNS query during identifier validation
  31. ErrorDNSType
  32. // ErrorExternalAccountRequiredType request must include a value for the “externalAccountBinding” field
  33. ErrorExternalAccountRequiredType
  34. // ErrorIncorrectResponseType received didn’t match the challenge’s requirements
  35. ErrorIncorrectResponseType
  36. // ErrorInvalidContactType URL for an account was invalid
  37. ErrorInvalidContactType
  38. // ErrorMalformedType request message was malformed
  39. ErrorMalformedType
  40. // ErrorOrderNotReadyType request attempted to finalize an order that is not ready to be finalized
  41. ErrorOrderNotReadyType
  42. // ErrorRateLimitedType request exceeds a rate limit
  43. ErrorRateLimitedType
  44. // ErrorRejectedIdentifierType server will not issue certificates for the identifier
  45. ErrorRejectedIdentifierType
  46. // ErrorServerInternalType server experienced an internal error
  47. ErrorServerInternalType
  48. // ErrorTLSType server received a TLS error during validation
  49. ErrorTLSType
  50. // ErrorUnauthorizedType client lacks sufficient authorization
  51. ErrorUnauthorizedType
  52. // ErrorUnsupportedContactType URL for an account used an unsupported protocol scheme
  53. ErrorUnsupportedContactType
  54. // ErrorUnsupportedIdentifierType identifier is of an unsupported type
  55. ErrorUnsupportedIdentifierType
  56. // ErrorUserActionRequiredType the “instance” URL and take actions specified there
  57. ErrorUserActionRequiredType
  58. // ErrorNotImplementedType operation is not implemented
  59. ErrorNotImplementedType
  60. )
  61. // String returns the string representation of the acme problem type,
  62. // fulfilling the Stringer interface.
  63. func (ap ProblemType) String() string {
  64. switch ap {
  65. case ErrorAccountDoesNotExistType:
  66. return "accountDoesNotExist"
  67. case ErrorAlreadyRevokedType:
  68. return "alreadyRevoked"
  69. case ErrorBadCSRType:
  70. return "badCSR"
  71. case ErrorBadNonceType:
  72. return "badNonce"
  73. case ErrorBadPublicKeyType:
  74. return "badPublicKey"
  75. case ErrorBadRevocationReasonType:
  76. return "badRevocationReason"
  77. case ErrorBadSignatureAlgorithmType:
  78. return "badSignatureAlgorithm"
  79. case ErrorCaaType:
  80. return "caa"
  81. case ErrorCompoundType:
  82. return "compound"
  83. case ErrorConnectionType:
  84. return "connection"
  85. case ErrorDNSType:
  86. return "dns"
  87. case ErrorExternalAccountRequiredType:
  88. return "externalAccountRequired"
  89. case ErrorInvalidContactType:
  90. return "incorrectResponse"
  91. case ErrorMalformedType:
  92. return "malformed"
  93. case ErrorOrderNotReadyType:
  94. return "orderNotReady"
  95. case ErrorRateLimitedType:
  96. return "rateLimited"
  97. case ErrorRejectedIdentifierType:
  98. return "rejectedIdentifier"
  99. case ErrorServerInternalType:
  100. return "serverInternal"
  101. case ErrorTLSType:
  102. return "tls"
  103. case ErrorUnauthorizedType:
  104. return "unauthorized"
  105. case ErrorUnsupportedContactType:
  106. return "unsupportedContact"
  107. case ErrorUnsupportedIdentifierType:
  108. return "unsupportedIdentifier"
  109. case ErrorUserActionRequiredType:
  110. return "userActionRequired"
  111. case ErrorNotImplementedType:
  112. return "notImplemented"
  113. default:
  114. return fmt.Sprintf("unsupported type ACME error type '%d'", int(ap))
  115. }
  116. }
  117. type errorMetadata struct {
  118. details string
  119. status int
  120. typ string
  121. String string
  122. }
  123. var (
  124. officialACMEPrefix = "urn:ietf:params: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. }
  324. // ToLog implements the EnableLogger interface.
  325. func (e *Error) ToLog() (interface{}, error) {
  326. b, err := json.Marshal(e)
  327. if err != nil {
  328. return nil, WrapErrorISE(err, "error marshaling acme.Error for logging")
  329. }
  330. return string(b), nil
  331. }