aerc/lib/crypto/gpg/gpgbin/decrypt.go
Tim Culverhouse 21fca56292 pgp: fix crash on gpg decrypt fail
The gpg decrypt function was catching errors but not returning them.
This patch returns errors that are caught instead of aerc panicking.

Reported-by: ReK2 <rek2@hispagatos.org>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: ReK2 <rek2@hispagatos.org>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-05-04 14:10:32 +02:00

35 lines
749 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)
err = g.cmd.Run()
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
}
}
outRdr := bytes.NewReader(g.stdout.Bytes())
parse(outRdr, md)
return md, nil
}