#89 Added getEncryptedMessageKeys() to get used encryption keys

And when no passphrases are entered, use `--pinentry-mode cancel`
This commit is contained in:
the-djmaze 2022-01-27 00:28:46 +01:00
parent d3b60bf097
commit dfd255545a
2 changed files with 43 additions and 8 deletions

View file

@ -500,9 +500,6 @@ class Message implements \JsonSerializable
$gEncryptedParts = $oBodyStructure->SearchByContentType('multipart/encrypted'); $gEncryptedParts = $oBodyStructure->SearchByContentType('multipart/encrypted');
foreach ($gEncryptedParts as $oPart) { foreach ($gEncryptedParts as $oPart) {
if ($oPart->IsPgpEncrypted()) { if ($oPart->IsPgpEncrypted()) {
if (!$oMessage->aPgpEncrypted) {
$oMessage->aPgpEncrypted = [];
}
$oMessage->aPgpEncrypted = [ $oMessage->aPgpEncrypted = [
'PartId' => $oPart->SubParts()[1]->PartID() 'PartId' => $oPart->SubParts()[1]->PartID()
]; ];
@ -582,6 +579,19 @@ class Message implements \JsonSerializable
} }
else else
{ {
if (\str_contains($sText, '-----BEGIN PGP MESSAGE-----'))
{
$keyIds = [];
if (\SnappyMail\PGP\GPG::isSupported()) {
$GPG = new \SnappyMail\PGP\GPG('');
$keyIds = $GPG->getEncryptedMessageKeys($sText);
}
$oMessage->aPgpEncrypted = [
'PartId' => $oPart->PartID(),
'KeyIds' => $keyIds
];
}
if ($oPart->IsFlowedFormat()) if ($oPart->IsFlowedFormat())
{ {
$sText = Utils::DecodeFlowedFormat($sText); $sText = Utils::DecodeFlowedFormat($sText);

View file

@ -203,7 +203,9 @@ class GPG
$fclose = $this->setOutput($output); $fclose = $this->setOutput($output);
if ($this->decryptKeys) {
$_ENV['PINENTRY_USER_DATA'] = \json_encode($this->decryptKeys); $_ENV['PINENTRY_USER_DATA'] = \json_encode($this->decryptKeys);
}
$result = $this->exec(['--decrypt','--skip-verify']); $result = $this->exec(['--decrypt','--skip-verify']);
@ -728,10 +730,12 @@ class GPG
$arguments[] = '--textmode'; $arguments[] = '--textmode';
} }
if ($this->signKeys) {
foreach ($this->signKeys as $fingerprint => $pass) { foreach ($this->signKeys as $fingerprint => $pass) {
$arguments[] = '--local-user ' . \escapeshellarg($fingerprint); $arguments[] = '--local-user ' . \escapeshellarg($fingerprint);
} }
$_ENV['PINENTRY_USER_DATA'] = \json_encode($this->signKeys); $_ENV['PINENTRY_USER_DATA'] = \json_encode($this->signKeys);
}
$result = $this->exec($arguments); $result = $this->exec($arguments);
@ -904,6 +908,26 @@ class GPG
// echo `gpg-agent --daemon --homedir $home 2>&1`; // echo `gpg-agent --daemon --homedir $home 2>&1`;
} }
public function getEncryptedMessageKeys(/*string|resource*/ $data) : array
{
$this->setInput($data);
// $_ENV['PINENTRY_USER_DATA'] = null;
$result = $this->exec(['--decrypt','--skip-verify']);
$info = [
'ENC_TO' => [],
// 'KEY_CONSIDERED' => [],
// 'NO_SECKEY' => [],
// 'errors' => $result['errors']
];
foreach ($result['status'] as $line) {
$tokens = \explode(' ', $line);
if (isset($info[$tokens[0]])) {
$info[$tokens[0]][] = $tokens[1];
}
}
return $info['ENC_TO'];
}
private function exec(array $arguments) /*: array|false*/ private function exec(array $arguments) /*: array|false*/
{ {
if (\version_compare($this->version, '2.2.5', '<')) { if (\version_compare($this->version, '2.2.5', '<')) {
@ -923,7 +947,8 @@ class GPG
// '--no-use-agent', // < 2.0.0 // '--no-use-agent', // < 2.0.0
'--exit-on-status-write-error', // 1.4.2+ '--exit-on-status-write-error', // 1.4.2+
'--trust-model always', // 1.3.2+ else --always-trust '--trust-model always', // 1.3.2+ else --always-trust
'--pinentry-mode loopback' // 2.1.13+ // If no passphrases are set, cancel them
'--pinentry-mode ' . (empty($_ENV['PINENTRY_USER_DATA']) ? 'cancel' : 'loopback') // 2.1.13+
]; ];
if (!$this->strict) { if (!$this->strict) {