chiark / gitweb /
admin: Initialize reference counter for client blocks.
[tripe] / keymgmt.c
index a10971344fdf6356404d08c1ee975dcb34c4dc1d..7163ddda5cd09720af6e4c15365ba0e81898972f 100644 (file)
--- a/keymgmt.c
+++ b/keymgmt.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: keymgmt.c,v 1.4 2004/04/03 12:35:13 mdw Exp $
+ * $Id$
  *
  * Key loading and storing
  *
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: keymgmt.c,v $
- * Revision 1.4  2004/04/03 12:35:13  mdw
- * Support elliptic curve key exchange.
- *
- * Revision 1.3  2001/06/22 19:40:36  mdw
- * Support expiry of other peers' public keys.
- *
- * Revision 1.2  2001/06/19 22:07:09  mdw
- * Cosmetic fixes.
- *
- * Revision 1.1  2001/02/03 20:26:37  mdw
- * Initial checkin.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "tripe.h"
@@ -51,6 +34,7 @@
 
 group *gg;
 mp *kpriv;
+algswitch algs;
 
 /*----- Static variables --------------------------------------------------*/
 
@@ -176,6 +160,132 @@ static const kgops kgec_ops = { "tripe-ec", kgec_priv, kgec_pub };
 
 static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
 
+/*----- Algswitch stuff ---------------------------------------------------*/
+
+/* --- @algs_get@ --- *
+ *
+ * Arguments:  @algswitch *a@ = where to put the algorithms
+ *             @key_file *kf@ = key file (for some stupid reason)
+ *             @key *k@ = key to inspect
+ *
+ * Returns:    Null if OK, or an error message.
+ *
+ * Use:                Extracts an algorithm choice from a key.
+ */
+
+static const char *algs_get(algswitch *a, key_file *kf, key *k)
+{
+  const char *p;
+  char *q;
+  dstr d = DSTR_INIT;
+  const char *e;
+
+#define FAIL(msg) do { e = msg; goto done; } while (0)
+
+  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)
+    FAIL("unknown-hash");
+
+  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 ((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 (!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");
+      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");
+    a->tagsz = a->h->hashsz/2;
+  }
+
+  e = 0;
+done:
+  dstr_destroy(&d);
+  return (e);
+}
+
+/* --- @algs_check@ --- *
+ *
+ * Arguments:  @algswitch *a@ = a choice of algorithms
+ *             @const group *g@ = the group we're working in
+ *
+ * Returns:    Null if OK, or an error message.
+ *
+ * Use:                Checks an algorithm choice for sensibleness.  This also
+ *             derives some useful information from the choices, and you
+ *             must call this before committing the algorithm selection
+ *             for use by @keyset@ functions.
+ */
+
+static const char *algs_check(algswitch *a, 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.
+   */
+
+  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");
+
+  /* --- Ensure that the tag size is sane --- */
+
+  if (a->tagsz > a->m->hashsz) return ("tag length too large");
+
+  /* --- Ensure the MGF accepts hashes as keys --- */
+
+  if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz)
+    return ("MGF not suitable -- restrictive key schedule");
+
+  /* --- All ship-shape and Bristol-fashion --- */
+
+  return (0);
+}
+
+/* --- @algs_samep@ --- *
+ *
+ * Arguments:  @const algswitch *a, *aa@ = two algorithm selections
+ *
+ * Returns:    Nonzero if the two selections are the same.
+ *
+ * Use:                Checks sameness of algorithm selections: used to ensure that
+ *             peers are using sensible algorithms.
+ */
+
+static int algs_samep(const algswitch *a, const algswitch *aa)
+{
+  return (a->c == aa->c && a->mgf == aa->mgf && a->h == aa->h &&
+         a->m == aa->m && a->tagsz == aa->tagsz);
+}
+
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @keymoan@ --- *
@@ -191,7 +301,13 @@ static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
  */
 
 static void keymoan(const char *file, int line, const char *msg, void *p)
-  { a_warn("%s:%i: error: %s", file, line, msg); }
+{
+  a_warn("KEYMGMT",
+        "key-file-error",
+        "%s:%i", file, line,
+        "%s", msg,
+        A_END);
+}
 
 /* --- @loadpriv@ --- *
  *
@@ -206,13 +322,14 @@ static int loadpriv(dstr *d)
 {
   key_file kf;
   key *k;
-  key_data *kd;
+  key_data **kd;
   dstr t = DSTR_INIT;
   group *g = 0;
   mp *x = 0;
   int rc = -1;
   const kgops **ko;
   const char *e;
+  algswitch a;
 
   /* --- Open the private key file --- */
 
@@ -242,7 +359,7 @@ tymatch:;
 
   /* --- Load the key --- */
 
-  if ((e = (*ko)->loadpriv(kd, &g, &x, &t)) != 0) {
+  if ((e = (*ko)->loadpriv(*kd, &g, &x, &t)) != 0) {
     dstr_putf(d, "error reading private key `%s': %s", t.buf, e);
     goto done_1;
   }
@@ -254,6 +371,15 @@ tymatch:;
     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);
+    goto done_1;
+  }
+
   /* --- Good, we're happy --- *
    *
    * Dodginess!  We change the group over here, but don't free any old group
@@ -280,12 +406,18 @@ tymatch:;
       trace(T_CRYPTO, "crypto: r = %s", mpstr(g->r));
       trace(T_CRYPTO, "crypto: h = %s", mpstr(g->h));
       trace(T_CRYPTO, "crypto: x = %s", mpstr(x));
+      trace(T_CRYPTO, "crypto: cipher = %s", a.c->name);
+      trace(T_CRYPTO, "crypto: mgf = %s", a.mgf->name);
+      trace(T_CRYPTO, "crypto: hash = %s", a.h->name);
+      trace(T_CRYPTO, "crypto: mac = %s/%lu",
+           a.m->name, (unsigned long)a.tagsz * 8);
     })
   })
 
   /* --- Success! --- */
 
   gg = g; g = 0;
+  algs = a;
   kpriv = x; x = 0;
   rc = 0;
 
@@ -324,16 +456,16 @@ static int loadpub(dstr *d)
   return (0);
 }
 
-/* --- @km_interval@ --- *
+/* --- @km_reload@ --- *
  *
  * Arguments:  ---
  *
  * Returns:    Zero if OK, nonzero to force reloading of keys.
  *
- * Use:                Called on the interval timer to perform various useful jobs.
+ * Use:                Checks the keyrings to see if they need reloading.
  */
 
-int km_interval(void)
+int km_reload(void)
 {
   dstr d = DSTR_INIT;
   key_file *kf;
@@ -345,7 +477,7 @@ int km_interval(void)
     T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
     DRESET(&d);
     if (loadpriv(&d))
-      a_warn("%s -- ignoring changes", d.buf);
+      a_warn("KEYMGMT", "bad-private-key", "%s", d.buf, A_END);
     else
       reload = 1;
   }
@@ -357,7 +489,7 @@ int km_interval(void)
     kf = kf_pub;
     DRESET(&d);
     if (loadpub(&d))
-      a_warn("%s -- ignoring changes", d.buf);
+      a_warn("KEYMGMT", "bad-public-keyring", "%s", d.buf, A_END);
     else {
       reload = 1;
       key_close(kf);
@@ -384,6 +516,7 @@ int km_interval(void)
 void km_init(const char *priv, const char *pub, const char *tag)
 {
   dstr d = DSTR_INIT;
+  const gchash *const *hh;
 
   kr_priv = priv;
   kr_pub = pub;
@@ -391,6 +524,13 @@ void km_init(const char *priv, const char *pub, const char *tag)
   fwatch_init(&w_priv, kr_priv);
   fwatch_init(&w_pub, kr_pub);
 
+  for (hh = ghashtab; *hh; hh++) {
+    if ((*hh)->hashsz > MAXHASHSZ) {
+      die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
+         (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ);
+    }
+  }
+
   DRESET(&d);
   if (loadpriv(&d))
     die(EXIT_FAILURE, "%s", d.buf);
@@ -412,18 +552,19 @@ void km_init(const char *priv, const char *pub, const char *tag)
 int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
 {
   key *k;
-  key_data *kd;
+  key_data **kd;
   dstr t = DSTR_INIT;
   const kgops **ko;
   const char *e;
   group *g = 0;
   ge *p = 0;
+  algswitch a;
   int rc = -1;
 
   /* --- Find the key --- */
 
   if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
-    a_warn("private key `%s' not found in keyring `%s'", tag_priv, kr_priv);
+    a_warn("KEYMGMT", "public-key", "%s", tag, "not-found", A_END);
     goto done;
   }
 
@@ -433,14 +574,17 @@ int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
     if (strcmp((*ko)->ty, k->type) == 0)
       goto tymatch;
   }
-  a_warn("public key `%s' has unknown type `%s'", t.buf, k->type);
+  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("error reading public key `%s': %s", t.buf, e);
+  if ((e = (*ko)->loadpub(*kd, &g, &p, &t)) != 0) {
+    a_warn("KEYMGMT", "public-key", "%s", t.buf, "bad", "%s", e, A_END);
     goto done;
   }
 
@@ -451,14 +595,34 @@ tymatch:;
    */
 
   if (!group_samep(gg, g)) {
-    a_warn("public key `%s' has incorrect group", t.buf);
+    a_warn("KEYMGMT", "public-key", "%s", t.buf, "incorrect-group", A_END);
     goto done;
   }
 
   /* --- Check the public group element --- */
 
   if (group_check(gg, p)) {
-    a_warn("public key `%s' has bad public group element", t.buf);
+    a_warn("KEYMGMT",
+          "public-key", "%s", t.buf,
+          "bad-public-group-element",
+          A_END);
+    goto done;
+  }
+
+  /* --- 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);
+    goto done;
+  }
+  if (!algs_samep(&a, &algs)) {
+    a_warn("KEYMGMT",
+          "public-key", "%s", t.buf,
+          "algorithm-mismatch",
+          A_END);
     goto done;
   }