2022-01-17 22:36:13 +00:00
|
|
|
package policy
|
2022-01-03 11:25:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-09-22 01:35:18 +00:00
|
|
|
"net/http"
|
2022-01-03 11:25:24 +00:00
|
|
|
"net/url"
|
|
|
|
|
2022-09-22 01:35:18 +00:00
|
|
|
"go.step.sm/crypto/x509util"
|
2022-01-17 22:36:13 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2022-03-21 14:53:59 +00:00
|
|
|
|
2022-09-22 01:35:18 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2022-01-03 11:25:24 +00:00
|
|
|
)
|
|
|
|
|
2022-01-18 13:39:21 +00:00
|
|
|
type NamePolicyReason int
|
|
|
|
|
|
|
|
const (
|
2022-04-25 23:47:07 +00:00
|
|
|
// NotAllowed results when an instance of NamePolicyEngine
|
|
|
|
// determines that there's a constraint which doesn't permit
|
|
|
|
// a DNS or another type of SAN to be signed (or otherwise used).
|
2022-04-26 11:12:16 +00:00
|
|
|
NotAllowed NamePolicyReason = iota + 1
|
2022-01-27 16:18:33 +00:00
|
|
|
// CannotParseDomain is returned when an error occurs
|
|
|
|
// when parsing the domain part of SAN or subject.
|
|
|
|
CannotParseDomain
|
|
|
|
// CannotParseRFC822Name is returned when an error
|
|
|
|
// occurs when parsing an email address.
|
|
|
|
CannotParseRFC822Name
|
|
|
|
// CannotMatch is the type of error returned when
|
|
|
|
// an error happens when matching SAN types.
|
|
|
|
CannotMatchNameToConstraint
|
2022-01-18 13:39:21 +00:00
|
|
|
)
|
|
|
|
|
2022-04-25 23:47:07 +00:00
|
|
|
type NameType string
|
|
|
|
|
|
|
|
const (
|
2022-04-28 12:49:23 +00:00
|
|
|
CNNameType NameType = "cn"
|
2022-04-25 23:47:07 +00:00
|
|
|
DNSNameType NameType = "dns"
|
|
|
|
IPNameType NameType = "ip"
|
|
|
|
EmailNameType NameType = "email"
|
|
|
|
URINameType NameType = "uri"
|
|
|
|
PrincipalNameType NameType = "principal"
|
|
|
|
)
|
|
|
|
|
2022-01-18 13:39:21 +00:00
|
|
|
type NamePolicyError struct {
|
2022-04-25 23:47:07 +00:00
|
|
|
Reason NamePolicyReason
|
|
|
|
NameType NameType
|
|
|
|
Name string
|
|
|
|
detail string
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:53:59 +00:00
|
|
|
func (e *NamePolicyError) Error() string {
|
2022-01-27 16:18:33 +00:00
|
|
|
switch e.Reason {
|
2022-04-25 23:47:07 +00:00
|
|
|
case NotAllowed:
|
|
|
|
return fmt.Sprintf("%s name %q not allowed", e.NameType, e.Name)
|
2022-01-27 16:18:33 +00:00
|
|
|
case CannotParseDomain:
|
2022-04-25 23:47:07 +00:00
|
|
|
return fmt.Sprintf("cannot parse %s domain %q", e.NameType, e.Name)
|
2022-01-27 16:18:33 +00:00
|
|
|
case CannotParseRFC822Name:
|
2022-04-25 23:47:07 +00:00
|
|
|
return fmt.Sprintf("cannot parse %s rfc822Name %q", e.NameType, e.Name)
|
2022-01-27 16:18:33 +00:00
|
|
|
case CannotMatchNameToConstraint:
|
2022-04-25 23:47:07 +00:00
|
|
|
return fmt.Sprintf("error matching %s name %q to constraint", e.NameType, e.Name)
|
2022-01-27 16:18:33 +00:00
|
|
|
default:
|
2022-04-25 23:47:07 +00:00
|
|
|
return fmt.Sprintf("unknown error reason (%d): %s", e.Reason, e.detail)
|
2022-01-03 11:25:24 +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 a NotAllowed NamePolicyError to an errs.Error.
|
|
|
|
func (e *NamePolicyError) As(v any) bool {
|
|
|
|
if e.Reason == NotAllowed {
|
|
|
|
if err, ok := v.(**errs.Error); ok {
|
|
|
|
*err = &errs.Error{
|
|
|
|
Status: http.StatusForbidden,
|
|
|
|
Msg: fmt.Sprintf("The request was forbidden by the certificate authority: %s", e.Error()),
|
|
|
|
Err: e,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:47:07 +00:00
|
|
|
func (e *NamePolicyError) Detail() string {
|
|
|
|
return e.detail
|
|
|
|
}
|
|
|
|
|
2022-01-03 11:25:24 +00:00
|
|
|
// NamePolicyEngine can be used to check that a CSR or Certificate meets all allowed and
|
|
|
|
// denied names before a CA creates and/or signs the Certificate.
|
2022-01-17 22:36:13 +00:00
|
|
|
// TODO(hs): the X509 RFC also defines name checks on directory name; support that?
|
2022-01-03 11:25:24 +00:00
|
|
|
// TODO(hs): implement Stringer interface: describe the contents of the NamePolicyEngine?
|
2022-01-27 16:18:33 +00:00
|
|
|
// TODO(hs): implement matching URI schemes, paths, etc; not just the domain part of URI domains
|
|
|
|
|
2022-01-03 11:25:24 +00:00
|
|
|
type NamePolicyEngine struct {
|
2022-01-17 21:49:47 +00:00
|
|
|
// verifySubjectCommonName is set when Subject Common Name must be verified
|
2022-01-03 14:32:58 +00:00
|
|
|
verifySubjectCommonName bool
|
2022-01-17 21:49:47 +00:00
|
|
|
// allowLiteralWildcardNames allows literal wildcard DNS domains
|
|
|
|
allowLiteralWildcardNames bool
|
|
|
|
|
|
|
|
// permitted and exluded constraints similar to x509 Name Constraints
|
2022-04-28 12:49:23 +00:00
|
|
|
permittedCommonNames []string
|
|
|
|
excludedCommonNames []string
|
2022-01-03 11:25:24 +00:00
|
|
|
permittedDNSDomains []string
|
|
|
|
excludedDNSDomains []string
|
|
|
|
permittedIPRanges []*net.IPNet
|
|
|
|
excludedIPRanges []*net.IPNet
|
|
|
|
permittedEmailAddresses []string
|
|
|
|
excludedEmailAddresses []string
|
|
|
|
permittedURIDomains []string
|
|
|
|
excludedURIDomains []string
|
2022-01-17 22:36:13 +00:00
|
|
|
permittedPrincipals []string
|
|
|
|
excludedPrincipals []string
|
2022-01-17 21:49:47 +00:00
|
|
|
|
|
|
|
// some internal counts for housekeeping
|
2022-04-28 12:49:23 +00:00
|
|
|
numberOfCommonNameConstraints int
|
2022-01-17 21:49:47 +00:00
|
|
|
numberOfDNSDomainConstraints int
|
|
|
|
numberOfIPRangeConstraints int
|
|
|
|
numberOfEmailAddressConstraints int
|
|
|
|
numberOfURIDomainConstraints int
|
2022-01-17 22:36:13 +00:00
|
|
|
numberOfPrincipalConstraints int
|
2022-01-17 21:49:47 +00:00
|
|
|
totalNumberOfPermittedConstraints int
|
|
|
|
totalNumberOfExcludedConstraints int
|
|
|
|
totalNumberOfConstraints int
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNamePolicyEngine creates a new NamePolicyEngine with NamePolicyOptions
|
|
|
|
func New(opts ...NamePolicyOption) (*NamePolicyEngine, error) {
|
|
|
|
e := &NamePolicyEngine{}
|
2022-01-17 21:49:47 +00:00
|
|
|
for _, option := range opts {
|
2022-01-03 11:25:24 +00:00
|
|
|
if err := option(e); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 12:49:23 +00:00
|
|
|
e.permittedCommonNames = removeDuplicates(e.permittedCommonNames)
|
2022-01-17 21:49:47 +00:00
|
|
|
e.permittedDNSDomains = removeDuplicates(e.permittedDNSDomains)
|
2022-04-25 23:47:07 +00:00
|
|
|
e.permittedIPRanges = removeDuplicateIPNets(e.permittedIPRanges)
|
2022-01-17 21:49:47 +00:00
|
|
|
e.permittedEmailAddresses = removeDuplicates(e.permittedEmailAddresses)
|
|
|
|
e.permittedURIDomains = removeDuplicates(e.permittedURIDomains)
|
2022-01-17 22:36:13 +00:00
|
|
|
e.permittedPrincipals = removeDuplicates(e.permittedPrincipals)
|
2022-01-17 21:49:47 +00:00
|
|
|
|
2022-04-28 12:49:23 +00:00
|
|
|
e.excludedCommonNames = removeDuplicates(e.excludedCommonNames)
|
2022-01-17 21:49:47 +00:00
|
|
|
e.excludedDNSDomains = removeDuplicates(e.excludedDNSDomains)
|
2022-04-25 23:47:07 +00:00
|
|
|
e.excludedIPRanges = removeDuplicateIPNets(e.excludedIPRanges)
|
2022-01-17 21:49:47 +00:00
|
|
|
e.excludedEmailAddresses = removeDuplicates(e.excludedEmailAddresses)
|
|
|
|
e.excludedURIDomains = removeDuplicates(e.excludedURIDomains)
|
2022-01-17 22:36:13 +00:00
|
|
|
e.excludedPrincipals = removeDuplicates(e.excludedPrincipals)
|
2022-01-17 21:49:47 +00:00
|
|
|
|
2022-04-28 12:49:23 +00:00
|
|
|
e.numberOfCommonNameConstraints = len(e.permittedCommonNames) + len(e.excludedCommonNames)
|
2022-01-17 21:49:47 +00:00
|
|
|
e.numberOfDNSDomainConstraints = len(e.permittedDNSDomains) + len(e.excludedDNSDomains)
|
|
|
|
e.numberOfIPRangeConstraints = len(e.permittedIPRanges) + len(e.excludedIPRanges)
|
|
|
|
e.numberOfEmailAddressConstraints = len(e.permittedEmailAddresses) + len(e.excludedEmailAddresses)
|
|
|
|
e.numberOfURIDomainConstraints = len(e.permittedURIDomains) + len(e.excludedURIDomains)
|
2022-01-17 22:36:13 +00:00
|
|
|
e.numberOfPrincipalConstraints = len(e.permittedPrincipals) + len(e.excludedPrincipals)
|
2022-01-17 21:49:47 +00:00
|
|
|
|
2022-04-28 12:49:23 +00:00
|
|
|
e.totalNumberOfPermittedConstraints = len(e.permittedCommonNames) + len(e.permittedDNSDomains) +
|
|
|
|
len(e.permittedIPRanges) + len(e.permittedEmailAddresses) + len(e.permittedURIDomains) +
|
|
|
|
len(e.permittedPrincipals)
|
2022-01-17 21:49:47 +00:00
|
|
|
|
2022-04-28 12:49:23 +00:00
|
|
|
e.totalNumberOfExcludedConstraints = len(e.excludedCommonNames) + len(e.excludedDNSDomains) +
|
|
|
|
len(e.excludedIPRanges) + len(e.excludedEmailAddresses) + len(e.excludedURIDomains) +
|
|
|
|
len(e.excludedPrincipals)
|
2022-01-17 21:49:47 +00:00
|
|
|
|
|
|
|
e.totalNumberOfConstraints = e.totalNumberOfPermittedConstraints + e.totalNumberOfExcludedConstraints
|
|
|
|
|
2022-01-03 11:25:24 +00:00
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:47:07 +00:00
|
|
|
// removeDuplicates returns a new slice of strings with
|
|
|
|
// duplicate values removed. It retains the order of elements
|
|
|
|
// in the source slice.
|
|
|
|
func removeDuplicates(items []string) (ret []string) {
|
|
|
|
// no need to remove dupes; return original
|
|
|
|
if len(items) <= 1 {
|
|
|
|
return items
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
2022-04-25 23:47:07 +00:00
|
|
|
|
|
|
|
keys := make(map[string]struct{}, len(items))
|
|
|
|
|
|
|
|
ret = make([]string, 0, len(items))
|
|
|
|
for _, item := range items {
|
|
|
|
if _, ok := keys[item]; ok {
|
|
|
|
continue
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
2022-04-25 23:47:07 +00:00
|
|
|
|
|
|
|
keys[item] = struct{}{}
|
|
|
|
ret = append(ret, item)
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
2022-04-25 23:47:07 +00:00
|
|
|
|
|
|
|
return
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 23:47:07 +00:00
|
|
|
// removeDuplicateIPNets returns a new slice of net.IPNets with
|
|
|
|
// duplicate values removed. It retains the order of elements in
|
|
|
|
// the source slice. An IPNet is considered duplicate if its CIDR
|
|
|
|
// notation exists multiple times in the slice.
|
|
|
|
func removeDuplicateIPNets(items []*net.IPNet) (ret []*net.IPNet) {
|
|
|
|
// no need to remove dupes; return original
|
|
|
|
if len(items) <= 1 {
|
|
|
|
return items
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
2022-04-25 23:47:07 +00:00
|
|
|
|
|
|
|
keys := make(map[string]struct{}, len(items))
|
|
|
|
|
|
|
|
ret = make([]*net.IPNet, 0, len(items))
|
|
|
|
for _, item := range items {
|
|
|
|
key := item.String() // use CIDR notation as key
|
|
|
|
if _, ok := keys[key]; ok {
|
|
|
|
continue
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
2022-04-25 23:47:07 +00:00
|
|
|
|
|
|
|
keys[key] = struct{}{}
|
|
|
|
ret = append(ret, item)
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
2022-04-25 23:47:07 +00:00
|
|
|
|
|
|
|
// TODO(hs): implement filter of fully overlapping ranges,
|
|
|
|
// so that the smaller ones are automatically removed?
|
|
|
|
|
|
|
|
return
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:29 +00:00
|
|
|
// IsX509CertificateAllowed verifies that all SANs in a Certificate are allowed.
|
2022-04-26 11:12:16 +00:00
|
|
|
func (e *NamePolicyEngine) IsX509CertificateAllowed(cert *x509.Certificate) error {
|
2022-04-28 12:49:23 +00:00
|
|
|
if err := e.validateNames(cert.DNSNames, cert.IPAddresses, cert.EmailAddresses, cert.URIs, []string{}); err != nil {
|
2022-04-26 11:12:16 +00:00
|
|
|
return err
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-04-28 12:49:23 +00:00
|
|
|
|
|
|
|
if e.verifySubjectCommonName {
|
|
|
|
return e.validateCommonName(cert.Subject.CommonName)
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:12:16 +00:00
|
|
|
return nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:29 +00:00
|
|
|
// IsX509CertificateRequestAllowed verifies that all names in the CSR are allowed.
|
2022-04-26 11:12:16 +00:00
|
|
|
func (e *NamePolicyEngine) IsX509CertificateRequestAllowed(csr *x509.CertificateRequest) error {
|
2022-04-28 12:49:23 +00:00
|
|
|
if err := e.validateNames(csr.DNSNames, csr.IPAddresses, csr.EmailAddresses, csr.URIs, []string{}); err != nil {
|
2022-04-26 11:12:16 +00:00
|
|
|
return err
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-04-28 12:49:23 +00:00
|
|
|
|
|
|
|
if e.verifySubjectCommonName {
|
|
|
|
return e.validateCommonName(csr.Subject.CommonName)
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:12:16 +00:00
|
|
|
return nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AreSANSAllowed verifies that all names in the slice of SANs are allowed.
|
|
|
|
// The SANs are first split into DNS names, IPs, email addresses and URIs.
|
2022-04-26 11:12:16 +00:00
|
|
|
func (e *NamePolicyEngine) AreSANsAllowed(sans []string) error {
|
2022-01-03 11:25:24 +00:00
|
|
|
dnsNames, ips, emails, uris := x509util.SplitSANs(sans)
|
2022-01-17 22:36:13 +00:00
|
|
|
if err := e.validateNames(dnsNames, ips, emails, uris, []string{}); err != nil {
|
2022-04-26 11:12:16 +00:00
|
|
|
return err
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-04-26 11:12:16 +00:00
|
|
|
return nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsDNSAllowed verifies a single DNS domain is allowed.
|
2022-04-26 11:12:16 +00:00
|
|
|
func (e *NamePolicyEngine) IsDNSAllowed(dns string) error {
|
2022-01-17 22:36:13 +00:00
|
|
|
if err := e.validateNames([]string{dns}, []net.IP{}, []string{}, []*url.URL{}, []string{}); err != nil {
|
2022-04-26 11:12:16 +00:00
|
|
|
return err
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-04-26 11:12:16 +00:00
|
|
|
return nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsIPAllowed verifies a single IP domain is allowed.
|
2022-04-26 11:12:16 +00:00
|
|
|
func (e *NamePolicyEngine) IsIPAllowed(ip net.IP) error {
|
2022-01-17 22:36:13 +00:00
|
|
|
if err := e.validateNames([]string{}, []net.IP{ip}, []string{}, []*url.URL{}, []string{}); err != nil {
|
2022-04-26 11:12:16 +00:00
|
|
|
return err
|
2022-01-17 22:36:13 +00:00
|
|
|
}
|
2022-04-26 11:12:16 +00:00
|
|
|
return nil
|
2022-01-17 22:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:29 +00:00
|
|
|
// IsSSHCertificateAllowed verifies that all principals in an SSH certificate are allowed.
|
2022-04-26 11:12:16 +00:00
|
|
|
func (e *NamePolicyEngine) IsSSHCertificateAllowed(cert *ssh.Certificate) error {
|
2022-02-01 13:58:13 +00:00
|
|
|
dnsNames, ips, emails, principals, err := splitSSHPrincipals(cert)
|
2022-01-25 13:59:55 +00:00
|
|
|
if err != nil {
|
2022-04-26 11:12:16 +00:00
|
|
|
return err
|
2022-01-25 13:59:55 +00:00
|
|
|
}
|
2022-02-01 13:58:13 +00:00
|
|
|
if err := e.validateNames(dnsNames, ips, emails, []*url.URL{}, principals); err != nil {
|
2022-04-26 11:12:16 +00:00
|
|
|
return err
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-04-26 11:12:16 +00:00
|
|
|
return nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 13:39:21 +00:00
|
|
|
// splitPrincipals splits SSH certificate principals into DNS names, emails and usernames.
|
2022-02-01 13:58:13 +00:00
|
|
|
func splitSSHPrincipals(cert *ssh.Certificate) (dnsNames []string, ips []net.IP, emails, principals []string, err error) {
|
2022-01-17 22:36:13 +00:00
|
|
|
dnsNames = []string{}
|
2022-01-18 13:39:21 +00:00
|
|
|
ips = []net.IP{}
|
2022-01-17 22:36:13 +00:00
|
|
|
emails = []string{}
|
2022-02-01 13:58:13 +00:00
|
|
|
principals = []string{}
|
2022-01-25 13:59:55 +00:00
|
|
|
var uris []*url.URL
|
|
|
|
switch cert.CertType {
|
|
|
|
case ssh.HostCert:
|
|
|
|
dnsNames, ips, emails, uris = x509util.SplitSANs(cert.ValidPrincipals)
|
2022-02-01 13:58:13 +00:00
|
|
|
if len(uris) > 0 {
|
|
|
|
err = fmt.Errorf("URL principals %v not expected in SSH host certificate ", uris)
|
2022-01-17 22:36:13 +00:00
|
|
|
}
|
2022-01-25 13:59:55 +00:00
|
|
|
case ssh.UserCert:
|
|
|
|
// re-using SplitSANs results in anything that can't be parsed as an IP, URI or email
|
2022-02-01 13:58:13 +00:00
|
|
|
// to be considered a username principal. This allows usernames like h.slatman to be present
|
2022-03-31 14:12:29 +00:00
|
|
|
// in the SSH certificate. We're exluding URIs, because they can be confusing
|
2022-01-25 13:59:55 +00:00
|
|
|
// when used in a SSH user certificate.
|
2022-02-01 13:58:13 +00:00
|
|
|
principals, ips, emails, uris = x509util.SplitSANs(cert.ValidPrincipals)
|
2022-04-18 19:47:31 +00:00
|
|
|
if len(ips) > 0 {
|
|
|
|
err = fmt.Errorf("IP principals %v not expected in SSH user certificate ", ips)
|
|
|
|
}
|
2022-02-01 13:58:13 +00:00
|
|
|
if len(uris) > 0 {
|
|
|
|
err = fmt.Errorf("URL principals %v not expected in SSH user certificate ", uris)
|
2022-01-25 13:59:55 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unexpected SSH certificate type %d", cert.CertType)
|
2022-01-17 22:36:13 +00:00
|
|
|
}
|
2022-01-25 13:59:55 +00:00
|
|
|
|
2022-01-17 22:36:13 +00:00
|
|
|
return
|
|
|
|
}
|