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"
|
2010-04-27 06:19:04 +01:00
|
|
|
"os"
|
2009-11-21 23:53:03 +00:00
|
|
|
)
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
func (c *Conn) clientHandshake() os.Error {
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash := newFinishedHash()
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
config := defaultConfig()
|
|
|
|
|
2009-11-21 23:53:03 +00:00
|
|
|
hello := &clientHelloMsg{
|
2010-04-27 06:19:04 +01:00
|
|
|
vers: maxVersion,
|
2010-03-02 21:46:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
t := uint32(config.Time())
|
|
|
|
hello.random[0] = byte(t >> 24)
|
|
|
|
hello.random[1] = byte(t >> 16)
|
|
|
|
hello.random[2] = byte(t >> 8)
|
|
|
|
hello.random[3] = byte(t)
|
2009-12-15 23:33:31 +00:00
|
|
|
_, err := io.ReadFull(config.Rand, hello.random[4:])
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertInternalError)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(hello.marshal())
|
2010-04-27 06:19:04 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, hello.marshal())
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
msg, err := c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
serverHello, ok := msg.(*serverHelloMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(serverHello.marshal())
|
2010-04-27 06:19:04 +01:00
|
|
|
|
|
|
|
vers, ok := mutualVersion(serverHello.vers)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2010-04-27 06:19:04 +01:00
|
|
|
c.sendAlert(alertProtocolVersion)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
c.vers = vers
|
|
|
|
c.haveVers = true
|
2009-11-21 23:53:03 +00:00
|
|
|
|
|
|
|
if serverHello.cipherSuite != TLS_RSA_WITH_RC4_128_SHA ||
|
|
|
|
serverHello.compressionMethod != compressionNone {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certMsg, ok := msg.(*certificateMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok || len(certMsg.certificates) == 0 {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
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 {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertBadCertificate)
|
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++ {
|
2010-04-27 06:19:04 +01:00
|
|
|
if err := certs[i-1].CheckSignatureFrom(certs[i]); err != nil {
|
|
|
|
return c.sendAlert(alertBadCertificate)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
// TODO(rsc): Find certificates for OS X 10.6.
|
|
|
|
if false && 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 {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertBadCertificate)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
if certs[len(certs)-1].CheckSignatureFrom(root) != nil {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertBadCertificate)
|
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 {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnsupportedCertificate)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
shd, ok := msg.(*serverHelloDoneMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
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)
|
2010-04-27 06:19:04 +01:00
|
|
|
preMasterSecret[0] = byte(hello.vers >> 8)
|
|
|
|
preMasterSecret[1] = byte(hello.vers)
|
2009-12-15 23:33:31 +00:00
|
|
|
_, err = io.ReadFull(config.Rand, preMasterSecret[2:])
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertInternalError)
|
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 {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertInternalError)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finishedHash.Write(ckx.marshal())
|
2010-04-27 06:19:04 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, ckx.marshal())
|
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)
|
2010-04-27 06:19:04 +01:00
|
|
|
|
|
|
|
c.out.prepareCipherSpec(cipher, hmac.New(sha1.New(), clientMAC))
|
|
|
|
c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
|
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())
|
2010-04-27 06:19:04 +01:00
|
|
|
c.writeRecord(recordTypeHandshake, finished.marshal())
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
cipher2, _ := rc4.NewCipher(serverKey)
|
2010-04-27 06:19:04 +01:00
|
|
|
c.in.prepareCipherSpec(cipher2, hmac.New(sha1.New(), serverMAC))
|
|
|
|
c.readRecord(recordTypeChangeCipherSpec)
|
|
|
|
if c.err != nil {
|
|
|
|
return c.err
|
|
|
|
}
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
serverFinished, ok := msg.(*finishedMsg)
|
2009-11-21 23:53:03 +00:00
|
|
|
if !ok {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
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 {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertHandshakeFailure)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
c.handshakeComplete = true
|
|
|
|
c.cipherSuite = TLS_RSA_WITH_RC4_128_SHA
|
|
|
|
return nil
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|