chiark / gitweb /
server/keymgmt.c: Don't leak `kdata' objects.
[tripe] / server / keymgmt.c
index d28421fe977e555e3bff1d62b2f1752ad807c050..8f0d1a2ad643700dcdda9bcac93e1a0f90eaa7d7 100644 (file)
@@ -166,7 +166,7 @@ static const kgops *kgtab[] = {
 /* --- @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
  *
@@ -180,7 +180,7 @@ static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
   const char *p;
   const bulkcrypto *bulk;
   char *q, *qq;
-  dstr d = DSTR_INIT;
+  dstr d = DSTR_INIT, dd = DSTR_INIT;
   int rc = -1;
 
   /* --- Hash function --- */
@@ -225,6 +225,25 @@ static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
     }
   }
 
+  /* --- 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)) {
@@ -270,6 +289,7 @@ static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
   rc = 0;
 done:
   dstr_destroy(&d);
+  dstr_destroy(&dd);
   return (rc);
 }
 
@@ -313,6 +333,12 @@ static int algs_check(algswitch *a, dstr *e, const group *g)
             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 --- */
 
@@ -347,7 +373,9 @@ 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 &&
+  return (group_samep(kdx->g, kdy->g) &&
+         a->bulk == aa->bulk &&
+         a->c == aa->c && a->b == aa->b &&
          a->mgf == aa->mgf && a->h == aa->h &&
          a->m == aa->m && a->tagsz == aa->tagsz);
 }
@@ -855,6 +883,7 @@ void km_unref(kdata *kd)
   G_DESTROY(kd->g, kd->kpub);
   xfree(kd->tag);
   G_DESTROYGROUP(kd->g);
+  DESTROY(kd);
 }
 
 /*----- That's all, folks -------------------------------------------------*/