File: crypto.go

package info (click to toggle)
aerc 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,900 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (56 lines) | stat: -rw-r--r-- 1,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package crypto

import (
	"bytes"
	"io"
	"slices"

	"git.sr.ht/~rjarry/aerc/config"
	"git.sr.ht/~rjarry/aerc/lib/crypto/gpg"
	"git.sr.ht/~rjarry/aerc/lib/crypto/pgp"
	"git.sr.ht/~rjarry/aerc/lib/log"
	"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
	Init() error
	Close()
	GetSignerKeyId(string) (string, error)
	GetKeyId(string) (string, error)
	ExportKey(string) (io.Reader, error)
}

func New() Provider {
	switch config.General.PgpProvider {
	case "auto":
		internal := &pgp.Mail{}
		if internal.KeyringExists() {
			log.Debugf("internal pgp keyring exists")
			return internal
		}
		log.Debugf("no internal pgp keyring, using system gpg")
		fallthrough
	case "gpg":
		return &gpg.Mail{}
	case "internal":
		return &pgp.Mail{}
	default:
		return nil
	}
}

func IsEncrypted(bs *models.BodyStructure) bool {
	if bs == nil {
		return false
	}
	if bs.MIMEType == "application" && bs.MIMESubType == "pgp-encrypted" {
		return true
	}
	return slices.ContainsFunc(bs.Parts, IsEncrypted)
}