| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import unittest |
| 4 |
import collections |
| 5 |
|
| 6 |
from cryptodatahub.common.exception import InvalidValue |
| 7 |
|
| 8 |
from cryptoparser.common.exception import InvalidType, NotEnoughData |
| 9 |
from cryptoparser.tls.openvpn import ( |
| 10 |
OpenVpnPacketAckV1, |
| 11 |
OpenVpnPacketBase, |
| 12 |
OpenVpnPacketControlV1, |
| 13 |
OpenVpnPacketHardResetClientV2, |
| 14 |
OpenVpnPacketHardResetServerV2, |
| 15 |
OpenVpnPacketVariant, |
| 16 |
OpenVpnPacketWrapperTcp, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class TestOpenVpnPacketWrapperTcp(unittest.TestCase): |
| 21 |
def test_error_not_enough_data(self): |
| 22 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 23 |
OpenVpnPacketWrapperTcp.parse_exact_size(b'\x00\x02\x00') |
| 24 |
|
| 25 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 26 |
|
| 27 |
def test_parse(self): |
| 28 |
packet = OpenVpnPacketWrapperTcp.parse_exact_size(b'\x00\x08\x00\x01\x02\x03\x04\x05\x06\x07') |
| 29 |
self.assertEqual(packet.payload, b'\x00\x01\x02\x03\x04\x05\x06\x07') |
| 30 |
|
| 31 |
def test_compose(self): |
| 32 |
packet = OpenVpnPacketWrapperTcp(b'\x00\x01\x02\x03\x04\x05\x06\x07') |
| 33 |
self.assertEqual(packet.compose(), b'\x00\x08\x00\x01\x02\x03\x04\x05\x06\x07') |
| 34 |
|
| 35 |
|
| 36 |
class TestOpenVpnPacketBase(unittest.TestCase): |
| 37 |
def test_error_not_enough_data(self): |
| 38 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 39 |
OpenVpnPacketBase.parse_header(b'\x00') |
| 40 |
|
| 41 |
self.assertEqual(context_manager.exception.bytes_needed, OpenVpnPacketBase.HEADER_SIZE - 1) |
| 42 |
|
| 43 |
def test_error_wrong_packet_type(self): |
| 44 |
with self.assertRaises(InvalidType): |
| 45 |
OpenVpnPacketAckV1.parse_header(b'\x00' * OpenVpnPacketBase.HEADER_SIZE) |
| 46 |
|
| 47 |
def test_packet_id_array(self): |
| 48 |
header_without_packet_array_dict = collections.OrderedDict([ |
| 49 |
('opcode', b'\x20'), |
| 50 |
('session_id', b'\x00\x01\x02\x03\x04\x05\x06\x07'), |
| 51 |
]) |
| 52 |
header_without_packet_array_bytes = b''.join(header_without_packet_array_dict.values()) |
| 53 |
|
| 54 |
session_id, packet_id_array, remote_session_id, header_length = OpenVpnPacketControlV1.parse_header( |
| 55 |
header_without_packet_array_bytes + b'\x00' |
| 56 |
) |
| 57 |
self.assertEqual(session_id, 0x0001020304050607) |
| 58 |
self.assertEqual(packet_id_array, []) |
| 59 |
self.assertEqual(remote_session_id, None) |
| 60 |
self.assertEqual(header_length, len(header_without_packet_array_bytes) + 1) |
| 61 |
|
| 62 |
packet_array_dict = collections.OrderedDict([ |
| 63 |
('packet_id_array_length', b'\x01'), |
| 64 |
('packet_id_array', b'\x04\x05\x06\x07'), |
| 65 |
('remote_session_id', b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 66 |
]) |
| 67 |
packet_array_bytes = b''.join(packet_array_dict.values()) |
| 68 |
|
| 69 |
session_id, packet_id_array, remote_session_id, header_length = OpenVpnPacketControlV1.parse_header( |
| 70 |
header_without_packet_array_bytes + packet_array_bytes |
| 71 |
) |
| 72 |
self.assertEqual(session_id, 0x0001020304050607) |
| 73 |
self.assertEqual(packet_id_array, [0x04050607]) |
| 74 |
self.assertEqual(remote_session_id, 0x08090a0b0c0d0e0f) |
| 75 |
self.assertEqual(header_length, len(header_without_packet_array_bytes) + len(packet_array_bytes)) |
| 76 |
|
| 77 |
|
| 78 |
class TestOpenVpnPacketControlV1(unittest.TestCase): |
| 79 |
def setUp(self): |
| 80 |
self.control_dict = collections.OrderedDict([ |
| 81 |
('opcode', b'\x20'), |
| 82 |
('session_id', b'\x00\x01\x02\x03\x04\x05\x06\x07'), |
| 83 |
('packet_id_array_length', b'\x01'), |
| 84 |
('packet_id_array', b'\x04\x05\x06\x07'), |
| 85 |
('remote_session_id', b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 86 |
('packet_id', b'\x00\x01\x02\x03'), |
| 87 |
('payload', b'\x00\x01\x02\x03'), |
| 88 |
]) |
| 89 |
self.control_bytes = b''.join(self.control_dict.values()) |
| 90 |
|
| 91 |
self.control = OpenVpnPacketControlV1( |
| 92 |
session_id=0x0001020304050607, |
| 93 |
packet_id_array=[0x04050607], |
| 94 |
remote_session_id=0x08090a0b0c0d0e0f, |
| 95 |
packet_id=0x00010203, |
| 96 |
payload=b'\x00\x01\x02\x03', |
| 97 |
) |
| 98 |
|
| 99 |
def test_parse(self): |
| 100 |
control = OpenVpnPacketVariant.parse_exact_size(self.control_bytes) |
| 101 |
self.assertEqual(control.session_id, 0x0001020304050607) |
| 102 |
self.assertEqual(control.packet_id_array, [0x04050607]) |
| 103 |
self.assertEqual(control.remote_session_id, 0x08090a0b0c0d0e0f) |
| 104 |
self.assertEqual(control.packet_id, 0x00010203) |
| 105 |
|
| 106 |
def test_compose(self): |
| 107 |
self.assertEqual(self.control.compose(), self.control_bytes) |
| 108 |
|
| 109 |
|
| 110 |
class TestOpenVpnPacketAckV1(unittest.TestCase): |
| 111 |
def setUp(self): |
| 112 |
self.ack_dict = collections.OrderedDict([ |
| 113 |
('opcode', b'\x28'), |
| 114 |
('session_id', b'\x00\x01\x02\x03\x04\x05\x06\x07'), |
| 115 |
('packet_id_array_length', b'\x01'), |
| 116 |
('packet_id_array', b'\x04\x05\x06\x07'), |
| 117 |
('remote_session_id', b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 118 |
]) |
| 119 |
self.ack_bytes = b''.join(self.ack_dict.values()) |
| 120 |
|
| 121 |
self.ack = OpenVpnPacketAckV1( |
| 122 |
session_id=0x0001020304050607, |
| 123 |
packet_id_array=[0x04050607], |
| 124 |
remote_session_id=0x08090a0b0c0d0e0f, |
| 125 |
) |
| 126 |
|
| 127 |
def test_parse(self): |
| 128 |
ack = OpenVpnPacketVariant.parse_exact_size(self.ack_bytes) |
| 129 |
self.assertEqual(ack.session_id, 0x0001020304050607) |
| 130 |
self.assertEqual(ack.packet_id_array, [0x04050607]) |
| 131 |
|
| 132 |
def test_compose(self): |
| 133 |
self.assertEqual(self.ack.compose(), self.ack_bytes) |
| 134 |
|
| 135 |
|
| 136 |
class TestOpenVpnPacketHardResetClientV2(unittest.TestCase): |
| 137 |
def setUp(self): |
| 138 |
self.hard_reset_client_dict = collections.OrderedDict([ |
| 139 |
('opcode', b'\x38'), |
| 140 |
('session_id', b'\x00\x01\x02\x03\x04\x05\x06\x07'), |
| 141 |
('packet_id_array_length', b'\x00'), |
| 142 |
('packet_id', b'\x00\x01\x02\x03'), |
| 143 |
]) |
| 144 |
self.hard_reset_client_bytes = b''.join(self.hard_reset_client_dict.values()) |
| 145 |
|
| 146 |
self.hard_reset_client = OpenVpnPacketHardResetClientV2( |
| 147 |
session_id=0x0001020304050607, |
| 148 |
packet_id=0x00010203, |
| 149 |
) |
| 150 |
|
| 151 |
def test_error_non_empty_packet_id_array(self): |
| 152 |
hard_reset_client_dict = collections.OrderedDict([ |
| 153 |
('opcode', b'\x38'), |
| 154 |
('session_id', b'\x00\x01\x02\x03\x04\x05\x06\x07'), |
| 155 |
('packet_id_array_length', b'\x01'), |
| 156 |
('packet_id_array', b'\x00\x01\x02\x03'), |
| 157 |
('remote_session_id', b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 158 |
]) |
| 159 |
hard_reset_client_bytes = b''.join(hard_reset_client_dict.values()) |
| 160 |
with self.assertRaises(InvalidValue) as context_manager: |
| 161 |
OpenVpnPacketVariant.parse_exact_size(hard_reset_client_bytes) |
| 162 |
self.assertEqual(context_manager.exception.value, [0x00010203]) |
| 163 |
|
| 164 |
def test_parse(self): |
| 165 |
hard_reset_client = OpenVpnPacketVariant.parse_exact_size(self.hard_reset_client_bytes) |
| 166 |
self.assertEqual(hard_reset_client.session_id, 0x0001020304050607) |
| 167 |
self.assertEqual(hard_reset_client.packet_id_array, []) |
| 168 |
self.assertEqual(hard_reset_client.packet_id, 0x00010203) |
| 169 |
|
| 170 |
def test_compose(self): |
| 171 |
self.assertEqual(self.hard_reset_client.compose(), self.hard_reset_client_bytes) |
| 172 |
|
| 173 |
|
| 174 |
class TestOpenVpnPacketHardResetServerV2(unittest.TestCase): |
| 175 |
def setUp(self): |
| 176 |
self.hard_reset_server_dict = collections.OrderedDict([ |
| 177 |
('opcode', b'\x40'), |
| 178 |
('session_id', b'\x00\x01\x02\x03\x04\x05\x06\x07'), |
| 179 |
('packet_id_array_length', b'\x01'), |
| 180 |
('packet_id_array', b'\x04\x05\x06\x07'), |
| 181 |
('remote_session_id', b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 182 |
('packet_id', b'\x00\x01\x02\x03'), |
| 183 |
]) |
| 184 |
self.hard_reset_server_bytes = b''.join(self.hard_reset_server_dict.values()) |
| 185 |
|
| 186 |
self.hard_reset_server = OpenVpnPacketHardResetServerV2( |
| 187 |
session_id=0x0001020304050607, |
| 188 |
packet_id_array=[0x04050607], |
| 189 |
remote_session_id=0x08090a0b0c0d0e0f, |
| 190 |
packet_id=0x00010203, |
| 191 |
) |
| 192 |
|
| 193 |
def test_parse(self): |
| 194 |
hard_reset_server = OpenVpnPacketVariant.parse_exact_size(self.hard_reset_server_bytes) |
| 195 |
self.assertEqual(hard_reset_server.session_id, 0x0001020304050607) |
| 196 |
self.assertEqual(hard_reset_server.packet_id_array, [0x04050607]) |
| 197 |
self.assertEqual(hard_reset_server.remote_session_id, 0x08090a0b0c0d0e0f) |
| 198 |
self.assertEqual(hard_reset_server.packet_id, 0x00010203) |
| 199 |
|
| 200 |
def test_compose(self): |
| 201 |
self.assertEqual(self.hard_reset_server.compose(), self.hard_reset_server_bytes) |