chiark / gitweb /
server/keyexch.c: Prefix crypto-details trace messages correctly.
[tripe] / server / keymgmt.c
index 601f42ed3db2ce1a8e861e1c39f90e7ad9fd22b7..c7d0c6bc1e52c4373e710f874dc745d8b69a9eca 100644 (file)
@@ -46,115 +46,135 @@ static fwatch w_priv, w_pub;
 
 typedef struct kgops {
   const char *ty;
-  const char *(*loadpriv)(key_data *, group **, mp **, dstr *);
-  const char *(*loadpub)(key_data *, group **, ge **, dstr *);
+  int (*loadpriv)(key_data *, group **, mp **, dstr *, dstr *);
+  int (*loadpub)(key_data *, group **, ge **, dstr *, dstr *);
 } kgops;
 
 /* --- Diffie-Hellman --- */
 
-static const char *kgdh_priv(key_data *kd, group **g, mp **x, dstr *t)
+static int kgdh_priv(key_data *kd, group **g, mp **x, dstr *t, dstr *e)
 {
   key_packstruct kps[DH_PRIVFETCHSZ];
   key_packdef *kp;
   dh_priv dp;
-  const char *e;
   int rc;
 
   kp = key_fetchinit(dh_privfetch, kps, &dp);
   if ((rc = key_unpack(kp, kd, t)) != 0) {
-    e = key_strerror(rc);
-    goto done;
+    a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
+    goto fail_0;
   }
   *g = group_prime(&dp.dp);
   *x = MP_COPY(dp.x);
-  e = 0;
+  rc = 0;
+  goto done;
+fail_0:
+  rc = -1;
 done:
   key_fetchdone(kp);
-  return (e);
+  return (rc);
 }
 
-static const char *kgdh_pub(key_data *kd, group **g, ge **p, dstr *t)
+static int kgdh_pub(key_data *kd, group **g, ge **p, dstr *t, dstr *e)
 {
   key_packstruct kps[DH_PUBFETCHSZ];
   key_packdef *kp;
   dh_pub dp;
-  const char *e;
   int rc;
 
   kp = key_fetchinit(dh_pubfetch, kps, &dp);
   if ((rc = key_unpack(kp, kd, t)) != 0) {
-    e = key_strerror(rc);
-    goto done;
+    a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
+    goto fail_0;
   }
   *g = group_prime(&dp.dp);
   *p = G_CREATE(*g);
   if (G_FROMINT(*g, *p, dp.y)) {
-    e = "bad public value";
-    goto done;
+    a_format(e, "bad-public-vector", A_END);
+    goto fail_1;
   }
-  e = 0;
+  rc = 0;
+  goto done;
+fail_1:
+  G_DESTROY(*g, *p);
+  G_DESTROYGROUP(*g);
+fail_0:
+  rc = -1;
 done:
   key_fetchdone(kp);
-  return (e);
+  return (rc);
 }
 
-static const kgops kgdh_ops = { "tripe-dh", kgdh_priv, kgdh_pub };
+static const kgops kgdh_ops = { "dh", kgdh_priv, kgdh_pub };
 
 /* --- Elliptic curve --- */
 
-static const char *kgec_priv(key_data *kd, group **g, mp **x, dstr *t)
+static int kgec_priv(key_data *kd, group **g, mp **x, dstr *t, dstr *e)
 {
   key_packstruct kps[EC_PRIVFETCHSZ];
   key_packdef *kp;
   ec_priv ep;
   ec_info ei;
-  const char *e;
+  const char *err;
   int rc;
 
   kp = key_fetchinit(ec_privfetch, kps, &ep);
   if ((rc = key_unpack(kp, kd, t)) != 0) {
-    e = key_strerror(rc);
-    goto done;
+    a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
+    goto fail_0;
+  }
+  if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
+    a_format(e, "decode-failed", "%s", err, A_END);
+    goto fail_0;
   }
-  if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
-    goto done;
   *g = group_ec(&ei);
   *x = MP_COPY(ep.x);
-  e = 0;
+  rc = 0;
+  goto done;
+fail_0:
+  rc = -1;
 done:
   key_fetchdone(kp);
-  return (e);
+  return (rc);
 }
 
-static const char *kgec_pub(key_data *kd, group **g, ge **p, dstr *t)
+static int kgec_pub(key_data *kd, group **g, ge **p, dstr *t, dstr *e)
 {
   key_packstruct kps[EC_PUBFETCHSZ];
   key_packdef *kp;
   ec_pub ep;
   ec_info ei;
-  const char *e;
+  const char *err;
   int rc;
 
   kp = key_fetchinit(ec_pubfetch, kps, &ep);
   if ((rc = key_unpack(kp, kd, t)) != 0) {
-    e = key_strerror(rc);
-    goto done;
+    a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
+    goto fail_0;
+  }
+  if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
+    a_format(e, "decode-failed", "%s", err, A_END);
+    goto fail_0;
   }
-  if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
-    goto done;
   *g = group_ec(&ei);
   *p = G_CREATE(*g);
   if (G_FROMEC(*g, *p, &ep.p)) {
-    e = "bad public point";
-    goto done;
+    a_format(e, "bad-public-vector", A_END);
+    goto fail_1;
   }
-  e = 0;
+  rc = 0;
+  goto done;
+fail_1:
+  G_DESTROY(*g, *p);
+  G_DESTROYGROUP(*g);
+fail_0:
+  rc = -1;
 done:
   key_fetchdone(kp);
-  return (e);
+  return (rc);
 }
 
-static const kgops kgec_ops = { "tripe-ec", kgec_priv, kgec_pub };
+static const kgops kgec_ops = { "ec", kgec_priv, kgec_pub };
 
 /* --- Table of supported key types --- */
 
@@ -165,76 +185,98 @@ static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
 /* --- @algs_get@ --- *
  *
  * Arguments:  @algswitch *a@ = where to put the algorithms
- *             @key_file *kf@ = key file (for some stupid reason)
+ *             @dstr *e@ = where to write errror tokens
+ *             @key_file *kf@ = key file
  *             @key *k@ = key to inspect
  *
- * Returns:    Null if OK, or an error message.
+ * Returns:    Zero if OK; nonzero on error.
  *
  * Use:                Extracts an algorithm choice from a key.
  */
 
-static const char *algs_get(algswitch *a, key_file *kf, key *k)
+static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
 {
   const char *p;
-  char *q;
+  char *q, *qq;
   dstr d = DSTR_INIT;
-  const char *e;
+  int rc = -1;
+
+  /* --- Symmetric encryption for bulk data --- */
+
+  if ((p = key_getattr(kf, k, "cipher")) == 0) p = "blowfish-cbc";
+  if ((a->c = gcipher_byname(p)) == 0) {
+    a_format(e, "unknown-cipher", "%s", p, A_END);
+    goto done;
+  }
 
-#define FAIL(msg) do { e = msg; goto done; } while (0)
+  /* --- Hash function --- */
 
-  if ((p = key_getattr(kf, k, "cipher")) == 0)
-    p = "blowfish-cbc";
-  if ((a->c = gcipher_byname(p)) == 0)
-    FAIL("unknown-cipher");
+  if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160";
+  if ((a->h = ghash_byname(p)) == 0) {
+    a_format(e, "unknown-hash", "%s", p, A_END);
+    goto done;
+  }
 
-  if ((p = key_getattr(kf, k, "hash")) == 0)
-    p = "rmd160";
-  if ((a->h = ghash_byname(p)) == 0)
-    FAIL("unknown-hash");
+  /* --- Symmetric encryption for key derivation --- */
 
   if ((p = key_getattr(kf, k, "mgf")) == 0) {
     dstr_reset(&d);
     dstr_putf(&d, "%s-mgf", a->h->name);
     p = d.buf;
   }
-  if ((a->mgf = gcipher_byname(p)) == 0)
-    FAIL("unknown-mgf-cipher");
+  if ((a->mgf = gcipher_byname(p)) == 0) {
+    a_format(e, "unknown-mgf-cipher", "%s", p, A_END);
+    goto done;
+  }
+
+  /* --- Message authentication for bulk data --- */
 
   if ((p = key_getattr(kf, k, "mac")) != 0) {
     dstr_reset(&d);
     dstr_puts(&d, p);
     if ((q = strchr(d.buf, '/')) != 0)
       *q++ = 0;
-    if ((a->m = gmac_byname(d.buf)) == 0)
-      FAIL("unknown-mac");
+    if ((a->m = gmac_byname(d.buf)) == 0) {
+      a_format(e, "unknown-mac", "%s", d.buf, A_END);
+      goto done;
+    }
     if (!q)
       a->tagsz = a->m->hashsz;
     else {
-      unsigned long n = strtoul(q, &q, 0);
-      if (*q) FAIL("bad-tag-length-string");
-      if (n%8 || n > ~(size_t)0) FAIL("bad-tag-length");
+      unsigned long n = strtoul(q, &qq, 0);
+      if (*qq)  {
+       a_format(e, "bad-tag-length-string", "%s", q, A_END);
+       goto done;
+      }
+      if (n%8 || n/8 > a->m->hashsz) {
+       a_format(e, "bad-tag-length", "%lu", n, A_END);
+       goto done;
+      }
       a->tagsz = n/8;
     }
   } else {
     dstr_reset(&d);
     dstr_putf(&d, "%s-hmac", a->h->name);
-    if ((a->m = gmac_byname(d.buf)) == 0)
-      FAIL("no-hmac-for-hash");
+    if ((a->m = gmac_byname(d.buf)) == 0) {
+      a_format(e, "no-hmac-for-hash", "%s", a->h->name, A_END);
+      goto done;
+    }
     a->tagsz = a->h->hashsz/2;
   }
 
-  e = 0;
+  rc = 0;
 done:
   dstr_destroy(&d);
-  return (e);
+  return (rc);
 }
 
 /* --- @algs_check@ --- *
  *
  * Arguments:  @algswitch *a@ = a choice of algorithms
+ *             @dstr *e@ = where to write error tokens
  *             @const group *g@ = the group we're working in
  *
- * Returns:    Null if OK, or an error message.
+ * Returns:    Zero if OK; nonzero on error.
  *
  * Use:                Checks an algorithm choice for sensibleness.  This also
  *             derives some useful information from the choices, and you
@@ -242,28 +284,42 @@ done:
  *             for use by @keyset@ functions.
  */
 
-static const char *algs_check(algswitch *a, const group *g)
+static int algs_check(algswitch *a, dstr *e, const group *g)
 {
   /* --- Derive the key sizes --- *
    *
    * Must ensure that we have non-empty keys.  This isn't ideal, but it
-   * provides a handy sanity check.
+   * provides a handy sanity check.  Also must be based on a 64- or 128-bit
+   * block cipher or we can't do the data expiry properly.
    */
 
   a->hashsz = a->h->hashsz;
-  if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0)
-    return ("no key size found for cipher");
-  if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0)
-    return ("no key size found for MAC");
+  if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0) {
+    a_format(e, "cipher", "%s", a->c->name,
+            "no-key-size", "%lu", (unsigned long)a->hashsz,
+            A_END);
+    return (-1);
+  }
+  if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0) {
+    a_format(e, "mac", "%s", a->m->name,
+            "no-key-size", "%lu", (unsigned long)a->hashsz,
+            A_END);
+    return (-1);
+  }
 
-  /* --- Ensure that the tag size is sane --- */
+  /* --- Derive the data limit --- */
 
-  if (a->tagsz > a->m->hashsz) return ("tag length too large");
+  if (a->c->blksz < 16) a->expsz = MEG(64);
+  else a->expsz = MEG(2048);
 
   /* --- Ensure the MGF accepts hashes as keys --- */
 
-  if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz)
-    return ("MGF not suitable -- restrictive key schedule");
+  if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) {
+    a_format(e, "mgf", "%s", a->mgf->name,
+            "restrictive-key-schedule",
+            A_END);
+    return (-1);
+  }
 
   /* --- All ship-shape and Bristol-fashion --- */
 
@@ -293,7 +349,7 @@ static int algs_samep(const algswitch *a, const algswitch *aa)
  * Arguments:  @const char *file@ = name of the file
  *             @int line@ = line number in file
  *             @const char *msg@ = error message
- *             @void *p@ = argument pointer
+ *             @void *p@ = argument pointer (indicates which keyring)
  *
  * Returns:    ---
  *
@@ -302,11 +358,50 @@ static int algs_samep(const algswitch *a, const algswitch *aa)
 
 static void keymoan(const char *file, int line, const char *msg, void *p)
 {
-  a_warn("KEYMGMT",
-        "key-file-error",
-        "%s:%i", file, line,
-        "%s", msg,
-        A_END);
+  const char *kind = p;
+
+  if (!line) {
+    a_warn("KEYMGMT", "%s-keyring", kind, "%s", file,
+          "io-error", "?ERRNO", A_END);
+  } else {
+    a_warn("KEYMGMT", "%s-keyring", kind, "%s", file, "line", "%d", line,
+          "%s", msg, A_END);
+  }
+}
+
+/* --- @keykg@ --- *
+ *
+ * Arguments:  @key_file *kf@ = pointer to key file
+ *             @key *k@ = pointer to key
+ *             @const char **tyr@ = where to put the type string
+ *
+ * Returns:    Pointer to indicated key-group options, or null.
+ *
+ * Use:                Looks up a key's group indicator and tries to find a matching
+ *             table entry.
+ */
+
+static const kgops *keykg(key_file *kf, key *k, const char **tyr)
+{
+  const char *ty;
+  const kgops **ko;
+
+  /* --- Look up the key type in the table --- *
+   *
+   * There are several places to look for this.  The most obvious is the
+   * `kx-group' key attribute.  But there's also the key type itself.
+   */
+
+  ty = key_getattr(kf, k, "kx-group");
+  if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
+  if (!ty) ty = "dh";
+  if (tyr) *tyr = ty;
+
+  for (ko = kgtab; *ko; ko++) {
+    if (strcmp((*ko)->ty, ty) == 0)
+      return (*ko);
+  }
+  return (0);
 }
 
 /* --- @loadpriv@ --- *
@@ -318,65 +413,70 @@ static void keymoan(const char *file, int line, const char *msg, void *p)
  * Use:                Loads the private key from its keyfile.
  */
 
-static int loadpriv(dstr *d)
+static int loadpriv(void)
 {
   key_file kf;
   key *k;
   key_data **kd;
   dstr t = DSTR_INIT;
+  dstr e = DSTR_INIT;
   group *g = 0;
   mp *x = 0;
   int rc = -1;
-  const kgops **ko;
-  const char *e;
+  const kgops *ko;
+  const char *err, *tag, *ty;
   algswitch a;
 
   /* --- Open the private key file --- */
 
-  if (key_open(&kf, kr_priv, KOPEN_READ, keymoan, 0)) {
-    dstr_putf(d, "error reading private keyring `%s': %s",
-             kr_priv, strerror(errno));
+  if (key_open(&kf, kr_priv, KOPEN_READ, keymoan, "private"))
     goto done_0;
-  }
 
   /* --- Find the private key --- */
 
-  if (key_qtag(&kf, tag_priv, &t, &k, &kd)) {
-    dstr_putf(d, "private key `%s' not found in keyring `%s'",
-             tag_priv, kr_priv);
+  if (tag_priv ?
+      key_qtag(&kf, tag = tag_priv, &t, &k, &kd) :
+      key_qtag(&kf, tag = "tripe", &t, &k, &kd) &&
+       key_qtag(&kf, tag = "tripe-dh", &t, &k, &kd)) {
+    a_warn("KEYMGMT", "private-keyring", "%s", kr_priv,
+          "key-not-found", "%s", tag, A_END);
     goto done_1;
   }
 
   /* --- Look up the key type in the table --- */
 
-  for (ko = kgtab; *ko; ko++) {
-    if (strcmp((*ko)->ty, k->type) == 0)
-      goto tymatch;
+  if ((ko = keykg(&kf, k, &ty)) == 0) {
+    a_warn("KEYMGMT", "private-keyring",
+          "%s", kr_priv, "key", "%s", t.buf,
+          "unknown-group-type", "%s", ty, A_END);
+    goto done_1;
   }
-  dstr_putf(d, "private key `%s' has unknown type `%s'", t.buf, k->type);
-  goto done_1;
-tymatch:;
 
   /* --- Load the key --- */
 
-  if ((e = (*ko)->loadpriv(*kd, &g, &x, &t)) != 0) {
-    dstr_putf(d, "error reading private key `%s': %s", t.buf, e);
+  if (ko->loadpriv(*kd, &g, &x, &t, &e)) {
+    a_warn("KEYMGMT", "private-keyring",
+          "%s", kr_priv, "key", "%s", t.buf,
+          "*%s", e.buf, A_END);
     goto done_1;
   }
 
   /* --- Check that the key is sensible --- */
 
-  if ((e = G_CHECK(g, &rand_global)) != 0) {
-    dstr_putf(d, "bad group in private key `%s': %s", t.buf, e);
+  if ((err = G_CHECK(g, &rand_global)) != 0) {
+    a_warn("KEYMGMT", "private-keyring",
+          "%s", kr_priv, "key", "%s", t.buf,
+          "bad-group", "%s", err, A_END);
     goto done_1;
   }
 
   /* --- Collect the algorithms --- */
 
-  if ((e = algs_get(&a, &kf, k)) != 0 ||
-      (e = algs_check(&a, g)) != 0) {
-    dstr_putf(d, "bad symmetric algorithm selection in private key `%s': %s",
-             t.buf, e);
+  if (algs_get(&a, &e, &kf, k) ||
+      algs_check(&a, &e, g)) {
+    a_warn("KEYMGMT", "private-keyring",
+          "%s", kr_priv, "key", "%s", t.buf,
+          "*%s", e.buf, A_END);
     goto done_1;
   }
 
@@ -390,7 +490,9 @@ tymatch:;
 
   if (gg) {
     if (!group_samep(g, gg)) {
-      dstr_putf(d, "private key `%s' has different group", t.buf);
+      a_warn("KEYMGMT", "private-keyring",
+            "%s", kr_priv, "key", "%s", t.buf,
+            "changed-group", A_END);
       goto done_1;
     }
     G_DESTROYGROUP(gg);
@@ -433,6 +535,7 @@ done_1:
   key_close(&kf);
 done_0:
   dstr_destroy(&t);
+  dstr_destroy(&e);
   if (x) mp_drop(x);
   if (g) G_DESTROYGROUP(g);
   return (rc);
@@ -447,13 +550,11 @@ done_0:
  * Use:                Reloads the public keyring.
  */
 
-static int loadpub(dstr *d)
+static int loadpub(void)
 {
   key_file *kf = CREATE(key_file);
 
-  if (key_open(kf, kr_pub, KOPEN_READ, keymoan, 0)) {
-    dstr_putf(d, "error reading public keyring `%s': %s",
-             kr_pub, strerror(errno));
+  if (key_open(kf, kr_pub, KOPEN_READ, keymoan, "public")) {
     DESTROY(kf);
     return (-1);
   }
@@ -473,7 +574,6 @@ static int loadpub(dstr *d)
 
 int km_reload(void)
 {
-  dstr d = DSTR_INIT;
   key_file *kf;
   int reload = 0;
 
@@ -481,10 +581,7 @@ int km_reload(void)
 
   if (fwatch_update(&w_priv, kr_priv)) {
     T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
-    DRESET(&d);
-    if (loadpriv(&d))
-      a_warn("KEYMGMT", "bad-private-key", "%s", d.buf, A_END);
-    else
+    if (!loadpriv())
       reload = 1;
   }
 
@@ -493,10 +590,7 @@ int km_reload(void)
   if (fwatch_update(&w_pub, kr_pub)) {
     T( trace(T_KEYMGMT, "keymgmt: public keyring updated: reloading..."); )
     kf = kf_pub;
-    DRESET(&d);
-    if (loadpub(&d))
-      a_warn("KEYMGMT", "bad-public-keyring", "%s", d.buf, A_END);
-    else {
+    if (!loadpub()) {
       reload = 1;
       key_close(kf);
       DESTROY(kf);
@@ -521,7 +615,6 @@ int km_reload(void)
 
 void km_init(const char *priv, const char *pub, const char *tag)
 {
-  dstr d = DSTR_INIT;
   const gchash *const *hh;
 
   kr_priv = priv;
@@ -537,11 +630,8 @@ void km_init(const char *priv, const char *pub, const char *tag)
     }
   }
 
-  DRESET(&d);
-  if (loadpriv(&d))
-    die(EXIT_FAILURE, "%s", d.buf);
-  if (loadpub(&d))
-    die(EXIT_FAILURE, "%s", d.buf);
+  if (loadpriv() || loadpub())
+    exit(EXIT_FAILURE);
 }
 
 /* --- @km_getpubkey@ --- *
@@ -560,8 +650,9 @@ int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
   key *k;
   key_data **kd;
   dstr t = DSTR_INIT;
-  const kgops **ko;
-  const char *e;
+  dstr e = DSTR_INIT;
+  const kgops *ko;
+  const char *ty;
   group *g = 0;
   ge *p = 0;
   algswitch a;
@@ -570,27 +661,26 @@ int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
   /* --- Find the key --- */
 
   if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
-    a_warn("KEYMGMT", "public-key", "%s", tag, "not-found", A_END);
+    a_warn("KEYMGMT", "public-keyring", "%s", kr_pub,
+          "key-not-found", "%s", tag, A_END);
     goto done;
   }
 
   /* --- Look up the key type in the table --- */
 
-  for (ko = kgtab; *ko; ko++) {
-    if (strcmp((*ko)->ty, k->type) == 0)
-      goto tymatch;
+  if ((ko = keykg(kf_pub, k, &ty)) == 0) {
+    a_warn("KEYMGMT", "public-keyring",
+          "%s", kr_pub, "key", "%s", t.buf,
+          "unknown-group-type", "%s", ty, A_END);
+    goto done;
   }
-  a_warn("KEYMGMT",
-        "public-key", "%s", t.buf,
-        "unknown-type", "%s", k->type,
-        A_END);
-  goto done;
-tymatch:;
 
   /* --- Load the key --- */
 
-  if ((e = (*ko)->loadpub(*kd, &g, &p, &t)) != 0) {
-    a_warn("KEYMGMT", "public-key", "%s", t.buf, "bad", "%s", e, A_END);
+  if (ko->loadpub(*kd, &g, &p, &t, &e)) {
+    a_warn("KEYMGMT", "public-keyring",
+          "%s", kr_pub, "key", "%s", t.buf,
+          "*%s", e.buf, A_END);
     goto done;
   }
 
@@ -601,15 +691,17 @@ tymatch:;
    */
 
   if (!group_samep(gg, g)) {
-    a_warn("KEYMGMT", "public-key", "%s", t.buf, "incorrect-group", A_END);
+    a_warn("KEYMGMT", "public-keyring",
+          "%s", kr_pub, "key", "%s", t.buf,
+          "*%s", e.buf, A_END);
     goto done;
   }
 
   /* --- Check the public group element --- */
 
   if (group_check(gg, p)) {
-    a_warn("KEYMGMT",
-          "public-key", "%s", t.buf,
+    a_warn("KEYMGMT", "public-keyring",
+          "%s", kr_pub, "key", "%s", t.buf,
           "bad-public-group-element",
           A_END);
     goto done;
@@ -617,18 +709,16 @@ tymatch:;
 
   /* --- Check the algorithms --- */
 
-  if ((e = algs_get(&a, kf_pub, k)) != 0) {
-    a_warn("KEYMGMT",
-          "public-key", "%s", t.buf,
-          "bad-algorithm-selection", e,
-          A_END);
+  if (algs_get(&a, &e, kf_pub, k)) {
+    a_warn("KEYMGMT", "public-keyring",
+          "%s", kr_pub, "key", "%s", t.buf,
+          "*%s", e.buf, A_END);
     goto done;
   }
   if (!algs_samep(&a, &algs)) {
-    a_warn("KEYMGMT",
-          "public-key", "%s", t.buf,
-          "algorithm-mismatch",
-          A_END);
+    a_warn("KEYMGMT", "public-keyring",
+          "%s", kr_pub, "key", "%s", t.buf,
+          "algorithm-mismatch", A_END);
     goto done;
   }
 
@@ -651,6 +741,7 @@ done:
   if (p) G_DESTROY(g, p);
   if (g) G_DESTROYGROUP(g);
   dstr_destroy(&t);
+  dstr_destroy(&e);
   return (rc);
 }