ContentEncryptionKeyFactory::build()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.6257
c 0
b 0
f 0
cc 6
nc 6
nop 3
1
<?php namespace jwe\impl;
2
/**
3
 * Copyright 2015 OpenStack Foundation
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 * Unless required by applicable law or agreed to in writing, software
9
 * distributed under the License is distributed on an "AS IS" BASIS,
10
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
 * See the License for the specific language governing permissions and
12
 * limitations under the License.
13
 **/
14
use jwa\cryptographic_algorithms\content_encryption\ContentEncryptionAlgorithm;
15
use jwa\cryptographic_algorithms\EncryptionAlgorithm;
16
use jwe\KeyManagementModeValues;
17
use security\Key;
18
use utils\services\Utils_Registry;
19
/**
20
 * Class ContentEncryptionKeyFactory
21
 *
22
 * Creates the CEK
23
 *
24
 * @package jwe\impl
25
 */
26
final class ContentEncryptionKeyFactory
27
{
28
29
    /**
30
     * @param Key $management_key
31
     * @param $key_management_mode
32
     * @param ContentEncryptionAlgorithm $enc
33
     * @return Key
34
     * @throws \Exception
35
     */
36
    static public function build(Key $management_key, $key_management_mode, ContentEncryptionAlgorithm $enc)
37
    {
38
39
        $cek = null;
0 ignored issues
show
Unused Code introduced by
$cek is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
41
        switch ($key_management_mode) {
42
            /**
43
             * When Key Wrapping, Key Encryption, or Key Agreement with Key
44
             * Wrapping are employed, generate a random CEK value
45
             */
46
            case KeyManagementModeValues::KeyWrapping:
47
            case KeyManagementModeValues::KeyEncryption:
48
            case KeyManagementModeValues::KeyAgreementWithKeyWrapping:
49
            {
50
                // calculate it
51
                $generator = Utils_Registry::getInstance()->get(Utils_Registry::RandomNumberGeneratorService);
52
                /**
53
                 * The CEK MUST have a length equal to that required for the
54
                 * content encryption algorithm.
55
                 */
56
                $rnd       = $generator->invoke($enc->getMinKeyLen()/8);
0 ignored issues
show
Unused Code introduced by
The call to IService::invoke() has too many arguments starting with $enc->getMinKeyLen() / 8.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
                $cek       = new _ContentEncryptionKey($enc->getName(), 'RAW', $rnd);
58
            }
59
            break;
60
            case KeyManagementModeValues::DirectEncryption:
61
            {
62
                $cek = $management_key;
63
            }
64
            break;
65
            case KeyManagementModeValues::DirectKeyAgreement:
66
            {
67
                throw new \Exception('unsupported KKM!');
68
            }
69
            break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
            default:
71
            {
72
                throw new \Exception('unsupported KKM!');
73
            }
74
            break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
75
        }
76
        return $cek;
77
    }
78
79
    /**
80
     * @param string $value
81
     * @param EncryptionAlgorithm $alg
82
     * @return Key
83
     */
84
    static public function fromRaw($value, EncryptionAlgorithm $alg){
85
        return  new _ContentEncryptionKey($alg->getName(), 'RAW', $value);
86
    }
87
}