Passed
Push — release-4-alpha ( 53e910...362819 )
by Tim
02:18
created

EncryptedAssertion   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A toXML() 0 15 2
B setAssertion() 0 33 7
A getAssertion() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2;
6
7
use RobRichards\XMLSecLibs\XMLSecEnc;
8
use RobRichards\XMLSecLibs\XMLSecurityKey;
9
10
/**
11
 * Class handling encrypted assertions.
12
 *
13
 * @package SimpleSAMLphp
14
 */
15
class EncryptedAssertion
16
{
17
    /**
18
     * The current encrypted assertion.
19
     *
20
     * @var \DOMElement
21
     */
22
    private $encryptedData;
23
24
25
    /**
26
     * Constructor for SAML 2 encrypted assertions.
27
     *
28
     * @param \DOMElement|null $xml The encrypted assertion XML element.
29
     * @throws \Exception
30
     */
31
    public function __construct(\DOMElement $xml = null)
32
    {
33
        if ($xml === null) {
34
            return;
35
        }
36
37
        /** @var \DOMElement[] $data */
38
        $data = Utils::xpQuery($xml, './xenc:EncryptedData');
39
        if (empty($data)) {
40
            throw new \Exception('Missing encrypted data in <saml:EncryptedAssertion>.');
41
        } elseif (count($data) > 1) {
42
            throw new \Exception('More than one encrypted data element in <saml:EncryptedAssertion>.');
43
        }
44
        $this->encryptedData = $data[0];
45
    }
46
47
48
    /**
49
     * Set the assertion.
50
     *
51
     * @param \SAML2\Assertion $assertion The assertion.
52
     * @param XMLSecurityKey  $key       The key we should use to encrypt the assertion.
53
     * @throws \Exception
54
     * @return void
55
     */
56
    public function setAssertion(Assertion $assertion, XMLSecurityKey $key)
57
    {
58
        $xml = $assertion->toXML();
59
60
        Utils::getContainer()->debugMessage($xml, 'encrypt');
61
62
        $enc = new XMLSecEnc();
63
        $enc->setNode($xml);
64
        $enc->type = XMLSecEnc::Element;
65
66
        switch ($key->type) {
67
            case XMLSecurityKey::TRIPLEDES_CBC:
68
            case XMLSecurityKey::AES128_CBC:
69
            case XMLSecurityKey::AES192_CBC:
70
            case XMLSecurityKey::AES256_CBC:
71
                $symmetricKey = $key;
72
                break;
73
74
            case XMLSecurityKey::RSA_1_5:
75
            case XMLSecurityKey::RSA_OAEP_MGF1P:
76
                $symmetricKey = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
77
                $symmetricKey->generateSessionKey();
78
79
                $enc->encryptKey($key, $symmetricKey);
80
81
                break;
82
83
            default:
84
                throw new \Exception('Unknown key type for encryption: '.$key->type);
85
        }
86
87
        /** @var \DOMElement encryptedData */
88
        $this->encryptedData = $enc->encryptNode($symmetricKey);
89
    }
90
91
92
    /**
93
     * Retrieve the assertion.
94
     *
95
     * @param  XMLSecurityKey  $inputKey  The key we should use to decrypt the assertion.
96
     * @param  array           $blacklist Blacklisted decryption algorithms.
97
     * @return \SAML2\Assertion The decrypted assertion.
98
     */
99
    public function getAssertion(XMLSecurityKey $inputKey, array $blacklist = []) : Assertion
100
    {
101
        $assertionXML = Utils::decryptElement($this->encryptedData, $inputKey, $blacklist);
102
103
        Utils::getContainer()->debugMessage($assertionXML, 'decrypt');
104
105
        return new Assertion($assertionXML);
106
    }
107
108
109
    /**
110
     * Convert this encrypted assertion to an XML element.
111
     *
112
     * @param  \DOMNode|null $parentElement The DOM node the assertion should be created in.
113
     * @return \DOMElement   This encrypted assertion.
114
     */
115
    public function toXML(\DOMNode $parentElement = null) : \DOMElement
116
    {
117
        if ($parentElement === null) {
118
            $document = DOMDocumentFactory::create();
119
            $parentElement = $document;
120
        } else {
121
            $document = $parentElement->ownerDocument;
122
        }
123
124
        $root = $document->createElementNS(Constants::NS_SAML, 'saml:'.'EncryptedAssertion');
125
        $parentElement->appendChild($root);
126
127
        $root->appendChild($document->importNode($this->encryptedData, true));
128
129
        return $root;
130
    }
131
}
132