| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class provides the functionality to encrypt |
| 5 |
* and decrypt access tokens stored by the application |
| 6 |
* @author Ben Tadiar <ben@handcraftedbyben.co.uk> |
| 7 |
* @link https://github.com/benthedesigner/dropbox |
| 8 |
* @package Dropbox\Oauth |
| 9 |
* @subpackage Storage |
| 10 |
*/ |
| 11 |
|
| 12 |
class Dropbox_Encrypter |
| 13 |
{ |
| 14 |
// Encryption settings - default settings yield encryption to AES (256-bit) standard |
| 15 |
// @todo Provide PHPDOC for each class constant |
| 16 |
const KEY_SIZE = 32; |
| 17 |
const IV_SIZE = 16; |
| 18 |
|
| 19 |
/** |
| 20 |
* Encryption key |
| 21 |
* @var null|string |
| 22 |
*/ |
| 23 |
private $key = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Check Mcrypt is loaded and set the encryption key |
| 27 |
* @param string $key |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function __construct($key) |
| 31 |
{ |
| 32 |
if (preg_match('/^[A-Za-z0-9]+$/', $key) && $length = strlen($key) === self::KEY_SIZE) { |
| 33 |
# Short-cut so that the mbstring extension is not required |
| 34 |
$this->key = $key; |
| 35 |
} elseif (($length = mb_strlen($key, '8bit')) !== self::KEY_SIZE) { |
| 36 |
throw new Dropbox_Exception('Expecting a ' . self::KEY_SIZE . ' byte key, got ' . $length); |
| 37 |
} else { |
| 38 |
// Set the encryption key |
| 39 |
$this->key = $key; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Encrypt the OAuth token |
| 45 |
* @param \stdClass $token Serialized token object |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function encrypt($token) |
| 49 |
{ |
| 50 |
|
| 51 |
// Encryption: we always use phpseclib for this |
| 52 |
global $iwp_backup_core; |
| 53 |
$ensure_phpseclib = $iwp_backup_core->ensure_phpseclib('Crypt_AES'); |
| 54 |
|
| 55 |
if (is_wp_error($ensure_phpseclib)) { |
| 56 |
$iwp_backup_core->log("Failed to load phpseclib classes (".$ensure_phpseclib->get_error_code()."): ".$ensure_phpseclib->get_error_message()); |
| 57 |
$iwp_backup_core->log("Failed to load phpseclib classes (".$ensure_phpseclib->get_error_code()."): ".$ensure_phpseclib->get_error_message(), 'error'); |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
$iwp_backup_core->ensure_phpseclib('Crypt_Rijndael'); |
| 62 |
|
| 63 |
if (!function_exists('crypt_random_string')) require_once($GLOBALS['iwp_mmb_plugin_dir'].'/lib/phpseclib/phpseclib/phpseclib/Crypt/Random.php'); |
| 64 |
|
| 65 |
$iv = crypt_random_string(self::IV_SIZE); |
| 66 |
|
| 67 |
// Defaults to CBC mode |
| 68 |
$rijndael = new Crypt_Rijndael(); |
| 69 |
|
| 70 |
$rijndael->setKey($this->key); |
| 71 |
|
| 72 |
$rijndael->setIV($iv); |
| 73 |
|
| 74 |
$cipherText = $rijndael->encrypt($token); |
| 75 |
|
| 76 |
return base64_encode($iv . $cipherText); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Decrypt the ciphertext |
| 81 |
* @param string $cipherText |
| 82 |
* @return object \stdClass Unserialized token |
| 83 |
*/ |
| 84 |
public function decrypt($cipherText) |
| 85 |
{ |
| 86 |
|
| 87 |
// Decryption: prefer mcrypt, if available (since it can decrypt data encrypted by either mcrypt or phpseclib) |
| 88 |
|
| 89 |
$cipherText = base64_decode($cipherText); |
| 90 |
$iv = substr($cipherText, 0, self::IV_SIZE); |
| 91 |
$cipherText = substr($cipherText, self::IV_SIZE); |
| 92 |
|
| 93 |
if (function_exists('mcrypt_decrypt')) { |
| 94 |
// @codingStandardsIgnoreLine |
| 95 |
$token = @mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $cipherText, MCRYPT_MODE_CBC, $iv); |
| 96 |
} else { |
| 97 |
global $iwp_backup_core; |
| 98 |
$iwp_backup_core->ensure_phpseclib('Crypt_Rijndael'); |
| 99 |
|
| 100 |
$rijndael = new Crypt_Rijndael(); |
| 101 |
$rijndael->setKey($this->key); |
| 102 |
$rijndael->setIV($iv); |
| 103 |
$token = $rijndael->decrypt($cipherText); |
| 104 |
} |
| 105 |
|
| 106 |
return $token; |
| 107 |
} |
| 108 |
} |
| 109 |
|