Subversion Repositories oidplus

Rev

Rev 846 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /**
  4.  * PublicKeyLoader
  5.  *
  6.  * Returns a PublicKey or PrivateKey object.
  7.  *
  8.  * @category  Crypt
  9.  * @package   PublicKeyLoader
  10.  * @author    Jim Wigginton <terrafrost@php.net>
  11.  * @copyright 2009 Jim Wigginton
  12.  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  13.  * @link      http://phpseclib.sourceforge.net
  14.  */
  15.  
  16. namespace phpseclib3\Crypt;
  17.  
  18. use phpseclib3\Crypt\Common\AsymmetricKey;
  19. use phpseclib3\Crypt\Common\PrivateKey;
  20. use phpseclib3\Crypt\Common\PublicKey;
  21. use phpseclib3\Exception\NoKeyLoadedException;
  22. use phpseclib3\File\X509;
  23.  
  24. /**
  25.  * PublicKeyLoader
  26.  *
  27.  * @package Common
  28.  * @author  Jim Wigginton <terrafrost@php.net>
  29.  * @access  public
  30.  */
  31. abstract class PublicKeyLoader
  32. {
  33.     /**
  34.      * Loads a public or private key
  35.      *
  36.      * @return AsymmetricKey
  37.      * @access public
  38.      * @param string|array $key
  39.      * @param string $password optional
  40.      */
  41.     public static function load($key, $password = false)
  42.     {
  43.         try {
  44.             return EC::load($key, $password);
  45.         } catch (NoKeyLoadedException $e) {
  46.         }
  47.  
  48.         try {
  49.             return RSA::load($key, $password);
  50.         } catch (NoKeyLoadedException $e) {
  51.         }
  52.  
  53.         try {
  54.             return DSA::load($key, $password);
  55.         } catch (NoKeyLoadedException $e) {
  56.         }
  57.  
  58.         try {
  59.             $x509 = new X509();
  60.             $x509->loadX509($key);
  61.             $key = $x509->getPublicKey();
  62.             if ($key) {
  63.                 return $key;
  64.             }
  65.         } catch (\Exception $e) {
  66.         }
  67.  
  68.         throw new NoKeyLoadedException('Unable to read key');
  69.     }
  70.  
  71.     /**
  72.      * Loads a private key
  73.      *
  74.      * @return PrivateKey
  75.      * @access public
  76.      * @param string|array $key
  77.      * @param string $password optional
  78.      */
  79.     public static function loadPrivateKey($key, $password = false)
  80.     {
  81.         $key = self::load($key, $password);
  82.         if (!$key instanceof PrivateKey) {
  83.             throw new NoKeyLoadedException('The key that was loaded was not a private key');
  84.         }
  85.         return $key;
  86.     }
  87.  
  88.     /**
  89.      * Loads a public key
  90.      *
  91.      * @return PublicKey
  92.      * @access public
  93.      * @param string|array $key
  94.      */
  95.     public static function loadPublicKey($key)
  96.     {
  97.         $key = self::load($key);
  98.         if (!$key instanceof PublicKey) {
  99.             throw new NoKeyLoadedException('The key that was loaded was not a public key');
  100.         }
  101.         return $key;
  102.     }
  103.  
  104.     /**
  105.      * Loads parameters
  106.      *
  107.      * @return AsymmetricKey
  108.      * @access public
  109.      * @param string|array $key
  110.      */
  111.     public static function loadParameters($key)
  112.     {
  113.         $key = self::load($key);
  114.         if (!$key instanceof PrivateKey && !$key instanceof PublicKey) {
  115.             throw new NoKeyLoadedException('The key that was loaded was not a parameter');
  116.         }
  117.         return $key;
  118.     }
  119. }
  120.