-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAbstractKeyTransporter.php
More file actions
112 lines (92 loc) · 2.84 KB
/
AbstractKeyTransporter.php
File metadata and controls
112 lines (92 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace SimpleSAML\XMLSecurity\Alg\KeyTransport;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XMLSecurity\Backend;
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
use SimpleSAML\XMLSecurity\Key\KeyInterface;
/**
* An abstract class that implements a generic key transport algorithm.
*
* @package simplesamlphp/xml-security
*/
abstract class AbstractKeyTransporter implements KeyTransportAlgorithmInterface
{
protected const string DEFAULT_BACKEND = Backend\OpenSSL::class;
/** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend */
protected EncryptionBackend $backend;
/**
* Build a key transport algorithm.
*
* Extend this class to implement your own key transporters.
*
* WARNING: remember to adjust the type of the key to the one that works with your algorithm!
*
* @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The encryption key.
* @param string $algId The identifier of this algorithm.
*/
public function __construct(
#[\SensitiveParameter]
private KeyInterface $key,
protected string $algId,
) {
Assert::oneOf(
$algId,
static::getSupportedAlgorithms(),
'Unsupported algorithm for ' . static::class,
UnsupportedAlgorithmException::class,
);
/** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend $backend */
$backend = new (static::DEFAULT_BACKEND)();
$this->setBackend($backend);
}
/**
*/
public function getAlgorithmId(): string
{
return $this->algId;
}
/**
* @return \SimpleSAML\XMLSecurity\Key\KeyInterface
*/
public function getKey(): KeyInterface
{
return $this->key;
}
/**
* @inheritDoc
*/
public function setBackend(?EncryptionBackend $backend): void
{
if ($backend === null) {
return;
}
$this->backend = $backend;
$this->backend->setCipher($this->algId);
}
/**
* Encrypt a given key with this cipher and the loaded key.
*
* @param string $plaintext The original key to encrypt.
*
* @return string The encrypted key (ciphertext).
*/
public function encrypt(string $plaintext): string
{
return $this->backend->encrypt($this->key, $plaintext);
}
/**
* Decrypt a given key with this cipher and the loaded key.
*
* @note The class of the returned key will depend on the algorithm it is going to be used for.
*
* @param string $ciphertext The encrypted key.
*
* @return string The decrypted key.
*/
public function decrypt(string $ciphertext): string
{
return $this->backend->decrypt($this->key, $ciphertext);
}
}