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 (
|
2012-01-05 17:05:38 +00:00
|
|
|
"bytes"
|
2011-02-01 16:02:48 +00:00
|
|
|
"crypto"
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/subtle"
|
|
|
|
"crypto/x509"
|
2011-11-02 02:04:37 +00:00
|
|
|
"errors"
|
2009-12-15 23:33:31 +00:00
|
|
|
"io"
|
2012-01-05 17:05:38 +00:00
|
|
|
"strconv"
|
2009-11-21 23:53:03 +00:00
|
|
|
)
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (c *Conn) clientHandshake() error {
|
2011-09-14 20:32:19 +01:00
|
|
|
finishedHash := newFinishedHash(versionTLS10)
|
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
|
|
|
}
|
|
|
|
|
2011-11-30 17:01:46 +00:00
|
|
|
t := uint32(c.config.time().Unix())
|
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)
|
2011-11-02 02:04:37 +00:00
|
|
|
return errors.New("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)
|
2012-01-31 16:22:47 +00:00
|
|
|
if !ok || vers < versionTLS10 {
|
|
|
|
// TLS 1.0 is the minimum version supported as a client.
|
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)
|
2011-11-02 02:04:37 +00:00
|
|
|
return errors.New("server advertised unrequested NPN")
|
2011-03-29 22:53:09 +01:00
|
|
|
}
|
|
|
|
|
2011-11-28 20:34:16 +00:00
|
|
|
suite := mutualCipherSuite(c.config.cipherSuites(), serverHello.cipherSuite)
|
2010-12-15 16:49:55 +00:00
|
|
|
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)
|
2011-11-02 02:04:37 +00:00
|
|
|
return errors.New("failed to parse certificate from server: " + err.Error())
|
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
|
|
|
}
|
|
|
|
|
2011-10-13 18:59:13 +01:00
|
|
|
if !c.config.InsecureSkipVerify {
|
2011-04-19 14:57:58 +01:00
|
|
|
opts := x509.VerifyOptions{
|
2012-03-07 18:12:35 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 17:05:38 +00:00
|
|
|
var certToSend *Certificate
|
2012-03-19 16:34:35 +00:00
|
|
|
var certRequested bool
|
2010-08-16 16:22:22 +01:00
|
|
|
certReq, ok := msg.(*certificateRequestMsg)
|
|
|
|
if ok {
|
2012-03-19 16:34:35 +00:00
|
|
|
certRequested = true
|
|
|
|
|
2012-01-05 17:05:38 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
finishedHash.Write(certReq.marshal())
|
|
|
|
|
|
|
|
// For now, we only know how to sign challenges with RSA
|
2010-08-16 16:22:22 +01:00
|
|
|
rsaAvail := false
|
|
|
|
for _, certType := range certReq.certificateTypes {
|
|
|
|
if certType == certTypeRSASign {
|
|
|
|
rsaAvail = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 17:05:38 +00:00
|
|
|
// We need to search our list of client certs for one
|
|
|
|
// where SignatureAlgorithm is RSA and the Issuer is in
|
|
|
|
// certReq.certificateAuthorities
|
|
|
|
findCert:
|
|
|
|
for i, cert := range c.config.Certificates {
|
|
|
|
if !rsaAvail {
|
|
|
|
continue
|
|
|
|
}
|
2010-08-16 16:22:22 +01:00
|
|
|
|
2012-01-05 17:05:38 +00:00
|
|
|
leaf := cert.Leaf
|
|
|
|
if leaf == nil {
|
|
|
|
if leaf, err = x509.ParseCertificate(cert.Certificate[0]); err != nil {
|
|
|
|
c.sendAlert(alertInternalError)
|
|
|
|
return errors.New("tls: failed to parse client certificate #" + strconv.Itoa(i) + ": " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if leaf.PublicKeyAlgorithm != x509.RSA {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(certReq.certificateAuthorities) == 0 {
|
|
|
|
// they gave us an empty list, so just take the
|
|
|
|
// first RSA cert from c.config.Certificates
|
|
|
|
certToSend = &cert
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ca := range certReq.certificateAuthorities {
|
|
|
|
if bytes.Equal(leaf.RawIssuer, ca) {
|
|
|
|
certToSend = &cert
|
|
|
|
break findCert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-16 16:22:22 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2012-03-19 16:34:35 +00:00
|
|
|
// If the server requested a certificate then we have to send a
|
|
|
|
// Certificate message, even if it's empty because we don't have a
|
|
|
|
// certificate to send.
|
|
|
|
if certRequested {
|
2010-08-16 16:22:22 +01:00
|
|
|
certMsg = new(certificateMsg)
|
2012-03-19 16:34:35 +00:00
|
|
|
if certToSend != nil {
|
|
|
|
certMsg.certificates = certToSend.Certificate
|
|
|
|
}
|
2010-08-16 16:22:22 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-01-05 17:05:38 +00:00
|
|
|
if certToSend != nil {
|
2010-08-16 16:22:22 +01:00
|
|
|
certVerify := new(certificateVerifyMsg)
|
2011-12-06 23:25:14 +00:00
|
|
|
digest := make([]byte, 0, 36)
|
|
|
|
digest = finishedHash.serverMD5.Sum(digest)
|
|
|
|
digest = finishedHash.serverSHA1.Sum(digest)
|
2011-12-19 15:39:30 +00:00
|
|
|
signed, err := rsa.SignPKCS1v15(c.config.rand(), c.config.Certificates[0].PrivateKey.(*rsa.PrivateKey), crypto.MD5SHA1, digest)
|
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 :=
|
2011-09-14 20:32:19 +01:00
|
|
|
keysFromPreMasterSecret(c.vers, preMasterSecret, hello.random, serverHello.random, suite.macLen, suite.keyLen, suite.ivLen)
|
2010-04-27 06:19:04 +01:00
|
|
|
|
2012-02-22 19:27:45 +00:00
|
|
|
clientCipher := suite.cipher(clientKey, clientIV, false /* not for reading */)
|
2011-09-14 20:32:19 +01:00
|
|
|
clientHash := suite.mac(c.vers, clientMAC)
|
|
|
|
c.out.prepareCipherSpec(c.vers, 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
|
|
|
|
2012-02-22 19:27:45 +00:00
|
|
|
serverCipher := suite.cipher(serverKey, serverIV, true /* for reading */)
|
2011-09-14 20:32:19 +01:00
|
|
|
serverHash := suite.mac(c.vers, serverMAC)
|
|
|
|
c.in.prepareCipherSpec(c.vers, 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
|
2011-11-28 20:34:16 +00:00
|
|
|
c.cipherSuite = suite.id
|
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
|
|
|
|
}
|