u_conn.go 32 KB

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