aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/contrib/tls/record.py
blob: 481c93bccf566f77e0b42a6dc8046db2b47003b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import absolute_import, division, print_function

from enum import Enum

from characteristic import attributes

from construct import Container

from . import _constructs


@attributes(['major', 'minor'])
class ProtocolVersion(object):
    """
    An object representing a ProtocolVersion struct.
    """


@attributes(['type', 'version', 'fragment'])
class TLSPlaintext(object):
    """
    An object representing a TLSPlaintext struct.
    """
    def as_bytes(self):
        return _constructs.TLSPlaintext.build(
            Container(
                type=self.type.value,
                version=Container(major=self.version.major,
                                  minor=self.version.minor),
                length=len(self.fragment),
                fragment=self.fragment
            )
        )

    @classmethod
    def from_bytes(cls, bytes):
        """
        Parse a ``TLSPlaintext`` struct.

        :param bytes: the bytes representing the input.
        :return: TLSPlaintext object.
        """
        construct = _constructs.TLSPlaintext.parse(bytes)
        return cls(
            type=ContentType(construct.type),
            version=ProtocolVersion(
                major=construct.version.major,
                minor=construct.version.minor
            ),
            fragment=construct.fragment
        )


@attributes(['type', 'version', 'fragment'])
class TLSCompressed(object):
    """
    An object representing a TLSCompressed struct.
    """
    @classmethod
    def from_bytes(cls, bytes):
        """
        Parse a ``TLSCompressed`` struct.

        :param bytes: the bytes representing the input.
        :return: TLSCompressed object.
        """
        construct = _constructs.TLSCompressed.parse(bytes)
        return cls(
            type=ContentType(construct.type),
            version=ProtocolVersion(
                major=construct.version.major,
                minor=construct.version.minor
            ),
            fragment=construct.fragment
        )


@attributes(['type', 'version', 'fragment'])
class TLSCiphertext(object):
    """
    An object representing a TLSCiphertext struct.
    """
    @classmethod
    def from_bytes(cls, bytes):
        """
        Parse a ``TLSCiphertext`` struct.

        :param bytes: the bytes representing the input.
        :return: TLSCiphertext object.
        """
        construct = _constructs.TLSCiphertext.parse(bytes)
        return cls(
            type=ContentType(construct.type),
            version=ProtocolVersion(
                major=construct.version.major,
                minor=construct.version.minor
            ),
            fragment=construct.fragment
        )


class ContentType(Enum):
    CHANGE_CIPHER_SPEC = 20
    ALERT = 21
    HANDSHAKE = 22
    APPLICATION_DATA = 23