Newer
Older
Jaime Perez Crespo
committed
<?php
namespace SimpleSAML\Test\Utils;
use SimpleSAML\Utils\Crypto;
use \SimpleSAML_Configuration as Configuration;
use \org\bovigo\vfs\vfsStream;
Jaime Perez Crespo
committed
/**
* Tests for SimpleSAML\Utils\Crypto.
*/
class CryptoTest extends \PHPUnit_Framework_TestCase
Jaime Perez Crespo
committed
{
const ROOTDIRNAME = 'testdir';
const DEFAULTCERTDIR = 'certdir';
public function setUp()
{
$this->root = vfsStream::setup(
self::ROOTDIRNAME,
null,
array(
self::DEFAULTCERTDIR => array(),
)
);
$this->root_directory = vfsStream::url(self::ROOTDIRNAME);
$this->certdir = $this->root_directory.DIRECTORY_SEPARATOR.self::DEFAULTCERTDIR;
}
Jaime Perez Crespo
committed
/**
* Test invalid input provided to the aesDecrypt() method.
*
* @expectedException \InvalidArgumentException
*
Jaime Perez Crespo
committed
*/
public function testAesDecryptBadInput()
{
$m = new \ReflectionMethod('\SimpleSAML\Utils\Crypto', '_aesDecrypt');
$m->setAccessible(true);
$m->invokeArgs(null, array(array(), 'SECRET'));
Jaime Perez Crespo
committed
}
/**
* Test invalid input provided to the aesEncrypt() method.
*
* @expectedException \InvalidArgumentException
*
Jaime Perez Crespo
committed
*/
public function testAesEncryptBadInput()
{
$m = new \ReflectionMethod('\SimpleSAML\Utils\Crypto', '_aesEncrypt');
$m->setAccessible(true);
$m->invokeArgs(null, array(array(), 'SECRET'));
Jaime Perez Crespo
committed
}
/**
* Test that aesDecrypt() works properly, being able to decrypt some previously known (and correct)
* ciphertext.
Jaime Perez Crespo
committed
*/
public function testAesDecrypt()
{
if (!extension_loaded('openssl')) {
Jaime Perez Crespo
committed
$this->setExpectedException('\SimpleSAML_Error_Exception');
}
$secret = 'SUPER_SECRET_SALT';
$m = new \ReflectionMethod('\SimpleSAML\Utils\Crypto', '_aesDecrypt');
Jaime Perez Crespo
committed
$m->setAccessible(true);
$plaintext = 'SUPER_SECRET_TEXT';
Jaime Pérez Crespo
committed
$ciphertext = 'uR2Yu0r4itInKx91D/l9y/08L5CIQyev9nAr27fh3Sshous4vbXRRcMcjqHDOrquD+2vqLyw7ygnbA9jA9TpB4hLZocvAWcTN8tyO82hiSY=';
Jaime Perez Crespo
committed
$this->assertEquals($plaintext, $m->invokeArgs(null, array(base64_decode($ciphertext), $secret)));
}
/**
* Test that aesEncrypt() produces ciphertexts that aesDecrypt() can decrypt.
* @covers \SimpleSAML\Utils\Crypto::_aesDecrypt
* @covers \SimpleSAML\Utils\Crypto::_aesEncrypt
Jaime Perez Crespo
committed
*/
public function testAesEncrypt()
{
if (!extension_loaded('openssl')) {
Jaime Perez Crespo
committed
$this->setExpectedException('\SimpleSAML_Error_Exception');
}
$secret = 'SUPER_SECRET_SALT';
$e = new \ReflectionMethod('\SimpleSAML\Utils\Crypto', '_aesEncrypt');
$d = new \ReflectionMethod('\SimpleSAML\Utils\Crypto', '_aesDecrypt');
Jaime Perez Crespo
committed
$e->setAccessible(true);
$d->setAccessible(true);
$original_plaintext = 'SUPER_SECRET_TEXT';
$ciphertext = $e->invokeArgs(null, array($original_plaintext, $secret));
$decrypted_plaintext = $d->invokeArgs(null, array($ciphertext, $secret));
$this->assertEquals($original_plaintext, $decrypted_plaintext);
}
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
/**
* Test that the pem2der() and der2pem() methods work correctly.
*
* @covers \SimpleSAML\Utils\Crypto::der2pem
* @covers \SimpleSAML\Utils\Crypto::pem2der
*/
public function testFormatConversion()
{
$pem = <<<PHP
-----BEGIN CERTIFICATE-----
MIIF8zCCA9ugAwIBAgIJANSv0D4ZoP9iMA0GCSqGSIb3DQEBCwUAMIGPMQswCQYD
VQQGEwJFWDEQMA4GA1UECAwHRXhhbXBsZTEQMA4GA1UEBwwHRXhhbXBsZTEQMA4G
A1UECgwHRXhhbXBsZTEQMA4GA1UECwwHRXhhbXBsZT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R0OBBYEFOk6cEb397GMRCJe9xMIZ/y3yFvEMB8GA1UdIwQYMBaA
FOk6cEb397GMRCJe9xMIZ/y3yFvEMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEL
BQADggIBACc1c8j7oZeVDd8O2k97kY/7pHypVZswLfmg1UqbUmYYqQ9lM6FD0J8R
P+B8i7zST09pJ0FOsCsbyUKQmMIq/citTKmgk8NLK8otWHewHs5KTpsEvJm9XV4s
QjF07GBECJdQWu93Rn8FdR9eJ+H0Y0oHbBu3OtSbHFHyDvaCI5bxM/5FPf4HkJil
qIQunhO5gkz21ebukQUgiZ1YmFl0LjxGUDUDwnQ/3kOejlMUQv+ZXdQp/SaX1z5c
dQlGl/8HDs1YAM3duvdMCXn2LP3QuhrphT/+2o+ZkY32I1p/Q0fDNaE4u7JjaxAd
6+ijpmzZwgG5cFVU+sEeDqCI5MFn2JKiSCrHAHFMTnkpq687qBTLWoYTJ4coxtvs
kmvdoZytKiSf7aDzGQK345BSZWJ+D5RJr2250PHMMeNkFBc+GdGiRsABhhHQAqtE
7TVgdwvc8CYCfXlhRzdSowAVWibiftfPMmItM8Z0w5T/iPW0MsiCLGa5AvCHicN7
pfajpJ9ZzdyLIo6dVjdQtl+S1rpFCx7ziVN8tCCX4fAVCqRqZJaG/UMLvguVqayb
3Aw1B/fVvWoAnAzVN5ZEClZvuyjImnNZpnYSWHzCJ/9JTqB7rq93nf6Olp9QXD5y
5iHKlJ6FlnuhcGCDsUCvG8qCw9FfoS0tuS4tKoQ5WHGQx3sKmr/D
-----END CERTIFICATE-----
PHP;
$this->assertEquals(trim($pem), trim(Crypto::der2pem(Crypto::pem2der($pem))));
}
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
* @covers \SimpleSAML\Utils\Crypto::pwHash
*/
public function testGoodPwHash()
{
$pw = "password";
$algorithm = "SHA1";
$res = Crypto::pwHash($pw, $algorithm);
/*
* echo -n "password" | sha1sum | awk -F " " '{print $1}' | xxd -r -p | base64
* W6ph5Mm5Pz8GgiULbPgzG37mj9g=
*/
$expected = "{SHA}W6ph5Mm5Pz8GgiULbPgzG37mj9g=";
$this->assertEquals($expected, $res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::pwHash
*/
public function testGoodSaltedPwHash()
{
$pw = "password";
$algorithm = "SSHA1";
$salt = "salt";
$res = Crypto::pwHash($pw, $algorithm, $salt);
/*
* echo -n "password""salt" | sha1sum | awk -v salt=$(echo -n "salt" | xxd -u -p) -F " " '{print $1 salt}' | xxd -r -p | base64
* yI6cZwQadOA1e+/f+T+H3eCQQhRzYWx0
*/
$expected = "{SSHA}yI6cZwQadOA1e+/f+T+H3eCQQhRzYWx0";
$this->assertEquals($expected, $res);
}
/**
* @expectedException \SimpleSAML_Error_Exception
*
* @covers \SimpleSAML\Utils\Crypto::pwHash
*/
public function testBadHashAlgorithm()
{
$pw = "password";
$algorithm = "wtf";
Crypto::pwHash($pw, $algorithm);
}
/**
* @covers \SimpleSAML\Utils\Crypto::pwValid
*/
public function testGoodPwValid()
{
$pw = "password";
$algorithm = "SHA1";
$hash = Crypto::pwHash($pw, $algorithm);
$res = Crypto::pwValid($hash, $pw);
$this->assertTrue($res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::pwValid
*/
public function testGoodSaltedPwValid()
{
$pw = "password";
$algorithm = "SSHA1";
$salt = "salt";
$hash = Crypto::pwHash($pw, $algorithm, $salt);
$res = Crypto::pwValid($hash, $pw);
$this->assertTrue($res);
}
/**
* @expectedException \SimpleSAML_Error_Exception
*
* @covers \SimpleSAML\Utils\Crypto::pwValid
*/
public function testBadHashAlgorithmValid()
{
$pw = "password";
$algorithm = "wtf";
$hash = "{".$algorithm."}B64STRING";
Crypto::pwValid($hash, $algorithm);
}
/**
* @covers \SimpleSAML\Utils\Crypto::secureCompare
*/
public function testSecureCompareEqual()
{
$res = Crypto::secureCompare("string", "string");
$this->assertTrue($res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::secureCompare
*/
public function testSecureCompareNotEqual()
{
$res = Crypto::secureCompare("string1", "string2");
$this->assertFalse($res);
}
/**
* @expectedException \SimpleSAML_Error_Exception
*
* @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
*/
public function testLoadPrivateKeyRequiredMetadataMissing()
{
$config = new Configuration(array(), 'test');
$required = true;
Crypto::loadPrivateKey($config, $required);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
*/
public function testLoadPrivateKeyNotRequiredMetadataMissing()
{
$config = new Configuration(array(), 'test');
$required = false;
$res = Crypto::loadPrivateKey($config, $required);
$this->assertNull($res);
}
/**
* @expectedException \SimpleSAML_Error_Exception
*
* @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
*/
public function testLoadPrivateKeyMissingFile()
{
$config = new Configuration(array('privatekey' => 'nonexistant'), 'test');
Crypto::loadPrivateKey($config, false, '', true);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
*/
public function testLoadPrivateKeyBasic()
{
$filename = $this->certdir.DIRECTORY_SEPARATOR.'key';
$data = 'data';
$config = new Configuration(array('privatekey' => $filename), 'test');
$full_path = true;
file_put_contents($filename, $data);
$res = Crypto::loadPrivateKey($config, false, '', $full_path);
$expected = array('PEM' => $data);
$this->assertEquals($expected, $res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
*/
public function testLoadPrivateKeyPassword()
{
$password = 'password';
$filename = $this->certdir.DIRECTORY_SEPARATOR.'key';
$data = 'data';
$config = new Configuration(
array(
'privatekey' => $filename,
'privatekey_pass' => $password,
),
'test'
);
$full_path = true;
file_put_contents($filename, $data);
$res = Crypto::loadPrivateKey($config, false, '', $full_path);
$expected = array('PEM' => $data, 'password' => $password);
$this->assertEquals($expected, $res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
*/
public function testLoadPrivateKeyPrefix()
{
$prefix = 'prefix';
$password = 'password';
$filename = $this->certdir.DIRECTORY_SEPARATOR.'key';
$data = 'data';
$config = new Configuration(
array(
$prefix.'privatekey' => $filename,
$prefix.'privatekey_pass' => $password,
),
'test'
);
$full_path = true;
file_put_contents($filename, $data);
$res = Crypto::loadPrivateKey($config, false, $prefix, $full_path);
$expected = array('PEM' => $data, 'password' => $password);
$this->assertEquals($expected, $res);
}
/**
* @expectedException \SimpleSAML_Error_Exception
*
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyRequiredMetadataMissing()
{
$config = new Configuration(array(), 'test');
$required = true;
Crypto::loadPublicKey($config, $required);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyNotRequiredMetadataMissing()
{
$config = new Configuration(array(), 'test');
$required = false;
$res = Crypto::loadPublicKey($config, $required);
$this->assertNull($res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyFingerprintBasicString()
{
$fingerprint = 'fingerprint';
$config = new Configuration(array('certFingerprint' => $fingerprint), 'test');
$res = Crypto::loadPublicKey($config);
$expected = array('certFingerprint' => array($fingerprint));
$this->assertEquals($expected, $res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyFingerprintBasicArray()
{
$fingerprint1 = 'fingerprint1';
$fingerprint2 = 'fingerprint2';
$config = new Configuration(
array(
'certFingerprint' => array(
$fingerprint1,
$fingerprint2
),
),
'test'
);
$res = Crypto::loadPublicKey($config);
$expected = array('certFingerprint' => array($fingerprint1, $fingerprint2));
$this->assertEquals($expected, $res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyFingerprintLowercase()
{
$fingerprint = 'FINGERPRINT';
$config = new Configuration(array('certFingerprint' => $fingerprint), 'test');
$res = Crypto::loadPublicKey($config);
$expected = array('certFingerprint' => array(strtolower($fingerprint)));
$this->assertEquals($expected, $res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyFingerprintRemoveColons()
{
$fingerprint = 'f:i:n:g:e:r:p:r:i:n:t';
$config = new Configuration(array('certFingerprint' => $fingerprint), 'test');
$res = Crypto::loadPublicKey($config);
$expected = array('certFingerprint' => array(str_replace(':', '', $fingerprint)));
$this->assertEquals($expected, $res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyNotX509Certificate()
{
$config = new Configuration(
array(
'keys' => array(
array(
'X509Certificate' => '',
'type' => 'NotX509Certificate',
'signing' => true
),
),
),
'test'
);
$res = Crypto::loadPublicKey($config);
$this->assertNull($res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyNotSigning()
{
$config = new Configuration(
array(
'keys' => array(
array(
'X509Certificate' => '',
'type' => 'X509Certificate',
'signing' => false
),
),
),
'test'
);
$res = Crypto::loadPublicKey($config);
$this->assertNull($res);
}
/**
* @covers \SimpleSAML\Utils\Crypto::loadPublicKey
*/
public function testLoadPublicKeyBasic()
{
$x509certificate = 'x509certificate';
$config = new Configuration(
array(
'keys' => array(
array(
'X509Certificate' => $x509certificate,
'type' => 'X509Certificate',
'signing' => true
),
),
),
'test'
);
$pubkey = Crypto::loadPublicKey($config);
$res = $pubkey['certData'];
$expected = $x509certificate;
$this->assertEquals($expected, $res);
}