[PATCH] Add keygrip to LISTKEYS output.

Ben Kibbey bjk at luxsci.net
Tue Jul 19 03:11:48 CEST 2016


* src/gpgme.h.in: Add GPGME_KEYLIST_MODE_WITH_KEYGRIP.
(_gpgme_subkey): Add grp member.
* src/engine-gpg.c (gpg_keylist_build_options): Parse the new mode and
add gpg option --with-keygrip.
* src/gpgme-tool.c (result_add_grp): New.
(cmd_keylist): Add option --with-keygrip and grp XML result element.
* src/keylist.c (keylist_colon_handler): Set grp member to keygrip.
--

Not sure whether to add the keygrip to the output by default or not so
this is why --with-keygrip is added as an option to LISTKEYS.

Signed-off-by: Ben Kibbey <bjk at luxsci.net>
---
 src/engine-gpg.c |  2 ++
 src/gpgme-tool.c | 46 ++++++++++++++++++++++++++++++++++++++--------
 src/gpgme.h.in   |  4 ++++
 src/keylist.c    | 20 +++++++++++++++++++-
 4 files changed, 63 insertions(+), 9 deletions(-)

diff --git a/src/engine-gpg.c b/src/engine-gpg.c
index 16571a5..5537c33 100644
--- a/src/engine-gpg.c
+++ b/src/engine-gpg.c
@@ -2291,6 +2291,8 @@ gpg_keylist_build_options (engine_gpg_t gpg, int secret_only,
     err = add_arg (gpg, "--with-fingerprint");
   if (!err && (mode & GPGME_KEYLIST_MODE_WITH_SECRET))
     err = add_arg (gpg, "--with-secret");
+  if (!err && (mode & GPGME_KEYLIST_MODE_WITH_KEYGRIP))
+    err = add_arg (gpg, "--with-keygrip");
   if (!err
       && (mode & GPGME_KEYLIST_MODE_SIGS)
       && (mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS))
diff --git a/src/gpgme-tool.c b/src/gpgme-tool.c
index 5ad5b25..cb6ce37 100644
--- a/src/gpgme-tool.c
+++ b/src/gpgme-tool.c
@@ -556,6 +556,14 @@ result_add_fpr (struct result_xml_state *state, char *name, char *fpr)
   return 0;
 }
 
+gpg_error_t
+result_add_grp (struct result_xml_state *state, char *name, char *grp)
+{
+  result_xml_tag_start (state, name, NULL);
+  result_xml_tag_data (state, grp);
+  result_xml_tag_end (state);
+  return 0;
+}
 
 gpg_error_t
 result_add_timestamp (struct result_xml_state *state, char *name,
@@ -2810,7 +2818,7 @@ cmd_delete (assuan_context_t ctx, char *line)
 
 
 static const char hlp_keylist[] =
-  "KEYLIST [--secret-only] [<patterns>]\n"
+  "KEYLIST [--secret-only] [--with-keygrip] [<patterns>]\n"
   "\n"
   "List all certificates or only those specified by PATTERNS.  Each\n"
   "pattern shall be a percent-plus escaped certificate specification.";
@@ -2825,16 +2833,36 @@ cmd_keylist (assuan_context_t ctx, char *line)
   int secret_only = 0;
   int idx, indent=2;
   const char *pattern[MAX_CMD_KEYLIST_PATTERN+1];
-  const char optstr[] = "--secret-only";
   char *p;
 
-  if (!strncasecmp (line, optstr, strlen (optstr)))
+  do
     {
-      secret_only = 1;
-      line += strlen (optstr);
-      while (*line && !spacep (line))
-	line++;
-    }
+      char *s, *l;
+
+      if ((s = strstr (line, "--secret-only")))
+        secret_only = 1;
+      else if ((s = strstr (line, "--with-keygrip")))
+        {
+          gpgme_keylist_mode_t mode = gpgme_get_keylist_mode (server->gt->ctx);
+
+          mode |= GPGME_KEYLIST_MODE_WITH_KEYGRIP;
+          err = gpgme_set_keylist_mode (server->gt->ctx, mode);
+          if (err)
+            return err;
+        }
+      else
+        break;
+
+      l = s;
+
+      while (*s && !spacep (s))
+        s++;
+
+      while (*s)
+        *l++ = *s++;
+
+      *l = 0;
+    } while (1);
 
   idx = 0;
   for (p=line; *p; line = p)
@@ -2901,6 +2929,8 @@ cmd_keylist (assuan_context_t ctx, char *line)
 	    result_add_keyid (&state, "keyid", subkey->keyid);
             if (subkey->fpr)
               result_add_fpr (&state, "fpr", subkey->fpr);
+            if (subkey->grp)
+              result_add_grp (&state, "grp", subkey->grp);
             result_add_value (&state, "secret", subkey->secret);
             result_add_value (&state, "is_cardkey", subkey->is_cardkey);
             if (subkey->card_number)
diff --git a/src/gpgme.h.in b/src/gpgme.h.in
index 49d56c3..d24978c 100644
--- a/src/gpgme.h.in
+++ b/src/gpgme.h.in
@@ -413,6 +413,7 @@ gpgme_protocol_t;
 #define GPGME_KEYLIST_MODE_WITH_SECRET       	16
 #define GPGME_KEYLIST_MODE_EPHEMERAL            128
 #define GPGME_KEYLIST_MODE_VALIDATE		256
+#define GPGME_KEYLIST_MODE_WITH_KEYGRIP		512
 
 typedef unsigned int gpgme_keylist_mode_t;
 
@@ -680,6 +681,9 @@ struct _gpgme_subkey
   /* The fingerprint of the subkey in hex digit form.  */
   char *fpr;
 
+  /* The keygrip of the subkey in hex digit form.  */
+  char *grp;
+
   /* The creation timestamp, -1 if invalid, 0 if not available.  */
   long int timestamp;
 
diff --git a/src/keylist.c b/src/keylist.c
index fcf574f..531bf51 100644
--- a/src/keylist.c
+++ b/src/keylist.c
@@ -427,7 +427,7 @@ keylist_colon_handler (void *priv, char *line)
   enum
     {
       RT_NONE, RT_SIG, RT_UID, RT_SUB, RT_PUB, RT_FPR,
-      RT_SSB, RT_SEC, RT_CRT, RT_CRS, RT_REV, RT_SPK
+      RT_SSB, RT_SEC, RT_CRT, RT_CRS, RT_REV, RT_SPK, RT_GRP
     }
   rectype = RT_NONE;
 #define NR_FIELDS 17
@@ -479,6 +479,8 @@ keylist_colon_handler (void *priv, char *line)
     rectype = RT_CRS;
   else if (!strcmp (field[0], "fpr") && key)
     rectype = RT_FPR;
+  else if (!strcmp (field[0], "grp") && key)
+    rectype = RT_GRP;
   else if (!strcmp (field[0], "uid") && key)
     rectype = RT_UID;
   else if (!strcmp (field[0], "sub") && key)
@@ -717,6 +719,22 @@ keylist_colon_handler (void *priv, char *line)
 	}
       break;
 
+    case RT_GRP:
+      /* Field 10 has the keygrip (take only the first one).  */
+      if (fields >= 10 && field[9] && *field[9])
+	{
+          /* Need to apply it to the last subkey because all subkeys
+             do have keygrips. */
+          subkey = key->_last_subkey;
+          if (!subkey->grp)
+            {
+              subkey->grp = strdup (field[9]);
+              if (!subkey->grp)
+                return gpg_error_from_syserror ();
+            }
+	}
+      break;
+
     case RT_SIG:
     case RT_REV:
       if (!opd->tmp_uid)
-- 
2.8.1




More information about the Gnupg-devel mailing list