Complex classes like RSA often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RSA, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class RSA |
||
20 | { |
||
21 | /**#@+ |
||
22 | * @see self::encrypt() |
||
23 | * @see self::decrypt() |
||
24 | */ |
||
25 | /** |
||
26 | * Use {@link http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding Optimal Asymmetric Encryption Padding} |
||
27 | * (OAEP) for encryption / decryption. |
||
28 | * |
||
29 | * Uses sha1 by default. |
||
30 | * |
||
31 | * @see self::setHash() |
||
32 | * @see self::setMGFHash() |
||
33 | */ |
||
34 | const ENCRYPTION_OAEP = 1; |
||
35 | |||
36 | /** |
||
37 | * Use PKCS#1 padding. |
||
38 | * |
||
39 | * Although self::ENCRYPTION_OAEP offers more security, including PKCS#1 padding is necessary for purposes of backwards |
||
40 | * compatibility with protocols (like SSH-1) written before OAEP's introduction. |
||
41 | */ |
||
42 | const ENCRYPTION_PKCS1 = 2; |
||
43 | |||
44 | /**#@-*/ |
||
45 | |||
46 | /**#@+ |
||
47 | * @see self::sign() |
||
48 | * @see self::verify() |
||
49 | * @see self::setHash() |
||
50 | */ |
||
51 | /** |
||
52 | * Use the Probabilistic Signature Scheme for signing. |
||
53 | * |
||
54 | * Uses sha1 by default. |
||
55 | * |
||
56 | * @see self::setSaltLength() |
||
57 | * @see self::setMGFHash() |
||
58 | */ |
||
59 | const SIGNATURE_PSS = 1; |
||
60 | /** |
||
61 | * Use the PKCS#1 scheme by default. |
||
62 | * |
||
63 | * Although self::SIGNATURE_PSS offers more security, including PKCS#1 signing is necessary for purposes of backwards |
||
64 | * compatibility with protocols (like SSH-2) written before PSS's introduction. |
||
65 | */ |
||
66 | const SIGNATURE_PKCS1 = 2; |
||
67 | /**#@-*/ |
||
68 | |||
69 | /**#@+ |
||
70 | * @see \phpseclib\Crypt\RSA::createKey() |
||
71 | */ |
||
72 | /** |
||
73 | * ASN1 Integer. |
||
74 | */ |
||
75 | const ASN1_INTEGER = 2; |
||
76 | /** |
||
77 | * ASN1 Bit String. |
||
78 | */ |
||
79 | const ASN1_BITSTRING = 3; |
||
80 | /** |
||
81 | * ASN1 Octet String. |
||
82 | */ |
||
83 | const ASN1_OCTETSTRING = 4; |
||
84 | /** |
||
85 | * ASN1 Object Identifier. |
||
86 | */ |
||
87 | const ASN1_OBJECT = 6; |
||
88 | /** |
||
89 | * ASN1 Sequence (with the constucted bit set). |
||
90 | */ |
||
91 | const ASN1_SEQUENCE = 48; |
||
92 | /**#@-*/ |
||
93 | |||
94 | /**#@+ |
||
95 | * @see \phpseclib\Crypt\RSA::__construct() |
||
96 | */ |
||
97 | /** |
||
98 | * To use the pure-PHP implementation. |
||
99 | */ |
||
100 | const MODE_INTERNAL = 1; |
||
101 | /** |
||
102 | * To use the OpenSSL library. |
||
103 | * |
||
104 | * (if enabled; otherwise, the internal implementation will be used) |
||
105 | */ |
||
106 | const MODE_OPENSSL = 2; |
||
107 | /**#@-*/ |
||
108 | |||
109 | /**#@+ |
||
110 | * @see \phpseclib\Crypt\RSA::createKey() |
||
111 | * @see \phpseclib\Crypt\RSA::setPrivateKeyFormat() |
||
112 | */ |
||
113 | /** |
||
114 | * PKCS#1 formatted private key. |
||
115 | * |
||
116 | * Used by OpenSSH |
||
117 | */ |
||
118 | const PRIVATE_FORMAT_PKCS1 = 0; |
||
119 | /** |
||
120 | * PuTTY formatted private key. |
||
121 | */ |
||
122 | const PRIVATE_FORMAT_PUTTY = 1; |
||
123 | /** |
||
124 | * XML formatted private key. |
||
125 | */ |
||
126 | const PRIVATE_FORMAT_XML = 2; |
||
127 | /** |
||
128 | * PKCS#8 formatted private key. |
||
129 | */ |
||
130 | const PRIVATE_FORMAT_PKCS8 = 8; |
||
131 | /**#@-*/ |
||
132 | |||
133 | /**#@+ |
||
134 | * @see \phpseclib\Crypt\RSA::createKey() |
||
135 | * @see \phpseclib\Crypt\RSA::setPublicKeyFormat() |
||
136 | */ |
||
137 | /** |
||
138 | * Raw public key. |
||
139 | * |
||
140 | * An array containing two \Jose\Util\BigInteger objects. |
||
141 | * |
||
142 | * The exponent can be indexed with any of the following: |
||
143 | * |
||
144 | * 0, e, exponent, publicExponent |
||
145 | * |
||
146 | * The modulus can be indexed with any of the following: |
||
147 | * |
||
148 | * 1, n, modulo, modulus |
||
149 | */ |
||
150 | const PUBLIC_FORMAT_RAW = 3; |
||
151 | /** |
||
152 | * PKCS#1 formatted public key (raw). |
||
153 | * |
||
154 | * Used by File/X509.php |
||
155 | * |
||
156 | * Has the following header: |
||
157 | * |
||
158 | * -----BEGIN RSA PUBLIC KEY----- |
||
159 | * |
||
160 | * Analogous to ssh-keygen's pem format (as specified by -m) |
||
161 | */ |
||
162 | const PUBLIC_FORMAT_PKCS1 = 4; |
||
163 | const PUBLIC_FORMAT_PKCS1_RAW = 4; |
||
164 | /** |
||
165 | * XML formatted public key. |
||
166 | */ |
||
167 | const PUBLIC_FORMAT_XML = 5; |
||
168 | /** |
||
169 | * OpenSSH formatted public key. |
||
170 | * |
||
171 | * Place in $HOME/.ssh/authorized_keys |
||
172 | */ |
||
173 | const PUBLIC_FORMAT_OPENSSH = 6; |
||
174 | /** |
||
175 | * PKCS#1 formatted public key (encapsulated). |
||
176 | * |
||
177 | * Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set) |
||
178 | * |
||
179 | * Has the following header: |
||
180 | * |
||
181 | * -----BEGIN PUBLIC KEY----- |
||
182 | * |
||
183 | * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8 |
||
184 | * is specific to private keys it's basically creating a DER-encoded wrapper |
||
185 | * for keys. This just extends that same concept to public keys (much like ssh-keygen) |
||
186 | */ |
||
187 | const PUBLIC_FORMAT_PKCS8 = 7; |
||
188 | /**#@-*/ |
||
189 | |||
190 | /** |
||
191 | * Precomputed Zero. |
||
192 | * |
||
193 | * @var array |
||
194 | */ |
||
195 | private $zero; |
||
196 | |||
197 | /** |
||
198 | * Precomputed One. |
||
199 | * |
||
200 | * @var array |
||
201 | */ |
||
202 | private $one; |
||
203 | |||
204 | /** |
||
205 | * Private Key Format. |
||
206 | * |
||
207 | * @var int |
||
208 | */ |
||
209 | private $privateKeyFormat = self::PRIVATE_FORMAT_PKCS1; |
||
210 | |||
211 | /** |
||
212 | * Public Key Format. |
||
213 | * |
||
214 | * @var int |
||
215 | */ |
||
216 | private $publicKeyFormat = self::PUBLIC_FORMAT_PKCS8; |
||
217 | |||
218 | /** |
||
219 | * Modulus (ie. n). |
||
220 | * |
||
221 | * @var \Jose\Util\BigInteger |
||
222 | */ |
||
223 | private $modulus; |
||
224 | |||
225 | /** |
||
226 | * Modulus length. |
||
227 | * |
||
228 | * @var \Jose\Util\BigInteger |
||
229 | */ |
||
230 | private $k; |
||
231 | |||
232 | /** |
||
233 | * Exponent (ie. e or d). |
||
234 | * |
||
235 | * @var \Jose\Util\BigInteger |
||
236 | */ |
||
237 | private $exponent; |
||
238 | |||
239 | /** |
||
240 | * Primes for Chinese Remainder Theorem (ie. p and q). |
||
241 | * |
||
242 | * @var array |
||
243 | */ |
||
244 | private $primes; |
||
245 | |||
246 | /** |
||
247 | * Exponents for Chinese Remainder Theorem (ie. dP and dQ). |
||
248 | * |
||
249 | * @var array |
||
250 | */ |
||
251 | private $exponents; |
||
252 | |||
253 | /** |
||
254 | * Coefficients for Chinese Remainder Theorem (ie. qInv). |
||
255 | * |
||
256 | * @var array |
||
257 | */ |
||
258 | private $coefficients; |
||
259 | |||
260 | /** |
||
261 | * Hash name. |
||
262 | * |
||
263 | * @var string |
||
264 | */ |
||
265 | private $hashName; |
||
266 | |||
267 | /** |
||
268 | * Hash function. |
||
269 | * |
||
270 | * @var \Jose\Util\Hash |
||
271 | */ |
||
272 | private $hash; |
||
273 | |||
274 | /** |
||
275 | * Length of hash function output. |
||
276 | * |
||
277 | * @var int |
||
278 | */ |
||
279 | private $hLen; |
||
280 | |||
281 | /** |
||
282 | * Length of salt. |
||
283 | * |
||
284 | * @var int |
||
285 | */ |
||
286 | private $sLen; |
||
287 | |||
288 | /** |
||
289 | * Hash function for the Mask Generation Function. |
||
290 | * |
||
291 | * @var \Jose\Util\Hash |
||
292 | */ |
||
293 | private $mgfHash; |
||
294 | |||
295 | /** |
||
296 | * Length of MGF hash function output. |
||
297 | * |
||
298 | * @var int |
||
299 | */ |
||
300 | private $mgfHLen; |
||
301 | |||
302 | /** |
||
303 | * Encryption mode. |
||
304 | * |
||
305 | * @var int |
||
306 | */ |
||
307 | private $encryptionMode = self::ENCRYPTION_OAEP; |
||
308 | |||
309 | /** |
||
310 | * Signature mode. |
||
311 | * |
||
312 | * @var int |
||
313 | */ |
||
314 | private $signatureMode = self::SIGNATURE_PSS; |
||
315 | |||
316 | /** |
||
317 | * Public Exponent. |
||
318 | * |
||
319 | * @var mixed |
||
320 | */ |
||
321 | private $publicExponent = false; |
||
322 | |||
323 | /** |
||
324 | * Password. |
||
325 | * |
||
326 | * @var string |
||
327 | */ |
||
328 | private $password = false; |
||
329 | |||
330 | /** |
||
331 | * Components. |
||
332 | * |
||
333 | * For use with parsing XML formatted keys. PHP's XML Parser functions use utilized - instead of PHP's DOM functions - |
||
334 | * because PHP's XML Parser functions work on PHP4 whereas PHP's DOM functions - although surperior - don't. |
||
335 | * |
||
336 | * @see self::_start_element_handler() |
||
337 | * |
||
338 | * @var array |
||
339 | */ |
||
340 | private $components = []; |
||
341 | |||
342 | /** |
||
343 | * Current String. |
||
344 | * |
||
345 | * For use with parsing XML formatted keys. |
||
346 | * |
||
347 | * @see self::_character_handler() |
||
348 | * @see self::_stop_element_handler() |
||
349 | * |
||
350 | * @var mixed |
||
351 | */ |
||
352 | private $current; |
||
353 | |||
354 | /** |
||
355 | * OpenSSL configuration file name. |
||
356 | * |
||
357 | * Set to null to use system configuration file. |
||
358 | * |
||
359 | * @see self::createKey() |
||
360 | * |
||
361 | * @var mixed |
||
362 | * @Access public |
||
363 | */ |
||
364 | private $configFile; |
||
365 | |||
366 | /** |
||
367 | * Public key comment field. |
||
368 | * |
||
369 | * @var string |
||
370 | */ |
||
371 | private $comment = 'phpseclib-generated-key'; |
||
372 | |||
373 | /** |
||
374 | * The constructor. |
||
375 | * |
||
376 | * If you want to make use of the openssl extension, you'll need to set the mode manually, yourself. The reason |
||
377 | * \phpseclib\Crypt\RSA doesn't do it is because OpenSSL doesn't fail gracefully. openssl_pkey_new(), in particular, requires |
||
378 | * openssl.cnf be present somewhere and, unfortunately, the only real way to find out is too late. |
||
379 | */ |
||
380 | public function __construct() |
||
441 | |||
442 | /** |
||
443 | * Break a public or private key down into its constituant components. |
||
444 | * |
||
445 | * @see self::_convertPublicKey() |
||
446 | * @see self::_convertPrivateKey() |
||
447 | * |
||
448 | * @param string $key |
||
449 | * @param int $type |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | private function _parseKey($key, $type) |
||
842 | |||
843 | /** |
||
844 | * Start Element Handler. |
||
845 | * |
||
846 | * Called by xml_set_element_handler() |
||
847 | * |
||
848 | * @param resource $parser |
||
849 | * @param string $name |
||
850 | * @param array $attribs |
||
851 | */ |
||
852 | private function _start_element_handler($parser, $name, $attribs) |
||
882 | |||
883 | /** |
||
884 | * Stop Element Handler. |
||
885 | * |
||
886 | * Called by xml_set_element_handler() |
||
887 | * |
||
888 | * @param resource $parser |
||
889 | * @param string $name |
||
890 | */ |
||
891 | private function _stop_element_handler($parser, $name) |
||
898 | |||
899 | /** |
||
900 | * Data Handler. |
||
901 | * |
||
902 | * Called by xml_set_character_data_handler() |
||
903 | * |
||
904 | * @param resource $parser |
||
905 | * @param string $data |
||
906 | */ |
||
907 | public function _data_handler($parser, $data) |
||
914 | |||
915 | /** |
||
916 | * Loads a public or private key. |
||
917 | * |
||
918 | * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed) |
||
919 | * |
||
920 | * @param string $key |
||
921 | * @param int $type optional |
||
922 | */ |
||
923 | public function loadKey($key, $type = false) |
||
1027 | |||
1028 | /** |
||
1029 | * DER-decode the length. |
||
1030 | * |
||
1031 | * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See |
||
1032 | * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information. |
||
1033 | * |
||
1034 | * @param string $string |
||
1035 | * |
||
1036 | * @return int |
||
1037 | */ |
||
1038 | private function _decodeLength(&$string) |
||
1049 | |||
1050 | /** |
||
1051 | * String Shift. |
||
1052 | * |
||
1053 | * Inspired by array_shift |
||
1054 | * |
||
1055 | * @param string $string |
||
1056 | * @param int $index |
||
1057 | * |
||
1058 | * @return string |
||
1059 | */ |
||
1060 | private function _string_shift(&$string, $index = 1) |
||
1067 | |||
1068 | /** |
||
1069 | * Determines which hashing function should be used. |
||
1070 | * |
||
1071 | * Used with signature production / verification and (if the encryption mode is self::ENCRYPTION_OAEP) encryption and |
||
1072 | * decryption. If $hash isn't supported, sha1 is used. |
||
1073 | * |
||
1074 | * @param string $hash |
||
1075 | */ |
||
1076 | public function setHash($hash) |
||
1096 | |||
1097 | /** |
||
1098 | * Determines which hashing function should be used for the mask generation function. |
||
1099 | * |
||
1100 | * The mask generation function is used by self::ENCRYPTION_OAEP and self::SIGNATURE_PSS and although it's |
||
1101 | * best if Hash and MGFHash are set to the same thing this is not a requirement. |
||
1102 | * |
||
1103 | * @param string $hash |
||
1104 | */ |
||
1105 | public function setMGFHash($hash) |
||
1125 | |||
1126 | /** |
||
1127 | * Determines the salt length. |
||
1128 | * |
||
1129 | * To quote from {@link http://tools.ietf.org/html/rfc3447#page-38 RFC3447#page-38}: |
||
1130 | * |
||
1131 | * Typical salt lengths in octets are hLen (the length of the output |
||
1132 | * of the hash function Hash) and 0. |
||
1133 | * |
||
1134 | * @param int $format |
||
1135 | */ |
||
1136 | public function setSaltLength($sLen) |
||
1140 | |||
1141 | /** |
||
1142 | * Integer-to-Octet-String primitive. |
||
1143 | * |
||
1144 | * See {@link http://tools.ietf.org/html/rfc3447#section-4.1 RFC3447#section-4.1}. |
||
1145 | * |
||
1146 | * @param \Jose\Util\BigInteger $x |
||
1147 | * @param int $xLen |
||
1148 | * |
||
1149 | * @return string |
||
1150 | */ |
||
1151 | private function _i2osp($x, $xLen) |
||
1162 | |||
1163 | /** |
||
1164 | * Octet-String-to-Integer primitive. |
||
1165 | * |
||
1166 | * See {@link http://tools.ietf.org/html/rfc3447#section-4.2 RFC3447#section-4.2}. |
||
1167 | * |
||
1168 | * @param string $x |
||
1169 | * |
||
1170 | * @return \Jose\Util\BigInteger |
||
1171 | */ |
||
1172 | private function _os2ip($x) |
||
1176 | |||
1177 | /** |
||
1178 | * Exponentiate with or without Chinese Remainder Theorem. |
||
1179 | * |
||
1180 | * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.2}. |
||
1181 | * |
||
1182 | * @param \Jose\Util\BigInteger $x |
||
1183 | * |
||
1184 | * @return \Jose\Util\BigInteger |
||
1185 | */ |
||
1186 | private function _exponentiate($x) |
||
1253 | |||
1254 | /** |
||
1255 | * Performs RSA Blinding. |
||
1256 | * |
||
1257 | * Protects against timing attacks by employing RSA Blinding. |
||
1258 | * Returns $x->modPow($this->exponents[$i], $this->primes[$i]) |
||
1259 | * |
||
1260 | * @param \Jose\Util\BigInteger $x |
||
1261 | * @param \Jose\Util\BigInteger $r |
||
1262 | * @param int $i |
||
1263 | * |
||
1264 | * @return \Jose\Util\BigInteger |
||
1265 | */ |
||
1266 | private function _blind($x, $r, $i) |
||
1277 | |||
1278 | /** |
||
1279 | * Performs blinded RSA equality testing. |
||
1280 | * |
||
1281 | * Protects against a particular type of timing attack described. |
||
1282 | * |
||
1283 | * See {@link http://codahale.com/a-lesson-in-timing-attacks/ A Lesson In Timing Attacks (or, Don't use MessageDigest.isEquals)} |
||
1284 | * |
||
1285 | * Thanks for the heads up singpolyma! |
||
1286 | * |
||
1287 | * @param string $x |
||
1288 | * @param string $y |
||
1289 | * |
||
1290 | * @return bool |
||
1291 | */ |
||
1292 | private function _equals($x, $y) |
||
1305 | |||
1306 | /** |
||
1307 | * RSAEP. |
||
1308 | * |
||
1309 | * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.1}. |
||
1310 | * |
||
1311 | * @param \Jose\Util\BigInteger $m |
||
1312 | * |
||
1313 | * @return \Jose\Util\BigInteger |
||
1314 | */ |
||
1315 | private function _rsaep($m) |
||
1325 | |||
1326 | /** |
||
1327 | * RSADP. |
||
1328 | * |
||
1329 | * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}. |
||
1330 | * |
||
1331 | * @param \Jose\Util\BigInteger $c |
||
1332 | * |
||
1333 | * @return \Jose\Util\BigInteger |
||
1334 | */ |
||
1335 | private function _rsadp($c) |
||
1345 | |||
1346 | /** |
||
1347 | * RSASP1. |
||
1348 | * |
||
1349 | * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}. |
||
1350 | * |
||
1351 | * @param \Jose\Util\BigInteger $m |
||
1352 | * |
||
1353 | * @return \Jose\Util\BigInteger |
||
1354 | */ |
||
1355 | private function _rsasp1($m) |
||
1365 | |||
1366 | /** |
||
1367 | * RSAVP1. |
||
1368 | * |
||
1369 | * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.2 RFC3447#section-5.2.2}. |
||
1370 | * |
||
1371 | * @param \Jose\Util\BigInteger $s |
||
1372 | * |
||
1373 | * @return \Jose\Util\BigInteger |
||
1374 | */ |
||
1375 | private function _rsavp1($s) |
||
1385 | |||
1386 | /** |
||
1387 | * MGF1. |
||
1388 | * |
||
1389 | * See {@link http://tools.ietf.org/html/rfc3447#appendix-B.2.1 RFC3447#appendix-B.2.1}. |
||
1390 | * |
||
1391 | * @param string $mgfSeed |
||
1392 | * @param int $mgfLen |
||
1393 | * |
||
1394 | * @return string |
||
1395 | */ |
||
1396 | private function _mgf1($mgfSeed, $maskLen) |
||
1409 | |||
1410 | /** |
||
1411 | * RSAES-OAEP-ENCRYPT. |
||
1412 | * |
||
1413 | * See {@link http://tools.ietf.org/html/rfc3447#section-7.1.1 RFC3447#section-7.1.1} and |
||
1414 | * {http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding OAES}. |
||
1415 | * |
||
1416 | * @param string $m |
||
1417 | * @param string $l |
||
1418 | * |
||
1419 | * @return string |
||
1420 | */ |
||
1421 | private function _rsaes_oaep_encrypt($m, $l = '') |
||
1458 | |||
1459 | /** |
||
1460 | * RSAES-OAEP-DECRYPT. |
||
1461 | * |
||
1462 | * See {@link http://tools.ietf.org/html/rfc3447#section-7.1.2 RFC3447#section-7.1.2}. The fact that the error |
||
1463 | * messages aren't distinguishable from one another hinders debugging, but, to quote from RFC3447#section-7.1.2: |
||
1464 | * |
||
1465 | * Note. Care must be taken to ensure that an opponent cannot |
||
1466 | * distinguish the different error conditions in Step 3.g, whether by |
||
1467 | * error message or timing, or, more generally, learn partial |
||
1468 | * information about the encoded message EM. Otherwise an opponent may |
||
1469 | * be able to obtain useful information about the decryption of the |
||
1470 | * ciphertext C, leading to a chosen-ciphertext attack such as the one |
||
1471 | * observed by Manger [36]. |
||
1472 | * |
||
1473 | * As for $l... to quote from {@link http://tools.ietf.org/html/rfc3447#page-17 RFC3447#page-17}: |
||
1474 | * |
||
1475 | * Both the encryption and the decryption operations of RSAES-OAEP take |
||
1476 | * the value of a label L as input. In this version of PKCS #1, L is |
||
1477 | * the empty string; other uses of the label are outside the scope of |
||
1478 | * this document. |
||
1479 | * |
||
1480 | * @param string $c |
||
1481 | * @param string $l |
||
1482 | * |
||
1483 | * @return string |
||
1484 | */ |
||
1485 | private function _rsaes_oaep_decrypt($c, $l = '') |
||
1537 | |||
1538 | /** |
||
1539 | * RSAES-PKCS1-V1_5-ENCRYPT. |
||
1540 | * |
||
1541 | * See {@link http://tools.ietf.org/html/rfc3447#section-7.2.1 RFC3447#section-7.2.1}. |
||
1542 | * |
||
1543 | * @param string $m |
||
1544 | * |
||
1545 | * @return string |
||
1546 | */ |
||
1547 | private function _rsaes_pkcs1_v1_5_encrypt($m) |
||
1586 | |||
1587 | /** |
||
1588 | * RSAES-PKCS1-V1_5-DECRYPT. |
||
1589 | * |
||
1590 | * See {@link http://tools.ietf.org/html/rfc3447#section-7.2.2 RFC3447#section-7.2.2}. |
||
1591 | * |
||
1592 | * For compatibility purposes, this function departs slightly from the description given in RFC3447. |
||
1593 | * The reason being that RFC2313#section-8.1 (PKCS#1 v1.5) states that ciphertext's encrypted by the |
||
1594 | * private key should have the second byte set to either 0 or 1 and that ciphertext's encrypted by the |
||
1595 | * public key should have the second byte set to 2. In RFC3447 (PKCS#1 v2.1), the second byte is supposed |
||
1596 | * to be 2 regardless of which key is used. For compatibility purposes, we'll just check to make sure the |
||
1597 | * second byte is 2 or less. If it is, we'll accept the decrypted string as valid. |
||
1598 | * |
||
1599 | * As a consequence of this, a private key encrypted ciphertext produced with \phpseclib\Crypt\RSA may not decrypt |
||
1600 | * with a strictly PKCS#1 v1.5 compliant RSA implementation. Public key encrypted ciphertext's should but |
||
1601 | * not private key encrypted ciphertext's. |
||
1602 | * |
||
1603 | * @param string $c |
||
1604 | * |
||
1605 | * @return string |
||
1606 | */ |
||
1607 | private function _rsaes_pkcs1_v1_5_decrypt($c) |
||
1650 | |||
1651 | /** |
||
1652 | * EMSA-PSS-ENCODE. |
||
1653 | * |
||
1654 | * See {@link http://tools.ietf.org/html/rfc3447#section-9.1.1 RFC3447#section-9.1.1}. |
||
1655 | * |
||
1656 | * @param string $m |
||
1657 | * @param int $emBits |
||
1658 | */ |
||
1659 | private function _emsa_pss_encode($m, $emBits) |
||
1686 | |||
1687 | /** |
||
1688 | * EMSA-PSS-VERIFY. |
||
1689 | * |
||
1690 | * See {@link http://tools.ietf.org/html/rfc3447#section-9.1.2 RFC3447#section-9.1.2}. |
||
1691 | * |
||
1692 | * @param string $m |
||
1693 | * @param string $em |
||
1694 | * @param int $emBits |
||
1695 | * |
||
1696 | * @return string |
||
1697 | */ |
||
1698 | private function _emsa_pss_verify($m, $em, $emBits) |
||
1734 | |||
1735 | /** |
||
1736 | * RSASSA-PSS-SIGN. |
||
1737 | * |
||
1738 | * See {@link http://tools.ietf.org/html/rfc3447#section-8.1.1 RFC3447#section-8.1.1}. |
||
1739 | * |
||
1740 | * @param string $m |
||
1741 | * |
||
1742 | * @return string |
||
1743 | */ |
||
1744 | private function _rsassa_pss_sign($m) |
||
1760 | |||
1761 | /** |
||
1762 | * RSASSA-PSS-VERIFY. |
||
1763 | * |
||
1764 | * See {@link http://tools.ietf.org/html/rfc3447#section-8.1.2 RFC3447#section-8.1.2}. |
||
1765 | * |
||
1766 | * @param string $m |
||
1767 | * @param string $s |
||
1768 | * |
||
1769 | * @return string |
||
1770 | */ |
||
1771 | private function _rsassa_pss_verify($m, $s) |
||
1803 | |||
1804 | /** |
||
1805 | * EMSA-PKCS1-V1_5-ENCODE. |
||
1806 | * |
||
1807 | * See {@link http://tools.ietf.org/html/rfc3447#section-9.2 RFC3447#section-9.2}. |
||
1808 | * |
||
1809 | * @param string $m |
||
1810 | * @param int $emLen |
||
1811 | * |
||
1812 | * @return string |
||
1813 | */ |
||
1814 | private function _emsa_pkcs1_v1_5_encode($m, $emLen) |
||
1856 | |||
1857 | /** |
||
1858 | * RSASSA-PKCS1-V1_5-SIGN. |
||
1859 | * |
||
1860 | * See {@link http://tools.ietf.org/html/rfc3447#section-8.2.1 RFC3447#section-8.2.1}. |
||
1861 | * |
||
1862 | * @param string $m |
||
1863 | * |
||
1864 | * @return string |
||
1865 | */ |
||
1866 | private function _rsassa_pkcs1_v1_5_sign($m) |
||
1887 | |||
1888 | /** |
||
1889 | * RSASSA-PKCS1-V1_5-VERIFY. |
||
1890 | * |
||
1891 | * See {@link http://tools.ietf.org/html/rfc3447#section-8.2.2 RFC3447#section-8.2.2}. |
||
1892 | * |
||
1893 | * @param string $m |
||
1894 | * |
||
1895 | * @return string |
||
1896 | */ |
||
1897 | private function _rsassa_pkcs1_v1_5_verify($m, $s) |
||
1935 | |||
1936 | /** |
||
1937 | * Set Encryption Mode. |
||
1938 | * |
||
1939 | * Valid values include self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1. |
||
1940 | * |
||
1941 | * @param int $mode |
||
1942 | */ |
||
1943 | public function setEncryptionMode($mode) |
||
1947 | |||
1948 | /** |
||
1949 | * Set Signature Mode. |
||
1950 | * |
||
1951 | * Valid values include self::SIGNATURE_PSS and self::SIGNATURE_PKCS1 |
||
1952 | * |
||
1953 | * @param int $mode |
||
1954 | */ |
||
1955 | public function setSignatureMode($mode) |
||
1959 | |||
1960 | /** |
||
1961 | * Encryption. |
||
1962 | * |
||
1963 | * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. |
||
1964 | * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will |
||
1965 | * be concatenated together. |
||
1966 | * |
||
1967 | * @see self::decrypt() |
||
1968 | * |
||
1969 | * @param string $plaintext |
||
1970 | * |
||
1971 | * @return string |
||
1972 | */ |
||
1973 | public function encrypt($plaintext) |
||
2005 | |||
2006 | /** |
||
2007 | * Decryption. |
||
2008 | * |
||
2009 | * @see self::encrypt() |
||
2010 | * |
||
2011 | * @param string $plaintext |
||
2012 | * |
||
2013 | * @return string |
||
2014 | */ |
||
2015 | public function decrypt($ciphertext) |
||
2045 | |||
2046 | /** |
||
2047 | * Create a signature. |
||
2048 | * |
||
2049 | * @see self::verify() |
||
2050 | * |
||
2051 | * @param string $message |
||
2052 | * |
||
2053 | * @return string |
||
2054 | */ |
||
2055 | public function sign($message) |
||
2069 | |||
2070 | /** |
||
2071 | * Verifies a signature. |
||
2072 | * |
||
2073 | * @see self::sign() |
||
2074 | * |
||
2075 | * @param string $message |
||
2076 | * @param string $signature |
||
2077 | * |
||
2078 | * @return bool |
||
2079 | */ |
||
2080 | public function verify($message, $signature) |
||
2094 | |||
2095 | /** |
||
2096 | * Extract raw BER from Base64 encoding. |
||
2097 | * |
||
2098 | * @param string $str |
||
2099 | * |
||
2100 | * @return string |
||
2101 | */ |
||
2102 | private function _extractBER($str) |
||
2122 | |||
2123 | /** |
||
2124 | * Defines the public key. |
||
2125 | * |
||
2126 | * Some private key formats define the public exponent and some don't. Those that don't define it are problematic when |
||
2127 | * used in certain contexts. For example, in SSH-2, RSA authentication works by sending the public key along with a |
||
2128 | * message signed by the private key to the server. The SSH-2 server looks the public key up in an index of public keys |
||
2129 | * and if it's present then proceeds to verify the signature. Problem is, if your private key doesn't include the public |
||
2130 | * exponent this won't work unless you manually add the public exponent. phpseclib tries to guess if the key being used |
||
2131 | * is the public key but in the event that it guesses incorrectly you might still want to explicitly set the key as being |
||
2132 | * public. |
||
2133 | * |
||
2134 | * Do note that when a new key is loaded the index will be cleared. |
||
2135 | * |
||
2136 | * Returns true on success, false on failure |
||
2137 | * |
||
2138 | * @see self::getPublicKey() |
||
2139 | * |
||
2140 | * @param string $key optional |
||
2141 | * @param int $type optional |
||
2142 | * |
||
2143 | * @return bool |
||
2144 | */ |
||
2145 | private function setPublicKey($key = false, $type = false) |
||
2190 | } |
||
2191 |
If you suppress an error, we recommend checking for the error condition explicitly: