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