chiark / gitweb /
Upgrade licence to GPLv3+.
[tripe] / server / keymgmt.c
index 17325dce2d96e7ca78f029a4fd126d6344c865d9..e0861069aacda0333ed4449588fb356550ce2401 100644 (file)
  *
  * This file is part of Trivial IP Encryption (TrIPE).
  *
- * TrIPE is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * TrIPE is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your
+ * option) any later version.
  *
- * TrIPE is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * TrIPE is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with TrIPE; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with TrIPE.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 /*----- Header files ------------------------------------------------------*/
 
 #include "tripe.h"
 
-/*----- Key groups --------------------------------------------------------*/
-
-/* The key-loading functions here must fill in the kdata slot @g@ and
- * either @kpriv@ or @kpub@ as appropriate.  The caller will take care of
- * determining @kpub@ given a private key, and of ensuring that @kpriv@ is
- * null for a public key.
- */
-
-typedef struct kgops {
-  const char *ty;
-  int (*loadpriv)(key_data *, kdata *, dstr *, dstr *);
-  int (*loadpub)(key_data *, kdata *, dstr *, dstr *);
-} kgops;
-
-/* --- @KLOAD@ --- *
- *
- * Arguments:  @ty@, @TY@ = key type name (lower- and upper-case)
- *             @which@, @WHICH@ = `pub' or `priv' (and upper-case)
- *             @setgroup@ = code to initialize @kd->g@
- *             @setpriv@ = code to initialize @kd->kpriv@
- *             @setpub@ = code to initialize @kd->kpub@
- *
- * Use:                Generates the body of one of the (rather tedious) key loading
- *             functions.  See the description of @KEYTYPES@ below for the
- *             details.
- */
-
-#define KLOAD(ty, TY, which, WHICH, setgroup, setpriv, setpub)         \
-static int kg##ty##_##which(key_data *d, kdata *kd, dstr *t, dstr *e)  \
-{                                                                      \
-  key_packstruct kps[TY##_##WHICH##FETCHSZ];                           \
-  key_packdef *kp;                                                     \
-  ty##_##which p;                                                      \
-  int rc;                                                              \
-                                                                       \
-  /* --- Initialize things we've not set up yet --- */                 \
-                                                                       \
-  kd->g = 0; kd->kpub = 0;                                             \
-                                                                       \
-  /* --- Unpack the key --- */                                         \
-                                                                       \
-  kp = key_fetchinit(ty##_##which##fetch, kps, &p);                    \
-  if ((rc = key_unpack(kp, d, t)) != 0) {                              \
-    a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);       \
-    goto fail;                                                         \
-  }                                                                    \
-                                                                       \
-  /* --- Extract the pieces of the key --- */                          \
-                                                                       \
-  setgroup;                                                            \
-  setpriv;                                                             \
-  kd->kpub = G_CREATE(kd->g);                                          \
-  setpub;                                                              \
-                                                                       \
-  /* --- We win --- */                                                 \
-                                                                       \
-  rc = 0;                                                              \
-  goto done;                                                           \
-                                                                       \
-fail:                                                                  \
-  if (kd->kpub) G_DESTROY(kd->g, kd->kpub);                            \
-  if (kd->g) G_DESTROYGROUP(kd->g);                                    \
-  rc = -1;                                                             \
-                                                                       \
-done:                                                                  \
-  key_fetchdone(kp);                                                   \
-  return (rc);                                                         \
-}
-
-/* --- @KEYTYPES@ --- *
- *
- * A list of the various key types, and how to unpack them.  Each entry in
- * the list has the form
- *
- *     _(ty, TY, setgroup, setpriv, setpub)
- *
- * The @ty@ and @TY@ are lower- and upper-case versions of the key type name,
- * and there should be @key_fetchdef@s called @ty_{priv,pub}fetch@.
- *
- * The @setgroup@, @setpriv@ and @setpub@ items are code fragments which are
- * passed to @KLOAD@ to build appropriate key-loading methods.  By the time
- * these code fragments are run, the key has been unpacked from the incoming
- * key data using @ty_whichfetch@ into a @ty_which@ structure named @p@.
- * They can report errors by writing an appropriate token sequence to @e@ and
- * jumping to @fail@.
- */
-
-#define KEYTYPES(_)                                                    \
-                                                                       \
-  /* --- Diffie-Hellman --- */                                         \
-                                                                       \
-  _(dh, DH,                                                            \
-    { kd->g = group_prime(&p.dp); },                                   \
-    { kd->kpriv = MP_COPY(p.x); },                                     \
-    { if (G_FROMINT(kd->g, kd->kpub, p.y)) {                           \
-       a_format(e, "bad-public-vector", A_END);                        \
-       goto fail;                                                      \
-      }                                                                        \
-    })                                                                 \
-                                                                       \
-  /* --- Elliptic curves --- */                                                \
-                                                                       \
-  _(ec, EC,                                                            \
-    { ec_info ei; const char *err;                                     \
-      if ((err = ec_getinfo(&ei, p.cstr)) != 0) {                      \
-       a_format(e, "decode-failed", "%s", err, A_END);                 \
-       goto fail;                                                      \
-      }                                                                        \
-      kd->g = group_ec(&ei);                                           \
-    },                                                                 \
-    { kd->kpriv = MP_COPY(p.x); },                                     \
-    { if (G_FROMEC(kd->g, kd->kpub, &p.p)) {                           \
-       a_format(e, "bad-public-vector", A_END);                        \
-       goto fail;                                                      \
-      }                                                                        \
-    })
-
-#define KEYTYPE_DEF(ty, TY, setgroup, setpriv, setpub)                 \
-  KLOAD(ty, TY, priv, PRIV, setgroup, setpriv,                         \
-       { G_EXP(kd->g, kd->kpub, kd->g->g, kd->kpriv); })               \
-  KLOAD(ty, TY, pub, PUB, setgroup, { }, setpub)                       \
-  static const kgops kg##ty##_ops = { #ty, kg##ty##_priv, kg##ty##_pub };
-KEYTYPES(KEYTYPE_DEF)
-
-/* --- Table of supported key types --- */
-
-static const kgops *kgtab[] = {
-#define KEYTYPE_ENTRY(ty, TY, setgroup, setpriv, setpub) &kg##ty##_ops,
-  KEYTYPES(KEYTYPE_ENTRY)
-#undef KEYTYPE_ENTRY
-  0
-};
-
 /*----- Algswitch stuff ---------------------------------------------------*/
 
 /* --- @algs_get@ --- *
  *
  * Arguments:  @algswitch *a@ = where to put the algorithms
- *             @dstr *e@ = where to write errror tokens
+ *             @dstr *e@ = where to write error tokens
  *             @key_file *kf@ = key file
  *             @key *k@ = key to inspect
  *
@@ -178,8 +44,7 @@ static const kgops *kgtab[] = {
 static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
 {
   const char *p;
-  const bulkcrypto *bulk;
-  char *q, *qq;
+  const bulkops *bops;
   dstr d = DSTR_INIT, dd = DSTR_INIT;
   int rc = -1;
 
@@ -206,83 +71,13 @@ static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
   /* --- Bulk crypto transform --- */
 
   if ((p = key_getattr(kf, k, "bulk")) == 0) p = "v0";
-  for (bulk = bulktab; bulk->name && strcmp(p, bulk->name) != 0; bulk++);
-  if (!bulk->name) {
+  for (bops = bulktab; bops->name && strcmp(p, bops->name) != 0; bops++);
+  if (!bops->name) {
     a_format(e, "unknown-bulk-transform", "%s", p, A_END);
     goto done;
   }
-  a->bulk = bulk;
-
-  /* --- Symmetric encryption for bulk data --- */
-
-  if (!(a->bulk->prim & BCP_CIPHER))
-    a->c = 0;
-  else {
-    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;
-    }
-  }
-
-  /* --- Block cipher for miscellaneous use --- */
-
-  if (!(a->bulk->prim & BCP_BLKC))
-    a->b = 0;
-  else {
-    if ((p = key_getattr(kf, k, "blkc")) == 0) {
-      dstr_reset(&dd);
-      dstr_puts(&dd, a->c ? a->c->name : "rijndael-");
-      if ((q = strrchr(dd.buf, '-')) != 0) *q = 0;
-      p = dd.buf;
-    }
-    dstr_reset(&d);
-    dstr_putf(&d, "%s-ecb", p);
-    if ((a->b = gcipher_byname(d.buf)) == 0) {
-      a_format(e, "unknown-blkc", "%s", p, A_END);
-      goto done;
-    }
-  }
-
-  /* --- Message authentication for bulk data --- */
-
-  if (!(a->bulk->prim & BCP_MAC)) {
-    a->m = 0;
-    a->tagsz = 0;
-  } else {
-    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) {
-       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, &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) {
-       a_format(e, "no-hmac-for-hash", "%s", a->h->name, A_END);
-       goto done;
-      }
-      a->tagsz = a->h->hashsz/2;
-    }
-  }
+  if ((a->bulk = bops->getalgs(a, e, kf, k)) == 0) goto done;
+  a->bulk->ops = bops;
 
   /* --- All done --- */
 
@@ -297,7 +92,7 @@ done:
  *
  * Arguments:  @algswitch *a@ = a choice of algorithms
  *             @dstr *e@ = where to write error tokens
- *             @const group *g@ = the group we're working in
+ *             @const dhgrp *grp@ = the group we're working in
  *
  * Returns:    Zero if OK; nonzero on error.
  *
@@ -307,45 +102,9 @@ done:
  *             for use by @keyset@ functions.
  */
 
-static int algs_check(algswitch *a, dstr *e, const group *g)
+static int algs_check(algswitch *a, dstr *e, const dhgrp *grp)
 {
-  /* --- Check the bulk crypto transform --- */
-
-  if (a->bulk->check(a, e)) return (-1);
-
-  /* --- Derive the key sizes --- *
-   *
-   * Must ensure that we have non-empty keys.  This isn't ideal, but it
-   * 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->c && (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->m && (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);
-  }
-  if (a->b && (a->bksz = keysz(a->hashsz, a->b->keysz)) == 0) {
-    a_format(e, "blkc", "%.*s", strlen(a->b->name) - 4, a->b->name,
-            "no-key-size", "%lu", (unsigned long)a->hashsz,
-            A_END);
-    return (-1);
-  }
-
-  /* --- Derive the data limit --- */
-
-  if (a->c && 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) {
     a_format(e, "mgf", "%s", a->mgf->name,
@@ -354,7 +113,7 @@ static int algs_check(algswitch *a, dstr *e, const group *g)
     return (-1);
   }
 
-  /* --- All ship-shape and Bristol-fashion --- */
+  if (a->bulk->ops->checkalgs(a->bulk, a, e)) return (-1);
 
   return (0);
 }
@@ -373,17 +132,19 @@ int km_samealgsp(const kdata *kdx, const kdata *kdy)
 {
   const algswitch *a = &kdx->algs, *aa = &kdy->algs;
 
-  return (group_samep(kdx->g, kdy->g) &&
-         a->c == aa->c && a->b == aa->b &&
+  return (kdx->grp->ops == kdy->grp->ops &&
+         kdx->grp->ops->samegrpp(kdx->grp, kdy->grp) &&
          a->mgf == aa->mgf && a->h == aa->h &&
-         a->m == aa->m && a->tagsz == aa->tagsz);
+         a->bulk->ops == aa->bulk->ops &&
+         a->bulk->ops->samealgsp(a->bulk, aa->bulk));
 }
 
 /*----- Key data and key nodes --------------------------------------------*/
 
 typedef struct keyhalf {
   const char *kind;
-  int (*load)(const kgops *, key_data *, kdata *, dstr *, dstr *);
+  int (*load)(key_file *, key *, key_data *,
+             const dhops *, kdata *, dstr *, dstr *);
   const char *kr;
   key_file *kf;
   fwatch w;
@@ -392,8 +153,10 @@ typedef struct keyhalf {
 
 /* --- @kh_loadpub@, @kh_loadpriv@ --- *
  *
- * Arguments:  @const kgops *ko@ = key-group operations for key type
- *             @key_data *d@ = key data object as stored in keyring
+ * Arguments:  @const dhops *dh@ = Diffie--Hellman operations for key type
+ *             @key_file *kf@ = key file from which the key was loaded
+ *             @key *k@ = the key object we're loading
+ *             @key_data *d@ = the key data to load
  *             @kdata *kd@ = our key-data object to fill in
  *             @dstr *t@ = the key tag name
  *             @dstr *e@ = a string to write error tokens to
@@ -401,10 +164,10 @@ typedef struct keyhalf {
  * Returns:    Zero on success, @-1@ on error.
  *
  * Use:                These functions handle the main difference between public and
- *             private key halves.  They are responsible for setting @g@,
- *             @kpriv@ and @kpub@ appropriately in all keys, handling the
- *             mismatch between the largely half-indifferent calling code
- *             and the group-specific loading functions.
+ *             private key halves.  They are responsible for setting @grp@,
+ *             @k@ and @K@ appropriately in all keys, handling the mismatch
+ *             between the largely half-indifferent calling code and the
+ *             group-specific loading functions.
  *
  *             The function @kh_loadpriv@ is also responsible for checking
  *             the group for goodness.  We don't bother checking public
@@ -413,45 +176,55 @@ typedef struct keyhalf {
  *             checked.
  */
 
-static int kh_loadpub(const kgops *ko, key_data *d, kdata *kd,
-                     dstr *t, dstr *e)
+static int kh_loadpub(key_file *kf, key *k, key_data *d,
+                     const dhops *dh, kdata *kd, dstr *t, dstr *e)
 {
   int rc;
 
-  if ((rc = ko->loadpub(d, kd, t, e)) != 0)
+  if ((rc = dh->ldpub(kf, k, d, kd, t, e)) != 0)
     goto fail_0;
-  if (group_check(kd->g, kd->kpub)) {
+  kd->grp->ops = dh;
+  if (kd->grp->ops->checkge(kd->grp, kd->K)) {
     a_format(e, "bad-public-group-element", A_END);
     goto fail_1;
   }
-  kd->kpriv = 0;
   return (0);
 
 fail_1:
-  G_DESTROY(kd->g, kd->kpub);
-  G_DESTROYGROUP(kd->g);
+  kd->grp->ops->freege(kd->grp, kd->K);
+  kd->grp->ops->freegrp(kd->grp);
 fail_0:
   return (-1);
 }
 
-static int kh_loadpriv(const kgops *ko, key_data *d, kdata *kd,
-                      dstr *t, dstr *e)
+static int kh_loadpriv(key_file *kf, key *k, key_data *d,
+                      const dhops *dh, kdata *kd, dstr *t, dstr *e)
 {
   int rc;
   const char *err;
+  dhge *K;
+  int ok;
 
-  if ((rc = ko->loadpriv(d, kd, t, e)) != 0)
+  if ((rc = dh->ldpriv(kf, k, d, kd, t, e)) != 0)
     goto fail_0;
-  if ((err = G_CHECK(kd->g, &rand_global)) != 0) {
+  kd->grp->ops = dh;
+  if ((err = kd->grp->ops->checkgrp(kd->grp)) != 0) {
     a_format(e, "bad-group", "%s", err, A_END);
     goto fail_1;
   }
+  K = kd->grp->ops->mul(kd->grp, kd->k, 0);
+  ok = kd->grp->ops->eq(kd->grp, kd->K, K);
+  kd->grp->ops->freege(kd->grp, K);
+  if (!ok) {
+    a_format(e, "incorrect-public-key", A_END);
+    goto fail_1;
+  }
   return (0);
 
 fail_1:
-  mp_drop(kd->kpriv);
-  G_DESTROY(kd->g, kd->kpub);
-  G_DESTROYGROUP(kd->g);
+  kd->grp->ops->freesc(kd->grp, kd->k);
+  kd->grp->ops->freege(kd->grp, kd->K);
+  kd->grp->ops->freegrp(kd->grp);
 fail_0:
   return (-1);
 }
@@ -563,7 +336,8 @@ static kdata *kh_load(keyhalf *kh, const char *tag, int complainp)
   key_data **d;
   kdata *kd;
   const char *ty;
-  const kgops **ko;
+  const dhops *dh;
+  T( const dhgrp *g; )
 
   /* --- Find the key and grab its tag --- */
 
@@ -586,24 +360,24 @@ static kdata *kh_load(keyhalf *kh, const char *tag, int complainp)
   if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
   if (!ty) ty = "dh";
 
-  for (ko = kgtab; *ko; ko++)
-    if (strcmp((*ko)->ty, ty) == 0) goto foundko;
+  for (dh = dhtab; dh->name; dh++)
+    if (strcmp(dh->name, ty) == 0) goto founddh;
   a_warn("KEYMGMT", "%s-keyring", kh->kind,
         "%s", kh->kr, "key", "%s", t.buf,
         "unknown-group-type", "%s", ty, A_END);
   goto fail_0;
 
-foundko:
+founddh:
   kd = CREATE(kdata);
-  if (kh->load(*ko, *d, kd, &t, &e)) {
+  if (kh->load(kh->kf, k, *d, dh, kd, &t, &e)) {
     a_warn("KEYMGMT", "%s-keyring", kh->kind,
-          "%s", kh->kr, "key" "%s", t.buf,
+          "%s", kh->kr, "key", "%s", t.buf,
           "*%s", e.buf, A_END);
     goto fail_1;
   }
 
   if (algs_get(&kd->algs, &e, kh->kf, k) ||
-      (kd->kpriv && algs_check(&kd->algs, &e, kd->g))) {
+      (kd->k && algs_check(&kd->algs, &e, kd->grp))) {
     a_warn("KEYMGMT", "%s-keyring", kh->kind,
           "%s", kh->kr, "key", "%s", t.buf,
           "*%s", e.buf, A_END);
@@ -611,7 +385,6 @@ foundko:
   }
 
   kd->tag = xstrdup(t.buf);
-  kd->indexsz = mp_octets(kd->g->r);
   kd->ref = 1;
   kd->kn = 0;
   kd->t_exp = k->exp;
@@ -619,24 +392,21 @@ foundko:
   IF_TRACING(T_KEYMGMT, {
     trace(T_KEYMGMT, "keymgmt: loaded %s key `%s'", kh->kind, t.buf);
     IF_TRACING(T_CRYPTO, {
-      trace(T_CRYPTO, "crypto: r = %s", mpstr(kd->g->r));
-      trace(T_CRYPTO, "crypto: h = %s", mpstr(kd->g->h));
-      if (kd->kpriv)
-       trace(T_CRYPTO, "crypto: x = %s", mpstr(kd->kpriv));
-      trace(T_CRYPTO, "crypto: cipher = %s", kd->algs.c->name);
-      trace(T_CRYPTO, "crypto: mgf = %s", kd->algs.mgf->name);
-      trace(T_CRYPTO, "crypto: hash = %s", kd->algs.h->name);
-      trace(T_CRYPTO, "crypto: mac = %s/%lu",
-           kd->algs.m->name, (unsigned long)kd->algs.tagsz * 8);
+      g = kd->grp;
+      g->ops->tracegrp(g);
+      if (kd->k)
+       trace(T_CRYPTO, "crypto: k = %s", g->ops->scstr(g, kd->k));
+      trace(T_CRYPTO, "crypto: K = %s", g->ops->gestr(g, kd->K));
+      kd->algs.bulk->ops->tracealgs(kd->algs.bulk);
     })
   })
 
   goto done;
 
 fail_2:
-  if (kd->kpriv) mp_drop(kd->kpriv);
-  G_DESTROY(kd->g, kd->kpub);
-  G_DESTROYGROUP(kd->g);
+  if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
+  kd->grp->ops->freege(kd->grp, kd->K);
+  kd->grp->ops->freegrp(kd->grp);
 fail_1:
   DESTROY(kd);
 fail_0:
@@ -748,7 +518,7 @@ static int kh_refresh(keyhalf *kh)
     kn->f &= ~KNF_BROKEN;
     if (kd->t_exp == kn->kd->t_exp &&
        km_samealgsp(kd, kn->kd) &&
-       G_EQ(kd->g, kd->kpub, kn->kd->kpub)) {
+       kd->grp->ops->eq(kd->grp, kd->K, kn->kd->K)) {
       T( trace(T_KEYMGMT, "keymgmt: key `%s' unchanged", SYM_NAME(kn)); )
       continue;
     }
@@ -878,10 +648,11 @@ void km_ref(kdata *kd) { kd->ref++; }
 void km_unref(kdata *kd)
 {
   if (--kd->ref) return;
-  if (kd->kpriv) mp_drop(kd->kpriv);
-  G_DESTROY(kd->g, kd->kpub);
+  if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
+  kd->grp->ops->freege(kd->grp, kd->K);
+  kd->grp->ops->freegrp(kd->grp);
   xfree(kd->tag);
-  G_DESTROYGROUP(kd->g);
+  DESTROY(kd);
 }
 
 /*----- That's all, folks -------------------------------------------------*/