| |
| |
| |
|
|
| package x509 |
|
|
| import ( |
| "bytes" |
| "fmt" |
| "net" |
| "net/netip" |
| "net/url" |
| "slices" |
| "strings" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| type nameConstraintsSet[T *net.IPNet | string, V net.IP | string] struct { |
| set []T |
| } |
|
|
| |
| |
| |
| func (nc *nameConstraintsSet[T, V]) sortAndPrune(cmp func(T, T) int, subset func(T, T) bool) { |
| if len(nc.set) < 2 { |
| return |
| } |
|
|
| slices.SortFunc(nc.set, cmp) |
|
|
| if len(nc.set) < 2 { |
| return |
| } |
| writeIndex := 1 |
| for readIndex := 1; readIndex < len(nc.set); readIndex++ { |
| if !subset(nc.set[writeIndex-1], nc.set[readIndex]) { |
| nc.set[writeIndex] = nc.set[readIndex] |
| writeIndex++ |
| } |
| } |
| nc.set = nc.set[:writeIndex] |
| } |
|
|
| |
| |
| |
| |
| |
| func (nc *nameConstraintsSet[T, V]) search(s V, cmp func(T, V) int, match func(T, V) bool) (lowerBound T, exactMatch bool) { |
| if len(nc.set) == 0 { |
| return lowerBound, false |
| } |
| |
| i, found := slices.BinarySearchFunc(nc.set, s, cmp) |
| |
| if found { |
| return nc.set[i], true |
| } |
|
|
| if i < 0 { |
| return lowerBound, false |
| } |
|
|
| var constraint T |
| if i == 0 { |
| constraint = nc.set[0] |
| } else { |
| constraint = nc.set[i-1] |
| } |
| if match(constraint, s) { |
| return constraint, true |
| } |
| return lowerBound, false |
| } |
|
|
| func ipNetworkSubset(a, b *net.IPNet) bool { |
| if !a.Contains(b.IP) { |
| return false |
| } |
| broadcast := make(net.IP, len(b.IP)) |
| for i := range b.IP { |
| broadcast[i] = b.IP[i] | (^b.Mask[i]) |
| } |
| return a.Contains(broadcast) |
| } |
|
|
| func ipNetworkCompare(a, b *net.IPNet) int { |
| i := bytes.Compare(a.IP, b.IP) |
| if i != 0 { |
| return i |
| } |
| return bytes.Compare(a.Mask, b.Mask) |
| } |
|
|
| func ipBinarySearch(constraint *net.IPNet, target net.IP) int { |
| return bytes.Compare(constraint.IP, target) |
| } |
|
|
| func ipMatch(constraint *net.IPNet, target net.IP) bool { |
| return constraint.Contains(target) |
| } |
|
|
| type ipConstraints struct { |
| |
| |
| |
| |
| |
|
|
| ipv4 *nameConstraintsSet[*net.IPNet, net.IP] |
| ipv6 *nameConstraintsSet[*net.IPNet, net.IP] |
| } |
|
|
| func newIPNetConstraints(l []*net.IPNet) interface { |
| query(net.IP) (*net.IPNet, bool) |
| } { |
| if len(l) == 0 { |
| return nil |
| } |
| var ipv4, ipv6 []*net.IPNet |
| for _, n := range l { |
| if len(n.IP) == net.IPv4len { |
| ipv4 = append(ipv4, n) |
| } else { |
| ipv6 = append(ipv6, n) |
| } |
| } |
| var v4c, v6c *nameConstraintsSet[*net.IPNet, net.IP] |
| if len(ipv4) > 0 { |
| v4c = &nameConstraintsSet[*net.IPNet, net.IP]{ |
| set: ipv4, |
| } |
| v4c.sortAndPrune(ipNetworkCompare, ipNetworkSubset) |
| } |
| if len(ipv6) > 0 { |
| v6c = &nameConstraintsSet[*net.IPNet, net.IP]{ |
| set: ipv6, |
| } |
| v6c.sortAndPrune(ipNetworkCompare, ipNetworkSubset) |
| } |
| return &ipConstraints{ipv4: v4c, ipv6: v6c} |
| } |
|
|
| func (ipc *ipConstraints) query(ip net.IP) (*net.IPNet, bool) { |
| var c *nameConstraintsSet[*net.IPNet, net.IP] |
| if len(ip) == net.IPv4len { |
| c = ipc.ipv4 |
| } else { |
| c = ipc.ipv6 |
| } |
| if c == nil { |
| return nil, false |
| } |
| return c.search(ip, ipBinarySearch, ipMatch) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func dnsHasSuffix(a, b string) bool { |
| lenA := len(a) |
| lenB := len(b) |
| if lenA > lenB { |
| return false |
| } |
| i := lenA - 1 |
| offset := lenA - lenB |
| for ; i >= 0; i-- { |
| ar, br := a[i], b[i-(offset)] |
| if ar == br { |
| continue |
| } |
| if br < ar { |
| ar, br = br, ar |
| } |
| if 'A' <= ar && ar <= 'Z' && br == ar+'a'-'A' { |
| continue |
| } |
| return false |
| } |
|
|
| if a[0] != '.' && lenB > lenA && b[lenB-lenA-1] != '.' { |
| return false |
| } |
|
|
| return true |
| } |
|
|
| |
| |
| var dnsCompareTable [256]byte |
|
|
| func init() { |
| |
| |
| |
| for i := 0; i < 256; i++ { |
| c := byte(i) |
| if 'A' <= c && c <= 'Z' { |
| |
| c += 'a' - 'A' |
| } |
| dnsCompareTable[i] = c |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| dnsCompareTable['.'] = 0 |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func dnsCompare(a, b string) int { |
| idxA := len(a) - 1 |
| idxB := len(b) - 1 |
|
|
| for idxA >= 0 && idxB >= 0 { |
| byteA := dnsCompareTable[a[idxA]] |
| byteB := dnsCompareTable[b[idxB]] |
| if byteA == byteB { |
| idxA-- |
| idxB-- |
| continue |
| } |
| ret := 1 |
| if byteA < byteB { |
| ret = -1 |
| } |
| return ret |
| } |
|
|
| ret := 0 |
| if idxA < idxB { |
| ret = -1 |
| } else if idxB < idxA { |
| ret = 1 |
| } |
| return ret |
| } |
|
|
| type dnsConstraints struct { |
| |
| |
| all bool |
|
|
| |
| |
| permitted bool |
|
|
| constraints *nameConstraintsSet[string, string] |
|
|
| |
| |
| |
| |
| parentConstraints map[string]string |
| } |
|
|
| func newDNSConstraints(l []string, permitted bool) interface{ query(string) (string, bool) } { |
| if len(l) == 0 { |
| return nil |
| } |
| for _, n := range l { |
| if len(n) == 0 { |
| return &dnsConstraints{all: true} |
| } |
| } |
| constraints := slices.Clone(l) |
|
|
| nc := &dnsConstraints{ |
| constraints: &nameConstraintsSet[string, string]{ |
| set: constraints, |
| }, |
| permitted: permitted, |
| } |
|
|
| nc.constraints.sortAndPrune(dnsCompare, dnsHasSuffix) |
|
|
| if !permitted { |
| parentConstraints := map[string]string{} |
| for _, name := range nc.constraints.set { |
| name = strings.ToLower(name) |
| trimmedName := trimFirstLabel(name) |
| if trimmedName == "" { |
| continue |
| } |
| parentConstraints[trimmedName] = name |
| } |
| if len(parentConstraints) > 0 { |
| nc.parentConstraints = parentConstraints |
| } |
| } |
|
|
| return nc |
| } |
|
|
| func (dnc *dnsConstraints) query(s string) (string, bool) { |
| if dnc.all { |
| return "", true |
| } |
|
|
| constraint, match := dnc.constraints.search(s, dnsCompare, dnsHasSuffix) |
| if match { |
| return constraint, true |
| } |
|
|
| if !dnc.permitted && len(s) > 0 && s[0] == '*' { |
| s = strings.ToLower(s) |
| trimmed := trimFirstLabel(s) |
| if constraint, found := dnc.parentConstraints[trimmed]; found { |
| return constraint, true |
| } |
| } |
| return "", false |
| } |
|
|
| type emailConstraints struct { |
| dnsConstraints interface{ query(string) (string, bool) } |
|
|
| |
| |
| |
| |
| |
| |
| fullEmails map[rfc2821Mailbox]struct{} |
| } |
|
|
| func newEmailConstraints(l []string, permitted bool) interface { |
| query(rfc2821Mailbox) (string, bool) |
| } { |
| if len(l) == 0 { |
| return nil |
| } |
| exactMap := map[rfc2821Mailbox]struct{}{} |
| var domains []string |
| for _, c := range l { |
| if !strings.ContainsRune(c, '@') { |
| domains = append(domains, c) |
| continue |
| } |
| parsed, ok := parseRFC2821Mailbox(c) |
| if !ok { |
| |
| |
| |
| |
| continue |
| } |
| parsed.domain = strings.ToLower(parsed.domain) |
| exactMap[parsed] = struct{}{} |
| } |
| ec := &emailConstraints{ |
| fullEmails: exactMap, |
| } |
| if len(domains) > 0 { |
| ec.dnsConstraints = newDNSConstraints(domains, permitted) |
| } |
| return ec |
| } |
|
|
| func (ec *emailConstraints) query(s rfc2821Mailbox) (string, bool) { |
| if len(ec.fullEmails) > 0 { |
| if _, ok := ec.fullEmails[s]; ok { |
| return fmt.Sprintf("%s@%s", s.local, s.domain), true |
| } |
| } |
| if ec.dnsConstraints == nil { |
| return "", false |
| } |
| constraint, found := ec.dnsConstraints.query(s.domain) |
| return constraint, found |
| } |
|
|
| type constraints[T any, V any] struct { |
| constraintType string |
| permitted interface{ query(V) (T, bool) } |
| excluded interface{ query(V) (T, bool) } |
| } |
|
|
| func checkConstraints[T string | *net.IPNet, V any, P string | net.IP | parsedURI | rfc2821Mailbox](c constraints[T, V], s V, p P) error { |
| if c.permitted != nil { |
| if _, found := c.permitted.query(s); !found { |
| return fmt.Errorf("%s %q is not permitted by any constraint", c.constraintType, p) |
| } |
| } |
| if c.excluded != nil { |
| if constraint, found := c.excluded.query(s); found { |
| return fmt.Errorf("%s %q is excluded by constraint %q", c.constraintType, p, constraint) |
| } |
| } |
| return nil |
| } |
|
|
| type chainConstraints struct { |
| ip constraints[*net.IPNet, net.IP] |
| dns constraints[string, string] |
| uri constraints[string, string] |
| email constraints[string, rfc2821Mailbox] |
|
|
| index int |
| next *chainConstraints |
| } |
|
|
| func (cc *chainConstraints) check(dns []string, uris []parsedURI, emails []rfc2821Mailbox, ips []net.IP) error { |
| for _, ip := range ips { |
| if err := checkConstraints(cc.ip, ip, ip); err != nil { |
| return err |
| } |
| } |
| for _, d := range dns { |
| if !domainNameValid(d, false) { |
| return fmt.Errorf("x509: cannot parse dnsName %q", d) |
| } |
| if err := checkConstraints(cc.dns, d, d); err != nil { |
| return err |
| } |
| } |
| for _, u := range uris { |
| if !domainNameValid(u.domain, false) { |
| return fmt.Errorf("x509: internal error: URI SAN %q failed to parse", u) |
| } |
| if err := checkConstraints(cc.uri, u.domain, u); err != nil { |
| return err |
| } |
| } |
| for _, e := range emails { |
| if !domainNameValid(e.domain, false) { |
| return fmt.Errorf("x509: cannot parse rfc822Name %q", e) |
| } |
| if err := checkConstraints(cc.email, e, e); err != nil { |
| return err |
| } |
| } |
| return nil |
| } |
|
|
| func checkChainConstraints(chain []*Certificate) error { |
| var currentConstraints *chainConstraints |
| var last *chainConstraints |
| for i, c := range chain { |
| if !c.hasNameConstraints() { |
| continue |
| } |
| cc := &chainConstraints{ |
| ip: constraints[*net.IPNet, net.IP]{"IP address", newIPNetConstraints(c.PermittedIPRanges), newIPNetConstraints(c.ExcludedIPRanges)}, |
| dns: constraints[string, string]{"DNS name", newDNSConstraints(c.PermittedDNSDomains, true), newDNSConstraints(c.ExcludedDNSDomains, false)}, |
| uri: constraints[string, string]{"URI", newDNSConstraints(c.PermittedURIDomains, true), newDNSConstraints(c.ExcludedURIDomains, false)}, |
| email: constraints[string, rfc2821Mailbox]{"email address", newEmailConstraints(c.PermittedEmailAddresses, true), newEmailConstraints(c.ExcludedEmailAddresses, false)}, |
| index: i, |
| } |
| if currentConstraints == nil { |
| currentConstraints = cc |
| last = cc |
| } else if last != nil { |
| last.next = cc |
| last = cc |
| } |
| } |
| if currentConstraints == nil { |
| return nil |
| } |
|
|
| for i, c := range chain { |
| if !c.hasSANExtension() { |
| continue |
| } |
| if i >= currentConstraints.index { |
| for currentConstraints.index <= i { |
| if currentConstraints.next == nil { |
| return nil |
| } |
| currentConstraints = currentConstraints.next |
| } |
| } |
|
|
| uris, err := parseURIs(c.URIs) |
| if err != nil { |
| return err |
| } |
| emails, err := parseMailboxes(c.EmailAddresses) |
| if err != nil { |
| return err |
| } |
|
|
| for n := currentConstraints; n != nil; n = n.next { |
| if err := n.check(c.DNSNames, uris, emails, c.IPAddresses); err != nil { |
| return err |
| } |
| } |
| } |
|
|
| return nil |
| } |
|
|
| type parsedURI struct { |
| uri *url.URL |
| domain string |
| } |
|
|
| func (u parsedURI) String() string { |
| return u.uri.String() |
| } |
|
|
| func parseURIs(uris []*url.URL) ([]parsedURI, error) { |
| parsed := make([]parsedURI, 0, len(uris)) |
| for _, uri := range uris { |
| host := strings.ToLower(uri.Host) |
| if len(host) == 0 { |
| return nil, fmt.Errorf("URI with empty host (%q) cannot be matched against constraints", uri.String()) |
| } |
| if strings.Contains(host, ":") && !strings.HasSuffix(host, "]") { |
| var err error |
| host, _, err = net.SplitHostPort(uri.Host) |
| if err != nil { |
| return nil, fmt.Errorf("cannot parse URI host %q: %v", uri.Host, err) |
| } |
| } |
|
|
| |
| |
| |
| if _, err := netip.ParseAddr(host); err == nil || (strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]")) { |
| return nil, fmt.Errorf("URI with IP (%q) cannot be matched against constraints", uri.String()) |
| } |
|
|
| parsed = append(parsed, parsedURI{uri, host}) |
| } |
| return parsed, nil |
| } |
|
|
| func parseMailboxes(emails []string) ([]rfc2821Mailbox, error) { |
| parsed := make([]rfc2821Mailbox, 0, len(emails)) |
| for _, email := range emails { |
| mailbox, ok := parseRFC2821Mailbox(email) |
| if !ok { |
| return nil, fmt.Errorf("cannot parse rfc822Name %q", email) |
| } |
| mailbox.domain = strings.ToLower(mailbox.domain) |
| parsed = append(parsed, mailbox) |
| } |
| return parsed, nil |
| } |
|
|
| func trimFirstLabel(dnsName string) string { |
| firstDotInd := strings.IndexByte(dnsName, '.') |
| if firstDotInd < 0 { |
| |
| return "" |
| } |
| return dnsName[firstDotInd:] |
| } |
|
|