PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.13.4
JetBackup – Backup, Restore & Migrate v3.1.13.4
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / RSA / Formats / Keys / OpenSSH.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / RSA / Formats / Keys Last commit date
.htaccess 9 months ago JWK.php 9 months ago MSBLOB.php 9 months ago OpenSSH.php 9 months ago PKCS1.php 9 months ago PKCS8.php 9 months ago PSS.php 9 months ago PuTTY.php 9 months ago Raw.php 9 months ago XML.php 9 months ago index.html 9 months ago web.config 9 months ago
OpenSSH.php
121 lines
1 <?php
2
3 /**
4 * OpenSSH Formatted RSA Key Handler
5 *
6 * PHP version 5
7 *
8 * Place in $HOME/.ssh/authorized_keys
9 *
10 * @author Jim Wigginton <terrafrost@php.net>
11 * @copyright 2015 Jim Wigginton
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @link http://phpseclib.sourceforge.net
14 */
15
16 declare(strict_types=1);
17
18 namespace phpseclib3\Crypt\RSA\Formats\Keys;
19
20 use phpseclib3\Common\Functions\Strings;
21 use phpseclib3\Crypt\Common\Formats\Keys\OpenSSH as Progenitor;
22 use phpseclib3\Exception\RuntimeException;
23 use phpseclib3\Math\BigInteger;
24
25 /**
26 * OpenSSH Formatted RSA Key Handler
27 *
28 * @author Jim Wigginton <terrafrost@php.net>
29 */
30 abstract class OpenSSH extends Progenitor
31 {
32 /**
33 * Supported Key Types
34 *
35 * @var array
36 */
37 protected static $types = ['ssh-rsa'];
38
39 /**
40 * Break a public or private key down into its constituent components
41 *
42 * @param string|array $key
43 */
44 public static function load($key, ?string $password = null): array
45 {
46 static $one;
47 if (!isset($one)) {
48 $one = new BigInteger(1);
49 }
50
51 $parsed = parent::load($key, $password);
52
53 if (isset($parsed['paddedKey'])) {
54 [$type] = Strings::unpackSSH2('s', $parsed['paddedKey']);
55 if ($type != $parsed['type']) {
56 throw new RuntimeException("The public and private keys are not of the same type ($type vs $parsed[type])");
57 }
58
59 $primes = $coefficients = [];
60
61 [
62 $modulus,
63 $publicExponent,
64 $privateExponent,
65 $coefficients[2],
66 $primes[1],
67 $primes[2],
68 $comment,
69 ] = Strings::unpackSSH2('i6s', $parsed['paddedKey']);
70
71 $temp = $primes[1]->subtract($one);
72 $exponents = [1 => $publicExponent->modInverse($temp)];
73 $temp = $primes[2]->subtract($one);
74 $exponents[] = $publicExponent->modInverse($temp);
75
76 $isPublicKey = false;
77
78 return compact('publicExponent', 'modulus', 'privateExponent', 'primes', 'coefficients', 'exponents', 'comment', 'isPublicKey');
79 }
80
81 [$publicExponent, $modulus] = Strings::unpackSSH2('ii', $parsed['publicKey']);
82
83 return [
84 'isPublicKey' => true,
85 'modulus' => $modulus,
86 'publicExponent' => $publicExponent,
87 'comment' => $parsed['comment'],
88 ];
89 }
90
91 /**
92 * Convert a public key to the appropriate format
93 *
94 * @param array $options optional
95 */
96 public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = []): string
97 {
98 $RSAPublicKey = Strings::packSSH2('sii', 'ssh-rsa', $e, $n);
99
100 if ($options['binary'] ?? self::$binary) {
101 return $RSAPublicKey;
102 }
103
104 $comment = $options['comment'] ?? self::$comment;
105 $RSAPublicKey = 'ssh-rsa ' . base64_encode($RSAPublicKey) . ' ' . $comment;
106
107 return $RSAPublicKey;
108 }
109
110 /**
111 * Convert a private key to the appropriate format.
112 */
113 public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string
114 {
115 $publicKey = self::savePublicKey($n, $e, ['binary' => true]);
116 $privateKey = Strings::packSSH2('si6', 'ssh-rsa', $n, $e, $d, $coefficients[2], $primes[1], $primes[2]);
117
118 return self::wrapPrivateKey($publicKey, $privateKey, $password, $options);
119 }
120 }
121