ssl_ciphersuites.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2012 Google Inc. All rights reserved.
  2. """
  3. Nicely formatted cipher suite definitions for TLS
  4. A list of cipher suites in the form of CipherSuite objects.
  5. These are supposed to be immutable; don't mess with them.
  6. """
  7. class CipherSuite(object):
  8. """
  9. Encapsulates a cipher suite.
  10. Members/args:
  11. * code: two-byte ID code, as int
  12. * name: as in 'TLS_RSA_WITH_RC4_40_MD5'
  13. * kx: key exchange algorithm, string
  14. * auth: authentication algorithm, string
  15. * encoding: encoding algorithm
  16. * mac: message authentication code algorithm
  17. """
  18. def __init__(self, code, name, kx, auth, encoding, mac):
  19. self.code = code
  20. self.name = name
  21. self.kx = kx
  22. self.auth = auth
  23. self.encoding = encoding
  24. self.mac = mac
  25. def __repr__(self):
  26. return 'CipherSuite(%s)' % self.name
  27. MAC_SIZES = {
  28. 'MD5': 16,
  29. 'SHA': 20,
  30. 'SHA256': 32, # I guess
  31. }
  32. BLOCK_SIZES = {
  33. 'AES_256_CBC': 16,
  34. }
  35. @property
  36. def mac_size(self):
  37. """In bytes. Default to 0."""
  38. return self.MAC_SIZES.get(self.mac, 0)
  39. @property
  40. def block_size(self):
  41. """In bytes. Default to 1."""
  42. return self.BLOCK_SIZES.get(self.encoding, 1)
  43. # master list of CipherSuite Objects
  44. CIPHERSUITES = [
  45. # not a real cipher suite, can be ignored, see RFC5746
  46. CipherSuite(0xff, 'TLS_EMPTY_RENEGOTIATION_INFO', 'NULL', 'NULL', 'NULL', 'NULL'),
  47. CipherSuite(0x00, 'TLS_NULL_WITH_NULL_NULL', 'NULL', 'NULL', 'NULL', 'NULL'),
  48. CipherSuite(0x01, 'TLS_RSA_WITH_NULL_MD5', 'RSA', 'RSA', 'NULL', 'MD5'),
  49. CipherSuite(0x02, 'TLS_RSA_WITH_NULL_SHA', 'RSA', 'RSA', 'NULL', 'SHA'),
  50. CipherSuite(0x0039, 'TLS_DHE_RSA_WITH_AES_256_CBC_SHA', 'DHE', 'RSA', 'AES_256_CBC', 'SHA'), # not sure I got the kx/auth thing right.
  51. CipherSuite(0xffff, 'UNKNOWN_CIPHER', '', '', '', '')
  52. ]
  53. # BY_CODE = decoder_dict((cipher.code, cipher) for cipher in CIPHERSUITES)
  54. # BY_NAME = decoder_dict((suite.name, suite) for suite in CIPHERSUITES)
  55. # NULL_SUITE = BY_CODE[0x00]