EncryptedElementWriter::serialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 9.9332
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Model\Assertion;
13
14
use LightSaml\Model\Context\DeserializationContext;
15
use LightSaml\Model\Context\SerializationContext;
16
use LightSaml\Error\LightSamlException;
17
use LightSaml\Model\AbstractSamlModel;
18
use RobRichards\XMLSecLibs\XMLSecurityKey;
19
use RobRichards\XMLSecLibs\XMLSecEnc;
20
21
abstract class EncryptedElementWriter extends EncryptedElement
22
{
23
    /** @var \DOMElement */
24
    protected $encryptedElement;
25
26
    /** @var string */
27
    protected $blockEncryptionAlgorithm = XMLSecurityKey::AES128_CBC;
28
29
    /** @var string */
30
    protected $keyTransportEncryption = XMLSecurityKey::RSA_1_5;
31
32
    /**
33
     * @param string $blockEncryptionAlgorithm
34
     * @param string $keyTransportEncryption
35
     */
36
    public function __construct($blockEncryptionAlgorithm = XMLSecurityKey::AES128_CBC, $keyTransportEncryption = XMLSecurityKey::RSA_1_5)
37
    {
38
        $this->blockEncryptionAlgorithm = $blockEncryptionAlgorithm;
39
        $this->keyTransportEncryption = $keyTransportEncryption;
40
    }
41
42
    /**
43
     * @param AbstractSamlModel $object
44
     * @param XMLSecurityKey    $key
45
     *
46
     * @return SerializationContext
47
     */
48
    public function encrypt(AbstractSamlModel $object, XMLSecurityKey $key)
49
    {
50
        $oldKey = $key;
51
        $key = new XMLSecurityKey($this->keyTransportEncryption, ['type' => 'public']);
52
        $key->loadKey($oldKey->key);
53
54
        $serializationContext = new SerializationContext();
55
        $object->serialize($serializationContext->getDocument(), $serializationContext);
56
57
        $enc = new XMLSecEnc();
58
        $enc->setNode($serializationContext->getDocument()->firstChild);
59
        $enc->type = XMLSecEnc::Element;
60
61
        switch ($key->type) {
62
            case XMLSecurityKey::TRIPLEDES_CBC:
63
            case XMLSecurityKey::AES128_CBC:
64
            case XMLSecurityKey::AES192_CBC:
65
            case XMLSecurityKey::AES256_CBC:
66
                $symmetricKey = $key;
67
                break;
68
69
            case XMLSecurityKey::RSA_1_5:
70
            case XMLSecurityKey::RSA_SHA1:
71
            case XMLSecurityKey::RSA_SHA256:
72
            case XMLSecurityKey::RSA_SHA384:
73
            case XMLSecurityKey::RSA_SHA512:
74
            case XMLSecurityKey::RSA_OAEP_MGF1P:
75
                $symmetricKey = new XMLSecurityKey($this->blockEncryptionAlgorithm);
76
                $symmetricKey->generateSessionKey();
77
78
                $enc->encryptKey($key, $symmetricKey);
79
80
                break;
81
82
            default:
83
                throw new LightSamlException(sprintf('Unknown key type for encryption: "%s"', $key->type));
84
        }
85
86
        $this->encryptedElement = $enc->encryptNode($symmetricKey);
87
88
        return $serializationContext;
89
    }
90
91
    /**
92
     * @param \DOMNode             $parent
93
     * @param SerializationContext $context
94
     *
95
     * @return \DOMElement
96
     */
97
    abstract protected function createRootElement(\DOMNode $parent, SerializationContext $context);
98
99
    /**
100
     * @param \DOMNode             $parent
101
     * @param SerializationContext $context
102
     *
103
     * @return void
104
     */
105
    public function serialize(\DOMNode $parent, SerializationContext $context)
106
    {
107
        if (null === $this->encryptedElement) {
108
            throw new LightSamlException('Encrypted element missing');
109
        }
110
111
        $root = $this->createRootElement($parent, $context);
112
113
        $root->appendChild($context->getDocument()->importNode($this->encryptedElement, true));
114
    }
115
116
    /**
117
     * @param \DOMNode               $node
118
     * @param DeserializationContext $context
119
     */
120
    public function deserialize(\DOMNode $node, DeserializationContext $context)
121
    {
122
        throw new \LogicException('EncryptedElementWriter can not be used for deserialization');
123
    }
124
}
125