You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto/tls: adjust dynamic record sizes to grow arithmetically The current code, introduced after Go 1.6 to improve latency on low-bandwidth connections, sends 1 kB packets until 1 MB has been sent, and then sends 16 kB packets (the maximum record size). Unfortunately this decreases throughput for 1-16 MB responses by 20% or so. Following discussion on #15713, change cutoff to 128 kB sent and also grow the size allowed for successive packets: 1 kB, 2 kB, 3 kB, ..., 15 kB, 16 kB. This fixes the throughput problems: the overhead is now closer to 2%. I hope this still helps with latency but I don't have a great way to test it. At the least, it's not worse than Go 1.6. Comparing MaxPacket vs DynamicPacket benchmarks: name maxpkt time/op dyn. time/op delta Throughput/1MB-8 5.07ms ± 7% 5.21ms ± 7% +2.73% (p=0.023 n=16+16) Throughput/2MB-8 15.7ms ±201% 8.4ms ± 5% ~ (p=0.604 n=20+16) Throughput/4MB-8 14.3ms ± 1% 14.5ms ± 1% +1.53% (p=0.000 n=16+16) Throughput/8MB-8 26.6ms ± 1% 26.8ms ± 1% +0.47% (p=0.003 n=19+18) Throughput/16MB-8 51.0ms ± 1% 51.3ms ± 1% +0.47% (p=0.000 n=20+20) Throughput/32MB-8 100ms ± 1% 100ms ± 1% +0.24% (p=0.033 n=20+20) Throughput/64MB-8 197ms ± 0% 198ms ± 0% +0.56% (p=0.000 n=18+7) The small MB runs are bimodal in both cases, probably GC pauses. But there's clearly no general slowdown anymore. Fixes #15713. Change-Id: I5fc44680ba71812d24baac142bceee0e23f2e382 Reviewed-on: https://go-review.googlesource.com/23487 Reviewed-by: Ian Lance Taylor <iant@golang.org>
пре 8 година
crypto/tls: adjust dynamic record sizes to grow arithmetically The current code, introduced after Go 1.6 to improve latency on low-bandwidth connections, sends 1 kB packets until 1 MB has been sent, and then sends 16 kB packets (the maximum record size). Unfortunately this decreases throughput for 1-16 MB responses by 20% or so. Following discussion on #15713, change cutoff to 128 kB sent and also grow the size allowed for successive packets: 1 kB, 2 kB, 3 kB, ..., 15 kB, 16 kB. This fixes the throughput problems: the overhead is now closer to 2%. I hope this still helps with latency but I don't have a great way to test it. At the least, it's not worse than Go 1.6. Comparing MaxPacket vs DynamicPacket benchmarks: name maxpkt time/op dyn. time/op delta Throughput/1MB-8 5.07ms ± 7% 5.21ms ± 7% +2.73% (p=0.023 n=16+16) Throughput/2MB-8 15.7ms ±201% 8.4ms ± 5% ~ (p=0.604 n=20+16) Throughput/4MB-8 14.3ms ± 1% 14.5ms ± 1% +1.53% (p=0.000 n=16+16) Throughput/8MB-8 26.6ms ± 1% 26.8ms ± 1% +0.47% (p=0.003 n=19+18) Throughput/16MB-8 51.0ms ± 1% 51.3ms ± 1% +0.47% (p=0.000 n=20+20) Throughput/32MB-8 100ms ± 1% 100ms ± 1% +0.24% (p=0.033 n=20+20) Throughput/64MB-8 197ms ± 0% 198ms ± 0% +0.56% (p=0.000 n=18+7) The small MB runs are bimodal in both cases, probably GC pauses. But there's clearly no general slowdown anymore. Fixes #15713. Change-Id: I5fc44680ba71812d24baac142bceee0e23f2e382 Reviewed-on: https://go-review.googlesource.com/23487 Reviewed-by: Ian Lance Taylor <iant@golang.org>
пре 8 година
crypto/tls: make Conn.Read return (n, io.EOF) when EOF is next in buffer Update #3514 An io.Reader is permitted to return either (n, nil) or (n, io.EOF) on EOF or other error. The tls package previously always returned (n, nil) for a read of size n if n bytes were available, not surfacing errors at the same time. Amazon's HTTPS frontends like to hang up on clients without sending the appropriate HTTP headers. (In their defense, they're allowed to hang up any time, but generally a server hangs up after a bit of inactivity, not immediately.) In any case, the Go HTTP client tries to re-use connections by looking at whether the response headers say to keep the connection open, and because the connection looks okay, under heavy load it's possible we'll reuse it immediately, writing the next request, just as the Transport's always-reading goroutine returns from tls.Conn.Read and sees (0, io.EOF). But because Amazon does send an AlertCloseNotify record before it hangs up on us, and the tls package does its own internal buffering (up to 1024 bytes) of pending data, we have the AlertCloseNotify in an unread buffer when our Conn.Read (to the HTTP Transport code) reads its final bit of data in the HTTP response body. This change makes that final Read return (n, io.EOF) when an AlertCloseNotify record is buffered right after, if we'd otherwise return (n, nil). A dependent change in the HTTP code then notes whether a client connection has seen an io.EOF and uses that as an additional signal to not reuse a HTTPS connection. With both changes, the majority of Amazon request failures go away. Without either one, 10-20 goroutines hitting the S3 API leads to such an error rate that empirically up to 5 retries are needed to complete an API call. LGTM=agl, rsc R=agl, rsc CC=golang-codereviews https://golang.org/cl/76400046
пре 10 година
crypto/tls: adjust dynamic record sizes to grow arithmetically The current code, introduced after Go 1.6 to improve latency on low-bandwidth connections, sends 1 kB packets until 1 MB has been sent, and then sends 16 kB packets (the maximum record size). Unfortunately this decreases throughput for 1-16 MB responses by 20% or so. Following discussion on #15713, change cutoff to 128 kB sent and also grow the size allowed for successive packets: 1 kB, 2 kB, 3 kB, ..., 15 kB, 16 kB. This fixes the throughput problems: the overhead is now closer to 2%. I hope this still helps with latency but I don't have a great way to test it. At the least, it's not worse than Go 1.6. Comparing MaxPacket vs DynamicPacket benchmarks: name maxpkt time/op dyn. time/op delta Throughput/1MB-8 5.07ms ± 7% 5.21ms ± 7% +2.73% (p=0.023 n=16+16) Throughput/2MB-8 15.7ms ±201% 8.4ms ± 5% ~ (p=0.604 n=20+16) Throughput/4MB-8 14.3ms ± 1% 14.5ms ± 1% +1.53% (p=0.000 n=16+16) Throughput/8MB-8 26.6ms ± 1% 26.8ms ± 1% +0.47% (p=0.003 n=19+18) Throughput/16MB-8 51.0ms ± 1% 51.3ms ± 1% +0.47% (p=0.000 n=20+20) Throughput/32MB-8 100ms ± 1% 100ms ± 1% +0.24% (p=0.033 n=20+20) Throughput/64MB-8 197ms ± 0% 198ms ± 0% +0.56% (p=0.000 n=18+7) The small MB runs are bimodal in both cases, probably GC pauses. But there's clearly no general slowdown anymore. Fixes #15713. Change-Id: I5fc44680ba71812d24baac142bceee0e23f2e382 Reviewed-on: https://go-review.googlesource.com/23487 Reviewed-by: Ian Lance Taylor <iant@golang.org>
пре 8 година
crypto/tls: adjust dynamic record sizes to grow arithmetically The current code, introduced after Go 1.6 to improve latency on low-bandwidth connections, sends 1 kB packets until 1 MB has been sent, and then sends 16 kB packets (the maximum record size). Unfortunately this decreases throughput for 1-16 MB responses by 20% or so. Following discussion on #15713, change cutoff to 128 kB sent and also grow the size allowed for successive packets: 1 kB, 2 kB, 3 kB, ..., 15 kB, 16 kB. This fixes the throughput problems: the overhead is now closer to 2%. I hope this still helps with latency but I don't have a great way to test it. At the least, it's not worse than Go 1.6. Comparing MaxPacket vs DynamicPacket benchmarks: name maxpkt time/op dyn. time/op delta Throughput/1MB-8 5.07ms ± 7% 5.21ms ± 7% +2.73% (p=0.023 n=16+16) Throughput/2MB-8 15.7ms ±201% 8.4ms ± 5% ~ (p=0.604 n=20+16) Throughput/4MB-8 14.3ms ± 1% 14.5ms ± 1% +1.53% (p=0.000 n=16+16) Throughput/8MB-8 26.6ms ± 1% 26.8ms ± 1% +0.47% (p=0.003 n=19+18) Throughput/16MB-8 51.0ms ± 1% 51.3ms ± 1% +0.47% (p=0.000 n=20+20) Throughput/32MB-8 100ms ± 1% 100ms ± 1% +0.24% (p=0.033 n=20+20) Throughput/64MB-8 197ms ± 0% 198ms ± 0% +0.56% (p=0.000 n=18+7) The small MB runs are bimodal in both cases, probably GC pauses. But there's clearly no general slowdown anymore. Fixes #15713. Change-Id: I5fc44680ba71812d24baac142bceee0e23f2e382 Reviewed-on: https://go-review.googlesource.com/23487 Reviewed-by: Ian Lance Taylor <iant@golang.org>
пре 8 година
crypto/tls: adjust dynamic record sizes to grow arithmetically The current code, introduced after Go 1.6 to improve latency on low-bandwidth connections, sends 1 kB packets until 1 MB has been sent, and then sends 16 kB packets (the maximum record size). Unfortunately this decreases throughput for 1-16 MB responses by 20% or so. Following discussion on #15713, change cutoff to 128 kB sent and also grow the size allowed for successive packets: 1 kB, 2 kB, 3 kB, ..., 15 kB, 16 kB. This fixes the throughput problems: the overhead is now closer to 2%. I hope this still helps with latency but I don't have a great way to test it. At the least, it's not worse than Go 1.6. Comparing MaxPacket vs DynamicPacket benchmarks: name maxpkt time/op dyn. time/op delta Throughput/1MB-8 5.07ms ± 7% 5.21ms ± 7% +2.73% (p=0.023 n=16+16) Throughput/2MB-8 15.7ms ±201% 8.4ms ± 5% ~ (p=0.604 n=20+16) Throughput/4MB-8 14.3ms ± 1% 14.5ms ± 1% +1.53% (p=0.000 n=16+16) Throughput/8MB-8 26.6ms ± 1% 26.8ms ± 1% +0.47% (p=0.003 n=19+18) Throughput/16MB-8 51.0ms ± 1% 51.3ms ± 1% +0.47% (p=0.000 n=20+20) Throughput/32MB-8 100ms ± 1% 100ms ± 1% +0.24% (p=0.033 n=20+20) Throughput/64MB-8 197ms ± 0% 198ms ± 0% +0.56% (p=0.000 n=18+7) The small MB runs are bimodal in both cases, probably GC pauses. But there's clearly no general slowdown anymore. Fixes #15713. Change-Id: I5fc44680ba71812d24baac142bceee0e23f2e382 Reviewed-on: https://go-review.googlesource.com/23487 Reviewed-by: Ian Lance Taylor <iant@golang.org>
пре 8 година
crypto/tls: make Conn.Read return (n, io.EOF) when EOF is next in buffer Update #3514 An io.Reader is permitted to return either (n, nil) or (n, io.EOF) on EOF or other error. The tls package previously always returned (n, nil) for a read of size n if n bytes were available, not surfacing errors at the same time. Amazon's HTTPS frontends like to hang up on clients without sending the appropriate HTTP headers. (In their defense, they're allowed to hang up any time, but generally a server hangs up after a bit of inactivity, not immediately.) In any case, the Go HTTP client tries to re-use connections by looking at whether the response headers say to keep the connection open, and because the connection looks okay, under heavy load it's possible we'll reuse it immediately, writing the next request, just as the Transport's always-reading goroutine returns from tls.Conn.Read and sees (0, io.EOF). But because Amazon does send an AlertCloseNotify record before it hangs up on us, and the tls package does its own internal buffering (up to 1024 bytes) of pending data, we have the AlertCloseNotify in an unread buffer when our Conn.Read (to the HTTP Transport code) reads its final bit of data in the HTTP response body. This change makes that final Read return (n, io.EOF) when an AlertCloseNotify record is buffered right after, if we'd otherwise return (n, nil). A dependent change in the HTTP code then notes whether a client connection has seen an io.EOF and uses that as an additional signal to not reuse a HTTPS connection. With both changes, the majority of Amazon request failures go away. Without either one, 10-20 goroutines hitting the S3 API leads to such an error rate that empirically up to 5 retries are needed to complete an API call. LGTM=agl, rsc R=agl, rsc CC=golang-codereviews https://golang.org/cl/76400046
пре 10 година
crypto/tls: make Conn.Read return (n, io.EOF) when EOF is next in buffer Update #3514 An io.Reader is permitted to return either (n, nil) or (n, io.EOF) on EOF or other error. The tls package previously always returned (n, nil) for a read of size n if n bytes were available, not surfacing errors at the same time. Amazon's HTTPS frontends like to hang up on clients without sending the appropriate HTTP headers. (In their defense, they're allowed to hang up any time, but generally a server hangs up after a bit of inactivity, not immediately.) In any case, the Go HTTP client tries to re-use connections by looking at whether the response headers say to keep the connection open, and because the connection looks okay, under heavy load it's possible we'll reuse it immediately, writing the next request, just as the Transport's always-reading goroutine returns from tls.Conn.Read and sees (0, io.EOF). But because Amazon does send an AlertCloseNotify record before it hangs up on us, and the tls package does its own internal buffering (up to 1024 bytes) of pending data, we have the AlertCloseNotify in an unread buffer when our Conn.Read (to the HTTP Transport code) reads its final bit of data in the HTTP response body. This change makes that final Read return (n, io.EOF) when an AlertCloseNotify record is buffered right after, if we'd otherwise return (n, nil). A dependent change in the HTTP code then notes whether a client connection has seen an io.EOF and uses that as an additional signal to not reuse a HTTPS connection. With both changes, the majority of Amazon request failures go away. Without either one, 10-20 goroutines hitting the S3 API leads to such an error rate that empirically up to 5 retries are needed to complete an API call. LGTM=agl, rsc R=agl, rsc CC=golang-codereviews https://golang.org/cl/76400046
пре 10 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. // Copyright 2010 The Go Authors. 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. // TLS low level connection and record layer
  5. package tls
  6. import (
  7. "bytes"
  8. "crypto/cipher"
  9. "crypto/subtle"
  10. "crypto/x509"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "net"
  15. "sync"
  16. "sync/atomic"
  17. "time"
  18. )
  19. // A Conn represents a secured connection.
  20. // It implements the net.Conn interface.
  21. type Conn struct {
  22. // constant
  23. conn net.Conn
  24. isClient bool
  25. // constant after handshake; protected by handshakeMutex
  26. handshakeMutex sync.Mutex // handshakeMutex < in.Mutex, out.Mutex, errMutex
  27. // handshakeCond, if not nil, indicates that a goroutine is committed
  28. // to running the handshake for this Conn. Other goroutines that need
  29. // to wait for the handshake can wait on this, under handshakeMutex.
  30. handshakeCond *sync.Cond
  31. handshakeErr error // error resulting from handshake
  32. vers uint16 // TLS version
  33. haveVers bool // version has been negotiated
  34. config *Config // configuration passed to constructor
  35. // handshakeComplete is true if the connection is currently transferring
  36. // application data (i.e. is not currently processing a handshake).
  37. handshakeComplete bool
  38. // handshakes counts the number of handshakes performed on the
  39. // connection so far. If renegotiation is disabled then this is either
  40. // zero or one.
  41. handshakes int
  42. didResume bool // whether this connection was a session resumption
  43. cipherSuite uint16
  44. ocspResponse []byte // stapled OCSP response
  45. scts [][]byte // signed certificate timestamps from server
  46. peerCertificates []*x509.Certificate
  47. // verifiedChains contains the certificate chains that we built, as
  48. // opposed to the ones presented by the server.
  49. verifiedChains [][]*x509.Certificate
  50. // serverName contains the server name indicated by the client, if any.
  51. serverName string
  52. // secureRenegotiation is true if the server echoed the secure
  53. // renegotiation extension. (This is meaningless as a server because
  54. // renegotiation is not supported in that case.)
  55. secureRenegotiation bool
  56. // clientFinishedIsFirst is true if the client sent the first Finished
  57. // message during the most recent handshake. This is recorded because
  58. // the first transmitted Finished message is the tls-unique
  59. // channel-binding value.
  60. clientFinishedIsFirst bool
  61. // closeNotifyErr is any error from sending the alertCloseNotify record.
  62. closeNotifyErr error
  63. // closeNotifySent is true if the Conn attempted to send an
  64. // alertCloseNotify record.
  65. closeNotifySent bool
  66. // clientFinished and serverFinished contain the Finished message sent
  67. // by the client or server in the most recent handshake. This is
  68. // retained to support the renegotiation extension and tls-unique
  69. // channel-binding.
  70. clientFinished [12]byte
  71. serverFinished [12]byte
  72. clientProtocol string
  73. clientProtocolFallback bool
  74. // input/output
  75. in, out halfConn // in.Mutex < out.Mutex
  76. rawInput *block // raw input, right off the wire
  77. input *block // application data waiting to be read
  78. hand bytes.Buffer // handshake data waiting to be read
  79. buffering bool // whether records are buffered in sendBuf
  80. sendBuf []byte // a buffer of records waiting to be sent
  81. // bytesSent counts the bytes of application data sent.
  82. // packetsSent counts packets.
  83. bytesSent int64
  84. packetsSent int64
  85. // activeCall is an atomic int32; the low bit is whether Close has
  86. // been called. the rest of the bits are the number of goroutines
  87. // in Conn.Write.
  88. activeCall int32
  89. tmp [16]byte
  90. }
  91. // Access to net.Conn methods.
  92. // Cannot just embed net.Conn because that would
  93. // export the struct field too.
  94. // LocalAddr returns the local network address.
  95. func (c *Conn) LocalAddr() net.Addr {
  96. return c.conn.LocalAddr()
  97. }
  98. // RemoteAddr returns the remote network address.
  99. func (c *Conn) RemoteAddr() net.Addr {
  100. return c.conn.RemoteAddr()
  101. }
  102. // SetDeadline sets the read and write deadlines associated with the connection.
  103. // A zero value for t means Read and Write will not time out.
  104. // After a Write has timed out, the TLS state is corrupt and all future writes will return the same error.
  105. func (c *Conn) SetDeadline(t time.Time) error {
  106. return c.conn.SetDeadline(t)
  107. }
  108. // SetReadDeadline sets the read deadline on the underlying connection.
  109. // A zero value for t means Read will not time out.
  110. func (c *Conn) SetReadDeadline(t time.Time) error {
  111. return c.conn.SetReadDeadline(t)
  112. }
  113. // SetWriteDeadline sets the write deadline on the underlying connection.
  114. // A zero value for t means Write will not time out.
  115. // After a Write has timed out, the TLS state is corrupt and all future writes will return the same error.
  116. func (c *Conn) SetWriteDeadline(t time.Time) error {
  117. return c.conn.SetWriteDeadline(t)
  118. }
  119. // A halfConn represents one direction of the record layer
  120. // connection, either sending or receiving.
  121. type halfConn struct {
  122. sync.Mutex
  123. err error // first permanent error
  124. version uint16 // protocol version
  125. cipher interface{} // cipher algorithm
  126. mac macFunction
  127. seq [8]byte // 64-bit sequence number
  128. bfree *block // list of free blocks
  129. additionalData [13]byte // to avoid allocs; interface method args escape
  130. nextCipher interface{} // next encryption state
  131. nextMac macFunction // next MAC algorithm
  132. // used to save allocating a new buffer for each MAC.
  133. inDigestBuf, outDigestBuf []byte
  134. }
  135. func (hc *halfConn) setErrorLocked(err error) error {
  136. hc.err = err
  137. return err
  138. }
  139. // prepareCipherSpec sets the encryption and MAC states
  140. // that a subsequent changeCipherSpec will use.
  141. func (hc *halfConn) prepareCipherSpec(version uint16, cipher interface{}, mac macFunction) {
  142. hc.version = version
  143. hc.nextCipher = cipher
  144. hc.nextMac = mac
  145. }
  146. // changeCipherSpec changes the encryption and MAC states
  147. // to the ones previously passed to prepareCipherSpec.
  148. func (hc *halfConn) changeCipherSpec() error {
  149. if hc.nextCipher == nil {
  150. return alertInternalError
  151. }
  152. hc.cipher = hc.nextCipher
  153. hc.mac = hc.nextMac
  154. hc.nextCipher = nil
  155. hc.nextMac = nil
  156. for i := range hc.seq {
  157. hc.seq[i] = 0
  158. }
  159. return nil
  160. }
  161. // incSeq increments the sequence number.
  162. func (hc *halfConn) incSeq() {
  163. for i := 7; i >= 0; i-- {
  164. hc.seq[i]++
  165. if hc.seq[i] != 0 {
  166. return
  167. }
  168. }
  169. // Not allowed to let sequence number wrap.
  170. // Instead, must renegotiate before it does.
  171. // Not likely enough to bother.
  172. panic("TLS: sequence number wraparound")
  173. }
  174. // extractPadding returns, in constant time, the length of the padding to remove
  175. // from the end of payload. It also returns a byte which is equal to 255 if the
  176. // padding was valid and 0 otherwise. See RFC 2246, section 6.2.3.2
  177. func extractPadding(payload []byte) (toRemove int, good byte) {
  178. if len(payload) < 1 {
  179. return 0, 0
  180. }
  181. paddingLen := payload[len(payload)-1]
  182. t := uint(len(payload)-1) - uint(paddingLen)
  183. // if len(payload) >= (paddingLen - 1) then the MSB of t is zero
  184. good = byte(int32(^t) >> 31)
  185. toCheck := 255 // the maximum possible padding length
  186. // The length of the padded data is public, so we can use an if here
  187. if toCheck+1 > len(payload) {
  188. toCheck = len(payload) - 1
  189. }
  190. for i := 0; i < toCheck; i++ {
  191. t := uint(paddingLen) - uint(i)
  192. // if i <= paddingLen then the MSB of t is zero
  193. mask := byte(int32(^t) >> 31)
  194. b := payload[len(payload)-1-i]
  195. good &^= mask&paddingLen ^ mask&b
  196. }
  197. // We AND together the bits of good and replicate the result across
  198. // all the bits.
  199. good &= good << 4
  200. good &= good << 2
  201. good &= good << 1
  202. good = uint8(int8(good) >> 7)
  203. toRemove = int(paddingLen) + 1
  204. return
  205. }
  206. // extractPaddingSSL30 is a replacement for extractPadding in the case that the
  207. // protocol version is SSLv3. In this version, the contents of the padding
  208. // are random and cannot be checked.
  209. func extractPaddingSSL30(payload []byte) (toRemove int, good byte) {
  210. if len(payload) < 1 {
  211. return 0, 0
  212. }
  213. paddingLen := int(payload[len(payload)-1]) + 1
  214. if paddingLen > len(payload) {
  215. return 0, 0
  216. }
  217. return paddingLen, 255
  218. }
  219. func roundUp(a, b int) int {
  220. return a + (b-a%b)%b
  221. }
  222. // cbcMode is an interface for block ciphers using cipher block chaining.
  223. type cbcMode interface {
  224. cipher.BlockMode
  225. SetIV([]byte)
  226. }
  227. // decrypt checks and strips the mac and decrypts the data in b. Returns a
  228. // success boolean, the number of bytes to skip from the start of the record in
  229. // order to get the application payload, and an optional alert value.
  230. func (hc *halfConn) decrypt(b *block) (ok bool, prefixLen int, alertValue alert) {
  231. // pull out payload
  232. payload := b.data[recordHeaderLen:]
  233. macSize := 0
  234. if hc.mac != nil {
  235. macSize = hc.mac.Size()
  236. }
  237. paddingGood := byte(255)
  238. paddingLen := 0
  239. explicitIVLen := 0
  240. // decrypt
  241. if hc.cipher != nil {
  242. switch c := hc.cipher.(type) {
  243. case cipher.Stream:
  244. c.XORKeyStream(payload, payload)
  245. case aead:
  246. explicitIVLen = c.explicitNonceLen()
  247. if len(payload) < explicitIVLen {
  248. return false, 0, alertBadRecordMAC
  249. }
  250. nonce := payload[:explicitIVLen]
  251. payload = payload[explicitIVLen:]
  252. if len(nonce) == 0 {
  253. nonce = hc.seq[:]
  254. }
  255. copy(hc.additionalData[:], hc.seq[:])
  256. copy(hc.additionalData[8:], b.data[:3])
  257. n := len(payload) - c.Overhead()
  258. hc.additionalData[11] = byte(n >> 8)
  259. hc.additionalData[12] = byte(n)
  260. var err error
  261. payload, err = c.Open(payload[:0], nonce, payload, hc.additionalData[:])
  262. if err != nil {
  263. return false, 0, alertBadRecordMAC
  264. }
  265. b.resize(recordHeaderLen + explicitIVLen + len(payload))
  266. case cbcMode:
  267. blockSize := c.BlockSize()
  268. if hc.version >= VersionTLS11 {
  269. explicitIVLen = blockSize
  270. }
  271. if len(payload)%blockSize != 0 || len(payload) < roundUp(explicitIVLen+macSize+1, blockSize) {
  272. return false, 0, alertBadRecordMAC
  273. }
  274. if explicitIVLen > 0 {
  275. c.SetIV(payload[:explicitIVLen])
  276. payload = payload[explicitIVLen:]
  277. }
  278. c.CryptBlocks(payload, payload)
  279. if hc.version == VersionSSL30 {
  280. paddingLen, paddingGood = extractPaddingSSL30(payload)
  281. } else {
  282. paddingLen, paddingGood = extractPadding(payload)
  283. // To protect against CBC padding oracles like Lucky13, the data
  284. // past paddingLen (which is secret) is passed to the MAC
  285. // function as extra data, to be fed into the HMAC after
  286. // computing the digest. This makes the MAC constant time as
  287. // long as the digest computation is constant time and does not
  288. // affect the subsequent write.
  289. }
  290. default:
  291. panic("unknown cipher type")
  292. }
  293. }
  294. // check, strip mac
  295. if hc.mac != nil {
  296. if len(payload) < macSize {
  297. return false, 0, alertBadRecordMAC
  298. }
  299. // strip mac off payload, b.data
  300. n := len(payload) - macSize - paddingLen
  301. n = subtle.ConstantTimeSelect(int(uint32(n)>>31), 0, n) // if n < 0 { n = 0 }
  302. b.data[3] = byte(n >> 8)
  303. b.data[4] = byte(n)
  304. remoteMAC := payload[n : n+macSize]
  305. localMAC := hc.mac.MAC(hc.inDigestBuf, hc.seq[0:], b.data[:recordHeaderLen], payload[:n], payload[n+macSize:])
  306. if subtle.ConstantTimeCompare(localMAC, remoteMAC) != 1 || paddingGood != 255 {
  307. return false, 0, alertBadRecordMAC
  308. }
  309. hc.inDigestBuf = localMAC
  310. b.resize(recordHeaderLen + explicitIVLen + n)
  311. }
  312. hc.incSeq()
  313. return true, recordHeaderLen + explicitIVLen, 0
  314. }
  315. // padToBlockSize calculates the needed padding block, if any, for a payload.
  316. // On exit, prefix aliases payload and extends to the end of the last full
  317. // block of payload. finalBlock is a fresh slice which contains the contents of
  318. // any suffix of payload as well as the needed padding to make finalBlock a
  319. // full block.
  320. func padToBlockSize(payload []byte, blockSize int) (prefix, finalBlock []byte) {
  321. overrun := len(payload) % blockSize
  322. paddingLen := blockSize - overrun
  323. prefix = payload[:len(payload)-overrun]
  324. finalBlock = make([]byte, blockSize)
  325. copy(finalBlock, payload[len(payload)-overrun:])
  326. for i := overrun; i < blockSize; i++ {
  327. finalBlock[i] = byte(paddingLen - 1)
  328. }
  329. return
  330. }
  331. // encrypt encrypts and macs the data in b.
  332. func (hc *halfConn) encrypt(b *block, explicitIVLen int) (bool, alert) {
  333. // mac
  334. if hc.mac != nil {
  335. mac := hc.mac.MAC(hc.outDigestBuf, hc.seq[0:], b.data[:recordHeaderLen], b.data[recordHeaderLen+explicitIVLen:], nil)
  336. n := len(b.data)
  337. b.resize(n + len(mac))
  338. copy(b.data[n:], mac)
  339. hc.outDigestBuf = mac
  340. }
  341. payload := b.data[recordHeaderLen:]
  342. // encrypt
  343. if hc.cipher != nil {
  344. switch c := hc.cipher.(type) {
  345. case cipher.Stream:
  346. c.XORKeyStream(payload, payload)
  347. case aead:
  348. payloadLen := len(b.data) - recordHeaderLen - explicitIVLen
  349. b.resize(len(b.data) + c.Overhead())
  350. nonce := b.data[recordHeaderLen : recordHeaderLen+explicitIVLen]
  351. if len(nonce) == 0 {
  352. nonce = hc.seq[:]
  353. }
  354. payload := b.data[recordHeaderLen+explicitIVLen:]
  355. payload = payload[:payloadLen]
  356. copy(hc.additionalData[:], hc.seq[:])
  357. copy(hc.additionalData[8:], b.data[:3])
  358. hc.additionalData[11] = byte(payloadLen >> 8)
  359. hc.additionalData[12] = byte(payloadLen)
  360. c.Seal(payload[:0], nonce, payload, hc.additionalData[:])
  361. case cbcMode:
  362. blockSize := c.BlockSize()
  363. if explicitIVLen > 0 {
  364. c.SetIV(payload[:explicitIVLen])
  365. payload = payload[explicitIVLen:]
  366. }
  367. prefix, finalBlock := padToBlockSize(payload, blockSize)
  368. b.resize(recordHeaderLen + explicitIVLen + len(prefix) + len(finalBlock))
  369. c.CryptBlocks(b.data[recordHeaderLen+explicitIVLen:], prefix)
  370. c.CryptBlocks(b.data[recordHeaderLen+explicitIVLen+len(prefix):], finalBlock)
  371. default:
  372. panic("unknown cipher type")
  373. }
  374. }
  375. // update length to include MAC and any block padding needed.
  376. n := len(b.data) - recordHeaderLen
  377. b.data[3] = byte(n >> 8)
  378. b.data[4] = byte(n)
  379. hc.incSeq()
  380. return true, 0
  381. }
  382. // A block is a simple data buffer.
  383. type block struct {
  384. data []byte
  385. off int // index for Read
  386. link *block
  387. }
  388. // resize resizes block to be n bytes, growing if necessary.
  389. func (b *block) resize(n int) {
  390. if n > cap(b.data) {
  391. b.reserve(n)
  392. }
  393. b.data = b.data[0:n]
  394. }
  395. // reserve makes sure that block contains a capacity of at least n bytes.
  396. func (b *block) reserve(n int) {
  397. if cap(b.data) >= n {
  398. return
  399. }
  400. m := cap(b.data)
  401. if m == 0 {
  402. m = 1024
  403. }
  404. for m < n {
  405. m *= 2
  406. }
  407. data := make([]byte, len(b.data), m)
  408. copy(data, b.data)
  409. b.data = data
  410. }
  411. // readFromUntil reads from r into b until b contains at least n bytes
  412. // or else returns an error.
  413. func (b *block) readFromUntil(r io.Reader, n int) error {
  414. // quick case
  415. if len(b.data) >= n {
  416. return nil
  417. }
  418. // read until have enough.
  419. b.reserve(n)
  420. for {
  421. m, err := r.Read(b.data[len(b.data):cap(b.data)])
  422. b.data = b.data[0 : len(b.data)+m]
  423. if len(b.data) >= n {
  424. // TODO(bradfitz,agl): slightly suspicious
  425. // that we're throwing away r.Read's err here.
  426. break
  427. }
  428. if err != nil {
  429. return err
  430. }
  431. }
  432. return nil
  433. }
  434. func (b *block) Read(p []byte) (n int, err error) {
  435. n = copy(p, b.data[b.off:])
  436. b.off += n
  437. return
  438. }
  439. // newBlock allocates a new block, from hc's free list if possible.
  440. func (hc *halfConn) newBlock() *block {
  441. b := hc.bfree
  442. if b == nil {
  443. return new(block)
  444. }
  445. hc.bfree = b.link
  446. b.link = nil
  447. b.resize(0)
  448. return b
  449. }
  450. // freeBlock returns a block to hc's free list.
  451. // The protocol is such that each side only has a block or two on
  452. // its free list at a time, so there's no need to worry about
  453. // trimming the list, etc.
  454. func (hc *halfConn) freeBlock(b *block) {
  455. b.link = hc.bfree
  456. hc.bfree = b
  457. }
  458. // splitBlock splits a block after the first n bytes,
  459. // returning a block with those n bytes and a
  460. // block with the remainder. the latter may be nil.
  461. func (hc *halfConn) splitBlock(b *block, n int) (*block, *block) {
  462. if len(b.data) <= n {
  463. return b, nil
  464. }
  465. bb := hc.newBlock()
  466. bb.resize(len(b.data) - n)
  467. copy(bb.data, b.data[n:])
  468. b.data = b.data[0:n]
  469. return b, bb
  470. }
  471. // RecordHeaderError results when a TLS record header is invalid.
  472. type RecordHeaderError struct {
  473. // Msg contains a human readable string that describes the error.
  474. Msg string
  475. // RecordHeader contains the five bytes of TLS record header that
  476. // triggered the error.
  477. RecordHeader [5]byte
  478. }
  479. func (e RecordHeaderError) Error() string { return "tls: " + e.Msg }
  480. func (c *Conn) newRecordHeaderError(msg string) (err RecordHeaderError) {
  481. err.Msg = msg
  482. copy(err.RecordHeader[:], c.rawInput.data)
  483. return err
  484. }
  485. // readRecord reads the next TLS record from the connection
  486. // and updates the record layer state.
  487. // c.in.Mutex <= L; c.input == nil.
  488. func (c *Conn) readRecord(want recordType) error {
  489. // Caller must be in sync with connection:
  490. // handshake data if handshake not yet completed,
  491. // else application data.
  492. switch want {
  493. default:
  494. c.sendAlert(alertInternalError)
  495. return c.in.setErrorLocked(errors.New("tls: unknown record type requested"))
  496. case recordTypeHandshake, recordTypeChangeCipherSpec:
  497. if c.handshakeComplete {
  498. c.sendAlert(alertInternalError)
  499. return c.in.setErrorLocked(errors.New("tls: handshake or ChangeCipherSpec requested while not in handshake"))
  500. }
  501. case recordTypeApplicationData:
  502. if !c.handshakeComplete {
  503. c.sendAlert(alertInternalError)
  504. return c.in.setErrorLocked(errors.New("tls: application data record requested while in handshake"))
  505. }
  506. }
  507. Again:
  508. if c.rawInput == nil {
  509. c.rawInput = c.in.newBlock()
  510. }
  511. b := c.rawInput
  512. // Read header, payload.
  513. if err := b.readFromUntil(c.conn, recordHeaderLen); err != nil {
  514. // RFC suggests that EOF without an alertCloseNotify is
  515. // an error, but popular web sites seem to do this,
  516. // so we can't make it an error.
  517. // if err == io.EOF {
  518. // err = io.ErrUnexpectedEOF
  519. // }
  520. if e, ok := err.(net.Error); !ok || !e.Temporary() {
  521. c.in.setErrorLocked(err)
  522. }
  523. return err
  524. }
  525. typ := recordType(b.data[0])
  526. // No valid TLS record has a type of 0x80, however SSLv2 handshakes
  527. // start with a uint16 length where the MSB is set and the first record
  528. // is always < 256 bytes long. Therefore typ == 0x80 strongly suggests
  529. // an SSLv2 client.
  530. if want == recordTypeHandshake && typ == 0x80 {
  531. c.sendAlert(alertProtocolVersion)
  532. return c.in.setErrorLocked(c.newRecordHeaderError("unsupported SSLv2 handshake received"))
  533. }
  534. vers := uint16(b.data[1])<<8 | uint16(b.data[2])
  535. n := int(b.data[3])<<8 | int(b.data[4])
  536. if c.haveVers && vers != c.vers {
  537. c.sendAlert(alertProtocolVersion)
  538. msg := fmt.Sprintf("received record with version %x when expecting version %x", vers, c.vers)
  539. return c.in.setErrorLocked(c.newRecordHeaderError(msg))
  540. }
  541. if n > maxCiphertext {
  542. c.sendAlert(alertRecordOverflow)
  543. msg := fmt.Sprintf("oversized record received with length %d", n)
  544. return c.in.setErrorLocked(c.newRecordHeaderError(msg))
  545. }
  546. if !c.haveVers {
  547. // First message, be extra suspicious: this might not be a TLS
  548. // client. Bail out before reading a full 'body', if possible.
  549. // The current max version is 3.3 so if the version is >= 16.0,
  550. // it's probably not real.
  551. if (typ != recordTypeAlert && typ != want) || vers >= 0x1000 {
  552. c.sendAlert(alertUnexpectedMessage)
  553. return c.in.setErrorLocked(c.newRecordHeaderError("first record does not look like a TLS handshake"))
  554. }
  555. }
  556. if err := b.readFromUntil(c.conn, recordHeaderLen+n); err != nil {
  557. if err == io.EOF {
  558. err = io.ErrUnexpectedEOF
  559. }
  560. if e, ok := err.(net.Error); !ok || !e.Temporary() {
  561. c.in.setErrorLocked(err)
  562. }
  563. return err
  564. }
  565. // Process message.
  566. b, c.rawInput = c.in.splitBlock(b, recordHeaderLen+n)
  567. ok, off, alertValue := c.in.decrypt(b)
  568. if !ok {
  569. c.in.freeBlock(b)
  570. return c.in.setErrorLocked(c.sendAlert(alertValue))
  571. }
  572. b.off = off
  573. data := b.data[b.off:]
  574. if len(data) > maxPlaintext {
  575. err := c.sendAlert(alertRecordOverflow)
  576. c.in.freeBlock(b)
  577. return c.in.setErrorLocked(err)
  578. }
  579. switch typ {
  580. default:
  581. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  582. case recordTypeAlert:
  583. if len(data) != 2 {
  584. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  585. break
  586. }
  587. if alert(data[1]) == alertCloseNotify {
  588. c.in.setErrorLocked(io.EOF)
  589. break
  590. }
  591. switch data[0] {
  592. case alertLevelWarning:
  593. // drop on the floor
  594. c.in.freeBlock(b)
  595. goto Again
  596. case alertLevelError:
  597. c.in.setErrorLocked(&net.OpError{Op: "remote error", Err: alert(data[1])})
  598. default:
  599. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  600. }
  601. case recordTypeChangeCipherSpec:
  602. if typ != want || len(data) != 1 || data[0] != 1 {
  603. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  604. break
  605. }
  606. err := c.in.changeCipherSpec()
  607. if err != nil {
  608. c.in.setErrorLocked(c.sendAlert(err.(alert)))
  609. }
  610. case recordTypeApplicationData:
  611. if typ != want {
  612. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  613. break
  614. }
  615. c.input = b
  616. b = nil
  617. case recordTypeHandshake:
  618. // TODO(rsc): Should at least pick off connection close.
  619. if typ != want && !(c.isClient && c.config.Renegotiation != RenegotiateNever) {
  620. return c.in.setErrorLocked(c.sendAlert(alertNoRenegotiation))
  621. }
  622. c.hand.Write(data)
  623. }
  624. if b != nil {
  625. c.in.freeBlock(b)
  626. }
  627. return c.in.err
  628. }
  629. // sendAlert sends a TLS alert message.
  630. // c.out.Mutex <= L.
  631. func (c *Conn) sendAlertLocked(err alert) error {
  632. switch err {
  633. case alertNoRenegotiation, alertCloseNotify:
  634. c.tmp[0] = alertLevelWarning
  635. default:
  636. c.tmp[0] = alertLevelError
  637. }
  638. c.tmp[1] = byte(err)
  639. _, writeErr := c.writeRecordLocked(recordTypeAlert, c.tmp[0:2])
  640. if err == alertCloseNotify {
  641. // closeNotify is a special case in that it isn't an error.
  642. return writeErr
  643. }
  644. return c.out.setErrorLocked(&net.OpError{Op: "local error", Err: err})
  645. }
  646. // sendAlert sends a TLS alert message.
  647. // L < c.out.Mutex.
  648. func (c *Conn) sendAlert(err alert) error {
  649. c.out.Lock()
  650. defer c.out.Unlock()
  651. return c.sendAlertLocked(err)
  652. }
  653. const (
  654. // tcpMSSEstimate is a conservative estimate of the TCP maximum segment
  655. // size (MSS). A constant is used, rather than querying the kernel for
  656. // the actual MSS, to avoid complexity. The value here is the IPv6
  657. // minimum MTU (1280 bytes) minus the overhead of an IPv6 header (40
  658. // bytes) and a TCP header with timestamps (32 bytes).
  659. tcpMSSEstimate = 1208
  660. // recordSizeBoostThreshold is the number of bytes of application data
  661. // sent after which the TLS record size will be increased to the
  662. // maximum.
  663. recordSizeBoostThreshold = 128 * 1024
  664. )
  665. // maxPayloadSizeForWrite returns the maximum TLS payload size to use for the
  666. // next application data record. There is the following trade-off:
  667. //
  668. // - For latency-sensitive applications, such as web browsing, each TLS
  669. // record should fit in one TCP segment.
  670. // - For throughput-sensitive applications, such as large file transfers,
  671. // larger TLS records better amortize framing and encryption overheads.
  672. //
  673. // A simple heuristic that works well in practice is to use small records for
  674. // the first 1MB of data, then use larger records for subsequent data, and
  675. // reset back to smaller records after the connection becomes idle. See "High
  676. // Performance Web Networking", Chapter 4, or:
  677. // https://www.igvita.com/2013/10/24/optimizing-tls-record-size-and-buffering-latency/
  678. //
  679. // In the interests of simplicity and determinism, this code does not attempt
  680. // to reset the record size once the connection is idle, however.
  681. //
  682. // c.out.Mutex <= L.
  683. func (c *Conn) maxPayloadSizeForWrite(typ recordType, explicitIVLen int) int {
  684. if c.config.DynamicRecordSizingDisabled || typ != recordTypeApplicationData {
  685. return maxPlaintext
  686. }
  687. if c.bytesSent >= recordSizeBoostThreshold {
  688. return maxPlaintext
  689. }
  690. // Subtract TLS overheads to get the maximum payload size.
  691. macSize := 0
  692. if c.out.mac != nil {
  693. macSize = c.out.mac.Size()
  694. }
  695. payloadBytes := tcpMSSEstimate - recordHeaderLen - explicitIVLen
  696. if c.out.cipher != nil {
  697. switch ciph := c.out.cipher.(type) {
  698. case cipher.Stream:
  699. payloadBytes -= macSize
  700. case cipher.AEAD:
  701. payloadBytes -= ciph.Overhead()
  702. case cbcMode:
  703. blockSize := ciph.BlockSize()
  704. // The payload must fit in a multiple of blockSize, with
  705. // room for at least one padding byte.
  706. payloadBytes = (payloadBytes & ^(blockSize - 1)) - 1
  707. // The MAC is appended before padding so affects the
  708. // payload size directly.
  709. payloadBytes -= macSize
  710. default:
  711. panic("unknown cipher type")
  712. }
  713. }
  714. // Allow packet growth in arithmetic progression up to max.
  715. pkt := c.packetsSent
  716. c.packetsSent++
  717. if pkt > 1000 {
  718. return maxPlaintext // avoid overflow in multiply below
  719. }
  720. n := payloadBytes * int(pkt+1)
  721. if n > maxPlaintext {
  722. n = maxPlaintext
  723. }
  724. return n
  725. }
  726. // c.out.Mutex <= L.
  727. func (c *Conn) write(data []byte) (int, error) {
  728. if c.buffering {
  729. c.sendBuf = append(c.sendBuf, data...)
  730. return len(data), nil
  731. }
  732. n, err := c.conn.Write(data)
  733. c.bytesSent += int64(n)
  734. return n, err
  735. }
  736. func (c *Conn) flush() (int, error) {
  737. if len(c.sendBuf) == 0 {
  738. return 0, nil
  739. }
  740. n, err := c.conn.Write(c.sendBuf)
  741. c.bytesSent += int64(n)
  742. c.sendBuf = nil
  743. c.buffering = false
  744. return n, err
  745. }
  746. // writeRecordLocked writes a TLS record with the given type and payload to the
  747. // connection and updates the record layer state.
  748. // c.out.Mutex <= L.
  749. func (c *Conn) writeRecordLocked(typ recordType, data []byte) (int, error) {
  750. b := c.out.newBlock()
  751. defer c.out.freeBlock(b)
  752. var n int
  753. for len(data) > 0 {
  754. explicitIVLen := 0
  755. explicitIVIsSeq := false
  756. var cbc cbcMode
  757. if c.out.version >= VersionTLS11 {
  758. var ok bool
  759. if cbc, ok = c.out.cipher.(cbcMode); ok {
  760. explicitIVLen = cbc.BlockSize()
  761. }
  762. }
  763. if explicitIVLen == 0 {
  764. if c, ok := c.out.cipher.(aead); ok {
  765. explicitIVLen = c.explicitNonceLen()
  766. // The AES-GCM construction in TLS has an
  767. // explicit nonce so that the nonce can be
  768. // random. However, the nonce is only 8 bytes
  769. // which is too small for a secure, random
  770. // nonce. Therefore we use the sequence number
  771. // as the nonce.
  772. explicitIVIsSeq = explicitIVLen > 0
  773. }
  774. }
  775. m := len(data)
  776. if maxPayload := c.maxPayloadSizeForWrite(typ, explicitIVLen); m > maxPayload {
  777. m = maxPayload
  778. }
  779. b.resize(recordHeaderLen + explicitIVLen + m)
  780. b.data[0] = byte(typ)
  781. vers := c.vers
  782. if vers == 0 {
  783. // Some TLS servers fail if the record version is
  784. // greater than TLS 1.0 for the initial ClientHello.
  785. vers = VersionTLS10
  786. }
  787. b.data[1] = byte(vers >> 8)
  788. b.data[2] = byte(vers)
  789. b.data[3] = byte(m >> 8)
  790. b.data[4] = byte(m)
  791. if explicitIVLen > 0 {
  792. explicitIV := b.data[recordHeaderLen : recordHeaderLen+explicitIVLen]
  793. if explicitIVIsSeq {
  794. copy(explicitIV, c.out.seq[:])
  795. } else {
  796. if _, err := io.ReadFull(c.config.rand(), explicitIV); err != nil {
  797. return n, err
  798. }
  799. }
  800. }
  801. copy(b.data[recordHeaderLen+explicitIVLen:], data)
  802. c.out.encrypt(b, explicitIVLen)
  803. if _, err := c.write(b.data); err != nil {
  804. return n, err
  805. }
  806. n += m
  807. data = data[m:]
  808. }
  809. if typ == recordTypeChangeCipherSpec {
  810. if err := c.out.changeCipherSpec(); err != nil {
  811. return n, c.sendAlertLocked(err.(alert))
  812. }
  813. }
  814. return n, nil
  815. }
  816. // writeRecord writes a TLS record with the given type and payload to the
  817. // connection and updates the record layer state.
  818. // L < c.out.Mutex.
  819. func (c *Conn) writeRecord(typ recordType, data []byte) (int, error) {
  820. c.out.Lock()
  821. defer c.out.Unlock()
  822. return c.writeRecordLocked(typ, data)
  823. }
  824. // readHandshake reads the next handshake message from
  825. // the record layer.
  826. // c.in.Mutex < L; c.out.Mutex < L.
  827. func (c *Conn) readHandshake() (interface{}, error) {
  828. for c.hand.Len() < 4 {
  829. if err := c.in.err; err != nil {
  830. return nil, err
  831. }
  832. if err := c.readRecord(recordTypeHandshake); err != nil {
  833. return nil, err
  834. }
  835. }
  836. data := c.hand.Bytes()
  837. n := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  838. if n > maxHandshake {
  839. c.sendAlertLocked(alertInternalError)
  840. return nil, c.in.setErrorLocked(fmt.Errorf("tls: handshake message of length %d bytes exceeds maximum of %d bytes", n, maxHandshake))
  841. }
  842. for c.hand.Len() < 4+n {
  843. if err := c.in.err; err != nil {
  844. return nil, err
  845. }
  846. if err := c.readRecord(recordTypeHandshake); err != nil {
  847. return nil, err
  848. }
  849. }
  850. data = c.hand.Next(4 + n)
  851. var m handshakeMessage
  852. switch data[0] {
  853. case typeHelloRequest:
  854. m = new(helloRequestMsg)
  855. case typeClientHello:
  856. m = new(clientHelloMsg)
  857. case typeServerHello:
  858. m = new(serverHelloMsg)
  859. case typeNewSessionTicket:
  860. m = new(newSessionTicketMsg)
  861. case typeCertificate:
  862. m = new(certificateMsg)
  863. case typeCertificateRequest:
  864. m = &certificateRequestMsg{
  865. hasSignatureAndHash: c.vers >= VersionTLS12,
  866. }
  867. case typeCertificateStatus:
  868. m = new(certificateStatusMsg)
  869. case typeServerKeyExchange:
  870. m = new(serverKeyExchangeMsg)
  871. case typeServerHelloDone:
  872. m = new(serverHelloDoneMsg)
  873. case typeClientKeyExchange:
  874. m = new(clientKeyExchangeMsg)
  875. case typeCertificateVerify:
  876. m = &certificateVerifyMsg{
  877. hasSignatureAndHash: c.vers >= VersionTLS12,
  878. }
  879. case typeNextProtocol:
  880. m = new(nextProtoMsg)
  881. case typeFinished:
  882. m = new(finishedMsg)
  883. default:
  884. return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  885. }
  886. // The handshake message unmarshalers
  887. // expect to be able to keep references to data,
  888. // so pass in a fresh copy that won't be overwritten.
  889. data = append([]byte(nil), data...)
  890. if !m.unmarshal(data) {
  891. return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  892. }
  893. return m, nil
  894. }
  895. var (
  896. errClosed = errors.New("tls: use of closed connection")
  897. errShutdown = errors.New("tls: protocol is shutdown")
  898. )
  899. // Write writes data to the connection.
  900. func (c *Conn) Write(b []byte) (int, error) {
  901. // interlock with Close below
  902. for {
  903. x := atomic.LoadInt32(&c.activeCall)
  904. if x&1 != 0 {
  905. return 0, errClosed
  906. }
  907. if atomic.CompareAndSwapInt32(&c.activeCall, x, x+2) {
  908. defer atomic.AddInt32(&c.activeCall, -2)
  909. break
  910. }
  911. }
  912. if err := c.Handshake(); err != nil {
  913. return 0, err
  914. }
  915. c.out.Lock()
  916. defer c.out.Unlock()
  917. if err := c.out.err; err != nil {
  918. return 0, err
  919. }
  920. if !c.handshakeComplete {
  921. return 0, alertInternalError
  922. }
  923. if c.closeNotifySent {
  924. return 0, errShutdown
  925. }
  926. // SSL 3.0 and TLS 1.0 are susceptible to a chosen-plaintext
  927. // attack when using block mode ciphers due to predictable IVs.
  928. // This can be prevented by splitting each Application Data
  929. // record into two records, effectively randomizing the IV.
  930. //
  931. // http://www.openssl.org/~bodo/tls-cbc.txt
  932. // https://bugzilla.mozilla.org/show_bug.cgi?id=665814
  933. // http://www.imperialviolet.org/2012/01/15/beastfollowup.html
  934. var m int
  935. if len(b) > 1 && c.vers <= VersionTLS10 {
  936. if _, ok := c.out.cipher.(cipher.BlockMode); ok {
  937. n, err := c.writeRecordLocked(recordTypeApplicationData, b[:1])
  938. if err != nil {
  939. return n, c.out.setErrorLocked(err)
  940. }
  941. m, b = 1, b[1:]
  942. }
  943. }
  944. n, err := c.writeRecordLocked(recordTypeApplicationData, b)
  945. return n + m, c.out.setErrorLocked(err)
  946. }
  947. // handleRenegotiation processes a HelloRequest handshake message.
  948. // c.in.Mutex <= L
  949. func (c *Conn) handleRenegotiation() error {
  950. msg, err := c.readHandshake()
  951. if err != nil {
  952. return err
  953. }
  954. _, ok := msg.(*helloRequestMsg)
  955. if !ok {
  956. c.sendAlert(alertUnexpectedMessage)
  957. return alertUnexpectedMessage
  958. }
  959. if !c.isClient {
  960. return c.sendAlert(alertNoRenegotiation)
  961. }
  962. switch c.config.Renegotiation {
  963. case RenegotiateNever:
  964. return c.sendAlert(alertNoRenegotiation)
  965. case RenegotiateOnceAsClient:
  966. if c.handshakes > 1 {
  967. return c.sendAlert(alertNoRenegotiation)
  968. }
  969. case RenegotiateFreelyAsClient:
  970. // Ok.
  971. default:
  972. c.sendAlert(alertInternalError)
  973. return errors.New("tls: unknown Renegotiation value")
  974. }
  975. c.handshakeMutex.Lock()
  976. defer c.handshakeMutex.Unlock()
  977. c.handshakeComplete = false
  978. if c.handshakeErr = c.clientHandshake(); c.handshakeErr == nil {
  979. c.handshakes++
  980. }
  981. return c.handshakeErr
  982. }
  983. // Read can be made to time out and return a net.Error with Timeout() == true
  984. // after a fixed time limit; see SetDeadline and SetReadDeadline.
  985. func (c *Conn) Read(b []byte) (n int, err error) {
  986. if err = c.Handshake(); err != nil {
  987. return
  988. }
  989. if len(b) == 0 {
  990. // Put this after Handshake, in case people were calling
  991. // Read(nil) for the side effect of the Handshake.
  992. return
  993. }
  994. c.in.Lock()
  995. defer c.in.Unlock()
  996. // Some OpenSSL servers send empty records in order to randomize the
  997. // CBC IV. So this loop ignores a limited number of empty records.
  998. const maxConsecutiveEmptyRecords = 100
  999. for emptyRecordCount := 0; emptyRecordCount <= maxConsecutiveEmptyRecords; emptyRecordCount++ {
  1000. for c.input == nil && c.in.err == nil {
  1001. if err := c.readRecord(recordTypeApplicationData); err != nil {
  1002. // Soft error, like EAGAIN
  1003. return 0, err
  1004. }
  1005. if c.hand.Len() > 0 {
  1006. // We received handshake bytes, indicating the
  1007. // start of a renegotiation.
  1008. if err := c.handleRenegotiation(); err != nil {
  1009. return 0, err
  1010. }
  1011. }
  1012. }
  1013. if err := c.in.err; err != nil {
  1014. return 0, err
  1015. }
  1016. n, err = c.input.Read(b)
  1017. if c.input.off >= len(c.input.data) {
  1018. c.in.freeBlock(c.input)
  1019. c.input = nil
  1020. }
  1021. // If a close-notify alert is waiting, read it so that
  1022. // we can return (n, EOF) instead of (n, nil), to signal
  1023. // to the HTTP response reading goroutine that the
  1024. // connection is now closed. This eliminates a race
  1025. // where the HTTP response reading goroutine would
  1026. // otherwise not observe the EOF until its next read,
  1027. // by which time a client goroutine might have already
  1028. // tried to reuse the HTTP connection for a new
  1029. // request.
  1030. // See https://codereview.appspot.com/76400046
  1031. // and https://golang.org/issue/3514
  1032. if ri := c.rawInput; ri != nil &&
  1033. n != 0 && err == nil &&
  1034. c.input == nil && len(ri.data) > 0 && recordType(ri.data[0]) == recordTypeAlert {
  1035. if recErr := c.readRecord(recordTypeApplicationData); recErr != nil {
  1036. err = recErr // will be io.EOF on closeNotify
  1037. }
  1038. }
  1039. if n != 0 || err != nil {
  1040. return n, err
  1041. }
  1042. }
  1043. return 0, io.ErrNoProgress
  1044. }
  1045. // Close closes the connection.
  1046. func (c *Conn) Close() error {
  1047. // Interlock with Conn.Write above.
  1048. var x int32
  1049. for {
  1050. x = atomic.LoadInt32(&c.activeCall)
  1051. if x&1 != 0 {
  1052. return errClosed
  1053. }
  1054. if atomic.CompareAndSwapInt32(&c.activeCall, x, x|1) {
  1055. break
  1056. }
  1057. }
  1058. if x != 0 {
  1059. // io.Writer and io.Closer should not be used concurrently.
  1060. // If Close is called while a Write is currently in-flight,
  1061. // interpret that as a sign that this Close is really just
  1062. // being used to break the Write and/or clean up resources and
  1063. // avoid sending the alertCloseNotify, which may block
  1064. // waiting on handshakeMutex or the c.out mutex.
  1065. return c.conn.Close()
  1066. }
  1067. var alertErr error
  1068. c.handshakeMutex.Lock()
  1069. defer c.handshakeMutex.Unlock()
  1070. if c.handshakeComplete {
  1071. alertErr = c.closeNotify()
  1072. }
  1073. if err := c.conn.Close(); err != nil {
  1074. return err
  1075. }
  1076. return alertErr
  1077. }
  1078. var errEarlyCloseWrite = errors.New("tls: CloseWrite called before handshake complete")
  1079. // CloseWrite shuts down the writing side of the connection. It should only be
  1080. // called once the handshake has completed and does not call CloseWrite on the
  1081. // underlying connection. Most callers should just use Close.
  1082. func (c *Conn) CloseWrite() error {
  1083. c.handshakeMutex.Lock()
  1084. defer c.handshakeMutex.Unlock()
  1085. if !c.handshakeComplete {
  1086. return errEarlyCloseWrite
  1087. }
  1088. return c.closeNotify()
  1089. }
  1090. func (c *Conn) closeNotify() error {
  1091. c.out.Lock()
  1092. defer c.out.Unlock()
  1093. if !c.closeNotifySent {
  1094. c.closeNotifyErr = c.sendAlertLocked(alertCloseNotify)
  1095. c.closeNotifySent = true
  1096. }
  1097. return c.closeNotifyErr
  1098. }
  1099. // Handshake runs the client or server handshake
  1100. // protocol if it has not yet been run.
  1101. // Most uses of this package need not call Handshake
  1102. // explicitly: the first Read or Write will call it automatically.
  1103. func (c *Conn) Handshake() error {
  1104. // c.handshakeErr and c.handshakeComplete are protected by
  1105. // c.handshakeMutex. In order to perform a handshake, we need to lock
  1106. // c.in also and c.handshakeMutex must be locked after c.in.
  1107. //
  1108. // However, if a Read() operation is hanging then it'll be holding the
  1109. // lock on c.in and so taking it here would cause all operations that
  1110. // need to check whether a handshake is pending (such as Write) to
  1111. // block.
  1112. //
  1113. // Thus we first take c.handshakeMutex to check whether a handshake is
  1114. // needed.
  1115. //
  1116. // If so then, previously, this code would unlock handshakeMutex and
  1117. // then lock c.in and handshakeMutex in the correct order to run the
  1118. // handshake. The problem was that it was possible for a Read to
  1119. // complete the handshake once handshakeMutex was unlocked and then
  1120. // keep c.in while waiting for network data. Thus a concurrent
  1121. // operation could be blocked on c.in.
  1122. //
  1123. // Thus handshakeCond is used to signal that a goroutine is committed
  1124. // to running the handshake and other goroutines can wait on it if they
  1125. // need. handshakeCond is protected by handshakeMutex.
  1126. c.handshakeMutex.Lock()
  1127. defer c.handshakeMutex.Unlock()
  1128. for {
  1129. if err := c.handshakeErr; err != nil {
  1130. return err
  1131. }
  1132. if c.handshakeComplete {
  1133. return nil
  1134. }
  1135. if c.handshakeCond == nil {
  1136. break
  1137. }
  1138. c.handshakeCond.Wait()
  1139. }
  1140. // Set handshakeCond to indicate that this goroutine is committing to
  1141. // running the handshake.
  1142. c.handshakeCond = sync.NewCond(&c.handshakeMutex)
  1143. c.handshakeMutex.Unlock()
  1144. c.in.Lock()
  1145. defer c.in.Unlock()
  1146. c.handshakeMutex.Lock()
  1147. // The handshake cannot have completed when handshakeMutex was unlocked
  1148. // because this goroutine set handshakeCond.
  1149. if c.handshakeErr != nil || c.handshakeComplete {
  1150. panic("handshake should not have been able to complete after handshakeCond was set")
  1151. }
  1152. if c.isClient {
  1153. c.handshakeErr = c.clientHandshake()
  1154. } else {
  1155. c.handshakeErr = c.serverHandshake()
  1156. }
  1157. if c.handshakeErr == nil {
  1158. c.handshakes++
  1159. } else {
  1160. // If an error occurred during the hadshake try to flush the
  1161. // alert that might be left in the buffer.
  1162. c.flush()
  1163. }
  1164. if c.handshakeErr == nil && !c.handshakeComplete {
  1165. panic("handshake should have had a result.")
  1166. }
  1167. // Wake any other goroutines that are waiting for this handshake to
  1168. // complete.
  1169. c.handshakeCond.Broadcast()
  1170. c.handshakeCond = nil
  1171. return c.handshakeErr
  1172. }
  1173. // ConnectionState returns basic TLS details about the connection.
  1174. func (c *Conn) ConnectionState() ConnectionState {
  1175. c.handshakeMutex.Lock()
  1176. defer c.handshakeMutex.Unlock()
  1177. var state ConnectionState
  1178. state.HandshakeComplete = c.handshakeComplete
  1179. state.ServerName = c.serverName
  1180. if c.handshakeComplete {
  1181. state.Version = c.vers
  1182. state.NegotiatedProtocol = c.clientProtocol
  1183. state.DidResume = c.didResume
  1184. state.NegotiatedProtocolIsMutual = !c.clientProtocolFallback
  1185. state.CipherSuite = c.cipherSuite
  1186. state.PeerCertificates = c.peerCertificates
  1187. state.VerifiedChains = c.verifiedChains
  1188. state.SignedCertificateTimestamps = c.scts
  1189. state.OCSPResponse = c.ocspResponse
  1190. if !c.didResume {
  1191. if c.clientFinishedIsFirst {
  1192. state.TLSUnique = c.clientFinished[:]
  1193. } else {
  1194. state.TLSUnique = c.serverFinished[:]
  1195. }
  1196. }
  1197. }
  1198. return state
  1199. }
  1200. // OCSPResponse returns the stapled OCSP response from the TLS server, if
  1201. // any. (Only valid for client connections.)
  1202. func (c *Conn) OCSPResponse() []byte {
  1203. c.handshakeMutex.Lock()
  1204. defer c.handshakeMutex.Unlock()
  1205. return c.ocspResponse
  1206. }
  1207. // VerifyHostname checks that the peer certificate chain is valid for
  1208. // connecting to host. If so, it returns nil; if not, it returns an error
  1209. // describing the problem.
  1210. func (c *Conn) VerifyHostname(host string) error {
  1211. c.handshakeMutex.Lock()
  1212. defer c.handshakeMutex.Unlock()
  1213. if !c.isClient {
  1214. return errors.New("tls: VerifyHostname called on TLS server connection")
  1215. }
  1216. if !c.handshakeComplete {
  1217. return errors.New("tls: handshake has not yet been performed")
  1218. }
  1219. if len(c.verifiedChains) == 0 {
  1220. return errors.New("tls: handshake did not verify certificate chain")
  1221. }
  1222. return c.peerCertificates[0].VerifyHostname(host)
  1223. }