...
Run Format

Source file src/crypto/tls/conn.go

Documentation: crypto/tls

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

View as plain text