|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\Alg\Encryption; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use SimpleSAML\XMLSecurity\Alg\Encryption\AES; |
|
9
|
|
|
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory; |
|
10
|
|
|
use SimpleSAML\XMLSecurity\Alg\Encryption\TripleDES; |
|
11
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
|
12
|
|
|
use SimpleSAML\XMLSecurity\Exception\BlacklistedAlgorithmException; |
|
13
|
|
|
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException; |
|
14
|
|
|
use SimpleSAML\XMLSecurity\Key\SymmetricKey; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Tests for \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory. |
|
18
|
|
|
* |
|
19
|
|
|
* @package simplesamlphp/xml-security |
|
20
|
|
|
*/ |
|
21
|
|
|
class EncryptionAlgorithmFactoryTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */ |
|
24
|
|
|
protected static SymmetricKey $skey; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
public static function setUpBeforeClass(): void |
|
28
|
|
|
{ |
|
29
|
|
|
self::$skey = SymmetricKey::generate(16); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Test for unsupported algorithms. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function testGetUnknownAlgorithm(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$factory = new EncryptionAlgorithmFactory([]); |
|
39
|
|
|
$this->expectException(UnsupportedAlgorithmException::class); |
|
40
|
|
|
$factory->getAlgorithm('Unsupported algorithm identifier', self::$skey); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Test the default blacklisted algorithms. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testDefaultBlacklistedAlgorithms(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$factory = new EncryptionAlgorithmFactory(); |
|
50
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128, self::$skey); |
|
51
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
52
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES128, $algorithm->getAlgorithmId()); |
|
53
|
|
|
$this->assertEquals(self::$skey, $algorithm->getKey()); |
|
54
|
|
|
|
|
55
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128_GCM, self::$skey); |
|
56
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
57
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES128_GCM, $algorithm->getAlgorithmId()); |
|
58
|
|
|
|
|
59
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192, self::$skey); |
|
60
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
61
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES192, $algorithm->getAlgorithmId()); |
|
62
|
|
|
|
|
63
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192_GCM, self::$skey); |
|
64
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
65
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES192_GCM, $algorithm->getAlgorithmId()); |
|
66
|
|
|
|
|
67
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256, self::$skey); |
|
68
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
69
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES256, $algorithm->getAlgorithmId()); |
|
70
|
|
|
|
|
71
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256_GCM, self::$skey); |
|
72
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
73
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES256_GCM, $algorithm->getAlgorithmId()); |
|
74
|
|
|
|
|
75
|
|
|
$this->expectException(BlacklistedAlgorithmException::class); |
|
76
|
|
|
$factory->getAlgorithm(C::BLOCK_ENC_3DES, self::$skey); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Test for manually blacklisted algorithms. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function testBlacklistedAlgorithm(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$factory = new EncryptionAlgorithmFactory([C::BLOCK_ENC_AES256_GCM]); |
|
86
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_3DES, self::$skey); |
|
87
|
|
|
$this->assertInstanceOf(TripleDES::class, $algorithm); |
|
88
|
|
|
$this->assertEquals(C::BLOCK_ENC_3DES, $algorithm->getAlgorithmId()); |
|
89
|
|
|
|
|
90
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128, self::$skey); |
|
91
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
92
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES128, $algorithm->getAlgorithmId()); |
|
93
|
|
|
|
|
94
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128_GCM, self::$skey); |
|
95
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
96
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES128_GCM, $algorithm->getAlgorithmId()); |
|
97
|
|
|
|
|
98
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192, self::$skey); |
|
99
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
100
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES192, $algorithm->getAlgorithmId()); |
|
101
|
|
|
|
|
102
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192_GCM, self::$skey); |
|
103
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
104
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES192_GCM, $algorithm->getAlgorithmId()); |
|
105
|
|
|
|
|
106
|
|
|
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256, self::$skey); |
|
107
|
|
|
$this->assertInstanceOf(AES::class, $algorithm); |
|
108
|
|
|
$this->assertEquals(C::BLOCK_ENC_AES256, $algorithm->getAlgorithmId()); |
|
109
|
|
|
|
|
110
|
|
|
$this->expectException(BlacklistedAlgorithmException::class); |
|
111
|
|
|
$factory->getAlgorithm(C::BLOCK_ENC_AES256_GCM, self::$skey); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|