1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\PKCS5\ASN1\AlgorithmIdentifier; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
9
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
10
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier; |
11
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Cipher\BlockCipherAlgorithmIdentifier; |
12
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\SpecificAlgorithmIdentifier; |
13
|
|
|
|
14
|
|
|
/* |
15
|
|
|
From RFC 2898 - A.4 PBES2: |
16
|
|
|
|
17
|
|
|
The parameters field associated with this OID in an |
18
|
|
|
AlgorithmIdentifier shall have type PBES2-params: |
19
|
|
|
|
20
|
|
|
PBES2-params ::= SEQUENCE { |
21
|
|
|
keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, |
22
|
|
|
encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} } |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Algorithm identifier for PBES2 encryption scheme. |
27
|
|
|
* |
28
|
|
|
* @see https://tools.ietf.org/html/rfc2898#section-6.2 |
29
|
|
|
* @see https://tools.ietf.org/html/rfc2898#appendix-A.4 |
30
|
|
|
*/ |
31
|
|
|
class PBES2AlgorithmIdentifier extends PBEAlgorithmIdentifier |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* PBKDF2 algorithm identifier. |
35
|
|
|
* |
36
|
|
|
* @var PBKDF2AlgorithmIdentifier |
37
|
|
|
*/ |
38
|
|
|
protected $_kdf; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Encryption algorithm identifier. |
42
|
|
|
* |
43
|
|
|
* @var BlockCipherAlgorithmIdentifier |
44
|
|
|
*/ |
45
|
|
|
protected $_es; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor. |
49
|
|
|
* |
50
|
|
|
* @param PBKDF2AlgorithmIdentifier $kdf |
51
|
|
|
* @param BlockCipherAlgorithmIdentifier $es |
52
|
|
|
*/ |
53
|
2 |
|
public function __construct(PBKDF2AlgorithmIdentifier $kdf, |
54
|
|
|
BlockCipherAlgorithmIdentifier $es) |
55
|
|
|
{ |
56
|
2 |
|
parent::__construct($kdf->salt(), $kdf->iterationCount()); |
57
|
2 |
|
$this->_oid = self::OID_PBES2; |
58
|
2 |
|
$this->_kdf = $kdf; |
59
|
2 |
|
$this->_es = $es; |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function name(): string |
66
|
|
|
{ |
67
|
1 |
|
return 'pkcs5PBES2'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
4 |
|
public static function fromASN1Params( |
76
|
|
|
?UnspecifiedType $params = null): SpecificAlgorithmIdentifier |
77
|
|
|
{ |
78
|
4 |
|
if (!isset($params)) { |
79
|
1 |
|
throw new \UnexpectedValueException('No parameters.'); |
80
|
|
|
} |
81
|
3 |
|
$seq = $params->asSequence(); |
82
|
3 |
|
$kdf = PBKDF2AlgorithmIdentifier::fromASN1($seq->at(0)->asSequence()); |
83
|
|
|
// ensure we got proper key derivation function algorithm |
84
|
3 |
|
if (!($kdf instanceof PBKDF2AlgorithmIdentifier)) { |
85
|
1 |
|
throw new \UnexpectedValueException( |
86
|
1 |
|
'KDF algorithm ' . $kdf->oid() . ' not supported.'); |
87
|
|
|
} |
88
|
2 |
|
$es = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
89
|
|
|
// ensure we got proper encryption algorithm |
90
|
2 |
|
if (!($es instanceof BlockCipherAlgorithmIdentifier)) { |
91
|
1 |
|
throw new \UnexpectedValueException( |
92
|
1 |
|
'ES algorithm ' . $es->oid() . ' not supported.'); |
93
|
|
|
} |
94
|
1 |
|
return new self($kdf, $es); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get key derivation function algorithm identifier. |
99
|
|
|
* |
100
|
|
|
* @return PBKDF2AlgorithmIdentifier |
101
|
|
|
*/ |
102
|
3 |
|
public function kdfAlgorithmIdentifier(): PBKDF2AlgorithmIdentifier |
103
|
|
|
{ |
104
|
3 |
|
return $this->_kdf; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get encryption scheme algorithm identifier. |
109
|
|
|
* |
110
|
|
|
* @return BlockCipherAlgorithmIdentifier |
111
|
|
|
*/ |
112
|
3 |
|
public function esAlgorithmIdentifier(): BlockCipherAlgorithmIdentifier |
113
|
|
|
{ |
114
|
3 |
|
return $this->_es; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
* |
120
|
|
|
* @return Sequence |
121
|
|
|
*/ |
122
|
1 |
|
protected function _paramsASN1(): ?Element |
123
|
|
|
{ |
124
|
1 |
|
return new Sequence($this->_kdf->toASN1(), $this->_es->toASN1()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|