1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWE; |
4
|
|
|
|
5
|
|
|
use JWX\JWE\KeyAlgorithm\KeyAlgorithmFactory; |
6
|
|
|
use JWX\JWK\JWK; |
7
|
|
|
use JWX\JWT\Header\Header; |
8
|
|
|
use JWX\JWT\Header\HeaderParameters; |
9
|
|
|
use JWX\JWT\Parameter\AlgorithmParameterValue; |
10
|
|
|
use JWX\JWT\Parameter\JWTParameter; |
11
|
|
|
use JWX\JWT\Parameter\KeyIDParameter; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Base class for algorithms used for CEK management for the content encryption |
16
|
|
|
* algorithms. |
17
|
|
|
*/ |
18
|
|
|
abstract class KeyManagementAlgorithm implements |
19
|
|
|
AlgorithmParameterValue, |
|
|
|
|
20
|
|
|
HeaderParameters |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* ID of the key used by the algorithm. |
24
|
|
|
* |
25
|
|
|
* If set, KeyID parameter shall be automatically inserted into JWE's |
26
|
|
|
* header. |
27
|
|
|
* |
28
|
|
|
* @var string|null $_keyID |
29
|
|
|
*/ |
30
|
|
|
protected $_keyID; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Encrypt a key. |
34
|
|
|
* |
35
|
|
|
* @param string $key Key to be encrypted |
36
|
|
|
* @param Header $header Reference to the Header variable, that shall |
37
|
|
|
* be updated to contain parameters specific to the encryption |
38
|
|
|
* @return string Ciphertext |
39
|
|
|
*/ |
40
|
|
|
abstract protected function _encryptKey($key, Header &$header); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Decrypt a key. |
44
|
|
|
* |
45
|
|
|
* @param string $ciphertext Ciphertext of the encrypted key |
46
|
|
|
* @param Header $header Header possibly containing encoding specific |
47
|
|
|
* parameters |
48
|
|
|
* @return string Plaintext key |
49
|
|
|
*/ |
50
|
|
|
abstract protected function _decryptKey($ciphertext, Header $header); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Encrypt a key to be inserted into JWE header. |
54
|
|
|
* |
55
|
|
|
* @param string $cek Content encryption key |
56
|
|
|
* @param Header|null $header Optional reference to the Header variable, |
57
|
|
|
* which may be updated to contain parameters specific to this |
58
|
|
|
* encrypt invocation. If the variable is referenced, but is a null, |
59
|
|
|
* it shall be initialized to an empty Header. |
60
|
|
|
* @throws \RuntimeException For generic errors |
61
|
|
|
* @return string Encrypted key |
62
|
|
|
*/ |
63
|
49 |
|
final public function encrypt($cek, Header &$header = null) { |
64
|
49 |
|
if (!isset($header)) { |
65
|
34 |
|
$header = new Header(); |
66
|
34 |
|
} |
67
|
49 |
|
return $this->_encryptKey($cek, $header); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Decrypt a CEK from the encrypted data. |
72
|
|
|
* |
73
|
|
|
* @param string $data Encrypted key |
74
|
|
|
* @param Header|null Optional header containing parameters required to |
75
|
|
|
* decrypt the key. |
76
|
|
|
* @throws \RuntimeException For generic errors |
77
|
|
|
* @return string Content encryption key |
78
|
|
|
*/ |
79
|
44 |
|
final public function decrypt($data, Header $header = null) { |
80
|
44 |
|
if (!isset($header)) { |
81
|
40 |
|
$header = new Header(); |
82
|
40 |
|
} |
83
|
44 |
|
return $this->_decryptKey($data, $header); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get content encryption key for the encryption. |
88
|
|
|
* |
89
|
|
|
* Returned key may be random depending on the key management algorithm. |
90
|
|
|
* |
91
|
|
|
* @param int $length Required key size in bytes |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
abstract public function cekForEncryption($length); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Initialize key management algorithm from a JWK and a header. |
98
|
|
|
* |
99
|
|
|
* @param JWK $jwk |
100
|
|
|
* @param Header $header |
101
|
|
|
* @return KeyManagementAlgorithm |
102
|
|
|
*/ |
103
|
3 |
|
public static function fromJWK(JWK $jwk, Header $header) { |
104
|
3 |
|
$factory = new KeyAlgorithmFactory($header); |
105
|
3 |
|
return $factory->algoByKey($jwk); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get self with key ID. |
110
|
|
|
* |
111
|
|
|
* @param string|null $id Key ID or null to remove |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
3 |
|
public function withKeyID($id) { |
115
|
3 |
|
$obj = clone $this; |
116
|
3 |
|
$obj->_keyID = $id; |
117
|
3 |
|
return $obj; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
123
|
|
|
* @return JWTParameter[] |
124
|
|
|
*/ |
125
|
19 |
View Code Duplication |
public function headerParameters() { |
|
|
|
|
126
|
19 |
|
$params = array(); |
127
|
19 |
|
if (isset($this->_keyID)) { |
128
|
3 |
|
$params[] = new KeyIDParameter($this->_keyID); |
129
|
3 |
|
} |
130
|
19 |
|
return $params; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|