EncryptableElementTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 102
rs 10
c 3
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 44 2
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;
10
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
11
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
12
use SimpleSAML\XMLSecurity\Constants as C;
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;
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
 * @phpstan-ignore trait.unused
26
 */
27
trait EncryptableElementTrait
28
{
29
    /**
30
     * The length of the session key to use when encrypting.
31
     *
32
     * Override to change it if desired.
33
     *
34
     * @var int
35
     */
36
    protected int $sessionKeyLen = 16;
37
38
    /**
39
     * The identifier of the block cipher to use to encrypt this object.
40
     *
41
     * Override to change it if desired.
42
     *
43
     * @var string
44
     */
45
    protected string $blockCipherAlgId = C::BLOCK_ENC_AES256_GCM;
46
47
48
    /**
49
     * Encryt this object.
50
     *
51
     * @param \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface $encryptor The encryptor to use,
52
     * either to encrypt the object itself, or to encrypt a session key (if the encryptor implements a key transport
53
     * algorithm).
54
     *
55
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
56
     */
57
    public function encrypt(EncryptionAlgorithmInterface $encryptor): EncryptedData
58
    {
59
        $keyInfo = null;
60
        if (in_array($encryptor->getAlgorithmId(), C::$KEY_TRANSPORT_ALGORITHMS)) {
61
            // the encryptor uses a key transport algorithm, use that to generate a session key
62
            $sessionKey = SymmetricKey::generate($this->sessionKeyLen);
63
64
            $encryptedKey = EncryptedKey::fromKey(
65
                $sessionKey,
66
                $encryptor,
67
                new EncryptionMethod(
68
                    AnyURIValue::fromString($encryptor->getAlgorithmId()),
69
                ),
70
            );
71
72
            $keyInfo = new KeyInfo([$encryptedKey]);
73
74
            $factory = new EncryptionAlgorithmFactory(
75
                $this->getBlacklistedAlgorithms() ?? EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
76
            );
77
            $encryptor = $factory->getAlgorithm($this->blockCipherAlgId, $sessionKey);
78
            $encryptor->setBackend($this->getEncryptionBackend());
79
        }
80
81
        $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

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