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
64 - def create(self, masterSecret, sessionID, cipherSuite, 65 srpUsername, clientCertChain, serverCertChain, 66 tackExt, tackInHelloExt, serverName, resumable=True, 67 encryptThenMAC=False):
68 self.masterSecret = masterSecret 69 self.sessionID = sessionID 70 self.cipherSuite = cipherSuite 71 self.srpUsername = srpUsername 72 self.clientCertChain = clientCertChain 73 self.serverCertChain = serverCertChain 74 self.tackExt = tackExt 75 self.tackInHelloExt = tackInHelloExt 76 self.serverName = serverName 77 self.resumable = resumable 78 self.encryptThenMAC = encryptThenMAC
79
80 - def _clone(self):
81 other = Session() 82 other.masterSecret = self.masterSecret 83 other.sessionID = self.sessionID 84 other.cipherSuite = self.cipherSuite 85 other.srpUsername = self.srpUsername 86 other.clientCertChain = self.clientCertChain 87 other.serverCertChain = self.serverCertChain 88 other.tackExt = self.tackExt 89 other.tackInHelloExt = self.tackInHelloExt 90 other.serverName = self.serverName 91 other.resumable = self.resumable 92 other.encryptThenMAC = self.encryptThenMAC 93 return other
94
95 - def valid(self):
96 """If this session can be used for session resumption. 97 98 @rtype: bool 99 @return: If this session can be used for session resumption. 100 """ 101 return self.resumable and self.sessionID
102
103 - def _setResumable(self, boolean):
104 #Only let it be set to True if the sessionID is non-null 105 if (not boolean) or (boolean and self.sessionID): 106 self.resumable = boolean
107
108 - def getTackId(self):
109 if self.tackExt and self.tackExt.tack: 110 return self.tackExt.tack.getTackId() 111 else: 112 return None
113
114 - def getBreakSigs(self):
115 if self.tackExt and self.tackExt.break_sigs: 116 return self.tackExt.break_sigs 117 else: 118 return None
119
120 - def getCipherName(self):
121 """Get the name of the cipher used with this connection. 122 123 @rtype: str 124 @return: The name of the cipher used with this connection. 125 """ 126 return CipherSuite.canonicalCipherName(self.cipherSuite)
127
128 - def getMacName(self):
129 """Get the name of the HMAC hash algo used with this connection. 130 131 @rtype: str 132 @return: The name of the HMAC hash algo used with this connection. 133 """ 134 return CipherSuite.canonicalMacName(self.cipherSuite)
135