Source file src/crypto/tls/upki.go

     1  // Copyright 2026 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 tls
     6  
     7  import (
     8  	"crypto/internal/upki"
     9  	"crypto/x509"
    10  	"errors"
    11  	"fmt"
    12  	"internal/godebug"
    13  	"os"
    14  	"path/filepath"
    15  	"runtime"
    16  )
    17  
    18  // tlsupki is a GODEBUG setting that can be set to 0 to disable UPKI
    19  // revocation checking for configurations that rely on platform-default
    20  // cache discovery.
    21  var tlsupki = godebug.New("tlsupki")
    22  
    23  // UPKIRevocationConfig controls local, offline UPKI revocation checking of
    24  // server certificates.
    25  //
    26  // It doesn't configure downloading or updating revocation data.
    27  type UPKIRevocationConfig struct {
    28  	// Disabled disables UPKI revocation checking, including automatic
    29  	// discovery of a platform-default cache.
    30  	Disabled bool
    31  
    32  	// Directory is a C2SP upki-revocation cache directory. It contains
    33  	// index.bin directly.
    34  	//
    35  	// If Directory is empty, platform-default directories are searched for
    36  	// index.bin in user-before-system order. Discovery skips missing paths,
    37  	// non-directory path components, and paths the process lacks permission
    38  	// to open.
    39  	Directory string
    40  
    41  	// MissingData controls the result when a lookup needs an index but its
    42  	// path is missing, a parent is not a directory, or access is denied.
    43  	// By default, the connection proceeds if automatic discovery finds no
    44  	// available index. [UPKIRevocationPolicyReject] requires an available index.
    45  	// Errors reading or parsing an opened index or its filters are fatal.
    46  	MissingData UPKIRevocationPolicy
    47  
    48  	// NotCovered controls the result when the revocation data cannot
    49  	// determine a certificate's status, including when covering filters have
    50  	// no information about the certificate's issuer. By default the connection
    51  	// proceeds. [UPKIRevocationPolicyReject] produces an error if there isn't
    52  	// sufficient coverage to make a revocation determination.
    53  	NotCovered UPKIRevocationPolicy
    54  
    55  	// MissingSCTs controls the result when a certificate contains no embedded
    56  	// Signed Certificate Timestamps. A malformed embedded SCT extension is an
    57  	// error if a cache is explicitly configured or found during discovery.
    58  	// By default a certificate without SCTs proceeds without error.
    59  	// [UPKIRevocationPolicyReject] produces an error if the SCTs required for
    60  	// a revocation determination aren't present.
    61  	MissingSCTs UPKIRevocationPolicy
    62  }
    63  
    64  // ErrUPKIRevoked is returned as a wrapped [CertificateVerificationError]
    65  // when a certificate is determined to be revoked.
    66  var ErrUPKIRevoked = errors.New("certificate is revoked according to UPKI")
    67  
    68  // UPKIRevocationPolicy controls the result of an inconclusive UPKI revocation
    69  // check.
    70  type UPKIRevocationPolicy uint8
    71  
    72  const (
    73  	// UPKIRevocationPolicyDefault uses the default for the corresponding
    74  	// UPKIRevocationConfig field.
    75  	UPKIRevocationPolicyDefault UPKIRevocationPolicy = iota
    76  
    77  	// UPKIRevocationPolicyAllow allows the connection to proceed when the
    78  	// revocation check is inconclusive for the corresponding reason.
    79  	UPKIRevocationPolicyAllow
    80  
    81  	// UPKIRevocationPolicyReject rejects the connection when the revocation
    82  	// check is inconclusive for the corresponding reason.
    83  	UPKIRevocationPolicyReject
    84  )
    85  
    86  func (c *Config) checkUPKIRevocation(chains [][]*x509.Certificate) error {
    87  	// ECH rejection can produce verified chains for the public name even
    88  	// when InsecureSkipVerify is set.
    89  	if len(chains) == 0 {
    90  		return nil
    91  	}
    92  
    93  	cfg := c.UPKIRevocation
    94  	missingDataRejects, err := upkiPolicyReject(cfg.MissingData, cfg.Directory != "")
    95  	if err != nil {
    96  		return err
    97  	}
    98  	notCoveredRejects, err := upkiPolicyReject(cfg.NotCovered, false)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	missingSCTsRejects, err := upkiPolicyReject(cfg.MissingSCTs, false)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	if cfg.Disabled {
   108  		return nil
   109  	}
   110  	// GODEBUG=tlsupki=0 turns off the default-on checking. A Config that
   111  	// names a cache directory explicitly keeps checking it.
   112  	if cfg.Directory == "" && tlsupki.Value() == "0" {
   113  		tlsupki.IncNonDefault()
   114  
   115  		return nil
   116  	}
   117  
   118  	// During discovery, leave malformed SCTs for upki.Check to report if
   119  	// we find an index. MissingSCTs applies before any cache access.
   120  	scts, err := upki.ExtractSCTs(chains[0][0])
   121  	if err != nil && cfg.Directory != "" {
   122  		return fmt.Errorf("UPKI revocation check failed: extracting SCTs: %w", err)
   123  	}
   124  	if err == nil && len(scts) == 0 {
   125  		if missingSCTsRejects {
   126  			return errors.New("UPKI revocation check requires embedded SCTs")
   127  		}
   128  
   129  		return nil
   130  	}
   131  
   132  	directories := []string{cfg.Directory}
   133  	if cfg.Directory == "" {
   134  		directories = defaultUPKIRevocationDirectories()
   135  	}
   136  	dir, index, err := openUPKICacheIndex(directories)
   137  	if err != nil {
   138  		if errors.Is(err, upki.ErrNoData) && !missingDataRejects {
   139  			return nil
   140  		}
   141  		return err
   142  	}
   143  
   144  	result, err := upki.Check(dir, index, chains)
   145  	if err != nil {
   146  		if errors.Is(err, upki.ErrNoData) && cfg.MissingData == UPKIRevocationPolicyAllow {
   147  			return nil
   148  		}
   149  
   150  		return fmt.Errorf("UPKI revocation check failed: %w", err)
   151  	}
   152  
   153  	switch result {
   154  	case upki.Revoked:
   155  		return ErrUPKIRevoked
   156  	case upki.NotRevoked:
   157  		return nil
   158  	case upki.NotCovered:
   159  		if notCoveredRejects {
   160  			return errors.New("certificate is not covered by UPKI revocation data")
   161  		}
   162  
   163  		return nil
   164  	default:
   165  		return fmt.Errorf("UPKI revocation check returned invalid result %d", result)
   166  	}
   167  }
   168  
   169  func upkiPolicyReject(policy UPKIRevocationPolicy, defaultReject bool) (bool, error) {
   170  	switch policy {
   171  	case UPKIRevocationPolicyDefault:
   172  		return defaultReject, nil
   173  	case UPKIRevocationPolicyAllow:
   174  		return false, nil
   175  	case UPKIRevocationPolicyReject:
   176  		return true, nil
   177  	default:
   178  		return false, fmt.Errorf("invalid UPKI revocation policy %d", policy)
   179  	}
   180  }
   181  
   182  func openUPKICacheIndex(directories []string) (string, *os.File, error) {
   183  	unavailable := upki.ErrNoData
   184  	for _, dir := range directories {
   185  		index, err := upki.OpenIndex(filepath.Join(dir, upki.IndexFilename))
   186  		if err == nil {
   187  			return dir, index, nil
   188  		}
   189  		if !errors.Is(err, upki.ErrNoData) {
   190  			return "", nil, fmt.Errorf("opening UPKI revocation index: %w", err)
   191  		}
   192  		// Prefer an inaccessible path over a missing one in the diagnostic.
   193  		if unavailable == upki.ErrNoData || errors.Is(unavailable, os.ErrNotExist) {
   194  			unavailable = err
   195  		}
   196  	}
   197  
   198  	return "", nil, unavailable
   199  }
   200  
   201  func defaultUPKIRevocationDirectories() []string {
   202  	userCache, _ := os.UserCacheDir()
   203  	return upkiRevocationDirectories(runtime.GOOS, userCache, os.Getenv("ProgramData"))
   204  }
   205  
   206  func upkiRevocationDirectories(goos, userCache, programData string) []string {
   207  	var directories []string
   208  	if userCache != "" {
   209  		switch goos {
   210  		case "darwin":
   211  			directories = append(directories, filepath.Join(userCache, "dev.rustls.upki", "revocation"))
   212  		case "windows":
   213  			directories = append(directories, filepath.Join(userCache, "rustls", "upki", "cache", "revocation"))
   214  		default:
   215  			directories = append(directories, filepath.Join(userCache, "upki", "revocation"))
   216  		}
   217  	}
   218  
   219  	switch goos {
   220  	case "darwin":
   221  		directories = append(directories, "/Library/Caches/dev.rustls.upki/revocation")
   222  	case "windows":
   223  		if programData == "" {
   224  			programData = `C:\ProgramData`
   225  		}
   226  		directories = append(directories, filepath.Join(programData, "rustls", "upki", "cache", "revocation"))
   227  	case "aix", "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
   228  		directories = append(directories, "/var/cache/upki/revocation")
   229  	}
   230  
   231  	return directories
   232  }
   233  
   234  func (c *Conn) verifyUPKIRevocation() error {
   235  	if err := c.config.checkUPKIRevocation(c.verifiedChains); err != nil {
   236  		c.sendAlert(alertBadCertificate)
   237  		return &CertificateVerificationError{
   238  			UnverifiedCertificates: c.peerCertificates,
   239  			Err:                    err,
   240  		}
   241  	}
   242  
   243  	return nil
   244  }
   245  

View as plain text