2010-12-15 16:49:55 +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 (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2011-10-11 18:07:32 +01:00
|
|
|
"crypto/des"
|
2010-12-15 16:49:55 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rc4"
|
2011-09-14 20:32:19 +01:00
|
|
|
"crypto/sha1"
|
2010-12-16 22:10:50 +00:00
|
|
|
"crypto/x509"
|
2010-12-15 16:49:55 +00:00
|
|
|
"hash"
|
|
|
|
)
|
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
// a keyAgreement implements the client and server side of a TLS key agreement
|
|
|
|
// protocol by generating and processing key exchange messages.
|
|
|
|
type keyAgreement interface {
|
|
|
|
// On the server side, the first two methods are called in order.
|
|
|
|
|
|
|
|
// In the case that the key agreement protocol doesn't use a
|
|
|
|
// ServerKeyExchange message, generateServerKeyExchange can return nil,
|
|
|
|
// nil.
|
2012-04-12 17:35:21 +01:00
|
|
|
generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
|
|
|
|
processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
|
2010-12-16 22:10:50 +00:00
|
|
|
|
|
|
|
// On the client side, the next two methods are called in order.
|
|
|
|
|
|
|
|
// This method may not be called if the server doesn't send a
|
|
|
|
// ServerKeyExchange message.
|
2011-11-02 02:04:37 +00:00
|
|
|
processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
|
|
|
|
generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
|
2010-12-16 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
// A cipherSuite is a specific combination of key agreement, cipher and MAC
|
|
|
|
// function. All cipher suites currently assume RSA key agreement.
|
|
|
|
type cipherSuite struct {
|
2011-11-28 20:34:16 +00:00
|
|
|
id uint16
|
2010-12-15 16:49:55 +00:00
|
|
|
// the lengths, in bytes, of the key material needed for each component.
|
2010-12-15 18:58:57 +00:00
|
|
|
keyLen int
|
|
|
|
macLen int
|
|
|
|
ivLen int
|
2010-12-16 22:10:50 +00:00
|
|
|
ka func() keyAgreement
|
|
|
|
// If elliptic is set, a server will only consider this ciphersuite if
|
|
|
|
// the ClientHello indicated that the client supports an elliptic curve
|
|
|
|
// and point format that we can handle.
|
|
|
|
elliptic bool
|
|
|
|
cipher func(key, iv []byte, isRead bool) interface{}
|
2011-09-14 20:32:19 +01:00
|
|
|
mac func(version uint16, macKey []byte) macFunction
|
2010-12-15 16:49:55 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 20:34:16 +00:00
|
|
|
var cipherSuites = []*cipherSuite{
|
2011-12-02 19:14:25 +00:00
|
|
|
{TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, false, cipherRC4, macSHA1},
|
|
|
|
{TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, false, cipher3DES, macSHA1},
|
|
|
|
{TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, false, cipherAES, macSHA1},
|
2012-05-18 16:06:58 +01:00
|
|
|
{TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, false, cipherAES, macSHA1},
|
2011-12-02 19:14:25 +00:00
|
|
|
{TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, true, cipherRC4, macSHA1},
|
|
|
|
{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, true, cipher3DES, macSHA1},
|
|
|
|
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, true, cipherAES, macSHA1},
|
2012-05-18 16:06:58 +01:00
|
|
|
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, true, cipherAES, macSHA1},
|
2010-12-15 16:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cipherRC4(key, iv []byte, isRead bool) interface{} {
|
|
|
|
cipher, _ := rc4.NewCipher(key)
|
|
|
|
return cipher
|
|
|
|
}
|
|
|
|
|
2011-10-11 18:07:32 +01:00
|
|
|
func cipher3DES(key, iv []byte, isRead bool) interface{} {
|
|
|
|
block, _ := des.NewTripleDESCipher(key)
|
|
|
|
if isRead {
|
|
|
|
return cipher.NewCBCDecrypter(block, iv)
|
|
|
|
}
|
|
|
|
return cipher.NewCBCEncrypter(block, iv)
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
func cipherAES(key, iv []byte, isRead bool) interface{} {
|
|
|
|
block, _ := aes.NewCipher(key)
|
|
|
|
if isRead {
|
|
|
|
return cipher.NewCBCDecrypter(block, iv)
|
|
|
|
}
|
|
|
|
return cipher.NewCBCEncrypter(block, iv)
|
|
|
|
}
|
|
|
|
|
2011-09-14 20:32:19 +01:00
|
|
|
// macSHA1 returns a macFunction for the given protocol version.
|
|
|
|
func macSHA1(version uint16, key []byte) macFunction {
|
|
|
|
if version == versionSSL30 {
|
|
|
|
mac := ssl30MAC{
|
|
|
|
h: sha1.New(),
|
|
|
|
key: make([]byte, len(key)),
|
|
|
|
}
|
|
|
|
copy(mac.key, key)
|
|
|
|
return mac
|
|
|
|
}
|
2012-01-19 22:28:38 +00:00
|
|
|
return tls10MAC{hmac.New(sha1.New, key)}
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type macFunction interface {
|
|
|
|
Size() int
|
2011-12-06 23:25:14 +00:00
|
|
|
MAC(digestBuf, seq, data []byte) []byte
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ssl30MAC implements the SSLv3 MAC function, as defined in
|
|
|
|
// www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
|
|
|
|
type ssl30MAC struct {
|
|
|
|
h hash.Hash
|
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ssl30MAC) Size() int {
|
|
|
|
return s.h.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
var ssl30Pad1 = [48]byte{0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36}
|
|
|
|
|
|
|
|
var ssl30Pad2 = [48]byte{0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c}
|
|
|
|
|
2011-12-06 23:25:14 +00:00
|
|
|
func (s ssl30MAC) MAC(digestBuf, seq, record []byte) []byte {
|
2011-09-14 20:32:19 +01:00
|
|
|
padLength := 48
|
|
|
|
if s.h.Size() == 20 {
|
|
|
|
padLength = 40
|
|
|
|
}
|
|
|
|
|
|
|
|
s.h.Reset()
|
|
|
|
s.h.Write(s.key)
|
|
|
|
s.h.Write(ssl30Pad1[:padLength])
|
|
|
|
s.h.Write(seq)
|
|
|
|
s.h.Write(record[:1])
|
|
|
|
s.h.Write(record[3:5])
|
|
|
|
s.h.Write(record[recordHeaderLen:])
|
2011-12-06 23:25:14 +00:00
|
|
|
digestBuf = s.h.Sum(digestBuf[:0])
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
s.h.Reset()
|
|
|
|
s.h.Write(s.key)
|
|
|
|
s.h.Write(ssl30Pad2[:padLength])
|
2011-12-06 23:25:14 +00:00
|
|
|
s.h.Write(digestBuf)
|
|
|
|
return s.h.Sum(digestBuf[:0])
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
|
|
|
|
type tls10MAC struct {
|
|
|
|
h hash.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s tls10MAC) Size() int {
|
|
|
|
return s.h.Size()
|
|
|
|
}
|
|
|
|
|
2011-12-06 23:25:14 +00:00
|
|
|
func (s tls10MAC) MAC(digestBuf, seq, record []byte) []byte {
|
2011-09-14 20:32:19 +01:00
|
|
|
s.h.Reset()
|
|
|
|
s.h.Write(seq)
|
|
|
|
s.h.Write(record)
|
2011-12-06 23:25:14 +00:00
|
|
|
return s.h.Sum(digestBuf[:0])
|
2010-12-15 16:49:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-16 22:10:50 +00:00
|
|
|
func rsaKA() keyAgreement {
|
|
|
|
return rsaKeyAgreement{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ecdheRSAKA() keyAgreement {
|
|
|
|
return new(ecdheRSAKeyAgreement)
|
|
|
|
}
|
|
|
|
|
2011-11-28 20:34:16 +00:00
|
|
|
// mutualCipherSuite returns a cipherSuite given a list of supported
|
2010-12-15 16:49:55 +00:00
|
|
|
// ciphersuites and the id requested by the peer.
|
2011-11-28 20:34:16 +00:00
|
|
|
func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
|
2010-12-15 16:49:55 +00:00
|
|
|
for _, id := range have {
|
2010-12-15 18:58:57 +00:00
|
|
|
if id == want {
|
2011-11-28 20:34:16 +00:00
|
|
|
for _, suite := range cipherSuites {
|
|
|
|
if suite.id == want {
|
|
|
|
return suite
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2010-12-15 16:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-28 20:34:16 +00:00
|
|
|
return nil
|
2010-12-15 16:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A list of the possible cipher suite ids. Taken from
|
|
|
|
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
|
|
|
|
const (
|
2011-10-11 18:07:32 +01:00
|
|
|
TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
|
|
|
|
TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
|
|
|
|
TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
|
2012-05-18 16:06:58 +01:00
|
|
|
TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
|
2011-10-11 18:07:32 +01:00
|
|
|
TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
|
|
|
|
TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
|
2012-05-18 16:06:58 +01:00
|
|
|
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
|
2010-12-15 16:49:55 +00:00
|
|
|
)
|