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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
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"
|
|
|
|
"encoding/asn1"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-08-24 06:44:23 +01:00
|
|
|
"math/big"
|
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,
|
|
|
|
}
|
|
|
|
isResume, err := hs.readClientHello()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2014-08-08 00:13:38 +01:00
|
|
|
if c.config.Bugs.RenewTicketOnResume {
|
|
|
|
if err := hs.sendSessionTicket(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
if err := hs.sendFinished(); err != nil {
|
|
|
|
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
|
|
|
// Most retransmits are triggered by a timeout, but the final
|
|
|
|
// leg of the handshake is retransmited upon re-receiving a
|
|
|
|
// Finished.
|
2015-01-31 22:16:01 +00:00
|
|
|
if err := c.simulatePacketLoss(func() {
|
|
|
|
c.writeRecord(recordTypeHandshake, hs.finishedBytes)
|
|
|
|
c.dtlsFlushHandshake(false)
|
|
|
|
}); err != nil {
|
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
|
|
|
return err
|
|
|
|
}
|
2014-08-24 06:44:23 +01:00
|
|
|
if err := hs.readFinished(isResume); err != nil {
|
2014-06-20 20:00:00 +01:00
|
|
|
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
|
|
|
|
}
|
2014-08-24 06:44:23 +01:00
|
|
|
if err := hs.readFinished(isResume); err != nil {
|
2014-06-20 20:00:00 +01:00
|
|
|
return err
|
|
|
|
}
|
2014-08-24 08:47:07 +01:00
|
|
|
if c.config.Bugs.ExpectFalseStart {
|
|
|
|
if err := c.readRecord(recordTypeApplicationData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
if err := hs.sendSessionTicket(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hs.sendFinished(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.handshakeComplete = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readClientHello reads a ClientHello message from the client and decides
|
|
|
|
// whether we will perform session resumption.
|
|
|
|
func (hs *serverHandshakeState) readClientHello() (isResume bool, err error) {
|
|
|
|
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 {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
hs.clientHello, ok = msg.(*clientHelloMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return false, unexpectedMessageError(hs.clientHello, msg)
|
|
|
|
}
|
2014-11-22 06:47:56 +00:00
|
|
|
if config.Bugs.RequireFastradioPadding && len(hs.clientHello.raw) < 1000 {
|
|
|
|
return false, errors.New("tls: ClientHello record size should be larger than 1000 bytes when padding enabled.")
|
|
|
|
}
|
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{
|
2014-08-16 17:07:27 +01:00
|
|
|
vers: VersionTLS10,
|
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)
|
|
|
|
return false, errors.New("dtls: short read from Rand: " + err.Error())
|
|
|
|
}
|
|
|
|
c.writeRecord(recordTypeHandshake, helloVerifyRequest.marshal())
|
2015-01-31 22:16:01 +00:00
|
|
|
if err := c.dtlsFlushHandshake(true); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
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 {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
newClientHello, ok := msg.(*clientHelloMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return false, unexpectedMessageError(hs.clientHello, msg)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(newClientHello.cookie, helloVerifyRequest.cookie) {
|
|
|
|
return false, errors.New("dtls: invalid cookie")
|
|
|
|
}
|
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) {
|
2014-08-04 06:23:53 +01:00
|
|
|
return false, errors.New("dtls: retransmitted ClientHello does not match")
|
|
|
|
}
|
|
|
|
hs.clientHello = newClientHello
|
|
|
|
}
|
|
|
|
|
2014-11-23 17:11:01 +00:00
|
|
|
if config.Bugs.RequireSameRenegoClientVersion && c.clientVersion != 0 {
|
|
|
|
if c.clientVersion != hs.clientHello.vers {
|
|
|
|
return false, fmt.Errorf("tls: client offered different version on renego")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.clientVersion = hs.clientHello.vers
|
|
|
|
|
2015-01-26 15:22:13 +00:00
|
|
|
// Reject < 1.2 ClientHellos with signature_algorithms.
|
|
|
|
if c.clientVersion < VersionTLS12 && len(hs.clientHello.signatureAndHashes) > 0 {
|
|
|
|
return false, fmt.Errorf("tls: client included signature_algorithms before TLS 1.2")
|
|
|
|
}
|
|
|
|
|
2014-08-16 17:07:27 +01:00
|
|
|
c.vers, ok = config.mutualVersion(hs.clientHello.vers)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertProtocolVersion)
|
|
|
|
return false, fmt.Errorf("tls: client offered an unsupported, maximum protocol version of %x", hs.clientHello.vers)
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
c.haveVers = true
|
|
|
|
|
|
|
|
hs.hello = new(serverHelloMsg)
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.hello.isDTLS = c.isDTLS
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
supportedCurve := false
|
|
|
|
preferredCurves := config.curvePreferences()
|
|
|
|
Curves:
|
|
|
|
for _, curve := range hs.clientHello.supportedCurves {
|
|
|
|
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
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.hello.vers = c.vers
|
|
|
|
hs.hello.random = make([]byte, 32)
|
|
|
|
_, err = io.ReadFull(config.rand(), hs.hello.random)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return false, err
|
|
|
|
}
|
2014-10-29 00:29:33 +00:00
|
|
|
|
2014-10-29 02:06:14 +00:00
|
|
|
if !bytes.Equal(c.clientVerify, hs.clientHello.secureRenegotiation) {
|
2014-10-29 00:29:33 +00:00
|
|
|
c.sendAlert(alertHandshakeFailure)
|
2014-10-29 02:06:14 +00:00
|
|
|
return false, errors.New("tls: renegotiation mismatch")
|
2014-10-29 00:29:33 +00:00
|
|
|
}
|
2014-10-29 02:06:14 +00:00
|
|
|
|
|
|
|
if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo {
|
|
|
|
hs.hello.secureRenegotiation = append(hs.hello.secureRenegotiation, c.clientVerify...)
|
|
|
|
hs.hello.secureRenegotiation = append(hs.hello.secureRenegotiation, c.serverVerify...)
|
|
|
|
if c.config.Bugs.BadRenegotiationInfo {
|
|
|
|
hs.hello.secureRenegotiation[0] ^= 0x80
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hs.hello.secureRenegotiation = hs.clientHello.secureRenegotiation
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
hs.hello.compressionMethod = compressionNone
|
2014-07-05 05:23:20 +01:00
|
|
|
hs.hello.duplicateExtension = c.config.Bugs.DuplicateExtension
|
2014-06-20 20:00:00 +01:00
|
|
|
if len(hs.clientHello.serverName) > 0 {
|
|
|
|
c.serverName = hs.clientHello.serverName
|
|
|
|
}
|
2014-09-15 21:51:51 +01:00
|
|
|
|
|
|
|
if len(hs.clientHello.alpnProtocols) > 0 {
|
|
|
|
if selectedProto, fallback := mutualProtocol(hs.clientHello.alpnProtocols, c.config.NextProtos); !fallback {
|
|
|
|
hs.hello.alpnProtocol = selectedProto
|
|
|
|
c.clientProtocol = selectedProto
|
2014-09-06 18:21:53 +01:00
|
|
|
c.usedALPN = true
|
2014-09-15 21:51:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 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 {
|
|
|
|
hs.hello.nextProtoNeg = true
|
|
|
|
hs.hello.nextProtos = config.NextProtos
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
hs.hello.extendedMasterSecret = c.vers >= VersionTLS10 && hs.clientHello.extendedMasterSecret && !c.config.Bugs.NoExtendedMasterSecret
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if len(config.Certificates) == 0 {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return false, errors.New("tls: no certificates configured")
|
|
|
|
}
|
|
|
|
hs.cert = &config.Certificates[0]
|
|
|
|
if len(hs.clientHello.serverName) > 0 {
|
|
|
|
hs.cert = config.getCertificateForName(hs.clientHello.serverName)
|
|
|
|
}
|
2014-09-06 17:45:15 +01:00
|
|
|
if expected := c.config.Bugs.ExpectServerName; expected != "" && expected != hs.clientHello.serverName {
|
|
|
|
return false, errors.New("tls: unexpected server name")
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-08-24 06:44:23 +01:00
|
|
|
if hs.clientHello.channelIDSupported && config.RequestChannelID {
|
|
|
|
hs.hello.channelIDRequested = true
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
hs.hello.srtpProtectionProfile = p1
|
|
|
|
c.srtpProtectionProfile = p1
|
|
|
|
break SRTPLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.config.Bugs.SendSRTPProtectionProfile != 0 {
|
|
|
|
hs.hello.srtpProtectionProfile = c.config.Bugs.SendSRTPProtectionProfile
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
_, hs.ecdsaOk = hs.cert.PrivateKey.(*ecdsa.PrivateKey)
|
|
|
|
|
|
|
|
if hs.checkForResumption() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:03:11 +01:00
|
|
|
var scsvFound bool
|
|
|
|
|
|
|
|
for _, cipherSuite := range hs.clientHello.cipherSuites {
|
|
|
|
if cipherSuite == fallbackSCSV {
|
|
|
|
scsvFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !scsvFound && config.Bugs.FailIfNotFallbackSCSV {
|
|
|
|
return false, errors.New("tls: no fallback SCSV found when expected")
|
|
|
|
} else if scsvFound && !config.Bugs.FailIfNotFallbackSCSV {
|
|
|
|
return false, errors.New("tls: fallback SCSV found when not expected")
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
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 {
|
|
|
|
if hs.suite = c.tryCipherSuite(id, supportedList, c.vers, hs.ellipticOk, hs.ecdsaOk); hs.suite != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkForResumption returns true if we should perform resumption on this connection.
|
|
|
|
func (hs *serverHandshakeState) checkForResumption() bool {
|
|
|
|
c := hs.c
|
|
|
|
|
2014-11-17 08:19:02 +00:00
|
|
|
if len(hs.clientHello.sessionTicket) > 0 {
|
|
|
|
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
|
|
|
|
if hs.sessionState, ok = c.decryptTicket(hs.clientHello.sessionTicket); !ok {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-10 07:37:15 +00:00
|
|
|
// Never resume a session for a different SSL version.
|
|
|
|
if !c.config.Bugs.AllowSessionVersionMismatch && c.vers != hs.sessionState.vers {
|
|
|
|
return false
|
2014-06-20 20:00:00 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we also support the ciphersuite from the session.
|
|
|
|
hs.suite = c.tryCipherSuite(hs.sessionState.cipherSuite, c.config.cipherSuites(), hs.sessionState.vers, hs.ellipticOk, hs.ecdsaOk)
|
|
|
|
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
|
|
|
|
// 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
|
2014-08-08 00:13:38 +01:00
|
|
|
hs.hello.ticketSupported = c.config.Bugs.RenewTicketOnResume
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
hs.finishedHash = newFinishedHash(c.vers, 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 {
|
2014-06-20 20:00:00 +01:00
|
|
|
hs.hello.ocspStapling = true
|
|
|
|
}
|
|
|
|
|
2014-11-25 06:55:35 +00:00
|
|
|
if hs.clientHello.sctListSupported && len(hs.cert.SignedCertificateTimestampList) > 0 {
|
|
|
|
hs.hello.sctList = hs.cert.SignedCertificateTimestampList
|
|
|
|
}
|
|
|
|
|
2014-11-17 08:19:02 +00:00
|
|
|
hs.hello.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
|
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
c.extendedMasterSecret = hs.hello.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.
|
|
|
|
if !hs.hello.ticketSupported && config.ServerSessionCache != nil {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
hs.finishedHash = newFinishedHash(c.vers, 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
|
|
|
|
|
|
|
c.writeRecord(recordTypeHandshake, hs.hello.marshal())
|
|
|
|
|
2014-10-27 05:06:24 +00:00
|
|
|
if !isPSK {
|
|
|
|
certMsg := new(certificateMsg)
|
|
|
|
certMsg.certificates = hs.cert.Certificate
|
|
|
|
if !config.Bugs.UnauthenticatedECDH {
|
|
|
|
hs.writeServerHash(certMsg.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, certMsg.marshal())
|
|
|
|
}
|
2014-07-12 05:48:23 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if hs.hello.ocspStapling {
|
|
|
|
certStatus := new(certificateStatusMsg)
|
|
|
|
certStatus.statusType = statusTypeOCSP
|
|
|
|
certStatus.response = hs.cert.OCSPStaple
|
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)
|
|
|
|
skx, err := keyAgreement.generateServerKeyExchange(config, hs.cert, hs.clientHello, hs.hello)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return err
|
|
|
|
}
|
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{
|
|
|
|
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 {
|
|
|
|
certReq.hasSignatureAndHash = true
|
2014-11-14 06:43:59 +00:00
|
|
|
if !config.Bugs.NoSignatureAndHashes {
|
|
|
|
certReq.signatureAndHashes = config.signatureAndHashesForServer()
|
|
|
|
}
|
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())
|
2015-01-31 22:16:01 +00:00
|
|
|
if err := c.dtlsFlushHandshake(true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
2014-06-20 20:00:00 +01:00
|
|
|
if certMsg, ok = msg.(*certificateMsg); !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(certMsg, msg)
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
hs.writeClientHash(certMsg.marshal())
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if len(certMsg.certificates) == 0 {
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub, err = hs.processCertsFromClient(certMsg.certificates)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
var signatureAndHash signatureAndHash
|
|
|
|
if certVerify.hasSignatureAndHash {
|
|
|
|
signatureAndHash = certVerify.signatureAndHash
|
2014-11-14 06:43:59 +00:00
|
|
|
if !isSupportedSignatureAndHash(signatureAndHash, config.signatureAndHashesForServer()) {
|
|
|
|
return errors.New("tls: unsupported hash function for client certificate")
|
|
|
|
}
|
2014-07-18 20:03:41 +01:00
|
|
|
} else {
|
|
|
|
// Before TLS 1.2 the signature algorithm was implicit
|
|
|
|
// from the key type, and only one hash per signature
|
|
|
|
// algorithm was possible. Leave the hash as zero.
|
|
|
|
switch pub.(type) {
|
|
|
|
case *ecdsa.PublicKey:
|
|
|
|
signatureAndHash.signature = signatureECDSA
|
|
|
|
case *rsa.PublicKey:
|
|
|
|
signatureAndHash.signature = signatureRSA
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
switch key := pub.(type) {
|
|
|
|
case *ecdsa.PublicKey:
|
2014-07-18 20:03:41 +01:00
|
|
|
if signatureAndHash.signature != signatureECDSA {
|
|
|
|
err = errors.New("tls: bad signature type for client's ECDSA certificate")
|
|
|
|
break
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
ecdsaSig := new(ecdsaSignature)
|
|
|
|
if _, err = asn1.Unmarshal(certVerify.signature, ecdsaSig); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
|
|
|
|
err = errors.New("ECDSA signature contained zero or negative values")
|
|
|
|
break
|
|
|
|
}
|
2014-07-18 20:03:41 +01:00
|
|
|
var digest []byte
|
2014-08-28 04:13:20 +01:00
|
|
|
digest, _, err = hs.finishedHash.hashForClientCertificate(signatureAndHash, hs.masterSecret)
|
2014-07-18 20:03:41 +01:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
if !ecdsa.Verify(key, digest, ecdsaSig.R, ecdsaSig.S) {
|
|
|
|
err = errors.New("ECDSA verification failure")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case *rsa.PublicKey:
|
2014-07-18 20:03:41 +01:00
|
|
|
if signatureAndHash.signature != signatureRSA {
|
|
|
|
err = errors.New("tls: bad signature type for client's RSA certificate")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var digest []byte
|
|
|
|
var hashFunc crypto.Hash
|
2014-08-28 04:13:20 +01:00
|
|
|
digest, hashFunc, err = hs.finishedHash.hashForClientCertificate(signatureAndHash, hs.masterSecret)
|
2014-07-18 20:03:41 +01:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
err = rsa.VerifyPKCS1v15(key, hashFunc, digest, certVerify.signature)
|
|
|
|
}
|
|
|
|
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 :=
|
|
|
|
keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.clientHello.random, hs.hello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
clientCipher = hs.suite.aead(clientKey, clientIV)
|
|
|
|
serverCipher = hs.suite.aead(serverKey, serverIV)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.in.prepareCipherSpec(c.vers, clientCipher, clientHash)
|
|
|
|
c.out.prepareCipherSpec(c.vers, serverCipher, serverHash)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-24 06:44:23 +01:00
|
|
|
func (hs *serverHandshakeState) readFinished(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
|
|
|
|
}
|
|
|
|
|
|
|
|
if hs.hello.nextProtoNeg {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-24 06:44:23 +01:00
|
|
|
if hs.hello.channelIDRequested {
|
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encryptedExtensions, ok := msg.(*encryptedExtensionsMsg)
|
|
|
|
if !ok {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return unexpectedMessageError(encryptedExtensions, msg)
|
|
|
|
}
|
|
|
|
x := new(big.Int).SetBytes(encryptedExtensions.channelID[0:32])
|
|
|
|
y := new(big.Int).SetBytes(encryptedExtensions.channelID[32:64])
|
|
|
|
r := new(big.Int).SetBytes(encryptedExtensions.channelID[64:96])
|
|
|
|
s := new(big.Int).SetBytes(encryptedExtensions.channelID[96:128])
|
|
|
|
if !elliptic.P256().IsOnCurve(x, y) {
|
|
|
|
return errors.New("tls: invalid channel ID public key")
|
|
|
|
}
|
|
|
|
channelID := &ecdsa.PublicKey{elliptic.P256(), x, y}
|
|
|
|
var resumeHash []byte
|
|
|
|
if isResume {
|
|
|
|
resumeHash = hs.sessionState.handshakeHash
|
|
|
|
}
|
|
|
|
if !ecdsa.Verify(channelID, hs.finishedHash.hashForChannelID(resumeHash), r, s) {
|
|
|
|
return errors.New("tls: invalid channel ID signature")
|
|
|
|
}
|
|
|
|
c.channelID = channelID
|
|
|
|
|
|
|
|
hs.writeClientHash(encryptedExtensions.marshal())
|
|
|
|
}
|
|
|
|
|
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...)
|
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,
|
|
|
|
handshakeHash: hs.finishedHash.server.Sum(nil),
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2014-11-17 08:19:02 +00:00
|
|
|
|
|
|
|
if !hs.hello.ticketSupported || hs.c.config.Bugs.SkipNewSessionTicket {
|
|
|
|
if c.config.ServerSessionCache != nil && len(hs.hello.sessionId) != 0 {
|
|
|
|
c.config.ServerSessionCache.Put(string(hs.hello.sessionId), &state)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(newSessionTicketMsg)
|
|
|
|
|
|
|
|
var err error
|
2014-06-20 20:00:00 +01:00
|
|
|
m.ticket, err = c.encryptTicket(&state)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *serverHandshakeState) sendFinished() error {
|
|
|
|
c := hs.c
|
|
|
|
|
2014-07-21 21:14:03 +01:00
|
|
|
finished := new(finishedMsg)
|
|
|
|
finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret)
|
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:]
|
|
|
|
}
|
2015-01-31 22:16:01 +00:00
|
|
|
if err := c.dtlsFlushHandshake(true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-21 21:14:03 +01:00
|
|
|
|
2014-07-19 22:39:58 +01:00
|
|
|
if !c.config.Bugs.SkipChangeCipherSpec {
|
|
|
|
c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2014-07-21 21:14:03 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, postCCSBytes)
|
2015-01-31 22:16:01 +00:00
|
|
|
if err := c.dtlsFlushHandshake(false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
c.cipherSuite = hs.suite.id
|
|
|
|
|
|
|
|
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 {
|
|
|
|
var pub crypto.PublicKey
|
|
|
|
switch key := certs[0].PublicKey.(type) {
|
|
|
|
case *ecdsa.PublicKey, *rsa.PublicKey:
|
|
|
|
pub = key
|
|
|
|
default:
|
|
|
|
c.sendAlert(alertUnsupportedCertificate)
|
|
|
|
return nil, fmt.Errorf("tls: client's certificate contains an unsupported public key of type %T", certs[0].PublicKey)
|
|
|
|
}
|
|
|
|
c.peerCertificates = certs
|
|
|
|
return pub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, 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.
|
|
|
|
func (c *Conn) tryCipherSuite(id uint16, supportedCipherSuites []uint16, version uint16, ellipticOk, ecdsaOk bool) *cipherSuite {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
// Don't select a ciphersuite which we can't
|
|
|
|
// support for this client.
|
|
|
|
if (candidate.flags&suiteECDHE != 0) && !ellipticOk {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (candidate.flags&suiteECDSA != 0) != ecdsaOk {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-31 07:23:49 +01:00
|
|
|
if !c.config.Bugs.SkipCipherVersionCheck && version < VersionTLS12 && candidate.flags&suiteTLS12 != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
continue
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
if c.isDTLS && candidate.flags&suiteNoDTLS != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
return candidate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|