Package tlslite :: Module session
[hide private]
[frames] | no frames]

Source Code for Module tlslite.session

  1  # Authors:  
  2  #   Trevor Perrin 
  3  #   Dave Baggett (Arcode Corporation) - canonicalCipherName 
  4  # 
  5  # See the LICENSE file for legal information regarding use of this file. 
  6   
  7  """Class representing a TLS session.""" 
  8   
  9  from .utils.compat import * 
 10  from .mathtls import * 
 11  from .constants import * 
 12   
13 -class Session(object):
14 """ 15 This class represents a TLS session. 16 17 TLS distinguishes between connections and sessions. A new 18 handshake creates both a connection and a session. Data is 19 transmitted over the connection. 20 21 The session contains a more permanent record of the handshake. The 22 session can be inspected to determine handshake results. The 23 session can also be used to create a new connection through 24 "session resumption". If the client and server both support this, 25 they can create a new connection based on an old session without 26 the overhead of a full handshake. 27 28 The session for a L{tlslite.TLSConnection.TLSConnection} can be 29 retrieved from the connection's 'session' attribute. 30 31 @type srpUsername: str 32 @ivar srpUsername: The client's SRP username (or None). 33 34 @type clientCertChain: L{tlslite.x509certchain.X509CertChain} 35 @ivar clientCertChain: The client's certificate chain (or None). 36 37 @type serverCertChain: L{tlslite.x509certchain.X509CertChain} 38 @ivar serverCertChain: The server's certificate chain (or None). 39 40 @type tackExt: L{tack.structures.TackExtension.TackExtension} 41 @ivar tackExt: The server's TackExtension (or None). 42 43 @type tackInHelloExt: L{bool} 44 @ivar tackInHelloExt:True if a TACK was presented via TLS Extension. 45 46 @type encryptThenMAC: bool 47 @ivar encryptThenMAC: True if connection uses CBC cipher in 48 encrypt-then-MAC mode 49 """ 50
51 - def __init__(self):
52 self.masterSecret = bytearray(0) 53 self.sessionID = bytearray(0) 54 self.cipherSuite = 0 55 self.srpUsername = "" 56 self.clientCertChain = None 57 self.serverCertChain = None 58 self.tackExt = None 59 self.tackInHelloExt = False 60 self.serverName = "" 61 self.resumable = False 62 self.encryptThenMAC = False 63 self.extendedMasterSecret = False
64
65 - def create(self, masterSecret, sessionID, cipherSuite, 66 srpUsername, clientCertChain, serverCertChain, 67 tackExt, tackInHelloExt, serverName, resumable=True, 68 encryptThenMAC=False, extendedMasterSecret=False):
69 self.masterSecret = masterSecret 70 self.sessionID = sessionID 71 self.cipherSuite = cipherSuite 72 self.srpUsername = srpUsername 73 self.clientCertChain = clientCertChain 74 self.serverCertChain = serverCertChain 75 self.tackExt = tackExt 76 self.tackInHelloExt = tackInHelloExt 77 self.serverName = serverName 78 self.resumable = resumable 79 self.encryptThenMAC = encryptThenMAC 80 self.extendedMasterSecret = extendedMasterSecret
81
82 - def _clone(self):
83 other = Session() 84 other.masterSecret = self.masterSecret 85 other.sessionID = self.sessionID 86 other.cipherSuite = self.cipherSuite 87 other.srpUsername = self.srpUsername 88 other.clientCertChain = self.clientCertChain 89 other.serverCertChain = self.serverCertChain 90 other.tackExt = self.tackExt 91 other.tackInHelloExt = self.tackInHelloExt 92 other.serverName = self.serverName 93 other.resumable = self.resumable 94 other.encryptThenMAC = self.encryptThenMAC 95 other.extendedMasterSecret = self.extendedMasterSecret 96 return other
97
98 - def valid(self):
99 """If this session can be used for session resumption. 100 101 @rtype: bool 102 @return: If this session can be used for session resumption. 103 """ 104 return self.resumable and self.sessionID
105
106 - def _setResumable(self, boolean):
107 #Only let it be set to True if the sessionID is non-null 108 if (not boolean) or (boolean and self.sessionID): 109 self.resumable = boolean
110
111 - def getTackId(self):
112 if self.tackExt and self.tackExt.tack: 113 return self.tackExt.tack.getTackId() 114 else: 115 return None
116
117 - def getBreakSigs(self):
118 if self.tackExt and self.tackExt.break_sigs: 119 return self.tackExt.break_sigs 120 else: 121 return None
122
123 - def getCipherName(self):
124 """Get the name of the cipher used with this connection. 125 126 @rtype: str 127 @return: The name of the cipher used with this connection. 128 """ 129 return CipherSuite.canonicalCipherName(self.cipherSuite)
130
131 - def getMacName(self):
132 """Get the name of the HMAC hash algo used with this connection. 133 134 @rtype: str 135 @return: The name of the HMAC hash algo used with this connection. 136 """ 137 return CipherSuite.canonicalMacName(self.cipherSuite)
138