Completed
Push — release-4-alpha ( 8e16f1...9463e8 )
by Tim
04:12 queued 01:59
created

EncryptedAssertion::wasSignedAtConstruction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * @var bool
27
     */
28
    protected $wasSignedAtConstruction = false;
29
30
    /**
31
     * Constructor for SAML 2 encrypted assertions.
32
     *
33
     * @param \DOMElement|null $xml The encrypted assertion XML element.
34
     * @throws \Exception
35
     */
36
    public function __construct(\DOMElement $xml = null)
37
    {
38
        if ($xml === null) {
39
            return;
40
        }
41
42
        /** @var \DOMElement[] $data */
43
        $data = Utils::xpQuery($xml, './xenc:EncryptedData');
44
        if (empty($data)) {
45
            throw new \Exception('Missing encrypted data in <saml:EncryptedAssertion>.');
46
        } elseif (count($data) > 1) {
47
            throw new \Exception('More than one encrypted data element in <saml:EncryptedAssertion>.');
48
        }
49
        $this->encryptedData = $data[0];
50
    }
51
52
53
    /**
54
     * @return bool
55
     */
56
    public function wasSignedAtConstruction()
57
    {
58
        return $this->wasSignedAtConstruction;
59
    }
60
61
    /**
62
     * Set the assertion.
63
     *
64
     * @param \SAML2\Assertion $assertion The assertion.
65
     * @param XMLSecurityKey  $key       The key we should use to encrypt the assertion.
66
     * @throws \Exception
67
     * @return void
68
     */
69
    public function setAssertion(Assertion $assertion, XMLSecurityKey $key)
70
    {
71
        $xml = $assertion->toXML();
72
73
        Utils::getContainer()->debugMessage($xml, 'encrypt');
74
75
        $enc = new XMLSecEnc();
76
        $enc->setNode($xml);
77
        $enc->type = XMLSecEnc::Element;
78
79
        switch ($key->type) {
80
            case XMLSecurityKey::TRIPLEDES_CBC:
81
            case XMLSecurityKey::AES128_CBC:
82
            case XMLSecurityKey::AES192_CBC:
83
            case XMLSecurityKey::AES256_CBC:
84
                $symmetricKey = $key;
85
                break;
86
87
            case XMLSecurityKey::RSA_1_5:
88
            case XMLSecurityKey::RSA_OAEP_MGF1P:
89
                $symmetricKey = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
90
                $symmetricKey->generateSessionKey();
91
92
                $enc->encryptKey($key, $symmetricKey);
93
94
                break;
95
96
            default:
97
                throw new \Exception('Unknown key type for encryption: '.$key->type);
98
        }
99
100
        /** @var \DOMElement encryptedData */
101
        $this->encryptedData = $enc->encryptNode($symmetricKey);
102
    }
103
104
105
    /**
106
     * Retrieve the assertion.
107
     *
108
     * @param  XMLSecurityKey  $inputKey  The key we should use to decrypt the assertion.
109
     * @param  array           $blacklist Blacklisted decryption algorithms.
110
     * @return \SAML2\Assertion The decrypted assertion.
111
     */
112
    public function getAssertion(XMLSecurityKey $inputKey, array $blacklist = []) : Assertion
113
    {
114
        $assertionXML = Utils::decryptElement($this->encryptedData, $inputKey, $blacklist);
115
116
        Utils::getContainer()->debugMessage($assertionXML, 'decrypt');
117
118
        return new Assertion($assertionXML);
119
    }
120
121
122
    /**
123
     * Convert this encrypted assertion to an XML element.
124
     *
125
     * @param  \DOMNode|null $parentElement The DOM node the assertion should be created in.
126
     * @return \DOMElement   This encrypted assertion.
127
     */
128
    public function toXML(\DOMNode $parentElement = null) : \DOMElement
129
    {
130
        if ($parentElement === null) {
131
            $document = DOMDocumentFactory::create();
132
            $parentElement = $document;
133
        } else {
134
            $document = $parentElement->ownerDocument;
135
        }
136
137
        $root = $document->createElementNS(Constants::NS_SAML, 'saml:'.'EncryptedAssertion');
138
        $parentElement->appendChild($root);
139
140
        $root->appendChild($document->importNode($this->encryptedData, true));
141
142
        return $root;
143
    }
144
}
145