Issues (311)

src/XML/EncryptableElementTrait.php (6 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML;
6
7
use SimpleSAML\XMLSchema\Type\AnyURIValue;
0 ignored issues
show
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...
8
use SimpleSAML\XMLSchema\Type\Base64BinaryValue;
0 ignored issues
show
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...
9
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
0 ignored issues
show
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...
10
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
11
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
12
use SimpleSAML\XMLSecurity\Constants as C;
0 ignored issues
show
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\xenc\CipherData;
16
use SimpleSAML\XMLSecurity\XML\xenc\CipherValue;
0 ignored issues
show
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...
17
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
18
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
19
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod;
20
21
/**
22
 * Trait aggregating functionality for elements that can be encrypted.
23
 *
24
 * @package simplesamlphp/xml-security
25
 *
26
 * @phpstan-ignore trait.unused
27
 */
28
trait EncryptableElementTrait
29
{
30
    /**
31
     * The length of the session key to use when encrypting.
32
     *
33
     * Override to change it if desired.
34
     */
35
    protected int $sessionKeyLen = 16;
36
37
    /**
38
     * The identifier of the block cipher to use to encrypt this object.
39
     *
40
     * Override to change it if desired.
41
     */
42
    protected string $blockCipherAlgId = C::BLOCK_ENC_AES256_GCM;
43
44
45
    /**
46
     * Encryt this object.
47
     *
48
     * @param \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface $encryptor The encryptor to use,
49
     * either to encrypt the object itself, or to encrypt a session key (if the encryptor implements a key transport
50
     * algorithm).
51
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $type
52
     *
53
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
54
     */
55
    public function encrypt(EncryptionAlgorithmInterface $encryptor, ?AnyURIValue $type = null): EncryptedData
56
    {
57
        $keyInfo = null;
58
        if (in_array($encryptor->getAlgorithmId(), C::$KEY_TRANSPORT_ALGORITHMS)) {
59
            // the encryptor uses a key transport algorithm, use that to generate a session key
60
            $sessionKey = SymmetricKey::generate($this->sessionKeyLen);
61
62
            $encryptedKey = EncryptedKey::fromKey(
63
                $sessionKey,
64
                $encryptor,
65
                new EncryptionMethod(
66
                    AnyURIValue::fromString($encryptor->getAlgorithmId()),
67
                ),
68
            );
69
70
            $keyInfo = new KeyInfo([$encryptedKey]);
71
72
            $factory = new EncryptionAlgorithmFactory(
73
                $this->getBlacklistedAlgorithms() ?? EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
74
            );
75
            $encryptor = $factory->getAlgorithm($this->blockCipherAlgId, $sessionKey);
76
            $encryptor->setBackend($this->getEncryptionBackend());
77
        }
78
79
        $xmlRepresentation = $this->toXML();
0 ignored issues
show
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

79
        /** @scrutinizer ignore-call */ 
80
        $xmlRepresentation = $this->toXML();
Loading history...
80
81
        return new EncryptedData(
82
            new CipherData(
83
                new CipherValue(
84
                    Base64BinaryValue::fromString(
85
                        base64_encode($encryptor->encrypt(
86
                            $xmlRepresentation->ownerDocument->saveXML($xmlRepresentation),
87
                        )),
88
                    ),
89
                ),
90
            ),
91
            null,
92
            $type,
93
            null,
94
            null,
95
            new EncryptionMethod(
96
                AnyURIValue::fromString($encryptor->getAlgorithmId()),
97
            ),
98
            $keyInfo,
99
        );
100
    }
101
102
103
    /**
104
     * Get the encryption backend to use for any encryption operation.
105
     *
106
     * @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null The encryption backend to use, or null if we
107
     * want to use the default.
108
     */
109
    abstract public function getEncryptionBackend(): ?EncryptionBackend;
110
111
112
    /**
113
     * Get the list of algorithms that are blacklisted for any encryption operation.
114
     *
115
     * @return string[]|null An array with all algorithm identifiers that are blacklisted, or null to use this
116
     * libraries default.
117
     */
118
    abstract public function getBlacklistedAlgorithms(): ?array;
119
120
121
    /**
122
     * Return a string representation of this object.
123
     */
124
    abstract public function __toString(): string;
125
}
126