diff -r 5b37998e522e -r 162c1de6545a web/lib/Zend/InfoCard/Cipher/Pki/Adapter/Rsa.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/lib/Zend/InfoCard/Cipher/Pki/Adapter/Rsa.php Fri Mar 11 15:05:35 2011 +0100 @@ -0,0 +1,120 @@ +setPadding($padding); + } + + /** + * Decrypts RSA encrypted data using the given private key + * + * @throws Zend_InfoCard_Cipher_Exception + * @param string $encryptedData The encrypted data in binary format + * @param string $privateKey The private key in binary format + * @param string $password The private key passphrase + * @param integer $padding The padding to use during decryption (of not provided object value will be used) + * @return string The decrypted data + */ + public function decrypt($encryptedData, $privateKey, $password = null, $padding = null) + { + $private_key = openssl_pkey_get_private(array($privateKey, $password)); + + if(!$private_key) { + require_once 'Zend/InfoCard/Cipher/Exception.php'; + throw new Zend_InfoCard_Cipher_Exception("Failed to load private key"); + } + + if($padding !== null) { + try { + $this->setPadding($padding); + } catch(Exception $e) { + openssl_free_key($private_key); + throw $e; + } + } + + switch($this->getPadding()) { + case self::NO_PADDING: + $openssl_padding = OPENSSL_NO_PADDING; + break; + case self::OAEP_PADDING: + $openssl_padding = OPENSSL_PKCS1_OAEP_PADDING; + break; + } + + $result = openssl_private_decrypt($encryptedData, $decryptedData, $private_key, $openssl_padding); + + openssl_free_key($private_key); + + if(!$result) { + require_once 'Zend/InfoCard/Cipher/Exception.php'; + throw new Zend_InfoCard_Cipher_Exception("Unable to Decrypt Value using provided private key"); + } + + if($this->getPadding() == self::NO_PADDING) { + $decryptedData = substr($decryptedData, 2); + $start = strpos($decryptedData, 0) + 1; + $decryptedData = substr($decryptedData, $start); + } + + return $decryptedData; + } +}