| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
# pylint: disable=too-many-lines |
| 3 |
|
| 4 |
import collections |
| 5 |
import ipaddress |
| 6 |
import unittest |
| 7 |
|
| 8 |
from cryptodatahub.common.algorithm import Signature |
| 9 |
from cryptodatahub.common.exception import InvalidValue |
| 10 |
from cryptodatahub.ike.algorithm import ( |
| 11 |
Ikev2AuthenticationMethod, |
| 12 |
Ikev2CertificateType, |
| 13 |
Ikev2DiffieHellmanGroup, |
| 14 |
Ikev2IdType, |
| 15 |
Ikev2NotifyType, |
| 16 |
Ikev2ProtocolId, |
| 17 |
Ikev2TransformAttributeType, |
| 18 |
) |
| 19 |
|
| 20 |
from cryptoparser.common.exception import InvalidType, NotEnoughData, TooMuchData |
| 21 |
from cryptoparser.ike.ikev2 import ( |
| 22 |
Ikev2AuthDigitalSignatureEnvelope, |
| 23 |
Ikev2NotifyPayloadInvalidKe, |
| 24 |
Ikev2PayloadAuthentication, |
| 25 |
Ikev2PayloadCertificate, |
| 26 |
Ikev2PayloadCertificateRequest, |
| 27 |
Ikev2PayloadDelete, |
| 28 |
Ikev2PayloadEap, |
| 29 |
Ikev2PayloadEncryptedAndAuthenticated, |
| 30 |
Ikev2PayloadFlags, |
| 31 |
Ikev2PayloadIdentificationInitiator, |
| 32 |
Ikev2PayloadIdentificationInitiatorDerAsn1Gn, |
| 33 |
Ikev2PayloadIdentificationInitiatorFcName, |
| 34 |
Ikev2PayloadIdentificationInitiatorFqdn, |
| 35 |
Ikev2PayloadIdentificationInitiatorIpv6Addr, |
| 36 |
Ikev2PayloadIdentificationInitiatorKeyId, |
| 37 |
Ikev2PayloadIdentificationInitiatorNull, |
| 38 |
Ikev2PayloadIdentificationInitiatorRfc822Addr, |
| 39 |
Ikev2PayloadIdentificationResponder, |
| 40 |
Ikev2PayloadIdentificationResponderDerAsn1Dn, |
| 41 |
Ikev2PayloadIdentificationResponderFqdn, |
| 42 |
Ikev2PayloadIdentificationResponderIpv4Addr, |
| 43 |
Ikev2PayloadIdentificationResponderIpv6Addr, |
| 44 |
Ikev2PayloadKeyExchange, |
| 45 |
Ikev2PayloadNonce, |
| 46 |
Ikev2PayloadNotifyAuthenticationFailed, |
| 47 |
Ikev2PayloadNotifyUnparsed, |
| 48 |
Ikev2PayloadType, |
| 49 |
Ikev2PayloadVendorId, |
| 50 |
TransformAttributeKeyLength, |
| 51 |
|
| 52 |
) |
| 53 |
|
| 54 |
from .classes import Ikev2PayloadBaseTest, Ikev2PayloadNotifyNoDataTest |
| 55 |
|
| 56 |
|
| 57 |
class TestIkev2PayloadBase(unittest.TestCase): |
| 58 |
def setUp(self): |
| 59 |
self.test_data = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 60 |
self.test_payload_minimal = Ikev2PayloadBaseTest( |
| 61 |
flags=set(), |
| 62 |
test_data=b'', |
| 63 |
) |
| 64 |
self.test_payload_minimal.next_payload = Ikev2PayloadType.NONE |
| 65 |
|
| 66 |
self.test_dict_minimal = collections.OrderedDict([ |
| 67 |
('next_payload', b'\x00'), |
| 68 |
('flags', b'\x00'), |
| 69 |
('payload_length', b'\x00\x04'), |
| 70 |
('test_data', b''), |
| 71 |
]) |
| 72 |
self.test_bytes_minimal = b''.join(self.test_dict_minimal.values()) |
| 73 |
|
| 74 |
self.test_payload_with_data = Ikev2PayloadBaseTest( |
| 75 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 76 |
test_data=self.test_data |
| 77 |
) |
| 78 |
self.test_payload_with_data.next_payload = Ikev2PayloadType.NONE |
| 79 |
|
| 80 |
self.test_dict_with_data = collections.OrderedDict([ |
| 81 |
('next_payload', b'\x00'), |
| 82 |
('flags', b'\x80'), |
| 83 |
('payload_length', b'\x00\x14'), |
| 84 |
('test_data', self.test_data), |
| 85 |
]) |
| 86 |
self.test_bytes_with_data = b''.join(self.test_dict_with_data.values()) |
| 87 |
|
| 88 |
def test_parse(self): |
| 89 |
parsed_payload = Ikev2PayloadBaseTest.parse_exact_size(self.test_bytes_minimal) |
| 90 |
self.assertEqual(parsed_payload.flags, self.test_payload_minimal.flags) |
| 91 |
self.assertEqual(parsed_payload.test_data, self.test_payload_minimal.test_data) |
| 92 |
self.assertEqual(parsed_payload.next_payload, self.test_payload_minimal.next_payload) |
| 93 |
|
| 94 |
parsed_payload = Ikev2PayloadBaseTest.parse_exact_size(self.test_bytes_with_data) |
| 95 |
self.assertEqual(parsed_payload.flags, self.test_payload_with_data.flags) |
| 96 |
self.assertEqual(parsed_payload.test_data, self.test_payload_with_data.test_data) |
| 97 |
self.assertEqual(parsed_payload.next_payload, self.test_payload_with_data.next_payload) |
| 98 |
|
| 99 |
def test_compose(self): |
| 100 |
self.assertEqual(self.test_payload_minimal.compose(), self.test_bytes_minimal) |
| 101 |
self.assertEqual(self.test_payload_with_data.compose(), self.test_bytes_with_data) |
| 102 |
|
| 103 |
def test_error_parse_not_enough_data(self): |
| 104 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 105 |
Ikev2PayloadBaseTest.parse_exact_size(b'\x00') |
| 106 |
self.assertEqual(context_manager.exception.bytes_needed, 3) |
| 107 |
|
| 108 |
def test_error_payload_validation(self): |
| 109 |
with self.assertRaises(TypeError) as context_manager: |
| 110 |
Ikev2PayloadBaseTest( |
| 111 |
flags={'invalid_flag'}, |
| 112 |
test_data=self.test_data |
| 113 |
) |
| 114 |
exception_str = str(context_manager.exception) |
| 115 |
self.assertIn("flags", exception_str) |
| 116 |
self.assertIn("must be", exception_str) |
| 117 |
self.assertIn("Ikev2PayloadFlags", exception_str) |
| 118 |
|
| 119 |
def test_next_payload(self): |
| 120 |
payload = Ikev2PayloadBaseTest( |
| 121 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 122 |
test_data=self.test_data |
| 123 |
) |
| 124 |
payload.next_payload = Ikev2PayloadType.KE |
| 125 |
|
| 126 |
composed = payload.compose() |
| 127 |
self.assertEqual(composed[0], Ikev2PayloadType.KE.value.code) |
| 128 |
|
| 129 |
parsed, _ = Ikev2PayloadBaseTest._parse(composed) # pylint: disable=protected-access |
| 130 |
self.assertEqual(parsed.next_payload, Ikev2PayloadType.KE) |
| 131 |
|
| 132 |
|
| 133 |
class TestIkev2PayloadKeyExchange(unittest.TestCase): |
| 134 |
def setUp(self): |
| 135 |
self.dh_group = Ikev2DiffieHellmanGroup.MODP_GROUP_2048_BIT |
| 136 |
self.key_exchange_data = b'\x00\x01\x02\x03\x04\x05\x06\x07' |
| 137 |
self.key_exchange_payload = Ikev2PayloadKeyExchange( |
| 138 |
flags=set(), |
| 139 |
dh_group=self.dh_group, |
| 140 |
key_exchange_data=self.key_exchange_data |
| 141 |
) |
| 142 |
self.key_exchange_payload.next_payload = Ikev2PayloadType.NONE |
| 143 |
|
| 144 |
self.key_exchange_dict = collections.OrderedDict([ |
| 145 |
('next_payload', b'\x00'), |
| 146 |
('flags', b'\x00'), |
| 147 |
('payload_length', b'\x00\x10'), |
| 148 |
('dh_group', b'\x00\x0e'), |
| 149 |
('reserved2', b'\x00\x00'), |
| 150 |
('key_exchange_data', self.key_exchange_data), |
| 151 |
]) |
| 152 |
self.key_exchange_bytes = b''.join(self.key_exchange_dict.values()) |
| 153 |
|
| 154 |
def test_get_payload_type(self): |
| 155 |
self.assertEqual(Ikev2PayloadKeyExchange.get_payload_type(), Ikev2PayloadType.KE) |
| 156 |
|
| 157 |
def test_parse(self): |
| 158 |
parsed_ke = Ikev2PayloadKeyExchange.parse_exact_size(self.key_exchange_bytes) |
| 159 |
self.assertEqual(parsed_ke.dh_group, self.dh_group) |
| 160 |
self.assertEqual(parsed_ke.key_exchange_data, self.key_exchange_data) |
| 161 |
|
| 162 |
def test_compose(self): |
| 163 |
composed_bytes = self.key_exchange_payload.compose() |
| 164 |
self.assertGreater(len(composed_bytes), Ikev2PayloadKeyExchange.HEADER_SIZE) |
| 165 |
|
| 166 |
parsed_ke = Ikev2PayloadKeyExchange.parse_exact_size(composed_bytes) |
| 167 |
self.assertEqual(parsed_ke.key_exchange_data, self.key_exchange_payload.key_exchange_data) |
| 168 |
|
| 169 |
def test_round_trip(self): |
| 170 |
composed_bytes = self.key_exchange_payload.compose() |
| 171 |
parsed_payload: Ikev2PayloadKeyExchange = Ikev2PayloadKeyExchange.parse_exact_size(composed_bytes) |
| 172 |
|
| 173 |
self.assertEqual(parsed_payload.dh_group, self.key_exchange_payload.dh_group) |
| 174 |
self.assertEqual(parsed_payload.key_exchange_data, self.key_exchange_payload.key_exchange_data) |
| 175 |
self.assertEqual(parsed_payload.flags, self.key_exchange_payload.flags) |
| 176 |
self.assertEqual(parsed_payload.next_payload, self.key_exchange_payload.next_payload) |
| 177 |
|
| 178 |
|
| 179 |
class TestIkev2PayloadNonce(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 180 |
def setUp(self): |
| 181 |
self.nonce_data = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 182 |
self.nonce_payload = Ikev2PayloadNonce( |
| 183 |
flags=set(), |
| 184 |
nonce_data=self.nonce_data |
| 185 |
) |
| 186 |
self.nonce_payload.next_payload = Ikev2PayloadType.NONE |
| 187 |
|
| 188 |
self.nonce_dict = collections.OrderedDict([ |
| 189 |
('next_payload', b'\x00'), |
| 190 |
('flags', b'\x00'), |
| 191 |
('payload_length', b'\x00\x14'), |
| 192 |
('nonce_data', self.nonce_data), |
| 193 |
]) |
| 194 |
self.nonce_bytes = b''.join(self.nonce_dict.values()) |
| 195 |
|
| 196 |
self.nonce_dict_too_small = collections.OrderedDict([ |
| 197 |
('next_payload', b'\x00'), |
| 198 |
('flags', b'\x00'), |
| 199 |
('payload_length', b'\x00\x13'), |
| 200 |
('nonce_data', b'\x00' * 15), |
| 201 |
]) |
| 202 |
self.nonce_bytes_too_small = b''.join(self.nonce_dict_too_small.values()) |
| 203 |
|
| 204 |
self.nonce_dict_too_large = collections.OrderedDict([ |
| 205 |
('next_payload', b'\x00'), |
| 206 |
('flags', b'\x00'), |
| 207 |
('payload_length', b'\x01\x05'), |
| 208 |
('nonce_data', b'\x00' * 257), |
| 209 |
]) |
| 210 |
self.nonce_bytes_too_large = b''.join(self.nonce_dict_too_large.values()) |
| 211 |
|
| 212 |
def test_get_payload_type(self): |
| 213 |
self.assertEqual(Ikev2PayloadNonce.get_payload_type(), Ikev2PayloadType.NONCE) |
| 214 |
|
| 215 |
def test_parse(self): |
| 216 |
parsed_nonce = Ikev2PayloadNonce.parse_exact_size(self.nonce_bytes) |
| 217 |
self.assertEqual(parsed_nonce.nonce_data, self.nonce_data) |
| 218 |
self.assertEqual(parsed_nonce.next_payload, Ikev2PayloadType.NONE) |
| 219 |
self.assertEqual(parsed_nonce.flags, set()) |
| 220 |
|
| 221 |
def test_compose(self): |
| 222 |
composed_bytes = self.nonce_payload.compose() |
| 223 |
self.assertIsInstance(composed_bytes, (bytes, bytearray)) |
| 224 |
self.assertGreater(len(composed_bytes), Ikev2PayloadNonce.HEADER_SIZE) |
| 225 |
|
| 226 |
parsed_nonce = Ikev2PayloadNonce.parse_exact_size(composed_bytes) |
| 227 |
self.assertEqual(parsed_nonce.nonce_data, self.nonce_payload.nonce_data) |
| 228 |
self.assertEqual(parsed_nonce.next_payload, self.nonce_payload.next_payload) |
| 229 |
self.assertEqual(parsed_nonce.flags, self.nonce_payload.flags) |
| 230 |
|
| 231 |
def test_error_invalid_nonce_data(self): |
| 232 |
with self.assertRaises(Exception) as context_manager: |
| 233 |
Ikev2PayloadNonce( |
| 234 |
flags=set(), |
| 235 |
nonce_data="not_bytes" |
| 236 |
) |
| 237 |
self.assertTrue(len(str(context_manager.exception)) > 0) |
| 238 |
|
| 239 |
def test_minimal_nonce_data(self): |
| 240 |
nonce_empty = Ikev2PayloadNonce( |
| 241 |
flags=set(), |
| 242 |
nonce_data=b'\x00' * 16 |
| 243 |
) |
| 244 |
nonce_empty.next_payload = Ikev2PayloadType.NONE |
| 245 |
composed_bytes = nonce_empty.compose() |
| 246 |
parsed_nonce = Ikev2PayloadNonce.parse_exact_size(composed_bytes) |
| 247 |
self.assertEqual(parsed_nonce.nonce_data, b'\x00' * 16) |
| 248 |
|
| 249 |
def test_too_small_nonce_data(self): |
| 250 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 251 |
Ikev2PayloadNonce.parse_exact_size(self.nonce_bytes_too_small) |
| 252 |
self.assertEqual(context_manager.exception.bytes_needed, 16 - 15) |
| 253 |
|
| 254 |
def test_too_large_nonce_data(self): |
| 255 |
with self.assertRaises(TooMuchData) as context_manager: |
| 256 |
Ikev2PayloadNonce.parse_exact_size(self.nonce_bytes_too_large) |
| 257 |
self.assertEqual(context_manager.exception.bytes_needed, 257 - 256) |
| 258 |
|
| 259 |
def test_error_nonce_data_validation_min_length(self): |
| 260 |
with self.assertRaises(ValueError): |
| 261 |
Ikev2PayloadNonce(flags=set(), nonce_data=b'\x01\x02\x03\x04\x05\x06\x07\x08') |
| 262 |
|
| 263 |
def test_error_nonce_data_validation_max_length(self): |
| 264 |
with self.assertRaises(ValueError): |
| 265 |
Ikev2PayloadNonce(flags=set(), nonce_data=b'\x01' * 257) |
| 266 |
|
| 267 |
def test_round_trip(self): |
| 268 |
composed_bytes = self.nonce_payload.compose() |
| 269 |
parsed_payload: Ikev2PayloadNonce = Ikev2PayloadNonce.parse_exact_size(composed_bytes) |
| 270 |
|
| 271 |
self.assertEqual(parsed_payload.nonce_data, self.nonce_payload.nonce_data) |
| 272 |
self.assertEqual(parsed_payload.flags, self.nonce_payload.flags) |
| 273 |
self.assertEqual(parsed_payload.next_payload, self.nonce_payload.next_payload) |
| 274 |
|
| 275 |
|
| 276 |
class TestIkev2PayloadDelete(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 277 |
def setUp(self): |
| 278 |
self.protocol_id = Ikev2ProtocolId.IKE |
| 279 |
self.spis = [0x1234567890abcdef, 0xfedcba0987654321] |
| 280 |
self.delete_payload = Ikev2PayloadDelete( |
| 281 |
flags=set(), |
| 282 |
protocol_id=self.protocol_id, |
| 283 |
spis=self.spis |
| 284 |
) |
| 285 |
self.delete_payload.next_payload = Ikev2PayloadType.NONE |
| 286 |
|
| 287 |
self.delete_dict = collections.OrderedDict([ |
| 288 |
('next_payload', b'\x00'), # NONE |
| 289 |
('flags', b'\x00'), |
| 290 |
('payload_length', b'\x00\x18'), # 24 bytes (header + protocol_id + spi_size + num_spis + spis) |
| 291 |
('protocol_id', b'\x01'), # IKE protocol |
| 292 |
('spi_size', b'\x10'), # 16 bytes (2 SPIs * 8 bytes each) |
| 293 |
('num_spis', b'\x00\x02'), # 2 SPIs |
| 294 |
('spis', b'\x12\x34\x56\x78\x90\xab\xcd\xef\xfe\xdc\xba\x09\x87\x65\x43\x21'), |
| 295 |
]) |
| 296 |
self.delete_bytes = b''.join(self.delete_dict.values()) |
| 297 |
|
| 298 |
self.empty_delete_payload = Ikev2PayloadDelete( |
| 299 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 300 |
protocol_id=self.protocol_id, |
| 301 |
spis=[] |
| 302 |
) |
| 303 |
self.empty_delete_payload.next_payload = Ikev2PayloadType.NONE |
| 304 |
|
| 305 |
self.empty_delete_dict = collections.OrderedDict([ |
| 306 |
('next_payload', b'\x00'), # NONE |
| 307 |
('flags', b'\x80'), # CRITICAL (0x80) |
| 308 |
('payload_length', b'\x00\x08'), # 8 bytes (header + protocol_id + spi_size + num_spis) |
| 309 |
('protocol_id', b'\x01'), # IKE protocol |
| 310 |
('spi_size', b'\x00'), |
| 311 |
('num_spis', b'\x00\x00'), |
| 312 |
]) |
| 313 |
self.empty_delete_bytes = b''.join(self.empty_delete_dict.values()) |
| 314 |
|
| 315 |
def test_get_payload_type(self): |
| 316 |
self.assertEqual(Ikev2PayloadDelete.get_payload_type(), Ikev2PayloadType.DELETE) |
| 317 |
|
| 318 |
def test_parse(self): |
| 319 |
parsed_delete = Ikev2PayloadDelete.parse_exact_size(self.delete_bytes) |
| 320 |
self.assertEqual(parsed_delete.protocol_id, self.protocol_id) |
| 321 |
self.assertEqual(parsed_delete.spis, self.spis) |
| 322 |
self.assertEqual(parsed_delete.next_payload, Ikev2PayloadType.NONE) |
| 323 |
self.assertEqual(parsed_delete.flags, set()) |
| 324 |
|
| 325 |
def test_compose(self): |
| 326 |
composed_bytes = self.delete_payload.compose() |
| 327 |
self.assertIsInstance(composed_bytes, (bytes, bytearray)) |
| 328 |
self.assertGreater(len(composed_bytes), Ikev2PayloadDelete.HEADER_SIZE) |
| 329 |
|
| 330 |
parsed_delete = Ikev2PayloadDelete.parse_exact_size(composed_bytes) |
| 331 |
self.assertEqual(parsed_delete.protocol_id, self.delete_payload.protocol_id) |
| 332 |
self.assertEqual(parsed_delete.spis, self.delete_payload.spis) |
| 333 |
self.assertEqual(parsed_delete.next_payload, self.delete_payload.next_payload) |
| 334 |
self.assertEqual(parsed_delete.flags, self.delete_payload.flags) |
| 335 |
|
| 336 |
def test_empty_spis(self): |
| 337 |
composed_bytes = self.empty_delete_payload.compose() |
| 338 |
parsed_delete = Ikev2PayloadDelete.parse_exact_size(composed_bytes) |
| 339 |
self.assertEqual(parsed_delete.spis, []) |
| 340 |
self.assertEqual(parsed_delete.protocol_id, self.protocol_id) |
| 341 |
self.assertEqual(parsed_delete.flags, {Ikev2PayloadFlags.CRITICAL}) |
| 342 |
|
| 343 |
def test_error_parse_not_enough_data(self): |
| 344 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 345 |
Ikev2PayloadDelete.parse_exact_size(b'\x00\x01\x02') |
| 346 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 347 |
|
| 348 |
def test_error_invalid_protocol_id(self): |
| 349 |
with self.assertRaises(TypeError): |
| 350 |
Ikev2PayloadDelete( |
| 351 |
flags=set(), |
| 352 |
protocol_id="invalid_protocol", |
| 353 |
spis=self.spis |
| 354 |
) |
| 355 |
|
| 356 |
def test_error_invalid_spis_type(self): |
| 357 |
with self.assertRaises(TypeError): |
| 358 |
Ikev2PayloadDelete( |
| 359 |
flags=set(), |
| 360 |
protocol_id=self.protocol_id, |
| 361 |
spis=["not_an_int"] |
| 362 |
) |
| 363 |
|
| 364 |
def test_round_trip(self): |
| 365 |
composed_bytes = self.delete_payload.compose() |
| 366 |
parsed_payload: Ikev2PayloadDelete = Ikev2PayloadDelete.parse_exact_size(composed_bytes) |
| 367 |
|
| 368 |
self.assertEqual(parsed_payload.protocol_id, self.delete_payload.protocol_id) |
| 369 |
self.assertEqual(parsed_payload.spis, self.delete_payload.spis) |
| 370 |
self.assertEqual(parsed_payload.flags, self.delete_payload.flags) |
| 371 |
self.assertEqual(parsed_payload.next_payload, self.delete_payload.next_payload) |
| 372 |
|
| 373 |
|
| 374 |
class TestIkev2PayloadNotifyNoData(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 375 |
def setUp(self): |
| 376 |
self.protocol_id = Ikev2ProtocolId.IKE |
| 377 |
self.notify_type = Ikev2NotifyType.AUTHENTICATION_FAILED |
| 378 |
self.spi = b'\x00\x01\x02\x03\x04\x05\x06\x07' |
| 379 |
|
| 380 |
self.notify_payload_minimal = Ikev2PayloadNotifyNoDataTest( |
| 381 |
flags=set(), |
| 382 |
protocol_id=self.protocol_id, |
| 383 |
notify_type=self.notify_type, |
| 384 |
spi=b'' |
| 385 |
) |
| 386 |
self.notify_payload_minimal.next_payload = Ikev2PayloadType.NONE |
| 387 |
|
| 388 |
self.notify_dict_minimal = collections.OrderedDict([ |
| 389 |
('next_payload', b'\x00'), # NONE |
| 390 |
('flags', b'\x00'), |
| 391 |
('payload_length', b'\x00\x08'), # 8 bytes (header + protocol_id + spi_size + notify_type) |
| 392 |
('protocol_id', b'\x01'), # IKE protocol |
| 393 |
('spi_size', b'\x00'), |
| 394 |
('notify_type', b'\x00\x18'), # AUTHENTICATION_FAILED (0x0018) |
| 395 |
]) |
| 396 |
self.notify_bytes_minimal = b''.join(self.notify_dict_minimal.values()) |
| 397 |
|
| 398 |
self.notify_payload_with_spi = Ikev2PayloadNotifyNoDataTest( |
| 399 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 400 |
protocol_id=self.protocol_id, |
| 401 |
notify_type=self.notify_type, |
| 402 |
spi=self.spi |
| 403 |
) |
| 404 |
self.notify_payload_with_spi.next_payload = Ikev2PayloadType.NONE |
| 405 |
|
| 406 |
self.notify_dict_with_spi = collections.OrderedDict([ |
| 407 |
('next_payload', b'\x00'), # NONE |
| 408 |
('flags', b'\x80'), # CRITICAL (0x80) |
| 409 |
('payload_length', b'\x00\x10'), # 16 bytes (header + protocol_id + spi_size + notify_type + spi) |
| 410 |
('protocol_id', b'\x01'), # IKE protocol |
| 411 |
('spi_size', b'\x08'), |
| 412 |
('notify_type', b'\x00\x18'), # AUTHENTICATION_FAILED (0x0018) |
| 413 |
('spi', self.spi), |
| 414 |
]) |
| 415 |
self.notify_bytes_with_spi = b''.join(self.notify_dict_with_spi.values()) |
| 416 |
|
| 417 |
def test_parse(self): |
| 418 |
parsed_notify = Ikev2PayloadNotifyNoDataTest.parse_exact_size(self.notify_bytes_minimal) |
| 419 |
self.assertEqual(parsed_notify.protocol_id, self.protocol_id) |
| 420 |
self.assertEqual(parsed_notify.type, self.notify_type) |
| 421 |
self.assertEqual(parsed_notify.spi, b'') |
| 422 |
self.assertEqual(parsed_notify.next_payload, Ikev2PayloadType.NONE) |
| 423 |
self.assertEqual(parsed_notify.flags, set()) |
| 424 |
|
| 425 |
parsed_notify = Ikev2PayloadNotifyNoDataTest.parse_exact_size(self.notify_bytes_with_spi) |
| 426 |
self.assertEqual(parsed_notify.protocol_id, self.protocol_id) |
| 427 |
self.assertEqual(parsed_notify.type, self.notify_type) |
| 428 |
self.assertEqual(parsed_notify.spi, self.spi) |
| 429 |
self.assertEqual(parsed_notify.next_payload, Ikev2PayloadType.NONE) |
| 430 |
self.assertEqual(parsed_notify.flags, {Ikev2PayloadFlags.CRITICAL}) |
| 431 |
|
| 432 |
def test_compose(self): |
| 433 |
self.assertEqual(self.notify_payload_minimal.compose(), self.notify_bytes_minimal) |
| 434 |
self.assertEqual(self.notify_payload_with_spi.compose(), self.notify_bytes_with_spi) |
| 435 |
|
| 436 |
def test_error_parse_not_enough_data(self): |
| 437 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 438 |
Ikev2PayloadNotifyNoDataTest.parse_exact_size(b'\x00\x01\x02') |
| 439 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 440 |
|
| 441 |
def test_error_payload_validation(self): |
| 442 |
with self.assertRaises(TypeError) as context_manager: |
| 443 |
Ikev2PayloadNotifyNoDataTest( |
| 444 |
flags={'invalid_flag'}, |
| 445 |
protocol_id=self.protocol_id, |
| 446 |
notify_type=self.notify_type, |
| 447 |
spi=self.spi |
| 448 |
) |
| 449 |
exception_str = str(context_manager.exception) |
| 450 |
self.assertIn("flags", exception_str) |
| 451 |
self.assertIn("must be", exception_str) |
| 452 |
self.assertIn("Ikev2PayloadFlags", exception_str) |
| 453 |
|
| 454 |
def test_error_invalid_protocol_id(self): |
| 455 |
with self.assertRaises(TypeError): |
| 456 |
Ikev2PayloadNotifyNoDataTest( |
| 457 |
flags=set(), |
| 458 |
protocol_id="invalid_protocol", |
| 459 |
notify_type=self.notify_type, |
| 460 |
spi=self.spi |
| 461 |
) |
| 462 |
|
| 463 |
def test_error_invalid_notify_type(self): |
| 464 |
with self.assertRaises(TypeError): |
| 465 |
Ikev2PayloadNotifyNoDataTest( |
| 466 |
flags=set(), |
| 467 |
protocol_id=self.protocol_id, |
| 468 |
notify_type="invalid_type", |
| 469 |
spi=self.spi |
| 470 |
) |
| 471 |
|
| 472 |
def test_error_invalid_spi(self): |
| 473 |
# The spi attribute has converter=bytes, so test with something that can't be converted |
| 474 |
with self.assertRaises((TypeError, ValueError)): |
| 475 |
Ikev2PayloadNotifyNoDataTest( |
| 476 |
flags=set(), |
| 477 |
protocol_id=self.protocol_id, |
| 478 |
notify_type=self.notify_type, |
| 479 |
spi=None |
| 480 |
) |
| 481 |
|
| 482 |
|
| 483 |
class TestIkev2PayloadNotifyAuthenticationFailed(unittest.TestCase): |
| 484 |
def setUp(self): |
| 485 |
self.protocol_id = Ikev2ProtocolId.IKE |
| 486 |
|
| 487 |
self.notify_dict_authentication_failed = collections.OrderedDict([ |
| 488 |
('next_payload', b'\x00'), # NONE |
| 489 |
('flags', b'\x00'), |
| 490 |
('payload_length', b'\x00\x08'), # 8 bytes (header + protocol_id + spi_size + notify_type) |
| 491 |
('protocol_id', b'\x01'), # IKE protocol |
| 492 |
('spi_size', b'\x00'), |
| 493 |
('notify_type', b'\x00\x18'), # AUTHENTICATION_FAILED (0x0018) |
| 494 |
]) |
| 495 |
self.notify_bytes_authentication_failed = b''.join(self.notify_dict_authentication_failed.values()) |
| 496 |
|
| 497 |
def test_get_message_type(self): |
| 498 |
self.assertEqual(Ikev2PayloadNotifyAuthenticationFailed._get_message_type(), # pylint: disable=protected-access |
| 499 |
Ikev2NotifyType.AUTHENTICATION_FAILED) |
| 500 |
|
| 501 |
def test_parse_authentication_failed(self): |
| 502 |
parsed_notify = Ikev2PayloadNotifyAuthenticationFailed.parse_exact_size( |
| 503 |
self.notify_bytes_authentication_failed) |
| 504 |
self.assertEqual(parsed_notify.type, Ikev2NotifyType.AUTHENTICATION_FAILED) |
| 505 |
self.assertEqual(parsed_notify.protocol_id, self.protocol_id) |
| 506 |
|
| 507 |
|
| 508 |
class TestIkev2PayloadNotifyUnparsed(unittest.TestCase): |
| 509 |
def setUp(self): |
| 510 |
self.notify_type = Ikev2NotifyType.INVALID_SYNTAX |
| 511 |
self.notify_data = ( |
| 512 |
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 513 |
b'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' |
| 514 |
) |
| 515 |
|
| 516 |
self.notify_dict_with_data = collections.OrderedDict([ |
| 517 |
('next_payload', b'\x00'), # NONE |
| 518 |
('flags', b'\x00'), |
| 519 |
('payload_length', b'\x00\x28'), # 40 bytes (8 header + 32 data) |
| 520 |
('protocol_id', b'\x01'), # IKE protocol |
| 521 |
('spi_size', b'\x00'), |
| 522 |
('notify_type', b'\x00\x07'), # INVALID_SYNTAX (0x0007) |
| 523 |
]) |
| 524 |
self.notify_bytes_with_data = b''.join(self.notify_dict_with_data.values()) + self.notify_data |
| 525 |
|
| 526 |
def test_parse(self): |
| 527 |
parsed_notify: Ikev2PayloadNotifyUnparsed = Ikev2PayloadNotifyUnparsed.parse_exact_size( |
| 528 |
self.notify_bytes_with_data) |
| 529 |
self.assertEqual(parsed_notify.data, self.notify_data) # pylint: disable=no-member |
| 530 |
|
| 531 |
def test_compose(self): |
| 532 |
notify_payload = Ikev2PayloadNotifyUnparsed( |
| 533 |
flags=set(), |
| 534 |
protocol_id=Ikev2ProtocolId.IKE, |
| 535 |
type=self.notify_type, |
| 536 |
spi=b'', |
| 537 |
data=self.notify_data |
| 538 |
) |
| 539 |
notify_payload.next_payload = Ikev2PayloadType.NONE |
| 540 |
|
| 541 |
composed_bytes = notify_payload.compose() |
| 542 |
self.assertEqual(composed_bytes, self.notify_bytes_with_data) |
| 543 |
|
| 544 |
def test_round_trip(self): |
| 545 |
original_payload = Ikev2PayloadNotifyUnparsed( |
| 546 |
flags=set(), |
| 547 |
protocol_id=Ikev2ProtocolId.IKE, |
| 548 |
type=self.notify_type, |
| 549 |
spi=b'', |
| 550 |
data=self.notify_data |
| 551 |
) |
| 552 |
original_payload.next_payload = Ikev2PayloadType.NONE |
| 553 |
|
| 554 |
composed_bytes = original_payload.compose() |
| 555 |
parsed_payload: Ikev2PayloadNotifyUnparsed = Ikev2PayloadNotifyUnparsed.parse_exact_size(composed_bytes) |
| 556 |
|
| 557 |
self.assertEqual(parsed_payload.data, original_payload.data) # pylint: disable=no-member |
| 558 |
|
| 559 |
|
| 560 |
class TestIkev2NotifyPayloadInvalidKe(unittest.TestCase): |
| 561 |
def setUp(self): |
| 562 |
self.dh_group = Ikev2DiffieHellmanGroup.MODP_GROUP_2048_BIT |
| 563 |
|
| 564 |
self.invalid_ke_dict = collections.OrderedDict([ |
| 565 |
('next_payload', b'\x00'), # NONE |
| 566 |
('flags', b'\x00'), |
| 567 |
('payload_length', b'\x00\x0a'), # 10 bytes (8 header + 2 dh_group) |
| 568 |
('protocol_id', b'\x01'), # IKE protocol |
| 569 |
('spi_size', b'\x00'), |
| 570 |
('notify_type', b'\x00\x11'), # INVALID_KE_PAYLOAD (0x0011) |
| 571 |
('dh_group', b'\x00\x0e'), # MODP_GROUP_2048_BIT (0x000e) |
| 572 |
]) |
| 573 |
self.invalid_ke_bytes = b''.join(self.invalid_ke_dict.values()) |
| 574 |
self.invalid_ke_payload = Ikev2NotifyPayloadInvalidKe( |
| 575 |
flags=set(), |
| 576 |
protocol_id=Ikev2ProtocolId.IKE, |
| 577 |
type=Ikev2NotifyType.INVALID_KE_PAYLOAD, |
| 578 |
spi=b'', |
| 579 |
dh_group=self.dh_group |
| 580 |
) |
| 581 |
self.invalid_ke_payload.next_payload = Ikev2PayloadType.NONE |
| 582 |
|
| 583 |
def test_get_message_type(self): |
| 584 |
self.assertEqual(Ikev2NotifyPayloadInvalidKe._get_message_type(), # pylint: disable=protected-access |
| 585 |
Ikev2NotifyType.INVALID_KE_PAYLOAD) |
| 586 |
|
| 587 |
def test_parse(self): |
| 588 |
parsed_notify: Ikev2NotifyPayloadInvalidKe = Ikev2NotifyPayloadInvalidKe.parse_exact_size(self.invalid_ke_bytes) |
| 589 |
self.assertEqual(parsed_notify.type, Ikev2NotifyType.INVALID_KE_PAYLOAD) |
| 590 |
self.assertEqual(parsed_notify.protocol_id, Ikev2ProtocolId.IKE) |
| 591 |
self.assertEqual(parsed_notify.dh_group, self.dh_group) # pylint: disable=no-member |
| 592 |
|
| 593 |
def test_compose(self): |
| 594 |
composed_bytes = self.invalid_ke_payload.compose() |
| 595 |
self.assertEqual(composed_bytes, self.invalid_ke_bytes) |
| 596 |
|
| 597 |
def test_round_trip(self): |
| 598 |
composed_bytes = self.invalid_ke_payload.compose() |
| 599 |
parsed_payload: Ikev2NotifyPayloadInvalidKe = Ikev2NotifyPayloadInvalidKe.parse_exact_size(composed_bytes) |
| 600 |
|
| 601 |
# Verify all attributes are preserved |
| 602 |
self.assertEqual(parsed_payload.dh_group, self.invalid_ke_payload.dh_group) # pylint: disable=no-member |
| 603 |
self.assertEqual(parsed_payload.flags, self.invalid_ke_payload.flags) |
| 604 |
self.assertEqual(parsed_payload.next_payload, self.invalid_ke_payload.next_payload) |
| 605 |
self.assertEqual(parsed_payload.spi, self.invalid_ke_payload.spi) |
| 606 |
|
| 607 |
|
| 608 |
class TestIkev2PayloadCertificateRequest(unittest.TestCase): |
| 609 |
_CERT_ENCODING = Ikev2CertificateType.X509_CERTIFICATE_SIGNATURE |
| 610 |
_CERTIFICATION_AUTHORITY = ( |
| 611 |
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 612 |
b'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' |
| 613 |
) |
| 614 |
_CERTREQ_BYTES = bytes.fromhex( |
| 615 |
'00' # next_payload = NONE |
| 616 |
'00' # flags |
| 617 |
'0025' # payload_length = 37 (4 header + 1 cert_encoding + 32 certification authority) |
| 618 |
'04' # cert_encoding = X.509 certificate signature |
| 619 |
) + _CERTIFICATION_AUTHORITY |
| 620 |
|
| 621 |
def test_get_payload_type(self): |
| 622 |
self.assertEqual(Ikev2PayloadCertificateRequest.get_payload_type(), Ikev2PayloadType.CERTREQ) |
| 623 |
|
| 624 |
def test_parse(self): |
| 625 |
parsed_certreq: Ikev2PayloadCertificateRequest = Ikev2PayloadCertificateRequest.parse_exact_size( |
| 626 |
self._CERTREQ_BYTES) |
| 627 |
self.assertEqual(parsed_certreq.next_payload, Ikev2PayloadType.NONE) |
| 628 |
self.assertEqual(parsed_certreq.flags, set()) |
| 629 |
self.assertEqual(parsed_certreq.cert_encoding, self._CERT_ENCODING) |
| 630 |
self.assertEqual(parsed_certreq.certification_authority, self._CERTIFICATION_AUTHORITY) |
| 631 |
|
| 632 |
def test_compose(self): |
| 633 |
certreq_payload = Ikev2PayloadCertificateRequest( |
| 634 |
flags=set(), |
| 635 |
cert_encoding=self._CERT_ENCODING, |
| 636 |
certification_authority=self._CERTIFICATION_AUTHORITY, |
| 637 |
) |
| 638 |
certreq_payload.next_payload = Ikev2PayloadType.NONE |
| 639 |
self.assertEqual(certreq_payload.compose(), self._CERTREQ_BYTES) |
| 640 |
|
| 641 |
def test_round_trip(self): |
| 642 |
certreq_payload = Ikev2PayloadCertificateRequest( |
| 643 |
flags=set(), |
| 644 |
cert_encoding=self._CERT_ENCODING, |
| 645 |
certification_authority=self._CERTIFICATION_AUTHORITY, |
| 646 |
) |
| 647 |
certreq_payload.next_payload = Ikev2PayloadType.NONE |
| 648 |
composed_bytes = certreq_payload.compose() |
| 649 |
parsed_payload: Ikev2PayloadCertificateRequest = Ikev2PayloadCertificateRequest.parse_exact_size( |
| 650 |
composed_bytes) |
| 651 |
|
| 652 |
self.assertEqual(parsed_payload.cert_encoding, certreq_payload.cert_encoding) |
| 653 |
self.assertEqual(parsed_payload.certification_authority, certreq_payload.certification_authority) |
| 654 |
self.assertEqual(parsed_payload.flags, certreq_payload.flags) |
| 655 |
self.assertEqual(parsed_payload.next_payload, certreq_payload.next_payload) |
| 656 |
|
| 657 |
def test_error_parse_not_enough_data(self): |
| 658 |
incomplete_data = self._CERTREQ_BYTES[:-5] |
| 659 |
|
| 660 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 661 |
Ikev2PayloadCertificateRequest.parse_exact_size(incomplete_data) |
| 662 |
self.assertEqual(context_manager.exception.bytes_needed, 5) |
| 663 |
|
| 664 |
def test_get_authority_hashes_returns_empty_list_for_empty(self): |
| 665 |
payload = Ikev2PayloadCertificateRequest( |
| 666 |
flags=set(), cert_encoding=self._CERT_ENCODING, certification_authority=b'', |
| 667 |
) |
| 668 |
self.assertEqual(payload.get_authority_hashes(), []) |
| 669 |
|
| 670 |
def test_get_authority_hashes_splits_single_20_byte_hash(self): |
| 671 |
hash_a = b'\x00' * 20 |
| 672 |
payload = Ikev2PayloadCertificateRequest( |
| 673 |
flags=set(), cert_encoding=self._CERT_ENCODING, certification_authority=hash_a, |
| 674 |
) |
| 675 |
self.assertEqual(payload.get_authority_hashes(), [hash_a]) |
| 676 |
|
| 677 |
def test_get_authority_hashes_splits_multiple_concatenated_hashes(self): |
| 678 |
hash_a = b'\x00' * 20 |
| 679 |
hash_b = b'\xff' * 20 |
| 680 |
hash_c = b'\x42' * 20 |
| 681 |
payload = Ikev2PayloadCertificateRequest( |
| 682 |
flags=set(), |
| 683 |
cert_encoding=self._CERT_ENCODING, |
| 684 |
certification_authority=hash_a + hash_b + hash_c, |
| 685 |
) |
| 686 |
self.assertEqual(payload.get_authority_hashes(), [hash_a, hash_b, hash_c]) |
| 687 |
|
| 688 |
def test_get_authority_hashes_drops_trailing_partial_chunk(self): |
| 689 |
hash_a = b'\x00' * 20 |
| 690 |
payload = Ikev2PayloadCertificateRequest( |
| 691 |
flags=set(), |
| 692 |
cert_encoding=self._CERT_ENCODING, |
| 693 |
certification_authority=hash_a + b'\xaa\xbb\xcc', # 3 stray octets |
| 694 |
) |
| 695 |
self.assertEqual(payload.get_authority_hashes(), [hash_a]) |
| 696 |
|
| 697 |
def test_get_authority_hashes_accepts_bytearray_certification_authority(self): |
| 698 |
hash_a = b'\x00' * 20 |
| 699 |
payload = Ikev2PayloadCertificateRequest( |
| 700 |
flags=set(), |
| 701 |
cert_encoding=self._CERT_ENCODING, |
| 702 |
certification_authority=bytearray(hash_a), |
| 703 |
) |
| 704 |
self.assertEqual(payload.get_authority_hashes(), [hash_a]) |
| 705 |
|
| 706 |
|
| 707 |
class TestIkev2PayloadCertificate(unittest.TestCase): |
| 708 |
_CERT_ENCODING = Ikev2CertificateType.X509_CERTIFICATE_SIGNATURE |
| 709 |
_CERTIFICATE_DATA = ( |
| 710 |
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 711 |
b'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' |
| 712 |
) |
| 713 |
_CERT_BYTES = bytes.fromhex( |
| 714 |
'00' # next_payload = NONE |
| 715 |
'00' # flags |
| 716 |
'0025' # payload_length = 37 (4 header + 1 cert_encoding + 32 data) |
| 717 |
'04' # cert_encoding = X.509 certificate signature |
| 718 |
) + _CERTIFICATE_DATA |
| 719 |
|
| 720 |
def test_get_payload_type(self): |
| 721 |
self.assertEqual(Ikev2PayloadCertificate.get_payload_type(), Ikev2PayloadType.CERT) |
| 722 |
|
| 723 |
def test_parse(self): |
| 724 |
parsed_cert: Ikev2PayloadCertificate = Ikev2PayloadCertificate.parse_exact_size(self._CERT_BYTES) |
| 725 |
self.assertEqual(parsed_cert.next_payload, Ikev2PayloadType.NONE) |
| 726 |
self.assertEqual(parsed_cert.flags, set()) |
| 727 |
self.assertEqual(parsed_cert.cert_encoding, self._CERT_ENCODING) |
| 728 |
self.assertEqual(parsed_cert.certificate_data, self._CERTIFICATE_DATA) |
| 729 |
|
| 730 |
def test_compose(self): |
| 731 |
cert_payload = Ikev2PayloadCertificate( |
| 732 |
flags=set(), |
| 733 |
cert_encoding=self._CERT_ENCODING, |
| 734 |
certificate_data=self._CERTIFICATE_DATA, |
| 735 |
) |
| 736 |
cert_payload.next_payload = Ikev2PayloadType.NONE |
| 737 |
self.assertEqual(cert_payload.compose(), self._CERT_BYTES) |
| 738 |
|
| 739 |
def test_round_trip(self): |
| 740 |
cert_payload = Ikev2PayloadCertificate( |
| 741 |
flags=set(), |
| 742 |
cert_encoding=self._CERT_ENCODING, |
| 743 |
certificate_data=self._CERTIFICATE_DATA, |
| 744 |
) |
| 745 |
cert_payload.next_payload = Ikev2PayloadType.NONE |
| 746 |
composed_bytes = cert_payload.compose() |
| 747 |
parsed_payload: Ikev2PayloadCertificate = Ikev2PayloadCertificate.parse_exact_size(composed_bytes) |
| 748 |
|
| 749 |
self.assertEqual(parsed_payload.cert_encoding, cert_payload.cert_encoding) |
| 750 |
self.assertEqual(parsed_payload.certificate_data, cert_payload.certificate_data) |
| 751 |
self.assertEqual(parsed_payload.flags, cert_payload.flags) |
| 752 |
self.assertEqual(parsed_payload.next_payload, cert_payload.next_payload) |
| 753 |
|
| 754 |
def test_error_parse_not_enough_data(self): |
| 755 |
incomplete_data = self._CERT_BYTES[:-5] |
| 756 |
|
| 757 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 758 |
Ikev2PayloadCertificate.parse_exact_size(incomplete_data) |
| 759 |
self.assertEqual(context_manager.exception.bytes_needed, 5) |
| 760 |
|
| 761 |
|
| 762 |
class TestIkev2PayloadIdentificationInitiator(unittest.TestCase): |
| 763 |
_IDENTIFIER = 'scanner@cryptolyzer' |
| 764 |
_ID_BYTES = bytes.fromhex( |
| 765 |
'00' # next_payload = NONE |
| 766 |
'00' # flags |
| 767 |
'001b' # payload_length = 27 (4 header + 1 id_type + 3 reserved + 19 id_data) |
| 768 |
'03' # id_type = RFC822_ADDR |
| 769 |
'000000' # reserved |
| 770 |
) + _IDENTIFIER.encode('ascii') |
| 771 |
|
| 772 |
def test_get_payload_type(self): |
| 773 |
self.assertEqual( |
| 774 |
Ikev2PayloadIdentificationInitiatorRfc822Addr.get_payload_type(), |
| 775 |
Ikev2PayloadType.IDI, |
| 776 |
) |
| 777 |
|
| 778 |
def test_parse(self): |
| 779 |
parsed = Ikev2PayloadIdentificationInitiatorRfc822Addr.parse_exact_size(self._ID_BYTES) |
| 780 |
self.assertEqual(parsed.next_payload, Ikev2PayloadType.NONE) |
| 781 |
self.assertEqual(parsed.flags, set()) |
| 782 |
self.assertEqual(parsed.id_type, Ikev2IdType.RFC822_ADDR) |
| 783 |
self.assertEqual(parsed.identifier, self._IDENTIFIER) # pylint: disable=no-member |
| 784 |
|
| 785 |
def test_compose(self): |
| 786 |
id_payload = Ikev2PayloadIdentificationInitiatorRfc822Addr( |
| 787 |
flags=set(), |
| 788 |
identifier=self._IDENTIFIER, |
| 789 |
) |
| 790 |
id_payload.next_payload = Ikev2PayloadType.NONE |
| 791 |
self.assertEqual(id_payload.compose(), self._ID_BYTES) |
| 792 |
|
| 793 |
def test_round_trip(self): |
| 794 |
id_payload = Ikev2PayloadIdentificationInitiatorRfc822Addr( |
| 795 |
flags=set(), |
| 796 |
identifier=self._IDENTIFIER, |
| 797 |
) |
| 798 |
id_payload.next_payload = Ikev2PayloadType.NONE |
| 799 |
composed = id_payload.compose() |
| 800 |
parsed = Ikev2PayloadIdentificationInitiatorRfc822Addr.parse_exact_size(composed) |
| 801 |
|
| 802 |
self.assertEqual(parsed.id_type, id_payload.id_type) |
| 803 |
identifier_parsed = parsed.identifier # pylint: disable=no-member |
| 804 |
identifier_expected = id_payload.identifier # pylint: disable=no-member |
| 805 |
self.assertEqual(identifier_parsed, identifier_expected) |
| 806 |
self.assertEqual(parsed.flags, id_payload.flags) |
| 807 |
self.assertEqual(parsed.next_payload, id_payload.next_payload) |
| 808 |
|
| 809 |
|
| 810 |
class TestIkev2PayloadIdentificationResponder(unittest.TestCase): |
| 811 |
_IDENTIFIER = ipaddress.IPv4Address('192.0.2.1') |
| 812 |
|
| 813 |
def test_get_payload_type(self): |
| 814 |
self.assertEqual( |
| 815 |
Ikev2PayloadIdentificationResponderIpv4Addr.get_payload_type(), |
| 816 |
Ikev2PayloadType.IDR, |
| 817 |
) |
| 818 |
|
| 819 |
def test_round_trip(self): |
| 820 |
id_payload = Ikev2PayloadIdentificationResponderIpv4Addr( |
| 821 |
flags=set(), |
| 822 |
identifier=self._IDENTIFIER, |
| 823 |
) |
| 824 |
id_payload.next_payload = Ikev2PayloadType.NONE |
| 825 |
composed = id_payload.compose() |
| 826 |
parsed = Ikev2PayloadIdentificationResponderIpv4Addr.parse_exact_size(composed) |
| 827 |
self.assertEqual(parsed.id_type, Ikev2IdType.IPV4_ADDR) |
| 828 |
self.assertEqual(parsed.identifier, self._IDENTIFIER) # pylint: disable=no-member |
| 829 |
|
| 830 |
def test_round_trip_der_asn1_dn(self): |
| 831 |
dn_bytes = ( |
| 832 |
b'\x30\x1a\x31\x18\x30\x16\x06\x03\x55\x04\x03\x0c\x0f' |
| 833 |
b'gateway.test.org' |
| 834 |
) |
| 835 |
payload = Ikev2PayloadIdentificationResponderDerAsn1Dn( |
| 836 |
flags=set(), |
| 837 |
identifier=dn_bytes, |
| 838 |
) |
| 839 |
payload.next_payload = Ikev2PayloadType.NONE |
| 840 |
|
| 841 |
composed = payload.compose() |
| 842 |
parsed = Ikev2PayloadIdentificationResponderDerAsn1Dn.parse_exact_size(composed) |
| 843 |
self.assertEqual(parsed.id_type, Ikev2IdType.DER_ASN1_DN) |
| 844 |
self.assertEqual(parsed.identifier, dn_bytes) # pylint: disable=no-member |
| 845 |
|
| 846 |
def test_ipv4_decode_rejects_wrong_length(self): |
| 847 |
with self.assertRaises(InvalidType): |
| 848 |
Ikev2PayloadIdentificationResponderIpv4Addr._decode_identifier( # pylint: disable=protected-access |
| 849 |
b'\xc0\x00\x02', |
| 850 |
) |
| 851 |
|
| 852 |
def test_ipv6_decode_rejects_wrong_length(self): |
| 853 |
with self.assertRaises(InvalidType): |
| 854 |
Ikev2PayloadIdentificationResponderIpv6Addr._decode_identifier( # pylint: disable=protected-access |
| 855 |
b'\x00' * 15, |
| 856 |
) |
| 857 |
|
| 858 |
|
| 859 |
class TestIkev2PayloadIdentificationVariants(unittest.TestCase): |
| 860 |
def test_initiator_fqdn_round_trip(self): |
| 861 |
payload = Ikev2PayloadIdentificationInitiatorFqdn( |
| 862 |
flags=set(), identifier='gateway.example.com', |
| 863 |
) |
| 864 |
payload.next_payload = Ikev2PayloadType.NONE |
| 865 |
parsed = Ikev2PayloadIdentificationInitiatorFqdn.parse_exact_size(payload.compose()) |
| 866 |
self.assertEqual(parsed.id_type, Ikev2IdType.FQDN) |
| 867 |
self.assertEqual(parsed.identifier, 'gateway.example.com') # pylint: disable=no-member |
| 868 |
|
| 869 |
def test_initiator_ipv6_round_trip(self): |
| 870 |
addr = ipaddress.IPv6Address('2001:db8::1') |
| 871 |
payload = Ikev2PayloadIdentificationInitiatorIpv6Addr( |
| 872 |
flags=set(), identifier=addr, |
| 873 |
) |
| 874 |
payload.next_payload = Ikev2PayloadType.NONE |
| 875 |
parsed = Ikev2PayloadIdentificationInitiatorIpv6Addr.parse_exact_size(payload.compose()) |
| 876 |
self.assertEqual(parsed.id_type, Ikev2IdType.IPV6_ADDR) |
| 877 |
self.assertEqual(parsed.identifier, addr) # pylint: disable=no-member |
| 878 |
|
| 879 |
def test_initiator_key_id_round_trip(self): |
| 880 |
payload = Ikev2PayloadIdentificationInitiatorKeyId( |
| 881 |
flags=set(), identifier=b'\x01\x02\x03', |
| 882 |
) |
| 883 |
payload.next_payload = Ikev2PayloadType.NONE |
| 884 |
parsed = Ikev2PayloadIdentificationInitiatorKeyId.parse_exact_size(payload.compose()) |
| 885 |
self.assertEqual(parsed.id_type, Ikev2IdType.KEY_ID) |
| 886 |
self.assertEqual(parsed.identifier, b'\x01\x02\x03') # pylint: disable=no-member |
| 887 |
|
| 888 |
def test_initiator_der_asn1_gn_round_trip(self): |
| 889 |
payload = Ikev2PayloadIdentificationInitiatorDerAsn1Gn( |
| 890 |
flags=set(), identifier=b'\xab\xcd\xef', |
| 891 |
) |
| 892 |
payload.next_payload = Ikev2PayloadType.NONE |
| 893 |
parsed = Ikev2PayloadIdentificationInitiatorDerAsn1Gn.parse_exact_size(payload.compose()) |
| 894 |
self.assertEqual(parsed.id_type, Ikev2IdType.DER_ASN1_GN) |
| 895 |
self.assertEqual(parsed.identifier, b'\xab\xcd\xef') # pylint: disable=no-member |
| 896 |
|
| 897 |
def test_initiator_fc_name_round_trip(self): |
| 898 |
payload = Ikev2PayloadIdentificationInitiatorFcName( |
| 899 |
flags=set(), identifier=b'\x10\x20\x30\x40\x50\x60\x70\x80', |
| 900 |
) |
| 901 |
payload.next_payload = Ikev2PayloadType.NONE |
| 902 |
parsed = Ikev2PayloadIdentificationInitiatorFcName.parse_exact_size(payload.compose()) |
| 903 |
self.assertEqual(parsed.id_type, Ikev2IdType.FC_NAME) |
| 904 |
self.assertEqual(parsed.identifier, b'\x10\x20\x30\x40\x50\x60\x70\x80') # pylint: disable=no-member |
| 905 |
|
| 906 |
def test_initiator_null_round_trip(self): |
| 907 |
payload = Ikev2PayloadIdentificationInitiatorNull( |
| 908 |
flags=set(), identifier=b'', |
| 909 |
) |
| 910 |
payload.next_payload = Ikev2PayloadType.NONE |
| 911 |
parsed = Ikev2PayloadIdentificationInitiatorNull.parse_exact_size(payload.compose()) |
| 912 |
self.assertEqual(parsed.id_type, Ikev2IdType.NULL) |
| 913 |
self.assertEqual(parsed.identifier, b'') # pylint: disable=no-member |
| 914 |
|
| 915 |
def test_id_payload_rejects_negative_id_data_length(self): |
| 916 |
# payload_length=6 → id_data_length = 6 - 4 - 4 = -2 → NotEnoughData |
| 917 |
bogus = b'\x00\x00\x00\x06\x02\x00\x00\x00\x00\x00' |
| 918 |
with self.assertRaises(NotEnoughData): |
| 919 |
Ikev2PayloadIdentificationInitiatorFqdn.parse_exact_size(bogus) |
| 920 |
|
| 921 |
def test_id_payload_rejects_id_type_mismatch(self): |
| 922 |
# Wire id_type = 0x01 (IPV4_ADDR); caller uses FQDN subclass. |
| 923 |
wire = b'\x00\x00\x00\x0c\x01\x00\x00\x00\xc0\x00\x02\x01' |
| 924 |
with self.assertRaises(InvalidType): |
| 925 |
Ikev2PayloadIdentificationInitiatorFqdn.parse_exact_size(wire) |
| 926 |
|
| 927 |
def test_initiator_variant_dispatch_picks_subclass(self): |
| 928 |
source = Ikev2PayloadIdentificationInitiatorFqdn( |
| 929 |
flags=set(), identifier='initiator.example', |
| 930 |
) |
| 931 |
source.next_payload = Ikev2PayloadType.NONE |
| 932 |
parsed = Ikev2PayloadIdentificationInitiator.parse_exact_size(source.compose()) |
| 933 |
self.assertIsInstance(parsed, Ikev2PayloadIdentificationInitiatorFqdn) |
| 934 |
|
| 935 |
def test_responder_variant_dispatch_picks_subclass(self): |
| 936 |
source = Ikev2PayloadIdentificationResponderFqdn( |
| 937 |
flags=set(), identifier='responder.example', |
| 938 |
) |
| 939 |
source.next_payload = Ikev2PayloadType.NONE |
| 940 |
parsed = Ikev2PayloadIdentificationResponder.parse_exact_size(source.compose()) |
| 941 |
self.assertIsInstance(parsed, Ikev2PayloadIdentificationResponderFqdn) |
| 942 |
|
| 943 |
def test_rfc822_mixin_returns_none_for_ikev1(self): |
| 944 |
self.assertIsNone(Ikev2PayloadIdentificationInitiatorRfc822Addr.get_id_type_ikev1()) |
| 945 |
|
| 946 |
def test_fc_name_mixin_returns_none_for_ikev1(self): |
| 947 |
self.assertIsNone(Ikev2PayloadIdentificationInitiatorFcName.get_id_type_ikev1()) |
| 948 |
|
| 949 |
def test_null_mixin_returns_none_for_ikev1(self): |
| 950 |
self.assertIsNone(Ikev2PayloadIdentificationInitiatorNull.get_id_type_ikev1()) |
| 951 |
|
| 952 |
|
| 953 |
class TestIkev2PayloadAuthentication(unittest.TestCase): |
| 954 |
_AUTH_METHOD = Ikev2AuthenticationMethod.RSA_DIGITAL_SIGNATURE |
| 955 |
_AUTH_DATA = b'\xab' * 256 # 2048-bit RSA signature |
| 956 |
_AUTH_BYTES = bytes.fromhex( |
| 957 |
'00' # next_payload = NONE |
| 958 |
'00' # flags |
| 959 |
'0108' # payload_length = 264 (4 header + 1 auth_method + 3 reserved + 256 auth_data) |
| 960 |
'01' # auth_method = RSA_DIGITAL_SIGNATURE |
| 961 |
'000000' # reserved |
| 962 |
) + _AUTH_DATA |
| 963 |
|
| 964 |
def test_get_payload_type(self): |
| 965 |
self.assertEqual(Ikev2PayloadAuthentication.get_payload_type(), Ikev2PayloadType.AUTH) |
| 966 |
|
| 967 |
def test_parse(self): |
| 968 |
parsed: Ikev2PayloadAuthentication = Ikev2PayloadAuthentication.parse_exact_size(self._AUTH_BYTES) |
| 969 |
self.assertEqual(parsed.auth_method, self._AUTH_METHOD) |
| 970 |
self.assertEqual(parsed.auth_data, self._AUTH_DATA) |
| 971 |
|
| 972 |
def test_round_trip_rsa(self): |
| 973 |
auth_payload = Ikev2PayloadAuthentication( |
| 974 |
flags=set(), |
| 975 |
auth_method=self._AUTH_METHOD, |
| 976 |
auth_data=self._AUTH_DATA, |
| 977 |
) |
| 978 |
auth_payload.next_payload = Ikev2PayloadType.NONE |
| 979 |
composed = auth_payload.compose() |
| 980 |
parsed: Ikev2PayloadAuthentication = Ikev2PayloadAuthentication.parse_exact_size(composed) |
| 981 |
self.assertEqual(parsed.auth_method, self._AUTH_METHOD) |
| 982 |
self.assertEqual(parsed.auth_data, self._AUTH_DATA) |
| 983 |
|
| 984 |
def test_round_trip_ecdsa_256(self): |
| 985 |
payload = Ikev2PayloadAuthentication( |
| 986 |
flags=set(), |
| 987 |
auth_method=Ikev2AuthenticationMethod.ECDSA_SHA_256_P_256, |
| 988 |
auth_data=b'\xcd' * 64, # r||s for P-256 |
| 989 |
) |
| 990 |
payload.next_payload = Ikev2PayloadType.NONE |
| 991 |
composed = payload.compose() |
| 992 |
parsed: Ikev2PayloadAuthentication = Ikev2PayloadAuthentication.parse_exact_size(composed) |
| 993 |
self.assertEqual(parsed.auth_method, Ikev2AuthenticationMethod.ECDSA_SHA_256_P_256) |
| 994 |
self.assertEqual(len(parsed.auth_data), 64) |
| 995 |
|
| 996 |
def test_error_parse_negative_auth_data_length(self): |
| 997 |
# payload_length=6 → auth_data_length = 6 - 4 - 4 = -2 → NotEnoughData |
| 998 |
bogus = b'\x00\x00\x00\x06\x01\x00\x00\x00\x00\x00' |
| 999 |
with self.assertRaises(NotEnoughData): |
| 1000 |
Ikev2PayloadAuthentication.parse_exact_size(bogus) |
| 1001 |
|
| 1002 |
def test_round_trip_null(self): |
| 1003 |
payload = Ikev2PayloadAuthentication( |
| 1004 |
flags=set(), |
| 1005 |
auth_method=Ikev2AuthenticationMethod.NULL_AUTHENTICATION, |
| 1006 |
auth_data=b'\xef' * 32, # HMAC(SK_pi, ...) output sized to the prf |
| 1007 |
) |
| 1008 |
payload.next_payload = Ikev2PayloadType.NONE |
| 1009 |
composed = payload.compose() |
| 1010 |
parsed: Ikev2PayloadAuthentication = Ikev2PayloadAuthentication.parse_exact_size(composed) |
| 1011 |
self.assertEqual(parsed.auth_method, Ikev2AuthenticationMethod.NULL_AUTHENTICATION) |
| 1012 |
|
| 1013 |
|
| 1014 |
class TestIkev2AuthDigitalSignatureEnvelope(unittest.TestCase): |
| 1015 |
# AlgorithmIdentifier for ecdsa-with-SHA256 (OID 1.2.840.10045.4.3.2), |
| 1016 |
# encoded as a DER SEQUENCE so asn1crypto can decode the OID. |
| 1017 |
_SIGNATURE_ALGORITHM = Signature.ECDSA_WITH_SHA2_256 |
| 1018 |
_ALGORITHM_IDENTIFIER = bytes.fromhex('300a06082a8648ce3d040302') |
| 1019 |
_SIGNATURE = b'\xab' * 64 # P-256 r||s |
| 1020 |
_ENVELOPE_BYTES = bytes([len(_ALGORITHM_IDENTIFIER)]) + _ALGORITHM_IDENTIFIER + _SIGNATURE |
| 1021 |
|
| 1022 |
def test_round_trip(self): |
| 1023 |
envelope = Ikev2AuthDigitalSignatureEnvelope( |
| 1024 |
signature_algorithm=self._SIGNATURE_ALGORITHM, |
| 1025 |
signature=self._SIGNATURE, |
| 1026 |
) |
| 1027 |
composed = envelope.compose() |
| 1028 |
self.assertEqual(composed, self._ENVELOPE_BYTES) |
| 1029 |
|
| 1030 |
parsed = Ikev2AuthDigitalSignatureEnvelope.from_bytes(composed) |
| 1031 |
self.assertEqual(parsed.signature_algorithm, self._SIGNATURE_ALGORITHM) |
| 1032 |
self.assertEqual(parsed.signature, self._SIGNATURE) |
| 1033 |
|
| 1034 |
def test_from_auth_payload(self): |
| 1035 |
payload = Ikev2PayloadAuthentication( |
| 1036 |
flags=set(), |
| 1037 |
auth_method=Ikev2AuthenticationMethod.DIGITAL_SIGNATURE, |
| 1038 |
auth_data=self._ENVELOPE_BYTES, |
| 1039 |
) |
| 1040 |
payload.next_payload = Ikev2PayloadType.NONE |
| 1041 |
|
| 1042 |
envelope: Ikev2AuthDigitalSignatureEnvelope = payload.parse_digital_signature_envelope() |
| 1043 |
self.assertEqual(envelope.signature_algorithm, self._SIGNATURE_ALGORITHM) |
| 1044 |
self.assertEqual(envelope.signature, self._SIGNATURE) |
| 1045 |
|
| 1046 |
def test_wrong_auth_method_rejected(self): |
| 1047 |
payload = Ikev2PayloadAuthentication( |
| 1048 |
flags=set(), |
| 1049 |
auth_method=Ikev2AuthenticationMethod.RSA_DIGITAL_SIGNATURE, |
| 1050 |
auth_data=self._ENVELOPE_BYTES, |
| 1051 |
) |
| 1052 |
payload.next_payload = Ikev2PayloadType.NONE |
| 1053 |
|
| 1054 |
with self.assertRaises(InvalidType): |
| 1055 |
payload.parse_digital_signature_envelope() |
| 1056 |
|
| 1057 |
def test_parse_unknown_oid_raises_invalid_value(self): |
| 1058 |
# AlgorithmIdentifier for the made-up OID 1.2.3.4.5 (not in |
| 1059 |
# cryptodatahub's Signature registry). |
| 1060 |
# DER: SEQUENCE(0x30) len=6 OID(0x06) len=4 first-two(0x2A) 03 04 05 |
| 1061 |
algorithm_identifier = bytes.fromhex('300606042a030405') |
| 1062 |
envelope_bytes = ( |
| 1063 |
bytes([len(algorithm_identifier)]) + algorithm_identifier + b'\xcc' * 32 |
| 1064 |
) |
| 1065 |
with self.assertRaises(InvalidValue): |
| 1066 |
Ikev2AuthDigitalSignatureEnvelope.from_bytes(envelope_bytes) |
| 1067 |
|
| 1068 |
|
| 1069 |
class TestIkev2PayloadEncryptedAndAuthenticated(unittest.TestCase): |
| 1070 |
# 16-octet IV + 16 octets of "encrypted" body + 1 octet pad length + 16 octet ICV |
| 1071 |
_ENCRYPTED_DATA = b'\x11' * 16 + b'\x22' * 16 + b'\x00' + b'\x33' * 16 |
| 1072 |
_SK_BYTES = bytes.fromhex( |
| 1073 |
'23' # next_payload = IDI (35 = inner first payload after decrypt) |
| 1074 |
'00' # flags |
| 1075 |
'0035' # payload_length = 53 (4 header + 49 encrypted_data) |
| 1076 |
) + _ENCRYPTED_DATA |
| 1077 |
|
| 1078 |
def test_get_payload_type(self): |
| 1079 |
self.assertEqual(Ikev2PayloadEncryptedAndAuthenticated.get_payload_type(), Ikev2PayloadType.SK) |
| 1080 |
|
| 1081 |
def test_parse(self): |
| 1082 |
parsed: Ikev2PayloadEncryptedAndAuthenticated |
| 1083 |
parsed = Ikev2PayloadEncryptedAndAuthenticated.parse_exact_size(self._SK_BYTES) |
| 1084 |
self.assertEqual(parsed.next_payload, Ikev2PayloadType.IDI) |
| 1085 |
self.assertEqual(parsed.flags, set()) |
| 1086 |
self.assertEqual(parsed.encrypted_data, self._ENCRYPTED_DATA) |
| 1087 |
|
| 1088 |
def test_round_trip(self): |
| 1089 |
sk_payload = Ikev2PayloadEncryptedAndAuthenticated( |
| 1090 |
flags=set(), |
| 1091 |
encrypted_data=self._ENCRYPTED_DATA, |
| 1092 |
) |
| 1093 |
sk_payload.next_payload = Ikev2PayloadType.IDI |
| 1094 |
composed = sk_payload.compose() |
| 1095 |
self.assertEqual(composed, self._SK_BYTES) |
| 1096 |
|
| 1097 |
parsed: Ikev2PayloadEncryptedAndAuthenticated |
| 1098 |
parsed = Ikev2PayloadEncryptedAndAuthenticated.parse_exact_size(composed) |
| 1099 |
self.assertEqual(parsed.encrypted_data, self._ENCRYPTED_DATA) |
| 1100 |
self.assertEqual(parsed.next_payload, Ikev2PayloadType.IDI) |
| 1101 |
|
| 1102 |
|
| 1103 |
class TestIkev2PayloadEap(unittest.TestCase): |
| 1104 |
_EAP_DATA = b'\x01\x01\x00\x05\x01' # EAP-Request/Identity-ish |
| 1105 |
_EAP_BYTES = bytes.fromhex( |
| 1106 |
'00' # next_payload = NONE |
| 1107 |
'00' # flags |
| 1108 |
'0009' # payload_length = 9 (4 header + 5 eap_data) |
| 1109 |
) + _EAP_DATA |
| 1110 |
|
| 1111 |
def test_get_payload_type(self): |
| 1112 |
self.assertEqual(Ikev2PayloadEap.get_payload_type(), Ikev2PayloadType.EAP) |
| 1113 |
|
| 1114 |
def test_round_trip(self): |
| 1115 |
eap_payload = Ikev2PayloadEap( |
| 1116 |
flags=set(), |
| 1117 |
eap_data=self._EAP_DATA, |
| 1118 |
) |
| 1119 |
eap_payload.next_payload = Ikev2PayloadType.NONE |
| 1120 |
composed = eap_payload.compose() |
| 1121 |
self.assertEqual(composed, self._EAP_BYTES) |
| 1122 |
parsed: Ikev2PayloadEap = Ikev2PayloadEap.parse_exact_size(composed) |
| 1123 |
self.assertEqual(parsed.eap_data, self._EAP_DATA) |
| 1124 |
|
| 1125 |
|
| 1126 |
class TestIkev2PayloadVendorId(unittest.TestCase): |
| 1127 |
def setUp(self): |
| 1128 |
self.vendor_id = ( |
| 1129 |
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 1130 |
b'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' |
| 1131 |
) |
| 1132 |
|
| 1133 |
self.vendor_dict = collections.OrderedDict([ |
| 1134 |
('next_payload', b'\x00'), # NONE |
| 1135 |
('flags', b'\x80'), # CRITICAL (0x80) |
| 1136 |
('payload_length', b'\x00\x24'), # 36 bytes (4 header + 32 vendor_id) |
| 1137 |
('vendor_id', self.vendor_id), |
| 1138 |
]) |
| 1139 |
self.vendor_bytes = b''.join(self.vendor_dict.values()) |
| 1140 |
|
| 1141 |
self.vendor_payload = Ikev2PayloadVendorId( |
| 1142 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 1143 |
vendor_id=self.vendor_id |
| 1144 |
) |
| 1145 |
self.vendor_payload.next_payload = Ikev2PayloadType.NONE |
| 1146 |
|
| 1147 |
def test_get_payload_type(self): |
| 1148 |
self.assertEqual(Ikev2PayloadVendorId.get_payload_type(), Ikev2PayloadType.VENDOR_ID) |
| 1149 |
|
| 1150 |
def test_parse(self): |
| 1151 |
parsed_vendor: Ikev2PayloadVendorId = Ikev2PayloadVendorId.parse_exact_size(self.vendor_bytes) |
| 1152 |
self.assertEqual(parsed_vendor.next_payload, Ikev2PayloadType.NONE) |
| 1153 |
self.assertEqual(parsed_vendor.flags, {Ikev2PayloadFlags.CRITICAL}) |
| 1154 |
self.assertEqual(parsed_vendor.vendor_id, self.vendor_id) |
| 1155 |
|
| 1156 |
def test_compose(self): |
| 1157 |
composed_bytes = self.vendor_payload.compose() |
| 1158 |
self.assertEqual(composed_bytes, self.vendor_bytes) |
| 1159 |
|
| 1160 |
def test_round_trip(self): |
| 1161 |
composed_bytes = self.vendor_payload.compose() |
| 1162 |
parsed_payload: Ikev2PayloadVendorId = Ikev2PayloadVendorId.parse_exact_size(composed_bytes) |
| 1163 |
|
| 1164 |
self.assertEqual(parsed_payload.vendor_id, self.vendor_payload.vendor_id) |
| 1165 |
self.assertEqual(parsed_payload.flags, self.vendor_payload.flags) |
| 1166 |
self.assertEqual(parsed_payload.next_payload, self.vendor_payload.next_payload) |
| 1167 |
|
| 1168 |
def test_error_parse_not_enough_data(self): |
| 1169 |
incomplete_data = self.vendor_bytes[:-10] |
| 1170 |
|
| 1171 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 1172 |
Ikev2PayloadVendorId.parse_exact_size(incomplete_data) |
| 1173 |
self.assertEqual(context_manager.exception.bytes_needed, 10) |
| 1174 |
|
| 1175 |
|
| 1176 |
class TestTransformAttributeKeyLength(unittest.TestCase): |
| 1177 |
def setUp(self): |
| 1178 |
self.key_length_value = 128 |
| 1179 |
self.key_length_attribute = TransformAttributeKeyLength(value=self.key_length_value) |
| 1180 |
|
| 1181 |
def test_parse(self): |
| 1182 |
composed_bytes = self.key_length_attribute.compose() |
| 1183 |
parsed_attribute = TransformAttributeKeyLength.parse_exact_size(composed_bytes) |
| 1184 |
|
| 1185 |
self.assertEqual(parsed_attribute.value, self.key_length_attribute.value) |
| 1186 |
|
| 1187 |
def test_compose(self): |
| 1188 |
composed_bytes = self.key_length_attribute.compose() |
| 1189 |
self.assertEqual(len(composed_bytes), 4) |
| 1190 |
self.assertEqual(composed_bytes[0], 0x80) |
| 1191 |
self.assertEqual(composed_bytes[1], Ikev2TransformAttributeType.KEY_LENGTH.value.code) |
| 1192 |
self.assertEqual(composed_bytes[2:4], b'\x00\x80') |
| 1193 |
|
| 1194 |
def test_round_trip(self): |
| 1195 |
composed_bytes = self.key_length_attribute.compose() |
| 1196 |
parsed_attribute = TransformAttributeKeyLength.parse_exact_size(composed_bytes) |
| 1197 |
|
| 1198 |
self.assertEqual(parsed_attribute.value, self.key_length_attribute.value) |
| 1199 |
|
| 1200 |
def test_error_parse_not_enough_data(self): |
| 1201 |
incomplete_data = b'\x80\x0e\x00' |
| 1202 |
|
| 1203 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 1204 |
TransformAttributeKeyLength.parse_exact_size(incomplete_data) |
| 1205 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 1206 |
|
| 1207 |
|
| 1208 |
if __name__ == '__main__': |
| 1209 |
unittest.main() |