Issues (234)

src/XML/EncryptableElementTrait.php (4 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML;
6
7
use SimpleSAML\XMLSchema\Type\AnyURIValue;
8
use SimpleSAML\XMLSchema\Type\Base64BinaryValue;
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
     *
52
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
53
     */
54
    public function encrypt(EncryptionAlgorithmInterface $encryptor): EncryptedData
55
    {
56
        $keyInfo = null;
57
        if (in_array($encryptor->getAlgorithmId(), C::$KEY_TRANSPORT_ALGORITHMS)) {
58
            // the encryptor uses a key transport algorithm, use that to generate a session key
59
            $sessionKey = SymmetricKey::generate($this->sessionKeyLen);
60
61
            $encryptedKey = EncryptedKey::fromKey(
62
                $sessionKey,
63
                $encryptor,
64
                new EncryptionMethod(
65
                    AnyURIValue::fromString($encryptor->getAlgorithmId()),
66
                ),
67
            );
68
69
            $keyInfo = new KeyInfo([$encryptedKey]);
70
71
            $factory = new EncryptionAlgorithmFactory(
72
                $this->getBlacklistedAlgorithms() ?? EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
73
            );
74
            $encryptor = $factory->getAlgorithm($this->blockCipherAlgId, $sessionKey);
75
            $encryptor->setBackend($this->getEncryptionBackend());
76
        }
77
78
        $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

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