aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/contrib/tls/hello_message.py
blob: 23cd872bb22fa5e64d310c7392c71aa3c55e2637 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 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 six import BytesIO

from . import _constructs


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


@attributes(['gmt_unix_time', 'random_bytes'])
class Random(object):
    """
    An object representing a Random struct.
    """


@attributes(['type', 'data'])
class Extension(object):
    """
    An object representing an Extension struct.
    """
    def as_bytes(self):
        return _constructs.Extension.build(Container(
            type=self.type.value, length=len(self.data), data=self.data))


@attributes(['client_version', 'random', 'session_id', 'cipher_suites',
             'compression_methods', 'extensions'])
class ClientHello(object):
    """
    An object representing a ClientHello message.
    """
    def as_bytes(self):
        return _constructs.ClientHello.build(
            Container(
                version=Container(major=self.client_version.major,
                                  minor=self.client_version.minor),
                random=Container(
                    gmt_unix_time=self.random.gmt_unix_time,
                    random_bytes=self.random.random_bytes
                ),
                session_id=Container(length=len(self.session_id),
                                     session_id=self.session_id),
                cipher_suites=Container(length=len(self.cipher_suites) * 2,
                                        cipher_suites=self.cipher_suites),
                compression_methods=Container(
                    length=len(self.compression_methods),
                    compression_methods=self.compression_methods
                ),
                extensions_length=sum([2 + 2 + len(ext.data)
                                       for ext in self.extensions]),
                extensions_bytes=b''.join(
                    [ext.as_bytes() for ext in self.extensions]
                )
            )
        )

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

        :param bytes: the bytes representing the input.
        :return: ClientHello object.
        """
        construct = _constructs.ClientHello.parse(bytes)
        # XXX Is there a better way in Construct to parse an array of
        # variable-length structs?
        extensions = []
        extensions_io = BytesIO(construct.extensions_bytes)
        while extensions_io.tell() < construct.extensions_length:
            extension_construct = _constructs.Extension.parse_stream(
                extensions_io)
            extensions.append(
                Extension(type=ExtensionType(extension_construct.type),
                          data=extension_construct.data))
        return ClientHello(
            client_version=ProtocolVersion(
                major=construct.version.major,
                minor=construct.version.minor,
            ),
            random=Random(
                gmt_unix_time=construct.random.gmt_unix_time,
                random_bytes=construct.random.random_bytes,
            ),
            session_id=construct.session_id.session_id,
            # TODO: cipher suites should be enums
            cipher_suites=construct.cipher_suites.cipher_suites,
            compression_methods=(
                construct.compression_methods.compression_methods
            ),
            extensions=extensions,
        )


class ExtensionType(Enum):
    SIGNATURE_ALGORITHMS = 13
    # XXX: See http://tools.ietf.org/html/rfc5246#ref-TLSEXT


@attributes(['server_version', 'random', 'session_id', 'cipher_suite',
             'compression_method', 'extensions'])
class ServerHello(object):
    """
    An object representing a ServerHello message.
    """
    def as_bytes(self):
        return _constructs.ServerHello.build(
            Container(
                version=Container(major=self.server_version.major,
                                  minor=self.server_version.minor),
                random=Container(
                    gmt_unix_time=self.random.gmt_unix_time,
                    random_bytes=self.random.random_bytes
                ),
                session_id=Container(length=len(self.session_id),
                                     session_id=self.session_id),
                cipher_suite=self.cipher_suite,
                compression_method=self.compression_method.value,
                extensions_length=sum([2 + 2 + len(ext.data)
                                       for ext in self.extensions]),
                extensions_bytes=b''.join(
                    [ext.as_bytes() for ext in self.extensions]
                )
            )
        )

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

        :param bytes: the bytes representing the input.
        :return: ServerHello object.
        """
        construct = _constructs.ServerHello.parse(bytes)
        # XXX: Find a better way to parse extensions
        extensions = []
        extensions_io = BytesIO(construct.extensions_bytes)
        while extensions_io.tell() < construct.extensions_length:
            extension_construct = _constructs.Extension.parse_stream(
                extensions_io)
            extensions.append(
                Extension(type=ExtensionType(extension_construct.type),
                          data=extension_construct.data))
        return ServerHello(
            server_version=ProtocolVersion(
                major=construct.version.major,
                minor=construct.version.minor,
            ),
            random=Random(
                gmt_unix_time=construct.random.gmt_unix_time,
                random_bytes=construct.random.random_bytes,
            ),
            session_id=construct.session_id.session_id,
            cipher_suite=construct.cipher_suite,
            compression_method=CompressionMethod(construct.compression_method),
            extensions=extensions,
        )


class CompressionMethod(Enum):
    NULL = 0