|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Alg\Encryption; |
|
6
|
|
|
|
|
7
|
|
|
use SimpleSAML\Assert\Assert; |
|
8
|
|
|
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend; |
|
9
|
|
|
use SimpleSAML\XMLSecurity\Exception\RuntimeException; |
|
10
|
|
|
use SimpleSAML\XMLSecurity\Key\AbstractKey; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* An abstract class that implements a generic encryption algorithm |
|
14
|
|
|
* |
|
15
|
|
|
* @package simplesamlphp/xml-security |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractEncryptor implements EncryptionAlgorithmInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var \SimpleSAML\XMLSecurity\Key\AbstractKey */ |
|
20
|
|
|
private AbstractKey $key; |
|
21
|
|
|
|
|
22
|
|
|
/** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend */ |
|
23
|
|
|
protected EncryptionBackend $backend; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected string $default_backend; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected string $algId; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Build an encryption algorithm. |
|
34
|
|
|
* |
|
35
|
|
|
* Extend this class to implement your own encryptors. |
|
36
|
|
|
* |
|
37
|
|
|
* WARNING: remember to adjust the type of the key to the one that works with your algorithm! |
|
38
|
|
|
* |
|
39
|
|
|
* @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The signing key. |
|
40
|
|
|
* @param string $algId The identifier of this algorithm. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(AbstractKey $key, string $algId) |
|
43
|
|
|
{ |
|
44
|
|
|
Assert::oneOf( |
|
45
|
|
|
$algId, |
|
46
|
|
|
static::getSupportedAlgorithms(), |
|
47
|
|
|
'Unsupported algorithm for ' . static::class, |
|
48
|
|
|
RuntimeException::class |
|
|
|
|
|
|
49
|
|
|
); |
|
50
|
|
|
$this->key = $key; |
|
51
|
|
|
$this->algId = $algId; |
|
52
|
|
|
$this->setBackend(new $this->default_backend()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getAlgorithmId(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->algId; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return \SimpleSAML\XMLSecurity\Key\AbstractKey |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getKey(): AbstractKey |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->key; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritDoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setBackend(?EncryptionBackend $backend): void |
|
78
|
|
|
{ |
|
79
|
|
|
if ($backend === null) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->backend = $backend; |
|
84
|
|
|
$this->backend->setCipher($this->algId); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Encrypt a given plaintext with the current algorithm and key. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $plaintext The plaintext to encrypt. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string The (binary) ciphertext. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function encrypt(string $plaintext): string |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->backend->encrypt($this->key, $plaintext); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Decrypt a given ciphertext with the current algorithm and key. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string The (binary) ciphertext to decrypt. |
|
|
|
|
|
|
105
|
|
|
* |
|
106
|
|
|
* @return string The decrypted plaintext. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function decrypt(string $ciphertext): string |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->backend->decrypt($this->key, $ciphertext); |
|
111
|
|
|
} |
|
112
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.