Skip to content

Commit 98a42a6

Browse files
committed
Replace PrivateKey class with the one from xml-security
1 parent 6e3c2fe commit 98a42a6

File tree

5 files changed

+17
-127
lines changed

5 files changed

+17
-127
lines changed

src/Certificate/PrivateKey.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Certificate/PrivateKeyLoader.php

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,26 @@
44

55
namespace SimpleSAML\SAML2\Certificate;
66

7-
use SimpleSAML\SAML2\Certificate\PrivateKey;
87
use SimpleSAML\SAML2\Configuration\DecryptionProvider;
98
use SimpleSAML\SAML2\Configuration\PrivateKey as PrivateKeyConfiguration;
10-
use SimpleSAML\SAML2\Constants as C;
119
use SimpleSAML\SAML2\Utilities\ArrayCollection;
12-
use SimpleSAML\SAML2\Utilities\File;
13-
use SimpleSAML\XMLSecurity\XMLSecurityKey;
10+
use SimpleSAML\XMLSecurity\Key\PrivateKey;
11+
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
1412

1513
class PrivateKeyLoader
1614
{
1715
/**
1816
* Loads a private key based on the configuration given.
1917
*
2018
* @param \SimpleSAML\SAML2\Configuration\PrivateKey $key
21-
* @return \SimpleSAML\SAML2\Certificate\PrivateKey
19+
* @return \SimpleSAML\XMLSecurity\Key\PrivateKey
2220
*/
2321
public function loadPrivateKey(PrivateKeyConfiguration $key): PrivateKey
2422
{
25-
if ($key->isFile()) {
26-
$privateKey = File::getFileContents($key->getFilePath());
27-
} else {
28-
$privateKey = $key->getContents();
29-
}
30-
31-
return PrivateKey::create($privateKey, $key->getPassPhrase());
23+
return PrivateKey::fromFile(
24+
$key->isFile() ? $key->getFilePath() : $key->getContents(),
25+
$key->getPassPhrase(),
26+
);
3227
}
3328

3429

@@ -46,8 +41,7 @@ public function loadDecryptionKeys(
4641

4742
$senderSharedKey = $identityProvider->getSharedKey();
4843
if ($senderSharedKey !== null) {
49-
$key = new XMLSecurityKey(C::BLOCK_ENC_AES128);
50-
$key->loadKey($senderSharedKey);
44+
$key = new SymmetricKey($senderSharedKey);
5145
$decryptionKeys->add($key);
5246

5347
return $decryptionKeys;
@@ -56,32 +50,13 @@ public function loadDecryptionKeys(
5650
$newPrivateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_NEW);
5751
if ($newPrivateKey instanceof PrivateKeyConfiguration) {
5852
$loadedKey = $this->loadPrivateKey($newPrivateKey);
59-
$decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
53+
$decryptionKeys->add($loadedKey);
6054
}
6155

6256
$privateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_DEFAULT, true);
6357
$loadedKey = $this->loadPrivateKey($privateKey);
64-
$decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
58+
$decryptionKeys->add($loadedKey);
6559

6660
return $decryptionKeys;
6761
}
68-
69-
70-
/**
71-
* @param \SimpleSAML\SAML2\Certificate\PrivateKey $privateKey
72-
* @throws \Exception
73-
* @return \SimpleSAML\XMLSecurity\XMLSecurityKey
74-
*/
75-
private function convertPrivateKeyToRsaKey(PrivateKey $privateKey): XMLSecurityKey
76-
{
77-
$key = new XMLSecurityKey(XMLSecurityKey::RSA_1_5, ['type' => 'private']);
78-
$passphrase = $privateKey->getPassphrase();
79-
if ($passphrase) {
80-
$key->passphrase = $passphrase;
81-
}
82-
83-
$key->loadKey($privateKey->getKeyAsString());
84-
85-
return $key;
86-
}
8762
}

src/Configuration/IdentityProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SimpleSAML\SAML2\Configuration;
66

77
use RuntimeException;
8+
use SimpleSAML\XMLSecurity\Constants as C;
89

910
use function array_filter;
1011
use function array_pop;
@@ -108,7 +109,7 @@ public function getPrivateKey(string $name, ?bool $required = null)
108109
*/
109110
public function getBlacklistedAlgorithms(): ?array
110111
{
111-
return $this->get('blacklistedEncryptionAlgorithms');
112+
return $this->get('blacklistedEncryptionAlgorithms', [C::KEY_TRANSPORT_RSA_1_5]);
112113
}
113114

114115

tests/SAML2/Certificate/PrivateKeyLoaderTest.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\Attributes\Group;
1010
use PHPUnit\Framework\TestCase;
11-
use SimpleSAML\SAML2\Certificate\PrivateKey;
1211
use SimpleSAML\SAML2\Certificate\PrivateKeyLoader;
1312
use SimpleSAML\SAML2\Configuration\PrivateKey as ConfPrivateKey;
13+
use SimpleSAML\XMLSecurity\Key\PrivateKey;
1414
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
1515

1616
/**
@@ -42,11 +42,6 @@ public function testLoadingAConfiguredPrivateKeyReturnsACertificatePrivateKey(
4242
$resultingKey = self::$privateKeyLoader->loadPrivateKey($configuredKey);
4343

4444
$this->assertInstanceOf(PrivateKey::class, $resultingKey);
45-
$this->assertEquals(
46-
trim($resultingKey->getKeyAsString()),
47-
PEMCertificatesMock::loadPlainKeyFile(PEMCertificatesMock::BROKEN_PRIVATE_KEY),
48-
);
49-
$this->assertEquals($resultingKey->getPassphrase(), $configuredKey->getPassPhrase());
5045
}
5146

5247

@@ -58,24 +53,18 @@ public function testLoadingAConfiguredPrivateKeyReturnsACertificatePrivateKey(
5853
public static function privateKeyTestProvider(): array
5954
{
6055
return [
61-
'no passphrase' => [
62-
new ConfPrivateKey(
63-
PEMCertificatesMock::buildKeysPath(PEMCertificatesMock::BROKEN_PRIVATE_KEY),
64-
ConfPrivateKey::NAME_DEFAULT,
65-
),
66-
],
6756
'with passphrase' => [
6857
new ConfPrivateKey(
69-
PEMCertificatesMock::buildKeysPath(PEMCertificatesMock::BROKEN_PRIVATE_KEY),
58+
PEMCertificatesMock::buildKeysPath(PEMCertificatesMock::PRIVATE_KEY),
7059
ConfPrivateKey::NAME_DEFAULT,
71-
'foo bar baz',
60+
'1234',
7261
),
7362
],
7463
'private key as contents' => [
7564
new ConfPrivateKey(
76-
PEMCertificatesMock::loadPlainKeyFile(PEMCertificatesMock::BROKEN_PRIVATE_KEY),
65+
PEMCertificatesMock::loadPlainKeyFile(PEMCertificatesMock::PRIVATE_KEY),
7766
ConfPrivateKey::NAME_DEFAULT,
78-
'',
67+
'1234',
7968
false,
8069
),
8170
],

tests/SAML2/Certificate/PrivateKeyTest.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)