1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWE\KeyAlgorithm; |
6
|
|
|
|
7
|
|
|
use Sop\JWX\JWA\JWA; |
8
|
|
|
use Sop\JWX\JWE\KeyManagementAlgorithm; |
9
|
|
|
use Sop\JWX\JWK\JWK; |
10
|
|
|
use Sop\JWX\JWK\Symmetric\SymmetricKeyJWK; |
11
|
|
|
use Sop\JWX\JWT\Header\Header; |
12
|
|
|
use Sop\JWX\JWT\Parameter\AlgorithmParameter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Algorithm to carry CEK in plaintext. |
16
|
|
|
* |
17
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-4.5 |
18
|
|
|
*/ |
19
|
|
|
class DirectCEKAlgorithm extends KeyManagementAlgorithm |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Content encryption key. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $_cek; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $cek Content encryption key |
32
|
|
|
*/ |
33
|
16 |
|
public function __construct(string $cek) |
34
|
|
|
{ |
35
|
16 |
|
$this->_cek = $cek; |
36
|
16 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initialize from JWK. |
40
|
|
|
* |
41
|
|
|
* @param JWK $jwk |
42
|
|
|
* @param Header $header |
43
|
|
|
* |
44
|
|
|
* @throws \UnexpectedValueException |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
12 |
|
public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
49
|
|
|
{ |
50
|
12 |
|
$jwk = SymmetricKeyJWK::fromJWK($jwk); |
51
|
12 |
|
$alg = JWA::deriveAlgorithmName($header); |
52
|
12 |
|
if (JWA::ALGO_DIR !== $alg) { |
53
|
1 |
|
throw new \UnexpectedValueException("Invalid algorithm '{$alg}'."); |
54
|
|
|
} |
55
|
11 |
|
return new self($jwk->key()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get content encryption key. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
2 |
|
public function cek(): string |
64
|
|
|
{ |
65
|
2 |
|
return $this->_cek; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
8 |
|
public function cekForEncryption(int $length): string |
72
|
|
|
{ |
73
|
8 |
|
if (strlen($this->_cek) !== $length) { |
74
|
1 |
|
throw new \UnexpectedValueException('Invalid key length.'); |
75
|
|
|
} |
76
|
7 |
|
return $this->_cek; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
18 |
|
public function algorithmParamValue(): string |
83
|
|
|
{ |
84
|
18 |
|
return JWA::ALGO_DIR; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
8 |
|
public function headerParameters(): array |
91
|
|
|
{ |
92
|
8 |
|
return array_merge(parent::headerParameters(), |
93
|
8 |
|
[AlgorithmParameter::fromAlgorithm($this)]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
8 |
|
protected function _encryptKey(string $key, Header &$header): string |
100
|
|
|
{ |
101
|
8 |
|
if ($key !== $this->_cek) { |
102
|
1 |
|
throw new \LogicException("Content encryption key doesn't match."); |
103
|
|
|
} |
104
|
7 |
|
return ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
10 |
|
protected function _decryptKey(string $ciphertext, Header $header): string |
111
|
|
|
{ |
112
|
10 |
|
if ('' !== $ciphertext) { |
113
|
1 |
|
throw new \UnexpectedValueException( |
114
|
1 |
|
'Encrypted key must be an empty octet sequence.'); |
115
|
|
|
} |
116
|
9 |
|
return $this->_cek; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|