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; |
|
|
|
|
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); |
|
|
|
|
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; |
|
|
|
|
70
|
|
|
default: |
71
|
|
|
{ |
72
|
|
|
throw new \Exception('unsupported KKM!'); |
73
|
|
|
} |
74
|
|
|
break; |
|
|
|
|
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
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.