/gdata/tlslite/Session.py

http://radioappz.googlecode.com/ · Python · 131 lines · 101 code · 8 blank · 22 comment · 8 complexity · 9ec7230f0342c0de6bb977fbeeef8d16 MD5 · raw file

  1. """Class representing a TLS session."""
  2. from utils.compat import *
  3. from mathtls import *
  4. from constants import *
  5. class Session:
  6. """
  7. This class represents a TLS session.
  8. TLS distinguishes between connections and sessions. A new
  9. handshake creates both a connection and a session. Data is
  10. transmitted over the connection.
  11. The session contains a more permanent record of the handshake. The
  12. session can be inspected to determine handshake results. The
  13. session can also be used to create a new connection through
  14. "session resumption". If the client and server both support this,
  15. they can create a new connection based on an old session without
  16. the overhead of a full handshake.
  17. The session for a L{tlslite.TLSConnection.TLSConnection} can be
  18. retrieved from the connection's 'session' attribute.
  19. @type srpUsername: str
  20. @ivar srpUsername: The client's SRP username (or None).
  21. @type sharedKeyUsername: str
  22. @ivar sharedKeyUsername: The client's shared-key username (or
  23. None).
  24. @type clientCertChain: L{tlslite.X509CertChain.X509CertChain} or
  25. L{cryptoIDlib.CertChain.CertChain}
  26. @ivar clientCertChain: The client's certificate chain (or None).
  27. @type serverCertChain: L{tlslite.X509CertChain.X509CertChain} or
  28. L{cryptoIDlib.CertChain.CertChain}
  29. @ivar serverCertChain: The server's certificate chain (or None).
  30. """
  31. def __init__(self):
  32. self.masterSecret = createByteArraySequence([])
  33. self.sessionID = createByteArraySequence([])
  34. self.cipherSuite = 0
  35. self.srpUsername = None
  36. self.sharedKeyUsername = None
  37. self.clientCertChain = None
  38. self.serverCertChain = None
  39. self.resumable = False
  40. self.sharedKey = False
  41. def _clone(self):
  42. other = Session()
  43. other.masterSecret = self.masterSecret
  44. other.sessionID = self.sessionID
  45. other.cipherSuite = self.cipherSuite
  46. other.srpUsername = self.srpUsername
  47. other.sharedKeyUsername = self.sharedKeyUsername
  48. other.clientCertChain = self.clientCertChain
  49. other.serverCertChain = self.serverCertChain
  50. other.resumable = self.resumable
  51. other.sharedKey = self.sharedKey
  52. return other
  53. def _calcMasterSecret(self, version, premasterSecret, clientRandom,
  54. serverRandom):
  55. if version == (3,0):
  56. self.masterSecret = PRF_SSL(premasterSecret,
  57. concatArrays(clientRandom, serverRandom), 48)
  58. elif version in ((3,1), (3,2)):
  59. self.masterSecret = PRF(premasterSecret, "master secret",
  60. concatArrays(clientRandom, serverRandom), 48)
  61. else:
  62. raise AssertionError()
  63. def valid(self):
  64. """If this session can be used for session resumption.
  65. @rtype: bool
  66. @return: If this session can be used for session resumption.
  67. """
  68. return self.resumable or self.sharedKey
  69. def _setResumable(self, boolean):
  70. #Only let it be set if this isn't a shared key
  71. if not self.sharedKey:
  72. #Only let it be set to True if the sessionID is non-null
  73. if (not boolean) or (boolean and self.sessionID):
  74. self.resumable = boolean
  75. def getCipherName(self):
  76. """Get the name of the cipher used with this connection.
  77. @rtype: str
  78. @return: The name of the cipher used with this connection.
  79. Either 'aes128', 'aes256', 'rc4', or '3des'.
  80. """
  81. if self.cipherSuite in CipherSuite.aes128Suites:
  82. return "aes128"
  83. elif self.cipherSuite in CipherSuite.aes256Suites:
  84. return "aes256"
  85. elif self.cipherSuite in CipherSuite.rc4Suites:
  86. return "rc4"
  87. elif self.cipherSuite in CipherSuite.tripleDESSuites:
  88. return "3des"
  89. else:
  90. return None
  91. def _createSharedKey(self, sharedKeyUsername, sharedKey):
  92. if len(sharedKeyUsername)>16:
  93. raise ValueError()
  94. if len(sharedKey)>47:
  95. raise ValueError()
  96. self.sharedKeyUsername = sharedKeyUsername
  97. self.sessionID = createByteArrayZeros(16)
  98. for x in range(len(sharedKeyUsername)):
  99. self.sessionID[x] = ord(sharedKeyUsername[x])
  100. premasterSecret = createByteArrayZeros(48)
  101. sharedKey = chr(len(sharedKey)) + sharedKey
  102. for x in range(48):
  103. premasterSecret[x] = ord(sharedKey[x % len(sharedKey)])
  104. self.masterSecret = PRF(premasterSecret, "shared secret",
  105. createByteArraySequence([]), 48)
  106. self.sharedKey = True
  107. return self