1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MesCryptoBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mes\Security\CryptoBundle\Tests; |
13
|
|
|
|
14
|
|
|
use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException; |
15
|
|
|
use Mes\Security\CryptoBundle\Encryption; |
16
|
|
|
use Mes\Security\CryptoBundle\EncryptionInterface; |
17
|
|
|
use Mes\Security\CryptoBundle\KeyGenerator\KeyGenerator; |
18
|
|
|
use Mes\Security\CryptoBundle\KeyGenerator\KeyGeneratorInterface; |
19
|
|
|
use Mes\Security\CryptoBundle\Model\KeyInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class EncryptionTest. |
23
|
|
|
*/ |
24
|
|
|
class EncryptionTest extends \PHPUnit_Framework_TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var EncryptionInterface |
28
|
|
|
*/ |
29
|
|
|
private $encryption; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var KeyGeneratorInterface |
33
|
|
|
*/ |
34
|
|
|
private $generator; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->encryption = new Encryption(); |
39
|
|
|
$this->generator = new KeyGenerator(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function tearDown() |
43
|
|
|
{ |
44
|
|
|
$this->encryption = null; |
45
|
|
|
$this->generator = null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/* =================================== |
49
|
|
|
* |
50
|
|
|
* EncryptionInterface::EncryptWithKey |
51
|
|
|
* |
52
|
|
|
* =================================== |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function testEncryptWithKeyEncryptsPlaintext() |
59
|
|
|
{ |
60
|
|
|
$key = $this->generator->generate(); |
61
|
|
|
$plaintext = 'The quick brown fox jumps over the lazy dog'; |
62
|
|
|
$ciphertext = $this->encryption->encryptWithKey($plaintext, $key); |
63
|
|
|
|
64
|
|
|
$this->assertTrue(ctype_print($ciphertext), 'is printable'); |
65
|
|
|
|
66
|
|
|
return array( |
67
|
|
|
'ciphertext' => $ciphertext, |
68
|
|
|
'key_encoded' => $key->getEncoded(), |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintext |
74
|
|
|
* |
75
|
|
|
* @param $args |
76
|
|
|
*/ |
77
|
|
|
public function testDecryptWithKeyDecryptsCiphertext($args) |
78
|
|
|
{ |
79
|
|
|
$key = $this->generator->generateFromAscii($args['key_encoded']); |
80
|
|
|
$plaintext = $this->encryption->decryptWithKey($args['ciphertext'], $key); |
81
|
|
|
|
82
|
|
|
$this->assertSame('The quick brown fox jumps over the lazy dog', $plaintext); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintext |
87
|
|
|
* |
88
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
89
|
|
|
* |
90
|
|
|
* @param $args |
91
|
|
|
*/ |
92
|
|
|
public function testDecryptWithKeyThrowsExceptionBecauseCiphertextIsCorrupted($args) |
93
|
|
|
{ |
94
|
|
|
$key = $this->generator->generateFromAscii($args['key_encoded']); |
95
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'].'{FakeString}', $key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintext |
100
|
|
|
* |
101
|
|
|
* @expectedException \Defuse\Crypto\Exception\BadFormatException |
102
|
|
|
* |
103
|
|
|
* @param $args |
104
|
|
|
*/ |
105
|
|
|
public function testDecryptWithKeyThrowsExceptionBecauseKeyIsCorrupted($args) |
106
|
|
|
{ |
107
|
|
|
$key = $this->generator->generateFromAscii($args['key_encoded'].'{FakeString}'); |
108
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'], $key); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/* ============================================================= |
112
|
|
|
* |
113
|
|
|
* EncryptionInterface::EncryptWithKey and secret-protected Key |
114
|
|
|
* |
115
|
|
|
* ============================================================= |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function testEncryptWithKeyEncryptsPlaintextWithPassword() |
122
|
|
|
{ |
123
|
|
|
$key = $this->generator->generate('ThisIsASecretPassword'); |
124
|
|
|
|
125
|
|
|
$this->assertInstanceOf('\Defuse\Crypto\KeyProtectedByPassword', $key->getRawKey()); |
126
|
|
|
$this->assertSame('ThisIsASecretPassword', $key->getSecret()); |
127
|
|
|
|
128
|
|
|
$plaintext = 'The quick brown fox jumps over the lazy dog'; |
129
|
|
|
$ciphertext = $this->encryption->encryptWithKey($plaintext, $key); |
130
|
|
|
|
131
|
|
|
$this->assertInstanceOf('\Defuse\Crypto\Key', $key->getRawKey()); |
132
|
|
|
$this->assertTrue(ctype_print($ciphertext), 'is printable'); |
133
|
|
|
$this->assertTrue(ctype_print($key->getEncoded()), 'is printable'); |
134
|
|
|
|
135
|
|
|
return array( |
136
|
|
|
'ciphertext' => $ciphertext, |
137
|
|
|
'key_encoded' => $key->getEncoded(), |
138
|
|
|
'secret' => $key->getSecret(), |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
144
|
|
|
* |
145
|
|
|
* @param $args |
146
|
|
|
*/ |
147
|
|
|
public function testDecryptWithKeyDecryptsCiphertextWithPassword($args) |
148
|
|
|
{ |
149
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'], $args['secret']); |
150
|
|
|
|
151
|
|
|
$this->assertInstanceOf('\Defuse\Crypto\KeyProtectedByPassword', $keyFromAscii->getRawKey()); |
152
|
|
|
$this->assertSame($args['secret'], $keyFromAscii->getSecret()); |
153
|
|
|
|
154
|
|
|
$plaintext = $this->encryption->decryptWithKey($args['ciphertext'], $keyFromAscii); |
155
|
|
|
|
156
|
|
|
$this->assertSame('The quick brown fox jumps over the lazy dog', $plaintext); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
161
|
|
|
* |
162
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
163
|
|
|
* |
164
|
|
|
* @param $args |
165
|
|
|
*/ |
166
|
|
|
public function testDecryptWithKeyThrowsExceptionWithCiphertextWithPasswordBecauseSecretIsCorrupted($args) |
167
|
|
|
{ |
168
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'], $args['secret'].'{FakeString}'); |
169
|
|
|
|
170
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'], $keyFromAscii); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
175
|
|
|
* |
176
|
|
|
* @expectedException \Defuse\Crypto\Exception\BadFormatException |
177
|
|
|
* |
178
|
|
|
* @param $args |
179
|
|
|
*/ |
180
|
|
|
public function testDecryptWithKeyThrowsExceptionWithCiphertextWithPasswordBecauseKeyIsCorrupted($args) |
181
|
|
|
{ |
182
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'].'{FakeString}', $args['secret']); |
183
|
|
|
|
184
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'], $keyFromAscii); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
189
|
|
|
* |
190
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
191
|
|
|
* |
192
|
|
|
* @param $args |
193
|
|
|
*/ |
194
|
|
|
public function testDecryptWithKeyThrowsExceptionWithCiphertextWithPasswordBecauseCiphertextIsCorrupted($args) |
195
|
|
|
{ |
196
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'], $args['secret']); |
197
|
|
|
|
198
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'].'{FakeString}', $keyFromAscii); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/* ================================================================ |
202
|
|
|
* |
203
|
|
|
* EncryptionInterface::EncryptFileWithKey and secret-protected Key |
204
|
|
|
* |
205
|
|
|
* ================================================================ |
206
|
|
|
*/ |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
public function testEncryptFileWithKeyEncryptsFile() |
212
|
|
|
{ |
213
|
|
|
/** @var KeyInterface $key */ |
214
|
|
|
$key = $this->generator->generate('CryptoSecret'); |
215
|
|
|
|
216
|
|
|
// Create file to encrypt. |
217
|
|
|
$tmpfname = tempnam(__DIR__, 'CRYPTO_'); |
218
|
|
|
$plainContent = "Dinanzi a me non fuor cose create se non etterne, e io etterno duro. Lasciate ogni speranza, voi ch'intrate."; |
219
|
|
|
$handle = fopen($tmpfname, 'w'); |
220
|
|
|
fwrite($handle, $plainContent); |
221
|
|
|
fclose($handle); |
222
|
|
|
|
223
|
|
|
$filename = md5(uniqid()); |
224
|
|
|
$encryptedFilename = __DIR__."/ENCRYPTED_$filename.crypto"; |
225
|
|
|
|
226
|
|
|
$this->encryption->encryptFileWithKey($tmpfname, $encryptedFilename, $key); |
227
|
|
|
|
228
|
|
|
$this->assertFileExists($encryptedFilename, sprintf('%s file must exists', $encryptedFilename)); |
229
|
|
|
$this->assertGreaterThan(0, (new \SplFileInfo($encryptedFilename))->getSize()); |
230
|
|
|
|
231
|
|
|
unlink($tmpfname); |
232
|
|
|
|
233
|
|
|
return array( |
234
|
|
|
'key' => $key->getEncoded(), |
235
|
|
|
'secret' => $key->getSecret(), |
236
|
|
|
'encryptedFile' => $encryptedFilename, |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @depends testEncryptFileWithKeyEncryptsFile |
242
|
|
|
* |
243
|
|
|
* @param $args |
244
|
|
|
*/ |
245
|
|
|
public function testDecryptFileWithKeyDecryptsEncryptedFile($args) |
246
|
|
|
{ |
247
|
|
|
/** @var KeyInterface $key */ |
248
|
|
|
$key = $this->generator->generateFromAscii($args['key'], $args['secret']); |
249
|
|
|
|
250
|
|
|
$tmpDecryptedFile = tempnam(__DIR__, '_CRYPTO'); |
251
|
|
|
|
252
|
|
|
$this->encryption->decryptFileWithKey($args['encryptedFile'], $tmpDecryptedFile, $key); |
253
|
|
|
|
254
|
|
|
$this->assertFileExists($tmpDecryptedFile); |
255
|
|
|
$this->assertGreaterThan(0, (new \SplFileInfo($tmpDecryptedFile))->getSize()); |
256
|
|
|
$this->assertContains("Dinanzi a me non fuor cose create se non etterne, e io etterno duro. Lasciate ogni speranza, voi ch'intrate.", file_get_contents($tmpDecryptedFile)); |
257
|
|
|
|
258
|
|
|
unlink($tmpDecryptedFile); |
259
|
|
|
unlink($args['encryptedFile']); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/* ======================================== |
263
|
|
|
* |
264
|
|
|
* EncryptionInterface::EncryptWithPassword |
265
|
|
|
* |
266
|
|
|
* ======================================== |
267
|
|
|
*/ |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
|
|
public function testEncryptWithPasswordEncryptsPlaintext() |
273
|
|
|
{ |
274
|
|
|
$plaintext = 'The quick brown fox jumps over the lazy dog'; |
275
|
|
|
$ciphertext = $this->encryption->encryptWithPassword($plaintext, 'SuperSecretPa$$word'); |
276
|
|
|
|
277
|
|
|
$this->assertTrue(ctype_print($ciphertext), 'is printable'); |
278
|
|
|
|
279
|
|
|
return array( |
280
|
|
|
'ciphertext' => $ciphertext, |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @depends testEncryptWithPasswordEncryptsPlaintext |
286
|
|
|
* |
287
|
|
|
* @param $args |
288
|
|
|
*/ |
289
|
|
|
public function testDecryptWithPasswordDecryptsCiphertext($args) |
290
|
|
|
{ |
291
|
|
|
$plaintext = $this->encryption->decryptWithPassword($args['ciphertext'], 'SuperSecretPa$$word'); |
292
|
|
|
|
293
|
|
|
$this->assertSame('The quick brown fox jumps over the lazy dog', $plaintext, sprintf("'%s' is correct.", $plaintext)); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @depends testEncryptWithPasswordEncryptsPlaintext |
298
|
|
|
* |
299
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
300
|
|
|
* |
301
|
|
|
* @param $args |
302
|
|
|
*/ |
303
|
|
|
public function testDecryptWithPasswordThrowsException($args) |
304
|
|
|
{ |
305
|
|
|
$this->encryption->decryptWithPassword($args['ciphertext'], 'SuperSecretPa$$wordIncorrect'); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/* ============================================ |
309
|
|
|
* |
310
|
|
|
* EncryptionInterface::EncryptFileWithPassword |
311
|
|
|
* |
312
|
|
|
* ============================================ |
313
|
|
|
*/ |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
|
|
public function testEncryptFileWithPasswordEncryptsFile() |
319
|
|
|
{ |
320
|
|
|
// Create file to encrypt. |
321
|
|
|
$tmpfname = tempnam(__DIR__, 'CRYPTO_'); |
322
|
|
|
$plainContent = "Dinanzi a me non fuor cose create se non etterne, e io etterno duro. Lasciate ogni speranza, voi ch'intrate."; |
323
|
|
|
$handle = fopen($tmpfname, 'w'); |
324
|
|
|
fwrite($handle, $plainContent); |
325
|
|
|
fclose($handle); |
326
|
|
|
|
327
|
|
|
$filename = md5(uniqid()); |
328
|
|
|
$encryptedFilename = __DIR__."/ENCRYPTED_$filename.crypto"; |
329
|
|
|
|
330
|
|
|
$this->encryption->encryptFileWithPassword($tmpfname, $encryptedFilename, 'SuperSecretPa$$word'); |
331
|
|
|
|
332
|
|
|
$this->assertFileExists($encryptedFilename, sprintf('%s file must exists', $encryptedFilename)); |
333
|
|
|
$this->assertGreaterThan(0, (new \SplFileInfo($encryptedFilename))->getSize()); |
334
|
|
|
|
335
|
|
|
unlink($tmpfname); |
336
|
|
|
|
337
|
|
|
return array( |
338
|
|
|
'encryptedFile' => $encryptedFilename, |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @depends testEncryptFileWithPasswordEncryptsFile |
344
|
|
|
* |
345
|
|
|
* @param $args |
346
|
|
|
*/ |
347
|
|
|
public function testDecryptFileWithPasswordDecryptsEncryptedFile($args) |
348
|
|
|
{ |
349
|
|
|
$tmpDecryptedFile = tempnam(__DIR__, '_CRYPTO'); |
350
|
|
|
|
351
|
|
|
$this->encryption->decryptFileWithPassword($args['encryptedFile'], $tmpDecryptedFile, 'SuperSecretPa$$word'); |
352
|
|
|
|
353
|
|
|
$this->assertFileExists($tmpDecryptedFile); |
354
|
|
|
$this->assertGreaterThan(0, (new \SplFileInfo($tmpDecryptedFile))->getSize()); |
355
|
|
|
$this->assertContains("Dinanzi a me non fuor cose create se non etterne, e io etterno duro. Lasciate ogni speranza, voi ch'intrate.", file_get_contents($tmpDecryptedFile)); |
356
|
|
|
|
357
|
|
|
unlink($tmpDecryptedFile); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @depends testEncryptFileWithPasswordEncryptsFile |
362
|
|
|
* |
363
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
364
|
|
|
* |
365
|
|
|
* @param $args |
366
|
|
|
* |
367
|
|
|
* @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
368
|
|
|
*/ |
369
|
|
|
public function testDecryptFileWithPasswordThrowsExceptionDecryptsEncryptedFile($args) |
370
|
|
|
{ |
371
|
|
|
$tmpDecryptedFile = tempnam(__DIR__, '_CRYPTO'); |
372
|
|
|
|
373
|
|
|
try { |
374
|
|
|
$this->encryption->decryptFileWithPassword($args['encryptedFile'], $tmpDecryptedFile, 'SuperSecretPa$$wordIncorrect'); |
375
|
|
|
} catch (WrongKeyOrModifiedCiphertextException $e) { |
376
|
|
|
unlink($tmpDecryptedFile); |
377
|
|
|
unlink($args['encryptedFile']); |
378
|
|
|
|
379
|
|
|
throw $e; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|