u_common.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2017 Google Inc. 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. package tls
  5. import (
  6. "crypto/hmac"
  7. "crypto/sha512"
  8. "fmt"
  9. )
  10. // Naming convention:
  11. // Unsupported things are prefixed with "Fake"
  12. // Things, supported by utls, but not crypto/tls' are prefixed with "utls"
  13. // Supported things, that have changed their ID are prefixed with "Old"
  14. // Supported but disabled things are prefixed with "Disabled". We will _enable_ them.
  15. const (
  16. utlsExtensionPadding uint16 = 21
  17. utlsExtensionExtendedMasterSecret uint16 = 23 // https://tools.ietf.org/html/rfc7627
  18. // https://datatracker.ietf.org/doc/html/rfc8879#section-7.1
  19. utlsExtensionCompressCertificate uint16 = 27
  20. // extensions with 'fake' prefix break connection, if server echoes them back
  21. fakeExtensionChannelID uint16 = 30032 // not IANA assigned
  22. fakeRecordSizeLimit uint16 = 0x001c
  23. // https://datatracker.ietf.org/doc/html/rfc8879#section-7.2
  24. typeCompressedCertificate uint8 = 25
  25. )
  26. const (
  27. OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc13)
  28. OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc14)
  29. DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = uint16(0xc024)
  30. DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = uint16(0xc028)
  31. DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256 = uint16(0x003d)
  32. FAKE_OLD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc15) // we can try to craft these ciphersuites
  33. FAKE_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = uint16(0x009e) // from existing pieces, if needed
  34. FAKE_TLS_DHE_RSA_WITH_AES_128_CBC_SHA = uint16(0x0033)
  35. FAKE_TLS_DHE_RSA_WITH_AES_256_CBC_SHA = uint16(0x0039)
  36. FAKE_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = uint16(0x009f)
  37. FAKE_TLS_RSA_WITH_RC4_128_MD5 = uint16(0x0004)
  38. FAKE_TLS_EMPTY_RENEGOTIATION_INFO_SCSV = uint16(0x00ff)
  39. )
  40. // newest signatures
  41. var (
  42. FakePKCS1WithSHA224 SignatureScheme = 0x0301
  43. FakeECDSAWithSHA224 SignatureScheme = 0x0303
  44. // fakeEd25519 = SignatureAndHash{0x08, 0x07}
  45. // fakeEd448 = SignatureAndHash{0x08, 0x08}
  46. )
  47. // fake curves(groups)
  48. var (
  49. FakeFFDHE2048 = uint16(0x0100)
  50. FakeFFDHE3072 = uint16(0x0101)
  51. )
  52. // https://tools.ietf.org/html/draft-ietf-tls-certificate-compression-04
  53. type CertCompressionAlgo uint16
  54. const (
  55. CertCompressionZlib CertCompressionAlgo = 0x0001
  56. CertCompressionBrotli CertCompressionAlgo = 0x0002
  57. CertCompressionZstd CertCompressionAlgo = 0x0003
  58. )
  59. const (
  60. PskModePlain uint8 = pskModePlain
  61. PskModeDHE uint8 = pskModeDHE
  62. )
  63. type ClientHelloID struct {
  64. Client string
  65. // Version specifies version of a mimicked clients (e.g. browsers).
  66. // Not used in randomized, custom handshake, and default Go.
  67. Version string
  68. // Seed is only used for randomized fingerprints to seed PRNG.
  69. // Must not be modified once set.
  70. Seed *PRNGSeed
  71. }
  72. func (p *ClientHelloID) Str() string {
  73. return fmt.Sprintf("%s-%s", p.Client, p.Version)
  74. }
  75. func (p *ClientHelloID) IsSet() bool {
  76. return (p.Client == "") && (p.Version == "")
  77. }
  78. const (
  79. // clients
  80. helloGolang = "Golang"
  81. helloRandomized = "Randomized"
  82. helloRandomizedALPN = "Randomized-ALPN"
  83. helloRandomizedNoALPN = "Randomized-NoALPN"
  84. helloCustom = "Custom"
  85. helloFirefox = "Firefox"
  86. helloChrome = "Chrome"
  87. helloIOS = "iOS"
  88. helloAndroid = "Android"
  89. // versions
  90. helloAutoVers = "0"
  91. )
  92. type ClientHelloSpec struct {
  93. CipherSuites []uint16 // nil => default
  94. CompressionMethods []uint8 // nil => no compression
  95. Extensions []TLSExtension // nil => no extensions
  96. TLSVersMin uint16 // [1.0-1.3] default: parse from .Extensions, if SupportedVersions ext is not present => 1.0
  97. TLSVersMax uint16 // [1.2-1.3] default: parse from .Extensions, if SupportedVersions ext is not present => 1.2
  98. // GreaseStyle: currently only random
  99. // sessionID may or may not depend on ticket; nil => random
  100. GetSessionID func(ticket []byte) [32]byte
  101. // TLSFingerprintLink string // ?? link to tlsfingerprint.io for informational purposes
  102. }
  103. var (
  104. // HelloGolang will use default "crypto/tls" handshake marshaling codepath, which WILL
  105. // overwrite your changes to Hello(Config, Session are fine).
  106. // You might want to call BuildHandshakeState() before applying any changes.
  107. // UConn.Extensions will be completely ignored.
  108. HelloGolang = ClientHelloID{helloGolang, helloAutoVers, nil}
  109. // HelloCustom will prepare ClientHello with empty uconn.Extensions so you can fill it with
  110. // TLSExtensions manually or use ApplyPreset function
  111. HelloCustom = ClientHelloID{helloCustom, helloAutoVers, nil}
  112. // HelloRandomized* randomly adds/reorders extensions, ciphersuites, etc.
  113. HelloRandomized = ClientHelloID{helloRandomized, helloAutoVers, nil}
  114. HelloRandomizedALPN = ClientHelloID{helloRandomizedALPN, helloAutoVers, nil}
  115. HelloRandomizedNoALPN = ClientHelloID{helloRandomizedNoALPN, helloAutoVers, nil}
  116. // The rest will will parrot given browser.
  117. HelloFirefox_Auto = HelloFirefox_102
  118. HelloFirefox_55 = ClientHelloID{helloFirefox, "55", nil}
  119. HelloFirefox_56 = ClientHelloID{helloFirefox, "56", nil}
  120. HelloFirefox_63 = ClientHelloID{helloFirefox, "63", nil}
  121. HelloFirefox_65 = ClientHelloID{helloFirefox, "65", nil}
  122. HelloFirefox_99 = ClientHelloID{helloFirefox, "99", nil}
  123. HelloFirefox_102 = ClientHelloID{helloFirefox, "102", nil}
  124. HelloChrome_Auto = HelloChrome_102
  125. HelloChrome_58 = ClientHelloID{helloChrome, "58", nil}
  126. HelloChrome_62 = ClientHelloID{helloChrome, "62", nil}
  127. HelloChrome_70 = ClientHelloID{helloChrome, "70", nil}
  128. HelloChrome_72 = ClientHelloID{helloChrome, "72", nil}
  129. HelloChrome_83 = ClientHelloID{helloChrome, "83", nil}
  130. HelloChrome_87 = ClientHelloID{helloChrome, "87", nil}
  131. HelloChrome_96 = ClientHelloID{helloChrome, "96", nil}
  132. HelloChrome_100 = ClientHelloID{helloChrome, "100", nil}
  133. HelloChrome_102 = ClientHelloID{helloChrome, "102", nil}
  134. HelloIOS_Auto = HelloIOS_14
  135. HelloIOS_11_1 = ClientHelloID{helloIOS, "111", nil} // legacy "111" means 11.1
  136. HelloIOS_12_1 = ClientHelloID{helloIOS, "12.1", nil}
  137. HelloIOS_13 = ClientHelloID{helloIOS, "13", nil}
  138. HelloIOS_14 = ClientHelloID{helloIOS, "14", nil}
  139. HelloAndroid_11_OkHttp = ClientHelloID{helloAndroid, "11", nil}
  140. )
  141. // based on spec's GreaseStyle, GREASE_PLACEHOLDER may be replaced by another GREASE value
  142. // https://tools.ietf.org/html/draft-ietf-tls-grease-01
  143. const GREASE_PLACEHOLDER = 0x0a0a
  144. func isGREASEUint16(v uint16) bool {
  145. // First byte is same as second byte
  146. // and lowest nibble is 0xa
  147. return ((v >> 8) == v&0xff) && v&0xf == 0xa
  148. }
  149. func unGREASEUint16(v uint16) uint16 {
  150. if isGREASEUint16(v) {
  151. return GREASE_PLACEHOLDER
  152. } else {
  153. return v
  154. }
  155. }
  156. // utlsMacSHA384 returns a SHA-384 based MAC. These are only supported in TLS 1.2
  157. // so the given version is ignored.
  158. func utlsMacSHA384(version uint16, key []byte) macFunction {
  159. return tls10MAC{h: hmac.New(sha512.New384, key)}
  160. }
  161. var utlsSupportedCipherSuites []*cipherSuite
  162. func init() {
  163. utlsSupportedCipherSuites = append(cipherSuites, []*cipherSuite{
  164. {OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheRSAKA,
  165. suiteECDHE | suiteTLS12 | suiteDefaultOff, nil, nil, aeadChaCha20Poly1305},
  166. {OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheECDSAKA,
  167. suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, nil, nil, aeadChaCha20Poly1305},
  168. }...)
  169. }
  170. // EnableWeakCiphers allows utls connections to continue in some cases, when weak cipher was chosen.
  171. // This provides better compatibility with servers on the web, but weakens security. Feel free
  172. // to use this option if you establish additional secure connection inside of utls connection.
  173. // This option does not change the shape of parrots (i.e. same ciphers will be offered either way).
  174. // Must be called before establishing any connections.
  175. func EnableWeakCiphers() {
  176. utlsSupportedCipherSuites = append(cipherSuites, []*cipherSuite{
  177. {DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, rsaKA,
  178. suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  179. {DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheECDSAKA,
  180. suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  181. {DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheRSAKA,
  182. suiteECDHE | suiteTLS12 | suiteDefaultOff | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  183. }...)
  184. }