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