1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Alg\Encryption; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\XMLSecurity\Alg\AbstractAlgorithmFactory; |
8
|
|
|
use SimpleSAML\XMLSecurity\Constants; |
9
|
|
|
use SimpleSAML\XMLSecurity\Key\AbstractKey; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Factory class to create and configure encryption algorithms. |
13
|
|
|
* |
14
|
|
|
* @package simplesamlphp/xml-security |
15
|
|
|
*/ |
16
|
|
|
final class EncryptionAlgorithmFactory extends AbstractAlgorithmFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* A cache of algorithm implementations indexed by algorithm ID. |
20
|
|
|
* |
21
|
|
|
* @var string[] |
22
|
|
|
*/ |
23
|
|
|
protected static array $cache = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Whether the factory has been initialized or not. |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected static bool $initialized = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* An array of blacklisted algorithms. |
34
|
|
|
* |
35
|
|
|
* Defaults to 3DES. |
36
|
|
|
* |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
protected array $blacklist = [ |
40
|
|
|
Constants::BLOCK_ENC_3DES, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Build a factory that creates encryption algorithms. |
46
|
|
|
* |
47
|
|
|
* @param array|null $blacklist A list of algorithms forbidden for their use. |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $blacklist = null) |
50
|
|
|
{ |
51
|
|
|
parent::__construct( |
52
|
|
|
$blacklist, |
53
|
|
|
[ |
54
|
|
|
TripleDES::class, |
55
|
|
|
AES::class, |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the name of the abstract class our algorithm implementations must extend. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected static function getExpectedParent(): string |
67
|
|
|
{ |
68
|
|
|
return EncryptionAlgorithmInterface::class; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get a new object implementing the given encryption algorithm. |
74
|
|
|
* |
75
|
|
|
* @param string $algId The identifier of the algorithm desired. |
76
|
|
|
* @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use with the given algorithm. |
77
|
|
|
* |
78
|
|
|
* @return \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface An object implementing the given |
79
|
|
|
* algorithm. |
80
|
|
|
* |
81
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If an error occurs, e.g. the given algorithm |
82
|
|
|
* is blacklisted, unknown or the given key is not suitable for it. |
83
|
|
|
*/ |
84
|
|
|
public function getAlgorithm(string $algId, AbstractKey $key): EncryptionAlgorithmInterface |
85
|
|
|
{ |
86
|
|
|
return parent::getAlgorithm($algId, $key); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |