web/enmi/Zend/InfoCard/Cipher/Pki/Adapter/Rsa.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_InfoCard
       
    17  * @subpackage Zend_InfoCard_Cipher
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Rsa.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * Zend_InfoCard_Cipher_Pki_Adapter_Abstract
       
    25  */
       
    26 require_once 'Zend/InfoCard/Cipher/Pki/Adapter/Abstract.php';
       
    27 
       
    28 /**
       
    29  * Zend_InfoCard_Cipher_Pki_Rsa_Interface
       
    30  */
       
    31 require_once 'Zend/InfoCard/Cipher/Pki/Rsa/Interface.php';
       
    32 
       
    33 /**
       
    34  * RSA Public Key Encryption Cipher Object for the InfoCard component. Relies on OpenSSL
       
    35  * to implement the RSA algorithm
       
    36  *
       
    37  * @category   Zend
       
    38  * @package    Zend_InfoCard
       
    39  * @subpackage Zend_InfoCard_Cipher
       
    40  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    42  */
       
    43 class Zend_InfoCard_Cipher_Pki_Adapter_Rsa
       
    44     extends Zend_InfoCard_Cipher_Pki_Adapter_Abstract
       
    45     implements Zend_InfoCard_Cipher_Pki_Rsa_Interface
       
    46 {
       
    47 
       
    48     /**
       
    49      * Object Constructor
       
    50      *
       
    51      * @param integer $padding The type of Padding to use
       
    52      */
       
    53     public function __construct($padding = Zend_InfoCard_Cipher_Pki_Adapter_Abstract::NO_PADDING)
       
    54     {
       
    55         // Can't test this..
       
    56         // @codeCoverageIgnoreStart
       
    57         if(!extension_loaded('openssl')) {
       
    58             require_once 'Zend/InfoCard/Cipher/Exception.php';
       
    59             throw new Zend_InfoCard_Cipher_Exception("Use of this PKI RSA Adapter requires the openssl extension loaded");
       
    60         }
       
    61         // @codeCoverageIgnoreEnd
       
    62 
       
    63         $this->setPadding($padding);
       
    64     }
       
    65 
       
    66     /**
       
    67      * Decrypts RSA encrypted data using the given private key
       
    68      *
       
    69      * @throws Zend_InfoCard_Cipher_Exception
       
    70      * @param string $encryptedData The encrypted data in binary format
       
    71      * @param string $privateKey The private key in binary format
       
    72      * @param string $password The private key passphrase
       
    73      * @param integer $padding The padding to use during decryption (of not provided object value will be used)
       
    74      * @return string The decrypted data
       
    75      */
       
    76     public function decrypt($encryptedData, $privateKey, $password = null, $padding = null)
       
    77     {
       
    78         $private_key = openssl_pkey_get_private(array($privateKey, $password));
       
    79 
       
    80         if(!$private_key) {
       
    81             require_once 'Zend/InfoCard/Cipher/Exception.php';
       
    82             throw new Zend_InfoCard_Cipher_Exception("Failed to load private key");
       
    83         }
       
    84 
       
    85         if($padding !== null) {
       
    86             try {
       
    87                 $this->setPadding($padding);
       
    88             } catch(Exception $e) {
       
    89                 openssl_free_key($private_key);
       
    90                 throw $e;
       
    91             }
       
    92         }
       
    93 
       
    94         switch($this->getPadding()) {
       
    95             case self::NO_PADDING:
       
    96                 $openssl_padding = OPENSSL_NO_PADDING;
       
    97                 break;
       
    98             case self::OAEP_PADDING:
       
    99                 $openssl_padding = OPENSSL_PKCS1_OAEP_PADDING;
       
   100                 break;
       
   101         }
       
   102 
       
   103         $result = openssl_private_decrypt($encryptedData, $decryptedData, $private_key, $openssl_padding);
       
   104 
       
   105         openssl_free_key($private_key);
       
   106 
       
   107         if(!$result) {
       
   108             require_once 'Zend/InfoCard/Cipher/Exception.php';
       
   109             throw new Zend_InfoCard_Cipher_Exception("Unable to Decrypt Value using provided private key");
       
   110         }
       
   111 
       
   112         if($this->getPadding() == self::NO_PADDING) {
       
   113             $decryptedData = substr($decryptedData, 2);
       
   114             $start = strpos($decryptedData, 0) + 1;
       
   115             $decryptedData = substr($decryptedData, $start);
       
   116         }
       
   117 
       
   118         return $decryptedData;
       
   119     }
       
   120 }