File: CryptoTest.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (624 lines) | stat: -rw-r--r-- 17,153 bytes parent folder | download | duplicates (3)
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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Utils;

use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\Utils\Crypto;

/**
 * Tests for SimpleSAML\Utils\Crypto.
 */
class CryptoTest extends TestCase
{
    private const ROOTDIRNAME = 'testdir';

    private const DEFAULTCERTDIR = 'certdir';

    /** @var \org\bovigo\vfs\vfsStreamDirectory */
    protected $root;

    /** @var string */
    protected $root_directory;

    /** @var string */
    protected $certdir;


    /**
     * @return void
     */
    public function setUp()
    {
        $this->root = vfsStream::setup(
            self::ROOTDIRNAME,
            null,
            [
                self::DEFAULTCERTDIR => [],
            ]
        );
        $this->root_directory = vfsStream::url(self::ROOTDIRNAME);
        $this->certdir = $this->root_directory . DIRECTORY_SEPARATOR . self::DEFAULTCERTDIR;
    }


    /**
     * Test that aesDecrypt() works properly, being able to decrypt some previously known (and correct)
     * ciphertext.
     *
     * @covers \SimpleSAML\Utils\Crypto::aesDecrypt
     * @return void
     */
    public function testAesDecrypt(): void
    {
        if (!extension_loaded('openssl')) {
            $this->expectException(Error\Exception::class);
        }

        $secret = 'SUPER_SECRET_SALT';
        $m = new ReflectionMethod('\SimpleSAML\Utils\Crypto', 'aesDecryptInternal');
        $m->setAccessible(true);

        $plaintext = 'SUPER_SECRET_TEXT';
        $ciphertext = 'uR2Yu0r4itInKx91D/l9y/08L5CIQyev9nAr27fh3Sshous4'
            . 'vbXRRcMcjqHDOrquD+2vqLyw7ygnbA9jA9TpB4hLZocvAWcTN8tyO82hiSY=';
        $this->assertEquals($plaintext, $m->invokeArgs(null, [base64_decode($ciphertext), $secret]));
    }


    /**
     * Test that aesEncrypt() produces ciphertexts that aesDecrypt() can decrypt.
     *
     * @covers \SimpleSAML\Utils\Crypto::aesDecrypt
     * @covers \SimpleSAML\Utils\Crypto::aesEncrypt
     * @return void
     */
    public function testAesEncrypt(): void
    {
        if (!extension_loaded('openssl')) {
            $this->expectException(Error\Exception::class);
        }

        $secret = 'SUPER_SECRET_SALT';
        $e = new ReflectionMethod('\SimpleSAML\Utils\Crypto', 'aesEncryptInternal');
        $d = new ReflectionMethod('\SimpleSAML\Utils\Crypto', 'aesDecryptInternal');
        $e->setAccessible(true);
        $d->setAccessible(true);

        $original_plaintext = 'SUPER_SECRET_TEXT';
        $ciphertext = $e->invokeArgs(null, [$original_plaintext, $secret]);
        $decrypted_plaintext = $d->invokeArgs(null, [$ciphertext, $secret]);
        $this->assertEquals($original_plaintext, $decrypted_plaintext);
    }


    /**
     * Test that the pem2der() and der2pem() methods work correctly.
     *
     * @covers \SimpleSAML\Utils\Crypto::der2pem
     * @covers \SimpleSAML\Utils\Crypto::pem2der
     * @return void
     */
    public function testFormatConversion(): void
    {
        $pem = <<<PHP
-----BEGIN CERTIFICATE-----
MIIF8zCCA9ugAwIBAgIJANSv0D4ZoP9iMA0GCSqGSIb3DQEBCwUAMIGPMQswCQYD
VQQGEwJFWDEQMA4GA1UECAwHRXhhbXBsZTEQMA4GA1UEBwwHRXhhbXBsZTEQMA4G
A1UECgwHRXhhbXBsZTEQMA4GA1UECwwHRXhhbXBsZTEUMBIGA1UEAwwLZXhhb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Je9xMIZ/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))));
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::pwHash
     * @deprecated To be removed for 2.0
     * @return void
     */
    public function testGoodPwHash(): void
    {
        $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
     * @deprecated To be removed for 2.0
     * @return void
     */
    public function testGoodSaltedPwHash(): void
    {
        $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);
    }


    /**
     * @deprecated To be removed for 2.0
     *
     * @covers \SimpleSAML\Utils\Crypto::pwHash
     * @return void
     */
    public function testBadHashAlgorithm(): void
    {
        $this->expectException(\SimpleSAML\Error\Exception::class);
        $pw = "password";
        $algorithm = "wtf";

        Crypto::pwHash($pw, $algorithm);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::pwValid
     * @return void
     */
    public function testGoodPwValid(): void
    {
        $pw = "password";

        $hash = Crypto::pwHash($pw);
        $res = Crypto::pwValid($hash, $pw);

        $this->assertTrue($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::pwValid
     * @return void
     */
    public function testBadPwInvalid(): void
    {
        $pw = "password";
        $pw2 = "password2";

        $hash = Crypto::pwHash($pw);
        $res = Crypto::pwValid($hash, $pw2);

        $this->assertFalse($res);
    }

    /**
     * Check that hash cannot be used to authenticate ith.
     */
    public function testHashAsPwInvalid(): void
    {
        $pw = "password";

        $hash = Crypto::pwHash($pw);
        $this->expectException(Error\Exception::class);
        $res = Crypto::pwValid($hash, $hash);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::pwValid
     * @deprecated To be removed for 2.0
     * @return void
     */
    public function testGoodPwValidOld()
    {
        $pw = "password";
        $algorithm = "SHA1";

        $hash = Crypto::pwHash($pw, $algorithm);
        $res = Crypto::pwValid($hash, $pw);

        $this->assertTrue($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::pwValid
     * @deprecated To be removed for 2.0
     * @return void
     */
    public function testGoodSaltedPwValid()
    {
        $pw = "password";
        $algorithm = "SSHA1";
        $salt = "salt";

        $hash = Crypto::pwHash($pw, $algorithm, $salt);
        $res = Crypto::pwValid($hash, $pw);

        $this->assertTrue($res);
    }


    /**
     * @deprecated To be removed for 2.0
     *
     * @covers \SimpleSAML\Utils\Crypto::pwValid
     * @return void
     */
    public function testBadHashAlgorithmValid()
    {
        $this->expectException(\SimpleSAML\Error\Exception::class);
        $algorithm = "wtf";
        $hash = "{" . $algorithm . "}B64STRING";

        Crypto::pwValid($hash, $algorithm);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::secureCompare
     * @return void
     */
    public function testSecureCompareEqual(): void
    {
        $res = Crypto::secureCompare("string", "string");

        $this->assertTrue($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::secureCompare
     * @return void
     */
    public function testSecureCompareNotEqual(): void
    {
        $res = Crypto::secureCompare("string1", "string2");

        $this->assertFalse($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
     * @return void
     */
    public function testLoadPrivateKeyRequiredMetadataMissing(): void
    {
        $this->expectException(Error\Exception::class);
        $config = new Configuration([], 'test');
        $required = true;

        Crypto::loadPrivateKey($config, $required);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
     * @return void
     */
    public function testLoadPrivateKeyNotRequiredMetadataMissing(): void
    {
        $config = new Configuration([], 'test');
        $required = false;

        $res = Crypto::loadPrivateKey($config, $required);

        $this->assertNull($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
     * @return void
     */
    public function testLoadPrivateKeyMissingFile(): void
    {
        $this->expectException(Error\Exception::class);
        $config = new Configuration(['privatekey' => 'nonexistant'], 'test');

        Crypto::loadPrivateKey($config, false, '', true);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
     * @return void
     */
    public function testLoadPrivateKeyBasic(): void
    {
        $filename = $this->certdir . DIRECTORY_SEPARATOR . 'key';
        $data = 'data';
        $config = new Configuration(['privatekey' => $filename], 'test');
        $full_path = true;

        file_put_contents($filename, $data);

        $res = Crypto::loadPrivateKey($config, false, '', $full_path);
        $expected = ['PEM' => $data, 'password' => null];

        $this->assertEquals($expected, $res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
     * @return void
     */
    public function testLoadPrivateKeyPassword(): void
    {
        $password = 'password';
        $filename = $this->certdir . DIRECTORY_SEPARATOR . 'key';
        $data = 'data';
        $config = new Configuration(
            [
                'privatekey' => $filename,
                'privatekey_pass' => $password,
            ],
            'test'
        );
        $full_path = true;

        file_put_contents($filename, $data);

        $res = Crypto::loadPrivateKey($config, false, '', $full_path);
        $expected = ['PEM' => $data, 'password' => $password];

        $this->assertEquals($expected, $res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPrivateKey
     * @return void
     */
    public function testLoadPrivateKeyPrefix(): void
    {
        $prefix = 'prefix';
        $password = 'password';
        $filename = $this->certdir . DIRECTORY_SEPARATOR . 'key';
        $data = 'data';
        $config = new Configuration(
            [
                $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 = ['PEM' => $data, 'password' => $password];

        $this->assertEquals($expected, $res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyRequiredMetadataMissing(): void
    {
        $this->expectException(Error\Exception::class);
        $config = new Configuration([], 'test');
        $required = true;

        Crypto::loadPublicKey($config, $required);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyNotRequiredMetadataMissing(): void
    {
        $config = new Configuration([], 'test');
        $required = false;

        $res = Crypto::loadPublicKey($config, $required);

        $this->assertNull($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyFingerprintBasicString()
    {
        $fingerprint = 'fingerprint';
        $config = new Configuration(['certFingerprint' => $fingerprint], 'test');

        $res = Crypto::loadPublicKey($config);
        $expected = ['certFingerprint' => [$fingerprint]];

        $this->assertEquals($expected, $res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyFingerprintBasicArray()
    {
        $fingerprint1 = 'fingerprint1';
        $fingerprint2 = 'fingerprint2';
        $config = new Configuration(
            [
                'certFingerprint' => [
                    $fingerprint1,
                    $fingerprint2
                ],
            ],
            'test'
        );

        $res = Crypto::loadPublicKey($config);
        $expected = ['certFingerprint' => [$fingerprint1, $fingerprint2]];

        $this->assertEquals($expected, $res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyFingerprintLowercase()
    {
        $fingerprint = 'FINGERPRINT';
        $config = new Configuration(['certFingerprint' => $fingerprint], 'test');

        $res = Crypto::loadPublicKey($config);
        $expected = ['certFingerprint' => [strtolower($fingerprint)]];

        $this->assertEquals($expected, $res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyFingerprintRemoveColons()
    {
        $fingerprint = 'f:i:n:g:e:r:p:r:i:n:t';
        $config = new Configuration(['certFingerprint' => $fingerprint], 'test');

        $res = Crypto::loadPublicKey($config);
        $expected = ['certFingerprint' => [str_replace(':', '', $fingerprint)]];

        $this->assertEquals($expected, $res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyNotX509Certificate(): void
    {
        $config = new Configuration(
            [
                'keys' => [
                    [
                        'X509Certificate' => '',
                        'type' => 'NotX509Certificate',
                        'signing' => true
                    ],
                ],
            ],
            'test'
        );

        $res = Crypto::loadPublicKey($config);

        $this->assertNull($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyNotSigning(): void
    {
        $config = new Configuration(
            [
                'keys' => [
                    [
                        'X509Certificate' => '',
                        'type' => 'X509Certificate',
                        'signing' => false
                    ],
                ],
            ],
            'test'
        );

        $res = Crypto::loadPublicKey($config);

        $this->assertNull($res);
    }


    /**
     * @covers \SimpleSAML\Utils\Crypto::loadPublicKey
     * @return void
     */
    public function testLoadPublicKeyBasic(): void
    {
        $x509certificate = 'x509certificate';
        $config = new Configuration(
            [
                'keys' => [
                    [
                        'X509Certificate' => $x509certificate,
                        'type' => 'X509Certificate',
                        'signing' => true
                    ],
                ],
            ],
            'test'
        );

        /** @var array $pubkey */
        $pubkey = Crypto::loadPublicKey($config);
        $res = $pubkey['certData'];
        $expected = $x509certificate;

        $this->assertEquals($expected, $res);
    }
}