| 1 |
package gpg |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"os/exec" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/lib/crypto/gpg/gpgbin" |
| 9 |
"git.sr.ht/~rjarry/aerc/models" |
| 10 |
"github.com/ProtonMail/go-crypto/openpgp" |
| 11 |
"github.com/emersion/go-message/mail" |
| 12 |
) |
| 13 |
|
| 14 |
// Mail satisfies the PGPProvider interface in aerc |
| 15 |
type Mail struct{} |
| 16 |
|
| 17 |
func (m *Mail) Init() error { |
| 18 |
_, err := exec.LookPath("gpg") |
| 19 |
return err |
| 20 |
} |
| 21 |
|
| 22 |
func (m *Mail) Decrypt(r io.Reader, decryptKeys openpgp.PromptFunction) (*models.MessageDetails, error) { |
| 23 |
gpgReader, err := Read(r) |
| 24 |
if err != nil { |
| 25 |
return nil, err |
| 26 |
} |
| 27 |
md := gpgReader.MessageDetails |
| 28 |
md.SignatureValidity = models.Valid |
| 29 |
if md.SignatureError != "" { |
| 30 |
md.SignatureValidity = handleSignatureError(md.SignatureError) |
| 31 |
} |
| 32 |
return md, nil |
| 33 |
} |
| 34 |
|
| 35 |
func (m *Mail) ImportKeys(r io.Reader) error { |
| 36 |
return gpgbin.Import(r) |
| 37 |
} |
| 38 |
|
| 39 |
func (m *Mail) Encrypt(buf *bytes.Buffer, rcpts []string, signer string, decryptKeys openpgp.PromptFunction, header *mail.Header) (io.WriteCloser, error) { |
| 40 |
return Encrypt(buf, header.Header.Header, rcpts, signer) |
| 41 |
} |
| 42 |
|
| 43 |
func (m *Mail) Sign(buf *bytes.Buffer, signer string, decryptKeys openpgp.PromptFunction, header *mail.Header) (io.WriteCloser, error) { |
| 44 |
return Sign(buf, header.Header.Header, signer) |
| 45 |
} |
| 46 |
|
| 47 |
func (m *Mail) Close() {} |
| 48 |
|
| 49 |
func (m *Mail) GetSignerKeyId(s string) (string, error) { |
| 50 |
return gpgbin.GetPrivateKeyId(s) |
| 51 |
} |
| 52 |
|
| 53 |
func (m *Mail) GetKeyId(s string) (string, error) { |
| 54 |
return gpgbin.GetKeyId(s) |
| 55 |
} |
| 56 |
|
| 57 |
func (m *Mail) ExportKey(k string) (io.Reader, error) { |
| 58 |
return gpgbin.ExportPublicKey(k) |
| 59 |
} |
| 60 |
|
| 61 |
func handleSignatureError(e string) models.SignatureValidity { |
| 62 |
if e == "gpg: missing public key" { |
| 63 |
return models.UnknownEntity |
| 64 |
} |
| 65 |
if e == "gpg: header hash does not match actual sig hash" { |
| 66 |
return models.MicalgMismatch |
| 67 |
} |
| 68 |
return models.UnknownValidity |
| 69 |
} |