PrivateKeyLoader::convertPrivateKeyToRsaKey()   A
last analyzed

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\Certificate\PrivateKey;
8
use SimpleSAML\SAML2\Configuration\DecryptionProvider;
9
use SimpleSAML\SAML2\Configuration\PrivateKey as PrivateKeyConfiguration;
10
use SimpleSAML\SAML2\Utilities\ArrayCollection;
11
use SimpleSAML\SAML2\Utilities\File;
12
use SimpleSAML\XMLSecurity\XMLSecurityKey;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XMLSecurityKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class PrivateKeyLoader
15
{
16
    /**
17
     * Loads a private key based on the configuration given.
18
     *
19
     * @param \SimpleSAML\SAML2\Configuration\PrivateKey $key
20
     * @return \SimpleSAML\SAML2\Certificate\PrivateKey
21
     */
22
    public function loadPrivateKey(PrivateKeyConfiguration $key): PrivateKey
23
    {
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());
31
    }
32
33
34
    /**
35
     * @param \SimpleSAML\SAML2\Configuration\DecryptionProvider $identityProvider
36
     * @param \SimpleSAML\SAML2\Configuration\DecryptionProvider $serviceProvider
37
     * @throws \Exception
38
     * @return \SimpleSAML\SAML2\Utilities\ArrayCollection
39
     */
40
    public function loadDecryptionKeys(
41
        DecryptionProvider $identityProvider,
42
        DecryptionProvider $serviceProvider,
43
    ): ArrayCollection {
44
        $decryptionKeys = new ArrayCollection();
45
46
        $senderSharedKey = $identityProvider->getSharedKey();
47
        if ($senderSharedKey !== null) {
48
            $key = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
49
            $key->loadKey($senderSharedKey);
50
            $decryptionKeys->add($key);
51
52
            return $decryptionKeys;
53
        }
54
55
        $newPrivateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_NEW);
56
        if ($newPrivateKey instanceof PrivateKeyConfiguration) {
57
            $loadedKey = $this->loadPrivateKey($newPrivateKey);
58
            $decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
59
        }
60
61
        $privateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_DEFAULT, true);
62
        $loadedKey  = $this->loadPrivateKey($privateKey);
63
        $decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
64
65
        return $decryptionKeys;
66
    }
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
    }
86
}
87