claims.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package authority
  2. import (
  3. "net"
  4. "reflect"
  5. "time"
  6. "github.com/pkg/errors"
  7. x509 "github.com/smallstep/cli/pkg/x509"
  8. )
  9. // certClaim interface is implemented by types used to validate specific claims in a
  10. // certificate request.
  11. type certClaim interface {
  12. Valid(crt *x509.Certificate) error
  13. }
  14. // ValidateClaims returns nil if all the claims are validated, it will return
  15. // the first error if a claim fails.
  16. func validateClaims(crt *x509.Certificate, claims []certClaim) (err error) {
  17. for _, c := range claims {
  18. if err = c.Valid(crt); err != nil {
  19. return err
  20. }
  21. }
  22. return
  23. }
  24. // commonNameClaim validates the common name of a certificate request.
  25. type commonNameClaim struct {
  26. name string
  27. }
  28. // Valid checks that certificate request common name matches the one configured.
  29. func (c *commonNameClaim) Valid(crt *x509.Certificate) error {
  30. if crt.Subject.CommonName == "" {
  31. return errors.New("common name cannot be empty")
  32. }
  33. if crt.Subject.CommonName != c.name {
  34. return errors.Errorf("common name claim failed - got %s, want %s", crt.Subject.CommonName, c.name)
  35. }
  36. return nil
  37. }
  38. type dnsNamesClaim struct {
  39. names []string
  40. }
  41. // Valid checks that certificate request common name matches the one configured.
  42. func (c *dnsNamesClaim) Valid(crt *x509.Certificate) error {
  43. tokMap := make(map[string]int)
  44. for _, e := range c.names {
  45. tokMap[e] = 1
  46. }
  47. crtMap := make(map[string]int)
  48. for _, e := range crt.DNSNames {
  49. crtMap[e] = 1
  50. }
  51. if !reflect.DeepEqual(tokMap, crtMap) {
  52. return errors.Errorf("DNS names claim failed - got %s, want %s", crt.DNSNames, c.names)
  53. }
  54. return nil
  55. }
  56. type ipAddressesClaim struct {
  57. ips []net.IP
  58. }
  59. // Valid checks that certificate request common name matches the one configured.
  60. func (c *ipAddressesClaim) Valid(crt *x509.Certificate) error {
  61. tokMap := make(map[string]int)
  62. for _, e := range c.ips {
  63. tokMap[e.String()] = 1
  64. }
  65. crtMap := make(map[string]int)
  66. for _, e := range crt.IPAddresses {
  67. crtMap[e.String()] = 1
  68. }
  69. if !reflect.DeepEqual(tokMap, crtMap) {
  70. return errors.Errorf("IP Addresses claim failed - got %v, want %v", crt.IPAddresses, c.ips)
  71. }
  72. return nil
  73. }
  74. // certTemporalClaim validates the certificate temporal validity settings.
  75. type certTemporalClaim struct {
  76. min time.Duration
  77. max time.Duration
  78. }
  79. // Validate validates the certificate temporal validity settings.
  80. func (ctc *certTemporalClaim) Valid(crt *x509.Certificate) error {
  81. var (
  82. na = crt.NotAfter
  83. nb = crt.NotBefore
  84. d = na.Sub(nb)
  85. now = time.Now()
  86. )
  87. if na.Before(now) {
  88. return errors.Errorf("NotAfter: %v cannot be in the past", na)
  89. }
  90. if na.Before(nb) {
  91. return errors.Errorf("NotAfter: %v cannot be before NotBefore: %v", na, nb)
  92. }
  93. if d < ctc.min {
  94. return errors.Errorf("requested duration of %v is less than the authorized minimum certificate duration of %v",
  95. d, ctc.min)
  96. }
  97. if d > ctc.max {
  98. return errors.Errorf("requested duration of %v is more than the authorized maximum certificate duration of %v",
  99. d, ctc.max)
  100. }
  101. return nil
  102. }