u_common.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. )
  21. const (
  22. OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc13)
  23. OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc14)
  24. DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = uint16(0xc024)
  25. DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = uint16(0xc028)
  26. DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256 = uint16(0x003d)
  27. FAKE_OLD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc15) // we can try to craft these ciphersuites
  28. FAKE_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = uint16(0x009e) // from existing pieces, if needed
  29. FAKE_TLS_DHE_RSA_WITH_AES_128_CBC_SHA = uint16(0x0033)
  30. FAKE_TLS_DHE_RSA_WITH_AES_256_CBC_SHA = uint16(0x0039)
  31. FAKE_TLS_RSA_WITH_RC4_128_MD5 = uint16(0x0004)
  32. FAKE_TLS_EMPTY_RENEGOTIATION_INFO_SCSV = uint16(0x00ff)
  33. )
  34. // newest signatures
  35. var (
  36. fakeRsaPssSha256 = SignatureAndHash{0x08, 0x04} // also declared in common.go as type SignatureScheme,
  37. fakeRsaPssSha384 = SignatureAndHash{0x08, 0x05} // but not used by default and not implemented
  38. fakeRsaPssSha512 = SignatureAndHash{0x08, 0x06}
  39. // fakeEd25519 = SignatureAndHash{0x08, 0x07}
  40. // fakeEd448 = SignatureAndHash{0x08, 0x08}
  41. )
  42. // IDs of hash functions in signatures
  43. const (
  44. disabledHashSHA512 uint8 = 6 // Supported, but disabled by default. Will be enabled, as needed
  45. fakeHashSHA224 uint8 = 3 // Supported, but we won't enable it: sounds esoteric and fishy
  46. )
  47. type ClientHelloID struct {
  48. Browser string
  49. Version uint16
  50. // TODO: consider adding OS?
  51. }
  52. func (p *ClientHelloID) Str() string {
  53. return fmt.Sprintf("%s-%d", p.Browser, p.Version)
  54. }
  55. const (
  56. helloGolang = "Golang"
  57. helloRandomized = "Randomized"
  58. helloCustom = "Custom"
  59. helloFirefox = "Firefox"
  60. helloChrome = "Chrome"
  61. helloAndroid = "Android"
  62. helloiOSSafari = "iOSSafari"
  63. )
  64. const (
  65. helloAutoVers = iota
  66. helloRandomizedALPN
  67. helloRandomizedNoALPN
  68. )
  69. var (
  70. // HelloGolang will use default "crypto/tls" handshake marshaling codepath, which WILL
  71. // overwrite your changes to Hello(Config, Session are fine).
  72. // You might want to call BuildHandshakeState() before applying any changes.
  73. // UConn.Extensions will be completely ignored.
  74. HelloGolang ClientHelloID = ClientHelloID{helloGolang, helloAutoVers}
  75. // HelloCustom will prepare ClientHello with empty uconn.Extensions so you can fill it with TLSExtension's manually
  76. HelloCustom ClientHelloID = ClientHelloID{helloCustom, helloAutoVers}
  77. // HelloRandomized* randomly adds/reorders extensions, ciphersuites, etc.
  78. HelloRandomized ClientHelloID = ClientHelloID{helloRandomized, helloAutoVers}
  79. HelloRandomizedALPN ClientHelloID = ClientHelloID{helloRandomized, helloRandomizedALPN}
  80. HelloRandomizedNoALPN ClientHelloID = ClientHelloID{helloRandomized, helloRandomizedNoALPN}
  81. // The rest will will parrot given browser.
  82. HelloFirefox_Auto ClientHelloID = ClientHelloID{helloFirefox, helloAutoVers}
  83. HelloFirefox_55 = ClientHelloID{helloFirefox, 55}
  84. HelloFirefox_56 = ClientHelloID{helloFirefox, 56}
  85. HelloChrome_Auto ClientHelloID = ClientHelloID{helloChrome, helloAutoVers}
  86. HelloChrome_57 ClientHelloID = ClientHelloID{helloChrome, 57}
  87. HelloChrome_58 ClientHelloID = ClientHelloID{helloChrome, 58}
  88. HelloChrome_62 ClientHelloID = ClientHelloID{helloChrome, 62}
  89. HelloAndroid_Auto ClientHelloID = ClientHelloID{helloAndroid, helloAutoVers}
  90. HelloAndroid_6_0_Browser ClientHelloID = ClientHelloID{helloAndroid, 23}
  91. HelloAndroid_5_1_Browser ClientHelloID = ClientHelloID{helloAndroid, 22}
  92. HelloiOSSafari_11_3_1 ClientHelloID = ClientHelloID{helloiOSSafari, 1131}
  93. )
  94. // utlsMacSHA384 returns a SHA-384.
  95. func utlsMacSHA384(version uint16, key []byte) macFunction {
  96. return tls10MAC{hmac.New(sha512.New384, key)}
  97. }
  98. var utlsSupportedSignatureAlgorithms []signatureAndHash
  99. var utlsSupportedCipherSuites []*cipherSuite
  100. func init() {
  101. utlsSupportedSignatureAlgorithms = append(supportedSignatureAlgorithms,
  102. []signatureAndHash{{disabledHashSHA512, signatureRSA}, {disabledHashSHA512, signatureECDSA}}...)
  103. utlsSupportedCipherSuites = append(cipherSuites, []*cipherSuite{
  104. {OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheRSAKA,
  105. suiteECDHE | suiteTLS12 | suiteDefaultOff, nil, nil, aeadChaCha20Poly1305},
  106. {OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheECDSAKA,
  107. suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, nil, nil, aeadChaCha20Poly1305},
  108. // The following weak ciphersuites are enabled for maximum compatibility,
  109. // given that we establish secure connections within the utls connection.
  110. {DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, rsaKA,
  111. suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  112. {DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheECDSAKA,
  113. suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  114. {DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheRSAKA,
  115. suiteECDHE | suiteTLS12 | suiteDefaultOff | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  116. }...)
  117. }