|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2015 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jose\Algorithm\KeyEncryption; |
|
13
|
|
|
|
|
14
|
|
|
use Base64Url\Base64Url; |
|
15
|
|
|
use Jose\Object\JWKInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class AESKW. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AESKW implements KeyEncryptionInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param \Jose\Object\JWKInterface $key |
|
24
|
|
|
* @param string $cek |
|
25
|
|
|
* @param array $header |
|
26
|
|
|
* |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
public function encryptKey(JWKInterface $key, $cek, array &$header) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->checkKey($key); |
|
32
|
|
|
$wrapper = $this->getWrapper(); |
|
33
|
|
|
|
|
34
|
|
|
return $wrapper->wrap(Base64Url::decode($key->get('k')), $cek); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \Jose\Object\JWKInterface $key |
|
39
|
|
|
* @param string $encryted_cek |
|
40
|
|
|
* @param array $header |
|
41
|
|
|
* |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
*/ |
|
44
|
|
|
public function decryptKey(JWKInterface $key, $encryted_cek, array $header) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->checkKey($key); |
|
47
|
|
|
$wrapper = $this->getWrapper(); |
|
48
|
|
|
|
|
49
|
|
|
return $wrapper->unwrap(Base64Url::decode($key->get('k')), $encryted_cek); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param \Jose\Object\JWKInterface $key |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function checkKey(JWKInterface $key) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$key->has('kty') || 'oct' !== $key->get('kty') || !$key->has('k')) { |
|
58
|
|
|
throw new \InvalidArgumentException('The key is not valid'); |
|
59
|
|
|
} |
|
60
|
|
|
if ($this->getKeySize() !== strlen(Base64Url::decode($key->get('k')))) { |
|
61
|
|
|
throw new \InvalidArgumentException('The key size is not valid'); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return int |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract protected function getKeySize(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return \AESKW\A128KW|\AESKW\A192KW|\AESKW\A256KW |
|
72
|
|
|
*/ |
|
73
|
|
|
abstract protected function getWrapper(); |
|
74
|
|
|
} |
|
75
|
|
|
|