1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\Certificate; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\Certificate\Exception\InvalidKeyUsageException; |
10
|
|
|
use SimpleSAML\SAML2\Exception\InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
use function array_key_exists; |
13
|
|
|
use function is_string; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Simple DTO wrapper for (X509) keys. Implements ArrayAccess |
17
|
|
|
* for easier backwards compatibility. |
18
|
|
|
*/ |
19
|
|
|
class Key implements ArrayAccess |
20
|
|
|
{ |
21
|
|
|
// Possible key usages |
22
|
|
|
public const USAGE_SIGNING = 'signing'; |
23
|
|
|
|
24
|
|
|
public const USAGE_ENCRYPTION = 'encryption'; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
protected array $keyData = []; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $keyData |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $keyData) |
34
|
|
|
{ |
35
|
|
|
// forcing usage of offsetSet |
36
|
|
|
foreach ($keyData as $property => $value) { |
37
|
|
|
$this->offsetSet($property, $value); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Whether or not the key is configured to be used for usage given |
44
|
|
|
* |
45
|
|
|
* @param string $usage |
46
|
|
|
* @return bool |
47
|
|
|
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function canBeUsedFor(string $usage): bool |
50
|
|
|
{ |
51
|
|
|
Assert::oneOf( |
52
|
|
|
$usage, |
53
|
|
|
[self::USAGE_ENCRYPTION, self::USAGE_SIGNING], |
54
|
|
|
'Invalid key usage given: "%s", usages "%2$s" allowed', |
55
|
|
|
InvalidKeyUsageException::class, |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return isset($this->keyData[$usage]) && $this->keyData[$usage]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $offset |
64
|
|
|
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function offsetExists(mixed $offset): bool |
68
|
|
|
{ |
69
|
|
|
if (!is_string($offset)) { |
70
|
|
|
throw InvalidArgumentException::invalidType('string', $offset); |
71
|
|
|
} |
72
|
|
|
return array_key_exists($offset, $this->keyData); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $offset |
78
|
|
|
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
public function offsetGet($offset): mixed |
82
|
|
|
{ |
83
|
|
|
if (!is_string($offset)) { |
84
|
|
|
throw InvalidArgumentException::invalidType('string', $offset); |
85
|
|
|
} |
86
|
|
|
return $this->keyData[$offset]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $offset |
92
|
|
|
* @param mixed $value |
93
|
|
|
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
public function offsetSet(mixed $offset, mixed $value): void |
96
|
|
|
{ |
97
|
|
|
if (!is_string($offset)) { |
98
|
|
|
throw InvalidArgumentException::invalidType('string', $offset); |
99
|
|
|
} |
100
|
|
|
$this->keyData[$offset] = $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param mixed $offset |
106
|
|
|
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException |
107
|
|
|
*/ |
108
|
|
|
public function offsetUnset(mixed $offset): void |
109
|
|
|
{ |
110
|
|
|
if (!is_string($offset)) { |
111
|
|
|
throw InvalidArgumentException::invalidType('string', $offset); |
112
|
|
|
} |
113
|
|
|
unset($this->keyData[$offset]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|