2014-06-20 20:00:00 +01: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.
|
|
|
|
|
2015-09-29 23:21:04 +01:00
|
|
|
package runner
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
import (
|
2016-07-01 22:50:32 +01:00
|
|
|
"crypto"
|
2014-06-20 20:00:00 +01:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/des"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
2014-08-31 07:06:47 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
2014-06-20 20:00:00 +01:00
|
|
|
"crypto/x509"
|
|
|
|
"hash"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2018-11-12 21:53:42 +00:00
|
|
|
generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg, uint16) (*serverKeyExchangeMsg, error)
|
2014-06-20 20:00:00 +01:00
|
|
|
processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
|
|
|
|
|
|
|
|
// 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.
|
2018-07-17 20:49:41 +01:00
|
|
|
processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, crypto.PublicKey, *serverKeyExchangeMsg) error
|
2014-06-20 20:00:00 +01:00
|
|
|
generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
|
2016-06-21 23:19:24 +01:00
|
|
|
|
|
|
|
// peerSignatureAlgorithm returns the signature algorithm used by the
|
|
|
|
// peer, or zero if not applicable.
|
|
|
|
peerSignatureAlgorithm() signatureAlgorithm
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// suiteECDH indicates that the cipher suite involves elliptic curve
|
|
|
|
// Diffie-Hellman. This means that it should only be selected when the
|
|
|
|
// client indicates that it supports ECC with a curve and point format
|
|
|
|
// that we're happy with.
|
|
|
|
suiteECDHE = 1 << iota
|
|
|
|
// suiteECDSA indicates that the cipher suite involves an ECDSA
|
|
|
|
// signature and therefore may only be selected when the server's
|
|
|
|
// certificate is ECDSA. If this is not set then the cipher suite is
|
|
|
|
// RSA based.
|
|
|
|
suiteECDSA
|
|
|
|
// suiteTLS12 indicates that the cipher suite should only be advertised
|
2016-06-15 02:14:35 +01:00
|
|
|
// and accepted when using TLS 1.2 or greater.
|
2014-06-20 20:00:00 +01:00
|
|
|
suiteTLS12
|
2016-06-15 02:14:35 +01:00
|
|
|
// suiteTLS13 indicates that the cipher suite can be used with TLS 1.3.
|
|
|
|
// Cipher suites lacking this flag may not be used with TLS 1.3.
|
|
|
|
suiteTLS13
|
2014-06-20 20:00:00 +01:00
|
|
|
// suiteSHA384 indicates that the cipher suite uses SHA384 as the
|
|
|
|
// handshake hash.
|
|
|
|
suiteSHA384
|
2014-10-27 05:06:24 +00:00
|
|
|
// suitePSK indicates that the cipher suite authenticates with
|
|
|
|
// a pre-shared key rather than a server private key.
|
|
|
|
suitePSK
|
2014-06-20 20:00:00 +01:00
|
|
|
)
|
|
|
|
|
2015-04-07 05:46:46 +01:00
|
|
|
type tlsAead struct {
|
|
|
|
cipher.AEAD
|
|
|
|
explicitNonce bool
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01: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 {
|
|
|
|
id uint16
|
|
|
|
// the lengths, in bytes, of the key material needed for each component.
|
|
|
|
keyLen int
|
|
|
|
macLen int
|
2016-06-15 02:14:35 +01:00
|
|
|
ivLen func(version uint16) int
|
2014-06-20 20:00:00 +01:00
|
|
|
ka func(version uint16) keyAgreement
|
|
|
|
// flags is a bitmask of the suite* values, above.
|
|
|
|
flags int
|
|
|
|
cipher func(key, iv []byte, isRead bool) interface{}
|
|
|
|
mac func(version uint16, macKey []byte) macFunction
|
2016-06-15 02:14:35 +01:00
|
|
|
aead func(version uint16, key, fixedNonce []byte) *tlsAead
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 22:50:32 +01:00
|
|
|
func (cs cipherSuite) hash() crypto.Hash {
|
|
|
|
if cs.flags&suiteSHA384 != 0 {
|
|
|
|
return crypto.SHA384
|
|
|
|
}
|
|
|
|
return crypto.SHA256
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
var cipherSuites = []*cipherSuite{
|
2016-09-06 19:13:43 +01:00
|
|
|
{TLS_CHACHA20_POLY1305_SHA256, 32, 0, ivLenChaCha20Poly1305, nil, suiteTLS13, nil, nil, aeadCHACHA20POLY1305},
|
|
|
|
{TLS_AES_128_GCM_SHA256, 16, 0, ivLenAESGCM, nil, suiteTLS13, nil, nil, aeadAESGCM},
|
|
|
|
{TLS_AES_256_GCM_SHA384, 32, 0, ivLenAESGCM, nil, suiteTLS13 | suiteSHA384, nil, nil, aeadAESGCM},
|
|
|
|
{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, ivLenChaCha20Poly1305, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadCHACHA20POLY1305},
|
|
|
|
{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, ivLenChaCha20Poly1305, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadCHACHA20POLY1305},
|
|
|
|
{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, ivLenAESGCM, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
|
|
|
|
{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, ivLenAESGCM, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
|
|
|
|
{TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, ivLenAESGCM, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
|
|
|
|
{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, ivLenAESGCM, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
|
2016-06-15 02:14:35 +01:00
|
|
|
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, ivLenAES, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
|
|
|
|
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, ivLenAES, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, cipherAES, macSHA256, nil},
|
|
|
|
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, ivLenAES, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, ivLenAES, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, ivLenAES, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil},
|
|
|
|
{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, ivLenAES, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil},
|
|
|
|
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, ivLenAES, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, ivLenAES, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, ivLenAESGCM, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
|
|
|
|
{TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, ivLenAESGCM, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
|
|
|
|
{TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, ivLenAES, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
|
|
|
|
{TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, ivLenAES, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
|
|
|
|
{TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, ivLenAES, rsaKA, 0, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, ivLenAES, rsaKA, 0, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, ivLen3DES, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
|
|
|
|
{TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, ivLen3DES, rsaKA, 0, cipher3DES, macSHA1, nil},
|
2016-09-06 19:13:43 +01:00
|
|
|
{TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 32, 0, ivLenChaCha20Poly1305, ecdhePSKKA, suiteECDHE | suitePSK | suiteTLS12, nil, nil, aeadCHACHA20POLY1305},
|
2016-06-15 02:14:35 +01:00
|
|
|
{TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, 16, 20, ivLenAES, ecdhePSKKA, suiteECDHE | suitePSK, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, 32, 20, ivLenAES, ecdhePSKKA, suiteECDHE | suitePSK, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_PSK_WITH_AES_128_CBC_SHA, 16, 20, ivLenAES, pskKA, suitePSK, cipherAES, macSHA1, nil},
|
|
|
|
{TLS_PSK_WITH_AES_256_CBC_SHA, 32, 20, ivLenAES, pskKA, suitePSK, cipherAES, macSHA1, nil},
|
2017-10-28 21:08:58 +01:00
|
|
|
{TLS_RSA_WITH_NULL_SHA, 0, 20, noIV, rsaKA, 0, cipherNull, macSHA1, nil},
|
2016-06-15 02:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func noIV(vers uint16) int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func ivLenChaCha20Poly1305(vers uint16) int {
|
|
|
|
return 12
|
|
|
|
}
|
|
|
|
|
|
|
|
func ivLenAESGCM(vers uint16) int {
|
|
|
|
if vers >= VersionTLS13 {
|
|
|
|
return 12
|
|
|
|
}
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
|
|
|
|
func ivLenAES(vers uint16) int {
|
|
|
|
return 16
|
|
|
|
}
|
|
|
|
|
|
|
|
func ivLen3DES(vers uint16) int {
|
|
|
|
return 8
|
2015-09-03 03:48:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type nullCipher struct{}
|
|
|
|
|
|
|
|
func cipherNull(key, iv []byte, isRead bool) interface{} {
|
|
|
|
return nullCipher{}
|
2014-06-20 20:00:00 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cipherAES(key, iv []byte, isRead bool) interface{} {
|
|
|
|
block, _ := aes.NewCipher(key)
|
|
|
|
if isRead {
|
|
|
|
return cipher.NewCBCDecrypter(block, iv)
|
|
|
|
}
|
|
|
|
return cipher.NewCBCEncrypter(block, iv)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
return tls10MAC{hmac.New(sha1.New, key)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func macMD5(version uint16, key []byte) macFunction {
|
|
|
|
if version == VersionSSL30 {
|
|
|
|
mac := ssl30MAC{
|
|
|
|
h: md5.New(),
|
|
|
|
key: make([]byte, len(key)),
|
|
|
|
}
|
|
|
|
copy(mac.key, key)
|
|
|
|
return mac
|
|
|
|
}
|
|
|
|
return tls10MAC{hmac.New(md5.New, key)}
|
|
|
|
}
|
|
|
|
|
2014-08-31 07:06:47 +01:00
|
|
|
func macSHA256(version uint16, key []byte) macFunction {
|
|
|
|
if version == VersionSSL30 {
|
|
|
|
mac := ssl30MAC{
|
|
|
|
h: sha256.New(),
|
|
|
|
key: make([]byte, len(key)),
|
|
|
|
}
|
|
|
|
copy(mac.key, key)
|
|
|
|
return mac
|
|
|
|
}
|
|
|
|
return tls10MAC{hmac.New(sha256.New, key)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func macSHA384(version uint16, key []byte) macFunction {
|
|
|
|
if version == VersionSSL30 {
|
|
|
|
mac := ssl30MAC{
|
|
|
|
h: sha512.New384(),
|
|
|
|
key: make([]byte, len(key)),
|
|
|
|
}
|
|
|
|
copy(mac.key, key)
|
|
|
|
return mac
|
|
|
|
}
|
|
|
|
return tls10MAC{hmac.New(sha512.New384, key)}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
type macFunction interface {
|
|
|
|
Size() int
|
2014-08-04 06:23:53 +01:00
|
|
|
MAC(digestBuf, seq, header, length, data []byte) []byte
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
|
|
|
|
// each call.
|
|
|
|
type fixedNonceAEAD struct {
|
|
|
|
// sealNonce and openNonce are buffers where the larger nonce will be
|
|
|
|
// constructed. Since a seal and open operation may be running
|
|
|
|
// concurrently, there is a separate buffer for each.
|
|
|
|
sealNonce, openNonce []byte
|
|
|
|
aead cipher.AEAD
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fixedNonceAEAD) NonceSize() int { return 8 }
|
|
|
|
func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
|
|
|
|
|
|
|
|
func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
|
|
|
|
copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
|
|
|
|
return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
|
|
|
|
copy(f.openNonce[len(f.openNonce)-8:], nonce)
|
|
|
|
return f.aead.Open(out, f.openNonce, plaintext, additionalData)
|
|
|
|
}
|
|
|
|
|
2016-06-15 02:14:35 +01:00
|
|
|
func aeadAESGCM(version uint16, key, fixedNonce []byte) *tlsAead {
|
2014-06-20 20:00:00 +01:00
|
|
|
aes, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
aead, err := cipher.NewGCM(aes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
|
|
|
|
copy(nonce1, fixedNonce)
|
|
|
|
copy(nonce2, fixedNonce)
|
|
|
|
|
2016-06-15 02:14:35 +01:00
|
|
|
if version >= VersionTLS13 {
|
|
|
|
return &tlsAead{&xorNonceAEAD{nonce1, nonce2, aead}, false}
|
|
|
|
}
|
|
|
|
|
2015-04-07 05:46:46 +01:00
|
|
|
return &tlsAead{&fixedNonceAEAD{nonce1, nonce2, aead}, true}
|
|
|
|
}
|
|
|
|
|
2015-12-10 03:13:03 +00:00
|
|
|
func xorSlice(out, in []byte) {
|
|
|
|
for i := range out {
|
|
|
|
out[i] ^= in[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// xorNonceAEAD wraps an AEAD and XORs a fixed portion of the nonce, left-padded
|
|
|
|
// if necessary, each call.
|
|
|
|
type xorNonceAEAD struct {
|
|
|
|
// sealNonce and openNonce are buffers where the larger nonce will be
|
|
|
|
// constructed. Since a seal and open operation may be running
|
|
|
|
// concurrently, there is a separate buffer for each.
|
|
|
|
sealNonce, openNonce []byte
|
|
|
|
aead cipher.AEAD
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *xorNonceAEAD) NonceSize() int { return 8 }
|
|
|
|
func (x *xorNonceAEAD) Overhead() int { return x.aead.Overhead() }
|
|
|
|
|
|
|
|
func (x *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
|
|
|
|
xorSlice(x.sealNonce[len(x.sealNonce)-len(nonce):], nonce)
|
|
|
|
ret := x.aead.Seal(out, x.sealNonce, plaintext, additionalData)
|
|
|
|
xorSlice(x.sealNonce[len(x.sealNonce)-len(nonce):], nonce)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *xorNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
|
|
|
|
xorSlice(x.openNonce[len(x.openNonce)-len(nonce):], nonce)
|
|
|
|
ret, err := x.aead.Open(out, x.openNonce, plaintext, additionalData)
|
|
|
|
xorSlice(x.openNonce[len(x.openNonce)-len(nonce):], nonce)
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2016-06-15 02:14:35 +01:00
|
|
|
func aeadCHACHA20POLY1305(version uint16, key, fixedNonce []byte) *tlsAead {
|
2015-12-10 03:13:03 +00:00
|
|
|
aead, err := newChaCha20Poly1305(key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce1, nonce2 := make([]byte, len(fixedNonce)), make([]byte, len(fixedNonce))
|
|
|
|
copy(nonce1, fixedNonce)
|
|
|
|
copy(nonce2, fixedNonce)
|
|
|
|
|
|
|
|
return &tlsAead{&xorNonceAEAD{nonce1, nonce2, aead}, false}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +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}
|
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
func (s ssl30MAC) MAC(digestBuf, seq, header, length, data []byte) []byte {
|
2014-06-20 20:00:00 +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(header[:1])
|
2014-08-04 06:23:53 +01:00
|
|
|
s.h.Write(length)
|
2014-06-20 20:00:00 +01:00
|
|
|
s.h.Write(data)
|
|
|
|
digestBuf = s.h.Sum(digestBuf[:0])
|
|
|
|
|
|
|
|
s.h.Reset()
|
|
|
|
s.h.Write(s.key)
|
|
|
|
s.h.Write(ssl30Pad2[:padLength])
|
|
|
|
s.h.Write(digestBuf)
|
|
|
|
return s.h.Sum(digestBuf[:0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
func (s tls10MAC) MAC(digestBuf, seq, header, length, data []byte) []byte {
|
2014-06-20 20:00:00 +01:00
|
|
|
s.h.Reset()
|
|
|
|
s.h.Write(seq)
|
|
|
|
s.h.Write(header)
|
2014-08-04 06:23:53 +01:00
|
|
|
s.h.Write(length)
|
2014-06-20 20:00:00 +01:00
|
|
|
s.h.Write(data)
|
|
|
|
return s.h.Sum(digestBuf[:0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func rsaKA(version uint16) keyAgreement {
|
2015-02-26 20:16:22 +00:00
|
|
|
return &rsaKeyAgreement{version: version}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ecdheECDSAKA(version uint16) keyAgreement {
|
|
|
|
return &ecdheKeyAgreement{
|
2014-10-27 06:23:15 +00:00
|
|
|
auth: &signedKeyAgreement{
|
2016-06-21 23:19:24 +01:00
|
|
|
keyType: keyTypeECDSA,
|
2014-08-02 22:35:45 +01:00
|
|
|
version: version,
|
|
|
|
},
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ecdheRSAKA(version uint16) keyAgreement {
|
|
|
|
return &ecdheKeyAgreement{
|
2014-10-27 06:23:15 +00:00
|
|
|
auth: &signedKeyAgreement{
|
2016-06-21 23:19:24 +01:00
|
|
|
keyType: keyTypeRSA,
|
2014-08-02 22:35:45 +01:00
|
|
|
version: version,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 05:06:24 +00:00
|
|
|
func pskKA(version uint16) keyAgreement {
|
2014-10-27 06:23:15 +00:00
|
|
|
return &pskKeyAgreement{
|
|
|
|
base: &nilKeyAgreement{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ecdhePSKKA(version uint16) keyAgreement {
|
|
|
|
return &pskKeyAgreement{
|
|
|
|
base: &ecdheKeyAgreement{
|
|
|
|
auth: &nilKeyAgreementAuthentication{},
|
|
|
|
},
|
|
|
|
}
|
2014-10-27 05:06:24 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
// mutualCipherSuite returns a cipherSuite given a list of supported
|
|
|
|
// ciphersuites and the id requested by the peer.
|
|
|
|
func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
|
|
|
|
for _, id := range have {
|
|
|
|
if id == want {
|
2016-11-01 17:39:36 +00:00
|
|
|
return cipherSuiteFromID(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cipherSuiteFromID(id uint16) *cipherSuite {
|
|
|
|
for _, suite := range cipherSuites {
|
|
|
|
if suite.id == id {
|
|
|
|
return suite
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A list of the possible cipher suite ids. Taken from
|
|
|
|
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
|
|
|
|
const (
|
2015-12-10 03:13:03 +00:00
|
|
|
TLS_RSA_WITH_NULL_SHA uint16 = 0x0002
|
|
|
|
TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
|
|
|
|
TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
|
|
|
|
TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
|
|
|
|
TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
|
|
|
|
TLS_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x003d
|
|
|
|
TLS_PSK_WITH_AES_128_CBC_SHA uint16 = 0x008c
|
|
|
|
TLS_PSK_WITH_AES_256_CBC_SHA uint16 = 0x008d
|
|
|
|
TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
|
|
|
|
TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
|
|
|
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
|
|
|
|
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
|
|
|
|
TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
|
|
|
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
|
|
|
|
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc024
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc028
|
|
|
|
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
|
|
|
|
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
|
|
|
|
TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA uint16 = 0xc035
|
|
|
|
TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA uint16 = 0xc036
|
|
|
|
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca8
|
|
|
|
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
|
|
|
|
TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xccac
|
|
|
|
renegotiationSCSV uint16 = 0x00ff
|
|
|
|
fallbackSCSV uint16 = 0x5600
|
2014-06-20 20:00:00 +01:00
|
|
|
)
|
2014-10-27 06:23:15 +00:00
|
|
|
|
|
|
|
// Additional cipher suite IDs, not IANA-assigned.
|
|
|
|
const (
|
2017-01-24 00:28:12 +00:00
|
|
|
TLS_AES_128_GCM_SHA256 uint16 = 0x1301
|
|
|
|
TLS_AES_256_GCM_SHA384 uint16 = 0x1302
|
|
|
|
TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
|
2014-10-27 06:23:15 +00:00
|
|
|
)
|