PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/elgg/mod/openid_server/Crypt/RSA/Key.php

https://bitbucket.org/rhizomatik/lorea_production/
PHP | 314 lines | 78 code | 21 blank | 215 comment | 5 complexity | ac760134b1a8129680453917de5003d2 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  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 Alexander Valyalkin
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version CVS: $Id: Key.php,v 1.6 2009/01/05 08:30:29 clockwerx Exp $
  22. * @link http://pear.php.net/package/Crypt_RSA
  23. */
  24. /**
  25. * RSA error handling facilities
  26. */
  27. require_once 'Crypt/RSA/ErrorHandler.php';
  28. /**
  29. * loader for RSA math wrappers
  30. */
  31. require_once 'Crypt/RSA/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 Alexander Valyalkin
  87. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  88. * @version Release: @package_version@
  89. * @link http://pear.php.net/package/Crypt_RSA
  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. * You should pass in the name of math wrapper, which will be used to
  134. * perform different operations with big integers.
  135. * See contents of Crypt/RSA/Math folder for examples of wrappers.
  136. * Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
  137. *
  138. * @param string $modulus key modulus
  139. * @param string $exp key exponent
  140. * @param string $key_type type of the key (public or private)
  141. * @param string $wrapper_name wrapper to use
  142. * @param string $error_handler name of error handler function
  143. *
  144. * @access public
  145. */
  146. function Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name = 'default', $error_handler = '')
  147. {
  148. // set error handler
  149. $this->setErrorHandler($error_handler);
  150. // try to load math wrapper $wrapper_name
  151. $obj = &Crypt_RSA_MathLoader::loadWrapper($wrapper_name);
  152. if ($this->isError($obj)) {
  153. // error during loading of math wrapper
  154. $this->pushError($obj); // push error object into error list
  155. return;
  156. }
  157. $this->_math_obj = &$obj;
  158. $this->_modulus = $modulus;
  159. $this->_exp = $exp;
  160. if (!in_array($key_type, array('private', 'public'))) {
  161. $this->pushError('invalid key type. It must be private or public', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
  162. return;
  163. }
  164. $this->_key_type = $key_type;
  165. /* check length of modulus & exponent ( abs(modulus) > abs(exp) ) */
  166. $mod_num = $this->_math_obj->bin2int($this->_modulus);
  167. $exp_num = $this->_math_obj->bin2int($this->_exp);
  168. if ($this->_math_obj->cmpAbs($mod_num, $exp_num) <= 0) {
  169. $this->pushError('modulus must be greater than exponent', CRYPT_RSA_ERROR_EXP_GE_MOD);
  170. return;
  171. }
  172. // determine key length
  173. $this->_key_len = $this->_math_obj->bitLen($mod_num);
  174. }
  175. /**
  176. * Crypt_RSA_Key factory.
  177. *
  178. * @param string $modulus key modulus
  179. * @param string $exp key exponent
  180. * @param string $key_type type of the key (public or private)
  181. * @param string $wrapper_name wrapper to use
  182. * @param string $error_handler name of error handler function
  183. *
  184. * @return object new Crypt_RSA_Key object on success or PEAR_Error object on failure
  185. * @access public
  186. */
  187. function factory($modulus, $exp, $key_type, $wrapper_name = 'default', $error_handler = '')
  188. {
  189. $obj = new Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name, $error_handler);
  190. if ($obj->isError()) {
  191. // error during creating a new object. Retrurn PEAR_Error object
  192. return $obj->getLastError();
  193. }
  194. // object created successfully. Return it
  195. return $obj;
  196. }
  197. /**
  198. * Calculates bit length of the key
  199. *
  200. * @return int bit length of key
  201. * @access public
  202. */
  203. function getKeyLength()
  204. {
  205. return $this->_key_len;
  206. }
  207. /**
  208. * Returns modulus part of the key as binary string,
  209. * which can be used to construct new Crypt_RSA_Key object.
  210. *
  211. * @return string modulus as binary string
  212. * @access public
  213. */
  214. function getModulus()
  215. {
  216. return $this->_modulus;
  217. }
  218. /**
  219. * Returns exponent part of the key as binary string,
  220. * which can be used to construct new Crypt_RSA_Key object.
  221. *
  222. * @return string exponent as binary string
  223. * @access public
  224. */
  225. function getExponent()
  226. {
  227. return $this->_exp;
  228. }
  229. /**
  230. * Returns key type (public, private)
  231. *
  232. * @return string key type (public, private)
  233. * @access public
  234. */
  235. function getKeyType()
  236. {
  237. return $this->_key_type;
  238. }
  239. /**
  240. * Returns string representation of key
  241. *
  242. * @return string key, serialized to string
  243. * @access public
  244. */
  245. function toString()
  246. {
  247. return base64_encode(
  248. serialize(
  249. array($this->_modulus, $this->_exp, $this->_key_type)
  250. )
  251. );
  252. }
  253. /**
  254. * Returns Crypt_RSA_Key object, unserialized from
  255. * string representation of key.
  256. *
  257. * optional parameter $wrapper_name - is the name of math wrapper,
  258. * which will be used during unserialization of this object.
  259. *
  260. * This function can be called statically:
  261. * $key = Crypt_RSA_Key::fromString($key_in_string, 'BigInt');
  262. *
  263. * @param string $key_str RSA key, serialized into string
  264. * @param string $wrapper_name optional math wrapper name
  265. *
  266. * @return object key as Crypt_RSA_Key object
  267. * @access public
  268. * @static
  269. */
  270. function fromString($key_str, $wrapper_name = 'default')
  271. {
  272. list($modulus, $exponent, $key_type) = unserialize(base64_decode($key_str));
  273. $obj = new Crypt_RSA_Key($modulus, $exponent, $key_type, $wrapper_name);
  274. return $obj;
  275. }
  276. /**
  277. * Validates key
  278. * This function can be called statically:
  279. * $is_valid = Crypt_RSA_Key::isValid($key)
  280. *
  281. * Returns true, if $key is valid Crypt_RSA key, else returns false
  282. *
  283. * @param object $key Crypt_RSA_Key object for validating
  284. *
  285. * @return bool true if $key is valid, else false
  286. * @access public
  287. */
  288. function isValid($key)
  289. {
  290. return (is_object($key) && strtolower(get_class($key)) === strtolower(__CLASS__));
  291. }
  292. }
  293. ?>