Alternative TLS implementation in Go
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

conn.go 28 KiB

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 år sedan
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 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. "time"
  17. )
  18. // A Conn represents a secured connection.
  19. // It implements the net.Conn interface.
  20. type Conn struct {
  21. // constant
  22. conn net.Conn
  23. isClient bool
  24. // constant after handshake; protected by handshakeMutex
  25. handshakeMutex sync.Mutex // handshakeMutex < in.Mutex, out.Mutex, errMutex
  26. handshakeErr error // error resulting from handshake
  27. vers uint16 // TLS version
  28. haveVers bool // version has been negotiated
  29. config *Config // configuration passed to constructor
  30. handshakeComplete bool
  31. didResume bool // whether this connection was a session resumption
  32. cipherSuite uint16
  33. ocspResponse []byte // stapled OCSP response
  34. scts [][]byte // signed certificate timestamps from server
  35. peerCertificates []*x509.Certificate
  36. // verifiedChains contains the certificate chains that we built, as
  37. // opposed to the ones presented by the server.
  38. verifiedChains [][]*x509.Certificate
  39. // serverName contains the server name indicated by the client, if any.
  40. serverName string
  41. // firstFinished contains the first Finished hash sent during the
  42. // handshake. This is the "tls-unique" channel binding value.
  43. firstFinished [12]byte
  44. clientProtocol string
  45. clientProtocolFallback bool
  46. // input/output
  47. in, out halfConn // in.Mutex < out.Mutex
  48. rawInput *block // raw input, right off the wire
  49. input *block // application data waiting to be read
  50. hand bytes.Buffer // handshake data waiting to be read
  51. tmp [16]byte
  52. }
  53. // Access to net.Conn methods.
  54. // Cannot just embed net.Conn because that would
  55. // export the struct field too.
  56. // LocalAddr returns the local network address.
  57. func (c *Conn) LocalAddr() net.Addr {
  58. return c.conn.LocalAddr()
  59. }
  60. // RemoteAddr returns the remote network address.
  61. func (c *Conn) RemoteAddr() net.Addr {
  62. return c.conn.RemoteAddr()
  63. }
  64. // SetDeadline sets the read and write deadlines associated with the connection.
  65. // A zero value for t means Read and Write will not time out.
  66. // After a Write has timed out, the TLS state is corrupt and all future writes will return the same error.
  67. func (c *Conn) SetDeadline(t time.Time) error {
  68. return c.conn.SetDeadline(t)
  69. }
  70. // SetReadDeadline sets the read deadline on the underlying connection.
  71. // A zero value for t means Read will not time out.
  72. func (c *Conn) SetReadDeadline(t time.Time) error {
  73. return c.conn.SetReadDeadline(t)
  74. }
  75. // SetWriteDeadline sets the write deadline on the underlying connection.
  76. // A zero value for t means Write will not time out.
  77. // After a Write has timed out, the TLS state is corrupt and all future writes will return the same error.
  78. func (c *Conn) SetWriteDeadline(t time.Time) error {
  79. return c.conn.SetWriteDeadline(t)
  80. }
  81. // A halfConn represents one direction of the record layer
  82. // connection, either sending or receiving.
  83. type halfConn struct {
  84. sync.Mutex
  85. err error // first permanent error
  86. version uint16 // protocol version
  87. cipher interface{} // cipher algorithm
  88. mac macFunction
  89. seq [8]byte // 64-bit sequence number
  90. bfree *block // list of free blocks
  91. nextCipher interface{} // next encryption state
  92. nextMac macFunction // next MAC algorithm
  93. // used to save allocating a new buffer for each MAC.
  94. inDigestBuf, outDigestBuf []byte
  95. }
  96. func (hc *halfConn) setErrorLocked(err error) error {
  97. hc.err = err
  98. return err
  99. }
  100. func (hc *halfConn) error() error {
  101. hc.Lock()
  102. err := hc.err
  103. hc.Unlock()
  104. return err
  105. }
  106. // prepareCipherSpec sets the encryption and MAC states
  107. // that a subsequent changeCipherSpec will use.
  108. func (hc *halfConn) prepareCipherSpec(version uint16, cipher interface{}, mac macFunction) {
  109. hc.version = version
  110. hc.nextCipher = cipher
  111. hc.nextMac = mac
  112. }
  113. // changeCipherSpec changes the encryption and MAC states
  114. // to the ones previously passed to prepareCipherSpec.
  115. func (hc *halfConn) changeCipherSpec() error {
  116. if hc.nextCipher == nil {
  117. return alertInternalError
  118. }
  119. hc.cipher = hc.nextCipher
  120. hc.mac = hc.nextMac
  121. hc.nextCipher = nil
  122. hc.nextMac = nil
  123. for i := range hc.seq {
  124. hc.seq[i] = 0
  125. }
  126. return nil
  127. }
  128. // incSeq increments the sequence number.
  129. func (hc *halfConn) incSeq() {
  130. for i := 7; i >= 0; i-- {
  131. hc.seq[i]++
  132. if hc.seq[i] != 0 {
  133. return
  134. }
  135. }
  136. // Not allowed to let sequence number wrap.
  137. // Instead, must renegotiate before it does.
  138. // Not likely enough to bother.
  139. panic("TLS: sequence number wraparound")
  140. }
  141. // resetSeq resets the sequence number to zero.
  142. func (hc *halfConn) resetSeq() {
  143. for i := range hc.seq {
  144. hc.seq[i] = 0
  145. }
  146. }
  147. // removePadding returns an unpadded slice, in constant time, which is a prefix
  148. // of the input. It also returns a byte which is equal to 255 if the padding
  149. // was valid and 0 otherwise. See RFC 2246, section 6.2.3.2
  150. func removePadding(payload []byte) ([]byte, byte) {
  151. if len(payload) < 1 {
  152. return payload, 0
  153. }
  154. paddingLen := payload[len(payload)-1]
  155. t := uint(len(payload)-1) - uint(paddingLen)
  156. // if len(payload) >= (paddingLen - 1) then the MSB of t is zero
  157. good := byte(int32(^t) >> 31)
  158. toCheck := 255 // the maximum possible padding length
  159. // The length of the padded data is public, so we can use an if here
  160. if toCheck+1 > len(payload) {
  161. toCheck = len(payload) - 1
  162. }
  163. for i := 0; i < toCheck; i++ {
  164. t := uint(paddingLen) - uint(i)
  165. // if i <= paddingLen then the MSB of t is zero
  166. mask := byte(int32(^t) >> 31)
  167. b := payload[len(payload)-1-i]
  168. good &^= mask&paddingLen ^ mask&b
  169. }
  170. // We AND together the bits of good and replicate the result across
  171. // all the bits.
  172. good &= good << 4
  173. good &= good << 2
  174. good &= good << 1
  175. good = uint8(int8(good) >> 7)
  176. toRemove := good&paddingLen + 1
  177. return payload[:len(payload)-int(toRemove)], good
  178. }
  179. // removePaddingSSL30 is a replacement for removePadding in the case that the
  180. // protocol version is SSLv3. In this version, the contents of the padding
  181. // are random and cannot be checked.
  182. func removePaddingSSL30(payload []byte) ([]byte, byte) {
  183. if len(payload) < 1 {
  184. return payload, 0
  185. }
  186. paddingLen := int(payload[len(payload)-1]) + 1
  187. if paddingLen > len(payload) {
  188. return payload, 0
  189. }
  190. return payload[:len(payload)-paddingLen], 255
  191. }
  192. func roundUp(a, b int) int {
  193. return a + (b-a%b)%b
  194. }
  195. // cbcMode is an interface for block ciphers using cipher block chaining.
  196. type cbcMode interface {
  197. cipher.BlockMode
  198. SetIV([]byte)
  199. }
  200. // decrypt checks and strips the mac and decrypts the data in b. Returns a
  201. // success boolean, the number of bytes to skip from the start of the record in
  202. // order to get the application payload, and an optional alert value.
  203. func (hc *halfConn) decrypt(b *block) (ok bool, prefixLen int, alertValue alert) {
  204. // pull out payload
  205. payload := b.data[recordHeaderLen:]
  206. macSize := 0
  207. if hc.mac != nil {
  208. macSize = hc.mac.Size()
  209. }
  210. paddingGood := byte(255)
  211. explicitIVLen := 0
  212. // decrypt
  213. if hc.cipher != nil {
  214. switch c := hc.cipher.(type) {
  215. case cipher.Stream:
  216. c.XORKeyStream(payload, payload)
  217. case cipher.AEAD:
  218. explicitIVLen = 8
  219. if len(payload) < explicitIVLen {
  220. return false, 0, alertBadRecordMAC
  221. }
  222. nonce := payload[:8]
  223. payload = payload[8:]
  224. var additionalData [13]byte
  225. copy(additionalData[:], hc.seq[:])
  226. copy(additionalData[8:], b.data[:3])
  227. n := len(payload) - c.Overhead()
  228. additionalData[11] = byte(n >> 8)
  229. additionalData[12] = byte(n)
  230. var err error
  231. payload, err = c.Open(payload[:0], nonce, payload, additionalData[:])
  232. if err != nil {
  233. return false, 0, alertBadRecordMAC
  234. }
  235. b.resize(recordHeaderLen + explicitIVLen + len(payload))
  236. case cbcMode:
  237. blockSize := c.BlockSize()
  238. if hc.version >= VersionTLS11 {
  239. explicitIVLen = blockSize
  240. }
  241. if len(payload)%blockSize != 0 || len(payload) < roundUp(explicitIVLen+macSize+1, blockSize) {
  242. return false, 0, alertBadRecordMAC
  243. }
  244. if explicitIVLen > 0 {
  245. c.SetIV(payload[:explicitIVLen])
  246. payload = payload[explicitIVLen:]
  247. }
  248. c.CryptBlocks(payload, payload)
  249. if hc.version == VersionSSL30 {
  250. payload, paddingGood = removePaddingSSL30(payload)
  251. } else {
  252. payload, paddingGood = removePadding(payload)
  253. }
  254. b.resize(recordHeaderLen + explicitIVLen + len(payload))
  255. // note that we still have a timing side-channel in the
  256. // MAC check, below. An attacker can align the record
  257. // so that a correct padding will cause one less hash
  258. // block to be calculated. Then they can iteratively
  259. // decrypt a record by breaking each byte. See
  260. // "Password Interception in a SSL/TLS Channel", Brice
  261. // Canvel et al.
  262. //
  263. // However, our behavior matches OpenSSL, so we leak
  264. // only as much as they do.
  265. default:
  266. panic("unknown cipher type")
  267. }
  268. }
  269. // check, strip mac
  270. if hc.mac != nil {
  271. if len(payload) < macSize {
  272. return false, 0, alertBadRecordMAC
  273. }
  274. // strip mac off payload, b.data
  275. n := len(payload) - macSize
  276. b.data[3] = byte(n >> 8)
  277. b.data[4] = byte(n)
  278. b.resize(recordHeaderLen + explicitIVLen + n)
  279. remoteMAC := payload[n:]
  280. localMAC := hc.mac.MAC(hc.inDigestBuf, hc.seq[0:], b.data[:recordHeaderLen], payload[:n])
  281. if subtle.ConstantTimeCompare(localMAC, remoteMAC) != 1 || paddingGood != 255 {
  282. return false, 0, alertBadRecordMAC
  283. }
  284. hc.inDigestBuf = localMAC
  285. }
  286. hc.incSeq()
  287. return true, recordHeaderLen + explicitIVLen, 0
  288. }
  289. // padToBlockSize calculates the needed padding block, if any, for a payload.
  290. // On exit, prefix aliases payload and extends to the end of the last full
  291. // block of payload. finalBlock is a fresh slice which contains the contents of
  292. // any suffix of payload as well as the needed padding to make finalBlock a
  293. // full block.
  294. func padToBlockSize(payload []byte, blockSize int) (prefix, finalBlock []byte) {
  295. overrun := len(payload) % blockSize
  296. paddingLen := blockSize - overrun
  297. prefix = payload[:len(payload)-overrun]
  298. finalBlock = make([]byte, blockSize)
  299. copy(finalBlock, payload[len(payload)-overrun:])
  300. for i := overrun; i < blockSize; i++ {
  301. finalBlock[i] = byte(paddingLen - 1)
  302. }
  303. return
  304. }
  305. // encrypt encrypts and macs the data in b.
  306. func (hc *halfConn) encrypt(b *block, explicitIVLen int) (bool, alert) {
  307. // mac
  308. if hc.mac != nil {
  309. mac := hc.mac.MAC(hc.outDigestBuf, hc.seq[0:], b.data[:recordHeaderLen], b.data[recordHeaderLen+explicitIVLen:])
  310. n := len(b.data)
  311. b.resize(n + len(mac))
  312. copy(b.data[n:], mac)
  313. hc.outDigestBuf = mac
  314. }
  315. payload := b.data[recordHeaderLen:]
  316. // encrypt
  317. if hc.cipher != nil {
  318. switch c := hc.cipher.(type) {
  319. case cipher.Stream:
  320. c.XORKeyStream(payload, payload)
  321. case cipher.AEAD:
  322. payloadLen := len(b.data) - recordHeaderLen - explicitIVLen
  323. b.resize(len(b.data) + c.Overhead())
  324. nonce := b.data[recordHeaderLen : recordHeaderLen+explicitIVLen]
  325. payload := b.data[recordHeaderLen+explicitIVLen:]
  326. payload = payload[:payloadLen]
  327. var additionalData [13]byte
  328. copy(additionalData[:], hc.seq[:])
  329. copy(additionalData[8:], b.data[:3])
  330. additionalData[11] = byte(payloadLen >> 8)
  331. additionalData[12] = byte(payloadLen)
  332. c.Seal(payload[:0], nonce, payload, additionalData[:])
  333. case cbcMode:
  334. blockSize := c.BlockSize()
  335. if explicitIVLen > 0 {
  336. c.SetIV(payload[:explicitIVLen])
  337. payload = payload[explicitIVLen:]
  338. }
  339. prefix, finalBlock := padToBlockSize(payload, blockSize)
  340. b.resize(recordHeaderLen + explicitIVLen + len(prefix) + len(finalBlock))
  341. c.CryptBlocks(b.data[recordHeaderLen+explicitIVLen:], prefix)
  342. c.CryptBlocks(b.data[recordHeaderLen+explicitIVLen+len(prefix):], finalBlock)
  343. default:
  344. panic("unknown cipher type")
  345. }
  346. }
  347. // update length to include MAC and any block padding needed.
  348. n := len(b.data) - recordHeaderLen
  349. b.data[3] = byte(n >> 8)
  350. b.data[4] = byte(n)
  351. hc.incSeq()
  352. return true, 0
  353. }
  354. // A block is a simple data buffer.
  355. type block struct {
  356. data []byte
  357. off int // index for Read
  358. link *block
  359. }
  360. // resize resizes block to be n bytes, growing if necessary.
  361. func (b *block) resize(n int) {
  362. if n > cap(b.data) {
  363. b.reserve(n)
  364. }
  365. b.data = b.data[0:n]
  366. }
  367. // reserve makes sure that block contains a capacity of at least n bytes.
  368. func (b *block) reserve(n int) {
  369. if cap(b.data) >= n {
  370. return
  371. }
  372. m := cap(b.data)
  373. if m == 0 {
  374. m = 1024
  375. }
  376. for m < n {
  377. m *= 2
  378. }
  379. data := make([]byte, len(b.data), m)
  380. copy(data, b.data)
  381. b.data = data
  382. }
  383. // readFromUntil reads from r into b until b contains at least n bytes
  384. // or else returns an error.
  385. func (b *block) readFromUntil(r io.Reader, n int) error {
  386. // quick case
  387. if len(b.data) >= n {
  388. return nil
  389. }
  390. // read until have enough.
  391. b.reserve(n)
  392. for {
  393. m, err := r.Read(b.data[len(b.data):cap(b.data)])
  394. b.data = b.data[0 : len(b.data)+m]
  395. if len(b.data) >= n {
  396. // TODO(bradfitz,agl): slightly suspicious
  397. // that we're throwing away r.Read's err here.
  398. break
  399. }
  400. if err != nil {
  401. return err
  402. }
  403. }
  404. return nil
  405. }
  406. func (b *block) Read(p []byte) (n int, err error) {
  407. n = copy(p, b.data[b.off:])
  408. b.off += n
  409. return
  410. }
  411. // newBlock allocates a new block, from hc's free list if possible.
  412. func (hc *halfConn) newBlock() *block {
  413. b := hc.bfree
  414. if b == nil {
  415. return new(block)
  416. }
  417. hc.bfree = b.link
  418. b.link = nil
  419. b.resize(0)
  420. return b
  421. }
  422. // freeBlock returns a block to hc's free list.
  423. // The protocol is such that each side only has a block or two on
  424. // its free list at a time, so there's no need to worry about
  425. // trimming the list, etc.
  426. func (hc *halfConn) freeBlock(b *block) {
  427. b.link = hc.bfree
  428. hc.bfree = b
  429. }
  430. // splitBlock splits a block after the first n bytes,
  431. // returning a block with those n bytes and a
  432. // block with the remainder. the latter may be nil.
  433. func (hc *halfConn) splitBlock(b *block, n int) (*block, *block) {
  434. if len(b.data) <= n {
  435. return b, nil
  436. }
  437. bb := hc.newBlock()
  438. bb.resize(len(b.data) - n)
  439. copy(bb.data, b.data[n:])
  440. b.data = b.data[0:n]
  441. return b, bb
  442. }
  443. // readRecord reads the next TLS record from the connection
  444. // and updates the record layer state.
  445. // c.in.Mutex <= L; c.input == nil.
  446. func (c *Conn) readRecord(want recordType) error {
  447. // Caller must be in sync with connection:
  448. // handshake data if handshake not yet completed,
  449. // else application data. (We don't support renegotiation.)
  450. switch want {
  451. default:
  452. c.sendAlert(alertInternalError)
  453. return c.in.setErrorLocked(errors.New("tls: unknown record type requested"))
  454. case recordTypeHandshake, recordTypeChangeCipherSpec:
  455. if c.handshakeComplete {
  456. c.sendAlert(alertInternalError)
  457. return c.in.setErrorLocked(errors.New("tls: handshake or ChangeCipherSpec requested after handshake complete"))
  458. }
  459. case recordTypeApplicationData:
  460. if !c.handshakeComplete {
  461. c.sendAlert(alertInternalError)
  462. return c.in.setErrorLocked(errors.New("tls: application data record requested before handshake complete"))
  463. }
  464. }
  465. Again:
  466. if c.rawInput == nil {
  467. c.rawInput = c.in.newBlock()
  468. }
  469. b := c.rawInput
  470. // Read header, payload.
  471. if err := b.readFromUntil(c.conn, recordHeaderLen); err != nil {
  472. // RFC suggests that EOF without an alertCloseNotify is
  473. // an error, but popular web sites seem to do this,
  474. // so we can't make it an error.
  475. // if err == io.EOF {
  476. // err = io.ErrUnexpectedEOF
  477. // }
  478. if e, ok := err.(net.Error); !ok || !e.Temporary() {
  479. c.in.setErrorLocked(err)
  480. }
  481. return err
  482. }
  483. typ := recordType(b.data[0])
  484. // No valid TLS record has a type of 0x80, however SSLv2 handshakes
  485. // start with a uint16 length where the MSB is set and the first record
  486. // is always < 256 bytes long. Therefore typ == 0x80 strongly suggests
  487. // an SSLv2 client.
  488. if want == recordTypeHandshake && typ == 0x80 {
  489. c.sendAlert(alertProtocolVersion)
  490. return c.in.setErrorLocked(errors.New("tls: unsupported SSLv2 handshake received"))
  491. }
  492. vers := uint16(b.data[1])<<8 | uint16(b.data[2])
  493. n := int(b.data[3])<<8 | int(b.data[4])
  494. if c.haveVers && vers != c.vers {
  495. c.sendAlert(alertProtocolVersion)
  496. return c.in.setErrorLocked(fmt.Errorf("tls: received record with version %x when expecting version %x", vers, c.vers))
  497. }
  498. if n > maxCiphertext {
  499. c.sendAlert(alertRecordOverflow)
  500. return c.in.setErrorLocked(fmt.Errorf("tls: oversized record received with length %d", n))
  501. }
  502. if !c.haveVers {
  503. // First message, be extra suspicious: this might not be a TLS
  504. // client. Bail out before reading a full 'body', if possible.
  505. // The current max version is 3.3 so if the version is >= 16.0,
  506. // it's probably not real.
  507. if (typ != recordTypeAlert && typ != want) || vers >= 0x1000 {
  508. c.sendAlert(alertUnexpectedMessage)
  509. return c.in.setErrorLocked(fmt.Errorf("tls: first record does not look like a TLS handshake"))
  510. }
  511. }
  512. if err := b.readFromUntil(c.conn, recordHeaderLen+n); err != nil {
  513. if err == io.EOF {
  514. err = io.ErrUnexpectedEOF
  515. }
  516. if e, ok := err.(net.Error); !ok || !e.Temporary() {
  517. c.in.setErrorLocked(err)
  518. }
  519. return err
  520. }
  521. // Process message.
  522. b, c.rawInput = c.in.splitBlock(b, recordHeaderLen+n)
  523. ok, off, err := c.in.decrypt(b)
  524. if !ok {
  525. c.in.setErrorLocked(c.sendAlert(err))
  526. }
  527. b.off = off
  528. data := b.data[b.off:]
  529. if len(data) > maxPlaintext {
  530. err := c.sendAlert(alertRecordOverflow)
  531. c.in.freeBlock(b)
  532. return c.in.setErrorLocked(err)
  533. }
  534. switch typ {
  535. default:
  536. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  537. case recordTypeAlert:
  538. if len(data) != 2 {
  539. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  540. break
  541. }
  542. if alert(data[1]) == alertCloseNotify {
  543. c.in.setErrorLocked(io.EOF)
  544. break
  545. }
  546. switch data[0] {
  547. case alertLevelWarning:
  548. // drop on the floor
  549. c.in.freeBlock(b)
  550. goto Again
  551. case alertLevelError:
  552. c.in.setErrorLocked(&net.OpError{Op: "remote error", Err: alert(data[1])})
  553. default:
  554. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  555. }
  556. case recordTypeChangeCipherSpec:
  557. if typ != want || len(data) != 1 || data[0] != 1 {
  558. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  559. break
  560. }
  561. err := c.in.changeCipherSpec()
  562. if err != nil {
  563. c.in.setErrorLocked(c.sendAlert(err.(alert)))
  564. }
  565. case recordTypeApplicationData:
  566. if typ != want {
  567. c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  568. break
  569. }
  570. c.input = b
  571. b = nil
  572. case recordTypeHandshake:
  573. // TODO(rsc): Should at least pick off connection close.
  574. if typ != want {
  575. return c.in.setErrorLocked(c.sendAlert(alertNoRenegotiation))
  576. }
  577. c.hand.Write(data)
  578. }
  579. if b != nil {
  580. c.in.freeBlock(b)
  581. }
  582. return c.in.err
  583. }
  584. // sendAlert sends a TLS alert message.
  585. // c.out.Mutex <= L.
  586. func (c *Conn) sendAlertLocked(err alert) error {
  587. switch err {
  588. case alertNoRenegotiation, alertCloseNotify:
  589. c.tmp[0] = alertLevelWarning
  590. default:
  591. c.tmp[0] = alertLevelError
  592. }
  593. c.tmp[1] = byte(err)
  594. c.writeRecord(recordTypeAlert, c.tmp[0:2])
  595. // closeNotify is a special case in that it isn't an error:
  596. if err != alertCloseNotify {
  597. return c.out.setErrorLocked(&net.OpError{Op: "local error", Err: err})
  598. }
  599. return nil
  600. }
  601. // sendAlert sends a TLS alert message.
  602. // L < c.out.Mutex.
  603. func (c *Conn) sendAlert(err alert) error {
  604. c.out.Lock()
  605. defer c.out.Unlock()
  606. return c.sendAlertLocked(err)
  607. }
  608. // writeRecord writes a TLS record with the given type and payload
  609. // to the connection and updates the record layer state.
  610. // c.out.Mutex <= L.
  611. func (c *Conn) writeRecord(typ recordType, data []byte) (n int, err error) {
  612. b := c.out.newBlock()
  613. for len(data) > 0 {
  614. m := len(data)
  615. if m > maxPlaintext {
  616. m = maxPlaintext
  617. }
  618. explicitIVLen := 0
  619. explicitIVIsSeq := false
  620. var cbc cbcMode
  621. if c.out.version >= VersionTLS11 {
  622. var ok bool
  623. if cbc, ok = c.out.cipher.(cbcMode); ok {
  624. explicitIVLen = cbc.BlockSize()
  625. }
  626. }
  627. if explicitIVLen == 0 {
  628. if _, ok := c.out.cipher.(cipher.AEAD); ok {
  629. explicitIVLen = 8
  630. // The AES-GCM construction in TLS has an
  631. // explicit nonce so that the nonce can be
  632. // random. However, the nonce is only 8 bytes
  633. // which is too small for a secure, random
  634. // nonce. Therefore we use the sequence number
  635. // as the nonce.
  636. explicitIVIsSeq = true
  637. }
  638. }
  639. b.resize(recordHeaderLen + explicitIVLen + m)
  640. b.data[0] = byte(typ)
  641. vers := c.vers
  642. if vers == 0 {
  643. // Some TLS servers fail if the record version is
  644. // greater than TLS 1.0 for the initial ClientHello.
  645. vers = VersionTLS10
  646. }
  647. b.data[1] = byte(vers >> 8)
  648. b.data[2] = byte(vers)
  649. b.data[3] = byte(m >> 8)
  650. b.data[4] = byte(m)
  651. if explicitIVLen > 0 {
  652. explicitIV := b.data[recordHeaderLen : recordHeaderLen+explicitIVLen]
  653. if explicitIVIsSeq {
  654. copy(explicitIV, c.out.seq[:])
  655. } else {
  656. if _, err = io.ReadFull(c.config.rand(), explicitIV); err != nil {
  657. break
  658. }
  659. }
  660. }
  661. copy(b.data[recordHeaderLen+explicitIVLen:], data)
  662. c.out.encrypt(b, explicitIVLen)
  663. _, err = c.conn.Write(b.data)
  664. if err != nil {
  665. break
  666. }
  667. n += m
  668. data = data[m:]
  669. }
  670. c.out.freeBlock(b)
  671. if typ == recordTypeChangeCipherSpec {
  672. err = c.out.changeCipherSpec()
  673. if err != nil {
  674. // Cannot call sendAlert directly,
  675. // because we already hold c.out.Mutex.
  676. c.tmp[0] = alertLevelError
  677. c.tmp[1] = byte(err.(alert))
  678. c.writeRecord(recordTypeAlert, c.tmp[0:2])
  679. return n, c.out.setErrorLocked(&net.OpError{Op: "local error", Err: err})
  680. }
  681. }
  682. return
  683. }
  684. // readHandshake reads the next handshake message from
  685. // the record layer.
  686. // c.in.Mutex < L; c.out.Mutex < L.
  687. func (c *Conn) readHandshake() (interface{}, error) {
  688. for c.hand.Len() < 4 {
  689. if err := c.in.err; err != nil {
  690. return nil, err
  691. }
  692. if err := c.readRecord(recordTypeHandshake); err != nil {
  693. return nil, err
  694. }
  695. }
  696. data := c.hand.Bytes()
  697. n := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  698. if n > maxHandshake {
  699. return nil, c.in.setErrorLocked(c.sendAlert(alertInternalError))
  700. }
  701. for c.hand.Len() < 4+n {
  702. if err := c.in.err; err != nil {
  703. return nil, err
  704. }
  705. if err := c.readRecord(recordTypeHandshake); err != nil {
  706. return nil, err
  707. }
  708. }
  709. data = c.hand.Next(4 + n)
  710. var m handshakeMessage
  711. switch data[0] {
  712. case typeClientHello:
  713. m = new(clientHelloMsg)
  714. case typeServerHello:
  715. m = new(serverHelloMsg)
  716. case typeNewSessionTicket:
  717. m = new(newSessionTicketMsg)
  718. case typeCertificate:
  719. m = new(certificateMsg)
  720. case typeCertificateRequest:
  721. m = &certificateRequestMsg{
  722. hasSignatureAndHash: c.vers >= VersionTLS12,
  723. }
  724. case typeCertificateStatus:
  725. m = new(certificateStatusMsg)
  726. case typeServerKeyExchange:
  727. m = new(serverKeyExchangeMsg)
  728. case typeServerHelloDone:
  729. m = new(serverHelloDoneMsg)
  730. case typeClientKeyExchange:
  731. m = new(clientKeyExchangeMsg)
  732. case typeCertificateVerify:
  733. m = &certificateVerifyMsg{
  734. hasSignatureAndHash: c.vers >= VersionTLS12,
  735. }
  736. case typeNextProtocol:
  737. m = new(nextProtoMsg)
  738. case typeFinished:
  739. m = new(finishedMsg)
  740. default:
  741. return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  742. }
  743. // The handshake message unmarshallers
  744. // expect to be able to keep references to data,
  745. // so pass in a fresh copy that won't be overwritten.
  746. data = append([]byte(nil), data...)
  747. if !m.unmarshal(data) {
  748. return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage))
  749. }
  750. return m, nil
  751. }
  752. // Write writes data to the connection.
  753. func (c *Conn) Write(b []byte) (int, error) {
  754. if err := c.Handshake(); err != nil {
  755. return 0, err
  756. }
  757. c.out.Lock()
  758. defer c.out.Unlock()
  759. if err := c.out.err; err != nil {
  760. return 0, err
  761. }
  762. if !c.handshakeComplete {
  763. return 0, alertInternalError
  764. }
  765. // SSL 3.0 and TLS 1.0 are susceptible to a chosen-plaintext
  766. // attack when using block mode ciphers due to predictable IVs.
  767. // This can be prevented by splitting each Application Data
  768. // record into two records, effectively randomizing the IV.
  769. //
  770. // http://www.openssl.org/~bodo/tls-cbc.txt
  771. // https://bugzilla.mozilla.org/show_bug.cgi?id=665814
  772. // http://www.imperialviolet.org/2012/01/15/beastfollowup.html
  773. var m int
  774. if len(b) > 1 && c.vers <= VersionTLS10 {
  775. if _, ok := c.out.cipher.(cipher.BlockMode); ok {
  776. n, err := c.writeRecord(recordTypeApplicationData, b[:1])
  777. if err != nil {
  778. return n, c.out.setErrorLocked(err)
  779. }
  780. m, b = 1, b[1:]
  781. }
  782. }
  783. n, err := c.writeRecord(recordTypeApplicationData, b)
  784. return n + m, c.out.setErrorLocked(err)
  785. }
  786. // Read can be made to time out and return a net.Error with Timeout() == true
  787. // after a fixed time limit; see SetDeadline and SetReadDeadline.
  788. func (c *Conn) Read(b []byte) (n int, err error) {
  789. if err = c.Handshake(); err != nil {
  790. return
  791. }
  792. if len(b) == 0 {
  793. // Put this after Handshake, in case people were calling
  794. // Read(nil) for the side effect of the Handshake.
  795. return
  796. }
  797. c.in.Lock()
  798. defer c.in.Unlock()
  799. // Some OpenSSL servers send empty records in order to randomize the
  800. // CBC IV. So this loop ignores a limited number of empty records.
  801. const maxConsecutiveEmptyRecords = 100
  802. for emptyRecordCount := 0; emptyRecordCount <= maxConsecutiveEmptyRecords; emptyRecordCount++ {
  803. for c.input == nil && c.in.err == nil {
  804. if err := c.readRecord(recordTypeApplicationData); err != nil {
  805. // Soft error, like EAGAIN
  806. return 0, err
  807. }
  808. }
  809. if err := c.in.err; err != nil {
  810. return 0, err
  811. }
  812. n, err = c.input.Read(b)
  813. if c.input.off >= len(c.input.data) {
  814. c.in.freeBlock(c.input)
  815. c.input = nil
  816. }
  817. // If a close-notify alert is waiting, read it so that
  818. // we can return (n, EOF) instead of (n, nil), to signal
  819. // to the HTTP response reading goroutine that the
  820. // connection is now closed. This eliminates a race
  821. // where the HTTP response reading goroutine would
  822. // otherwise not observe the EOF until its next read,
  823. // by which time a client goroutine might have already
  824. // tried to reuse the HTTP connection for a new
  825. // request.
  826. // See https://codereview.appspot.com/76400046
  827. // and http://golang.org/issue/3514
  828. if ri := c.rawInput; ri != nil &&
  829. n != 0 && err == nil &&
  830. c.input == nil && len(ri.data) > 0 && recordType(ri.data[0]) == recordTypeAlert {
  831. if recErr := c.readRecord(recordTypeApplicationData); recErr != nil {
  832. err = recErr // will be io.EOF on closeNotify
  833. }
  834. }
  835. if n != 0 || err != nil {
  836. return n, err
  837. }
  838. }
  839. return 0, io.ErrNoProgress
  840. }
  841. // Close closes the connection.
  842. func (c *Conn) Close() error {
  843. var alertErr error
  844. c.handshakeMutex.Lock()
  845. defer c.handshakeMutex.Unlock()
  846. if c.handshakeComplete {
  847. alertErr = c.sendAlert(alertCloseNotify)
  848. }
  849. if err := c.conn.Close(); err != nil {
  850. return err
  851. }
  852. return alertErr
  853. }
  854. // Handshake runs the client or server handshake
  855. // protocol if it has not yet been run.
  856. // Most uses of this package need not call Handshake
  857. // explicitly: the first Read or Write will call it automatically.
  858. func (c *Conn) Handshake() error {
  859. c.handshakeMutex.Lock()
  860. defer c.handshakeMutex.Unlock()
  861. if err := c.handshakeErr; err != nil {
  862. return err
  863. }
  864. if c.handshakeComplete {
  865. return nil
  866. }
  867. if c.isClient {
  868. c.handshakeErr = c.clientHandshake()
  869. } else {
  870. c.handshakeErr = c.serverHandshake()
  871. }
  872. return c.handshakeErr
  873. }
  874. // ConnectionState returns basic TLS details about the connection.
  875. func (c *Conn) ConnectionState() ConnectionState {
  876. c.handshakeMutex.Lock()
  877. defer c.handshakeMutex.Unlock()
  878. var state ConnectionState
  879. state.HandshakeComplete = c.handshakeComplete
  880. if c.handshakeComplete {
  881. state.Version = c.vers
  882. state.NegotiatedProtocol = c.clientProtocol
  883. state.DidResume = c.didResume
  884. state.NegotiatedProtocolIsMutual = !c.clientProtocolFallback
  885. state.CipherSuite = c.cipherSuite
  886. state.PeerCertificates = c.peerCertificates
  887. state.VerifiedChains = c.verifiedChains
  888. state.ServerName = c.serverName
  889. state.SignedCertificateTimestamps = c.scts
  890. state.OCSPResponse = c.ocspResponse
  891. if !c.didResume {
  892. state.TLSUnique = c.firstFinished[:]
  893. }
  894. }
  895. return state
  896. }
  897. // OCSPResponse returns the stapled OCSP response from the TLS server, if
  898. // any. (Only valid for client connections.)
  899. func (c *Conn) OCSPResponse() []byte {
  900. c.handshakeMutex.Lock()
  901. defer c.handshakeMutex.Unlock()
  902. return c.ocspResponse
  903. }
  904. // VerifyHostname checks that the peer certificate chain is valid for
  905. // connecting to host. If so, it returns nil; if not, it returns an error
  906. // describing the problem.
  907. func (c *Conn) VerifyHostname(host string) error {
  908. c.handshakeMutex.Lock()
  909. defer c.handshakeMutex.Unlock()
  910. if !c.isClient {
  911. return errors.New("tls: VerifyHostname called on TLS server connection")
  912. }
  913. if !c.handshakeComplete {
  914. return errors.New("tls: handshake has not yet been performed")
  915. }
  916. return c.peerCertificates[0].VerifyHostname(host)
  917. }