errors.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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: 400,
  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. if err == nil {
  170. return nil
  171. }
  172. return &Error{
  173. Type: serverInternalErr,
  174. Detail: "The server experienced an internal error",
  175. Status: 500,
  176. Err: err,
  177. }
  178. }
  179. // NotImplemented returns a new acme error.
  180. func NotImplemented(err error) *Error {
  181. return &Error{
  182. Type: notImplemented,
  183. Detail: "The requested operation is not implemented",
  184. Status: 501,
  185. Err: err,
  186. }
  187. }
  188. // TLSErr returns a new acme error.
  189. func TLSErr(err error) *Error {
  190. return &Error{
  191. Type: tlsErr,
  192. Detail: "The server received a TLS error during validation",
  193. Status: 400,
  194. Err: err,
  195. }
  196. }
  197. // UnauthorizedErr returns a new acme error.
  198. func UnauthorizedErr(err error) *Error {
  199. return &Error{
  200. Type: unauthorizedErr,
  201. Detail: "The client lacks sufficient authorization",
  202. Status: 401,
  203. Err: err,
  204. }
  205. }
  206. // UnsupportedContactErr returns a new acme error.
  207. func UnsupportedContactErr(err error) *Error {
  208. return &Error{
  209. Type: unsupportedContactErr,
  210. Detail: "A contact URL for an account used an unsupported protocol scheme",
  211. Status: 400,
  212. Err: err,
  213. }
  214. }
  215. // UnsupportedIdentifierErr returns a new acme error.
  216. func UnsupportedIdentifierErr(err error) *Error {
  217. return &Error{
  218. Type: unsupportedIdentifierErr,
  219. Detail: "An identifier is of an unsupported type",
  220. Status: 400,
  221. Err: err,
  222. }
  223. }
  224. // UserActionRequiredErr returns a new acme error.
  225. func UserActionRequiredErr(err error) *Error {
  226. return &Error{
  227. Type: userActionRequiredErr,
  228. Detail: "Visit the “instance” URL and take actions specified there",
  229. Status: 400,
  230. Err: err,
  231. }
  232. }
  233. // ProbType is the type of the ACME problem.
  234. type ProbType int
  235. const (
  236. // The request specified an account that does not exist
  237. accountDoesNotExistErr ProbType = iota
  238. // The request specified a certificate to be revoked that has already been revoked
  239. alreadyRevokedErr
  240. // The CSR is unacceptable (e.g., due to a short key)
  241. badCSRErr
  242. // The client sent an unacceptable anti-replay nonce
  243. badNonceErr
  244. // The JWS was signed by a public key the server does not support
  245. badPublicKeyErr
  246. // The revocation reason provided is not allowed by the server
  247. badRevocationReasonErr
  248. // The JWS was signed with an algorithm the server does not support
  249. badSignatureAlgorithmErr
  250. // Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate
  251. caaErr
  252. // Specific error conditions are indicated in the “subproblems” array.
  253. compoundErr
  254. // The server could not connect to validation target
  255. connectionErr
  256. // There was a problem with a DNS query during identifier validation
  257. dnsErr
  258. // The request must include a value for the “externalAccountBinding” field
  259. externalAccountRequiredErr
  260. // Response received didn’t match the challenge’s requirements
  261. incorrectResponseErr
  262. // A contact URL for an account was invalid
  263. invalidContactErr
  264. // The request message was malformed
  265. malformedErr
  266. // The request attempted to finalize an order that is not ready to be finalized
  267. orderNotReadyErr
  268. // The request exceeds a rate limit
  269. rateLimitedErr
  270. // The server will not issue certificates for the identifier
  271. rejectedIdentifierErr
  272. // The server experienced an internal error
  273. serverInternalErr
  274. // The server received a TLS error during validation
  275. tlsErr
  276. // The client lacks sufficient authorization
  277. unauthorizedErr
  278. // A contact URL for an account used an unsupported protocol scheme
  279. unsupportedContactErr
  280. // An identifier is of an unsupported type
  281. unsupportedIdentifierErr
  282. // Visit the “instance” URL and take actions specified there
  283. userActionRequiredErr
  284. // The operation is not implemented
  285. notImplemented
  286. )
  287. // String returns the string representation of the acme problem type,
  288. // fulfilling the Stringer interface.
  289. func (ap ProbType) String() string {
  290. switch ap {
  291. case accountDoesNotExistErr:
  292. return "accountDoesNotExist"
  293. case alreadyRevokedErr:
  294. return "alreadyRevoked"
  295. case badCSRErr:
  296. return "badCSR"
  297. case badNonceErr:
  298. return "badNonce"
  299. case badPublicKeyErr:
  300. return "badPublicKey"
  301. case badRevocationReasonErr:
  302. return "badRevocationReason"
  303. case badSignatureAlgorithmErr:
  304. return "badSignatureAlgorithm"
  305. case caaErr:
  306. return "caa"
  307. case compoundErr:
  308. return "compound"
  309. case connectionErr:
  310. return "connection"
  311. case dnsErr:
  312. return "dns"
  313. case externalAccountRequiredErr:
  314. return "externalAccountRequired"
  315. case incorrectResponseErr:
  316. return "incorrectResponse"
  317. case invalidContactErr:
  318. return "invalidContact"
  319. case malformedErr:
  320. return "malformed"
  321. case orderNotReadyErr:
  322. return "orderNotReady"
  323. case rateLimitedErr:
  324. return "rateLimited"
  325. case rejectedIdentifierErr:
  326. return "rejectedIdentifier"
  327. case serverInternalErr:
  328. return "serverInternal"
  329. case tlsErr:
  330. return "tls"
  331. case unauthorizedErr:
  332. return "unauthorized"
  333. case unsupportedContactErr:
  334. return "unsupportedContact"
  335. case unsupportedIdentifierErr:
  336. return "unsupportedIdentifier"
  337. case userActionRequiredErr:
  338. return "userActionRequired"
  339. case notImplemented:
  340. return "notImplemented"
  341. default:
  342. return "unsupported type"
  343. }
  344. }
  345. // Error is an ACME error type complete with problem document.
  346. type Error struct {
  347. Type ProbType
  348. Detail string
  349. Err error
  350. Status int
  351. Sub []*Error
  352. Identifier *Identifier
  353. }
  354. // Wrap attempts to wrap the internal error.
  355. func Wrap(err error, wrap string) *Error {
  356. switch e := err.(type) {
  357. case nil:
  358. return nil
  359. case *Error:
  360. if e.Err == nil {
  361. e.Err = errors.New(wrap + "; " + e.Detail)
  362. } else {
  363. e.Err = errors.Wrap(e.Err, wrap)
  364. }
  365. return e
  366. default:
  367. return ServerInternalErr(errors.Wrap(err, wrap))
  368. }
  369. }
  370. // Error implements the error interface.
  371. func (e *Error) Error() string {
  372. if e.Err == nil {
  373. return e.Detail
  374. }
  375. return e.Err.Error()
  376. }
  377. // Cause returns the internal error and implements the Causer interface.
  378. func (e *Error) Cause() error {
  379. if e.Err == nil {
  380. return errors.New(e.Detail)
  381. }
  382. return e.Err
  383. }
  384. // Official returns true if this error's type is listed in §6.7 of RFC 8555.
  385. // Error types in §6.7 are registered under IETF urn namespace:
  386. //
  387. // "urn:ietf:params:acme:error:"
  388. //
  389. // and should include the namespace as a prefix when appearing as a problem
  390. // document.
  391. //
  392. // RFC 8555 also says:
  393. //
  394. // This list is not exhaustive. The server MAY return errors whose
  395. // "type" field is set to a URI other than those defined above. Servers
  396. // MUST NOT use the ACME URN namespace for errors not listed in the
  397. // appropriate IANA registry (see Section 9.6). Clients SHOULD display
  398. // the "detail" field of all errors.
  399. //
  400. // In this case Official returns `false` so that a different namespace can
  401. // be used.
  402. func (e *Error) Official() bool {
  403. return e.Type != notImplemented
  404. }
  405. // ToACME returns an acme representation of the problem type.
  406. // For official errors, the IETF ACME namespace is prepended to the error type.
  407. // For our own errors, we use an (yet) unregistered smallstep acme namespace.
  408. func (e *Error) ToACME() *AError {
  409. prefix := "urn:step:acme:error"
  410. if e.Official() {
  411. prefix = "urn:ietf:params:acme:error:"
  412. }
  413. ae := &AError{
  414. Type: prefix + e.Type.String(),
  415. Detail: e.Error(),
  416. Status: e.Status,
  417. }
  418. if e.Identifier != nil {
  419. ae.Identifier = *e.Identifier
  420. }
  421. for _, p := range e.Sub {
  422. ae.Subproblems = append(ae.Subproblems, p.ToACME())
  423. }
  424. return ae
  425. }
  426. // StatusCode returns the status code and implements the StatusCode interface.
  427. func (e *Error) StatusCode() int {
  428. return e.Status
  429. }
  430. // AError is the error type as seen in acme request/responses.
  431. type AError struct {
  432. Type string `json:"type"`
  433. Detail string `json:"detail"`
  434. Identifier interface{} `json:"identifier,omitempty"`
  435. Subproblems []interface{} `json:"subproblems,omitempty"`
  436. Status int `json:"-"`
  437. }
  438. // Error allows AError to implement the error interface.
  439. func (ae *AError) Error() string {
  440. return ae.Detail
  441. }
  442. // StatusCode returns the status code and implements the StatusCode interface.
  443. func (ae *AError) StatusCode() int {
  444. return ae.Status
  445. }