1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\Alg\KeyTransport; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory; |
9
|
|
|
use SimpleSAML\XMLSecurity\Alg\KeyTransport\RSA; |
10
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
11
|
|
|
use SimpleSAML\XMLSecurity\Exception\BlacklistedAlgorithmException; |
12
|
|
|
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException; |
13
|
|
|
use SimpleSAML\XMLSecurity\Key\PublicKey; |
14
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tests for \SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportALgorithmFactory |
18
|
|
|
* |
19
|
|
|
* @package simplesamlphp/xml-security |
20
|
|
|
*/ |
21
|
|
|
class KeyTransportAlgorithmFactoryTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var \SimpleSAML\XMLSecurity\Key\PublicKey */ |
24
|
|
|
protected static PublicKey $pkey; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public static function setUpBeforeClass(): void |
28
|
|
|
{ |
29
|
|
|
self::$pkey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Test for unsupported algorithms. |
35
|
|
|
*/ |
36
|
|
|
public function testGetUnknownAlgorithm(): void |
37
|
|
|
{ |
38
|
|
|
$factory = new KeyTransportAlgorithmFactory([]); |
39
|
|
|
$this->expectException(UnsupportedAlgorithmException::class); |
40
|
|
|
$factory->getAlgorithm('Unsupported algorithm identifier', self::$pkey); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Test the default blacklisted algorithms. |
46
|
|
|
*/ |
47
|
|
|
public function testDefaultBlacklistedAlgorithm(): void |
48
|
|
|
{ |
49
|
|
|
$factory = new KeyTransportAlgorithmFactory(); |
50
|
|
|
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$pkey); |
51
|
|
|
$this->assertInstanceOf(RSA::class, $algorithm); |
52
|
|
|
$this->assertEquals(C::KEY_TRANSPORT_OAEP, $algorithm->getAlgorithmId()); |
53
|
|
|
|
54
|
|
|
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$pkey); |
55
|
|
|
$this->assertInstanceOf(RSA::class, $algorithm); |
56
|
|
|
$this->assertEquals(C::KEY_TRANSPORT_OAEP_MGF1P, $algorithm->getAlgorithmId()); |
57
|
|
|
|
58
|
|
|
$this->expectException(BlacklistedAlgorithmException::class); |
59
|
|
|
$factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$pkey); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test for manually blacklisted algorithms. |
65
|
|
|
*/ |
66
|
|
|
public function testBlacklistedAlgorithm(): void |
67
|
|
|
{ |
68
|
|
|
$factory = new KeyTransportAlgorithmFactory([C::KEY_TRANSPORT_OAEP_MGF1P]); |
69
|
|
|
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$pkey); |
70
|
|
|
$this->assertInstanceOf(RSA::class, $algorithm); |
71
|
|
|
$this->assertEquals(C::KEY_TRANSPORT_OAEP, $algorithm->getAlgorithmId()); |
72
|
|
|
$this->assertEquals(self::$pkey, $algorithm->getKey()); |
73
|
|
|
|
74
|
|
|
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$pkey); |
75
|
|
|
$this->assertInstanceOf(RSA::class, $algorithm); |
76
|
|
|
$this->assertEquals(C::KEY_TRANSPORT_RSA_1_5, $algorithm->getAlgorithmId()); |
77
|
|
|
|
78
|
|
|
$this->expectException(BlacklistedAlgorithmException::class); |
79
|
|
|
$factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$pkey); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|