| 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 ( |
| 8 |
Ikev2DiffieHellmanGroup, |
| 9 |
Ikev2EncryptionAlgorithm, |
| 10 |
Ikev2IntegrityAlgorithm, |
| 11 |
Ikev2ProtocolId, |
| 12 |
Ikev2PseudorandomFunction, |
| 13 |
Ikev2TransformType, |
| 14 |
) |
| 15 |
|
| 16 |
from cryptoparser.common.exception import NotEnoughData |
| 17 |
from cryptoparser.ike.ikev2 import ( |
| 18 |
Ikev2Proposal, |
| 19 |
Ikev2ProposalNextPayload, |
| 20 |
Ikev2PayloadSecurityAssociation, |
| 21 |
Ikev2PayloadFlags, |
| 22 |
Ikev2PayloadType, |
| 23 |
Ikev2TransformPrf, |
| 24 |
Ikev2TransformDhGroup, |
| 25 |
Ikev2TransformEncryptionAlgorithm, |
| 26 |
Ikev2TransformIntegrity, |
| 27 |
TransformAttributeSignatureAlgorithm, |
| 28 |
TransformNextPayload, |
| 29 |
) |
| 30 |
|
| 31 |
from .classes import TransformTest |
| 32 |
|
| 33 |
|
| 34 |
class TestTransformAttributeSignatureAlgorithm(unittest.TestCase): |
| 35 |
def setUp(self): |
| 36 |
self.signature_algorithm = ( |
| 37 |
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 38 |
b'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' |
| 39 |
) |
| 40 |
|
| 41 |
self.signature_algorithm_payload = TransformAttributeSignatureAlgorithm( |
| 42 |
signature_algorithm=self.signature_algorithm |
| 43 |
) |
| 44 |
|
| 45 |
self.signature_algorithm_dict = collections.OrderedDict([ |
| 46 |
('format', b'\x00'), # TYPE_LENGTH_VALUE |
| 47 |
('type', b'\x12'), # SIGNATURE_ALGORITHM = 18 |
| 48 |
('length', b'\x00\x20'), # 32 bytes length |
| 49 |
('signature_algorithm', self.signature_algorithm), |
| 50 |
]) |
| 51 |
self.signature_algorithm_bytes = b''.join(self.signature_algorithm_dict.values()) |
| 52 |
|
| 53 |
def test_parse(self): |
| 54 |
parsed_signature_algorithm = TransformAttributeSignatureAlgorithm.parse_exact_size( |
| 55 |
self.signature_algorithm_bytes |
| 56 |
) |
| 57 |
self.assertEqual(parsed_signature_algorithm.signature_algorithm, self.signature_algorithm) |
| 58 |
|
| 59 |
def test_compose(self): |
| 60 |
composed_bytes = self.signature_algorithm_payload.compose() |
| 61 |
|
| 62 |
parsed_signature_algorithm = TransformAttributeSignatureAlgorithm.parse_exact_size(composed_bytes) |
| 63 |
self.assertEqual( |
| 64 |
parsed_signature_algorithm.signature_algorithm, |
| 65 |
self.signature_algorithm_payload.signature_algorithm |
| 66 |
) |
| 67 |
|
| 68 |
def test_round_trip(self): |
| 69 |
composed_bytes = self.signature_algorithm_payload.compose() |
| 70 |
parsed_payload: TransformAttributeSignatureAlgorithm = ( |
| 71 |
TransformAttributeSignatureAlgorithm.parse_exact_size(composed_bytes) |
| 72 |
) |
| 73 |
|
| 74 |
self.assertEqual(parsed_payload.signature_algorithm, self.signature_algorithm_payload.signature_algorithm) |
| 75 |
|
| 76 |
def test_error_parse_not_enough_data(self): |
| 77 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 78 |
TransformAttributeSignatureAlgorithm.parse_exact_size(b'\x00\x12') |
| 79 |
self.assertEqual(context_manager.exception.bytes_needed, 2) |
| 80 |
|
| 81 |
incomplete = self.signature_algorithm_bytes[:-1] |
| 82 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 83 |
TransformAttributeSignatureAlgorithm.parse_exact_size(incomplete) |
| 84 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 85 |
|
| 86 |
def test_error_invalid_type(self): |
| 87 |
wrong_type_header = b'\x00\x11\x00\x20' # wrong type 0x11 instead of 0x12 |
| 88 |
wrong_type_bytes = wrong_type_header + self.signature_algorithm |
| 89 |
with self.assertRaises(InvalidValue): |
| 90 |
TransformAttributeSignatureAlgorithm.parse_exact_size(wrong_type_bytes) |
| 91 |
|
| 92 |
def test_error_invalid_signature_algorithm_type(self): |
| 93 |
with self.assertRaises(TypeError): |
| 94 |
TransformAttributeSignatureAlgorithm( |
| 95 |
signature_algorithm="not_bytes" |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
class TestTransform(unittest.TestCase): |
| 100 |
def setUp(self): |
| 101 |
self.transform_id = Ikev2PseudorandomFunction.PRF_HMAC_SHA1 |
| 102 |
|
| 103 |
self.transform = TransformTest( |
| 104 |
transform_id=self.transform_id |
| 105 |
) |
| 106 |
self.transform.next_payload = TransformNextPayload.LAST |
| 107 |
|
| 108 |
self.transform_dict = collections.OrderedDict([ |
| 109 |
('next_payload', b'\x00'), # LAST |
| 110 |
('reserved1', b'\x00'), |
| 111 |
('transform_length', b'\x00\x08'), # 8 bytes header only |
| 112 |
('transform_type', b'\x02'), # PRF |
| 113 |
('reserved2', b'\x00'), |
| 114 |
('transform_id', b'\x00\x02'), # PRF_HMAC_SHA1 |
| 115 |
]) |
| 116 |
self.transform_bytes = b''.join(self.transform_dict.values()) |
| 117 |
|
| 118 |
def test_parse(self): |
| 119 |
parsed_transform = TransformTest.parse_exact_size(self.transform_bytes) |
| 120 |
self.assertEqual(parsed_transform.transform_id, self.transform_id) |
| 121 |
self.assertEqual(parsed_transform.next_payload, TransformNextPayload.LAST) |
| 122 |
|
| 123 |
def test_compose(self): |
| 124 |
composed_bytes = self.transform.compose() |
| 125 |
self.assertEqual(len(composed_bytes), 8) # Header only, no attributes |
| 126 |
|
| 127 |
parsed_transform = TransformTest.parse_exact_size(composed_bytes) |
| 128 |
self.assertEqual(parsed_transform.transform_id, self.transform.transform_id) |
| 129 |
|
| 130 |
def test_round_trip(self): |
| 131 |
composed_bytes = self.transform.compose() |
| 132 |
parsed_payload: TransformTest = TransformTest.parse_exact_size(composed_bytes) |
| 133 |
|
| 134 |
self.assertEqual(parsed_payload.transform_id, self.transform.transform_id) |
| 135 |
self.assertEqual(parsed_payload.next_payload, self.transform.next_payload) |
| 136 |
|
| 137 |
def test_get_transform_type(self): |
| 138 |
self.assertEqual(TransformTest.get_transform_type(), Ikev2TransformType.PRF) |
| 139 |
|
| 140 |
def test_get_transform_id_class(self): |
| 141 |
self.assertEqual( |
| 142 |
TransformTest._get_transform_id_class(), # pylint: disable=protected-access |
| 143 |
Ikev2PseudorandomFunction |
| 144 |
) |
| 145 |
|
| 146 |
def test_error_parse_not_enough_data(self): |
| 147 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 148 |
TransformTest.parse_exact_size(b'\x00\x01\x02') |
| 149 |
self.assertEqual(context_manager.exception.bytes_needed, 5) |
| 150 |
|
| 151 |
incomplete = self.transform_bytes[:-1] |
| 152 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 153 |
TransformTest.parse_exact_size(incomplete) |
| 154 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 155 |
|
| 156 |
def test_error_invalid_transform_id(self): |
| 157 |
with self.assertRaises(InvalidValue): |
| 158 |
TransformTest( |
| 159 |
transform_id="invalid_id" |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
class TestIkev2TransformPrf(unittest.TestCase): |
| 164 |
def test_get_transform_type(self): |
| 165 |
self.assertEqual(Ikev2TransformPrf.get_transform_type(), Ikev2TransformType.PRF) |
| 166 |
|
| 167 |
def test_get_transform_id_class(self): |
| 168 |
self.assertEqual( |
| 169 |
Ikev2TransformPrf._get_transform_id_class(), # pylint: disable=protected-access |
| 170 |
Ikev2PseudorandomFunction |
| 171 |
) |
| 172 |
|
| 173 |
|
| 174 |
class TestIkev2TransformDhGroup(unittest.TestCase): |
| 175 |
def test_get_transform_type(self): |
| 176 |
self.assertEqual(Ikev2TransformDhGroup.get_transform_type(), Ikev2TransformType.DH) |
| 177 |
|
| 178 |
def test_get_transform_id_class(self): |
| 179 |
self.assertEqual( |
| 180 |
Ikev2TransformDhGroup._get_transform_id_class(), # pylint: disable=protected-access |
| 181 |
Ikev2DiffieHellmanGroup |
| 182 |
) |
| 183 |
|
| 184 |
|
| 185 |
class TestIkev2TransformIntegrity(unittest.TestCase): |
| 186 |
def test_get_transform_type(self): |
| 187 |
self.assertEqual(Ikev2TransformIntegrity.get_transform_type(), Ikev2TransformType.INTEG) |
| 188 |
|
| 189 |
def test_get_transform_id_class(self): |
| 190 |
self.assertEqual( |
| 191 |
Ikev2TransformIntegrity._get_transform_id_class(), # pylint: disable=protected-access |
| 192 |
Ikev2IntegrityAlgorithm |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
class TestIkev2TransformEncryptionAlgorithm(unittest.TestCase): |
| 197 |
def setUp(self): |
| 198 |
self.encryption_algorithm = Ikev2EncryptionAlgorithm.ENCR_AES_CBC |
| 199 |
self.key_length = 128 |
| 200 |
self.transform = Ikev2TransformEncryptionAlgorithm( |
| 201 |
transform_id=self.encryption_algorithm, |
| 202 |
key_length=self.key_length |
| 203 |
) |
| 204 |
self.transform.next_payload = TransformNextPayload.LAST |
| 205 |
|
| 206 |
def test_get_transform_type(self): |
| 207 |
self.assertEqual(Ikev2TransformEncryptionAlgorithm.get_transform_type(), Ikev2TransformType.ENCR) |
| 208 |
|
| 209 |
def test_get_transform_id_class(self): |
| 210 |
self.assertEqual( |
| 211 |
Ikev2TransformEncryptionAlgorithm._get_transform_id_class(), # pylint: disable=protected-access |
| 212 |
Ikev2EncryptionAlgorithm |
| 213 |
) |
| 214 |
|
| 215 |
def test_key_length_value_support(self): |
| 216 |
different_key_lengths = [128, 192, 256] |
| 217 |
|
| 218 |
for key_length in different_key_lengths: |
| 219 |
transform = Ikev2TransformEncryptionAlgorithm( |
| 220 |
transform_id=self.encryption_algorithm, |
| 221 |
key_length=key_length |
| 222 |
) |
| 223 |
self.assertEqual(transform.key_length, key_length) |
| 224 |
|
| 225 |
def test_parse(self): |
| 226 |
composed_bytes = self.transform.compose() |
| 227 |
parsed_transform = Ikev2TransformEncryptionAlgorithm.parse_exact_size(composed_bytes) |
| 228 |
|
| 229 |
self.assertEqual(parsed_transform.transform_id, self.transform.transform_id) |
| 230 |
self.assertEqual(parsed_transform.key_length, self.transform.key_length) # pylint: disable=no-member |
| 231 |
|
| 232 |
def test_compose(self): |
| 233 |
composed_bytes = self.transform.compose() |
| 234 |
self.assertGreater(len(composed_bytes), 8) |
| 235 |
|
| 236 |
def test_round_trip(self): |
| 237 |
composed_bytes = self.transform.compose() |
| 238 |
parsed_transform = Ikev2TransformEncryptionAlgorithm.parse_exact_size(composed_bytes) |
| 239 |
|
| 240 |
self.assertEqual(parsed_transform.transform_id, self.transform.transform_id) |
| 241 |
self.assertEqual(parsed_transform.key_length, self.transform.key_length) # pylint: disable=no-member |
| 242 |
|
| 243 |
def test_error_parse_not_enough_data(self): |
| 244 |
incomplete_data = b'\x00\x00\x00\x08\x01\x00' |
| 245 |
|
| 246 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 247 |
Ikev2TransformEncryptionAlgorithm.parse_exact_size(incomplete_data) |
| 248 |
self.assertGreater(context_manager.exception.bytes_needed, 0) |
| 249 |
|
| 250 |
def test_round_trip_without_key_length(self): |
| 251 |
transform_without_key = Ikev2TransformEncryptionAlgorithm( |
| 252 |
transform_id=Ikev2EncryptionAlgorithm.ENCR_DES, |
| 253 |
key_length=None, |
| 254 |
) |
| 255 |
transform_without_key.next_payload = TransformNextPayload.LAST |
| 256 |
composed_bytes = transform_without_key.compose() |
| 257 |
parsed_transform = Ikev2TransformEncryptionAlgorithm.parse_exact_size(composed_bytes) |
| 258 |
|
| 259 |
self.assertEqual(parsed_transform.transform_id, Ikev2EncryptionAlgorithm.ENCR_DES) |
| 260 |
self.assertIsNone(parsed_transform.key_length) # pylint: disable=no-member |
| 261 |
|
| 262 |
|
| 263 |
class TestIkev2Proposal(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 264 |
def setUp(self): |
| 265 |
self.transform_prf = Ikev2TransformPrf( |
| 266 |
transform_id=Ikev2PseudorandomFunction.PRF_HMAC_SHA1 |
| 267 |
) |
| 268 |
self.transform_prf.next_payload = TransformNextPayload.LAST |
| 269 |
|
| 270 |
self.protocol_id = Ikev2ProtocolId.IKE |
| 271 |
self.spi = b'\x00\x01\x02\x03' |
| 272 |
|
| 273 |
self.proposal_minimal = Ikev2Proposal( |
| 274 |
protocol_id=self.protocol_id, |
| 275 |
transforms=[self.transform_prf], |
| 276 |
spi=b'' |
| 277 |
) |
| 278 |
self.proposal_minimal.last = Ikev2ProposalNextPayload.LAST |
| 279 |
self.proposal_minimal.proposal_number = 1 |
| 280 |
|
| 281 |
self.proposal_with_spi = Ikev2Proposal( |
| 282 |
protocol_id=self.protocol_id, |
| 283 |
transforms=[self.transform_prf], |
| 284 |
spi=self.spi |
| 285 |
) |
| 286 |
self.proposal_with_spi.last = Ikev2ProposalNextPayload.MORE |
| 287 |
self.proposal_with_spi.proposal_number = 2 |
| 288 |
|
| 289 |
self.proposal_dict_minimal = collections.OrderedDict([ |
| 290 |
('last', b'\x00'), # LAST |
| 291 |
('reserved', b'\x00'), |
| 292 |
('proposal_length', b'\x00\x10'), # 16 bytes total (8 header + 8 transform) |
| 293 |
('proposal_number', b'\x01'), # 1 |
| 294 |
('protocol_id', b'\x01'), # IKE |
| 295 |
('spi_size', b'\x00'), # 0 bytes |
| 296 |
('transform_count', b'\x01'), # 1 transform |
| 297 |
# No SPI data |
| 298 |
# Transform data: next_payload(1) + reserved(1) + length(2) + type(1) + reserved(1) + id(2) |
| 299 |
('transform_data', b'\x00\x00\x00\x08\x02\x00\x00\x02'), # PRF_HMAC_SHA1 |
| 300 |
]) |
| 301 |
self.proposal_bytes_minimal = b''.join(self.proposal_dict_minimal.values()) |
| 302 |
self.proposal_minimal = Ikev2Proposal( |
| 303 |
protocol_id=self.protocol_id, |
| 304 |
transforms=[self.transform_prf], |
| 305 |
spi=b'' |
| 306 |
) |
| 307 |
self.proposal_minimal.last = Ikev2ProposalNextPayload.LAST |
| 308 |
self.proposal_minimal.proposal_number = 1 |
| 309 |
|
| 310 |
self.proposal_dict_with_spi = collections.OrderedDict([ |
| 311 |
('last', b'\x02'), # MORE enum value |
| 312 |
('reserved', b'\x00'), |
| 313 |
('proposal_length', b'\x00\x10'), # 16 bytes total (8 header + 8 transform) |
| 314 |
('proposal_number', b'\x02'), # 2 |
| 315 |
('protocol_id', b'\x01'), # IKE |
| 316 |
('spi_size', b'\x04'), # 4 bytes |
| 317 |
('transform_count', b'\x01'), # 1 transform |
| 318 |
('spi', self.spi), # SPI data |
| 319 |
('transform_data', b'\x00\x00\x00\x08\x02\x00\x00\x02'), # PRF_HMAC_SHA1 |
| 320 |
]) |
| 321 |
self.proposal_bytes_with_spi = b''.join(self.proposal_dict_with_spi.values()) |
| 322 |
self.proposal_with_spi = Ikev2Proposal( |
| 323 |
protocol_id=self.protocol_id, |
| 324 |
transforms=[self.transform_prf], |
| 325 |
spi=self.spi |
| 326 |
) |
| 327 |
self.proposal_with_spi.last = Ikev2ProposalNextPayload.MORE |
| 328 |
self.proposal_with_spi.proposal_number = 2 |
| 329 |
|
| 330 |
def test_parse(self): |
| 331 |
parsed_proposal = Ikev2Proposal.parse_exact_size(self.proposal_bytes_minimal) |
| 332 |
self.assertEqual(parsed_proposal.protocol_id, self.protocol_id) |
| 333 |
self.assertEqual(parsed_proposal.spi, b'') |
| 334 |
self.assertEqual(len(parsed_proposal.transforms), 1) |
| 335 |
self.assertEqual(parsed_proposal.last, set()) |
| 336 |
self.assertEqual(parsed_proposal.proposal_number, 1) |
| 337 |
|
| 338 |
parsed_proposal_spi = Ikev2Proposal.parse_exact_size(self.proposal_bytes_with_spi) |
| 339 |
self.assertEqual(parsed_proposal_spi.protocol_id, self.protocol_id) |
| 340 |
self.assertEqual(parsed_proposal_spi.spi, self.spi) |
| 341 |
self.assertEqual(len(parsed_proposal_spi.transforms), 1) |
| 342 |
self.assertEqual(parsed_proposal_spi.last, {Ikev2ProposalNextPayload.MORE}) |
| 343 |
self.assertEqual(parsed_proposal_spi.proposal_number, 2) |
| 344 |
|
| 345 |
def test_compose(self): |
| 346 |
composed_bytes = self.proposal_minimal.compose() |
| 347 |
self.assertEqual(composed_bytes, self.proposal_bytes_minimal) |
| 348 |
|
| 349 |
composed_bytes = self.proposal_with_spi.compose() |
| 350 |
self.assertEqual(composed_bytes, self.proposal_bytes_with_spi) |
| 351 |
|
| 352 |
def test_round_trip(self): |
| 353 |
composed_bytes = self.proposal_minimal.compose() |
| 354 |
parsed_proposal: Ikev2Proposal = Ikev2Proposal.parse_exact_size(composed_bytes) |
| 355 |
|
| 356 |
self.assertEqual(parsed_proposal.protocol_id, self.proposal_minimal.protocol_id) |
| 357 |
self.assertEqual(parsed_proposal.spi, self.proposal_minimal.spi) |
| 358 |
self.assertEqual(len(parsed_proposal.transforms), len(self.proposal_minimal.transforms)) |
| 359 |
self.assertEqual(parsed_proposal.last, set()) |
| 360 |
self.assertEqual(parsed_proposal.proposal_number, self.proposal_minimal.proposal_number) |
| 361 |
|
| 362 |
def test_error_parse_not_enough_data(self): |
| 363 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 364 |
Ikev2Proposal.parse_exact_size(b'\x00\x01\x02') |
| 365 |
self.assertEqual(context_manager.exception.bytes_needed, Ikev2Proposal.HEADER_SIZE - 3) |
| 366 |
|
| 367 |
incomplete = self.proposal_bytes_minimal[:-1] |
| 368 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 369 |
Ikev2Proposal.parse_exact_size(incomplete) |
| 370 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 371 |
|
| 372 |
def test_error_invalid_protocol_id(self): |
| 373 |
with self.assertRaises(TypeError): |
| 374 |
Ikev2Proposal( |
| 375 |
protocol_id="invalid_protocol", |
| 376 |
transforms=[self.transform_prf] |
| 377 |
) |
| 378 |
|
| 379 |
def test_error_invalid_transforms(self): |
| 380 |
with self.assertRaises(TypeError): |
| 381 |
Ikev2Proposal( |
| 382 |
protocol_id=self.protocol_id, |
| 383 |
transforms=["invalid_transform"] |
| 384 |
) |
| 385 |
|
| 386 |
def test_error_invalid_spi_type(self): |
| 387 |
with self.assertRaises(TypeError): |
| 388 |
Ikev2Proposal( |
| 389 |
protocol_id=self.protocol_id, |
| 390 |
transforms=[self.transform_prf], |
| 391 |
spi="invalid_spi" |
| 392 |
) |
| 393 |
|
| 394 |
|
| 395 |
class TestIkev2PayloadSecurityAssociation(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 396 |
def setUp(self): |
| 397 |
self.transform_prf = Ikev2TransformPrf( |
| 398 |
transform_id=Ikev2PseudorandomFunction.PRF_HMAC_SHA1 |
| 399 |
) |
| 400 |
self.transform_prf.next_payload = TransformNextPayload.LAST |
| 401 |
|
| 402 |
self.proposal_single = Ikev2Proposal( |
| 403 |
protocol_id=Ikev2ProtocolId.IKE, |
| 404 |
transforms=[self.transform_prf], |
| 405 |
spi=b'' |
| 406 |
) |
| 407 |
|
| 408 |
self.proposal_with_spi = Ikev2Proposal( |
| 409 |
protocol_id=Ikev2ProtocolId.IKE, |
| 410 |
transforms=[self.transform_prf], |
| 411 |
spi=b'\x00\x01\x02\x03' |
| 412 |
) |
| 413 |
|
| 414 |
self.sa_single_proposal = Ikev2PayloadSecurityAssociation( |
| 415 |
flags=set(), |
| 416 |
proposals=[self.proposal_single] |
| 417 |
) |
| 418 |
self.sa_single_proposal.next_payload = Ikev2PayloadType.NONE |
| 419 |
|
| 420 |
self.sa_multiple_proposals = Ikev2PayloadSecurityAssociation( |
| 421 |
flags={Ikev2PayloadFlags.CRITICAL}, |
| 422 |
proposals=[self.proposal_single, self.proposal_with_spi] |
| 423 |
) |
| 424 |
self.sa_multiple_proposals.next_payload = Ikev2PayloadType.KE |
| 425 |
|
| 426 |
# Single proposal: header(4) + proposal(16) = 20 bytes total |
| 427 |
self.sa_dict_single = collections.OrderedDict([ |
| 428 |
('next_payload', b'\x00'), # NONE |
| 429 |
('flags', b'\x00'), # No flags |
| 430 |
('payload_length', b'\x00\x14'), # 20 bytes total |
| 431 |
('proposals_data', b'\x00\x00\x00\x10\x01\x01\x00\x01\x00\x00\x00\x08\x02\x00\x00\x02'), |
| 432 |
]) |
| 433 |
self.sa_bytes_single = b''.join(self.sa_dict_single.values()) |
| 434 |
|
| 435 |
# Multiple proposals: header(4) + proposal1(16) + proposal2(20) = 40 bytes total |
| 436 |
self.sa_dict_multiple = collections.OrderedDict([ |
| 437 |
('next_payload', b'\x22'), # KE |
| 438 |
('flags', b'\x80'), # CRITICAL |
| 439 |
('payload_length', b'\x00\x28'), # 40 bytes total |
| 440 |
('proposals_data', ( |
| 441 |
b'\x02\x00\x00\x10\x01\x01\x00\x01\x00\x00\x00\x08\x02\x00\x00\x02' + # proposal 1 |
| 442 |
b'\x00\x00\x00\x10\x02\x01\x04\x01\x00\x01\x02\x03\x00\x00\x00\x08\x02\x00\x00\x02' # proposal 2 |
| 443 |
)), |
| 444 |
]) |
| 445 |
self.sa_bytes_multiple = b''.join(self.sa_dict_multiple.values()) |
| 446 |
|
| 447 |
def test_parse(self): |
| 448 |
parsed_sa = Ikev2PayloadSecurityAssociation.parse_exact_size(self.sa_bytes_single) |
| 449 |
self.assertEqual(parsed_sa.next_payload, Ikev2PayloadType.NONE) |
| 450 |
self.assertEqual(parsed_sa.flags, set()) |
| 451 |
self.assertEqual(len(parsed_sa.proposals), 1) |
| 452 |
self.assertEqual(parsed_sa.proposals[0].protocol_id, Ikev2ProtocolId.IKE) |
| 453 |
|
| 454 |
parsed_sa_multiple = Ikev2PayloadSecurityAssociation.parse_exact_size(self.sa_bytes_multiple) |
| 455 |
self.assertEqual(parsed_sa_multiple.next_payload, Ikev2PayloadType.KE) |
| 456 |
self.assertEqual(parsed_sa_multiple.flags, {Ikev2PayloadFlags.CRITICAL}) |
| 457 |
self.assertEqual(len(parsed_sa_multiple.proposals), 2) |
| 458 |
|
| 459 |
def test_compose(self): |
| 460 |
composed_bytes = self.sa_single_proposal.compose() |
| 461 |
self.assertEqual(composed_bytes, self.sa_bytes_single) |
| 462 |
|
| 463 |
composed_bytes_multiple = self.sa_multiple_proposals.compose() |
| 464 |
self.assertEqual(composed_bytes_multiple, self.sa_bytes_multiple) |
| 465 |
|
| 466 |
def test_round_trip(self): |
| 467 |
composed_bytes = self.sa_single_proposal.compose() |
| 468 |
parsed_sa: Ikev2PayloadSecurityAssociation = Ikev2PayloadSecurityAssociation.parse_exact_size(composed_bytes) |
| 469 |
|
| 470 |
self.assertEqual(parsed_sa.next_payload, self.sa_single_proposal.next_payload) |
| 471 |
self.assertEqual(parsed_sa.flags, self.sa_single_proposal.flags) |
| 472 |
self.assertEqual(len(parsed_sa.proposals), len(self.sa_single_proposal.proposals)) |
| 473 |
|
| 474 |
def test_get_payload_type(self): |
| 475 |
self.assertEqual(Ikev2PayloadSecurityAssociation.get_payload_type(), Ikev2PayloadType.SA) |
| 476 |
|
| 477 |
def test_error_parse_not_enough_data(self): |
| 478 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 479 |
Ikev2PayloadSecurityAssociation.parse_exact_size(b'\x00\x01\x02') |
| 480 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 481 |
|
| 482 |
incomplete = self.sa_bytes_single[:-1] |
| 483 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 484 |
Ikev2PayloadSecurityAssociation.parse_exact_size(incomplete) |
| 485 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 486 |
|
| 487 |
def test_error_invalid_proposals(self): |
| 488 |
with self.assertRaises(TypeError): |
| 489 |
Ikev2PayloadSecurityAssociation( |
| 490 |
flags=set(), |
| 491 |
proposals=["invalid_proposal"] |
| 492 |
) |
| 493 |
|
| 494 |
def test_get_transform_by_type(self): |
| 495 |
transform_prf = Ikev2TransformPrf( |
| 496 |
transform_id=Ikev2PseudorandomFunction.PRF_HMAC_SHA1 |
| 497 |
) |
| 498 |
transform_prf.next_payload = TransformNextPayload.LAST |
| 499 |
|
| 500 |
transform_dh = Ikev2TransformDhGroup( |
| 501 |
transform_id=Ikev2DiffieHellmanGroup.MODP_GROUP_2048_BIT |
| 502 |
) |
| 503 |
transform_dh.next_payload = TransformNextPayload.LAST |
| 504 |
|
| 505 |
transform_encr = Ikev2TransformEncryptionAlgorithm( |
| 506 |
transform_id=Ikev2EncryptionAlgorithm.ENCR_AES_CBC, |
| 507 |
key_length=128 |
| 508 |
) |
| 509 |
transform_encr.next_payload = TransformNextPayload.LAST |
| 510 |
|
| 511 |
proposal = Ikev2Proposal( |
| 512 |
protocol_id=Ikev2ProtocolId.IKE, |
| 513 |
transforms=[transform_prf, transform_dh, transform_encr], |
| 514 |
spi=b'' |
| 515 |
) |
| 516 |
|
| 517 |
sa_payload = Ikev2PayloadSecurityAssociation( |
| 518 |
flags=set(), |
| 519 |
proposals=[proposal] |
| 520 |
) |
| 521 |
|
| 522 |
found_prf = sa_payload.get_transform_by_type(Ikev2TransformType.PRF) |
| 523 |
self.assertEqual(found_prf, transform_prf) |
| 524 |
self.assertEqual(found_prf.get_transform_type(), Ikev2TransformType.PRF) |
| 525 |
|
| 526 |
found_dh = sa_payload.get_transform_by_type(Ikev2TransformType.DH) |
| 527 |
self.assertEqual(found_dh, transform_dh) |
| 528 |
self.assertEqual(found_dh.get_transform_type(), Ikev2TransformType.DH) |
| 529 |
|
| 530 |
found_encr = sa_payload.get_transform_by_type(Ikev2TransformType.ENCR) |
| 531 |
self.assertEqual(found_encr, transform_encr) |
| 532 |
self.assertEqual(found_encr.get_transform_type(), Ikev2TransformType.ENCR) |
| 533 |
|
| 534 |
transform_integ = Ikev2TransformIntegrity( |
| 535 |
transform_id=Ikev2IntegrityAlgorithm.AUTH_HMAC_SHA1_96 |
| 536 |
) |
| 537 |
transform_integ.next_payload = TransformNextPayload.LAST |
| 538 |
|
| 539 |
proposal_with_integ = Ikev2Proposal( |
| 540 |
protocol_id=Ikev2ProtocolId.IKE, |
| 541 |
transforms=[transform_integ], |
| 542 |
spi=b'' |
| 543 |
) |
| 544 |
|
| 545 |
sa_payload_with_integ = Ikev2PayloadSecurityAssociation( |
| 546 |
flags=set(), |
| 547 |
proposals=[proposal_with_integ] |
| 548 |
) |
| 549 |
|
| 550 |
found_integ = sa_payload_with_integ.get_transform_by_type(Ikev2TransformType.INTEG) |
| 551 |
self.assertEqual(found_integ, transform_integ) |
| 552 |
self.assertEqual(found_integ.get_transform_type(), Ikev2TransformType.INTEG) |
| 553 |
|
| 554 |
def test_get_transform_by_type_multiple_proposals(self): |
| 555 |
transform_prf1 = Ikev2TransformPrf( |
| 556 |
transform_id=Ikev2PseudorandomFunction.PRF_HMAC_SHA1 |
| 557 |
) |
| 558 |
transform_prf1.next_payload = TransformNextPayload.LAST |
| 559 |
|
| 560 |
transform_prf2 = Ikev2TransformPrf( |
| 561 |
transform_id=Ikev2PseudorandomFunction.PRF_HMAC_SHA2_256 |
| 562 |
) |
| 563 |
transform_prf2.next_payload = TransformNextPayload.LAST |
| 564 |
|
| 565 |
proposal1 = Ikev2Proposal( |
| 566 |
protocol_id=Ikev2ProtocolId.IKE, |
| 567 |
transforms=[transform_prf1], |
| 568 |
spi=b'' |
| 569 |
) |
| 570 |
|
| 571 |
proposal2 = Ikev2Proposal( |
| 572 |
protocol_id=Ikev2ProtocolId.IKE, |
| 573 |
transforms=[transform_prf2], |
| 574 |
spi=b'' |
| 575 |
) |
| 576 |
|
| 577 |
sa_payload = Ikev2PayloadSecurityAssociation( |
| 578 |
flags=set(), |
| 579 |
proposals=[proposal1, proposal2] |
| 580 |
) |
| 581 |
|
| 582 |
found_prf = sa_payload.get_transform_by_type(Ikev2TransformType.PRF) |
| 583 |
self.assertEqual(found_prf.get_transform_type(), Ikev2TransformType.PRF) |
| 584 |
self.assertIn(found_prf, [transform_prf1, transform_prf2]) |
| 585 |
|
| 586 |
def test_get_transform_by_type_not_found(self): |
| 587 |
sa_payload = Ikev2PayloadSecurityAssociation( |
| 588 |
flags=set(), |
| 589 |
proposals=[self.proposal_single] |
| 590 |
) |
| 591 |
|
| 592 |
with self.assertRaises(KeyError) as context_manager: |
| 593 |
sa_payload.get_transform_by_type(Ikev2TransformType.DH) |
| 594 |
self.assertEqual(context_manager.exception.args[0], Ikev2TransformType.DH) |
| 595 |
|
| 596 |
def test_get_transform_by_type_empty_proposals(self): |
| 597 |
sa_payload = Ikev2PayloadSecurityAssociation( |
| 598 |
flags=set(), |
| 599 |
proposals=[] |
| 600 |
) |
| 601 |
|
| 602 |
with self.assertRaises(KeyError) as context_manager: |
| 603 |
sa_payload.get_transform_by_type(Ikev2TransformType.PRF) |
| 604 |
self.assertEqual(context_manager.exception.args[0], Ikev2TransformType.PRF) |
| 605 |
|
| 606 |
|
| 607 |
if __name__ == '__main__': |
| 608 |
unittest.main() |