2022-09-20 02:45:13 +00:00
|
|
|
package constraints
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-09-20 21:45:47 +00:00
|
|
|
"net/http"
|
2022-09-20 02:45:13 +00:00
|
|
|
"net/url"
|
2022-09-22 01:35:18 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/errs"
|
2022-09-20 02:45:13 +00:00
|
|
|
)
|
|
|
|
|
2022-09-20 18:33:36 +00:00
|
|
|
// ConstraintError is the typed error that will be returned if a constraint
|
|
|
|
// error is found.
|
2022-09-20 02:45:13 +00:00
|
|
|
type ConstraintError struct {
|
2022-09-20 17:36:44 +00:00
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Detail string
|
2022-09-20 02:45:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:33:36 +00:00
|
|
|
// Error implements the error interface.
|
2022-09-20 02:45:13 +00:00
|
|
|
func (e ConstraintError) Error() string {
|
2022-09-20 17:36:44 +00:00
|
|
|
return e.Detail
|
2022-09-20 02:45:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 01:35:18 +00:00
|
|
|
// As implements the As(any) bool interface and allows to use "errors.As()" to
|
|
|
|
// convert the ConstraintError to an errs.Error.
|
|
|
|
func (e ConstraintError) As(v any) bool {
|
|
|
|
if err, ok := v.(**errs.Error); ok {
|
|
|
|
*err = &errs.Error{
|
|
|
|
Status: http.StatusForbidden,
|
|
|
|
Msg: e.Detail,
|
|
|
|
Err: e,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2022-09-20 21:45:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:38:32 +00:00
|
|
|
// Engine implements a constraint validator for DNS names, IP addresses, Email
|
2022-09-20 18:36:45 +00:00
|
|
|
// addresses and URIs.
|
2022-09-20 18:38:32 +00:00
|
|
|
type Engine struct {
|
2022-09-20 02:45:13 +00:00
|
|
|
hasNameConstraints bool
|
|
|
|
permittedDNSDomains []string
|
|
|
|
excludedDNSDomains []string
|
|
|
|
permittedIPRanges []*net.IPNet
|
|
|
|
excludedIPRanges []*net.IPNet
|
|
|
|
permittedEmailAddresses []string
|
|
|
|
excludedEmailAddresses []string
|
|
|
|
permittedURIDomains []string
|
|
|
|
excludedURIDomains []string
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:38:32 +00:00
|
|
|
// New creates a constraint validation engine that contains the given chain of
|
2022-09-20 18:33:36 +00:00
|
|
|
// certificates.
|
2022-09-20 18:38:32 +00:00
|
|
|
func New(chain ...*x509.Certificate) *Engine {
|
2022-09-20 21:45:47 +00:00
|
|
|
e := new(Engine)
|
2022-09-20 02:45:13 +00:00
|
|
|
for _, crt := range chain {
|
2022-09-20 21:45:47 +00:00
|
|
|
e.permittedDNSDomains = append(e.permittedDNSDomains, crt.PermittedDNSDomains...)
|
|
|
|
e.excludedDNSDomains = append(e.excludedDNSDomains, crt.ExcludedDNSDomains...)
|
|
|
|
e.permittedIPRanges = append(e.permittedIPRanges, crt.PermittedIPRanges...)
|
|
|
|
e.excludedIPRanges = append(e.excludedIPRanges, crt.ExcludedIPRanges...)
|
|
|
|
e.permittedEmailAddresses = append(e.permittedEmailAddresses, crt.PermittedEmailAddresses...)
|
|
|
|
e.excludedEmailAddresses = append(e.excludedEmailAddresses, crt.ExcludedEmailAddresses...)
|
|
|
|
e.permittedURIDomains = append(e.permittedURIDomains, crt.PermittedURIDomains...)
|
|
|
|
e.excludedURIDomains = append(e.excludedURIDomains, crt.ExcludedURIDomains...)
|
2022-09-20 02:45:13 +00:00
|
|
|
}
|
2022-09-22 18:35:11 +00:00
|
|
|
|
|
|
|
e.hasNameConstraints = len(e.permittedDNSDomains) > 0 || len(e.excludedDNSDomains) > 0 ||
|
|
|
|
len(e.permittedIPRanges) > 0 || len(e.excludedIPRanges) > 0 ||
|
|
|
|
len(e.permittedEmailAddresses) > 0 || len(e.excludedEmailAddresses) > 0 ||
|
|
|
|
len(e.permittedURIDomains) > 0 || len(e.excludedURIDomains) > 0
|
|
|
|
|
2022-09-20 21:45:47 +00:00
|
|
|
return e
|
2022-09-20 02:45:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:33:36 +00:00
|
|
|
// Validate checks the given names with the name constraints defined in the
|
|
|
|
// service.
|
2022-09-20 21:45:47 +00:00
|
|
|
func (e *Engine) Validate(dnsNames []string, ipAddresses []net.IP, emailAddresses []string, uris []*url.URL) error {
|
|
|
|
if e == nil || !e.hasNameConstraints {
|
2022-09-20 02:45:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range dnsNames {
|
2022-09-20 21:45:47 +00:00
|
|
|
if err := checkNameConstraints("DNS name", name, name, e.permittedDNSDomains, e.excludedDNSDomains,
|
2022-09-20 02:45:13 +00:00
|
|
|
func(parsedName, constraint any) (bool, error) {
|
|
|
|
return matchDomainConstraint(parsedName.(string), constraint.(string))
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ipAddresses {
|
2022-09-20 21:45:47 +00:00
|
|
|
if err := checkNameConstraints("IP address", ip.String(), ip, e.permittedIPRanges, e.excludedIPRanges,
|
2022-09-20 02:45:13 +00:00
|
|
|
func(parsedName, constraint any) (bool, error) {
|
|
|
|
return matchIPConstraint(parsedName.(net.IP), constraint.(*net.IPNet))
|
2022-09-20 17:36:44 +00:00
|
|
|
},
|
|
|
|
); err != nil {
|
2022-09-20 02:45:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, email := range emailAddresses {
|
|
|
|
mailbox, ok := parseRFC2821Mailbox(email)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot parse rfc822Name %q", email)
|
|
|
|
}
|
2022-09-20 21:45:47 +00:00
|
|
|
if err := checkNameConstraints("Email address", email, mailbox, e.permittedEmailAddresses, e.excludedEmailAddresses,
|
2022-09-20 02:45:13 +00:00
|
|
|
func(parsedName, constraint any) (bool, error) {
|
|
|
|
return matchEmailConstraint(parsedName.(rfc2821Mailbox), constraint.(string))
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uri := range uris {
|
2022-09-20 21:45:47 +00:00
|
|
|
if err := checkNameConstraints("URI", uri.String(), uri, e.permittedURIDomains, e.excludedURIDomains,
|
2022-09-20 02:45:13 +00:00
|
|
|
func(parsedName, constraint any) (bool, error) {
|
|
|
|
return matchURIConstraint(parsedName.(*url.URL), constraint.(string))
|
2022-09-20 17:36:44 +00:00
|
|
|
},
|
|
|
|
); err != nil {
|
2022-09-20 02:45:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-20 20:12:34 +00:00
|
|
|
|
2022-09-23 17:50:44 +00:00
|
|
|
// ValidateCertificate validates the DNS names, IP addresses, Email addresses
|
|
|
|
// and URIs present in the given certificate.
|
2022-09-20 21:45:47 +00:00
|
|
|
func (e *Engine) ValidateCertificate(cert *x509.Certificate) error {
|
|
|
|
return e.Validate(cert.DNSNames, cert.IPAddresses, cert.EmailAddresses, cert.URIs)
|
2022-09-20 20:12:34 +00:00
|
|
|
}
|