.htaccess
1 year ago
JWK.php
1 year ago
MSBLOB.php
1 year ago
OpenSSH.php
1 year ago
PKCS1.php
1 year ago
PKCS8.php
1 year ago
PSS.php
1 year ago
PuTTY.php
1 year ago
Raw.php
1 year ago
XML.php
1 year ago
index.html
1 year ago
web.config
1 year ago
PKCS8.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * PKCS#8 Formatted RSA Key Handler |
| 5 | * |
| 6 | * PHP version 5 |
| 7 | * |
| 8 | * Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set) |
| 9 | * |
| 10 | * Processes keys with the following headers: |
| 11 | * |
| 12 | * -----BEGIN ENCRYPTED PRIVATE KEY----- |
| 13 | * -----BEGIN PRIVATE KEY----- |
| 14 | * -----BEGIN PUBLIC KEY----- |
| 15 | * |
| 16 | * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8 |
| 17 | * is specific to private keys it's basically creating a DER-encoded wrapper |
| 18 | * for keys. This just extends that same concept to public keys (much like ssh-keygen) |
| 19 | * |
| 20 | * @author Jim Wigginton <terrafrost@php.net> |
| 21 | * @copyright 2015 Jim Wigginton |
| 22 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 23 | * @link http://phpseclib.sourceforge.net |
| 24 | */ |
| 25 | |
| 26 | declare(strict_types=1); |
| 27 | |
| 28 | namespace phpseclib3\Crypt\RSA\Formats\Keys; |
| 29 | |
| 30 | use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor; |
| 31 | use phpseclib3\File\ASN1; |
| 32 | use phpseclib3\Math\BigInteger; |
| 33 | |
| 34 | /** |
| 35 | * PKCS#8 Formatted RSA Key Handler |
| 36 | * |
| 37 | * @author Jim Wigginton <terrafrost@php.net> |
| 38 | */ |
| 39 | abstract class PKCS8 extends Progenitor |
| 40 | { |
| 41 | /** |
| 42 | * OID Name |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public const OID_NAME = 'rsaEncryption'; |
| 47 | |
| 48 | /** |
| 49 | * OID Value |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | public const OID_VALUE = '1.2.840.113549.1.1.1'; |
| 54 | |
| 55 | /** |
| 56 | * Child OIDs loaded |
| 57 | * |
| 58 | * @var bool |
| 59 | */ |
| 60 | protected static $childOIDsLoaded = false; |
| 61 | |
| 62 | /** |
| 63 | * Break a public or private key down into its constituent components |
| 64 | * |
| 65 | * @param string|array $key |
| 66 | */ |
| 67 | public static function load($key, ?string $password = null): array |
| 68 | { |
| 69 | $key = parent::load($key, $password); |
| 70 | |
| 71 | if (isset($key['privateKey'])) { |
| 72 | $components['isPublicKey'] = false; |
| 73 | $type = 'private'; |
| 74 | } else { |
| 75 | $components['isPublicKey'] = true; |
| 76 | $type = 'public'; |
| 77 | } |
| 78 | |
| 79 | $result = $components + PKCS1::load($key[$type . 'Key']); |
| 80 | |
| 81 | if (isset($key['meta'])) { |
| 82 | $result['meta'] = $key['meta']; |
| 83 | } |
| 84 | |
| 85 | return $result; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Convert a private key to the appropriate format. |
| 90 | */ |
| 91 | public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string |
| 92 | { |
| 93 | $key = PKCS1::savePrivateKey($n, $e, $d, $primes, $exponents, $coefficients); |
| 94 | $key = ASN1::extractBER($key); |
| 95 | return self::wrapPrivateKey($key, [], null, $password, null, '', $options); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Convert a public key to the appropriate format |
| 100 | * |
| 101 | * @param array $options optional |
| 102 | */ |
| 103 | public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = []): string |
| 104 | { |
| 105 | $key = PKCS1::savePublicKey($n, $e); |
| 106 | $key = ASN1::extractBER($key); |
| 107 | return self::wrapPublicKey($key, null); |
| 108 | } |
| 109 | } |
| 110 |