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 as C; |
||
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 = 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($encryptor->getAlgorithmId()), |
||
65 | ); |
||
66 | |||
67 | $keyInfo = new KeyInfo([$encryptedKey]); |
||
68 | |||
69 | $factory = new EncryptionAlgorithmFactory( |
||
70 | $this->getBlacklistedAlgorithms() ?? EncryptionAlgorithmFactory::DEFAULT_BLACKLIST, |
||
71 | ); |
||
72 | $encryptor = $factory->getAlgorithm($this->blockCipherAlgId, $sessionKey); |
||
73 | $encryptor->setBackend($this->getEncryptionBackend()); |
||
74 | } |
||
75 | |||
76 | $xmlRepresentation = $this->toXML(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
77 | |||
78 | return new EncryptedData( |
||
79 | new CipherData( |
||
80 | new CipherValue( |
||
81 | base64_encode($encryptor->encrypt($xmlRepresentation->ownerDocument->saveXML($xmlRepresentation))), |
||
82 | ), |
||
83 | ), |
||
84 | null, |
||
85 | C::XMLENC_ELEMENT, |
||
86 | null, |
||
87 | null, |
||
88 | new EncryptionMethod($encryptor->getAlgorithmId()), |
||
89 | $keyInfo, |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | |||
94 | /** |
||
95 | * Get the encryption backend to use for any encryption operation. |
||
96 | * |
||
97 | * @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null The encryption backend to use, or null if we |
||
98 | * want to use the default. |
||
99 | */ |
||
100 | abstract public function getEncryptionBackend(): ?EncryptionBackend; |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Get the list of algorithms that are blacklisted for any encryption operation. |
||
105 | * |
||
106 | * @return string[]|null An array with all algorithm identifiers that are blacklisted, or null to use this |
||
107 | * libraries default. |
||
108 | */ |
||
109 | abstract public function getBlacklistedAlgorithms(): ?array; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Return a string representation of this object. |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | abstract public function __toString(): string; |
||
118 | } |
||
119 |