| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
from test.common.classes import TestClasses |
| 6 |
|
| 7 |
from cryptodatahub.common.entity import Entity |
| 8 |
from cryptodatahub.common.exception import InvalidProposalToken, InvalidValue |
| 9 |
from cryptodatahub.ike.version import IkeVersion |
| 10 |
from cryptodatahub.ike.algorithm import ( |
| 11 |
IkeEncryptionBulkCipherEntry, |
| 12 |
IkeLongNameParams, |
| 13 |
IkeVendorId, |
| 14 |
Ikev2PayloadType, |
| 15 |
Ikev2NotifyType, |
| 16 |
Ikev2ExtendedSequenceNumber, |
| 17 |
Ikev2TransformType, |
| 18 |
Ikev2AuthenticationMethod, |
| 19 |
Ikev2DiffieHellmanGroup, |
| 20 |
Ikev2EncryptionAlgorithm, |
| 21 |
Ikev2EncryptionAlgorithmParams, |
| 22 |
Ikev2HashAlgorithm, |
| 23 |
Ikev2IntegrityAlgorithm, |
| 24 |
Ikev2PseudorandomFunction, |
| 25 |
Ikev1AttributeType, |
| 26 |
Ikev1AuthenticationMethod, |
| 27 |
Ikev1CertificateType, |
| 28 |
Ikev1DiffieHellmanGroup, |
| 29 |
Ikev1Doi, |
| 30 |
Ikev1EncryptionAlgorithm, |
| 31 |
Ikev1EncryptionAlgorithmParams, |
| 32 |
Ikev1GroupType, |
| 33 |
Ikev1HashAlgorithm, |
| 34 |
Ikev1LifeType, |
| 35 |
Ikev1NotifyType, |
| 36 |
Ikev1PayloadType, |
| 37 |
Ikev1ProtocolId, |
| 38 |
Ikev1TransformType, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
class TestIkev2PseudorandomFunction(TestClasses.TestJsonCodeNumericBase): |
| 43 |
@classmethod |
| 44 |
def _get_class(cls): |
| 45 |
return Ikev2PseudorandomFunction |
| 46 |
|
| 47 |
def test_str(self): |
| 48 |
self.assertEqual(str(Ikev2PseudorandomFunction.PRF_HMAC_MD5.value), 'MD5') |
| 49 |
|
| 50 |
def test_from_name(self): |
| 51 |
for pseudorandom_function in Ikev2PseudorandomFunction: |
| 52 |
self.assertEqual( |
| 53 |
Ikev2PseudorandomFunction.from_name(pseudorandom_function.name.lower()), |
| 54 |
pseudorandom_function |
| 55 |
) |
| 56 |
with self.assertRaises(InvalidValue): |
| 57 |
Ikev2PseudorandomFunction.from_name('no-such-name') |
| 58 |
|
| 59 |
def test_from_implementation_name(self): |
| 60 |
for pseudorandom_function in Ikev2PseudorandomFunction: |
| 61 |
for entity, token in pseudorandom_function.value.names.items(): |
| 62 |
if token is not None: |
| 63 |
self.assertEqual( |
| 64 |
Ikev2PseudorandomFunction.from_implementation_name(entity, token), |
| 65 |
pseudorandom_function |
| 66 |
) |
| 67 |
with self.assertRaises(InvalidValue): |
| 68 |
Ikev2PseudorandomFunction.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 69 |
|
| 70 |
|
| 71 |
class TestIkev2AuthenticationMethod(TestClasses.TestJsonCodeNumericBase): |
| 72 |
@classmethod |
| 73 |
def _get_class(cls): |
| 74 |
return Ikev2AuthenticationMethod |
| 75 |
|
| 76 |
def test_str(self): |
| 77 |
self.assertEqual( |
| 78 |
str(Ikev2AuthenticationMethod.RSA_DIGITAL_SIGNATURE.value), |
| 79 |
'RSA Digital Signature' |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
class TestIkev2DiffieHellmanGroup(TestClasses.TestJsonCodeNumericBase): |
| 84 |
@classmethod |
| 85 |
def _get_class(cls): |
| 86 |
return Ikev2DiffieHellmanGroup |
| 87 |
|
| 88 |
def test_str(self): |
| 89 |
self.assertEqual( |
| 90 |
str(Ikev2DiffieHellmanGroup.MODP_GROUP_768_BIT.value), |
| 91 |
'768-bit MODP Group/Oakley Group 1' |
| 92 |
) |
| 93 |
|
| 94 |
def test_from_name(self): |
| 95 |
for diffie_hellman_group in Ikev2DiffieHellmanGroup: |
| 96 |
self.assertEqual( |
| 97 |
Ikev2DiffieHellmanGroup.from_name(diffie_hellman_group.name.lower()), |
| 98 |
diffie_hellman_group |
| 99 |
) |
| 100 |
with self.assertRaises(InvalidValue): |
| 101 |
Ikev2DiffieHellmanGroup.from_name('no-such-name') |
| 102 |
|
| 103 |
def test_from_implementation_name(self): |
| 104 |
for diffie_hellman_group in Ikev2DiffieHellmanGroup: |
| 105 |
for entity, token in diffie_hellman_group.value.names.items(): |
| 106 |
if token is not None: |
| 107 |
self.assertEqual( |
| 108 |
Ikev2DiffieHellmanGroup.from_implementation_name(entity, token), |
| 109 |
diffie_hellman_group |
| 110 |
) |
| 111 |
with self.assertRaises(InvalidValue): |
| 112 |
Ikev2DiffieHellmanGroup.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 113 |
|
| 114 |
|
| 115 |
class TestIkev2EncryptionAlgorithm(TestClasses.TestJsonCodeNumericBase): |
| 116 |
@classmethod |
| 117 |
def _get_class(cls): |
| 118 |
return Ikev2EncryptionAlgorithm |
| 119 |
|
| 120 |
def test_str(self): |
| 121 |
self.assertEqual( |
| 122 |
str(Ikev2EncryptionAlgorithm.ENCR_DES_IV64.value), |
| 123 |
'DES (CBC)' |
| 124 |
) |
| 125 |
self.assertEqual( |
| 126 |
str(Ikev2EncryptionAlgorithmParams( |
| 127 |
code=0, |
| 128 |
bulk_ciphers=[{ |
| 129 |
"cipher": "CHACHA20", |
| 130 |
"names": {"STRONGSWAN": None, "LIBRESWAN": None, "OPENSWAN": None}, |
| 131 |
}], |
| 132 |
block_cipher_mode=None, |
| 133 |
)), |
| 134 |
'ChaCha20' |
| 135 |
) |
| 136 |
self.assertEqual( |
| 137 |
str(Ikev2EncryptionAlgorithm.ENCR_NULL.value), |
| 138 |
'null' |
| 139 |
) |
| 140 |
existing_entry = IkeEncryptionBulkCipherEntry( |
| 141 |
cipher='AES_128', |
| 142 |
names={'STRONGSWAN': None, 'LIBRESWAN': None, 'OPENSWAN': None}, |
| 143 |
) |
| 144 |
self.assertEqual( |
| 145 |
str(Ikev2EncryptionAlgorithmParams( |
| 146 |
code=0, |
| 147 |
bulk_ciphers=[existing_entry], |
| 148 |
block_cipher_mode=None, |
| 149 |
)), |
| 150 |
'AES-128' |
| 151 |
) |
| 152 |
|
| 153 |
def test_aead_via_block_cipher_mode(self): |
| 154 |
# AEAD algorithms: mode carries aead=True, propagated to encr.aead |
| 155 |
self.assertTrue(Ikev2EncryptionAlgorithm.ENCR_AES_GCM_16.value.aead) |
| 156 |
self.assertTrue(Ikev2EncryptionAlgorithm.ENCR_AES_CCM_8.value.aead) |
| 157 |
self.assertTrue(Ikev2EncryptionAlgorithm.ENCR_NULL_AUTH_AES_GMAC.value.aead) |
| 158 |
self.assertTrue(Ikev2EncryptionAlgorithm.ENCR_KUZNYECHIK_MGM_KTREE.value.aead) |
| 159 |
self.assertTrue(Ikev2EncryptionAlgorithm.ENCR_KUZNYECHIK_MGM_MAC_KTREE.value.aead) |
| 160 |
# Non-AEAD algorithms: mode carries aead=False |
| 161 |
self.assertFalse(Ikev2EncryptionAlgorithm.ENCR_AES_CBC.value.aead) |
| 162 |
self.assertFalse(Ikev2EncryptionAlgorithm.ENCR_AES_CTR.value.aead) |
| 163 |
# ChaCha20-Poly1305: stream cipher, no block_cipher_mode; AEAD via bulk cipher |
| 164 |
chacha = Ikev2EncryptionAlgorithm.ENCR_CHACHA20_POLY1305.value |
| 165 |
self.assertIsNone(chacha.block_cipher_mode) |
| 166 |
self.assertTrue(chacha.aead) |
| 167 |
|
| 168 |
def test_from_name(self): |
| 169 |
for encryption_algorithm in Ikev2EncryptionAlgorithm: |
| 170 |
self.assertEqual( |
| 171 |
Ikev2EncryptionAlgorithm.from_name(encryption_algorithm.name.lower()), |
| 172 |
encryption_algorithm |
| 173 |
) |
| 174 |
with self.assertRaises(InvalidValue): |
| 175 |
Ikev2EncryptionAlgorithm.from_name('no-such-name') |
| 176 |
|
| 177 |
def test_from_implementation_name(self): |
| 178 |
for encryption_algorithm in Ikev2EncryptionAlgorithm: |
| 179 |
for bulk_cipher in encryption_algorithm.value.bulk_ciphers: |
| 180 |
for entity, token in bulk_cipher.names.items(): |
| 181 |
if token is not None: |
| 182 |
self.assertEqual( |
| 183 |
Ikev2EncryptionAlgorithm.from_implementation_name(entity, token), |
| 184 |
(encryption_algorithm, bulk_cipher.cipher.value.key_size) |
| 185 |
) |
| 186 |
with self.assertRaises(InvalidValue): |
| 187 |
Ikev2EncryptionAlgorithm.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 188 |
|
| 189 |
def test_from_proposal_token(self): |
| 190 |
self.assertEqual( |
| 191 |
Ikev2EncryptionAlgorithm.from_proposal_token(Entity.STRONGSWAN, 'aes256gcm16'), |
| 192 |
(Ikev2EncryptionAlgorithm.ENCR_AES_GCM_16, 256) |
| 193 |
) |
| 194 |
self.assertEqual( |
| 195 |
Ikev2EncryptionAlgorithm.from_proposal_token(None, 'encr_aes_cbc/128'), |
| 196 |
(Ikev2EncryptionAlgorithm.ENCR_AES_CBC, 128) |
| 197 |
) |
| 198 |
self.assertEqual( |
| 199 |
Ikev2EncryptionAlgorithm.from_proposal_token(None, 'encr_chacha20_poly1305'), |
| 200 |
(Ikev2EncryptionAlgorithm.ENCR_CHACHA20_POLY1305, 256) |
| 201 |
) |
| 202 |
|
| 203 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 204 |
Ikev2EncryptionAlgorithm.from_proposal_token(Entity.STRONGSWAN, 'no-such-cipher') |
| 205 |
self.assertEqual(context_manager.exception.hint, 'unknown strongswan token') |
| 206 |
|
| 207 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 208 |
Ikev2EncryptionAlgorithm.from_proposal_token(None, 'encr_null') |
| 209 |
self.assertEqual(context_manager.exception.hint, 'encryption algorithm has no offerable key length') |
| 210 |
|
| 211 |
|
| 212 |
class TestIkev2HashAlgorithm(TestClasses.TestJsonCodeNumericBase): |
| 213 |
@classmethod |
| 214 |
def _get_class(cls): |
| 215 |
return Ikev2HashAlgorithm |
| 216 |
|
| 217 |
def test_str(self): |
| 218 |
self.assertEqual( |
| 219 |
str(Ikev2HashAlgorithm.SHA2_256.value), |
| 220 |
'SHA-256' |
| 221 |
) |
| 222 |
|
| 223 |
def test_str_identity(self): |
| 224 |
self.assertIsNone(Ikev2HashAlgorithm.IDENTITY.value.hash_algorithm) |
| 225 |
self.assertEqual( |
| 226 |
str(Ikev2HashAlgorithm.IDENTITY.value), |
| 227 |
'Identity' |
| 228 |
) |
| 229 |
|
| 230 |
|
| 231 |
class TestIkev2IntegrityAlgorithm(TestClasses.TestJsonCodeNumericBase): |
| 232 |
@classmethod |
| 233 |
def _get_class(cls): |
| 234 |
return Ikev2IntegrityAlgorithm |
| 235 |
|
| 236 |
def test_str(self): |
| 237 |
self.assertEqual( |
| 238 |
str(Ikev2IntegrityAlgorithm.AUTH_HMAC_MD5_96.value), |
| 239 |
'MD5' |
| 240 |
) |
| 241 |
self.assertEqual( |
| 242 |
str(Ikev2IntegrityAlgorithm.NONE.value), |
| 243 |
'null' |
| 244 |
) |
| 245 |
|
| 246 |
def test_from_name(self): |
| 247 |
for integrity_algorithm in Ikev2IntegrityAlgorithm: |
| 248 |
self.assertEqual( |
| 249 |
Ikev2IntegrityAlgorithm.from_name(integrity_algorithm.name.lower()), |
| 250 |
integrity_algorithm |
| 251 |
) |
| 252 |
with self.assertRaises(InvalidValue): |
| 253 |
Ikev2IntegrityAlgorithm.from_name('no-such-name') |
| 254 |
|
| 255 |
def test_from_implementation_name(self): |
| 256 |
for integrity_algorithm in Ikev2IntegrityAlgorithm: |
| 257 |
for entity, token in integrity_algorithm.value.names.items(): |
| 258 |
if token is not None: |
| 259 |
self.assertEqual( |
| 260 |
Ikev2IntegrityAlgorithm.from_implementation_name(entity, token), |
| 261 |
integrity_algorithm |
| 262 |
) |
| 263 |
with self.assertRaises(InvalidValue): |
| 264 |
Ikev2IntegrityAlgorithm.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 265 |
|
| 266 |
|
| 267 |
class TestIkev2PayloadType(TestClasses.TestJsonCodeNumericBase): |
| 268 |
@classmethod |
| 269 |
def _get_class(cls): |
| 270 |
return Ikev2PayloadType |
| 271 |
|
| 272 |
|
| 273 |
class TestIkev2NotifyType(TestClasses.TestJsonCodeNumericBase): |
| 274 |
@classmethod |
| 275 |
def _get_class(cls): |
| 276 |
return Ikev2NotifyType |
| 277 |
|
| 278 |
|
| 279 |
class TestIkev2ExtendedSequenceNumber(TestClasses.TestJsonCodeNumericBase): |
| 280 |
@classmethod |
| 281 |
def _get_class(cls): |
| 282 |
return Ikev2ExtendedSequenceNumber |
| 283 |
|
| 284 |
|
| 285 |
class TestIkev2TransformType(TestClasses.TestJsonCodeNumericBase): |
| 286 |
@classmethod |
| 287 |
def _get_class(cls): |
| 288 |
return Ikev2TransformType |
| 289 |
|
| 290 |
|
| 291 |
class TestIkev1AttributeType(TestClasses.TestJsonCodeNumericBase): |
| 292 |
@classmethod |
| 293 |
def _get_class(cls): |
| 294 |
return Ikev1AttributeType |
| 295 |
|
| 296 |
|
| 297 |
class TestIkev1Doi(TestClasses.TestJsonCodeNumericBase): |
| 298 |
@classmethod |
| 299 |
def _get_class(cls): |
| 300 |
return Ikev1Doi |
| 301 |
|
| 302 |
|
| 303 |
class TestIkev1NotifyType(TestClasses.TestJsonCodeNumericBase): |
| 304 |
@classmethod |
| 305 |
def _get_class(cls): |
| 306 |
return Ikev1NotifyType |
| 307 |
|
| 308 |
|
| 309 |
class TestIkev1PayloadType(TestClasses.TestJsonCodeNumericBase): |
| 310 |
@classmethod |
| 311 |
def _get_class(cls): |
| 312 |
return Ikev1PayloadType |
| 313 |
|
| 314 |
|
| 315 |
class TestIkev1ProtocolId(TestClasses.TestJsonCodeNumericBase): |
| 316 |
@classmethod |
| 317 |
def _get_class(cls): |
| 318 |
return Ikev1ProtocolId |
| 319 |
|
| 320 |
|
| 321 |
class TestIkev1TransformType(TestClasses.TestJsonCodeNumericBase): |
| 322 |
@classmethod |
| 323 |
def _get_class(cls): |
| 324 |
return Ikev1TransformType |
| 325 |
|
| 326 |
|
| 327 |
class TestIkev1LifeType(TestClasses.TestJsonCodeNumericBase): |
| 328 |
@classmethod |
| 329 |
def _get_class(cls): |
| 330 |
return Ikev1LifeType |
| 331 |
|
| 332 |
|
| 333 |
class TestIkev1GroupType(TestClasses.TestJsonCodeNumericBase): |
| 334 |
@classmethod |
| 335 |
def _get_class(cls): |
| 336 |
return Ikev1GroupType |
| 337 |
|
| 338 |
|
| 339 |
class TestIkeLongNameParams(unittest.TestCase): |
| 340 |
def test_str_returns_long_name(self): |
| 341 |
class _ConcreteLongName(IkeLongNameParams): |
| 342 |
@classmethod |
| 343 |
def get_code_size(cls): |
| 344 |
return 1 |
| 345 |
|
| 346 |
entry = _ConcreteLongName(code=1, long_name='human readable') |
| 347 |
self.assertEqual(str(entry), 'human readable') |
| 348 |
|
| 349 |
|
| 350 |
class TestIkev1CertificateType(TestClasses.TestJsonCodeNumericBase): |
| 351 |
@classmethod |
| 352 |
def _get_class(cls): |
| 353 |
return Ikev1CertificateType |
| 354 |
|
| 355 |
def test_str(self): |
| 356 |
self.assertEqual( |
| 357 |
str(Ikev1CertificateType.X509_CERTIFICATE_SIGNATURE.value), |
| 358 |
'X.509 Certificate - Signature', |
| 359 |
) |
| 360 |
|
| 361 |
|
| 362 |
class TestIkev1AuthenticationMethod(TestClasses.TestJsonCodeNumericBase): |
| 363 |
@classmethod |
| 364 |
def _get_class(cls): |
| 365 |
return Ikev1AuthenticationMethod |
| 366 |
|
| 367 |
def test_str(self): |
| 368 |
self.assertEqual( |
| 369 |
str(Ikev1AuthenticationMethod.RSA_SIGNATURES.value), |
| 370 |
'RSA Signatures', |
| 371 |
) |
| 372 |
|
| 373 |
def test_from_name(self): |
| 374 |
for authentication_method in Ikev1AuthenticationMethod: |
| 375 |
self.assertEqual( |
| 376 |
Ikev1AuthenticationMethod.from_name(authentication_method.name.lower()), |
| 377 |
authentication_method |
| 378 |
) |
| 379 |
with self.assertRaises(InvalidValue): |
| 380 |
Ikev1AuthenticationMethod.from_name('no-such-name') |
| 381 |
|
| 382 |
def test_from_implementation_name(self): |
| 383 |
for authentication_method in Ikev1AuthenticationMethod: |
| 384 |
for entity, token in authentication_method.value.names.items(): |
| 385 |
if token is not None: |
| 386 |
self.assertEqual( |
| 387 |
Ikev1AuthenticationMethod.from_implementation_name(entity, token), |
| 388 |
authentication_method |
| 389 |
) |
| 390 |
self.assertEqual( |
| 391 |
Ikev1AuthenticationMethod.from_implementation_name(Entity.STRONGSWAN, 'psk'), |
| 392 |
Ikev1AuthenticationMethod.PRE_SHARED_KEY |
| 393 |
) |
| 394 |
with self.assertRaises(InvalidValue): |
| 395 |
Ikev1AuthenticationMethod.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 396 |
with self.assertRaises(InvalidValue): |
| 397 |
Ikev1AuthenticationMethod.from_implementation_name(Entity.STRONGSWAN, None) |
| 398 |
|
| 399 |
|
| 400 |
class TestIkev1DiffieHellmanGroup(TestClasses.TestJsonCodeNumericBase): |
| 401 |
@classmethod |
| 402 |
def _get_class(cls): |
| 403 |
return Ikev1DiffieHellmanGroup |
| 404 |
|
| 405 |
def test_str(self): |
| 406 |
self.assertEqual( |
| 407 |
str(Ikev1DiffieHellmanGroup.MODP_768_BIT.value), |
| 408 |
'768-bit MODP Group/Oakley Group 1' |
| 409 |
) |
| 410 |
|
| 411 |
def test_from_name(self): |
| 412 |
for diffie_hellman_group in Ikev1DiffieHellmanGroup: |
| 413 |
self.assertEqual( |
| 414 |
Ikev1DiffieHellmanGroup.from_name(diffie_hellman_group.name.lower()), |
| 415 |
diffie_hellman_group |
| 416 |
) |
| 417 |
with self.assertRaises(InvalidValue): |
| 418 |
Ikev1DiffieHellmanGroup.from_name('no-such-name') |
| 419 |
|
| 420 |
def test_from_implementation_name(self): |
| 421 |
for diffie_hellman_group in Ikev1DiffieHellmanGroup: |
| 422 |
for entity, token in diffie_hellman_group.value.names.items(): |
| 423 |
if token is not None: |
| 424 |
self.assertEqual( |
| 425 |
Ikev1DiffieHellmanGroup.from_implementation_name(entity, token), |
| 426 |
diffie_hellman_group |
| 427 |
) |
| 428 |
with self.assertRaises(InvalidValue): |
| 429 |
Ikev1DiffieHellmanGroup.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 430 |
|
| 431 |
|
| 432 |
class TestIkev1EncryptionAlgorithm(TestClasses.TestJsonCodeNumericBase): |
| 433 |
@classmethod |
| 434 |
def _get_class(cls): |
| 435 |
return Ikev1EncryptionAlgorithm |
| 436 |
|
| 437 |
def test_str(self): |
| 438 |
self.assertEqual( |
| 439 |
str(Ikev1EncryptionAlgorithm.DES_CBC.value), |
| 440 |
'DES (CBC)' |
| 441 |
) |
| 442 |
self.assertEqual( |
| 443 |
str(Ikev1EncryptionAlgorithmParams( |
| 444 |
code=0, |
| 445 |
bulk_ciphers=[], |
| 446 |
block_cipher_mode=None, |
| 447 |
)), |
| 448 |
'null' |
| 449 |
) |
| 450 |
self.assertEqual( |
| 451 |
str(Ikev1EncryptionAlgorithmParams( |
| 452 |
code=0, |
| 453 |
bulk_ciphers=[{ |
| 454 |
"cipher": "AES_128", |
| 455 |
"names": {"STRONGSWAN": None, "LIBRESWAN": None, "OPENSWAN": None}, |
| 456 |
}], |
| 457 |
block_cipher_mode=None, |
| 458 |
)), |
| 459 |
'AES-128' |
| 460 |
) |
| 461 |
|
| 462 |
def test_from_name(self): |
| 463 |
for encryption_algorithm in Ikev1EncryptionAlgorithm: |
| 464 |
self.assertEqual( |
| 465 |
Ikev1EncryptionAlgorithm.from_name(encryption_algorithm.name.lower()), |
| 466 |
encryption_algorithm |
| 467 |
) |
| 468 |
with self.assertRaises(InvalidValue): |
| 469 |
Ikev1EncryptionAlgorithm.from_name('no-such-name') |
| 470 |
|
| 471 |
def test_from_implementation_name(self): |
| 472 |
for encryption_algorithm in Ikev1EncryptionAlgorithm: |
| 473 |
for bulk_cipher in encryption_algorithm.value.bulk_ciphers: |
| 474 |
for entity, token in bulk_cipher.names.items(): |
| 475 |
if token is not None: |
| 476 |
self.assertEqual( |
| 477 |
Ikev1EncryptionAlgorithm.from_implementation_name(entity, token), |
| 478 |
(encryption_algorithm, bulk_cipher.cipher.value.key_size) |
| 479 |
) |
| 480 |
with self.assertRaises(InvalidValue): |
| 481 |
Ikev1EncryptionAlgorithm.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 482 |
|
| 483 |
def test_from_proposal_token(self): |
| 484 |
self.assertEqual( |
| 485 |
Ikev1EncryptionAlgorithm.from_proposal_token(Entity.STRONGSWAN, 'aes256'), |
| 486 |
(Ikev1EncryptionAlgorithm.AES_CBC, 256) |
| 487 |
) |
| 488 |
self.assertEqual( |
| 489 |
Ikev1EncryptionAlgorithm.from_proposal_token(Entity.LIBRESWAN, 'aes_gcm_c256'), |
| 490 |
(Ikev1EncryptionAlgorithm.AES_GCM_16, 256) |
| 491 |
) |
| 492 |
self.assertEqual( |
| 493 |
Ikev1EncryptionAlgorithm.from_proposal_token(None, 'aes_cbc/128'), |
| 494 |
(Ikev1EncryptionAlgorithm.AES_CBC, 128) |
| 495 |
) |
| 496 |
self.assertEqual( |
| 497 |
Ikev1EncryptionAlgorithm.from_proposal_token(None, 'des_cbc'), |
| 498 |
(Ikev1EncryptionAlgorithm.DES_CBC, 56) |
| 499 |
) |
| 500 |
|
| 501 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 502 |
Ikev1EncryptionAlgorithm.from_proposal_token(None, 'no_such_cipher/128') |
| 503 |
self.assertEqual(context_manager.exception.hint, 'unknown iana token') |
| 504 |
|
| 505 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 506 |
Ikev1EncryptionAlgorithm.from_proposal_token(None, 'aes_cbc') |
| 507 |
self.assertEqual( |
| 508 |
context_manager.exception.hint, 'key length is required on a variable-key encryption algorithm' |
| 509 |
) |
| 510 |
|
| 511 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 512 |
Ikev1EncryptionAlgorithm.from_proposal_token(None, 'des_cbc/56') |
| 513 |
self.assertEqual( |
| 514 |
context_manager.exception.hint, 'key length is forbidden on a fixed-key encryption algorithm' |
| 515 |
) |
| 516 |
|
| 517 |
for key_length_text in ('64', 'notanumber'): |
| 518 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 519 |
Ikev1EncryptionAlgorithm.from_proposal_token(None, f'aes_cbc/{key_length_text}') |
| 520 |
self.assertIn('key length must be one of', context_manager.exception.hint) |
| 521 |
|
| 522 |
|
| 523 |
class TestIkev1HashAlgorithm(TestClasses.TestJsonCodeNumericBase): |
| 524 |
@classmethod |
| 525 |
def _get_class(cls): |
| 526 |
return Ikev1HashAlgorithm |
| 527 |
|
| 528 |
def test_str(self): |
| 529 |
self.assertEqual( |
| 530 |
str(Ikev1HashAlgorithm.MD5.value), |
| 531 |
'MD5' |
| 532 |
) |
| 533 |
|
| 534 |
def test_from_name(self): |
| 535 |
for hash_algorithm in Ikev1HashAlgorithm: |
| 536 |
self.assertEqual( |
| 537 |
Ikev1HashAlgorithm.from_name(hash_algorithm.name.lower()), |
| 538 |
hash_algorithm |
| 539 |
) |
| 540 |
with self.assertRaises(InvalidValue): |
| 541 |
Ikev1HashAlgorithm.from_name('no-such-name') |
| 542 |
|
| 543 |
def test_from_implementation_name(self): |
| 544 |
for hash_algorithm in Ikev1HashAlgorithm: |
| 545 |
for entity, token in hash_algorithm.value.names.items(): |
| 546 |
if token is not None: |
| 547 |
self.assertEqual( |
| 548 |
Ikev1HashAlgorithm.from_implementation_name(entity, token), |
| 549 |
hash_algorithm |
| 550 |
) |
| 551 |
with self.assertRaises(InvalidValue): |
| 552 |
Ikev1HashAlgorithm.from_implementation_name(Entity.STRONGSWAN, 'no-such-token') |
| 553 |
|
| 554 |
def test_from_proposal_token(self): |
| 555 |
self.assertEqual( |
| 556 |
Ikev1HashAlgorithm.from_proposal_token(Entity.STRONGSWAN, 'sha256'), |
| 557 |
Ikev1HashAlgorithm.SHA2_256 |
| 558 |
) |
| 559 |
self.assertEqual( |
| 560 |
Ikev1HashAlgorithm.from_proposal_token(None, 'sha2_256'), |
| 561 |
Ikev1HashAlgorithm.SHA2_256 |
| 562 |
) |
| 563 |
|
| 564 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 565 |
Ikev1HashAlgorithm.from_proposal_token(Entity.STRONGSWAN, 'no-such-hash') |
| 566 |
self.assertEqual(context_manager.exception.hint, 'unknown strongswan token') |
| 567 |
|
| 568 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 569 |
Ikev1HashAlgorithm.from_proposal_token(None, 'no-such-hash') |
| 570 |
self.assertEqual(context_manager.exception.hint, 'unknown iana token') |
| 571 |
|
| 572 |
with self.assertRaises(InvalidProposalToken) as context_manager: |
| 573 |
Ikev1HashAlgorithm.from_proposal_token(None, 'sha2_256/128') |
| 574 |
self.assertEqual(context_manager.exception.hint, 'key length is forbidden on a non-encryption algorithm') |
| 575 |
|
| 576 |
|
| 577 |
class TestIkeVendorId(TestClasses.TestJsonBase): |
| 578 |
@classmethod |
| 579 |
def _get_class(cls): |
| 580 |
return IkeVendorId |
| 581 |
|
| 582 |
def test_from_binary(self): |
| 583 |
self.assertEqual( |
| 584 |
IkeVendorId.from_binary(bytes.fromhex('fbf47614984031fa8e3bb6198089b223')), |
| 585 |
IkeVendorId.SSH_IPSEC_EXPRESS_1_1_0, |
| 586 |
) |
| 587 |
self.assertEqual( |
| 588 |
IkeVendorId.from_binary(bytearray.fromhex('fbf47614984031fa8e3bb6198089b223')), |
| 589 |
IkeVendorId.SSH_IPSEC_EXPRESS_1_1_0, |
| 590 |
) |
| 591 |
|
| 592 |
def test_from_binary_unknown(self): |
| 593 |
with self.assertRaises(InvalidValue): |
| 594 |
IkeVendorId.from_binary(b'\x00\x01\x02\x03') |
| 595 |
|
| 596 |
def test_str(self): |
| 597 |
self.assertEqual( |
| 598 |
str(IkeVendorId.SSH_IPSEC_EXPRESS_1_1_0.value), |
| 599 |
'SSH Communications Security IPSEC Express version 1.1.0', |
| 600 |
) |
| 601 |
self.assertEqual( |
| 602 |
str(IkeVendorId.DRAFT_IETF_IPSEC_NAT_T_IKE_02N.value), |
| 603 |
'draft-ietf-ipsec-nat-t-ike-02\\n', |
| 604 |
) |
| 605 |
|
| 606 |
|
| 607 |
class TestIkeVersion(TestClasses.TestJsonCodeNumericBase): |
| 608 |
@classmethod |
| 609 |
def _get_class(cls): |
| 610 |
return IkeVersion |