|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\CryptoTypes\Asymmetric\RSA; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
|
8
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
|
9
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
|
10
|
|
|
use Sop\CryptoEncoding\PEM; |
|
11
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\RSAEncryptionAlgorithmIdentifier; |
|
12
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType; |
|
13
|
|
|
use Sop\CryptoTypes\Asymmetric\PrivateKey; |
|
14
|
|
|
use Sop\CryptoTypes\Asymmetric\PublicKey; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Implements PKCS #1 RSAPrivateKey ASN.1 type. |
|
18
|
|
|
* |
|
19
|
|
|
* @see https://tools.ietf.org/html/rfc2437#section-11.1.2 |
|
20
|
|
|
*/ |
|
21
|
|
|
class RSAPrivateKey extends PrivateKey |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Modulus as a base 10 integer. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $_modulus; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Public exponent as a base 10 integer. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_publicExponent; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Private exponent as a base 10 integer. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $_privateExponent; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* First prime factor as a base 10 integer. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $_prime1; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Second prime factor as a base 10 integer. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $_prime2; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* First factor exponent as a base 10 integer. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $_exponent1; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Second factor exponent as a base 10 integer. |
|
67
|
|
|
* |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $_exponent2; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* CRT coefficient of the second factor as a base 10 integer. |
|
74
|
|
|
* |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $_coefficient; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Constructor. |
|
81
|
|
|
* |
|
82
|
|
|
* @param int|string $n Modulus |
|
83
|
|
|
* @param int|string $e Public exponent |
|
84
|
|
|
* @param int|string $d Private exponent |
|
85
|
|
|
* @param int|string $p First prime factor |
|
86
|
|
|
* @param int|string $q Second prime factor |
|
87
|
|
|
* @param int|string $dp First factor exponent |
|
88
|
|
|
* @param int|string $dq Second factor exponent |
|
89
|
|
|
* @param int|string $qi CRT coefficient of the second factor |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public function __construct($n, $e, $d, $p, $q, $dp, $dq, $qi) |
|
92
|
|
|
{ |
|
93
|
9 |
|
$this->_modulus = strval($n); |
|
94
|
9 |
|
$this->_publicExponent = strval($e); |
|
95
|
9 |
|
$this->_privateExponent = strval($d); |
|
96
|
9 |
|
$this->_prime1 = strval($p); |
|
97
|
9 |
|
$this->_prime2 = strval($q); |
|
98
|
9 |
|
$this->_exponent1 = strval($dp); |
|
99
|
9 |
|
$this->_exponent2 = strval($dq); |
|
100
|
9 |
|
$this->_coefficient = strval($qi); |
|
101
|
9 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Initialize from ASN.1. |
|
105
|
|
|
* |
|
106
|
|
|
* @param Sequence $seq |
|
107
|
|
|
* |
|
108
|
|
|
* @throws \UnexpectedValueException |
|
109
|
|
|
* |
|
110
|
|
|
* @return self |
|
111
|
|
|
*/ |
|
112
|
10 |
|
public static function fromASN1(Sequence $seq): RSAPrivateKey |
|
113
|
|
|
{ |
|
114
|
10 |
|
$version = $seq->at(0)->asInteger()->intNumber(); |
|
115
|
10 |
|
if (0 !== $version) { |
|
116
|
1 |
|
throw new \UnexpectedValueException('Version must be 0.'); |
|
117
|
|
|
} |
|
118
|
|
|
// helper function get integer from given index |
|
119
|
|
|
$get_int = function ($idx) use ($seq) { |
|
120
|
9 |
|
return $seq->at($idx)->asInteger()->number(); |
|
121
|
9 |
|
}; |
|
122
|
9 |
|
$n = $get_int(1); |
|
123
|
9 |
|
$e = $get_int(2); |
|
124
|
9 |
|
$d = $get_int(3); |
|
125
|
9 |
|
$p = $get_int(4); |
|
126
|
9 |
|
$q = $get_int(5); |
|
127
|
9 |
|
$dp = $get_int(6); |
|
128
|
9 |
|
$dq = $get_int(7); |
|
129
|
9 |
|
$qi = $get_int(8); |
|
130
|
9 |
|
return new self($n, $e, $d, $p, $q, $dp, $dq, $qi); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Initialize from DER data. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $data |
|
137
|
|
|
* |
|
138
|
|
|
* @return self |
|
139
|
|
|
*/ |
|
140
|
9 |
|
public static function fromDER(string $data): RSAPrivateKey |
|
141
|
|
|
{ |
|
142
|
9 |
|
return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @see PrivateKey::fromPEM() |
|
147
|
|
|
* |
|
148
|
|
|
* @param PEM $pem |
|
149
|
|
|
* |
|
150
|
|
|
* @throws \UnexpectedValueException |
|
151
|
|
|
* |
|
152
|
|
|
* @return self |
|
153
|
|
|
*/ |
|
154
|
4 |
|
public static function fromPEM(PEM $pem): RSAPrivateKey |
|
155
|
|
|
{ |
|
156
|
4 |
|
$pk = parent::fromPEM($pem); |
|
157
|
3 |
|
if (!($pk instanceof self)) { |
|
158
|
1 |
|
throw new \UnexpectedValueException('Not an RSA private key.'); |
|
159
|
|
|
} |
|
160
|
2 |
|
return $pk; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get modulus. |
|
165
|
|
|
* |
|
166
|
|
|
* @return string Base 10 integer |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function modulus(): string |
|
169
|
|
|
{ |
|
170
|
1 |
|
return $this->_modulus; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get public exponent. |
|
175
|
|
|
* |
|
176
|
|
|
* @return string Base 10 integer |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function publicExponent(): string |
|
179
|
|
|
{ |
|
180
|
1 |
|
return $this->_publicExponent; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get private exponent. |
|
185
|
|
|
* |
|
186
|
|
|
* @return string Base 10 integer |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function privateExponent(): string |
|
189
|
|
|
{ |
|
190
|
1 |
|
return $this->_privateExponent; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get first prime factor. |
|
195
|
|
|
* |
|
196
|
|
|
* @return string Base 10 integer |
|
197
|
|
|
*/ |
|
198
|
1 |
|
public function prime1(): string |
|
199
|
|
|
{ |
|
200
|
1 |
|
return $this->_prime1; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get second prime factor. |
|
205
|
|
|
* |
|
206
|
|
|
* @return string Base 10 integer |
|
207
|
|
|
*/ |
|
208
|
1 |
|
public function prime2(): string |
|
209
|
|
|
{ |
|
210
|
1 |
|
return $this->_prime2; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Get first factor exponent. |
|
215
|
|
|
* |
|
216
|
|
|
* @return string Base 10 integer |
|
217
|
|
|
*/ |
|
218
|
1 |
|
public function exponent1(): string |
|
219
|
|
|
{ |
|
220
|
1 |
|
return $this->_exponent1; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get second factor exponent. |
|
225
|
|
|
* |
|
226
|
|
|
* @return string Base 10 integer |
|
227
|
|
|
*/ |
|
228
|
1 |
|
public function exponent2(): string |
|
229
|
|
|
{ |
|
230
|
1 |
|
return $this->_exponent2; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Get CRT coefficient of the second factor. |
|
235
|
|
|
* |
|
236
|
|
|
* @return string Base 10 integer |
|
237
|
|
|
*/ |
|
238
|
1 |
|
public function coefficient(): string |
|
239
|
|
|
{ |
|
240
|
1 |
|
return $this->_coefficient; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* {@inheritdoc} |
|
245
|
|
|
*/ |
|
246
|
2 |
|
public function algorithmIdentifier(): AlgorithmIdentifierType |
|
247
|
|
|
{ |
|
248
|
2 |
|
return new RSAEncryptionAlgorithmIdentifier(); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* {@inheritdoc} |
|
253
|
|
|
* |
|
254
|
|
|
* @return RSAPublicKey |
|
255
|
|
|
*/ |
|
256
|
2 |
|
public function publicKey(): PublicKey |
|
257
|
|
|
{ |
|
258
|
2 |
|
return new RSAPublicKey($this->_modulus, $this->_publicExponent); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Generate ASN.1 structure. |
|
263
|
|
|
* |
|
264
|
|
|
* @return Sequence |
|
265
|
|
|
*/ |
|
266
|
3 |
|
public function toASN1(): Sequence |
|
267
|
|
|
{ |
|
268
|
3 |
|
return new Sequence(new Integer(0), |
|
269
|
3 |
|
new Integer($this->_modulus), |
|
270
|
3 |
|
new Integer($this->_publicExponent), |
|
271
|
3 |
|
new Integer($this->_privateExponent), |
|
272
|
3 |
|
new Integer($this->_prime1), |
|
273
|
3 |
|
new Integer($this->_prime2), |
|
274
|
3 |
|
new Integer($this->_exponent1), |
|
275
|
3 |
|
new Integer($this->_exponent2), |
|
276
|
3 |
|
new Integer($this->_coefficient)); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* {@inheritdoc} |
|
281
|
|
|
*/ |
|
282
|
3 |
|
public function toDER(): string |
|
283
|
|
|
{ |
|
284
|
3 |
|
return $this->toASN1()->toDER(); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* {@inheritdoc} |
|
289
|
|
|
*/ |
|
290
|
1 |
|
public function toPEM(): PEM |
|
291
|
|
|
{ |
|
292
|
1 |
|
return new PEM(PEM::TYPE_RSA_PRIVATE_KEY, $this->toDER()); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|