Skip to content

Commit 0f47bf5

Browse files
committed
Replace PrivateKey class with the one from xml-security
1 parent a104180 commit 0f47bf5

File tree

5 files changed

+17
-126
lines changed

5 files changed

+17
-126
lines changed

src/Certificate/PrivateKey.php

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

src/Certificate/PrivateKeyLoader.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +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;
109
use SimpleSAML\SAML2\Utilities\ArrayCollection;
11-
use SimpleSAML\SAML2\Utilities\File;
12-
use SimpleSAML\XMLSecurity\XMLSecurityKey;
10+
use SimpleSAML\XMLSecurity\Key\PrivateKey;
11+
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
1312

1413
class PrivateKeyLoader
1514
{
1615
/**
1716
* Loads a private key based on the configuration given.
1817
*
1918
* @param \SimpleSAML\SAML2\Configuration\PrivateKey $key
20-
* @return \SimpleSAML\SAML2\Certificate\PrivateKey
19+
* @return \SimpleSAML\XMLSecurity\Key\PrivateKey
2120
*/
2221
public function loadPrivateKey(PrivateKeyConfiguration $key): PrivateKey
2322
{
24-
if ($key->isFile()) {
25-
$privateKey = File::getFileContents($key->getFilePath());
26-
} else {
27-
$privateKey = $key->getContents();
28-
}
29-
30-
return PrivateKey::create($privateKey, $key->getPassPhrase());
23+
return PrivateKey::fromFile(
24+
$key->isFile() ? $key->getFilePath() : $key->getContents(),
25+
$key->getPassPhrase(),
26+
);
3127
}
3228

3329

@@ -45,8 +41,7 @@ public function loadDecryptionKeys(
4541

4642
$senderSharedKey = $identityProvider->getSharedKey();
4743
if ($senderSharedKey !== null) {
48-
$key = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
49-
$key->loadKey($senderSharedKey);
44+
$key = new SymmetricKey($senderSharedKey);
5045
$decryptionKeys->add($key);
5146

5247
return $decryptionKeys;
@@ -55,32 +50,13 @@ public function loadDecryptionKeys(
5550
$newPrivateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_NEW);
5651
if ($newPrivateKey instanceof PrivateKeyConfiguration) {
5752
$loadedKey = $this->loadPrivateKey($newPrivateKey);
58-
$decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
53+
$decryptionKeys->add($loadedKey);
5954
}
6055

6156
$privateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_DEFAULT, true);
6257
$loadedKey = $this->loadPrivateKey($privateKey);
63-
$decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
58+
$decryptionKeys->add($loadedKey);
6459

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

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)