Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
Adam Langley | dc7e9c4 | 2015-09-29 15:21:04 -0700 | [diff] [blame] | 7 | package runner |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "crypto/cipher" |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 12 | "crypto/ecdsa" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 13 | "crypto/subtle" |
| 14 | "crypto/x509" |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 15 | "encoding/binary" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 16 | "errors" |
| 17 | "fmt" |
| 18 | "io" |
| 19 | "net" |
| 20 | "sync" |
| 21 | "time" |
| 22 | ) |
| 23 | |
| 24 | // A Conn represents a secured connection. |
| 25 | // It implements the net.Conn interface. |
| 26 | type Conn struct { |
| 27 | // constant |
| 28 | conn net.Conn |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 29 | isDTLS bool |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 30 | isClient bool |
| 31 | |
| 32 | // constant after handshake; protected by handshakeMutex |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 33 | handshakeMutex sync.Mutex // handshakeMutex < in.Mutex, out.Mutex, errMutex |
| 34 | handshakeErr error // error resulting from handshake |
| 35 | vers uint16 // TLS version |
| 36 | haveVers bool // version has been negotiated |
| 37 | config *Config // configuration passed to constructor |
| 38 | handshakeComplete bool |
| 39 | didResume bool // whether this connection was a session resumption |
| 40 | extendedMasterSecret bool // whether this session used an extended master secret |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 41 | cipherSuite *cipherSuite |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 42 | ocspResponse []byte // stapled OCSP response |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 43 | sctList []byte // signed certificate timestamp list |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 44 | peerCertificates []*x509.Certificate |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 45 | // verifiedChains contains the certificate chains that we built, as |
| 46 | // opposed to the ones presented by the server. |
| 47 | verifiedChains [][]*x509.Certificate |
| 48 | // serverName contains the server name indicated by the client, if any. |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 49 | serverName string |
| 50 | // firstFinished contains the first Finished hash sent during the |
| 51 | // handshake. This is the "tls-unique" channel binding value. |
| 52 | firstFinished [12]byte |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 53 | // peerSignatureAlgorithm contains the signature algorithm that was used |
| 54 | // by the peer in the handshake, or zero if not applicable. |
| 55 | peerSignatureAlgorithm signatureAlgorithm |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 56 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 57 | clientRandom, serverRandom [32]byte |
David Benjamin | 97a0a08 | 2016-07-13 17:57:35 -0400 | [diff] [blame] | 58 | exporterSecret []byte |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 59 | |
| 60 | clientProtocol string |
| 61 | clientProtocolFallback bool |
David Benjamin | fc7b086 | 2014-09-06 13:21:53 -0400 | [diff] [blame] | 62 | usedALPN bool |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 63 | |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 64 | // verify_data values for the renegotiation extension. |
| 65 | clientVerify []byte |
| 66 | serverVerify []byte |
| 67 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 68 | channelID *ecdsa.PublicKey |
| 69 | |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 70 | srtpProtectionProfile uint16 |
| 71 | |
David Benjamin | c44b1df | 2014-11-23 12:11:01 -0500 | [diff] [blame] | 72 | clientVersion uint16 |
| 73 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 74 | // input/output |
| 75 | in, out halfConn // in.Mutex < out.Mutex |
| 76 | rawInput *block // raw input, right off the wire |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 77 | input *block // application record waiting to be read |
| 78 | hand bytes.Buffer // handshake record waiting to be read |
| 79 | |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 80 | // pendingFlight, if PackHandshakeFlight is enabled, is the buffer of |
| 81 | // handshake data to be split into records at the end of the flight. |
| 82 | pendingFlight bytes.Buffer |
| 83 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 84 | // DTLS state |
| 85 | sendHandshakeSeq uint16 |
| 86 | recvHandshakeSeq uint16 |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 87 | handMsg []byte // pending assembled handshake message |
| 88 | handMsgLen int // handshake message length, not including the header |
| 89 | pendingFragments [][]byte // pending outgoing handshake fragments. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | |
| 91 | tmp [16]byte |
| 92 | } |
| 93 | |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 94 | func (c *Conn) init() { |
| 95 | c.in.isDTLS = c.isDTLS |
| 96 | c.out.isDTLS = c.isDTLS |
| 97 | c.in.config = c.config |
| 98 | c.out.config = c.config |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 99 | |
| 100 | c.out.updateOutSeq() |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 103 | // Access to net.Conn methods. |
| 104 | // Cannot just embed net.Conn because that would |
| 105 | // export the struct field too. |
| 106 | |
| 107 | // LocalAddr returns the local network address. |
| 108 | func (c *Conn) LocalAddr() net.Addr { |
| 109 | return c.conn.LocalAddr() |
| 110 | } |
| 111 | |
| 112 | // RemoteAddr returns the remote network address. |
| 113 | func (c *Conn) RemoteAddr() net.Addr { |
| 114 | return c.conn.RemoteAddr() |
| 115 | } |
| 116 | |
| 117 | // SetDeadline sets the read and write deadlines associated with the connection. |
| 118 | // A zero value for t means Read and Write will not time out. |
| 119 | // After a Write has timed out, the TLS state is corrupt and all future writes will return the same error. |
| 120 | func (c *Conn) SetDeadline(t time.Time) error { |
| 121 | return c.conn.SetDeadline(t) |
| 122 | } |
| 123 | |
| 124 | // SetReadDeadline sets the read deadline on the underlying connection. |
| 125 | // A zero value for t means Read will not time out. |
| 126 | func (c *Conn) SetReadDeadline(t time.Time) error { |
| 127 | return c.conn.SetReadDeadline(t) |
| 128 | } |
| 129 | |
| 130 | // SetWriteDeadline sets the write deadline on the underlying conneciton. |
| 131 | // A zero value for t means Write will not time out. |
| 132 | // After a Write has timed out, the TLS state is corrupt and all future writes will return the same error. |
| 133 | func (c *Conn) SetWriteDeadline(t time.Time) error { |
| 134 | return c.conn.SetWriteDeadline(t) |
| 135 | } |
| 136 | |
| 137 | // A halfConn represents one direction of the record layer |
| 138 | // connection, either sending or receiving. |
| 139 | type halfConn struct { |
| 140 | sync.Mutex |
| 141 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 142 | err error // first permanent error |
| 143 | version uint16 // protocol version |
| 144 | isDTLS bool |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | cipher interface{} // cipher algorithm |
| 146 | mac macFunction |
| 147 | seq [8]byte // 64-bit sequence number |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 148 | outSeq [8]byte // Mapped sequence number |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | bfree *block // list of free blocks |
| 150 | |
| 151 | nextCipher interface{} // next encryption state |
| 152 | nextMac macFunction // next MAC algorithm |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 153 | nextSeq [6]byte // next epoch's starting sequence number in DTLS |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
| 155 | // used to save allocating a new buffer for each MAC. |
| 156 | inDigestBuf, outDigestBuf []byte |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | |
| 158 | config *Config |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | func (hc *halfConn) setErrorLocked(err error) error { |
| 162 | hc.err = err |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | func (hc *halfConn) error() error { |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 167 | // This should be locked, but I've removed it for the renegotiation |
| 168 | // tests since we don't concurrently read and write the same tls.Conn |
| 169 | // in any case during testing. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | err := hc.err |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | return err |
| 172 | } |
| 173 | |
| 174 | // prepareCipherSpec sets the encryption and MAC states |
| 175 | // that a subsequent changeCipherSpec will use. |
| 176 | func (hc *halfConn) prepareCipherSpec(version uint16, cipher interface{}, mac macFunction) { |
| 177 | hc.version = version |
| 178 | hc.nextCipher = cipher |
| 179 | hc.nextMac = mac |
| 180 | } |
| 181 | |
| 182 | // changeCipherSpec changes the encryption and MAC states |
| 183 | // to the ones previously passed to prepareCipherSpec. |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | func (hc *halfConn) changeCipherSpec(config *Config) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | if hc.nextCipher == nil { |
| 186 | return alertInternalError |
| 187 | } |
| 188 | hc.cipher = hc.nextCipher |
| 189 | hc.mac = hc.nextMac |
| 190 | hc.nextCipher = nil |
| 191 | hc.nextMac = nil |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | hc.config = config |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 193 | hc.incEpoch() |
David Benjamin | f2b8363 | 2016-03-01 22:57:46 -0500 | [diff] [blame] | 194 | |
| 195 | if config.Bugs.NullAllCiphers { |
| 196 | hc.cipher = nil |
| 197 | hc.mac = nil |
| 198 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | return nil |
| 200 | } |
| 201 | |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 202 | // updateKeys sets the current cipher state. |
| 203 | func (hc *halfConn) updateKeys(cipher interface{}, version uint16) { |
| 204 | hc.version = version |
| 205 | hc.cipher = cipher |
| 206 | hc.incEpoch() |
| 207 | } |
| 208 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | // incSeq increments the sequence number. |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 210 | func (hc *halfConn) incSeq(isOutgoing bool) { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 211 | limit := 0 |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 212 | increment := uint64(1) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 213 | if hc.isDTLS { |
| 214 | // Increment up to the epoch in DTLS. |
| 215 | limit = 2 |
| 216 | } |
| 217 | for i := 7; i >= limit; i-- { |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 218 | increment += uint64(hc.seq[i]) |
| 219 | hc.seq[i] = byte(increment) |
| 220 | increment >>= 8 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Not allowed to let sequence number wrap. |
| 224 | // Instead, must renegotiate before it does. |
| 225 | // Not likely enough to bother. |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 226 | if increment != 0 { |
| 227 | panic("TLS: sequence number wraparound") |
| 228 | } |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 229 | |
| 230 | hc.updateOutSeq() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | } |
| 232 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 233 | // incNextSeq increments the starting sequence number for the next epoch. |
| 234 | func (hc *halfConn) incNextSeq() { |
| 235 | for i := len(hc.nextSeq) - 1; i >= 0; i-- { |
| 236 | hc.nextSeq[i]++ |
| 237 | if hc.nextSeq[i] != 0 { |
| 238 | return |
| 239 | } |
| 240 | } |
| 241 | panic("TLS: sequence number wraparound") |
| 242 | } |
| 243 | |
| 244 | // incEpoch resets the sequence number. In DTLS, it also increments the epoch |
| 245 | // half of the sequence number. |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 246 | func (hc *halfConn) incEpoch() { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 247 | if hc.isDTLS { |
| 248 | for i := 1; i >= 0; i-- { |
| 249 | hc.seq[i]++ |
| 250 | if hc.seq[i] != 0 { |
| 251 | break |
| 252 | } |
| 253 | if i == 0 { |
| 254 | panic("TLS: epoch number wraparound") |
| 255 | } |
| 256 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 257 | copy(hc.seq[2:], hc.nextSeq[:]) |
| 258 | for i := range hc.nextSeq { |
| 259 | hc.nextSeq[i] = 0 |
| 260 | } |
| 261 | } else { |
| 262 | for i := range hc.seq { |
| 263 | hc.seq[i] = 0 |
| 264 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 265 | } |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 266 | |
| 267 | hc.updateOutSeq() |
| 268 | } |
| 269 | |
| 270 | func (hc *halfConn) updateOutSeq() { |
| 271 | if hc.config.Bugs.SequenceNumberMapping != nil { |
| 272 | seqU64 := binary.BigEndian.Uint64(hc.seq[:]) |
| 273 | seqU64 = hc.config.Bugs.SequenceNumberMapping(seqU64) |
| 274 | binary.BigEndian.PutUint64(hc.outSeq[:], seqU64) |
| 275 | |
| 276 | // The DTLS epoch cannot be changed. |
| 277 | copy(hc.outSeq[:2], hc.seq[:2]) |
| 278 | return |
| 279 | } |
| 280 | |
| 281 | copy(hc.outSeq[:], hc.seq[:]) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | func (hc *halfConn) recordHeaderLen() int { |
| 285 | if hc.isDTLS { |
| 286 | return dtlsRecordHeaderLen |
| 287 | } |
| 288 | return tlsRecordHeaderLen |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // removePadding returns an unpadded slice, in constant time, which is a prefix |
| 292 | // of the input. It also returns a byte which is equal to 255 if the padding |
| 293 | // was valid and 0 otherwise. See RFC 2246, section 6.2.3.2 |
| 294 | func removePadding(payload []byte) ([]byte, byte) { |
| 295 | if len(payload) < 1 { |
| 296 | return payload, 0 |
| 297 | } |
| 298 | |
| 299 | paddingLen := payload[len(payload)-1] |
| 300 | t := uint(len(payload)-1) - uint(paddingLen) |
| 301 | // if len(payload) >= (paddingLen - 1) then the MSB of t is zero |
| 302 | good := byte(int32(^t) >> 31) |
| 303 | |
| 304 | toCheck := 255 // the maximum possible padding length |
| 305 | // The length of the padded data is public, so we can use an if here |
| 306 | if toCheck+1 > len(payload) { |
| 307 | toCheck = len(payload) - 1 |
| 308 | } |
| 309 | |
| 310 | for i := 0; i < toCheck; i++ { |
| 311 | t := uint(paddingLen) - uint(i) |
| 312 | // if i <= paddingLen then the MSB of t is zero |
| 313 | mask := byte(int32(^t) >> 31) |
| 314 | b := payload[len(payload)-1-i] |
| 315 | good &^= mask&paddingLen ^ mask&b |
| 316 | } |
| 317 | |
| 318 | // We AND together the bits of good and replicate the result across |
| 319 | // all the bits. |
| 320 | good &= good << 4 |
| 321 | good &= good << 2 |
| 322 | good &= good << 1 |
| 323 | good = uint8(int8(good) >> 7) |
| 324 | |
| 325 | toRemove := good&paddingLen + 1 |
| 326 | return payload[:len(payload)-int(toRemove)], good |
| 327 | } |
| 328 | |
| 329 | // removePaddingSSL30 is a replacement for removePadding in the case that the |
| 330 | // protocol version is SSLv3. In this version, the contents of the padding |
| 331 | // are random and cannot be checked. |
| 332 | func removePaddingSSL30(payload []byte) ([]byte, byte) { |
| 333 | if len(payload) < 1 { |
| 334 | return payload, 0 |
| 335 | } |
| 336 | |
| 337 | paddingLen := int(payload[len(payload)-1]) + 1 |
| 338 | if paddingLen > len(payload) { |
| 339 | return payload, 0 |
| 340 | } |
| 341 | |
| 342 | return payload[:len(payload)-paddingLen], 255 |
| 343 | } |
| 344 | |
| 345 | func roundUp(a, b int) int { |
| 346 | return a + (b-a%b)%b |
| 347 | } |
| 348 | |
| 349 | // cbcMode is an interface for block ciphers using cipher block chaining. |
| 350 | type cbcMode interface { |
| 351 | cipher.BlockMode |
| 352 | SetIV([]byte) |
| 353 | } |
| 354 | |
| 355 | // decrypt checks and strips the mac and decrypts the data in b. Returns a |
| 356 | // success boolean, the number of bytes to skip from the start of the record in |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 357 | // order to get the application payload, the encrypted record type (or 0 |
| 358 | // if there is none), and an optional alert value. |
| 359 | func (hc *halfConn) decrypt(b *block) (ok bool, prefixLen int, contentType recordType, alertValue alert) { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 360 | recordHeaderLen := hc.recordHeaderLen() |
| 361 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | // pull out payload |
| 363 | payload := b.data[recordHeaderLen:] |
| 364 | |
| 365 | macSize := 0 |
| 366 | if hc.mac != nil { |
| 367 | macSize = hc.mac.Size() |
| 368 | } |
| 369 | |
| 370 | paddingGood := byte(255) |
| 371 | explicitIVLen := 0 |
| 372 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 373 | seq := hc.seq[:] |
| 374 | if hc.isDTLS { |
| 375 | // DTLS sequence numbers are explicit. |
| 376 | seq = b.data[3:11] |
| 377 | } |
| 378 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | // decrypt |
| 380 | if hc.cipher != nil { |
| 381 | switch c := hc.cipher.(type) { |
| 382 | case cipher.Stream: |
| 383 | c.XORKeyStream(payload, payload) |
David Benjamin | e9a80ff | 2015-04-07 00:46:46 -0400 | [diff] [blame] | 384 | case *tlsAead: |
| 385 | nonce := seq |
| 386 | if c.explicitNonce { |
| 387 | explicitIVLen = 8 |
| 388 | if len(payload) < explicitIVLen { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 389 | return false, 0, 0, alertBadRecordMAC |
David Benjamin | e9a80ff | 2015-04-07 00:46:46 -0400 | [diff] [blame] | 390 | } |
| 391 | nonce = payload[:8] |
| 392 | payload = payload[8:] |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 394 | |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 395 | var additionalData []byte |
| 396 | if hc.version < VersionTLS13 { |
| 397 | additionalData = make([]byte, 13) |
| 398 | copy(additionalData, seq) |
| 399 | copy(additionalData[8:], b.data[:3]) |
| 400 | n := len(payload) - c.Overhead() |
| 401 | additionalData[11] = byte(n >> 8) |
| 402 | additionalData[12] = byte(n) |
| 403 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 404 | var err error |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 405 | payload, err = c.Open(payload[:0], nonce, payload, additionalData) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | if err != nil { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 407 | return false, 0, 0, alertBadRecordMAC |
| 408 | } |
| 409 | if hc.version >= VersionTLS13 { |
| 410 | i := len(payload) |
| 411 | for i > 0 && payload[i-1] == 0 { |
| 412 | i-- |
| 413 | } |
| 414 | payload = payload[:i] |
| 415 | if len(payload) == 0 { |
| 416 | return false, 0, 0, alertUnexpectedMessage |
| 417 | } |
| 418 | contentType = recordType(payload[len(payload)-1]) |
| 419 | payload = payload[:len(payload)-1] |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 420 | } |
| 421 | b.resize(recordHeaderLen + explicitIVLen + len(payload)) |
| 422 | case cbcMode: |
| 423 | blockSize := c.BlockSize() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 424 | if hc.version >= VersionTLS11 || hc.isDTLS { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | explicitIVLen = blockSize |
| 426 | } |
| 427 | |
| 428 | if len(payload)%blockSize != 0 || len(payload) < roundUp(explicitIVLen+macSize+1, blockSize) { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 429 | return false, 0, 0, alertBadRecordMAC |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if explicitIVLen > 0 { |
| 433 | c.SetIV(payload[:explicitIVLen]) |
| 434 | payload = payload[explicitIVLen:] |
| 435 | } |
| 436 | c.CryptBlocks(payload, payload) |
| 437 | if hc.version == VersionSSL30 { |
| 438 | payload, paddingGood = removePaddingSSL30(payload) |
| 439 | } else { |
| 440 | payload, paddingGood = removePadding(payload) |
| 441 | } |
| 442 | b.resize(recordHeaderLen + explicitIVLen + len(payload)) |
| 443 | |
| 444 | // note that we still have a timing side-channel in the |
| 445 | // MAC check, below. An attacker can align the record |
| 446 | // so that a correct padding will cause one less hash |
| 447 | // block to be calculated. Then they can iteratively |
| 448 | // decrypt a record by breaking each byte. See |
| 449 | // "Password Interception in a SSL/TLS Channel", Brice |
| 450 | // Canvel et al. |
| 451 | // |
| 452 | // However, our behavior matches OpenSSL, so we leak |
| 453 | // only as much as they do. |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 454 | case nullCipher: |
| 455 | break |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 456 | default: |
| 457 | panic("unknown cipher type") |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // check, strip mac |
| 462 | if hc.mac != nil { |
| 463 | if len(payload) < macSize { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 464 | return false, 0, 0, alertBadRecordMAC |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // strip mac off payload, b.data |
| 468 | n := len(payload) - macSize |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 469 | b.data[recordHeaderLen-2] = byte(n >> 8) |
| 470 | b.data[recordHeaderLen-1] = byte(n) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | b.resize(recordHeaderLen + explicitIVLen + n) |
| 472 | remoteMAC := payload[n:] |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 473 | localMAC := hc.mac.MAC(hc.inDigestBuf, seq, b.data[:3], b.data[recordHeaderLen-2:recordHeaderLen], payload[:n]) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | |
| 475 | if subtle.ConstantTimeCompare(localMAC, remoteMAC) != 1 || paddingGood != 255 { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 476 | return false, 0, 0, alertBadRecordMAC |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | } |
| 478 | hc.inDigestBuf = localMAC |
| 479 | } |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 480 | hc.incSeq(false) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 482 | return true, recordHeaderLen + explicitIVLen, contentType, 0 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // padToBlockSize calculates the needed padding block, if any, for a payload. |
| 486 | // On exit, prefix aliases payload and extends to the end of the last full |
| 487 | // block of payload. finalBlock is a fresh slice which contains the contents of |
| 488 | // any suffix of payload as well as the needed padding to make finalBlock a |
| 489 | // full block. |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | func padToBlockSize(payload []byte, blockSize int, config *Config) (prefix, finalBlock []byte) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | overrun := len(payload) % blockSize |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 492 | prefix = payload[:len(payload)-overrun] |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | |
| 494 | paddingLen := blockSize - overrun |
| 495 | finalSize := blockSize |
| 496 | if config.Bugs.MaxPadding { |
| 497 | for paddingLen+blockSize <= 256 { |
| 498 | paddingLen += blockSize |
| 499 | } |
| 500 | finalSize = 256 |
| 501 | } |
| 502 | finalBlock = make([]byte, finalSize) |
| 503 | for i := range finalBlock { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | finalBlock[i] = byte(paddingLen - 1) |
| 505 | } |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 506 | if config.Bugs.PaddingFirstByteBad || config.Bugs.PaddingFirstByteBadIf255 && paddingLen == 256 { |
| 507 | finalBlock[overrun] ^= 0xff |
| 508 | } |
| 509 | copy(finalBlock, payload[len(payload)-overrun:]) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | return |
| 511 | } |
| 512 | |
| 513 | // encrypt encrypts and macs the data in b. |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 514 | func (hc *halfConn) encrypt(b *block, explicitIVLen int, typ recordType) (bool, alert) { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 515 | recordHeaderLen := hc.recordHeaderLen() |
| 516 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | // mac |
| 518 | if hc.mac != nil { |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 519 | mac := hc.mac.MAC(hc.outDigestBuf, hc.outSeq[0:], b.data[:3], b.data[recordHeaderLen-2:recordHeaderLen], b.data[recordHeaderLen+explicitIVLen:]) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 520 | |
| 521 | n := len(b.data) |
| 522 | b.resize(n + len(mac)) |
| 523 | copy(b.data[n:], mac) |
| 524 | hc.outDigestBuf = mac |
| 525 | } |
| 526 | |
| 527 | payload := b.data[recordHeaderLen:] |
| 528 | |
| 529 | // encrypt |
| 530 | if hc.cipher != nil { |
| 531 | switch c := hc.cipher.(type) { |
| 532 | case cipher.Stream: |
| 533 | c.XORKeyStream(payload, payload) |
David Benjamin | e9a80ff | 2015-04-07 00:46:46 -0400 | [diff] [blame] | 534 | case *tlsAead: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | payloadLen := len(b.data) - recordHeaderLen - explicitIVLen |
David Benjamin | c9ae27c | 2016-06-24 22:56:37 -0400 | [diff] [blame] | 536 | paddingLen := 0 |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 537 | if hc.version >= VersionTLS13 { |
David Benjamin | c9ae27c | 2016-06-24 22:56:37 -0400 | [diff] [blame] | 538 | payloadLen++ |
| 539 | paddingLen = hc.config.Bugs.RecordPadding |
| 540 | } |
| 541 | if hc.config.Bugs.OmitRecordContents { |
| 542 | payloadLen = 0 |
| 543 | } |
| 544 | b.resize(recordHeaderLen + explicitIVLen + payloadLen + paddingLen + c.Overhead()) |
| 545 | if hc.version >= VersionTLS13 { |
| 546 | if !hc.config.Bugs.OmitRecordContents { |
| 547 | b.data[payloadLen+recordHeaderLen-1] = byte(typ) |
| 548 | } |
| 549 | for i := 0; i < hc.config.Bugs.RecordPadding; i++ { |
| 550 | b.data[payloadLen+recordHeaderLen+i] = 0 |
| 551 | } |
| 552 | payloadLen += paddingLen |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 553 | } |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 554 | nonce := hc.outSeq[:] |
David Benjamin | e9a80ff | 2015-04-07 00:46:46 -0400 | [diff] [blame] | 555 | if c.explicitNonce { |
| 556 | nonce = b.data[recordHeaderLen : recordHeaderLen+explicitIVLen] |
| 557 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | payload := b.data[recordHeaderLen+explicitIVLen:] |
| 559 | payload = payload[:payloadLen] |
| 560 | |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 561 | var additionalData []byte |
| 562 | if hc.version < VersionTLS13 { |
| 563 | additionalData = make([]byte, 13) |
| 564 | copy(additionalData, hc.outSeq[:]) |
| 565 | copy(additionalData[8:], b.data[:3]) |
| 566 | additionalData[11] = byte(payloadLen >> 8) |
| 567 | additionalData[12] = byte(payloadLen) |
| 568 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 570 | c.Seal(payload[:0], nonce, payload, additionalData) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 571 | case cbcMode: |
| 572 | blockSize := c.BlockSize() |
| 573 | if explicitIVLen > 0 { |
| 574 | c.SetIV(payload[:explicitIVLen]) |
| 575 | payload = payload[explicitIVLen:] |
| 576 | } |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 577 | prefix, finalBlock := padToBlockSize(payload, blockSize, hc.config) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 578 | b.resize(recordHeaderLen + explicitIVLen + len(prefix) + len(finalBlock)) |
| 579 | c.CryptBlocks(b.data[recordHeaderLen+explicitIVLen:], prefix) |
| 580 | c.CryptBlocks(b.data[recordHeaderLen+explicitIVLen+len(prefix):], finalBlock) |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 581 | case nullCipher: |
| 582 | break |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 583 | default: |
| 584 | panic("unknown cipher type") |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // update length to include MAC and any block padding needed. |
| 589 | n := len(b.data) - recordHeaderLen |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 590 | b.data[recordHeaderLen-2] = byte(n >> 8) |
| 591 | b.data[recordHeaderLen-1] = byte(n) |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 592 | hc.incSeq(true) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 593 | |
| 594 | return true, 0 |
| 595 | } |
| 596 | |
| 597 | // A block is a simple data buffer. |
| 598 | type block struct { |
| 599 | data []byte |
| 600 | off int // index for Read |
| 601 | link *block |
| 602 | } |
| 603 | |
| 604 | // resize resizes block to be n bytes, growing if necessary. |
| 605 | func (b *block) resize(n int) { |
| 606 | if n > cap(b.data) { |
| 607 | b.reserve(n) |
| 608 | } |
| 609 | b.data = b.data[0:n] |
| 610 | } |
| 611 | |
| 612 | // reserve makes sure that block contains a capacity of at least n bytes. |
| 613 | func (b *block) reserve(n int) { |
| 614 | if cap(b.data) >= n { |
| 615 | return |
| 616 | } |
| 617 | m := cap(b.data) |
| 618 | if m == 0 { |
| 619 | m = 1024 |
| 620 | } |
| 621 | for m < n { |
| 622 | m *= 2 |
| 623 | } |
| 624 | data := make([]byte, len(b.data), m) |
| 625 | copy(data, b.data) |
| 626 | b.data = data |
| 627 | } |
| 628 | |
| 629 | // readFromUntil reads from r into b until b contains at least n bytes |
| 630 | // or else returns an error. |
| 631 | func (b *block) readFromUntil(r io.Reader, n int) error { |
| 632 | // quick case |
| 633 | if len(b.data) >= n { |
| 634 | return nil |
| 635 | } |
| 636 | |
| 637 | // read until have enough. |
| 638 | b.reserve(n) |
| 639 | for { |
| 640 | m, err := r.Read(b.data[len(b.data):cap(b.data)]) |
| 641 | b.data = b.data[0 : len(b.data)+m] |
| 642 | if len(b.data) >= n { |
| 643 | // TODO(bradfitz,agl): slightly suspicious |
| 644 | // that we're throwing away r.Read's err here. |
| 645 | break |
| 646 | } |
| 647 | if err != nil { |
| 648 | return err |
| 649 | } |
| 650 | } |
| 651 | return nil |
| 652 | } |
| 653 | |
| 654 | func (b *block) Read(p []byte) (n int, err error) { |
| 655 | n = copy(p, b.data[b.off:]) |
| 656 | b.off += n |
| 657 | return |
| 658 | } |
| 659 | |
| 660 | // newBlock allocates a new block, from hc's free list if possible. |
| 661 | func (hc *halfConn) newBlock() *block { |
| 662 | b := hc.bfree |
| 663 | if b == nil { |
| 664 | return new(block) |
| 665 | } |
| 666 | hc.bfree = b.link |
| 667 | b.link = nil |
| 668 | b.resize(0) |
| 669 | return b |
| 670 | } |
| 671 | |
| 672 | // freeBlock returns a block to hc's free list. |
| 673 | // The protocol is such that each side only has a block or two on |
| 674 | // its free list at a time, so there's no need to worry about |
| 675 | // trimming the list, etc. |
| 676 | func (hc *halfConn) freeBlock(b *block) { |
| 677 | b.link = hc.bfree |
| 678 | hc.bfree = b |
| 679 | } |
| 680 | |
| 681 | // splitBlock splits a block after the first n bytes, |
| 682 | // returning a block with those n bytes and a |
| 683 | // block with the remainder. the latter may be nil. |
| 684 | func (hc *halfConn) splitBlock(b *block, n int) (*block, *block) { |
| 685 | if len(b.data) <= n { |
| 686 | return b, nil |
| 687 | } |
| 688 | bb := hc.newBlock() |
| 689 | bb.resize(len(b.data) - n) |
| 690 | copy(bb.data, b.data[n:]) |
| 691 | b.data = b.data[0:n] |
| 692 | return b, bb |
| 693 | } |
| 694 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 695 | func (c *Conn) doReadRecord(want recordType) (recordType, *block, error) { |
| 696 | if c.isDTLS { |
| 697 | return c.dtlsDoReadRecord(want) |
| 698 | } |
| 699 | |
| 700 | recordHeaderLen := tlsRecordHeaderLen |
| 701 | |
| 702 | if c.rawInput == nil { |
| 703 | c.rawInput = c.in.newBlock() |
| 704 | } |
| 705 | b := c.rawInput |
| 706 | |
| 707 | // Read header, payload. |
| 708 | if err := b.readFromUntil(c.conn, recordHeaderLen); err != nil { |
| 709 | // RFC suggests that EOF without an alertCloseNotify is |
| 710 | // an error, but popular web sites seem to do this, |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 711 | // so we can't make it an error, outside of tests. |
| 712 | if err == io.EOF && c.config.Bugs.ExpectCloseNotify { |
| 713 | err = io.ErrUnexpectedEOF |
| 714 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 715 | if e, ok := err.(net.Error); !ok || !e.Temporary() { |
| 716 | c.in.setErrorLocked(err) |
| 717 | } |
| 718 | return 0, nil, err |
| 719 | } |
| 720 | typ := recordType(b.data[0]) |
| 721 | |
| 722 | // No valid TLS record has a type of 0x80, however SSLv2 handshakes |
| 723 | // start with a uint16 length where the MSB is set and the first record |
| 724 | // is always < 256 bytes long. Therefore typ == 0x80 strongly suggests |
| 725 | // an SSLv2 client. |
| 726 | if want == recordTypeHandshake && typ == 0x80 { |
| 727 | c.sendAlert(alertProtocolVersion) |
| 728 | return 0, nil, c.in.setErrorLocked(errors.New("tls: unsupported SSLv2 handshake received")) |
| 729 | } |
| 730 | |
| 731 | vers := uint16(b.data[1])<<8 | uint16(b.data[2]) |
| 732 | n := int(b.data[3])<<8 | int(b.data[4]) |
David Benjamin | bde0039 | 2016-06-21 12:19:28 -0400 | [diff] [blame] | 733 | // Alerts sent near version negotiation do not have a well-defined |
| 734 | // record-layer version prior to TLS 1.3. (In TLS 1.3, the record-layer |
| 735 | // version is irrelevant.) |
| 736 | if typ != recordTypeAlert { |
| 737 | if c.haveVers { |
| 738 | if vers != c.vers && c.vers < VersionTLS13 { |
| 739 | c.sendAlert(alertProtocolVersion) |
| 740 | return 0, nil, c.in.setErrorLocked(fmt.Errorf("tls: received record with version %x when expecting version %x", vers, c.vers)) |
| 741 | } |
| 742 | } else { |
| 743 | if expect := c.config.Bugs.ExpectInitialRecordVersion; expect != 0 && vers != expect { |
| 744 | c.sendAlert(alertProtocolVersion) |
| 745 | return 0, nil, c.in.setErrorLocked(fmt.Errorf("tls: received record with version %x when expecting version %x", vers, expect)) |
| 746 | } |
David Benjamin | 1e29a6b | 2014-12-10 02:27:24 -0500 | [diff] [blame] | 747 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 748 | } |
| 749 | if n > maxCiphertext { |
| 750 | c.sendAlert(alertRecordOverflow) |
| 751 | return 0, nil, c.in.setErrorLocked(fmt.Errorf("tls: oversized record received with length %d", n)) |
| 752 | } |
| 753 | if !c.haveVers { |
| 754 | // First message, be extra suspicious: |
| 755 | // this might not be a TLS client. |
| 756 | // Bail out before reading a full 'body', if possible. |
| 757 | // The current max version is 3.1. |
| 758 | // If the version is >= 16.0, it's probably not real. |
| 759 | // Similarly, a clientHello message encodes in |
| 760 | // well under a kilobyte. If the length is >= 12 kB, |
| 761 | // it's probably not real. |
| 762 | if (typ != recordTypeAlert && typ != want) || vers >= 0x1000 || n >= 0x3000 { |
| 763 | c.sendAlert(alertUnexpectedMessage) |
| 764 | return 0, nil, c.in.setErrorLocked(fmt.Errorf("tls: first record does not look like a TLS handshake")) |
| 765 | } |
| 766 | } |
| 767 | if err := b.readFromUntil(c.conn, recordHeaderLen+n); err != nil { |
| 768 | if err == io.EOF { |
| 769 | err = io.ErrUnexpectedEOF |
| 770 | } |
| 771 | if e, ok := err.(net.Error); !ok || !e.Temporary() { |
| 772 | c.in.setErrorLocked(err) |
| 773 | } |
| 774 | return 0, nil, err |
| 775 | } |
| 776 | |
| 777 | // Process message. |
| 778 | b, c.rawInput = c.in.splitBlock(b, recordHeaderLen+n) |
David Benjamin | ff26f09 | 2016-07-01 16:13:42 -0400 | [diff] [blame] | 779 | ok, off, encTyp, alertValue := c.in.decrypt(b) |
| 780 | if !ok { |
| 781 | return 0, nil, c.in.setErrorLocked(c.sendAlert(alertValue)) |
| 782 | } |
| 783 | b.off = off |
| 784 | |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 785 | if c.vers >= VersionTLS13 && c.in.cipher != nil { |
David Benjamin | c9ae27c | 2016-06-24 22:56:37 -0400 | [diff] [blame] | 786 | if typ != recordTypeApplicationData { |
| 787 | return 0, nil, c.in.setErrorLocked(fmt.Errorf("tls: outer record type is not application data")) |
| 788 | } |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 789 | typ = encTyp |
| 790 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 791 | return typ, b, nil |
| 792 | } |
| 793 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | // readRecord reads the next TLS record from the connection |
| 795 | // and updates the record layer state. |
| 796 | // c.in.Mutex <= L; c.input == nil. |
| 797 | func (c *Conn) readRecord(want recordType) error { |
| 798 | // Caller must be in sync with connection: |
| 799 | // handshake data if handshake not yet completed, |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 800 | // else application data. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 801 | switch want { |
| 802 | default: |
| 803 | c.sendAlert(alertInternalError) |
| 804 | return c.in.setErrorLocked(errors.New("tls: unknown record type requested")) |
| 805 | case recordTypeHandshake, recordTypeChangeCipherSpec: |
| 806 | if c.handshakeComplete { |
| 807 | c.sendAlert(alertInternalError) |
| 808 | return c.in.setErrorLocked(errors.New("tls: handshake or ChangeCipherSpec requested after handshake complete")) |
| 809 | } |
| 810 | case recordTypeApplicationData: |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 811 | if !c.handshakeComplete && !c.config.Bugs.ExpectFalseStart { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 812 | c.sendAlert(alertInternalError) |
| 813 | return c.in.setErrorLocked(errors.New("tls: application data record requested before handshake complete")) |
| 814 | } |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 815 | case recordTypeAlert: |
| 816 | // Looking for a close_notify. Note: unlike a real |
| 817 | // implementation, this is not tolerant of additional records. |
| 818 | // See the documentation for ExpectCloseNotify. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | Again: |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 822 | typ, b, err := c.doReadRecord(want) |
| 823 | if err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 824 | return err |
| 825 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 826 | data := b.data[b.off:] |
| 827 | if len(data) > maxPlaintext { |
| 828 | err := c.sendAlert(alertRecordOverflow) |
| 829 | c.in.freeBlock(b) |
| 830 | return c.in.setErrorLocked(err) |
| 831 | } |
| 832 | |
| 833 | switch typ { |
| 834 | default: |
| 835 | c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage)) |
| 836 | |
| 837 | case recordTypeAlert: |
| 838 | if len(data) != 2 { |
| 839 | c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage)) |
| 840 | break |
| 841 | } |
| 842 | if alert(data[1]) == alertCloseNotify { |
| 843 | c.in.setErrorLocked(io.EOF) |
| 844 | break |
| 845 | } |
| 846 | switch data[0] { |
| 847 | case alertLevelWarning: |
| 848 | // drop on the floor |
| 849 | c.in.freeBlock(b) |
| 850 | goto Again |
| 851 | case alertLevelError: |
| 852 | c.in.setErrorLocked(&net.OpError{Op: "remote error", Err: alert(data[1])}) |
| 853 | default: |
| 854 | c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage)) |
| 855 | } |
| 856 | |
| 857 | case recordTypeChangeCipherSpec: |
| 858 | if typ != want || len(data) != 1 || data[0] != 1 { |
| 859 | c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage)) |
| 860 | break |
| 861 | } |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 862 | err := c.in.changeCipherSpec(c.config) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 863 | if err != nil { |
| 864 | c.in.setErrorLocked(c.sendAlert(err.(alert))) |
| 865 | } |
| 866 | |
| 867 | case recordTypeApplicationData: |
| 868 | if typ != want { |
| 869 | c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage)) |
| 870 | break |
| 871 | } |
| 872 | c.input = b |
| 873 | b = nil |
| 874 | |
| 875 | case recordTypeHandshake: |
| 876 | // TODO(rsc): Should at least pick off connection close. |
| 877 | if typ != want { |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 878 | // A client might need to process a HelloRequest from |
| 879 | // the server, thus receiving a handshake message when |
David Benjamin | d9b091b | 2015-01-27 01:10:54 -0500 | [diff] [blame] | 880 | // application data is expected is ok. |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 881 | if !c.isClient || want != recordTypeApplicationData { |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 882 | return c.in.setErrorLocked(c.sendAlert(alertNoRenegotiation)) |
| 883 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 884 | } |
| 885 | c.hand.Write(data) |
| 886 | } |
| 887 | |
| 888 | if b != nil { |
| 889 | c.in.freeBlock(b) |
| 890 | } |
| 891 | return c.in.err |
| 892 | } |
| 893 | |
| 894 | // sendAlert sends a TLS alert message. |
| 895 | // c.out.Mutex <= L. |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 896 | func (c *Conn) sendAlertLocked(level byte, err alert) error { |
| 897 | c.tmp[0] = level |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 898 | c.tmp[1] = byte(err) |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 899 | if c.config.Bugs.FragmentAlert { |
| 900 | c.writeRecord(recordTypeAlert, c.tmp[0:1]) |
| 901 | c.writeRecord(recordTypeAlert, c.tmp[1:2]) |
David Benjamin | 0d3a8c6 | 2016-03-11 22:25:18 -0500 | [diff] [blame] | 902 | } else if c.config.Bugs.DoubleAlert { |
| 903 | copy(c.tmp[2:4], c.tmp[0:2]) |
| 904 | c.writeRecord(recordTypeAlert, c.tmp[0:4]) |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 905 | } else { |
| 906 | c.writeRecord(recordTypeAlert, c.tmp[0:2]) |
| 907 | } |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 908 | // Error alerts are fatal to the connection. |
| 909 | if level == alertLevelError { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | return c.out.setErrorLocked(&net.OpError{Op: "local error", Err: err}) |
| 911 | } |
| 912 | return nil |
| 913 | } |
| 914 | |
| 915 | // sendAlert sends a TLS alert message. |
| 916 | // L < c.out.Mutex. |
| 917 | func (c *Conn) sendAlert(err alert) error { |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 918 | level := byte(alertLevelError) |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 919 | if err == alertNoRenegotiation || err == alertCloseNotify || err == alertNoCertficate { |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 920 | level = alertLevelWarning |
| 921 | } |
| 922 | return c.SendAlert(level, err) |
| 923 | } |
| 924 | |
| 925 | func (c *Conn) SendAlert(level byte, err alert) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 926 | c.out.Lock() |
| 927 | defer c.out.Unlock() |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 928 | return c.sendAlertLocked(level, err) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 929 | } |
| 930 | |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 931 | // writeV2Record writes a record for a V2ClientHello. |
| 932 | func (c *Conn) writeV2Record(data []byte) (n int, err error) { |
| 933 | record := make([]byte, 2+len(data)) |
| 934 | record[0] = uint8(len(data)>>8) | 0x80 |
| 935 | record[1] = uint8(len(data)) |
| 936 | copy(record[2:], data) |
| 937 | return c.conn.Write(record) |
| 938 | } |
| 939 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 940 | // writeRecord writes a TLS record with the given type and payload |
| 941 | // to the connection and updates the record layer state. |
| 942 | // c.out.Mutex <= L. |
| 943 | func (c *Conn) writeRecord(typ recordType, data []byte) (n int, err error) { |
David Benjamin | 0b8d5da | 2016-07-15 00:39:56 -0400 | [diff] [blame] | 944 | if wrongType := c.config.Bugs.SendWrongMessageType; wrongType != 0 { |
| 945 | if typ == recordTypeHandshake && data[0] == wrongType { |
| 946 | newData := make([]byte, len(data)) |
| 947 | copy(newData, data) |
| 948 | newData[0] += 42 |
| 949 | data = newData |
| 950 | } |
| 951 | } |
| 952 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 953 | if c.isDTLS { |
| 954 | return c.dtlsWriteRecord(typ, data) |
| 955 | } |
| 956 | |
David Benjamin | 71dd666 | 2016-07-08 14:10:48 -0700 | [diff] [blame] | 957 | if typ == recordTypeHandshake { |
| 958 | if c.config.Bugs.SendHelloRequestBeforeEveryHandshakeMessage { |
| 959 | newData := make([]byte, 0, 4+len(data)) |
| 960 | newData = append(newData, typeHelloRequest, 0, 0, 0) |
| 961 | newData = append(newData, data...) |
| 962 | data = newData |
| 963 | } |
| 964 | |
| 965 | if c.config.Bugs.PackHandshakeFlight { |
| 966 | c.pendingFlight.Write(data) |
| 967 | return len(data), nil |
| 968 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | return c.doWriteRecord(typ, data) |
| 972 | } |
| 973 | |
| 974 | func (c *Conn) doWriteRecord(typ recordType, data []byte) (n int, err error) { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 975 | recordHeaderLen := tlsRecordHeaderLen |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 976 | b := c.out.newBlock() |
David Benjamin | 9821454 | 2014-08-07 18:02:39 -0400 | [diff] [blame] | 977 | first := true |
| 978 | isClientHello := typ == recordTypeHandshake && len(data) > 0 && data[0] == typeClientHello |
David Benjamin | a8ebe22 | 2015-06-06 03:04:39 -0400 | [diff] [blame] | 979 | for len(data) > 0 || first { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 980 | m := len(data) |
David Benjamin | 2c99d28 | 2015-09-01 10:23:00 -0400 | [diff] [blame] | 981 | if m > maxPlaintext && !c.config.Bugs.SendLargeRecords { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 982 | m = maxPlaintext |
| 983 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 984 | if typ == recordTypeHandshake && c.config.Bugs.MaxHandshakeRecordLength > 0 && m > c.config.Bugs.MaxHandshakeRecordLength { |
| 985 | m = c.config.Bugs.MaxHandshakeRecordLength |
David Benjamin | 9821454 | 2014-08-07 18:02:39 -0400 | [diff] [blame] | 986 | // By default, do not fragment the client_version or |
| 987 | // server_version, which are located in the first 6 |
| 988 | // bytes. |
| 989 | if first && isClientHello && !c.config.Bugs.FragmentClientVersion && m < 6 { |
| 990 | m = 6 |
| 991 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 992 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 993 | explicitIVLen := 0 |
| 994 | explicitIVIsSeq := false |
David Benjamin | 9821454 | 2014-08-07 18:02:39 -0400 | [diff] [blame] | 995 | first = false |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 996 | |
| 997 | var cbc cbcMode |
| 998 | if c.out.version >= VersionTLS11 { |
| 999 | var ok bool |
| 1000 | if cbc, ok = c.out.cipher.(cbcMode); ok { |
| 1001 | explicitIVLen = cbc.BlockSize() |
| 1002 | } |
| 1003 | } |
| 1004 | if explicitIVLen == 0 { |
David Benjamin | e9a80ff | 2015-04-07 00:46:46 -0400 | [diff] [blame] | 1005 | if aead, ok := c.out.cipher.(*tlsAead); ok && aead.explicitNonce { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1006 | explicitIVLen = 8 |
| 1007 | // The AES-GCM construction in TLS has an |
| 1008 | // explicit nonce so that the nonce can be |
| 1009 | // random. However, the nonce is only 8 bytes |
| 1010 | // which is too small for a secure, random |
| 1011 | // nonce. Therefore we use the sequence number |
| 1012 | // as the nonce. |
| 1013 | explicitIVIsSeq = true |
| 1014 | } |
| 1015 | } |
| 1016 | b.resize(recordHeaderLen + explicitIVLen + m) |
| 1017 | b.data[0] = byte(typ) |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1018 | if c.vers >= VersionTLS13 && c.out.cipher != nil { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1019 | b.data[0] = byte(recordTypeApplicationData) |
David Benjamin | c9ae27c | 2016-06-24 22:56:37 -0400 | [diff] [blame] | 1020 | if outerType := c.config.Bugs.OuterRecordType; outerType != 0 { |
| 1021 | b.data[0] = byte(outerType) |
| 1022 | } |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1023 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1024 | vers := c.vers |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1025 | if vers == 0 || vers >= VersionTLS13 { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1026 | // Some TLS servers fail if the record version is |
| 1027 | // greater than TLS 1.0 for the initial ClientHello. |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1028 | // |
| 1029 | // TLS 1.3 fixes the version number in the record |
| 1030 | // layer to {3, 1}. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1031 | vers = VersionTLS10 |
| 1032 | } |
| 1033 | b.data[1] = byte(vers >> 8) |
| 1034 | b.data[2] = byte(vers) |
| 1035 | b.data[3] = byte(m >> 8) |
| 1036 | b.data[4] = byte(m) |
| 1037 | if explicitIVLen > 0 { |
| 1038 | explicitIV := b.data[recordHeaderLen : recordHeaderLen+explicitIVLen] |
| 1039 | if explicitIVIsSeq { |
| 1040 | copy(explicitIV, c.out.seq[:]) |
| 1041 | } else { |
| 1042 | if _, err = io.ReadFull(c.config.rand(), explicitIV); err != nil { |
| 1043 | break |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | copy(b.data[recordHeaderLen+explicitIVLen:], data) |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1048 | c.out.encrypt(b, explicitIVLen, typ) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1049 | _, err = c.conn.Write(b.data) |
| 1050 | if err != nil { |
| 1051 | break |
| 1052 | } |
| 1053 | n += m |
| 1054 | data = data[m:] |
| 1055 | } |
| 1056 | c.out.freeBlock(b) |
| 1057 | |
| 1058 | if typ == recordTypeChangeCipherSpec { |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1059 | err = c.out.changeCipherSpec(c.config) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1060 | if err != nil { |
| 1061 | // Cannot call sendAlert directly, |
| 1062 | // because we already hold c.out.Mutex. |
| 1063 | c.tmp[0] = alertLevelError |
| 1064 | c.tmp[1] = byte(err.(alert)) |
| 1065 | c.writeRecord(recordTypeAlert, c.tmp[0:2]) |
| 1066 | return n, c.out.setErrorLocked(&net.OpError{Op: "local error", Err: err}) |
| 1067 | } |
| 1068 | } |
| 1069 | return |
| 1070 | } |
| 1071 | |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1072 | func (c *Conn) flushHandshake() error { |
| 1073 | if c.isDTLS { |
| 1074 | return c.dtlsFlushHandshake() |
| 1075 | } |
| 1076 | |
| 1077 | for c.pendingFlight.Len() > 0 { |
| 1078 | var buf [maxPlaintext]byte |
| 1079 | n, _ := c.pendingFlight.Read(buf[:]) |
| 1080 | if _, err := c.doWriteRecord(recordTypeHandshake, buf[:n]); err != nil { |
| 1081 | return err |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | c.pendingFlight.Reset() |
| 1086 | return nil |
| 1087 | } |
| 1088 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1089 | func (c *Conn) doReadHandshake() ([]byte, error) { |
| 1090 | if c.isDTLS { |
| 1091 | return c.dtlsDoReadHandshake() |
| 1092 | } |
| 1093 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1094 | for c.hand.Len() < 4 { |
| 1095 | if err := c.in.err; err != nil { |
| 1096 | return nil, err |
| 1097 | } |
| 1098 | if err := c.readRecord(recordTypeHandshake); err != nil { |
| 1099 | return nil, err |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | data := c.hand.Bytes() |
| 1104 | n := int(data[1])<<16 | int(data[2])<<8 | int(data[3]) |
| 1105 | if n > maxHandshake { |
| 1106 | return nil, c.in.setErrorLocked(c.sendAlert(alertInternalError)) |
| 1107 | } |
| 1108 | for c.hand.Len() < 4+n { |
| 1109 | if err := c.in.err; err != nil { |
| 1110 | return nil, err |
| 1111 | } |
| 1112 | if err := c.readRecord(recordTypeHandshake); err != nil { |
| 1113 | return nil, err |
| 1114 | } |
| 1115 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1116 | return c.hand.Next(4 + n), nil |
| 1117 | } |
| 1118 | |
| 1119 | // readHandshake reads the next handshake message from |
| 1120 | // the record layer. |
| 1121 | // c.in.Mutex < L; c.out.Mutex < L. |
| 1122 | func (c *Conn) readHandshake() (interface{}, error) { |
| 1123 | data, err := c.doReadHandshake() |
| 1124 | if err != nil { |
| 1125 | return nil, err |
| 1126 | } |
| 1127 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1128 | var m handshakeMessage |
| 1129 | switch data[0] { |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1130 | case typeHelloRequest: |
| 1131 | m = new(helloRequestMsg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1132 | case typeClientHello: |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1133 | m = &clientHelloMsg{ |
| 1134 | isDTLS: c.isDTLS, |
| 1135 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1136 | case typeServerHello: |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1137 | m = &serverHelloMsg{ |
| 1138 | isDTLS: c.isDTLS, |
| 1139 | } |
Nick Harper | dcfbc67 | 2016-07-16 17:47:31 +0200 | [diff] [blame^] | 1140 | case typeHelloRetryRequest: |
| 1141 | m = new(helloRetryRequestMsg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1142 | case typeNewSessionTicket: |
| 1143 | m = new(newSessionTicketMsg) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 1144 | case typeEncryptedExtensions: |
| 1145 | m = new(encryptedExtensionsMsg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1146 | case typeCertificate: |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 1147 | m = &certificateMsg{ |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 1148 | hasRequestContext: c.vers >= VersionTLS13, |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 1149 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1150 | case typeCertificateRequest: |
| 1151 | m = &certificateRequestMsg{ |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1152 | hasSignatureAlgorithm: c.vers >= VersionTLS12, |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 1153 | hasRequestContext: c.vers >= VersionTLS13, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1154 | } |
| 1155 | case typeCertificateStatus: |
| 1156 | m = new(certificateStatusMsg) |
| 1157 | case typeServerKeyExchange: |
| 1158 | m = new(serverKeyExchangeMsg) |
| 1159 | case typeServerHelloDone: |
| 1160 | m = new(serverHelloDoneMsg) |
| 1161 | case typeClientKeyExchange: |
| 1162 | m = new(clientKeyExchangeMsg) |
| 1163 | case typeCertificateVerify: |
| 1164 | m = &certificateVerifyMsg{ |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1165 | hasSignatureAlgorithm: c.vers >= VersionTLS12, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1166 | } |
| 1167 | case typeNextProtocol: |
| 1168 | m = new(nextProtoMsg) |
| 1169 | case typeFinished: |
| 1170 | m = new(finishedMsg) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1171 | case typeHelloVerifyRequest: |
| 1172 | m = new(helloVerifyRequestMsg) |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1173 | case typeChannelID: |
| 1174 | m = new(channelIDMsg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1175 | default: |
| 1176 | return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage)) |
| 1177 | } |
| 1178 | |
| 1179 | // The handshake message unmarshallers |
| 1180 | // expect to be able to keep references to data, |
| 1181 | // so pass in a fresh copy that won't be overwritten. |
| 1182 | data = append([]byte(nil), data...) |
| 1183 | |
| 1184 | if !m.unmarshal(data) { |
| 1185 | return nil, c.in.setErrorLocked(c.sendAlert(alertUnexpectedMessage)) |
| 1186 | } |
| 1187 | return m, nil |
| 1188 | } |
| 1189 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1190 | // skipPacket processes all the DTLS records in packet. It updates |
| 1191 | // sequence number expectations but otherwise ignores them. |
| 1192 | func (c *Conn) skipPacket(packet []byte) error { |
| 1193 | for len(packet) > 0 { |
David Benjamin | 6ca9355 | 2015-08-28 16:16:25 -0400 | [diff] [blame] | 1194 | if len(packet) < 13 { |
| 1195 | return errors.New("tls: bad packet") |
| 1196 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1197 | // Dropped packets are completely ignored save to update |
| 1198 | // expected sequence numbers for this and the next epoch. (We |
| 1199 | // don't assert on the contents of the packets both for |
| 1200 | // simplicity and because a previous test with one shorter |
| 1201 | // timeout schedule would have done so.) |
| 1202 | epoch := packet[3:5] |
| 1203 | seq := packet[5:11] |
| 1204 | length := uint16(packet[11])<<8 | uint16(packet[12]) |
| 1205 | if bytes.Equal(c.in.seq[:2], epoch) { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1206 | if bytes.Compare(seq, c.in.seq[2:]) < 0 { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1207 | return errors.New("tls: sequence mismatch") |
| 1208 | } |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1209 | copy(c.in.seq[2:], seq) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1210 | c.in.incSeq(false) |
| 1211 | } else { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1212 | if bytes.Compare(seq, c.in.nextSeq[:]) < 0 { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1213 | return errors.New("tls: sequence mismatch") |
| 1214 | } |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1215 | copy(c.in.nextSeq[:], seq) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1216 | c.in.incNextSeq() |
| 1217 | } |
David Benjamin | 6ca9355 | 2015-08-28 16:16:25 -0400 | [diff] [blame] | 1218 | if len(packet) < 13+int(length) { |
| 1219 | return errors.New("tls: bad packet") |
| 1220 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1221 | packet = packet[13+length:] |
| 1222 | } |
| 1223 | return nil |
| 1224 | } |
| 1225 | |
| 1226 | // simulatePacketLoss simulates the loss of a handshake leg from the |
| 1227 | // peer based on the schedule in c.config.Bugs. If resendFunc is |
| 1228 | // non-nil, it is called after each simulated timeout to retransmit |
| 1229 | // handshake messages from the local end. This is used in cases where |
| 1230 | // the peer retransmits on a stale Finished rather than a timeout. |
| 1231 | func (c *Conn) simulatePacketLoss(resendFunc func()) error { |
| 1232 | if len(c.config.Bugs.TimeoutSchedule) == 0 { |
| 1233 | return nil |
| 1234 | } |
| 1235 | if !c.isDTLS { |
| 1236 | return errors.New("tls: TimeoutSchedule may only be set in DTLS") |
| 1237 | } |
| 1238 | if c.config.Bugs.PacketAdaptor == nil { |
| 1239 | return errors.New("tls: TimeoutSchedule set without PacketAdapter") |
| 1240 | } |
| 1241 | for _, timeout := range c.config.Bugs.TimeoutSchedule { |
| 1242 | // Simulate a timeout. |
| 1243 | packets, err := c.config.Bugs.PacketAdaptor.SendReadTimeout(timeout) |
| 1244 | if err != nil { |
| 1245 | return err |
| 1246 | } |
| 1247 | for _, packet := range packets { |
| 1248 | if err := c.skipPacket(packet); err != nil { |
| 1249 | return err |
| 1250 | } |
| 1251 | } |
| 1252 | if resendFunc != nil { |
| 1253 | resendFunc() |
| 1254 | } |
| 1255 | } |
| 1256 | return nil |
| 1257 | } |
| 1258 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1259 | // Write writes data to the connection. |
| 1260 | func (c *Conn) Write(b []byte) (int, error) { |
| 1261 | if err := c.Handshake(); err != nil { |
| 1262 | return 0, err |
| 1263 | } |
| 1264 | |
| 1265 | c.out.Lock() |
| 1266 | defer c.out.Unlock() |
| 1267 | |
| 1268 | if err := c.out.err; err != nil { |
| 1269 | return 0, err |
| 1270 | } |
| 1271 | |
| 1272 | if !c.handshakeComplete { |
| 1273 | return 0, alertInternalError |
| 1274 | } |
| 1275 | |
David Benjamin | 3fd1fbd | 2015-02-03 16:07:32 -0500 | [diff] [blame] | 1276 | if c.config.Bugs.SendSpuriousAlert != 0 { |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 1277 | c.sendAlertLocked(alertLevelError, c.config.Bugs.SendSpuriousAlert) |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 1278 | } |
| 1279 | |
Adam Langley | 27a0d08 | 2015-11-03 13:34:10 -0800 | [diff] [blame] | 1280 | if c.config.Bugs.SendHelloRequestBeforeEveryAppDataRecord { |
| 1281 | c.writeRecord(recordTypeHandshake, []byte{typeHelloRequest, 0, 0, 0}) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1282 | c.flushHandshake() |
Adam Langley | 27a0d08 | 2015-11-03 13:34:10 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1285 | // SSL 3.0 and TLS 1.0 are susceptible to a chosen-plaintext |
| 1286 | // attack when using block mode ciphers due to predictable IVs. |
| 1287 | // This can be prevented by splitting each Application Data |
| 1288 | // record into two records, effectively randomizing the IV. |
| 1289 | // |
| 1290 | // http://www.openssl.org/~bodo/tls-cbc.txt |
| 1291 | // https://bugzilla.mozilla.org/show_bug.cgi?id=665814 |
| 1292 | // http://www.imperialviolet.org/2012/01/15/beastfollowup.html |
| 1293 | |
| 1294 | var m int |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1295 | if len(b) > 1 && c.vers <= VersionTLS10 && !c.isDTLS { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1296 | if _, ok := c.out.cipher.(cipher.BlockMode); ok { |
| 1297 | n, err := c.writeRecord(recordTypeApplicationData, b[:1]) |
| 1298 | if err != nil { |
| 1299 | return n, c.out.setErrorLocked(err) |
| 1300 | } |
| 1301 | m, b = 1, b[1:] |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | n, err := c.writeRecord(recordTypeApplicationData, b) |
| 1306 | return n + m, c.out.setErrorLocked(err) |
| 1307 | } |
| 1308 | |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1309 | func (c *Conn) handleRenegotiation() error { |
| 1310 | c.handshakeComplete = false |
| 1311 | if !c.isClient { |
| 1312 | panic("renegotiation should only happen for a client") |
| 1313 | } |
| 1314 | |
| 1315 | msg, err := c.readHandshake() |
| 1316 | if err != nil { |
| 1317 | return err |
| 1318 | } |
| 1319 | _, ok := msg.(*helloRequestMsg) |
| 1320 | if !ok { |
| 1321 | c.sendAlert(alertUnexpectedMessage) |
| 1322 | return alertUnexpectedMessage |
| 1323 | } |
| 1324 | |
| 1325 | return c.Handshake() |
| 1326 | } |
| 1327 | |
Adam Langley | cf2d4f4 | 2014-10-28 19:06:14 -0700 | [diff] [blame] | 1328 | func (c *Conn) Renegotiate() error { |
| 1329 | if !c.isClient { |
David Benjamin | ef5dfd2 | 2015-12-06 13:17:07 -0500 | [diff] [blame] | 1330 | helloReq := new(helloRequestMsg).marshal() |
| 1331 | if c.config.Bugs.BadHelloRequest != nil { |
| 1332 | helloReq = c.config.Bugs.BadHelloRequest |
| 1333 | } |
| 1334 | c.writeRecord(recordTypeHandshake, helloReq) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1335 | c.flushHandshake() |
Adam Langley | cf2d4f4 | 2014-10-28 19:06:14 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | c.handshakeComplete = false |
| 1339 | return c.Handshake() |
| 1340 | } |
| 1341 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1342 | // Read can be made to time out and return a net.Error with Timeout() == true |
| 1343 | // after a fixed time limit; see SetDeadline and SetReadDeadline. |
| 1344 | func (c *Conn) Read(b []byte) (n int, err error) { |
| 1345 | if err = c.Handshake(); err != nil { |
| 1346 | return |
| 1347 | } |
| 1348 | |
| 1349 | c.in.Lock() |
| 1350 | defer c.in.Unlock() |
| 1351 | |
| 1352 | // Some OpenSSL servers send empty records in order to randomize the |
| 1353 | // CBC IV. So this loop ignores a limited number of empty records. |
| 1354 | const maxConsecutiveEmptyRecords = 100 |
| 1355 | for emptyRecordCount := 0; emptyRecordCount <= maxConsecutiveEmptyRecords; emptyRecordCount++ { |
| 1356 | for c.input == nil && c.in.err == nil { |
| 1357 | if err := c.readRecord(recordTypeApplicationData); err != nil { |
| 1358 | // Soft error, like EAGAIN |
| 1359 | return 0, err |
| 1360 | } |
David Benjamin | d9b091b | 2015-01-27 01:10:54 -0500 | [diff] [blame] | 1361 | if c.hand.Len() > 0 { |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1362 | // We received handshake bytes, indicating the |
David Benjamin | d9b091b | 2015-01-27 01:10:54 -0500 | [diff] [blame] | 1363 | // start of a renegotiation. |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1364 | if err := c.handleRenegotiation(); err != nil { |
| 1365 | return 0, err |
| 1366 | } |
| 1367 | continue |
| 1368 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1369 | } |
| 1370 | if err := c.in.err; err != nil { |
| 1371 | return 0, err |
| 1372 | } |
| 1373 | |
| 1374 | n, err = c.input.Read(b) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1375 | if c.input.off >= len(c.input.data) || c.isDTLS { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1376 | c.in.freeBlock(c.input) |
| 1377 | c.input = nil |
| 1378 | } |
| 1379 | |
| 1380 | // If a close-notify alert is waiting, read it so that |
| 1381 | // we can return (n, EOF) instead of (n, nil), to signal |
| 1382 | // to the HTTP response reading goroutine that the |
| 1383 | // connection is now closed. This eliminates a race |
| 1384 | // where the HTTP response reading goroutine would |
| 1385 | // otherwise not observe the EOF until its next read, |
| 1386 | // by which time a client goroutine might have already |
| 1387 | // tried to reuse the HTTP connection for a new |
| 1388 | // request. |
| 1389 | // See https://codereview.appspot.com/76400046 |
| 1390 | // and http://golang.org/issue/3514 |
| 1391 | if ri := c.rawInput; ri != nil && |
| 1392 | n != 0 && err == nil && |
| 1393 | c.input == nil && len(ri.data) > 0 && recordType(ri.data[0]) == recordTypeAlert { |
| 1394 | if recErr := c.readRecord(recordTypeApplicationData); recErr != nil { |
| 1395 | err = recErr // will be io.EOF on closeNotify |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | if n != 0 || err != nil { |
| 1400 | return n, err |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | return 0, io.ErrNoProgress |
| 1405 | } |
| 1406 | |
| 1407 | // Close closes the connection. |
| 1408 | func (c *Conn) Close() error { |
| 1409 | var alertErr error |
| 1410 | |
| 1411 | c.handshakeMutex.Lock() |
| 1412 | defer c.handshakeMutex.Unlock() |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1413 | if c.handshakeComplete && !c.config.Bugs.NoCloseNotify { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1414 | alert := alertCloseNotify |
| 1415 | if c.config.Bugs.SendAlertOnShutdown != 0 { |
| 1416 | alert = c.config.Bugs.SendAlertOnShutdown |
| 1417 | } |
| 1418 | alertErr = c.sendAlert(alert) |
David Benjamin | 4d55961 | 2016-05-18 14:31:51 -0400 | [diff] [blame] | 1419 | // Clear local alerts when sending alerts so we continue to wait |
| 1420 | // for the peer rather than closing the socket early. |
| 1421 | if opErr, ok := alertErr.(*net.OpError); ok && opErr.Op == "local error" { |
| 1422 | alertErr = nil |
| 1423 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1426 | // Consume a close_notify from the peer if one hasn't been received |
| 1427 | // already. This avoids the peer from failing |SSL_shutdown| due to a |
| 1428 | // write failing. |
| 1429 | if c.handshakeComplete && alertErr == nil && c.config.Bugs.ExpectCloseNotify { |
| 1430 | for c.in.error() == nil { |
| 1431 | c.readRecord(recordTypeAlert) |
| 1432 | } |
| 1433 | if c.in.error() != io.EOF { |
| 1434 | alertErr = c.in.error() |
| 1435 | } |
| 1436 | } |
| 1437 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1438 | if err := c.conn.Close(); err != nil { |
| 1439 | return err |
| 1440 | } |
| 1441 | return alertErr |
| 1442 | } |
| 1443 | |
| 1444 | // Handshake runs the client or server handshake |
| 1445 | // protocol if it has not yet been run. |
| 1446 | // Most uses of this package need not call Handshake |
| 1447 | // explicitly: the first Read or Write will call it automatically. |
| 1448 | func (c *Conn) Handshake() error { |
| 1449 | c.handshakeMutex.Lock() |
| 1450 | defer c.handshakeMutex.Unlock() |
| 1451 | if err := c.handshakeErr; err != nil { |
| 1452 | return err |
| 1453 | } |
| 1454 | if c.handshakeComplete { |
| 1455 | return nil |
| 1456 | } |
| 1457 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1458 | if c.isDTLS && c.config.Bugs.SendSplitAlert { |
| 1459 | c.conn.Write([]byte{ |
| 1460 | byte(recordTypeAlert), // type |
| 1461 | 0xfe, 0xff, // version |
| 1462 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // sequence |
| 1463 | 0x0, 0x2, // length |
| 1464 | }) |
| 1465 | c.conn.Write([]byte{alertLevelError, byte(alertInternalError)}) |
| 1466 | } |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 1467 | if data := c.config.Bugs.AppDataBeforeHandshake; data != nil { |
| 1468 | c.writeRecord(recordTypeApplicationData, data) |
| 1469 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1470 | if c.isClient { |
| 1471 | c.handshakeErr = c.clientHandshake() |
| 1472 | } else { |
| 1473 | c.handshakeErr = c.serverHandshake() |
| 1474 | } |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 1475 | if c.handshakeErr == nil && c.config.Bugs.SendInvalidRecordType { |
| 1476 | c.writeRecord(recordType(42), []byte("invalid record")) |
| 1477 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1478 | return c.handshakeErr |
| 1479 | } |
| 1480 | |
| 1481 | // ConnectionState returns basic TLS details about the connection. |
| 1482 | func (c *Conn) ConnectionState() ConnectionState { |
| 1483 | c.handshakeMutex.Lock() |
| 1484 | defer c.handshakeMutex.Unlock() |
| 1485 | |
| 1486 | var state ConnectionState |
| 1487 | state.HandshakeComplete = c.handshakeComplete |
| 1488 | if c.handshakeComplete { |
| 1489 | state.Version = c.vers |
| 1490 | state.NegotiatedProtocol = c.clientProtocol |
| 1491 | state.DidResume = c.didResume |
| 1492 | state.NegotiatedProtocolIsMutual = !c.clientProtocolFallback |
David Benjamin | fc7b086 | 2014-09-06 13:21:53 -0400 | [diff] [blame] | 1493 | state.NegotiatedProtocolFromALPN = c.usedALPN |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1494 | state.CipherSuite = c.cipherSuite.id |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1495 | state.PeerCertificates = c.peerCertificates |
| 1496 | state.VerifiedChains = c.verifiedChains |
| 1497 | state.ServerName = c.serverName |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1498 | state.ChannelID = c.channelID |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 1499 | state.SRTPProtectionProfile = c.srtpProtectionProfile |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1500 | state.TLSUnique = c.firstFinished[:] |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1501 | state.SCTList = c.sctList |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1502 | state.PeerSignatureAlgorithm = c.peerSignatureAlgorithm |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | return state |
| 1506 | } |
| 1507 | |
| 1508 | // OCSPResponse returns the stapled OCSP response from the TLS server, if |
| 1509 | // any. (Only valid for client connections.) |
| 1510 | func (c *Conn) OCSPResponse() []byte { |
| 1511 | c.handshakeMutex.Lock() |
| 1512 | defer c.handshakeMutex.Unlock() |
| 1513 | |
| 1514 | return c.ocspResponse |
| 1515 | } |
| 1516 | |
| 1517 | // VerifyHostname checks that the peer certificate chain is valid for |
| 1518 | // connecting to host. If so, it returns nil; if not, it returns an error |
| 1519 | // describing the problem. |
| 1520 | func (c *Conn) VerifyHostname(host string) error { |
| 1521 | c.handshakeMutex.Lock() |
| 1522 | defer c.handshakeMutex.Unlock() |
| 1523 | if !c.isClient { |
| 1524 | return errors.New("tls: VerifyHostname called on TLS server connection") |
| 1525 | } |
| 1526 | if !c.handshakeComplete { |
| 1527 | return errors.New("tls: handshake has not yet been performed") |
| 1528 | } |
| 1529 | return c.peerCertificates[0].VerifyHostname(host) |
| 1530 | } |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1531 | |
| 1532 | // ExportKeyingMaterial exports keying material from the current connection |
| 1533 | // state, as per RFC 5705. |
| 1534 | func (c *Conn) ExportKeyingMaterial(length int, label, context []byte, useContext bool) ([]byte, error) { |
| 1535 | c.handshakeMutex.Lock() |
| 1536 | defer c.handshakeMutex.Unlock() |
| 1537 | if !c.handshakeComplete { |
| 1538 | return nil, errors.New("tls: handshake has not yet been performed") |
| 1539 | } |
| 1540 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 1541 | if c.vers >= VersionTLS13 { |
David Benjamin | 97a0a08 | 2016-07-13 17:57:35 -0400 | [diff] [blame] | 1542 | // TODO(davidben): What should we do with useContext? See |
| 1543 | // https://github.com/tlswg/tls13-spec/issues/546 |
| 1544 | return hkdfExpandLabel(c.cipherSuite.hash(), c.exporterSecret, label, context, length), nil |
| 1545 | } |
| 1546 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1547 | seedLen := len(c.clientRandom) + len(c.serverRandom) |
| 1548 | if useContext { |
| 1549 | seedLen += 2 + len(context) |
| 1550 | } |
| 1551 | seed := make([]byte, 0, seedLen) |
| 1552 | seed = append(seed, c.clientRandom[:]...) |
| 1553 | seed = append(seed, c.serverRandom[:]...) |
| 1554 | if useContext { |
| 1555 | seed = append(seed, byte(len(context)>>8), byte(len(context))) |
| 1556 | seed = append(seed, context...) |
| 1557 | } |
| 1558 | result := make([]byte, length) |
David Benjamin | 97a0a08 | 2016-07-13 17:57:35 -0400 | [diff] [blame] | 1559 | prfForVersion(c.vers, c.cipherSuite)(result, c.exporterSecret, label, seed) |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1560 | return result, nil |
| 1561 | } |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 1562 | |
| 1563 | // noRenegotiationInfo returns true if the renegotiation info extension |
| 1564 | // should be supported in the current handshake. |
| 1565 | func (c *Conn) noRenegotiationInfo() bool { |
| 1566 | if c.config.Bugs.NoRenegotiationInfo { |
| 1567 | return true |
| 1568 | } |
| 1569 | if c.cipherSuite == nil && c.config.Bugs.NoRenegotiationInfoInInitial { |
| 1570 | return true |
| 1571 | } |
| 1572 | if c.cipherSuite != nil && c.config.Bugs.NoRenegotiationInfoAfterInitial { |
| 1573 | return true |
| 1574 | } |
| 1575 | return false |
| 1576 | } |