1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\Alg\Signature; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SimpleSAML\XMLSecurity\Alg\Signature\HMAC; |
9
|
|
|
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory; |
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\Key\SymmetricKey; |
15
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests for SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory |
19
|
|
|
* |
20
|
|
|
* @package simplesamlphp/xml-security |
21
|
|
|
*/ |
22
|
|
|
final class SignatureAlgorithmFactoryTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */ |
25
|
|
|
protected static SymmetricKey $skey; |
26
|
|
|
|
27
|
|
|
/** @var \SimpleSAML\XMLSecurity\Key\PublicKey */ |
28
|
|
|
protected static PublicKey $pkey; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public static function setUpBeforeClass(): void |
32
|
|
|
{ |
33
|
|
|
self::$skey = SymmetricKey::generate(16); |
34
|
|
|
self::$pkey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test obtaining the digest algorithm from a signature algorithm. |
40
|
|
|
*/ |
41
|
|
|
public function testGetDigestAlgorithm(): void |
42
|
|
|
{ |
43
|
|
|
$factory = new SignatureAlgorithmFactory([]); |
44
|
|
|
|
45
|
|
|
foreach (C::$HMAC_DIGESTS as $signature => $digest) { |
46
|
|
|
$alg = $factory->getAlgorithm($signature, self::$skey); |
47
|
|
|
$this->assertEquals($digest, $alg->getDigest()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
foreach (C::$RSA_DIGESTS as $signature => $digest) { |
51
|
|
|
$alg = $factory->getAlgorithm($signature, self::$pkey); |
52
|
|
|
$this->assertEquals($digest, $alg->getDigest()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Test for unsupported algorithms. |
59
|
|
|
*/ |
60
|
|
|
public function testGetUnknownAlgorithm(): void |
61
|
|
|
{ |
62
|
|
|
$factory = new SignatureAlgorithmFactory([]); |
63
|
|
|
$this->expectException(UnsupportedAlgorithmException::class); |
64
|
|
|
$factory->getAlgorithm('Unsupported algorithm identifier', self::$skey); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test for blacklisted algorithms. |
70
|
|
|
*/ |
71
|
|
|
public function testBlacklistedAlgorithm(): void |
72
|
|
|
{ |
73
|
|
|
$factory = new SignatureAlgorithmFactory([C::SIG_RSA_SHA1]); |
74
|
|
|
$algorithm = $factory->getAlgorithm(C::SIG_HMAC_SHA1, self::$skey); |
75
|
|
|
$this->assertInstanceOf(HMAC::class, $algorithm); |
76
|
|
|
$this->assertEquals(C::SIG_HMAC_SHA1, $algorithm->getAlgorithmId()); |
77
|
|
|
$this->assertEquals(self::$skey, $algorithm->getKey()); |
78
|
|
|
|
79
|
|
|
$this->expectException(BlacklistedAlgorithmException::class); |
80
|
|
|
$factory->getAlgorithm(C::SIG_RSA_SHA1, self::$pkey); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|