Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cipher_suites.go 3.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. package tls
  5. import (
  6. "crypto/aes"
  7. "crypto/cipher"
  8. "crypto/hmac"
  9. "crypto/rc4"
  10. "crypto/x509"
  11. "hash"
  12. "os"
  13. )
  14. // a keyAgreement implements the client and server side of a TLS key agreement
  15. // protocol by generating and processing key exchange messages.
  16. type keyAgreement interface {
  17. // On the server side, the first two methods are called in order.
  18. // In the case that the key agreement protocol doesn't use a
  19. // ServerKeyExchange message, generateServerKeyExchange can return nil,
  20. // nil.
  21. generateServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, os.Error)
  22. processClientKeyExchange(*Config, *clientKeyExchangeMsg) ([]byte, os.Error)
  23. // On the client side, the next two methods are called in order.
  24. // This method may not be called if the server doesn't send a
  25. // ServerKeyExchange message.
  26. processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) os.Error
  27. generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, os.Error)
  28. }
  29. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  30. // function. All cipher suites currently assume RSA key agreement.
  31. type cipherSuite struct {
  32. // the lengths, in bytes, of the key material needed for each component.
  33. keyLen int
  34. macLen int
  35. ivLen int
  36. ka func() keyAgreement
  37. // If elliptic is set, a server will only consider this ciphersuite if
  38. // the ClientHello indicated that the client supports an elliptic curve
  39. // and point format that we can handle.
  40. elliptic bool
  41. cipher func(key, iv []byte, isRead bool) interface{}
  42. mac func(macKey []byte) hash.Hash
  43. }
  44. var cipherSuites = map[uint16]*cipherSuite{
  45. TLS_RSA_WITH_RC4_128_SHA: &cipherSuite{16, 20, 0, rsaKA, false, cipherRC4, hmacSHA1},
  46. TLS_RSA_WITH_AES_128_CBC_SHA: &cipherSuite{16, 20, 16, rsaKA, false, cipherAES, hmacSHA1},
  47. TLS_ECDHE_RSA_WITH_RC4_128_SHA: &cipherSuite{16, 20, 0, ecdheRSAKA, true, cipherRC4, hmacSHA1},
  48. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: &cipherSuite{16, 20, 16, ecdheRSAKA, true, cipherAES, hmacSHA1},
  49. }
  50. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  51. cipher, _ := rc4.NewCipher(key)
  52. return cipher
  53. }
  54. func cipherAES(key, iv []byte, isRead bool) interface{} {
  55. block, _ := aes.NewCipher(key)
  56. if isRead {
  57. return cipher.NewCBCDecrypter(block, iv)
  58. }
  59. return cipher.NewCBCEncrypter(block, iv)
  60. }
  61. func hmacSHA1(key []byte) hash.Hash {
  62. return hmac.NewSHA1(key)
  63. }
  64. func rsaKA() keyAgreement {
  65. return rsaKeyAgreement{}
  66. }
  67. func ecdheRSAKA() keyAgreement {
  68. return new(ecdheRSAKeyAgreement)
  69. }
  70. // mutualCipherSuite returns a cipherSuite and its id given a list of supported
  71. // ciphersuites and the id requested by the peer.
  72. func mutualCipherSuite(have []uint16, want uint16) (suite *cipherSuite, id uint16) {
  73. for _, id := range have {
  74. if id == want {
  75. return cipherSuites[id], id
  76. }
  77. }
  78. return
  79. }
  80. // A list of the possible cipher suite ids. Taken from
  81. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  82. const (
  83. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  84. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  85. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  86. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  87. )