|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\Key; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM; |
|
9
|
|
|
use SimpleSAML\XMLSecurity\Exception\IOException; |
|
10
|
|
|
use SimpleSAML\XMLSecurity\Key\PublicKey; |
|
11
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
|
12
|
|
|
|
|
13
|
|
|
use function file_get_contents; |
|
14
|
|
|
use function openssl_pkey_get_details; |
|
15
|
|
|
use function openssl_pkey_get_public; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Tests for SimpleSAML\XMLSecurity\Key\PublicKey. |
|
19
|
|
|
* |
|
20
|
|
|
* @package SimpleSAML\XMLSecurity\Key |
|
21
|
|
|
*/ |
|
22
|
|
|
final class PublicKeyTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var array */ |
|
25
|
|
|
protected static $pubKey = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected static string $f; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initialize the test by loading the file ourselves. |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function setUpBeforeClass(): void |
|
35
|
|
|
{ |
|
36
|
|
|
self::$f = PEMCertificatesMock::getPlainPublicKey(PEMCertificatesMock::PUBLIC_KEY); |
|
37
|
|
|
self::$pubKey = openssl_pkey_get_details(openssl_pkey_get_public(self::$f)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Cover basic creation and retrieval. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testCreation(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$k = new PublicKey(PEM::fromString(self::$f)); |
|
46
|
|
|
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_public($k->getMaterial())); |
|
47
|
|
|
$this->assertEquals(self::$pubKey['key'], $keyDetails['key']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Test creation from a file containing the PEM-encoded public key. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testFromFile(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$k = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY); |
|
57
|
|
|
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_public($k->getMaterial())); |
|
58
|
|
|
$this->assertEquals(self::$pubKey['key'], $keyDetails['key']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Test failure to create key from missing file. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testFromMissingFile(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$this->expectException(IOException::class); |
|
68
|
|
|
@PublicKey::fromFile('foo/bar'); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Test creation from the RSA public key details (modulus and exponent). |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testFromDetails(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$k = PublicKey::fromDetails(self::$pubKey['rsa']['n'], self::$pubKey['rsa']['e']); |
|
78
|
|
|
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_public($k->getMaterial())); |
|
79
|
|
|
$this->assertEquals(self::$pubKey['key'], $keyDetails['key']); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: