Beispiel #1
0
 /**
  * Retrieves RSA keypair from PEM-encoded string, containing RSA private key.
  * Example of such string:
  * -----BEGIN RSA PRIVATE KEY-----
  * MCsCAQACBHtvbSECAwEAAQIEeYrk3QIDAOF3AgMAjCcCAmdnAgJMawIDALEk
  * -----END RSA PRIVATE KEY-----
  *
  * Wrapper: Name of math wrapper, which will be used to
  * perform different operations with big integers.
  * See contents of Crypt/RSA/Math folder for examples of wrappers.
  * Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
  *
  * @param string $str           PEM-encoded string
  * @param string $wrapper_name  Wrapper name
  * @param string $error_handler name of error handler function
  *
  * @return Crypt_RSA_KeyPair object on success, PEAR_Error object on error
  * @access public
  * @static
  */
 function &fromPEMString($str, $wrapper_name = 'default', $error_handler = '')
 {
     if (isset($this)) {
         if ($wrapper_name == 'default') {
             $wrapper_name = $this->_math_obj->getWrapperName();
         }
         if ($error_handler == '') {
             $error_handler = $this->_error_handler;
         }
     }
     $err_handler = new Crypt_RSA_ErrorHandler();
     $err_handler->setErrorHandler($error_handler);
     // search for base64-encoded private key
     if (!preg_match('/-----BEGIN RSA PRIVATE KEY-----([^-]+)-----END RSA PRIVATE KEY-----/', $str, $matches)) {
         $err_handler->pushError("can't find RSA private key in the string [{$str}]");
         return $err_handler->getLastError();
     }
     // parse private key. It is ASN.1-encoded
     $str = base64_decode($matches[1]);
     $pos = 0;
     $tmp = Crypt_RSA_KeyPair::_ASN1Parse($str, $pos, $err_handler);
     if ($err_handler->isError()) {
         return $err_handler->getLastError();
     }
     if ($tmp['tag'] != 0x10) {
         $errstr = sprintf("wrong ASN tag value: 0x%02x. Expected 0x10 (SEQUENCE)", $tmp['tag']);
         $err_handler->pushError($errstr);
         return $err_handler->getLastError();
     }
     // parse ASN.1 SEQUENCE for RSA private key
     $attr_names = Crypt_RSA_KeyPair::_get_attr_names();
     $n = sizeof($attr_names);
     $rsa_attrs = array();
     for ($i = 0; $i < $n; $i++) {
         $tmp = Crypt_RSA_KeyPair::_ASN1ParseInt($str, $pos, $err_handler);
         if ($err_handler->isError()) {
             return $err_handler->getLastError();
         }
         $attr = $attr_names[$i];
         $rsa_attrs[$attr] = $tmp;
     }
     // create Crypt_RSA_KeyPair object.
     $keypair = new Crypt_RSA_KeyPair($rsa_attrs, $wrapper_name, $error_handler);
     if ($keypair->isError()) {
         return $keypair->getLastError();
     }
     return $keypair;
 }
 /**
  * Crypt_RSA_KeyPair factory.
  *
  * @param int $key_len bit length of key pair, which will be generated in constructor
  * @param string $wrapper_name
  *        Name of math wrapper, which will be used to
  *        perform different operations with big integers.
  *        See contents of Crypt/RSA/Math folder for examples of wrappers.
  *        Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
  *
  * @return object   new Crypt_RSA_KeyPair object on success or PEAR_Error object on failure
  * @access public
  */
 function &factory($key_len, $wrapper_name = 'default')
 {
     $obj = new Crypt_RSA_KeyPair($key_len, $wrapper_name);
     if ($obj->isError()) {
         // error during creating a new object. Retrurn PEAR_Error object
         return $obj->getLastError();
     }
     // object created successfully. Return it
     return $obj;
 }