u_conn.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. "bufio"
  7. "bytes"
  8. "context"
  9. "crypto/cipher"
  10. "encoding/binary"
  11. "errors"
  12. "fmt"
  13. "hash"
  14. "io"
  15. "net"
  16. "strconv"
  17. )
  18. type ClientHelloBuildStatus int
  19. const NotBuilt ClientHelloBuildStatus = 0
  20. const BuildByUtls ClientHelloBuildStatus = 1
  21. const BuildByGoTLS ClientHelloBuildStatus = 2
  22. type UConn struct {
  23. *Conn
  24. Extensions []TLSExtension
  25. ClientHelloID ClientHelloID
  26. sessionController *sessionController
  27. clientHelloBuildStatus ClientHelloBuildStatus
  28. clientHelloSpec *ClientHelloSpec
  29. HandshakeState PubClientHandshakeState
  30. greaseSeed [ssl_grease_last_index]uint16
  31. omitSNIExtension bool
  32. // skipResumptionOnNilExtension is copied from `Config.PreferSkipResumptionOnNilExtension`.
  33. //
  34. // By default, if ClientHelloSpec is predefined or utls-generated (as opposed to HelloCustom), this flag will be updated to true.
  35. skipResumptionOnNilExtension bool
  36. // certCompressionAlgs represents the set of advertised certificate compression
  37. // algorithms, as specified in the ClientHello. This is only relevant client-side, for the
  38. // server certificate. All other forms of certificate compression are unsupported.
  39. certCompressionAlgs []CertCompressionAlgo
  40. // ech extension is a shortcut to the ECH extension in the Extensions slice if there is one.
  41. ech ECHExtension
  42. // echContext represents the context for Encrypted ClientHello (ECH) operations.
  43. // It holds the necessary state and parameters required to manage ECH during
  44. // the TLS handshake process.
  45. echContext *echContext
  46. }
  47. // UClient returns a new uTLS client, with behavior depending on clientHelloID.
  48. // Config CAN be nil, but make sure to eventually specify ServerName.
  49. func UClient(conn net.Conn, config *Config, clientHelloID ClientHelloID) *UConn {
  50. if config == nil {
  51. config = &Config{}
  52. }
  53. tlsConn := Conn{conn: conn, config: config, isClient: true}
  54. handshakeState := PubClientHandshakeState{C: &tlsConn, Hello: &PubClientHelloMsg{}}
  55. uconn := UConn{Conn: &tlsConn, ClientHelloID: clientHelloID, HandshakeState: handshakeState}
  56. uconn.HandshakeState.uconn = &uconn
  57. uconn.handshakeFn = uconn.clientHandshake
  58. uconn.sessionController = newSessionController(&uconn)
  59. uconn.utls.sessionController = uconn.sessionController
  60. uconn.skipResumptionOnNilExtension = config.PreferSkipResumptionOnNilExtension || clientHelloID.Client != helloCustom
  61. return &uconn
  62. }
  63. // BuildHandshakeState behavior varies based on ClientHelloID and
  64. // whether it was already called before.
  65. // If HelloGolang:
  66. //
  67. // [only once] make default ClientHello and overwrite existing state
  68. //
  69. // If any other mimicking ClientHelloID is used:
  70. //
  71. // [only once] make ClientHello based on ID and overwrite existing state
  72. // [each call] apply uconn.Extensions config to internal crypto/tls structures
  73. // [each call] marshal ClientHello.
  74. //
  75. // BuildHandshakeState is automatically called before uTLS performs handshake,
  76. // and should only be called explicitly to inspect/change fields of
  77. // default/mimicked ClientHello.
  78. // With the excpetion of session ticket and psk extensions, which cannot be changed
  79. // after calling BuildHandshakeState, all other fields can be modified.
  80. func (uconn *UConn) BuildHandshakeState() error {
  81. return uconn.buildHandshakeState(true)
  82. }
  83. // BuildHandshakeStateWithoutSession is the same as BuildHandshakeState, but does not
  84. // set the session. This is only useful when you want to inspect the ClientHello before
  85. // setting the session manually through SetSessionTicketExtension or SetPSKExtension.
  86. // BuildHandshakeState is automatically called before uTLS performs handshake.
  87. func (uconn *UConn) BuildHandshakeStateWithoutSession() error {
  88. return uconn.buildHandshakeState(false)
  89. }
  90. func (uconn *UConn) buildHandshakeState(loadSession bool) error {
  91. if uconn.ClientHelloID == HelloGolang {
  92. if uconn.clientHelloBuildStatus == BuildByGoTLS {
  93. return nil
  94. }
  95. uAssert(uconn.clientHelloBuildStatus == NotBuilt, "BuildHandshakeState failed: invalid call, client hello has already been built by utls")
  96. // use default Golang ClientHello.
  97. hello, keySharePrivate, ech, err := uconn.makeClientHello()
  98. if err != nil {
  99. return err
  100. }
  101. uconn.HandshakeState.Hello = hello.getPublicPtr()
  102. uconn.HandshakeState.State13.KeyShareKeys = keySharePrivate.toPublic()
  103. uconn.HandshakeState.C = uconn.Conn
  104. uconn.echContext = ech
  105. uconn.clientHelloBuildStatus = BuildByGoTLS
  106. } else {
  107. uAssert(uconn.clientHelloBuildStatus == BuildByUtls || uconn.clientHelloBuildStatus == NotBuilt, "BuildHandshakeState failed: invalid call, client hello has already been built by go-tls")
  108. if uconn.clientHelloBuildStatus == NotBuilt {
  109. err := uconn.applyPresetByID(uconn.ClientHelloID)
  110. if err != nil {
  111. return err
  112. }
  113. if uconn.omitSNIExtension {
  114. uconn.removeSNIExtension()
  115. }
  116. }
  117. err := uconn.ApplyConfig()
  118. if err != nil {
  119. return err
  120. }
  121. if loadSession {
  122. err = uconn.uLoadSession()
  123. if err != nil {
  124. return err
  125. }
  126. }
  127. err = uconn.MarshalClientHello()
  128. if err != nil {
  129. return err
  130. }
  131. if loadSession {
  132. uconn.uApplyPatch()
  133. uconn.sessionController.finalCheck()
  134. uconn.clientHelloBuildStatus = BuildByUtls
  135. }
  136. }
  137. return nil
  138. }
  139. func (uconn *UConn) uLoadSession() error {
  140. if cfg := uconn.config; cfg.SessionTicketsDisabled || cfg.ClientSessionCache == nil {
  141. return nil
  142. }
  143. switch uconn.sessionController.shouldLoadSession() {
  144. case shouldReturn:
  145. case shouldSetTicket:
  146. uconn.sessionController.setSessionTicketToUConn()
  147. case shouldSetPsk:
  148. uconn.sessionController.setPskToUConn()
  149. case shouldLoad:
  150. hello := uconn.HandshakeState.Hello.getPrivatePtr()
  151. uconn.sessionController.utlsAboutToLoadSession()
  152. session, earlySecret, binderKey, err := uconn.loadSession(hello)
  153. if session == nil || err != nil {
  154. return err
  155. }
  156. // [Psiphon] TODO: session should be validated before being used.
  157. if session.version == VersionTLS12 {
  158. // [Psiphon] SECTION BEGIN
  159. // Should not attempt to resume a session ticket if the sessionTicketExt is nil.
  160. // In upstream uTLS code, this check is skipped in initSessionTicketExt if
  161. // skipResumptionOnNilExtension is true.
  162. if uconn.sessionController.sessionTicketExt == nil {
  163. return nil
  164. }
  165. if mutualCipherSuite(uconn.HandshakeState.Hello.CipherSuites, session.cipherSuite) == nil {
  166. // The TLS 1.2 cipher suite must match the resumed session.
  167. return nil
  168. }
  169. // [Psiphon] SECTION END
  170. // We use the session ticket extension for tls 1.2 session resumption
  171. uconn.sessionController.initSessionTicketExt(session, hello.sessionTicket)
  172. uconn.sessionController.setSessionTicketToUConn()
  173. } else {
  174. uconn.sessionController.initPskExt(session, earlySecret, binderKey, hello.pskIdentities)
  175. }
  176. }
  177. return nil
  178. }
  179. func (uconn *UConn) uApplyPatch() {
  180. helloLen := len(uconn.HandshakeState.Hello.Original)
  181. if uconn.sessionController.shouldUpdateBinders() {
  182. uconn.sessionController.updateBinders()
  183. uconn.sessionController.setPskToUConn()
  184. }
  185. uAssert(helloLen == len(uconn.HandshakeState.Hello.Original), "tls: uApplyPatch Failed: the patch should never change the length of the marshaled clientHello")
  186. }
  187. func (uconn *UConn) DidTls12Resume() bool {
  188. return uconn.didResume
  189. }
  190. // SetSessionState sets the session ticket, which may be preshared or fake.
  191. // If session is nil, the body of session ticket extension will be unset,
  192. // but the extension itself still MAY be present for mimicking purposes.
  193. // Session tickets to be reused - use same cache on following connections.
  194. //
  195. // Deprecated: This method is deprecated in favor of SetSessionTicketExtension,
  196. // as it only handles session override of TLS 1.2
  197. func (uconn *UConn) SetSessionState(cs *ClientSessionState) error {
  198. sessionTicketExt := &SessionTicketExtension{Initialized: true}
  199. if cs != nil {
  200. sessionTicketExt.Session = cs.session
  201. }
  202. return uconn.SetSessionTicketExtension(sessionTicketExt)
  203. }
  204. // SetSessionTicket sets the session ticket extension.
  205. // If extension is nil, this will be a no-op.
  206. func (uconn *UConn) SetSessionTicketExtension(sessionTicketExt ISessionTicketExtension) error {
  207. if uconn.config.SessionTicketsDisabled || uconn.config.ClientSessionCache == nil {
  208. return fmt.Errorf("tls: SetSessionTicketExtension failed: session is disabled")
  209. }
  210. if sessionTicketExt == nil {
  211. return nil
  212. }
  213. return uconn.sessionController.overrideSessionTicketExt(sessionTicketExt)
  214. }
  215. // SetPskExtension sets the psk extension for tls 1.3 resumption. This is a no-op if the psk is nil.
  216. func (uconn *UConn) SetPskExtension(pskExt PreSharedKeyExtension) error {
  217. if uconn.config.SessionTicketsDisabled || uconn.config.ClientSessionCache == nil {
  218. return fmt.Errorf("tls: SetPskExtension failed: session is disabled")
  219. }
  220. if pskExt == nil {
  221. return nil
  222. }
  223. uconn.HandshakeState.Hello.TicketSupported = true
  224. return uconn.sessionController.overridePskExt(pskExt)
  225. }
  226. // If you want session tickets to be reused - use same cache on following connections
  227. func (uconn *UConn) SetSessionCache(cache ClientSessionCache) {
  228. uconn.config.ClientSessionCache = cache
  229. uconn.HandshakeState.Hello.TicketSupported = true
  230. }
  231. // SetClientRandom sets client random explicitly.
  232. // BuildHandshakeFirst() must be called before SetClientRandom.
  233. // r must to be 32 bytes long.
  234. func (uconn *UConn) SetClientRandom(r []byte) error {
  235. if len(r) != 32 {
  236. return errors.New("Incorrect client random length! Expected: 32, got: " + strconv.Itoa(len(r)))
  237. } else {
  238. uconn.HandshakeState.Hello.Random = make([]byte, 32)
  239. copy(uconn.HandshakeState.Hello.Random, r)
  240. return nil
  241. }
  242. }
  243. func (uconn *UConn) SetSNI(sni string) {
  244. hname := hostnameInSNI(sni)
  245. uconn.config.ServerName = hname
  246. for _, ext := range uconn.Extensions {
  247. sniExt, ok := ext.(*SNIExtension)
  248. if ok {
  249. sniExt.ServerName = hname
  250. }
  251. }
  252. }
  253. // RemoveSNIExtension removes SNI from the list of extensions sent in ClientHello
  254. // It returns an error when used with HelloGolang ClientHelloID
  255. func (uconn *UConn) RemoveSNIExtension() error {
  256. if uconn.ClientHelloID == HelloGolang {
  257. return fmt.Errorf("cannot call RemoveSNIExtension on a UConn with a HelloGolang ClientHelloID")
  258. }
  259. uconn.omitSNIExtension = true
  260. return nil
  261. }
  262. func (uconn *UConn) removeSNIExtension() {
  263. filteredExts := make([]TLSExtension, 0, len(uconn.Extensions))
  264. for _, e := range uconn.Extensions {
  265. if _, ok := e.(*SNIExtension); !ok {
  266. filteredExts = append(filteredExts, e)
  267. }
  268. }
  269. uconn.Extensions = filteredExts
  270. }
  271. // Handshake runs the client handshake using given clientHandshakeState
  272. // Requires hs.hello, and, optionally, hs.session to be set.
  273. func (c *UConn) Handshake() error {
  274. return c.HandshakeContext(context.Background())
  275. }
  276. // HandshakeContext runs the client or server handshake
  277. // protocol if it has not yet been run.
  278. //
  279. // The provided Context must be non-nil. If the context is canceled before
  280. // the handshake is complete, the handshake is interrupted and an error is returned.
  281. // Once the handshake has completed, cancellation of the context will not affect the
  282. // connection.
  283. func (c *UConn) HandshakeContext(ctx context.Context) error {
  284. // Delegate to unexported method for named return
  285. // without confusing documented signature.
  286. return c.handshakeContext(ctx)
  287. }
  288. func (c *UConn) handshakeContext(ctx context.Context) (ret error) {
  289. // Fast sync/atomic-based exit if there is no handshake in flight and the
  290. // last one succeeded without an error. Avoids the expensive context setup
  291. // and mutex for most Read and Write calls.
  292. if c.isHandshakeComplete.Load() {
  293. return nil
  294. }
  295. handshakeCtx, cancel := context.WithCancel(ctx)
  296. // Note: defer this before starting the "interrupter" goroutine
  297. // so that we can tell the difference between the input being canceled and
  298. // this cancellation. In the former case, we need to close the connection.
  299. defer cancel()
  300. // Start the "interrupter" goroutine, if this context might be canceled.
  301. // (The background context cannot).
  302. //
  303. // The interrupter goroutine waits for the input context to be done and
  304. // closes the connection if this happens before the function returns.
  305. if c.quic != nil {
  306. c.quic.cancelc = handshakeCtx.Done()
  307. c.quic.cancel = cancel
  308. } else if ctx.Done() != nil {
  309. done := make(chan struct{})
  310. interruptRes := make(chan error, 1)
  311. defer func() {
  312. close(done)
  313. if ctxErr := <-interruptRes; ctxErr != nil {
  314. // Return context error to user.
  315. ret = ctxErr
  316. }
  317. }()
  318. go func() {
  319. select {
  320. case <-handshakeCtx.Done():
  321. // Close the connection, discarding the error
  322. _ = c.conn.Close()
  323. interruptRes <- handshakeCtx.Err()
  324. case <-done:
  325. interruptRes <- nil
  326. }
  327. }()
  328. }
  329. c.handshakeMutex.Lock()
  330. defer c.handshakeMutex.Unlock()
  331. if err := c.handshakeErr; err != nil {
  332. return err
  333. }
  334. if c.isHandshakeComplete.Load() {
  335. return nil
  336. }
  337. c.in.Lock()
  338. defer c.in.Unlock()
  339. // [uTLS section begins]
  340. if c.isClient {
  341. err := c.BuildHandshakeState()
  342. if err != nil {
  343. return err
  344. }
  345. }
  346. // [uTLS section ends]
  347. c.handshakeErr = c.handshakeFn(handshakeCtx)
  348. if c.handshakeErr == nil {
  349. c.handshakes++
  350. } else {
  351. // If an error occurred during the hadshake try to flush the
  352. // alert that might be left in the buffer.
  353. c.flush()
  354. }
  355. if c.handshakeErr == nil && !c.isHandshakeComplete.Load() {
  356. c.handshakeErr = errors.New("tls: internal error: handshake should have had a result")
  357. }
  358. if c.handshakeErr != nil && c.isHandshakeComplete.Load() {
  359. panic("tls: internal error: handshake returned an error but is marked successful")
  360. }
  361. if c.quic != nil {
  362. if c.handshakeErr == nil {
  363. c.quicHandshakeComplete()
  364. // Provide the 1-RTT read secret now that the handshake is complete.
  365. // The QUIC layer MUST NOT decrypt 1-RTT packets prior to completing
  366. // the handshake (RFC 9001, Section 5.7).
  367. c.quicSetReadSecret(QUICEncryptionLevelApplication, c.cipherSuite, c.in.trafficSecret)
  368. } else {
  369. var a alert
  370. c.out.Lock()
  371. if !errors.As(c.out.err, &a) {
  372. a = alertInternalError
  373. }
  374. c.out.Unlock()
  375. // Return an error which wraps both the handshake error and
  376. // any alert error we may have sent, or alertInternalError
  377. // if we didn't send an alert.
  378. // Truncate the text of the alert to 0 characters.
  379. c.handshakeErr = fmt.Errorf("%w%.0w", c.handshakeErr, AlertError(a))
  380. }
  381. close(c.quic.blockedc)
  382. close(c.quic.signalc)
  383. }
  384. return c.handshakeErr
  385. }
  386. // Copy-pasted from tls.Conn in its entirety. But c.Handshake() is now utls' one, not tls.
  387. // Write writes data to the connection.
  388. func (c *UConn) Write(b []byte) (int, error) {
  389. // interlock with Close below
  390. for {
  391. x := c.activeCall.Load()
  392. if x&1 != 0 {
  393. return 0, net.ErrClosed
  394. }
  395. if c.activeCall.CompareAndSwap(x, x+2) {
  396. defer c.activeCall.Add(-2)
  397. break
  398. }
  399. }
  400. if err := c.Handshake(); err != nil {
  401. return 0, err
  402. }
  403. c.out.Lock()
  404. defer c.out.Unlock()
  405. if err := c.out.err; err != nil {
  406. return 0, err
  407. }
  408. if !c.isHandshakeComplete.Load() {
  409. return 0, alertInternalError
  410. }
  411. if c.closeNotifySent {
  412. return 0, errShutdown
  413. }
  414. // SSL 3.0 and TLS 1.0 are susceptible to a chosen-plaintext
  415. // attack when using block mode ciphers due to predictable IVs.
  416. // This can be prevented by splitting each Application Data
  417. // record into two records, effectively randomizing the IV.
  418. //
  419. // https://www.openssl.org/~bodo/tls-cbc.txt
  420. // https://bugzilla.mozilla.org/show_bug.cgi?id=665814
  421. // https://www.imperialviolet.org/2012/01/15/beastfollowup.html
  422. var m int
  423. if len(b) > 1 && c.vers <= VersionTLS10 {
  424. if _, ok := c.out.cipher.(cipher.BlockMode); ok {
  425. n, err := c.writeRecordLocked(recordTypeApplicationData, b[:1])
  426. if err != nil {
  427. return n, c.out.setErrorLocked(err)
  428. }
  429. m, b = 1, b[1:]
  430. }
  431. }
  432. n, err := c.writeRecordLocked(recordTypeApplicationData, b)
  433. return n + m, c.out.setErrorLocked(err)
  434. }
  435. // clientHandshakeWithOneState checks that exactly one expected state is set (1.2 or 1.3)
  436. // and performs client TLS handshake with that state
  437. func (c *UConn) clientHandshake(ctx context.Context) (err error) {
  438. // [uTLS section begins]
  439. hello := c.HandshakeState.Hello.getPrivatePtr()
  440. ech := c.echContext
  441. defer func() { c.HandshakeState.Hello = hello.getPublicPtr() }()
  442. sessionIsLocked := c.utls.sessionController.isSessionLocked()
  443. // after this point exactly 1 out of 2 HandshakeState pointers is non-nil,
  444. // useTLS13 variable tells which pointer
  445. // [uTLS section ends]
  446. if c.config == nil {
  447. c.config = defaultConfig()
  448. }
  449. // This may be a renegotiation handshake, in which case some fields
  450. // need to be reset.
  451. c.didResume = false
  452. // [uTLS section begins]
  453. // don't make new ClientHello, use hs.hello
  454. // preserve the checks from beginning and end of makeClientHello()
  455. if len(c.config.ServerName) == 0 && !c.config.InsecureSkipVerify && len(c.config.InsecureServerNameToVerify) == 0 {
  456. return errors.New("tls: at least one of ServerName, InsecureSkipVerify or InsecureServerNameToVerify must be specified in the tls.Config")
  457. }
  458. nextProtosLength := 0
  459. for _, proto := range c.config.NextProtos {
  460. if l := len(proto); l == 0 || l > 255 {
  461. return errors.New("tls: invalid NextProtos value")
  462. } else {
  463. nextProtosLength += 1 + l
  464. }
  465. }
  466. if nextProtosLength > 0xffff {
  467. return errors.New("tls: NextProtos values too large")
  468. }
  469. if c.handshakes > 0 {
  470. hello.secureRenegotiation = c.clientFinished[:]
  471. }
  472. var (
  473. session *SessionState
  474. earlySecret []byte
  475. binderKey []byte
  476. )
  477. if !sessionIsLocked {
  478. // [uTLS section ends]
  479. session, earlySecret, binderKey, err = c.loadSession(hello)
  480. // [uTLS section start]
  481. } else {
  482. session = c.HandshakeState.Session
  483. earlySecret = c.HandshakeState.State13.EarlySecret
  484. binderKey = c.HandshakeState.State13.BinderKey
  485. }
  486. // [uTLS section ends]
  487. if err != nil {
  488. return err
  489. }
  490. if session != nil {
  491. defer func() {
  492. // If we got a handshake failure when resuming a session, throw away
  493. // the session ticket. See RFC 5077, Section 3.2.
  494. //
  495. // RFC 8446 makes no mention of dropping tickets on failure, but it
  496. // does require servers to abort on invalid binders, so we need to
  497. // delete tickets to recover from a corrupted PSK.
  498. if err != nil {
  499. if cacheKey := c.clientSessionCacheKey(); cacheKey != "" {
  500. c.config.ClientSessionCache.Put(cacheKey, nil)
  501. }
  502. }
  503. }()
  504. }
  505. if ech != nil {
  506. // Split hello into inner and outer
  507. ech.innerHello = hello.clone()
  508. // Overwrite the server name in the outer hello with the public facing
  509. // name.
  510. hello.serverName = string(ech.config.PublicName)
  511. // Generate a new random for the outer hello.
  512. hello.random = make([]byte, 32)
  513. _, err = io.ReadFull(c.config.rand(), hello.random)
  514. if err != nil {
  515. return errors.New("tls: short read from Rand: " + err.Error())
  516. }
  517. // NOTE: we don't do PSK GREASE, in line with boringssl, it's meant to
  518. // work around _possibly_ broken middleboxes, but there is little-to-no
  519. // evidence that this is actually a problem.
  520. if err := computeAndUpdateOuterECHExtension(hello, ech.innerHello, ech, true); err != nil {
  521. return err
  522. }
  523. }
  524. c.serverName = hello.serverName
  525. if _, err := c.writeHandshakeRecord(hello, nil); err != nil {
  526. return err
  527. }
  528. // [Psiphon]
  529. // Client sent a session ticket or PSK.
  530. if session != nil {
  531. c.clientSentTicket = true
  532. }
  533. if hello.earlyData {
  534. suite := cipherSuiteTLS13ByID(session.cipherSuite)
  535. transcript := suite.hash.New()
  536. if err := transcriptMsg(hello, transcript); err != nil {
  537. return err
  538. }
  539. earlyTrafficSecret := suite.deriveSecret(earlySecret, clientEarlyTrafficLabel, transcript)
  540. c.quicSetWriteSecret(QUICEncryptionLevelEarly, suite.id, earlyTrafficSecret)
  541. }
  542. msg, err := c.readHandshake(nil)
  543. if err != nil {
  544. return err
  545. }
  546. serverHello, ok := msg.(*serverHelloMsg)
  547. if !ok {
  548. c.sendAlert(alertUnexpectedMessage)
  549. return unexpectedMessageError(serverHello, msg)
  550. }
  551. if err := c.pickTLSVersion(serverHello); err != nil {
  552. return err
  553. }
  554. // If we are negotiating a protocol version that's lower than what we
  555. // support, check for the server downgrade canaries.
  556. // See RFC 8446, Section 4.1.3.
  557. maxVers := c.config.maxSupportedVersion(roleClient)
  558. tls12Downgrade := string(serverHello.random[24:]) == downgradeCanaryTLS12
  559. tls11Downgrade := string(serverHello.random[24:]) == downgradeCanaryTLS11
  560. if maxVers == VersionTLS13 && c.vers <= VersionTLS12 && (tls12Downgrade || tls11Downgrade) ||
  561. maxVers == VersionTLS12 && c.vers <= VersionTLS11 && tls11Downgrade {
  562. c.sendAlert(alertIllegalParameter)
  563. return errors.New("tls: downgrade attempt detected, possibly due to a MitM attack or a broken middlebox")
  564. }
  565. // uTLS: do not create new handshakeState, use existing one
  566. if c.vers == VersionTLS13 {
  567. hs13 := c.HandshakeState.toPrivate13()
  568. hs13.serverHello = serverHello
  569. hs13.hello = hello
  570. hs13.echContext = ech
  571. if !sessionIsLocked {
  572. hs13.earlySecret = earlySecret
  573. hs13.binderKey = binderKey
  574. hs13.session = session
  575. }
  576. hs13.ctx = ctx
  577. // In TLS 1.3, session tickets are delivered after the handshake.
  578. err = hs13.handshake()
  579. if handshakeState := hs13.toPublic13(); handshakeState != nil {
  580. c.HandshakeState = *handshakeState
  581. }
  582. return err
  583. }
  584. hs12 := c.HandshakeState.toPrivate12()
  585. hs12.serverHello = serverHello
  586. hs12.hello = hello
  587. hs12.ctx = ctx
  588. hs12.session = session
  589. err = hs12.handshake()
  590. if handshakeState := hs12.toPublic12(); handshakeState != nil {
  591. c.HandshakeState = *handshakeState
  592. }
  593. if err != nil {
  594. return err
  595. }
  596. return nil
  597. }
  598. func (uconn *UConn) ApplyConfig() error {
  599. for _, ext := range uconn.Extensions {
  600. err := ext.writeToUConn(uconn)
  601. if err != nil {
  602. return err
  603. }
  604. }
  605. return nil
  606. }
  607. func (uconn *UConn) MarshalClientHello() error {
  608. if len(uconn.config.ECHConfigs) > 0 && uconn.ech != nil {
  609. if err := uconn.ech.Configure(uconn.config.ECHConfigs); err != nil {
  610. return err
  611. }
  612. return uconn.ech.MarshalClientHello(uconn)
  613. }
  614. return uconn.MarshalClientHelloNoECH() // if no ECH pointer, just marshal normally
  615. }
  616. // MarshalClientHelloNoECH marshals ClientHello as if there was no
  617. // ECH extension present.
  618. func (uconn *UConn) MarshalClientHelloNoECH() error {
  619. hello := uconn.HandshakeState.Hello
  620. headerLength := 2 + 32 + 1 + len(hello.SessionId) +
  621. 2 + len(hello.CipherSuites)*2 +
  622. 1 + len(hello.CompressionMethods)
  623. extensionsLen := 0
  624. var paddingExt *UtlsPaddingExtension // reference to padding extension, if present
  625. for _, ext := range uconn.Extensions {
  626. if pe, ok := ext.(*UtlsPaddingExtension); !ok {
  627. // If not padding - just add length of extension to total length
  628. extensionsLen += ext.Len()
  629. } else {
  630. // If padding - process it later
  631. if paddingExt == nil {
  632. paddingExt = pe
  633. } else {
  634. return errors.New("multiple padding extensions")
  635. }
  636. }
  637. }
  638. if paddingExt != nil {
  639. // determine padding extension presence and length
  640. paddingExt.Update(headerLength + 4 + extensionsLen + 2)
  641. extensionsLen += paddingExt.Len()
  642. }
  643. helloLen := headerLength
  644. if len(uconn.Extensions) > 0 {
  645. helloLen += 2 + extensionsLen // 2 bytes for extensions' length
  646. }
  647. helloBuffer := bytes.Buffer{}
  648. bufferedWriter := bufio.NewWriterSize(&helloBuffer, helloLen+4) // 1 byte for tls record type, 3 for length
  649. // We use buffered Writer to avoid checking write errors after every Write(): whenever first error happens
  650. // Write() will become noop, and error will be accessible via Flush(), which is called once in the end
  651. binary.Write(bufferedWriter, binary.BigEndian, typeClientHello)
  652. helloLenBytes := []byte{byte(helloLen >> 16), byte(helloLen >> 8), byte(helloLen)} // poor man's uint24
  653. binary.Write(bufferedWriter, binary.BigEndian, helloLenBytes)
  654. binary.Write(bufferedWriter, binary.BigEndian, hello.Vers)
  655. binary.Write(bufferedWriter, binary.BigEndian, hello.Random)
  656. binary.Write(bufferedWriter, binary.BigEndian, uint8(len(hello.SessionId)))
  657. binary.Write(bufferedWriter, binary.BigEndian, hello.SessionId)
  658. binary.Write(bufferedWriter, binary.BigEndian, uint16(len(hello.CipherSuites)<<1))
  659. for _, suite := range hello.CipherSuites {
  660. binary.Write(bufferedWriter, binary.BigEndian, suite)
  661. }
  662. binary.Write(bufferedWriter, binary.BigEndian, uint8(len(hello.CompressionMethods)))
  663. binary.Write(bufferedWriter, binary.BigEndian, hello.CompressionMethods)
  664. if len(uconn.Extensions) > 0 {
  665. binary.Write(bufferedWriter, binary.BigEndian, uint16(extensionsLen))
  666. for _, ext := range uconn.Extensions {
  667. if _, err := bufferedWriter.ReadFrom(ext); err != nil {
  668. return err
  669. }
  670. }
  671. }
  672. err := bufferedWriter.Flush()
  673. if err != nil {
  674. return err
  675. }
  676. if helloBuffer.Len() != 4+helloLen {
  677. return errors.New("utls: unexpected ClientHello length. Expected: " + strconv.Itoa(4+helloLen) +
  678. ". Got: " + strconv.Itoa(helloBuffer.Len()))
  679. }
  680. hello.Original = helloBuffer.Bytes()
  681. return nil
  682. }
  683. // get current state of cipher and encrypt zeros to get keystream
  684. func (uconn *UConn) GetOutKeystream(length int) ([]byte, error) {
  685. zeros := make([]byte, length)
  686. if outCipher, ok := uconn.out.cipher.(cipher.AEAD); ok {
  687. // AEAD.Seal() does not mutate internal state, other ciphers might
  688. return outCipher.Seal(nil, uconn.out.seq[:], zeros, nil), nil
  689. }
  690. return nil, errors.New("could not convert OutCipher to cipher.AEAD")
  691. }
  692. // SetTLSVers sets min and max TLS version in all appropriate places.
  693. // Function will use first non-zero version parsed in following order:
  694. // 1. Provided minTLSVers, maxTLSVers
  695. // 2. specExtensions may have SupportedVersionsExtension
  696. // 3. [default] min = TLS 1.0, max = TLS 1.2
  697. //
  698. // Error is only returned if things are in clearly undesirable state
  699. // to help user fix them.
  700. func (uconn *UConn) SetTLSVers(minTLSVers, maxTLSVers uint16, specExtensions []TLSExtension) error {
  701. if minTLSVers == 0 && maxTLSVers == 0 {
  702. // if version is not set explicitly in the ClientHelloSpec, check the SupportedVersions extension
  703. supportedVersionsExtensionsPresent := 0
  704. for _, e := range specExtensions {
  705. switch ext := e.(type) {
  706. case *SupportedVersionsExtension:
  707. findVersionsInSupportedVersionsExtensions := func(versions []uint16) (uint16, uint16) {
  708. // returns (minVers, maxVers)
  709. minVers := uint16(0)
  710. maxVers := uint16(0)
  711. for _, vers := range versions {
  712. if isGREASEUint16(vers) {
  713. continue
  714. }
  715. if maxVers < vers || maxVers == 0 {
  716. maxVers = vers
  717. }
  718. if minVers > vers || minVers == 0 {
  719. minVers = vers
  720. }
  721. }
  722. return minVers, maxVers
  723. }
  724. supportedVersionsExtensionsPresent += 1
  725. minTLSVers, maxTLSVers = findVersionsInSupportedVersionsExtensions(ext.Versions)
  726. if minTLSVers == 0 && maxTLSVers == 0 {
  727. return fmt.Errorf("SupportedVersions extension has invalid Versions field")
  728. } // else: proceed
  729. }
  730. }
  731. switch supportedVersionsExtensionsPresent {
  732. case 0:
  733. // if mandatory for TLS 1.3 extension is not present, just default to 1.2
  734. minTLSVers = VersionTLS10
  735. maxTLSVers = VersionTLS12
  736. case 1:
  737. default:
  738. return fmt.Errorf("uconn.Extensions contains %v separate SupportedVersions extensions",
  739. supportedVersionsExtensionsPresent)
  740. }
  741. }
  742. if minTLSVers < VersionTLS10 || minTLSVers > VersionTLS13 {
  743. return fmt.Errorf("uTLS does not support 0x%X as min version", minTLSVers)
  744. }
  745. if maxTLSVers < VersionTLS10 || maxTLSVers > VersionTLS13 {
  746. return fmt.Errorf("uTLS does not support 0x%X as max version", maxTLSVers)
  747. }
  748. uconn.HandshakeState.Hello.SupportedVersions = makeSupportedVersions(minTLSVers, maxTLSVers)
  749. uconn.config.MinVersion = minTLSVers
  750. uconn.config.MaxVersion = maxTLSVers
  751. return nil
  752. }
  753. func (uconn *UConn) SetUnderlyingConn(c net.Conn) {
  754. uconn.Conn.conn = c
  755. }
  756. func (uconn *UConn) GetUnderlyingConn() net.Conn {
  757. return uconn.Conn.conn
  758. }
  759. // MakeConnWithCompleteHandshake allows to forge both server and client side TLS connections.
  760. // Major Hack Alert.
  761. func MakeConnWithCompleteHandshake(tcpConn net.Conn, version uint16, cipherSuite uint16, masterSecret []byte, clientRandom []byte, serverRandom []byte, isClient bool) *Conn {
  762. tlsConn := &Conn{conn: tcpConn, config: &Config{}, isClient: isClient}
  763. cs := cipherSuiteByID(cipherSuite)
  764. if cs != nil {
  765. // This is mostly borrowed from establishKeys()
  766. clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV :=
  767. keysFromMasterSecret(version, cs, masterSecret, clientRandom, serverRandom,
  768. cs.macLen, cs.keyLen, cs.ivLen)
  769. var clientCipher, serverCipher interface{}
  770. var clientHash, serverHash hash.Hash
  771. if cs.cipher != nil {
  772. clientCipher = cs.cipher(clientKey, clientIV, true /* for reading */)
  773. clientHash = cs.mac(clientMAC)
  774. serverCipher = cs.cipher(serverKey, serverIV, false /* not for reading */)
  775. serverHash = cs.mac(serverMAC)
  776. } else {
  777. clientCipher = cs.aead(clientKey, clientIV)
  778. serverCipher = cs.aead(serverKey, serverIV)
  779. }
  780. if isClient {
  781. tlsConn.in.prepareCipherSpec(version, serverCipher, serverHash)
  782. tlsConn.out.prepareCipherSpec(version, clientCipher, clientHash)
  783. } else {
  784. tlsConn.in.prepareCipherSpec(version, clientCipher, clientHash)
  785. tlsConn.out.prepareCipherSpec(version, serverCipher, serverHash)
  786. }
  787. // skip the handshake states
  788. tlsConn.isHandshakeComplete.Store(true)
  789. tlsConn.cipherSuite = cipherSuite
  790. tlsConn.haveVers = true
  791. tlsConn.vers = version
  792. // Update to the new cipher specs
  793. // and consume the finished messages
  794. tlsConn.in.changeCipherSpec()
  795. tlsConn.out.changeCipherSpec()
  796. tlsConn.in.incSeq()
  797. tlsConn.out.incSeq()
  798. return tlsConn
  799. } else {
  800. // TODO: Support TLS 1.3 Cipher Suites
  801. return nil
  802. }
  803. }
  804. func makeSupportedVersions(minVers, maxVers uint16) []uint16 {
  805. a := make([]uint16, maxVers-minVers+1)
  806. for i := range a {
  807. a[i] = maxVers - uint16(i)
  808. }
  809. return a
  810. }
  811. // Extending (*Conn).readHandshake() to support more customized handshake messages.
  812. func (c *Conn) utlsHandshakeMessageType(msgType byte) (handshakeMessage, error) {
  813. switch msgType {
  814. case utlsTypeCompressedCertificate:
  815. return new(utlsCompressedCertificateMsg), nil
  816. case utlsTypeEncryptedExtensions:
  817. if c.isClient {
  818. return new(encryptedExtensionsMsg), nil
  819. } else {
  820. return new(utlsClientEncryptedExtensionsMsg), nil
  821. }
  822. default:
  823. return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  824. }
  825. }
  826. // Extending (*Conn).connectionStateLocked()
  827. func (c *Conn) utlsConnectionStateLocked(state *ConnectionState) {
  828. state.PeerApplicationSettings = c.utls.peerApplicationSettings
  829. state.ECHRetryConfigs = c.utls.echRetryConfigs
  830. }
  831. type utlsConnExtraFields struct {
  832. // Application Settings (ALPS)
  833. hasApplicationSettings bool
  834. peerApplicationSettings []byte
  835. localApplicationSettings []byte
  836. // Encrypted Client Hello (ECH)
  837. echRetryConfigs []ECHConfig
  838. sessionController *sessionController
  839. }
  840. // Read reads data from the connection.
  841. //
  842. // As Read calls [Conn.Handshake], in order to prevent indefinite blocking a deadline
  843. // must be set for both Read and [Conn.Write] before Read is called when the handshake
  844. // has not yet completed. See [Conn.SetDeadline], [Conn.SetReadDeadline], and
  845. // [Conn.SetWriteDeadline].
  846. func (c *UConn) Read(b []byte) (int, error) {
  847. if err := c.Handshake(); err != nil {
  848. return 0, err
  849. }
  850. if len(b) == 0 {
  851. // Put this after Handshake, in case people were calling
  852. // Read(nil) for the side effect of the Handshake.
  853. return 0, nil
  854. }
  855. c.in.Lock()
  856. defer c.in.Unlock()
  857. for c.input.Len() == 0 {
  858. if err := c.readRecord(); err != nil {
  859. return 0, err
  860. }
  861. for c.hand.Len() > 0 {
  862. if err := c.handlePostHandshakeMessage(); err != nil {
  863. return 0, err
  864. }
  865. }
  866. }
  867. n, _ := c.input.Read(b)
  868. // If a close-notify alert is waiting, read it so that we can return (n,
  869. // EOF) instead of (n, nil), to signal to the HTTP response reading
  870. // goroutine that the connection is now closed. This eliminates a race
  871. // where the HTTP response reading goroutine would otherwise not observe
  872. // the EOF until its next read, by which time a client goroutine might
  873. // have already tried to reuse the HTTP connection for a new request.
  874. // See https://golang.org/cl/76400046 and https://golang.org/issue/3514
  875. if n != 0 && c.input.Len() == 0 && c.rawInput.Len() > 0 &&
  876. recordType(c.rawInput.Bytes()[0]) == recordTypeAlert {
  877. if err := c.readRecord(); err != nil {
  878. return n, err // will be io.EOF on closeNotify
  879. }
  880. }
  881. return n, nil
  882. }
  883. // handleRenegotiation processes a HelloRequest handshake message.
  884. func (c *UConn) handleRenegotiation() error {
  885. if c.vers == VersionTLS13 {
  886. return errors.New("tls: internal error: unexpected renegotiation")
  887. }
  888. msg, err := c.readHandshake(nil)
  889. if err != nil {
  890. return err
  891. }
  892. helloReq, ok := msg.(*helloRequestMsg)
  893. if !ok {
  894. c.sendAlert(alertUnexpectedMessage)
  895. return unexpectedMessageError(helloReq, msg)
  896. }
  897. if !c.isClient {
  898. return c.sendAlert(alertNoRenegotiation)
  899. }
  900. switch c.config.Renegotiation {
  901. case RenegotiateNever:
  902. return c.sendAlert(alertNoRenegotiation)
  903. case RenegotiateOnceAsClient:
  904. if c.handshakes > 1 {
  905. return c.sendAlert(alertNoRenegotiation)
  906. }
  907. case RenegotiateFreelyAsClient:
  908. // Ok.
  909. default:
  910. c.sendAlert(alertInternalError)
  911. return errors.New("tls: unknown Renegotiation value")
  912. }
  913. c.handshakeMutex.Lock()
  914. defer c.handshakeMutex.Unlock()
  915. c.isHandshakeComplete.Store(false)
  916. // [uTLS section begins]
  917. if err = c.BuildHandshakeState(); err != nil {
  918. return err
  919. }
  920. // [uTLS section ends]
  921. if c.handshakeErr = c.clientHandshake(context.Background()); c.handshakeErr == nil {
  922. c.handshakes++
  923. }
  924. return c.handshakeErr
  925. }
  926. // handlePostHandshakeMessage processes a handshake message arrived after the
  927. // handshake is complete. Up to TLS 1.2, it indicates the start of a renegotiation.
  928. func (c *UConn) handlePostHandshakeMessage() error {
  929. if c.vers != VersionTLS13 {
  930. return c.handleRenegotiation()
  931. }
  932. msg, err := c.readHandshake(nil)
  933. if err != nil {
  934. return err
  935. }
  936. c.retryCount++
  937. if c.retryCount > maxUselessRecords {
  938. c.sendAlert(alertUnexpectedMessage)
  939. return c.in.setErrorLocked(errors.New("tls: too many non-advancing records"))
  940. }
  941. switch msg := msg.(type) {
  942. case *newSessionTicketMsgTLS13:
  943. return c.handleNewSessionTicket(msg)
  944. case *keyUpdateMsg:
  945. return c.handleKeyUpdate(msg)
  946. }
  947. // The QUIC layer is supposed to treat an unexpected post-handshake CertificateRequest
  948. // as a QUIC-level PROTOCOL_VIOLATION error (RFC 9001, Section 4.4). Returning an
  949. // unexpected_message alert here doesn't provide it with enough information to distinguish
  950. // this condition from other unexpected messages. This is probably fine.
  951. c.sendAlert(alertUnexpectedMessage)
  952. return fmt.Errorf("tls: received unexpected handshake message of type %T", msg)
  953. }