| 1 |
package gpgbin |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/models" |
| 9 |
) |
| 10 |
|
| 11 |
// Decrypt runs gpg --decrypt on the contents of r. If the packet is signed, |
| 12 |
// the signature is also verified |
| 13 |
func Decrypt(r io.Reader) (*models.MessageDetails, error) { |
| 14 |
md := new(models.MessageDetails) |
| 15 |
orig, err := io.ReadAll(r) |
| 16 |
if err != nil { |
| 17 |
return md, err |
| 18 |
} |
| 19 |
args := []string{"--decrypt"} |
| 20 |
g := newGpg(bytes.NewReader(orig), args) |
| 21 |
_ = g.cmd.Run() |
| 22 |
// Always parse stdout, even if there was an error running command. |
| 23 |
// We'll find the error in the parsing |
| 24 |
err = parseStatusFd(bytes.NewReader(g.stderr.Bytes()), md) |
| 25 |
|
| 26 |
if errors.Is(err, NoValidOpenPgpData) { |
| 27 |
md.Body = bytes.NewReader(orig) |
| 28 |
return md, nil |
| 29 |
} else if err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
|
| 33 |
md.Body = bytes.NewReader(g.stdout.Bytes()) |
| 34 |
return md, nil |
| 35 |
} |