diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2022-04-29 11:19:52 -0500 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-05-04 14:07:15 +0200 |
commit | dbf52bb4b48748586bb6343ae4ad6d424f0631ac (patch) | |
tree | bd806636b0be51f07218f5a9db9be45af72db9ba /lib/crypto/gpg/gpgbin | |
parent | b29293d7b53c73629911ec75b2ec5954d365feed (diff) | |
download | aerc-dbf52bb4b48748586bb6343ae4ad6d424f0631ac.tar.gz |
pgp: check for signing key before signing time
Check that the signing key exists when the user issues the :sign
command. The signing key ID will be displayed in the security status
also, allowing the user to see what key will be used to sign the
message.
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: Jens Grassel <jens@wegtam.com>
Diffstat (limited to 'lib/crypto/gpg/gpgbin')
-rw-r--r-- | lib/crypto/gpg/gpgbin/gpgbin.go | 23 | ||||
-rw-r--r-- | lib/crypto/gpg/gpgbin/keys.go | 13 |
2 files changed, 36 insertions, 0 deletions
diff --git a/lib/crypto/gpg/gpgbin/gpgbin.go b/lib/crypto/gpg/gpgbin/gpgbin.go index da046f46..3ee81399 100644 --- a/lib/crypto/gpg/gpgbin/gpgbin.go +++ b/lib/crypto/gpg/gpgbin/gpgbin.go @@ -77,6 +77,29 @@ func getIdentity(key uint64) string { return "" } +// getKeyId returns the 16 digit key id, if key exists +func getKeyId(s string, private bool) string { + cmd := exec.Command("gpg", "--with-colons", "--batch") + listArg := "--list-keys" + if private { + listArg = "--list-secret-keys" + } + cmd.Args = append(cmd.Args, listArg, s) + + var outbuf strings.Builder + cmd.Stdout = &outbuf + cmd.Run() + out := strings.Split(outbuf.String(), "\n") + for _, line := range out { + if strings.HasPrefix(line, "fpr") { + flds := strings.Split(line, ":") + id := flds[9] + return id[len(id)-16:] + } + } + return "" +} + // longKeyToUint64 returns a uint64 version of the given key func longKeyToUint64(key string) (uint64, error) { fpr := string(key[len(key)-16:]) diff --git a/lib/crypto/gpg/gpgbin/keys.go b/lib/crypto/gpg/gpgbin/keys.go new file mode 100644 index 00000000..660ce821 --- /dev/null +++ b/lib/crypto/gpg/gpgbin/keys.go @@ -0,0 +1,13 @@ +package gpgbin + +import "fmt" + +// GetPrivateKeyId runs gpg --list-secret-keys s +func GetPrivateKeyId(s string) (string, error) { + private := true + id := getKeyId(s, private) + if id == "" { + return "", fmt.Errorf("no private key found") + } + return id, nil +} |