server/keymgmt.c: Track and find keys by their 32-bit IDs.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 3 Sep 2017 23:17:40 +0000 (00:17 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 16 Jun 2018 18:13:41 +0000 (19:13 +0100)
server/keymgmt.c
server/tripe-admin.5.in
server/tripe.h

index 51a13d7..7386408 100644 (file)
@@ -387,6 +387,7 @@ founddh:
   kd->tag = xstrdup(t.buf);
   kd->ref = 1;
   kd->kn = 0;
+  kd->id = k->id;
   kd->t_exp = k->exp;
 
   IF_TRACING(T_KEYMGMT, {
@@ -616,6 +617,41 @@ kdata *km_findpriv(const char *tag)
   else return (kh_find(&priv, tag ? tag : "tripe-dh", 1));
 }
 
+/* --- @km_findpubbyid@, @km_findprivbyid@ --- *
+ *
+ * Arguments:  @uint32 id@ = key id to load
+ *
+ * Returns:    Pointer to the kdata object if successful, or null on error.
+ *
+ * Use:                Fetches a public or private key from the keyring given its
+ *             numeric id.
+ */
+
+static kdata *findbyid(keyhalf *kh, uint32 id)
+{
+  key *k;
+  kdata *kd;
+
+  k = key_byid(kh->kf, id); if (!k) goto notfound;
+  kd = kh_find(kh, k->tag, 1); if (!kd) goto notfound;
+  if (kd->id != id) { km_unref(kd); goto notfound; }
+  return (kd);
+
+notfound:
+  a_warn("KX", "%s-keyring", kh->kind, "%s", kh->kr,
+        "unknown-key-id", "0x%08lx", (unsigned long)id,
+        A_END);
+  return (0);
+}
+
+kdata *km_findpubbyid(uint32 id) { return (findbyid(&pub, id)); }
+
+kdata *km_findprivbyid(uint32 id)
+{
+  if (id == master->id) { km_ref(master); return (master); }
+  else return findbyid(&priv, id);
+}
+
 /* --- @km_tag@ --- *
  *
  * Arguments:  @kdata *kd@ - pointer to the kdata object
index 5836eea..a737e77 100644 (file)
@@ -1384,6 +1384,11 @@ A key named
 .I tag
 couldn't be found in the keyring.
 .SP
+.BI "KEYMGMT " which "-keyring " file " unknown-key-id 0x" keyid
+A key with the given
+.I keyid
+(in hex) was requested but not found.
+.SP
 .BI "KEYMGMT " which "-keyring " file " line " line " " message
 The contents of the keyring file are invalid.  There may well be a bug
 in the
index 940e3d6..d3dce08 100644 (file)
@@ -402,6 +402,7 @@ struct algswitch {
 struct kdata {
   unsigned ref;                                /* Reference counter */
   struct knode *kn;                    /* Pointer to cache entry */
+  uint32 id;                           /* The underlying key's id */
   char *tag;                           /* Full tag name of the key */
   dhgrp *grp;                          /* The group we work in */
   dhsc *k;                             /* The private key (or null) */
@@ -827,6 +828,19 @@ extern int km_reload(void);
 extern kdata *km_findpub(const char */*tag*/);
 extern kdata *km_findpriv(const char */*tag*/);
 
+/* --- @km_findpubbyid@, @km_findprivbyid@ --- *
+ *
+ * Arguments:  @uint32 id@ = key id to load
+ *
+ * Returns:    Pointer to the kdata object if successful, or null on error.
+ *
+ * Use:                Fetches a public or private key from the keyring given its
+ *             numeric id.
+ */
+
+extern kdata *km_findpubbyid(uint32 /*id*/);
+extern kdata *km_findprivbyid(uint32 /*id*/);
+
 /* --- @km_samealgsp@ --- *
  *
  * Arguments:  @const kdata *kdx, *kdy@ = two key data objects