phpseclib/phpseclib/Crypt/PublicKeyLoader.php

108 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/**
* PublicKeyLoader
*
* Returns a PublicKey or PrivateKey object.
*
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2009 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
2022-06-04 15:31:21 +00:00
declare(strict_types=1);
namespace phpseclib3\Crypt;
2021-01-12 15:20:41 +00:00
use phpseclib3\Crypt\Common\AsymmetricKey;
use phpseclib3\Crypt\Common\PrivateKey;
2022-01-30 15:34:42 +00:00
use phpseclib3\Crypt\Common\PublicKey;
use phpseclib3\Exception\NoKeyLoadedException;
use phpseclib3\File\X509;
/**
* PublicKeyLoader
*
* @author Jim Wigginton <terrafrost@php.net>
*/
abstract class PublicKeyLoader
{
/**
* Loads a public or private key
*
2020-12-23 09:37:05 +00:00
* @param string|array $key
*/
2022-07-09 02:42:28 +00:00
public static function load($key, ?string $password = null): AsymmetricKey
{
try {
return EC::load($key, $password);
2022-02-17 02:25:59 +00:00
} catch (NoKeyLoadedException $e) {
}
try {
return RSA::load($key, $password);
2022-02-17 02:25:59 +00:00
} catch (NoKeyLoadedException $e) {
}
try {
return DSA::load($key, $password);
2022-02-17 02:25:59 +00:00
} catch (NoKeyLoadedException $e) {
}
try {
$x509 = new X509();
$x509->loadX509($key);
$key = $x509->getPublicKey();
if ($key) {
return $key;
}
2022-02-17 02:25:59 +00:00
} catch (\Exception $e) {
}
throw new NoKeyLoadedException('Unable to read key');
}
/**
* Loads a private key
*
* @param string|array $key
*/
2022-07-09 02:42:28 +00:00
public static function loadPrivateKey($key, ?string $password = null): PrivateKey
{
$key = self::load($key, $password);
if (!$key instanceof PrivateKey) {
throw new NoKeyLoadedException('The key that was loaded was not a private key');
}
return $key;
}
/**
* Loads a public key
*
* @param string|array $key
*/
2022-06-04 15:31:21 +00:00
public static function loadPublicKey($key): PublicKey
{
$key = self::load($key);
if (!$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a public key');
}
return $key;
}
/**
* Loads parameters
*
* @param string|array $key
*/
2022-06-04 15:31:21 +00:00
public static function loadParameters($key): AsymmetricKey
{
$key = self::load($key);
if (!$key instanceof PrivateKey && !$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a parameter');
}
return $key;
}
}