summary refs log tree commit diff stats
path: root/lib/keystore.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2020-03-03 16:20:07 -0500
committerDrew DeVault <sir@cmpwn.com>2020-03-03 16:49:52 -0500
commitf3158b36f1f210ff54febbe82b571c1379b30c98 (patch)
tree10cde839c9517609f55b8f1057b1cf84ac592632 /lib/keystore.go
parent89f1684ea4b5e680db7ff06a54b2d4e78212cd12 (diff)
downloadaerc-f3158b36f1f210ff54febbe82b571c1379b30c98.tar.gz
Initial support for PGP decryption & signatures
Diffstat (limited to 'lib/keystore.go')
-rw-r--r--lib/keystore.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/keystore.go b/lib/keystore.go
new file mode 100644
index 0000000..dcdbd74
--- /dev/null
+++ b/lib/keystore.go
@@ -0,0 +1,81 @@
+package lib
+
+import (
+	"io"
+	"os"
+	"path"
+
+	"github.com/kyoh86/xdg"
+	"golang.org/x/crypto/openpgp"
+	"golang.org/x/crypto/openpgp/packet"
+)
+
+var (
+	Keyring openpgp.EntityList
+
+	locked bool
+)
+
+func InitKeyring() {
+	os.MkdirAll(path.Join(xdg.DataHome(), "aerc"), 0700)
+
+	lockpath := path.Join(xdg.DataHome(), "aerc", "keyring.lock")
+	lockfile, err := os.OpenFile(lockpath, os.O_CREATE|os.O_EXCL, 0600)
+	if err != nil {
+		// TODO: Consider connecting to main process over IPC socket
+		locked = false
+	} else {
+		locked = true
+		lockfile.Close()
+	}
+
+	keypath := path.Join(xdg.DataHome(), "aerc", "keyring.asc")
+	keyfile, err := os.Open(keypath)
+	if os.IsNotExist(err) {
+		return
+	} else if err != nil {
+		panic(err)
+	}
+	defer keyfile.Close()
+
+	Keyring, err = openpgp.ReadKeyRing(keyfile)
+	if err != nil {
+		panic(err)
+	}
+}
+
+func UnlockKeyring() {
+	if !locked {
+		return
+	}
+	lockpath := path.Join(xdg.DataHome(), "aerc", "keyring.lock")
+	os.Remove(lockpath)
+}
+
+func ImportKeys(r io.Reader) error {
+	keys, err := openpgp.ReadKeyRing(r)
+	if err != nil {
+		return err
+	}
+	Keyring = append(Keyring, keys...)
+	if locked {
+		keypath := path.Join(xdg.DataHome(), "aerc", "keyring.asc")
+		keyfile, err := os.OpenFile(keypath, os.O_CREATE|os.O_APPEND, 0600)
+		if err != nil {
+			return err
+		}
+		defer keyfile.Close()
+
+		for _, key := range keys {
+			if key.PrivateKey != nil {
+				err = key.SerializePrivate(keyfile, &packet.Config{})
+			} else {
+				err = key.Serialize(keyfile)
+			}
+			if err != nil {
+				return err
+			}
+		}
+	}
+	return nil
+}
n234' href='#n234'>234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270