| 1 |
// Copyright 2010 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package runner |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/aes" |
| 9 |
"crypto/cipher" |
| 10 |
"crypto/des" |
| 11 |
"crypto/hmac" |
| 12 |
"crypto/md5" |
| 13 |
"crypto/rc4" |
| 14 |
"crypto/sha1" |
| 15 |
"crypto/sha256" |
| 16 |
"crypto/sha512" |
| 17 |
"crypto/x509" |
| 18 |
"hash" |
| 19 |
) |
| 20 |
|
| 21 |
// a keyAgreement implements the client and server side of a TLS key agreement |
| 22 |
// protocol by generating and processing key exchange messages. |
| 23 |
type keyAgreement interface { |
| 24 |
// On the server side, the first two methods are called in order. |
| 25 |
|
| 26 |
// In the case that the key agreement protocol doesn't use a |
| 27 |
// ServerKeyExchange message, generateServerKeyExchange can return nil, |
| 28 |
// nil. |
| 29 |
generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error) |
| 30 |
processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error) |
| 31 |
|
| 32 |
// On the client side, the next two methods are called in order. |
| 33 |
|
| 34 |
// This method may not be called if the server doesn't send a |
| 35 |
// ServerKeyExchange message. |
| 36 |
processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error |
| 37 |
generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error) |
| 38 |
} |
| 39 |
|
| 40 |
const ( |
| 41 |
// suiteECDH indicates that the cipher suite involves elliptic curve |
| 42 |
// Diffie-Hellman. This means that it should only be selected when the |
| 43 |
// client indicates that it supports ECC with a curve and point format |
| 44 |
// that we're happy with. |
| 45 |
suiteECDHE = 1 << iota |
| 46 |
// suiteCECPQ1 indicates that the cipher suite uses the |
| 47 |
// experimental, temporary, and non-standard CECPQ1 key agreement. |
| 48 |
suiteCECPQ1 |
| 49 |
// suiteECDSA indicates that the cipher suite involves an ECDSA |
| 50 |
// signature and therefore may only be selected when the server's |
| 51 |
// certificate is ECDSA. If this is not set then the cipher suite is |
| 52 |
// RSA based. |
| 53 |
suiteECDSA |
| 54 |
// suiteTLS12 indicates that the cipher suite should only be advertised |
| 55 |
// and accepted when using TLS 1.2. |
| 56 |
suiteTLS12 |
| 57 |
// suiteSHA384 indicates that the cipher suite uses SHA384 as the |
| 58 |
// handshake hash. |
| 59 |
suiteSHA384 |
| 60 |
// suiteNoDTLS indicates that the cipher suite cannot be used |
| 61 |
// in DTLS. |
| 62 |
suiteNoDTLS |
| 63 |
// suitePSK indicates that the cipher suite authenticates with |
| 64 |
// a pre-shared key rather than a server private key. |
| 65 |
suitePSK |
| 66 |
) |
| 67 |
|
| 68 |
type tlsAead struct { |
| 69 |
cipher.AEAD |
| 70 |
explicitNonce bool |
| 71 |
} |
| 72 |
|
| 73 |
// A cipherSuite is a specific combination of key agreement, cipher and MAC |
| 74 |
// function. All cipher suites currently assume RSA key agreement. |
| 75 |
type cipherSuite struct { |
| 76 |
id uint16 |
| 77 |
// the lengths, in bytes, of the key material needed for each component. |
| 78 |
keyLen int |
| 79 |
macLen int |
| 80 |
ivLen int |
| 81 |
ka func(version uint16) keyAgreement |
| 82 |
// flags is a bitmask of the suite* values, above. |
| 83 |
flags int |
| 84 |
cipher func(key, iv []byte, isRead bool) interface{} |
| 85 |
mac func(version uint16, macKey []byte) macFunction |
| 86 |
aead func(key, fixedNonce []byte) *tlsAead |
| 87 |
} |
| 88 |
|
| 89 |
var cipherSuites = []*cipherSuite{ |
| 90 |
// Ciphersuite order is chosen so that ECDHE comes before plain RSA |
| 91 |
// and RC4 comes before AES (because of the Lucky13 attack). |
| 92 |
{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadCHACHA20POLY1305}, |
| 93 |
{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadCHACHA20POLY1305}, |
| 94 |
{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256_OLD, 32, 0, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadCHACHA20POLY1305Old}, |
| 95 |
{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256_OLD, 32, 0, 0, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadCHACHA20POLY1305Old}, |
| 96 |
{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM}, |
| 97 |
{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM}, |
| 98 |
{TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM}, |
| 99 |
{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM}, |
| 100 |
{TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteNoDTLS, cipherRC4, macSHA1, nil}, |
| 101 |
{TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteNoDTLS, cipherRC4, macSHA1, nil}, |
| 102 |
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil}, |
| 103 |
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, cipherAES, macSHA256, nil}, |
| 104 |
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil}, |
| 105 |
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil}, |
| 106 |
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil}, |
| 107 |
{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil}, |
| 108 |
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil}, |
| 109 |
{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil}, |
| 110 |
{TLS_CECPQ1_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, cecpq1RSAKA, suiteCECPQ1 | suiteTLS12, nil, nil, aeadCHACHA20POLY1305}, |
| 111 |
{TLS_CECPQ1_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, cecpq1ECDSAKA, suiteCECPQ1 | suiteECDSA | suiteTLS12, nil, nil, aeadCHACHA20POLY1305}, |
| 112 |
{TLS_CECPQ1_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, cecpq1RSAKA, suiteCECPQ1 | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM}, |
| 113 |
{TLS_CECPQ1_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, cecpq1ECDSAKA, suiteCECPQ1 | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM}, |
| 114 |
{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, dheRSAKA, suiteTLS12, nil, nil, aeadAESGCM}, |
| 115 |
{TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, dheRSAKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM}, |
| 116 |
{TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, dheRSAKA, suiteTLS12, cipherAES, macSHA256, nil}, |
| 117 |
{TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, dheRSAKA, suiteTLS12, cipherAES, macSHA256, nil}, |
| 118 |
{TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil}, |
| 119 |
{TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil}, |
| 120 |
{TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM}, |
| 121 |
{TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM}, |
| 122 |
{TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteNoDTLS, cipherRC4, macSHA1, nil}, |
| 123 |
{TLS_RSA_WITH_RC4_128_MD5, 16, 16, 0, rsaKA, suiteNoDTLS, cipherRC4, macMD5, nil}, |
| 124 |
{TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil}, |
| 125 |
{TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil}, |
| 126 |
{TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil}, |
| 127 |
{TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil}, |
| 128 |
{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil}, |
| 129 |
{TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, dheRSAKA, 0, cipher3DES, macSHA1, nil}, |
| 130 |
{TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil}, |
| 131 |
{TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 12, ecdhePSKKA, suiteECDHE | suitePSK | suiteTLS12, nil, nil, aeadCHACHA20POLY1305}, |
| 132 |
{TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdhePSKKA, suiteECDHE | suitePSK | suiteTLS12, nil, nil, aeadAESGCM}, |
| 133 |
{TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdhePSKKA, suiteECDHE | suitePSK | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM}, |
| 134 |
{TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdhePSKKA, suiteECDHE | suitePSK, cipherAES, macSHA1, nil}, |
| 135 |
{TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdhePSKKA, suiteECDHE | suitePSK, cipherAES, macSHA1, nil}, |
| 136 |
{TLS_PSK_WITH_RC4_128_SHA, 16, 20, 0, pskKA, suiteNoDTLS | suitePSK, cipherRC4, macSHA1, nil}, |
| 137 |
{TLS_PSK_WITH_AES_128_CBC_SHA, 16, 20, 16, pskKA, suitePSK, cipherAES, macSHA1, nil}, |
| 138 |
{TLS_PSK_WITH_AES_256_CBC_SHA, 32, 20, 16, pskKA, suitePSK, cipherAES, macSHA1, nil}, |
| 139 |
{TLS_RSA_WITH_NULL_SHA, 0, 20, 0, rsaKA, suiteNoDTLS, cipherNull, macSHA1, nil}, |
| 140 |
} |
| 141 |
|
| 142 |
type nullCipher struct{} |
| 143 |
|
| 144 |
func cipherNull(key, iv []byte, isRead bool) interface{} { |
| 145 |
return nullCipher{} |
| 146 |
} |
| 147 |
|
| 148 |
func cipherRC4(key, iv []byte, isRead bool) interface{} { |
| 149 |
cipher, _ := rc4.NewCipher(key) |
| 150 |
return cipher |
| 151 |
} |
| 152 |
|
| 153 |
func cipher3DES(key, iv []byte, isRead bool) interface{} { |
| 154 |
block, _ := des.NewTripleDESCipher(key) |
| 155 |
if isRead { |
| 156 |
return cipher.NewCBCDecrypter(block, iv) |
| 157 |
} |
| 158 |
return cipher.NewCBCEncrypter(block, iv) |
| 159 |
} |
| 160 |
|
| 161 |
func cipherAES(key, iv []byte, isRead bool) interface{} { |
| 162 |
block, _ := aes.NewCipher(key) |
| 163 |
if isRead { |
| 164 |
return cipher.NewCBCDecrypter(block, iv) |
| 165 |
} |
| 166 |
return cipher.NewCBCEncrypter(block, iv) |
| 167 |
} |
| 168 |
|
| 169 |
// macSHA1 returns a macFunction for the given protocol version. |
| 170 |
func macSHA1(version uint16, key []byte) macFunction { |
| 171 |
if version == VersionSSL30 { |
| 172 |
mac := ssl30MAC{ |
| 173 |
h: sha1.New(), |
| 174 |
key: make([]byte, len(key)), |
| 175 |
} |
| 176 |
copy(mac.key, key) |
| 177 |
return mac |
| 178 |
} |
| 179 |
return tls10MAC{hmac.New(sha1.New, key)} |
| 180 |
} |
| 181 |
|
| 182 |
func macMD5(version uint16, key []byte) macFunction { |
| 183 |
if version == VersionSSL30 { |
| 184 |
mac := ssl30MAC{ |
| 185 |
h: md5.New(), |
| 186 |
key: make([]byte, len(key)), |
| 187 |
} |
| 188 |
copy(mac.key, key) |
| 189 |
return mac |
| 190 |
} |
| 191 |
return tls10MAC{hmac.New(md5.New, key)} |
| 192 |
} |
| 193 |
|
| 194 |
func macSHA256(version uint16, key []byte) macFunction { |
| 195 |
if version == VersionSSL30 { |
| 196 |
mac := ssl30MAC{ |
| 197 |
h: sha256.New(), |
| 198 |
key: make([]byte, len(key)), |
| 199 |
} |
| 200 |
copy(mac.key, key) |
| 201 |
return mac |
| 202 |
} |
| 203 |
return tls10MAC{hmac.New(sha256.New, key)} |
| 204 |
} |
| 205 |
|
| 206 |
func macSHA384(version uint16, key []byte) macFunction { |
| 207 |
if version == VersionSSL30 { |
| 208 |
mac := ssl30MAC{ |
| 209 |
h: sha512.New384(), |
| 210 |
key: make([]byte, len(key)), |
| 211 |
} |
| 212 |
copy(mac.key, key) |
| 213 |
return mac |
| 214 |
} |
| 215 |
return tls10MAC{hmac.New(sha512.New384, key)} |
| 216 |
} |
| 217 |
|
| 218 |
type macFunction interface { |
| 219 |
Size() int |
| 220 |
MAC(digestBuf, seq, header, length, data []byte) []byte |
| 221 |
} |
| 222 |
|
| 223 |
// fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to |
| 224 |
// each call. |
| 225 |
type fixedNonceAEAD struct { |
| 226 |
// sealNonce and openNonce are buffers where the larger nonce will be |
| 227 |
// constructed. Since a seal and open operation may be running |
| 228 |
// concurrently, there is a separate buffer for each. |
| 229 |
sealNonce, openNonce []byte |
| 230 |
aead cipher.AEAD |
| 231 |
} |
| 232 |
|
| 233 |
func (f *fixedNonceAEAD) NonceSize() int { return 8 } |
| 234 |
func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() } |
| 235 |
|
| 236 |
func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte { |
| 237 |
copy(f.sealNonce[len(f.sealNonce)-8:], nonce) |
| 238 |
return f.aead.Seal(out, f.sealNonce, plaintext, additionalData) |
| 239 |
} |
| 240 |
|
| 241 |
func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) { |
| 242 |
copy(f.openNonce[len(f.openNonce)-8:], nonce) |
| 243 |
return f.aead.Open(out, f.openNonce, plaintext, additionalData) |
| 244 |
} |
| 245 |
|
| 246 |
func aeadAESGCM(key, fixedNonce []byte) *tlsAead { |
| 247 |
aes, err := aes.NewCipher(key) |
| 248 |
if err != nil { |
| 249 |
panic(err) |
| 250 |
} |
| 251 |
aead, err := cipher.NewGCM(aes) |
| 252 |
if err != nil { |
| 253 |
panic(err) |
| 254 |
} |
| 255 |
|
| 256 |
nonce1, nonce2 := make([]byte, 12), make([]byte, 12) |
| 257 |
copy(nonce1, fixedNonce) |
| 258 |
copy(nonce2, fixedNonce) |
| 259 |
|
| 260 |
return &tlsAead{&fixedNonceAEAD{nonce1, nonce2, aead}, true} |
| 261 |
} |
| 262 |
|
| 263 |
func aeadCHACHA20POLY1305Old(key, fixedNonce []byte) *tlsAead { |
| 264 |
aead, err := newChaCha20Poly1305Old(key) |
| 265 |
if err != nil { |
| 266 |
panic(err) |
| 267 |
} |
| 268 |
return &tlsAead{aead, false} |
| 269 |
} |
| 270 |
|
| 271 |
func xorSlice(out, in []byte) { |
| 272 |
for i := range out { |
| 273 |
out[i] ^= in[i] |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// xorNonceAEAD wraps an AEAD and XORs a fixed portion of the nonce, left-padded |
| 278 |
// if necessary, each call. |
| 279 |
type xorNonceAEAD struct { |
| 280 |
// sealNonce and openNonce are buffers where the larger nonce will be |
| 281 |
// constructed. Since a seal and open operation may be running |
| 282 |
// concurrently, there is a separate buffer for each. |
| 283 |
sealNonce, openNonce []byte |
| 284 |
aead cipher.AEAD |
| 285 |
} |
| 286 |
|
| 287 |
func (x *xorNonceAEAD) NonceSize() int { return 8 } |
| 288 |
func (x *xorNonceAEAD) Overhead() int { return x.aead.Overhead() } |
| 289 |
|
| 290 |
func (x *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte { |
| 291 |
xorSlice(x.sealNonce[len(x.sealNonce)-len(nonce):], nonce) |
| 292 |
ret := x.aead.Seal(out, x.sealNonce, plaintext, additionalData) |
| 293 |
xorSlice(x.sealNonce[len(x.sealNonce)-len(nonce):], nonce) |
| 294 |
return ret |
| 295 |
} |
| 296 |
|
| 297 |
func (x *xorNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) { |
| 298 |
xorSlice(x.openNonce[len(x.openNonce)-len(nonce):], nonce) |
| 299 |
ret, err := x.aead.Open(out, x.openNonce, plaintext, additionalData) |
| 300 |
xorSlice(x.openNonce[len(x.openNonce)-len(nonce):], nonce) |
| 301 |
return ret, err |
| 302 |
} |
| 303 |
|
| 304 |
func aeadCHACHA20POLY1305(key, fixedNonce []byte) *tlsAead { |
| 305 |
aead, err := newChaCha20Poly1305(key) |
| 306 |
if err != nil { |
| 307 |
panic(err) |
| 308 |
} |
| 309 |
|
| 310 |
nonce1, nonce2 := make([]byte, len(fixedNonce)), make([]byte, len(fixedNonce)) |
| 311 |
copy(nonce1, fixedNonce) |
| 312 |
copy(nonce2, fixedNonce) |
| 313 |
|
| 314 |
return &tlsAead{&xorNonceAEAD{nonce1, nonce2, aead}, false} |
| 315 |
} |
| 316 |
|
| 317 |
// ssl30MAC implements the SSLv3 MAC function, as defined in |
| 318 |
// www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1 |
| 319 |
type ssl30MAC struct { |
| 320 |
h hash.Hash |
| 321 |
key []byte |
| 322 |
} |
| 323 |
|
| 324 |
func (s ssl30MAC) Size() int { |
| 325 |
return s.h.Size() |
| 326 |
} |
| 327 |
|
| 328 |
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} |
| 329 |
|
| 330 |
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} |
| 331 |
|
| 332 |
func (s ssl30MAC) MAC(digestBuf, seq, header, length, data []byte) []byte { |
| 333 |
padLength := 48 |
| 334 |
if s.h.Size() == 20 { |
| 335 |
padLength = 40 |
| 336 |
} |
| 337 |
|
| 338 |
s.h.Reset() |
| 339 |
s.h.Write(s.key) |
| 340 |
s.h.Write(ssl30Pad1[:padLength]) |
| 341 |
s.h.Write(seq) |
| 342 |
s.h.Write(header[:1]) |
| 343 |
s.h.Write(length) |
| 344 |
s.h.Write(data) |
| 345 |
digestBuf = s.h.Sum(digestBuf[:0]) |
| 346 |
|
| 347 |
s.h.Reset() |
| 348 |
s.h.Write(s.key) |
| 349 |
s.h.Write(ssl30Pad2[:padLength]) |
| 350 |
s.h.Write(digestBuf) |
| 351 |
return s.h.Sum(digestBuf[:0]) |
| 352 |
} |
| 353 |
|
| 354 |
// tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3. |
| 355 |
type tls10MAC struct { |
| 356 |
h hash.Hash |
| 357 |
} |
| 358 |
|
| 359 |
func (s tls10MAC) Size() int { |
| 360 |
return s.h.Size() |
| 361 |
} |
| 362 |
|
| 363 |
func (s tls10MAC) MAC(digestBuf, seq, header, length, data []byte) []byte { |
| 364 |
s.h.Reset() |
| 365 |
s.h.Write(seq) |
| 366 |
s.h.Write(header) |
| 367 |
s.h.Write(length) |
| 368 |
s.h.Write(data) |
| 369 |
return s.h.Sum(digestBuf[:0]) |
| 370 |
} |
| 371 |
|
| 372 |
func rsaKA(version uint16) keyAgreement { |
| 373 |
return &rsaKeyAgreement{version: version} |
| 374 |
} |
| 375 |
|
| 376 |
func ecdheECDSAKA(version uint16) keyAgreement { |
| 377 |
return &ecdheKeyAgreement{ |
| 378 |
auth: &signedKeyAgreement{ |
| 379 |
sigType: signatureECDSA, |
| 380 |
version: version, |
| 381 |
}, |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
func cecpq1ECDSAKA(version uint16) keyAgreement { |
| 386 |
return &cecpq1KeyAgreement{ |
| 387 |
auth: &signedKeyAgreement{ |
| 388 |
sigType: signatureECDSA, |
| 389 |
version: version, |
| 390 |
}, |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
func ecdheRSAKA(version uint16) keyAgreement { |
| 395 |
return &ecdheKeyAgreement{ |
| 396 |
auth: &signedKeyAgreement{ |
| 397 |
sigType: signatureRSA, |
| 398 |
version: version, |
| 399 |
}, |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
func cecpq1RSAKA(version uint16) keyAgreement { |
| 404 |
return &cecpq1KeyAgreement{ |
| 405 |
auth: &signedKeyAgreement{ |
| 406 |
sigType: signatureRSA, |
| 407 |
version: version, |
| 408 |
}, |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
func dheRSAKA(version uint16) keyAgreement { |
| 413 |
return &dheKeyAgreement{ |
| 414 |
auth: &signedKeyAgreement{ |
| 415 |
sigType: signatureRSA, |
| 416 |
version: version, |
| 417 |
}, |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
func pskKA(version uint16) keyAgreement { |
| 422 |
return &pskKeyAgreement{ |
| 423 |
base: &nilKeyAgreement{}, |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
func ecdhePSKKA(version uint16) keyAgreement { |
| 428 |
return &pskKeyAgreement{ |
| 429 |
base: &ecdheKeyAgreement{ |
| 430 |
auth: &nilKeyAgreementAuthentication{}, |
| 431 |
}, |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
// mutualCipherSuite returns a cipherSuite given a list of supported |
| 436 |
// ciphersuites and the id requested by the peer. |
| 437 |
func mutualCipherSuite(have []uint16, want uint16) *cipherSuite { |
| 438 |
for _, id := range have { |
| 439 |
if id == want { |
| 440 |
for _, suite := range cipherSuites { |
| 441 |
if suite.id == want { |
| 442 |
return suite |
| 443 |
} |
| 444 |
} |
| 445 |
return nil |
| 446 |
} |
| 447 |
} |
| 448 |
return nil |
| 449 |
} |
| 450 |
|
| 451 |
// A list of the possible cipher suite ids. Taken from |
| 452 |
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml |
| 453 |
const ( |
| 454 |
TLS_RSA_WITH_NULL_SHA uint16 = 0x0002 |
| 455 |
TLS_RSA_WITH_RC4_128_MD5 uint16 = 0x0004 |
| 456 |
TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005 |
| 457 |
TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a |
| 458 |
TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x0016 |
| 459 |
TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f |
| 460 |
TLS_DHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0x0033 |
| 461 |
TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035 |
| 462 |
TLS_DHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0039 |
| 463 |
TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c |
| 464 |
TLS_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x003d |
| 465 |
TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x0067 |
| 466 |
TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x006b |
| 467 |
TLS_PSK_WITH_RC4_128_SHA uint16 = 0x008a |
| 468 |
TLS_PSK_WITH_AES_128_CBC_SHA uint16 = 0x008c |
| 469 |
TLS_PSK_WITH_AES_256_CBC_SHA uint16 = 0x008d |
| 470 |
TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c |
| 471 |
TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d |
| 472 |
TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009e |
| 473 |
TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009f |
| 474 |
TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007 |
| 475 |
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009 |
| 476 |
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a |
| 477 |
TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011 |
| 478 |
TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012 |
| 479 |
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013 |
| 480 |
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014 |
| 481 |
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023 |
| 482 |
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc024 |
| 483 |
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027 |
| 484 |
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc028 |
| 485 |
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b |
| 486 |
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c |
| 487 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f |
| 488 |
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030 |
| 489 |
TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA uint16 = 0xc035 |
| 490 |
TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA uint16 = 0xc036 |
| 491 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca8 |
| 492 |
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9 |
| 493 |
TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xccac |
| 494 |
renegotiationSCSV uint16 = 0x00ff |
| 495 |
fallbackSCSV uint16 = 0x5600 |
| 496 |
) |
| 497 |
|
| 498 |
// Additional cipher suite IDs, not IANA-assigned. |
| 499 |
const ( |
| 500 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256_OLD uint16 = 0xcc13 |
| 501 |
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256_OLD uint16 = 0xcc14 |
| 502 |
TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 uint16 = 0xd001 |
| 503 |
TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384 uint16 = 0xd002 |
| 504 |
TLS_CECPQ1_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0x16b7 |
| 505 |
TLS_CECPQ1_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0x16b8 |
| 506 |
TLS_CECPQ1_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x16b9 |
| 507 |
TLS_CECPQ1_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0x16ba |
| 508 |
) |