.links: Drop obsolete `lib-config.in' file.
[u/mdw/catacomb] / key-text.c
index a072368..ff9e705 100644 (file)
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: key-text.c,v 1.5 2004/04/01 13:42:48 mdw Exp $
+ * $Id$
  *
  * Key textual encoding
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb 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 Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: key-text.c,v $
- * Revision 1.5  2004/04/01 13:42:48  mdw
- * Missed off <ctype.h>\!
- *
- * Revision 1.4  2004/03/28 01:58:47  mdw
- * Generate, store and retreive elliptic curve keys.
- *
- * Revision 1.3  2001/02/03 11:57:00  mdw
- * Track mLib change: symbols no longer need to include a terminating
- * null.
- *
- * Revision 1.2  2000/06/17 11:27:20  mdw
- * Use secure memory interface from MP library.
- *
- * Revision 1.1  2000/02/12 18:21:02  mdw
- * Overhaul of key management (again).
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <ctype.h>
 /* --- @key_read@ --- *
  *
  * Arguments:  @const char *p@ = pointer to textual key representation
- *             @key_data *k@ = pointer to output block for key data
  *             @char **pp@ = where to store the end pointer
  *
- * Returns:    Zero if all went well, nonzero if there was a problem.
+ * Returns:    The newly-read key data, or null if it failed.
  *
  * Use:                Parses a textual key description.
  */
 
-int key_read(const char *p, key_data *k, char **pp)
+key_data *key_read(const char *p, char **pp)
 {
   unsigned e;
+  key_data *kd;
 
   /* --- Read the encoding type --- *
    *
@@ -93,13 +72,12 @@ int key_read(const char *p, key_data *k, char **pp)
   else {
     char *q;
     if (key_readflags(p, &q, &e, 0))
-      return (-1);
+      return (0);
     p = q + 1;
   }
 
   /* --- Now scan the data based on the encoding type --- */
 
-  k->e = e;
   switch (e & KF_ENCMASK) {
 
     /* --- Binary encoding --- *
@@ -118,9 +96,7 @@ int key_read(const char *p, key_data *k, char **pp)
       base64_init(&b);
       base64_decode(&b, p, sz, &d);
       base64_decode(&b, 0, 0, &d);
-      k->u.k.k = sub_alloc(d.len);
-      k->u.k.sz = d.len;
-      memcpy(k->u.k.k, d.buf, d.len);
+      kd = key_newbinary(e, d.buf, d.len);
       dstr_destroy(&d);
       p += sz;
     } break;
@@ -132,10 +108,11 @@ int key_read(const char *p, key_data *k, char **pp)
 
     case KENC_MP: {
       char *q;
-      mp *m = mp_readstring(k->e & KF_BURN ? MP_NEWSEC : MP_NEW, p, &q, 0);
+      mp *m = mp_readstring(e & KF_BURN ? MP_NEWSEC : MP_NEW, p, &q, 0);
       if (!m)
-       return (-1);
-      k->u.m = m;
+       return (0);
+      kd = key_newmp(e, m);
+      MP_DROP(m);
       p = q;
     } break;
 
@@ -164,7 +141,7 @@ int key_read(const char *p, key_data *k, char **pp)
        p++;
       }
       DPUTZ(&d);
-      k->u.p = xstrdup(d.buf);
+      kd = key_newstring(e, d.buf);
       dstr_destroy(&d);
     } break;
 
@@ -175,12 +152,14 @@ int key_read(const char *p, key_data *k, char **pp)
      */
 
     case KENC_EC: {
+      ec pt = EC_INIT;
       qd_parse qd;
       qd.p = p;
       qd.e = 0;
-      EC_CREATE(&k->u.e);
-      if (!ec_ptparse(&qd, &k->u.e))
-       return (-1);
+      if (!ec_ptparse(&qd, &pt))
+       return (0);
+      kd = key_newec(e, &pt);
+      EC_DESTROY(&pt);
       p = qd.p;
     } break;
 
@@ -196,21 +175,20 @@ int key_read(const char *p, key_data *k, char **pp)
 
     case KENC_STRUCT: {
       dstr d = DSTR_INIT;
+      key_data *nkd;
       char *q;
 
       /* --- Read the opening bracket --- */
 
-      k->e &= KF_ENCMASK;
+      kd = key_newstruct();
       if (*p != '[')
-       return (-1);
+       return (0);
       p++;
-      sym_create(&k->u.s);
 
       /* --- Read named key subparts --- */
 
       for (;;) {
        size_t sz;
-       key_struct *ks;
 
        /* --- Stop if there's a close-bracket --- *
         *
@@ -233,24 +211,11 @@ int key_read(const char *p, key_data *k, char **pp)
        DPUTM(&d, p, sz);
        DPUTZ(&d);
 
-       /* --- Add an appropriate block to the key table --- *
-        *
-        * Simply destroy old data if there's already a match.
-        */
-
-       {
-         unsigned f;
-         ks = sym_find(&k->u.s, d.buf, d.len, sizeof(*ks), &f);
-         if (f)
-           key_destroy(&ks->k);
-       }
-
        /* --- Read the key data for the subkey --- */
 
-       if (key_read(q + 1, &ks->k, &q)) {
-         sym_remove(&k->u.s, ks);
+       if ((nkd = key_read(q + 1, &q)) == 0)
          goto fail;
-       }
+       key_structsteal(kd, d.buf, nkd);
        p = q;
 
        /* --- Read the comma or close-bracket --- */
@@ -273,21 +238,21 @@ int key_read(const char *p, key_data *k, char **pp)
 
     fail:
       dstr_destroy(&d);
-      key_destroy(k);
-      return (-1);
+      return (0);
     } break;
 
     /* --- Anything else is unknown --- */
 
     default:
-      return (-1);
+      return (0);
   }
 
   /* --- Return the end pointer --- */
 
+  kd->e = e;
   if (pp)
     *pp = (char *)p;
-  return (0);
+  return (kd);
 }
 
 /* --- @key_write@ --- *
@@ -300,7 +265,7 @@ int key_read(const char *p, key_data *k, char **pp)
  *
  * Use:                Writes a key in a textual encoding.
  */
+
 int key_write(key_data *k, dstr *d, const key_filter *kf)
 {
   int rc = 0;
@@ -348,19 +313,19 @@ int key_write(key_data *k, dstr *d, const key_filter *kf)
       rc = 1;
       break;
     case KENC_STRUCT: {
-      sym_iter i;
-      key_struct *ks;
+      key_subkeyiter i;
+      const char *tag;
       char del = 0;
       size_t n = d->len;
 
       DPUTS(d, "struct:[");
-      for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) {
+      for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) {
        size_t o = d->len;
        if (del)
          DPUTC(d, del);
-       DPUTS(d, SYM_NAME(ks));
+       DPUTS(d, tag);
        DPUTC(d, '=');
-       if (!key_write(&ks->k, d, kf))
+       if (!key_write(k, d, kf))
          d->len = o;
        else {
          del = ',';