| 1 |
package crypto |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"slices" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/config" |
| 9 |
"git.sr.ht/~rjarry/aerc/lib/crypto/gpg" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/crypto/pgp" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 12 |
"git.sr.ht/~rjarry/aerc/models" |
| 13 |
"github.com/ProtonMail/go-crypto/openpgp" |
| 14 |
"github.com/emersion/go-message/mail" |
| 15 |
) |
| 16 |
|
| 17 |
type Provider interface { |
| 18 |
Decrypt(io.Reader, openpgp.PromptFunction) (*models.MessageDetails, error) |
| 19 |
Encrypt(*bytes.Buffer, []string, string, openpgp.PromptFunction, *mail.Header) (io.WriteCloser, error) |
| 20 |
Sign(*bytes.Buffer, string, openpgp.PromptFunction, *mail.Header) (io.WriteCloser, error) |
| 21 |
ImportKeys(io.Reader) error |
| 22 |
Init() error |
| 23 |
Close() |
| 24 |
GetSignerKeyId(string) (string, error) |
| 25 |
GetKeyId(string) (string, error) |
| 26 |
ExportKey(string) (io.Reader, error) |
| 27 |
} |
| 28 |
|
| 29 |
func New() Provider { |
| 30 |
switch config.General().PgpProvider { |
| 31 |
case "auto": |
| 32 |
internal := &pgp.Mail{} |
| 33 |
if internal.KeyringExists() { |
| 34 |
log.Debugf("internal pgp keyring exists") |
| 35 |
return internal |
| 36 |
} |
| 37 |
log.Debugf("no internal pgp keyring, using system gpg") |
| 38 |
fallthrough |
| 39 |
case "gpg": |
| 40 |
return &gpg.Mail{} |
| 41 |
case "internal": |
| 42 |
return &pgp.Mail{} |
| 43 |
default: |
| 44 |
return nil |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func IsEncrypted(bs *models.BodyStructure) bool { |
| 49 |
if bs == nil { |
| 50 |
return false |
| 51 |
} |
| 52 |
if bs.MIMEType == "application" && bs.MIMESubType == "pgp-encrypted" { |
| 53 |
return true |
| 54 |
} |
| 55 |
return slices.ContainsFunc(bs.Parts, IsEncrypted) |
| 56 |
} |