u_common.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "hash"
  12. "log"
  13. "github.com/Psiphon-Labs/utls/internal/helper"
  14. "golang.org/x/crypto/cryptobyte"
  15. )
  16. // Naming convention:
  17. // Unsupported things are prefixed with "Fake"
  18. // Things, supported by utls, but not crypto/tls' are prefixed with "utls"
  19. // Supported things, that have changed their ID are prefixed with "Old"
  20. // Supported but disabled things are prefixed with "Disabled". We will _enable_ them.
  21. // TLS handshake message types.
  22. const (
  23. utlsTypeEncryptedExtensions uint8 = 8 // implemention incomplete by crypto/tls
  24. // https://datatracker.ietf.org/doc/html/rfc8879#section-7.2
  25. utlsTypeCompressedCertificate uint8 = 25
  26. )
  27. // TLS
  28. const (
  29. extensionNextProtoNeg uint16 = 13172 // not IANA assigned. Removed by crypto/tls since Nov 2019
  30. utlsExtensionPadding uint16 = 21
  31. utlsExtensionCompressCertificate uint16 = 27 // https://datatracker.ietf.org/doc/html/rfc8879#section-7.1
  32. utlsExtensionApplicationSettings uint16 = 17513 // not IANA assigned
  33. utlsFakeExtensionCustom uint16 = 1234 // not IANA assigned, for ALPS
  34. utlsExtensionECH uint16 = 0xfe0d // draft-ietf-tls-esni-17
  35. utlsExtensionECHOuterExtensions uint16 = 0xfd00 // draft-ietf-tls-esni-17
  36. // extensions with 'fake' prefix break connection, if server echoes them back
  37. fakeExtensionEncryptThenMAC uint16 = 22
  38. fakeExtensionTokenBinding uint16 = 24
  39. fakeExtensionDelegatedCredentials uint16 = 34
  40. fakeExtensionPreSharedKey uint16 = 41
  41. fakeOldExtensionChannelID uint16 = 30031 // not IANA assigned
  42. fakeExtensionChannelID uint16 = 30032 // not IANA assigned
  43. )
  44. const (
  45. OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc13)
  46. OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc14)
  47. DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = uint16(0xc024)
  48. DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = uint16(0xc028)
  49. DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256 = uint16(0x003d)
  50. FAKE_OLD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = uint16(0xcc15) // we can try to craft these ciphersuites
  51. FAKE_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = uint16(0x009e) // from existing pieces, if needed
  52. FAKE_TLS_DHE_RSA_WITH_AES_128_CBC_SHA = uint16(0x0033)
  53. FAKE_TLS_DHE_RSA_WITH_AES_256_CBC_SHA = uint16(0x0039)
  54. FAKE_TLS_RSA_WITH_RC4_128_MD5 = uint16(0x0004)
  55. FAKE_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = uint16(0x009f)
  56. FAKE_TLS_DHE_DSS_WITH_AES_128_CBC_SHA = uint16(0x0032)
  57. FAKE_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = uint16(0x006b)
  58. FAKE_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = uint16(0x0067)
  59. FAKE_TLS_EMPTY_RENEGOTIATION_INFO_SCSV = uint16(0x00ff)
  60. // https://docs.microsoft.com/en-us/dotnet/api/system.net.security.tlsciphersuite?view=netcore-3.1
  61. FAKE_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA = uint16(0xc008)
  62. )
  63. const (
  64. CurveSECP256R1 CurveID = 0x0017
  65. CurveSECP384R1 CurveID = 0x0018
  66. CurveSECP521R1 CurveID = 0x0019
  67. CurveX25519 CurveID = 0x001d
  68. FakeCurveFFDHE2048 CurveID = 0x0100
  69. FakeCurveFFDHE3072 CurveID = 0x0101
  70. FakeCurveFFDHE4096 CurveID = 0x0102
  71. FakeCurveFFDHE6144 CurveID = 0x0103
  72. FakeCurveFFDHE8192 CurveID = 0x0104
  73. )
  74. // Other things
  75. const (
  76. fakeRecordSizeLimit uint16 = 0x001c
  77. )
  78. // newest signatures
  79. var (
  80. FakePKCS1WithSHA224 SignatureScheme = 0x0301
  81. FakeECDSAWithSHA224 SignatureScheme = 0x0303
  82. FakeSHA1WithDSA SignatureScheme = 0x0202
  83. FakeSHA256WithDSA SignatureScheme = 0x0402
  84. // fakeEd25519 = SignatureAndHash{0x08, 0x07}
  85. // fakeEd448 = SignatureAndHash{0x08, 0x08}
  86. )
  87. // fake curves(groups)
  88. var (
  89. FakeFFDHE2048 = uint16(0x0100)
  90. FakeFFDHE3072 = uint16(0x0101)
  91. )
  92. // https://tools.ietf.org/html/draft-ietf-tls-certificate-compression-04
  93. type CertCompressionAlgo uint16
  94. const (
  95. CertCompressionZlib CertCompressionAlgo = 0x0001
  96. CertCompressionBrotli CertCompressionAlgo = 0x0002
  97. CertCompressionZstd CertCompressionAlgo = 0x0003
  98. )
  99. const (
  100. PskModePlain uint8 = pskModePlain
  101. PskModeDHE uint8 = pskModeDHE
  102. )
  103. type ClientHelloID struct {
  104. Client string
  105. // Version specifies version of a mimicked clients (e.g. browsers).
  106. // Not used in randomized, custom handshake, and default Go.
  107. Version string
  108. // Seed is only used for randomized fingerprints to seed PRNG.
  109. // Must not be modified once set.
  110. Seed *PRNGSeed
  111. // Weights are only used for randomized fingerprints in func
  112. // generateRandomizedSpec(). Must not be modified once set.
  113. Weights *Weights
  114. }
  115. func (p *ClientHelloID) Str() string {
  116. return fmt.Sprintf("%s-%s", p.Client, p.Version)
  117. }
  118. func (p *ClientHelloID) IsSet() bool {
  119. return (p.Client == "") && (p.Version == "")
  120. }
  121. const (
  122. // clients
  123. helloGolang = "Golang"
  124. helloRandomized = "Randomized"
  125. helloRandomizedALPN = "Randomized-ALPN"
  126. helloRandomizedNoALPN = "Randomized-NoALPN"
  127. helloCustom = "Custom"
  128. helloFirefox = "Firefox"
  129. helloChrome = "Chrome"
  130. helloIOS = "iOS"
  131. helloAndroid = "Android"
  132. helloEdge = "Edge"
  133. helloSafari = "Safari"
  134. hello360 = "360Browser"
  135. helloQQ = "QQBrowser"
  136. // versions
  137. helloAutoVers = "0"
  138. )
  139. type ClientHelloSpec struct {
  140. CipherSuites []uint16 // nil => default
  141. CompressionMethods []uint8 // nil => no compression
  142. Extensions []TLSExtension // nil => no extensions
  143. TLSVersMin uint16 // [1.0-1.3] default: parse from .Extensions, if SupportedVersions ext is not present => 1.0
  144. TLSVersMax uint16 // [1.2-1.3] default: parse from .Extensions, if SupportedVersions ext is not present => 1.2
  145. // GreaseStyle: currently only random
  146. // sessionID may or may not depend on ticket; nil => random
  147. GetSessionID func(ticket []byte) [32]byte
  148. // TLSFingerprintLink string // ?? link to tlsfingerprint.io for informational purposes
  149. }
  150. // ReadCipherSuites is a helper function to construct a list of cipher suites from
  151. // a []byte into []uint16.
  152. //
  153. // example: []byte{0x13, 0x01, 0x13, 0x02, 0x13, 0x03} => []uint16{0x1301, 0x1302, 0x1303}
  154. func (chs *ClientHelloSpec) ReadCipherSuites(b []byte) error {
  155. cipherSuites := []uint16{}
  156. s := cryptobyte.String(b)
  157. for !s.Empty() {
  158. var suite uint16
  159. if !s.ReadUint16(&suite) {
  160. return errors.New("unable to read ciphersuite")
  161. }
  162. cipherSuites = append(cipherSuites, unGREASEUint16(suite))
  163. }
  164. chs.CipherSuites = cipherSuites
  165. return nil
  166. }
  167. // ReadCompressionMethods is a helper function to construct a list of compression
  168. // methods from a []byte into []uint8.
  169. func (chs *ClientHelloSpec) ReadCompressionMethods(compressionMethods []byte) error {
  170. chs.CompressionMethods = compressionMethods
  171. return nil
  172. }
  173. // ReadTLSExtensions is a helper function to construct a list of TLS extensions from
  174. // a byte slice into []TLSExtension.
  175. func (chs *ClientHelloSpec) ReadTLSExtensions(b []byte, allowBluntMimicry bool, realPSK bool) error {
  176. extensions := cryptobyte.String(b)
  177. for !extensions.Empty() {
  178. var extension uint16
  179. var extData cryptobyte.String
  180. if !extensions.ReadUint16(&extension) {
  181. return fmt.Errorf("unable to read extension ID")
  182. }
  183. if !extensions.ReadUint16LengthPrefixed(&extData) {
  184. return fmt.Errorf("unable to read data for extension %x", extension)
  185. }
  186. ext := ExtensionFromID(extension)
  187. extWriter, ok := ext.(TLSExtensionWriter)
  188. if ext != nil && ok { // known extension and implements TLSExtensionWriter properly
  189. switch extension {
  190. case extensionPreSharedKey:
  191. // PSK extension, need to see if we do real or fake PSK
  192. if realPSK {
  193. extWriter = &UtlsPreSharedKeyExtension{}
  194. } else {
  195. extWriter = &FakePreSharedKeyExtension{}
  196. }
  197. case extensionSupportedVersions:
  198. chs.TLSVersMin = 0
  199. chs.TLSVersMax = 0
  200. }
  201. if _, err := extWriter.Write(extData); err != nil {
  202. return err
  203. }
  204. chs.Extensions = append(chs.Extensions, extWriter)
  205. } else {
  206. if allowBluntMimicry {
  207. chs.Extensions = append(chs.Extensions, &GenericExtension{extension, extData})
  208. } else {
  209. return fmt.Errorf("unsupported extension %d", extension)
  210. }
  211. }
  212. }
  213. return nil
  214. }
  215. func (chs *ClientHelloSpec) AlwaysAddPadding() {
  216. alreadyHasPadding := false
  217. for idx, ext := range chs.Extensions {
  218. if _, ok := ext.(*UtlsPaddingExtension); ok {
  219. alreadyHasPadding = true
  220. break
  221. }
  222. if _, ok := ext.(PreSharedKeyExtension); ok {
  223. alreadyHasPadding = true // PSK must be last, so we can't append padding after it
  224. // instead we will insert padding before PSK
  225. chs.Extensions = append(chs.Extensions[:idx], append([]TLSExtension{&UtlsPaddingExtension{GetPaddingLen: BoringPaddingStyle}}, chs.Extensions[idx:]...)...)
  226. break
  227. }
  228. }
  229. if !alreadyHasPadding {
  230. chs.Extensions = append(chs.Extensions, &UtlsPaddingExtension{GetPaddingLen: BoringPaddingStyle})
  231. }
  232. }
  233. // Import TLS ClientHello data from client.tlsfingerprint.io:8443
  234. //
  235. // data is a map of []byte with following keys:
  236. // - cipher_suites: [10, 10, 19, 1, 19, 2, 19, 3, 192, 43, 192, 47, 192, 44, 192, 48, 204, 169, 204, 168, 192, 19, 192, 20, 0, 156, 0, 157, 0, 47, 0, 53]
  237. // - compression_methods: [0] => null
  238. // - extensions: [10, 10, 255, 1, 0, 45, 0, 35, 0, 16, 68, 105, 0, 11, 0, 43, 0, 18, 0, 13, 0, 0, 0, 10, 0, 27, 0, 5, 0, 51, 0, 23, 10, 10, 0, 21]
  239. // - pt_fmts (ec_point_formats): [1, 0] => len: 1, content: 0x00
  240. // - sig_algs (signature_algorithms): [0, 16, 4, 3, 8, 4, 4, 1, 5, 3, 8, 5, 5, 1, 8, 6, 6, 1] => len: 16, content: 0x0403, 0x0804, 0x0401, 0x0503, 0x0805, 0x0501, 0x0806, 0x0601
  241. // - supported_versions: [10, 10, 3, 4, 3, 3] => 0x0a0a, 0x0304, 0x0303 (GREASE, TLS 1.3, TLS 1.2)
  242. // - curves (named_groups, supported_groups): [0, 8, 10, 10, 0, 29, 0, 23, 0, 24] => len: 8, content: GREASE, 0x001d, 0x0017, 0x0018
  243. // - alpn: [0, 12, 2, 104, 50, 8, 104, 116, 116, 112, 47, 49, 46, 49] => len: 12, content: h2, http/1.1
  244. // - key_share: [10, 10, 0, 1, 0, 29, 0, 32] => {group: 0x0a0a, len:1}, {group: 0x001d, len:32}
  245. // - psk_key_exchange_modes: [1] => psk_dhe_ke(0x01)
  246. // - cert_compression_algs: [2, 0, 2] => brotli (0x0002)
  247. // - record_size_limit: [0, 255] => 255
  248. //
  249. // TLSVersMin/TLSVersMax are set to 0 if supported_versions is present.
  250. // To prevent conflict, they should be set manually if needed BEFORE calling this function.
  251. func (chs *ClientHelloSpec) ImportTLSClientHello(data map[string][]byte) error {
  252. var tlsExtensionTypes []uint16
  253. var err error
  254. if data["cipher_suites"] == nil {
  255. return errors.New("cipher_suites is required")
  256. }
  257. chs.CipherSuites, err = helper.Uint8to16(data["cipher_suites"])
  258. if err != nil {
  259. return err
  260. }
  261. if data["compression_methods"] == nil {
  262. return errors.New("compression_methods is required")
  263. }
  264. chs.CompressionMethods = data["compression_methods"]
  265. if data["extensions"] == nil {
  266. return errors.New("extensions is required")
  267. }
  268. tlsExtensionTypes, err = helper.Uint8to16(data["extensions"])
  269. if err != nil {
  270. return err
  271. }
  272. for _, extType := range tlsExtensionTypes {
  273. extension := ExtensionFromID(extType)
  274. extWriter, ok := extension.(TLSExtensionWriter)
  275. if !ok {
  276. return fmt.Errorf("unsupported extension %d", extType)
  277. }
  278. if extension == nil || !ok {
  279. log.Printf("[Warning] Unsupported extension %d added as a &GenericExtension without Data", extType)
  280. chs.Extensions = append(chs.Extensions, &GenericExtension{extType, []byte{}})
  281. } else {
  282. switch extType {
  283. case extensionSupportedPoints:
  284. if data["pt_fmts"] == nil {
  285. return errors.New("pt_fmts is required")
  286. }
  287. _, err = extWriter.Write(data["pt_fmts"])
  288. if err != nil {
  289. return err
  290. }
  291. case extensionSignatureAlgorithms:
  292. if data["sig_algs"] == nil {
  293. return errors.New("sig_algs is required")
  294. }
  295. _, err = extWriter.Write(data["sig_algs"])
  296. if err != nil {
  297. return err
  298. }
  299. case extensionSupportedVersions:
  300. chs.TLSVersMin = 0
  301. chs.TLSVersMax = 0
  302. if data["supported_versions"] == nil {
  303. return errors.New("supported_versions is required")
  304. }
  305. // need to add uint8 length prefix
  306. fixedData := make([]byte, len(data["supported_versions"])+1)
  307. fixedData[0] = uint8(len(data["supported_versions"]) & 0xff)
  308. copy(fixedData[1:], data["supported_versions"])
  309. _, err = extWriter.Write(fixedData)
  310. if err != nil {
  311. return err
  312. }
  313. case extensionSupportedCurves:
  314. if data["curves"] == nil {
  315. return errors.New("curves is required")
  316. }
  317. _, err = extWriter.Write(data["curves"])
  318. if err != nil {
  319. return err
  320. }
  321. case extensionALPN:
  322. if data["alpn"] == nil {
  323. return errors.New("alpn is required")
  324. }
  325. _, err = extWriter.Write(data["alpn"])
  326. if err != nil {
  327. return err
  328. }
  329. case extensionKeyShare:
  330. if data["key_share"] == nil {
  331. return errors.New("key_share is required")
  332. }
  333. // need to add (zero) data per each key share, [10, 10, 0, 1] => [10, 10, 0, 1, 0]
  334. fixedData := make([]byte, 0)
  335. for i := 0; i < len(data["key_share"]); i += 4 {
  336. fixedData = append(fixedData, data["key_share"][i:i+4]...)
  337. for j := 0; j < int(data["key_share"][i+3]); j++ {
  338. fixedData = append(fixedData, 0)
  339. }
  340. }
  341. // add uint16 length prefix
  342. fixedData = append([]byte{uint8(len(fixedData) >> 8), uint8(len(fixedData) & 0xff)}, fixedData...)
  343. _, err = extWriter.Write(fixedData)
  344. if err != nil {
  345. return err
  346. }
  347. case extensionPSKModes:
  348. if data["psk_key_exchange_modes"] == nil {
  349. return errors.New("psk_key_exchange_modes is required")
  350. }
  351. // need to add uint8 length prefix
  352. fixedData := make([]byte, len(data["psk_key_exchange_modes"])+1)
  353. fixedData[0] = uint8(len(data["psk_key_exchange_modes"]) & 0xff)
  354. copy(fixedData[1:], data["psk_key_exchange_modes"])
  355. _, err = extWriter.Write(fixedData)
  356. if err != nil {
  357. return err
  358. }
  359. case utlsExtensionCompressCertificate:
  360. if data["cert_compression_algs"] == nil {
  361. return errors.New("cert_compression_algs is required")
  362. }
  363. // need to add uint8 length prefix
  364. fixedData := make([]byte, len(data["cert_compression_algs"])+1)
  365. fixedData[0] = uint8(len(data["cert_compression_algs"]) & 0xff)
  366. copy(fixedData[1:], data["cert_compression_algs"])
  367. _, err = extWriter.Write(fixedData)
  368. if err != nil {
  369. return err
  370. }
  371. case fakeRecordSizeLimit:
  372. if data["record_size_limit"] == nil {
  373. return errors.New("record_size_limit is required")
  374. }
  375. _, err = extWriter.Write(data["record_size_limit"]) // uint16 as []byte
  376. if err != nil {
  377. return err
  378. }
  379. case utlsExtensionApplicationSettings:
  380. // TODO: tlsfingerprint.io should record/provide application settings data
  381. extWriter.(*ApplicationSettingsExtension).SupportedProtocols = []string{"h2"}
  382. case extensionPreSharedKey:
  383. log.Printf("[Warning] PSK extension added without data")
  384. default:
  385. if !isGREASEUint16(extType) {
  386. log.Printf("[Warning] extension %d added without data", extType)
  387. } /*else {
  388. log.Printf("[Warning] GREASE extension added but ID/Data discarded. They will be automatically re-GREASEd on ApplyPreset() call.")
  389. }*/
  390. }
  391. chs.Extensions = append(chs.Extensions, extWriter)
  392. }
  393. }
  394. return nil
  395. }
  396. // ImportTLSClientHelloFromJSON imports ClientHelloSpec from JSON data from client.tlsfingerprint.io format
  397. //
  398. // It calls ImportTLSClientHello internally after unmarshaling JSON data into map[string][]byte
  399. func (chs *ClientHelloSpec) ImportTLSClientHelloFromJSON(jsonB []byte) error {
  400. var data map[string][]byte
  401. err := json.Unmarshal(jsonB, &data)
  402. if err != nil {
  403. return err
  404. }
  405. return chs.ImportTLSClientHello(data)
  406. }
  407. // FromRaw converts a ClientHello message in the form of raw bytes into a ClientHelloSpec.
  408. //
  409. // ctrlFlags: []bool{bluntMimicry, realPSK}
  410. func (chs *ClientHelloSpec) FromRaw(raw []byte, ctrlFlags ...bool) error {
  411. if chs == nil {
  412. return errors.New("cannot unmarshal into nil ClientHelloSpec")
  413. }
  414. var bluntMimicry = false
  415. var realPSK = false
  416. if len(ctrlFlags) > 0 {
  417. bluntMimicry = ctrlFlags[0]
  418. }
  419. if len(ctrlFlags) > 1 {
  420. realPSK = ctrlFlags[1]
  421. }
  422. *chs = ClientHelloSpec{} // reset
  423. s := cryptobyte.String(raw)
  424. var contentType uint8
  425. var recordVersion uint16
  426. if !s.ReadUint8(&contentType) || // record type
  427. !s.ReadUint16(&recordVersion) || !s.Skip(2) { // record version and length
  428. return errors.New("unable to read record type, version, and length")
  429. }
  430. if recordType(contentType) != recordTypeHandshake {
  431. return errors.New("record is not a handshake")
  432. }
  433. var handshakeVersion uint16
  434. var handshakeType uint8
  435. if !s.ReadUint8(&handshakeType) || !s.Skip(3) || // message type and 3 byte length
  436. !s.ReadUint16(&handshakeVersion) || !s.Skip(32) { // 32 byte random
  437. return errors.New("unable to read handshake message type, length, and random")
  438. }
  439. if handshakeType != typeClientHello {
  440. return errors.New("handshake message is not a ClientHello")
  441. }
  442. chs.TLSVersMin = recordVersion
  443. chs.TLSVersMax = handshakeVersion
  444. var ignoredSessionID cryptobyte.String
  445. if !s.ReadUint8LengthPrefixed(&ignoredSessionID) {
  446. return errors.New("unable to read session id")
  447. }
  448. // CipherSuites
  449. var cipherSuitesBytes cryptobyte.String
  450. if !s.ReadUint16LengthPrefixed(&cipherSuitesBytes) {
  451. return errors.New("unable to read ciphersuites")
  452. }
  453. if err := chs.ReadCipherSuites(cipherSuitesBytes); err != nil {
  454. return err
  455. }
  456. // CompressionMethods
  457. var compressionMethods cryptobyte.String
  458. if !s.ReadUint8LengthPrefixed(&compressionMethods) {
  459. return errors.New("unable to read compression methods")
  460. }
  461. if err := chs.ReadCompressionMethods(compressionMethods); err != nil {
  462. return err
  463. }
  464. if s.Empty() {
  465. // Extensions are optional
  466. return nil
  467. }
  468. var extensions cryptobyte.String
  469. if !s.ReadUint16LengthPrefixed(&extensions) {
  470. return errors.New("unable to read extensions data")
  471. }
  472. if err := chs.ReadTLSExtensions(extensions, bluntMimicry, realPSK); err != nil {
  473. return err
  474. }
  475. // if extension list includes padding, we update the padding-to-len according to
  476. // the raw ClientHello length
  477. for _, ext := range chs.Extensions {
  478. if _, ok := ext.(*UtlsPaddingExtension); ok {
  479. ext.(*UtlsPaddingExtension).GetPaddingLen = AlwaysPadToLen(len(raw) - 5)
  480. break
  481. }
  482. }
  483. return nil
  484. }
  485. // UnmarshalJSON unmarshals a ClientHello message in the form of JSON into a ClientHelloSpec.
  486. func (chs *ClientHelloSpec) UnmarshalJSON(jsonB []byte) error {
  487. var chsju ClientHelloSpecJSONUnmarshaler
  488. if err := json.Unmarshal(jsonB, &chsju); err != nil {
  489. return err
  490. }
  491. *chs = chsju.ClientHelloSpec()
  492. return nil
  493. }
  494. var (
  495. // HelloGolang will use default "crypto/tls" handshake marshaling codepath, which WILL
  496. // overwrite your changes to Hello(Config, Session are fine).
  497. // You might want to call BuildHandshakeState() before applying any changes.
  498. // UConn.Extensions will be completely ignored.
  499. HelloGolang = ClientHelloID{helloGolang, helloAutoVers, nil, nil}
  500. // HelloCustom will prepare ClientHello with empty uconn.Extensions so you can fill it with
  501. // TLSExtensions manually or use ApplyPreset function
  502. HelloCustom = ClientHelloID{helloCustom, helloAutoVers, nil, nil}
  503. // HelloRandomized* randomly adds/reorders extensions, ciphersuites, etc.
  504. HelloRandomized = ClientHelloID{helloRandomized, helloAutoVers, nil, nil}
  505. HelloRandomizedALPN = ClientHelloID{helloRandomizedALPN, helloAutoVers, nil, nil}
  506. HelloRandomizedNoALPN = ClientHelloID{helloRandomizedNoALPN, helloAutoVers, nil, nil}
  507. // The rest will will parrot given browser.
  508. HelloFirefox_Auto = HelloFirefox_120
  509. HelloFirefox_55 = ClientHelloID{helloFirefox, "55", nil, nil}
  510. HelloFirefox_56 = ClientHelloID{helloFirefox, "56", nil, nil}
  511. HelloFirefox_63 = ClientHelloID{helloFirefox, "63", nil, nil}
  512. HelloFirefox_65 = ClientHelloID{helloFirefox, "65", nil, nil}
  513. HelloFirefox_99 = ClientHelloID{helloFirefox, "99", nil, nil}
  514. HelloFirefox_102 = ClientHelloID{helloFirefox, "102", nil, nil}
  515. HelloFirefox_105 = ClientHelloID{helloFirefox, "105", nil, nil}
  516. HelloFirefox_120 = ClientHelloID{helloFirefox, "120", nil, nil}
  517. HelloChrome_Auto = HelloChrome_120
  518. HelloChrome_58 = ClientHelloID{helloChrome, "58", nil, nil}
  519. HelloChrome_62 = ClientHelloID{helloChrome, "62", nil, nil}
  520. HelloChrome_70 = ClientHelloID{helloChrome, "70", nil, nil}
  521. HelloChrome_72 = ClientHelloID{helloChrome, "72", nil, nil}
  522. HelloChrome_83 = ClientHelloID{helloChrome, "83", nil, nil}
  523. HelloChrome_87 = ClientHelloID{helloChrome, "87", nil, nil}
  524. HelloChrome_96 = ClientHelloID{helloChrome, "96", nil, nil}
  525. HelloChrome_100 = ClientHelloID{helloChrome, "100", nil, nil}
  526. HelloChrome_102 = ClientHelloID{helloChrome, "102", nil, nil}
  527. HelloChrome_106_Shuffle = ClientHelloID{helloChrome, "106", nil, nil} // TLS Extension shuffler enabled starting from 106
  528. // Chrome w/ PSK: Chrome start sending this ClientHello after doing TLS 1.3 handshake with the same server.
  529. // Beta: PSK extension added. However, uTLS doesn't ship with full PSK support.
  530. // Use at your own discretion.
  531. HelloChrome_PSK_Auto = HelloChrome_114_Padding_PSK_Shuf
  532. HelloChrome_100_PSK = ClientHelloID{helloChrome, "100_PSK", nil, nil}
  533. HelloChrome_112_PSK_Shuf = ClientHelloID{helloChrome, "112_PSK", nil, nil}
  534. HelloChrome_114_Padding_PSK_Shuf = ClientHelloID{helloChrome, "114_PSK", nil, nil}
  535. // Chrome w/ Post-Quantum Key Agreement
  536. // Beta: PQ extension added. However, uTLS doesn't ship with full PQ support. Use at your own discretion.
  537. HelloChrome_115_PQ = ClientHelloID{helloChrome, "115_PQ", nil, nil}
  538. HelloChrome_115_PQ_PSK = ClientHelloID{helloChrome, "115_PQ_PSK", nil, nil}
  539. // Chrome ECH
  540. HelloChrome_120 = ClientHelloID{helloChrome, "120", nil, nil}
  541. // Chrome w/ Post-Quantum Key Agreement and Encrypted ClientHello
  542. HelloChrome_120_PQ = ClientHelloID{helloChrome, "120_PQ", nil, nil}
  543. HelloIOS_Auto = HelloIOS_14
  544. HelloIOS_11_1 = ClientHelloID{helloIOS, "111", nil, nil} // legacy "111" means 11.1
  545. HelloIOS_12_1 = ClientHelloID{helloIOS, "12.1", nil, nil}
  546. HelloIOS_13 = ClientHelloID{helloIOS, "13", nil, nil}
  547. HelloIOS_14 = ClientHelloID{helloIOS, "14", nil, nil}
  548. HelloAndroid_11_OkHttp = ClientHelloID{helloAndroid, "11", nil, nil}
  549. HelloEdge_Auto = HelloEdge_85 // HelloEdge_106 seems to be incompatible with this library
  550. HelloEdge_85 = ClientHelloID{helloEdge, "85", nil, nil}
  551. HelloEdge_106 = ClientHelloID{helloEdge, "106", nil, nil}
  552. HelloSafari_Auto = HelloSafari_16_0
  553. HelloSafari_16_0 = ClientHelloID{helloSafari, "16.0", nil, nil}
  554. Hello360_Auto = Hello360_7_5 // Hello360_11_0 seems to be incompatible with this library
  555. Hello360_7_5 = ClientHelloID{hello360, "7.5", nil, nil}
  556. Hello360_11_0 = ClientHelloID{hello360, "11.0", nil, nil}
  557. HelloQQ_Auto = HelloQQ_11_1
  558. HelloQQ_11_1 = ClientHelloID{helloQQ, "11.1", nil, nil}
  559. )
  560. type Weights struct {
  561. Extensions_Append_ALPN float64
  562. TLSVersMax_Set_VersionTLS13 float64
  563. CipherSuites_Remove_RandomCiphers float64
  564. SigAndHashAlgos_Append_ECDSAWithSHA1 float64
  565. SigAndHashAlgos_Append_ECDSAWithP521AndSHA512 float64
  566. SigAndHashAlgos_Append_PSSWithSHA256 float64
  567. SigAndHashAlgos_Append_PSSWithSHA384_PSSWithSHA512 float64
  568. CurveIDs_Append_X25519 float64
  569. CurveIDs_Append_CurveP521 float64
  570. Extensions_Append_Padding float64
  571. Extensions_Append_Status float64
  572. Extensions_Append_SCT float64
  573. Extensions_Append_Reneg float64
  574. Extensions_Append_EMS float64
  575. FirstKeyShare_Set_CurveP256 float64
  576. Extensions_Append_ALPS float64
  577. }
  578. // Do not modify them directly as they may being used. If you
  579. // want to use your custom weights, please make a copy first.
  580. var DefaultWeights = Weights{
  581. Extensions_Append_ALPN: 0.7,
  582. TLSVersMax_Set_VersionTLS13: 0.4,
  583. CipherSuites_Remove_RandomCiphers: 0.4,
  584. SigAndHashAlgos_Append_ECDSAWithSHA1: 0.63,
  585. SigAndHashAlgos_Append_ECDSAWithP521AndSHA512: 0.59,
  586. SigAndHashAlgos_Append_PSSWithSHA256: 0.51,
  587. SigAndHashAlgos_Append_PSSWithSHA384_PSSWithSHA512: 0.9,
  588. CurveIDs_Append_X25519: 0.71,
  589. CurveIDs_Append_CurveP521: 0.46,
  590. Extensions_Append_Padding: 0.62,
  591. Extensions_Append_Status: 0.74,
  592. Extensions_Append_SCT: 0.46,
  593. Extensions_Append_Reneg: 0.75,
  594. Extensions_Append_EMS: 0.77,
  595. FirstKeyShare_Set_CurveP256: 0.25,
  596. Extensions_Append_ALPS: 0.33,
  597. }
  598. // based on spec's GreaseStyle, GREASE_PLACEHOLDER may be replaced by another GREASE value
  599. // https://tools.ietf.org/html/draft-ietf-tls-grease-01
  600. const GREASE_PLACEHOLDER = 0x0a0a
  601. func isGREASEUint16(v uint16) bool {
  602. // First byte is same as second byte
  603. // and lowest nibble is 0xa
  604. return ((v >> 8) == v&0xff) && v&0xf == 0xa
  605. }
  606. func unGREASEUint16(v uint16) uint16 {
  607. if isGREASEUint16(v) {
  608. return GREASE_PLACEHOLDER
  609. } else {
  610. return v
  611. }
  612. }
  613. // utlsMacSHA384 returns a SHA-384 based MAC. These are only supported in TLS 1.2
  614. // so the given version is ignored.
  615. func utlsMacSHA384(key []byte) hash.Hash {
  616. return hmac.New(sha512.New384, key)
  617. }
  618. var utlsSupportedCipherSuites []*cipherSuite
  619. func init() {
  620. utlsSupportedCipherSuites = append(cipherSuites, []*cipherSuite{
  621. {OLD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheRSAKA,
  622. suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  623. {OLD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheECDSAKA,
  624. suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  625. }...)
  626. }
  627. // EnableWeakCiphers allows utls connections to continue in some cases, when weak cipher was chosen.
  628. // This provides better compatibility with servers on the web, but weakens security. Feel free
  629. // to use this option if you establish additional secure connection inside of utls connection.
  630. // This option does not change the shape of parrots (i.e. same ciphers will be offered either way).
  631. // Must be called before establishing any connections.
  632. func EnableWeakCiphers() {
  633. utlsSupportedCipherSuites = append(cipherSuites, []*cipherSuite{
  634. {DISABLED_TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, rsaKA,
  635. suiteTLS12, cipherAES, macSHA256, nil},
  636. {DISABLED_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheECDSAKA,
  637. suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  638. {DISABLED_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheRSAKA,
  639. suiteECDHE | suiteTLS12 | suiteSHA384, cipherAES, utlsMacSHA384, nil},
  640. }...)
  641. }
  642. func mapSlice[T any, U any](slice []T, transform func(T) U) []U {
  643. newSlice := make([]U, 0, len(slice))
  644. for _, t := range slice {
  645. newSlice = append(newSlice, transform(t))
  646. }
  647. return newSlice
  648. }
  649. func panicOnNil(caller string, params ...any) {
  650. for i, p := range params {
  651. if p == nil {
  652. panic(fmt.Sprintf("tls: %s failed: the [%d] parameter is nil", caller, i))
  653. }
  654. }
  655. }
  656. func anyTrue[T any](slice []T, predicate func(i int, t *T) bool) bool {
  657. for i := 0; i < len(slice); i++ {
  658. if predicate(i, &slice[i]) {
  659. return true
  660. }
  661. }
  662. return false
  663. }
  664. func allTrue[T any](slice []T, predicate func(i int, t *T) bool) bool {
  665. for i := 0; i < len(slice); i++ {
  666. if !predicate(i, &slice[i]) {
  667. return false
  668. }
  669. }
  670. return true
  671. }
  672. func uAssert(condition bool, msg string) {
  673. if !condition {
  674. panic(msg)
  675. }
  676. }
  677. func sliceEq[T comparable](sliceA []T, sliceB []T) bool {
  678. if len(sliceA) != len(sliceB) {
  679. return false
  680. }
  681. for i := 0; i < len(sliceA); i++ {
  682. if sliceA[i] != sliceB[i] {
  683. return false
  684. }
  685. }
  686. return true
  687. }
  688. type Initializable interface {
  689. // IsInitialized returns a boolean indicating whether the extension has been initialized.
  690. // If false is returned, utls will initialize the extension.
  691. IsInitialized() bool
  692. }