tls¶
Python TLS API¶
-
class
ClientTLS
(server_hostname, cipher_suites, trust_root=DEFAULT, client_certificate_store=None)¶ Parameters: - server_hostname (bytes) – The hostname of the server that will be connected to.
- cipher_suites (list) – The list of supported cipher suites.
- trust_root (TrustStore) – The trust root.
- client_certificate_store (ClientCertificateStore) – The certificate that the client will present to the server.
-
start
(write_to_wire_callback, wire_close_callback, verify_callback=None)¶ Parameters: - write_to_wire_callback (callable) – Callable of one argument of
bytes
type. It will be called when TLS data should be sent over the transport. - wire_close_callback (callable) – Callable of one argument of
bool
type, calledimmediate
. It will be called when the TLS protocols mandates a transport shutdown. The read side of the connection must always be shut down immediately and no further data should be delivered to the connection. Ifimmediate
is True, then the transport should close the write side of the transport and free all associated resources as soon as possible. Ifimmediate
is False, then the transport should make a reasonable attempt to deliver the bytes already sent towrite_to_wire_callback
(which will be aclose_alert
message), meaning it can wait for a configured timeout before closing down the write side of the connection. - verify_callback (callable) – Callable of two arguments: a list of
Certificate
objects, and aConnection
object. It will be called once per negotiation with a list of Certificates and the connection object. The certificates are in chain order, starting with the leaf certificate and ending with the root-most certificate. Specifying a verify_callback does not override the basic verification that PyTLS does, such as certificate chain validation, basic certificate checks, and hostname validation. verify_callback has no particular contract; return values will be ignored. If any exception is raised, the connection will be invalidated and any future calls toConnection.data_from_wire()
orConnection.data_from_application()
will raiseInvalidatedError
. It’s up to the user to decide what to do during verification, such as invokingConnection.send_alert()
or simply closing the connection.
Return Connection: the client connection.
Start a TLS connection. The write_to_wire_callback will be invoked with the initial data for TLS negotiation.
- write_to_wire_callback (callable) – Callable of one argument of
-
class
ServerTLS
(certificates, dh_params=None)¶ Parameters: - certificates (ServerCertificates) – A collection of server certificates, usually an instance of either
ServerCertificateChain
orSNIServerCertificates
. - dh_params (bytes) – Optional diffie-hellman parameters in DER format.
-
start
(write_to_wire_callback, verify_callback=None)¶ Return Connection: the server connection. See ClientTLS.start.
- certificates (ServerCertificates) – A collection of server certificates, usually an instance of either
-
class
Connection
¶ -
data_from_wire
(input)¶ Parameters: input (bytes) – Data that was received from some low-level transport and should be processed by the TLS implementation.
Return bytes: Any application data that was in the input.
Raises: - TLSAlertError – When certain TLS Alert messages occur in the input.
- BadTLSDataError – When the input data was somehow invalid, such as when decryption failed or the protocol was not followed.
- InvalidatedError – When the connection has been invalidated due to a previous error and will accept no further data.
Given data read from a transport, invoke any callbacks for e.g. connection negotiation or heartbeats, etc, and return decrypted application data, if any. If the input data is somehow invalid, a TLS Alert message will be passed to the write callback, and a BadTLSDataError will be raised. In certain cases of receipt of invalid data, after (sometimes) sending a TLS Alert, this connection will be invalidated such that data_from_wire and data_from_application will raise
InvalidatedError
. Note that any incomplete data in the input may be buffered by the implementation until further calls to data_from_wire complete the messages.
-
data_from_application
(output)¶ Parameters: output (bytes) – Application data to encrypt and send over the transport. Raises: InvalidatedError – When the connection has been invalidated due to a previous error. Given plaintext application data, invoke the write callback with the encrypted data.
-
send_alert
(alert_code, level=None)¶ Parameters: - alert_code – The alert code to send in a TLS Alert message. Must be one of the constants specified in this module (TBD).
- level – Must be ALERT_WARNING or ALERT_FATAL. If not specified, a default will be specified based on alert_code if the TLS specification mandates a particular level for the code.
Raises: InvalidAlertLevel – When an alert_code is passed that is incompatible with the passed level.
Invoke the write callback with a TLS alert message. Usually this is invoked automatically by a method like data_from_wire, but it may be useful to call this in your verify_callback. If the level is passed, the alert code must be compatible according to the TLS spec, otherwise
InvalidAlertLevel
will be raised. If the level is not passed and the alert code is ambiguous according to the spec,InvalidAlertLevel
will also be raised in this case. Certain send_alert() calls may invalidate the connection, in which case further calls to data_from_application and data_from_wire will fail withInvalidatedError
.
-
application_finished
()¶ Indicate that the application is finished sending data to
data_from_application
. If the connection has already started, this will invoke the write callback with a TLS Finished message.
-
Certificate APIs¶
Definition: a “leaf” certificate is a non-CA certificate.
-
class
ClientCertificateStore
¶ -
get_certificate_chain_for_roots
(roots, certificate_chain_callback)¶ Parameters: This method is intended to be implemented by the user, NOT called by the user.
Get the client certificate chain to send to the server, based on the roots specified by the server. The result should be specified by calling certificate_chain_callback. It must be passed either a single certificate chain (with ONE leaf certificate that MUST have a private key), or None to indicate no client certificates are available.
The certificates must chain to one of the roots specified by the server, or
NoCertificateChainError
will be raised. Invoking this callback more than once will result inInvalidatedError
being raised. The callback may also raiseLeafCertificateHasNoPrivateKeyError
,MoreThanOneLeafCertificateError
, orNoLeafCertificateError
.
-
get_default_certificate_chain
(certificate_chain_callback)¶ Parameters: certificate_chain_callback (callable) – The callback that this method should eventually invoke to specify the client certificates to send. This method is intended to be implemented by the user, NOT called by the user.
Get the default client certificate in the case that the server did not provide roots that the client certificate must chain to. The result should be specified by calling certificate_chain_callback. It must be passed either a single certificate chain (with ONE leaf certificate that MUST have a private key), or None to indicate no client certificates are available.
Invoking this callback more than once will result in
InvalidatedError
being raised. The callback may also raiseLeafCertificateHasNoPrivateKeyError
,MoreThanOneLeafCertificateError
, orNoLeafCertificateError
.
-
-
class
TrustStore
(certificates)¶ Parameters: certificates (set) – A set of Certificate objects, none of which may have private keys. Create a store of trusted CA certificates to be used with ClientTLS. No methods are public. If any private keys are found in any of the certificates,
ExtraneousPrivateKeyError
will be raised.
-
class
ServerCertificates
¶ An abstract base class representing the type of operations possible on a collection of server certificates.
-
get_certificate_chain_for_server_name
(server_name, certificate_chain_callback)¶ Parameters: This method is intended to be implemented by the user, NOT called by the user.
Get the server chain to send to the client when the client is using Server Name Indication (SNI). Implement this method to invoke the certificate_chain_callback with a collection of certificates with ONE leaf certificate that MUST have a private key. None may be passed to the certificate_chain_callback in case no certificates can be found, in which case a TLS Alert will be sent. Passing a “default” certificate chain that doesn’t match the server name is acceptable.
Invoking this callback more than once will result in
InvalidatedError
being raised. The callback may be invoked at any point after this method is invoked; it needn’t be invoked synchronously. The callback may also raiseLeafCertificateHasNoPrivateKeyError
, orNoLeafCertificateError
.
-
-
class
ServerCertificateChain
(chain)¶ provides ServerCertificates
Parameters: chain (set) – A single chain of certificates, the leaf of which MUSt have a private key. Specify the certificate chain that will be sent to all clients.
-
class
SNIServerCertificates
(certificates, default=set())¶ provides ServerCertificates
Parameters: Represents a SNI-capable set of certificates for use with ServerTLS.
Exceptions¶
-
class
TLSAlertError
¶ Attribute alert_code: code of the alert Attribute alert_level: level of the alert Raised when a TLS Alert message was received from the peer.
-
class
BadTLSDataError
¶ Raised when invalid TLS data was received from the peer.
-
class
InvalidatedError
¶ Raised when it’s no longer valid to call a method or callback based on previous state. e.g., a certificate_chain_callback from
ServerCertificates.get_certificate_chain_for_server_name
being invoked a second time, orConnection.data_from_wire
being invoked after a connection has been invalidated due to incorrect data.
-
class
InvalidAlertLevel
¶ Raised when an alert code is not allowed to have the specified alert level.
-
class
LeafCertificateHasNoPrivateKeyError
¶ Raised when the leaf certificate doesn’t have a private key.
-
class
MoreThanOneLeafCertificateError
¶ Raised when there’s more than one leaf certificate in a set of certificates.
-
class
NoLeafCertificateError
¶ Raised when there are no leaf certificates in a set of certificates. A “leaf” is defined as a non-CA certificate.
-
class
NoCertificateChainError
¶ A certificate chain cannot be found between a specified leaf and a specified root.
-
class
ExtraneousPrivateKeyError
¶ A private key was found associated with a certificate when it shouldn’t have been.
TODO¶
- Certificates
- TODO: design factories for building sets or chains of certificates from PEM
files that are strict about:
- private keys where they don’t belong, or lack of private keys where we should have them
- chain files that have things that aren’t a part of the chain
- TODO: design factories for building sets or chains of certificates from PEM
files that are strict about:
- Determine better names for methods
- look through the past ten years of CVEs on OpenSSL, SecureTransport, GnuTLS,
PolarSSL, etc.
- old TLS Finished security flaw, having to do with half-closed sockets.
- timing attacks: http://armoredbarista.blogspot.de/2014/04/easter-hack-even-more-critical-bugs-in.html
- Determine if the TLS implementation needs a clock (are there specific
timeouts we need to wait for, etc).
- look up what the requirements for responding to a handshake. scenario: client sends ClientHello (to renegotiate), server already had a huge amount of data in its write buffer. how long should client wait to receive ServerHello?
- add a method to ServerCertificates for non-SNI case
- Actually, I don’t think we need one yet. What’s the use case for dynamic lookup of non-SNI server certificates?
- alerts
- perhaps alert() should be removed.
- figure out which TLS Alerts actually matter.
- make alert take constants for level and code instead of integers.
- pin against port and host (???)
- connections should probably have a .cipher_suite, .tls_version, .session_id, .tls_extensions, and lots more
- allow disabling certain options (tls versions or algorithm choices) that we know are less secure than mandatory options.
- alternative cert validation support such as DANE or TACK.
Future Work¶
- Session resumption:
- ensure there’s a solid way to invalidate session-resumption data on receipt of an alert (on both client and server)
- maybe allow clients to request renegotiation, if there are good use cases.
- maybe allow servers to request renegotiation, if there are good use cases.
- Is there a use case for making dh_params per-server-cert-chain in the SNI case? Some rumblings in this area, but no clear reason.
No. | Client | Server |
---|---|---|
1 | Send: - ClientHello |
<wait for ClientHello> |
2 | <wait for ServerHelloDone> | Send:
|
3 | Send:
|
<wait for client’s Finished> |
4 | <wait for server’s Finished> | Send:
|
Hello Messages¶
- ClientHello and ServerHello establish:
- Protocol version
- Session ID
- Cipher suite
- Compression method
- And generate:
- ClientHello.random
- ServerHello.random
- When the client sends a ClientHello to server, it can either respond with a ServerHello, or ignore it, leading to a fatal error and the closing of the connection.
2.Server:¶
- Certificate:
- If it is to be authenticated
- ServerKeyExchange:
- If server doesn’t have a certificate, or
- if server’s certificate is for signing only
- CertificateRequest:
- If:
- server is authenticated, and
- it is appropriate to the cipher suite selected
- If:
- ServerHelloDone:
- To indicate that the hello-message phase of the handshake is complete
3.Client:¶
- Certificate:
- If the server sent a CertificateRequest
- ClientKeyExchange:
- The content of this message depends on the public key algorithm selected between ClientHello and ServerHello
- CertificateVerify:
- If the client certificate sent is with signing ability
- Digitally signed
- Verifies the possession of private key in certificate
- ChangeCipherSpec:
- Send this and copy the pending cipher spec into the current cipher spec
- Finished
- Sent under the new algorithms, keys, and secrets
4.Server:¶
- ChangeCipherSpec:
- Send this and copy the pending cipher spec into the current cipher spec
- Finished
- Sent under the new cipher spec
Session Resumption:¶
No. | Client | Server |
---|---|---|
1 | Send:
|
<wait for ClientHello> |
2 | <wait for Server’s Finished> | Check session cache for a match.
|
3 | <wait for server’s Finished> | Send:
|
4 | Send:
|
<wait for client’s Finished> |
1.Client:¶
- ClientHello:
- Sent using the session ID of the session to be resumed.
3.Server:¶
- ServerHello:
- Sent with the same Session ID value (as in the ClientHello message).
Server as a state machine:¶
Input | Current State | Next State | Output |
---|---|---|---|
ClientHello | IDLE | CHECK_SESSION_CACHE | – |
id_found_somehow |
CHECK_SESSION_CACHE | WAIT_RESUME | (ServerHello, [ChangeCipherSpec], Finished) |
id_not_found_somehow |
CHECK_SESSION_CACHE | WAIT | (ServerHello, Certificate*, ServerKeyExchange*, CertificateRequest*, ServerHelloDone) |
Finished (from Client) | WAIT | APP_DATA | ([ChangeCipherSpec], Finished) |
Finished (from Client) | WAIT_RESUME | APP_DATA | – |
ClientHello | APP_DATA | APP_DATA | Alert(no_renegotiation) |
Client as a state machine:¶
Input | Current State | Next State | Output |
---|---|---|---|
– | IDLE | WAIT_1 | ClientHello |
ServerHelloDone | WAIT_1 | WAIT_2 | (Certificate*, ClientKeyExchange, CertificateVerify*, [ChangeCipherSpec], Finished) |
Finished (from Server) | WAIT_1 | APP_DATA | ([ChangeCipherSpec], Finished) |
Finished (from Server) | WAIT_2 | APP_DATA | – |
HelloRequest | APP_DATA | APP_DATA | Alert(no_renegotiation) |
* Indicates optional or situation-dependent messages that are not always sent.
Note: To help avoid pipeline stalls, ChangeCipherSpec is an independent TLS protocol content type, and is not actually a TLS handshake message.
Common states for both state machines:¶
Input | Current State | Next State | Output |
---|---|---|---|
Alert(close_notify) | APP_DATA | SHUTDOWN | (Alert(close_notify),
close_callback(False) ,
indicate_EOF_to_the_application_somehow ) |
Session.alert(close_notify) |
APP_DATA | HOST_INITIATED_CLOSING | Alert(close_notify) |
Alert(close_notify) | HOST_INITIATED_CLOSING | SHUTDOWN | (close_callback(True) ,
indicate_EOF_to_the_application_somehow ) |
Session.write_data |
APP_DATA | APP_DATA | write_callback() |
tls¶
tls package¶
Subpackages¶
tls.test package¶
Submodules¶
tls.test.test_alert module¶
tls.test.test_ciphersuites module¶
tls.test.test_hello_messages module¶
-
class
tls.test.test_hello_messages.
TestClientHello
¶ Bases:
object
Tests for the parsing of ClientHello messages.
-
test_as_bytes_no_extensions
()¶ ClientHello.as_bytes()
returns the bytes it was created with
-
test_as_bytes_with_extensions
()¶ ClientHello.as_bytes()
returns the bytes it was created with
-
test_resumption_no_extensions
()¶ parse_client_hello()
returns an instance ofClientHello
.
-
-
class
tls.test.test_hello_messages.
TestServerHello
¶ Bases:
object
Tests for the parsing of ServerHello messages.
-
test_as_bytes_no_extensions
()¶ ServerHello.as_bytes()
returns the bytes it was created with
-
test_as_bytes_with_extensions
()¶ ServerHello.as_bytes()
returns the bytes it was created with
-
test_parse_server_hello
()¶ parse_server_hello()
returns an instance ofServerHello
.
-
test_parse_server_hello_extensions
()¶ parse_server_hello()
returns an instance ofServerHello
.
-
tls.test.test_message module¶
-
class
tls.test.test_message.
TestCertificateParsing
¶ Bases:
object
Tests for parsing of Certificate messages.
-
class
tls.test.test_message.
TestCertificateRequestParsing
¶ Bases:
object
Tests for parsing of CertificateRequest messages.
-
class
tls.test.test_message.
TestHandshakeStructParsing
¶ Bases:
object
Tests for parsing of Handshake structs.
tls.test.test_record module¶
-
class
tls.test.test_record.
TestTLSCiphertextParser
¶ Bases:
object
Tests for parsing of TLSCiphertext records.
-
test_parse_tls_ciphertext_handshake
()¶ TLSCiphertext
, which has attributes representing all the fields in the TLSCiphertext struct.
-
-
class
tls.test.test_record.
TestTLSCompressedParsing
¶ Bases:
object
Tests for parsing of TLSCompressed records.
-
test_incomplete_packet
()¶ Reject an incomplete packet
-
test_not_enough_data_to_fragment
()¶ Detect insufficient data to fragment.
-
test_parse_tls_compressed_handshake
()¶ TLSCompressed
, which has attributes representing all the fields in the TLSCompressed struct.
-
test_parse_tls_compressed_wrong_type
()¶ Raise an error when the type is not one of those defined in ContentType
-
-
class
tls.test.test_record.
TestTLSPlaintextParsing
¶ Bases:
object
Tests for parsing of TLSPlaintext records.
-
test_as_bytes
()¶ Construct a TLSPlaintext object as bytes.
-
test_incomplete_packet
()¶ Reject an incomplete packet
-
test_not_enough_data_to_fragment
()¶ Detect insufficient data to fragment.
-
test_parse_tls_plaintext_handshake
()¶ parse_tls_plaintext()
returns an instance ofTLSPlaintext
, which has attributes representing all the fields in the TLSPlaintext struct.
-
test_parse_tls_plaintext_wrong_type
()¶ Raise an error when the type is not one of those defined in ContentType
-
tls.test.test_utils module¶
-
class
tls.test.test_utils.
IntegerEnum
¶ Bases:
enum.Enum
An enum of
int
instances. Used as a test fixture.
-
class
tls.test.test_utils.
TestBytesAdapter
¶ Bases:
object
Tests for
tls.utils.BytesAdapter
.-
bytes_adapted
()¶ A
tls.utils.BytesAdapter
that adapts a trivialconstruct.Construct()
.
-
test_decode_passes_value_through
(bytes_adapted, value)¶
-
-
class
tls.test.test_utils.
TestEnumClass
¶ Bases:
object
Tests for
tls.utils.EnumClass()
.-
UBInt8Enum
()¶ A
tls.utils.EnumClass()
that adaptsIntegerEnum
‘s members toUBInt8()
.
-
test_build
(UBInt8Enum)¶ tls.utils.EnumClass()
encodes members of its enum according to its construct.
-
test_build_enum_has_wrong_type
(UBInt8Enum)¶ tls.utils.EnumClass()
raisesconstruct.adapters.MappingError
when encoding something that isn’t a member of its enum.
-
test_parse
(UBInt8Enum)¶ tls.utils.EnumClass()
decodes a binary sequence as members of its enum via its construct.
-
-
class
tls.test.test_utils.
TestEnumSwitch
¶ Bases:
object
Tests for
tls.utils.EnumSwitch()
.-
UBInt8EnumMappedStruct
()¶ A
construct.core.Struct
containing antls.utils.EnumSwitch()
that switches onIntegerEnum
. The struct’svalue
field varies depending on the value of itstype
and the corresponding enum member specified in thevalue_choices
dictionary passed to thetls.utils.EnumSwitch()
.
-
test_build
(UBInt8EnumMappedStruct, type_, value, encoded)¶ A struct that contains
tls.utils.EnumSwitch()
encodes itsvalue_field
according to the enum member specified in itstype_field
.
-
test_parse
(UBInt8EnumMappedStruct, type_, value, encoded)¶ A struct that contains
tls.utils.EnumSwitch()
decodes its value field according to the enum member specified by itstype_field
.
-
test_round_trip
(UBInt8EnumMappedStruct, type_, value, encoded)¶ A struct that contains
tls.utils.EnumSwitch()
decodes a binary sequence encoded by a struct with that sametls.utils.EnumSwitch()
and vice versa.
-
-
class
tls.test.test_utils.
TestPrefixedBytesWithDefaultLength
¶ Bases:
object
Tests for
tls.utils.PrefixedBytes()
with the defaultconstruct.macros.UBInt8()
length_field
construct.-
prefixed_bytes
()¶ A trivial
tls.utils.PrefixedBytes()
construct with the defaultconstruct.macros.UBInt8()
length field.
-
test_build
(prefixed_bytes, bytestring, encoded)¶ tls.utils.PrefixedBytes()
encodesbytes
as a length-prefixed byte sequence.
-
test_parse
(prefixed_bytes, bytestring, encoded)¶ tls.utils.PrefixedBytes()
decodes a length-prefixed byte sequence asbytes
.
-
test_round_trip
(prefixed_bytes, bytestring, encoded)¶ tls.utils.PrefixedBytes()
decodes a length-prefixed binary sequence encoded bytls.utils.PrefixedBytes()
and vice versa.
-
-
class
tls.test.test_utils.
TestPrefixedBytesWithOverriddenLength
¶ Bases:
object
Tests for
tls.utils.PrefixedBytes()
with a user-suppliedlength_field
construct.-
test_build
(bytestring, encoded, length_field)¶ tls.utils.PrefixedBytes()
uses the suppliedlength_field
to encodebytes
as a length-prefix binary sequence.
-
test_parse
(bytestring, encoded, length_field)¶ tls.utils.PrefixedBytes()
decodes a length-prefixed binary sequence intobytes
according to the suppliedlength_field
.
-
test_round_trip
(bytestring, encoded, length_field)¶ tls.utils.PrefixedBytes()
decodes a length-prefixed binary sequence encoded bytls.utils.PrefixedBytes()
when the two share alength_field
and vice versa.
-
-
class
tls.test.test_utils.
TestTLSPrefixedArray
¶ Bases:
object
Tests for
tls.utils.TLSPrefixedArray()
.-
test_build
(tls_array, ints, uint8_encoded)¶ A
tls.utils.TLSPrefixedArray()
specialized on a givenconstruct.Construct()
encodes a sequence of objects as a 16-bit length followed by each object as encoded by that construct.
-
test_parse
(tls_array, ints, uint8_encoded)¶ A
tls.utils.TLSPrefixedArray()
specialized on a givenconstruct.Construct()
decodes a binary sequence, prefixed by its 16-bit length, as alist
of objects decoded by that construct.
-
test_round_trip
(tls_array, ints, uint8_encoded)¶ A
tls.utils.TLSPrefixedArray()
decodes a length-prefixed binary sequence encoded by atls.utils.TLSPrefixedArray()
specialized on the same construct and vice versa.
-
tls_array
()¶ A
tls.utils.TLSPrefixedArray()
ofconstruct.macros.UBInt8()
.
-
Module contents¶
Submodules¶
tls.alert_message module¶
tls.ciphersuites module¶
tls.exceptions module¶
tls.hello_message module¶
-
class
tls.hello_message.
ClientHello
(*args, **kw)¶ Bases:
object
An object representing a ClientHello message.
-
classmethod
from_bytes
(bytes)¶ Parse a
ClientHello
struct.Parameters: bytes – the bytes representing the input. Returns: ClientHello object.
-
classmethod
-
class
tls.hello_message.
Extension
(*args, **kw)¶ Bases:
object
An object representing an Extension struct.
tls.message module¶
-
class
tls.message.
Certificate
(*args, **kw)¶ Bases:
object
An object representing a Certificate struct.
-
classmethod
from_bytes
(bytes)¶ Parse a
Certificate
struct.Parameters: bytes – the bytes representing the input. Returns: Certificate object.
-
classmethod
-
class
tls.message.
CertificateRequest
(*args, **kw)¶ Bases:
object
An object representing a CertificateRequest struct.
-
classmethod
from_bytes
(bytes)¶ Parse a
CertificateRequest
struct.Parameters: bytes – the bytes representing the input. Returns: CertificateRequest object.
-
classmethod
-
class
tls.message.
Handshake
(*args, **kw)¶ Bases:
object
An object representing a Handshake struct.
-
classmethod
from_bytes
(bytes)¶ Parse a
Handshake
struct.Parameters: bytes – the bytes representing the input. Returns: Handshake object.
-
classmethod
-
class
tls.message.
PreMasterSecret
(*args, **kw)¶ Bases:
object
An object representing a PreMasterSecret struct.
-
classmethod
from_bytes
(bytes)¶ Parse a
PreMasterSecret
struct.Parameters: bytes – the bytes representing the input. Returns: CertificateRequest object.
-
classmethod
tls.record module¶
-
class
tls.record.
ProtocolVersion
(*args, **kw)¶ Bases:
object
An object representing a ProtocolVersion struct.
-
class
tls.record.
TLSCiphertext
(*args, **kw)¶ Bases:
object
An object representing a TLSCiphertext struct.
-
classmethod
from_bytes
(bytes)¶ Parse a
TLSCiphertext
struct.Parameters: bytes – the bytes representing the input. Returns: TLSCiphertext object.
-
classmethod
tls.utils module¶
-
tls.utils.
EnumClass
(type_field, type_enum)¶ Maps the members of an
enum.Enum
to a single kind ofconstruct.Construct
.Parameters: - type_field (
construct.Construct
) – The construct that represents the enum’s members. The type of this should correspond to the enum members’ types; for instance, an enum with a maximum value of 65535 would use aconstruct.macros.UBInt16
. - type_enum (
enum.Enum
) – The enum to encode and decode.
- type_field (
-
tls.utils.
EnumSwitch
(type_field, type_enum, value_field, value_choices)¶ Maps the members of an
enum.Enum
to arbitraryconstruct.Constructs()
. It returns a tuple intended to be spliced into anotherconstruct.Construct()
‘s definition:>>> from tls.utils import EnumSwitch >>> import construct, enum >>> class IntEnum(enum.Enum): ... VALUE = 1 ... >>> construct.Struct( ... "name", ... construct.UBInt8("an_integer"), ... *EnumSwitch(type_field=construct.UBInt8("type"), ... type_enum=IntEnum, ... value_field="value", ... value_choices={ ... IntEnum.VALUE: construct.UBInt8("first"), ... }) ... ) ... Struct('name')
Parameters: - type_field (
construct.Construct
) – The construct that represents the enum’s members. The type of this should correspond to the enum members’ types, so an enum with a maximum value of 65535, for example, would use aconstruct.macros.UBInt16
. - type_enum (
enum.Enum
) – The enum to encode and decode. - value_field (
str
) – The attribute name under which this value will be accessible. - value_choices (
dict
) – A dictionary that maps members of type_enum to subconstructs. This followsconstruct.core.Switch()
‘s API, so_default_
will match any members without an explicit mapping.
Returns: A
tuple
of the form (EnumClass()
,construct.core.Switch()
)- type_field (
-
tls.utils.
PrefixedBytes
(name, length_field=FormatField('length'))¶ Length-prefixed binary data. This is like a
construct.macros.PascalString()
that raises aconstrcut.AdaptationError
when encoding something other thanbytes
.Parameters: - name (
str
) – The attribute name under which this value will be accessible. - length_field (a
construct.core.FormatField
) – (optional) The prefixed length field. Defaults toconstruct.macros.UBInt8()
.
- name (
-
tls.utils.
TLSPrefixedArray
(subconn, length_name='length')¶ The TLS vector type. It specializes on another
construct.Construct
and then encodes or decodes an arbitrarily long list or array of those constructs, prepending or reading a leading 16 bit length.Parameters: - subconn (construct.Construct) – The construct this array contains.
- length_field (
str
) – (optional) The attribute name under which theconstruct.macros.UBInt16
representing this array’s length will be accessible. You do not need to provide this when encoding a python sequence!