| 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 runner |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto" |
| 10 |
"crypto/ecdsa" |
| 11 |
"crypto/elliptic" |
| 12 |
"crypto/rsa" |
| 13 |
"crypto/subtle" |
| 14 |
"crypto/x509" |
| 15 |
"encoding/asn1" |
| 16 |
"errors" |
| 17 |
"fmt" |
| 18 |
"io" |
| 19 |
"math/big" |
| 20 |
) |
| 21 |
|
| 22 |
// serverHandshakeState contains details of a server handshake in progress. |
| 23 |
// It's discarded once the handshake has completed. |
| 24 |
type serverHandshakeState struct { |
| 25 |
c *Conn |
| 26 |
clientHello *clientHelloMsg |
| 27 |
hello *serverHelloMsg |
| 28 |
suite *cipherSuite |
| 29 |
ellipticOk bool |
| 30 |
ecdsaOk bool |
| 31 |
sessionState *sessionState |
| 32 |
finishedHash finishedHash |
| 33 |
masterSecret []byte |
| 34 |
certsFromClient [][]byte |
| 35 |
cert *Certificate |
| 36 |
finishedBytes []byte |
| 37 |
} |
| 38 |
|
| 39 |
// serverHandshake performs a TLS handshake as a server. |
| 40 |
func (c *Conn) serverHandshake() error { |
| 41 |
config := c.config |
| 42 |
|
| 43 |
// If this is the first server handshake, we generate a random key to |
| 44 |
// encrypt the tickets with. |
| 45 |
config.serverInitOnce.Do(config.serverInit) |
| 46 |
|
| 47 |
c.sendHandshakeSeq = 0 |
| 48 |
c.recvHandshakeSeq = 0 |
| 49 |
|
| 50 |
hs := serverHandshakeState{ |
| 51 |
c: c, |
| 52 |
} |
| 53 |
isResume, err := hs.readClientHello() |
| 54 |
if err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
|
| 58 |
// For an overview of TLS handshaking, see https://tools.ietf.org/html/rfc5246#section-7.3 |
| 59 |
if isResume { |
| 60 |
// The client has included a session ticket and so we do an abbreviated handshake. |
| 61 |
if err := hs.doResumeHandshake(); err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
if err := hs.establishKeys(); err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
if c.config.Bugs.RenewTicketOnResume { |
| 68 |
if err := hs.sendSessionTicket(); err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
} |
| 72 |
if err := hs.sendFinished(c.firstFinished[:]); err != nil { |
| 73 |
return err |
| 74 |
} |
| 75 |
// Most retransmits are triggered by a timeout, but the final |
| 76 |
// leg of the handshake is retransmited upon re-receiving a |
| 77 |
// Finished. |
| 78 |
if err := c.simulatePacketLoss(func() { |
| 79 |
c.writeRecord(recordTypeHandshake, hs.finishedBytes) |
| 80 |
c.dtlsFlushHandshake() |
| 81 |
}); err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
if err := hs.readFinished(nil, isResume); err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
c.didResume = true |
| 88 |
} else { |
| 89 |
// The client didn't include a session ticket, or it wasn't |
| 90 |
// valid so we do a full handshake. |
| 91 |
if err := hs.doFullHandshake(); err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
if err := hs.establishKeys(); err != nil { |
| 95 |
return err |
| 96 |
} |
| 97 |
if err := hs.readFinished(c.firstFinished[:], isResume); err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
if c.config.Bugs.AlertBeforeFalseStartTest != 0 { |
| 101 |
c.sendAlert(c.config.Bugs.AlertBeforeFalseStartTest) |
| 102 |
} |
| 103 |
if c.config.Bugs.ExpectFalseStart { |
| 104 |
if err := c.readRecord(recordTypeApplicationData); err != nil { |
| 105 |
return fmt.Errorf("tls: peer did not false start: %s", err) |
| 106 |
} |
| 107 |
} |
| 108 |
if err := hs.sendSessionTicket(); err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
if err := hs.sendFinished(nil); err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
} |
| 115 |
c.handshakeComplete = true |
| 116 |
copy(c.clientRandom[:], hs.clientHello.random) |
| 117 |
copy(c.serverRandom[:], hs.hello.random) |
| 118 |
copy(c.masterSecret[:], hs.masterSecret) |
| 119 |
|
| 120 |
return nil |
| 121 |
} |
| 122 |
|
| 123 |
// readClientHello reads a ClientHello message from the client and decides |
| 124 |
// whether we will perform session resumption. |
| 125 |
func (hs *serverHandshakeState) readClientHello() (isResume bool, err error) { |
| 126 |
config := hs.c.config |
| 127 |
c := hs.c |
| 128 |
|
| 129 |
if err := c.simulatePacketLoss(nil); err != nil { |
| 130 |
return false, err |
| 131 |
} |
| 132 |
msg, err := c.readHandshake() |
| 133 |
if err != nil { |
| 134 |
return false, err |
| 135 |
} |
| 136 |
var ok bool |
| 137 |
hs.clientHello, ok = msg.(*clientHelloMsg) |
| 138 |
if !ok { |
| 139 |
c.sendAlert(alertUnexpectedMessage) |
| 140 |
return false, unexpectedMessageError(hs.clientHello, msg) |
| 141 |
} |
| 142 |
if size := config.Bugs.RequireClientHelloSize; size != 0 && len(hs.clientHello.raw) != size { |
| 143 |
return false, fmt.Errorf("tls: ClientHello record size is %d, but expected %d", len(hs.clientHello.raw), size) |
| 144 |
} |
| 145 |
|
| 146 |
if c.isDTLS && !config.Bugs.SkipHelloVerifyRequest { |
| 147 |
// Per RFC 6347, the version field in HelloVerifyRequest SHOULD |
| 148 |
// be always DTLS 1.0 |
| 149 |
helloVerifyRequest := &helloVerifyRequestMsg{ |
| 150 |
vers: VersionTLS10, |
| 151 |
cookie: make([]byte, 32), |
| 152 |
} |
| 153 |
if _, err := io.ReadFull(c.config.rand(), helloVerifyRequest.cookie); err != nil { |
| 154 |
c.sendAlert(alertInternalError) |
| 155 |
return false, errors.New("dtls: short read from Rand: " + err.Error()) |
| 156 |
} |
| 157 |
c.writeRecord(recordTypeHandshake, helloVerifyRequest.marshal()) |
| 158 |
c.dtlsFlushHandshake() |
| 159 |
|
| 160 |
if err := c.simulatePacketLoss(nil); err != nil { |
| 161 |
return false, err |
| 162 |
} |
| 163 |
msg, err := c.readHandshake() |
| 164 |
if err != nil { |
| 165 |
return false, err |
| 166 |
} |
| 167 |
newClientHello, ok := msg.(*clientHelloMsg) |
| 168 |
if !ok { |
| 169 |
c.sendAlert(alertUnexpectedMessage) |
| 170 |
return false, unexpectedMessageError(hs.clientHello, msg) |
| 171 |
} |
| 172 |
if !bytes.Equal(newClientHello.cookie, helloVerifyRequest.cookie) { |
| 173 |
return false, errors.New("dtls: invalid cookie") |
| 174 |
} |
| 175 |
|
| 176 |
// Apart from the cookie, the two ClientHellos must |
| 177 |
// match. Note that clientHello.equal compares the |
| 178 |
// serialization, so we make a copy. |
| 179 |
oldClientHelloCopy := *hs.clientHello |
| 180 |
oldClientHelloCopy.raw = nil |
| 181 |
oldClientHelloCopy.cookie = nil |
| 182 |
newClientHelloCopy := *newClientHello |
| 183 |
newClientHelloCopy.raw = nil |
| 184 |
newClientHelloCopy.cookie = nil |
| 185 |
if !oldClientHelloCopy.equal(&newClientHelloCopy) { |
| 186 |
return false, errors.New("dtls: retransmitted ClientHello does not match") |
| 187 |
} |
| 188 |
hs.clientHello = newClientHello |
| 189 |
} |
| 190 |
|
| 191 |
if config.Bugs.RequireSameRenegoClientVersion && c.clientVersion != 0 { |
| 192 |
if c.clientVersion != hs.clientHello.vers { |
| 193 |
return false, fmt.Errorf("tls: client offered different version on renego") |
| 194 |
} |
| 195 |
} |
| 196 |
c.clientVersion = hs.clientHello.vers |
| 197 |
|
| 198 |
// Reject < 1.2 ClientHellos with signature_algorithms. |
| 199 |
if c.clientVersion < VersionTLS12 && len(hs.clientHello.signatureAndHashes) > 0 { |
| 200 |
return false, fmt.Errorf("tls: client included signature_algorithms before TLS 1.2") |
| 201 |
} |
| 202 |
if config.Bugs.IgnorePeerSignatureAlgorithmPreferences { |
| 203 |
hs.clientHello.signatureAndHashes = config.signatureAndHashesForServer() |
| 204 |
} |
| 205 |
|
| 206 |
// Check the client cipher list is consistent with the version. |
| 207 |
if hs.clientHello.vers < VersionTLS12 { |
| 208 |
for _, id := range hs.clientHello.cipherSuites { |
| 209 |
if isTLS12Cipher(id) { |
| 210 |
return false, fmt.Errorf("tls: client offered TLS 1.2 cipher before TLS 1.2") |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
c.vers, ok = config.mutualVersion(hs.clientHello.vers) |
| 216 |
if !ok { |
| 217 |
c.sendAlert(alertProtocolVersion) |
| 218 |
return false, fmt.Errorf("tls: client offered an unsupported, maximum protocol version of %x", hs.clientHello.vers) |
| 219 |
} |
| 220 |
c.haveVers = true |
| 221 |
|
| 222 |
hs.hello = &serverHelloMsg{ |
| 223 |
isDTLS: c.isDTLS, |
| 224 |
customExtension: config.Bugs.CustomExtension, |
| 225 |
npnLast: config.Bugs.SwapNPNAndALPN, |
| 226 |
} |
| 227 |
|
| 228 |
supportedCurve := false |
| 229 |
preferredCurves := config.curvePreferences() |
| 230 |
if config.Bugs.IgnorePeerCurvePreferences { |
| 231 |
hs.clientHello.supportedCurves = preferredCurves |
| 232 |
} |
| 233 |
Curves: |
| 234 |
for _, curve := range hs.clientHello.supportedCurves { |
| 235 |
for _, supported := range preferredCurves { |
| 236 |
if supported == curve { |
| 237 |
supportedCurve = true |
| 238 |
break Curves |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
supportedPointFormat := false |
| 244 |
for _, pointFormat := range hs.clientHello.supportedPoints { |
| 245 |
if pointFormat == pointFormatUncompressed { |
| 246 |
supportedPointFormat = true |
| 247 |
break |
| 248 |
} |
| 249 |
} |
| 250 |
hs.ellipticOk = supportedCurve && supportedPointFormat |
| 251 |
|
| 252 |
foundCompression := false |
| 253 |
// We only support null compression, so check that the client offered it. |
| 254 |
for _, compression := range hs.clientHello.compressionMethods { |
| 255 |
if compression == compressionNone { |
| 256 |
foundCompression = true |
| 257 |
break |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if !foundCompression { |
| 262 |
c.sendAlert(alertHandshakeFailure) |
| 263 |
return false, errors.New("tls: client does not support uncompressed connections") |
| 264 |
} |
| 265 |
|
| 266 |
hs.hello.vers = c.vers |
| 267 |
hs.hello.random = make([]byte, 32) |
| 268 |
_, err = io.ReadFull(config.rand(), hs.hello.random) |
| 269 |
if err != nil { |
| 270 |
c.sendAlert(alertInternalError) |
| 271 |
return false, err |
| 272 |
} |
| 273 |
|
| 274 |
if !bytes.Equal(c.clientVerify, hs.clientHello.secureRenegotiation) { |
| 275 |
c.sendAlert(alertHandshakeFailure) |
| 276 |
return false, errors.New("tls: renegotiation mismatch") |
| 277 |
} |
| 278 |
|
| 279 |
if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 280 |
hs.hello.secureRenegotiation = append(hs.hello.secureRenegotiation, c.clientVerify...) |
| 281 |
hs.hello.secureRenegotiation = append(hs.hello.secureRenegotiation, c.serverVerify...) |
| 282 |
if c.config.Bugs.BadRenegotiationInfo { |
| 283 |
hs.hello.secureRenegotiation[0] ^= 0x80 |
| 284 |
} |
| 285 |
} else { |
| 286 |
hs.hello.secureRenegotiation = hs.clientHello.secureRenegotiation |
| 287 |
} |
| 288 |
|
| 289 |
if c.noRenegotiationInfo() { |
| 290 |
hs.hello.secureRenegotiation = nil |
| 291 |
} |
| 292 |
|
| 293 |
hs.hello.compressionMethod = compressionNone |
| 294 |
hs.hello.duplicateExtension = c.config.Bugs.DuplicateExtension |
| 295 |
if len(hs.clientHello.serverName) > 0 { |
| 296 |
c.serverName = hs.clientHello.serverName |
| 297 |
} |
| 298 |
|
| 299 |
if len(hs.clientHello.alpnProtocols) > 0 { |
| 300 |
if proto := c.config.Bugs.ALPNProtocol; proto != nil { |
| 301 |
hs.hello.alpnProtocol = *proto |
| 302 |
hs.hello.alpnProtocolEmpty = len(*proto) == 0 |
| 303 |
c.clientProtocol = *proto |
| 304 |
c.usedALPN = true |
| 305 |
} else if selectedProto, fallback := mutualProtocol(hs.clientHello.alpnProtocols, c.config.NextProtos); !fallback { |
| 306 |
hs.hello.alpnProtocol = selectedProto |
| 307 |
c.clientProtocol = selectedProto |
| 308 |
c.usedALPN = true |
| 309 |
} |
| 310 |
} |
| 311 |
if len(hs.clientHello.alpnProtocols) == 0 || c.config.Bugs.NegotiateALPNAndNPN { |
| 312 |
// Although sending an empty NPN extension is reasonable, Firefox has |
| 313 |
// had a bug around this. Best to send nothing at all if |
| 314 |
// config.NextProtos is empty. See |
| 315 |
// https://code.google.com/p/go/issues/detail?id=5445. |
| 316 |
if hs.clientHello.nextProtoNeg && len(config.NextProtos) > 0 { |
| 317 |
hs.hello.nextProtoNeg = true |
| 318 |
hs.hello.nextProtos = config.NextProtos |
| 319 |
} |
| 320 |
} |
| 321 |
hs.hello.extendedMasterSecret = c.vers >= VersionTLS10 && hs.clientHello.extendedMasterSecret && !c.config.Bugs.NoExtendedMasterSecret |
| 322 |
|
| 323 |
if len(config.Certificates) == 0 { |
| 324 |
c.sendAlert(alertInternalError) |
| 325 |
return false, errors.New("tls: no certificates configured") |
| 326 |
} |
| 327 |
hs.cert = &config.Certificates[0] |
| 328 |
if len(hs.clientHello.serverName) > 0 { |
| 329 |
hs.cert = config.getCertificateForName(hs.clientHello.serverName) |
| 330 |
} |
| 331 |
if expected := c.config.Bugs.ExpectServerName; expected != "" && expected != hs.clientHello.serverName { |
| 332 |
return false, errors.New("tls: unexpected server name") |
| 333 |
} |
| 334 |
|
| 335 |
if hs.clientHello.channelIDSupported && config.RequestChannelID { |
| 336 |
hs.hello.channelIDRequested = true |
| 337 |
} |
| 338 |
|
| 339 |
if hs.clientHello.srtpProtectionProfiles != nil { |
| 340 |
SRTPLoop: |
| 341 |
for _, p1 := range c.config.SRTPProtectionProfiles { |
| 342 |
for _, p2 := range hs.clientHello.srtpProtectionProfiles { |
| 343 |
if p1 == p2 { |
| 344 |
hs.hello.srtpProtectionProfile = p1 |
| 345 |
c.srtpProtectionProfile = p1 |
| 346 |
break SRTPLoop |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
if c.config.Bugs.SendSRTPProtectionProfile != 0 { |
| 353 |
hs.hello.srtpProtectionProfile = c.config.Bugs.SendSRTPProtectionProfile |
| 354 |
} |
| 355 |
|
| 356 |
if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 357 |
if hs.clientHello.customExtension != *expected { |
| 358 |
return false, fmt.Errorf("tls: bad custom extension contents %q", hs.clientHello.customExtension) |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
_, hs.ecdsaOk = hs.cert.PrivateKey.(*ecdsa.PrivateKey) |
| 363 |
|
| 364 |
// For test purposes, check that the peer never offers a session when |
| 365 |
// renegotiating. |
| 366 |
if c.cipherSuite != nil && len(hs.clientHello.sessionId) > 0 && c.config.Bugs.FailIfResumeOnRenego { |
| 367 |
return false, errors.New("tls: offered resumption on renegotiation") |
| 368 |
} |
| 369 |
|
| 370 |
if c.config.Bugs.FailIfSessionOffered && (len(hs.clientHello.sessionTicket) > 0 || len(hs.clientHello.sessionId) > 0) { |
| 371 |
return false, errors.New("tls: client offered a session ticket or ID") |
| 372 |
} |
| 373 |
|
| 374 |
if hs.checkForResumption() { |
| 375 |
return true, nil |
| 376 |
} |
| 377 |
|
| 378 |
var scsvFound bool |
| 379 |
|
| 380 |
for _, cipherSuite := range hs.clientHello.cipherSuites { |
| 381 |
if cipherSuite == fallbackSCSV { |
| 382 |
scsvFound = true |
| 383 |
break |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
if !scsvFound && config.Bugs.FailIfNotFallbackSCSV { |
| 388 |
return false, errors.New("tls: no fallback SCSV found when expected") |
| 389 |
} else if scsvFound && !config.Bugs.FailIfNotFallbackSCSV { |
| 390 |
return false, errors.New("tls: fallback SCSV found when not expected") |
| 391 |
} |
| 392 |
|
| 393 |
if config.Bugs.IgnorePeerCipherPreferences { |
| 394 |
hs.clientHello.cipherSuites = c.config.cipherSuites() |
| 395 |
} |
| 396 |
var preferenceList, supportedList []uint16 |
| 397 |
if c.config.PreferServerCipherSuites { |
| 398 |
preferenceList = c.config.cipherSuites() |
| 399 |
supportedList = hs.clientHello.cipherSuites |
| 400 |
} else { |
| 401 |
preferenceList = hs.clientHello.cipherSuites |
| 402 |
supportedList = c.config.cipherSuites() |
| 403 |
} |
| 404 |
|
| 405 |
for _, id := range preferenceList { |
| 406 |
if hs.suite = c.tryCipherSuite(id, supportedList, c.vers, hs.ellipticOk, hs.ecdsaOk); hs.suite != nil { |
| 407 |
break |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if hs.suite == nil { |
| 412 |
c.sendAlert(alertHandshakeFailure) |
| 413 |
return false, errors.New("tls: no cipher suite supported by both client and server") |
| 414 |
} |
| 415 |
|
| 416 |
return false, nil |
| 417 |
} |
| 418 |
|
| 419 |
// checkForResumption returns true if we should perform resumption on this connection. |
| 420 |
func (hs *serverHandshakeState) checkForResumption() bool { |
| 421 |
c := hs.c |
| 422 |
|
| 423 |
if len(hs.clientHello.sessionTicket) > 0 { |
| 424 |
if c.config.SessionTicketsDisabled { |
| 425 |
return false |
| 426 |
} |
| 427 |
|
| 428 |
var ok bool |
| 429 |
if hs.sessionState, ok = c.decryptTicket(hs.clientHello.sessionTicket); !ok { |
| 430 |
return false |
| 431 |
} |
| 432 |
} else { |
| 433 |
if c.config.ServerSessionCache == nil { |
| 434 |
return false |
| 435 |
} |
| 436 |
|
| 437 |
var ok bool |
| 438 |
sessionId := string(hs.clientHello.sessionId) |
| 439 |
if hs.sessionState, ok = c.config.ServerSessionCache.Get(sessionId); !ok { |
| 440 |
return false |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
// Never resume a session for a different SSL version. |
| 445 |
if !c.config.Bugs.AllowSessionVersionMismatch && c.vers != hs.sessionState.vers { |
| 446 |
return false |
| 447 |
} |
| 448 |
|
| 449 |
cipherSuiteOk := false |
| 450 |
// Check that the client is still offering the ciphersuite in the session. |
| 451 |
for _, id := range hs.clientHello.cipherSuites { |
| 452 |
if id == hs.sessionState.cipherSuite { |
| 453 |
cipherSuiteOk = true |
| 454 |
break |
| 455 |
} |
| 456 |
} |
| 457 |
if !cipherSuiteOk { |
| 458 |
return false |
| 459 |
} |
| 460 |
|
| 461 |
// Check that we also support the ciphersuite from the session. |
| 462 |
hs.suite = c.tryCipherSuite(hs.sessionState.cipherSuite, c.config.cipherSuites(), hs.sessionState.vers, hs.ellipticOk, hs.ecdsaOk) |
| 463 |
if hs.suite == nil { |
| 464 |
return false |
| 465 |
} |
| 466 |
|
| 467 |
sessionHasClientCerts := len(hs.sessionState.certificates) != 0 |
| 468 |
needClientCerts := c.config.ClientAuth == RequireAnyClientCert || c.config.ClientAuth == RequireAndVerifyClientCert |
| 469 |
if needClientCerts && !sessionHasClientCerts { |
| 470 |
return false |
| 471 |
} |
| 472 |
if sessionHasClientCerts && c.config.ClientAuth == NoClientCert { |
| 473 |
return false |
| 474 |
} |
| 475 |
|
| 476 |
return true |
| 477 |
} |
| 478 |
|
| 479 |
func (hs *serverHandshakeState) doResumeHandshake() error { |
| 480 |
c := hs.c |
| 481 |
|
| 482 |
hs.hello.cipherSuite = hs.suite.id |
| 483 |
if c.config.Bugs.SendCipherSuite != 0 { |
| 484 |
hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite |
| 485 |
} |
| 486 |
// We echo the client's session ID in the ServerHello to let it know |
| 487 |
// that we're doing a resumption. |
| 488 |
hs.hello.sessionId = hs.clientHello.sessionId |
| 489 |
hs.hello.ticketSupported = c.config.Bugs.RenewTicketOnResume |
| 490 |
|
| 491 |
if c.config.Bugs.SendSCTListOnResume != nil { |
| 492 |
hs.hello.sctList = c.config.Bugs.SendSCTListOnResume |
| 493 |
} |
| 494 |
|
| 495 |
hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
| 496 |
hs.finishedHash.discardHandshakeBuffer() |
| 497 |
hs.writeClientHash(hs.clientHello.marshal()) |
| 498 |
hs.writeServerHash(hs.hello.marshal()) |
| 499 |
|
| 500 |
c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 501 |
|
| 502 |
if len(hs.sessionState.certificates) > 0 { |
| 503 |
if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil { |
| 504 |
return err |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
hs.masterSecret = hs.sessionState.masterSecret |
| 509 |
c.extendedMasterSecret = hs.sessionState.extendedMasterSecret |
| 510 |
|
| 511 |
return nil |
| 512 |
} |
| 513 |
|
| 514 |
func (hs *serverHandshakeState) doFullHandshake() error { |
| 515 |
config := hs.c.config |
| 516 |
c := hs.c |
| 517 |
|
| 518 |
isPSK := hs.suite.flags&suitePSK != 0 |
| 519 |
if !isPSK && hs.clientHello.ocspStapling && len(hs.cert.OCSPStaple) > 0 { |
| 520 |
hs.hello.ocspStapling = true |
| 521 |
} |
| 522 |
|
| 523 |
if hs.clientHello.sctListSupported && len(hs.cert.SignedCertificateTimestampList) > 0 { |
| 524 |
hs.hello.sctList = hs.cert.SignedCertificateTimestampList |
| 525 |
} |
| 526 |
|
| 527 |
hs.hello.ticketSupported = hs.clientHello.ticketSupported && !config.SessionTicketsDisabled && c.vers > VersionSSL30 |
| 528 |
hs.hello.cipherSuite = hs.suite.id |
| 529 |
if config.Bugs.SendCipherSuite != 0 { |
| 530 |
hs.hello.cipherSuite = config.Bugs.SendCipherSuite |
| 531 |
} |
| 532 |
c.extendedMasterSecret = hs.hello.extendedMasterSecret |
| 533 |
|
| 534 |
// Generate a session ID if we're to save the session. |
| 535 |
if !hs.hello.ticketSupported && config.ServerSessionCache != nil { |
| 536 |
hs.hello.sessionId = make([]byte, 32) |
| 537 |
if _, err := io.ReadFull(config.rand(), hs.hello.sessionId); err != nil { |
| 538 |
c.sendAlert(alertInternalError) |
| 539 |
return errors.New("tls: short read from Rand: " + err.Error()) |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
| 544 |
hs.writeClientHash(hs.clientHello.marshal()) |
| 545 |
hs.writeServerHash(hs.hello.marshal()) |
| 546 |
|
| 547 |
c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 548 |
|
| 549 |
if !isPSK { |
| 550 |
certMsg := new(certificateMsg) |
| 551 |
if !config.Bugs.EmptyCertificateList { |
| 552 |
certMsg.certificates = hs.cert.Certificate |
| 553 |
} |
| 554 |
if !config.Bugs.UnauthenticatedECDH { |
| 555 |
certMsgBytes := certMsg.marshal() |
| 556 |
if config.Bugs.WrongCertificateMessageType { |
| 557 |
certMsgBytes[0] += 42 |
| 558 |
} |
| 559 |
hs.writeServerHash(certMsgBytes) |
| 560 |
c.writeRecord(recordTypeHandshake, certMsgBytes) |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
if hs.hello.ocspStapling && !c.config.Bugs.SkipCertificateStatus { |
| 565 |
certStatus := new(certificateStatusMsg) |
| 566 |
certStatus.statusType = statusTypeOCSP |
| 567 |
certStatus.response = hs.cert.OCSPStaple |
| 568 |
hs.writeServerHash(certStatus.marshal()) |
| 569 |
c.writeRecord(recordTypeHandshake, certStatus.marshal()) |
| 570 |
} |
| 571 |
|
| 572 |
keyAgreement := hs.suite.ka(c.vers) |
| 573 |
skx, err := keyAgreement.generateServerKeyExchange(config, hs.cert, hs.clientHello, hs.hello) |
| 574 |
if err != nil { |
| 575 |
c.sendAlert(alertHandshakeFailure) |
| 576 |
return err |
| 577 |
} |
| 578 |
if skx != nil && !config.Bugs.SkipServerKeyExchange { |
| 579 |
hs.writeServerHash(skx.marshal()) |
| 580 |
c.writeRecord(recordTypeHandshake, skx.marshal()) |
| 581 |
} |
| 582 |
|
| 583 |
if config.ClientAuth >= RequestClientCert { |
| 584 |
// Request a client certificate |
| 585 |
certReq := &certificateRequestMsg{ |
| 586 |
certificateTypes: config.ClientCertificateTypes, |
| 587 |
} |
| 588 |
if certReq.certificateTypes == nil { |
| 589 |
certReq.certificateTypes = []byte{ |
| 590 |
byte(CertTypeRSASign), |
| 591 |
byte(CertTypeECDSASign), |
| 592 |
} |
| 593 |
} |
| 594 |
if c.vers >= VersionTLS12 { |
| 595 |
certReq.hasSignatureAndHash = true |
| 596 |
if !config.Bugs.NoSignatureAndHashes { |
| 597 |
certReq.signatureAndHashes = config.signatureAndHashesForServer() |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
// An empty list of certificateAuthorities signals to |
| 602 |
// the client that it may send any certificate in response |
| 603 |
// to our request. When we know the CAs we trust, then |
| 604 |
// we can send them down, so that the client can choose |
| 605 |
// an appropriate certificate to give to us. |
| 606 |
if config.ClientCAs != nil { |
| 607 |
certReq.certificateAuthorities = config.ClientCAs.Subjects() |
| 608 |
} |
| 609 |
hs.writeServerHash(certReq.marshal()) |
| 610 |
c.writeRecord(recordTypeHandshake, certReq.marshal()) |
| 611 |
} |
| 612 |
|
| 613 |
helloDone := new(serverHelloDoneMsg) |
| 614 |
hs.writeServerHash(helloDone.marshal()) |
| 615 |
c.writeRecord(recordTypeHandshake, helloDone.marshal()) |
| 616 |
c.dtlsFlushHandshake() |
| 617 |
|
| 618 |
var pub crypto.PublicKey // public key for client auth, if any |
| 619 |
|
| 620 |
if err := c.simulatePacketLoss(nil); err != nil { |
| 621 |
return err |
| 622 |
} |
| 623 |
msg, err := c.readHandshake() |
| 624 |
if err != nil { |
| 625 |
return err |
| 626 |
} |
| 627 |
|
| 628 |
var ok bool |
| 629 |
// If we requested a client certificate, then the client must send a |
| 630 |
// certificate message, even if it's empty. |
| 631 |
if config.ClientAuth >= RequestClientCert { |
| 632 |
var certMsg *certificateMsg |
| 633 |
var certificates [][]byte |
| 634 |
if certMsg, ok = msg.(*certificateMsg); ok { |
| 635 |
if c.vers == VersionSSL30 && len(certMsg.certificates) == 0 { |
| 636 |
return errors.New("tls: empty certificate message in SSL 3.0") |
| 637 |
} |
| 638 |
|
| 639 |
hs.writeClientHash(certMsg.marshal()) |
| 640 |
certificates = certMsg.certificates |
| 641 |
} else if c.vers != VersionSSL30 { |
| 642 |
// In TLS, the Certificate message is required. In SSL |
| 643 |
// 3.0, the peer skips it when sending no certificates. |
| 644 |
c.sendAlert(alertUnexpectedMessage) |
| 645 |
return unexpectedMessageError(certMsg, msg) |
| 646 |
} |
| 647 |
|
| 648 |
if len(certificates) == 0 { |
| 649 |
// The client didn't actually send a certificate |
| 650 |
switch config.ClientAuth { |
| 651 |
case RequireAnyClientCert, RequireAndVerifyClientCert: |
| 652 |
c.sendAlert(alertBadCertificate) |
| 653 |
return errors.New("tls: client didn't provide a certificate") |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
pub, err = hs.processCertsFromClient(certificates) |
| 658 |
if err != nil { |
| 659 |
return err |
| 660 |
} |
| 661 |
|
| 662 |
if ok { |
| 663 |
msg, err = c.readHandshake() |
| 664 |
if err != nil { |
| 665 |
return err |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
// Get client key exchange |
| 671 |
ckx, ok := msg.(*clientKeyExchangeMsg) |
| 672 |
if !ok { |
| 673 |
c.sendAlert(alertUnexpectedMessage) |
| 674 |
return unexpectedMessageError(ckx, msg) |
| 675 |
} |
| 676 |
hs.writeClientHash(ckx.marshal()) |
| 677 |
|
| 678 |
preMasterSecret, err := keyAgreement.processClientKeyExchange(config, hs.cert, ckx, c.vers) |
| 679 |
if err != nil { |
| 680 |
c.sendAlert(alertHandshakeFailure) |
| 681 |
return err |
| 682 |
} |
| 683 |
if c.extendedMasterSecret { |
| 684 |
hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 685 |
} else { |
| 686 |
if c.config.Bugs.RequireExtendedMasterSecret { |
| 687 |
return errors.New("tls: extended master secret required but not supported by peer") |
| 688 |
} |
| 689 |
hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.clientHello.random, hs.hello.random) |
| 690 |
} |
| 691 |
|
| 692 |
// If we received a client cert in response to our certificate request message, |
| 693 |
// the client will send us a certificateVerifyMsg immediately after the |
| 694 |
// clientKeyExchangeMsg. This message is a digest of all preceding |
| 695 |
// handshake-layer messages that is signed using the private key corresponding |
| 696 |
// to the client's certificate. This allows us to verify that the client is in |
| 697 |
// possession of the private key of the certificate. |
| 698 |
if len(c.peerCertificates) > 0 { |
| 699 |
msg, err = c.readHandshake() |
| 700 |
if err != nil { |
| 701 |
return err |
| 702 |
} |
| 703 |
certVerify, ok := msg.(*certificateVerifyMsg) |
| 704 |
if !ok { |
| 705 |
c.sendAlert(alertUnexpectedMessage) |
| 706 |
return unexpectedMessageError(certVerify, msg) |
| 707 |
} |
| 708 |
|
| 709 |
// Determine the signature type. |
| 710 |
var signatureAndHash signatureAndHash |
| 711 |
if certVerify.hasSignatureAndHash { |
| 712 |
signatureAndHash = certVerify.signatureAndHash |
| 713 |
if !isSupportedSignatureAndHash(signatureAndHash, config.signatureAndHashesForServer()) { |
| 714 |
return errors.New("tls: unsupported hash function for client certificate") |
| 715 |
} |
| 716 |
c.clientCertSignatureHash = signatureAndHash.hash |
| 717 |
} else { |
| 718 |
// Before TLS 1.2 the signature algorithm was implicit |
| 719 |
// from the key type, and only one hash per signature |
| 720 |
// algorithm was possible. Leave the hash as zero. |
| 721 |
switch pub.(type) { |
| 722 |
case *ecdsa.PublicKey: |
| 723 |
signatureAndHash.signature = signatureECDSA |
| 724 |
case *rsa.PublicKey: |
| 725 |
signatureAndHash.signature = signatureRSA |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
switch key := pub.(type) { |
| 730 |
case *ecdsa.PublicKey: |
| 731 |
if signatureAndHash.signature != signatureECDSA { |
| 732 |
err = errors.New("tls: bad signature type for client's ECDSA certificate") |
| 733 |
break |
| 734 |
} |
| 735 |
ecdsaSig := new(ecdsaSignature) |
| 736 |
if _, err = asn1.Unmarshal(certVerify.signature, ecdsaSig); err != nil { |
| 737 |
break |
| 738 |
} |
| 739 |
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 { |
| 740 |
err = errors.New("ECDSA signature contained zero or negative values") |
| 741 |
break |
| 742 |
} |
| 743 |
var digest []byte |
| 744 |
digest, _, err = hs.finishedHash.hashForClientCertificate(signatureAndHash, hs.masterSecret) |
| 745 |
if err != nil { |
| 746 |
break |
| 747 |
} |
| 748 |
if !ecdsa.Verify(key, digest, ecdsaSig.R, ecdsaSig.S) { |
| 749 |
err = errors.New("ECDSA verification failure") |
| 750 |
break |
| 751 |
} |
| 752 |
case *rsa.PublicKey: |
| 753 |
if signatureAndHash.signature != signatureRSA { |
| 754 |
err = errors.New("tls: bad signature type for client's RSA certificate") |
| 755 |
break |
| 756 |
} |
| 757 |
var digest []byte |
| 758 |
var hashFunc crypto.Hash |
| 759 |
digest, hashFunc, err = hs.finishedHash.hashForClientCertificate(signatureAndHash, hs.masterSecret) |
| 760 |
if err != nil { |
| 761 |
break |
| 762 |
} |
| 763 |
err = rsa.VerifyPKCS1v15(key, hashFunc, digest, certVerify.signature) |
| 764 |
} |
| 765 |
if err != nil { |
| 766 |
c.sendAlert(alertBadCertificate) |
| 767 |
return errors.New("could not validate signature of connection nonces: " + err.Error()) |
| 768 |
} |
| 769 |
|
| 770 |
hs.writeClientHash(certVerify.marshal()) |
| 771 |
} |
| 772 |
|
| 773 |
hs.finishedHash.discardHandshakeBuffer() |
| 774 |
|
| 775 |
return nil |
| 776 |
} |
| 777 |
|
| 778 |
func (hs *serverHandshakeState) establishKeys() error { |
| 779 |
c := hs.c |
| 780 |
|
| 781 |
clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
| 782 |
keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.clientHello.random, hs.hello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen) |
| 783 |
|
| 784 |
var clientCipher, serverCipher interface{} |
| 785 |
var clientHash, serverHash macFunction |
| 786 |
|
| 787 |
if hs.suite.aead == nil { |
| 788 |
clientCipher = hs.suite.cipher(clientKey, clientIV, true /* for reading */) |
| 789 |
clientHash = hs.suite.mac(c.vers, clientMAC) |
| 790 |
serverCipher = hs.suite.cipher(serverKey, serverIV, false /* not for reading */) |
| 791 |
serverHash = hs.suite.mac(c.vers, serverMAC) |
| 792 |
} else { |
| 793 |
clientCipher = hs.suite.aead(clientKey, clientIV) |
| 794 |
serverCipher = hs.suite.aead(serverKey, serverIV) |
| 795 |
} |
| 796 |
|
| 797 |
c.in.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 798 |
c.out.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 799 |
|
| 800 |
return nil |
| 801 |
} |
| 802 |
|
| 803 |
func (hs *serverHandshakeState) readFinished(out []byte, isResume bool) error { |
| 804 |
c := hs.c |
| 805 |
|
| 806 |
c.readRecord(recordTypeChangeCipherSpec) |
| 807 |
if err := c.in.error(); err != nil { |
| 808 |
return err |
| 809 |
} |
| 810 |
|
| 811 |
if hs.hello.nextProtoNeg { |
| 812 |
msg, err := c.readHandshake() |
| 813 |
if err != nil { |
| 814 |
return err |
| 815 |
} |
| 816 |
nextProto, ok := msg.(*nextProtoMsg) |
| 817 |
if !ok { |
| 818 |
c.sendAlert(alertUnexpectedMessage) |
| 819 |
return unexpectedMessageError(nextProto, msg) |
| 820 |
} |
| 821 |
hs.writeClientHash(nextProto.marshal()) |
| 822 |
c.clientProtocol = nextProto.proto |
| 823 |
} |
| 824 |
|
| 825 |
if hs.hello.channelIDRequested { |
| 826 |
msg, err := c.readHandshake() |
| 827 |
if err != nil { |
| 828 |
return err |
| 829 |
} |
| 830 |
encryptedExtensions, ok := msg.(*encryptedExtensionsMsg) |
| 831 |
if !ok { |
| 832 |
c.sendAlert(alertUnexpectedMessage) |
| 833 |
return unexpectedMessageError(encryptedExtensions, msg) |
| 834 |
} |
| 835 |
x := new(big.Int).SetBytes(encryptedExtensions.channelID[0:32]) |
| 836 |
y := new(big.Int).SetBytes(encryptedExtensions.channelID[32:64]) |
| 837 |
r := new(big.Int).SetBytes(encryptedExtensions.channelID[64:96]) |
| 838 |
s := new(big.Int).SetBytes(encryptedExtensions.channelID[96:128]) |
| 839 |
if !elliptic.P256().IsOnCurve(x, y) { |
| 840 |
return errors.New("tls: invalid channel ID public key") |
| 841 |
} |
| 842 |
channelID := &ecdsa.PublicKey{elliptic.P256(), x, y} |
| 843 |
var resumeHash []byte |
| 844 |
if isResume { |
| 845 |
resumeHash = hs.sessionState.handshakeHash |
| 846 |
} |
| 847 |
if !ecdsa.Verify(channelID, hs.finishedHash.hashForChannelID(resumeHash), r, s) { |
| 848 |
return errors.New("tls: invalid channel ID signature") |
| 849 |
} |
| 850 |
c.channelID = channelID |
| 851 |
|
| 852 |
hs.writeClientHash(encryptedExtensions.marshal()) |
| 853 |
} |
| 854 |
|
| 855 |
msg, err := c.readHandshake() |
| 856 |
if err != nil { |
| 857 |
return err |
| 858 |
} |
| 859 |
clientFinished, ok := msg.(*finishedMsg) |
| 860 |
if !ok { |
| 861 |
c.sendAlert(alertUnexpectedMessage) |
| 862 |
return unexpectedMessageError(clientFinished, msg) |
| 863 |
} |
| 864 |
|
| 865 |
verify := hs.finishedHash.clientSum(hs.masterSecret) |
| 866 |
if len(verify) != len(clientFinished.verifyData) || |
| 867 |
subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 868 |
c.sendAlert(alertHandshakeFailure) |
| 869 |
return errors.New("tls: client's Finished message is incorrect") |
| 870 |
} |
| 871 |
c.clientVerify = append(c.clientVerify[:0], clientFinished.verifyData...) |
| 872 |
copy(out, clientFinished.verifyData) |
| 873 |
|
| 874 |
hs.writeClientHash(clientFinished.marshal()) |
| 875 |
return nil |
| 876 |
} |
| 877 |
|
| 878 |
func (hs *serverHandshakeState) sendSessionTicket() error { |
| 879 |
c := hs.c |
| 880 |
state := sessionState{ |
| 881 |
vers: c.vers, |
| 882 |
cipherSuite: hs.suite.id, |
| 883 |
masterSecret: hs.masterSecret, |
| 884 |
certificates: hs.certsFromClient, |
| 885 |
handshakeHash: hs.finishedHash.server.Sum(nil), |
| 886 |
} |
| 887 |
|
| 888 |
if !hs.hello.ticketSupported || hs.c.config.Bugs.SkipNewSessionTicket { |
| 889 |
if c.config.ServerSessionCache != nil && len(hs.hello.sessionId) != 0 { |
| 890 |
c.config.ServerSessionCache.Put(string(hs.hello.sessionId), &state) |
| 891 |
} |
| 892 |
return nil |
| 893 |
} |
| 894 |
|
| 895 |
m := new(newSessionTicketMsg) |
| 896 |
|
| 897 |
if !c.config.Bugs.SendEmptySessionTicket { |
| 898 |
var err error |
| 899 |
m.ticket, err = c.encryptTicket(&state) |
| 900 |
if err != nil { |
| 901 |
return err |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
hs.writeServerHash(m.marshal()) |
| 906 |
c.writeRecord(recordTypeHandshake, m.marshal()) |
| 907 |
|
| 908 |
return nil |
| 909 |
} |
| 910 |
|
| 911 |
func (hs *serverHandshakeState) sendFinished(out []byte) error { |
| 912 |
c := hs.c |
| 913 |
|
| 914 |
finished := new(finishedMsg) |
| 915 |
finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret) |
| 916 |
copy(out, finished.verifyData) |
| 917 |
if c.config.Bugs.BadFinished { |
| 918 |
finished.verifyData[0]++ |
| 919 |
} |
| 920 |
c.serverVerify = append(c.serverVerify[:0], finished.verifyData...) |
| 921 |
hs.finishedBytes = finished.marshal() |
| 922 |
hs.writeServerHash(hs.finishedBytes) |
| 923 |
postCCSBytes := hs.finishedBytes |
| 924 |
|
| 925 |
if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 926 |
c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 927 |
postCCSBytes = postCCSBytes[5:] |
| 928 |
} |
| 929 |
c.dtlsFlushHandshake() |
| 930 |
|
| 931 |
if !c.config.Bugs.SkipChangeCipherSpec { |
| 932 |
ccs := []byte{1} |
| 933 |
if c.config.Bugs.BadChangeCipherSpec != nil { |
| 934 |
ccs = c.config.Bugs.BadChangeCipherSpec |
| 935 |
} |
| 936 |
c.writeRecord(recordTypeChangeCipherSpec, ccs) |
| 937 |
} |
| 938 |
|
| 939 |
if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 940 |
c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 941 |
} |
| 942 |
if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 943 |
c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 944 |
return errors.New("tls: simulating post-CCS alert") |
| 945 |
} |
| 946 |
|
| 947 |
if !c.config.Bugs.SkipFinished { |
| 948 |
c.writeRecord(recordTypeHandshake, postCCSBytes) |
| 949 |
c.dtlsFlushHandshake() |
| 950 |
} |
| 951 |
|
| 952 |
c.cipherSuite = hs.suite |
| 953 |
|
| 954 |
return nil |
| 955 |
} |
| 956 |
|
| 957 |
// processCertsFromClient takes a chain of client certificates either from a |
| 958 |
// Certificates message or from a sessionState and verifies them. It returns |
| 959 |
// the public key of the leaf certificate. |
| 960 |
func (hs *serverHandshakeState) processCertsFromClient(certificates [][]byte) (crypto.PublicKey, error) { |
| 961 |
c := hs.c |
| 962 |
|
| 963 |
hs.certsFromClient = certificates |
| 964 |
certs := make([]*x509.Certificate, len(certificates)) |
| 965 |
var err error |
| 966 |
for i, asn1Data := range certificates { |
| 967 |
if certs[i], err = x509.ParseCertificate(asn1Data); err != nil { |
| 968 |
c.sendAlert(alertBadCertificate) |
| 969 |
return nil, errors.New("tls: failed to parse client certificate: " + err.Error()) |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
if c.config.ClientAuth >= VerifyClientCertIfGiven && len(certs) > 0 { |
| 974 |
opts := x509.VerifyOptions{ |
| 975 |
Roots: c.config.ClientCAs, |
| 976 |
CurrentTime: c.config.time(), |
| 977 |
Intermediates: x509.NewCertPool(), |
| 978 |
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, |
| 979 |
} |
| 980 |
|
| 981 |
for _, cert := range certs[1:] { |
| 982 |
opts.Intermediates.AddCert(cert) |
| 983 |
} |
| 984 |
|
| 985 |
chains, err := certs[0].Verify(opts) |
| 986 |
if err != nil { |
| 987 |
c.sendAlert(alertBadCertificate) |
| 988 |
return nil, errors.New("tls: failed to verify client's certificate: " + err.Error()) |
| 989 |
} |
| 990 |
|
| 991 |
ok := false |
| 992 |
for _, ku := range certs[0].ExtKeyUsage { |
| 993 |
if ku == x509.ExtKeyUsageClientAuth { |
| 994 |
ok = true |
| 995 |
break |
| 996 |
} |
| 997 |
} |
| 998 |
if !ok { |
| 999 |
c.sendAlert(alertHandshakeFailure) |
| 1000 |
return nil, errors.New("tls: client's certificate's extended key usage doesn't permit it to be used for client authentication") |
| 1001 |
} |
| 1002 |
|
| 1003 |
c.verifiedChains = chains |
| 1004 |
} |
| 1005 |
|
| 1006 |
if len(certs) > 0 { |
| 1007 |
var pub crypto.PublicKey |
| 1008 |
switch key := certs[0].PublicKey.(type) { |
| 1009 |
case *ecdsa.PublicKey, *rsa.PublicKey: |
| 1010 |
pub = key |
| 1011 |
default: |
| 1012 |
c.sendAlert(alertUnsupportedCertificate) |
| 1013 |
return nil, fmt.Errorf("tls: client's certificate contains an unsupported public key of type %T", certs[0].PublicKey) |
| 1014 |
} |
| 1015 |
c.peerCertificates = certs |
| 1016 |
return pub, nil |
| 1017 |
} |
| 1018 |
|
| 1019 |
return nil, nil |
| 1020 |
} |
| 1021 |
|
| 1022 |
func (hs *serverHandshakeState) writeServerHash(msg []byte) { |
| 1023 |
// writeServerHash is called before writeRecord. |
| 1024 |
hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 1025 |
} |
| 1026 |
|
| 1027 |
func (hs *serverHandshakeState) writeClientHash(msg []byte) { |
| 1028 |
// writeClientHash is called after readHandshake. |
| 1029 |
hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 1030 |
} |
| 1031 |
|
| 1032 |
func (hs *serverHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 1033 |
if hs.c.isDTLS { |
| 1034 |
// This is somewhat hacky. DTLS hashes a slightly different format. |
| 1035 |
// First, the TLS header. |
| 1036 |
hs.finishedHash.Write(msg[:4]) |
| 1037 |
// Then the sequence number and reassembled fragment offset (always 0). |
| 1038 |
hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 1039 |
// Then the reassembled fragment (always equal to the message length). |
| 1040 |
hs.finishedHash.Write(msg[1:4]) |
| 1041 |
// And then the message body. |
| 1042 |
hs.finishedHash.Write(msg[4:]) |
| 1043 |
} else { |
| 1044 |
hs.finishedHash.Write(msg) |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
// tryCipherSuite returns a cipherSuite with the given id if that cipher suite |
| 1049 |
// is acceptable to use. |
| 1050 |
func (c *Conn) tryCipherSuite(id uint16, supportedCipherSuites []uint16, version uint16, ellipticOk, ecdsaOk bool) *cipherSuite { |
| 1051 |
for _, supported := range supportedCipherSuites { |
| 1052 |
if id == supported { |
| 1053 |
var candidate *cipherSuite |
| 1054 |
|
| 1055 |
for _, s := range cipherSuites { |
| 1056 |
if s.id == id { |
| 1057 |
candidate = s |
| 1058 |
break |
| 1059 |
} |
| 1060 |
} |
| 1061 |
if candidate == nil { |
| 1062 |
continue |
| 1063 |
} |
| 1064 |
// Don't select a ciphersuite which we can't |
| 1065 |
// support for this client. |
| 1066 |
if (candidate.flags&suiteECDHE != 0) && !ellipticOk { |
| 1067 |
continue |
| 1068 |
} |
| 1069 |
if (candidate.flags&suiteECDSA != 0) != ecdsaOk { |
| 1070 |
continue |
| 1071 |
} |
| 1072 |
if !c.config.Bugs.SkipCipherVersionCheck && version < VersionTLS12 && candidate.flags&suiteTLS12 != 0 { |
| 1073 |
continue |
| 1074 |
} |
| 1075 |
if c.isDTLS && candidate.flags&suiteNoDTLS != 0 { |
| 1076 |
continue |
| 1077 |
} |
| 1078 |
return candidate |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
return nil |
| 1083 |
} |
| 1084 |
|
| 1085 |
func isTLS12Cipher(id uint16) bool { |
| 1086 |
for _, cipher := range cipherSuites { |
| 1087 |
if cipher.id != id { |
| 1088 |
continue |
| 1089 |
} |
| 1090 |
return cipher.flags&suiteTLS12 != 0 |
| 1091 |
} |
| 1092 |
// Unknown cipher. |
| 1093 |
return false |
| 1094 |
} |