| 403 | # |
| 404 | ##################################################################### |
| 405 | class TLS(object): |
| 406 | |
| 407 | def __init__(self, data): |
| 408 | |
| 409 | data_length = len(data) |
| 410 | offset = 0 |
| 411 | |
| 412 | ######################### |
| 413 | # Unpack TLSPlaintext # |
| 414 | ######################### |
| 415 | if data_length >= offset+5: |
| 416 | (self.ContentType, self.ProtocolVersion, self.TLSRecordLength) = struct.unpack( |
| 417 | '!BHH', data[offset:offset+5]) |
| 418 | offset += 5 |
| 419 | else: |
| 420 | raise InsufficientData('%d bytes received by TLS' % data_length) |
| 421 | |
| 422 | # |
| 423 | # For now, only interested in TLS 1.0. |
| 424 | # Reason: SSL2.0 records do not support the server_name extension, which is the primary |
| 425 | # motivation for creating this library. Needs to be updated/extended eventually. |
| 426 | # |
| 427 | if self.ProtocolVersion != TLS1_VERSION and self.ProtocolVersion != SSL3_VERSION and self.ProtocolVersion != TLS1_2_VERSION: |
| 428 | raise UnsupportedOption( |
| 429 | 'Protocol version 0x%x not supported' % self.ProtocolVersion) |
| 430 | |
| 431 | ################# |
| 432 | # Check Size # |
| 433 | ################# |
| 434 | self.recordbytes = self.TLSRecordLength + 5 |
| 435 | if data_length < self.recordbytes: |
| 436 | raise InsufficientData('%d bytes received by TLS' % data_length) |
| 437 | |
| 438 | ######################################################################### |
| 439 | # Content Types - Only Handshake supported for now |
| 440 | ######################################################################### |
| 441 | self.Handshakes = [] |
| 442 | if self.ContentType == SSL3_RT_HANDSHAKE: |
| 443 | |
| 444 | ############################### |
| 445 | # Loop Through Handshakes # |
| 446 | ############################### |
| 447 | while self.recordbytes >= offset + 4: # Need minimum four bytes for the rest to contain another Handshake |
| 448 | |
| 449 | HandshakeType = data[offset] |
| 450 | offset += 1 |
| 451 | |
| 452 | # |
| 453 | # Handshake Record length |
| 454 | # |
| 455 | HandshakeLength = struct.unpack( |
| 456 | '!I', b'\x00' + data[offset:offset+3])[0] |
| 457 | offset += 3 |
| 458 | |
| 459 | # |
| 460 | # Parse Handshake SubType |
| 461 | # |
| 462 | if HandshakeType == SSL3_MT_CLIENT_HELLO: |