Passed
Push — master ( d81bf3...5005c4 )
by Tim
01:40
created

EncryptableElementTrait::encrypt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 27
nc 2
nop 1
dl 0
loc 44
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML;
6
7
use SimpleSAML\SAML2\Compat\ContainerSingleton;
8
use SimpleSAML\XMLSchema\Type\AnyURIValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\AnyURIValue 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...
9
use SimpleSAML\XMLSchema\Type\Base64BinaryValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\Base64BinaryValue 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...
10
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\A...ryptionAlgorithmFactory 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...
11
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
12
use SimpleSAML\XMLSecurity\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\Constants 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
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
14
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
15
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait as ParentEncryptableElementTrait;
16
use SimpleSAML\XMLSecurity\XML\xenc\CipherData;
17
use SimpleSAML\XMLSecurity\XML\xenc\CipherValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\xenc\CipherValue 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...
18
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
19
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
20
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod;
21
22
use function in_array;
23
24
/**
25
 * Trait aggregating functionality for elements that are encrypted.
26
 *
27
 * @package simplesamlphp/saml2
28
 */
29
trait EncryptableElementTrait
30
{
31
    use ParentEncryptableElementTrait;
32
33
34
    /**
35
     * Encryt this object.
36
     *
37
     * @param \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface $encryptor The encryptor to use,
38
     * either to encrypt the object itself, or to encrypt a session key (if the encryptor implements a key transport
39
     * algorithm).
40
     *
41
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
42
     */
43
    public function encrypt(EncryptionAlgorithmInterface $encryptor): EncryptedData
44
    {
45
        $keyInfo = null;
46
        if (in_array($encryptor->getAlgorithmId(), C::$KEY_TRANSPORT_ALGORITHMS)) {
47
            // the encryptor uses a key transport algorithm, use that to generate a session key
48
            $sessionKey = SymmetricKey::generate($this->sessionKeyLen);
49
50
            $encryptedKey = EncryptedKey::fromKey(
51
                $sessionKey,
52
                $encryptor,
53
                new EncryptionMethod(
54
                    AnyURIValue::fromString($encryptor->getAlgorithmId()),
55
                ),
56
            );
57
58
            $keyInfo = new KeyInfo([$encryptedKey]);
59
60
            $factory = new EncryptionAlgorithmFactory(
61
                $this->getBlacklistedAlgorithms() ?? EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
62
            );
63
            $encryptor = $factory->getAlgorithm($this->blockCipherAlgId, $sessionKey);
64
            $encryptor->setBackend($this->getEncryptionBackend());
65
        }
66
67
        $xmlRepresentation = $this->toXML();
0 ignored issues
show
Bug introduced by
It seems like toXML() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        /** @scrutinizer ignore-call */ 
68
        $xmlRepresentation = $this->toXML();
Loading history...
68
69
        return new EncryptedData(
70
            new CipherData(
71
                new CipherValue(
72
                    Base64BinaryValue::fromString(
73
                        base64_encode($encryptor->encrypt(
74
                            $xmlRepresentation->ownerDocument->saveXML($xmlRepresentation),
75
                        )),
76
                    ),
77
                ),
78
            ),
79
            null,
80
            AnyURIValue::fromString(C::XMLENC_ELEMENT),
81
            null,
82
            null,
83
            new EncryptionMethod(
84
                AnyURIValue::fromString($encryptor->getAlgorithmId()),
85
            ),
86
            $keyInfo,
87
        );
88
    }
89
90
91
    /**
92
     * @return array|null
93
     */
94
    public function getBlacklistedAlgorithms(): ?array
95
    {
96
        $container = ContainerSingleton::getInstance();
97
        return $container->getBlacklistedEncryptionAlgorithms();
98
    }
99
}
100