tlslite.utils.codec module
Classes for reading/writing binary data (such as TLS records).
- exception tlslite.utils.codec.BadCertificateError[source]
Bases:
SyntaxError
Exception raised in case of bad certificate.
- exception tlslite.utils.codec.DecodeError[source]
Bases:
SyntaxError
Exception raised in case of decoding errors.
- class tlslite.utils.codec.Parser(bytes)[source]
Bases:
object
Parser for TLV and LV byte-based encodings.
Parser that can handle arbitrary byte-based encodings usually employed in Type-Length-Value or Length-Value binary encoding protocols like ASN.1 or TLS
Note: if the raw bytes don’t match expected values (like trying to read a 4-byte integer from a 2-byte buffer), most methods will raise a DecodeError exception.
TODO: don’t use an exception used by language parser to indicate errors in application code.
- Variables:
- __init__(bytes)[source]
Bind raw bytes with parser.
- Parameters:
bytes (bytearray) – bytes to be parsed/interpreted
- atLengthCheck()[source]
Check if there is data in structure left for parsing.
Returns True if the whole structure was parsed, False if there is some data left.
Will raise an exception if overflow occured (amount of data read was greater than expected size)
- getVarBytes(lengthLength)[source]
Read a variable length string with a fixed length.
see Writer.add_var_bytes() for an inverse of this method
- getVarTupleList(elemLength, elemNum, lengthLength)[source]
Read a variable length list of same sized tuples.
- setLengthCheck(length)[source]
Set length of struct and start a length check for parsing.
- Parameters:
length (int) – expected size of parsed struct in bytes
- class tlslite.utils.codec.Writer[source]
Bases:
object
Serialisation helper for complex byte-based structures.
- add(x, length)[source]
Add a single positive integer value x, encode it in length bytes
Encode positive integer x in big-endian format using length bytes, add to the internal buffer.
- addFixSeq(seq, length)[source]
Add a list of items, encode every item in length bytes
Uses the unbounded iterable seq to produce items, each of which is then encoded to length bytes
- addVarSeq(seq, length, lengthLength)[source]
Add a bounded list of same-sized values
Create a list of specific length with all items being of the same size
- tlslite.utils.codec.bytes_to_int(bytes, byteorder='big', *, signed=False)
Return the integer represented by the given array of bytes.
- bytes
Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.
- signed
Indicates whether two’s complement is used to represent the integer.