Passed
Pull Request — master (#26)
by Jaime Pérez
02:36
created

EncryptableElementTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 32 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML;
6
7
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
8
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
9
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
10
use SimpleSAML\XMLSecurity\Constants;
11
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
12
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
13
use SimpleSAML\XMLSecurity\XML\xenc\CipherData;
14
use SimpleSAML\XMLSecurity\XML\xenc\CipherValue;
15
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
16
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
17
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod;
18
19
/**
20
 * Trait aggregating functionality for elements that can be encrypted.
21
 *
22
 * @package simplesamlphp/xml-security
23
 */
24
trait EncryptableElementTrait
25
{
26
    /**
27
     * The length of the session key to use when encrypting.
28
     *
29
     * Override to change it if desired.
30
     *
31
     * @var int
32
     */
33
    protected int $sessionKeyLen = 16;
34
35
    /**
36
     * The identifier of the block cipher to use to encrypt this object.
37
     *
38
     * Override to change it if desired.
39
     *
40
     * @var string
41
     */
42
    protected string $blockCipherAlgId = Constants::BLOCK_ENC_AES256_GCM;
43
44
    /**
45
     * The type of the encrypted data.
46
     *
47
     * @var string|null
48
     */
49
    protected ?string $dataType = null;
50
51
52
    /**
53
     * Encryt this object.
54
     *
55
     * @param \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface $encryptor The encryptor to use,
56
     * either to encrypt the object itself, or to encrypt a session key (if the encryptor implements a key transport
57
     * algorithm).
58
     *
59
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
60
     */
61
    public function encrypt(EncryptionAlgorithmInterface $encryptor): EncryptedData
62
    {
63
        $keyInfo = null;
64
        if (in_array($encryptor->getAlgorithmId(), Constants::$KEY_TRANSPORT_ALGORITHMS)) {
65
            // the encryptor uses a key transport algorithm, use that to generate a session key
66
            $sessionKey = SymmetricKey::generate($this->sessionKeyLen);
67
68
            $encryptedKey = EncryptedKey::fromKey(
69
                $sessionKey,
70
                $encryptor,
71
                new EncryptionMethod($encryptor->getAlgorithmId())
72
            );
73
74
            $keyInfo = new KeyInfo([$encryptedKey]);
75
76
            $factory = new EncryptionAlgorithmFactory($this->getBlacklistedAlgorithms());
77
            $encryptor = $factory->getAlgorithm($this->blockCipherAlgId, $sessionKey);
78
            $encryptor->setBackend($this->getEncryptionBackend());
79
        }
80
81
        return new EncryptedData(
82
            new CipherData(
83
                new CipherValue(
84
                    base64_encode($encryptor->encrypt($this->__toString()))
85
                )
86
            ),
87
            null,
88
            $this->dataType,
89
            null,
90
            null,
91
            new EncryptionMethod($encryptor->getAlgorithmId()),
92
            $keyInfo
93
        );
94
    }
95
96
97
    /**
98
     * Get the encryption backend to use for any encryption operation.
99
     *
100
     * @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null The encryption backend to use, or null if we
101
     * want to use the default.
102
     */
103
    abstract public function getEncryptionBackend(): ?EncryptionBackend;
104
105
106
    /**
107
     * Get the list of algorithms that are blacklisted for any encryption operation.
108
     *
109
     * @return string[]|null An array with all algorithm identifiers that are blacklisted, or null if we want to use the
110
     * defaults.
111
     */
112
    abstract public function getBlacklistedAlgorithms(): ?array;
113
114
115
    /**
116
     * Return a string representation of this object.
117
     *
118
     * @return string
119
     */
120
    abstract public function __toString(): string;
121
}
122