errors.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package signed
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // ErrInsufficientSignatures - can not create enough signatures on a piece of
  7. // metadata
  8. type ErrInsufficientSignatures struct {
  9. FoundKeys int
  10. NeededKeys int
  11. MissingKeyIDs []string
  12. }
  13. func (e ErrInsufficientSignatures) Error() string {
  14. candidates := ""
  15. if len(e.MissingKeyIDs) > 0 {
  16. candidates = fmt.Sprintf(" (%s)", strings.Join(e.MissingKeyIDs, ", "))
  17. }
  18. if e.FoundKeys == 0 {
  19. return fmt.Sprintf("signing keys not available: need %d keys from %d possible keys%s",
  20. e.NeededKeys, len(e.MissingKeyIDs), candidates)
  21. }
  22. return fmt.Sprintf("not enough signing keys: found %d of %d needed keys - %d other possible keys%s",
  23. e.FoundKeys, e.NeededKeys, len(e.MissingKeyIDs), candidates)
  24. }
  25. // ErrExpired indicates a piece of metadata has expired
  26. type ErrExpired struct {
  27. Role string
  28. Expired string
  29. }
  30. func (e ErrExpired) Error() string {
  31. return fmt.Sprintf("%s expired at %v", e.Role, e.Expired)
  32. }
  33. // ErrLowVersion indicates the piece of metadata has a version number lower than
  34. // a version number we're already seen for this role
  35. type ErrLowVersion struct {
  36. Actual int
  37. Current int
  38. }
  39. func (e ErrLowVersion) Error() string {
  40. return fmt.Sprintf("version %d is lower than current version %d", e.Actual, e.Current)
  41. }
  42. // ErrRoleThreshold indicates we did not validate enough signatures to meet the threshold
  43. type ErrRoleThreshold struct {
  44. Msg string
  45. }
  46. func (e ErrRoleThreshold) Error() string {
  47. if e.Msg == "" {
  48. return "valid signatures did not meet threshold"
  49. }
  50. return e.Msg
  51. }
  52. // ErrInvalidKeyType indicates the types for the key and signature it's associated with are
  53. // mismatched. Probably a sign of malicious behaviour
  54. type ErrInvalidKeyType struct{}
  55. func (e ErrInvalidKeyType) Error() string {
  56. return "key type is not valid for signature"
  57. }
  58. // ErrInvalidKeyID indicates the specified key ID was incorrect for its associated data
  59. type ErrInvalidKeyID struct{}
  60. func (e ErrInvalidKeyID) Error() string {
  61. return "key ID is not valid for key content"
  62. }
  63. // ErrInvalidKeyLength indicates that while we may support the cipher, the provided
  64. // key length is not specifically supported, i.e. we support RSA, but not 1024 bit keys
  65. type ErrInvalidKeyLength struct {
  66. msg string
  67. }
  68. func (e ErrInvalidKeyLength) Error() string {
  69. return fmt.Sprintf("key length is not supported: %s", e.msg)
  70. }
  71. // ErrNoKeys indicates no signing keys were found when trying to sign
  72. type ErrNoKeys struct {
  73. KeyIDs []string
  74. }
  75. func (e ErrNoKeys) Error() string {
  76. return fmt.Sprintf("could not find necessary signing keys, at least one of these keys must be available: %s",
  77. strings.Join(e.KeyIDs, ", "))
  78. }