github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/crypto/tls/cipher_suites.go (about) 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 tls 6 7 import ( 8 "crypto" 9 "crypto/aes" 10 "crypto/cipher" 11 "crypto/des" 12 "crypto/hmac" 13 "crypto/rc4" 14 "crypto/sha1" 15 "crypto/x509" 16 "hash" 17 ) 18 19 // a keyAgreement implements the client and server side of a TLS key agreement 20 // protocol by generating and processing key exchange messages. 21 type keyAgreement interface { 22 // On the server side, the first two methods are called in order. 23 24 // In the case that the key agreement protocol doesn't use a 25 // ServerKeyExchange message, generateServerKeyExchange can return nil, 26 // nil. 27 generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error) 28 processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error) 29 30 // On the client side, the next two methods are called in order. 31 32 // This method may not be called if the server doesn't send a 33 // ServerKeyExchange message. 34 processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error 35 generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error) 36 } 37 38 const ( 39 // suiteECDH indicates that the cipher suite involves elliptic curve 40 // Diffie-Hellman. This means that it should only be selected when the 41 // client indicates that it supports ECC with a curve and point format 42 // that we're happy with. 43 suiteECDHE = 1 << iota 44 // suiteECDSA indicates that the cipher suite involves an ECDSA 45 // signature and therefore may only be selected when the server's 46 // certificate is ECDSA. If this is not set then the cipher suite is 47 // RSA based. 48 suiteECDSA 49 // suiteTLS12 indicates that the cipher suite should only be advertised 50 // and accepted when using TLS 1.2. 51 suiteTLS12 52 // suiteDefaultOff indicates that this cipher suite is not included by 53 // default. 54 suiteDefaultOff 55 ) 56 57 // A cipherSuite is a specific combination of key agreement, cipher and MAC 58 // function. All cipher suites currently assume RSA key agreement. 59 type cipherSuite struct { 60 id uint16 61 // the lengths, in bytes, of the key material needed for each component. 62 keyLen int 63 macLen int 64 ivLen int 65 ka func(version uint16) keyAgreement 66 // flags is a bitmask of the suite* values, above. 67 flags int 68 cipher func(key, iv []byte, isRead bool) interface{} 69 mac func(version uint16, macKey []byte) macFunction 70 aead func(key, fixedNonce []byte) cipher.AEAD 71 tls12Hash crypto.Hash 72 } 73 74 var cipherSuites = []*cipherSuite{ 75 // Ciphersuite order is chosen so that ECDHE comes before plain RSA 76 // and RC4 comes before AES (because of the Lucky13 attack). 77 {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM, crypto.SHA256}, 78 {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM, crypto.SHA256}, 79 {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM, crypto.SHA384}, 80 {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM, crypto.SHA384}, 81 {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil, crypto.SHA256}, 82 {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil, crypto.SHA256}, 83 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil, crypto.SHA256}, 84 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil, crypto.SHA256}, 85 {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil, crypto.SHA256}, 86 {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil, crypto.SHA256}, 87 {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil, crypto.SHA256}, 88 {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil, crypto.SHA256}, 89 {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil, crypto.SHA256}, 90 {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil, crypto.SHA256}, 91 {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil, crypto.SHA256}, 92 } 93 94 func cipherRC4(key, iv []byte, isRead bool) interface{} { 95 cipher, _ := rc4.NewCipher(key) 96 return cipher 97 } 98 99 func cipher3DES(key, iv []byte, isRead bool) interface{} { 100 block, _ := des.NewTripleDESCipher(key) 101 if isRead { 102 return cipher.NewCBCDecrypter(block, iv) 103 } 104 return cipher.NewCBCEncrypter(block, iv) 105 } 106 107 func cipherAES(key, iv []byte, isRead bool) interface{} { 108 block, _ := aes.NewCipher(key) 109 if isRead { 110 return cipher.NewCBCDecrypter(block, iv) 111 } 112 return cipher.NewCBCEncrypter(block, iv) 113 } 114 115 // macSHA1 returns a macFunction for the given protocol version. 116 func macSHA1(version uint16, key []byte) macFunction { 117 if version == VersionSSL30 { 118 mac := ssl30MAC{ 119 h: sha1.New(), 120 key: make([]byte, len(key)), 121 } 122 copy(mac.key, key) 123 return mac 124 } 125 return tls10MAC{hmac.New(sha1.New, key)} 126 } 127 128 type macFunction interface { 129 Size() int 130 MAC(digestBuf, seq, header, data []byte) []byte 131 } 132 133 // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to 134 // each call. 135 type fixedNonceAEAD struct { 136 // sealNonce and openNonce are buffers where the larger nonce will be 137 // constructed. Since a seal and open operation may be running 138 // concurrently, there is a separate buffer for each. 139 sealNonce, openNonce []byte 140 aead cipher.AEAD 141 } 142 143 func (f *fixedNonceAEAD) NonceSize() int { return 8 } 144 func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() } 145 146 func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte { 147 copy(f.sealNonce[len(f.sealNonce)-8:], nonce) 148 return f.aead.Seal(out, f.sealNonce, plaintext, additionalData) 149 } 150 151 func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) { 152 copy(f.openNonce[len(f.openNonce)-8:], nonce) 153 return f.aead.Open(out, f.openNonce, plaintext, additionalData) 154 } 155 156 func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD { 157 aes, err := aes.NewCipher(key) 158 if err != nil { 159 panic(err) 160 } 161 aead, err := cipher.NewGCM(aes) 162 if err != nil { 163 panic(err) 164 } 165 166 nonce1, nonce2 := make([]byte, 12), make([]byte, 12) 167 copy(nonce1, fixedNonce) 168 copy(nonce2, fixedNonce) 169 170 return &fixedNonceAEAD{nonce1, nonce2, aead} 171 } 172 173 // ssl30MAC implements the SSLv3 MAC function, as defined in 174 // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1 175 type ssl30MAC struct { 176 h hash.Hash 177 key []byte 178 } 179 180 func (s ssl30MAC) Size() int { 181 return s.h.Size() 182 } 183 184 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} 185 186 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} 187 188 func (s ssl30MAC) MAC(digestBuf, seq, header, data []byte) []byte { 189 padLength := 48 190 if s.h.Size() == 20 { 191 padLength = 40 192 } 193 194 s.h.Reset() 195 s.h.Write(s.key) 196 s.h.Write(ssl30Pad1[:padLength]) 197 s.h.Write(seq) 198 s.h.Write(header[:1]) 199 s.h.Write(header[3:5]) 200 s.h.Write(data) 201 digestBuf = s.h.Sum(digestBuf[:0]) 202 203 s.h.Reset() 204 s.h.Write(s.key) 205 s.h.Write(ssl30Pad2[:padLength]) 206 s.h.Write(digestBuf) 207 return s.h.Sum(digestBuf[:0]) 208 } 209 210 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3. 211 type tls10MAC struct { 212 h hash.Hash 213 } 214 215 func (s tls10MAC) Size() int { 216 return s.h.Size() 217 } 218 219 func (s tls10MAC) MAC(digestBuf, seq, header, data []byte) []byte { 220 s.h.Reset() 221 s.h.Write(seq) 222 s.h.Write(header) 223 s.h.Write(data) 224 return s.h.Sum(digestBuf[:0]) 225 } 226 227 func rsaKA(version uint16) keyAgreement { 228 return rsaKeyAgreement{} 229 } 230 231 func ecdheECDSAKA(version uint16) keyAgreement { 232 return &ecdheKeyAgreement{ 233 sigType: signatureECDSA, 234 version: version, 235 } 236 } 237 238 func ecdheRSAKA(version uint16) keyAgreement { 239 return &ecdheKeyAgreement{ 240 sigType: signatureRSA, 241 version: version, 242 } 243 } 244 245 // mutualCipherSuite returns a cipherSuite given a list of supported 246 // ciphersuites and the id requested by the peer. 247 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite { 248 for _, id := range have { 249 if id == want { 250 for _, suite := range cipherSuites { 251 if suite.id == want { 252 return suite 253 } 254 } 255 return nil 256 } 257 } 258 return nil 259 } 260 261 // A list of the possible cipher suite ids. Taken from 262 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml 263 const ( 264 TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005 265 TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a 266 TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f 267 TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035 268 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007 269 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009 270 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a 271 TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011 272 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012 273 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013 274 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014 275 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f 276 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b 277 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030 278 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c 279 280 // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator 281 // that the client is doing version fallback. See 282 // https://tools.ietf.org/html/draft-ietf-tls-downgrade-scsv-00. 283 TLS_FALLBACK_SCSV uint16 = 0x5600 284 )