From b57fceaad4bfcbd4ca3022e013b73eff72079c0b Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Thu, 5 May 2022 12:53:16 -0500 Subject: pgp: add attach key command Add compose command ("attach-key") to attach the public key associated with the sending account. Public key is attached in ascii armor format, with the mimetype set according to RFC 3156 ("application/pgp-keys"). Signed-off-by: Tim Culverhouse Tested-by: Koni Marti --- lib/crypto/pgp/pgp.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lib/crypto/pgp/pgp.go') diff --git a/lib/crypto/pgp/pgp.go b/lib/crypto/pgp/pgp.go index f0f3f655..4dbe37c7 100644 --- a/lib/crypto/pgp/pgp.go +++ b/lib/crypto/pgp/pgp.go @@ -13,6 +13,7 @@ import ( "git.sr.ht/~rjarry/aerc/models" "github.com/ProtonMail/go-crypto/openpgp" + "github.com/ProtonMail/go-crypto/openpgp/armor" "github.com/ProtonMail/go-crypto/openpgp/packet" "github.com/emersion/go-message/mail" "github.com/emersion/go-pgpmail" @@ -271,6 +272,39 @@ func (m *Mail) GetKeyId(s string) (string, error) { return entity.PrimaryKey.KeyIdString(), nil } +func (m *Mail) ExportKey(k string) (io.Reader, error) { + var err error + var entity *openpgp.Entity + switch strings.Contains(k, "@") { + case true: + entity, err = m.getSignerEntityByEmail(k) + if err != nil { + return nil, err + } + case false: + entity, err = m.getSignerEntityByKeyId(k) + if err != nil { + return nil, err + } + } + pks := bytes.NewBuffer(nil) + err = entity.Serialize(pks) + if err != nil { + return nil, fmt.Errorf("pgp: error exporting key: %v", err) + } + pka := bytes.NewBuffer(nil) + w, err := armor.Encode(pka, "PGP PUBLIC KEY BLOCK", map[string]string{}) + if err != nil { + return nil, fmt.Errorf("pgp: error exporting key: %v", err) + } + w.Write(pks.Bytes()) + if err != nil { + return nil, fmt.Errorf("pgp: error exporting key: %v", err) + } + w.Close() + return pka, nil +} + func handleSignatureError(e string) models.SignatureValidity { if e == "openpgp: signature made by unknown entity" { return models.UnknownEntity -- cgit