aboutsummaryrefslogblamecommitdiffstats
path: root/lib/crypto/crypto.go
blob: e58e60752b6be9c0263355ca210f14bacf1c15f5 (plain) (tree)
1
2
3
4
5
6
7
8
9




               
 
                                       
                                               
                                               
                                    









                                                                                                              
                    
               
                                              
                                        
                                            

 

                                           







                                                                       

                                  
                        
                                  

                          

         














                                                                              
package crypto

import (
	"bytes"
	"io"

	"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/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
	}
	for _, part := range bs.Parts {
		if IsEncrypted(part) {
			return true
		}
	}
	return false
}