2009-11-05 23:44:32 +00: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 tls
|
|
|
|
|
|
|
|
// The handshake goroutine reads handshake messages from the record processor
|
|
|
|
// and outputs messages to be written on another channel. It updates the record
|
|
|
|
// processor with the state of the connection via the control channel. In the
|
|
|
|
// case of handshake messages that need synchronous processing (because they
|
|
|
|
// affect the handling of the next record) the record processor knows about
|
|
|
|
// them and either waits for a control message (Finished) or includes a reply
|
|
|
|
// channel in the message (ChangeCipherSpec).
|
|
|
|
|
|
|
|
import (
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rc4"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/subtle"
|
|
|
|
"io"
|
2009-11-05 23:44:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cipherSuite struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
id uint16 // The number of this suite on the wire.
|
|
|
|
hashLength, cipherKeyLength int
|
2009-11-05 23:44:32 +00:00
|
|
|
// TODO(agl): need a method to create the cipher and hash interfaces.
|
|
|
|
}
|
|
|
|
|
|
|
|
var cipherSuites = []cipherSuite{
|
|
|
|
cipherSuite{TLS_RSA_WITH_RC4_128_SHA, 20, 16},
|
|
|
|
}
|
|
|
|
|
|
|
|
// A serverHandshake performs the server side of the TLS 1.1 handshake protocol.
|
|
|
|
type serverHandshake struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan chan<- interface{}
|
|
|
|
controlChan chan<- interface{}
|
|
|
|
msgChan <-chan interface{}
|
|
|
|
config *Config
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *serverHandshake) loop(writeChan chan<- interface{}, controlChan chan<- interface{}, msgChan <-chan interface{}, config *Config) {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.writeChan = writeChan
|
|
|
|
h.controlChan = controlChan
|
|
|
|
h.msgChan = msgChan
|
|
|
|
h.config = config
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
defer close(writeChan)
|
|
|
|
defer close(controlChan)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
clientHello, ok := h.readHandshakeMsg().(*clientHelloMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
major, minor, ok := mutualVersion(clientHello.major, clientHello.minor)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertProtocolVersion)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash := newFinishedHash()
|
|
|
|
finishedHash.Write(clientHello.marshal())
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
hello := new(serverHelloMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
// We only support a single ciphersuite so we look for it in the list
|
|
|
|
// of client supported suites.
|
|
|
|
//
|
|
|
|
// TODO(agl): Add additional cipher suites.
|
2009-12-15 23:33:31 +00:00
|
|
|
var suite *cipherSuite
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
for _, id := range clientHello.cipherSuites {
|
|
|
|
for _, supported := range cipherSuites {
|
|
|
|
if supported.id == id {
|
2009-12-15 23:33:31 +00:00
|
|
|
suite = &supported
|
|
|
|
break
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
foundCompression := false
|
2009-11-05 23:44:32 +00:00
|
|
|
// We only support null compression, so check that the client offered it.
|
|
|
|
for _, compression := range clientHello.compressionMethods {
|
|
|
|
if compression == compressionNone {
|
2009-12-15 23:33:31 +00:00
|
|
|
foundCompression = true
|
|
|
|
break
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if suite == nil || !foundCompression {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertHandshakeFailure)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hello.major = major
|
|
|
|
hello.minor = minor
|
|
|
|
hello.cipherSuite = suite.id
|
|
|
|
currentTime := uint32(config.Time())
|
|
|
|
hello.random = make([]byte, 32)
|
|
|
|
hello.random[0] = byte(currentTime >> 24)
|
|
|
|
hello.random[1] = byte(currentTime >> 16)
|
|
|
|
hello.random[2] = byte(currentTime >> 8)
|
|
|
|
hello.random[3] = byte(currentTime)
|
|
|
|
_, err := io.ReadFull(config.Rand, hello.random[4:])
|
2009-11-05 23:44:32 +00:00
|
|
|
if err != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertInternalError)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
hello.compressionMethod = compressionNone
|
2009-12-23 19:13:09 +00:00
|
|
|
if clientHello.nextProtoNeg {
|
|
|
|
hello.nextProtoNeg = true
|
|
|
|
hello.nextProtos = config.NextProtos
|
|
|
|
}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(hello.marshal())
|
|
|
|
writeChan <- writerSetVersion{major, minor}
|
|
|
|
writeChan <- hello
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
if len(config.Certificates) == 0 {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertInternalError)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
certMsg := new(certificateMsg)
|
|
|
|
certMsg.certificates = config.Certificates[0].Certificate
|
|
|
|
finishedHash.Write(certMsg.marshal())
|
|
|
|
writeChan <- certMsg
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
helloDone := new(serverHelloDoneMsg)
|
|
|
|
finishedHash.Write(helloDone.marshal())
|
|
|
|
writeChan <- helloDone
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
ckx, ok := h.readHandshakeMsg().(*clientKeyExchangeMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(ckx.marshal())
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
preMasterSecret := make([]byte, 48)
|
|
|
|
_, err = io.ReadFull(config.Rand, preMasterSecret[2:])
|
2009-11-05 23:44:32 +00:00
|
|
|
if err != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertInternalError)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
err = rsa.DecryptPKCS1v15SessionKey(config.Rand, config.Certificates[0].PrivateKey, ckx.ciphertext, preMasterSecret)
|
2009-11-05 23:44:32 +00:00
|
|
|
if err != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertHandshakeFailure)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
// We don't check the version number in the premaster secret. For one,
|
|
|
|
// by checking it, we would leak information about the validity of the
|
|
|
|
// encrypted pre-master secret. Secondly, it provides only a small
|
|
|
|
// benefit against a downgrade attack and some implementations send the
|
|
|
|
// wrong version anyway. See the discussion at the end of section
|
|
|
|
// 7.4.7.1 of RFC 4346.
|
|
|
|
|
|
|
|
masterSecret, clientMAC, serverMAC, clientKey, serverKey :=
|
2009-12-15 23:33:31 +00:00
|
|
|
keysFromPreMasterSecret11(preMasterSecret, clientHello.random, hello.random, suite.hashLength, suite.cipherKeyLength)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
_, ok = h.readHandshakeMsg().(changeCipherSpec)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
cipher, _ := rc4.NewCipher(clientKey)
|
|
|
|
controlChan <- &newCipherSpec{cipher, hmac.New(sha1.New(), clientMAC)}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-23 19:13:09 +00:00
|
|
|
clientProtocol := ""
|
|
|
|
if hello.nextProtoNeg {
|
|
|
|
nextProto, ok := h.readHandshakeMsg().(*nextProtoMsg)
|
|
|
|
if !ok {
|
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
finishedHash.Write(nextProto.marshal())
|
|
|
|
clientProtocol = nextProto.proto
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
clientFinished, ok := h.readHandshakeMsg().(*finishedMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
verify := finishedHash.clientSum(masterSecret)
|
2009-11-05 23:44:32 +00:00
|
|
|
if len(verify) != len(clientFinished.verifyData) ||
|
|
|
|
subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertHandshakeFailure)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-23 19:13:09 +00:00
|
|
|
controlChan <- ConnectionState{true, "TLS_RSA_WITH_RC4_128_SHA", 0, clientProtocol}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(clientFinished.marshal())
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
cipher2, _ := rc4.NewCipher(serverKey)
|
|
|
|
writeChan <- writerChangeCipherSpec{cipher2, hmac.New(sha1.New(), serverMAC)}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finished := new(finishedMsg)
|
|
|
|
finished.verifyData = finishedHash.serverSum(masterSecret)
|
|
|
|
writeChan <- finished
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan <- writerEnableApplicationData{}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
for {
|
2009-12-15 23:33:31 +00:00
|
|
|
_, ok := h.readHandshakeMsg().(*clientHelloMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
// We reject all renegotication requests.
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan <- alert{alertLevelWarning, alertNoRenegotiation}
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *serverHandshake) readHandshakeMsg() interface{} {
|
2009-12-15 23:33:31 +00:00
|
|
|
v := <-h.msgChan
|
2009-11-05 23:44:32 +00:00
|
|
|
if closed(h.msgChan) {
|
|
|
|
// If the channel closed then the processor received an error
|
|
|
|
// from the peer and we don't want to echo it back to them.
|
2009-12-15 23:33:31 +00:00
|
|
|
h.msgChan = nil
|
|
|
|
return 0
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
if _, ok := v.(alert); ok {
|
|
|
|
// We got an alert from the processor. We forward to the writer
|
|
|
|
// and shutdown.
|
2009-12-15 23:33:31 +00:00
|
|
|
h.writeChan <- v
|
|
|
|
h.msgChan = nil
|
|
|
|
return 0
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
return v
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *serverHandshake) error(e alertType) {
|
|
|
|
if h.msgChan != nil {
|
|
|
|
// If we didn't get an error from the processor, then we need
|
|
|
|
// to tell it about the error.
|
2009-11-06 00:59:39 +00:00
|
|
|
go func() {
|
|
|
|
for _ = range h.msgChan {
|
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
}()
|
2009-12-23 19:13:09 +00:00
|
|
|
h.controlChan <- ConnectionState{false, "", e, ""}
|
2009-12-15 23:33:31 +00:00
|
|
|
close(h.controlChan)
|
|
|
|
h.writeChan <- alert{alertLevelError, e}
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|