Exemplo n.º 1
0
 def UBInt8EnumMappedStruct(self):  # noqa
     """
     A :py:class:`construct.core.Struct` containing an
     :py:func:`tls._common._constructs.EnumSwitch` that switches on
     :py:class:`IntegerEnum`.  The struct's ``value`` field varies
     depending on the value of its ``type`` and the corresponding
     enum member specified in the ``value_choices`` dictionary
     passed to the :py:func:`tls._common._constructs.EnumSwitch`.
     """
     return Struct(
         "UBInt8EnumMappedStruct",
         *EnumSwitch(type_field=UBInt8("type"),
                     type_enum=IntegerEnum,
                     value_field="value",
                     value_choices={
                         IntegerEnum.FIRST: UBInt16("UBInt16"),
                         IntegerEnum.SECOND: UBInt24("UBInt24")
                     }))
Exemplo n.º 2
0
def test_ubint24():
    assert isinstance(UBInt24("test"), _UBInt24)
Exemplo n.º 3
0
ASN1Cert = Struct(
    "ASN1Cert",
    SizeWithin(UBInt32("length"), min_size=1, max_size=2**24 - 1),
    Bytes("asn1_cert", lambda ctx: ctx.length),
)

Certificate = Struct(
    "Certificate",
    SizeWithin(UBInt32("certificates_length"), min_size=1, max_size=2**24 - 1),
    Bytes("certificates_bytes", lambda ctx: ctx.certificates_length),
)

Handshake = Struct(
    "Handshake",
    UBInt8("msg_type"),
    UBInt24("length"),
    Bytes("body", lambda ctx: ctx.length),
)

Alert = Struct(
    "Alert",
    UBInt8("level"),
    UBInt8("description"),
)

URLAndHash = Struct(
    "url_and_hash",
    SizeWithin(UBInt16("length"), min_size=1, max_size=2**16 - 1),
    Bytes("url", lambda ctx: ctx.length),
    TLSOneOf(UBInt8('padding'), [1]),
    Bytes("sha1_hash", 20),
Exemplo n.º 4
0
    def test_round_trip(self, prefixed_bytes, bytestring, encoded):
        """
        :py:meth:`tls._common._constructs.PrefixedBytes` decodes a
        length-prefixed binary sequence encoded by
        :py:meth:`tls._common._constructs.PrefixedBytes` and vice versa.
        """
        parsed = prefixed_bytes.parse(encoded)
        assert prefixed_bytes.build(parsed) == encoded
        unparsed = prefixed_bytes.build(bytestring)
        assert prefixed_bytes.parse(unparsed) == bytestring


@pytest.mark.parametrize(
    "bytestring,encoded,length_field",
    [(b"", b"\x00\x00" + b"", UBInt16("length")),
     (b"some value", b"\x00\x00\x0A" + b"some value", UBInt24("length"))])
class TestPrefixedBytesWithOverriddenLength(object):
    """
    Tests for :py:func:`tls._common._constructs.PrefixedBytes` with a
    user-supplied ``length_field`` construct.
    """
    def test_build(self, bytestring, encoded, length_field):
        """
        :py:meth:`tls._common._constructs.PrefixedBytes` uses the supplied
        ``length_field`` to encode :class:`bytes` as a length-prefix
        binary sequence.
        """
        prefixed_bytes = PrefixedBytes("name", length_field=length_field)
        assert prefixed_bytes.build(bytestring) == encoded

    def test_parse(self, bytestring, encoded, length_field):
Exemplo n.º 5
0
    PrefixedBytes("dh_p", UBInt16("dh_p_length")),
    PrefixedBytes("dh_g", UBInt16("dh_g_length")),
    PrefixedBytes("dh_Ys", UBInt16("dh_Ys_length")),
)

PreMasterSecret = Struct(
    "pre_master_secret",
    ProtocolVersion,
    Bytes("random_bytes", 46),
)

ASN1Cert = Struct(
    "ASN1Cert",
    PrefixedBytes(
        "asn1_cert",
        SizeWithin(UBInt24("length"), min_size=1, max_size=2**24 - 1)))

# https://tools.ietf.org/html/rfc5246#section-7.4.2
Certificate = Struct(
    "Certificate",
    TLSPrefixedArray("certificate_list",
                     ASN1Cert,
                     length_validator=partial(SizeWithin,
                                              min_size=1,
                                              max_size=2**24 - 1),
                     length_field_size=UBInt24),
)

Handshake = Struct(
    "Handshake",
    UBInt8("msg_type"),