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 |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * The MIT License (MIT) |
||
7 | * |
||
8 | * Copyright (c) 2014-2019 Spomky-Labs |
||
9 | * |
||
10 | * This software may be modified and distributed under the terms |
||
11 | * of the MIT license. See the LICENSE file for details. |
||
12 | */ |
||
13 | |||
14 | namespace Jose\Component\Signature\Algorithm\Util; |
||
15 | |||
16 | use InvalidArgumentException; |
||
17 | use Jose\Component\Core\Util\BigInteger; |
||
18 | use Jose\Component\Core\Util\Hash; |
||
19 | use Jose\Component\Core\Util\RSAKey; |
||
20 | use RuntimeException; |
||
21 | |||
22 | /** |
||
23 | * @internal |
||
24 | */ |
||
25 | class RSA |
||
26 | { |
||
27 | /** |
||
28 | * Probabilistic Signature Scheme. |
||
29 | */ |
||
30 | public const SIGNATURE_PSS = 1; |
||
31 | |||
32 | /** |
||
33 | * Use the PKCS#1. |
||
34 | */ |
||
35 | public const SIGNATURE_PKCS1 = 2; |
||
36 | |||
37 | /** |
||
38 | * @throws RuntimeException if the data cannot be signed |
||
39 | * @throws InvalidArgumentException if the signature mode is not supported |
||
40 | */ |
||
41 | public static function sign(RSAKey $key, string $message, string $hash, int $mode): string |
||
42 | { |
||
43 | switch ($mode) { |
||
44 | case self::SIGNATURE_PSS: |
||
45 | return self::signWithPSS($key, $message, $hash); |
||
46 | case self::SIGNATURE_PKCS1: |
||
47 | $result = openssl_sign($message, $signature, $key->toPEM(), $hash); |
||
48 | if (true !== $result) { |
||
49 | throw new RuntimeException('Unable to sign the data'); |
||
50 | } |
||
51 | |||
52 | return $signature; |
||
53 | default: |
||
54 | throw new InvalidArgumentException('Unsupported mode.'); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Create a signature. |
||
60 | */ |
||
61 | public static function signWithPSS(RSAKey $key, string $message, string $hash): string |
||
62 | { |
||
63 | $em = self::encodeEMSAPSS($message, 8 * $key->getModulusLength() - 1, Hash::$hash()); |
||
64 | $message = BigInteger::createFromBinaryString($em); |
||
65 | $signature = RSAKey::exponentiate($key, $message); |
||
0 ignored issues
–
show
|
|||
66 | |||
67 | return self::convertIntegerToOctetString($signature, $key->getModulusLength()); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Create a signature. |
||
72 | * |
||
73 | * @deprecated Please use openssl_sign |
||
74 | */ |
||
75 | public static function signWithPKCS15(RSAKey $key, string $message, string $hash): string |
||
76 | { |
||
77 | $em = self::encodeEMSA15($message, $key->getModulusLength(), Hash::$hash()); |
||
78 | $message = BigInteger::createFromBinaryString($em); |
||
79 | $signature = RSAKey::exponentiate($key, $message); |
||
0 ignored issues
–
show
$key is of type object<Jose\Component\Core\Util\RSAKey> , but the function expects a object<self> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
80 | |||
81 | return self::convertIntegerToOctetString($signature, $key->getModulusLength()); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @throws InvalidArgumentException if the signature mode is not supported |
||
86 | */ |
||
87 | public static function verify(RSAKey $key, string $message, string $signature, string $hash, int $mode): bool |
||
88 | { |
||
89 | switch ($mode) { |
||
90 | case self::SIGNATURE_PSS: |
||
91 | return self::verifyWithPSS($key, $message, $signature, $hash); |
||
92 | case self::SIGNATURE_PKCS1: |
||
93 | return 1 === openssl_verify($message, $signature, $key->toPEM(), $hash); |
||
94 | default: |
||
95 | throw new InvalidArgumentException('Unsupported mode.'); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Verifies a signature. |
||
101 | * |
||
102 | * @throws RuntimeException if the signature cannot be verified |
||
103 | */ |
||
104 | public static function verifyWithPSS(RSAKey $key, string $message, string $signature, string $hash): bool |
||
105 | { |
||
106 | if (mb_strlen($signature, '8bit') !== $key->getModulusLength()) { |
||
107 | throw new RuntimeException(); |
||
108 | } |
||
109 | $s2 = BigInteger::createFromBinaryString($signature); |
||
110 | $m2 = RSAKey::exponentiate($key, $s2); |
||
0 ignored issues
–
show
$key is of type object<Jose\Component\Core\Util\RSAKey> , but the function expects a object<self> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
111 | $em = self::convertIntegerToOctetString($m2, $key->getModulusLength()); |
||
112 | $modBits = 8 * $key->getModulusLength(); |
||
113 | |||
114 | return self::verifyEMSAPSS($message, $em, $modBits - 1, Hash::$hash()); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Verifies a signature. |
||
119 | * |
||
120 | * @deprecated Please use openssl_sign |
||
121 | * |
||
122 | * @throws RuntimeException if the signature cannot be verified |
||
123 | */ |
||
124 | public static function verifyWithPKCS15(RSAKey $key, string $message, string $signature, string $hash): bool |
||
125 | { |
||
126 | if (mb_strlen($signature, '8bit') !== $key->getModulusLength()) { |
||
127 | throw new RuntimeException(); |
||
128 | } |
||
129 | $signature = BigInteger::createFromBinaryString($signature); |
||
130 | $m2 = RSAKey::exponentiate($key, $signature); |
||
0 ignored issues
–
show
$key is of type object<Jose\Component\Core\Util\RSAKey> , but the function expects a object<self> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
131 | $em = self::convertIntegerToOctetString($m2, $key->getModulusLength()); |
||
132 | |||
133 | return hash_equals($em, self::encodeEMSA15($message, $key->getModulusLength(), Hash::$hash())); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @throws RuntimeException if the value cannot be converted |
||
138 | */ |
||
139 | private static function convertIntegerToOctetString(BigInteger $x, int $xLen): string |
||
140 | { |
||
141 | $x = $x->toBytes(); |
||
142 | if (mb_strlen($x, '8bit') > $xLen) { |
||
143 | throw new RuntimeException(); |
||
144 | } |
||
145 | |||
146 | return str_pad($x, $xLen, \chr(0), STR_PAD_LEFT); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * MGF1. |
||
151 | */ |
||
152 | private static function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string |
||
153 | { |
||
154 | $t = ''; |
||
155 | $count = ceil($maskLen / $mgfHash->getLength()); |
||
156 | for ($i = 0; $i < $count; ++$i) { |
||
157 | $c = pack('N', $i); |
||
158 | $t .= $mgfHash->hash($mgfSeed.$c); |
||
159 | } |
||
160 | |||
161 | return mb_substr($t, 0, $maskLen, '8bit'); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * EMSA-PSS-ENCODE. |
||
166 | * |
||
167 | * @throws RuntimeException if the message length is invalid |
||
168 | */ |
||
169 | private static function encodeEMSAPSS(string $message, int $modulusLength, Hash $hash): string |
||
170 | { |
||
171 | $emLen = ($modulusLength + 1) >> 3; |
||
172 | $sLen = $hash->getLength(); |
||
173 | $mHash = $hash->hash($message); |
||
174 | if ($emLen <= $hash->getLength() + $sLen + 2) { |
||
175 | throw new RuntimeException(); |
||
176 | } |
||
177 | $salt = random_bytes($sLen); |
||
178 | $m2 = "\0\0\0\0\0\0\0\0".$mHash.$salt; |
||
179 | $h = $hash->hash($m2); |
||
180 | $ps = str_repeat(\chr(0), $emLen - $sLen - $hash->getLength() - 2); |
||
181 | $db = $ps.\chr(1).$salt; |
||
182 | $dbMask = self::getMGF1($h, $emLen - $hash->getLength() - 1, $hash); |
||
183 | $maskedDB = $db ^ $dbMask; |
||
184 | $maskedDB[0] = ~\chr(0xFF << ($modulusLength & 7)) & $maskedDB[0]; |
||
185 | $em = $maskedDB.$h.\chr(0xBC); |
||
186 | |||
187 | return $em; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * EMSA-PSS-VERIFY. |
||
192 | * |
||
193 | * @throws InvalidArgumentException if the signature cannot be verified |
||
194 | */ |
||
195 | private static function verifyEMSAPSS(string $m, string $em, int $emBits, Hash $hash): bool |
||
196 | { |
||
197 | $emLen = ($emBits + 1) >> 3; |
||
198 | $sLen = $hash->getLength(); |
||
199 | $mHash = $hash->hash($m); |
||
200 | if ($emLen < $hash->getLength() + $sLen + 2) { |
||
201 | throw new InvalidArgumentException(); |
||
202 | } |
||
203 | if ($em[mb_strlen($em, '8bit') - 1] !== \chr(0xBC)) { |
||
204 | throw new InvalidArgumentException(); |
||
205 | } |
||
206 | $maskedDB = mb_substr($em, 0, -$hash->getLength() - 1, '8bit'); |
||
207 | $h = mb_substr($em, -$hash->getLength() - 1, $hash->getLength(), '8bit'); |
||
208 | $temp = \chr(0xFF << ($emBits & 7)); |
||
209 | if ((~$maskedDB[0] & $temp) !== $temp) { |
||
210 | throw new InvalidArgumentException(); |
||
211 | } |
||
212 | $dbMask = self::getMGF1($h, $emLen - $hash->getLength() - 1, $hash/*MGF*/); |
||
213 | $db = $maskedDB ^ $dbMask; |
||
214 | $db[0] = ~\chr(0xFF << ($emBits & 7)) & $db[0]; |
||
215 | $temp = $emLen - $hash->getLength() - $sLen - 2; |
||
216 | if (mb_substr($db, 0, $temp, '8bit') !== str_repeat(\chr(0), $temp)) { |
||
217 | throw new InvalidArgumentException(); |
||
218 | } |
||
219 | if (1 !== \ord($db[$temp])) { |
||
220 | throw new InvalidArgumentException(); |
||
221 | } |
||
222 | $salt = mb_substr($db, $temp + 1, null, '8bit'); // should be $sLen long |
||
223 | $m2 = "\0\0\0\0\0\0\0\0".$mHash.$salt; |
||
224 | $h2 = $hash->hash($m2); |
||
225 | |||
226 | return hash_equals($h, $h2); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @throws RuntimeException if the value cannot be encoded |
||
231 | */ |
||
232 | private static function encodeEMSA15(string $m, int $emBits, Hash $hash): string |
||
233 | { |
||
234 | $h = $hash->hash($m); |
||
235 | $t = $hash->t(); |
||
236 | $t .= $h; |
||
237 | $tLen = mb_strlen($t, '8bit'); |
||
238 | if ($emBits < $tLen + 11) { |
||
239 | throw new RuntimeException(); |
||
240 | } |
||
241 | $ps = str_repeat(\chr(0xFF), $emBits - $tLen - 3); |
||
242 | |||
243 | return "\0\1{$ps}\0{$t}"; |
||
244 | } |
||
245 | } |
||
246 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: