This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace jwk\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 jwk\exceptions\InvalidJWKType; |
||
15 | use jwk\exceptions\InvalidJWKUseException; |
||
16 | use jwk\exceptions\RSAJWKMissingPrivateKeyParamException; |
||
17 | use jwk\exceptions\RSAJWKMissingPublicKeyParamException; |
||
18 | use jwk\IAsymmetricJWK; |
||
19 | use jwk\JSONWebKeyKeyOperationsValues; |
||
20 | use jwk\JSONWebKeyParameters; |
||
21 | use jwk\JSONWebKeyTypes; |
||
22 | use jwk\JSONWebKeyVisibility; |
||
23 | use jwk\RSAKeysParameters; |
||
24 | use security\Key; |
||
25 | use security\KeyPair; |
||
26 | use security\PrivateKey; |
||
27 | use security\PublicKey; |
||
28 | use security\rsa\RSAFacade; |
||
29 | use security\rsa\RSAPrivateKey; |
||
30 | use security\rsa\RSAPublicKey; |
||
31 | use utils\json_types\Base64urlUInt; |
||
32 | use utils\json_types\StringOrURI; |
||
33 | /** |
||
34 | * Class RSAJWK |
||
35 | * @package jwk\impl |
||
36 | */ |
||
37 | final class RSAJWK extends AsymmetricJWK |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * @param array $headers |
||
42 | * @throws RSAJWKMissingPrivateKeyParamException |
||
43 | * @throws RSAJWKMissingPublicKeyParamException |
||
44 | */ |
||
45 | protected function __construct($headers = array()) |
||
46 | { |
||
47 | |||
48 | $this->set[JSONWebKeyParameters::KeyType] = new StringOrURI(JSONWebKeyTypes::RSA); |
||
49 | |||
50 | parent::__construct($headers); |
||
51 | |||
52 | if (count($headers) === 0) return; |
||
53 | |||
54 | View Code Duplication | foreach (RSAKeysParameters::$public_key_params as $p) { |
|
55 | if (!array_key_exists($p, $headers)) |
||
56 | throw new RSAJWKMissingPublicKeyParamException(); |
||
57 | $this->set[$p] = new Base64urlUInt($headers[$p]); |
||
58 | } |
||
59 | |||
60 | $this->visibility = JSONWebKeyVisibility::PublicOnly; |
||
61 | |||
62 | //calculate public key |
||
63 | $this->public_key = RSAFacade::getInstance()->buildPublicKey($this[RSAKeysParameters::Modulus]->toBigInt(), $this[RSAKeysParameters::Exponent]->toBigInt()); |
||
64 | |||
65 | if (in_array(RSAKeysParameters::PrivateExponent, $headers)) { |
||
66 | // its a private key |
||
67 | $this->visibility = JSONWebKeyVisibility::IncludePrivate; |
||
68 | |||
69 | $this[RSAKeysParameters::PrivateExponent] = new Base64urlUInt($headers[RSAKeysParameters::PrivateExponent]); |
||
70 | //its has one private param, must have all ... |
||
71 | if (in_array(RSAKeysParameters::FirstPrimeFactor, $headers)) { |
||
72 | View Code Duplication | foreach (RSAKeysParameters::$producers_private_key_params as $p) { |
|
73 | if (!array_key_exists($p, $headers)) |
||
74 | throw new RSAJWKMissingPrivateKeyParamException(); |
||
75 | $this->set[$p] = new Base64urlUInt($headers[$p]); |
||
76 | } |
||
77 | $this->private_key = RSAFacade::getInstance()->buildPrivateKey( |
||
78 | $this[RSAKeysParameters::Modulus]->toBigInt(), |
||
79 | $this[RSAKeysParameters::Exponent]->toBigInt(), |
||
80 | $this[RSAKeysParameters::PrivateExponent]->toBigInt(), |
||
81 | $this[RSAKeysParameters::FirstPrimeFactor]->toBigInt(), |
||
82 | $this[RSAKeysParameters::SecondPrimeFactor]->toBigInt(), |
||
83 | $this[RSAKeysParameters::FirstFactorCRTExponent]->toBigInt(), |
||
84 | $this[RSAKeysParameters::SecondFactorCRTExponent]->toBigInt(), |
||
85 | $this[RSAKeysParameters::FirstCRTCoefficient]->toBigInt() |
||
86 | ); |
||
87 | } else { |
||
88 | $this->private_key = RSAFacade::getInstance()->buildMinimalPrivateKey( |
||
89 | $this[RSAKeysParameters::Modulus]->toBigInt(), |
||
90 | $this[RSAKeysParameters::PrivateExponent]->toBigInt() |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getType() |
||
101 | { |
||
102 | return JSONWebKeyTypes::RSA; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param KeyPair $keys |
||
107 | * @return IAsymmetricJWK |
||
108 | */ |
||
109 | static public function fromKeys(KeyPair $keys) |
||
110 | { |
||
111 | if(!($keys->getPrivate() instanceof RSAPrivateKey)) |
||
112 | throw new \RuntimeException('Private key of invalid type!'); |
||
113 | |||
114 | if(!($keys->getPublic() instanceof RSAPublicKey)) |
||
115 | throw new \RuntimeException('Public key of invalid type!'); |
||
116 | |||
117 | $jwk = new RSAJWK(); |
||
118 | $jwk->public_key = $keys->getPublic(); |
||
119 | $jwk->private_key = $keys->getPrivate(); |
||
120 | $jwk->set[RSAKeysParameters::Exponent] = Base64urlUInt::fromBigInt($jwk->public_key->getPublicExponent()); |
||
121 | $jwk->set[RSAKeysParameters::Modulus] = Base64urlUInt::fromBigInt($jwk->public_key->getModulus()); |
||
122 | $jwk->set[RSAKeysParameters::PrivateExponent] = Base64urlUInt::fromBigInt($jwk->private_key->getPrivateExponent()); |
||
123 | return $jwk; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param PublicKey $public_key |
||
128 | * @return IAsymmetricJWK |
||
129 | * @throws InvalidJWKType |
||
130 | */ |
||
131 | static public function fromPublicKey(PublicKey $public_key) |
||
132 | { |
||
133 | if (!($public_key instanceof RSAPublicKey)) throw new InvalidJWKType(); |
||
134 | $jwk = new RSAJWK(); |
||
135 | $jwk->public_key = $public_key; |
||
136 | $jwk->set[RSAKeysParameters::Exponent] = Base64urlUInt::fromBigInt($public_key->getPublicExponent()); |
||
137 | $jwk->set[RSAKeysParameters::Modulus] = Base64urlUInt::fromBigInt($public_key->getModulus()); |
||
138 | return $jwk; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param PrivateKey $private_key |
||
143 | * @return IAsymmetricJWK|null |
||
144 | * @throws InvalidJWKType |
||
145 | */ |
||
146 | static public function fromPrivateKey(PrivateKey $private_key) |
||
147 | { |
||
148 | if (!($private_key instanceof RSAPrivateKey)) throw new InvalidJWKType(); |
||
149 | $jwk = new RSAJWK(); |
||
150 | $jwk->private_key = $private_key; |
||
151 | $jwk->set[RSAKeysParameters::Exponent] = Base64urlUInt::fromBigInt($private_key->getPublicExponent()); |
||
152 | $jwk->set[RSAKeysParameters::Modulus] = Base64urlUInt::fromBigInt($private_key->getModulus()); |
||
153 | $jwk->set[RSAKeysParameters::PrivateExponent] = Base64urlUInt::fromBigInt($private_key->getPrivateExponent()); |
||
154 | return $jwk; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param string $key_op |
||
159 | * @return Key |
||
160 | * @throws InvalidJWKUseException |
||
161 | */ |
||
162 | public function getKey($key_op = JSONWebKeyKeyOperationsValues::ComputeDigitalSignatureOrMAC) |
||
163 | { |
||
164 | switch($key_op){ |
||
165 | case JSONWebKeyKeyOperationsValues::ComputeDigitalSignatureOrMAC: |
||
166 | case JSONWebKeyKeyOperationsValues::DecryptContentAndValidateDecryption: { |
||
167 | return $this->getPrivateKey(); |
||
168 | } |
||
169 | break; |
||
0 ignored issues
–
show
|
|||
170 | case JSONWebKeyKeyOperationsValues::VerifyDigitalSignatureOrMAC: |
||
171 | case JSONWebKeyKeyOperationsValues::EncryptContent: { |
||
172 | return $this->getPublicKey(); |
||
173 | } |
||
174 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The break statement is not necessary if it is preceded for example by a return statement: switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive. ![]() |
|||
175 | default:{ |
||
176 | throw new InvalidJWKUseException(sprintf('key_op %s', $key_op)); |
||
177 | } |
||
178 | break; |
||
0 ignored issues
–
show
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 function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @override |
||
184 | * @return array |
||
185 | */ |
||
186 | public function toArray() |
||
187 | { |
||
188 | $res = parent::toArray(); |
||
189 | if($this->visibility === JSONWebKeyVisibility::PublicOnly){ |
||
190 | //remove private attributes |
||
191 | unset($res[RSAKeysParameters::PrivateExponent]); |
||
192 | unset($res[RSAKeysParameters::FirstPrimeFactor]); |
||
193 | unset($res[RSAKeysParameters::SecondPrimeFactor]); |
||
194 | unset($res[RSAKeysParameters::FirstFactorCRTExponent]); |
||
195 | unset($res[RSAKeysParameters::SecondFactorCRTExponent]); |
||
196 | unset($res[RSAKeysParameters::FirstCRTCoefficient]); |
||
197 | } |
||
198 | return $res; |
||
199 | } |
||
200 | |||
201 | } |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.