EncryptedElementWriter::encrypt()   B
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 41
rs 7.3166
cc 11
nc 11
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Error\LightSamlException;
15
use LightSaml\Model\AbstractSamlModel;
16
use LightSaml\Model\Context\DeserializationContext;
17
use LightSaml\Model\Context\SerializationContext;
18
use RobRichards\XMLSecLibs\XMLSecEnc;
19
use RobRichards\XMLSecLibs\XMLSecurityKey;
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
     * @return SerializationContext
44
     */
45
    public function encrypt(AbstractSamlModel $object, XMLSecurityKey $key)
46
    {
47
        $oldKey = $key;
48
        $key = new XMLSecurityKey($this->keyTransportEncryption, ['type' => 'public']);
49
        $key->loadKey($oldKey->key);
50
51
        $serializationContext = new SerializationContext();
52
        $object->serialize($serializationContext->getDocument(), $serializationContext);
53
54
        $enc = new XMLSecEnc();
55
        $enc->setNode($serializationContext->getDocument()->firstChild);
56
        $enc->type = XMLSecEnc::Element;
57
58
        switch ($key->type) {
59
            case XMLSecurityKey::TRIPLEDES_CBC:
60
            case XMLSecurityKey::AES128_CBC:
61
            case XMLSecurityKey::AES192_CBC:
62
            case XMLSecurityKey::AES256_CBC:
63
                $symmetricKey = $key;
64
                break;
65
66
            case XMLSecurityKey::RSA_1_5:
67
            case XMLSecurityKey::RSA_SHA1:
68
            case XMLSecurityKey::RSA_SHA256:
69
            case XMLSecurityKey::RSA_SHA384:
70
            case XMLSecurityKey::RSA_SHA512:
71
            case XMLSecurityKey::RSA_OAEP_MGF1P:
72
                $symmetricKey = new XMLSecurityKey($this->blockEncryptionAlgorithm);
73
                $symmetricKey->generateSessionKey();
74
75
                $enc->encryptKey($key, $symmetricKey);
76
77
                break;
78
79
            default:
80
                throw new LightSamlException(sprintf('Unknown key type for encryption: "%s"', $key->type));
81
        }
82
83
        $this->encryptedElement = $enc->encryptNode($symmetricKey);
84
85
        return $serializationContext;
86
    }
87
88
    /**
89
     * @return \DOMElement
90
     */
91
    abstract protected function createRootElement(\DOMNode $parent, SerializationContext $context);
92
93
    /**
94
     * @return void
95
     */
96
    public function serialize(\DOMNode $parent, SerializationContext $context)
97
    {
98
        if (null === $this->encryptedElement) {
99
            throw new LightSamlException('Encrypted element missing');
100
        }
101
102
        $root = $this->createRootElement($parent, $context);
103
104
        $root->appendChild($context->getDocument()->importNode($this->encryptedElement, true));
105
    }
106
107
    public function deserialize(\DOMNode $node, DeserializationContext $context)
108
    {
109
        throw new \LogicException('EncryptedElementWriter can not be used for deserialization');
110
    }
111
}
112