1
2
3
4 """
5 A state machine for using TLS Lite with asynchronous I/O.
6 """
7
9 """
10 This is an abstract class that's used to integrate TLS Lite with
11 asyncore and Twisted.
12
13 This class signals wantsReadsEvent() and wantsWriteEvent(). When
14 the underlying socket has become readable or writeable, the event
15 should be passed to this class by calling inReadEvent() or
16 inWriteEvent(). This class will then try to read or write through
17 the socket, and will update its state appropriately.
18
19 This class will forward higher-level events to its subclass. For
20 example, when a complete TLS record has been received,
21 outReadEvent() will be called with the decrypted data.
22 """
23
26
28
29
30
31 self.handshaker = None
32 self.closer = None
33 self.reader = None
34 self.writer = None
35
36
37
38
39
40
41 self.result = None
42
44
45
46 activeOps = 0
47 if self.handshaker:
48 activeOps += 1
49 if self.closer:
50 activeOps += 1
51 if self.reader:
52 activeOps += 1
53 if self.writer:
54 activeOps += 1
55
56 if self.result == None:
57 if activeOps != 0:
58 raise AssertionError()
59 elif self.result in (0,1):
60 if activeOps != 1:
61 raise AssertionError()
62 else:
63 raise AssertionError()
64 if activeOps > maxActive:
65 raise AssertionError()
66
68 """If the state machine wants to read.
69
70 If an operation is active, this returns whether or not the
71 operation wants to read from the socket. If an operation is
72 not active, this returns None.
73
74 @rtype: bool or None
75 @return: If the state machine wants to read.
76 """
77 if self.result != None:
78 return self.result == 0
79 return None
80
82 """If the state machine wants to write.
83
84 If an operation is active, this returns whether or not the
85 operation wants to write to the socket. If an operation is
86 not active, this returns None.
87
88 @rtype: bool or None
89 @return: If the state machine wants to write.
90 """
91 if self.result != None:
92 return self.result == 1
93 return None
94
96 """Called when a handshake operation completes.
97
98 May be overridden in subclass.
99 """
100 pass
101
103 """Called when a close operation completes.
104
105 May be overridden in subclass.
106 """
107 pass
108
110 """Called when a read operation completes.
111
112 May be overridden in subclass."""
113 pass
114
116 """Called when a write operation completes.
117
118 May be overridden in subclass."""
119 pass
120
122 """Tell the state machine it can read from the socket."""
123 try:
124 self._checkAssert()
125 if self.handshaker:
126 self._doHandshakeOp()
127 elif self.closer:
128 self._doCloseOp()
129 elif self.reader:
130 self._doReadOp()
131 elif self.writer:
132 self._doWriteOp()
133 else:
134 self.reader = self.tlsConnection.readAsync(16384)
135 self._doReadOp()
136 except:
137 self._clear()
138 raise
139
157
159 try:
160 self.result = self.handshaker.next()
161 except StopIteration:
162 self.handshaker = None
163 self.result = None
164 self.outConnectEvent()
165
167 try:
168 self.result = self.closer.next()
169 except StopIteration:
170 self.closer = None
171 self.result = None
172 self.outCloseEvent()
173
175 self.result = self.reader.next()
176 if not self.result in (0,1):
177 readBuffer = self.result
178 self.reader = None
179 self.result = None
180 self.outReadEvent(readBuffer)
181
183 try:
184 self.result = self.writer.next()
185 except StopIteration:
186 self.writer = None
187 self.result = None
188
190 """Start a handshake operation.
191
192 @type handshaker: generator
193 @param handshaker: A generator created by using one of the
194 asynchronous handshake functions (i.e. handshakeServerAsync, or
195 handshakeClientxxx(..., async=True).
196 """
197 try:
198 self._checkAssert(0)
199 self.handshaker = handshaker
200 self._doHandshakeOp()
201 except:
202 self._clear()
203 raise
204
206 """Start a handshake operation.
207
208 The arguments passed to this function will be forwarded to
209 L{tlslite.tlsconnection.TLSConnection.handshakeServerAsync}.
210 """
211 handshaker = self.tlsConnection.handshakeServerAsync(**args)
212 self.setHandshakeOp(handshaker)
213
224
226 """Start a write operation.
227
228 @type writeBuffer: str
229 @param writeBuffer: The string to transmit.
230 """
231 try:
232 self._checkAssert(0)
233 self.writer = self.tlsConnection.writeAsync(writeBuffer)
234 self._doWriteOp()
235 except:
236 self._clear()
237 raise
238