Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Encryption/Algorithm/KeyEncryption/AESKW.php

raw symbols SCIP
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 in_array;
13 use function is_string;
14
15 abstract class AESKW implements KeyWrapping
16 {
17 public function __construct()
18 {
19 if (! interface_exists(WrapperInterface::class)) {
20 throw new RuntimeException('Please install "spomky-labs/aes-key-wrap" to use AES-KW algorithms');
21 }
22 }
23
24 public function allowedKeyTypes(): array
25 {
26 return ['oct'];
27 }
28
29 /**
30 * @param array<string, mixed> $completeHeader
31 * @param array<string, mixed> $additionalHeader
32 */
33 public function wrapKey(JWK $key, string $cek, array $completeHeader, array &$additionalHeader): string
34 {
35 $k = $this->getKey($key);
36 $wrapper = $this->getWrapper();
37
38 return $wrapper::wrap($k, $cek);
39 }
40
41 /**
42 * @param array<string, mixed> $completeHeader
43 */
44 public function unwrapKey(JWK $key, string $encrypted_cek, array $completeHeader): string
45 {
46 $k = $this->getKey($key);
47 $wrapper = $this->getWrapper();
48
49 return $wrapper::unwrap($k, $encrypted_cek);
50 }
51
52 public function getKeyManagementMode(): string
53 {
54 return self::MODE_WRAP;
55 }
56
57 abstract protected function getWrapper(): WrapperInterface;
58
59 private function getKey(JWK $key): string
60 {
61 if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
62 throw new InvalidArgumentException('Wrong key type.');
63 }
64 if (! $key->has('k')) {
65 throw new InvalidArgumentException('The key parameter "k" is missing.');
66 }
67 $k = $key->get('k');
68 if (! is_string($k)) {
69 throw new InvalidArgumentException('The key parameter "k" is invalid.');
70 }
71
72 return Base64UrlSafe::decodeNoPadding($k);
73 }
74 }