2009-11-21 23:53:03 +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
|
|
|
|
|
|
|
|
import (
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rc4"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/subtle"
|
|
|
|
"crypto/x509"
|
|
|
|
"io"
|
2009-11-21 23:53:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A serverHandshake performs the server side of the TLS 1.1 handshake protocol.
|
|
|
|
type clientHandshake struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan chan<- interface{}
|
|
|
|
controlChan chan<- interface{}
|
|
|
|
msgChan <-chan interface{}
|
|
|
|
config *Config
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *clientHandshake) 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-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
defer close(writeChan)
|
|
|
|
defer close(controlChan)
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash := newFinishedHash()
|
2009-11-21 23:53:03 +00:00
|
|
|
|
|
|
|
hello := &clientHelloMsg{
|
2010-03-02 21:46:51 +00:00
|
|
|
major: defaultMajor,
|
|
|
|
minor: defaultMinor,
|
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
2009-11-21 23:53:03 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2010-03-02 21:46:51 +00:00
|
|
|
random: make([]byte, 32),
|
2009-12-15 23:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentTime := uint32(config.Time())
|
|
|
|
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-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertInternalError)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(hello.marshal())
|
|
|
|
writeChan <- writerSetVersion{defaultMajor, defaultMinor}
|
|
|
|
writeChan <- hello
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
serverHello, ok := h.readHandshakeMsg().(*serverHelloMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(serverHello.marshal())
|
|
|
|
major, minor, ok := mutualVersion(serverHello.major, serverHello.minor)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertProtocolVersion)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan <- writerSetVersion{major, minor}
|
2009-11-21 23:53:03 +00:00
|
|
|
|
|
|
|
if serverHello.cipherSuite != TLS_RSA_WITH_RC4_128_SHA ||
|
|
|
|
serverHello.compressionMethod != compressionNone {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
certMsg, ok := h.readHandshakeMsg().(*certificateMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok || len(certMsg.certificates) == 0 {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(certMsg.marshal())
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
certs := make([]*x509.Certificate, len(certMsg.certificates))
|
2009-11-21 23:53:03 +00:00
|
|
|
for i, asn1Data := range certMsg.certificates {
|
2009-12-15 23:33:31 +00:00
|
|
|
cert, err := x509.ParseCertificate(asn1Data)
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertBadCertificate)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
certs[i] = cert
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(agl): do better validation of certs: max path length, name restrictions etc.
|
|
|
|
for i := 1; i < len(certs); i++ {
|
|
|
|
if certs[i-1].CheckSignatureFrom(certs[i]) != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertBadCertificate)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.RootCAs != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
root := config.RootCAs.FindParent(certs[len(certs)-1])
|
2009-11-21 23:53:03 +00:00
|
|
|
if root == nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertBadCertificate)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
if certs[len(certs)-1].CheckSignatureFrom(root) != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertBadCertificate)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
pub, ok := certs[0].PublicKey.(*rsa.PublicKey)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnsupportedCertificate)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
shd, ok := h.readHandshakeMsg().(*serverHelloDoneMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(shd.marshal())
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
ckx := new(clientKeyExchangeMsg)
|
|
|
|
preMasterSecret := make([]byte, 48)
|
2009-11-21 23:53:03 +00:00
|
|
|
// Note that the version number in the preMasterSecret must be the
|
|
|
|
// version offered in the ClientHello.
|
2009-12-15 23:33:31 +00:00
|
|
|
preMasterSecret[0] = defaultMajor
|
|
|
|
preMasterSecret[1] = defaultMinor
|
|
|
|
_, err = io.ReadFull(config.Rand, preMasterSecret[2:])
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertInternalError)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
ckx.ciphertext, err = rsa.EncryptPKCS1v15(config.Rand, pub, preMasterSecret)
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertInternalError)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(ckx.marshal())
|
|
|
|
writeChan <- ckx
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
suite := cipherSuites[0]
|
2009-11-21 23:53:03 +00:00
|
|
|
masterSecret, clientMAC, serverMAC, clientKey, serverKey :=
|
2009-12-15 23:33:31 +00:00
|
|
|
keysFromPreMasterSecret11(preMasterSecret, hello.random, serverHello.random, suite.hashLength, suite.cipherKeyLength)
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
cipher, _ := rc4.NewCipher(clientKey)
|
|
|
|
writeChan <- writerChangeCipherSpec{cipher, hmac.New(sha1.New(), clientMAC)}
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finished := new(finishedMsg)
|
|
|
|
finished.verifyData = finishedHash.clientSum(masterSecret)
|
|
|
|
finishedHash.Write(finished.marshal())
|
|
|
|
writeChan <- finished
|
2009-11-21 23:53:03 +00:00
|
|
|
|
|
|
|
// TODO(agl): this is cut-through mode which should probably be an option.
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan <- writerEnableApplicationData{}
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
_, ok = h.readHandshakeMsg().(changeCipherSpec)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
cipher2, _ := rc4.NewCipher(serverKey)
|
|
|
|
controlChan <- &newCipherSpec{cipher2, hmac.New(sha1.New(), serverMAC)}
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
serverFinished, ok := h.readHandshakeMsg().(*finishedMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
verify := finishedHash.serverSum(masterSecret)
|
2009-11-21 23:53:03 +00:00
|
|
|
if len(verify) != len(serverFinished.verifyData) ||
|
|
|
|
subtle.ConstantTimeCompare(verify, serverFinished.verifyData) != 1 {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.error(alertHandshakeFailure)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-23 19:13:09 +00:00
|
|
|
controlChan <- ConnectionState{HandshakeComplete: true, CipherSuite: "TLS_RSA_WITH_RC4_128_SHA"}
|
2009-11-21 23:53:03 +00:00
|
|
|
|
|
|
|
// This should just block forever.
|
2009-12-15 23:33:31 +00:00
|
|
|
_ = h.readHandshakeMsg()
|
|
|
|
h.error(alertUnexpectedMessage)
|
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *clientHandshake) readHandshakeMsg() interface{} {
|
2009-12-15 23:33:31 +00:00
|
|
|
v := <-h.msgChan
|
2009-11-21 23:53:03 +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-21 23:53:03 +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-21 23:53:03 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
return v
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *clientHandshake) 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.
|
|
|
|
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{Error: e}
|
2009-12-15 23:33:31 +00:00
|
|
|
close(h.controlChan)
|
|
|
|
h.writeChan <- alert{alertLevelError, e}
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
}
|