aerc/lib/crypto/gpg/gpgbin/decrypt.go
Tim Culverhouse ccd76e6494 gpg: fix error handling during decryption
An non-zero exit code from the execution of gpg during decryption would
prevent aerc from parsing the output of gpg. The output should always be
parsed. Gpg can exit with an error due to not being able to validate a
signature. Aerc handles this error with the UI, and therefore all output
should be parsed regardless of exit state of gpg. The parsing of stdout
will find the errors and report back to aerc properly.

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
2022-06-28 22:00:04 +02:00

38 lines
862 B
Go

package gpgbin
import (
"bytes"
"io"
"io/ioutil"
"git.sr.ht/~rjarry/aerc/models"
)
// Decrypt runs gpg --decrypt on the contents of r. If the packet is signed,
// the signature is also verified
func Decrypt(r io.Reader) (*models.MessageDetails, error) {
md := new(models.MessageDetails)
orig, err := ioutil.ReadAll(r)
if err != nil {
return md, err
}
args := []string{"--decrypt"}
g := newGpg(bytes.NewReader(orig), args)
_ = g.cmd.Run()
outRdr := bytes.NewReader(g.stdout.Bytes())
// Always parse stdout, even if there was an error running command.
// We'll find the error in the parsing
err = parse(outRdr, md)
if err != nil {
err = parseError(g.stderr.String())
switch GPGErrors[err.Error()] {
case ERROR_NO_PGP_DATA_FOUND:
md.Body = bytes.NewReader(orig)
return md, nil
default:
return nil, err
}
}
return md, nil
}