tlslite.messagesocket module

Wrapper of TLS RecordLayer providing message-level abstraction

class tlslite.messagesocket.MessageSocket(sock, defragmenter)

Bases: tlslite.recordlayer.RecordLayer

TLS Record Layer socket that provides Message level abstraction

Because the record layer has a hard size limit on sent messages, they need to be fragmented before sending. Similarly, a single record layer record can include multiple handshake protocol messages (very common with ServerHello, Certificate and ServerHelloDone), as such, the user of RecordLayer needs to fragment those records into multiple messages. Unfortunately, fragmentation of messages requires some degree of knowledge about the messages passed and as such is outside scope of pure record layer implementation.

This class tries to provide a useful abstraction for handling Handshake protocol messages.

Variables:
  • recordSize (int) – maximum size of records sent through socket. Messages bigger than this size will be fragmented to smaller chunks. Setting it to higher value than the default 2^14 will make the implementation non RFC compliant and likely not interoperable with other peers.
  • defragmenter (Defragmenter) – defragmenter used for read records
  • unfragmentedDataTypes (tuple) – data types which will be passed as-read, TLS application_data by default
__init__(sock, defragmenter)

Apply TLS Record Layer abstraction to raw network socket.

Parameters:
  • sock (socket.socket) – network socket to wrap
  • defragmenter (Defragmenter) – defragmenter to apply on the records read
flush()

Empty the queue of messages to write

Will fragment the messages and write them in as little records as possible.

Return type:generator
flushBlocking()

Blocking variant of flush().

queueMessage(msg)

Queue message for sending

If the message is of same type as messages in queue, the message is just added to queue.

If the message is of different type as messages in queue, the queue is flushed and then the message is queued.

Return type:generator
queueMessageBlocking(msg)

Blocking variant of queueMessage().

recvMessage()

Read next message in queue

will return a 0 or 1 if the read is blocking, a tuple of RecordHeader3 and Parser in case a message was received.

Return type:generator
recvMessageBlocking()

Blocking variant of recvMessage().

sendMessage(msg)

Fragment and send a message.

If a messages already of same type reside in queue, the message if first added to it and then the queue is flushed.

If the message is of different type than the queue, the queue is flushed, the message is added to queue and the queue is flushed again.

Use the sendRecord() message if you want to send a message outside the queue, or a message of zero size.

Return type:generator
sendMessageBlocking(msg)

Blocking variant of sendMessage().