forked from wechatpay-apiv3/wechatpay-php
-
Notifications
You must be signed in to change notification settings - Fork 6
/
RsaTest.php
364 lines (316 loc) · 15.6 KB
/
RsaTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php declare(strict_types=1);
namespace WeChatPay\Tests\Crypto;
use const PHP_MAJOR_VERSION;
use const OPENSSL_PKCS1_OAEP_PADDING;
use const OPENSSL_PKCS1_PADDING;
use function file_get_contents;
use function method_exists;
use function openssl_pkey_get_private;
use function openssl_pkey_get_public;
use function openssl_x509_read;
use function preg_match;
use function random_bytes;
use function sprintf;
use function str_replace;
use function substr;
use function rtrim;
use UnexpectedValueException;
use WeChatPay\Crypto\Rsa;
use PHPUnit\Framework\TestCase;
class RsaTest extends TestCase
{
private const BASE64_EXPRESSION = '#^[a-zA-Z0-9\+/]+={0,2}$#';
private const FIXTURES = __DIR__ . '/../fixtures/mock.%s.%s';
private const EVELOPE = '#-{5}BEGIN[^-]+-{5}\r?\n(?<base64>[^-]+)\r?\n-{5}END[^-]+-{5}#';
public function testClassConstants(): void
{
self::assertIsString(Rsa::KEY_TYPE_PRIVATE);
self::assertIsString(Rsa::KEY_TYPE_PUBLIC);
}
/**
* @param string $type
* @param string $suffix
*/
private function getMockContents(string $type, string $suffix): string
{
$file = sprintf(self::FIXTURES, $type, $suffix);
$pkey = file_get_contents($file);
preg_match(self::EVELOPE, $pkey ?: '', $matches);
return str_replace(["\r", "\n"], '', $matches['base64'] ?? '');
}
public function testFromPkcs8(): void
{
$thing = $this->getMockContents('pkcs8', 'key');
self::assertIsString($thing);
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(self::BASE64_EXPRESSION, $thing);
} else {
self::assertRegExp(self::BASE64_EXPRESSION, $thing);
}
$pkey = Rsa::fromPkcs8($thing);
if (8 === PHP_MAJOR_VERSION) {
self::assertIsObject($pkey);
} else {
self::assertIsResource($pkey);
}
}
public function testPkcs1ToSpki(): void
{
/**
* @var string $spki
* @var string $pkcs1
*/
[, , [$spki], [$pkcs1]] = array_values($this->keyPhrasesDataProvider());
self::assertStringStartsWith('public.spki://', $spki);
self::assertStringStartsWith('public.pkcs1://', $pkcs1);
self::assertEquals(substr($spki, 14), Rsa::pkcs1ToSpki(substr($pkcs1, 15)));
}
/**
* @return array<string,array{string,string}>
*/
public function pkcs1PhrasesDataProvider(): array
{
return [
'`private.pkcs1://`' => [$this->getMockContents('pkcs1', 'key'), Rsa::KEY_TYPE_PRIVATE],
'`public.pkcs1://`' => [$this->getMockContents('pkcs1', 'pem'), Rsa::KEY_TYPE_PUBLIC],
];
}
/**
* @dataProvider pkcs1PhrasesDataProvider
*
* @param string $thing
*/
public function testFromPkcs1(string $thing, string $type): void
{
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(self::BASE64_EXPRESSION, $thing);
} else {
self::assertRegExp(self::BASE64_EXPRESSION, $thing);
}
$pkey = Rsa::fromPkcs1($thing, $type);
if (8 === PHP_MAJOR_VERSION) {
self::assertIsObject($pkey);
} else {
self::assertIsResource($pkey);
}
}
public function testFromSpki(): void
{
$thing = $this->getMockContents('spki', 'pem');
self::assertIsString($thing);
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(self::BASE64_EXPRESSION, $thing);
} else {
self::assertRegExp(self::BASE64_EXPRESSION, $thing);
}
$pkey = Rsa::fromSpki($thing);
if (8 === PHP_MAJOR_VERSION) {
self::assertIsObject($pkey);
} else {
self::assertIsResource($pkey);
}
}
/**
* @return array<string,array{\OpenSSLAsymmetricKey|resource|string|mixed,string}>
*/
public function keyPhrasesDataProvider(): array
{
return [
'`private.pkcs1://` string' => ['private.pkcs1://' . $this->getMockContents('pkcs1', 'key'), Rsa::KEY_TYPE_PRIVATE],
'`private.pkcs8://` string' => ['private.pkcs8://' . $this->getMockContents('pkcs8', 'key'), Rsa::KEY_TYPE_PRIVATE],
'`public.spki://` string' => ['public.spki://' . $this->getMockContents('spki', 'pem'), Rsa::KEY_TYPE_PUBLIC],
'`public.pkcs1://` string' => ['public.pkcs1://' . $this->getMockContents('pkcs1', 'pem'), Rsa::KEY_TYPE_PUBLIC],
'`file://` PKCS#1 privateKey path string' => [$f = 'file://' . sprintf(self::FIXTURES, 'pkcs1', 'key'), Rsa::KEY_TYPE_PRIVATE],
'OpenSSLAsymmetricKey/resource(private)1' => [openssl_pkey_get_private($f), Rsa::KEY_TYPE_PRIVATE],
'PKCS#1 privateKey contents' => [$f = (string)file_get_contents($f), Rsa::KEY_TYPE_PRIVATE],
'OpenSSLAsymmetricKey/resource(private)2' => [openssl_pkey_get_private($f), Rsa::KEY_TYPE_PRIVATE],
'`file://` PKCS#8 privateKey path string' => [$f = 'file://' . sprintf(self::FIXTURES, 'pkcs8', 'key'), Rsa::KEY_TYPE_PRIVATE],
'OpenSSLAsymmetricKey/resource(private)3' => [openssl_pkey_get_private($f), Rsa::KEY_TYPE_PRIVATE],
'PKCS#8 privateKey contents' => [$f = (string)file_get_contents($f), Rsa::KEY_TYPE_PRIVATE],
'OpenSSLAsymmetricKey/resource(private)4' => [openssl_pkey_get_private($f), Rsa::KEY_TYPE_PRIVATE],
'`file://` SPKI publicKey path string' => [$f = 'file://' . sprintf(self::FIXTURES, 'spki', 'pem'), Rsa::KEY_TYPE_PUBLIC],
'OpenSSLAsymmetricKey/resource(public)1' => [openssl_pkey_get_public($f), Rsa::KEY_TYPE_PUBLIC],
'SKPI publicKey contents' => [$f = (string)file_get_contents($f), Rsa::KEY_TYPE_PUBLIC],
'OpenSSLAsymmetricKey/resource(public)2' => [openssl_pkey_get_public($f), Rsa::KEY_TYPE_PUBLIC],
'pkcs1 publicKey contents' => [(string)file_get_contents(sprintf(self::FIXTURES, 'pkcs1', 'pem')), Rsa::KEY_TYPE_PUBLIC],
'`file://` x509 certificate string' => [$f = 'file://' . sprintf(self::FIXTURES, 'sha256', 'crt'), Rsa::KEY_TYPE_PUBLIC],
'x509 certificate contents string' => [$f = (string)file_get_contents($f), Rsa::KEY_TYPE_PUBLIC],
'OpenSSLCertificate/resource' => [openssl_x509_read($f), Rsa::KEY_TYPE_PUBLIC],
'`file://` PKCS#8 encrypted privateKey' => [
[
$f = 'file://' . sprintf(self::FIXTURES, 'encrypted.pkcs8', 'key'),
$w = rtrim((string)file_get_contents(sprintf(self::FIXTURES, 'pwd', 'txt')))
],
Rsa::KEY_TYPE_PRIVATE
],
'PKCS#8 encrypted privateKey contents' => [[(string)file_get_contents($f), $w], Rsa::KEY_TYPE_PRIVATE],
];
}
/**
* @dataProvider keyPhrasesDataProvider
*
* @param \OpenSSLAsymmetricKey|resource|string|mixed $thing
*/
public function testFrom($thing, string $type): void
{
$pkey = Rsa::from($thing, $type);
if (8 === PHP_MAJOR_VERSION) {
self::assertIsObject($pkey);
} else {
self::assertIsResource($pkey);
}
}
/**
* @return array<string,array{string,string|\OpenSSLAsymmetricKey|resource|mixed,string|\OpenSSLAsymmetricKey|resource|mixed}>
*/
public function keysProvider(): array
{
/**
* @var string $pub1
* @var string $pub2
* @var string $pri1
* @var string $pri2
*/
[
[$pri1], [$pri2],
[$pub1], [$pub2],
[$pri3], [$pri4], [$pri5], [$pri6],
[$pri7], [$pri8], [$pri9], [$pri0],
[$pub3], [$pub4], [$pub5], [$pub6],
[$pub7], [$crt1], [$crt2], [$crt3]
,[$encryptedKey1] ,[$encryptedKey2]
] = array_values($this->keyPhrasesDataProvider());
$keys = [
'plaintext, `public.spki://`, `private.pkcs1://`' => [random_bytes( 8), Rsa::fromSpki(substr($pub1, 14)), Rsa::fromPkcs1(substr($pri1, 16))],
'plaintext, `public.spki://`, `private.pkcs8://`' => [random_bytes(16), Rsa::fromSpki(substr($pub1, 14)), Rsa::fromPkcs8(substr($pri2, 16))],
'plaintext, `public.pkcs1://`, `private.pkcs1://`' => [random_bytes(24), Rsa::fromPkcs1(substr($pub2, 15), Rsa::KEY_TYPE_PUBLIC), Rsa::fromPkcs1(substr($pri1, 16))],
'plaintext, `public.pkcs1://`, `private.pkcs8://`' => [random_bytes(32), Rsa::fromPkcs1(substr($pub2, 15), Rsa::KEY_TYPE_PUBLIC), Rsa::fromPkcs8(substr($pri2, 16))],
'plaintext, `pkcs#1 pubkey content`, `private.pkcs1://`' => [random_bytes(40), Rsa::from($pub7, Rsa::KEY_TYPE_PUBLIC), Rsa::fromPkcs1(substr($pri1, 16))],
'plaintext, `pkcs#1 pubkey content`, `private.pkcs8://`' => [random_bytes(48), Rsa::from($pub7, Rsa::KEY_TYPE_PUBLIC), Rsa::fromPkcs8(substr($pri2, 16))],
'txt, `SPKI file://pubkey`, [`file://`,``] privateKey' => [random_bytes(64), $pub3, [$pri3, '']],
'txt, `SPKI pubkey content`, [`contents`,``] privateKey' => [random_bytes(72), $pub5, [$pri5, '']],
'str, `SPKI file://pubkey`, [`file://`privateKey, pwd]' => [random_bytes(64), $pub3, $encryptedKey1],
'str, `SPKI pubkey content`, [`encrypted contents`,pwd]' => [random_bytes(72), $pub5, $encryptedKey2],
];
foreach ([$pub3, $pub4, $pub5, $pub6, $crt1, $crt2, $crt3] as $pubIndex => $pub) {
foreach ([$pri1, $pri2, $pri3, $pri4, $pri5, $pri6, $pri7, $pri8, $pri9, $pri0] as $priIndex => $pri) {
$keys["plaintext, publicKey{$pubIndex}, privateKey{$priIndex}"] = [random_bytes(56), Rsa::from($pub, Rsa::KEY_TYPE_PUBLIC), Rsa::from($pri, Rsa::KEY_TYPE_PRIVATE)];
}
}
return $keys;
}
/**
* @dataProvider keysProvider
* @param string $plaintext
* @param object|resource|mixed $publicKey
*/
public function testEncrypt(string $plaintext, $publicKey): void
{
$ciphertext = Rsa::encrypt($plaintext, $publicKey);
self::assertIsString($ciphertext);
self::assertNotEquals($plaintext, $ciphertext);
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(self::BASE64_EXPRESSION, $ciphertext);
} else {
self::assertRegExp(self::BASE64_EXPRESSION, $ciphertext);
}
}
/**
* @dataProvider keysProvider
* @param string $plaintext
* @param object|resource|mixed $publicKey
* @param object|resource|mixed $privateKey
*/
public function testDecrypt(string $plaintext, $publicKey, $privateKey): void
{
$ciphertext = Rsa::encrypt($plaintext, $publicKey);
self::assertIsString($ciphertext);
self::assertNotEquals($plaintext, $ciphertext);
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(self::BASE64_EXPRESSION, $ciphertext);
} else {
self::assertRegExp(self::BASE64_EXPRESSION, $ciphertext);
}
$mytext = Rsa::decrypt($ciphertext, $privateKey);
self::assertIsString($mytext);
self::assertEquals($plaintext, $mytext);
}
/**
* @return array<string,array{string,array{\OpenSSLAsymmetricKey|resource|mixed,int},array{\OpenSSLAsymmetricKey|resource|mixed,int},?class-string<\UnexpectedValueException>}>
*/
public function crossPaddingPhrasesProvider(): array
{
[, , , , , , , [$privateKey], , , , , , [$publicKey]] = array_values($this->keyPhrasesDataProvider());
return [
'encrypted as OPENSSL_PKCS1_OAEP_PADDING, and decrpted as OPENSSL_PKCS1_PADDING' => [
random_bytes(32), [$publicKey, OPENSSL_PKCS1_OAEP_PADDING], [$privateKey, OPENSSL_PKCS1_PADDING], UnexpectedValueException::class
],
'encrypted as OPENSSL_PKCS1_PADDING, and decrpted as OPENSSL_PKCS1_OAEP_PADDING' => [
random_bytes(32), [$publicKey, OPENSSL_PKCS1_PADDING], [$privateKey, OPENSSL_PKCS1_OAEP_PADDING], UnexpectedValueException::class
],
'encrypted as OPENSSL_PKCS1_OAEP_PADDING, and decrpted as OPENSSL_PKCS1_OAEP_PADDING' => [
random_bytes(32), [$publicKey, OPENSSL_PKCS1_OAEP_PADDING], [$privateKey, OPENSSL_PKCS1_OAEP_PADDING], null
],
'encrypted as OPENSSL_PKCS1_PADDING, and decrpted as OPENSSL_PKCS1_PADDING' => [
random_bytes(32), [$publicKey, OPENSSL_PKCS1_PADDING], [$privateKey, OPENSSL_PKCS1_PADDING], UnexpectedValueException::class
],
];
}
/**
* @dataProvider crossPaddingPhrasesProvider
* @param string $plaintext
* @param array{\OpenSSLAsymmetricKey|resource|mixed,int} $publicKeyAndPaddingMode
* @param array{\OpenSSLAsymmetricKey|resource|mixed,int} $privateKeyAndPaddingMode
* @param ?class-string<\UnexpectedValueException> $exception
*/
public function testCrossEncryptDecryptWithDifferentPadding(
string $plaintext, array $publicKeyAndPaddingMode, array $privateKeyAndPaddingMode, ?string $exception = null
): void
{
if ($exception) {
$this->expectException($exception);
}
$ciphertext = Rsa::encrypt($plaintext, ...$publicKeyAndPaddingMode);
$decrypted = Rsa::decrypt($ciphertext, ...$privateKeyAndPaddingMode);
if ($exception === null) {
self::assertNotEmpty($ciphertext);
self::assertNotEmpty($decrypted);
self::assertEquals($plaintext, $decrypted);
}
}
/**
* @dataProvider keysProvider
* @param string $plaintext
* @param object|resource|mixed $publicKey
* @param object|resource|mixed $privateKey
*/
public function testSign(string $plaintext, $publicKey, $privateKey): void
{
$signature = Rsa::sign($plaintext, $privateKey);
self::assertIsString($signature);
self::assertNotEquals($plaintext, $signature);
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(self::BASE64_EXPRESSION, $signature);
} else {
self::assertRegExp(self::BASE64_EXPRESSION, $signature);
}
}
/**
* @dataProvider keysProvider
* @param string $plaintext
* @param object|resource|mixed $publicKey
* @param object|resource|mixed $privateKey
*/
public function testVerify(string $plaintext, $publicKey, $privateKey): void
{
$signature = Rsa::sign($plaintext, $privateKey);
self::assertIsString($signature);
self::assertNotEquals($plaintext, $signature);
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(self::BASE64_EXPRESSION, $signature);
} else {
self::assertRegExp(self::BASE64_EXPRESSION, $signature);
}
self::assertTrue(Rsa::verify($plaintext, $signature, $publicKey));
}
}