Fix GPG/PGP exec() return false handling

This commit is contained in:
the-djmaze 2024-04-28 14:49:48 +02:00
parent f9b5a37555
commit 685b2ea93a

View file

@ -83,28 +83,17 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
protected function listDecryptKeys(/*string|resource*/ $input, /*string|resource*/ $output = null)
{
$this->setInput($input);
$fclose = $this->setOutput($output);
$_ENV['PINENTRY_USER_DATA'] = '';
$result = $this->exec(['--list-packets']);
$fclose && \fclose($fclose);
return $output ? true : ($result ? $result['output'] : false);
return $this->execOutput(['--list-packets'], $output);
}
protected function _decrypt(/*string|resource*/ $input, /*string|resource*/ $output = null)
{
$this->setInput($input);
$fclose = $this->setOutput($output);
if ($this->pinentries) {
$_ENV['PINENTRY_USER_DATA'] = \json_encode(\array_map('strval', $this->pinentries));
}
$result = $this->exec(['--decrypt','--skip-verify']);
$fclose && \fclose($fclose);
return $output ? true : ($result ? $result['output'] : false);
return $this->execOutput(['--decrypt','--skip-verify'], $output);
}
/**
@ -169,8 +158,6 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$this->setInput($input);
$fclose = $this->setOutput($output);
$arguments = [
'--encrypt'
];
@ -182,11 +169,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$arguments[] = '--recipient ' . \escapeshellarg($fingerprint);
}
$result = $this->exec($arguments);
$fclose && \fclose($fclose);
return $output ? true : $result['output'];
return $this->execOutput($arguments, $output);
}
/**
@ -257,7 +240,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
'--armor',
\escapeshellarg($keys[0]['subkeys'][0]['fingerprint']),
]);
return $result['output'];
return $result ? $result['output'] : false;
}
/**
@ -315,6 +298,9 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$settings->algo(),
$settings->usage
]));
if (!$result) {
return false;
}
$fingerprint = '';
foreach ($result['status'] as $line) {
@ -364,7 +350,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$this->setInput($input);
$result = $this->exec($arguments);
if ($result) {
foreach ($result['status'] as $line) {
if (false !== \strpos($line, 'IMPORT_RES')) {
$line = \explode(' ', \explode('IMPORT_RES ', $line)[1]);
@ -381,7 +367,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
];
}
}
}
return false;
}
@ -433,7 +419,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
// $result['errors'][0] = 'gpg: error reading key: No public key'
// print_r($result);
return true;
return !!$result;
}
/**
@ -458,6 +444,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$result = $this->exec($arguments);
$keys = [];
if ($result) {
$key = null; // current key
$subKey = null; // current sub-key
@ -559,6 +546,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
if ($key) {
$keys[] = $key;
}
}
return $keys;
}
@ -615,8 +603,6 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$this->setInput($input);
$fclose = $this->setOutput($output);
$arguments = [];
switch ($this->signmode)
@ -647,11 +633,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$_ENV['PINENTRY_USER_DATA'] = \json_encode(\array_map('strval', $this->pinentries));
}
$result = $this->exec($arguments);
$fclose && \fclose($fclose);
return $output ? true : $result['output'];
return $this->execOutput($arguments, $output);
}
/**
@ -708,6 +690,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$result = $this->exec($arguments);
$signatures = [];
if ($result) {
foreach ($result['status'] as $line) {
$tokens = \explode(' ', $line);
switch ($tokens[0])
@ -748,6 +731,7 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
$signatures[$last]['valid'] = 0;
}
}
}
return $signatures;
}
@ -799,16 +783,26 @@ class PGP extends Base implements \SnappyMail\PGP\PGPInterface
// 'KEY_CONSIDERED' => [],
// 'NO_SECKEY' => [],
];
if ($result) {
foreach ($result['status'] as $line) {
$tokens = \explode(' ', $line);
if (isset($info[$tokens[0]])) {
$info[$tokens[0]][] = $tokens[1];
}
}
}
$this->_debug('END DETECT MESSAGE KEY IDs');
return $info['ENC_TO'];
}
protected function execOutput(array $arguments, /*string|resource*/ $output = null)
{
$fclose = $this->setOutput($output);
$result = $this->exec($arguments);
$fclose && \fclose($fclose);
return $output ? true : ($result ? $result['output'] : false);
}
private function exec(array $arguments, bool $throw = true) /*: array|false*/
{
if (\version_compare($this->version, '2.2.5', '<')) {