...

Source file src/github.com/google/certificate-transparency-go/x509/verify.go

Documentation: github.com/google/certificate-transparency-go/x509

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package x509
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"net"
    12  	"net/url"
    13  	"os"
    14  	"reflect"
    15  	"runtime"
    16  	"strings"
    17  	"time"
    18  	"unicode/utf8"
    19  )
    20  
    21  // ignoreCN disables interpreting Common Name as a hostname. See issue 24151.
    22  var ignoreCN = strings.Contains(os.Getenv("GODEBUG"), "x509ignoreCN=1")
    23  
    24  type InvalidReason int
    25  
    26  const (
    27  	// NotAuthorizedToSign results when a certificate is signed by another
    28  	// which isn't marked as a CA certificate.
    29  	NotAuthorizedToSign InvalidReason = iota
    30  	// Expired results when a certificate has expired, based on the time
    31  	// given in the VerifyOptions.
    32  	Expired
    33  	// CANotAuthorizedForThisName results when an intermediate or root
    34  	// certificate has a name constraint which doesn't permit a DNS or
    35  	// other name (including IP address) in the leaf certificate.
    36  	CANotAuthorizedForThisName
    37  	// TooManyIntermediates results when a path length constraint is
    38  	// violated.
    39  	TooManyIntermediates
    40  	// IncompatibleUsage results when the certificate's key usage indicates
    41  	// that it may only be used for a different purpose.
    42  	IncompatibleUsage
    43  	// NameMismatch results when the subject name of a parent certificate
    44  	// does not match the issuer name in the child.
    45  	NameMismatch
    46  	// NameConstraintsWithoutSANs results when a leaf certificate doesn't
    47  	// contain a Subject Alternative Name extension, but a CA certificate
    48  	// contains name constraints, and the Common Name can be interpreted as
    49  	// a hostname.
    50  	//
    51  	// You can avoid this error by setting the experimental GODEBUG environment
    52  	// variable to "x509ignoreCN=1", disabling Common Name matching entirely.
    53  	// This behavior might become the default in the future.
    54  	NameConstraintsWithoutSANs
    55  	// UnconstrainedName results when a CA certificate contains permitted
    56  	// name constraints, but leaf certificate contains a name of an
    57  	// unsupported or unconstrained type.
    58  	UnconstrainedName
    59  	// TooManyConstraints results when the number of comparison operations
    60  	// needed to check a certificate exceeds the limit set by
    61  	// VerifyOptions.MaxConstraintComparisions. This limit exists to
    62  	// prevent pathological certificates can consuming excessive amounts of
    63  	// CPU time to verify.
    64  	TooManyConstraints
    65  	// CANotAuthorizedForExtKeyUsage results when an intermediate or root
    66  	// certificate does not permit a requested extended key usage.
    67  	CANotAuthorizedForExtKeyUsage
    68  )
    69  
    70  // CertificateInvalidError results when an odd error occurs. Users of this
    71  // library probably want to handle all these errors uniformly.
    72  type CertificateInvalidError struct {
    73  	Cert   *Certificate
    74  	Reason InvalidReason
    75  	Detail string
    76  }
    77  
    78  func (e CertificateInvalidError) Error() string {
    79  	switch e.Reason {
    80  	case NotAuthorizedToSign:
    81  		return "x509: certificate is not authorized to sign other certificates"
    82  	case Expired:
    83  		return "x509: certificate has expired or is not yet valid: " + e.Detail
    84  	case CANotAuthorizedForThisName:
    85  		return "x509: a root or intermediate certificate is not authorized to sign for this name: " + e.Detail
    86  	case CANotAuthorizedForExtKeyUsage:
    87  		return "x509: a root or intermediate certificate is not authorized for an extended key usage: " + e.Detail
    88  	case TooManyIntermediates:
    89  		return "x509: too many intermediates for path length constraint"
    90  	case IncompatibleUsage:
    91  		return "x509: certificate specifies an incompatible key usage"
    92  	case NameMismatch:
    93  		return "x509: issuer name does not match subject from issuing certificate"
    94  	case NameConstraintsWithoutSANs:
    95  		return "x509: issuer has name constraints but leaf doesn't have a SAN extension"
    96  	case UnconstrainedName:
    97  		return "x509: issuer has name constraints but leaf contains unknown or unconstrained name: " + e.Detail
    98  	}
    99  	return "x509: unknown error"
   100  }
   101  
   102  // HostnameError results when the set of authorized names doesn't match the
   103  // requested name.
   104  type HostnameError struct {
   105  	Certificate *Certificate
   106  	Host        string
   107  }
   108  
   109  func (h HostnameError) Error() string {
   110  	c := h.Certificate
   111  
   112  	if !c.hasSANExtension() && !validHostname(c.Subject.CommonName) &&
   113  		matchHostnames(toLowerCaseASCII(c.Subject.CommonName), toLowerCaseASCII(h.Host)) {
   114  		// This would have validated, if it weren't for the validHostname check on Common Name.
   115  		return "x509: Common Name is not a valid hostname: " + c.Subject.CommonName
   116  	}
   117  
   118  	var valid string
   119  	if ip := net.ParseIP(h.Host); ip != nil {
   120  		// Trying to validate an IP
   121  		if len(c.IPAddresses) == 0 {
   122  			return "x509: cannot validate certificate for " + h.Host + " because it doesn't contain any IP SANs"
   123  		}
   124  		for _, san := range c.IPAddresses {
   125  			if len(valid) > 0 {
   126  				valid += ", "
   127  			}
   128  			valid += san.String()
   129  		}
   130  	} else {
   131  		if c.commonNameAsHostname() {
   132  			valid = c.Subject.CommonName
   133  		} else {
   134  			valid = strings.Join(c.DNSNames, ", ")
   135  		}
   136  	}
   137  
   138  	if len(valid) == 0 {
   139  		return "x509: certificate is not valid for any names, but wanted to match " + h.Host
   140  	}
   141  	return "x509: certificate is valid for " + valid + ", not " + h.Host
   142  }
   143  
   144  // UnknownAuthorityError results when the certificate issuer is unknown
   145  type UnknownAuthorityError struct {
   146  	Cert *Certificate
   147  	// hintErr contains an error that may be helpful in determining why an
   148  	// authority wasn't found.
   149  	hintErr error
   150  	// hintCert contains a possible authority certificate that was rejected
   151  	// because of the error in hintErr.
   152  	hintCert *Certificate
   153  }
   154  
   155  func (e UnknownAuthorityError) Error() string {
   156  	s := "x509: certificate signed by unknown authority"
   157  	if e.hintErr != nil {
   158  		certName := e.hintCert.Subject.CommonName
   159  		if len(certName) == 0 {
   160  			if len(e.hintCert.Subject.Organization) > 0 {
   161  				certName = e.hintCert.Subject.Organization[0]
   162  			} else {
   163  				certName = "serial:" + e.hintCert.SerialNumber.String()
   164  			}
   165  		}
   166  		s += fmt.Sprintf(" (possibly because of %q while trying to verify candidate authority certificate %q)", e.hintErr, certName)
   167  	}
   168  	return s
   169  }
   170  
   171  // SystemRootsError results when we fail to load the system root certificates.
   172  type SystemRootsError struct {
   173  	Err error
   174  }
   175  
   176  func (se SystemRootsError) Error() string {
   177  	msg := "x509: failed to load system roots and no roots provided"
   178  	if se.Err != nil {
   179  		return msg + "; " + se.Err.Error()
   180  	}
   181  	return msg
   182  }
   183  
   184  // errNotParsed is returned when a certificate without ASN.1 contents is
   185  // verified. Platform-specific verification needs the ASN.1 contents.
   186  var errNotParsed = errors.New("x509: missing ASN.1 contents; use ParseCertificate")
   187  
   188  // VerifyOptions contains parameters for Certificate.Verify. It's a structure
   189  // because other PKIX verification APIs have ended up needing many options.
   190  type VerifyOptions struct {
   191  	DNSName       string
   192  	Intermediates *CertPool
   193  	Roots         *CertPool // if nil, the system roots are used
   194  	CurrentTime   time.Time // if zero, the current time is used
   195  	// Options to disable various verification checks.
   196  	DisableTimeChecks              bool
   197  	DisableCriticalExtensionChecks bool
   198  	DisableNameChecks              bool
   199  	DisableEKUChecks               bool
   200  	DisablePathLenChecks           bool
   201  	DisableNameConstraintChecks    bool
   202  	// KeyUsage specifies which Extended Key Usage values are acceptable. A leaf
   203  	// certificate is accepted if it contains any of the listed values. An empty
   204  	// list means ExtKeyUsageServerAuth. To accept any key usage, include
   205  	// ExtKeyUsageAny.
   206  	//
   207  	// Certificate chains are required to nest these extended key usage values.
   208  	// (This matches the Windows CryptoAPI behavior, but not the spec.)
   209  	KeyUsages []ExtKeyUsage
   210  	// MaxConstraintComparisions is the maximum number of comparisons to
   211  	// perform when checking a given certificate's name constraints. If
   212  	// zero, a sensible default is used. This limit prevents pathological
   213  	// certificates from consuming excessive amounts of CPU time when
   214  	// validating.
   215  	MaxConstraintComparisions int
   216  }
   217  
   218  const (
   219  	leafCertificate = iota
   220  	intermediateCertificate
   221  	rootCertificate
   222  )
   223  
   224  // rfc2821Mailbox represents a “mailbox” (which is an email address to most
   225  // people) by breaking it into the “local” (i.e. before the '@') and “domain”
   226  // parts.
   227  type rfc2821Mailbox struct {
   228  	local, domain string
   229  }
   230  
   231  // parseRFC2821Mailbox parses an email address into local and domain parts,
   232  // based on the ABNF for a “Mailbox” from RFC 2821. According to RFC 5280,
   233  // Section 4.2.1.6 that's correct for an rfc822Name from a certificate: “The
   234  // format of an rfc822Name is a "Mailbox" as defined in RFC 2821, Section 4.1.2”.
   235  func parseRFC2821Mailbox(in string) (mailbox rfc2821Mailbox, ok bool) {
   236  	if len(in) == 0 {
   237  		return mailbox, false
   238  	}
   239  
   240  	localPartBytes := make([]byte, 0, len(in)/2)
   241  
   242  	if in[0] == '"' {
   243  		// Quoted-string = DQUOTE *qcontent DQUOTE
   244  		// non-whitespace-control = %d1-8 / %d11 / %d12 / %d14-31 / %d127
   245  		// qcontent = qtext / quoted-pair
   246  		// qtext = non-whitespace-control /
   247  		//         %d33 / %d35-91 / %d93-126
   248  		// quoted-pair = ("\" text) / obs-qp
   249  		// text = %d1-9 / %d11 / %d12 / %d14-127 / obs-text
   250  		//
   251  		// (Names beginning with “obs-” are the obsolete syntax from RFC 2822,
   252  		// Section 4. Since it has been 16 years, we no longer accept that.)
   253  		in = in[1:]
   254  	QuotedString:
   255  		for {
   256  			if len(in) == 0 {
   257  				return mailbox, false
   258  			}
   259  			c := in[0]
   260  			in = in[1:]
   261  
   262  			switch {
   263  			case c == '"':
   264  				break QuotedString
   265  
   266  			case c == '\\':
   267  				// quoted-pair
   268  				if len(in) == 0 {
   269  					return mailbox, false
   270  				}
   271  				if in[0] == 11 ||
   272  					in[0] == 12 ||
   273  					(1 <= in[0] && in[0] <= 9) ||
   274  					(14 <= in[0] && in[0] <= 127) {
   275  					localPartBytes = append(localPartBytes, in[0])
   276  					in = in[1:]
   277  				} else {
   278  					return mailbox, false
   279  				}
   280  
   281  			case c == 11 ||
   282  				c == 12 ||
   283  				// Space (char 32) is not allowed based on the
   284  				// BNF, but RFC 3696 gives an example that
   285  				// assumes that it is. Several “verified”
   286  				// errata continue to argue about this point.
   287  				// We choose to accept it.
   288  				c == 32 ||
   289  				c == 33 ||
   290  				c == 127 ||
   291  				(1 <= c && c <= 8) ||
   292  				(14 <= c && c <= 31) ||
   293  				(35 <= c && c <= 91) ||
   294  				(93 <= c && c <= 126):
   295  				// qtext
   296  				localPartBytes = append(localPartBytes, c)
   297  
   298  			default:
   299  				return mailbox, false
   300  			}
   301  		}
   302  	} else {
   303  		// Atom ("." Atom)*
   304  	NextChar:
   305  		for len(in) > 0 {
   306  			// atext from RFC 2822, Section 3.2.4
   307  			c := in[0]
   308  
   309  			switch {
   310  			case c == '\\':
   311  				// Examples given in RFC 3696 suggest that
   312  				// escaped characters can appear outside of a
   313  				// quoted string. Several “verified” errata
   314  				// continue to argue the point. We choose to
   315  				// accept it.
   316  				in = in[1:]
   317  				if len(in) == 0 {
   318  					return mailbox, false
   319  				}
   320  				fallthrough
   321  
   322  			case ('0' <= c && c <= '9') ||
   323  				('a' <= c && c <= 'z') ||
   324  				('A' <= c && c <= 'Z') ||
   325  				c == '!' || c == '#' || c == '$' || c == '%' ||
   326  				c == '&' || c == '\'' || c == '*' || c == '+' ||
   327  				c == '-' || c == '/' || c == '=' || c == '?' ||
   328  				c == '^' || c == '_' || c == '`' || c == '{' ||
   329  				c == '|' || c == '}' || c == '~' || c == '.':
   330  				localPartBytes = append(localPartBytes, in[0])
   331  				in = in[1:]
   332  
   333  			default:
   334  				break NextChar
   335  			}
   336  		}
   337  
   338  		if len(localPartBytes) == 0 {
   339  			return mailbox, false
   340  		}
   341  
   342  		// From RFC 3696, Section 3:
   343  		// “period (".") may also appear, but may not be used to start
   344  		// or end the local part, nor may two or more consecutive
   345  		// periods appear.”
   346  		twoDots := []byte{'.', '.'}
   347  		if localPartBytes[0] == '.' ||
   348  			localPartBytes[len(localPartBytes)-1] == '.' ||
   349  			bytes.Contains(localPartBytes, twoDots) {
   350  			return mailbox, false
   351  		}
   352  	}
   353  
   354  	if len(in) == 0 || in[0] != '@' {
   355  		return mailbox, false
   356  	}
   357  	in = in[1:]
   358  
   359  	// The RFC species a format for domains, but that's known to be
   360  	// violated in practice so we accept that anything after an '@' is the
   361  	// domain part.
   362  	if _, ok := domainToReverseLabels(in); !ok {
   363  		return mailbox, false
   364  	}
   365  
   366  	mailbox.local = string(localPartBytes)
   367  	mailbox.domain = in
   368  	return mailbox, true
   369  }
   370  
   371  // domainToReverseLabels converts a textual domain name like foo.example.com to
   372  // the list of labels in reverse order, e.g. ["com", "example", "foo"].
   373  func domainToReverseLabels(domain string) (reverseLabels []string, ok bool) {
   374  	for len(domain) > 0 {
   375  		if i := strings.LastIndexByte(domain, '.'); i == -1 {
   376  			reverseLabels = append(reverseLabels, domain)
   377  			domain = ""
   378  		} else {
   379  			reverseLabels = append(reverseLabels, domain[i+1:])
   380  			domain = domain[:i]
   381  		}
   382  	}
   383  
   384  	if len(reverseLabels) > 0 && len(reverseLabels[0]) == 0 {
   385  		// An empty label at the end indicates an absolute value.
   386  		return nil, false
   387  	}
   388  
   389  	for _, label := range reverseLabels {
   390  		if len(label) == 0 {
   391  			// Empty labels are otherwise invalid.
   392  			return nil, false
   393  		}
   394  
   395  		for _, c := range label {
   396  			if c < 33 || c > 126 {
   397  				// Invalid character.
   398  				return nil, false
   399  			}
   400  		}
   401  	}
   402  
   403  	return reverseLabels, true
   404  }
   405  
   406  func matchEmailConstraint(mailbox rfc2821Mailbox, constraint string) (bool, error) {
   407  	// If the constraint contains an @, then it specifies an exact mailbox
   408  	// name.
   409  	if strings.Contains(constraint, "@") {
   410  		constraintMailbox, ok := parseRFC2821Mailbox(constraint)
   411  		if !ok {
   412  			return false, fmt.Errorf("x509: internal error: cannot parse constraint %q", constraint)
   413  		}
   414  		return mailbox.local == constraintMailbox.local && strings.EqualFold(mailbox.domain, constraintMailbox.domain), nil
   415  	}
   416  
   417  	// Otherwise the constraint is like a DNS constraint of the domain part
   418  	// of the mailbox.
   419  	return matchDomainConstraint(mailbox.domain, constraint)
   420  }
   421  
   422  func matchURIConstraint(uri *url.URL, constraint string) (bool, error) {
   423  	// From RFC 5280, Section 4.2.1.10:
   424  	// “a uniformResourceIdentifier that does not include an authority
   425  	// component with a host name specified as a fully qualified domain
   426  	// name (e.g., if the URI either does not include an authority
   427  	// component or includes an authority component in which the host name
   428  	// is specified as an IP address), then the application MUST reject the
   429  	// certificate.”
   430  
   431  	host := uri.Host
   432  	if len(host) == 0 {
   433  		return false, fmt.Errorf("URI with empty host (%q) cannot be matched against constraints", uri.String())
   434  	}
   435  
   436  	if strings.Contains(host, ":") && !strings.HasSuffix(host, "]") {
   437  		var err error
   438  		host, _, err = net.SplitHostPort(uri.Host)
   439  		if err != nil {
   440  			return false, err
   441  		}
   442  	}
   443  
   444  	if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") ||
   445  		net.ParseIP(host) != nil {
   446  		return false, fmt.Errorf("URI with IP (%q) cannot be matched against constraints", uri.String())
   447  	}
   448  
   449  	return matchDomainConstraint(host, constraint)
   450  }
   451  
   452  func matchIPConstraint(ip net.IP, constraint *net.IPNet) (bool, error) {
   453  	if len(ip) != len(constraint.IP) {
   454  		return false, nil
   455  	}
   456  
   457  	for i := range ip {
   458  		if mask := constraint.Mask[i]; ip[i]&mask != constraint.IP[i]&mask {
   459  			return false, nil
   460  		}
   461  	}
   462  
   463  	return true, nil
   464  }
   465  
   466  func matchDomainConstraint(domain, constraint string) (bool, error) {
   467  	// The meaning of zero length constraints is not specified, but this
   468  	// code follows NSS and accepts them as matching everything.
   469  	if len(constraint) == 0 {
   470  		return true, nil
   471  	}
   472  
   473  	domainLabels, ok := domainToReverseLabels(domain)
   474  	if !ok {
   475  		return false, fmt.Errorf("x509: internal error: cannot parse domain %q", domain)
   476  	}
   477  
   478  	// RFC 5280 says that a leading period in a domain name means that at
   479  	// least one label must be prepended, but only for URI and email
   480  	// constraints, not DNS constraints. The code also supports that
   481  	// behaviour for DNS constraints.
   482  
   483  	mustHaveSubdomains := false
   484  	if constraint[0] == '.' {
   485  		mustHaveSubdomains = true
   486  		constraint = constraint[1:]
   487  	}
   488  
   489  	constraintLabels, ok := domainToReverseLabels(constraint)
   490  	if !ok {
   491  		return false, fmt.Errorf("x509: internal error: cannot parse domain %q", constraint)
   492  	}
   493  
   494  	if len(domainLabels) < len(constraintLabels) ||
   495  		(mustHaveSubdomains && len(domainLabels) == len(constraintLabels)) {
   496  		return false, nil
   497  	}
   498  
   499  	for i, constraintLabel := range constraintLabels {
   500  		if !strings.EqualFold(constraintLabel, domainLabels[i]) {
   501  			return false, nil
   502  		}
   503  	}
   504  
   505  	return true, nil
   506  }
   507  
   508  // checkNameConstraints checks that c permits a child certificate to claim the
   509  // given name, of type nameType. The argument parsedName contains the parsed
   510  // form of name, suitable for passing to the match function. The total number
   511  // of comparisons is tracked in the given count and should not exceed the given
   512  // limit.
   513  func (c *Certificate) checkNameConstraints(count *int,
   514  	maxConstraintComparisons int,
   515  	nameType string,
   516  	name string,
   517  	parsedName interface{},
   518  	match func(parsedName, constraint interface{}) (match bool, err error),
   519  	permitted, excluded interface{}) error {
   520  
   521  	excludedValue := reflect.ValueOf(excluded)
   522  
   523  	*count += excludedValue.Len()
   524  	if *count > maxConstraintComparisons {
   525  		return CertificateInvalidError{c, TooManyConstraints, ""}
   526  	}
   527  
   528  	for i := 0; i < excludedValue.Len(); i++ {
   529  		constraint := excludedValue.Index(i).Interface()
   530  		match, err := match(parsedName, constraint)
   531  		if err != nil {
   532  			return CertificateInvalidError{c, CANotAuthorizedForThisName, err.Error()}
   533  		}
   534  
   535  		if match {
   536  			return CertificateInvalidError{c, CANotAuthorizedForThisName, fmt.Sprintf("%s %q is excluded by constraint %q", nameType, name, constraint)}
   537  		}
   538  	}
   539  
   540  	permittedValue := reflect.ValueOf(permitted)
   541  
   542  	*count += permittedValue.Len()
   543  	if *count > maxConstraintComparisons {
   544  		return CertificateInvalidError{c, TooManyConstraints, ""}
   545  	}
   546  
   547  	ok := true
   548  	for i := 0; i < permittedValue.Len(); i++ {
   549  		constraint := permittedValue.Index(i).Interface()
   550  
   551  		var err error
   552  		if ok, err = match(parsedName, constraint); err != nil {
   553  			return CertificateInvalidError{c, CANotAuthorizedForThisName, err.Error()}
   554  		}
   555  
   556  		if ok {
   557  			break
   558  		}
   559  	}
   560  
   561  	if !ok {
   562  		return CertificateInvalidError{c, CANotAuthorizedForThisName, fmt.Sprintf("%s %q is not permitted by any constraint", nameType, name)}
   563  	}
   564  
   565  	return nil
   566  }
   567  
   568  // isValid performs validity checks on c given that it is a candidate to append
   569  // to the chain in currentChain.
   570  func (c *Certificate) isValid(certType int, currentChain []*Certificate, opts *VerifyOptions) error {
   571  	if !opts.DisableCriticalExtensionChecks && len(c.UnhandledCriticalExtensions) > 0 {
   572  		return UnhandledCriticalExtension{ID: c.UnhandledCriticalExtensions[0]}
   573  	}
   574  
   575  	if !opts.DisableNameChecks && len(currentChain) > 0 {
   576  		child := currentChain[len(currentChain)-1]
   577  		if !bytes.Equal(child.RawIssuer, c.RawSubject) {
   578  			return CertificateInvalidError{c, NameMismatch, ""}
   579  		}
   580  	}
   581  
   582  	if !opts.DisableTimeChecks {
   583  		now := opts.CurrentTime
   584  		if now.IsZero() {
   585  			now = time.Now()
   586  		}
   587  		if now.Before(c.NotBefore) {
   588  			return CertificateInvalidError{
   589  				Cert:   c,
   590  				Reason: Expired,
   591  				Detail: fmt.Sprintf("current time %s is before %s", now.Format(time.RFC3339), c.NotBefore.Format(time.RFC3339)),
   592  			}
   593  		} else if now.After(c.NotAfter) {
   594  			return CertificateInvalidError{
   595  				Cert:   c,
   596  				Reason: Expired,
   597  				Detail: fmt.Sprintf("current time %s is after %s", now.Format(time.RFC3339), c.NotAfter.Format(time.RFC3339)),
   598  			}
   599  		}
   600  	}
   601  
   602  	maxConstraintComparisons := opts.MaxConstraintComparisions
   603  	if maxConstraintComparisons == 0 {
   604  		maxConstraintComparisons = 250000
   605  	}
   606  	comparisonCount := 0
   607  
   608  	var leaf *Certificate
   609  	if certType == intermediateCertificate || certType == rootCertificate {
   610  		if len(currentChain) == 0 {
   611  			return errors.New("x509: internal error: empty chain when appending CA cert")
   612  		}
   613  		leaf = currentChain[0]
   614  	}
   615  
   616  	checkNameConstraints := !opts.DisableNameConstraintChecks && (certType == intermediateCertificate || certType == rootCertificate) && c.hasNameConstraints()
   617  	if checkNameConstraints && leaf.commonNameAsHostname() {
   618  		// This is the deprecated, legacy case of depending on the commonName as
   619  		// a hostname. We don't enforce name constraints against the CN, but
   620  		// VerifyHostname will look for hostnames in there if there are no SANs.
   621  		// In order to ensure VerifyHostname will not accept an unchecked name,
   622  		// return an error here.
   623  		return CertificateInvalidError{c, NameConstraintsWithoutSANs, ""}
   624  	} else if checkNameConstraints && leaf.hasSANExtension() {
   625  		err := forEachSAN(leaf.getSANExtension(), func(tag int, data []byte) error {
   626  			switch tag {
   627  			case nameTypeEmail:
   628  				name := string(data)
   629  				mailbox, ok := parseRFC2821Mailbox(name)
   630  				if !ok {
   631  					return fmt.Errorf("x509: cannot parse rfc822Name %q", mailbox)
   632  				}
   633  
   634  				if err := c.checkNameConstraints(&comparisonCount, maxConstraintComparisons, "email address", name, mailbox,
   635  					func(parsedName, constraint interface{}) (bool, error) {
   636  						return matchEmailConstraint(parsedName.(rfc2821Mailbox), constraint.(string))
   637  					}, c.PermittedEmailAddresses, c.ExcludedEmailAddresses); err != nil {
   638  					return err
   639  				}
   640  
   641  			case nameTypeDNS:
   642  				name := string(data)
   643  				if _, ok := domainToReverseLabels(name); !ok {
   644  					return fmt.Errorf("x509: cannot parse dnsName %q", name)
   645  				}
   646  
   647  				if err := c.checkNameConstraints(&comparisonCount, maxConstraintComparisons, "DNS name", name, name,
   648  					func(parsedName, constraint interface{}) (bool, error) {
   649  						return matchDomainConstraint(parsedName.(string), constraint.(string))
   650  					}, c.PermittedDNSDomains, c.ExcludedDNSDomains); err != nil {
   651  					return err
   652  				}
   653  
   654  			case nameTypeURI:
   655  				name := string(data)
   656  				uri, err := url.Parse(name)
   657  				if err != nil {
   658  					return fmt.Errorf("x509: internal error: URI SAN %q failed to parse", name)
   659  				}
   660  
   661  				if err := c.checkNameConstraints(&comparisonCount, maxConstraintComparisons, "URI", name, uri,
   662  					func(parsedName, constraint interface{}) (bool, error) {
   663  						return matchURIConstraint(parsedName.(*url.URL), constraint.(string))
   664  					}, c.PermittedURIDomains, c.ExcludedURIDomains); err != nil {
   665  					return err
   666  				}
   667  
   668  			case nameTypeIP:
   669  				ip := net.IP(data)
   670  				if l := len(ip); l != net.IPv4len && l != net.IPv6len {
   671  					return fmt.Errorf("x509: internal error: IP SAN %x failed to parse", data)
   672  				}
   673  
   674  				if err := c.checkNameConstraints(&comparisonCount, maxConstraintComparisons, "IP address", ip.String(), ip,
   675  					func(parsedName, constraint interface{}) (bool, error) {
   676  						return matchIPConstraint(parsedName.(net.IP), constraint.(*net.IPNet))
   677  					}, c.PermittedIPRanges, c.ExcludedIPRanges); err != nil {
   678  					return err
   679  				}
   680  
   681  			default:
   682  				// Unknown SAN types are ignored.
   683  			}
   684  
   685  			return nil
   686  		})
   687  
   688  		if err != nil {
   689  			return err
   690  		}
   691  	}
   692  
   693  	// KeyUsage status flags are ignored. From Engineering Security, Peter
   694  	// Gutmann: A European government CA marked its signing certificates as
   695  	// being valid for encryption only, but no-one noticed. Another
   696  	// European CA marked its signature keys as not being valid for
   697  	// signatures. A different CA marked its own trusted root certificate
   698  	// as being invalid for certificate signing. Another national CA
   699  	// distributed a certificate to be used to encrypt data for the
   700  	// country’s tax authority that was marked as only being usable for
   701  	// digital signatures but not for encryption. Yet another CA reversed
   702  	// the order of the bit flags in the keyUsage due to confusion over
   703  	// encoding endianness, essentially setting a random keyUsage in
   704  	// certificates that it issued. Another CA created a self-invalidating
   705  	// certificate by adding a certificate policy statement stipulating
   706  	// that the certificate had to be used strictly as specified in the
   707  	// keyUsage, and a keyUsage containing a flag indicating that the RSA
   708  	// encryption key could only be used for Diffie-Hellman key agreement.
   709  
   710  	if certType == intermediateCertificate && (!c.BasicConstraintsValid || !c.IsCA) {
   711  		return CertificateInvalidError{c, NotAuthorizedToSign, ""}
   712  	}
   713  
   714  	if !opts.DisablePathLenChecks && c.BasicConstraintsValid && c.MaxPathLen >= 0 {
   715  		numIntermediates := len(currentChain) - 1
   716  		if numIntermediates > c.MaxPathLen {
   717  			return CertificateInvalidError{c, TooManyIntermediates, ""}
   718  		}
   719  	}
   720  
   721  	return nil
   722  }
   723  
   724  // Verify attempts to verify c by building one or more chains from c to a
   725  // certificate in opts.Roots, using certificates in opts.Intermediates if
   726  // needed. If successful, it returns one or more chains where the first
   727  // element of the chain is c and the last element is from opts.Roots.
   728  //
   729  // If opts.Roots is nil and system roots are unavailable the returned error
   730  // will be of type SystemRootsError.
   731  //
   732  // Name constraints in the intermediates will be applied to all names claimed
   733  // in the chain, not just opts.DNSName. Thus it is invalid for a leaf to claim
   734  // example.com if an intermediate doesn't permit it, even if example.com is not
   735  // the name being validated. Note that DirectoryName constraints are not
   736  // supported.
   737  //
   738  // Extended Key Usage values are enforced down a chain, so an intermediate or
   739  // root that enumerates EKUs prevents a leaf from asserting an EKU not in that
   740  // list.
   741  //
   742  // WARNING: this function doesn't do any revocation checking.
   743  func (c *Certificate) Verify(opts VerifyOptions) (chains [][]*Certificate, err error) {
   744  	// Platform-specific verification needs the ASN.1 contents so
   745  	// this makes the behavior consistent across platforms.
   746  	if len(c.Raw) == 0 {
   747  		return nil, errNotParsed
   748  	}
   749  	if opts.Intermediates != nil {
   750  		for _, intermediate := range opts.Intermediates.certs {
   751  			if len(intermediate.Raw) == 0 {
   752  				return nil, errNotParsed
   753  			}
   754  		}
   755  	}
   756  
   757  	// Use Windows's own verification and chain building.
   758  	if opts.Roots == nil && runtime.GOOS == "windows" {
   759  		return c.systemVerify(&opts)
   760  	}
   761  
   762  	if opts.Roots == nil {
   763  		opts.Roots = systemRootsPool()
   764  		if opts.Roots == nil {
   765  			return nil, SystemRootsError{systemRootsErr}
   766  		}
   767  	}
   768  
   769  	err = c.isValid(leafCertificate, nil, &opts)
   770  	if err != nil {
   771  		return
   772  	}
   773  
   774  	if len(opts.DNSName) > 0 {
   775  		err = c.VerifyHostname(opts.DNSName)
   776  		if err != nil {
   777  			return
   778  		}
   779  	}
   780  
   781  	var candidateChains [][]*Certificate
   782  	if opts.Roots.contains(c) {
   783  		candidateChains = append(candidateChains, []*Certificate{c})
   784  	} else {
   785  		if candidateChains, err = c.buildChains(nil, []*Certificate{c}, nil, &opts); err != nil {
   786  			return nil, err
   787  		}
   788  	}
   789  
   790  	keyUsages := opts.KeyUsages
   791  	if len(keyUsages) == 0 {
   792  		keyUsages = []ExtKeyUsage{ExtKeyUsageServerAuth}
   793  	}
   794  
   795  	// If any key usage is acceptable then we're done.
   796  	for _, usage := range keyUsages {
   797  		if usage == ExtKeyUsageAny {
   798  			return candidateChains, nil
   799  		}
   800  	}
   801  
   802  	for _, candidate := range candidateChains {
   803  		if opts.DisableEKUChecks || checkChainForKeyUsage(candidate, keyUsages) {
   804  			chains = append(chains, candidate)
   805  		}
   806  	}
   807  
   808  	if len(chains) == 0 {
   809  		return nil, CertificateInvalidError{c, IncompatibleUsage, ""}
   810  	}
   811  
   812  	return chains, nil
   813  }
   814  
   815  func appendToFreshChain(chain []*Certificate, cert *Certificate) []*Certificate {
   816  	n := make([]*Certificate, len(chain)+1)
   817  	copy(n, chain)
   818  	n[len(chain)] = cert
   819  	return n
   820  }
   821  
   822  // maxChainSignatureChecks is the maximum number of CheckSignatureFrom calls
   823  // that an invocation of buildChains will (tranistively) make. Most chains are
   824  // less than 15 certificates long, so this leaves space for multiple chains and
   825  // for failed checks due to different intermediates having the same Subject.
   826  const maxChainSignatureChecks = 100
   827  
   828  func (c *Certificate) buildChains(cache map[*Certificate][][]*Certificate, currentChain []*Certificate, sigChecks *int, opts *VerifyOptions) (chains [][]*Certificate, err error) {
   829  	var (
   830  		hintErr  error
   831  		hintCert *Certificate
   832  	)
   833  
   834  	considerCandidate := func(certType int, candidate *Certificate) {
   835  		for _, cert := range currentChain {
   836  			if cert.Equal(candidate) {
   837  				return
   838  			}
   839  		}
   840  
   841  		if sigChecks == nil {
   842  			sigChecks = new(int)
   843  		}
   844  		*sigChecks++
   845  		if *sigChecks > maxChainSignatureChecks {
   846  			err = errors.New("x509: signature check attempts limit reached while verifying certificate chain")
   847  			return
   848  		}
   849  
   850  		if err := c.CheckSignatureFrom(candidate); err != nil {
   851  			if hintErr == nil {
   852  				hintErr = err
   853  				hintCert = candidate
   854  			}
   855  			return
   856  		}
   857  
   858  		err = candidate.isValid(certType, currentChain, opts)
   859  		if err != nil {
   860  			return
   861  		}
   862  
   863  		switch certType {
   864  		case rootCertificate:
   865  			chains = append(chains, appendToFreshChain(currentChain, candidate))
   866  		case intermediateCertificate:
   867  			if cache == nil {
   868  				cache = make(map[*Certificate][][]*Certificate)
   869  			}
   870  			childChains, ok := cache[candidate]
   871  			if !ok {
   872  				childChains, err = candidate.buildChains(cache, appendToFreshChain(currentChain, candidate), sigChecks, opts)
   873  				cache[candidate] = childChains
   874  			}
   875  			chains = append(chains, childChains...)
   876  		}
   877  	}
   878  
   879  	for _, rootNum := range opts.Roots.findPotentialParents(c) {
   880  		considerCandidate(rootCertificate, opts.Roots.certs[rootNum])
   881  	}
   882  	for _, intermediateNum := range opts.Intermediates.findPotentialParents(c) {
   883  		considerCandidate(intermediateCertificate, opts.Intermediates.certs[intermediateNum])
   884  	}
   885  
   886  	if len(chains) > 0 {
   887  		err = nil
   888  	}
   889  	if len(chains) == 0 && err == nil {
   890  		err = UnknownAuthorityError{c, hintErr, hintCert}
   891  	}
   892  
   893  	return
   894  }
   895  
   896  // validHostname reports whether host is a valid hostname that can be matched or
   897  // matched against according to RFC 6125 2.2, with some leniency to accommodate
   898  // legacy values.
   899  func validHostname(host string) bool {
   900  	host = strings.TrimSuffix(host, ".")
   901  
   902  	if len(host) == 0 {
   903  		return false
   904  	}
   905  
   906  	for i, part := range strings.Split(host, ".") {
   907  		if part == "" {
   908  			// Empty label.
   909  			return false
   910  		}
   911  		if i == 0 && part == "*" {
   912  			// Only allow full left-most wildcards, as those are the only ones
   913  			// we match, and matching literal '*' characters is probably never
   914  			// the expected behavior.
   915  			continue
   916  		}
   917  		for j, c := range part {
   918  			if 'a' <= c && c <= 'z' {
   919  				continue
   920  			}
   921  			if '0' <= c && c <= '9' {
   922  				continue
   923  			}
   924  			if 'A' <= c && c <= 'Z' {
   925  				continue
   926  			}
   927  			if c == '-' && j != 0 {
   928  				continue
   929  			}
   930  			if c == '_' || c == ':' {
   931  				// Not valid characters in hostnames, but commonly
   932  				// found in deployments outside the WebPKI.
   933  				continue
   934  			}
   935  			return false
   936  		}
   937  	}
   938  
   939  	return true
   940  }
   941  
   942  // commonNameAsHostname reports whether the Common Name field should be
   943  // considered the hostname that the certificate is valid for. This is a legacy
   944  // behavior, disabled if the Subject Alt Name extension is present.
   945  //
   946  // It applies the strict validHostname check to the Common Name field, so that
   947  // certificates without SANs can still be validated against CAs with name
   948  // constraints if there is no risk the CN would be matched as a hostname.
   949  // See NameConstraintsWithoutSANs and issue 24151.
   950  func (c *Certificate) commonNameAsHostname() bool {
   951  	return !ignoreCN && !c.hasSANExtension() && validHostname(c.Subject.CommonName)
   952  }
   953  
   954  func matchHostnames(pattern, host string) bool {
   955  	host = strings.TrimSuffix(host, ".")
   956  	pattern = strings.TrimSuffix(pattern, ".")
   957  
   958  	if len(pattern) == 0 || len(host) == 0 {
   959  		return false
   960  	}
   961  
   962  	patternParts := strings.Split(pattern, ".")
   963  	hostParts := strings.Split(host, ".")
   964  
   965  	if len(patternParts) != len(hostParts) {
   966  		return false
   967  	}
   968  
   969  	for i, patternPart := range patternParts {
   970  		if i == 0 && patternPart == "*" {
   971  			continue
   972  		}
   973  		if patternPart != hostParts[i] {
   974  			return false
   975  		}
   976  	}
   977  
   978  	return true
   979  }
   980  
   981  // toLowerCaseASCII returns a lower-case version of in. See RFC 6125 6.4.1. We use
   982  // an explicitly ASCII function to avoid any sharp corners resulting from
   983  // performing Unicode operations on DNS labels.
   984  func toLowerCaseASCII(in string) string {
   985  	// If the string is already lower-case then there's nothing to do.
   986  	isAlreadyLowerCase := true
   987  	for _, c := range in {
   988  		if c == utf8.RuneError {
   989  			// If we get a UTF-8 error then there might be
   990  			// upper-case ASCII bytes in the invalid sequence.
   991  			isAlreadyLowerCase = false
   992  			break
   993  		}
   994  		if 'A' <= c && c <= 'Z' {
   995  			isAlreadyLowerCase = false
   996  			break
   997  		}
   998  	}
   999  
  1000  	if isAlreadyLowerCase {
  1001  		return in
  1002  	}
  1003  
  1004  	out := []byte(in)
  1005  	for i, c := range out {
  1006  		if 'A' <= c && c <= 'Z' {
  1007  			out[i] += 'a' - 'A'
  1008  		}
  1009  	}
  1010  	return string(out)
  1011  }
  1012  
  1013  // VerifyHostname returns nil if c is a valid certificate for the named host.
  1014  // Otherwise it returns an error describing the mismatch.
  1015  func (c *Certificate) VerifyHostname(h string) error {
  1016  	// IP addresses may be written in [ ].
  1017  	candidateIP := h
  1018  	if len(h) >= 3 && h[0] == '[' && h[len(h)-1] == ']' {
  1019  		candidateIP = h[1 : len(h)-1]
  1020  	}
  1021  	if ip := net.ParseIP(candidateIP); ip != nil {
  1022  		// We only match IP addresses against IP SANs.
  1023  		// See RFC 6125, Appendix B.2.
  1024  		for _, candidate := range c.IPAddresses {
  1025  			if ip.Equal(candidate) {
  1026  				return nil
  1027  			}
  1028  		}
  1029  		return HostnameError{c, candidateIP}
  1030  	}
  1031  
  1032  	lowered := toLowerCaseASCII(h)
  1033  
  1034  	if c.commonNameAsHostname() {
  1035  		if matchHostnames(toLowerCaseASCII(c.Subject.CommonName), lowered) {
  1036  			return nil
  1037  		}
  1038  	} else {
  1039  		for _, match := range c.DNSNames {
  1040  			if matchHostnames(toLowerCaseASCII(match), lowered) {
  1041  				return nil
  1042  			}
  1043  		}
  1044  	}
  1045  
  1046  	return HostnameError{c, h}
  1047  }
  1048  
  1049  func checkChainForKeyUsage(chain []*Certificate, keyUsages []ExtKeyUsage) bool {
  1050  	usages := make([]ExtKeyUsage, len(keyUsages))
  1051  	copy(usages, keyUsages)
  1052  
  1053  	if len(chain) == 0 {
  1054  		return false
  1055  	}
  1056  
  1057  	usagesRemaining := len(usages)
  1058  
  1059  	// We walk down the list and cross out any usages that aren't supported
  1060  	// by each certificate. If we cross out all the usages, then the chain
  1061  	// is unacceptable.
  1062  
  1063  NextCert:
  1064  	for i := len(chain) - 1; i >= 0; i-- {
  1065  		cert := chain[i]
  1066  		if len(cert.ExtKeyUsage) == 0 && len(cert.UnknownExtKeyUsage) == 0 {
  1067  			// The certificate doesn't have any extended key usage specified.
  1068  			continue
  1069  		}
  1070  
  1071  		for _, usage := range cert.ExtKeyUsage {
  1072  			if usage == ExtKeyUsageAny {
  1073  				// The certificate is explicitly good for any usage.
  1074  				continue NextCert
  1075  			}
  1076  		}
  1077  
  1078  		const invalidUsage ExtKeyUsage = -1
  1079  
  1080  	NextRequestedUsage:
  1081  		for i, requestedUsage := range usages {
  1082  			if requestedUsage == invalidUsage {
  1083  				continue
  1084  			}
  1085  
  1086  			for _, usage := range cert.ExtKeyUsage {
  1087  				if requestedUsage == usage {
  1088  					continue NextRequestedUsage
  1089  				} else if requestedUsage == ExtKeyUsageServerAuth &&
  1090  					(usage == ExtKeyUsageNetscapeServerGatedCrypto ||
  1091  						usage == ExtKeyUsageMicrosoftServerGatedCrypto) {
  1092  					// In order to support COMODO
  1093  					// certificate chains, we have to
  1094  					// accept Netscape or Microsoft SGC
  1095  					// usages as equal to ServerAuth.
  1096  					continue NextRequestedUsage
  1097  				}
  1098  			}
  1099  
  1100  			usages[i] = invalidUsage
  1101  			usagesRemaining--
  1102  			if usagesRemaining == 0 {
  1103  				return false
  1104  			}
  1105  		}
  1106  	}
  1107  
  1108  	return true
  1109  }
  1110  

View as plain text