errors.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package acme
  2. import (
  3. "github.com/pkg/errors"
  4. )
  5. // AccountDoesNotExistErr returns a new acme error.
  6. func AccountDoesNotExistErr(err error) *Error {
  7. return &Error{
  8. Type: accountDoesNotExistErr,
  9. Detail: "Account does not exist",
  10. Status: 404,
  11. Err: err,
  12. }
  13. }
  14. // AlreadyRevokedErr returns a new acme error.
  15. func AlreadyRevokedErr(err error) *Error {
  16. return &Error{
  17. Type: alreadyRevokedErr,
  18. Detail: "Certificate already revoked",
  19. Status: 400,
  20. Err: err,
  21. }
  22. }
  23. // BadCSRErr returns a new acme error.
  24. func BadCSRErr(err error) *Error {
  25. return &Error{
  26. Type: badCSRErr,
  27. Detail: "The CSR is unacceptable",
  28. Status: 400,
  29. Err: err,
  30. }
  31. }
  32. // BadNonceErr returns a new acme error.
  33. func BadNonceErr(err error) *Error {
  34. return &Error{
  35. Type: badNonceErr,
  36. Detail: "Unacceptable anti-replay nonce",
  37. Status: 400,
  38. Err: err,
  39. }
  40. }
  41. // BadPublicKeyErr returns a new acme error.
  42. func BadPublicKeyErr(err error) *Error {
  43. return &Error{
  44. Type: badPublicKeyErr,
  45. Detail: "The jws was signed by a public key the server does not support",
  46. Status: 400,
  47. Err: err,
  48. }
  49. }
  50. // BadRevocationReasonErr returns a new acme error.
  51. func BadRevocationReasonErr(err error) *Error {
  52. return &Error{
  53. Type: badRevocationReasonErr,
  54. Detail: "The revocation reason provided is not allowed by the server",
  55. Status: 400,
  56. Err: err,
  57. }
  58. }
  59. // BadSignatureAlgorithmErr returns a new acme error.
  60. func BadSignatureAlgorithmErr(err error) *Error {
  61. return &Error{
  62. Type: badSignatureAlgorithmErr,
  63. Detail: "The JWS was signed with an algorithm the server does not support",
  64. Status: 400,
  65. Err: err,
  66. }
  67. }
  68. // CaaErr returns a new acme error.
  69. func CaaErr(err error) *Error {
  70. return &Error{
  71. Type: caaErr,
  72. Detail: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
  73. Status: 400,
  74. Err: err,
  75. }
  76. }
  77. // CompoundErr returns a new acme error.
  78. func CompoundErr(err error) *Error {
  79. return &Error{
  80. Type: compoundErr,
  81. Detail: "Specific error conditions are indicated in the “subproblems” array",
  82. Status: 400,
  83. Err: err,
  84. }
  85. }
  86. // ConnectionErr returns a new acme error.
  87. func ConnectionErr(err error) *Error {
  88. return &Error{
  89. Type: connectionErr,
  90. Detail: "The server could not connect to validation target",
  91. Status: 400,
  92. Err: err,
  93. }
  94. }
  95. // DNSErr returns a new acme error.
  96. func DNSErr(err error) *Error {
  97. return &Error{
  98. Type: dnsErr,
  99. Detail: "There was a problem with a DNS query during identifier validation",
  100. Status: 400,
  101. Err: err,
  102. }
  103. }
  104. // ExternalAccountRequiredErr returns a new acme error.
  105. func ExternalAccountRequiredErr(err error) *Error {
  106. return &Error{
  107. Type: externalAccountRequiredErr,
  108. Detail: "The request must include a value for the \"externalAccountBinding\" field",
  109. Status: 400,
  110. Err: err,
  111. }
  112. }
  113. // IncorrectResponseErr returns a new acme error.
  114. func IncorrectResponseErr(err error) *Error {
  115. return &Error{
  116. Type: incorrectResponseErr,
  117. Detail: "Response received didn't match the challenge's requirements",
  118. Status: 400,
  119. Err: err,
  120. }
  121. }
  122. // InvalidContactErr returns a new acme error.
  123. func InvalidContactErr(err error) *Error {
  124. return &Error{
  125. Type: invalidContactErr,
  126. Detail: "A contact URL for an account was invalid",
  127. Status: 400,
  128. Err: err,
  129. }
  130. }
  131. // MalformedErr returns a new acme error.
  132. func MalformedErr(err error) *Error {
  133. return &Error{
  134. Type: malformedErr,
  135. Detail: "The request message was malformed",
  136. Status: 400,
  137. Err: err,
  138. }
  139. }
  140. // OrderNotReadyErr returns a new acme error.
  141. func OrderNotReadyErr(err error) *Error {
  142. return &Error{
  143. Type: orderNotReadyErr,
  144. Detail: "The request attempted to finalize an order that is not ready to be finalized",
  145. Status: 400,
  146. Err: err,
  147. }
  148. }
  149. // RateLimitedErr returns a new acme error.
  150. func RateLimitedErr(err error) *Error {
  151. return &Error{
  152. Type: rateLimitedErr,
  153. Detail: "The request exceeds a rate limit",
  154. Status: 400,
  155. Err: err,
  156. }
  157. }
  158. // RejectedIdentifierErr returns a new acme error.
  159. func RejectedIdentifierErr(err error) *Error {
  160. return &Error{
  161. Type: rejectedIdentifierErr,
  162. Detail: "The server will not issue certificates for the identifier",
  163. Status: 400,
  164. Err: err,
  165. }
  166. }
  167. // ServerInternalErr returns a new acme error.
  168. func ServerInternalErr(err error) *Error {
  169. return &Error{
  170. Type: serverInternalErr,
  171. Detail: "The server experienced an internal error",
  172. Status: 500,
  173. Err: err,
  174. }
  175. }
  176. // TLSErr returns a new acme error.
  177. func TLSErr(err error) *Error {
  178. return &Error{
  179. Type: tlsErr,
  180. Detail: "The server received a TLS error during validation",
  181. Status: 400,
  182. Err: err,
  183. }
  184. }
  185. // UnauthorizedErr returns a new acme error.
  186. func UnauthorizedErr(err error) *Error {
  187. return &Error{
  188. Type: unauthorizedErr,
  189. Detail: "The client lacks sufficient authorization",
  190. Status: 401,
  191. Err: err,
  192. }
  193. }
  194. // UnsupportedContactErr returns a new acme error.
  195. func UnsupportedContactErr(err error) *Error {
  196. return &Error{
  197. Type: unsupportedContactErr,
  198. Detail: "A contact URL for an account used an unsupported protocol scheme",
  199. Status: 400,
  200. Err: err,
  201. }
  202. }
  203. // UnsupportedIdentifierErr returns a new acme error.
  204. func UnsupportedIdentifierErr(err error) *Error {
  205. return &Error{
  206. Type: unsupportedIdentifierErr,
  207. Detail: "An identifier is of an unsupported type",
  208. Status: 400,
  209. Err: err,
  210. }
  211. }
  212. // UserActionRequiredErr returns a new acme error.
  213. func UserActionRequiredErr(err error) *Error {
  214. return &Error{
  215. Type: userActionRequiredErr,
  216. Detail: "Visit the “instance” URL and take actions specified there",
  217. Status: 400,
  218. Err: err,
  219. }
  220. }
  221. // ProbType is the type of the ACME problem.
  222. type ProbType int
  223. const (
  224. // The request specified an account that does not exist
  225. accountDoesNotExistErr ProbType = iota
  226. // The request specified a certificate to be revoked that has already been revoked
  227. alreadyRevokedErr
  228. // The CSR is unacceptable (e.g., due to a short key)
  229. badCSRErr
  230. // The client sent an unacceptable anti-replay nonce
  231. badNonceErr
  232. // The JWS was signed by a public key the server does not support
  233. badPublicKeyErr
  234. // The revocation reason provided is not allowed by the server
  235. badRevocationReasonErr
  236. // The JWS was signed with an algorithm the server does not support
  237. badSignatureAlgorithmErr
  238. // Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate
  239. caaErr
  240. // Specific error conditions are indicated in the “subproblems” array.
  241. compoundErr
  242. // The server could not connect to validation target
  243. connectionErr
  244. // There was a problem with a DNS query during identifier validation
  245. dnsErr
  246. // The request must include a value for the “externalAccountBinding” field
  247. externalAccountRequiredErr
  248. // Response received didn’t match the challenge’s requirements
  249. incorrectResponseErr
  250. // A contact URL for an account was invalid
  251. invalidContactErr
  252. // The request message was malformed
  253. malformedErr
  254. // The request attempted to finalize an order that is not ready to be finalized
  255. orderNotReadyErr
  256. // The request exceeds a rate limit
  257. rateLimitedErr
  258. // The server will not issue certificates for the identifier
  259. rejectedIdentifierErr
  260. // The server experienced an internal error
  261. serverInternalErr
  262. // The server received a TLS error during validation
  263. tlsErr
  264. // The client lacks sufficient authorization
  265. unauthorizedErr
  266. // A contact URL for an account used an unsupported protocol scheme
  267. unsupportedContactErr
  268. // An identifier is of an unsupported type
  269. unsupportedIdentifierErr
  270. // Visit the “instance” URL and take actions specified there
  271. userActionRequiredErr
  272. )
  273. // String returns the string representation of the acme problem type,
  274. // fulfilling the Stringer interface.
  275. func (ap ProbType) String() string {
  276. switch ap {
  277. case accountDoesNotExistErr:
  278. return "accountDoesNotExist"
  279. case alreadyRevokedErr:
  280. return "alreadyRevoked"
  281. case badCSRErr:
  282. return "badCSR"
  283. case badNonceErr:
  284. return "badNonce"
  285. case badPublicKeyErr:
  286. return "badPublicKey"
  287. case badRevocationReasonErr:
  288. return "badRevocationReason"
  289. case badSignatureAlgorithmErr:
  290. return "badSignatureAlgorithm"
  291. case caaErr:
  292. return "caa"
  293. case compoundErr:
  294. return "compound"
  295. case connectionErr:
  296. return "connection"
  297. case dnsErr:
  298. return "dns"
  299. case externalAccountRequiredErr:
  300. return "externalAccountRequired"
  301. case incorrectResponseErr:
  302. return "incorrectResponse"
  303. case invalidContactErr:
  304. return "invalidContact"
  305. case malformedErr:
  306. return "malformed"
  307. case orderNotReadyErr:
  308. return "orderNotReady"
  309. case rateLimitedErr:
  310. return "rateLimited"
  311. case rejectedIdentifierErr:
  312. return "rejectedIdentifier"
  313. case serverInternalErr:
  314. return "serverInternal"
  315. case tlsErr:
  316. return "tls"
  317. case unauthorizedErr:
  318. return "unauthorized"
  319. case unsupportedContactErr:
  320. return "unsupportedContact"
  321. case unsupportedIdentifierErr:
  322. return "unsupportedIdentifier"
  323. case userActionRequiredErr:
  324. return "userActionRequired"
  325. default:
  326. return "unsupported type"
  327. }
  328. }
  329. // Error is an ACME error type complete with problem document.
  330. type Error struct {
  331. Type ProbType
  332. Detail string
  333. Err error
  334. Status int
  335. Sub []*Error
  336. Identifier *Identifier
  337. }
  338. // Wrap attempts to wrap the internal error.
  339. func Wrap(err error, wrap string) *Error {
  340. switch e := err.(type) {
  341. case nil:
  342. return nil
  343. case *Error:
  344. if e.Err == nil {
  345. e.Err = errors.New(wrap + "; " + e.Detail)
  346. } else {
  347. e.Err = errors.Wrap(e.Err, wrap)
  348. }
  349. return e
  350. default:
  351. return ServerInternalErr(errors.Wrap(err, wrap))
  352. }
  353. }
  354. // Error implements the error interface.
  355. func (e *Error) Error() string {
  356. if e.Err == nil {
  357. return e.Detail
  358. }
  359. return e.Err.Error()
  360. }
  361. // Cause returns the internal error and implements the Causer interface.
  362. func (e *Error) Cause() error {
  363. if e.Err == nil {
  364. return errors.New(e.Detail)
  365. }
  366. return e.Err
  367. }
  368. // ToACME returns an acme representation of the problem type.
  369. func (e *Error) ToACME() *AError {
  370. ae := &AError{
  371. Type: "urn:ietf:params:acme:error:" + e.Type.String(),
  372. Detail: e.Error(),
  373. Status: e.Status,
  374. }
  375. if e.Identifier != nil {
  376. ae.Identifier = *e.Identifier
  377. }
  378. for _, p := range e.Sub {
  379. ae.Subproblems = append(ae.Subproblems, p.ToACME())
  380. }
  381. return ae
  382. }
  383. // StatusCode returns the status code and implements the StatusCode interface.
  384. func (e *Error) StatusCode() int {
  385. return e.Status
  386. }
  387. // AError is the error type as seen in acme request/responses.
  388. type AError struct {
  389. Type string `json:"type"`
  390. Detail string `json:"detail"`
  391. Identifier interface{} `json:"identifier,omitempty"`
  392. Subproblems []interface{} `json:"subproblems,omitempty"`
  393. Status int `json:"-"`
  394. }
  395. // Error allows AError to implement the error interface.
  396. func (ae *AError) Error() string {
  397. return ae.Detail
  398. }
  399. // StatusCode returns the status code and implements the StatusCode interface.
  400. func (ae *AError) StatusCode() int {
  401. return ae.Status
  402. }