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 (
|
2011-02-01 16:02:48 +00:00
|
|
|
"crypto"
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"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-07-14 15:40:15 +01:00
|
|
|
if c.config == nil {
|
|
|
|
c.config = defaultConfig()
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
|
2009-11-21 23:53:03 +00:00
|
|
|
hello := &clientHelloMsg{
|
2010-04-27 06:19:04 +01:00
|
|
|
vers: maxVersion,
|
2010-12-15 16:49:55 +00:00
|
|
|
cipherSuites: c.config.cipherSuites(),
|
2009-11-21 23:53:03 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2010-03-02 21:46:51 +00:00
|
|
|
random: make([]byte, 32),
|
2010-07-14 15:40:15 +01:00
|
|
|
ocspStapling: true,
|
2010-07-21 16:36:01 +01:00
|
|
|
serverName: c.config.ServerName,
|
2010-12-16 22:10:50 +00:00
|
|
|
supportedCurves: []uint16{curveP256, curveP384, curveP521},
|
|
|
|
supportedPoints: []uint8{pointFormatUncompressed},
|
2011-03-29 22:53:09 +01:00
|
|
|
nextProtoNeg: len(c.config.NextProtos) > 0,
|
2009-12-15 23:33:31 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
t := uint32(c.config.time())
|
2010-04-27 06:19:04 +01:00
|
|
|
hello.random[0] = byte(t >> 24)
|
|
|
|
hello.random[1] = byte(t >> 16)
|
|
|
|
hello.random[2] = byte(t >> 8)
|
|
|
|
hello.random[3] = byte(t)
|
2010-12-07 21:15:15 +00:00
|
|
|
_, err := io.ReadFull(c.config.rand(), hello.random[4:])
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2010-10-11 15:39:56 +01:00
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return os.ErrorString("short read from Rand")
|
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 {
|
2011-02-15 21:38:45 +00:00
|
|
|
return 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
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
if serverHello.compressionMethod != compressionNone {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2011-03-29 22:53:09 +01:00
|
|
|
if !hello.nextProtoNeg && serverHello.nextProtoNeg {
|
|
|
|
c.sendAlert(alertHandshakeFailure)
|
|
|
|
return os.ErrorString("server advertised unrequested NPN")
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
suite, suiteId := mutualCipherSuite(c.config.cipherSuites(), serverHello.cipherSuite)
|
|
|
|
if suite == nil {
|
|
|
|
return c.sendAlert(alertHandshakeFailure)
|
|
|
|
}
|
|
|
|
|
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-10-11 15:39:56 +01:00
|
|
|
c.sendAlert(alertBadCertificate)
|
|
|
|
return os.ErrorString("failed to parse certificate from server: " + err.String())
|
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
|
|
|
}
|
|
|
|
|
2010-11-05 13:54:56 +00:00
|
|
|
// If we don't have a root CA set configured then anything is accepted.
|
|
|
|
// TODO(rsc): Find certificates for OS X 10.6.
|
2011-04-19 14:57:58 +01:00
|
|
|
if c.config.RootCAs != nil {
|
|
|
|
opts := x509.VerifyOptions{
|
|
|
|
Roots: c.config.RootCAs,
|
2011-04-25 15:27:36 +01:00
|
|
|
CurrentTime: c.config.time(),
|
2011-04-19 14:57:58 +01:00
|
|
|
DNSName: c.config.ServerName,
|
|
|
|
Intermediates: x509.NewCertPool(),
|
2010-09-20 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
2011-04-19 14:57:58 +01:00
|
|
|
for i, cert := range certs {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
opts.Intermediates.AddCert(cert)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2011-04-19 14:57:58 +01:00
|
|
|
c.verifiedChains, err = certs[0].Verify(opts)
|
|
|
|
if err != nil {
|
2010-10-11 15:39:56 +01:00
|
|
|
c.sendAlert(alertBadCertificate)
|
2011-04-19 14:57:58 +01:00
|
|
|
return err
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
if _, ok := certs[0].PublicKey.(*rsa.PublicKey); !ok {
|
2010-04-27 06:19:04 +01:00
|
|
|
return c.sendAlert(alertUnsupportedCertificate)
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 16:36:01 +01:00
|
|
|
c.peerCertificates = certs
|
|
|
|
|
2011-04-14 19:47:28 +01:00
|
|
|
if serverHello.ocspStapling {
|
2010-07-14 15:40:15 +01:00
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cs, ok := msg.(*certificateStatusMsg)
|
|
|
|
if !ok {
|
|
|
|
return c.sendAlert(alertUnexpectedMessage)
|
|
|
|
}
|
|
|
|
finishedHash.Write(cs.marshal())
|
|
|
|
|
|
|
|
if cs.statusType == statusTypeOCSP {
|
|
|
|
c.ocspResponse = cs.response
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2010-08-16 16:22:22 +01:00
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
keyAgreement := suite.ka()
|
|
|
|
|
|
|
|
skx, ok := msg.(*serverKeyExchangeMsg)
|
|
|
|
if ok {
|
|
|
|
finishedHash.Write(skx.marshal())
|
|
|
|
err = keyAgreement.processServerKeyExchange(c.config, hello, serverHello, certs[0], skx)
|
|
|
|
if err != nil {
|
|
|
|
c.sendAlert(alertUnexpectedMessage)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-16 16:22:22 +01:00
|
|
|
transmitCert := false
|
|
|
|
certReq, ok := msg.(*certificateRequestMsg)
|
|
|
|
if ok {
|
|
|
|
// We only accept certificates with RSA keys.
|
|
|
|
rsaAvail := false
|
|
|
|
for _, certType := range certReq.certificateTypes {
|
|
|
|
if certType == certTypeRSASign {
|
|
|
|
rsaAvail = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now, only send a certificate back if the server gives us an
|
|
|
|
// empty list of certificateAuthorities.
|
|
|
|
//
|
|
|
|
// RFC 4346 on the certificateAuthorities field:
|
|
|
|
// A list of the distinguished names of acceptable certificate
|
|
|
|
// authorities. These distinguished names may specify a desired
|
|
|
|
// distinguished name for a root CA or for a subordinate CA; thus,
|
|
|
|
// this message can be used to describe both known roots and a
|
|
|
|
// desired authorization space. If the certificate_authorities
|
|
|
|
// list is empty then the client MAY send any certificate of the
|
|
|
|
// appropriate ClientCertificateType, unless there is some
|
|
|
|
// external arrangement to the contrary.
|
|
|
|
if rsaAvail && len(certReq.certificateAuthorities) == 0 {
|
|
|
|
transmitCert = true
|
|
|
|
}
|
|
|
|
|
|
|
|
finishedHash.Write(certReq.marshal())
|
|
|
|
|
|
|
|
msg, err = c.readHandshake()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
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
|
|
|
|
2010-08-16 16:22:22 +01:00
|
|
|
var cert *x509.Certificate
|
|
|
|
if transmitCert {
|
|
|
|
certMsg = new(certificateMsg)
|
|
|
|
if len(c.config.Certificates) > 0 {
|
|
|
|
cert, err = x509.ParseCertificate(c.config.Certificates[0].Certificate[0])
|
|
|
|
if err == nil && cert.PublicKeyAlgorithm == x509.RSA {
|
|
|
|
certMsg.certificates = c.config.Certificates[0].Certificate
|
|
|
|
} else {
|
|
|
|
cert = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finishedHash.Write(certMsg.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, certMsg.marshal())
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
preMasterSecret, ckx, err := keyAgreement.generateClientKeyExchange(c.config, hello, certs[0])
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
2010-12-16 22:10:50 +00:00
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return err
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2010-12-16 22:10:50 +00:00
|
|
|
if ckx != nil {
|
|
|
|
finishedHash.Write(ckx.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, ckx.marshal())
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2010-08-16 16:22:22 +01:00
|
|
|
if cert != nil {
|
|
|
|
certVerify := new(certificateVerifyMsg)
|
|
|
|
var digest [36]byte
|
|
|
|
copy(digest[0:16], finishedHash.serverMD5.Sum())
|
|
|
|
copy(digest[16:36], finishedHash.serverSHA1.Sum())
|
2011-02-01 16:02:48 +00:00
|
|
|
signed, err := rsa.SignPKCS1v15(c.config.rand(), c.config.Certificates[0].PrivateKey, crypto.MD5SHA1, digest[0:])
|
2010-08-16 16:22:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return c.sendAlert(alertInternalError)
|
|
|
|
}
|
|
|
|
certVerify.signature = signed
|
|
|
|
|
|
|
|
finishedHash.Write(certVerify.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, certVerify.marshal())
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
masterSecret, clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV :=
|
|
|
|
keysFromPreMasterSecret10(preMasterSecret, hello.random, serverHello.random, suite.macLen, suite.keyLen, suite.ivLen)
|
2010-04-27 06:19:04 +01:00
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
clientCipher := suite.cipher(clientKey, clientIV, false /* not for reading */ )
|
|
|
|
clientHash := suite.mac(clientMAC)
|
|
|
|
c.out.prepareCipherSpec(clientCipher, clientHash)
|
2010-04-27 06:19:04 +01:00
|
|
|
c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
|
2009-11-21 23:53:03 +00:00
|
|
|
|
2011-03-29 22:53:09 +01:00
|
|
|
if serverHello.nextProtoNeg {
|
|
|
|
nextProto := new(nextProtoMsg)
|
|
|
|
proto, fallback := mutualProtocol(c.config.NextProtos, serverHello.nextProtos)
|
|
|
|
nextProto.proto = proto
|
|
|
|
c.clientProtocol = proto
|
|
|
|
c.clientProtocolFallback = fallback
|
|
|
|
|
|
|
|
finishedHash.Write(nextProto.marshal())
|
|
|
|
c.writeRecord(recordTypeHandshake, nextProto.marshal())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
serverCipher := suite.cipher(serverKey, serverIV, true /* for reading */ )
|
|
|
|
serverHash := suite.mac(serverMAC)
|
|
|
|
c.in.prepareCipherSpec(serverCipher, serverHash)
|
2010-04-27 06:19:04 +01:00
|
|
|
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
|
2010-12-15 16:49:55 +00:00
|
|
|
c.cipherSuite = suiteId
|
2010-04-27 06:19:04 +01:00
|
|
|
return nil
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
2011-03-29 22:53:09 +01:00
|
|
|
|
|
|
|
// mutualProtocol finds the mutual Next Protocol Negotiation protocol given the
|
|
|
|
// set of client and server supported protocols. The set of client supported
|
|
|
|
// protocols must not be empty. It returns the resulting protocol and flag
|
|
|
|
// indicating if the fallback case was reached.
|
|
|
|
func mutualProtocol(clientProtos, serverProtos []string) (string, bool) {
|
|
|
|
for _, s := range serverProtos {
|
|
|
|
for _, c := range clientProtos {
|
|
|
|
if s == c {
|
|
|
|
return s, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientProtos[0], true
|
|
|
|
}
|