Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | // Copyright 2009 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 | package main |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "crypto/ecdsa" |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 10 | "crypto/elliptic" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 11 | "crypto/rsa" |
| 12 | "crypto/subtle" |
| 13 | "crypto/x509" |
| 14 | "encoding/asn1" |
| 15 | "errors" |
| 16 | "fmt" |
| 17 | "io" |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 18 | "math/big" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 19 | "net" |
| 20 | "strconv" |
| 21 | ) |
| 22 | |
| 23 | type clientHandshakeState struct { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 24 | c *Conn |
| 25 | serverHello *serverHelloMsg |
| 26 | hello *clientHelloMsg |
| 27 | suite *cipherSuite |
| 28 | finishedHash finishedHash |
| 29 | masterSecret []byte |
| 30 | session *ClientSessionState |
| 31 | finishedBytes []byte |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func (c *Conn) clientHandshake() error { |
| 35 | if c.config == nil { |
| 36 | c.config = defaultConfig() |
| 37 | } |
| 38 | |
| 39 | if len(c.config.ServerName) == 0 && !c.config.InsecureSkipVerify { |
| 40 | return errors.New("tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config") |
| 41 | } |
| 42 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 43 | c.sendHandshakeSeq = 0 |
| 44 | c.recvHandshakeSeq = 0 |
| 45 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 46 | nextProtosLength := 0 |
| 47 | for _, proto := range c.config.NextProtos { |
Adam Langley | efb0e16 | 2015-07-09 11:35:04 -0700 | [diff] [blame] | 48 | if l := len(proto); l > 255 { |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 49 | return errors.New("tls: invalid NextProtos value") |
| 50 | } else { |
| 51 | nextProtosLength += 1 + l |
| 52 | } |
| 53 | } |
| 54 | if nextProtosLength > 0xffff { |
| 55 | return errors.New("tls: NextProtos values too large") |
| 56 | } |
| 57 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | hello := &clientHelloMsg{ |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 59 | isDTLS: c.isDTLS, |
| 60 | vers: c.config.maxVersion(), |
| 61 | compressionMethods: []uint8{compressionNone}, |
| 62 | random: make([]byte, 32), |
| 63 | ocspStapling: true, |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 64 | sctListSupported: true, |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 65 | serverName: c.config.ServerName, |
| 66 | supportedCurves: c.config.curvePreferences(), |
| 67 | supportedPoints: []uint8{pointFormatUncompressed}, |
| 68 | nextProtoNeg: len(c.config.NextProtos) > 0, |
| 69 | secureRenegotiation: []byte{}, |
| 70 | alpnProtocols: c.config.NextProtos, |
| 71 | duplicateExtension: c.config.Bugs.DuplicateExtension, |
| 72 | channelIDSupported: c.config.ChannelID != nil, |
| 73 | npnLast: c.config.Bugs.SwapNPNAndALPN, |
| 74 | extendedMasterSecret: c.config.maxVersion() >= VersionTLS10, |
| 75 | srtpProtectionProfiles: c.config.SRTPProtectionProfiles, |
| 76 | srtpMasterKeyIdentifier: c.config.Bugs.SRTPMasterKeyIdentifer, |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 77 | customExtension: c.config.Bugs.CustomExtension, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 80 | if c.config.Bugs.SendClientVersion != 0 { |
| 81 | hello.vers = c.config.Bugs.SendClientVersion |
| 82 | } |
| 83 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 84 | if c.config.Bugs.NoExtendedMasterSecret { |
| 85 | hello.extendedMasterSecret = false |
| 86 | } |
| 87 | |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 88 | if c.config.Bugs.NoSupportedCurves { |
| 89 | hello.supportedCurves = nil |
| 90 | } |
| 91 | |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 92 | if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 93 | if c.config.Bugs.BadRenegotiationInfo { |
| 94 | hello.secureRenegotiation = append(hello.secureRenegotiation, c.clientVerify...) |
| 95 | hello.secureRenegotiation[0] ^= 0x80 |
| 96 | } else { |
| 97 | hello.secureRenegotiation = c.clientVerify |
| 98 | } |
| 99 | } |
| 100 | |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 101 | if c.config.Bugs.NoRenegotiationInfo { |
| 102 | hello.secureRenegotiation = nil |
| 103 | } |
| 104 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 105 | possibleCipherSuites := c.config.cipherSuites() |
| 106 | hello.cipherSuites = make([]uint16, 0, len(possibleCipherSuites)) |
| 107 | |
| 108 | NextCipherSuite: |
| 109 | for _, suiteId := range possibleCipherSuites { |
| 110 | for _, suite := range cipherSuites { |
| 111 | if suite.id != suiteId { |
| 112 | continue |
| 113 | } |
| 114 | // Don't advertise TLS 1.2-only cipher suites unless |
| 115 | // we're attempting TLS 1.2. |
| 116 | if hello.vers < VersionTLS12 && suite.flags&suiteTLS12 != 0 { |
| 117 | continue |
| 118 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 119 | // Don't advertise non-DTLS cipher suites on DTLS. |
David Benjamin | 0fa4012 | 2015-05-30 17:13:12 -0400 | [diff] [blame] | 120 | if c.isDTLS && suite.flags&suiteNoDTLS != 0 && !c.config.Bugs.EnableAllCiphersInDTLS { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 121 | continue |
| 122 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | hello.cipherSuites = append(hello.cipherSuites, suiteId) |
| 124 | continue NextCipherSuite |
| 125 | } |
| 126 | } |
| 127 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 128 | if c.config.Bugs.SendRenegotiationSCSV { |
| 129 | hello.cipherSuites = append(hello.cipherSuites, renegotiationSCSV) |
| 130 | } |
| 131 | |
David Benjamin | bef270a | 2014-08-02 04:22:02 -0400 | [diff] [blame] | 132 | if c.config.Bugs.SendFallbackSCSV { |
| 133 | hello.cipherSuites = append(hello.cipherSuites, fallbackSCSV) |
| 134 | } |
| 135 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | _, err := io.ReadFull(c.config.rand(), hello.random) |
| 137 | if err != nil { |
| 138 | c.sendAlert(alertInternalError) |
| 139 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 140 | } |
| 141 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 142 | if hello.vers >= VersionTLS12 && !c.config.Bugs.NoSignatureAndHashes { |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 143 | hello.signatureAndHashes = c.config.signatureAndHashesForClient() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | var session *ClientSessionState |
| 147 | var cacheKey string |
| 148 | sessionCache := c.config.ClientSessionCache |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
| 150 | if sessionCache != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 151 | hello.ticketSupported = !c.config.SessionTicketsDisabled |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | |
| 153 | // Try to resume a previously negotiated TLS session, if |
| 154 | // available. |
| 155 | cacheKey = clientSessionCacheKey(c.conn.RemoteAddr(), c.config) |
| 156 | candidateSession, ok := sessionCache.Get(cacheKey) |
| 157 | if ok { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 158 | ticketOk := !c.config.SessionTicketsDisabled || candidateSession.sessionTicket == nil |
| 159 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | // Check that the ciphersuite/version used for the |
| 161 | // previous session are still valid. |
| 162 | cipherSuiteOk := false |
| 163 | for _, id := range hello.cipherSuites { |
| 164 | if id == candidateSession.cipherSuite { |
| 165 | cipherSuiteOk = true |
| 166 | break |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | versOk := candidateSession.vers >= c.config.minVersion() && |
| 171 | candidateSession.vers <= c.config.maxVersion() |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 172 | if ticketOk && versOk && cipherSuiteOk { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | session = candidateSession |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if session != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 179 | if session.sessionTicket != nil { |
| 180 | hello.sessionTicket = session.sessionTicket |
| 181 | if c.config.Bugs.CorruptTicket { |
| 182 | hello.sessionTicket = make([]byte, len(session.sessionTicket)) |
| 183 | copy(hello.sessionTicket, session.sessionTicket) |
| 184 | if len(hello.sessionTicket) > 0 { |
| 185 | offset := 40 |
| 186 | if offset > len(hello.sessionTicket) { |
| 187 | offset = len(hello.sessionTicket) - 1 |
| 188 | } |
| 189 | hello.sessionTicket[offset] ^= 0x40 |
Adam Langley | 3831173 | 2014-10-16 19:04:35 -0700 | [diff] [blame] | 190 | } |
Adam Langley | 3831173 | 2014-10-16 19:04:35 -0700 | [diff] [blame] | 191 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 192 | // A random session ID is used to detect when the |
| 193 | // server accepted the ticket and is resuming a session |
| 194 | // (see RFC 5077). |
| 195 | sessionIdLen := 16 |
| 196 | if c.config.Bugs.OversizedSessionId { |
| 197 | sessionIdLen = 33 |
| 198 | } |
| 199 | hello.sessionId = make([]byte, sessionIdLen) |
| 200 | if _, err := io.ReadFull(c.config.rand(), hello.sessionId); err != nil { |
| 201 | c.sendAlert(alertInternalError) |
| 202 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 203 | } |
| 204 | } else { |
| 205 | hello.sessionId = session.sessionId |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 209 | var helloBytes []byte |
| 210 | if c.config.Bugs.SendV2ClientHello { |
David Benjamin | 94d701b | 2014-11-30 13:54:41 -0500 | [diff] [blame] | 211 | // Test that the peer left-pads random. |
| 212 | hello.random[0] = 0 |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 213 | v2Hello := &v2ClientHelloMsg{ |
| 214 | vers: hello.vers, |
| 215 | cipherSuites: hello.cipherSuites, |
| 216 | // No session resumption for V2ClientHello. |
| 217 | sessionId: nil, |
David Benjamin | 94d701b | 2014-11-30 13:54:41 -0500 | [diff] [blame] | 218 | challenge: hello.random[1:], |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 219 | } |
| 220 | helloBytes = v2Hello.marshal() |
| 221 | c.writeV2Record(helloBytes) |
| 222 | } else { |
| 223 | helloBytes = hello.marshal() |
| 224 | c.writeRecord(recordTypeHandshake, helloBytes) |
| 225 | } |
David Benjamin | a4e6d48 | 2015-03-02 19:10:53 -0500 | [diff] [blame] | 226 | c.dtlsFlushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 228 | if err := c.simulatePacketLoss(nil); err != nil { |
| 229 | return err |
| 230 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | msg, err := c.readHandshake() |
| 232 | if err != nil { |
| 233 | return err |
| 234 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 235 | |
| 236 | if c.isDTLS { |
| 237 | helloVerifyRequest, ok := msg.(*helloVerifyRequestMsg) |
| 238 | if ok { |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 239 | if helloVerifyRequest.vers != VersionTLS10 { |
| 240 | // Per RFC 6347, the version field in |
| 241 | // HelloVerifyRequest SHOULD be always DTLS |
| 242 | // 1.0. Enforce this for testing purposes. |
| 243 | return errors.New("dtls: bad HelloVerifyRequest version") |
| 244 | } |
| 245 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 246 | hello.raw = nil |
| 247 | hello.cookie = helloVerifyRequest.cookie |
| 248 | helloBytes = hello.marshal() |
| 249 | c.writeRecord(recordTypeHandshake, helloBytes) |
David Benjamin | a4e6d48 | 2015-03-02 19:10:53 -0500 | [diff] [blame] | 250 | c.dtlsFlushHandshake() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 251 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 252 | if err := c.simulatePacketLoss(nil); err != nil { |
| 253 | return err |
| 254 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 255 | msg, err = c.readHandshake() |
| 256 | if err != nil { |
| 257 | return err |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | serverHello, ok := msg.(*serverHelloMsg) |
| 263 | if !ok { |
| 264 | c.sendAlert(alertUnexpectedMessage) |
| 265 | return unexpectedMessageError(serverHello, msg) |
| 266 | } |
| 267 | |
David Benjamin | 76d8abe | 2014-08-14 16:25:34 -0400 | [diff] [blame] | 268 | c.vers, ok = c.config.mutualVersion(serverHello.vers) |
| 269 | if !ok { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | c.sendAlert(alertProtocolVersion) |
| 271 | return fmt.Errorf("tls: server selected unsupported protocol version %x", serverHello.vers) |
| 272 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 273 | c.haveVers = true |
| 274 | |
| 275 | suite := mutualCipherSuite(c.config.cipherSuites(), serverHello.cipherSuite) |
| 276 | if suite == nil { |
| 277 | c.sendAlert(alertHandshakeFailure) |
| 278 | return fmt.Errorf("tls: server selected an unsupported cipher suite") |
| 279 | } |
| 280 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 281 | if c.config.Bugs.RequireRenegotiationInfo && serverHello.secureRenegotiation == nil { |
| 282 | return errors.New("tls: renegotiation extension missing") |
| 283 | } |
| 284 | |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 285 | if len(c.clientVerify) > 0 && !c.config.Bugs.NoRenegotiationInfo { |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 286 | var expectedRenegInfo []byte |
| 287 | expectedRenegInfo = append(expectedRenegInfo, c.clientVerify...) |
| 288 | expectedRenegInfo = append(expectedRenegInfo, c.serverVerify...) |
| 289 | if !bytes.Equal(serverHello.secureRenegotiation, expectedRenegInfo) { |
| 290 | c.sendAlert(alertHandshakeFailure) |
| 291 | return fmt.Errorf("tls: renegotiation mismatch") |
| 292 | } |
| 293 | } |
| 294 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 295 | if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 296 | if serverHello.customExtension != *expected { |
| 297 | return fmt.Errorf("tls: bad custom extension contents %q", serverHello.customExtension) |
| 298 | } |
| 299 | } |
| 300 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 301 | hs := &clientHandshakeState{ |
| 302 | c: c, |
| 303 | serverHello: serverHello, |
| 304 | hello: hello, |
| 305 | suite: suite, |
| 306 | finishedHash: newFinishedHash(c.vers, suite), |
| 307 | session: session, |
| 308 | } |
| 309 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 310 | hs.writeHash(helloBytes, hs.c.sendHandshakeSeq-1) |
| 311 | hs.writeServerHash(hs.serverHello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 312 | |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 313 | if c.config.Bugs.EarlyChangeCipherSpec > 0 { |
| 314 | hs.establishKeys() |
| 315 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 316 | } |
| 317 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | isResume, err := hs.processServerHello() |
| 319 | if err != nil { |
| 320 | return err |
| 321 | } |
| 322 | |
| 323 | if isResume { |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 324 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 325 | if err := hs.establishKeys(); err != nil { |
| 326 | return err |
| 327 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | } |
| 329 | if err := hs.readSessionTicket(); err != nil { |
| 330 | return err |
| 331 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 332 | if err := hs.readFinished(c.firstFinished[:]); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 333 | return err |
| 334 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 335 | if err := hs.sendFinished(nil, isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | return err |
| 337 | } |
| 338 | } else { |
| 339 | if err := hs.doFullHandshake(); err != nil { |
| 340 | return err |
| 341 | } |
| 342 | if err := hs.establishKeys(); err != nil { |
| 343 | return err |
| 344 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 345 | if err := hs.sendFinished(c.firstFinished[:], isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 346 | return err |
| 347 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 348 | // Most retransmits are triggered by a timeout, but the final |
| 349 | // leg of the handshake is retransmited upon re-receiving a |
| 350 | // Finished. |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 351 | if err := c.simulatePacketLoss(func() { |
| 352 | c.writeRecord(recordTypeHandshake, hs.finishedBytes) |
David Benjamin | a4e6d48 | 2015-03-02 19:10:53 -0500 | [diff] [blame] | 353 | c.dtlsFlushHandshake() |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 354 | }); err != nil { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 355 | return err |
| 356 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 357 | if err := hs.readSessionTicket(); err != nil { |
| 358 | return err |
| 359 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 360 | if err := hs.readFinished(nil); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 361 | return err |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if sessionCache != nil && hs.session != nil && session != hs.session { |
| 366 | sessionCache.Put(cacheKey, hs.session) |
| 367 | } |
| 368 | |
| 369 | c.didResume = isResume |
| 370 | c.handshakeComplete = true |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 371 | c.cipherSuite = suite |
| 372 | copy(c.clientRandom[:], hs.hello.random) |
| 373 | copy(c.serverRandom[:], hs.serverHello.random) |
| 374 | copy(c.masterSecret[:], hs.masterSecret) |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 375 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | return nil |
| 377 | } |
| 378 | |
| 379 | func (hs *clientHandshakeState) doFullHandshake() error { |
| 380 | c := hs.c |
| 381 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 382 | var leaf *x509.Certificate |
| 383 | if hs.suite.flags&suitePSK == 0 { |
| 384 | msg, err := c.readHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 385 | if err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | return err |
| 387 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 388 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 389 | certMsg, ok := msg.(*certificateMsg) |
| 390 | if !ok || len(certMsg.certificates) == 0 { |
| 391 | c.sendAlert(alertUnexpectedMessage) |
| 392 | return unexpectedMessageError(certMsg, msg) |
| 393 | } |
| 394 | hs.writeServerHash(certMsg.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 395 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 396 | certs := make([]*x509.Certificate, len(certMsg.certificates)) |
| 397 | for i, asn1Data := range certMsg.certificates { |
| 398 | cert, err := x509.ParseCertificate(asn1Data) |
| 399 | if err != nil { |
| 400 | c.sendAlert(alertBadCertificate) |
| 401 | return errors.New("tls: failed to parse certificate from server: " + err.Error()) |
| 402 | } |
| 403 | certs[i] = cert |
| 404 | } |
| 405 | leaf = certs[0] |
| 406 | |
| 407 | if !c.config.InsecureSkipVerify { |
| 408 | opts := x509.VerifyOptions{ |
| 409 | Roots: c.config.RootCAs, |
| 410 | CurrentTime: c.config.time(), |
| 411 | DNSName: c.config.ServerName, |
| 412 | Intermediates: x509.NewCertPool(), |
| 413 | } |
| 414 | |
| 415 | for i, cert := range certs { |
| 416 | if i == 0 { |
| 417 | continue |
| 418 | } |
| 419 | opts.Intermediates.AddCert(cert) |
| 420 | } |
| 421 | c.verifiedChains, err = leaf.Verify(opts) |
| 422 | if err != nil { |
| 423 | c.sendAlert(alertBadCertificate) |
| 424 | return err |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | switch leaf.PublicKey.(type) { |
| 429 | case *rsa.PublicKey, *ecdsa.PublicKey: |
| 430 | break |
| 431 | default: |
| 432 | c.sendAlert(alertUnsupportedCertificate) |
| 433 | return fmt.Errorf("tls: server's certificate contains an unsupported type of public key: %T", leaf.PublicKey) |
| 434 | } |
| 435 | |
| 436 | c.peerCertificates = certs |
| 437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
| 439 | if hs.serverHello.ocspStapling { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 440 | msg, err := c.readHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | if err != nil { |
| 442 | return err |
| 443 | } |
| 444 | cs, ok := msg.(*certificateStatusMsg) |
| 445 | if !ok { |
| 446 | c.sendAlert(alertUnexpectedMessage) |
| 447 | return unexpectedMessageError(cs, msg) |
| 448 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 449 | hs.writeServerHash(cs.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 450 | |
| 451 | if cs.statusType == statusTypeOCSP { |
| 452 | c.ocspResponse = cs.response |
| 453 | } |
| 454 | } |
| 455 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 456 | msg, err := c.readHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | if err != nil { |
| 458 | return err |
| 459 | } |
| 460 | |
| 461 | keyAgreement := hs.suite.ka(c.vers) |
| 462 | |
| 463 | skx, ok := msg.(*serverKeyExchangeMsg) |
| 464 | if ok { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 465 | hs.writeServerHash(skx.marshal()) |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 466 | err = keyAgreement.processServerKeyExchange(c.config, hs.hello, hs.serverHello, leaf, skx) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 467 | if err != nil { |
| 468 | c.sendAlert(alertUnexpectedMessage) |
| 469 | return err |
| 470 | } |
| 471 | |
| 472 | msg, err = c.readHandshake() |
| 473 | if err != nil { |
| 474 | return err |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | var chainToSend *Certificate |
| 479 | var certRequested bool |
| 480 | certReq, ok := msg.(*certificateRequestMsg) |
| 481 | if ok { |
| 482 | certRequested = true |
| 483 | |
| 484 | // RFC 4346 on the certificateAuthorities field: |
| 485 | // A list of the distinguished names of acceptable certificate |
| 486 | // authorities. These distinguished names may specify a desired |
| 487 | // distinguished name for a root CA or for a subordinate CA; |
| 488 | // thus, this message can be used to describe both known roots |
| 489 | // and a desired authorization space. If the |
| 490 | // certificate_authorities list is empty then the client MAY |
| 491 | // send any certificate of the appropriate |
| 492 | // ClientCertificateType, unless there is some external |
| 493 | // arrangement to the contrary. |
| 494 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 495 | hs.writeServerHash(certReq.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 496 | |
| 497 | var rsaAvail, ecdsaAvail bool |
| 498 | for _, certType := range certReq.certificateTypes { |
| 499 | switch certType { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 500 | case CertTypeRSASign: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | rsaAvail = true |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 502 | case CertTypeECDSASign: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | ecdsaAvail = true |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | // We need to search our list of client certs for one |
| 508 | // where SignatureAlgorithm is RSA and the Issuer is in |
| 509 | // certReq.certificateAuthorities |
| 510 | findCert: |
| 511 | for i, chain := range c.config.Certificates { |
| 512 | if !rsaAvail && !ecdsaAvail { |
| 513 | continue |
| 514 | } |
| 515 | |
| 516 | for j, cert := range chain.Certificate { |
| 517 | x509Cert := chain.Leaf |
| 518 | // parse the certificate if this isn't the leaf |
| 519 | // node, or if chain.Leaf was nil |
| 520 | if j != 0 || x509Cert == nil { |
| 521 | if x509Cert, err = x509.ParseCertificate(cert); err != nil { |
| 522 | c.sendAlert(alertInternalError) |
| 523 | return errors.New("tls: failed to parse client certificate #" + strconv.Itoa(i) + ": " + err.Error()) |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | switch { |
| 528 | case rsaAvail && x509Cert.PublicKeyAlgorithm == x509.RSA: |
| 529 | case ecdsaAvail && x509Cert.PublicKeyAlgorithm == x509.ECDSA: |
| 530 | default: |
| 531 | continue findCert |
| 532 | } |
| 533 | |
| 534 | if len(certReq.certificateAuthorities) == 0 { |
| 535 | // they gave us an empty list, so just take the |
| 536 | // first RSA cert from c.config.Certificates |
| 537 | chainToSend = &chain |
| 538 | break findCert |
| 539 | } |
| 540 | |
| 541 | for _, ca := range certReq.certificateAuthorities { |
| 542 | if bytes.Equal(x509Cert.RawIssuer, ca) { |
| 543 | chainToSend = &chain |
| 544 | break findCert |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | msg, err = c.readHandshake() |
| 551 | if err != nil { |
| 552 | return err |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | shd, ok := msg.(*serverHelloDoneMsg) |
| 557 | if !ok { |
| 558 | c.sendAlert(alertUnexpectedMessage) |
| 559 | return unexpectedMessageError(shd, msg) |
| 560 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 561 | hs.writeServerHash(shd.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 562 | |
| 563 | // If the server requested a certificate then we have to send a |
| 564 | // Certificate message, even if it's empty because we don't have a |
| 565 | // certificate to send. |
| 566 | if certRequested { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 567 | certMsg := new(certificateMsg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | if chainToSend != nil { |
| 569 | certMsg.certificates = chainToSend.Certificate |
| 570 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 571 | hs.writeClientHash(certMsg.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 572 | c.writeRecord(recordTypeHandshake, certMsg.marshal()) |
| 573 | } |
| 574 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 575 | preMasterSecret, ckx, err := keyAgreement.generateClientKeyExchange(c.config, hs.hello, leaf) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 576 | if err != nil { |
| 577 | c.sendAlert(alertInternalError) |
| 578 | return err |
| 579 | } |
| 580 | if ckx != nil { |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 581 | if c.config.Bugs.EarlyChangeCipherSpec < 2 { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 582 | hs.writeClientHash(ckx.marshal()) |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 583 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 584 | c.writeRecord(recordTypeHandshake, ckx.marshal()) |
| 585 | } |
| 586 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 587 | if hs.serverHello.extendedMasterSecret && c.vers >= VersionTLS10 { |
| 588 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 589 | c.extendedMasterSecret = true |
| 590 | } else { |
| 591 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 592 | return errors.New("tls: extended master secret required but not supported by peer") |
| 593 | } |
| 594 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.hello.random, hs.serverHello.random) |
| 595 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 596 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 597 | if chainToSend != nil { |
| 598 | var signed []byte |
| 599 | certVerify := &certificateVerifyMsg{ |
| 600 | hasSignatureAndHash: c.vers >= VersionTLS12, |
| 601 | } |
| 602 | |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 603 | // Determine the hash to sign. |
| 604 | var signatureType uint8 |
| 605 | switch c.config.Certificates[0].PrivateKey.(type) { |
| 606 | case *ecdsa.PrivateKey: |
| 607 | signatureType = signatureECDSA |
| 608 | case *rsa.PrivateKey: |
| 609 | signatureType = signatureRSA |
| 610 | default: |
| 611 | c.sendAlert(alertInternalError) |
| 612 | return errors.New("unknown private key type") |
| 613 | } |
| 614 | if c.config.Bugs.IgnorePeerSignatureAlgorithmPreferences { |
| 615 | certReq.signatureAndHashes = c.config.signatureAndHashesForClient() |
| 616 | } |
| 617 | certVerify.signatureAndHash, err = hs.finishedHash.selectClientCertSignatureAlgorithm(certReq.signatureAndHashes, c.config.signatureAndHashesForClient(), signatureType) |
| 618 | if err != nil { |
| 619 | c.sendAlert(alertInternalError) |
| 620 | return err |
| 621 | } |
| 622 | digest, hashFunc, err := hs.finishedHash.hashForClientCertificate(certVerify.signatureAndHash, hs.masterSecret) |
| 623 | if err != nil { |
| 624 | c.sendAlert(alertInternalError) |
| 625 | return err |
| 626 | } |
David Benjamin | 6de0e53 | 2015-07-28 22:43:19 -0400 | [diff] [blame] | 627 | if c.config.Bugs.InvalidCertVerifySignature { |
| 628 | digest[0] ^= 0x80 |
| 629 | } |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 630 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 631 | switch key := c.config.Certificates[0].PrivateKey.(type) { |
| 632 | case *ecdsa.PrivateKey: |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 633 | var r, s *big.Int |
| 634 | r, s, err = ecdsa.Sign(c.config.rand(), key, digest) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | if err == nil { |
| 636 | signed, err = asn1.Marshal(ecdsaSignature{r, s}) |
| 637 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | case *rsa.PrivateKey: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 639 | signed, err = rsa.SignPKCS1v15(c.config.rand(), key, hashFunc, digest) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 640 | default: |
| 641 | err = errors.New("unknown private key type") |
| 642 | } |
| 643 | if err != nil { |
| 644 | c.sendAlert(alertInternalError) |
| 645 | return errors.New("tls: failed to sign handshake with client certificate: " + err.Error()) |
| 646 | } |
| 647 | certVerify.signature = signed |
| 648 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 649 | hs.writeClientHash(certVerify.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 650 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
| 651 | } |
David Benjamin | a4e6d48 | 2015-03-02 19:10:53 -0500 | [diff] [blame] | 652 | c.dtlsFlushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 653 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 654 | hs.finishedHash.discardHandshakeBuffer() |
| 655 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 656 | return nil |
| 657 | } |
| 658 | |
| 659 | func (hs *clientHandshakeState) establishKeys() error { |
| 660 | c := hs.c |
| 661 | |
| 662 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
| 663 | keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.hello.random, hs.serverHello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen) |
| 664 | var clientCipher, serverCipher interface{} |
| 665 | var clientHash, serverHash macFunction |
| 666 | if hs.suite.cipher != nil { |
| 667 | clientCipher = hs.suite.cipher(clientKey, clientIV, false /* not for reading */) |
| 668 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 669 | serverCipher = hs.suite.cipher(serverKey, serverIV, true /* for reading */) |
| 670 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 671 | } else { |
| 672 | clientCipher = hs.suite.aead(clientKey, clientIV) |
| 673 | serverCipher = hs.suite.aead(serverKey, serverIV) |
| 674 | } |
| 675 | |
| 676 | c.in.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 677 | c.out.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 678 | return nil |
| 679 | } |
| 680 | |
| 681 | func (hs *clientHandshakeState) serverResumedSession() bool { |
| 682 | // If the server responded with the same sessionId then it means the |
| 683 | // sessionTicket is being used to resume a TLS session. |
| 684 | return hs.session != nil && hs.hello.sessionId != nil && |
| 685 | bytes.Equal(hs.serverHello.sessionId, hs.hello.sessionId) |
| 686 | } |
| 687 | |
| 688 | func (hs *clientHandshakeState) processServerHello() (bool, error) { |
| 689 | c := hs.c |
| 690 | |
| 691 | if hs.serverHello.compressionMethod != compressionNone { |
| 692 | c.sendAlert(alertUnexpectedMessage) |
| 693 | return false, errors.New("tls: server selected unsupported compression format") |
| 694 | } |
| 695 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 696 | clientDidNPN := hs.hello.nextProtoNeg |
| 697 | clientDidALPN := len(hs.hello.alpnProtocols) > 0 |
| 698 | serverHasNPN := hs.serverHello.nextProtoNeg |
| 699 | serverHasALPN := len(hs.serverHello.alpnProtocol) > 0 |
| 700 | |
| 701 | if !clientDidNPN && serverHasNPN { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 702 | c.sendAlert(alertHandshakeFailure) |
| 703 | return false, errors.New("server advertised unrequested NPN extension") |
| 704 | } |
| 705 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 706 | if !clientDidALPN && serverHasALPN { |
| 707 | c.sendAlert(alertHandshakeFailure) |
| 708 | return false, errors.New("server advertised unrequested ALPN extension") |
| 709 | } |
| 710 | |
| 711 | if serverHasNPN && serverHasALPN { |
| 712 | c.sendAlert(alertHandshakeFailure) |
| 713 | return false, errors.New("server advertised both NPN and ALPN extensions") |
| 714 | } |
| 715 | |
| 716 | if serverHasALPN { |
| 717 | c.clientProtocol = hs.serverHello.alpnProtocol |
| 718 | c.clientProtocolFallback = false |
David Benjamin | fc7b086 | 2014-09-06 13:21:53 -0400 | [diff] [blame] | 719 | c.usedALPN = true |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 720 | } |
| 721 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 722 | if !hs.hello.channelIDSupported && hs.serverHello.channelIDRequested { |
| 723 | c.sendAlert(alertHandshakeFailure) |
| 724 | return false, errors.New("server advertised unrequested Channel ID extension") |
| 725 | } |
| 726 | |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 727 | if hs.serverHello.srtpProtectionProfile != 0 { |
| 728 | if hs.serverHello.srtpMasterKeyIdentifier != "" { |
| 729 | return false, errors.New("tls: server selected SRTP MKI value") |
| 730 | } |
| 731 | |
| 732 | found := false |
| 733 | for _, p := range c.config.SRTPProtectionProfiles { |
| 734 | if p == hs.serverHello.srtpProtectionProfile { |
| 735 | found = true |
| 736 | break |
| 737 | } |
| 738 | } |
| 739 | if !found { |
| 740 | return false, errors.New("tls: server advertised unsupported SRTP profile") |
| 741 | } |
| 742 | |
| 743 | c.srtpProtectionProfile = hs.serverHello.srtpProtectionProfile |
| 744 | } |
| 745 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 746 | if hs.serverResumedSession() { |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 747 | // For test purposes, assert that the server never accepts the |
| 748 | // resumption offer on renegotiation. |
| 749 | if c.cipherSuite != nil && c.config.Bugs.FailIfResumeOnRenego { |
| 750 | return false, errors.New("tls: server resumed session on renegotiation") |
| 751 | } |
| 752 | |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame^] | 753 | if hs.serverHello.sctList != nil { |
| 754 | return false, errors.New("tls: server sent SCT extension on session resumption") |
| 755 | } |
| 756 | |
| 757 | if hs.serverHello.ocspStapling { |
| 758 | return false, errors.New("tls: server sent OCSP extension on session resumption") |
| 759 | } |
| 760 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 761 | // Restore masterSecret and peerCerts from previous state |
| 762 | hs.masterSecret = hs.session.masterSecret |
| 763 | c.peerCertificates = hs.session.serverCertificates |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 764 | c.extendedMasterSecret = hs.session.extendedMasterSecret |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame^] | 765 | c.sctList = hs.session.sctList |
| 766 | c.ocspResponse = hs.session.ocspResponse |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 767 | hs.finishedHash.discardHandshakeBuffer() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 768 | return true, nil |
| 769 | } |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame^] | 770 | |
| 771 | if hs.serverHello.sctList != nil { |
| 772 | c.sctList = hs.serverHello.sctList |
| 773 | } |
| 774 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 775 | return false, nil |
| 776 | } |
| 777 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 778 | func (hs *clientHandshakeState) readFinished(out []byte) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 779 | c := hs.c |
| 780 | |
| 781 | c.readRecord(recordTypeChangeCipherSpec) |
| 782 | if err := c.in.error(); err != nil { |
| 783 | return err |
| 784 | } |
| 785 | |
| 786 | msg, err := c.readHandshake() |
| 787 | if err != nil { |
| 788 | return err |
| 789 | } |
| 790 | serverFinished, ok := msg.(*finishedMsg) |
| 791 | if !ok { |
| 792 | c.sendAlert(alertUnexpectedMessage) |
| 793 | return unexpectedMessageError(serverFinished, msg) |
| 794 | } |
| 795 | |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 796 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 797 | verify := hs.finishedHash.serverSum(hs.masterSecret) |
| 798 | if len(verify) != len(serverFinished.verifyData) || |
| 799 | subtle.ConstantTimeCompare(verify, serverFinished.verifyData) != 1 { |
| 800 | c.sendAlert(alertHandshakeFailure) |
| 801 | return errors.New("tls: server's Finished message was incorrect") |
| 802 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 803 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 804 | c.serverVerify = append(c.serverVerify[:0], serverFinished.verifyData...) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 805 | copy(out, serverFinished.verifyData) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 806 | hs.writeServerHash(serverFinished.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 807 | return nil |
| 808 | } |
| 809 | |
| 810 | func (hs *clientHandshakeState) readSessionTicket() error { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 811 | c := hs.c |
| 812 | |
| 813 | // Create a session with no server identifier. Either a |
| 814 | // session ID or session ticket will be attached. |
| 815 | session := &ClientSessionState{ |
| 816 | vers: c.vers, |
| 817 | cipherSuite: hs.suite.id, |
| 818 | masterSecret: hs.masterSecret, |
| 819 | handshakeHash: hs.finishedHash.server.Sum(nil), |
| 820 | serverCertificates: c.peerCertificates, |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame^] | 821 | sctList: c.sctList, |
| 822 | ocspResponse: c.ocspResponse, |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 823 | } |
| 824 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | if !hs.serverHello.ticketSupported { |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 826 | if c.config.Bugs.ExpectNewTicket { |
| 827 | return errors.New("tls: expected new ticket") |
| 828 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 829 | if hs.session == nil && len(hs.serverHello.sessionId) > 0 { |
| 830 | session.sessionId = hs.serverHello.sessionId |
| 831 | hs.session = session |
| 832 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 833 | return nil |
| 834 | } |
| 835 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 836 | msg, err := c.readHandshake() |
| 837 | if err != nil { |
| 838 | return err |
| 839 | } |
| 840 | sessionTicketMsg, ok := msg.(*newSessionTicketMsg) |
| 841 | if !ok { |
| 842 | c.sendAlert(alertUnexpectedMessage) |
| 843 | return unexpectedMessageError(sessionTicketMsg, msg) |
| 844 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 845 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 846 | session.sessionTicket = sessionTicketMsg.ticket |
| 847 | hs.session = session |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 848 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 849 | hs.writeServerHash(sessionTicketMsg.marshal()) |
| 850 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 851 | return nil |
| 852 | } |
| 853 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 854 | func (hs *clientHandshakeState) sendFinished(out []byte, isResume bool) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 855 | c := hs.c |
| 856 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 857 | var postCCSBytes []byte |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 858 | seqno := hs.c.sendHandshakeSeq |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 859 | if hs.serverHello.nextProtoNeg { |
| 860 | nextProto := new(nextProtoMsg) |
| 861 | proto, fallback := mutualProtocol(c.config.NextProtos, hs.serverHello.nextProtos) |
| 862 | nextProto.proto = proto |
| 863 | c.clientProtocol = proto |
| 864 | c.clientProtocolFallback = fallback |
| 865 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 866 | nextProtoBytes := nextProto.marshal() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 867 | hs.writeHash(nextProtoBytes, seqno) |
| 868 | seqno++ |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 869 | postCCSBytes = append(postCCSBytes, nextProtoBytes...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 870 | } |
| 871 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 872 | if hs.serverHello.channelIDRequested { |
| 873 | encryptedExtensions := new(encryptedExtensionsMsg) |
| 874 | if c.config.ChannelID.Curve != elliptic.P256() { |
| 875 | return fmt.Errorf("tls: Channel ID is not on P-256.") |
| 876 | } |
| 877 | var resumeHash []byte |
| 878 | if isResume { |
| 879 | resumeHash = hs.session.handshakeHash |
| 880 | } |
| 881 | r, s, err := ecdsa.Sign(c.config.rand(), c.config.ChannelID, hs.finishedHash.hashForChannelID(resumeHash)) |
| 882 | if err != nil { |
| 883 | return err |
| 884 | } |
| 885 | channelID := make([]byte, 128) |
| 886 | writeIntPadded(channelID[0:32], c.config.ChannelID.X) |
| 887 | writeIntPadded(channelID[32:64], c.config.ChannelID.Y) |
| 888 | writeIntPadded(channelID[64:96], r) |
| 889 | writeIntPadded(channelID[96:128], s) |
| 890 | encryptedExtensions.channelID = channelID |
| 891 | |
| 892 | c.channelID = &c.config.ChannelID.PublicKey |
| 893 | |
| 894 | encryptedExtensionsBytes := encryptedExtensions.marshal() |
| 895 | hs.writeHash(encryptedExtensionsBytes, seqno) |
| 896 | seqno++ |
| 897 | postCCSBytes = append(postCCSBytes, encryptedExtensionsBytes...) |
| 898 | } |
| 899 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 900 | finished := new(finishedMsg) |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 901 | if c.config.Bugs.EarlyChangeCipherSpec == 2 { |
| 902 | finished.verifyData = hs.finishedHash.clientSum(nil) |
| 903 | } else { |
| 904 | finished.verifyData = hs.finishedHash.clientSum(hs.masterSecret) |
| 905 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 906 | copy(out, finished.verifyData) |
David Benjamin | 513f0ea | 2015-04-02 19:33:31 -0400 | [diff] [blame] | 907 | if c.config.Bugs.BadFinished { |
| 908 | finished.verifyData[0]++ |
| 909 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 910 | c.clientVerify = append(c.clientVerify[:0], finished.verifyData...) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 911 | hs.finishedBytes = finished.marshal() |
| 912 | hs.writeHash(hs.finishedBytes, seqno) |
| 913 | postCCSBytes = append(postCCSBytes, hs.finishedBytes...) |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 914 | |
| 915 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 916 | c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 917 | postCCSBytes = postCCSBytes[5:] |
| 918 | } |
David Benjamin | a4e6d48 | 2015-03-02 19:10:53 -0500 | [diff] [blame] | 919 | c.dtlsFlushHandshake() |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 920 | |
| 921 | if !c.config.Bugs.SkipChangeCipherSpec && |
| 922 | c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 923 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 924 | } |
| 925 | |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 926 | if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 927 | c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 928 | } |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 929 | if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 930 | c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 931 | return errors.New("tls: simulating post-CCS alert") |
| 932 | } |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 933 | |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 934 | if !c.config.Bugs.SkipFinished { |
| 935 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
David Benjamin | a4e6d48 | 2015-03-02 19:10:53 -0500 | [diff] [blame] | 936 | c.dtlsFlushHandshake() |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 937 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 938 | return nil |
| 939 | } |
| 940 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 941 | func (hs *clientHandshakeState) writeClientHash(msg []byte) { |
| 942 | // writeClientHash is called before writeRecord. |
| 943 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 944 | } |
| 945 | |
| 946 | func (hs *clientHandshakeState) writeServerHash(msg []byte) { |
| 947 | // writeServerHash is called after readHandshake. |
| 948 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 949 | } |
| 950 | |
| 951 | func (hs *clientHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 952 | if hs.c.isDTLS { |
| 953 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 954 | // First, the TLS header. |
| 955 | hs.finishedHash.Write(msg[:4]) |
| 956 | // Then the sequence number and reassembled fragment offset (always 0). |
| 957 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 958 | // Then the reassembled fragment (always equal to the message length). |
| 959 | hs.finishedHash.Write(msg[1:4]) |
| 960 | // And then the message body. |
| 961 | hs.finishedHash.Write(msg[4:]) |
| 962 | } else { |
| 963 | hs.finishedHash.Write(msg) |
| 964 | } |
| 965 | } |
| 966 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 967 | // clientSessionCacheKey returns a key used to cache sessionTickets that could |
| 968 | // be used to resume previously negotiated TLS sessions with a server. |
| 969 | func clientSessionCacheKey(serverAddr net.Addr, config *Config) string { |
| 970 | if len(config.ServerName) > 0 { |
| 971 | return config.ServerName |
| 972 | } |
| 973 | return serverAddr.String() |
| 974 | } |
| 975 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 976 | // mutualProtocol finds the mutual Next Protocol Negotiation or ALPN protocol |
| 977 | // given list of possible protocols and a list of the preference order. The |
| 978 | // first list must not be empty. It returns the resulting protocol and flag |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 979 | // indicating if the fallback case was reached. |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 980 | func mutualProtocol(protos, preferenceProtos []string) (string, bool) { |
| 981 | for _, s := range preferenceProtos { |
| 982 | for _, c := range protos { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 983 | if s == c { |
| 984 | return s, false |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 989 | return protos[0], true |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 990 | } |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 991 | |
| 992 | // writeIntPadded writes x into b, padded up with leading zeros as |
| 993 | // needed. |
| 994 | func writeIntPadded(b []byte, x *big.Int) { |
| 995 | for i := range b { |
| 996 | b[i] = 0 |
| 997 | } |
| 998 | xb := x.Bytes() |
| 999 | copy(b[len(b)-len(xb):], xb) |
| 1000 | } |