2022-04-25 15:30:44 +02:00
|
|
|
package gpgbin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"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)
|
2022-08-17 16:19:45 +02:00
|
|
|
orig, err := io.ReadAll(r)
|
2022-04-25 15:30:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return md, err
|
|
|
|
}
|
|
|
|
args := []string{"--decrypt"}
|
|
|
|
g := newGpg(bytes.NewReader(orig), args)
|
2022-06-27 14:31:31 +02:00
|
|
|
_ = 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)
|
2022-04-25 15:30:44 +02:00
|
|
|
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:
|
2022-05-04 05:41:29 +02:00
|
|
|
return nil, err
|
2022-04-25 15:30:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return md, nil
|
|
|
|
}
|