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.

conn.go 39 KiB

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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. // Handshake messages are not allowed to fragment across the CCS
  607. if c.hand.Len() > 0 {
  608. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  609. break
  610. }
  611. err := c.in.changeCipherSpec()
  612. if err != nil {
  613. c.in.setErrorLocked(c.sendAlert(err.(alert)))
  614. }
  615. case recordTypeApplicationData:
  616. if typ != want {
  617. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  618. break
  619. }
  620. c.input = b
  621. b = nil
  622. case recordTypeHandshake:
  623. // TODO(rsc): Should at least pick off connection close.
  624. if typ != want && !(c.isClient && c.config.Renegotiation != RenegotiateNever) {
  625. return c.in.setErrorLocked(c.sendAlert(alertNoRenegotiation))
  626. }
  627. c.hand.Write(data)
  628. }
  629. if b != nil {
  630. c.in.freeBlock(b)
  631. }
  632. return c.in.err
  633. }
  634. // sendAlert sends a TLS alert message.
  635. // c.out.Mutex <= L.
  636. func (c *Conn) sendAlertLocked(err alert) error {
  637. switch err {
  638. case alertNoRenegotiation, alertCloseNotify:
  639. c.tmp[0] = alertLevelWarning
  640. default:
  641. c.tmp[0] = alertLevelError
  642. }
  643. c.tmp[1] = byte(err)
  644. _, writeErr := c.writeRecordLocked(recordTypeAlert, c.tmp[0:2])
  645. if err == alertCloseNotify {
  646. // closeNotify is a special case in that it isn't an error.
  647. return writeErr
  648. }
  649. return c.out.setErrorLocked(&net.OpError{Op: "local error", Err: err})
  650. }
  651. // sendAlert sends a TLS alert message.
  652. // L < c.out.Mutex.
  653. func (c *Conn) sendAlert(err alert) error {
  654. c.out.Lock()
  655. defer c.out.Unlock()
  656. return c.sendAlertLocked(err)
  657. }
  658. const (
  659. // tcpMSSEstimate is a conservative estimate of the TCP maximum segment
  660. // size (MSS). A constant is used, rather than querying the kernel for
  661. // the actual MSS, to avoid complexity. The value here is the IPv6
  662. // minimum MTU (1280 bytes) minus the overhead of an IPv6 header (40
  663. // bytes) and a TCP header with timestamps (32 bytes).
  664. tcpMSSEstimate = 1208
  665. // recordSizeBoostThreshold is the number of bytes of application data
  666. // sent after which the TLS record size will be increased to the
  667. // maximum.
  668. recordSizeBoostThreshold = 128 * 1024
  669. )
  670. // maxPayloadSizeForWrite returns the maximum TLS payload size to use for the
  671. // next application data record. There is the following trade-off:
  672. //
  673. // - For latency-sensitive applications, such as web browsing, each TLS
  674. // record should fit in one TCP segment.
  675. // - For throughput-sensitive applications, such as large file transfers,
  676. // larger TLS records better amortize framing and encryption overheads.
  677. //
  678. // A simple heuristic that works well in practice is to use small records for
  679. // the first 1MB of data, then use larger records for subsequent data, and
  680. // reset back to smaller records after the connection becomes idle. See "High
  681. // Performance Web Networking", Chapter 4, or:
  682. // https://www.igvita.com/2013/10/24/optimizing-tls-record-size-and-buffering-latency/
  683. //
  684. // In the interests of simplicity and determinism, this code does not attempt
  685. // to reset the record size once the connection is idle, however.
  686. //
  687. // c.out.Mutex <= L.
  688. func (c *Conn) maxPayloadSizeForWrite(typ recordType, explicitIVLen int) int {
  689. if c.config.DynamicRecordSizingDisabled || typ != recordTypeApplicationData {
  690. return maxPlaintext
  691. }
  692. if c.bytesSent >= recordSizeBoostThreshold {
  693. return maxPlaintext
  694. }
  695. // Subtract TLS overheads to get the maximum payload size.
  696. macSize := 0
  697. if c.out.mac != nil {
  698. macSize = c.out.mac.Size()
  699. }
  700. payloadBytes := tcpMSSEstimate - recordHeaderLen - explicitIVLen
  701. if c.out.cipher != nil {
  702. switch ciph := c.out.cipher.(type) {
  703. case cipher.Stream:
  704. payloadBytes -= macSize
  705. case cipher.AEAD:
  706. payloadBytes -= ciph.Overhead()
  707. case cbcMode:
  708. blockSize := ciph.BlockSize()
  709. // The payload must fit in a multiple of blockSize, with
  710. // room for at least one padding byte.
  711. payloadBytes = (payloadBytes & ^(blockSize - 1)) - 1
  712. // The MAC is appended before padding so affects the
  713. // payload size directly.
  714. payloadBytes -= macSize
  715. default:
  716. panic("unknown cipher type")
  717. }
  718. }
  719. // Allow packet growth in arithmetic progression up to max.
  720. pkt := c.packetsSent
  721. c.packetsSent++
  722. if pkt > 1000 {
  723. return maxPlaintext // avoid overflow in multiply below
  724. }
  725. n := payloadBytes * int(pkt+1)
  726. if n > maxPlaintext {
  727. n = maxPlaintext
  728. }
  729. return n
  730. }
  731. // c.out.Mutex <= L.
  732. func (c *Conn) write(data []byte) (int, error) {
  733. if c.buffering {
  734. c.sendBuf = append(c.sendBuf, data...)
  735. return len(data), nil
  736. }
  737. n, err := c.conn.Write(data)
  738. c.bytesSent += int64(n)
  739. return n, err
  740. }
  741. func (c *Conn) flush() (int, error) {
  742. if len(c.sendBuf) == 0 {
  743. return 0, nil
  744. }
  745. n, err := c.conn.Write(c.sendBuf)
  746. c.bytesSent += int64(n)
  747. c.sendBuf = nil
  748. c.buffering = false
  749. return n, err
  750. }
  751. // writeRecordLocked writes a TLS record with the given type and payload to the
  752. // connection and updates the record layer state.
  753. // c.out.Mutex <= L.
  754. func (c *Conn) writeRecordLocked(typ recordType, data []byte) (int, error) {
  755. b := c.out.newBlock()
  756. defer c.out.freeBlock(b)
  757. var n int
  758. for len(data) > 0 {
  759. explicitIVLen := 0
  760. explicitIVIsSeq := false
  761. var cbc cbcMode
  762. if c.out.version >= VersionTLS11 {
  763. var ok bool
  764. if cbc, ok = c.out.cipher.(cbcMode); ok {
  765. explicitIVLen = cbc.BlockSize()
  766. }
  767. }
  768. if explicitIVLen == 0 {
  769. if c, ok := c.out.cipher.(aead); ok {
  770. explicitIVLen = c.explicitNonceLen()
  771. // The AES-GCM construction in TLS has an
  772. // explicit nonce so that the nonce can be
  773. // random. However, the nonce is only 8 bytes
  774. // which is too small for a secure, random
  775. // nonce. Therefore we use the sequence number
  776. // as the nonce.
  777. explicitIVIsSeq = explicitIVLen > 0
  778. }
  779. }
  780. m := len(data)
  781. if maxPayload := c.maxPayloadSizeForWrite(typ, explicitIVLen); m > maxPayload {
  782. m = maxPayload
  783. }
  784. b.resize(recordHeaderLen + explicitIVLen + m)
  785. b.data[0] = byte(typ)
  786. vers := c.vers
  787. if vers == 0 {
  788. // Some TLS servers fail if the record version is
  789. // greater than TLS 1.0 for the initial ClientHello.
  790. vers = VersionTLS10
  791. }
  792. b.data[1] = byte(vers >> 8)
  793. b.data[2] = byte(vers)
  794. b.data[3] = byte(m >> 8)
  795. b.data[4] = byte(m)
  796. if explicitIVLen > 0 {
  797. explicitIV := b.data[recordHeaderLen : recordHeaderLen+explicitIVLen]
  798. if explicitIVIsSeq {
  799. copy(explicitIV, c.out.seq[:])
  800. } else {
  801. if _, err := io.ReadFull(c.config.rand(), explicitIV); err != nil {
  802. return n, err
  803. }
  804. }
  805. }
  806. copy(b.data[recordHeaderLen+explicitIVLen:], data)
  807. c.out.encrypt(b, explicitIVLen)
  808. if _, err := c.write(b.data); err != nil {
  809. return n, err
  810. }
  811. n += m
  812. data = data[m:]
  813. }
  814. if typ == recordTypeChangeCipherSpec {
  815. if err := c.out.changeCipherSpec(); err != nil {
  816. return n, c.sendAlertLocked(err.(alert))
  817. }
  818. }
  819. return n, nil
  820. }
  821. // writeRecord writes a TLS record with the given type and payload to the
  822. // connection and updates the record layer state.
  823. // L < c.out.Mutex.
  824. func (c *Conn) writeRecord(typ recordType, data []byte) (int, error) {
  825. c.out.Lock()
  826. defer c.out.Unlock()
  827. return c.writeRecordLocked(typ, data)
  828. }
  829. // readHandshake reads the next handshake message from
  830. // the record layer.
  831. // c.in.Mutex < L; c.out.Mutex < L.
  832. func (c *Conn) readHandshake() (interface{}, error) {
  833. for c.hand.Len() < 4 {
  834. if err := c.in.err; err != nil {
  835. return nil, err
  836. }
  837. if err := c.readRecord(recordTypeHandshake); err != nil {
  838. return nil, err
  839. }
  840. }
  841. data := c.hand.Bytes()
  842. n := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  843. if n > maxHandshake {
  844. c.sendAlertLocked(alertInternalError)
  845. return nil, c.in.setErrorLocked(fmt.Errorf("tls: handshake message of length %d bytes exceeds maximum of %d bytes", n, maxHandshake))
  846. }
  847. for c.hand.Len() < 4+n {
  848. if err := c.in.err; err != nil {
  849. return nil, err
  850. }
  851. if err := c.readRecord(recordTypeHandshake); err != nil {
  852. return nil, err
  853. }
  854. }
  855. data = c.hand.Next(4 + n)
  856. var m handshakeMessage
  857. switch data[0] {
  858. case typeHelloRequest:
  859. m = new(helloRequestMsg)
  860. case typeClientHello:
  861. m = new(clientHelloMsg)
  862. case typeServerHello:
  863. m = new(serverHelloMsg)
  864. case typeNewSessionTicket:
  865. m = new(newSessionTicketMsg)
  866. case typeCertificate:
  867. m = new(certificateMsg)
  868. case typeCertificateRequest:
  869. m = &certificateRequestMsg{
  870. hasSignatureAndHash: c.vers >= VersionTLS12,
  871. }
  872. case typeCertificateStatus:
  873. m = new(certificateStatusMsg)
  874. case typeServerKeyExchange:
  875. m = new(serverKeyExchangeMsg)
  876. case typeServerHelloDone:
  877. m = new(serverHelloDoneMsg)
  878. case typeClientKeyExchange:
  879. m = new(clientKeyExchangeMsg)
  880. case typeCertificateVerify:
  881. m = &certificateVerifyMsg{
  882. hasSignatureAndHash: c.vers >= VersionTLS12,
  883. }
  884. case typeNextProtocol:
  885. m = new(nextProtoMsg)
  886. case typeFinished:
  887. m = new(finishedMsg)
  888. default:
  889. return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  890. }
  891. // The handshake message unmarshalers
  892. // expect to be able to keep references to data,
  893. // so pass in a fresh copy that won't be overwritten.
  894. data = append([]byte(nil), data...)
  895. if !m.unmarshal(data) {
  896. return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  897. }
  898. return m, nil
  899. }
  900. var (
  901. errClosed = errors.New("tls: use of closed connection")
  902. errShutdown = errors.New("tls: protocol is shutdown")
  903. )
  904. // Write writes data to the connection.
  905. func (c *Conn) Write(b []byte) (int, error) {
  906. // interlock with Close below
  907. for {
  908. x := atomic.LoadInt32(&c.activeCall)
  909. if x&1 != 0 {
  910. return 0, errClosed
  911. }
  912. if atomic.CompareAndSwapInt32(&c.activeCall, x, x+2) {
  913. defer atomic.AddInt32(&c.activeCall, -2)
  914. break
  915. }
  916. }
  917. if err := c.Handshake(); err != nil {
  918. return 0, err
  919. }
  920. c.out.Lock()
  921. defer c.out.Unlock()
  922. if err := c.out.err; err != nil {
  923. return 0, err
  924. }
  925. if !c.handshakeComplete {
  926. return 0, alertInternalError
  927. }
  928. if c.closeNotifySent {
  929. return 0, errShutdown
  930. }
  931. // SSL 3.0 and TLS 1.0 are susceptible to a chosen-plaintext
  932. // attack when using block mode ciphers due to predictable IVs.
  933. // This can be prevented by splitting each Application Data
  934. // record into two records, effectively randomizing the IV.
  935. //
  936. // http://www.openssl.org/~bodo/tls-cbc.txt
  937. // https://bugzilla.mozilla.org/show_bug.cgi?id=665814
  938. // http://www.imperialviolet.org/2012/01/15/beastfollowup.html
  939. var m int
  940. if len(b) > 1 && c.vers <= VersionTLS10 {
  941. if _, ok := c.out.cipher.(cipher.BlockMode); ok {
  942. n, err := c.writeRecordLocked(recordTypeApplicationData, b[:1])
  943. if err != nil {
  944. return n, c.out.setErrorLocked(err)
  945. }
  946. m, b = 1, b[1:]
  947. }
  948. }
  949. n, err := c.writeRecordLocked(recordTypeApplicationData, b)
  950. return n + m, c.out.setErrorLocked(err)
  951. }
  952. // handleRenegotiation processes a HelloRequest handshake message.
  953. // c.in.Mutex <= L
  954. func (c *Conn) handleRenegotiation() error {
  955. msg, err := c.readHandshake()
  956. if err != nil {
  957. return err
  958. }
  959. _, ok := msg.(*helloRequestMsg)
  960. if !ok {
  961. c.sendAlert(alertUnexpectedMessage)
  962. return alertUnexpectedMessage
  963. }
  964. if !c.isClient {
  965. return c.sendAlert(alertNoRenegotiation)
  966. }
  967. switch c.config.Renegotiation {
  968. case RenegotiateNever:
  969. return c.sendAlert(alertNoRenegotiation)
  970. case RenegotiateOnceAsClient:
  971. if c.handshakes > 1 {
  972. return c.sendAlert(alertNoRenegotiation)
  973. }
  974. case RenegotiateFreelyAsClient:
  975. // Ok.
  976. default:
  977. c.sendAlert(alertInternalError)
  978. return errors.New("tls: unknown Renegotiation value")
  979. }
  980. c.handshakeMutex.Lock()
  981. defer c.handshakeMutex.Unlock()
  982. c.handshakeComplete = false
  983. if c.handshakeErr = c.clientHandshake(); c.handshakeErr == nil {
  984. c.handshakes++
  985. }
  986. return c.handshakeErr
  987. }
  988. // Read can be made to time out and return a net.Error with Timeout() == true
  989. // after a fixed time limit; see SetDeadline and SetReadDeadline.
  990. func (c *Conn) Read(b []byte) (n int, err error) {
  991. if err = c.Handshake(); err != nil {
  992. return
  993. }
  994. if len(b) == 0 {
  995. // Put this after Handshake, in case people were calling
  996. // Read(nil) for the side effect of the Handshake.
  997. return
  998. }
  999. c.in.Lock()
  1000. defer c.in.Unlock()
  1001. // Some OpenSSL servers send empty records in order to randomize the
  1002. // CBC IV. So this loop ignores a limited number of empty records.
  1003. const maxConsecutiveEmptyRecords = 100
  1004. for emptyRecordCount := 0; emptyRecordCount <= maxConsecutiveEmptyRecords; emptyRecordCount++ {
  1005. for c.input == nil && c.in.err == nil {
  1006. if err := c.readRecord(recordTypeApplicationData); err != nil {
  1007. // Soft error, like EAGAIN
  1008. return 0, err
  1009. }
  1010. if c.hand.Len() > 0 {
  1011. // We received handshake bytes, indicating the
  1012. // start of a renegotiation.
  1013. if err := c.handleRenegotiation(); err != nil {
  1014. return 0, err
  1015. }
  1016. }
  1017. }
  1018. if err := c.in.err; err != nil {
  1019. return 0, err
  1020. }
  1021. n, err = c.input.Read(b)
  1022. if c.input.off >= len(c.input.data) {
  1023. c.in.freeBlock(c.input)
  1024. c.input = nil
  1025. }
  1026. // If a close-notify alert is waiting, read it so that
  1027. // we can return (n, EOF) instead of (n, nil), to signal
  1028. // to the HTTP response reading goroutine that the
  1029. // connection is now closed. This eliminates a race
  1030. // where the HTTP response reading goroutine would
  1031. // otherwise not observe the EOF until its next read,
  1032. // by which time a client goroutine might have already
  1033. // tried to reuse the HTTP connection for a new
  1034. // request.
  1035. // See https://codereview.appspot.com/76400046
  1036. // and https://golang.org/issue/3514
  1037. if ri := c.rawInput; ri != nil &&
  1038. n != 0 && err == nil &&
  1039. c.input == nil && len(ri.data) > 0 && recordType(ri.data[0]) == recordTypeAlert {
  1040. if recErr := c.readRecord(recordTypeApplicationData); recErr != nil {
  1041. err = recErr // will be io.EOF on closeNotify
  1042. }
  1043. }
  1044. if n != 0 || err != nil {
  1045. return n, err
  1046. }
  1047. }
  1048. return 0, io.ErrNoProgress
  1049. }
  1050. // Close closes the connection.
  1051. func (c *Conn) Close() error {
  1052. // Interlock with Conn.Write above.
  1053. var x int32
  1054. for {
  1055. x = atomic.LoadInt32(&c.activeCall)
  1056. if x&1 != 0 {
  1057. return errClosed
  1058. }
  1059. if atomic.CompareAndSwapInt32(&c.activeCall, x, x|1) {
  1060. break
  1061. }
  1062. }
  1063. if x != 0 {
  1064. // io.Writer and io.Closer should not be used concurrently.
  1065. // If Close is called while a Write is currently in-flight,
  1066. // interpret that as a sign that this Close is really just
  1067. // being used to break the Write and/or clean up resources and
  1068. // avoid sending the alertCloseNotify, which may block
  1069. // waiting on handshakeMutex or the c.out mutex.
  1070. return c.conn.Close()
  1071. }
  1072. var alertErr error
  1073. c.handshakeMutex.Lock()
  1074. if c.handshakeComplete {
  1075. alertErr = c.closeNotify()
  1076. }
  1077. c.handshakeMutex.Unlock()
  1078. if err := c.conn.Close(); err != nil {
  1079. return err
  1080. }
  1081. return alertErr
  1082. }
  1083. var errEarlyCloseWrite = errors.New("tls: CloseWrite called before handshake complete")
  1084. // CloseWrite shuts down the writing side of the connection. It should only be
  1085. // called once the handshake has completed and does not call CloseWrite on the
  1086. // underlying connection. Most callers should just use Close.
  1087. func (c *Conn) CloseWrite() error {
  1088. c.handshakeMutex.Lock()
  1089. defer c.handshakeMutex.Unlock()
  1090. if !c.handshakeComplete {
  1091. return errEarlyCloseWrite
  1092. }
  1093. return c.closeNotify()
  1094. }
  1095. func (c *Conn) closeNotify() error {
  1096. c.out.Lock()
  1097. defer c.out.Unlock()
  1098. if !c.closeNotifySent {
  1099. c.closeNotifyErr = c.sendAlertLocked(alertCloseNotify)
  1100. c.closeNotifySent = true
  1101. }
  1102. return c.closeNotifyErr
  1103. }
  1104. // Handshake runs the client or server handshake
  1105. // protocol if it has not yet been run.
  1106. // Most uses of this package need not call Handshake
  1107. // explicitly: the first Read or Write will call it automatically.
  1108. func (c *Conn) Handshake() error {
  1109. // c.handshakeErr and c.handshakeComplete are protected by
  1110. // c.handshakeMutex. In order to perform a handshake, we need to lock
  1111. // c.in also and c.handshakeMutex must be locked after c.in.
  1112. //
  1113. // However, if a Read() operation is hanging then it'll be holding the
  1114. // lock on c.in and so taking it here would cause all operations that
  1115. // need to check whether a handshake is pending (such as Write) to
  1116. // block.
  1117. //
  1118. // Thus we first take c.handshakeMutex to check whether a handshake is
  1119. // needed.
  1120. //
  1121. // If so then, previously, this code would unlock handshakeMutex and
  1122. // then lock c.in and handshakeMutex in the correct order to run the
  1123. // handshake. The problem was that it was possible for a Read to
  1124. // complete the handshake once handshakeMutex was unlocked and then
  1125. // keep c.in while waiting for network data. Thus a concurrent
  1126. // operation could be blocked on c.in.
  1127. //
  1128. // Thus handshakeCond is used to signal that a goroutine is committed
  1129. // to running the handshake and other goroutines can wait on it if they
  1130. // need. handshakeCond is protected by handshakeMutex.
  1131. c.handshakeMutex.Lock()
  1132. defer c.handshakeMutex.Unlock()
  1133. for {
  1134. if err := c.handshakeErr; err != nil {
  1135. return err
  1136. }
  1137. if c.handshakeComplete {
  1138. return nil
  1139. }
  1140. if c.handshakeCond == nil {
  1141. break
  1142. }
  1143. c.handshakeCond.Wait()
  1144. }
  1145. // Set handshakeCond to indicate that this goroutine is committing to
  1146. // running the handshake.
  1147. c.handshakeCond = sync.NewCond(&c.handshakeMutex)
  1148. c.handshakeMutex.Unlock()
  1149. c.in.Lock()
  1150. defer c.in.Unlock()
  1151. c.handshakeMutex.Lock()
  1152. // The handshake cannot have completed when handshakeMutex was unlocked
  1153. // because this goroutine set handshakeCond.
  1154. if c.handshakeErr != nil || c.handshakeComplete {
  1155. panic("handshake should not have been able to complete after handshakeCond was set")
  1156. }
  1157. if c.isClient {
  1158. c.handshakeErr = c.clientHandshake()
  1159. } else {
  1160. c.handshakeErr = c.serverHandshake()
  1161. }
  1162. if c.handshakeErr == nil {
  1163. c.handshakes++
  1164. } else {
  1165. // If an error occurred during the hadshake try to flush the
  1166. // alert that might be left in the buffer.
  1167. c.flush()
  1168. }
  1169. if c.handshakeErr == nil && !c.handshakeComplete {
  1170. panic("handshake should have had a result.")
  1171. }
  1172. // Wake any other goroutines that are waiting for this handshake to
  1173. // complete.
  1174. c.handshakeCond.Broadcast()
  1175. c.handshakeCond = nil
  1176. return c.handshakeErr
  1177. }
  1178. // ConnectionState returns basic TLS details about the connection.
  1179. func (c *Conn) ConnectionState() ConnectionState {
  1180. c.handshakeMutex.Lock()
  1181. defer c.handshakeMutex.Unlock()
  1182. var state ConnectionState
  1183. state.HandshakeComplete = c.handshakeComplete
  1184. state.ServerName = c.serverName
  1185. if c.handshakeComplete {
  1186. state.Version = c.vers
  1187. state.NegotiatedProtocol = c.clientProtocol
  1188. state.DidResume = c.didResume
  1189. state.NegotiatedProtocolIsMutual = !c.clientProtocolFallback
  1190. state.CipherSuite = c.cipherSuite
  1191. state.PeerCertificates = c.peerCertificates
  1192. state.VerifiedChains = c.verifiedChains
  1193. state.SignedCertificateTimestamps = c.scts
  1194. state.OCSPResponse = c.ocspResponse
  1195. if !c.didResume {
  1196. if c.clientFinishedIsFirst {
  1197. state.TLSUnique = c.clientFinished[:]
  1198. } else {
  1199. state.TLSUnique = c.serverFinished[:]
  1200. }
  1201. }
  1202. }
  1203. return state
  1204. }
  1205. // OCSPResponse returns the stapled OCSP response from the TLS server, if
  1206. // any. (Only valid for client connections.)
  1207. func (c *Conn) OCSPResponse() []byte {
  1208. c.handshakeMutex.Lock()
  1209. defer c.handshakeMutex.Unlock()
  1210. return c.ocspResponse
  1211. }
  1212. // VerifyHostname checks that the peer certificate chain is valid for
  1213. // connecting to host. If so, it returns nil; if not, it returns an error
  1214. // describing the problem.
  1215. func (c *Conn) VerifyHostname(host string) error {
  1216. c.handshakeMutex.Lock()
  1217. defer c.handshakeMutex.Unlock()
  1218. if !c.isClient {
  1219. return errors.New("tls: VerifyHostname called on TLS server connection")
  1220. }
  1221. if !c.handshakeComplete {
  1222. return errors.New("tls: handshake has not yet been performed")
  1223. }
  1224. if len(c.verifiedChains) == 0 {
  1225. return errors.New("tls: handshake did not verify certificate chain")
  1226. }
  1227. return c.peerCertificates[0].VerifyHostname(host)
  1228. }