2010-12-15 16:49:55 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
// TLS low level connection and record layer
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2010-12-15 16:49:55 +00:00
|
|
|
"crypto/cipher"
|
2010-04-27 06:19:04 +01:00
|
|
|
"crypto/subtle"
|
2010-07-21 16:36:01 +01:00
|
|
|
"crypto/x509"
|
2011-11-02 02:04:37 +00:00
|
|
|
"errors"
|
2010-04-27 06:19:04 +01:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2012-01-19 00:24:06 +00:00
|
|
|
"time"
|
2010-04-27 06:19:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Conn represents a secured connection.
|
|
|
|
// It implements the net.Conn interface.
|
|
|
|
type Conn struct {
|
|
|
|
// constant
|
|
|
|
conn net.Conn
|
|
|
|
isClient bool
|
|
|
|
|
|
|
|
// constant after handshake; protected by handshakeMutex
|
|
|
|
handshakeMutex sync.Mutex // handshakeMutex < in.Mutex, out.Mutex, errMutex
|
|
|
|
vers uint16 // TLS version
|
|
|
|
haveVers bool // version has been negotiated
|
|
|
|
config *Config // configuration passed to constructor
|
|
|
|
handshakeComplete bool
|
2012-09-24 21:52:43 +01:00
|
|
|
didResume bool // whether this connection was a session resumption
|
2010-04-27 06:19:04 +01:00
|
|
|
cipherSuite uint16
|
2010-07-14 15:40:15 +01:00
|
|
|
ocspResponse []byte // stapled OCSP response
|
2010-07-21 16:36:01 +01:00
|
|
|
peerCertificates []*x509.Certificate
|
2011-05-18 18:14:56 +01:00
|
|
|
// verifiedChains contains the certificate chains that we built, as
|
2011-04-19 14:57:58 +01:00
|
|
|
// opposed to the ones presented by the server.
|
|
|
|
verifiedChains [][]*x509.Certificate
|
2011-10-08 15:11:38 +01:00
|
|
|
// serverName contains the server name indicated by the client, if any.
|
|
|
|
serverName string
|
2010-04-27 06:19:04 +01:00
|
|
|
|
2011-03-29 22:53:09 +01:00
|
|
|
clientProtocol string
|
|
|
|
clientProtocolFallback bool
|
2010-04-27 06:19:04 +01:00
|
|
|
|
|
|
|
// first permanent error
|
2012-09-06 08:50:26 +01:00
|
|
|
connErr
|
2010-04-27 06:19:04 +01:00
|
|
|
|
|
|
|
// input/output
|
|
|
|
in, out halfConn // in.Mutex < out.Mutex
|
|
|
|
rawInput *block // raw input, right off the wire
|
|
|
|
input *block // application data waiting to be read
|
|
|
|
hand bytes.Buffer // handshake data waiting to be read
|
|
|
|
|
|
|
|
tmp [16]byte
|
|
|
|
}
|
|
|
|
|
2012-09-06 08:50:26 +01:00
|
|
|
type connErr struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
value error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *connErr) setError(err error) error {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
2010-04-27 06:19:04 +01:00
|
|
|
|
2012-09-06 08:50:26 +01:00
|
|
|
if e.value == nil {
|
|
|
|
e.value = err
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-09-06 08:50:26 +01:00
|
|
|
func (e *connErr) error() error {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
return e.value
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Access to net.Conn methods.
|
|
|
|
// Cannot just embed net.Conn because that would
|
|
|
|
// export the struct field too.
|
|
|
|
|
|
|
|
// LocalAddr returns the local network address.
|
|
|
|
func (c *Conn) LocalAddr() net.Addr {
|
|
|
|
return c.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address.
|
|
|
|
func (c *Conn) RemoteAddr() net.Addr {
|
|
|
|
return c.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
2012-02-13 17:38:45 +00:00
|
|
|
// SetDeadline sets the read and write deadlines associated with the connection.
|
|
|
|
// A zero value for t means Read and Write will not time out.
|
|
|
|
// After a Write has timed out, the TLS state is corrupt and all future writes will return the same error.
|
2012-01-19 00:24:06 +00:00
|
|
|
func (c *Conn) SetDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetDeadline(t)
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 00:24:06 +00:00
|
|
|
// SetReadDeadline sets the read deadline on the underlying connection.
|
|
|
|
// A zero value for t means Read will not time out.
|
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetReadDeadline(t)
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 17:38:45 +00:00
|
|
|
// SetWriteDeadline sets the write deadline on the underlying conneciton.
|
|
|
|
// A zero value for t means Write will not time out.
|
|
|
|
// After a Write has timed out, the TLS state is corrupt and all future writes will return the same error.
|
2012-01-19 00:24:06 +00:00
|
|
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
2012-02-13 17:38:45 +00:00
|
|
|
return c.conn.SetWriteDeadline(t)
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A halfConn represents one direction of the record layer
|
|
|
|
// connection, either sending or receiving.
|
|
|
|
type halfConn struct {
|
|
|
|
sync.Mutex
|
2011-09-14 20:32:19 +01:00
|
|
|
version uint16 // protocol version
|
|
|
|
cipher interface{} // cipher algorithm
|
|
|
|
mac macFunction
|
|
|
|
seq [8]byte // 64-bit sequence number
|
|
|
|
bfree *block // list of free blocks
|
2010-04-27 06:19:04 +01:00
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
nextCipher interface{} // next encryption state
|
2011-09-14 20:32:19 +01:00
|
|
|
nextMac macFunction // next MAC algorithm
|
2011-12-06 23:25:14 +00:00
|
|
|
|
|
|
|
// used to save allocating a new buffer for each MAC.
|
|
|
|
inDigestBuf, outDigestBuf []byte
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepareCipherSpec sets the encryption and MAC states
|
|
|
|
// that a subsequent changeCipherSpec will use.
|
2011-09-14 20:32:19 +01:00
|
|
|
func (hc *halfConn) prepareCipherSpec(version uint16, cipher interface{}, mac macFunction) {
|
|
|
|
hc.version = version
|
2010-12-15 16:49:55 +00:00
|
|
|
hc.nextCipher = cipher
|
2010-04-27 06:19:04 +01:00
|
|
|
hc.nextMac = mac
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeCipherSpec changes the encryption and MAC states
|
|
|
|
// to the ones previously passed to prepareCipherSpec.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (hc *halfConn) changeCipherSpec() error {
|
2010-12-15 16:49:55 +00:00
|
|
|
if hc.nextCipher == nil {
|
2010-04-27 06:19:04 +01:00
|
|
|
return alertInternalError
|
|
|
|
}
|
2010-12-15 16:49:55 +00:00
|
|
|
hc.cipher = hc.nextCipher
|
2010-04-27 06:19:04 +01:00
|
|
|
hc.mac = hc.nextMac
|
2010-12-15 16:49:55 +00:00
|
|
|
hc.nextCipher = nil
|
2010-04-27 06:19:04 +01:00
|
|
|
hc.nextMac = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// incSeq increments the sequence number.
|
|
|
|
func (hc *halfConn) incSeq() {
|
|
|
|
for i := 7; i >= 0; i-- {
|
|
|
|
hc.seq[i]++
|
|
|
|
if hc.seq[i] != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not allowed to let sequence number wrap.
|
|
|
|
// Instead, must renegotiate before it does.
|
|
|
|
// Not likely enough to bother.
|
|
|
|
panic("TLS: sequence number wraparound")
|
|
|
|
}
|
|
|
|
|
|
|
|
// resetSeq resets the sequence number to zero.
|
|
|
|
func (hc *halfConn) resetSeq() {
|
|
|
|
for i := range hc.seq {
|
|
|
|
hc.seq[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
// removePadding returns an unpadded slice, in constant time, which is a prefix
|
|
|
|
// of the input. It also returns a byte which is equal to 255 if the padding
|
|
|
|
// was valid and 0 otherwise. See RFC 2246, section 6.2.3.2
|
|
|
|
func removePadding(payload []byte) ([]byte, byte) {
|
|
|
|
if len(payload) < 1 {
|
|
|
|
return payload, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
paddingLen := payload[len(payload)-1]
|
|
|
|
t := uint(len(payload)-1) - uint(paddingLen)
|
|
|
|
// if len(payload) >= (paddingLen - 1) then the MSB of t is zero
|
|
|
|
good := byte(int32(^t) >> 31)
|
|
|
|
|
|
|
|
toCheck := 255 // the maximum possible padding length
|
|
|
|
// The length of the padded data is public, so we can use an if here
|
|
|
|
if toCheck+1 > len(payload) {
|
|
|
|
toCheck = len(payload) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < toCheck; i++ {
|
|
|
|
t := uint(paddingLen) - uint(i)
|
|
|
|
// if i <= paddingLen then the MSB of t is zero
|
|
|
|
mask := byte(int32(^t) >> 31)
|
|
|
|
b := payload[len(payload)-1-i]
|
|
|
|
good &^= mask&paddingLen ^ mask&b
|
|
|
|
}
|
|
|
|
|
|
|
|
// We AND together the bits of good and replicate the result across
|
|
|
|
// all the bits.
|
|
|
|
good &= good << 4
|
|
|
|
good &= good << 2
|
|
|
|
good &= good << 1
|
|
|
|
good = uint8(int8(good) >> 7)
|
|
|
|
|
|
|
|
toRemove := good&paddingLen + 1
|
|
|
|
return payload[:len(payload)-int(toRemove)], good
|
|
|
|
}
|
|
|
|
|
2011-09-14 20:32:19 +01:00
|
|
|
// removePaddingSSL30 is a replacement for removePadding in the case that the
|
|
|
|
// protocol version is SSLv3. In this version, the contents of the padding
|
|
|
|
// are random and cannot be checked.
|
|
|
|
func removePaddingSSL30(payload []byte) ([]byte, byte) {
|
|
|
|
if len(payload) < 1 {
|
|
|
|
return payload, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
paddingLen := int(payload[len(payload)-1]) + 1
|
|
|
|
if paddingLen > len(payload) {
|
|
|
|
return payload, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload[:len(payload)-paddingLen], 255
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
func roundUp(a, b int) int {
|
|
|
|
return a + (b-a%b)%b
|
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
// decrypt checks and strips the mac and decrypts the data in b.
|
|
|
|
func (hc *halfConn) decrypt(b *block) (bool, alert) {
|
|
|
|
// pull out payload
|
|
|
|
payload := b.data[recordHeaderLen:]
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
macSize := 0
|
|
|
|
if hc.mac != nil {
|
|
|
|
macSize = hc.mac.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
paddingGood := byte(255)
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
// decrypt
|
2010-12-15 16:49:55 +00:00
|
|
|
if hc.cipher != nil {
|
|
|
|
switch c := hc.cipher.(type) {
|
|
|
|
case cipher.Stream:
|
|
|
|
c.XORKeyStream(payload, payload)
|
|
|
|
case cipher.BlockMode:
|
|
|
|
blockSize := c.BlockSize()
|
|
|
|
|
|
|
|
if len(payload)%blockSize != 0 || len(payload) < roundUp(macSize+1, blockSize) {
|
|
|
|
return false, alertBadRecordMAC
|
|
|
|
}
|
|
|
|
|
|
|
|
c.CryptBlocks(payload, payload)
|
2011-09-14 20:32:19 +01:00
|
|
|
if hc.version == versionSSL30 {
|
|
|
|
payload, paddingGood = removePaddingSSL30(payload)
|
|
|
|
} else {
|
|
|
|
payload, paddingGood = removePadding(payload)
|
|
|
|
}
|
2010-12-15 16:49:55 +00:00
|
|
|
b.resize(recordHeaderLen + len(payload))
|
|
|
|
|
|
|
|
// note that we still have a timing side-channel in the
|
|
|
|
// MAC check, below. An attacker can align the record
|
|
|
|
// so that a correct padding will cause one less hash
|
|
|
|
// block to be calculated. Then they can iteratively
|
|
|
|
// decrypt a record by breaking each byte. See
|
|
|
|
// "Password Interception in a SSL/TLS Channel", Brice
|
|
|
|
// Canvel et al.
|
|
|
|
//
|
2011-05-18 18:14:56 +01:00
|
|
|
// However, our behavior matches OpenSSL, so we leak
|
2010-12-15 16:49:55 +00:00
|
|
|
// only as much as they do.
|
|
|
|
default:
|
|
|
|
panic("unknown cipher type")
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check, strip mac
|
|
|
|
if hc.mac != nil {
|
2010-12-15 16:49:55 +00:00
|
|
|
if len(payload) < macSize {
|
2010-04-27 06:19:04 +01:00
|
|
|
return false, alertBadRecordMAC
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip mac off payload, b.data
|
2010-12-15 16:49:55 +00:00
|
|
|
n := len(payload) - macSize
|
2010-04-27 06:19:04 +01:00
|
|
|
b.data[3] = byte(n >> 8)
|
|
|
|
b.data[4] = byte(n)
|
2010-12-15 16:49:55 +00:00
|
|
|
b.resize(recordHeaderLen + n)
|
2010-04-27 06:19:04 +01:00
|
|
|
remoteMAC := payload[n:]
|
2011-12-06 23:25:14 +00:00
|
|
|
localMAC := hc.mac.MAC(hc.inDigestBuf, hc.seq[0:], b.data)
|
2010-04-27 06:19:04 +01:00
|
|
|
hc.incSeq()
|
|
|
|
|
2011-09-14 20:32:19 +01:00
|
|
|
if subtle.ConstantTimeCompare(localMAC, remoteMAC) != 1 || paddingGood != 255 {
|
2010-04-27 06:19:04 +01:00
|
|
|
return false, alertBadRecordMAC
|
|
|
|
}
|
2011-12-06 23:25:14 +00:00
|
|
|
hc.inDigestBuf = localMAC
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, 0
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
// padToBlockSize calculates the needed padding block, if any, for a payload.
|
|
|
|
// On exit, prefix aliases payload and extends to the end of the last full
|
|
|
|
// block of payload. finalBlock is a fresh slice which contains the contents of
|
|
|
|
// any suffix of payload as well as the needed padding to make finalBlock a
|
|
|
|
// full block.
|
|
|
|
func padToBlockSize(payload []byte, blockSize int) (prefix, finalBlock []byte) {
|
|
|
|
overrun := len(payload) % blockSize
|
|
|
|
paddingLen := blockSize - overrun
|
|
|
|
prefix = payload[:len(payload)-overrun]
|
|
|
|
finalBlock = make([]byte, blockSize)
|
|
|
|
copy(finalBlock, payload[len(payload)-overrun:])
|
|
|
|
for i := overrun; i < blockSize; i++ {
|
|
|
|
finalBlock[i] = byte(paddingLen - 1)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
// encrypt encrypts and macs the data in b.
|
|
|
|
func (hc *halfConn) encrypt(b *block) (bool, alert) {
|
|
|
|
// mac
|
|
|
|
if hc.mac != nil {
|
2011-12-06 23:25:14 +00:00
|
|
|
mac := hc.mac.MAC(hc.outDigestBuf, hc.seq[0:], b.data)
|
2010-04-27 06:19:04 +01:00
|
|
|
hc.incSeq()
|
2011-09-14 20:32:19 +01:00
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
n := len(b.data)
|
|
|
|
b.resize(n + len(mac))
|
|
|
|
copy(b.data[n:], mac)
|
2011-12-06 23:25:14 +00:00
|
|
|
hc.outDigestBuf = mac
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
payload := b.data[recordHeaderLen:]
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
// encrypt
|
2010-12-15 16:49:55 +00:00
|
|
|
if hc.cipher != nil {
|
|
|
|
switch c := hc.cipher.(type) {
|
|
|
|
case cipher.Stream:
|
|
|
|
c.XORKeyStream(payload, payload)
|
|
|
|
case cipher.BlockMode:
|
|
|
|
prefix, finalBlock := padToBlockSize(payload, c.BlockSize())
|
|
|
|
b.resize(recordHeaderLen + len(prefix) + len(finalBlock))
|
|
|
|
c.CryptBlocks(b.data[recordHeaderLen:], prefix)
|
|
|
|
c.CryptBlocks(b.data[recordHeaderLen+len(prefix):], finalBlock)
|
|
|
|
default:
|
|
|
|
panic("unknown cipher type")
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
// update length to include MAC and any block padding needed.
|
|
|
|
n := len(b.data) - recordHeaderLen
|
|
|
|
b.data[3] = byte(n >> 8)
|
|
|
|
b.data[4] = byte(n)
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
return true, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// A block is a simple data buffer.
|
|
|
|
type block struct {
|
|
|
|
data []byte
|
|
|
|
off int // index for Read
|
|
|
|
link *block
|
|
|
|
}
|
|
|
|
|
|
|
|
// resize resizes block to be n bytes, growing if necessary.
|
|
|
|
func (b *block) resize(n int) {
|
|
|
|
if n > cap(b.data) {
|
|
|
|
b.reserve(n)
|
|
|
|
}
|
|
|
|
b.data = b.data[0:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
// reserve makes sure that block contains a capacity of at least n bytes.
|
|
|
|
func (b *block) reserve(n int) {
|
|
|
|
if cap(b.data) >= n {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := cap(b.data)
|
|
|
|
if m == 0 {
|
|
|
|
m = 1024
|
|
|
|
}
|
|
|
|
for m < n {
|
|
|
|
m *= 2
|
|
|
|
}
|
|
|
|
data := make([]byte, len(b.data), m)
|
|
|
|
copy(data, b.data)
|
|
|
|
b.data = data
|
|
|
|
}
|
|
|
|
|
|
|
|
// readFromUntil reads from r into b until b contains at least n bytes
|
|
|
|
// or else returns an error.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (b *block) readFromUntil(r io.Reader, n int) error {
|
2010-04-27 06:19:04 +01:00
|
|
|
// quick case
|
|
|
|
if len(b.data) >= n {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// read until have enough.
|
|
|
|
b.reserve(n)
|
|
|
|
for {
|
|
|
|
m, err := r.Read(b.data[len(b.data):cap(b.data)])
|
|
|
|
b.data = b.data[0 : len(b.data)+m]
|
|
|
|
if len(b.data) >= n {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (b *block) Read(p []byte) (n int, err error) {
|
2010-04-27 06:19:04 +01:00
|
|
|
n = copy(p, b.data[b.off:])
|
|
|
|
b.off += n
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBlock allocates a new block, from hc's free list if possible.
|
|
|
|
func (hc *halfConn) newBlock() *block {
|
|
|
|
b := hc.bfree
|
|
|
|
if b == nil {
|
|
|
|
return new(block)
|
|
|
|
}
|
|
|
|
hc.bfree = b.link
|
|
|
|
b.link = nil
|
|
|
|
b.resize(0)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// freeBlock returns a block to hc's free list.
|
|
|
|
// The protocol is such that each side only has a block or two on
|
|
|
|
// its free list at a time, so there's no need to worry about
|
|
|
|
// trimming the list, etc.
|
|
|
|
func (hc *halfConn) freeBlock(b *block) {
|
|
|
|
b.link = hc.bfree
|
|
|
|
hc.bfree = b
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitBlock splits a block after the first n bytes,
|
|
|
|
// returning a block with those n bytes and a
|
2011-05-18 18:14:56 +01:00
|
|
|
// block with the remainder. the latter may be nil.
|
2010-04-27 06:19:04 +01:00
|
|
|
func (hc *halfConn) splitBlock(b *block, n int) (*block, *block) {
|
|
|
|
if len(b.data) <= n {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
bb := hc.newBlock()
|
|
|
|
bb.resize(len(b.data) - n)
|
|
|
|
copy(bb.data, b.data[n:])
|
|
|
|
b.data = b.data[0:n]
|
|
|
|
return b, bb
|
|
|
|
}
|
|
|
|
|
|
|
|
// readRecord reads the next TLS record from the connection
|
|
|
|
// and updates the record layer state.
|
|
|
|
// c.in.Mutex <= L; c.input == nil.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) readRecord(want recordType) error {
|
2010-04-27 06:19:04 +01:00
|
|
|
// Caller must be in sync with connection:
|
|
|
|
// handshake data if handshake not yet completed,
|
|
|
|
// else application data. (We don't support renegotiation.)
|
|
|
|
switch want {
|
|
|
|
default:
|
|
|
|
return c.sendAlert(alertInternalError)
|
|
|
|
case recordTypeHandshake, recordTypeChangeCipherSpec:
|
|
|
|
if c.handshakeComplete {
|
|
|
|
return c.sendAlert(alertInternalError)
|
|
|
|
}
|
|
|
|
case recordTypeApplicationData:
|
|
|
|
if !c.handshakeComplete {
|
|
|
|
return c.sendAlert(alertInternalError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Again:
|
|
|
|
if c.rawInput == nil {
|
|
|
|
c.rawInput = c.in.newBlock()
|
|
|
|
}
|
|
|
|
b := c.rawInput
|
|
|
|
|
|
|
|
// Read header, payload.
|
|
|
|
if err := b.readFromUntil(c.conn, recordHeaderLen); err != nil {
|
|
|
|
// RFC suggests that EOF without an alertCloseNotify is
|
|
|
|
// an error, but popular web sites seem to do this,
|
|
|
|
// so we can't make it an error.
|
2011-11-03 21:01:30 +00:00
|
|
|
// if err == io.EOF {
|
2010-04-27 06:19:04 +01:00
|
|
|
// err = io.ErrUnexpectedEOF
|
|
|
|
// }
|
|
|
|
if e, ok := err.(net.Error); !ok || !e.Temporary() {
|
|
|
|
c.setError(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
typ := recordType(b.data[0])
|
2012-08-23 21:44:44 +01:00
|
|
|
|
|
|
|
// No valid TLS record has a type of 0x80, however SSLv2 handshakes
|
|
|
|
// start with a uint16 length where the MSB is set and the first record
|
|
|
|
// is always < 256 bytes long. Therefore typ == 0x80 strongly suggests
|
|
|
|
// an SSLv2 client.
|
|
|
|
if want == recordTypeHandshake && typ == 0x80 {
|
|
|
|
c.sendAlert(alertProtocolVersion)
|
|
|
|
return errors.New("tls: unsupported SSLv2 handshake received")
|
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
vers := uint16(b.data[1])<<8 | uint16(b.data[2])
|
|
|
|
n := int(b.data[3])<<8 | int(b.data[4])
|
|
|
|
if c.haveVers && vers != c.vers {
|
|
|
|
return c.sendAlert(alertProtocolVersion)
|
|
|
|
}
|
|
|
|
if n > maxCiphertext {
|
|
|
|
return c.sendAlert(alertRecordOverflow)
|
|
|
|
}
|
2011-09-12 21:52:49 +01:00
|
|
|
if !c.haveVers {
|
|
|
|
// First message, be extra suspicious:
|
|
|
|
// this might not be a TLS client.
|
|
|
|
// Bail out before reading a full 'body', if possible.
|
|
|
|
// The current max version is 3.1.
|
|
|
|
// If the version is >= 16.0, it's probably not real.
|
|
|
|
// Similarly, a clientHello message encodes in
|
|
|
|
// well under a kilobyte. If the length is >= 12 kB,
|
|
|
|
// it's probably not real.
|
|
|
|
if (typ != recordTypeAlert && typ != want) || vers >= 0x1000 || n >= 0x3000 {
|
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
|
|
|
}
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
if err := b.readFromUntil(c.conn, recordHeaderLen+n); err != nil {
|
2011-11-02 02:04:37 +00:00
|
|
|
if err == io.EOF {
|
2010-04-27 06:19:04 +01:00
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
if e, ok := err.(net.Error); !ok || !e.Temporary() {
|
|
|
|
c.setError(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process message.
|
|
|
|
b, c.rawInput = c.in.splitBlock(b, recordHeaderLen+n)
|
|
|
|
b.off = recordHeaderLen
|
|
|
|
if ok, err := c.in.decrypt(b); !ok {
|
|
|
|
return c.sendAlert(err)
|
|
|
|
}
|
|
|
|
data := b.data[b.off:]
|
|
|
|
if len(data) > maxPlaintext {
|
|
|
|
c.sendAlert(alertRecordOverflow)
|
|
|
|
c.in.freeBlock(b)
|
|
|
|
return c.error()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
default:
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
|
|
|
|
case recordTypeAlert:
|
|
|
|
if len(data) != 2 {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if alert(data[1]) == alertCloseNotify {
|
2011-11-02 02:04:37 +00:00
|
|
|
c.setError(io.EOF)
|
2010-04-27 06:19:04 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
switch data[0] {
|
|
|
|
case alertLevelWarning:
|
|
|
|
// drop on the floor
|
|
|
|
c.in.freeBlock(b)
|
|
|
|
goto Again
|
|
|
|
case alertLevelError:
|
2011-11-02 02:04:37 +00:00
|
|
|
c.setError(&net.OpError{Op: "remote error", Err: alert(data[1])})
|
2010-04-27 06:19:04 +01:00
|
|
|
default:
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
case recordTypeChangeCipherSpec:
|
|
|
|
if typ != want || len(data) != 1 || data[0] != 1 {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err := c.in.changeCipherSpec()
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(err.(alert))
|
|
|
|
}
|
|
|
|
|
|
|
|
case recordTypeApplicationData:
|
|
|
|
if typ != want {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.input = b
|
|
|
|
b = nil
|
|
|
|
|
|
|
|
case recordTypeHandshake:
|
|
|
|
// TODO(rsc): Should at least pick off connection close.
|
|
|
|
if typ != want {
|
|
|
|
return c.sendAlert(alertNoRenegotiation)
|
|
|
|
}
|
|
|
|
c.hand.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b != nil {
|
|
|
|
c.in.freeBlock(b)
|
|
|
|
}
|
|
|
|
return c.error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendAlert sends a TLS alert message.
|
|
|
|
// c.out.Mutex <= L.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) sendAlertLocked(err alert) error {
|
2010-04-27 06:19:04 +01:00
|
|
|
c.tmp[0] = alertLevelError
|
|
|
|
if err == alertNoRenegotiation {
|
|
|
|
c.tmp[0] = alertLevelWarning
|
|
|
|
}
|
|
|
|
c.tmp[1] = byte(err)
|
|
|
|
c.writeRecord(recordTypeAlert, c.tmp[0:2])
|
2010-09-10 20:55:35 +01:00
|
|
|
// closeNotify is a special case in that it isn't an error:
|
|
|
|
if err != alertCloseNotify {
|
2011-11-02 02:04:37 +00:00
|
|
|
return c.setError(&net.OpError{Op: "local error", Err: err})
|
2010-09-10 20:55:35 +01:00
|
|
|
}
|
|
|
|
return nil
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendAlert sends a TLS alert message.
|
|
|
|
// L < c.out.Mutex.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) sendAlert(err alert) error {
|
2010-04-27 06:19:04 +01:00
|
|
|
c.out.Lock()
|
|
|
|
defer c.out.Unlock()
|
|
|
|
return c.sendAlertLocked(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeRecord writes a TLS record with the given type and payload
|
|
|
|
// to the connection and updates the record layer state.
|
|
|
|
// c.out.Mutex <= L.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) writeRecord(typ recordType, data []byte) (n int, err error) {
|
2010-04-27 06:19:04 +01:00
|
|
|
b := c.out.newBlock()
|
|
|
|
for len(data) > 0 {
|
|
|
|
m := len(data)
|
|
|
|
if m > maxPlaintext {
|
|
|
|
m = maxPlaintext
|
|
|
|
}
|
|
|
|
b.resize(recordHeaderLen + m)
|
|
|
|
b.data[0] = byte(typ)
|
|
|
|
vers := c.vers
|
|
|
|
if vers == 0 {
|
|
|
|
vers = maxVersion
|
|
|
|
}
|
|
|
|
b.data[1] = byte(vers >> 8)
|
|
|
|
b.data[2] = byte(vers)
|
|
|
|
b.data[3] = byte(m >> 8)
|
|
|
|
b.data[4] = byte(m)
|
|
|
|
copy(b.data[recordHeaderLen:], data)
|
|
|
|
c.out.encrypt(b)
|
|
|
|
_, err = c.conn.Write(b.data)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n += m
|
|
|
|
data = data[m:]
|
|
|
|
}
|
|
|
|
c.out.freeBlock(b)
|
|
|
|
|
|
|
|
if typ == recordTypeChangeCipherSpec {
|
|
|
|
err = c.out.changeCipherSpec()
|
|
|
|
if err != nil {
|
|
|
|
// Cannot call sendAlert directly,
|
|
|
|
// because we already hold c.out.Mutex.
|
|
|
|
c.tmp[0] = alertLevelError
|
|
|
|
c.tmp[1] = byte(err.(alert))
|
|
|
|
c.writeRecord(recordTypeAlert, c.tmp[0:2])
|
2012-09-06 08:50:26 +01:00
|
|
|
return n, c.setError(&net.OpError{Op: "local error", Err: err})
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// readHandshake reads the next handshake message from
|
|
|
|
// the record layer.
|
|
|
|
// c.in.Mutex < L; c.out.Mutex < L.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) readHandshake() (interface{}, error) {
|
2010-04-27 06:19:04 +01:00
|
|
|
for c.hand.Len() < 4 {
|
2012-09-06 08:50:26 +01:00
|
|
|
if err := c.error(); err != nil {
|
|
|
|
return nil, err
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
2011-10-18 17:59:32 +01:00
|
|
|
if err := c.readRecord(recordTypeHandshake); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data := c.hand.Bytes()
|
|
|
|
n := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
|
|
|
|
if n > maxHandshake {
|
|
|
|
c.sendAlert(alertInternalError)
|
2012-09-06 08:50:26 +01:00
|
|
|
return nil, c.error()
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
for c.hand.Len() < 4+n {
|
2012-09-06 08:50:26 +01:00
|
|
|
if err := c.error(); err != nil {
|
|
|
|
return nil, err
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
2011-10-18 17:59:32 +01:00
|
|
|
if err := c.readRecord(recordTypeHandshake); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
data = c.hand.Next(4 + n)
|
|
|
|
var m handshakeMessage
|
|
|
|
switch data[0] {
|
|
|
|
case typeClientHello:
|
|
|
|
m = new(clientHelloMsg)
|
|
|
|
case typeServerHello:
|
|
|
|
m = new(serverHelloMsg)
|
|
|
|
case typeCertificate:
|
|
|
|
m = new(certificateMsg)
|
2010-08-16 16:22:22 +01:00
|
|
|
case typeCertificateRequest:
|
|
|
|
m = new(certificateRequestMsg)
|
2010-07-14 15:40:15 +01:00
|
|
|
case typeCertificateStatus:
|
|
|
|
m = new(certificateStatusMsg)
|
2010-12-16 22:10:50 +00:00
|
|
|
case typeServerKeyExchange:
|
|
|
|
m = new(serverKeyExchangeMsg)
|
2010-04-27 06:19:04 +01:00
|
|
|
case typeServerHelloDone:
|
|
|
|
m = new(serverHelloDoneMsg)
|
|
|
|
case typeClientKeyExchange:
|
|
|
|
m = new(clientKeyExchangeMsg)
|
2010-08-16 16:22:22 +01:00
|
|
|
case typeCertificateVerify:
|
|
|
|
m = new(certificateVerifyMsg)
|
2010-04-27 06:19:04 +01:00
|
|
|
case typeNextProtocol:
|
|
|
|
m = new(nextProtoMsg)
|
|
|
|
case typeFinished:
|
|
|
|
m = new(finishedMsg)
|
|
|
|
default:
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return nil, alertUnexpectedMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// The handshake message unmarshallers
|
|
|
|
// expect to be able to keep references to data,
|
|
|
|
// so pass in a fresh copy that won't be overwritten.
|
2010-12-01 19:59:13 +00:00
|
|
|
data = append([]byte(nil), data...)
|
2010-04-27 06:19:04 +01:00
|
|
|
|
|
|
|
if !m.unmarshal(data) {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return nil, alertUnexpectedMessage
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes data to the connection.
|
2012-02-13 17:38:45 +00:00
|
|
|
func (c *Conn) Write(b []byte) (int, error) {
|
2012-09-06 08:50:26 +01:00
|
|
|
if err := c.error(); err != nil {
|
|
|
|
return 0, err
|
2012-02-13 17:38:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 08:50:26 +01:00
|
|
|
if err := c.Handshake(); err != nil {
|
|
|
|
return 0, c.setError(err)
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c.out.Lock()
|
|
|
|
defer c.out.Unlock()
|
|
|
|
|
|
|
|
if !c.handshakeComplete {
|
|
|
|
return 0, alertInternalError
|
|
|
|
}
|
2012-02-13 17:38:45 +00:00
|
|
|
|
2012-09-06 08:50:26 +01:00
|
|
|
n, err := c.writeRecord(recordTypeApplicationData, b)
|
|
|
|
return n, c.setError(err)
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
2011-11-14 03:42:42 +00:00
|
|
|
// Read can be made to time out and return a net.Error with Timeout() == true
|
2012-01-19 00:24:06 +00:00
|
|
|
// after a fixed time limit; see SetDeadline and SetReadDeadline.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) Read(b []byte) (n int, err error) {
|
2010-04-27 06:19:04 +01:00
|
|
|
if err = c.Handshake(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.in.Lock()
|
|
|
|
defer c.in.Unlock()
|
|
|
|
|
2012-09-06 08:50:26 +01:00
|
|
|
for c.input == nil && c.error() == nil {
|
2010-10-11 15:41:01 +01:00
|
|
|
if err := c.readRecord(recordTypeApplicationData); err != nil {
|
|
|
|
// Soft error, like EAGAIN
|
|
|
|
return 0, err
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
2012-09-06 08:50:26 +01:00
|
|
|
if err := c.error(); err != nil {
|
|
|
|
return 0, err
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
n, err = c.input.Read(b)
|
|
|
|
if c.input.off >= len(c.input.data) {
|
|
|
|
c.in.freeBlock(c.input)
|
|
|
|
c.input = nil
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) Close() error {
|
|
|
|
var alertErr error
|
2011-10-18 17:59:32 +01:00
|
|
|
|
|
|
|
c.handshakeMutex.Lock()
|
|
|
|
defer c.handshakeMutex.Unlock()
|
|
|
|
if c.handshakeComplete {
|
|
|
|
alertErr = c.sendAlert(alertCloseNotify)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.conn.Close(); err != nil {
|
2010-04-27 06:19:04 +01:00
|
|
|
return err
|
|
|
|
}
|
2011-10-18 17:59:32 +01:00
|
|
|
return alertErr
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handshake runs the client or server handshake
|
|
|
|
// protocol if it has not yet been run.
|
2010-08-30 00:59:59 +01:00
|
|
|
// Most uses of this package need not call Handshake
|
2010-04-27 06:19:04 +01:00
|
|
|
// explicitly: the first Read or Write will call it automatically.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) Handshake() error {
|
2010-04-27 06:19:04 +01:00
|
|
|
c.handshakeMutex.Lock()
|
|
|
|
defer c.handshakeMutex.Unlock()
|
|
|
|
if err := c.error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.handshakeComplete {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if c.isClient {
|
|
|
|
return c.clientHandshake()
|
|
|
|
}
|
|
|
|
return c.serverHandshake()
|
|
|
|
}
|
|
|
|
|
2010-07-14 15:40:15 +01:00
|
|
|
// ConnectionState returns basic TLS details about the connection.
|
|
|
|
func (c *Conn) ConnectionState() ConnectionState {
|
2010-04-27 06:19:04 +01:00
|
|
|
c.handshakeMutex.Lock()
|
|
|
|
defer c.handshakeMutex.Unlock()
|
2010-07-14 15:40:15 +01:00
|
|
|
|
|
|
|
var state ConnectionState
|
|
|
|
state.HandshakeComplete = c.handshakeComplete
|
|
|
|
if c.handshakeComplete {
|
|
|
|
state.NegotiatedProtocol = c.clientProtocol
|
2012-09-24 21:52:43 +01:00
|
|
|
state.DidResume = c.didResume
|
2011-03-29 22:53:09 +01:00
|
|
|
state.NegotiatedProtocolIsMutual = !c.clientProtocolFallback
|
2010-07-14 15:40:15 +01:00
|
|
|
state.CipherSuite = c.cipherSuite
|
2011-03-10 15:22:53 +00:00
|
|
|
state.PeerCertificates = c.peerCertificates
|
2011-05-05 18:44:36 +01:00
|
|
|
state.VerifiedChains = c.verifiedChains
|
2011-10-08 15:11:38 +01:00
|
|
|
state.ServerName = c.serverName
|
2010-07-14 15:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
// OCSPResponse returns the stapled OCSP response from the TLS server, if
|
|
|
|
// any. (Only valid for client connections.)
|
|
|
|
func (c *Conn) OCSPResponse() []byte {
|
|
|
|
c.handshakeMutex.Lock()
|
|
|
|
defer c.handshakeMutex.Unlock()
|
|
|
|
|
|
|
|
return c.ocspResponse
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
2010-07-21 16:36:01 +01:00
|
|
|
|
2010-09-12 04:41:12 +01:00
|
|
|
// VerifyHostname checks that the peer certificate chain is valid for
|
2011-11-02 02:58:09 +00:00
|
|
|
// connecting to host. If so, it returns nil; if not, it returns an error
|
2010-09-12 04:41:12 +01:00
|
|
|
// describing the problem.
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) VerifyHostname(host string) error {
|
2010-09-20 15:32:08 +01:00
|
|
|
c.handshakeMutex.Lock()
|
|
|
|
defer c.handshakeMutex.Unlock()
|
|
|
|
if !c.isClient {
|
2011-11-02 02:04:37 +00:00
|
|
|
return errors.New("VerifyHostname called on TLS server connection")
|
2010-09-20 15:32:08 +01:00
|
|
|
}
|
|
|
|
if !c.handshakeComplete {
|
2011-11-02 02:04:37 +00:00
|
|
|
return errors.New("TLS handshake has not yet been performed")
|
2010-09-20 15:32:08 +01:00
|
|
|
}
|
|
|
|
return c.peerCertificates[0].VerifyHostname(host)
|
2010-09-12 04:41:12 +01:00
|
|
|
}
|