|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jose\Component\Encryption\Algorithm\KeyEncryption; |
|
15
|
|
|
|
|
16
|
|
|
use Base64Url\Base64Url; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use Jose\Component\Core\JWK; |
|
19
|
|
|
use RuntimeException; |
|
20
|
|
|
|
|
21
|
|
|
abstract class AESCTR implements KeyEncryption |
|
22
|
|
|
{ |
|
23
|
|
|
public function allowedKeyTypes(): array |
|
24
|
|
|
{ |
|
25
|
|
|
return ['oct']; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws RuntimeException if the CEK cannot be encrypted |
|
30
|
|
|
*/ |
|
31
|
|
|
public function encryptKey(JWK $key, string $cek, array $completeHeader, array &$additionalHeader): string |
|
32
|
|
|
{ |
|
33
|
|
|
$k = $this->getKey($key); |
|
34
|
|
|
$iv = random_bytes(16); |
|
35
|
|
|
|
|
36
|
|
|
// We set header parameters |
|
37
|
|
|
$additionalHeader['iv'] = Base64Url::encode($iv); |
|
38
|
|
|
|
|
39
|
|
|
$result = openssl_encrypt($cek, $this->getMode(), $k, OPENSSL_RAW_DATA, $iv); |
|
40
|
|
|
if (false === $result) { |
|
41
|
|
|
throw new RuntimeException('Unable to encrypt the CEK'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws RuntimeException if the CEK cannot be decrypted |
|
49
|
|
|
*/ |
|
50
|
|
|
public function decryptKey(JWK $key, string $encrypted_cek, array $header): string |
|
51
|
|
|
{ |
|
52
|
|
|
$k = $this->getKey($key); |
|
53
|
|
|
$this->checkHeaderAdditionalParameters($header); |
|
54
|
|
|
$iv = Base64Url::decode($header['iv']); |
|
55
|
|
|
|
|
56
|
|
|
$result = openssl_decrypt($encrypted_cek, $this->getMode(), $k, OPENSSL_RAW_DATA, $iv); |
|
57
|
|
|
if (false === $result) { |
|
58
|
|
|
throw new RuntimeException('Unable to decrypt the CEK'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $result; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getKeyManagementMode(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return self::MODE_ENCRYPT; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
abstract protected function getMode(): string; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws InvalidArgumentException if the key is invalid |
|
73
|
|
|
*/ |
|
74
|
|
|
private function getKey(JWK $key): string |
|
75
|
|
|
{ |
|
76
|
|
|
if (!\in_array($key->get('kty'), $this->allowedKeyTypes(), true)) { |
|
77
|
|
|
throw new InvalidArgumentException('Wrong key type.'); |
|
78
|
|
|
} |
|
79
|
|
|
if (!$key->has('k')) { |
|
80
|
|
|
throw new InvalidArgumentException('The key parameter "k" is missing.'); |
|
81
|
|
|
} |
|
82
|
|
|
$k = $key->get('k'); |
|
83
|
|
|
if (!\is_string($k)) { |
|
84
|
|
|
throw new InvalidArgumentException('The key parameter "k" is invalid.'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return Base64Url::decode($k); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @throws InvalidArgumentException if the IV is missing or invalid |
|
92
|
|
|
*/ |
|
93
|
|
|
private function checkHeaderAdditionalParameters(array $header): void |
|
94
|
|
|
{ |
|
95
|
|
|
if (!isset($header['iv'])) { |
|
96
|
|
|
throw new InvalidArgumentException('The header parameter "iv" is missing.'); |
|
97
|
|
|
} |
|
98
|
|
|
if (!\is_string($header['iv'])) { |
|
99
|
|
|
throw new InvalidArgumentException('The header parameter "iv" is not valid.'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|