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

EncryptedElementTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 159
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fromUnencryptedElement() 0 37 7
A getEncryptedData() 0 3 1
A fromXML() 0 12 1
A setEncryptedKeys() 0 8 1
A setEncryptedData() 0 3 1
A getEncryptedKeys() 0 3 1
A toXML() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML;
6
7
use DOMElement;
8
use RobRichards\XMLSecLibs\XMLSecEnc;
9
use RobRichards\XMLSecLibs\XMLSecurityKey;
10
use SAML2\XML\xenc\EncryptedData;
11
use SAML2\XML\xenc\EncryptedKey;
12
use Webmozart\Assert\Assert;
13
14
/**
15
 * Trait aggregating functionality for encrypted elements.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
trait EncryptedElementTrait
20
{
21
22
    /**
23
     * The current encrypted ID.
24
     *
25
     * @var \SAML2\XML\xenc\EncryptedData
26
     * @psalm-suppress PropertyNotSetInConstructor
27
     */
28
    protected $encryptedData;
29
30
    /**
31
     * A list of encrypted keys.
32
     *
33
     * @var \SAML2\XML\xenc\EncryptedKey[]
34
     */
35
    protected $encryptedKeys = [];
36
37
38
    /**
39
     * Constructor for encrypted elements.
40
     *
41
     * @param \SAML2\XML\xenc\EncryptedData $encryptedData The EncryptedData object.
42
     * @param \SAML2\XML\xenc\EncryptedKey[] $encryptedKeys An array of zero or more EncryptedKey objects.
43
     */
44
    public function __construct(EncryptedData $encryptedData, array $encryptedKeys)
45
    {
46
        $this->setEncryptedData($encryptedData);
47
        $this->setEncryptedKeys($encryptedKeys);
48
    }
49
50
51
    /**
52
     * Get the EncryptedData object.
53
     *
54
     * @return \SAML2\XML\xenc\EncryptedData
55
     */
56
    public function getEncryptedData(): EncryptedData
57
    {
58
        return $this->encryptedData;
59
    }
60
61
62
    /**
63
     * @param \SAML2\XML\xenc\EncryptedData $encryptedData
64
     */
65
    protected function setEncryptedData(EncryptedData $encryptedData): void
66
    {
67
        $this->encryptedData = $encryptedData;
68
    }
69
70
71
    /**
72
     * Get the array of EncryptedKey objects
73
     *
74
     * @return \SAML2\XML\xenc\EncryptedKey[]
75
     */
76
    public function getEncryptedKeys(): array
77
    {
78
        return $this->encryptedKeys;
79
    }
80
81
82
    /**
83
     * @param \SAML2\XML\xenc\EncryptedKey[] $encryptedKeys
84
     */
85
    protected function setEncryptedKeys(array $encryptedKeys): void
86
    {
87
        Assert::allIsInstanceOf(
88
            $encryptedKeys,
89
            EncryptedKey::class,
90
            'All encrypted keys in <' . $this->getQualifiedName() . '> must be an instance of EncryptedKey.'
0 ignored issues
show
Bug introduced by
It seems like getQualifiedName() 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

90
            'All encrypted keys in <' . $this->/** @scrutinizer ignore-call */ getQualifiedName() . '> must be an instance of EncryptedKey.'
Loading history...
91
        );
92
        $this->encryptedKeys = $encryptedKeys;
93
    }
94
95
96
    /**
97
     * Create an encrypted element from a given unencrypted element and a key.
98
     *
99
     * @param \SAML2\XML\AbstractXMLElement $element
100
     * @param \RobRichards\XMLSecLibs\XMLSecurityKey $key
101
     *
102
     * @return \SAML2\EncryptedElementInterface
0 ignored issues
show
Bug introduced by
The type SAML2\EncryptedElementInterface 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...
103
     * @throws \Exception
104
     */
105
    public static function fromUnencryptedElement(
106
        AbstractXMLElement $element,
107
        XMLSecurityKey $key
108
    ): EncryptedElementInterface {
109
        $xml = $element->toXML();
110
111
        Utils::getContainer()->debugMessage($xml, 'encrypt');
0 ignored issues
show
Bug introduced by
The type SAML2\XML\Utils 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...
112
113
        $enc = new XMLSecEnc();
114
        $enc->setNode($xml);
115
        $enc->type = XMLSecEnc::Element;
116
117
        switch ($key->type) {
118
            case XMLSecurityKey::TRIPLEDES_CBC:
119
            case XMLSecurityKey::AES128_CBC:
120
            case XMLSecurityKey::AES192_CBC:
121
            case XMLSecurityKey::AES256_CBC:
122
                $symmetricKey = $key;
123
                break;
124
125
            case XMLSecurityKey::RSA_1_5:
126
            case XMLSecurityKey::RSA_OAEP_MGF1P:
127
                $symmetricKey = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
128
                $symmetricKey->generateSessionKey();
129
130
                $enc->encryptKey($key, $symmetricKey);
131
132
                break;
133
134
            default:
135
                throw new \Exception('Unknown key type for encryption: ' . $key->type);
136
        }
137
138
        $dom = $enc->encryptNode($symmetricKey);
139
        /** @var \SAML2\XML\xenc\EncryptedData $encData */
140
        $encData = EncryptedData::fromXML($dom);
141
        return new static($encData, []);
1 ignored issue
show
Bug Best Practice introduced by
The expression return new static($encData, array()) returns the type SAML2\XML\EncryptedElementTrait which is incompatible with the type-hinted return SAML2\XML\EncryptedElementInterface.
Loading history...
142
    }
143
144
145
    /**
146
     * @inheritDoc
147
     * @return \SAML2\XML\AbstractXMLElement
148
     */
149
    public static function fromXML(DOMElement $xml): object
150
    {
151
        Assert::same($xml->localName, self::getClassName(static::class));
152
        Assert::same($xml->namespaceURI, static::NS);
1 ignored issue
show
Bug introduced by
The constant SAML2\XML\EncryptedElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
153
154
        $ed = EncryptedData::getChildrenOfClass($xml);
155
        Assert::count($ed, 1, 'No more or less than one EncryptedData element allowed in ' .
156
            AbstractXMLElement::getClassName(static::class) . '.');
157
158
        $ek = EncryptedKey::getChildrenOfClass($xml);
159
160
        return new static($ed[0], $ek);
1 ignored issue
show
Bug Best Practice introduced by
The expression return new static($ed[0], $ek) returns the type SAML2\XML\EncryptedElementTrait which is incompatible with the documented return type SAML2\XML\AbstractXMLElement.
Loading history...
161
    }
162
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function toXML(DOMElement $parent = null): DOMElement
168
    {
169
        $e = $this->instantiateParentElement($parent);
0 ignored issues
show
Bug introduced by
It seems like instantiateParentElement() 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

169
        /** @scrutinizer ignore-call */ 
170
        $e = $this->instantiateParentElement($parent);
Loading history...
170
171
        $this->encryptedData->toXML($e);
172
173
        foreach ($this->encryptedKeys as $key) {
174
            $key->toXML($e);
175
        }
176
177
        return $e;
178
    }
179
}
180