1 | <?php |
||
21 | abstract class PBES2AESKW implements KeyWrappingInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $salt_size; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $nb_count; |
||
32 | |||
33 | /** |
||
34 | * @param int $salt_size |
||
35 | * @param int $nb_count |
||
36 | */ |
||
37 | public function __construct($salt_size = 64, $nb_count = 4096) |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function wrapKey(JWKInterface $key, $cek, array $complete_headers, array &$additional_headers) |
||
47 | { |
||
48 | $this->checkKey($key); |
||
49 | $this->checkHeaderAlgorithm($complete_headers); |
||
50 | $wrapper = $this->getWrapper(); |
||
51 | $hash_algorithm = $this->getHashAlgorithm(); |
||
52 | $key_size = $this->getKeySize(); |
||
53 | $salt = StringUtil::generateRandomBytes($this->salt_size); |
||
54 | $password = Base64Url::decode($key->get('k')); |
||
55 | |||
56 | // We set headers parameters |
||
57 | $additional_headers['p2s'] = Base64Url::encode($salt); |
||
58 | $additional_headers['p2c'] = $this->nb_count; |
||
59 | |||
60 | $derived_key = hash_pbkdf2($hash_algorithm, $password, $complete_headers['alg']."\x00".$salt, $this->nb_count, $key_size, true); |
||
61 | |||
62 | return $wrapper->wrap($derived_key, $cek); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function unwrapKey(JWKInterface $key, $encrypted_cek, array $header) |
||
69 | { |
||
70 | $this->checkKey($key); |
||
71 | $this->checkHeaderAlgorithm($header); |
||
72 | $this->checkHeaderAdditionalParameters($header); |
||
73 | $wrapper = $this->getWrapper(); |
||
74 | $hash_algorithm = $this->getHashAlgorithm(); |
||
75 | $key_size = $this->getKeySize(); |
||
76 | $salt = $header['alg']."\x00".Base64Url::decode($header['p2s']); |
||
77 | $count = $header['p2c']; |
||
78 | $password = Base64Url::decode($key->get('k')); |
||
79 | |||
80 | $derived_key = hash_pbkdf2($hash_algorithm, $password, $salt, $count, $key_size, true); |
||
81 | |||
82 | return $wrapper->unwrap($derived_key, $encrypted_cek); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function getKeyManagementMode() |
||
92 | |||
93 | /** |
||
94 | * @param JWKInterface $key |
||
95 | */ |
||
96 | protected function checkKey(JWKInterface $key) |
||
102 | |||
103 | /** |
||
104 | * @param array $header |
||
105 | */ |
||
106 | protected function checkHeaderAlgorithm(array $header) |
||
112 | |||
113 | /** |
||
114 | * @param array $header |
||
115 | */ |
||
116 | protected function checkHeaderAdditionalParameters(array $header) |
||
122 | |||
123 | /** |
||
124 | * @return \AESKW\A128KW|\AESKW\A192KW|\AESKW\A256KW |
||
125 | */ |
||
126 | abstract protected function getWrapper(); |
||
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | abstract protected function getHashAlgorithm(); |
||
132 | |||
133 | /** |
||
134 | * @return int |
||
135 | */ |
||
136 | abstract protected function getKeySize(); |
||
137 | } |
||
138 |