2022-04-25 15:30:44 +02:00
|
|
|
package gpgbin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-25 16:22:49 +02:00
|
|
|
"fmt"
|
2022-04-25 15:30:44 +02:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Encrypt runs gpg --encrypt [--sign] -r [recipient]. The default is to have
|
|
|
|
// --trust-model always set
|
|
|
|
func Encrypt(r io.Reader, to []string, from string) ([]byte, error) {
|
2022-07-31 22:16:40 +02:00
|
|
|
// TODO probably shouldn't have --trust-model always a default
|
2022-04-25 15:30:44 +02:00
|
|
|
args := []string{
|
|
|
|
"--armor",
|
|
|
|
"--trust-model", "always",
|
|
|
|
}
|
|
|
|
if from != "" {
|
|
|
|
args = append(args, "--sign", "--default-key", from)
|
|
|
|
}
|
|
|
|
for _, rcpt := range to {
|
|
|
|
args = append(args, "--recipient", rcpt)
|
|
|
|
}
|
|
|
|
args = append(args, "--encrypt", "-")
|
|
|
|
|
|
|
|
g := newGpg(r, args)
|
2022-08-04 22:48:14 +02:00
|
|
|
_ = g.cmd.Run()
|
2022-04-25 15:30:44 +02:00
|
|
|
outRdr := bytes.NewReader(g.stdout.Bytes())
|
|
|
|
var md models.MessageDetails
|
2022-08-04 22:48:14 +02:00
|
|
|
err := parse(outRdr, &md)
|
2022-06-25 16:22:49 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, fmt.Errorf("gpg: failure to encrypt: %w. check public key(s)", err)
|
2022-06-25 16:22:49 +02:00
|
|
|
}
|
2022-04-25 15:30:44 +02:00
|
|
|
var buf bytes.Buffer
|
2022-07-29 22:31:54 +02:00
|
|
|
_, _ = io.Copy(&buf, md.Body)
|
2022-04-25 15:30:44 +02:00
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|