| 1 |
package pgp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 12 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 13 |
"git.sr.ht/~rjarry/aerc/models" |
| 14 |
"github.com/ProtonMail/go-crypto/openpgp" |
| 15 |
"github.com/ProtonMail/go-crypto/openpgp/armor" |
| 16 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 17 |
"github.com/emersion/go-message/mail" |
| 18 |
"github.com/emersion/go-pgpmail" |
| 19 |
"github.com/pkg/errors" |
| 20 |
) |
| 21 |
|
| 22 |
type Mail struct{} |
| 23 |
|
| 24 |
var ( |
| 25 |
Keyring openpgp.EntityList |
| 26 |
|
| 27 |
locked bool |
| 28 |
) |
| 29 |
|
| 30 |
func (m *Mail) KeyringExists() bool { |
| 31 |
keypath := xdg.DataPath("aerc", "keyring.asc") |
| 32 |
keyfile, err := os.Open(keypath) |
| 33 |
if err != nil { |
| 34 |
return false |
| 35 |
} |
| 36 |
defer keyfile.Close() |
| 37 |
_, err = openpgp.ReadKeyRing(keyfile) |
| 38 |
return err == nil |
| 39 |
} |
| 40 |
|
| 41 |
func (m *Mail) Init() error { |
| 42 |
log.Debugf("Initializing PGP keyring") |
| 43 |
err := os.MkdirAll(xdg.DataPath("aerc"), 0o700) |
| 44 |
if err != nil { |
| 45 |
return fmt.Errorf("failed to create data directory: %w", err) |
| 46 |
} |
| 47 |
|
| 48 |
lockpath := xdg.DataPath("aerc", "keyring.lock") |
| 49 |
lockfile, err := os.OpenFile(lockpath, os.O_CREATE|os.O_EXCL, 0o600) |
| 50 |
if err != nil { |
| 51 |
// TODO: Consider connecting to main process over IPC socket |
| 52 |
locked = false |
| 53 |
} else { |
| 54 |
locked = true |
| 55 |
lockfile.Close() |
| 56 |
} |
| 57 |
|
| 58 |
keypath := xdg.DataPath("aerc", "keyring.asc") |
| 59 |
keyfile, err := os.Open(keypath) |
| 60 |
if os.IsNotExist(err) { |
| 61 |
return nil |
| 62 |
} else if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
defer keyfile.Close() |
| 66 |
|
| 67 |
Keyring, err = openpgp.ReadKeyRing(keyfile) |
| 68 |
if err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
func (m *Mail) Close() { |
| 75 |
if !locked { |
| 76 |
return |
| 77 |
} |
| 78 |
lockpath := xdg.DataPath("aerc", "keyring.lock") |
| 79 |
os.Remove(lockpath) |
| 80 |
} |
| 81 |
|
| 82 |
func (m *Mail) getEntityByEmail(email string) (e *openpgp.Entity, err error) { |
| 83 |
for _, entity := range Keyring { |
| 84 |
ident := entity.PrimaryIdentity() |
| 85 |
if ident != nil && ident.UserId.Email == email { |
| 86 |
return entity, nil |
| 87 |
} |
| 88 |
} |
| 89 |
return nil, fmt.Errorf("entity not found in keyring") |
| 90 |
} |
| 91 |
|
| 92 |
func (m *Mail) getSignerEntityByKeyId(id string) (*openpgp.Entity, error) { |
| 93 |
id = strings.ToUpper(id) |
| 94 |
for _, key := range Keyring.DecryptionKeys() { |
| 95 |
if key.Entity == nil { |
| 96 |
continue |
| 97 |
} |
| 98 |
kId := key.Entity.PrimaryKey.KeyIdString() |
| 99 |
if strings.Contains(kId, id) { |
| 100 |
return key.Entity, nil |
| 101 |
} |
| 102 |
} |
| 103 |
return nil, fmt.Errorf("entity not found in keyring") |
| 104 |
} |
| 105 |
|
| 106 |
func (m *Mail) getSignerEntityByEmail(email string) (e *openpgp.Entity, err error) { |
| 107 |
for _, key := range Keyring.DecryptionKeys() { |
| 108 |
if key.Entity == nil { |
| 109 |
continue |
| 110 |
} |
| 111 |
ident := key.Entity.PrimaryIdentity() |
| 112 |
if ident != nil && ident.UserId.Email == email { |
| 113 |
return key.Entity, nil |
| 114 |
} |
| 115 |
} |
| 116 |
return nil, fmt.Errorf("entity not found in keyring") |
| 117 |
} |
| 118 |
|
| 119 |
func (m *Mail) Decrypt(r io.Reader, decryptKeys openpgp.PromptFunction) (*models.MessageDetails, error) { |
| 120 |
md := new(models.MessageDetails) |
| 121 |
|
| 122 |
pgpReader, err := pgpmail.Read(r, Keyring, decryptKeys, nil) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
if pgpReader.MessageDetails.IsEncrypted { |
| 127 |
md.IsEncrypted = true |
| 128 |
md.DecryptedWith = pgpReader.MessageDetails.DecryptedWith.Entity.PrimaryIdentity().Name |
| 129 |
md.DecryptedWithKeyId = pgpReader.MessageDetails.DecryptedWith.PublicKey.KeyId |
| 130 |
} |
| 131 |
if pgpReader.MessageDetails.IsSigned { |
| 132 |
// we should consume the UnverifiedBody until EOF in order |
| 133 |
// to get the correct signature data |
| 134 |
data, err := io.ReadAll(pgpReader.MessageDetails.UnverifiedBody) |
| 135 |
if err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
pgpReader.MessageDetails.UnverifiedBody = bytes.NewReader(data) |
| 139 |
|
| 140 |
md.IsSigned = true |
| 141 |
md.SignedBy = "" |
| 142 |
md.SignedByKeyId = pgpReader.MessageDetails.SignedByKeyId |
| 143 |
md.SignatureValidity = models.Valid |
| 144 |
if pgpReader.MessageDetails.SignatureError != nil { |
| 145 |
md.SignatureError = pgpReader.MessageDetails.SignatureError.Error() |
| 146 |
md.SignatureValidity = handleSignatureError(md.SignatureError) |
| 147 |
} |
| 148 |
if pgpReader.MessageDetails.SignedBy != nil { |
| 149 |
md.SignedBy = pgpReader.MessageDetails.SignedBy.Entity.PrimaryIdentity().Name |
| 150 |
} |
| 151 |
} |
| 152 |
md.Body = pgpReader.MessageDetails.UnverifiedBody |
| 153 |
return md, nil |
| 154 |
} |
| 155 |
|
| 156 |
func (m *Mail) ImportKeys(r io.Reader) error { |
| 157 |
keys, err := openpgp.ReadKeyRing(r) |
| 158 |
if err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
Keyring = append(Keyring, keys...) |
| 162 |
if locked { |
| 163 |
keypath := xdg.DataPath("aerc", "keyring.asc") |
| 164 |
keyfile, err := os.OpenFile(keypath, os.O_CREATE|os.O_APPEND, 0o600) |
| 165 |
if err != nil { |
| 166 |
return err |
| 167 |
} |
| 168 |
defer keyfile.Close() |
| 169 |
|
| 170 |
for _, key := range keys { |
| 171 |
if key.PrivateKey != nil { |
| 172 |
err = key.SerializePrivate(keyfile, &packet.Config{}) |
| 173 |
} else { |
| 174 |
err = key.Serialize(keyfile) |
| 175 |
} |
| 176 |
if err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
func (m *Mail) Encrypt(buf *bytes.Buffer, rcpts []string, signer string, decryptKeys openpgp.PromptFunction, header *mail.Header) (io.WriteCloser, error) { |
| 185 |
var err error |
| 186 |
var to []*openpgp.Entity |
| 187 |
var signerEntity *openpgp.Entity |
| 188 |
if signer != "" { |
| 189 |
signerEntity, err = m.getSigner(signer, decryptKeys) |
| 190 |
if err != nil { |
| 191 |
return nil, err |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
for _, rcpt := range rcpts { |
| 196 |
toEntity, err := m.getEntityByEmail(rcpt) |
| 197 |
if err != nil { |
| 198 |
return nil, errors.Wrap(err, "no key for "+rcpt) |
| 199 |
} |
| 200 |
to = append(to, toEntity) |
| 201 |
} |
| 202 |
|
| 203 |
cleartext, err := pgpmail.Encrypt(buf, header.Header.Header, |
| 204 |
to, signerEntity, nil) |
| 205 |
if err != nil { |
| 206 |
return nil, err |
| 207 |
} |
| 208 |
return cleartext, nil |
| 209 |
} |
| 210 |
|
| 211 |
func (m *Mail) Sign(buf *bytes.Buffer, signer string, decryptKeys openpgp.PromptFunction, header *mail.Header) (io.WriteCloser, error) { |
| 212 |
var err error |
| 213 |
var signerEntity *openpgp.Entity |
| 214 |
if signer != "" { |
| 215 |
signerEntity, err = m.getSigner(signer, decryptKeys) |
| 216 |
if err != nil { |
| 217 |
return nil, err |
| 218 |
} |
| 219 |
} |
| 220 |
cleartext, err := pgpmail.Sign(buf, header.Header.Header, signerEntity, nil) |
| 221 |
if err != nil { |
| 222 |
return nil, err |
| 223 |
} |
| 224 |
return cleartext, nil |
| 225 |
} |
| 226 |
|
| 227 |
func (m *Mail) getSigner(signer string, decryptKeys openpgp.PromptFunction) (signerEntity *openpgp.Entity, err error) { |
| 228 |
switch strings.Contains(signer, "@") { |
| 229 |
case true: |
| 230 |
signerEntity, err = m.getSignerEntityByEmail(signer) |
| 231 |
if err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
case false: |
| 235 |
signerEntity, err = m.getSignerEntityByKeyId(signer) |
| 236 |
if err != nil { |
| 237 |
return nil, err |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
key, ok := signerEntity.SigningKey(time.Now()) |
| 242 |
if !ok { |
| 243 |
return nil, fmt.Errorf("no signing key found for %s", signer) |
| 244 |
} |
| 245 |
|
| 246 |
if !key.PrivateKey.Encrypted { |
| 247 |
return signerEntity, nil |
| 248 |
} |
| 249 |
|
| 250 |
_, err = decryptKeys([]openpgp.Key{key}, false) |
| 251 |
if err != nil { |
| 252 |
return nil, err |
| 253 |
} |
| 254 |
|
| 255 |
return signerEntity, nil |
| 256 |
} |
| 257 |
|
| 258 |
func (m *Mail) GetSignerKeyId(s string) (string, error) { |
| 259 |
var err error |
| 260 |
var signerEntity *openpgp.Entity |
| 261 |
switch strings.Contains(s, "@") { |
| 262 |
case true: |
| 263 |
signerEntity, err = m.getSignerEntityByEmail(s) |
| 264 |
if err != nil { |
| 265 |
return "", err |
| 266 |
} |
| 267 |
case false: |
| 268 |
signerEntity, err = m.getSignerEntityByKeyId(s) |
| 269 |
if err != nil { |
| 270 |
return "", err |
| 271 |
} |
| 272 |
} |
| 273 |
return signerEntity.PrimaryKey.KeyIdString(), nil |
| 274 |
} |
| 275 |
|
| 276 |
func (m *Mail) GetKeyId(s string) (string, error) { |
| 277 |
entity, err := m.getEntityByEmail(s) |
| 278 |
if err != nil { |
| 279 |
return "", err |
| 280 |
} |
| 281 |
return entity.PrimaryKey.KeyIdString(), nil |
| 282 |
} |
| 283 |
|
| 284 |
func (m *Mail) ExportKey(k string) (io.Reader, error) { |
| 285 |
var err error |
| 286 |
var entity *openpgp.Entity |
| 287 |
switch strings.Contains(k, "@") { |
| 288 |
case true: |
| 289 |
entity, err = m.getSignerEntityByEmail(k) |
| 290 |
if err != nil { |
| 291 |
return nil, err |
| 292 |
} |
| 293 |
case false: |
| 294 |
entity, err = m.getSignerEntityByKeyId(k) |
| 295 |
if err != nil { |
| 296 |
return nil, err |
| 297 |
} |
| 298 |
} |
| 299 |
pks := bytes.NewBuffer(nil) |
| 300 |
err = entity.Serialize(pks) |
| 301 |
if err != nil { |
| 302 |
return nil, fmt.Errorf("pgp: error exporting key: %w", err) |
| 303 |
} |
| 304 |
pka := bytes.NewBuffer(nil) |
| 305 |
w, err := armor.Encode(pka, "PGP PUBLIC KEY BLOCK", map[string]string{}) |
| 306 |
if err != nil { |
| 307 |
return nil, fmt.Errorf("pgp: error exporting key: %w", err) |
| 308 |
} |
| 309 |
_, err = w.Write(pks.Bytes()) |
| 310 |
if err != nil { |
| 311 |
return nil, fmt.Errorf("pgp: error exporting key: %w", err) |
| 312 |
} |
| 313 |
w.Close() |
| 314 |
return pka, nil |
| 315 |
} |
| 316 |
|
| 317 |
func handleSignatureError(e string) models.SignatureValidity { |
| 318 |
if e == "openpgp: signature made by unknown entity" { |
| 319 |
return models.UnknownEntity |
| 320 |
} |
| 321 |
if strings.HasPrefix(e, "pgpmail: unsupported micalg") { |
| 322 |
return models.UnsupportedMicalg |
| 323 |
} |
| 324 |
if strings.HasPrefix(e, "pgpmail") { |
| 325 |
return models.InvalidSignature |
| 326 |
} |
| 327 |
return models.UnknownValidity |
| 328 |
} |