u_common.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // extensions with 'fake' prefix break connection, if server echoes them back
  19. fakeExtensionChannelID uint16 = 30032 // not IANA assigned
  20. fakeCertCompressionAlgs uint16 = 0x001b
  21. fakeRecordSizeLimit uint16 = 0x001c
  22. )
  23. const (
  24. OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc13)
  25. OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc14)
  26. DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = uint16(0xc024)
  27. DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = uint16(0xc028)
  28. DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256 = uint16(0x003d)
  29. FAKE_OLD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc15) // we can try to craft these ciphersuites
  30. FAKE_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = uint16(0x009e) // from existing pieces, if needed
  31. FAKE_TLS_DHE_RSA_WITH_AES_128_CBC_SHA = uint16(0x0033)
  32. FAKE_TLS_DHE_RSA_WITH_AES_256_CBC_SHA = uint16(0x0039)
  33. FAKE_TLS_RSA_WITH_RC4_128_MD5 = uint16(0x0004)
  34. FAKE_TLS_EMPTY_RENEGOTIATION_INFO_SCSV = uint16(0x00ff)
  35. )
  36. // newest signatures
  37. var (
  38. FakePKCS1WithSHA224 SignatureScheme = 0x0301
  39. FakeECDSAWithSHA224 SignatureScheme = 0x0303
  40. // fakeEd25519 = SignatureAndHash{0x08, 0x07}
  41. // fakeEd448 = SignatureAndHash{0x08, 0x08}
  42. )
  43. // fake curves(groups)
  44. var (
  45. FakeFFDHE2048 = uint16(0x0100)
  46. FakeFFDHE3072 = uint16(0x0101)
  47. )
  48. type ClientHelloID struct {
  49. Browser string
  50. Version uint16
  51. // TODO: consider adding OS?
  52. }
  53. func (p *ClientHelloID) Str() string {
  54. return fmt.Sprintf("%s-%d", p.Browser, p.Version)
  55. }
  56. const (
  57. helloGolang = "Golang"
  58. helloRandomized = "Randomized"
  59. helloCustom = "Custom"
  60. helloFirefox = "Firefox"
  61. helloChrome = "Chrome"
  62. helloIOS = "iOS"
  63. helloAndroid = "Android"
  64. )
  65. const (
  66. helloAutoVers = iota
  67. helloRandomizedALPN
  68. helloRandomizedNoALPN
  69. )
  70. type ClientHelloSpec struct {
  71. CipherSuites []uint16 // nil => default
  72. CompressionMethods []uint8 // nil => no compression
  73. Extensions []TLSExtension // nil => no extensions
  74. TLSVersMin uint16 // [1.0-1.3]
  75. TLSVersMax uint16 // [1.2-1.3]
  76. // GreaseStyle: currently only random
  77. // sessionID may or may not depend on ticket; nil => random
  78. GetSessionID func(ticket []byte) [32]byte
  79. // TLSFingerprintLink string // ?? link to tlsfingerprint.io for informational purposes
  80. }
  81. var (
  82. // HelloGolang will use default "crypto/tls" handshake marshaling codepath, which WILL
  83. // overwrite your changes to Hello(Config, Session are fine).
  84. // You might want to call BuildHandshakeState() before applying any changes.
  85. // UConn.Extensions will be completely ignored.
  86. HelloGolang = ClientHelloID{helloGolang, helloAutoVers}
  87. // HelloCustom will prepare ClientHello with empty uconn.Extensions so you can fill it with
  88. // TLSExtensions manually or use ApplyPreset function
  89. HelloCustom = ClientHelloID{helloCustom, helloAutoVers}
  90. // HelloRandomized* randomly adds/reorders extensions, ciphersuites, etc.
  91. HelloRandomized = ClientHelloID{helloRandomized, helloAutoVers}
  92. HelloRandomizedALPN = ClientHelloID{helloRandomized, helloRandomizedALPN}
  93. HelloRandomizedNoALPN = ClientHelloID{helloRandomized, helloRandomizedNoALPN}
  94. // The rest will will parrot given browser.
  95. HelloFirefox_Auto = HelloFirefox_63
  96. HelloFirefox_55 = ClientHelloID{helloFirefox, 55}
  97. HelloFirefox_56 = ClientHelloID{helloFirefox, 56}
  98. HelloFirefox_63 = ClientHelloID{helloFirefox, 63}
  99. HelloChrome_Auto = HelloChrome_70
  100. HelloChrome_58 = ClientHelloID{helloChrome, 58}
  101. HelloChrome_62 = ClientHelloID{helloChrome, 62}
  102. HelloChrome_70 = ClientHelloID{helloChrome, 70}
  103. HelloIOS_Auto = HelloIOS_11_1
  104. HelloIOS_11_1 = ClientHelloID{helloIOS, 111}
  105. )
  106. // based on spec's GreaseStyle, GREASE_PLACEHOLDER may be replaced by another GREASE value
  107. // https://tools.ietf.org/html/draft-ietf-tls-grease-01
  108. const GREASE_PLACEHOLDER = 0x0a0a
  109. // utlsMacSHA384 returns a SHA-384 based MAC. These are only supported in TLS 1.2
  110. // so the given version is ignored.
  111. func utlsMacSHA384(version uint16, key []byte) macFunction {
  112. return tls10MAC{h: hmac.New(sha512.New384, key)}
  113. }
  114. var utlsSupportedCipherSuites []*cipherSuite
  115. func init() {
  116. utlsSupportedCipherSuites = append(cipherSuites, []*cipherSuite{
  117. {OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheRSAKA,
  118. suiteECDHE | suiteTLS12 | suiteDefaultOff, nil, nil, aeadChaCha20Poly1305},
  119. {OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheECDSAKA,
  120. suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, nil, nil, aeadChaCha20Poly1305},
  121. }...)
  122. }
  123. // EnableWeakCiphers allows utls connections to continue in some cases, when weak cipher was chosen.
  124. // This provides better compatibility with servers on the web, but weakens security. Feel free
  125. // to use this option if you establish additional secure connection inside of utls connection.
  126. // This option does not change the shape of parrots (i.e. same ciphers will be offered either way).
  127. // Must be called before establishing any connections.
  128. func EnableWeakCiphers() {
  129. utlsSupportedCipherSuites = append(cipherSuites, []*cipherSuite{
  130. {DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, rsaKA,
  131. suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  132. {DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheECDSAKA,
  133. suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  134. {DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheRSAKA,
  135. suiteECDHE | suiteTLS12 | suiteDefaultOff | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  136. }...)
  137. }