2010-12-16 22:10:50 +00:00
|
|
|
// Copyright 2010 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"
|
2010-12-16 22:10:50 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/x509"
|
2011-11-02 02:04:37 +00:00
|
|
|
"errors"
|
2010-12-16 22:10:50 +00:00
|
|
|
"io"
|
2011-11-08 23:40:58 +00:00
|
|
|
"math/big"
|
2010-12-16 22:10:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rsaKeyAgreement implements the standard TLS key agreement where the client
|
|
|
|
// encrypts the pre-master secret to the server's public key.
|
|
|
|
type rsaKeyAgreement struct{}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka rsaKeyAgreement) generateServerKeyExchange(config *Config, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka rsaKeyAgreement) processClientKeyExchange(config *Config, ckx *clientKeyExchangeMsg, version uint16) ([]byte, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
preMasterSecret := make([]byte, 48)
|
|
|
|
_, err := io.ReadFull(config.rand(), preMasterSecret[2:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ckx.ciphertext) < 2 {
|
2011-11-02 02:04:37 +00:00
|
|
|
return nil, errors.New("bad ClientKeyExchange")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
ciphertext := ckx.ciphertext
|
|
|
|
if version != versionSSL30 {
|
|
|
|
ciphertextLen := int(ckx.ciphertext[0])<<8 | int(ckx.ciphertext[1])
|
|
|
|
if ciphertextLen != len(ckx.ciphertext)-2 {
|
2011-11-02 02:04:37 +00:00
|
|
|
return nil, errors.New("bad ClientKeyExchange")
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
ciphertext = ckx.ciphertext[2:]
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 15:39:30 +00:00
|
|
|
err = rsa.DecryptPKCS1v15SessionKey(config.rand(), config.Certificates[0].PrivateKey.(*rsa.PrivateKey), ciphertext, preMasterSecret)
|
2010-12-16 22:10:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
return preMasterSecret, nil
|
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka rsaKeyAgreement) processServerKeyExchange(config *Config, clientHello *clientHelloMsg, serverHello *serverHelloMsg, cert *x509.Certificate, skx *serverKeyExchangeMsg) error {
|
|
|
|
return errors.New("unexpected ServerKeyExchange")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka rsaKeyAgreement) generateClientKeyExchange(config *Config, clientHello *clientHelloMsg, cert *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
preMasterSecret := make([]byte, 48)
|
|
|
|
preMasterSecret[0] = byte(clientHello.vers >> 8)
|
|
|
|
preMasterSecret[1] = byte(clientHello.vers)
|
|
|
|
_, err := io.ReadFull(config.rand(), preMasterSecret[2:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypted, err := rsa.EncryptPKCS1v15(config.rand(), cert.PublicKey.(*rsa.PublicKey), preMasterSecret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
ckx := new(clientKeyExchangeMsg)
|
|
|
|
ckx.ciphertext = make([]byte, len(encrypted)+2)
|
|
|
|
ckx.ciphertext[0] = byte(len(encrypted) >> 8)
|
|
|
|
ckx.ciphertext[1] = byte(len(encrypted))
|
|
|
|
copy(ckx.ciphertext[2:], encrypted)
|
|
|
|
return preMasterSecret, ckx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// md5SHA1Hash implements TLS 1.0's hybrid hash function which consists of the
|
|
|
|
// concatenation of an MD5 and SHA1 hash.
|
|
|
|
func md5SHA1Hash(slices ...[]byte) []byte {
|
|
|
|
md5sha1 := make([]byte, md5.Size+sha1.Size)
|
|
|
|
hmd5 := md5.New()
|
|
|
|
for _, slice := range slices {
|
|
|
|
hmd5.Write(slice)
|
|
|
|
}
|
2011-12-01 17:35:37 +00:00
|
|
|
copy(md5sha1, hmd5.Sum(nil))
|
2010-12-16 22:10:50 +00:00
|
|
|
|
|
|
|
hsha1 := sha1.New()
|
|
|
|
for _, slice := range slices {
|
|
|
|
hsha1.Write(slice)
|
|
|
|
}
|
2011-12-01 17:35:37 +00:00
|
|
|
copy(md5sha1[md5.Size:], hsha1.Sum(nil))
|
2010-12-16 22:10:50 +00:00
|
|
|
return md5sha1
|
|
|
|
}
|
|
|
|
|
|
|
|
// ecdheRSAKeyAgreement implements a TLS key agreement where the server
|
|
|
|
// generates a ephemeral EC public/private key pair and signs it. The
|
|
|
|
// pre-master secret is then calculated using ECDH.
|
|
|
|
type ecdheRSAKeyAgreement struct {
|
|
|
|
privateKey []byte
|
2012-01-19 13:39:03 +00:00
|
|
|
curve elliptic.Curve
|
2010-12-16 22:10:50 +00:00
|
|
|
x, y *big.Int
|
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka *ecdheRSAKeyAgreement) generateServerKeyExchange(config *Config, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
var curveid uint16
|
|
|
|
|
|
|
|
Curve:
|
|
|
|
for _, c := range clientHello.supportedCurves {
|
|
|
|
switch c {
|
|
|
|
case curveP256:
|
|
|
|
ka.curve = elliptic.P256()
|
|
|
|
curveid = c
|
|
|
|
break Curve
|
|
|
|
case curveP384:
|
|
|
|
ka.curve = elliptic.P384()
|
|
|
|
curveid = c
|
|
|
|
break Curve
|
|
|
|
case curveP521:
|
|
|
|
ka.curve = elliptic.P521()
|
|
|
|
curveid = c
|
|
|
|
break Curve
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var x, y *big.Int
|
2011-11-02 02:04:37 +00:00
|
|
|
var err error
|
2012-01-19 13:39:03 +00:00
|
|
|
ka.privateKey, x, y, err = elliptic.GenerateKey(ka.curve, config.rand())
|
2010-12-16 22:10:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-01-19 13:39:03 +00:00
|
|
|
ecdhePublic := elliptic.Marshal(ka.curve, x, y)
|
2010-12-16 22:10:50 +00:00
|
|
|
|
|
|
|
// http://tools.ietf.org/html/rfc4492#section-5.4
|
|
|
|
serverECDHParams := make([]byte, 1+2+1+len(ecdhePublic))
|
|
|
|
serverECDHParams[0] = 3 // named curve
|
|
|
|
serverECDHParams[1] = byte(curveid >> 8)
|
|
|
|
serverECDHParams[2] = byte(curveid)
|
|
|
|
serverECDHParams[3] = byte(len(ecdhePublic))
|
|
|
|
copy(serverECDHParams[4:], ecdhePublic)
|
|
|
|
|
|
|
|
md5sha1 := md5SHA1Hash(clientHello.random, hello.random, serverECDHParams)
|
2011-12-19 15:39:30 +00:00
|
|
|
sig, err := rsa.SignPKCS1v15(config.rand(), config.Certificates[0].PrivateKey.(*rsa.PrivateKey), crypto.MD5SHA1, md5sha1)
|
2010-12-16 22:10:50 +00:00
|
|
|
if err != nil {
|
2011-11-02 02:04:37 +00:00
|
|
|
return nil, errors.New("failed to sign ECDHE parameters: " + err.Error())
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
skx := new(serverKeyExchangeMsg)
|
|
|
|
skx.key = make([]byte, len(serverECDHParams)+2+len(sig))
|
|
|
|
copy(skx.key, serverECDHParams)
|
|
|
|
k := skx.key[len(serverECDHParams):]
|
|
|
|
k[0] = byte(len(sig) >> 8)
|
|
|
|
k[1] = byte(len(sig))
|
|
|
|
copy(k[2:], sig)
|
|
|
|
|
|
|
|
return skx, nil
|
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka *ecdheRSAKeyAgreement) processClientKeyExchange(config *Config, ckx *clientKeyExchangeMsg, version uint16) ([]byte, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
if len(ckx.ciphertext) == 0 || int(ckx.ciphertext[0]) != len(ckx.ciphertext)-1 {
|
2011-11-02 02:04:37 +00:00
|
|
|
return nil, errors.New("bad ClientKeyExchange")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2012-01-19 13:39:03 +00:00
|
|
|
x, y := elliptic.Unmarshal(ka.curve, ckx.ciphertext[1:])
|
2010-12-16 22:10:50 +00:00
|
|
|
if x == nil {
|
2011-11-02 02:04:37 +00:00
|
|
|
return nil, errors.New("bad ClientKeyExchange")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
x, _ = ka.curve.ScalarMult(x, y, ka.privateKey)
|
2012-01-19 13:39:03 +00:00
|
|
|
preMasterSecret := make([]byte, (ka.curve.Params().BitSize+7)>>3)
|
2010-12-16 22:10:50 +00:00
|
|
|
xBytes := x.Bytes()
|
|
|
|
copy(preMasterSecret[len(preMasterSecret)-len(xBytes):], xBytes)
|
|
|
|
|
|
|
|
return preMasterSecret, nil
|
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
var errServerKeyExchange = errors.New("invalid ServerKeyExchange")
|
2011-06-17 11:07:13 +01:00
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka *ecdheRSAKeyAgreement) processServerKeyExchange(config *Config, clientHello *clientHelloMsg, serverHello *serverHelloMsg, cert *x509.Certificate, skx *serverKeyExchangeMsg) error {
|
2010-12-16 22:10:50 +00:00
|
|
|
if len(skx.key) < 4 {
|
2011-06-17 11:07:13 +01:00
|
|
|
return errServerKeyExchange
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
if skx.key[0] != 3 { // named curve
|
2011-11-02 02:04:37 +00:00
|
|
|
return errors.New("server selected unsupported curve")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
curveid := uint16(skx.key[1])<<8 | uint16(skx.key[2])
|
|
|
|
|
|
|
|
switch curveid {
|
|
|
|
case curveP256:
|
|
|
|
ka.curve = elliptic.P256()
|
|
|
|
case curveP384:
|
|
|
|
ka.curve = elliptic.P384()
|
|
|
|
case curveP521:
|
|
|
|
ka.curve = elliptic.P521()
|
|
|
|
default:
|
2011-11-02 02:04:37 +00:00
|
|
|
return errors.New("server selected unsupported curve")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
publicLen := int(skx.key[3])
|
|
|
|
if publicLen+4 > len(skx.key) {
|
2011-06-17 11:07:13 +01:00
|
|
|
return errServerKeyExchange
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2012-01-19 13:39:03 +00:00
|
|
|
ka.x, ka.y = elliptic.Unmarshal(ka.curve, skx.key[4:4+publicLen])
|
2010-12-16 22:10:50 +00:00
|
|
|
if ka.x == nil {
|
2011-06-17 11:07:13 +01:00
|
|
|
return errServerKeyExchange
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
serverECDHParams := skx.key[:4+publicLen]
|
|
|
|
|
|
|
|
sig := skx.key[4+publicLen:]
|
|
|
|
if len(sig) < 2 {
|
2011-06-17 11:07:13 +01:00
|
|
|
return errServerKeyExchange
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
sigLen := int(sig[0])<<8 | int(sig[1])
|
|
|
|
if sigLen+2 != len(sig) {
|
2011-06-17 11:07:13 +01:00
|
|
|
return errServerKeyExchange
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
sig = sig[2:]
|
|
|
|
|
|
|
|
md5sha1 := md5SHA1Hash(clientHello.random, serverHello.random, serverECDHParams)
|
2011-02-01 16:02:48 +00:00
|
|
|
return rsa.VerifyPKCS1v15(cert.PublicKey.(*rsa.PublicKey), crypto.MD5SHA1, md5sha1, sig)
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (ka *ecdheRSAKeyAgreement) generateClientKeyExchange(config *Config, clientHello *clientHelloMsg, cert *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
if ka.curve == nil {
|
2011-11-02 02:04:37 +00:00
|
|
|
return nil, nil, errors.New("missing ServerKeyExchange message")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2012-01-19 13:39:03 +00:00
|
|
|
priv, mx, my, err := elliptic.GenerateKey(ka.curve, config.rand())
|
2010-12-16 22:10:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
x, _ := ka.curve.ScalarMult(ka.x, ka.y, priv)
|
2012-01-19 13:39:03 +00:00
|
|
|
preMasterSecret := make([]byte, (ka.curve.Params().BitSize+7)>>3)
|
2010-12-16 22:10:50 +00:00
|
|
|
xBytes := x.Bytes()
|
|
|
|
copy(preMasterSecret[len(preMasterSecret)-len(xBytes):], xBytes)
|
|
|
|
|
2012-01-19 13:39:03 +00:00
|
|
|
serialized := elliptic.Marshal(ka.curve, mx, my)
|
2010-12-16 22:10:50 +00:00
|
|
|
|
|
|
|
ckx := new(clientKeyExchangeMsg)
|
2011-05-18 18:14:56 +01:00
|
|
|
ckx.ciphertext = make([]byte, 1+len(serialized))
|
|
|
|
ckx.ciphertext[0] = byte(len(serialized))
|
|
|
|
copy(ckx.ciphertext[1:], serialized)
|
2010-12-16 22:10:50 +00:00
|
|
|
|
|
|
|
return preMasterSecret, ckx, nil
|
|
|
|
}
|