| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Encryption\Algorithm\KeyEncryption; |
| 6 |
|
| 7 |
use AESKW\Wrapper as WrapperInterface; |
| 8 |
use InvalidArgumentException; |
| 9 |
use Jose\Component\Core\JWK; |
| 10 |
use Jose\Component\Core\Util\Base64UrlSafe; |
| 11 |
use RuntimeException; |
| 12 |
use function extension_loaded; |
| 13 |
use function in_array; |
| 14 |
use function is_string; |
| 15 |
use const OPENSSL_RAW_DATA; |
| 16 |
|
| 17 |
abstract class AESGCMKW implements KeyWrapping |
| 18 |
{ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
if (! extension_loaded('openssl')) { |
| 22 |
throw new RuntimeException('Please install the OpenSSL extension'); |
| 23 |
} |
| 24 |
if (! interface_exists(WrapperInterface::class)) { |
| 25 |
throw new RuntimeException('Please install "spomky-labs/aes-key-wrap" to use AES-KW algorithms'); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function allowedKeyTypes(): array |
| 30 |
{ |
| 31 |
return ['oct']; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function wrapKey(JWK $key, string $cek, array $completeHeader, array &$additionalHeader): string |
| 39 |
{ |
| 40 |
$kek = $this->getKey($key); |
| 41 |
$iv = random_bytes(96 / 8); |
| 42 |
$additionalHeader['iv'] = Base64UrlSafe::encodeUnpadded($iv); |
| 43 |
|
| 44 |
$mode = sprintf('aes-%d-gcm', $this->getKeySize()); |
| 45 |
$tag = ''; |
| 46 |
$encrypted_cek = openssl_encrypt($cek, $mode, $kek, OPENSSL_RAW_DATA, $iv, $tag, ''); |
| 47 |
if ($encrypted_cek === false) { |
| 48 |
throw new RuntimeException('Unable to encrypt the CEK'); |
| 49 |
} |
| 50 |
$additionalHeader['tag'] = Base64UrlSafe::encodeUnpadded($tag); |
| 51 |
|
| 52 |
return $encrypted_cek; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function unwrapKey(JWK $key, string $encrypted_cek, array $completeHeader): string |
| 59 |
{ |
| 60 |
$kek = $this->getKey($key); |
| 61 |
(isset($completeHeader['iv']) && is_string($completeHeader['iv'])) || throw new InvalidArgumentException( |
| 62 |
'Parameter "iv" is missing.' |
| 63 |
); |
| 64 |
(isset($completeHeader['tag']) && is_string($completeHeader['tag'])) || throw new InvalidArgumentException( |
| 65 |
'Parameter "tag" is missing.' |
| 66 |
); |
| 67 |
|
| 68 |
$tag = Base64UrlSafe::decodeNoPadding($completeHeader['tag']); |
| 69 |
$iv = Base64UrlSafe::decodeNoPadding($completeHeader['iv']); |
| 70 |
|
| 71 |
$mode = sprintf('aes-%d-gcm', $this->getKeySize()); |
| 72 |
$cek = openssl_decrypt($encrypted_cek, $mode, $kek, OPENSSL_RAW_DATA, $iv, $tag, ''); |
| 73 |
if ($cek === false) { |
| 74 |
throw new RuntimeException('Unable to decrypt the CEK'); |
| 75 |
} |
| 76 |
|
| 77 |
return $cek; |
| 78 |
} |
| 79 |
|
| 80 |
public function getKeyManagementMode(): string |
| 81 |
{ |
| 82 |
return self::MODE_WRAP; |
| 83 |
} |
| 84 |
|
| 85 |
protected function getKey(JWK $key): string |
| 86 |
{ |
| 87 |
if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) { |
| 88 |
throw new InvalidArgumentException('Wrong key type.'); |
| 89 |
} |
| 90 |
if (! $key->has('k')) { |
| 91 |
throw new InvalidArgumentException('The key parameter "k" is missing.'); |
| 92 |
} |
| 93 |
$k = $key->get('k'); |
| 94 |
if (! is_string($k)) { |
| 95 |
throw new InvalidArgumentException('The key parameter "k" is invalid.'); |
| 96 |
} |
| 97 |
|
| 98 |
return Base64UrlSafe::decodeNoPadding($k); |
| 99 |
} |
| 100 |
|
| 101 |
abstract protected function getKeySize(): int; |
| 102 |
} |