constraints.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package constraints
  2. import (
  3. "crypto/x509"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "net/url"
  8. "github.com/smallstep/certificates/errs"
  9. )
  10. // ConstraintError is the typed error that will be returned if a constraint
  11. // error is found.
  12. type ConstraintError struct {
  13. Type string
  14. Name string
  15. Detail string
  16. }
  17. // Error implements the error interface.
  18. func (e ConstraintError) Error() string {
  19. return e.Detail
  20. }
  21. // As implements the As(any) bool interface and allows to use "errors.As()" to
  22. // convert the ConstraintError to an errs.Error.
  23. func (e ConstraintError) As(v any) bool {
  24. if err, ok := v.(**errs.Error); ok {
  25. *err = &errs.Error{
  26. Status: http.StatusForbidden,
  27. Msg: e.Detail,
  28. Err: e,
  29. }
  30. return true
  31. }
  32. return false
  33. }
  34. // Engine implements a constraint validator for DNS names, IP addresses, Email
  35. // addresses and URIs.
  36. type Engine struct {
  37. hasNameConstraints bool
  38. permittedDNSDomains []string
  39. excludedDNSDomains []string
  40. permittedIPRanges []*net.IPNet
  41. excludedIPRanges []*net.IPNet
  42. permittedEmailAddresses []string
  43. excludedEmailAddresses []string
  44. permittedURIDomains []string
  45. excludedURIDomains []string
  46. }
  47. // New creates a constraint validation engine that contains the given chain of
  48. // certificates.
  49. func New(chain ...*x509.Certificate) *Engine {
  50. e := new(Engine)
  51. for _, crt := range chain {
  52. e.permittedDNSDomains = append(e.permittedDNSDomains, crt.PermittedDNSDomains...)
  53. e.excludedDNSDomains = append(e.excludedDNSDomains, crt.ExcludedDNSDomains...)
  54. e.permittedIPRanges = append(e.permittedIPRanges, crt.PermittedIPRanges...)
  55. e.excludedIPRanges = append(e.excludedIPRanges, crt.ExcludedIPRanges...)
  56. e.permittedEmailAddresses = append(e.permittedEmailAddresses, crt.PermittedEmailAddresses...)
  57. e.excludedEmailAddresses = append(e.excludedEmailAddresses, crt.ExcludedEmailAddresses...)
  58. e.permittedURIDomains = append(e.permittedURIDomains, crt.PermittedURIDomains...)
  59. e.excludedURIDomains = append(e.excludedURIDomains, crt.ExcludedURIDomains...)
  60. }
  61. e.hasNameConstraints = len(e.permittedDNSDomains) > 0 || len(e.excludedDNSDomains) > 0 ||
  62. len(e.permittedIPRanges) > 0 || len(e.excludedIPRanges) > 0 ||
  63. len(e.permittedEmailAddresses) > 0 || len(e.excludedEmailAddresses) > 0 ||
  64. len(e.permittedURIDomains) > 0 || len(e.excludedURIDomains) > 0
  65. return e
  66. }
  67. // Validate checks the given names with the name constraints defined in the
  68. // service.
  69. func (e *Engine) Validate(dnsNames []string, ipAddresses []net.IP, emailAddresses []string, uris []*url.URL) error {
  70. if e == nil || !e.hasNameConstraints {
  71. return nil
  72. }
  73. for _, name := range dnsNames {
  74. if err := checkNameConstraints("DNS name", name, name, e.permittedDNSDomains, e.excludedDNSDomains,
  75. func(parsedName, constraint any) (bool, error) {
  76. return matchDomainConstraint(parsedName.(string), constraint.(string))
  77. },
  78. ); err != nil {
  79. return err
  80. }
  81. }
  82. for _, ip := range ipAddresses {
  83. if err := checkNameConstraints("IP address", ip.String(), ip, e.permittedIPRanges, e.excludedIPRanges,
  84. func(parsedName, constraint any) (bool, error) {
  85. return matchIPConstraint(parsedName.(net.IP), constraint.(*net.IPNet))
  86. },
  87. ); err != nil {
  88. return err
  89. }
  90. }
  91. for _, email := range emailAddresses {
  92. mailbox, ok := parseRFC2821Mailbox(email)
  93. if !ok {
  94. return fmt.Errorf("cannot parse rfc822Name %q", email)
  95. }
  96. if err := checkNameConstraints("Email address", email, mailbox, e.permittedEmailAddresses, e.excludedEmailAddresses,
  97. func(parsedName, constraint any) (bool, error) {
  98. return matchEmailConstraint(parsedName.(rfc2821Mailbox), constraint.(string))
  99. },
  100. ); err != nil {
  101. return err
  102. }
  103. }
  104. for _, uri := range uris {
  105. if err := checkNameConstraints("URI", uri.String(), uri, e.permittedURIDomains, e.excludedURIDomains,
  106. func(parsedName, constraint any) (bool, error) {
  107. return matchURIConstraint(parsedName.(*url.URL), constraint.(string))
  108. },
  109. ); err != nil {
  110. return err
  111. }
  112. }
  113. return nil
  114. }
  115. // ValidateCertificate validates the DNS names, IP addresses, Email addresses
  116. // and URIs present in the given certificate.
  117. func (e *Engine) ValidateCertificate(cert *x509.Certificate) error {
  118. return e.Validate(cert.DNSNames, cert.IPAddresses, cert.EmailAddresses, cert.URIs)
  119. }