|
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\Key\PrivateKey; |
|
10
|
|
|
|
|
11
|
|
|
use function file_get_contents; |
|
12
|
|
|
use function openssl_pkey_get_details; |
|
13
|
|
|
use function openssl_pkey_get_private; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Tests for SimpleSAML\XMLSecurity\Key\PrivateKey |
|
17
|
|
|
* |
|
18
|
|
|
* @package SimpleSAML\XMLSecurity\Key |
|
19
|
|
|
*/ |
|
20
|
|
|
final class PrivateKeyTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var array<string, string|int> */ |
|
23
|
|
|
protected static array $privKey = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected static string $f; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Initialize the test by loading the file ourselves. |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function setUpBeforeClass(): void |
|
33
|
|
|
{ |
|
34
|
|
|
self::$f = file_get_contents('resources/keys/privkey.pem'); |
|
35
|
|
|
self::$privKey = openssl_pkey_get_details(openssl_pkey_get_private(self::$f)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Cover basic creation and retrieval. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testCreation(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$k = new PrivateKey(PEM::fromString(self::$f)); |
|
45
|
|
|
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($k->getMaterial())); |
|
46
|
|
|
$this->assertEquals(self::$privKey['key'], $keyDetails['key']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Test creation from a file containing the PEM-encoded private key. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testFromFile(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$k = PrivateKey::fromFile('file://./resources/keys/privkey.pem'); |
|
56
|
|
|
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($k->getMaterial())); |
|
57
|
|
|
$this->assertEquals(self::$privKey['key'], $keyDetails['key']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Test creation from a file without file:// prefix succeeds. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testFromFileNoPrefix(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$k = PrivateKey::fromFile('./resources/keys/privkey.pem'); |
|
67
|
|
|
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($k->getMaterial())); |
|
68
|
|
|
$this->assertEquals(self::$privKey['key'], $keyDetails['key']); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|