| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Encryption\Algorithm\KeyEncryption; |
| 6 |
|
| 7 |
use AESKW\A128KW; |
| 8 |
use AESKW\A192KW; |
| 9 |
use AESKW\A256KW; |
| 10 |
use AESKW\Wrapper as WrapperInterface; |
| 11 |
use InvalidArgumentException; |
| 12 |
use Jose\Component\Core\JWK; |
| 13 |
use Jose\Component\Core\Util\Base64UrlSafe; |
| 14 |
use RuntimeException; |
| 15 |
use function in_array; |
| 16 |
use function is_int; |
| 17 |
use function is_string; |
| 18 |
use function sprintf; |
| 19 |
|
| 20 |
abstract class PBES2AESKW implements KeyWrapping |
| 21 |
{ |
| 22 |
public const DEFAULT_MAX_COUNT = 1_000_000; |
| 23 |
|
| 24 |
public function __construct( |
| 25 |
private readonly int $salt_size = 64, |
| 26 |
private readonly int $nb_count = 4096, |
| 27 |
private readonly int $max_count = self::DEFAULT_MAX_COUNT |
| 28 |
) { |
| 29 |
if (! interface_exists(WrapperInterface::class)) { |
| 30 |
throw new RuntimeException('Please install "spomky-labs/aes-key-wrap" to use AES-KW algorithms'); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function allowedKeyTypes(): array |
| 35 |
{ |
| 36 |
return ['oct']; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function wrapKey(JWK $key, string $cek, array $completeHeader, array &$additionalHeader): string |
| 44 |
{ |
| 45 |
$password = $this->getKey($key); |
| 46 |
$this->checkHeaderAlgorithm($completeHeader); |
| 47 |
$wrapper = $this->getWrapper(); |
| 48 |
$hash_algorithm = $this->getHashAlgorithm(); |
| 49 |
$key_size = $this->getKeySize(); |
| 50 |
$salt = random_bytes($this->salt_size); |
| 51 |
|
| 52 |
|
| 53 |
$additionalHeader['p2s'] = Base64UrlSafe::encodeUnpadded($salt); |
| 54 |
$additionalHeader['p2c'] = $this->nb_count; |
| 55 |
|
| 56 |
$derived_key = hash_pbkdf2( |
| 57 |
$hash_algorithm, |
| 58 |
$password, |
| 59 |
$completeHeader['alg'] . "\x00" . $salt, |
| 60 |
$this->nb_count, |
| 61 |
$key_size, |
| 62 |
true |
| 63 |
); |
| 64 |
|
| 65 |
return $wrapper::wrap($derived_key, $cek); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function unwrapKey(JWK $key, string $encrypted_cek, array $completeHeader): string |
| 72 |
{ |
| 73 |
$password = $this->getKey($key); |
| 74 |
$this->checkHeaderAlgorithm($completeHeader); |
| 75 |
$this->checkHeaderAdditionalParameters($completeHeader); |
| 76 |
$wrapper = $this->getWrapper(); |
| 77 |
$hash_algorithm = $this->getHashAlgorithm(); |
| 78 |
$key_size = $this->getKeySize(); |
| 79 |
$p2s = $completeHeader['p2s']; |
| 80 |
is_string($p2s) || throw new InvalidArgumentException('Invalid salt.'); |
| 81 |
$salt = $completeHeader['alg'] . "\x00" . Base64UrlSafe::decodeNoPadding($p2s); |
| 82 |
$count = $completeHeader['p2c']; |
| 83 |
is_int($count) || throw new InvalidArgumentException('Invalid counter.'); |
| 84 |
|
| 85 |
$derived_key = hash_pbkdf2($hash_algorithm, $password, $salt, $count, $key_size, true); |
| 86 |
|
| 87 |
return $wrapper::unwrap($derived_key, $encrypted_cek); |
| 88 |
} |
| 89 |
|
| 90 |
public function getKeyManagementMode(): string |
| 91 |
{ |
| 92 |
return self::MODE_WRAP; |
| 93 |
} |
| 94 |
|
| 95 |
protected function getKey(JWK $key): string |
| 96 |
{ |
| 97 |
if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) { |
| 98 |
throw new InvalidArgumentException('Wrong key type.'); |
| 99 |
} |
| 100 |
if (! $key->has('k')) { |
| 101 |
throw new InvalidArgumentException('The key parameter "k" is missing.'); |
| 102 |
} |
| 103 |
$k = $key->get('k'); |
| 104 |
if (! is_string($k)) { |
| 105 |
throw new InvalidArgumentException('The key parameter "k" is invalid.'); |
| 106 |
} |
| 107 |
|
| 108 |
return Base64UrlSafe::decodeNoPadding($k); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
protected function checkHeaderAlgorithm(array $header): void |
| 115 |
{ |
| 116 |
if (! isset($header['alg'])) { |
| 117 |
throw new InvalidArgumentException('The header parameter "alg" is missing.'); |
| 118 |
} |
| 119 |
if (! is_string($header['alg'])) { |
| 120 |
throw new InvalidArgumentException('The header parameter "alg" is not valid.'); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
protected function checkHeaderAdditionalParameters(array $header): void |
| 128 |
{ |
| 129 |
if (! isset($header['p2s'])) { |
| 130 |
throw new InvalidArgumentException('The header parameter "p2s" is missing.'); |
| 131 |
} |
| 132 |
if (! is_string($header['p2s'])) { |
| 133 |
throw new InvalidArgumentException('The header parameter "p2s" is not valid.'); |
| 134 |
} |
| 135 |
if (! isset($header['p2c'])) { |
| 136 |
throw new InvalidArgumentException('The header parameter "p2c" is missing.'); |
| 137 |
} |
| 138 |
if (! is_int($header['p2c']) || $header['p2c'] <= 0) { |
| 139 |
throw new InvalidArgumentException('The header parameter "p2c" is not valid.'); |
| 140 |
} |
| 141 |
if ($header['p2c'] > $this->max_count) { |
| 142 |
throw new InvalidArgumentException(sprintf( |
| 143 |
'The header parameter "p2c" is too large. The maximum allowed value is %d.', |
| 144 |
$this->max_count |
| 145 |
)); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
abstract protected function getWrapper(): A256KW|A128KW|A192KW; |
| 150 |
|
| 151 |
abstract protected function getHashAlgorithm(): string; |
| 152 |
|
| 153 |
abstract protected function getKeySize(): int; |
| 154 |
} |