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