1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace montross50\SSHPubKeyValidator; |
4
|
|
|
|
5
|
|
|
use phpseclib\Crypt\RSA; |
6
|
|
|
|
7
|
|
|
class SSHPubKeyValidator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var $crypto RSA |
11
|
|
|
*/ |
12
|
|
|
private $crypto; |
13
|
|
|
private $strict; |
14
|
|
|
const RSA_MIN_LENGTH_STRICT = 1024; |
15
|
|
|
const RSA_MAX_LENGTH_STRICT = 16384; |
16
|
|
|
const RSA_MIN_LENGTH_LOOSE = 768; |
17
|
|
|
const RSA_MAX_LENGTH_LOOSE = 16384; |
18
|
|
|
|
19
|
36 |
|
public function __construct($crypto = null, $strict = true) |
20
|
|
|
{ |
21
|
36 |
|
if ($crypto === null) { |
22
|
33 |
|
$crypto = new RSA(); |
23
|
11 |
|
} |
24
|
36 |
|
$this->crypto = $crypto; |
25
|
36 |
|
$this->strict = $strict; |
26
|
36 |
|
} |
27
|
|
|
|
28
|
33 |
|
public function validateKey($pubkey) |
29
|
|
|
{ |
30
|
|
|
//first we check basic validity |
31
|
33 |
|
$key_parts = explode(' ', $pubkey, 3); |
32
|
33 |
|
if (count($key_parts) < 2) { |
33
|
3 |
|
return false; |
34
|
|
|
} |
35
|
30 |
|
$algorithm = $key_parts[0]; |
36
|
30 |
|
$key = $key_parts[1]; |
37
|
30 |
|
if (!in_array($algorithm, array('ssh-rsa'))) { |
38
|
3 |
|
return false; |
39
|
|
|
} |
40
|
27 |
|
$key_base64_decoded = base64_decode($key, true); |
41
|
27 |
|
if ($key_base64_decoded === false) { |
42
|
3 |
|
return false; |
43
|
|
|
} |
44
|
24 |
|
$check = base64_decode(substr($key, 0, 16)); |
45
|
24 |
|
$check = preg_replace("/[^\w\-]/", "", $check); |
46
|
24 |
|
if ((string) $check !== (string) $algorithm) { |
47
|
3 |
|
return false; |
48
|
|
|
} |
49
|
|
|
//now we check if the key is truly valid |
50
|
21 |
|
$this->crypto->loadKey($pubkey, RSA::PUBLIC_FORMAT_OPENSSH); |
51
|
21 |
|
$pkcs8key = $this->crypto->getPublicKey(RSA::PUBLIC_FORMAT_PKCS8); |
52
|
|
|
|
53
|
21 |
|
$opensslPubKey = openssl_pkey_get_public($pkcs8key); |
54
|
21 |
|
if ($opensslPubKey === false) { |
55
|
3 |
|
return false; |
56
|
|
|
} |
57
|
18 |
|
$keyData = openssl_pkey_get_details($opensslPubKey); |
58
|
18 |
|
if ($keyData === false) { |
59
|
3 |
|
return false; |
60
|
|
|
} |
61
|
15 |
|
if ($this->strict) { |
62
|
12 |
|
$minLength = self::RSA_MIN_LENGTH_STRICT; |
63
|
12 |
|
$maxLength = self::RSA_MAX_LENGTH_STRICT; |
64
|
4 |
|
} else { |
65
|
3 |
|
$minLength = self::RSA_MIN_LENGTH_LOOSE; |
66
|
3 |
|
$maxLength = self::RSA_MAX_LENGTH_LOOSE; |
67
|
|
|
} |
68
|
15 |
|
if (isset($keyData['bits'])) { |
69
|
12 |
|
if ($keyData['bits'] < $minLength) { |
70
|
3 |
|
return false; |
71
|
|
|
} |
72
|
9 |
|
if ($keyData['bits'] > $maxLength) { |
73
|
7 |
|
return false; |
74
|
|
|
} |
75
|
2 |
|
} else { |
76
|
3 |
|
return false; |
77
|
|
|
} |
78
|
|
|
//everything is valid it seems |
79
|
6 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
public function getFingerprint($pubkey, $algo = 'md5') |
83
|
|
|
{ |
84
|
3 |
|
$this->crypto->loadKey($pubkey, RSA::PUBLIC_FORMAT_OPENSSH); |
85
|
3 |
|
return $this->crypto->getPublicKeyFingerprint($algo); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|