| 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 CIPHER = MCRYPT_RIJNDAEL_128; |
| 17 |
const MODE = MCRYPT_MODE_CBC; |
| 18 |
const KEY_SIZE = 32; |
| 19 |
const IV_SIZE = 16; |
| 20 |
|
| 21 |
/** |
| 22 |
* Encryption key |
| 23 |
* @var null|string |
| 24 |
*/ |
| 25 |
private $key = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Check Mcrypt is loaded and set the encryption key |
| 29 |
* @param string $key |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function __construct($key) |
| 33 |
{ |
| 34 |
if (!extension_loaded('mcrypt')) { |
| 35 |
throw new Dropbox_Exception('The storage encrypter requires the PHP MCrypt extension to be available. Please check your PHP configuration.'); |
| 36 |
} elseif (preg_match('/^[A-Za-z0-9]+$/', $key) && $length = strlen($key) === self::KEY_SIZE) { |
| 37 |
# Short-cut so that the mbstring extension is not required |
| 38 |
$this->key = $key; |
| 39 |
} elseif (($length = mb_strlen($key, '8bit')) !== self::KEY_SIZE) { |
| 40 |
throw new Dropbox_Exception('Expecting a ' . self::KEY_SIZE . ' byte key, got ' . $length); |
| 41 |
} else { |
| 42 |
// Set the encryption key |
| 43 |
$this->key = $key; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Encrypt the OAuth token |
| 49 |
* @param \stdClass $token Serialized token object |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function encrypt($token) |
| 53 |
{ |
| 54 |
// This now sends all Windows users to MCRYPT_RAND - used to send PHP>5.3 to MCRYPT_DEV_URANDOM, but we came across a user this failed for |
| 55 |
// Only MCRYPT_RAND is available on Windows prior to PHP 5.3 |
| 56 |
if (version_compare(phpversion(), '5.3.0', '<') && strtoupper(substr(php_uname('s'), 0, 3)) === 'WIN') { |
| 57 |
$crypt_source = MCRYPT_RAND; |
| 58 |
} elseif (@is_readable("/dev/urandom")) { |
| 59 |
// Note that is_readable is not a true test of whether the mcrypt_create_iv call would work, because when open_basedir restrictions exist, is_readable returns false, but mcrypt_create_iv is not subject to that restriction internally, so would actually have succeeded. |
| 60 |
$crypt_source = MCRYPT_DEV_URANDOM; |
| 61 |
} else { |
| 62 |
$crypt_source = MCRYPT_RAND; |
| 63 |
} |
| 64 |
$iv = @mcrypt_create_iv(self::IV_SIZE, $crypt_source); |
| 65 |
|
| 66 |
if ($iv === false && $crypt_source != MCRYPT_RAND) { |
| 67 |
$iv = mcrypt_create_iv(self::IV_SIZE, MCRYPT_RAND); |
| 68 |
} |
| 69 |
|
| 70 |
$cipherText = @mcrypt_encrypt(self::CIPHER, $this->key, $token, self::MODE, $iv); |
| 71 |
return base64_encode($iv . $cipherText); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Decrypt the ciphertext |
| 76 |
* @param string $cipherText |
| 77 |
* @return object \stdClass Unserialized token |
| 78 |
*/ |
| 79 |
public function decrypt($cipherText) |
| 80 |
{ |
| 81 |
$cipherText = base64_decode($cipherText); |
| 82 |
$iv = substr($cipherText, 0, self::IV_SIZE); |
| 83 |
$cipherText = substr($cipherText, self::IV_SIZE); |
| 84 |
$token = @mcrypt_decrypt(self::CIPHER, $this->key, $cipherText, self::MODE, $iv); |
| 85 |
return $token; |
| 86 |
} |
| 87 |
} |
| 88 |
|