Passed
Push — master ( a10418...0f47bf )
by Tim
04:00 queued 01:19
created

PrivateKeyLoader::convertPrivateKeyToRsaKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Certificate;
6
7
use SimpleSAML\SAML2\Configuration\DecryptionProvider;
8
use SimpleSAML\SAML2\Configuration\PrivateKey as PrivateKeyConfiguration;
9
use SimpleSAML\SAML2\Utilities\ArrayCollection;
10
use SimpleSAML\XMLSecurity\Key\PrivateKey;
11
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
12
13
class PrivateKeyLoader
14
{
15
    /**
16
     * Loads a private key based on the configuration given.
17
     *
18
     * @param \SimpleSAML\SAML2\Configuration\PrivateKey $key
19
     * @return \SimpleSAML\XMLSecurity\Key\PrivateKey
20
     */
21
    public function loadPrivateKey(PrivateKeyConfiguration $key): PrivateKey
22
    {
23
        return PrivateKey::fromFile(
24
            $key->isFile() ? $key->getFilePath() : $key->getContents(),
25
            $key->getPassPhrase(),
26
        );
27
    }
28
29
30
    /**
31
     * @param \SimpleSAML\SAML2\Configuration\DecryptionProvider $identityProvider
32
     * @param \SimpleSAML\SAML2\Configuration\DecryptionProvider $serviceProvider
33
     * @throws \Exception
34
     * @return \SimpleSAML\SAML2\Utilities\ArrayCollection
35
     */
36
    public function loadDecryptionKeys(
37
        DecryptionProvider $identityProvider,
38
        DecryptionProvider $serviceProvider,
39
    ): ArrayCollection {
40
        $decryptionKeys = new ArrayCollection();
41
42
        $senderSharedKey = $identityProvider->getSharedKey();
43
        if ($senderSharedKey !== null) {
44
            $key = new SymmetricKey($senderSharedKey);
45
            $decryptionKeys->add($key);
46
47
            return $decryptionKeys;
48
        }
49
50
        $newPrivateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_NEW);
51
        if ($newPrivateKey instanceof PrivateKeyConfiguration) {
52
            $loadedKey = $this->loadPrivateKey($newPrivateKey);
53
            $decryptionKeys->add($loadedKey);
54
        }
55
56
        $privateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_DEFAULT, true);
57
        $loadedKey  = $this->loadPrivateKey($privateKey);
58
        $decryptionKeys->add($loadedKey);
59
60
        return $decryptionKeys;
61
    }
62
}
63