PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/s3db3.5.10/pearlib/RSACrypt/Crypt/RSA/Key.php

https://code.google.com/p/s3db/
PHP | 316 lines | 80 code | 21 blank | 215 comment | 5 complexity | c08941007ed7a3abf3b53dab735791fc MD5 | raw file
  1. <?php
  2. /**
  3. * Crypt_RSA allows to do following operations:
  4. * - key pair generation
  5. * - encryption and decryption
  6. * - signing and sign validation
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * LICENSE: This source file is subject to version 3.0 of the PHP license
  11. * that is available through the world-wide-web at the following URI:
  12. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  13. * the PHP License and are unable to obtain it through the web, please
  14. * send a note to license@php.net so we can mail you a copy immediately.
  15. *
  16. * @category Encryption
  17. * @package Crypt_RSA
  18. * @author Alexander Valyalkin <valyala@gmail.com>
  19. * @copyright 2005, 2006 Alexander Valyalkin
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version 1.2.0b
  22. * @link http://pear.php.net/package/Crypt_RSA
  23. */
  24. /**
  25. * RSA error handling facilities
  26. */
  27. require_once 'ErrorHandler.php';
  28. /**
  29. * loader for RSA math wrappers
  30. */
  31. require_once 'MathLoader.php';
  32. /**
  33. * Crypt_RSA_Key class, derived from Crypt_RSA_ErrorHandler
  34. *
  35. * Provides the following functions:
  36. * - getKeyLength() - returns bit key length
  37. * - getExponent() - returns key exponent as binary string
  38. * - getModulus() - returns key modulus as binary string
  39. * - getKeyType() - returns type of the key (public or private)
  40. * - toString() - returns serialized key as string
  41. * - fromString($key_str) - static function; returns key, unserialized from string
  42. * - isValid($key) - static function for validating of $key
  43. *
  44. * Example usage:
  45. * // create new 1024-bit key pair
  46. * $key_pair = new Crypt_RSA_KeyPair(1024);
  47. *
  48. * // get public key (its class is Crypt_RSA_Key)
  49. * $key = $key_pair->getPublicKey();
  50. *
  51. * // get key length
  52. * $len = $key->getKeyLength();
  53. *
  54. * // get modulus as string
  55. * $modulus = $key->getModulus();
  56. *
  57. * // get exponent as string
  58. * $exponent = $key->getExponent();
  59. *
  60. * // get string represenation of key (use it instead of serialization of Crypt_RSA_Key object)
  61. * $key_in_str = $key->toString();
  62. *
  63. * // restore key object from string using 'BigInt' math wrapper
  64. * $key = Crypt_RSA_Key::fromString($key_in_str, 'BigInt');
  65. *
  66. * // error check
  67. * if ($key->isError()) {
  68. * echo "error while unserializing key object:\n";
  69. * $erorr = $key->getLastError();
  70. * echo $error->getMessage(), "\n";
  71. * }
  72. *
  73. * // validate key
  74. * if (Crypt_RSA_Key::isValid($key)) echo 'valid key';
  75. * else echo 'invalid key';
  76. *
  77. * // using factory() method instead of constructor (it returns PEAR_Error object on failure)
  78. * $rsa_obj = &Crypt_RSA_Key::factory($modulus, $exp, $key_type);
  79. * if (PEAR::isError($rsa_obj)) {
  80. * echo "error: ", $rsa_obj->getMessage(), "\n";
  81. * }
  82. *
  83. * @category Encryption
  84. * @package Crypt_RSA
  85. * @author Alexander Valyalkin <valyala@gmail.com>
  86. * @copyright 2005, 2006 Alexander Valyalkin
  87. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  88. * @link http://pear.php.net/package/Crypt_RSA
  89. * @version @package_version@
  90. * @access public
  91. */
  92. class Crypt_RSA_Key extends Crypt_RSA_ErrorHandler
  93. {
  94. /**
  95. * Reference to math wrapper object, which is used to
  96. * manipulate large integers in RSA algorithm.
  97. *
  98. * @var object of Crypt_RSA_Math_* class
  99. * @access private
  100. */
  101. var $_math_obj;
  102. /**
  103. * shared modulus
  104. *
  105. * @var string
  106. * @access private
  107. */
  108. var $_modulus;
  109. /**
  110. * exponent
  111. *
  112. * @var string
  113. * @access private
  114. */
  115. var $_exp;
  116. /**
  117. * key type (private or public)
  118. *
  119. * @var string
  120. * @access private
  121. */
  122. var $_key_type;
  123. /**
  124. * key length in bits
  125. *
  126. * @var int
  127. * @access private
  128. */
  129. var $_key_len;
  130. /**
  131. * Crypt_RSA_Key constructor.
  132. *
  133. * @param string $modulus key modulus
  134. * @param string $exp key exponent
  135. * @param string $key_type type of the key (public or private)
  136. * @param string $wrapper_name
  137. * Name of math wrapper, which will be used to
  138. * perform different operations with big integers.
  139. * See contents of Crypt/RSA/Math folder for examples of wrappers.
  140. * Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
  141. * @param string $error_handler name of error handler function
  142. *
  143. * @access public
  144. */
  145. function Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name = 'default', $error_handler = '')
  146. {
  147. // set error handler
  148. $this->setErrorHandler($error_handler);
  149. // try to load math wrapper $wrapper_name
  150. $obj = &Crypt_RSA_MathLoader::loadWrapper($wrapper_name);
  151. if ($this->isError($obj)) {
  152. // error during loading of math wrapper
  153. $this->pushError($obj); // push error object into error list
  154. return;
  155. }
  156. $this->_math_obj = &$obj;
  157. $this->_modulus = $modulus;
  158. $this->_exp = $exp;
  159. if (!in_array($key_type, array('private', 'public'))) {
  160. $this->pushError('invalid key type. It must be private or public', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
  161. return;
  162. }
  163. $this->_key_type = $key_type;
  164. /* check length of modulus & exponent ( abs(modulus) > abs(exp) ) */
  165. $mod_num = $this->_math_obj->bin2int($this->_modulus);
  166. $exp_num = $this->_math_obj->bin2int($this->_exp);
  167. if ($this->_math_obj->cmpAbs($mod_num, $exp_num) <= 0) {
  168. $this->pushError('modulus must be greater than exponent', CRYPT_RSA_ERROR_EXP_GE_MOD);
  169. return;
  170. }
  171. // determine key length
  172. $this->_key_len = $this->_math_obj->bitLen($mod_num);
  173. }
  174. /**
  175. * Crypt_RSA_Key factory.
  176. *
  177. * @param string $modulus key modulus
  178. * @param string $exp key exponent
  179. * @param string $key_type type of the key (public or private)
  180. * @param string $wrapper_name
  181. * Name of math wrapper, which will be used to
  182. * perform different operations with big integers.
  183. * See contents of Crypt/RSA/Math folder for examples of wrappers.
  184. * Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
  185. * @param string $error_handler name of error handler function
  186. *
  187. * @return object new Crypt_RSA_Key object on success or PEAR_Error object on failure
  188. * @access public
  189. */
  190. function &factory($modulus, $exp, $key_type, $wrapper_name = 'default', $error_handler = '')
  191. {
  192. $obj = &new Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name, $error_handler);
  193. if ($obj->isError()) {
  194. // error during creating a new object. Retrurn PEAR_Error object
  195. return $obj->getLastError();
  196. }
  197. // object created successfully. Return it
  198. return $obj;
  199. }
  200. /**
  201. * Calculates bit length of the key
  202. *
  203. * @return int bit length of key
  204. * @access public
  205. */
  206. function getKeyLength()
  207. {
  208. return $this->_key_len;
  209. }
  210. /**
  211. * Returns modulus part of the key as binary string,
  212. * which can be used to construct new Crypt_RSA_Key object.
  213. *
  214. * @return string modulus as binary string
  215. * @access public
  216. */
  217. function getModulus()
  218. {
  219. return $this->_modulus;
  220. }
  221. /**
  222. * Returns exponent part of the key as binary string,
  223. * which can be used to construct new Crypt_RSA_Key object.
  224. *
  225. * @return string exponent as binary string
  226. * @access public
  227. */
  228. function getExponent()
  229. {
  230. return $this->_exp;
  231. }
  232. /**
  233. * Returns key type (public, private)
  234. *
  235. * @return string key type (public, private)
  236. * @access public
  237. */
  238. function getKeyType()
  239. {
  240. return $this->_key_type;
  241. }
  242. /**
  243. * Returns string representation of key
  244. *
  245. * @return string key, serialized to string
  246. * @access public
  247. */
  248. function toString()
  249. {
  250. return base64_encode(
  251. serialize(array(
  252. $this->_modulus,
  253. $this->_exp,
  254. $this->_key_type,
  255. ))
  256. );
  257. }
  258. /**
  259. * Returns Crypt_RSA_Key object, unserialized from
  260. * string representation of key.
  261. *
  262. * optional parameter $wrapper_name - is the name of math wrapper,
  263. * which will be used during unserialization of this object.
  264. *
  265. * This function can be called statically:
  266. * $key = Crypt_RSA_Key::fromString($key_in_string, 'BigInt');
  267. *
  268. * @param $key_str RSA key, serialized into string
  269. * @param $wrapper_name optional math wrapper name
  270. * @return object key as Crypt_RSA_Key object
  271. * @access public
  272. */
  273. function &fromString($key_str, $wrapper_name = 'default')
  274. {
  275. list($modulus, $exponent, $key_type) = unserialize(base64_decode($key_str));
  276. $obj = &new Crypt_RSA_Key($modulus, $exponent, $key_type, $wrapper_name);
  277. return $obj;
  278. }
  279. /**
  280. * Validates key
  281. * This function can be called statically:
  282. * $is_valid = Crypt_RSA_Key::isValid($key)
  283. *
  284. * Returns true, if $key is valid Crypt_RSA key, else returns false
  285. *
  286. * @param object $key Crypt_RSA_Key object for validating
  287. * @return bool true if $key is valid, else false
  288. * @access public
  289. */
  290. function isValid($key)
  291. {
  292. return (is_object($key) && strtolower(get_class($key)) === strtolower(__CLASS__));
  293. }
  294. }
  295. ?>