errors.go 12 KB

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