.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
PKCS1.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * PKCS#1 Formatted DSA Key Handler |
| 5 | * |
| 6 | * PHP version 5 |
| 7 | * |
| 8 | * Used by File/X509.php |
| 9 | * |
| 10 | * Processes keys with the following headers: |
| 11 | * |
| 12 | * -----BEGIN DSA PRIVATE KEY----- |
| 13 | * -----BEGIN DSA PUBLIC KEY----- |
| 14 | * -----BEGIN DSA PARAMETERS----- |
| 15 | * |
| 16 | * Analogous to ssh-keygen's pem format (as specified by -m) |
| 17 | * |
| 18 | * Also, technically, PKCS1 decribes RSA but I am not aware of a formal specification for DSA. |
| 19 | * The DSA private key format seems to have been adapted from the RSA private key format so |
| 20 | * we're just re-using that as the name. |
| 21 | * |
| 22 | * @author Jim Wigginton <terrafrost@php.net> |
| 23 | * @copyright 2015 Jim Wigginton |
| 24 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 25 | * @link http://phpseclib.sourceforge.net |
| 26 | */ |
| 27 | |
| 28 | declare(strict_types=1); |
| 29 | |
| 30 | namespace phpseclib3\Crypt\DSA\Formats\Keys; |
| 31 | |
| 32 | use phpseclib3\Common\Functions\Strings; |
| 33 | use phpseclib3\Crypt\Common\Formats\Keys\PKCS1 as Progenitor; |
| 34 | use phpseclib3\Exception\RuntimeException; |
| 35 | use phpseclib3\File\ASN1; |
| 36 | use phpseclib3\File\ASN1\Maps; |
| 37 | use phpseclib3\Math\BigInteger; |
| 38 | |
| 39 | /** |
| 40 | * PKCS#1 Formatted DSA Key Handler |
| 41 | * |
| 42 | * @author Jim Wigginton <terrafrost@php.net> |
| 43 | */ |
| 44 | abstract class PKCS1 extends Progenitor |
| 45 | { |
| 46 | /** |
| 47 | * Break a public or private key down into its constituent components |
| 48 | * |
| 49 | * @param string|array $key |
| 50 | */ |
| 51 | public static function load($key, ?string $password = null): array |
| 52 | { |
| 53 | $key = parent::load($key, $password); |
| 54 | |
| 55 | $decoded = ASN1::decodeBER($key); |
| 56 | if (!$decoded) { |
| 57 | throw new RuntimeException('Unable to decode BER'); |
| 58 | } |
| 59 | |
| 60 | $key = ASN1::asn1map($decoded[0], Maps\DSAParams::MAP); |
| 61 | if (is_array($key)) { |
| 62 | return $key; |
| 63 | } |
| 64 | |
| 65 | $key = ASN1::asn1map($decoded[0], Maps\DSAPrivateKey::MAP); |
| 66 | if (is_array($key)) { |
| 67 | return $key; |
| 68 | } |
| 69 | |
| 70 | $key = ASN1::asn1map($decoded[0], Maps\DSAPublicKey::MAP); |
| 71 | if (is_array($key)) { |
| 72 | return $key; |
| 73 | } |
| 74 | |
| 75 | throw new RuntimeException('Unable to perform ASN1 mapping'); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Convert DSA parameters to the appropriate format |
| 80 | */ |
| 81 | public static function saveParameters(BigInteger $p, BigInteger $q, BigInteger $g): string |
| 82 | { |
| 83 | $key = [ |
| 84 | 'p' => $p, |
| 85 | 'q' => $q, |
| 86 | 'g' => $g, |
| 87 | ]; |
| 88 | |
| 89 | $key = ASN1::encodeDER($key, Maps\DSAParams::MAP); |
| 90 | |
| 91 | return "-----BEGIN DSA PARAMETERS-----\r\n" . |
| 92 | chunk_split(Strings::base64_encode($key), 64) . |
| 93 | "-----END DSA PARAMETERS-----\r\n"; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Convert a private key to the appropriate format. |
| 98 | * |
| 99 | * @param string $password optional |
| 100 | * @param array $options optional |
| 101 | */ |
| 102 | public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, string $password = '', array $options = []): string |
| 103 | { |
| 104 | $key = [ |
| 105 | 'version' => 0, |
| 106 | 'p' => $p, |
| 107 | 'q' => $q, |
| 108 | 'g' => $g, |
| 109 | 'y' => $y, |
| 110 | 'x' => $x, |
| 111 | ]; |
| 112 | |
| 113 | $key = ASN1::encodeDER($key, Maps\DSAPrivateKey::MAP); |
| 114 | |
| 115 | return self::wrapPrivateKey($key, 'DSA', $password, $options); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Convert a public key to the appropriate format |
| 120 | */ |
| 121 | public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y): string |
| 122 | { |
| 123 | $key = ASN1::encodeDER($y, Maps\DSAPublicKey::MAP); |
| 124 | |
| 125 | return self::wrapPublicKey($key, 'DSA'); |
| 126 | } |
| 127 | } |
| 128 |