.htaccess
1 year ago
OpenSSH.php
1 year ago
PKCS1.php
1 year ago
PKCS8.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
134 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * PKCS#8 Formatted DSA Key Handler |
| 5 | * |
| 6 | * PHP version 5 |
| 7 | * |
| 8 | * Processes keys with the following headers: |
| 9 | * |
| 10 | * -----BEGIN ENCRYPTED PRIVATE KEY----- |
| 11 | * -----BEGIN PRIVATE KEY----- |
| 12 | * -----BEGIN PUBLIC KEY----- |
| 13 | * |
| 14 | * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8 |
| 15 | * is specific to private keys it's basically creating a DER-encoded wrapper |
| 16 | * for keys. This just extends that same concept to public keys (much like ssh-keygen) |
| 17 | * |
| 18 | * @author Jim Wigginton <terrafrost@php.net> |
| 19 | * @copyright 2015 Jim Wigginton |
| 20 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 21 | * @link http://phpseclib.sourceforge.net |
| 22 | */ |
| 23 | |
| 24 | declare(strict_types=1); |
| 25 | |
| 26 | namespace phpseclib3\Crypt\DSA\Formats\Keys; |
| 27 | |
| 28 | use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor; |
| 29 | use phpseclib3\Exception\RuntimeException; |
| 30 | use phpseclib3\File\ASN1; |
| 31 | use phpseclib3\File\ASN1\Maps; |
| 32 | use phpseclib3\Math\BigInteger; |
| 33 | |
| 34 | /** |
| 35 | * PKCS#8 Formatted DSA 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 = 'id-dsa'; |
| 47 | |
| 48 | /** |
| 49 | * OID Value |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | public const OID_VALUE = '1.2.840.10040.4.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 | $type = isset($key['privateKey']) ? 'privateKey' : 'publicKey'; |
| 72 | |
| 73 | $decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element); |
| 74 | if (!$decoded) { |
| 75 | throw new RuntimeException('Unable to decode BER of parameters'); |
| 76 | } |
| 77 | $components = ASN1::asn1map($decoded[0], Maps\DSAParams::MAP); |
| 78 | if (!is_array($components)) { |
| 79 | throw new RuntimeException('Unable to perform ASN1 mapping on parameters'); |
| 80 | } |
| 81 | |
| 82 | $decoded = ASN1::decodeBER($key[$type]); |
| 83 | if (empty($decoded)) { |
| 84 | throw new RuntimeException('Unable to decode BER'); |
| 85 | } |
| 86 | |
| 87 | $var = $type == 'privateKey' ? 'x' : 'y'; |
| 88 | $components[$var] = ASN1::asn1map($decoded[0], Maps\DSAPublicKey::MAP); |
| 89 | if (!$components[$var] instanceof BigInteger) { |
| 90 | throw new RuntimeException('Unable to perform ASN1 mapping'); |
| 91 | } |
| 92 | |
| 93 | if (isset($key['meta'])) { |
| 94 | $components['meta'] = $key['meta']; |
| 95 | } |
| 96 | |
| 97 | return $components; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Convert a private key to the appropriate format. |
| 102 | */ |
| 103 | public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, ?string $password = null, array $options = []): string |
| 104 | { |
| 105 | $params = [ |
| 106 | 'p' => $p, |
| 107 | 'q' => $q, |
| 108 | 'g' => $g, |
| 109 | ]; |
| 110 | $params = ASN1::encodeDER($params, Maps\DSAParams::MAP); |
| 111 | $params = new ASN1\Element($params); |
| 112 | $key = ASN1::encodeDER($x, Maps\DSAPublicKey::MAP); |
| 113 | return self::wrapPrivateKey($key, [], $params, $password, null, '', $options); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Convert a public key to the appropriate format |
| 118 | * |
| 119 | * @param array $options optional |
| 120 | */ |
| 121 | public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, array $options = []): string |
| 122 | { |
| 123 | $params = [ |
| 124 | 'p' => $p, |
| 125 | 'q' => $q, |
| 126 | 'g' => $g, |
| 127 | ]; |
| 128 | $params = ASN1::encodeDER($params, Maps\DSAParams::MAP); |
| 129 | $params = new ASN1\Element($params); |
| 130 | $key = ASN1::encodeDER($y, Maps\DSAPublicKey::MAP); |
| 131 | return self::wrapPublicKey($key, $params); |
| 132 | } |
| 133 | } |
| 134 |