Source file src/crypto/tls/defaults.go

     1  // Copyright 2024 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  	"internal/godebug"
     9  	"slices"
    10  	_ "unsafe" // for linkname
    11  )
    12  
    13  // Defaults are collected in this file to allow distributions to more easily patch
    14  // them to apply local policies.
    15  
    16  // tlsmlkem=0 restores the pre-Go 1.24 default key exchanges.
    17  var tlsmlkem = godebug.New("tlsmlkem")
    18  
    19  // tlssecpmlkem=0 restores the pre-Go 1.26 default key exchanges.
    20  var tlssecpmlkem = godebug.New("tlssecpmlkem")
    21  
    22  // tlsechgrease=0 disables sending a GREASE'd encrypted_client_hello extension
    23  // on outer ClientHellos when no ECHConfigList is configured, restoring the
    24  // pre-Go 1.28 behavior. See RFC 9849, Section 6.2.
    25  var tlsechgrease = godebug.New("tlsechgrease")
    26  
    27  // defaultCurveEnabled returns whether the key exchange c is enabled by default.
    28  func defaultCurveEnabled(c CurveID) bool {
    29  	switch c {
    30  	case X25519, CurveP256, CurveP384, CurveP521:
    31  		return true
    32  	case X25519MLKEM768:
    33  		return tlsmlkem.Value() != "0"
    34  	case SecP256r1MLKEM768, SecP384r1MLKEM1024:
    35  		return tlsmlkem.Value() != "0" && tlssecpmlkem.Value() != "0"
    36  	default:
    37  		return false
    38  	}
    39  }
    40  
    41  // curvePreferenceOrder is the fixed preference order of key exchanges. It must
    42  // include every supported key exchange.
    43  func curvePreferenceOrder() []CurveID {
    44  	return []CurveID{
    45  		X25519MLKEM768, SecP256r1MLKEM768, SecP384r1MLKEM1024, MLKEM1024,
    46  		X25519, CurveP256, CurveP384, CurveP521,
    47  	}
    48  }
    49  
    50  // defaultSupportedSignatureAlgorithms returns the signature and hash algorithms that
    51  // the code advertises and supports in a TLS 1.2+ ClientHello and in a TLS 1.2+
    52  // CertificateRequest. The two fields are merged to match with TLS 1.3.
    53  // Note that in TLS 1.2, the ECDSA algorithms are not constrained to P-256, etc.
    54  func defaultSupportedSignatureAlgorithms() []SignatureScheme {
    55  	return []SignatureScheme{
    56  		MLDSA44,
    57  		MLDSA65,
    58  		MLDSA87,
    59  		PSSWithSHA256,
    60  		ECDSAWithP256AndSHA256,
    61  		Ed25519,
    62  		PSSWithSHA384,
    63  		PSSWithSHA512,
    64  		PKCS1WithSHA256,
    65  		PKCS1WithSHA384,
    66  		PKCS1WithSHA512,
    67  		ECDSAWithP384AndSHA384,
    68  		ECDSAWithP521AndSHA512,
    69  		PKCS1WithSHA1,
    70  		ECDSAWithSHA1,
    71  	}
    72  }
    73  
    74  func supportedCipherSuites(aesGCMPreferred bool) []uint16 {
    75  	if aesGCMPreferred {
    76  		return slices.Clone(cipherSuitesPreferenceOrder)
    77  	} else {
    78  		return slices.Clone(cipherSuitesPreferenceOrderNoAES)
    79  	}
    80  }
    81  
    82  func defaultCipherSuites(aesGCMPreferred bool) []uint16 {
    83  	cipherSuites := supportedCipherSuites(aesGCMPreferred)
    84  	return slices.DeleteFunc(cipherSuites, func(c uint16) bool {
    85  		return disabledCipherSuites[c]
    86  	})
    87  }
    88  
    89  // defaultCipherSuitesTLS13 is also the preference order, since there are no
    90  // disabled by default TLS 1.3 cipher suites. The same AES vs ChaCha20 logic as
    91  // cipherSuitesPreferenceOrder applies.
    92  //
    93  // defaultCipherSuitesTLS13 should be an internal detail,
    94  // but widely used packages access it using linkname.
    95  // Notable members of the hall of shame include:
    96  //   - github.com/quic-go/quic-go
    97  //   - github.com/sagernet/quic-go
    98  //
    99  // Do not remove or change the type signature.
   100  // See go.dev/issue/67401.
   101  //
   102  //go:linkname defaultCipherSuitesTLS13
   103  var defaultCipherSuitesTLS13 = []uint16{
   104  	TLS_AES_128_GCM_SHA256,
   105  	TLS_AES_256_GCM_SHA384,
   106  	TLS_CHACHA20_POLY1305_SHA256,
   107  }
   108  
   109  // defaultCipherSuitesTLS13NoAES should be an internal detail,
   110  // but widely used packages access it using linkname.
   111  // Notable members of the hall of shame include:
   112  //   - github.com/quic-go/quic-go
   113  //   - github.com/sagernet/quic-go
   114  //
   115  // Do not remove or change the type signature.
   116  // See go.dev/issue/67401.
   117  //
   118  //go:linkname defaultCipherSuitesTLS13NoAES
   119  var defaultCipherSuitesTLS13NoAES = []uint16{
   120  	TLS_CHACHA20_POLY1305_SHA256,
   121  	TLS_AES_128_GCM_SHA256,
   122  	TLS_AES_256_GCM_SHA384,
   123  }
   124  

View as plain text