| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import collections |
| 4 |
import unittest |
| 5 |
|
| 6 |
from cryptodatahub.common.exception import InvalidValue |
| 7 |
from cryptodatahub.ike.algorithm import Ikev2HashAlgorithm, Ikev2NotifyType, Ikev2ProtocolId |
| 8 |
|
| 9 |
from cryptoparser.common.exception import NotEnoughData, InvalidType |
| 10 |
from cryptoparser.ike.ikev2 import ( |
| 11 |
Ikev2PayloadFlags, |
| 12 |
Ikev2PayloadType, |
| 13 |
Ikev2PayloadNotifyUnparsed, |
| 14 |
Ikev2NotifyPayloadChildlessIkev2Supported, |
| 15 |
Ikev2NotifyPayloadCookie, |
| 16 |
Ikev2NotifyPayloadHttpCertLookupSupported, |
| 17 |
Ikev2NotifyPayloadIkev2FragmentationSupported, |
| 18 |
Ikev2NotifyPayloadIntermediateExchangeSupported, |
| 19 |
Ikev2NotifyPayloadNatDetectionDestinationIp, |
| 20 |
Ikev2NotifyPayloadNatDetectionSourceIp, |
| 21 |
Ikev2NotifyPayloadRedirectSupported, |
| 22 |
Ikev2NotifyPayloadSetWindowSize, |
| 23 |
Ikev2NotifyPayloadSignatureHashAlgorithms, |
| 24 |
Ikev2NotifyPayloadUsePpk, |
| 25 |
Ikev2NotifyPayloadUseTransportMode, |
| 26 |
Ikev2NotifyPayloadVariantResponder, |
| 27 |
) |
| 28 |
|
| 29 |
from . import classes as _ike_test_classes |
| 30 |
from .classes import Ikev2PayloadNotifyBaseTest, Ikev2PayloadNotifyNoDataTest |
| 31 |
|
| 32 |
|
| 33 |
class TestIkev2PayloadNotifyBase(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 34 |
def setUp(self): |
| 35 |
self.protocol_id = Ikev2ProtocolId.IKE |
| 36 |
self.notify_type = Ikev2NotifyType.AUTHENTICATION_FAILED |
| 37 |
self.spi = b'\x00\x01\x02\x03' |
| 38 |
self.test_data = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 39 |
|
| 40 |
self.notify_payload_minimal = Ikev2PayloadNotifyBaseTest( |
| 41 |
flags=set(), |
| 42 |
protocol_id=self.protocol_id, |
| 43 |
notify_type=self.notify_type, |
| 44 |
spi=b'', |
| 45 |
test_data=b'' |
| 46 |
) |
| 47 |
self.notify_payload_minimal.next_payload = Ikev2PayloadType.NONE |
| 48 |
|
| 49 |
self.notify_payload_with_data = Ikev2PayloadNotifyBaseTest( |
| 50 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 51 |
protocol_id=self.protocol_id, |
| 52 |
notify_type=self.notify_type, |
| 53 |
spi=self.spi, |
| 54 |
test_data=self.test_data |
| 55 |
) |
| 56 |
self.notify_payload_with_data.next_payload = Ikev2PayloadType.KE |
| 57 |
|
| 58 |
self.notify_dict_minimal = collections.OrderedDict([ |
| 59 |
('next_payload', b'\x00'), # NONE |
| 60 |
('flags', b'\x00'), # No flags |
| 61 |
('payload_length', b'\x00\x08'), # 4 bytes header + 4 bytes notify header + 0 bytes data |
| 62 |
('protocol_id', b'\x01'), # IKE |
| 63 |
('spi_size', b'\x00'), # 0 bytes SPI |
| 64 |
('notify_type', b'\x00\x18'), # AUTHENTICATION_FAILED (0x0018) |
| 65 |
]) |
| 66 |
self.notify_bytes_minimal = b''.join(self.notify_dict_minimal.values()) |
| 67 |
|
| 68 |
self.notify_dict_with_data = collections.OrderedDict([ |
| 69 |
('next_payload', b'\x22'), # KE |
| 70 |
('flags', b'\x80'), # CRITICAL |
| 71 |
('payload_length', b'\x00\x1c'), # 4 bytes header + 4 bytes notify header + 4 bytes SPI + 16 bytes data |
| 72 |
('protocol_id', b'\x01'), # IKE |
| 73 |
('spi_size', b'\x04'), # 4 bytes SPI |
| 74 |
('notify_type', b'\x00\x18'), # AUTHENTICATION_FAILED (0x0018) |
| 75 |
('spi', self.spi), # SPI data |
| 76 |
('test_data', self.test_data), # Notification data |
| 77 |
]) |
| 78 |
self.notify_bytes_with_data = b''.join(self.notify_dict_with_data.values()) |
| 79 |
|
| 80 |
def test_get_payload_type(self): |
| 81 |
self.assertEqual(Ikev2PayloadNotifyBaseTest.get_payload_type(), Ikev2PayloadType.NOTIFY) |
| 82 |
|
| 83 |
def test_parse(self): |
| 84 |
parsed_minimal: Ikev2PayloadNotifyBaseTest = Ikev2PayloadNotifyBaseTest.parse_exact_size( |
| 85 |
self.notify_bytes_minimal |
| 86 |
) |
| 87 |
self.assertEqual(parsed_minimal.flags, set()) |
| 88 |
self.assertEqual(parsed_minimal.next_payload, Ikev2PayloadType.NONE) |
| 89 |
self.assertEqual(parsed_minimal.protocol_id, self.protocol_id) |
| 90 |
self.assertEqual(parsed_minimal.type, self.notify_type) |
| 91 |
self.assertEqual(parsed_minimal.spi, b'') |
| 92 |
self.assertEqual(parsed_minimal.test_data, b'') |
| 93 |
|
| 94 |
parsed_with_data: Ikev2PayloadNotifyBaseTest = Ikev2PayloadNotifyBaseTest.parse_exact_size( |
| 95 |
self.notify_bytes_with_data |
| 96 |
) |
| 97 |
self.assertEqual(parsed_with_data.flags, {Ikev2PayloadFlags.CRITICAL}) |
| 98 |
self.assertEqual(parsed_with_data.next_payload, Ikev2PayloadType.KE) |
| 99 |
self.assertEqual(parsed_with_data.protocol_id, self.protocol_id) |
| 100 |
self.assertEqual(parsed_with_data.type, self.notify_type) |
| 101 |
self.assertEqual(parsed_with_data.spi, self.spi) |
| 102 |
self.assertEqual(parsed_with_data.test_data, self.test_data) |
| 103 |
|
| 104 |
def test_compose(self): |
| 105 |
composed_minimal = self.notify_payload_minimal.compose() |
| 106 |
self.assertEqual(composed_minimal, self.notify_bytes_minimal) |
| 107 |
|
| 108 |
composed_with_data = self.notify_payload_with_data.compose() |
| 109 |
self.assertEqual(composed_with_data, self.notify_bytes_with_data) |
| 110 |
|
| 111 |
def test_round_trip(self): |
| 112 |
composed_minimal = self.notify_payload_minimal.compose() |
| 113 |
parsed_minimal: Ikev2PayloadNotifyBaseTest = Ikev2PayloadNotifyBaseTest.parse_exact_size(composed_minimal) |
| 114 |
|
| 115 |
self.assertEqual(parsed_minimal.protocol_id, self.notify_payload_minimal.protocol_id) |
| 116 |
self.assertEqual(parsed_minimal.type, self.notify_payload_minimal.type) |
| 117 |
self.assertEqual(parsed_minimal.spi, self.notify_payload_minimal.spi) |
| 118 |
self.assertEqual(parsed_minimal.test_data, self.notify_payload_minimal.test_data) |
| 119 |
self.assertEqual(parsed_minimal.flags, self.notify_payload_minimal.flags) |
| 120 |
self.assertEqual(parsed_minimal.next_payload, self.notify_payload_minimal.next_payload) |
| 121 |
|
| 122 |
composed_with_data = self.notify_payload_with_data.compose() |
| 123 |
parsed_with_data: Ikev2PayloadNotifyBaseTest = Ikev2PayloadNotifyBaseTest.parse_exact_size(composed_with_data) |
| 124 |
|
| 125 |
self.assertEqual(parsed_with_data.protocol_id, self.notify_payload_with_data.protocol_id) |
| 126 |
self.assertEqual(parsed_with_data.type, self.notify_payload_with_data.type) |
| 127 |
self.assertEqual(parsed_with_data.spi, self.notify_payload_with_data.spi) |
| 128 |
self.assertEqual(parsed_with_data.test_data, self.notify_payload_with_data.test_data) |
| 129 |
self.assertEqual(parsed_with_data.flags, self.notify_payload_with_data.flags) |
| 130 |
self.assertEqual(parsed_with_data.next_payload, self.notify_payload_with_data.next_payload) |
| 131 |
|
| 132 |
def test_error_parse_not_enough_data(self): |
| 133 |
incomplete_data = self.notify_bytes_minimal[:-2] |
| 134 |
|
| 135 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 136 |
Ikev2PayloadNotifyBaseTest.parse_exact_size(incomplete_data) |
| 137 |
self.assertEqual(context_manager.exception.bytes_needed, 2) |
| 138 |
|
| 139 |
def test_error_invalid_protocol_id(self): |
| 140 |
with self.assertRaises(TypeError): |
| 141 |
Ikev2PayloadNotifyBaseTest( |
| 142 |
flags=set(), |
| 143 |
protocol_id="invalid", |
| 144 |
notify_type=self.notify_type, |
| 145 |
spi=b'', |
| 146 |
test_data=b'' |
| 147 |
) |
| 148 |
|
| 149 |
def test_error_invalid_notify_type(self): |
| 150 |
with self.assertRaises(TypeError): |
| 151 |
Ikev2PayloadNotifyBaseTest( |
| 152 |
flags=set(), |
| 153 |
protocol_id=self.protocol_id, |
| 154 |
notify_type="invalid", |
| 155 |
spi=b'', |
| 156 |
test_data=b'' |
| 157 |
) |
| 158 |
|
| 159 |
def test_error_invalid_spi(self): |
| 160 |
with self.assertRaises(TypeError): |
| 161 |
Ikev2PayloadNotifyBaseTest( |
| 162 |
flags=set(), |
| 163 |
protocol_id=self.protocol_id, |
| 164 |
notify_type=self.notify_type, |
| 165 |
spi=None, |
| 166 |
test_data=b'' |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
class TestIkev2PayloadNotifyNoData(unittest.TestCase): |
| 171 |
def setUp(self): |
| 172 |
self.wrong_notify_dict = collections.OrderedDict([ |
| 173 |
('next_payload', b'\x00'), # NONE |
| 174 |
('flags', b'\x00'), # No flags |
| 175 |
('payload_length', b'\x00\x08'), # 4 bytes header + 4 bytes notify header |
| 176 |
('protocol_id', b'\x01'), # IKE |
| 177 |
('spi_size', b'\x00'), # 0 bytes SPI |
| 178 |
('notify_type', b'\x00\x01'), # UNSUPPORTED_CRITICAL_PAYLOAD (not AUTHENTICATION_FAILED) |
| 179 |
]) |
| 180 |
self.wrong_notify_bytes = b''.join(self.wrong_notify_dict.values()) |
| 181 |
|
| 182 |
def test_get_message_type(self): |
| 183 |
# pylint: disable=protected-access |
| 184 |
self.assertEqual(Ikev2PayloadNotifyNoDataTest._get_message_type(), Ikev2NotifyType.AUTHENTICATION_FAILED) |
| 185 |
|
| 186 |
def test_error_invalid_notify_type(self): |
| 187 |
with self.assertRaises(InvalidType): |
| 188 |
Ikev2PayloadNotifyNoDataTest.parse_exact_size(self.wrong_notify_bytes) |
| 189 |
|
| 190 |
|
| 191 |
class TestIkev2PayloadNotifyUnparsed(unittest.TestCase): |
| 192 |
def setUp(self): |
| 193 |
self.protocol_id = Ikev2ProtocolId.IKE |
| 194 |
self.notify_type = Ikev2NotifyType.INVALID_SELECTORS # Different from AUTHENTICATION_FAILED |
| 195 |
self.spi = b'\x00\x01\x02\x03' |
| 196 |
self.data = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 197 |
|
| 198 |
self.notify_payload_with_data = Ikev2PayloadNotifyUnparsed( |
| 199 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 200 |
protocol_id=self.protocol_id, |
| 201 |
type=self.notify_type, |
| 202 |
spi=self.spi, |
| 203 |
data=self.data |
| 204 |
) |
| 205 |
self.notify_payload_with_data.next_payload = Ikev2PayloadType.KE |
| 206 |
|
| 207 |
def test_any_notify_type_support(self): |
| 208 |
different_notify_types = [ |
| 209 |
Ikev2NotifyType.AUTHENTICATION_FAILED, |
| 210 |
Ikev2NotifyType.INVALID_SELECTORS, |
| 211 |
Ikev2NotifyType.UNSUPPORTED_CRITICAL_PAYLOAD, |
| 212 |
] |
| 213 |
|
| 214 |
for notify_type in different_notify_types: |
| 215 |
payload = Ikev2PayloadNotifyUnparsed( |
| 216 |
flags=set(), |
| 217 |
protocol_id=self.protocol_id, |
| 218 |
type=notify_type, |
| 219 |
spi=b'', |
| 220 |
data=b'\x00\x01\x02\x03' |
| 221 |
) |
| 222 |
self.assertEqual(payload.type, notify_type) |
| 223 |
self.assertEqual(payload.data, b'\x00\x01\x02\x03') |
| 224 |
|
| 225 |
def test_raw_data_storage(self): |
| 226 |
payload = Ikev2PayloadNotifyUnparsed( |
| 227 |
flags=set(), |
| 228 |
protocol_id=self.protocol_id, |
| 229 |
type=self.notify_type, |
| 230 |
spi=b'', |
| 231 |
data=self.data |
| 232 |
) |
| 233 |
self.assertEqual(payload.data, self.data) |
| 234 |
|
| 235 |
different_data = b'\xff\xfe\xfd\xfc' |
| 236 |
payload_2 = Ikev2PayloadNotifyUnparsed( |
| 237 |
flags=set(), |
| 238 |
protocol_id=self.protocol_id, |
| 239 |
type=self.notify_type, |
| 240 |
spi=b'', |
| 241 |
data=different_data |
| 242 |
) |
| 243 |
self.assertEqual(payload_2.data, different_data) |
| 244 |
|
| 245 |
def test_round_trip_data_preservation(self): |
| 246 |
payload_no_spi = Ikev2PayloadNotifyUnparsed( |
| 247 |
flags=set(), |
| 248 |
protocol_id=self.protocol_id, |
| 249 |
type=self.notify_type, |
| 250 |
spi=b'', |
| 251 |
data=self.data |
| 252 |
) |
| 253 |
payload_no_spi.next_payload = Ikev2PayloadType.NONE |
| 254 |
|
| 255 |
composed_bytes = payload_no_spi.compose() |
| 256 |
parsed_payload: Ikev2PayloadNotifyUnparsed = Ikev2PayloadNotifyUnparsed.parse_exact_size(composed_bytes) |
| 257 |
|
| 258 |
self.assertEqual(parsed_payload.data, payload_no_spi.data) # pylint: disable=no-member |
| 259 |
self.assertEqual(parsed_payload.type, payload_no_spi.type) |
| 260 |
self.assertEqual(parsed_payload.spi, payload_no_spi.spi) |
| 261 |
|
| 262 |
def test_parse_with_spi(self): |
| 263 |
data_with_spi = b'\x00\x01\x02\x03\x04\x05\x06\x07' |
| 264 |
payload_with_spi = Ikev2PayloadNotifyUnparsed( |
| 265 |
flags=set(), |
| 266 |
protocol_id=self.protocol_id, |
| 267 |
type=self.notify_type, |
| 268 |
spi=self.spi, |
| 269 |
data=data_with_spi |
| 270 |
) |
| 271 |
payload_with_spi.next_payload = Ikev2PayloadType.NONE |
| 272 |
composed_bytes = payload_with_spi.compose() |
| 273 |
parsed_payload: Ikev2PayloadNotifyUnparsed = Ikev2PayloadNotifyUnparsed.parse_exact_size(composed_bytes) |
| 274 |
|
| 275 |
self.assertEqual(parsed_payload.protocol_id, self.protocol_id) |
| 276 |
self.assertEqual(parsed_payload.type, self.notify_type) |
| 277 |
self.assertEqual(parsed_payload.spi, self.spi) |
| 278 |
self.assertEqual(parsed_payload.data, data_with_spi) # pylint: disable=no-member |
| 279 |
self.assertEqual(parsed_payload.flags, set()) |
| 280 |
self.assertEqual(parsed_payload.next_payload, Ikev2PayloadType.NONE) |
| 281 |
|
| 282 |
|
| 283 |
class TestIkev2NotifyPayloadCookie(unittest.TestCase): |
| 284 |
_PROTOCOL_ID = Ikev2ProtocolId.IKE |
| 285 |
_COOKIE_DATA = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 286 |
|
| 287 |
def test_get_message_type(self): |
| 288 |
# pylint: disable=protected-access |
| 289 |
self.assertEqual(Ikev2NotifyPayloadCookie._get_message_type(), Ikev2NotifyType.COOKIE) |
| 290 |
|
| 291 |
def test_cookie_data_storage(self): |
| 292 |
payload = Ikev2NotifyPayloadCookie( |
| 293 |
flags=set(), |
| 294 |
protocol_id=self._PROTOCOL_ID, |
| 295 |
type=Ikev2NotifyType.COOKIE, |
| 296 |
spi=b'', |
| 297 |
cookie=self._COOKIE_DATA |
| 298 |
) |
| 299 |
self.assertEqual(payload.cookie, self._COOKIE_DATA) |
| 300 |
|
| 301 |
different_cookie = b'\xff\xfe\xfd\xfc\xfb\xfa' |
| 302 |
payload_2 = Ikev2NotifyPayloadCookie( |
| 303 |
flags=set(), |
| 304 |
protocol_id=self._PROTOCOL_ID, |
| 305 |
type=Ikev2NotifyType.COOKIE, |
| 306 |
spi=b'', |
| 307 |
cookie=different_cookie |
| 308 |
) |
| 309 |
self.assertEqual(payload_2.cookie, different_cookie) |
| 310 |
|
| 311 |
def test_round_trip_cookie_preservation(self): |
| 312 |
cookie_payload = Ikev2NotifyPayloadCookie( |
| 313 |
flags=set(), |
| 314 |
protocol_id=self._PROTOCOL_ID, |
| 315 |
type=Ikev2NotifyType.COOKIE, |
| 316 |
spi=b'', |
| 317 |
cookie=self._COOKIE_DATA |
| 318 |
) |
| 319 |
cookie_payload.next_payload = Ikev2PayloadType.NONE |
| 320 |
composed_bytes = cookie_payload.compose() |
| 321 |
parsed_payload: Ikev2NotifyPayloadCookie = Ikev2NotifyPayloadCookie.parse_exact_size(composed_bytes) |
| 322 |
|
| 323 |
self.assertEqual(parsed_payload.cookie, cookie_payload.cookie) # pylint: disable=no-member |
| 324 |
self.assertEqual(parsed_payload.type, cookie_payload.type) |
| 325 |
self.assertEqual(parsed_payload.spi, cookie_payload.spi) |
| 326 |
|
| 327 |
|
| 328 |
class TestIkev2NotifyPayloadSetWindowSize(unittest.TestCase): |
| 329 |
_PROTOCOL_ID = Ikev2ProtocolId.IKE |
| 330 |
_WINDOW_SIZE = 5 |
| 331 |
|
| 332 |
def test_get_message_type(self): |
| 333 |
# pylint: disable=protected-access |
| 334 |
self.assertEqual(Ikev2NotifyPayloadSetWindowSize._get_message_type(), Ikev2NotifyType.SET_WINDOW_SIZE) |
| 335 |
|
| 336 |
def test_window_size_storage(self): |
| 337 |
payload = Ikev2NotifyPayloadSetWindowSize( |
| 338 |
flags=set(), |
| 339 |
protocol_id=self._PROTOCOL_ID, |
| 340 |
type=Ikev2NotifyType.SET_WINDOW_SIZE, |
| 341 |
spi=b'', |
| 342 |
window_size=self._WINDOW_SIZE |
| 343 |
) |
| 344 |
self.assertEqual(payload.window_size, self._WINDOW_SIZE) |
| 345 |
|
| 346 |
different_window_size = 10 |
| 347 |
payload_2 = Ikev2NotifyPayloadSetWindowSize( |
| 348 |
flags=set(), |
| 349 |
protocol_id=self._PROTOCOL_ID, |
| 350 |
type=Ikev2NotifyType.SET_WINDOW_SIZE, |
| 351 |
spi=b'', |
| 352 |
window_size=different_window_size |
| 353 |
) |
| 354 |
self.assertEqual(payload_2.window_size, different_window_size) |
| 355 |
|
| 356 |
def test_round_trip_window_size_preservation(self): |
| 357 |
window_size_payload = Ikev2NotifyPayloadSetWindowSize( |
| 358 |
flags=set(), |
| 359 |
protocol_id=self._PROTOCOL_ID, |
| 360 |
type=Ikev2NotifyType.SET_WINDOW_SIZE, |
| 361 |
spi=b'', |
| 362 |
window_size=self._WINDOW_SIZE |
| 363 |
) |
| 364 |
window_size_payload.next_payload = Ikev2PayloadType.NONE |
| 365 |
composed_bytes = window_size_payload.compose() |
| 366 |
parsed_payload: Ikev2NotifyPayloadSetWindowSize = \ |
| 367 |
Ikev2NotifyPayloadSetWindowSize.parse_exact_size(composed_bytes) |
| 368 |
|
| 369 |
self.assertEqual(parsed_payload.window_size, window_size_payload.window_size) # pylint: disable=no-member |
| 370 |
self.assertEqual(parsed_payload.type, window_size_payload.type) |
| 371 |
self.assertEqual(parsed_payload.spi, window_size_payload.spi) |
| 372 |
|
| 373 |
def test_error_invalid_notification_data_length(self): |
| 374 |
wrong_length_bytes = bytes.fromhex( |
| 375 |
'00' # next_payload = NONE |
| 376 |
'00' # flags = 0 |
| 377 |
'000b' # payload_length = 11 (8 header + 3 data bytes) |
| 378 |
'01' # protocol_id = IKE |
| 379 |
'00' # spi_size = 0 |
| 380 |
'4001' # notify_type = SET_WINDOW_SIZE |
| 381 |
'aaaaaa' # 3 bytes data (must be exactly 4) |
| 382 |
) |
| 383 |
with self.assertRaises(InvalidValue): |
| 384 |
Ikev2NotifyPayloadSetWindowSize.parse_exact_size(wrong_length_bytes) |
| 385 |
|
| 386 |
|
| 387 |
class TestIkev2NotifyPayloadNatDetectionSourceIp(_ike_test_classes.Ikev2NotifyPayloadNatDetectionBaseTest): |
| 388 |
_NOTIFY_TYPE = Ikev2NotifyType.NAT_DETECTION_SOURCE_IP |
| 389 |
_PAYLOAD_CLASS = Ikev2NotifyPayloadNatDetectionSourceIp |
| 390 |
_NOTIFY_TYPE_BYTES = b'\x40\x04' |
| 391 |
|
| 392 |
|
| 393 |
class TestIkev2NotifyPayloadNatDetectionDestinationIp(_ike_test_classes.Ikev2NotifyPayloadNatDetectionBaseTest): |
| 394 |
_NOTIFY_TYPE = Ikev2NotifyType.NAT_DETECTION_DESTINATION_IP |
| 395 |
_PAYLOAD_CLASS = Ikev2NotifyPayloadNatDetectionDestinationIp |
| 396 |
_NOTIFY_TYPE_BYTES = b'\x40\x05' |
| 397 |
|
| 398 |
|
| 399 |
class TestIkev2NotifyPayloadUseTransportMode(unittest.TestCase): |
| 400 |
def test_get_message_type(self): |
| 401 |
# pylint: disable=protected-access |
| 402 |
self.assertEqual(Ikev2NotifyPayloadUseTransportMode._get_message_type(), Ikev2NotifyType.USE_TRANSPORT_MODE) |
| 403 |
|
| 404 |
def test_round_trip_preservation(self): |
| 405 |
transport_mode_payload = Ikev2NotifyPayloadUseTransportMode( |
| 406 |
flags=set(), |
| 407 |
protocol_id=Ikev2ProtocolId.IKE, |
| 408 |
type=Ikev2NotifyType.USE_TRANSPORT_MODE, |
| 409 |
spi=b'' |
| 410 |
) |
| 411 |
transport_mode_payload.next_payload = Ikev2PayloadType.NONE |
| 412 |
composed_bytes = transport_mode_payload.compose() |
| 413 |
parsed_payload: Ikev2NotifyPayloadUseTransportMode = \ |
| 414 |
Ikev2NotifyPayloadUseTransportMode.parse_exact_size(composed_bytes) |
| 415 |
|
| 416 |
self.assertEqual(parsed_payload.type, transport_mode_payload.type) |
| 417 |
self.assertEqual(parsed_payload.spi, transport_mode_payload.spi) |
| 418 |
self.assertEqual(parsed_payload.protocol_id, transport_mode_payload.protocol_id) |
| 419 |
self.assertEqual(parsed_payload.flags, transport_mode_payload.flags) |
| 420 |
self.assertEqual(parsed_payload.next_payload, transport_mode_payload.next_payload) |
| 421 |
|
| 422 |
|
| 423 |
class TestIkev2NotifyPayloadHttpCertLookupSupported(unittest.TestCase): |
| 424 |
def test_get_message_type(self): |
| 425 |
# pylint: disable=protected-access |
| 426 |
self.assertEqual( |
| 427 |
Ikev2NotifyPayloadHttpCertLookupSupported._get_message_type(), |
| 428 |
Ikev2NotifyType.HTTP_CERT_LOOKUP_SUPPORTED |
| 429 |
) |
| 430 |
|
| 431 |
def test_round_trip_preservation(self): |
| 432 |
http_cert_payload = Ikev2NotifyPayloadHttpCertLookupSupported( |
| 433 |
flags=set(), |
| 434 |
protocol_id=Ikev2ProtocolId.IKE, |
| 435 |
type=Ikev2NotifyType.HTTP_CERT_LOOKUP_SUPPORTED, |
| 436 |
spi=b'' |
| 437 |
) |
| 438 |
http_cert_payload.next_payload = Ikev2PayloadType.NONE |
| 439 |
composed_bytes = http_cert_payload.compose() |
| 440 |
parsed_payload: Ikev2NotifyPayloadHttpCertLookupSupported = \ |
| 441 |
Ikev2NotifyPayloadHttpCertLookupSupported.parse_exact_size(composed_bytes) |
| 442 |
|
| 443 |
self.assertEqual(parsed_payload.type, http_cert_payload.type) |
| 444 |
self.assertEqual(parsed_payload.spi, http_cert_payload.spi) |
| 445 |
self.assertEqual(parsed_payload.protocol_id, http_cert_payload.protocol_id) |
| 446 |
self.assertEqual(parsed_payload.flags, http_cert_payload.flags) |
| 447 |
self.assertEqual(parsed_payload.next_payload, http_cert_payload.next_payload) |
| 448 |
|
| 449 |
|
| 450 |
class TestIkev2NotifyPayloadIkev2FragmentationSupported(unittest.TestCase): |
| 451 |
def test_get_message_type(self): |
| 452 |
# pylint: disable=protected-access |
| 453 |
self.assertEqual( |
| 454 |
Ikev2NotifyPayloadIkev2FragmentationSupported._get_message_type(), |
| 455 |
Ikev2NotifyType.IKEV2_FRAGMENTATION_SUPPORTED, |
| 456 |
) |
| 457 |
|
| 458 |
def test_round_trip_preservation(self): |
| 459 |
fragmentation_payload = Ikev2NotifyPayloadIkev2FragmentationSupported( |
| 460 |
flags=set(), |
| 461 |
protocol_id=Ikev2ProtocolId.IKE, |
| 462 |
type=Ikev2NotifyType.IKEV2_FRAGMENTATION_SUPPORTED, |
| 463 |
spi=b'', |
| 464 |
) |
| 465 |
fragmentation_payload.next_payload = Ikev2PayloadType.NONE |
| 466 |
composed_bytes = fragmentation_payload.compose() |
| 467 |
parsed_payload = Ikev2NotifyPayloadIkev2FragmentationSupported.parse_exact_size(composed_bytes) |
| 468 |
self.assertEqual(parsed_payload.type, fragmentation_payload.type) |
| 469 |
|
| 470 |
|
| 471 |
class TestIkev2NotifyPayloadIntermediateExchangeSupported(unittest.TestCase): |
| 472 |
def test_get_message_type(self): |
| 473 |
# pylint: disable=protected-access |
| 474 |
self.assertEqual( |
| 475 |
Ikev2NotifyPayloadIntermediateExchangeSupported._get_message_type(), |
| 476 |
Ikev2NotifyType.INTERMEDIATE_EXCHANGE_SUPPORTED, |
| 477 |
) |
| 478 |
|
| 479 |
def test_round_trip_preservation(self): |
| 480 |
intermediate_exchange_payload = Ikev2NotifyPayloadIntermediateExchangeSupported( |
| 481 |
flags=set(), |
| 482 |
protocol_id=Ikev2ProtocolId.IKE, |
| 483 |
type=Ikev2NotifyType.INTERMEDIATE_EXCHANGE_SUPPORTED, |
| 484 |
spi=b'', |
| 485 |
) |
| 486 |
intermediate_exchange_payload.next_payload = Ikev2PayloadType.NONE |
| 487 |
composed_bytes = intermediate_exchange_payload.compose() |
| 488 |
parsed_payload = Ikev2NotifyPayloadIntermediateExchangeSupported.parse_exact_size(composed_bytes) |
| 489 |
self.assertEqual(parsed_payload.type, intermediate_exchange_payload.type) |
| 490 |
|
| 491 |
|
| 492 |
class TestIkev2NotifyPayloadUsePpk(unittest.TestCase): |
| 493 |
def test_get_message_type(self): |
| 494 |
# pylint: disable=protected-access |
| 495 |
self.assertEqual(Ikev2NotifyPayloadUsePpk._get_message_type(), Ikev2NotifyType.USE_PPK) |
| 496 |
|
| 497 |
def test_round_trip_preservation(self): |
| 498 |
use_ppk_payload = Ikev2NotifyPayloadUsePpk( |
| 499 |
flags=set(), |
| 500 |
protocol_id=Ikev2ProtocolId.IKE, |
| 501 |
type=Ikev2NotifyType.USE_PPK, |
| 502 |
spi=b'', |
| 503 |
) |
| 504 |
use_ppk_payload.next_payload = Ikev2PayloadType.NONE |
| 505 |
composed_bytes = use_ppk_payload.compose() |
| 506 |
parsed_payload = Ikev2NotifyPayloadUsePpk.parse_exact_size(composed_bytes) |
| 507 |
self.assertEqual(parsed_payload.type, use_ppk_payload.type) |
| 508 |
|
| 509 |
|
| 510 |
class TestIkev2NotifyPayloadRedirectSupported(unittest.TestCase): |
| 511 |
def test_get_message_type(self): |
| 512 |
# pylint: disable=protected-access |
| 513 |
self.assertEqual( |
| 514 |
Ikev2NotifyPayloadRedirectSupported._get_message_type(), |
| 515 |
Ikev2NotifyType.REDIRECT_SUPPORTED, |
| 516 |
) |
| 517 |
|
| 518 |
def test_round_trip_preservation(self): |
| 519 |
redirect_supported_payload = Ikev2NotifyPayloadRedirectSupported( |
| 520 |
flags=set(), |
| 521 |
protocol_id=Ikev2ProtocolId.IKE, |
| 522 |
type=Ikev2NotifyType.REDIRECT_SUPPORTED, |
| 523 |
spi=b'', |
| 524 |
) |
| 525 |
redirect_supported_payload.next_payload = Ikev2PayloadType.NONE |
| 526 |
composed_bytes = redirect_supported_payload.compose() |
| 527 |
parsed_payload = Ikev2NotifyPayloadRedirectSupported.parse_exact_size(composed_bytes) |
| 528 |
self.assertEqual(parsed_payload.type, redirect_supported_payload.type) |
| 529 |
|
| 530 |
|
| 531 |
class TestIkev2NotifyPayloadChildlessIkev2Supported(unittest.TestCase): |
| 532 |
def test_get_message_type(self): |
| 533 |
# pylint: disable=protected-access |
| 534 |
self.assertEqual( |
| 535 |
Ikev2NotifyPayloadChildlessIkev2Supported._get_message_type(), |
| 536 |
Ikev2NotifyType.CHILDLESS_IKEV2_SUPPORTED, |
| 537 |
) |
| 538 |
|
| 539 |
def test_round_trip_preservation(self): |
| 540 |
childless_ikev2_payload = Ikev2NotifyPayloadChildlessIkev2Supported( |
| 541 |
flags=set(), |
| 542 |
protocol_id=Ikev2ProtocolId.IKE, |
| 543 |
type=Ikev2NotifyType.CHILDLESS_IKEV2_SUPPORTED, |
| 544 |
spi=b'', |
| 545 |
) |
| 546 |
childless_ikev2_payload.next_payload = Ikev2PayloadType.NONE |
| 547 |
composed_bytes = childless_ikev2_payload.compose() |
| 548 |
parsed_payload = Ikev2NotifyPayloadChildlessIkev2Supported.parse_exact_size(composed_bytes) |
| 549 |
self.assertEqual(parsed_payload.type, childless_ikev2_payload.type) |
| 550 |
|
| 551 |
|
| 552 |
class TestIkev2NotifyPayloadSignatureHashAlgorithms(unittest.TestCase): |
| 553 |
# Five 16-bit hash algorithm identifiers per RFC 7427 (IANA registry |
| 554 |
# values: 1=SHA1, 2=SHA2-256, 3=SHA2-384, 4=SHA2-512, 5=IDENTITY) |
| 555 |
_HASH_ALGORITHMS = ( |
| 556 |
Ikev2HashAlgorithm.SHA1, |
| 557 |
Ikev2HashAlgorithm.SHA2_256, |
| 558 |
Ikev2HashAlgorithm.SHA2_384, |
| 559 |
Ikev2HashAlgorithm.SHA2_512, |
| 560 |
Ikev2HashAlgorithm.IDENTITY, |
| 561 |
) |
| 562 |
_PAYLOAD_BYTES = bytes.fromhex( |
| 563 |
'00' # next_payload = NONE |
| 564 |
'00' # flags = 0 |
| 565 |
'0012' # payload_length = 18 (8 header + 10 data) |
| 566 |
'01' # protocol_id = IKE |
| 567 |
'00' # spi_size = 0 |
| 568 |
'402f' # notify_type = SIGNATURE_HASH_ALGORITHMS (16431) |
| 569 |
'00010002000300040005' # five 16-bit hash algorithm IDs |
| 570 |
) |
| 571 |
|
| 572 |
def setUp(self): |
| 573 |
self.payload = Ikev2NotifyPayloadSignatureHashAlgorithms( |
| 574 |
flags=set(), |
| 575 |
protocol_id=Ikev2ProtocolId.IKE, |
| 576 |
type=Ikev2NotifyType.SIGNATURE_HASH_ALGORITHMS, |
| 577 |
spi=b'', |
| 578 |
hash_algorithms=self._HASH_ALGORITHMS, |
| 579 |
) |
| 580 |
self.payload.next_payload = Ikev2PayloadType.NONE |
| 581 |
|
| 582 |
def test_get_message_type(self): |
| 583 |
# pylint: disable=protected-access |
| 584 |
self.assertEqual( |
| 585 |
Ikev2NotifyPayloadSignatureHashAlgorithms._get_message_type(), |
| 586 |
Ikev2NotifyType.SIGNATURE_HASH_ALGORITHMS, |
| 587 |
) |
| 588 |
|
| 589 |
def test_parse(self): |
| 590 |
parsed = Ikev2NotifyPayloadSignatureHashAlgorithms.parse_exact_size(self._PAYLOAD_BYTES) |
| 591 |
self.assertEqual(parsed.type, Ikev2NotifyType.SIGNATURE_HASH_ALGORITHMS) |
| 592 |
self.assertEqual(parsed.hash_algorithms, self._HASH_ALGORITHMS) # pylint: disable=no-member |
| 593 |
|
| 594 |
def test_compose(self): |
| 595 |
self.assertEqual(self.payload.compose(), self._PAYLOAD_BYTES) |
| 596 |
|
| 597 |
def test_round_trip(self): |
| 598 |
composed = self.payload.compose() |
| 599 |
parsed = Ikev2NotifyPayloadSignatureHashAlgorithms.parse_exact_size(composed) |
| 600 |
self.assertEqual(parsed.hash_algorithms, self.payload.hash_algorithms) # pylint: disable=no-member |
| 601 |
|
| 602 |
def test_error_invalid_notification_data_length(self): |
| 603 |
odd_length_bytes = bytes.fromhex( |
| 604 |
'00' # next_payload = NONE |
| 605 |
'00' # flags = 0 |
| 606 |
'0009' # payload_length = 9 (8 header + 1 data byte) |
| 607 |
'01' # protocol_id = IKE |
| 608 |
'00' # spi_size = 0 |
| 609 |
'402f' # notify_type = SIGNATURE_HASH_ALGORITHMS |
| 610 |
'aa' # 1 byte data (must be even number of bytes) |
| 611 |
) |
| 612 |
with self.assertRaises(InvalidValue): |
| 613 |
Ikev2NotifyPayloadSignatureHashAlgorithms.parse_exact_size(odd_length_bytes) |
| 614 |
|
| 615 |
|
| 616 |
class TestIkev2NotifyPayloadVariantResponder(unittest.TestCase): |
| 617 |
_PROTOCOL_ID = Ikev2ProtocolId.IKE |
| 618 |
|
| 619 |
def test_parse_other_notify_type(self): |
| 620 |
other_notify = Ikev2PayloadNotifyUnparsed( |
| 621 |
flags=set(), |
| 622 |
protocol_id=self._PROTOCOL_ID, |
| 623 |
type=Ikev2NotifyType.AUTHENTICATION_FAILED, |
| 624 |
spi=b'', |
| 625 |
data=b'\x00\x01\x02\x03' |
| 626 |
) |
| 627 |
other_notify.next_payload = Ikev2PayloadType.NONE |
| 628 |
composed_bytes = other_notify.compose() |
| 629 |
|
| 630 |
parsed_payload = Ikev2NotifyPayloadVariantResponder.parse_exact_size(composed_bytes) |
| 631 |
self.assertIsInstance(parsed_payload, Ikev2PayloadNotifyUnparsed) |
| 632 |
self.assertEqual(parsed_payload.type, Ikev2NotifyType.AUTHENTICATION_FAILED) |
| 633 |
self.assertEqual(parsed_payload.data, b'\x00\x01\x02\x03') # pylint: disable=no-member |
| 634 |
|
| 635 |
def test_compose(self): |
| 636 |
cookie_data = b'\x00\x01\x02\x03' |
| 637 |
cookie_payload = Ikev2NotifyPayloadCookie( |
| 638 |
flags=set(), |
| 639 |
protocol_id=self._PROTOCOL_ID, |
| 640 |
type=Ikev2NotifyType.COOKIE, |
| 641 |
spi=b'', |
| 642 |
cookie=cookie_data |
| 643 |
) |
| 644 |
cookie_payload.next_payload = Ikev2PayloadType.NONE |
| 645 |
|
| 646 |
variant_parsable = Ikev2NotifyPayloadVariantResponder(variant=cookie_payload) |
| 647 |
composed_bytes = variant_parsable.compose() |
| 648 |
|
| 649 |
self.assertEqual(composed_bytes, cookie_payload.compose()) |
| 650 |
|
| 651 |
def test_round_trip(self): |
| 652 |
cookie_data = b'\x00\x01\x02\x03\x04\x05' |
| 653 |
cookie_payload = Ikev2NotifyPayloadCookie( |
| 654 |
flags=set(), |
| 655 |
protocol_id=self._PROTOCOL_ID, |
| 656 |
type=Ikev2NotifyType.COOKIE, |
| 657 |
spi=b'', |
| 658 |
cookie=cookie_data |
| 659 |
) |
| 660 |
cookie_payload.next_payload = Ikev2PayloadType.NONE |
| 661 |
|
| 662 |
variant_parsable = Ikev2NotifyPayloadVariantResponder(variant=cookie_payload) |
| 663 |
composed_bytes = variant_parsable.compose() |
| 664 |
parsed_payload = Ikev2NotifyPayloadVariantResponder.parse_exact_size(composed_bytes) |
| 665 |
|
| 666 |
self.assertIsInstance(parsed_payload, Ikev2NotifyPayloadCookie) |
| 667 |
self.assertEqual(parsed_payload.cookie, cookie_data) # pylint: disable=no-member |
| 668 |
self.assertEqual(parsed_payload.type, cookie_payload.type) |
| 669 |
self.assertEqual(parsed_payload.spi, cookie_payload.spi) |
| 670 |
|
| 671 |
|
| 672 |
if __name__ == '__main__': |
| 673 |
unittest.main() |