|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jose\Component\Encryption\Algorithm\KeyEncryption; |
|
15
|
|
|
|
|
16
|
|
|
use Jose\Component\Core\JWK; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ECDHESAESKW. |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class ECDHESAESKW implements KeyAgreementWrappingInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function allowedKeyTypes(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return ['EC', 'OKP']; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function wrapAgreementKey(JWK $receiver_key, string $cek, int $encryption_key_length, array $complete_header, array &$additional_header_values): string |
|
35
|
|
|
{ |
|
36
|
|
|
$ecdh_es = new ECDHES(); |
|
37
|
|
|
|
|
38
|
|
|
$agreement_key = $ecdh_es->getAgreementKey($this->getKeyLength(), $this->name(), $receiver_key->toPublic(), $complete_header, $additional_header_values); |
|
39
|
|
|
$wrapper = $this->getWrapper(); |
|
40
|
|
|
|
|
41
|
|
|
return $wrapper::wrap($agreement_key, $cek); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function unwrapAgreementKey(JWK $receiver_key, string $encrypted_cek, int $encryption_key_length, array $complete_header): string |
|
48
|
|
|
{ |
|
49
|
|
|
$ecdh_es = new ECDHES(); |
|
50
|
|
|
|
|
51
|
|
|
$agreement_key = $ecdh_es->getAgreementKey($this->getKeyLength(), $this->name(), $receiver_key, $complete_header); |
|
52
|
|
|
$wrapper = $this->getWrapper(); |
|
53
|
|
|
|
|
54
|
|
|
return $wrapper::unwrap($agreement_key, $encrypted_cek); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getKeyManagementMode(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return self::MODE_WRAP; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return \AESKW\A128KW|\AESKW\A192KW|\AESKW\A256KW |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract protected function getWrapper(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
abstract protected function getKeyLength(): int; |
|
74
|
|
|
} |
|
75
|
|
|
|