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"
|
2013-07-17 17:33:16 +01:00
|
|
|
"crypto/ecdsa"
|
2010-12-16 22:10:50 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha1"
|
2013-07-03 00:58:56 +01:00
|
|
|
"crypto/sha256"
|
2010-12-16 22:10:50 +00:00
|
|
|
"crypto/x509"
|
2013-07-17 17:33:16 +01:00
|
|
|
"encoding/asn1"
|
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
|
|
|
)
|
|
|
|
|
2014-02-12 16:20:01 +00:00
|
|
|
var errClientKeyExchange = errors.New("tls: invalid ClientKeyExchange message")
|
|
|
|
var errServerKeyExchange = errors.New("tls: invalid ServerKeyExchange message")
|
|
|
|
|
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{}
|
|
|
|
|
2012-04-12 17:35:21 +01:00
|
|
|
func (ka rsaKeyAgreement) generateServerKeyExchange(config *Config, cert *Certificate, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2012-04-12 17:35:21 +01:00
|
|
|
func (ka rsaKeyAgreement) processClientKeyExchange(config *Config, cert *Certificate, ckx *clientKeyExchangeMsg, version uint16) ([]byte, error) {
|
2010-12-16 22:10:50 +00:00
|
|
|
if len(ckx.ciphertext) < 2 {
|
2014-02-12 16:20:01 +00:00
|
|
|
return nil, errClientKeyExchange
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
ciphertext := ckx.ciphertext
|
2013-06-05 01:02:22 +01:00
|
|
|
if version != VersionSSL30 {
|
2011-09-14 20:32:19 +01:00
|
|
|
ciphertextLen := int(ckx.ciphertext[0])<<8 | int(ckx.ciphertext[1])
|
|
|
|
if ciphertextLen != len(ckx.ciphertext)-2 {
|
2014-02-12 16:20:01 +00:00
|
|
|
return nil, errClientKeyExchange
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
ciphertext = ckx.ciphertext[2:]
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2015-03-19 11:01:57 +00:00
|
|
|
priv, ok := cert.PrivateKey.(crypto.Decrypter)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("tls: certificate private key does not implement crypto.Decrypter")
|
|
|
|
}
|
|
|
|
// Perform contant time RSA PKCS#1 v1.5 decryption
|
|
|
|
preMasterSecret, err := priv.Decrypt(config.rand(), ciphertext, &rsa.PKCS1v15DecryptOptions{SessionKeyLen: 48})
|
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 {
|
2014-02-12 16:20:01 +00:00
|
|
|
return errors.New("tls: 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
|
|
|
|
}
|
|
|
|
|
2013-07-17 17:33:16 +01:00
|
|
|
// sha1Hash calculates a SHA1 hash over the given byte slices.
|
|
|
|
func sha1Hash(slices [][]byte) []byte {
|
|
|
|
hsha1 := sha1.New()
|
|
|
|
for _, slice := range slices {
|
|
|
|
hsha1.Write(slice)
|
|
|
|
}
|
|
|
|
return hsha1.Sum(nil)
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
// md5SHA1Hash implements TLS 1.0's hybrid hash function which consists of the
|
|
|
|
// concatenation of an MD5 and SHA1 hash.
|
2013-07-03 00:58:56 +01:00
|
|
|
func md5SHA1Hash(slices [][]byte) []byte {
|
2010-12-16 22:10:50 +00:00
|
|
|
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))
|
2013-07-17 17:33:16 +01:00
|
|
|
copy(md5sha1[md5.Size:], sha1Hash(slices))
|
2010-12-16 22:10:50 +00:00
|
|
|
return md5sha1
|
|
|
|
}
|
|
|
|
|
2013-07-03 00:58:56 +01:00
|
|
|
// sha256Hash implements TLS 1.2's hash function.
|
|
|
|
func sha256Hash(slices [][]byte) []byte {
|
|
|
|
h := sha256.New()
|
|
|
|
for _, slice := range slices {
|
|
|
|
h.Write(slice)
|
|
|
|
}
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hashForServerKeyExchange hashes the given slices and returns their digest
|
2013-10-21 21:35:09 +01:00
|
|
|
// and the identifier of the hash function used. The hashFunc argument is only
|
|
|
|
// used for >= TLS 1.2 and precisely identifies the hash function to use.
|
|
|
|
func hashForServerKeyExchange(sigType, hashFunc uint8, version uint16, slices ...[]byte) ([]byte, crypto.Hash, error) {
|
2013-07-03 00:58:56 +01:00
|
|
|
if version >= VersionTLS12 {
|
2013-10-21 21:35:09 +01:00
|
|
|
switch hashFunc {
|
|
|
|
case hashSHA256:
|
|
|
|
return sha256Hash(slices), crypto.SHA256, nil
|
|
|
|
case hashSHA1:
|
|
|
|
return sha1Hash(slices), crypto.SHA1, nil
|
|
|
|
default:
|
|
|
|
return nil, crypto.Hash(0), errors.New("tls: unknown hash function used by peer")
|
|
|
|
}
|
2013-07-03 00:58:56 +01:00
|
|
|
}
|
2013-07-17 17:33:16 +01:00
|
|
|
if sigType == signatureECDSA {
|
2013-10-21 21:35:09 +01:00
|
|
|
return sha1Hash(slices), crypto.SHA1, nil
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
2013-10-21 21:35:09 +01:00
|
|
|
return md5SHA1Hash(slices), crypto.MD5SHA1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pickTLS12HashForSignature returns a TLS 1.2 hash identifier for signing a
|
|
|
|
// ServerKeyExchange given the signature type being used and the client's
|
2014-05-02 21:17:55 +01:00
|
|
|
// advertised list of supported signature and hash combinations.
|
2013-10-21 21:35:09 +01:00
|
|
|
func pickTLS12HashForSignature(sigType uint8, clientSignatureAndHashes []signatureAndHash) (uint8, error) {
|
|
|
|
if len(clientSignatureAndHashes) == 0 {
|
|
|
|
// If the client didn't specify any signature_algorithms
|
|
|
|
// extension then we can assume that it supports SHA1. See
|
|
|
|
// http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
|
|
|
|
return hashSHA1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sigAndHash := range clientSignatureAndHashes {
|
|
|
|
if sigAndHash.signature != sigType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch sigAndHash.hash {
|
|
|
|
case hashSHA1, hashSHA256:
|
|
|
|
return sigAndHash.hash, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, errors.New("tls: client doesn't support any common hash functions")
|
2013-07-03 00:58:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 22:57:51 +00:00
|
|
|
func curveForCurveID(id CurveID) (elliptic.Curve, bool) {
|
|
|
|
switch id {
|
|
|
|
case CurveP256:
|
|
|
|
return elliptic.P256(), true
|
|
|
|
case CurveP384:
|
|
|
|
return elliptic.P384(), true
|
|
|
|
case CurveP521:
|
|
|
|
return elliptic.P521(), true
|
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
// ecdheRSAKeyAgreement implements a TLS key agreement where the server
|
|
|
|
// generates a ephemeral EC public/private key pair and signs it. The
|
2013-07-17 17:33:16 +01:00
|
|
|
// pre-master secret is then calculated using ECDH. The signature may
|
|
|
|
// either be ECDSA or RSA.
|
|
|
|
type ecdheKeyAgreement struct {
|
2013-07-03 00:58:56 +01:00
|
|
|
version uint16
|
2013-07-17 17:33:16 +01:00
|
|
|
sigType uint8
|
2010-12-16 22:10:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-07-17 17:33:16 +01:00
|
|
|
func (ka *ecdheKeyAgreement) generateServerKeyExchange(config *Config, cert *Certificate, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, error) {
|
2014-02-24 22:57:51 +00:00
|
|
|
var curveid CurveID
|
|
|
|
preferredCurves := config.curvePreferences()
|
|
|
|
|
|
|
|
NextCandidate:
|
|
|
|
for _, candidate := range preferredCurves {
|
|
|
|
for _, c := range clientHello.supportedCurves {
|
|
|
|
if candidate == c {
|
|
|
|
curveid = c
|
|
|
|
break NextCandidate
|
|
|
|
}
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-23 14:48:51 +00:00
|
|
|
if curveid == 0 {
|
|
|
|
return nil, errors.New("tls: no supported elliptic curves offered")
|
|
|
|
}
|
|
|
|
|
2014-02-24 22:57:51 +00:00
|
|
|
var ok bool
|
|
|
|
if ka.curve, ok = curveForCurveID(curveid); !ok {
|
|
|
|
return nil, errors.New("tls: preferredCurves includes unsupported curve")
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
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)
|
|
|
|
|
2013-10-21 21:35:09 +01:00
|
|
|
var tls12HashId uint8
|
|
|
|
if ka.version >= VersionTLS12 {
|
|
|
|
if tls12HashId, err = pickTLS12HashForSignature(ka.sigType, clientHello.signatureAndHashes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
digest, hashFunc, err := hashForServerKeyExchange(ka.sigType, tls12HashId, ka.version, clientHello.random, hello.random, serverECDHParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-19 11:01:57 +00:00
|
|
|
|
|
|
|
priv, ok := cert.PrivateKey.(crypto.Signer)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("tls: certificate private key does not implement crypto.Signer")
|
|
|
|
}
|
2013-07-17 17:33:16 +01:00
|
|
|
var sig []byte
|
|
|
|
switch ka.sigType {
|
|
|
|
case signatureECDSA:
|
2015-03-19 11:01:57 +00:00
|
|
|
_, ok := priv.Public().(*ecdsa.PublicKey)
|
2013-07-17 17:33:16 +01:00
|
|
|
if !ok {
|
2015-03-19 11:01:57 +00:00
|
|
|
return nil, errors.New("ECDHE ECDSA requires an ECDSA server key")
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
|
|
|
case signatureRSA:
|
2015-03-19 11:01:57 +00:00
|
|
|
_, ok := priv.Public().(*rsa.PublicKey)
|
2013-07-17 17:33:16 +01:00
|
|
|
if !ok {
|
2015-03-19 11:01:57 +00:00
|
|
|
return nil, errors.New("ECDHE RSA requires a RSA server key")
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown ECDHE signature algorithm")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2015-03-19 11:01:57 +00:00
|
|
|
sig, err = priv.Sign(config.rand(), digest, hashFunc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("failed to sign ECDHE parameters: " + err.Error())
|
|
|
|
}
|
2010-12-16 22:10:50 +00:00
|
|
|
|
|
|
|
skx := new(serverKeyExchangeMsg)
|
2013-07-03 00:58:56 +01:00
|
|
|
sigAndHashLen := 0
|
|
|
|
if ka.version >= VersionTLS12 {
|
|
|
|
sigAndHashLen = 2
|
|
|
|
}
|
|
|
|
skx.key = make([]byte, len(serverECDHParams)+sigAndHashLen+2+len(sig))
|
2010-12-16 22:10:50 +00:00
|
|
|
copy(skx.key, serverECDHParams)
|
|
|
|
k := skx.key[len(serverECDHParams):]
|
2013-07-03 00:58:56 +01:00
|
|
|
if ka.version >= VersionTLS12 {
|
2013-10-21 21:35:09 +01:00
|
|
|
k[0] = tls12HashId
|
2013-07-17 17:33:16 +01:00
|
|
|
k[1] = ka.sigType
|
2013-07-03 00:58:56 +01:00
|
|
|
k = k[2:]
|
|
|
|
}
|
2010-12-16 22:10:50 +00:00
|
|
|
k[0] = byte(len(sig) >> 8)
|
|
|
|
k[1] = byte(len(sig))
|
|
|
|
copy(k[2:], sig)
|
|
|
|
|
|
|
|
return skx, nil
|
|
|
|
}
|
|
|
|
|
2013-07-17 17:33:16 +01:00
|
|
|
func (ka *ecdheKeyAgreement) processClientKeyExchange(config *Config, cert *Certificate, 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 {
|
2014-02-12 16:20:01 +00:00
|
|
|
return nil, errClientKeyExchange
|
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 {
|
2014-02-12 16:20:01 +00:00
|
|
|
return nil, errClientKeyExchange
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2014-07-28 23:46:27 +01:00
|
|
|
if !ka.curve.IsOnCurve(x, y) {
|
|
|
|
return nil, errClientKeyExchange
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2013-07-17 17:33:16 +01:00
|
|
|
func (ka *ecdheKeyAgreement) 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
|
2014-02-24 22:57:51 +00:00
|
|
|
return errors.New("tls: server selected unsupported curve")
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
2014-02-24 22:57:51 +00:00
|
|
|
curveid := CurveID(skx.key[1])<<8 | CurveID(skx.key[2])
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
if ka.curve, ok = curveForCurveID(curveid); !ok {
|
|
|
|
return errors.New("tls: 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
|
|
|
}
|
2014-07-28 23:46:27 +01:00
|
|
|
if !ka.curve.IsOnCurve(ka.x, ka.y) {
|
|
|
|
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
|
|
|
}
|
2013-10-21 21:35:09 +01:00
|
|
|
|
|
|
|
var tls12HashId uint8
|
2013-07-03 00:58:56 +01:00
|
|
|
if ka.version >= VersionTLS12 {
|
2013-10-21 21:35:09 +01:00
|
|
|
// handle SignatureAndHashAlgorithm
|
|
|
|
var sigAndHash []uint8
|
|
|
|
sigAndHash, sig = sig[:2], sig[2:]
|
|
|
|
if sigAndHash[1] != ka.sigType {
|
|
|
|
return errServerKeyExchange
|
|
|
|
}
|
|
|
|
tls12HashId = sigAndHash[0]
|
2013-07-03 00:58:56 +01:00
|
|
|
if len(sig) < 2 {
|
|
|
|
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:]
|
|
|
|
|
2013-10-21 21:35:09 +01:00
|
|
|
digest, hashFunc, err := hashForServerKeyExchange(ka.sigType, tls12HashId, ka.version, clientHello.random, serverHello.random, serverECDHParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-17 17:33:16 +01:00
|
|
|
switch ka.sigType {
|
|
|
|
case signatureECDSA:
|
|
|
|
pubKey, ok := cert.PublicKey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("ECDHE ECDSA requires a ECDSA server public key")
|
|
|
|
}
|
|
|
|
ecdsaSig := new(ecdsaSignature)
|
|
|
|
if _, err := asn1.Unmarshal(sig, ecdsaSig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
|
|
|
|
return errors.New("ECDSA signature contained zero or negative values")
|
|
|
|
}
|
|
|
|
if !ecdsa.Verify(pubKey, digest, ecdsaSig.R, ecdsaSig.S) {
|
|
|
|
return errors.New("ECDSA verification failure")
|
|
|
|
}
|
|
|
|
case signatureRSA:
|
|
|
|
pubKey, ok := cert.PublicKey.(*rsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("ECDHE RSA requires a RSA server public key")
|
|
|
|
}
|
|
|
|
if err := rsa.VerifyPKCS1v15(pubKey, hashFunc, digest, sig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("unknown ECDHE signature algorithm")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:33:16 +01:00
|
|
|
func (ka *ecdheKeyAgreement) 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
|
|
|
|
}
|