.htaccess
1 year ago
PKCS1.php
1 year ago
PKCS8.php
1 year ago
index.html
1 year ago
web.config
1 year ago
PKCS8.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * PKCS#8 Formatted DH 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 | * @author Jim Wigginton <terrafrost@php.net> |
| 15 | * @copyright 2015 Jim Wigginton |
| 16 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 17 | * @link http://phpseclib.sourceforge.net |
| 18 | */ |
| 19 | |
| 20 | declare(strict_types=1); |
| 21 | |
| 22 | namespace phpseclib3\Crypt\DH\Formats\Keys; |
| 23 | |
| 24 | use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor; |
| 25 | use phpseclib3\Exception\RuntimeException; |
| 26 | use phpseclib3\File\ASN1; |
| 27 | use phpseclib3\File\ASN1\Maps; |
| 28 | use phpseclib3\Math\BigInteger; |
| 29 | |
| 30 | /** |
| 31 | * PKCS#8 Formatted DH Key Handler |
| 32 | * |
| 33 | * @author Jim Wigginton <terrafrost@php.net> |
| 34 | */ |
| 35 | abstract class PKCS8 extends Progenitor |
| 36 | { |
| 37 | /** |
| 38 | * OID Name |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public const OID_NAME = 'dhKeyAgreement'; |
| 43 | |
| 44 | /** |
| 45 | * OID Value |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | public const OID_VALUE = '1.2.840.113549.1.3.1'; |
| 50 | |
| 51 | /** |
| 52 | * Child OIDs loaded |
| 53 | * |
| 54 | * @var bool |
| 55 | */ |
| 56 | protected static $childOIDsLoaded = false; |
| 57 | |
| 58 | /** |
| 59 | * Break a public or private key down into its constituent components |
| 60 | * |
| 61 | * @param string|array $key |
| 62 | */ |
| 63 | public static function load($key, ?string $password = null): array |
| 64 | { |
| 65 | $key = parent::load($key, $password); |
| 66 | |
| 67 | $type = isset($key['privateKey']) ? 'privateKey' : 'publicKey'; |
| 68 | |
| 69 | $decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element); |
| 70 | if (empty($decoded)) { |
| 71 | throw new RuntimeException('Unable to decode BER of parameters'); |
| 72 | } |
| 73 | $components = ASN1::asn1map($decoded[0], Maps\DHParameter::MAP); |
| 74 | if (!is_array($components)) { |
| 75 | throw new RuntimeException('Unable to perform ASN1 mapping on parameters'); |
| 76 | } |
| 77 | |
| 78 | $decoded = ASN1::decodeBER($key[$type]); |
| 79 | switch (true) { |
| 80 | case !isset($decoded): |
| 81 | case !isset($decoded[0]['content']): |
| 82 | case !$decoded[0]['content'] instanceof BigInteger: |
| 83 | throw new RuntimeException('Unable to decode BER of parameters'); |
| 84 | } |
| 85 | $components[$type] = $decoded[0]['content']; |
| 86 | |
| 87 | return $components; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Convert a private key to the appropriate format. |
| 92 | */ |
| 93 | public static function savePrivateKey(BigInteger $prime, BigInteger $base, BigInteger $privateKey, BigInteger $publicKey, ?string $password = null, array $options = []): string |
| 94 | { |
| 95 | $params = [ |
| 96 | 'prime' => $prime, |
| 97 | 'base' => $base, |
| 98 | ]; |
| 99 | $params = ASN1::encodeDER($params, Maps\DHParameter::MAP); |
| 100 | $params = new ASN1\Element($params); |
| 101 | $key = ASN1::encodeDER($privateKey, ['type' => ASN1::TYPE_INTEGER]); |
| 102 | return self::wrapPrivateKey($key, [], $params, $password, null, '', $options); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Convert a public key to the appropriate format |
| 107 | * |
| 108 | * @param array $options optional |
| 109 | */ |
| 110 | public static function savePublicKey(BigInteger $prime, BigInteger $base, BigInteger $publicKey, array $options = []): string |
| 111 | { |
| 112 | $params = [ |
| 113 | 'prime' => $prime, |
| 114 | 'base' => $base, |
| 115 | ]; |
| 116 | $params = ASN1::encodeDER($params, Maps\DHParameter::MAP); |
| 117 | $params = new ASN1\Element($params); |
| 118 | $key = ASN1::encodeDER($publicKey, ['type' => ASN1::TYPE_INTEGER]); |
| 119 | return self::wrapPublicKey($key, $params); |
| 120 | } |
| 121 | } |
| 122 |