1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2SecurityTxt\helpers\GPG; |
4
|
|
|
|
5
|
|
|
use Crypt_GPG; |
6
|
|
|
use Crypt_GPG_Exception; |
7
|
|
|
use PEAR_Exception; |
8
|
|
|
use rhertogh\Yii2SecurityTxt\helpers\GPG\enums\GPGDriver; |
9
|
|
|
use rhertogh\Yii2SecurityTxt\helpers\GPG\traits\CryptGPGTrait; |
10
|
|
|
use rhertogh\Yii2SecurityTxt\helpers\GPG\traits\GnupgExtensionTrait; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A helper class for the GNU Privacy Guard. |
15
|
|
|
* It can run with either the Crypt_GPG package or the "gnupg" extension. |
16
|
|
|
* |
17
|
|
|
* @link https://packagist.org/packages/pear/crypt_gpg |
18
|
|
|
* @link https://www.php.net/manual/en/book.gnupg.php |
19
|
|
|
*/ |
20
|
|
|
class GPGHelper |
21
|
|
|
{ |
22
|
|
|
use CryptGPGTrait; |
23
|
|
|
use GnupgExtensionTrait; |
24
|
|
|
|
25
|
|
|
/** @var GPGDriver|null Specifies the library to use. |
26
|
|
|
* If `null` it will auto-detect which library to use in the following order: CryptGPG, GnupgExtension |
27
|
|
|
*/ |
28
|
|
|
public static GPGDriver|null $driver = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Signs a message. |
32
|
|
|
* |
33
|
|
|
* @throws InvalidConfigException |
34
|
|
|
* @throws Crypt_GPG_Exception |
35
|
|
|
* @throws PEAR_Exception |
36
|
|
|
*/ |
37
|
2 |
|
public static function sign(string $message, string $privateKey): string |
38
|
|
|
{ |
39
|
2 |
|
return match (static::determineDriver()) { |
40
|
2 |
|
GPGDriver::CryptGPG => static::signViaCryptGPG($message, $privateKey), |
41
|
2 |
|
GPGDriver::GnupgExtension => static::signViaGnupgExtension($message, $privateKey), |
42
|
2 |
|
}; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Verifies a message and returns the content or `false` if the signature is invalid. |
47
|
|
|
* |
48
|
|
|
* @throws InvalidConfigException |
49
|
|
|
* @throws Crypt_GPG_Exception |
50
|
|
|
* @throws PEAR_Exception |
51
|
|
|
*/ |
52
|
4 |
|
public static function verify(string $message, string $publicKey): string|false |
53
|
|
|
{ |
54
|
4 |
|
return match (static::determineDriver()) { |
55
|
4 |
|
GPGDriver::CryptGPG => static::verifyViaCryptGPG($message, $publicKey), |
56
|
4 |
|
GPGDriver::GnupgExtension => static::verifyViaGnupgExtension($message, $publicKey), |
57
|
4 |
|
}; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws InvalidConfigException |
62
|
|
|
*/ |
63
|
4 |
|
protected static function determineDriver(): GPGDriver |
64
|
|
|
{ |
65
|
2 |
|
$driver = static::$driver; |
66
|
|
|
|
67
|
2 |
|
if ($driver === null) { |
68
|
1 |
|
if (class_exists(Crypt_GPG::class)) { |
69
|
1 |
|
$driver = GPGDriver::CryptGPG; |
70
|
|
|
} elseif (extension_loaded('gnupg')) { |
71
|
|
|
$driver = GPGDriver::GnupgExtension; |
72
|
|
|
} else { |
73
|
1 |
|
throw new InvalidConfigException('Either the Crypt_GPG package (https://packagist.org/packages/pear/crypt_gpg)' |
74
|
1 |
|
. ' or the "gnupg" extension (https://www.php.net/manual/en/book.gnupg.php) must be installed.'); |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
4 |
|
if ($driver === GPGDriver::CryptGPG) { |
78
|
3 |
|
if (!class_exists(Crypt_GPG::class)) { |
79
|
3 |
|
throw new InvalidConfigException('When using the Crypt_GPG package (https://packagist.org/packages/pear/crypt_gpg) it must be installed.'); |
80
|
|
|
} |
81
|
3 |
|
} elseif ($driver === GPGDriver::GnupgExtension) { |
82
|
3 |
|
if (!extension_loaded('gnupg')) { |
83
|
3 |
|
throw new InvalidConfigException('When using the "gnupg" extension (https://www.php.net/manual/en/book.gnupg.php) it must be installed.'); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
throw new \LogicException('Unknown GPGDriver "' . $driver->name . '".'); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return $driver; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|