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  var tlsmlkem = godebug.New("tlsmlkem")
    17  
    18  // defaultCurvePreferences is the default set of supported key exchanges, as
    19  // well as the preference order.
    20  func defaultCurvePreferences() []CurveID {
    21  	if tlsmlkem.Value() == "0" {
    22  		return []CurveID{X25519, CurveP256, CurveP384, CurveP521}
    23  	}
    24  	return []CurveID{X25519MLKEM768, X25519, CurveP256, CurveP384, CurveP521}
    25  }
    26  
    27  // defaultSupportedSignatureAlgorithms contains the signature and hash algorithms that
    28  // the code advertises as supported in a TLS 1.2+ ClientHello and in a TLS 1.2+
    29  // CertificateRequest. The two fields are merged to match with TLS 1.3.
    30  // Note that in TLS 1.2, the ECDSA algorithms are not constrained to P-256, etc.
    31  var defaultSupportedSignatureAlgorithms = []SignatureScheme{
    32  	PSSWithSHA256,
    33  	ECDSAWithP256AndSHA256,
    34  	Ed25519,
    35  	PSSWithSHA384,
    36  	PSSWithSHA512,
    37  	PKCS1WithSHA256,
    38  	PKCS1WithSHA384,
    39  	PKCS1WithSHA512,
    40  	ECDSAWithP384AndSHA384,
    41  	ECDSAWithP521AndSHA512,
    42  	PKCS1WithSHA1,
    43  	ECDSAWithSHA1,
    44  }
    45  
    46  var tlsrsakex = godebug.New("tlsrsakex")
    47  var tls3des = godebug.New("tls3des")
    48  
    49  func supportedCipherSuites(aesGCMPreferred bool) []uint16 {
    50  	if aesGCMPreferred {
    51  		return slices.Clone(cipherSuitesPreferenceOrder)
    52  	} else {
    53  		return slices.Clone(cipherSuitesPreferenceOrderNoAES)
    54  	}
    55  }
    56  
    57  func defaultCipherSuites(aesGCMPreferred bool) []uint16 {
    58  	cipherSuites := supportedCipherSuites(aesGCMPreferred)
    59  	return slices.DeleteFunc(cipherSuites, func(c uint16) bool {
    60  		return disabledCipherSuites[c] ||
    61  			tlsrsakex.Value() != "1" && rsaKexCiphers[c] ||
    62  			tls3des.Value() != "1" && tdesCiphers[c]
    63  	})
    64  }
    65  
    66  // defaultCipherSuitesTLS13 is also the preference order, since there are no
    67  // disabled by default TLS 1.3 cipher suites. The same AES vs ChaCha20 logic as
    68  // cipherSuitesPreferenceOrder applies.
    69  //
    70  // defaultCipherSuitesTLS13 should be an internal detail,
    71  // but widely used packages access it using linkname.
    72  // Notable members of the hall of shame include:
    73  //   - github.com/quic-go/quic-go
    74  //   - github.com/sagernet/quic-go
    75  //
    76  // Do not remove or change the type signature.
    77  // See go.dev/issue/67401.
    78  //
    79  //go:linkname defaultCipherSuitesTLS13
    80  var defaultCipherSuitesTLS13 = []uint16{
    81  	TLS_AES_128_GCM_SHA256,
    82  	TLS_AES_256_GCM_SHA384,
    83  	TLS_CHACHA20_POLY1305_SHA256,
    84  }
    85  
    86  // defaultCipherSuitesTLS13NoAES should be an internal detail,
    87  // but widely used packages access it using linkname.
    88  // Notable members of the hall of shame include:
    89  //   - github.com/quic-go/quic-go
    90  //   - github.com/sagernet/quic-go
    91  //
    92  // Do not remove or change the type signature.
    93  // See go.dev/issue/67401.
    94  //
    95  //go:linkname defaultCipherSuitesTLS13NoAES
    96  var defaultCipherSuitesTLS13NoAES = []uint16{
    97  	TLS_CHACHA20_POLY1305_SHA256,
    98  	TLS_AES_128_GCM_SHA256,
    99  	TLS_AES_256_GCM_SHA384,
   100  }
   101  

View as plain text