Issues (81)

src/XML/EncryptableElementTrait.php (1 issue)

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

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