2
0

Rsa.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_InfoCard_Cipher_Pki_Adapter_Abstract
  24. */
  25. require_once 'Zend/InfoCard/Cipher/Pki/Adapter/Abstract.php';
  26. /**
  27. * Zend_InfoCard_Cipher_Pki_Rsa_Interface
  28. */
  29. require_once 'Zend/InfoCard/Cipher/Pki/Rsa/Interface.php';
  30. /**
  31. * RSA Public Key Encryption Cipher Object for the InfoCard component. Relies on OpenSSL
  32. * to implement the RSA algorithm
  33. *
  34. * @category Zend
  35. * @package Zend_InfoCard
  36. * @subpackage Zend_InfoCard_Cipher
  37. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_InfoCard_Cipher_Pki_Adapter_Rsa
  41. extends Zend_InfoCard_Cipher_Pki_Adapter_Abstract
  42. implements Zend_InfoCard_Cipher_Pki_Rsa_Interface
  43. {
  44. /**
  45. * Object Constructor
  46. *
  47. * @param integer $padding The type of Padding to use
  48. */
  49. public function __construct($padding = Zend_InfoCard_Cipher_Pki_Adapter_Abstract::NO_PADDING)
  50. {
  51. // Can't test this..
  52. // @codeCoverageIgnoreStart
  53. if(!extension_loaded('openssl')) {
  54. require_once 'Zend/InfoCard/Cipher/Exception.php';
  55. throw new Zend_InfoCard_Cipher_Exception("Use of this PKI RSA Adapter requires the openssl extension loaded");
  56. }
  57. // @codeCoverageIgnoreEnd
  58. $this->setPadding($padding);
  59. }
  60. /**
  61. * Decrypts RSA encrypted data using the given private key
  62. *
  63. * @throws Zend_InfoCard_Cipher_Exception
  64. * @param string $encryptedData The encrypted data in binary format
  65. * @param string $privateKey The private key in binary format
  66. * @param string $password The private key passphrase
  67. * @param integer $padding The padding to use during decryption (of not provided object value will be used)
  68. * @return string The decrypted data
  69. */
  70. public function decrypt($encryptedData, $privateKey, $password = null, $padding = null)
  71. {
  72. $private_key = openssl_pkey_get_private(array($privateKey, $password));
  73. if(!$private_key) {
  74. require_once 'Zend/InfoCard/Cipher/Exception.php';
  75. throw new Zend_InfoCard_Cipher_Exception("Failed to load private key");
  76. }
  77. if($padding !== null) {
  78. try {
  79. $this->setPadding($padding);
  80. } catch(Exception $e) {
  81. openssl_free_key($private_key);
  82. throw $e;
  83. }
  84. }
  85. switch($this->getPadding()) {
  86. case self::NO_PADDING:
  87. $openssl_padding = OPENSSL_NO_PADDING;
  88. break;
  89. case self::OAEP_PADDING:
  90. $openssl_padding = OPENSSL_PKCS1_OAEP_PADDING;
  91. break;
  92. }
  93. $result = openssl_private_decrypt($encryptedData, $decryptedData, $private_key, $openssl_padding);
  94. openssl_free_key($private_key);
  95. if(!$result) {
  96. require_once 'Zend/InfoCard/Cipher/Exception.php';
  97. throw new Zend_InfoCard_Cipher_Exception("Unable to Decrypt Value using provided private key");
  98. }
  99. if($this->getPadding() == self::NO_PADDING) {
  100. $decryptedData = substr($decryptedData, 2);
  101. $start = strpos($decryptedData, 0) + 1;
  102. $decryptedData = substr($decryptedData, $start);
  103. }
  104. return $decryptedData;
  105. }
  106. }