claims.go 2.9 KB

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