2014-06-20 20:00:00 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-09-29 23:21:04 +01:00
|
|
|
package runner
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
import (
|
2014-08-04 06:23:53 +01:00
|
|
|
"bytes"
|
2014-06-20 20:00:00 +01:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
2014-08-24 06:44:23 +01:00
|
|
|
"crypto/elliptic"
|
2014-06-20 20:00:00 +01:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/subtle"
|
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-08-24 06:44:23 +01:00
|
|
|
"math/big"
|
2016-11-01 17:39:36 +00:00
|
|
|
"time"
|
2017-03-29 00:28:44 +01:00
|
|
|
|
2018-09-07 20:51:08 +01:00
|
|
|
"boringssl.googlesource.com/boringssl/ssl/test/runner/ed25519"
|
2014-06-20 20:00:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// serverHandshakeState contains details of a server handshake in progress.
|
|
|
|
// It's discarded once the handshake has completed.
|
|
|
|
type serverHandshakeState struct {
|
|
|
|
c *Conn
|
|
|
|
clientHello *clientHelloMsg
|
|
|
|
hello *serverHelloMsg
|
|
|
|
suite *cipherSuite
|
|
|
|
ellipticOk bool
|
|
|
|
ecdsaOk bool
|
|
|
|
sessionState *sessionState
|
|
|
|
finishedHash finishedHash
|
|
|
|
masterSecret []byte
|
|
|
|
certsFromClient [][]byte
|
|
|
|
cert *Certificate
|
Add DTLS timeout and retransmit tests.
This extends the packet adaptor protocol to send three commands:
type command =
| Packet of []byte
| Timeout of time.Duration
| TimeoutAck
When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the
BIO_read, returns out of the SSL stack, advances the clock, calls
DTLSv1_handle_timeout, and continues.
If the Go side sends Timeout right between sending handshake flight N and
reading flight N+1, the shim won't read the Timeout until it has sent flight
N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes
after N+1. Go then drops all packets before the TimeoutAck, thus dropping one
transmit of flight N+1 without having to actually process the packets to
determine the end of the flight. The shim then sees the updated clock, calls
DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real.
When dropping packets, Go checks the epoch and increments sequence numbers so
that we can continue to be strict here. This requires tracking the initial
sequence number of the next epoch.
The final Finished message takes an additional special-case to test. DTLS
triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only
implements the former which should be sufficient (and is necessary) EXCEPT for
the final Finished message. If the peer's final Finished message is lost, it
won't be waiting for a message from us, so it won't time out anything. That
retransmit must be triggered on stale message, so we retransmit the Finished
message in Go.
Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c
Reviewed-on: https://boringssl-review.googlesource.com/3212
Reviewed-by: Adam Langley <agl@google.com>
2015-01-27 06:09:43 +00:00
|
|
|
finishedBytes []byte
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// serverHandshake performs a TLS handshake as a server.
|
|
|
|
func (c *Conn) serverHandshake() error {
|
|
|
|
config := c.config
|
|
|
|
|
|
|
|
// If this is the first server handshake, we generate a random key to
|
|
|
|
// encrypt the tickets with.
|
|
|
|
config.serverInitOnce.Do(config.serverInit)
|
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
c.sendHandshakeSeq = 0
|
|
|
|
c.recvHandshakeSeq = 0
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
hs := serverHandshakeState{
|
|
|
|
c: c,
|
|
|
|
}
|
2016-07-04 18:05:26 +01:00
|
|
|
if err := hs.readClientHello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-07-18 00:03:18 +01:00
|
|
|
if c.vers >= VersionTLS13 {
|
2016-07-08 01:36:52 +01:00
|
|
|
if err := hs.doTLS13Handshake(); err != nil {
|
2014-06-20 20:00:00 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
} else {
|
|
|
|
isResume, err := hs.processClientHello()
|
|
|
|
if err != nil {
|
2014-06-20 20:00:00 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
|
|
|
|
// For an overview of TLS handshaking, see https://tools.ietf.org/html/rfc5246#section-7.3
|
|
|
|
if isResume {
|
|
|
|
// The client has included a session ticket and so we do an abbreviated handshake.
|
|
|
|
if err := hs.doResumeHandshake(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hs.establishKeys(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.config.Bugs.RenewTicketOnResume {
|
|
|
|
if err := hs.sendSessionTicket(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 22:14:45 +01:00
|
|
|
if err := hs.sendFinished(c.firstFinished[:], isResume); err != nil {
|
2016-07-08 01:36:52 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Most retransmits are triggered by a timeout, but the final
|
|
|
|
// leg of the handshake is retransmited upon re-receiving a
|
|
|
|
// Finished.
|
|
|
|
if err := c.simulatePacketLoss(func() {
|
2016-07-27 22:40:37 +01:00
|
|
|
c.sendHandshakeSeq--
|
2016-07-08 01:36:52 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, hs.finishedBytes)
|
|
|
|
c.flushHandshake()
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hs.readFinished(nil, isResume); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.didResume = true
|
|
|
|
} else {
|
|
|
|
// The client didn't include a session ticket, or it wasn't
|
|
|
|
// valid so we do a full handshake.
|
|
|
|
if err := hs.doFullHandshake(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hs.establishKeys(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hs.readFinished(c.firstFinished[:], isResume); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.config.Bugs.AlertBeforeFalseStartTest != 0 {
|
|
|
|
c.sendAlert(c.config.Bugs.AlertBeforeFalseStartTest)
|
|
|
|
}
|
|
|
|
if c.config.Bugs.ExpectFalseStart {
|
|
|
|
if err := c.readRecord(recordTypeApplicationData); err != nil {
|
|
|
|
return fmt.Errorf("tls: peer did not false start: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 00:13:38 +01:00
|
|
|
if err := hs.sendSessionTicket(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-12 22:14:45 +01:00
|
|
|
if err := hs.sendFinished(nil, isResume); err != nil {
|
2016-07-08 01:36:52 +01:00
|
|
|
return err
|
2014-08-24 08:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 22:57:35 +01:00
|
|
|
|
|
|
|
c.exporterSecret = hs.masterSecret
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
c.handshakeComplete = true
|
2015-04-03 09:06:36 +01:00
|
|
|
copy(c.clientRandom[:], hs.clientHello.random)
|
|
|
|
copy(c.serverRandom[:], hs.hello.random)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-04 18:05:26 +01:00
|
|
|
// readClientHello reads a ClientHello message from the client and determines
|
|
|
|
// the protocol version.
|
|
|
|
func (hs *serverHandshakeState) readClientHello() error {
|
2014-06-20 20:00:00 +01:00
|
|
|
config := hs.c.config
|
|
|
|
c := hs.c
|
|
|
|
|
Add DTLS timeout and retransmit tests.
This extends the packet adaptor protocol to send three commands:
type command =
| Packet of []byte
| Timeout of time.Duration
| TimeoutAck
When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the
BIO_read, returns out of the SSL stack, advances the clock, calls
DTLSv1_handle_timeout, and continues.
If the Go side sends Timeout right between sending handshake flight N and
reading flight N+1, the shim won't read the Timeout until it has sent flight
N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes
after N+1. Go then drops all packets before the TimeoutAck, thus dropping one
transmit of flight N+1 without having to actually process the packets to
determine the end of the flight. The shim then sees the updated clock, calls
DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real.
When dropping packets, Go checks the epoch and increments sequence numbers so
that we can continue to be strict here. This requires tracking the initial
sequence number of the next epoch.
The final Finished message takes an additional special-case to test. DTLS
triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only
implements the former which should be sufficient (and is necessary) EXCEPT for
the final Finished message. If the peer's final Finished message is lost, it
won't be waiting for a message from us, so it won't time out anything. That
retransmit must be triggered on stale message, so we retransmit the Finished
message in Go.
Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c
Reviewed-on: https://boringssl-review.googlesource.com/3212
Reviewed-by: Adam Langley <agl@google.com>
2015-01-27 06:09:43 +00:00
|
|
|
if err := c.simulatePacketLoss(nil); err != nil {
|
2016-07-04 18:05:26 +01:00
|
|
|
return err
|
Add DTLS timeout and retransmit tests.
This extends the packet adaptor protocol to send three commands:
type command =
| Packet of []byte
| Timeout of time.Duration
| TimeoutAck
When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the
BIO_read, returns out of the SSL stack, advances the clock, calls
DTLSv1_handle_timeout, and continues.
If the Go side sends Timeout right between sending handshake flight N and
reading flight N+1, the shim won't read the Timeout until it has sent flight
N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes
after N+1. Go then drops all packets before the TimeoutAck, thus dropping one
transmit of flight N+1 without having to actually process the packets to
determine the end of the flight. The shim then sees the updated clock, calls
DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real.
When dropping packets, Go checks the epoch and increments sequence numbers so
that we can continue to be strict here. This requires tracking the initial
sequence number of the next epoch.
The final Finished message takes an additional special-case to test. DTLS
triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only
implements the former which should be sufficient (and is necessary) EXCEPT for
the final Finished message. If the peer's final Finished message is lost, it
won't be waiting for a message from us, so it won't time out anything. That
retransmit must be triggered on stale message, so we retransmit the Finished
message in Go.
Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c
Reviewed-on: https://boringssl-review.googlesource.com/3212
Reviewed-by: Adam Langley <agl@google.com>
2015-01-27 06:09:43 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
2016-07-04 18:05:26 +01:00
|
|
|
return err
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
hs.clientHello, ok = msg.(*clientHelloMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
2016-07-04 18:05:26 +01:00
|
|
|
return unexpectedMessageError(hs.clientHello, msg)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-07-21 01:43:53 +01:00
|
|
|
if size := config.Bugs.RequireClientHelloSize; size != 0 && len(hs.clientHello.raw) != size {
|
2016-07-04 18:05:26 +01:00
|
|
|
return fmt.Errorf("tls: ClientHello record size is %d, but expected %d", len(hs.clientHello.raw), size)
|
2014-11-22 06:47:56 +00:00
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
|
|
|
|
if c.isDTLS && !config.Bugs.SkipHelloVerifyRequest {
|
2014-08-16 17:07:27 +01:00
|
|
|
// Per RFC 6347, the version field in HelloVerifyRequest SHOULD
|
|
|
|
// be always DTLS 1.0
|
2014-08-04 06:23:53 +01:00
|
|
|
helloVerifyRequest := &helloVerifyRequestMsg{
|
2017-06-20 15:55:02 +01:00
|
|
|
vers: VersionDTLS10,
|
2014-08-04 06:23:53 +01:00
|
|
|
cookie: make([]byte, 32),
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(c.config.rand(), helloVerifyRequest.cookie); err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
2016-07-04 18:05:26 +01:00
|
|
|
return errors.New("dtls: short read from Rand: " + err.Error())
|
2014-08-04 06:23:53 +01:00
|
|
|
}
|
|
|
|
c.writeRecord(recordTypeHandshake, helloVerifyRequest.marshal())
|
2016-07-07 20:33:25 +01:00
|
|
|
c.flushHandshake()
|
2014-08-04 06:23:53 +01:00
|
|
|
|
Add DTLS timeout and retransmit tests.
This extends the packet adaptor protocol to send three commands:
type command =
| Packet of []byte
| Timeout of time.Duration
| TimeoutAck
When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the
BIO_read, returns out of the SSL stack, advances the clock, calls
DTLSv1_handle_timeout, and continues.
If the Go side sends Timeout right between sending handshake flight N and
reading flight N+1, the shim won't read the Timeout until it has sent flight
N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes
after N+1. Go then drops all packets before the TimeoutAck, thus dropping one
transmit of flight N+1 without having to actually process the packets to
determine the end of the flight. The shim then sees the updated clock, calls
DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real.
When dropping packets, Go checks the epoch and increments sequence numbers so
that we can continue to be strict here. This requires tracking the initial
sequence number of the next epoch.
The final Finished message takes an additional special-case to test. DTLS
triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only
implements the former which should be sufficient (and is necessary) EXCEPT for
the final Finished message. If the peer's final Finished message is lost, it
won't be waiting for a message from us, so it won't time out anything. That
retransmit must be triggered on stale message, so we retransmit the Finished
message in Go.
Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c
Reviewed-on: https://boringssl-review.googlesource.com/3212
Reviewed-by: Adam Langley <agl@google.com>
2015-01-27 06:09:43 +00:00
|
|
|
if err := c.simulatePacketLoss(nil); err != nil {
|
2016-07-04 18:05:26 +01:00
|
|
|
return err
|
Add DTLS timeout and retransmit tests.
This extends the packet adaptor protocol to send three commands:
type command =
| Packet of []byte
| Timeout of time.Duration
| TimeoutAck
When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the
BIO_read, returns out of the SSL stack, advances the clock, calls
DTLSv1_handle_timeout, and continues.
If the Go side sends Timeout right between sending handshake flight N and
reading flight N+1, the shim won't read the Timeout until it has sent flight
N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes
after N+1. Go then drops all packets before the TimeoutAck, thus dropping one
transmit of flight N+1 without having to actually process the packets to
determine the end of the flight. The shim then sees the updated clock, calls
DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real.
When dropping packets, Go checks the epoch and increments sequence numbers so
that we can continue to be strict here. This requires tracking the initial
sequence number of the next epoch.
The final Finished message takes an additional special-case to test. DTLS
triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only
implements the former which should be sufficient (and is necessary) EXCEPT for
the final Finished message. If the peer's final Finished message is lost, it
won't be waiting for a message from us, so it won't time out anything. That
retransmit must be triggered on stale message, so we retransmit the Finished
message in Go.
Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c
Reviewed-on: https://boringssl-review.googlesource.com/3212
Reviewed-by: Adam Langley <agl@google.com>
2015-01-27 06:09:43 +00:00
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
2016-07-04 18:05:26 +01:00
|
|
|
return err
|
2014-08-04 06:23:53 +01:00
|
|
|
}
|
|
|
|
newClientHello, ok := msg.(*clientHelloMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
2016-07-04 18:05:26 +01:00
|
|
|
return unexpectedMessageError(hs.clientHello, msg)
|
2014-08-04 06:23:53 +01:00
|
|
|
}
|
|
|
|
if !bytes.Equal(newClientHello.cookie, helloVerifyRequest.cookie) {
|
2016-07-04 18:05:26 +01:00
|
|
|
return errors.New("dtls: invalid cookie")
|
2014-08-04 06:23:53 +01:00
|
|
|
}
|
2014-08-16 06:37:34 +01:00
|
|
|
|
|
|
|
// Apart from the cookie, the two ClientHellos must
|
|
|
|
// match. Note that clientHello.equal compares the
|
|
|
|
// serialization, so we make a copy.
|
|
|
|
oldClientHelloCopy := *hs.clientHello
|
|
|
|
oldClientHelloCopy.raw = nil
|
|
|
|
oldClientHelloCopy.cookie = nil
|
|
|
|
newClientHelloCopy := *newClientHello
|
|
|
|
newClientHelloCopy.raw = nil
|
|
|
|
newClientHelloCopy.cookie = nil
|
|
|
|
if !oldClientHelloCopy.equal(&newClientHelloCopy) {
|
2016-07-04 18:05:26 +01:00
|
|
|
return errors.New("dtls: retransmitted ClientHello does not match")
|
2014-08-04 06:23:53 +01:00
|
|
|
}
|
|
|
|
hs.clientHello = newClientHello
|
|
|
|
}
|
|
|
|
|
2014-11-23 17:11:01 +00:00
|
|
|
if config.Bugs.RequireSameRenegoClientVersion && c.clientVersion != 0 {
|
|
|
|
if c.clientVersion != hs.clientHello.vers {
|
2016-07-04 18:05:26 +01:00
|
|
|
return fmt.Errorf("tls: client offered different version on renego")
|
2014-11-23 17:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 21:27:05 +01:00
|
|
|
|
2018-11-12 21:53:42 +00:00
|
|
|
if config.Bugs.FailIfCECPQ2Offered {
|
|
|
|
for _, offeredCurve := range hs.clientHello.supportedCurves {
|
|
|
|
if offeredCurve == CurveCECPQ2 {
|
|
|
|
return errors.New("tls: CECPQ2 was offered")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := config.Bugs.ExpectedKeyShares; expected != nil {
|
|
|
|
if len(expected) != len(hs.clientHello.keyShares) {
|
|
|
|
return fmt.Errorf("tls: expected %d key shares, but found %d", len(expected), len(hs.clientHello.keyShares))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, group := range expected {
|
|
|
|
if found := hs.clientHello.keyShares[i].group; found != group {
|
|
|
|
return fmt.Errorf("tls: key share #%d is for group %d, not %d", i, found, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-23 17:11:01 +00:00
|
|
|
c.clientVersion = hs.clientHello.vers
|
2016-09-15 21:27:05 +01:00
|
|
|
|
2017-06-20 15:55:02 +01:00
|
|
|
// Use the versions extension if supplied, otherwise use the legacy ClientHello version.
|
|
|
|
if len(hs.clientHello.supportedVersions) == 0 {
|
|
|
|
if c.isDTLS {
|
|
|
|
if hs.clientHello.vers <= VersionDTLS12 {
|
|
|
|
hs.clientHello.supportedVersions = append(hs.clientHello.supportedVersions, VersionDTLS12)
|
2016-09-27 21:39:52 +01:00
|
|
|
}
|
2017-06-20 15:55:02 +01:00
|
|
|
if hs.clientHello.vers <= VersionDTLS10 {
|
|
|
|
hs.clientHello.supportedVersions = append(hs.clientHello.supportedVersions, VersionDTLS10)
|
2016-09-15 21:27:05 +01:00
|
|
|
}
|
2017-06-20 15:55:02 +01:00
|
|
|
} else {
|
|
|
|
if hs.clientHello.vers >= VersionTLS12 {
|
|
|
|
hs.clientHello.supportedVersions = append(hs.clientHello.supportedVersions, VersionTLS12)
|
|
|
|
}
|
|
|
|
if hs.clientHello.vers >= VersionTLS11 {
|
|
|
|
hs.clientHello.supportedVersions = append(hs.clientHello.supportedVersions, VersionTLS11)
|
|
|
|
}
|
|
|
|
if hs.clientHello.vers >= VersionTLS10 {
|
|
|
|
hs.clientHello.supportedVersions = append(hs.clientHello.supportedVersions, VersionTLS10)
|
|
|
|
}
|
|
|
|
if hs.clientHello.vers >= VersionSSL30 {
|
|
|
|
hs.clientHello.supportedVersions = append(hs.clientHello.supportedVersions, VersionSSL30)
|
2016-09-15 21:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 15:55:02 +01:00
|
|
|
} else if config.Bugs.ExpectGREASE && !containsGREASE(hs.clientHello.supportedVersions) {
|
|
|
|
return errors.New("tls: no GREASE version value found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.haveVers {
|
|
|
|
if config.Bugs.NegotiateVersion != 0 {
|
|
|
|
c.wireVersion = config.Bugs.NegotiateVersion
|
|
|
|
} else {
|
|
|
|
var found bool
|
|
|
|
for _, vers := range hs.clientHello.supportedVersions {
|
|
|
|
if _, ok := config.isSupportedVersion(vers, c.isDTLS); ok {
|
|
|
|
c.wireVersion = vers
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
c.sendAlert(alertProtocolVersion)
|
|
|
|
return errors.New("tls: client did not offer any supported protocol versions")
|
|
|
|
}
|
2016-09-27 00:20:48 +01:00
|
|
|
}
|
2017-06-20 15:55:02 +01:00
|
|
|
} else if config.Bugs.NegotiateVersionOnRenego != 0 {
|
|
|
|
c.wireVersion = config.Bugs.NegotiateVersionOnRenego
|
|
|
|
}
|
|
|
|
|
|
|
|
c.vers, ok = wireToVersion(c.wireVersion, c.isDTLS)
|
|
|
|
if !ok {
|
|
|
|
panic("Could not map wire version")
|
2016-09-27 00:20:48 +01:00
|
|
|
}
|
|
|
|
c.haveVers = true
|
2014-11-23 17:11:01 +00:00
|
|
|
|
2017-06-20 15:55:02 +01:00
|
|
|
clientProtocol, ok := wireToVersion(c.clientVersion, c.isDTLS)
|
|
|
|
|
2015-01-26 15:22:13 +00:00
|
|
|
// Reject < 1.2 ClientHellos with signature_algorithms.
|
2017-06-20 15:55:02 +01:00
|
|
|
if ok && clientProtocol < VersionTLS12 && len(hs.clientHello.signatureAlgorithms) > 0 {
|
2016-07-04 18:05:26 +01:00
|
|
|
return fmt.Errorf("tls: client included signature_algorithms before TLS 1.2")
|
2015-03-16 21:49:43 +00:00
|
|
|
}
|
2015-01-26 15:22:13 +00:00
|
|
|
|
2015-11-05 23:23:20 +00:00
|
|
|
// Check the client cipher list is consistent with the version.
|
2017-06-20 15:55:02 +01:00
|
|
|
if ok && clientProtocol < VersionTLS12 {
|
2015-11-05 23:23:20 +00:00
|
|
|
for _, id := range hs.clientHello.cipherSuites {
|
|
|
|
if isTLS12Cipher(id) {
|
2016-07-04 18:05:26 +01:00
|
|
|
return fmt.Errorf("tls: client offered TLS 1.2 cipher before TLS 1.2")
|
2015-11-05 23:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 22:25:07 +01:00
|
|
|
if config.Bugs.ExpectNoTLS12Session {
|
2017-12-04 16:15:37 +00:00
|
|
|
if len(hs.clientHello.sessionId) > 0 && c.vers >= VersionTLS13 {
|
2016-08-08 22:25:07 +01:00
|
|
|
return fmt.Errorf("tls: client offered an unexpected session ID")
|
|
|
|
}
|
|
|
|
if len(hs.clientHello.sessionTicket) > 0 {
|
|
|
|
return fmt.Errorf("tls: client offered an unexpected session ticket")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Bugs.ExpectNoTLS13PSK && len(hs.clientHello.pskIdentities) > 0 {
|
|
|
|
return fmt.Errorf("tls: client offered unexpected PSK identities")
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:55:02 +01:00
|
|
|
var scsvFound bool
|
2016-07-04 18:05:26 +01:00
|
|
|
for _, cipherSuite := range hs.clientHello.cipherSuites {
|
|
|
|
if cipherSuite == fallbackSCSV {
|
|
|
|
scsvFound = true
|
2017-06-20 15:55:02 +01:00
|
|
|
break
|
2016-07-04 18:05:26 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 17:20:45 +01:00
|
|
|
|
2016-07-04 18:05:26 +01:00
|
|
|
if !scsvFound && config.Bugs.FailIfNotFallbackSCSV {
|
|
|
|
return errors.New("tls: no fallback SCSV found when expected")
|
|
|
|
} else if scsvFound && !config.Bugs.FailIfNotFallbackSCSV {
|
|
|
|
return errors.New("tls: fallback SCSV found when not expected")
|
2015-07-31 02:10:13 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-06-20 15:55:02 +01:00
|
|
|
if config.Bugs.ExpectGREASE && !containsGREASE(hs.clientHello.cipherSuites) {
|
2016-09-03 02:35:25 +01:00
|
|
|
return errors.New("tls: no GREASE cipher suite value found")
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:55:02 +01:00
|
|
|
var greaseFound bool
|
2016-09-03 02:35:25 +01:00
|
|
|
for _, curve := range hs.clientHello.supportedCurves {
|
|
|
|
if isGREASEValue(uint16(curve)) {
|
|
|
|
greaseFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !greaseFound && config.Bugs.ExpectGREASE {
|
|
|
|
return errors.New("tls: no GREASE curve value found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hs.clientHello.keyShares) > 0 {
|
|
|
|
greaseFound = false
|
|
|
|
for _, keyShare := range hs.clientHello.keyShares {
|
|
|
|
if isGREASEValue(uint16(keyShare.group)) {
|
|
|
|
greaseFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !greaseFound && config.Bugs.ExpectGREASE {
|
|
|
|
return errors.New("tls: no GREASE curve value found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 18:59:25 +01:00
|
|
|
if err := checkRSAPSSSupport(config.Bugs.ExpectRSAPSSSupport, hs.clientHello.signatureAlgorithms, hs.clientHello.signatureAlgorithmsCert); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:25:14 +00:00
|
|
|
applyBugsToClientHello(hs.clientHello, config)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyBugsToClientHello(clientHello *clientHelloMsg, config *Config) {
|
2016-07-04 18:05:26 +01:00
|
|
|
if config.Bugs.IgnorePeerSignatureAlgorithmPreferences {
|
2017-03-10 23:25:14 +00:00
|
|
|
clientHello.signatureAlgorithms = config.signSignatureAlgorithms()
|
2016-07-04 18:05:26 +01:00
|
|
|
}
|
2015-04-20 16:13:01 +01:00
|
|
|
if config.Bugs.IgnorePeerCurvePreferences {
|
2017-03-10 23:25:14 +00:00
|
|
|
clientHello.supportedCurves = config.curvePreferences()
|
2015-04-20 16:13:01 +01:00
|
|
|
}
|
2016-07-04 18:05:26 +01:00
|
|
|
if config.Bugs.IgnorePeerCipherPreferences {
|
2017-03-10 23:25:14 +00:00
|
|
|
clientHello.cipherSuites = config.cipherSuites()
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2016-07-04 18:05:26 +01:00
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
func (hs *serverHandshakeState) doTLS13Handshake() error {
|
|
|
|
c := hs.c
|
|
|
|
config := c.config
|
|
|
|
|
2018-01-22 20:20:31 +00:00
|
|
|
// We've read the ClientHello, so the next record must be preceded with ChangeCipherSpec.
|
|
|
|
c.expectTLS13ChangeCipherSpec = true
|
2017-11-16 23:36:38 +00:00
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
hs.hello = &serverHelloMsg{
|
2017-07-10 17:57:25 +01:00
|
|
|
isDTLS: c.isDTLS,
|
|
|
|
vers: c.wireVersion,
|
|
|
|
sessionId: hs.clientHello.sessionId,
|
2017-11-12 01:34:06 +00:00
|
|
|
compressionMethod: config.Bugs.SendCompressionMethod,
|
2017-07-10 17:57:25 +01:00
|
|
|
versOverride: config.Bugs.SendServerHelloVersion,
|
2018-05-05 03:00:33 +01:00
|
|
|
supportedVersOverride: config.Bugs.SendServerSupportedVersionExtension,
|
|
|
|
omitSupportedVers: config.Bugs.OmitServerSupportedVersionExtension,
|
2017-07-10 17:57:25 +01:00
|
|
|
customExtension: config.Bugs.CustomUnencryptedExtension,
|
|
|
|
unencryptedALPN: config.Bugs.SendUnencryptedALPN,
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
hs.hello.random = make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(config.rand(), hs.hello.random); err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLS 1.3 forbids clients from advertising any non-null compression.
|
|
|
|
if len(hs.clientHello.compressionMethods) != 1 || hs.clientHello.compressionMethods[0] != compressionNone {
|
|
|
|
return errors.New("tls: client sent compression method other than null for TLS 1.3")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare an EncryptedExtensions message, but do not send it yet.
|
|
|
|
encryptedExtensions := new(encryptedExtensionsMsg)
|
2016-07-11 18:19:03 +01:00
|
|
|
encryptedExtensions.empty = config.Bugs.EmptyEncryptedExtensions
|
2016-07-08 01:36:52 +01:00
|
|
|
if err := hs.processClientExtensions(&encryptedExtensions.extensions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-16 04:16:13 +00:00
|
|
|
// Select the cipher suite.
|
|
|
|
var preferenceList, supportedList []uint16
|
|
|
|
if config.PreferServerCipherSuites {
|
|
|
|
preferenceList = config.cipherSuites()
|
|
|
|
supportedList = hs.clientHello.cipherSuites
|
|
|
|
} else {
|
|
|
|
preferenceList = hs.clientHello.cipherSuites
|
|
|
|
supportedList = config.cipherSuites()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range preferenceList {
|
|
|
|
if hs.suite = c.tryCipherSuite(id, supportedList, c.vers, true, true); hs.suite != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hs.suite == nil {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return errors.New("tls: no cipher suite supported by both client and server")
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.hello.cipherSuite = hs.suite.id
|
|
|
|
if c.config.Bugs.SendCipherSuite != 0 {
|
|
|
|
hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
hs.finishedHash = newFinishedHash(c.wireVersion, c.isDTLS, hs.suite)
|
2016-11-16 04:16:13 +00:00
|
|
|
hs.finishedHash.discardHandshakeBuffer()
|
|
|
|
hs.writeClientHash(hs.clientHello.marshal())
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
supportedCurve := false
|
|
|
|
var selectedCurve CurveID
|
|
|
|
preferredCurves := config.curvePreferences()
|
|
|
|
Curves:
|
|
|
|
for _, curve := range hs.clientHello.supportedCurves {
|
|
|
|
for _, supported := range preferredCurves {
|
|
|
|
if supported == curve {
|
|
|
|
supportedCurve = true
|
|
|
|
selectedCurve = curve
|
|
|
|
break Curves
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:13:43 +01:00
|
|
|
if !supportedCurve {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return errors.New("tls: no curve supported by both client and server")
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
|
2016-08-08 22:25:07 +01:00
|
|
|
pskIdentities := hs.clientHello.pskIdentities
|
2016-11-01 17:39:36 +00:00
|
|
|
pskKEModes := hs.clientHello.pskKEModes
|
|
|
|
|
2016-08-08 22:25:07 +01:00
|
|
|
if len(pskIdentities) == 0 && len(hs.clientHello.sessionTicket) > 0 && c.config.Bugs.AcceptAnySession {
|
2016-09-01 17:29:49 +01:00
|
|
|
psk := pskIdentity{
|
2016-11-01 17:39:36 +00:00
|
|
|
ticket: hs.clientHello.sessionTicket,
|
2016-09-01 17:29:49 +01:00
|
|
|
}
|
|
|
|
pskIdentities = []pskIdentity{psk}
|
2016-11-01 17:39:36 +00:00
|
|
|
pskKEModes = []byte{pskDHEKEMode}
|
2016-08-08 22:25:07 +01:00
|
|
|
}
|
2016-09-01 17:29:49 +01:00
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
var pskIndex int
|
|
|
|
foundKEMode := bytes.IndexByte(pskKEModes, pskDHEKEMode) >= 0
|
2017-01-11 16:34:52 +00:00
|
|
|
if foundKEMode && !config.SessionTicketsDisabled {
|
2016-11-01 17:39:36 +00:00
|
|
|
for i, pskIdentity := range pskIdentities {
|
|
|
|
// TODO(svaldez): Check the obfuscatedTicketAge before accepting 0-RTT.
|
|
|
|
sessionState, ok := c.decryptTicket(pskIdentity.ticket)
|
|
|
|
if !ok {
|
|
|
|
continue
|
2016-09-01 17:29:49 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 04:16:13 +00:00
|
|
|
if !config.Bugs.AcceptAnySession {
|
2016-11-16 02:45:34 +00:00
|
|
|
if sessionState.vers != c.vers {
|
2016-11-01 17:39:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sessionState.ticketExpiration.Before(c.config.time()) {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-16 07:11:47 +00:00
|
|
|
sessionCipher := cipherSuiteFromID(sessionState.cipherSuite)
|
|
|
|
if sessionCipher == nil || sessionCipher.hash() != hs.suite.hash() {
|
2016-11-01 17:39:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-09-06 19:13:43 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
|
2016-11-16 02:48:17 +00:00
|
|
|
clientTicketAge := time.Duration(uint32(pskIdentity.obfuscatedTicketAge-sessionState.ticketAgeAdd)) * time.Millisecond
|
|
|
|
if config.Bugs.ExpectTicketAge != 0 && clientTicketAge != config.Bugs.ExpectTicketAge {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return errors.New("tls: invalid ticket age")
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
hs.sessionState = sessionState
|
|
|
|
hs.hello.hasPSKIdentity = true
|
|
|
|
hs.hello.pskIdentity = uint16(i)
|
|
|
|
pskIndex = i
|
|
|
|
if config.Bugs.SelectPSKIdentityOnResume != 0 {
|
|
|
|
hs.hello.pskIdentity = config.Bugs.SelectPSKIdentityOnResume
|
|
|
|
}
|
|
|
|
c.didResume = true
|
|
|
|
break
|
2016-10-06 03:33:19 +01:00
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
|
|
|
|
2016-10-06 03:33:19 +01:00
|
|
|
if config.Bugs.AlwaysSelectPSKIdentity {
|
|
|
|
hs.hello.hasPSKIdentity = true
|
|
|
|
hs.hello.pskIdentity = 0
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
// Verify the PSK binder. Note there may not be a PSK binder if
|
|
|
|
// AcceptAnyBinder is set. See https://crbug.com/boringssl/115.
|
|
|
|
if hs.sessionState != nil && !config.Bugs.AcceptAnySession {
|
|
|
|
binderToVerify := hs.clientHello.pskBinders[pskIndex]
|
2017-10-11 17:29:36 +01:00
|
|
|
if err := verifyPSKBinder(c.wireVersion, hs.clientHello, hs.sessionState, binderToVerify, []byte{}, []byte{}); err != nil {
|
2016-11-01 17:39:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
// Resolve PSK and compute the early secret.
|
2016-09-06 19:13:43 +01:00
|
|
|
if hs.sessionState != nil {
|
2016-12-04 05:02:43 +00:00
|
|
|
hs.finishedHash.addEntropy(hs.sessionState.masterSecret)
|
2016-07-26 00:16:28 +01:00
|
|
|
} else {
|
2016-12-04 05:02:43 +00:00
|
|
|
hs.finishedHash.addEntropy(hs.finishedHash.zeroSecret())
|
2016-07-26 00:16:28 +01:00
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
|
2016-09-06 19:13:43 +01:00
|
|
|
hs.hello.hasKeyShare = true
|
|
|
|
if hs.sessionState != nil && config.Bugs.NegotiatePSKResumption {
|
|
|
|
hs.hello.hasKeyShare = false
|
|
|
|
}
|
|
|
|
if config.Bugs.MissingKeyShare {
|
|
|
|
hs.hello.hasKeyShare = false
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
firstHelloRetryRequest := true
|
|
|
|
|
|
|
|
ResendHelloRetryRequest:
|
|
|
|
var sendHelloRetryRequest bool
|
2017-10-11 17:29:36 +01:00
|
|
|
cipherSuite := hs.suite.id
|
|
|
|
if config.Bugs.SendHelloRetryRequestCipherSuite != 0 {
|
|
|
|
cipherSuite = config.Bugs.SendHelloRetryRequestCipherSuite
|
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
helloRetryRequest := &helloRetryRequestMsg{
|
2017-06-20 15:55:02 +01:00
|
|
|
vers: c.wireVersion,
|
2017-11-07 22:09:52 +00:00
|
|
|
sessionId: hs.clientHello.sessionId,
|
2017-10-11 17:29:36 +01:00
|
|
|
cipherSuite: cipherSuite,
|
2017-11-12 01:34:06 +00:00
|
|
|
compressionMethod: config.Bugs.SendCompressionMethod,
|
2016-10-08 02:10:38 +01:00
|
|
|
duplicateExtensions: config.Bugs.DuplicateHelloRetryRequestExtensions,
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Bugs.AlwaysSendHelloRetryRequest {
|
|
|
|
sendHelloRetryRequest = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Bugs.SendHelloRetryRequestCookie != nil {
|
|
|
|
sendHelloRetryRequest = true
|
|
|
|
helloRetryRequest.cookie = config.Bugs.SendHelloRetryRequestCookie
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Bugs.CustomHelloRetryRequestExtension) > 0 {
|
|
|
|
sendHelloRetryRequest = true
|
|
|
|
helloRetryRequest.customExtension = config.Bugs.CustomHelloRetryRequestExtension
|
|
|
|
}
|
|
|
|
|
|
|
|
var selectedKeyShare *keyShareEntry
|
2016-09-06 19:13:43 +01:00
|
|
|
if hs.hello.hasKeyShare {
|
2016-07-08 01:36:52 +01:00
|
|
|
// Look for the key share corresponding to our selected curve.
|
|
|
|
for i := range hs.clientHello.keyShares {
|
|
|
|
if hs.clientHello.keyShares[i].group == selectedCurve {
|
|
|
|
selectedKeyShare = &hs.clientHello.keyShares[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 05:29:33 +01:00
|
|
|
if config.Bugs.ExpectMissingKeyShare && selectedKeyShare != nil {
|
|
|
|
return errors.New("tls: expected missing key share")
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
if selectedKeyShare == nil {
|
|
|
|
helloRetryRequest.hasSelectedGroup = true
|
|
|
|
helloRetryRequest.selectedGroup = selectedCurve
|
2016-07-18 17:40:30 +01:00
|
|
|
sendHelloRetryRequest = true
|
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
}
|
2016-07-16 16:47:31 +01:00
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
if config.Bugs.SendHelloRetryRequestCurve != 0 {
|
|
|
|
helloRetryRequest.hasSelectedGroup = true
|
|
|
|
helloRetryRequest.selectedGroup = config.Bugs.SendHelloRetryRequestCurve
|
|
|
|
sendHelloRetryRequest = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Bugs.SkipHelloRetryRequest {
|
|
|
|
sendHelloRetryRequest = false
|
|
|
|
}
|
2016-07-16 16:47:31 +01:00
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
if sendHelloRetryRequest {
|
2018-01-22 20:20:31 +00:00
|
|
|
if err := hs.finishedHash.UpdateForHelloRetryRequest(); err != nil {
|
|
|
|
return err
|
2017-10-11 17:29:36 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
oldClientHelloBytes := hs.clientHello.marshal()
|
2016-10-08 02:10:38 +01:00
|
|
|
hs.writeServerHash(helloRetryRequest.marshal())
|
2017-10-02 20:53:57 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, helloRetryRequest.marshal())
|
2016-10-08 02:10:38 +01:00
|
|
|
c.flushHandshake()
|
|
|
|
|
2018-01-22 20:20:31 +00:00
|
|
|
if !c.config.Bugs.SkipChangeCipherSpec {
|
2017-11-07 22:09:52 +00:00
|
|
|
c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:34:52 +00:00
|
|
|
if hs.clientHello.hasEarlyData {
|
|
|
|
c.skipEarlyData = true
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
// Read new ClientHello.
|
|
|
|
newMsg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newClientHello, ok := newMsg.(*clientHelloMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(newClientHello, newMsg)
|
|
|
|
}
|
|
|
|
hs.writeClientHash(newClientHello.marshal())
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
if config.Bugs.ExpectNoTLS13PSKAfterHRR && len(newClientHello.pskIdentities) > 0 {
|
|
|
|
return fmt.Errorf("tls: client offered unexpected PSK identities after HelloRetryRequest")
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:34:52 +00:00
|
|
|
if newClientHello.hasEarlyData {
|
|
|
|
return errors.New("tls: EarlyData sent in new ClientHello")
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:25:14 +00:00
|
|
|
applyBugsToClientHello(newClientHello, config)
|
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
// Check that the new ClientHello matches the old ClientHello,
|
|
|
|
// except for relevant modifications.
|
|
|
|
//
|
|
|
|
// TODO(davidben): Make this check more precise.
|
|
|
|
oldClientHelloCopy := *hs.clientHello
|
|
|
|
oldClientHelloCopy.raw = nil
|
|
|
|
oldClientHelloCopy.hasEarlyData = false
|
|
|
|
newClientHelloCopy := *newClientHello
|
|
|
|
newClientHelloCopy.raw = nil
|
|
|
|
|
|
|
|
if helloRetryRequest.hasSelectedGroup {
|
|
|
|
newKeyShares := newClientHelloCopy.keyShares
|
2016-11-01 17:39:36 +00:00
|
|
|
if len(newKeyShares) != 1 || newKeyShares[0].group != helloRetryRequest.selectedGroup {
|
|
|
|
return errors.New("tls: KeyShare from HelloRetryRequest not in new ClientHello")
|
2016-07-16 16:47:31 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
selectedKeyShare = &newKeyShares[0]
|
|
|
|
newClientHelloCopy.keyShares = oldClientHelloCopy.keyShares
|
2016-10-08 02:10:38 +01:00
|
|
|
}
|
2016-07-16 16:47:31 +01:00
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
if len(helloRetryRequest.cookie) > 0 {
|
|
|
|
if !bytes.Equal(newClientHelloCopy.tls13Cookie, helloRetryRequest.cookie) {
|
|
|
|
return errors.New("tls: cookie from HelloRetryRequest not present in new ClientHello")
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
newClientHelloCopy.tls13Cookie = nil
|
|
|
|
}
|
2016-11-15 09:19:55 +00:00
|
|
|
|
|
|
|
// PSK binders and obfuscated ticket age are both updated in the
|
|
|
|
// second ClientHello.
|
2018-01-22 20:20:31 +00:00
|
|
|
if len(oldClientHelloCopy.pskIdentities) != len(newClientHelloCopy.pskIdentities) {
|
2017-10-11 17:29:36 +01:00
|
|
|
newClientHelloCopy.pskIdentities = oldClientHelloCopy.pskIdentities
|
|
|
|
} else {
|
|
|
|
if len(oldClientHelloCopy.pskIdentities) != len(newClientHelloCopy.pskIdentities) {
|
|
|
|
return errors.New("tls: PSK identity count from old and new ClientHello do not match")
|
|
|
|
}
|
|
|
|
for i, identity := range oldClientHelloCopy.pskIdentities {
|
|
|
|
newClientHelloCopy.pskIdentities[i].obfuscatedTicketAge = identity.obfuscatedTicketAge
|
|
|
|
}
|
2016-11-15 09:19:55 +00:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
newClientHelloCopy.pskBinders = oldClientHelloCopy.pskBinders
|
2017-01-11 16:34:52 +00:00
|
|
|
newClientHelloCopy.hasEarlyData = oldClientHelloCopy.hasEarlyData
|
2016-07-18 17:40:30 +01:00
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
if !oldClientHelloCopy.equal(&newClientHelloCopy) {
|
|
|
|
return errors.New("tls: new ClientHello does not match")
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
if firstHelloRetryRequest && config.Bugs.SecondHelloRetryRequest {
|
|
|
|
firstHelloRetryRequest = false
|
|
|
|
goto ResendHelloRetryRequest
|
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
|
|
|
|
// Verify the PSK binder. Note there may not be a PSK binder if
|
|
|
|
// AcceptAnyBinder is set. See https://crbug.com/115.
|
|
|
|
if hs.sessionState != nil && !config.Bugs.AcceptAnySession {
|
|
|
|
binderToVerify := newClientHello.pskBinders[pskIndex]
|
2017-10-11 17:29:36 +01:00
|
|
|
if err := verifyPSKBinder(c.wireVersion, newClientHello, hs.sessionState, binderToVerify, oldClientHelloBytes, helloRetryRequest.marshal()); err != nil {
|
2016-11-01 17:39:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
}
|
|
|
|
|
2016-12-20 01:38:41 +00:00
|
|
|
// Decide whether or not to accept early data.
|
2017-01-11 16:34:52 +00:00
|
|
|
if !sendHelloRetryRequest && hs.clientHello.hasEarlyData {
|
|
|
|
if !config.Bugs.AlwaysRejectEarlyData && hs.sessionState != nil {
|
|
|
|
if c.clientProtocol == string(hs.sessionState.earlyALPN) || config.Bugs.AlwaysAcceptEarlyData {
|
|
|
|
encryptedExtensions.extensions.hasEarlyData = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if encryptedExtensions.extensions.hasEarlyData {
|
2018-01-22 20:20:31 +00:00
|
|
|
earlyTrafficSecret := hs.finishedHash.deriveSecret(earlyTrafficLabel)
|
|
|
|
c.earlyExporterSecret = hs.finishedHash.deriveSecret(earlyExporterLabel)
|
2017-10-11 17:29:36 +01:00
|
|
|
|
2017-10-17 22:14:58 +01:00
|
|
|
if err := c.useInTrafficSecret(c.wireVersion, hs.suite, earlyTrafficSecret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-20 01:38:41 +00:00
|
|
|
|
2017-12-08 22:05:06 +00:00
|
|
|
c.earlyCipherSuite = hs.suite
|
|
|
|
expectEarlyData := config.Bugs.ExpectEarlyData
|
|
|
|
if n := config.Bugs.ExpectEarlyKeyingMaterial; n > 0 {
|
|
|
|
exporter, err := c.ExportEarlyKeyingMaterial(n, []byte(config.Bugs.ExpectEarlyKeyingLabel), []byte(config.Bugs.ExpectEarlyKeyingContext))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
expectEarlyData = append([][]byte{exporter}, expectEarlyData...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, expectedMsg := range expectEarlyData {
|
2016-12-20 01:38:41 +00:00
|
|
|
if err := c.readRecord(recordTypeApplicationData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-08 22:05:06 +00:00
|
|
|
msg := c.input.data[c.input.off:]
|
|
|
|
if !bytes.Equal(msg, expectedMsg) {
|
|
|
|
return fmt.Errorf("tls: got early data record %x, wanted %x", msg, expectedMsg)
|
2016-12-20 01:38:41 +00:00
|
|
|
}
|
|
|
|
c.in.freeBlock(c.input)
|
|
|
|
c.input = nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.skipEarlyData = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:34:52 +00:00
|
|
|
if config.Bugs.SendEarlyDataExtension {
|
|
|
|
encryptedExtensions.extensions.hasEarlyData = true
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
// Resolve ECDHE and compute the handshake secret.
|
|
|
|
if hs.hello.hasKeyShare {
|
2016-07-08 01:36:52 +01:00
|
|
|
// Once a curve has been selected and a key share identified,
|
|
|
|
// the server needs to generate a public value and send it in
|
|
|
|
// the ServerHello.
|
2018-02-15 00:50:38 +00:00
|
|
|
curve, ok := curveForCurveID(selectedCurve, config)
|
2016-07-08 01:36:52 +01:00
|
|
|
if !ok {
|
|
|
|
panic("tls: server failed to look up curve ID")
|
|
|
|
}
|
2016-07-18 17:40:30 +01:00
|
|
|
c.curveID = selectedCurve
|
|
|
|
|
|
|
|
var peerKey []byte
|
|
|
|
if config.Bugs.SkipHelloRetryRequest {
|
|
|
|
// If skipping HelloRetryRequest, use a random key to
|
|
|
|
// avoid crashing.
|
2018-02-15 00:50:38 +00:00
|
|
|
curve2, _ := curveForCurveID(selectedCurve, config)
|
2016-07-18 17:40:30 +01:00
|
|
|
var err error
|
|
|
|
peerKey, err = curve2.offer(config.rand())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
peerKey = selectedKeyShare.keyExchange
|
|
|
|
}
|
|
|
|
|
2016-12-04 05:02:43 +00:00
|
|
|
publicKey, ecdheSecret, err := curve.accept(config.rand(), peerKey)
|
2016-07-08 01:36:52 +01:00
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return err
|
|
|
|
}
|
2017-10-11 17:29:36 +01:00
|
|
|
hs.finishedHash.nextSecret()
|
2016-12-04 05:02:43 +00:00
|
|
|
hs.finishedHash.addEntropy(ecdheSecret)
|
2016-07-08 01:36:52 +01:00
|
|
|
hs.hello.hasKeyShare = true
|
2016-07-15 11:51:15 +01:00
|
|
|
|
2016-07-18 17:40:30 +01:00
|
|
|
curveID := selectedCurve
|
2016-07-15 11:51:15 +01:00
|
|
|
if c.config.Bugs.SendCurve != 0 {
|
|
|
|
curveID = config.Bugs.SendCurve
|
|
|
|
}
|
|
|
|
if c.config.Bugs.InvalidECDHPoint {
|
|
|
|
publicKey[0] ^= 0xff
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
hs.hello.keyShare = keyShareEntry{
|
2016-07-15 11:51:15 +01:00
|
|
|
group: curveID,
|
2016-07-08 01:36:52 +01:00
|
|
|
keyExchange: publicKey,
|
|
|
|
}
|
2016-07-11 18:19:03 +01:00
|
|
|
|
|
|
|
if config.Bugs.EncryptedExtensionsWithKeyShare {
|
|
|
|
encryptedExtensions.extensions.hasKeyShare = true
|
|
|
|
encryptedExtensions.extensions.keyShare = keyShareEntry{
|
|
|
|
group: curveID,
|
|
|
|
keyExchange: publicKey,
|
|
|
|
}
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
} else {
|
2017-10-11 17:29:36 +01:00
|
|
|
hs.finishedHash.nextSecret()
|
2016-12-04 05:02:43 +00:00
|
|
|
hs.finishedHash.addEntropy(hs.finishedHash.zeroSecret())
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send unencrypted ServerHello.
|
|
|
|
hs.writeServerHash(hs.hello.marshal())
|
2016-07-15 04:36:30 +01:00
|
|
|
if config.Bugs.PartialEncryptedExtensionsWithServerHello {
|
|
|
|
helloBytes := hs.hello.marshal()
|
|
|
|
toWrite := make([]byte, 0, len(helloBytes)+1)
|
|
|
|
toWrite = append(toWrite, helloBytes...)
|
|
|
|
toWrite = append(toWrite, typeEncryptedExtensions)
|
|
|
|
c.writeRecord(recordTypeHandshake, toWrite)
|
|
|
|
} else {
|
2017-10-02 20:53:57 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, hs.hello.marshal())
|
2016-07-15 04:36:30 +01:00
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
c.flushHandshake()
|
|
|
|
|
2017-12-04 16:15:37 +00:00
|
|
|
if !c.config.Bugs.SkipChangeCipherSpec && !sendHelloRetryRequest {
|
2017-11-07 22:09:52 +00:00
|
|
|
c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < c.config.Bugs.SendExtraChangeCipherSpec; i++ {
|
2017-06-13 17:45:25 +01:00
|
|
|
c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
// Switch to handshake traffic keys.
|
2018-01-22 20:20:31 +00:00
|
|
|
serverHandshakeTrafficSecret := hs.finishedHash.deriveSecret(serverHandshakeTrafficLabel)
|
2017-10-17 07:56:02 +01:00
|
|
|
c.useOutTrafficSecret(c.wireVersion, hs.suite, serverHandshakeTrafficSecret)
|
2016-12-20 01:38:41 +00:00
|
|
|
// Derive handshake traffic read key, but don't switch yet.
|
2018-01-22 20:20:31 +00:00
|
|
|
clientHandshakeTrafficSecret := hs.finishedHash.deriveSecret(clientHandshakeTrafficLabel)
|
2016-07-07 03:22:55 +01:00
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
// Send EncryptedExtensions.
|
|
|
|
hs.writeServerHash(encryptedExtensions.marshal())
|
2016-07-15 04:36:30 +01:00
|
|
|
if config.Bugs.PartialEncryptedExtensionsWithServerHello {
|
|
|
|
// The first byte has already been sent.
|
|
|
|
c.writeRecord(recordTypeHandshake, encryptedExtensions.marshal()[1:])
|
|
|
|
} else {
|
|
|
|
c.writeRecord(recordTypeHandshake, encryptedExtensions.marshal())
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
if hs.sessionState == nil {
|
2016-07-08 01:36:52 +01:00
|
|
|
if config.ClientAuth >= RequestClientCert {
|
2016-07-09 22:26:01 +01:00
|
|
|
// Request a client certificate
|
|
|
|
certReq := &certificateRequestMsg{
|
2018-05-02 17:06:48 +01:00
|
|
|
vers: c.wireVersion,
|
2017-10-11 17:29:36 +01:00
|
|
|
hasSignatureAlgorithm: !config.Bugs.OmitCertificateRequestAlgorithms,
|
2016-07-09 22:26:01 +01:00
|
|
|
hasRequestContext: true,
|
2016-08-18 07:32:23 +01:00
|
|
|
requestContext: config.Bugs.SendRequestContext,
|
2017-10-11 17:29:36 +01:00
|
|
|
customExtension: config.Bugs.SendCustomCertificateRequest,
|
2016-07-09 22:26:01 +01:00
|
|
|
}
|
|
|
|
if !config.Bugs.NoSignatureAlgorithms {
|
2016-07-14 02:18:49 +01:00
|
|
|
certReq.signatureAlgorithms = config.verifySignatureAlgorithms()
|
2016-07-09 22:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// An empty list of certificateAuthorities signals to
|
|
|
|
// the client that it may send any certificate in response
|
|
|
|
// to our request. When we know the CAs we trust, then
|
|
|
|
// we can send them down, so that the client can choose
|
|
|
|
// an appropriate certificate to give to us.
|
|
|
|
if config.ClientCAs != nil {
|
|
|
|
certReq.certificateAuthorities = config.ClientCAs.Subjects()
|
|
|
|
}
|
|
|
|
hs.writeServerHash(certReq.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, certReq.marshal())
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
certMsg := &certificateMsg{
|
|
|
|
hasRequestContext: true,
|
|
|
|
}
|
|
|
|
if !config.Bugs.EmptyCertificateList {
|
2016-11-01 17:39:36 +00:00
|
|
|
for i, certData := range hs.cert.Certificate {
|
|
|
|
cert := certificateEntry{
|
|
|
|
data: certData,
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
if hs.clientHello.ocspStapling {
|
|
|
|
cert.ocspResponse = hs.cert.OCSPStaple
|
|
|
|
}
|
|
|
|
if hs.clientHello.sctListSupported {
|
|
|
|
cert.sctList = hs.cert.SignedCertificateTimestampList
|
|
|
|
}
|
|
|
|
cert.duplicateExtensions = config.Bugs.SendDuplicateCertExtensions
|
|
|
|
cert.extraExtension = config.Bugs.SendExtensionOnCertificate
|
|
|
|
} else {
|
|
|
|
if config.Bugs.SendOCSPOnIntermediates != nil {
|
|
|
|
cert.ocspResponse = config.Bugs.SendOCSPOnIntermediates
|
|
|
|
}
|
|
|
|
if config.Bugs.SendSCTOnIntermediates != nil {
|
|
|
|
cert.sctList = config.Bugs.SendSCTOnIntermediates
|
|
|
|
}
|
|
|
|
}
|
|
|
|
certMsg.certificates = append(certMsg.certificates, cert)
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
2016-07-13 21:58:23 +01:00
|
|
|
certMsgBytes := certMsg.marshal()
|
2018-06-08 00:39:49 +01:00
|
|
|
sentCompressedCertMsg := false
|
|
|
|
|
|
|
|
FindCertCompressionAlg:
|
|
|
|
for candidate, alg := range c.config.CertCompressionAlgs {
|
|
|
|
for _, id := range hs.clientHello.compressedCertAlgs {
|
|
|
|
if id == candidate {
|
|
|
|
if expected := config.Bugs.ExpectedCompressedCert; expected != 0 && expected != id {
|
|
|
|
return fmt.Errorf("expected to send compressed cert with alg %d, but picked %d", expected, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if override := config.Bugs.SendCertCompressionAlgId; override != 0 {
|
|
|
|
id = override
|
|
|
|
}
|
|
|
|
|
|
|
|
uncompressed := certMsgBytes[4:]
|
|
|
|
uncompressedLen := uint32(len(uncompressed))
|
|
|
|
if override := config.Bugs.SendCertUncompressedLength; override != 0 {
|
|
|
|
uncompressedLen = override
|
|
|
|
}
|
|
|
|
|
|
|
|
compressedCertMsgBytes := (&compressedCertificateMsg{
|
|
|
|
algID: id,
|
|
|
|
uncompressedLength: uncompressedLen,
|
|
|
|
compressed: alg.Compress(uncompressed),
|
|
|
|
}).marshal()
|
|
|
|
|
|
|
|
hs.writeServerHash(compressedCertMsgBytes)
|
|
|
|
c.writeRecord(recordTypeHandshake, compressedCertMsgBytes)
|
|
|
|
sentCompressedCertMsg = true
|
|
|
|
break FindCertCompressionAlg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sentCompressedCertMsg {
|
|
|
|
if config.Bugs.ExpectedCompressedCert != 0 {
|
|
|
|
return errors.New("unexpectedly sent uncompressed certificate")
|
|
|
|
}
|
|
|
|
hs.writeServerHash(certMsgBytes)
|
|
|
|
c.writeRecord(recordTypeHandshake, certMsgBytes)
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
|
|
|
|
certVerify := &certificateVerifyMsg{
|
|
|
|
hasSignatureAlgorithm: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the hash to sign.
|
|
|
|
privKey := hs.cert.PrivateKey
|
|
|
|
|
|
|
|
var err error
|
|
|
|
certVerify.signatureAlgorithm, err = selectSignatureAlgorithm(c.vers, privKey, config, hs.clientHello.signatureAlgorithms)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
input := hs.finishedHash.certificateVerifyInput(serverCertificateVerifyContextTLS13)
|
|
|
|
certVerify.signature, err = signMessage(c.vers, privKey, c.config, certVerify.signatureAlgorithm, input)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-15 11:51:15 +01:00
|
|
|
if config.Bugs.SendSignatureAlgorithm != 0 {
|
|
|
|
certVerify.signatureAlgorithm = config.Bugs.SendSignatureAlgorithm
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:01:06 +01:00
|
|
|
if !config.Bugs.SkipCertificateVerify {
|
|
|
|
hs.writeServerHash(certVerify.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, certVerify.marshal())
|
|
|
|
}
|
2016-09-06 19:13:43 +01:00
|
|
|
} else if hs.sessionState != nil {
|
2016-07-26 00:16:28 +01:00
|
|
|
// Pick up certificates from the session instead.
|
2016-10-04 22:51:35 +01:00
|
|
|
if len(hs.sessionState.certificates) > 0 {
|
2016-07-26 00:16:28 +01:00
|
|
|
if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
finished := new(finishedMsg)
|
2016-10-03 17:25:56 +01:00
|
|
|
finished.verifyData = hs.finishedHash.serverSum(serverHandshakeTrafficSecret)
|
2016-07-08 01:36:52 +01:00
|
|
|
if config.Bugs.BadFinished {
|
|
|
|
finished.verifyData[0]++
|
|
|
|
}
|
|
|
|
hs.writeServerHash(finished.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, finished.marshal())
|
2016-07-27 22:40:37 +01:00
|
|
|
if c.config.Bugs.SendExtraFinished {
|
|
|
|
c.writeRecord(recordTypeHandshake, finished.marshal())
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
c.flushHandshake()
|
|
|
|
|
2017-03-09 19:56:07 +00:00
|
|
|
if encryptedExtensions.extensions.hasEarlyData && !c.skipEarlyData {
|
|
|
|
for _, expectedMsg := range config.Bugs.ExpectLateEarlyData {
|
|
|
|
if err := c.readRecord(recordTypeApplicationData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(c.input.data[c.input.off:], expectedMsg) {
|
|
|
|
return errors.New("ExpectLateEarlyData: did not get expected message")
|
|
|
|
}
|
|
|
|
c.in.freeBlock(c.input)
|
|
|
|
c.input = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
// The various secrets do not incorporate the client's final leg, so
|
|
|
|
// derive them now before updating the handshake context.
|
2017-10-11 17:29:36 +01:00
|
|
|
hs.finishedHash.nextSecret()
|
2016-12-04 05:02:43 +00:00
|
|
|
hs.finishedHash.addEntropy(hs.finishedHash.zeroSecret())
|
2017-10-11 17:29:36 +01:00
|
|
|
|
2018-01-22 20:20:31 +00:00
|
|
|
clientTrafficSecret := hs.finishedHash.deriveSecret(clientApplicationTrafficLabel)
|
|
|
|
serverTrafficSecret := hs.finishedHash.deriveSecret(serverApplicationTrafficLabel)
|
|
|
|
c.exporterSecret = hs.finishedHash.deriveSecret(exporterLabel)
|
2016-07-08 01:36:52 +01:00
|
|
|
|
2016-07-15 04:15:40 +01:00
|
|
|
// Switch to application data keys on write. In particular, any alerts
|
|
|
|
// from the client certificate are sent over these keys.
|
2017-10-17 07:56:02 +01:00
|
|
|
c.useOutTrafficSecret(c.wireVersion, hs.suite, serverTrafficSecret)
|
2016-07-15 04:15:40 +01:00
|
|
|
|
2016-12-02 19:08:40 +00:00
|
|
|
// Send 0.5-RTT messages.
|
|
|
|
for _, halfRTTMsg := range config.Bugs.SendHalfRTTData {
|
|
|
|
if _, err := c.writeRecord(recordTypeApplicationData, halfRTTMsg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
// Read end_of_early_data.
|
2016-12-20 01:38:41 +00:00
|
|
|
if encryptedExtensions.extensions.hasEarlyData {
|
2018-01-22 20:20:31 +00:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-11 17:29:36 +01:00
|
|
|
|
2018-01-22 20:20:31 +00:00
|
|
|
endOfEarlyData, ok := msg.(*endOfEarlyDataMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(endOfEarlyData, msg)
|
2016-12-20 01:38:41 +00:00
|
|
|
}
|
2018-01-22 20:20:31 +00:00
|
|
|
hs.writeClientHash(endOfEarlyData.marshal())
|
2017-06-13 17:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-12-20 01:38:41 +00:00
|
|
|
// Switch input stream to handshake traffic keys.
|
2017-10-17 22:14:58 +01:00
|
|
|
if err := c.useInTrafficSecret(c.wireVersion, hs.suite, clientHandshakeTrafficSecret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-20 01:38:41 +00:00
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
// If we requested a client certificate, then the client must send a
|
|
|
|
// certificate message, even if it's empty.
|
|
|
|
if config.ClientAuth >= RequestClientCert {
|
2016-07-09 22:26:01 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
certMsg, ok := msg.(*certificateMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(certMsg, msg)
|
|
|
|
}
|
|
|
|
hs.writeClientHash(certMsg.marshal())
|
|
|
|
|
|
|
|
if len(certMsg.certificates) == 0 {
|
|
|
|
// The client didn't actually send a certificate
|
|
|
|
switch config.ClientAuth {
|
|
|
|
case RequireAnyClientCert, RequireAndVerifyClientCert:
|
2016-10-08 01:51:43 +01:00
|
|
|
c.sendAlert(alertCertificateRequired)
|
2016-07-09 22:26:01 +01:00
|
|
|
return errors.New("tls: client didn't provide a certificate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
var certs [][]byte
|
|
|
|
for _, cert := range certMsg.certificates {
|
|
|
|
certs = append(certs, cert.data)
|
|
|
|
// OCSP responses and SCT lists are not negotiated in
|
|
|
|
// client certificates.
|
|
|
|
if cert.ocspResponse != nil || cert.sctList != nil {
|
|
|
|
c.sendAlert(alertUnsupportedExtension)
|
|
|
|
return errors.New("tls: unexpected extensions in the client certificate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub, err := hs.processCertsFromClient(certs)
|
2016-07-09 22:26:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.peerCertificates) > 0 {
|
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
certVerify, ok := msg.(*certificateVerifyMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(certVerify, msg)
|
|
|
|
}
|
|
|
|
|
2016-07-14 02:18:49 +01:00
|
|
|
c.peerSignatureAlgorithm = certVerify.signatureAlgorithm
|
2016-07-09 22:26:01 +01:00
|
|
|
input := hs.finishedHash.certificateVerifyInput(clientCertificateVerifyContextTLS13)
|
|
|
|
if err := verifyMessage(c.vers, pub, config, certVerify.signatureAlgorithm, input, certVerify.signature); err != nil {
|
|
|
|
c.sendAlert(alertBadCertificate)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hs.writeClientHash(certVerify.marshal())
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
|
|
|
|
2016-09-24 00:25:11 +01:00
|
|
|
if encryptedExtensions.extensions.channelIDRequested {
|
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
channelIDMsg, ok := msg.(*channelIDMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(channelIDMsg, msg)
|
|
|
|
}
|
|
|
|
channelIDHash := crypto.SHA256.New()
|
|
|
|
channelIDHash.Write(hs.finishedHash.certificateVerifyInput(channelIDContextTLS13))
|
|
|
|
channelID, err := verifyChannelIDMessage(channelIDMsg, channelIDHash.Sum(nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.channelID = channelID
|
|
|
|
|
|
|
|
hs.writeClientHash(channelIDMsg.marshal())
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
// Read the client Finished message.
|
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clientFinished, ok := msg.(*finishedMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(clientFinished, msg)
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:25:56 +01:00
|
|
|
verify := hs.finishedHash.clientSum(clientHandshakeTrafficSecret)
|
2016-07-08 01:36:52 +01:00
|
|
|
if len(verify) != len(clientFinished.verifyData) ||
|
|
|
|
subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return errors.New("tls: client's Finished message was incorrect")
|
|
|
|
}
|
2016-07-13 22:57:35 +01:00
|
|
|
hs.writeClientHash(clientFinished.marshal())
|
2016-07-08 01:36:52 +01:00
|
|
|
|
2016-07-15 04:15:40 +01:00
|
|
|
// Switch to application data keys on read.
|
2017-10-17 22:14:58 +01:00
|
|
|
if err := c.useInTrafficSecret(c.wireVersion, hs.suite, clientTrafficSecret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
|
|
|
|
c.cipherSuite = hs.suite
|
2018-01-22 20:20:31 +00:00
|
|
|
c.resumptionSecret = hs.finishedHash.deriveSecret(resumptionLabel)
|
2016-07-18 00:25:41 +01:00
|
|
|
|
|
|
|
// TODO(davidben): Allow configuring the number of tickets sent for
|
|
|
|
// testing.
|
2016-11-01 17:39:36 +00:00
|
|
|
if !c.config.SessionTicketsDisabled && foundKEMode {
|
2016-07-18 00:25:41 +01:00
|
|
|
ticketCount := 2
|
|
|
|
for i := 0; i < ticketCount; i++ {
|
2017-10-11 17:29:36 +01:00
|
|
|
c.SendNewSessionTicket([]byte{byte(i)})
|
2016-07-18 00:25:41 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-04 18:05:26 +01:00
|
|
|
// processClientHello processes the ClientHello message from the client and
|
|
|
|
// decides whether we will perform session resumption.
|
|
|
|
func (hs *serverHandshakeState) processClientHello() (isResume bool, err error) {
|
|
|
|
config := hs.c.config
|
|
|
|
c := hs.c
|
|
|
|
|
|
|
|
hs.hello = &serverHelloMsg{
|
|
|
|
isDTLS: c.isDTLS,
|
2017-06-20 15:55:02 +01:00
|
|
|
vers: c.wireVersion,
|
2016-09-27 00:20:48 +01:00
|
|
|
versOverride: config.Bugs.SendServerHelloVersion,
|
2017-07-01 15:11:53 +01:00
|
|
|
compressionMethod: config.Bugs.SendCompressionMethod,
|
2017-07-10 17:57:25 +01:00
|
|
|
extensions: serverExtensions{
|
2018-05-05 03:00:33 +01:00
|
|
|
supportedVersion: config.Bugs.SendServerSupportedVersionExtension,
|
2017-07-10 17:57:25 +01:00
|
|
|
},
|
2017-07-14 23:40:34 +01:00
|
|
|
omitExtensions: config.Bugs.OmitExtensions,
|
|
|
|
emptyExtensions: config.Bugs.EmptyExtensions,
|
2016-07-04 18:05:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hs.hello.random = make([]byte, 32)
|
|
|
|
_, err = io.ReadFull(config.rand(), hs.hello.random)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return false, err
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2018-08-13 15:54:48 +01:00
|
|
|
|
|
|
|
_, supportsTLS13 := c.config.isSupportedVersion(VersionTLS13, false)
|
|
|
|
|
2018-08-15 04:26:39 +01:00
|
|
|
// Signal downgrades in the server random, per RFC 8446, section 4.1.3.
|
2018-08-13 15:54:48 +01:00
|
|
|
if supportsTLS13 || config.Bugs.SendTLS13DowngradeRandom {
|
|
|
|
if c.vers <= VersionTLS12 && config.maxVersion(c.isDTLS) >= VersionTLS13 {
|
|
|
|
copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS13)
|
|
|
|
}
|
|
|
|
if c.vers <= VersionTLS11 && config.maxVersion(c.isDTLS) == VersionTLS12 {
|
|
|
|
copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS12)
|
|
|
|
}
|
2017-12-18 23:00:23 +00:00
|
|
|
}
|
2018-11-19 21:49:56 +00:00
|
|
|
if config.Bugs.SendJDK11DowngradeRandom {
|
|
|
|
copy(hs.hello.random[len(hs.hello.random)-8:], downgradeJDK11)
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-07-17 16:12:34 +01:00
|
|
|
if len(hs.clientHello.sessionId) == 0 && c.config.Bugs.ExpectClientHelloSessionID {
|
|
|
|
return false, errors.New("tls: expected non-empty session ID from client")
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
foundCompression := false
|
|
|
|
// We only support null compression, so check that the client offered it.
|
|
|
|
for _, compression := range hs.clientHello.compressionMethods {
|
|
|
|
if compression == compressionNone {
|
|
|
|
foundCompression = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !foundCompression {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return false, errors.New("tls: client does not support uncompressed connections")
|
|
|
|
}
|
|
|
|
|
2016-07-04 18:05:26 +01:00
|
|
|
if err := hs.processClientExtensions(&hs.hello.extensions); err != nil {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false, err
|
|
|
|
}
|
2014-10-29 00:29:33 +00:00
|
|
|
|
2016-07-04 18:05:26 +01:00
|
|
|
supportedCurve := false
|
|
|
|
preferredCurves := config.curvePreferences()
|
|
|
|
Curves:
|
|
|
|
for _, curve := range hs.clientHello.supportedCurves {
|
2018-11-12 21:53:42 +00:00
|
|
|
if curve == CurveCECPQ2 && c.vers < VersionTLS13 {
|
|
|
|
// CECPQ2 is TLS 1.3-only.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-04 18:05:26 +01:00
|
|
|
for _, supported := range preferredCurves {
|
|
|
|
if supported == curve {
|
|
|
|
supportedCurve = true
|
|
|
|
break Curves
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
supportedPointFormat := false
|
|
|
|
for _, pointFormat := range hs.clientHello.supportedPoints {
|
|
|
|
if pointFormat == pointFormatUncompressed {
|
|
|
|
supportedPointFormat = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hs.ellipticOk = supportedCurve && supportedPointFormat
|
2016-07-04 17:20:45 +01:00
|
|
|
|
|
|
|
_, hs.ecdsaOk = hs.cert.PrivateKey.(*ecdsa.PrivateKey)
|
2017-03-29 00:28:44 +01:00
|
|
|
// Ed25519 also uses ECDSA certificates.
|
|
|
|
_, ed25519Ok := hs.cert.PrivateKey.(ed25519.PrivateKey)
|
|
|
|
hs.ecdsaOk = hs.ecdsaOk || ed25519Ok
|
2016-07-04 17:20:45 +01:00
|
|
|
|
|
|
|
// For test purposes, check that the peer never offers a session when
|
|
|
|
// renegotiating.
|
|
|
|
if c.cipherSuite != nil && len(hs.clientHello.sessionId) > 0 && c.config.Bugs.FailIfResumeOnRenego {
|
|
|
|
return false, errors.New("tls: offered resumption on renegotiation")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.config.Bugs.FailIfSessionOffered && (len(hs.clientHello.sessionTicket) > 0 || len(hs.clientHello.sessionId) > 0) {
|
|
|
|
return false, errors.New("tls: client offered a session ticket or ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
if hs.checkForResumption() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var preferenceList, supportedList []uint16
|
|
|
|
if c.config.PreferServerCipherSuites {
|
|
|
|
preferenceList = c.config.cipherSuites()
|
|
|
|
supportedList = hs.clientHello.cipherSuites
|
|
|
|
} else {
|
|
|
|
preferenceList = hs.clientHello.cipherSuites
|
|
|
|
supportedList = c.config.cipherSuites()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range preferenceList {
|
2016-09-06 19:13:43 +01:00
|
|
|
if hs.suite = c.tryCipherSuite(id, supportedList, c.vers, hs.ellipticOk, hs.ecdsaOk); hs.suite != nil {
|
2016-07-04 17:20:45 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hs.suite == nil {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return false, errors.New("tls: no cipher suite supported by both client and server")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processClientExtensions processes all ClientHello extensions not directly
|
|
|
|
// related to cipher suite negotiation and writes responses in serverExtensions.
|
|
|
|
func (hs *serverHandshakeState) processClientExtensions(serverExtensions *serverExtensions) error {
|
|
|
|
config := hs.c.config
|
|
|
|
c := hs.c
|
|
|
|
|
2016-07-18 00:03:18 +01:00
|
|
|
if c.vers < VersionTLS13 || config.Bugs.NegotiateRenegotiationInfoAtAllVersions {
|
2016-07-08 01:36:52 +01:00
|
|
|
if !bytes.Equal(c.clientVerify, hs.clientHello.secureRenegotiation) {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return errors.New("tls: renegotiation mismatch")
|
|
|
|
}
|
2014-10-29 02:06:14 +00:00
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo {
|
|
|
|
serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.clientVerify...)
|
|
|
|
serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.serverVerify...)
|
|
|
|
if c.config.Bugs.BadRenegotiationInfo {
|
|
|
|
serverExtensions.secureRenegotiation[0] ^= 0x80
|
|
|
|
}
|
2017-07-01 05:31:27 +01:00
|
|
|
if c.config.Bugs.BadRenegotiationInfoEnd {
|
|
|
|
serverExtensions.secureRenegotiation[len(serverExtensions.secureRenegotiation)-1] ^= 0x80
|
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
} else {
|
|
|
|
serverExtensions.secureRenegotiation = hs.clientHello.secureRenegotiation
|
2014-10-29 02:06:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 01:36:52 +01:00
|
|
|
if c.noRenegotiationInfo() {
|
|
|
|
serverExtensions.secureRenegotiation = nil
|
|
|
|
}
|
2015-05-16 04:09:47 +01:00
|
|
|
}
|
|
|
|
|
2016-07-04 17:20:45 +01:00
|
|
|
serverExtensions.duplicateExtension = c.config.Bugs.DuplicateExtension
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
if len(hs.clientHello.serverName) > 0 {
|
|
|
|
c.serverName = hs.clientHello.serverName
|
|
|
|
}
|
2016-07-04 17:20:45 +01:00
|
|
|
if len(config.Certificates) == 0 {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return errors.New("tls: no certificates configured")
|
|
|
|
}
|
|
|
|
hs.cert = &config.Certificates[0]
|
|
|
|
if len(hs.clientHello.serverName) > 0 {
|
|
|
|
hs.cert = config.getCertificateForName(hs.clientHello.serverName)
|
|
|
|
}
|
|
|
|
if expected := c.config.Bugs.ExpectServerName; expected != "" && expected != hs.clientHello.serverName {
|
|
|
|
return errors.New("tls: unexpected server name")
|
|
|
|
}
|
2014-09-15 21:51:51 +01:00
|
|
|
|
2017-03-01 01:54:28 +00:00
|
|
|
if cert := config.Bugs.RenegotiationCertificate; c.cipherSuite != nil && cert != nil {
|
|
|
|
hs.cert = cert
|
|
|
|
}
|
|
|
|
|
2014-09-15 21:51:51 +01:00
|
|
|
if len(hs.clientHello.alpnProtocols) > 0 {
|
2017-03-15 01:27:10 +00:00
|
|
|
// We will never offer ALPN as a client on renegotiation
|
|
|
|
// handshakes.
|
|
|
|
if len(c.clientVerify) > 0 {
|
|
|
|
return errors.New("tls: offered ALPN on renegotiation")
|
|
|
|
}
|
2015-07-09 19:35:04 +01:00
|
|
|
if proto := c.config.Bugs.ALPNProtocol; proto != nil {
|
2016-07-04 17:20:45 +01:00
|
|
|
serverExtensions.alpnProtocol = *proto
|
|
|
|
serverExtensions.alpnProtocolEmpty = len(*proto) == 0
|
2015-07-09 19:35:04 +01:00
|
|
|
c.clientProtocol = *proto
|
|
|
|
c.usedALPN = true
|
|
|
|
} else if selectedProto, fallback := mutualProtocol(hs.clientHello.alpnProtocols, c.config.NextProtos); !fallback {
|
2016-07-04 17:20:45 +01:00
|
|
|
serverExtensions.alpnProtocol = selectedProto
|
2014-09-15 21:51:51 +01:00
|
|
|
c.clientProtocol = selectedProto
|
2014-09-06 18:21:53 +01:00
|
|
|
c.usedALPN = true
|
2014-09-15 21:51:51 +01:00
|
|
|
}
|
2015-08-31 19:24:29 +01:00
|
|
|
}
|
2016-07-08 01:36:52 +01:00
|
|
|
|
2016-08-01 17:05:50 +01:00
|
|
|
if len(c.config.Bugs.SendALPN) > 0 {
|
|
|
|
serverExtensions.alpnProtocol = c.config.Bugs.SendALPN
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:03:18 +01:00
|
|
|
if c.vers < VersionTLS13 || config.Bugs.NegotiateNPNAtAllVersions {
|
2016-07-08 01:36:52 +01:00
|
|
|
if len(hs.clientHello.alpnProtocols) == 0 || c.config.Bugs.NegotiateALPNAndNPN {
|
|
|
|
// Although sending an empty NPN extension is reasonable, Firefox has
|
|
|
|
// had a bug around this. Best to send nothing at all if
|
|
|
|
// config.NextProtos is empty. See
|
|
|
|
// https://code.google.com/p/go/issues/detail?id=5445.
|
|
|
|
if hs.clientHello.nextProtoNeg && len(config.NextProtos) > 0 {
|
|
|
|
serverExtensions.nextProtoNeg = true
|
|
|
|
serverExtensions.nextProtos = config.NextProtos
|
2016-11-01 17:39:36 +00:00
|
|
|
serverExtensions.npnAfterAlpn = config.Bugs.SwapNPNAndALPN
|
2016-07-08 01:36:52 +01:00
|
|
|
}
|
2014-09-15 21:51:51 +01:00
|
|
|
}
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-12-22 23:50:43 +00:00
|
|
|
if len(hs.clientHello.quicTransportParams) > 0 {
|
|
|
|
c.quicTransportParams = hs.clientHello.quicTransportParams
|
|
|
|
serverExtensions.quicTransportParams = c.config.QUICTransportParams
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:03:18 +01:00
|
|
|
if c.vers < VersionTLS13 || config.Bugs.NegotiateEMSAtAllVersions {
|
2016-08-30 04:14:17 +01:00
|
|
|
disableEMS := config.Bugs.NoExtendedMasterSecret
|
|
|
|
if c.cipherSuite != nil {
|
|
|
|
disableEMS = config.Bugs.NoExtendedMasterSecretOnRenegotiation
|
|
|
|
}
|
|
|
|
serverExtensions.extendedMasterSecret = c.vers >= VersionTLS10 && hs.clientHello.extendedMasterSecret && !disableEMS
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-09-24 00:25:11 +01:00
|
|
|
if hs.clientHello.channelIDSupported && config.RequestChannelID {
|
|
|
|
serverExtensions.channelIDRequested = true
|
2014-08-24 06:44:23 +01:00
|
|
|
}
|
|
|
|
|
2017-09-21 23:02:22 +01:00
|
|
|
if config.TokenBindingParams != nil {
|
|
|
|
if !bytes.Equal(config.ExpectTokenBindingParams, hs.clientHello.tokenBindingParams) {
|
|
|
|
return errors.New("client did not send expected token binding params")
|
|
|
|
}
|
|
|
|
|
|
|
|
// For testing, blindly send whatever is set in config, even if
|
|
|
|
// it is invalid.
|
|
|
|
serverExtensions.tokenBindingParams = config.TokenBindingParams
|
|
|
|
serverExtensions.tokenBindingVersion = config.TokenBindingVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hs.clientHello.tokenBindingParams) > 0 && (!hs.clientHello.extendedMasterSecret || hs.clientHello.secureRenegotiation == nil) {
|
|
|
|
return errors.New("client sent Token Binding without EMS and/or RI")
|
|
|
|
}
|
|
|
|
|
2014-11-16 00:06:08 +00:00
|
|
|
if hs.clientHello.srtpProtectionProfiles != nil {
|
|
|
|
SRTPLoop:
|
|
|
|
for _, p1 := range c.config.SRTPProtectionProfiles {
|
|
|
|
for _, p2 := range hs.clientHello.srtpProtectionProfiles {
|
|
|
|
if p1 == p2 {
|
2016-07-04 17:20:45 +01:00
|
|
|
serverExtensions.srtpProtectionProfile = p1
|
2014-11-16 00:06:08 +00:00
|
|
|
c.srtpProtectionProfile = p1
|
|
|
|
break SRTPLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.config.Bugs.SendSRTPProtectionProfile != 0 {
|
2016-07-04 17:20:45 +01:00
|
|
|
serverExtensions.srtpProtectionProfile = c.config.Bugs.SendSRTPProtectionProfile
|
2014-11-16 00:06:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:10:13 +01:00
|
|
|
if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil {
|
|
|
|
if hs.clientHello.customExtension != *expected {
|
2016-07-04 17:20:45 +01:00
|
|
|
return fmt.Errorf("tls: bad custom extension contents %q", hs.clientHello.customExtension)
|
2014-06-23 20:03:11 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 17:20:45 +01:00
|
|
|
serverExtensions.customExtension = config.Bugs.CustomExtension
|
2014-06-23 20:03:11 +01:00
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
if c.config.Bugs.AdvertiseTicketExtension {
|
|
|
|
serverExtensions.ticketSupported = true
|
|
|
|
}
|
|
|
|
|
2016-12-22 14:16:57 +00:00
|
|
|
if c.config.Bugs.SendSupportedPointFormats != nil {
|
|
|
|
serverExtensions.supportedPoints = c.config.Bugs.SendSupportedPointFormats
|
|
|
|
}
|
|
|
|
|
2017-10-04 22:01:22 +01:00
|
|
|
if c.config.Bugs.SendServerSupportedCurves {
|
|
|
|
serverExtensions.supportedCurves = c.config.curvePreferences()
|
|
|
|
}
|
|
|
|
|
2016-09-03 02:35:25 +01:00
|
|
|
if !hs.clientHello.hasGREASEExtension && config.Bugs.ExpectGREASE {
|
|
|
|
return errors.New("tls: no GREASE extension found")
|
|
|
|
}
|
|
|
|
|
2017-02-06 18:49:07 +00:00
|
|
|
serverExtensions.serverNameAck = c.config.Bugs.SendServerNameAck
|
|
|
|
|
2016-07-04 17:20:45 +01:00
|
|
|
return nil
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkForResumption returns true if we should perform resumption on this connection.
|
|
|
|
func (hs *serverHandshakeState) checkForResumption() bool {
|
|
|
|
c := hs.c
|
|
|
|
|
2016-08-08 22:25:07 +01:00
|
|
|
ticket := hs.clientHello.sessionTicket
|
|
|
|
if len(ticket) == 0 && len(hs.clientHello.pskIdentities) > 0 && c.config.Bugs.AcceptAnySession {
|
2016-09-01 17:29:49 +01:00
|
|
|
ticket = hs.clientHello.pskIdentities[0].ticket
|
2016-08-08 22:25:07 +01:00
|
|
|
}
|
|
|
|
if len(ticket) > 0 {
|
2014-11-17 08:19:02 +00:00
|
|
|
if c.config.SessionTicketsDisabled {
|
|
|
|
return false
|
|
|
|
}
|
2014-09-24 20:19:56 +01:00
|
|
|
|
2014-11-17 08:19:02 +00:00
|
|
|
var ok bool
|
2016-08-08 22:25:07 +01:00
|
|
|
if hs.sessionState, ok = c.decryptTicket(ticket); !ok {
|
2014-11-17 08:19:02 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if c.config.ServerSessionCache == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
sessionId := string(hs.clientHello.sessionId)
|
|
|
|
if hs.sessionState, ok = c.config.ServerSessionCache.Get(sessionId); !ok {
|
|
|
|
return false
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-09-06 19:13:43 +01:00
|
|
|
if c.config.Bugs.AcceptAnySession {
|
|
|
|
// Replace the cipher suite with one known to work, to test
|
|
|
|
// cross-version resumption attempts.
|
|
|
|
hs.sessionState.cipherSuite = TLS_RSA_WITH_AES_128_CBC_SHA
|
|
|
|
} else {
|
2016-08-08 22:25:07 +01:00
|
|
|
// Never resume a session for a different SSL version.
|
|
|
|
if c.vers != hs.sessionState.vers {
|
|
|
|
return false
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-08-08 22:25:07 +01:00
|
|
|
cipherSuiteOk := false
|
|
|
|
// Check that the client is still offering the ciphersuite in the session.
|
|
|
|
for _, id := range hs.clientHello.cipherSuites {
|
|
|
|
if id == hs.sessionState.cipherSuite {
|
|
|
|
cipherSuiteOk = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !cipherSuiteOk {
|
|
|
|
return false
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we also support the ciphersuite from the session.
|
2016-09-06 19:13:43 +01:00
|
|
|
hs.suite = c.tryCipherSuite(hs.sessionState.cipherSuite, c.config.cipherSuites(), c.vers, hs.ellipticOk, hs.ecdsaOk)
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
if hs.suite == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionHasClientCerts := len(hs.sessionState.certificates) != 0
|
|
|
|
needClientCerts := c.config.ClientAuth == RequireAnyClientCert || c.config.ClientAuth == RequireAndVerifyClientCert
|
|
|
|
if needClientCerts && !sessionHasClientCerts {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if sessionHasClientCerts && c.config.ClientAuth == NoClientCert {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *serverHandshakeState) doResumeHandshake() error {
|
|
|
|
c := hs.c
|
|
|
|
|
|
|
|
hs.hello.cipherSuite = hs.suite.id
|
2015-03-16 22:02:20 +00:00
|
|
|
if c.config.Bugs.SendCipherSuite != 0 {
|
|
|
|
hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
// We echo the client's session ID in the ServerHello to let it know
|
|
|
|
// that we're doing a resumption.
|
|
|
|
hs.hello.sessionId = hs.clientHello.sessionId
|
2016-07-01 16:43:18 +01:00
|
|
|
hs.hello.extensions.ticketSupported = c.config.Bugs.RenewTicketOnResume
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-05-05 00:19:06 +01:00
|
|
|
if c.config.Bugs.SendSCTListOnResume != nil {
|
2016-07-01 16:43:18 +01:00
|
|
|
hs.hello.extensions.sctList = c.config.Bugs.SendSCTListOnResume
|
2016-05-05 00:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-04 21:32:16 +01:00
|
|
|
if c.config.Bugs.SendOCSPResponseOnResume != nil {
|
|
|
|
// There is no way, syntactically, to send an OCSP response on a
|
|
|
|
// resumption handshake.
|
|
|
|
hs.hello.extensions.ocspStapling = true
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
hs.finishedHash = newFinishedHash(c.wireVersion, c.isDTLS, hs.suite)
|
2014-08-28 04:13:20 +01:00
|
|
|
hs.finishedHash.discardHandshakeBuffer()
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeClientHash(hs.clientHello.marshal())
|
|
|
|
hs.writeServerHash(hs.hello.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
c.writeRecord(recordTypeHandshake, hs.hello.marshal())
|
|
|
|
|
|
|
|
if len(hs.sessionState.certificates) > 0 {
|
|
|
|
if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.masterSecret = hs.sessionState.masterSecret
|
2014-10-11 00:23:43 +01:00
|
|
|
c.extendedMasterSecret = hs.sessionState.extendedMasterSecret
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *serverHandshakeState) doFullHandshake() error {
|
|
|
|
config := hs.c.config
|
|
|
|
c := hs.c
|
|
|
|
|
2014-10-27 05:06:24 +00:00
|
|
|
isPSK := hs.suite.flags&suitePSK != 0
|
|
|
|
if !isPSK && hs.clientHello.ocspStapling && len(hs.cert.OCSPStaple) > 0 {
|
2016-07-01 16:43:18 +01:00
|
|
|
hs.hello.extensions.ocspStapling = true
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 06:55:35 +00:00
|
|
|
if hs.clientHello.sctListSupported && len(hs.cert.SignedCertificateTimestampList) > 0 {
|
2016-07-01 16:43:18 +01:00
|
|
|
hs.hello.extensions.sctList = hs.cert.SignedCertificateTimestampList
|
2014-11-25 06:55:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 06:09:41 +01:00
|
|
|
if len(c.clientVerify) > 0 && config.Bugs.SendSCTListOnRenegotiation != nil {
|
|
|
|
hs.hello.extensions.sctList = config.Bugs.SendSCTListOnRenegotiation
|
|
|
|
}
|
|
|
|
|
2016-07-01 16:43:18 +01:00
|
|
|
hs.hello.extensions.ticketSupported = hs.clientHello.ticketSupported && !config.SessionTicketsDisabled && c.vers > VersionSSL30
|
2014-06-20 20:00:00 +01:00
|
|
|
hs.hello.cipherSuite = hs.suite.id
|
2014-12-27 06:50:38 +00:00
|
|
|
if config.Bugs.SendCipherSuite != 0 {
|
|
|
|
hs.hello.cipherSuite = config.Bugs.SendCipherSuite
|
|
|
|
}
|
2016-07-01 16:43:18 +01:00
|
|
|
c.extendedMasterSecret = hs.hello.extensions.extendedMasterSecret
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-11-17 08:19:02 +00:00
|
|
|
// Generate a session ID if we're to save the session.
|
2016-07-01 16:43:18 +01:00
|
|
|
if !hs.hello.extensions.ticketSupported && config.ServerSessionCache != nil {
|
2014-11-17 08:19:02 +00:00
|
|
|
hs.hello.sessionId = make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(config.rand(), hs.hello.sessionId); err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return errors.New("tls: short read from Rand: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 21:33:12 +00:00
|
|
|
if config.Bugs.EchoSessionIDInFullHandshake {
|
|
|
|
hs.hello.sessionId = hs.clientHello.sessionId
|
|
|
|
}
|
2014-11-17 08:19:02 +00:00
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
hs.finishedHash = newFinishedHash(c.wireVersion, c.isDTLS, hs.suite)
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeClientHash(hs.clientHello.marshal())
|
|
|
|
hs.writeServerHash(hs.hello.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-09-04 19:18:58 +01:00
|
|
|
if config.Bugs.SendSNIWarningAlert {
|
|
|
|
c.SendAlert(alertLevelWarning, alertUnrecognizedName)
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, hs.hello.marshal())
|
|
|
|
|
2014-10-27 05:06:24 +00:00
|
|
|
if !isPSK {
|
|
|
|
certMsg := new(certificateMsg)
|
2015-06-07 16:42:34 +01:00
|
|
|
if !config.Bugs.EmptyCertificateList {
|
2016-11-01 17:39:36 +00:00
|
|
|
for _, certData := range hs.cert.Certificate {
|
|
|
|
certMsg.certificates = append(certMsg.certificates, certificateEntry{
|
|
|
|
data: certData,
|
|
|
|
})
|
|
|
|
}
|
2015-06-07 16:42:34 +01:00
|
|
|
}
|
2014-10-27 05:06:24 +00:00
|
|
|
if !config.Bugs.UnauthenticatedECDH {
|
2015-02-25 04:45:43 +00:00
|
|
|
certMsgBytes := certMsg.marshal()
|
|
|
|
hs.writeServerHash(certMsgBytes)
|
|
|
|
c.writeRecord(recordTypeHandshake, certMsgBytes)
|
2014-10-27 05:06:24 +00:00
|
|
|
}
|
2014-07-12 05:48:23 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-07-01 16:43:18 +01:00
|
|
|
if hs.hello.extensions.ocspStapling && !c.config.Bugs.SkipCertificateStatus {
|
2014-06-20 20:00:00 +01:00
|
|
|
certStatus := new(certificateStatusMsg)
|
|
|
|
certStatus.statusType = statusTypeOCSP
|
|
|
|
certStatus.response = hs.cert.OCSPStaple
|
2017-08-24 06:09:41 +01:00
|
|
|
if len(c.clientVerify) > 0 && config.Bugs.SendOCSPResponseOnRenegotiation != nil {
|
|
|
|
certStatus.response = config.Bugs.SendOCSPResponseOnRenegotiation
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeServerHash(certStatus.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, certStatus.marshal())
|
|
|
|
}
|
|
|
|
|
|
|
|
keyAgreement := hs.suite.ka(c.vers)
|
2018-11-12 21:53:42 +00:00
|
|
|
skx, err := keyAgreement.generateServerKeyExchange(config, hs.cert, hs.clientHello, hs.hello, c.vers)
|
2014-06-20 20:00:00 +01:00
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return err
|
|
|
|
}
|
2016-07-18 17:40:30 +01:00
|
|
|
if ecdhe, ok := keyAgreement.(*ecdheKeyAgreement); ok {
|
|
|
|
c.curveID = ecdhe.curveID
|
|
|
|
}
|
2014-07-12 18:27:45 +01:00
|
|
|
if skx != nil && !config.Bugs.SkipServerKeyExchange {
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeServerHash(skx.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, skx.marshal())
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ClientAuth >= RequestClientCert {
|
|
|
|
// Request a client certificate
|
2014-07-08 22:30:11 +01:00
|
|
|
certReq := &certificateRequestMsg{
|
2017-10-11 17:29:36 +01:00
|
|
|
vers: c.wireVersion,
|
2014-07-08 22:30:11 +01:00
|
|
|
certificateTypes: config.ClientCertificateTypes,
|
|
|
|
}
|
|
|
|
if certReq.certificateTypes == nil {
|
|
|
|
certReq.certificateTypes = []byte{
|
|
|
|
byte(CertTypeRSASign),
|
|
|
|
byte(CertTypeECDSASign),
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if c.vers >= VersionTLS12 {
|
2016-06-21 23:19:24 +01:00
|
|
|
certReq.hasSignatureAlgorithm = true
|
|
|
|
if !config.Bugs.NoSignatureAlgorithms {
|
2016-07-09 19:21:54 +01:00
|
|
|
certReq.signatureAlgorithms = config.verifySignatureAlgorithms()
|
2014-11-14 06:43:59 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// An empty list of certificateAuthorities signals to
|
|
|
|
// the client that it may send any certificate in response
|
|
|
|
// to our request. When we know the CAs we trust, then
|
|
|
|
// we can send them down, so that the client can choose
|
|
|
|
// an appropriate certificate to give to us.
|
|
|
|
if config.ClientCAs != nil {
|
|
|
|
certReq.certificateAuthorities = config.ClientCAs.Subjects()
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeServerHash(certReq.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, certReq.marshal())
|
|
|
|
}
|
|
|
|
|
|
|
|
helloDone := new(serverHelloDoneMsg)
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeServerHash(helloDone.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, helloDone.marshal())
|
2016-07-07 20:33:25 +01:00
|
|
|
c.flushHandshake()
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
var pub crypto.PublicKey // public key for client auth, if any
|
|
|
|
|
Add DTLS timeout and retransmit tests.
This extends the packet adaptor protocol to send three commands:
type command =
| Packet of []byte
| Timeout of time.Duration
| TimeoutAck
When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the
BIO_read, returns out of the SSL stack, advances the clock, calls
DTLSv1_handle_timeout, and continues.
If the Go side sends Timeout right between sending handshake flight N and
reading flight N+1, the shim won't read the Timeout until it has sent flight
N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes
after N+1. Go then drops all packets before the TimeoutAck, thus dropping one
transmit of flight N+1 without having to actually process the packets to
determine the end of the flight. The shim then sees the updated clock, calls
DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real.
When dropping packets, Go checks the epoch and increments sequence numbers so
that we can continue to be strict here. This requires tracking the initial
sequence number of the next epoch.
The final Finished message takes an additional special-case to test. DTLS
triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only
implements the former which should be sufficient (and is necessary) EXCEPT for
the final Finished message. If the peer's final Finished message is lost, it
won't be waiting for a message from us, so it won't time out anything. That
retransmit must be triggered on stale message, so we retransmit the Finished
message in Go.
Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c
Reviewed-on: https://boringssl-review.googlesource.com/3212
Reviewed-by: Adam Langley <agl@google.com>
2015-01-27 06:09:43 +00:00
|
|
|
if err := c.simulatePacketLoss(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
// If we requested a client certificate, then the client must send a
|
|
|
|
// certificate message, even if it's empty.
|
|
|
|
if config.ClientAuth >= RequestClientCert {
|
2014-10-27 05:06:24 +00:00
|
|
|
var certMsg *certificateMsg
|
2016-03-10 20:44:22 +00:00
|
|
|
var certificates [][]byte
|
|
|
|
if certMsg, ok = msg.(*certificateMsg); ok {
|
|
|
|
if c.vers == VersionSSL30 && len(certMsg.certificates) == 0 {
|
|
|
|
return errors.New("tls: empty certificate message in SSL 3.0")
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.writeClientHash(certMsg.marshal())
|
2016-11-01 17:39:36 +00:00
|
|
|
for _, cert := range certMsg.certificates {
|
|
|
|
certificates = append(certificates, cert.data)
|
|
|
|
}
|
2017-01-02 13:30:36 +00:00
|
|
|
} else if c.vers == VersionSSL30 {
|
|
|
|
// In SSL 3.0, no certificate is signaled by a warning
|
|
|
|
// alert which we translate to ssl3NoCertificateMsg.
|
|
|
|
if _, ok := msg.(*ssl3NoCertificateMsg); !ok {
|
|
|
|
return errors.New("tls: client provided neither a certificate nor no_certificate warning alert")
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-10 20:44:22 +00:00
|
|
|
// In TLS, the Certificate message is required. In SSL
|
|
|
|
// 3.0, the peer skips it when sending no certificates.
|
2014-06-20 20:00:00 +01:00
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(certMsg, msg)
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:44:22 +00:00
|
|
|
if len(certificates) == 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
// The client didn't actually send a certificate
|
|
|
|
switch config.ClientAuth {
|
|
|
|
case RequireAnyClientCert, RequireAndVerifyClientCert:
|
|
|
|
c.sendAlert(alertBadCertificate)
|
|
|
|
return errors.New("tls: client didn't provide a certificate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:44:22 +00:00
|
|
|
pub, err = hs.processCertsFromClient(certificates)
|
2014-06-20 20:00:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:30:36 +00:00
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get client key exchange
|
|
|
|
ckx, ok := msg.(*clientKeyExchangeMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(ckx, msg)
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeClientHash(ckx.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-08-28 04:13:20 +01:00
|
|
|
preMasterSecret, err := keyAgreement.processClientKeyExchange(config, hs.cert, ckx, c.vers)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return err
|
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
if c.extendedMasterSecret {
|
|
|
|
hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash)
|
|
|
|
} else {
|
|
|
|
if c.config.Bugs.RequireExtendedMasterSecret {
|
|
|
|
return errors.New("tls: extended master secret required but not supported by peer")
|
|
|
|
}
|
|
|
|
hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.clientHello.random, hs.hello.random)
|
|
|
|
}
|
2014-08-28 04:13:20 +01:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
// If we received a client cert in response to our certificate request message,
|
|
|
|
// the client will send us a certificateVerifyMsg immediately after the
|
|
|
|
// clientKeyExchangeMsg. This message is a digest of all preceding
|
|
|
|
// handshake-layer messages that is signed using the private key corresponding
|
|
|
|
// to the client's certificate. This allows us to verify that the client is in
|
|
|
|
// possession of the private key of the certificate.
|
|
|
|
if len(c.peerCertificates) > 0 {
|
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certVerify, ok := msg.(*certificateVerifyMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(certVerify, msg)
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:03:41 +01:00
|
|
|
// Determine the signature type.
|
2016-06-21 23:19:24 +01:00
|
|
|
var sigAlg signatureAlgorithm
|
|
|
|
if certVerify.hasSignatureAlgorithm {
|
|
|
|
sigAlg = certVerify.signatureAlgorithm
|
|
|
|
c.peerSignatureAlgorithm = sigAlg
|
2014-07-18 20:03:41 +01:00
|
|
|
}
|
|
|
|
|
2016-06-21 23:19:24 +01:00
|
|
|
if c.vers > VersionSSL30 {
|
2016-07-09 02:52:12 +01:00
|
|
|
err = verifyMessage(c.vers, pub, c.config, sigAlg, hs.finishedHash.buffer, certVerify.signature)
|
2016-06-21 23:19:24 +01:00
|
|
|
} else {
|
|
|
|
// SSL 3.0's client certificate construction is
|
|
|
|
// incompatible with signatureAlgorithm.
|
|
|
|
rsaPub, ok := pub.(*rsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("unsupported key type for client certificate")
|
|
|
|
} else {
|
|
|
|
digest := hs.finishedHash.hashForClientCertificateSSL3(hs.masterSecret)
|
|
|
|
err = rsa.VerifyPKCS1v15(rsaPub, crypto.MD5SHA1, digest, certVerify.signature)
|
2014-07-18 20:03:41 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertBadCertificate)
|
|
|
|
return errors.New("could not validate signature of connection nonces: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeClientHash(certVerify.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-08-28 04:13:20 +01:00
|
|
|
hs.finishedHash.discardHandshakeBuffer()
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *serverHandshakeState) establishKeys() error {
|
|
|
|
c := hs.c
|
|
|
|
|
|
|
|
clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV :=
|
2016-06-15 02:14:35 +01:00
|
|
|
keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.clientHello.random, hs.hello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen(c.vers))
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
var clientCipher, serverCipher interface{}
|
|
|
|
var clientHash, serverHash macFunction
|
|
|
|
|
|
|
|
if hs.suite.aead == nil {
|
|
|
|
clientCipher = hs.suite.cipher(clientKey, clientIV, true /* for reading */)
|
|
|
|
clientHash = hs.suite.mac(c.vers, clientMAC)
|
|
|
|
serverCipher = hs.suite.cipher(serverKey, serverIV, false /* not for reading */)
|
|
|
|
serverHash = hs.suite.mac(c.vers, serverMAC)
|
|
|
|
} else {
|
2016-06-15 02:14:35 +01:00
|
|
|
clientCipher = hs.suite.aead(c.vers, clientKey, clientIV)
|
|
|
|
serverCipher = hs.suite.aead(c.vers, serverKey, serverIV)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-09-11 18:53:08 +01:00
|
|
|
c.in.prepareCipherSpec(c.wireVersion, clientCipher, clientHash)
|
|
|
|
c.out.prepareCipherSpec(c.wireVersion, serverCipher, serverHash)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-03 17:57:23 +01:00
|
|
|
func (hs *serverHandshakeState) readFinished(out []byte, isResume bool) error {
|
2014-06-20 20:00:00 +01:00
|
|
|
c := hs.c
|
|
|
|
|
|
|
|
c.readRecord(recordTypeChangeCipherSpec)
|
|
|
|
if err := c.in.error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-01 16:43:18 +01:00
|
|
|
if hs.hello.extensions.nextProtoNeg {
|
2014-06-20 20:00:00 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nextProto, ok := msg.(*nextProtoMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(nextProto, msg)
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeClientHash(nextProto.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
c.clientProtocol = nextProto.proto
|
|
|
|
}
|
|
|
|
|
2016-07-01 16:43:18 +01:00
|
|
|
if hs.hello.extensions.channelIDRequested {
|
2014-08-24 06:44:23 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-30 23:56:53 +01:00
|
|
|
channelIDMsg, ok := msg.(*channelIDMsg)
|
2014-08-24 06:44:23 +01:00
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
2016-06-30 23:56:53 +01:00
|
|
|
return unexpectedMessageError(channelIDMsg, msg)
|
2014-08-24 06:44:23 +01:00
|
|
|
}
|
|
|
|
var resumeHash []byte
|
|
|
|
if isResume {
|
|
|
|
resumeHash = hs.sessionState.handshakeHash
|
|
|
|
}
|
2016-09-24 00:25:11 +01:00
|
|
|
channelID, err := verifyChannelIDMessage(channelIDMsg, hs.finishedHash.hashForChannelID(resumeHash))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-24 06:44:23 +01:00
|
|
|
}
|
|
|
|
c.channelID = channelID
|
|
|
|
|
2016-06-30 23:56:53 +01:00
|
|
|
hs.writeClientHash(channelIDMsg.marshal())
|
2014-08-24 06:44:23 +01:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clientFinished, ok := msg.(*finishedMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(clientFinished, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
verify := hs.finishedHash.clientSum(hs.masterSecret)
|
|
|
|
if len(verify) != len(clientFinished.verifyData) ||
|
|
|
|
subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return errors.New("tls: client's Finished message is incorrect")
|
|
|
|
}
|
2014-10-29 00:29:33 +00:00
|
|
|
c.clientVerify = append(c.clientVerify[:0], clientFinished.verifyData...)
|
2015-06-03 17:57:23 +01:00
|
|
|
copy(out, clientFinished.verifyData)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeClientHash(clientFinished.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *serverHandshakeState) sendSessionTicket() error {
|
|
|
|
c := hs.c
|
|
|
|
state := sessionState{
|
2014-08-24 06:44:23 +01:00
|
|
|
vers: c.vers,
|
|
|
|
cipherSuite: hs.suite.id,
|
|
|
|
masterSecret: hs.masterSecret,
|
|
|
|
certificates: hs.certsFromClient,
|
2016-10-17 23:05:35 +01:00
|
|
|
handshakeHash: hs.finishedHash.Sum(),
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2014-11-17 08:19:02 +00:00
|
|
|
|
2016-07-01 16:43:18 +01:00
|
|
|
if !hs.hello.extensions.ticketSupported || hs.c.config.Bugs.SkipNewSessionTicket {
|
2014-11-17 08:19:02 +00:00
|
|
|
if c.config.ServerSessionCache != nil && len(hs.hello.sessionId) != 0 {
|
|
|
|
c.config.ServerSessionCache.Put(string(hs.hello.sessionId), &state)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(newSessionTicketMsg)
|
2017-10-11 17:29:36 +01:00
|
|
|
m.vers = c.wireVersion
|
|
|
|
m.isDTLS = c.isDTLS
|
2017-01-28 19:00:32 +00:00
|
|
|
if c.config.Bugs.SendTicketLifetime != 0 {
|
|
|
|
m.ticketLifetime = uint32(c.config.Bugs.SendTicketLifetime / time.Second)
|
|
|
|
}
|
2014-11-17 08:19:02 +00:00
|
|
|
|
2015-10-23 22:41:12 +01:00
|
|
|
if !c.config.Bugs.SendEmptySessionTicket {
|
|
|
|
var err error
|
|
|
|
m.ticket, err = c.encryptTicket(&state)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeServerHash(m.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, m.marshal())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-12 22:14:45 +01:00
|
|
|
func (hs *serverHandshakeState) sendFinished(out []byte, isResume bool) error {
|
2014-06-20 20:00:00 +01:00
|
|
|
c := hs.c
|
|
|
|
|
2014-07-21 21:14:03 +01:00
|
|
|
finished := new(finishedMsg)
|
|
|
|
finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret)
|
2015-06-03 17:57:23 +01:00
|
|
|
copy(out, finished.verifyData)
|
2015-04-03 00:33:31 +01:00
|
|
|
if c.config.Bugs.BadFinished {
|
|
|
|
finished.verifyData[0]++
|
|
|
|
}
|
2014-10-29 00:29:33 +00:00
|
|
|
c.serverVerify = append(c.serverVerify[:0], finished.verifyData...)
|
Add DTLS timeout and retransmit tests.
This extends the packet adaptor protocol to send three commands:
type command =
| Packet of []byte
| Timeout of time.Duration
| TimeoutAck
When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the
BIO_read, returns out of the SSL stack, advances the clock, calls
DTLSv1_handle_timeout, and continues.
If the Go side sends Timeout right between sending handshake flight N and
reading flight N+1, the shim won't read the Timeout until it has sent flight
N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes
after N+1. Go then drops all packets before the TimeoutAck, thus dropping one
transmit of flight N+1 without having to actually process the packets to
determine the end of the flight. The shim then sees the updated clock, calls
DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real.
When dropping packets, Go checks the epoch and increments sequence numbers so
that we can continue to be strict here. This requires tracking the initial
sequence number of the next epoch.
The final Finished message takes an additional special-case to test. DTLS
triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only
implements the former which should be sufficient (and is necessary) EXCEPT for
the final Finished message. If the peer's final Finished message is lost, it
won't be waiting for a message from us, so it won't time out anything. That
retransmit must be triggered on stale message, so we retransmit the Finished
message in Go.
Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c
Reviewed-on: https://boringssl-review.googlesource.com/3212
Reviewed-by: Adam Langley <agl@google.com>
2015-01-27 06:09:43 +00:00
|
|
|
hs.finishedBytes = finished.marshal()
|
|
|
|
hs.writeServerHash(hs.finishedBytes)
|
|
|
|
postCCSBytes := hs.finishedBytes
|
2014-07-21 21:14:03 +01:00
|
|
|
|
|
|
|
if c.config.Bugs.FragmentAcrossChangeCipherSpec {
|
|
|
|
c.writeRecord(recordTypeHandshake, postCCSBytes[:5])
|
|
|
|
postCCSBytes = postCCSBytes[5:]
|
2016-07-15 04:10:43 +01:00
|
|
|
} else if c.config.Bugs.SendUnencryptedFinished {
|
|
|
|
c.writeRecord(recordTypeHandshake, postCCSBytes)
|
|
|
|
postCCSBytes = nil
|
2014-07-21 21:14:03 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 22:39:58 +01:00
|
|
|
if !c.config.Bugs.SkipChangeCipherSpec {
|
2015-11-26 17:07:28 +00:00
|
|
|
ccs := []byte{1}
|
|
|
|
if c.config.Bugs.BadChangeCipherSpec != nil {
|
|
|
|
ccs = c.config.Bugs.BadChangeCipherSpec
|
|
|
|
}
|
|
|
|
c.writeRecord(recordTypeChangeCipherSpec, ccs)
|
2014-07-19 22:39:58 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-26 04:52:39 +00:00
|
|
|
if c.config.Bugs.AppDataAfterChangeCipherSpec != nil {
|
|
|
|
c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec)
|
|
|
|
}
|
2015-03-12 19:09:02 +00:00
|
|
|
if c.config.Bugs.AlertAfterChangeCipherSpec != 0 {
|
|
|
|
c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec)
|
|
|
|
return errors.New("tls: simulating post-CCS alert")
|
|
|
|
}
|
2015-01-26 04:52:39 +00:00
|
|
|
|
2016-07-15 04:10:43 +01:00
|
|
|
if !c.config.Bugs.SkipFinished && len(postCCSBytes) > 0 {
|
2015-02-08 23:30:14 +00:00
|
|
|
c.writeRecord(recordTypeHandshake, postCCSBytes)
|
2016-07-27 22:40:37 +01:00
|
|
|
if c.config.Bugs.SendExtraFinished {
|
|
|
|
c.writeRecord(recordTypeHandshake, finished.marshal())
|
|
|
|
}
|
2017-06-26 03:42:55 +01:00
|
|
|
}
|
2016-07-27 22:40:37 +01:00
|
|
|
|
2017-10-12 22:14:45 +01:00
|
|
|
if isResume || (!c.config.Bugs.PackHelloRequestWithFinished && !c.config.Bugs.PackAppDataWithHandshake) {
|
|
|
|
// Defer flushing until Renegotiate() or Write().
|
2017-06-26 03:42:55 +01:00
|
|
|
c.flushHandshake()
|
2015-01-31 22:16:01 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-04-03 09:06:36 +01:00
|
|
|
c.cipherSuite = hs.suite
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processCertsFromClient takes a chain of client certificates either from a
|
|
|
|
// Certificates message or from a sessionState and verifies them. It returns
|
|
|
|
// the public key of the leaf certificate.
|
|
|
|
func (hs *serverHandshakeState) processCertsFromClient(certificates [][]byte) (crypto.PublicKey, error) {
|
|
|
|
c := hs.c
|
|
|
|
|
|
|
|
hs.certsFromClient = certificates
|
|
|
|
certs := make([]*x509.Certificate, len(certificates))
|
|
|
|
var err error
|
|
|
|
for i, asn1Data := range certificates {
|
|
|
|
if certs[i], err = x509.ParseCertificate(asn1Data); err != nil {
|
|
|
|
c.sendAlert(alertBadCertificate)
|
|
|
|
return nil, errors.New("tls: failed to parse client certificate: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.config.ClientAuth >= VerifyClientCertIfGiven && len(certs) > 0 {
|
|
|
|
opts := x509.VerifyOptions{
|
|
|
|
Roots: c.config.ClientCAs,
|
|
|
|
CurrentTime: c.config.time(),
|
|
|
|
Intermediates: x509.NewCertPool(),
|
|
|
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cert := range certs[1:] {
|
|
|
|
opts.Intermediates.AddCert(cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
chains, err := certs[0].Verify(opts)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertBadCertificate)
|
|
|
|
return nil, errors.New("tls: failed to verify client's certificate: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := false
|
|
|
|
for _, ku := range certs[0].ExtKeyUsage {
|
|
|
|
if ku == x509.ExtKeyUsageClientAuth {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return nil, errors.New("tls: client's certificate's extended key usage doesn't permit it to be used for client authentication")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.verifiedChains = chains
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(certs) > 0 {
|
2017-03-29 00:28:44 +01:00
|
|
|
pub := getCertificatePublicKey(certs[0])
|
|
|
|
switch pub.(type) {
|
|
|
|
case *ecdsa.PublicKey, *rsa.PublicKey, ed25519.PublicKey:
|
|
|
|
break
|
2014-06-20 20:00:00 +01:00
|
|
|
default:
|
|
|
|
c.sendAlert(alertUnsupportedCertificate)
|
2017-03-29 00:28:44 +01:00
|
|
|
return nil, fmt.Errorf("tls: client's certificate contains an unsupported public key of type %T", pub)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
c.peerCertificates = certs
|
|
|
|
return pub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-09-24 00:25:11 +01:00
|
|
|
func verifyChannelIDMessage(channelIDMsg *channelIDMsg, channelIDHash []byte) (*ecdsa.PublicKey, error) {
|
|
|
|
x := new(big.Int).SetBytes(channelIDMsg.channelID[0:32])
|
|
|
|
y := new(big.Int).SetBytes(channelIDMsg.channelID[32:64])
|
|
|
|
r := new(big.Int).SetBytes(channelIDMsg.channelID[64:96])
|
|
|
|
s := new(big.Int).SetBytes(channelIDMsg.channelID[96:128])
|
|
|
|
if !elliptic.P256().IsOnCurve(x, y) {
|
|
|
|
return nil, errors.New("tls: invalid channel ID public key")
|
|
|
|
}
|
2017-09-06 19:12:07 +01:00
|
|
|
channelID := &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
|
2016-09-24 00:25:11 +01:00
|
|
|
if !ecdsa.Verify(channelID, channelIDHash, r, s) {
|
|
|
|
return nil, errors.New("tls: invalid channel ID signature")
|
|
|
|
}
|
|
|
|
return channelID, nil
|
|
|
|
}
|
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
func (hs *serverHandshakeState) writeServerHash(msg []byte) {
|
|
|
|
// writeServerHash is called before writeRecord.
|
|
|
|
hs.writeHash(msg, hs.c.sendHandshakeSeq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *serverHandshakeState) writeClientHash(msg []byte) {
|
|
|
|
// writeClientHash is called after readHandshake.
|
|
|
|
hs.writeHash(msg, hs.c.recvHandshakeSeq-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *serverHandshakeState) writeHash(msg []byte, seqno uint16) {
|
|
|
|
if hs.c.isDTLS {
|
|
|
|
// This is somewhat hacky. DTLS hashes a slightly different format.
|
|
|
|
// First, the TLS header.
|
|
|
|
hs.finishedHash.Write(msg[:4])
|
|
|
|
// Then the sequence number and reassembled fragment offset (always 0).
|
|
|
|
hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0})
|
|
|
|
// Then the reassembled fragment (always equal to the message length).
|
|
|
|
hs.finishedHash.Write(msg[1:4])
|
|
|
|
// And then the message body.
|
|
|
|
hs.finishedHash.Write(msg[4:])
|
|
|
|
} else {
|
|
|
|
hs.finishedHash.Write(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
// tryCipherSuite returns a cipherSuite with the given id if that cipher suite
|
|
|
|
// is acceptable to use.
|
2016-09-06 19:13:43 +01:00
|
|
|
func (c *Conn) tryCipherSuite(id uint16, supportedCipherSuites []uint16, version uint16, ellipticOk, ecdsaOk bool) *cipherSuite {
|
2014-06-20 20:00:00 +01:00
|
|
|
for _, supported := range supportedCipherSuites {
|
|
|
|
if id == supported {
|
|
|
|
var candidate *cipherSuite
|
|
|
|
|
|
|
|
for _, s := range cipherSuites {
|
|
|
|
if s.id == id {
|
|
|
|
candidate = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if candidate == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-09-06 19:13:43 +01:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
// Don't select a ciphersuite which we can't
|
|
|
|
// support for this client.
|
2016-09-06 19:13:43 +01:00
|
|
|
if version >= VersionTLS13 || candidate.flags&suiteTLS13 != 0 {
|
|
|
|
if version < VersionTLS13 || candidate.flags&suiteTLS13 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return candidate
|
2016-10-04 22:51:35 +01:00
|
|
|
}
|
|
|
|
if (candidate.flags&suiteECDHE != 0) && !ellipticOk {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (candidate.flags&suiteECDSA != 0) != ecdsaOk {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if version < VersionTLS12 && candidate.flags&suiteTLS12 != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
return candidate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-05 23:23:20 +00:00
|
|
|
|
|
|
|
func isTLS12Cipher(id uint16) bool {
|
|
|
|
for _, cipher := range cipherSuites {
|
|
|
|
if cipher.id != id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return cipher.flags&suiteTLS12 != 0
|
|
|
|
}
|
|
|
|
// Unknown cipher.
|
|
|
|
return false
|
|
|
|
}
|
2016-09-03 02:35:25 +01:00
|
|
|
|
|
|
|
func isGREASEValue(val uint16) bool {
|
2016-09-26 23:30:05 +01:00
|
|
|
return val&0x0f0f == 0x0a0a && val&0xff == val>>8
|
2016-09-03 02:35:25 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
func verifyPSKBinder(version uint16, clientHello *clientHelloMsg, sessionState *sessionState, binderToVerify, firstClientHello, helloRetryRequest []byte) error {
|
2016-11-01 17:39:36 +00:00
|
|
|
binderLen := 2
|
|
|
|
for _, binder := range clientHello.pskBinders {
|
|
|
|
binderLen += 1 + len(binder)
|
|
|
|
}
|
|
|
|
|
|
|
|
truncatedHello := clientHello.marshal()
|
|
|
|
truncatedHello = truncatedHello[:len(truncatedHello)-binderLen]
|
|
|
|
pskCipherSuite := cipherSuiteFromID(sessionState.cipherSuite)
|
|
|
|
if pskCipherSuite == nil {
|
|
|
|
return errors.New("tls: Unknown cipher suite for PSK in session")
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:20:31 +00:00
|
|
|
binder := computePSKBinder(sessionState.masterSecret, version, resumptionPSKBinderLabel, pskCipherSuite, firstClientHello, helloRetryRequest, truncatedHello)
|
2016-11-01 17:39:36 +00:00
|
|
|
if !bytes.Equal(binder, binderToVerify) {
|
|
|
|
return errors.New("tls: PSK binder does not verify")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|