2022-04-25 15:30:43 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
|
2022-04-25 15:30:44 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/crypto/gpg"
|
2022-04-25 15:30:43 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/crypto/pgp"
|
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"github.com/ProtonMail/go-crypto/openpgp"
|
|
|
|
"github.com/emersion/go-message/mail"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Provider interface {
|
|
|
|
Decrypt(io.Reader, openpgp.PromptFunction) (*models.MessageDetails, error)
|
|
|
|
Encrypt(*bytes.Buffer, []string, string, openpgp.PromptFunction, *mail.Header) (io.WriteCloser, error)
|
|
|
|
Sign(*bytes.Buffer, string, openpgp.PromptFunction, *mail.Header) (io.WriteCloser, error)
|
|
|
|
ImportKeys(io.Reader) error
|
2022-07-19 22:31:51 +02:00
|
|
|
Init() error
|
2022-04-25 15:30:43 +02:00
|
|
|
Close()
|
2022-04-29 18:19:52 +02:00
|
|
|
GetSignerKeyId(string) (string, error)
|
2022-05-05 19:53:15 +02:00
|
|
|
GetKeyId(string) (string, error)
|
2022-05-05 19:53:16 +02:00
|
|
|
ExportKey(string) (io.Reader, error)
|
2022-04-25 15:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(s string) Provider {
|
|
|
|
switch s {
|
2022-04-25 15:30:44 +02:00
|
|
|
case "gpg":
|
|
|
|
return &gpg.Mail{}
|
2022-04-25 15:30:43 +02:00
|
|
|
default:
|
|
|
|
return &pgp.Mail{}
|
|
|
|
}
|
|
|
|
}
|
2022-08-31 02:25:31 +02:00
|
|
|
|
|
|
|
func IsEncrypted(bs *models.BodyStructure) bool {
|
|
|
|
if bs == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if bs.MIMEType == "application" && bs.MIMESubType == "pgp-encrypted" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, part := range bs.Parts {
|
|
|
|
if IsEncrypted(part) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|