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