1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2SecurityTxt\helpers\GPG\traits; |
4
|
|
|
|
5
|
|
|
use gnupg; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\base\InvalidConfigException; |
8
|
|
|
use yii\helpers\FileHelper; |
9
|
|
|
|
10
|
|
|
trait GnupgExtensionTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @throws InvalidConfigException |
14
|
|
|
*/ |
15
|
2 |
|
protected static function signViaGnupgExtension($message, $privateKey) |
16
|
|
|
{ |
17
|
2 |
|
Yii::beginProfile('Generate PGP signature', __METHOD__); |
18
|
2 |
|
$gpg = static::getGnupg($gnupgHome); |
19
|
|
|
try { |
20
|
2 |
|
$gpg->seterrormode(GNUPG_ERROR_EXCEPTION); |
21
|
|
|
|
22
|
2 |
|
$keyInfo = $gpg->import($privateKey); |
23
|
2 |
|
if ($keyInfo === false) { |
24
|
|
|
throw new InvalidConfigException('Unable to import private key. Debug info: ' |
25
|
|
|
. var_export(static::generateGnupgDebugInfo($gpg), true)); |
26
|
|
|
} |
27
|
2 |
|
$gpg->addsignkey($keyInfo['fingerprint']); |
28
|
|
|
|
29
|
2 |
|
$gpg->setsignmode(gnupg::SIG_MODE_CLEAR); |
|
|
|
|
30
|
2 |
|
$output = $gpg->sign($message); |
31
|
|
|
} finally { |
32
|
2 |
|
FileHelper::removeDirectory($gnupgHome); |
33
|
|
|
} |
34
|
2 |
|
if ($output === false) { |
35
|
|
|
throw new InvalidConfigException('Unable to sign the message. Debug info: ' |
36
|
|
|
. var_export(static::generateGnupgDebugInfo($gpg), true)); |
37
|
|
|
} |
38
|
2 |
|
Yii::endProfile('Generate PGP signature', __METHOD__); |
39
|
2 |
|
return $output; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws InvalidConfigException |
44
|
|
|
*/ |
45
|
2 |
|
protected static function verifyViaGnupgExtension($message, $publicKey) |
46
|
|
|
{ |
47
|
2 |
|
Yii::beginProfile('Verify PGP signature', __METHOD__); |
48
|
2 |
|
$gpg = static::getGnupg($gnupgHome); |
49
|
|
|
try { |
50
|
2 |
|
$gpg->seterrormode(GNUPG_ERROR_EXCEPTION); |
51
|
|
|
|
52
|
2 |
|
$keyInfo = $gpg->import($publicKey); |
53
|
2 |
|
if ($keyInfo === false) { |
54
|
|
|
throw new InvalidConfigException('Unable to import public key. Debug info: ' |
55
|
|
|
. var_export(static::generateGnupgDebugInfo($gpg), true)); |
56
|
|
|
} |
57
|
2 |
|
$gpg->addencryptkey($keyInfo['fingerprint']); |
58
|
2 |
|
$info = $gpg->verify($message,false,$plaintext); |
|
|
|
|
59
|
|
|
} finally { |
60
|
2 |
|
FileHelper::removeDirectory($gnupgHome); |
61
|
|
|
} |
62
|
2 |
|
if ($info === false) { |
63
|
|
|
throw new InvalidConfigException('Unable to verify the message. Debug info: ' |
64
|
|
|
. var_export(static::generateGnupgDebugInfo($gpg), true)); |
65
|
|
|
} |
66
|
2 |
|
if ($info[0]['summary'] !== 0) { |
67
|
|
|
// Invalid signature |
|
|
|
|
68
|
2 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
Yii::endProfile('Verify PGP signature', __METHOD__); |
72
|
2 |
|
return $plaintext; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected static function generateGnupgDebugInfo(gnupg $gpg) |
76
|
|
|
{ |
77
|
|
|
$debugInfo = [ |
78
|
|
|
'engineInfo' => $gpg->getengineinfo(), |
|
|
|
|
79
|
|
|
'errorInfo' => $gpg->geterrorinfo(), |
|
|
|
|
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
if (!is_writable($debugInfo['engineInfo']['home_dir'])) { |
83
|
|
|
$debugInfo['hint'] = 'The gnupg home directory (' . $debugInfo['engineInfo']['home_dir'] . ') is not writable.'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $debugInfo; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
protected static function getGnupg(&$gnupgHome = null): gnupg |
90
|
|
|
{ |
91
|
3 |
|
if (empty($gnupgHome)) { |
92
|
3 |
|
$gnupgHome = Yii::getAlias('@runtime') . '/gnupg/' . uniqid(more_entropy: true); |
|
|
|
|
93
|
3 |
|
FileHelper::createDirectory($gnupgHome); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
return new gnupg(['home_dir' => $gnupgHome]); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|