Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Signature/Algorithm/Util/RSA.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Signature\Algorithm\Util;
6
7 use InvalidArgumentException;
8 use Jose\Component\Core\Util\BigInteger;
9 use Jose\Component\Core\Util\Hash;
10 use Jose\Component\Core\Util\RSAKey;
11 use RuntimeException;
12 use function chr;
13 use function extension_loaded;
14 use function ord;
15 use const STR_PAD_LEFT;
16
17 /**
18 * @internal
19 */
20 final class RSA
21 {
22 /**
23 * Probabilistic Signature Scheme.
24 */
25 public const SIGNATURE_PSS = 1;
26
27 /**
28 * Use the PKCS#1.
29 */
30 public const SIGNATURE_PKCS1 = 2;
31
32 /**
33 * @return non-empty-string
34 */
35 public static function sign(RSAKey $key, string $message, string $hash, int $mode): string
36 {
37 switch ($mode) {
38 case self::SIGNATURE_PSS:
39 return self::signWithPSS($key, $message, $hash);
40
41 case self::SIGNATURE_PKCS1:
42 if (! extension_loaded('openssl')) {
43 throw new RuntimeException('Please install the OpenSSL extension');
44 }
45 $result = openssl_sign($message, $signature, $key->toPEM(), $hash);
46 if ($result !== true) {
47 throw new RuntimeException('Unable to sign the data');
48 }
49
50 return $signature;
51
52 default:
53 throw new InvalidArgumentException('Unsupported mode.');
54 }
55 }
56
57 /**
58 * Create a signature.
59 *
60 * @return non-empty-string
61 */
62 public static function signWithPSS(RSAKey $key, string $message, string $hash): string
63 {
64 $em = self::encodeEMSAPSS($message, 8 * $key->getModulusLength() - 1, Hash::$hash());
65 $message = BigInteger::createFromBinaryString($em);
66 $signature = RSAKey::exponentiate($key, $message);
67 $result = self::convertIntegerToOctetString($signature, $key->getModulusLength());
68 if ($result === '') {
69 throw new InvalidArgumentException('Invalid signature.');
70 }
71
72 return $result;
73 }
74
75 public static function verify(RSAKey $key, string $message, string $signature, string $hash, int $mode): bool
76 {
77 switch ($mode) {
78 case self::SIGNATURE_PSS:
79 return self::verifyWithPSS($key, $message, $signature, $hash);
80 case self::SIGNATURE_PKCS1:
81 if (! extension_loaded('openssl')) {
82 throw new RuntimeException('Please install the OpenSSL extension');
83 }
84 return openssl_verify($message, $signature, $key->toPEM(), $hash) === 1;
85 default:
86 throw new InvalidArgumentException('Unsupported mode.');
87 }
88 }
89
90 /**
91 * Verifies a signature.
92 */
93 public static function verifyWithPSS(RSAKey $key, string $message, string $signature, string $hash): bool
94 {
95 if (mb_strlen($signature, '8bit') !== $key->getModulusLength()) {
96 throw new RuntimeException();
97 }
98 $s2 = BigInteger::createFromBinaryString($signature);
99 $m2 = RSAKey::exponentiate($key, $s2);
100 $em = self::convertIntegerToOctetString($m2, $key->getModulusLength());
101 $modBits = 8 * $key->getModulusLength();
102
103 return self::verifyEMSAPSS($message, $em, $modBits - 1, Hash::$hash());
104 }
105
106 private static function convertIntegerToOctetString(BigInteger $x, int $xLen): string
107 {
108 $x = $x->toBytes();
109 if (mb_strlen($x, '8bit') > $xLen) {
110 throw new RuntimeException();
111 }
112
113 return str_pad($x, $xLen, chr(0), STR_PAD_LEFT);
114 }
115
116 /**
117 * MGF1.
118 */
119 private static function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string
120 {
121 $t = '';
122 $count = ceil($maskLen / $mgfHash->getLength());
123 for ($i = 0; $i < $count; ++$i) {
124 $c = pack('N', $i);
125 $t .= $mgfHash->hash($mgfSeed . $c);
126 }
127
128 return mb_substr($t, 0, $maskLen, '8bit');
129 }
130
131 /**
132 * EMSA-PSS-ENCODE.
133 */
134 private static function encodeEMSAPSS(string $message, int $modulusLength, Hash $hash): string
135 {
136 $emLen = ($modulusLength + 1) >> 3;
137 $sLen = $hash->getLength();
138 $mHash = $hash->hash($message);
139 if ($emLen <= $hash->getLength() + $sLen + 2) {
140 throw new RuntimeException();
141 }
142 $salt = random_bytes($sLen);
143 $m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt;
144 $h = $hash->hash($m2);
145 $ps = str_repeat(chr(0), $emLen - $sLen - $hash->getLength() - 2);
146 $db = $ps . chr(1) . $salt;
147 $dbMask = self::getMGF1($h, $emLen - $hash->getLength() - 1, $hash);
148 $maskedDB = $db ^ $dbMask;
149 $maskedDB[0] = ~chr(0xFF << ($modulusLength & 7)) & $maskedDB[0];
150
151 return $maskedDB . $h . chr(0xBC);
152 }
153
154 /**
155 * EMSA-PSS-VERIFY.
156 */
157 private static function verifyEMSAPSS(string $m, string $em, int $emBits, Hash $hash): bool
158 {
159 $emLen = ($emBits + 1) >> 3;
160 $sLen = $hash->getLength();
161 $mHash = $hash->hash($m);
162 if ($emLen < $hash->getLength() + $sLen + 2) {
163 throw new InvalidArgumentException();
164 }
165 if ($em[mb_strlen($em, '8bit') - 1] !== chr(0xBC)) {
166 throw new InvalidArgumentException();
167 }
168 $maskedDB = mb_substr($em, 0, -$hash->getLength() - 1, '8bit');
169 $h = mb_substr($em, -$hash->getLength() - 1, $hash->getLength(), '8bit');
170 $temp = chr(0xFF << ($emBits & 7));
171 if ((~$maskedDB[0] & $temp) !== $temp) {
172 throw new InvalidArgumentException();
173 }
174 $dbMask = self::getMGF1($h, $emLen - $hash->getLength() - 1, $hash/*MGF*/);
175 $db = $maskedDB ^ $dbMask;
176 $db[0] = ~chr(0xFF << ($emBits & 7)) & $db[0];
177 $temp = $emLen - $hash->getLength() - $sLen - 2;
178 if (mb_substr($db, 0, $temp, '8bit') !== str_repeat(chr(0), $temp)) {
179 throw new InvalidArgumentException();
180 }
181 if (ord($db[$temp]) !== 1) {
182 throw new InvalidArgumentException();
183 }
184 $salt = mb_substr($db, $temp + 1, null, '8bit'); // should be $sLen long
185 $m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt;
186 $h2 = $hash->hash($m2);
187
188 return hash_equals($h, $h2);
189 }
190 }