| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Crypter_OpenSslCrypter implements MWP_Crypter_Interface |
| 12 |
{ |
| 13 |
public function publicEncrypt($data, $publicKey) |
| 14 |
{ |
| 15 |
$errorCatcher = new MWP_Debug_ErrorCatcher(); |
| 16 |
$errorCatcher->register('openssl_public_encrypt'); |
| 17 |
/** @handled function */ |
| 18 |
$success = @openssl_public_encrypt($data, $crypted, $publicKey); |
| 19 |
$lastError = $errorCatcher->yieldErrorMessage(true); |
| 20 |
|
| 21 |
if ($success === false) { |
| 22 |
$error = $errorRow = ''; |
| 23 |
|
| 24 |
/** @handled function */ |
| 25 |
while (($errorRow = openssl_error_string()) !== false) { |
| 26 |
$error = $errorRow."\n".$error; |
| 27 |
} |
| 28 |
|
| 29 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::OPENSSL_ENCRYPT_ERROR, "There was an error while trying to use OpenSSL to encrypt a message.", array( |
| 30 |
'openSslError' => $error, |
| 31 |
'error' => isset($lastError['message']) ? $lastError['message'] : null, |
| 32 |
)); |
| 33 |
} |
| 34 |
|
| 35 |
return $crypted; |
| 36 |
} |
| 37 |
|
| 38 |
public function publicDecrypt($data, $publicKey) |
| 39 |
{ |
| 40 |
$errorCatcher = new MWP_Debug_ErrorCatcher(); |
| 41 |
$errorCatcher->register('openssl_public_decrypt'); |
| 42 |
/** @handled function */ |
| 43 |
$success = @openssl_public_decrypt($data, $decrypted, $publicKey); |
| 44 |
$lastError = $errorCatcher->yieldErrorMessage(true); |
| 45 |
|
| 46 |
if ($success === false && $lastError !== null) { |
| 47 |
$error = $errorRow = ''; |
| 48 |
|
| 49 |
/** @handled function */ |
| 50 |
while (($errorRow = openssl_error_string()) !== false) { |
| 51 |
$error = $errorRow."\n".$error; |
| 52 |
} |
| 53 |
|
| 54 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::OPENSSL_DECRYPT_ERROR, "There was an error while trying to use OpenSSL to decrypt a message.", array( |
| 55 |
'openSslError' => $error, |
| 56 |
'error' => $lastError, |
| 57 |
)); |
| 58 |
} |
| 59 |
|
| 60 |
return $decrypted === false ? null : $decrypted; |
| 61 |
} |
| 62 |
} |
| 63 |
|