OLD | NEW |
1 # Authors: | 1 # Authors: |
2 # Trevor Perrin | 2 # Trevor Perrin |
3 # Google (adapted by Sam Rushing) - NPN support | 3 # Google (adapted by Sam Rushing) - NPN support |
4 # Martin von Loewis - python 3 port | 4 # Martin von Loewis - python 3 port |
5 # | 5 # |
6 # See the LICENSE file for legal information regarding use of this file. | 6 # See the LICENSE file for legal information regarding use of this file. |
7 | 7 |
8 """Helper class for TLSConnection.""" | 8 """Helper class for TLSConnection.""" |
9 from __future__ import generators | 9 from __future__ import generators |
10 | 10 |
11 from .utils.compat import * | 11 from .utils.compat import * |
12 from .utils.cryptomath import * | 12 from .utils.cryptomath import * |
13 from .utils.cipherfactory import createAES, createRC4, createTripleDES | 13 from .utils.cipherfactory import createAES, createRC4, createTripleDES |
14 from .utils.codec import * | 14 from .utils.codec import * |
15 from .errors import * | 15 from .errors import * |
16 from .messages import * | 16 from .messages import * |
17 from .mathtls import * | 17 from .mathtls import * |
18 from .constants import * | 18 from .constants import * |
19 from .utils.cryptomath import getRandomBytes | 19 from .utils.cryptomath import getRandomBytes |
20 | 20 |
21 import socket | 21 import socket |
| 22 import struct |
22 import errno | 23 import errno |
23 import traceback | 24 import traceback |
24 | 25 |
25 class _ConnectionState(object): | 26 class _ConnectionState(object): |
26 def __init__(self): | 27 def __init__(self): |
27 self.macContext = None | 28 self.macContext = None |
28 self.encContext = None | 29 self.encContext = None |
29 self.seqnum = 0 | 30 self.seqnum = 0 |
30 | 31 |
31 def getSeqNumBytes(self): | 32 def getSeqNumBytes(self): |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 self.session.resumable = False | 515 self.session.resumable = False |
515 | 516 |
516 | 517 |
517 def _sendError(self, alertDescription, errorStr=None): | 518 def _sendError(self, alertDescription, errorStr=None): |
518 alert = Alert().create(alertDescription, AlertLevel.fatal) | 519 alert = Alert().create(alertDescription, AlertLevel.fatal) |
519 for result in self._sendMsg(alert): | 520 for result in self._sendMsg(alert): |
520 yield result | 521 yield result |
521 self._shutdown(False) | 522 self._shutdown(False) |
522 raise TLSLocalAlert(alert, errorStr) | 523 raise TLSLocalAlert(alert, errorStr) |
523 | 524 |
| 525 def _abortWithReset(self): |
| 526 #Set an SO_LINGER timeout of 0 to send a TCP RST. |
| 527 self.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, |
| 528 struct.pack('ii', 1, 0)) |
| 529 self._shutdown(False) |
| 530 |
524 def _sendMsgs(self, msgs): | 531 def _sendMsgs(self, msgs): |
525 randomizeFirstBlock = True | 532 randomizeFirstBlock = True |
526 for msg in msgs: | 533 for msg in msgs: |
527 for result in self._sendMsg(msg, randomizeFirstBlock): | 534 for result in self._sendMsg(msg, randomizeFirstBlock): |
528 yield result | 535 yield result |
529 randomizeFirstBlock = True | 536 randomizeFirstBlock = True |
530 | 537 |
531 def _sendMsg(self, msg, randomizeFirstBlock = True): | 538 def _sendMsg(self, msg, randomizeFirstBlock = True): |
532 #Whenever we're connected and asked to send an app data message, | 539 #Whenever we're connected and asked to send an app data message, |
533 #we first send the first byte of the message. This prevents | 540 #we first send the first byte of the message. This prevents |
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1142 imac_md5.update(compatHMAC(label + masterSecret + bytearray([0x36]*48))) | 1149 imac_md5.update(compatHMAC(label + masterSecret + bytearray([0x36]*48))) |
1143 imac_sha.update(compatHMAC(label + masterSecret + bytearray([0x36]*40))) | 1150 imac_sha.update(compatHMAC(label + masterSecret + bytearray([0x36]*40))) |
1144 | 1151 |
1145 md5Bytes = MD5(masterSecret + bytearray([0x5c]*48) + \ | 1152 md5Bytes = MD5(masterSecret + bytearray([0x5c]*48) + \ |
1146 bytearray(imac_md5.digest())) | 1153 bytearray(imac_md5.digest())) |
1147 shaBytes = SHA1(masterSecret + bytearray([0x5c]*40) + \ | 1154 shaBytes = SHA1(masterSecret + bytearray([0x5c]*40) + \ |
1148 bytearray(imac_sha.digest())) | 1155 bytearray(imac_sha.digest())) |
1149 | 1156 |
1150 return md5Bytes + shaBytes | 1157 return md5Bytes + shaBytes |
1151 | 1158 |
OLD | NEW |