Eliminate buggy clone-and-hack keyreport functions.
[u/mdw/catacomb] / catcrypt.c
index 08fbb53..bbe4660 100644 (file)
 
 /*----- Utilities ---------------------------------------------------------*/
 
-/* --- @keyreport@ --- *
- *
- * Arguments:   @const char *file@ = filename containing the error
- *              @int line@ = line number in file
- *              @const char *err@ = error text message
- *              @void *p@ = unimportant pointer
- *
- * Returns:     ---
- *
- * Use:         Reports errors during the opening of a key file.
- */
-
-static void keyreport(const char *file, int line, const char *err, void *p)
-{
-  moan("error in keyring `%s' at line `%s': %s", file, line, err);
-}
-
 /*----- Static variables --------------------------------------------------*/
 
 static const char *keyring = "keyring";
@@ -195,7 +178,7 @@ static int encrypt(int argc, char *argv[])
   if (argc - optind > 1 || (f & f_bogus))
     die(EXIT_FAILURE, "Usage: encrypt [-OPTIONS] [FILE]");
 
-  if (key_open(&kf, keyring, KOPEN_READ, keyreport, 0))
+  if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
     die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
   if ((k = key_bytag(&kf, kn)) == 0)
     die(EXIT_FAILURE, "key `%s' not found", kn);
@@ -276,8 +259,7 @@ static int encrypt(int argc, char *argv[])
   seq = 0;
   for (;;) {
     h = GM_INIT(m);
-    STORE32(bb, seq);
-    GH_HASH(h, bb, 4);
+    GH_HASHU32(h, seq);
     seq++;
     if (GC_CLASS(c)->blksz) {
       GC_ENCRYPT(cx, 0, bb, GC_CLASS(c)->blksz);
@@ -314,6 +296,7 @@ static int encrypt(int argc, char *argv[])
   freeenc(e);
   if (s) freesig(s);
   freekem(km);
+  if (fp != stdin) fclose(fp);
   if (of) fclose(ofp);
   key_close(&kf);
   dstr_destroy(&d);
@@ -366,7 +349,7 @@ static int decrypt(int argc, char *argv[])
       { "output",      OPTF_ARGREQ,    0,      'o' },
       { 0,             0,              0,      0 }
     };
-    i = mdwopt(argc, argv, "af:o:qv", opt, 0, 0, 0);
+    i = mdwopt(argc, argv, "abf:o:qv", opt, 0, 0, 0);
     if (i < 0) break;
     switch (i) {
       case 'a': ef = "pem"; break;
@@ -395,7 +378,7 @@ static int decrypt(int argc, char *argv[])
   } else
     optind++;
 
-  if (key_open(&kf, keyring, KOPEN_READ, keyreport, 0))
+  if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
     die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
 
   e = initdec(eo, fp, checkbdry, "CATCRYPT ENCRYPTED MESSAGE");
@@ -460,6 +443,7 @@ static int decrypt(int argc, char *argv[])
       key_fulltag(sk, &d);
       printf("INFO good-signature %s\n", d.buf);
     }
+    freesig(s);
   } else if (verb)
     printf("INFO no-signature\n");
 
@@ -487,8 +471,7 @@ static int decrypt(int argc, char *argv[])
       GC_SETIV(c, d.buf);
     }
     h = GM_INIT(m);
-    STORE32(d.buf, seq);
-    GH_HASH(h, d.buf, 4);
+    GH_HASHU32(h, seq);
     seq++;
     chunk_read(e, &d, &b);
     if ((tag = buf_get(&b, GM_CLASS(m)->hashsz)) == 0) {
@@ -501,6 +484,7 @@ static int decrypt(int argc, char *argv[])
        printf("FAIL bad ciphertext chunk: authentication failure\n");
       exit(EXIT_FAILURE);
     }
+    GH_DESTROY(h);
     if (!BLEFT(&b))
       break;
     GC_DECRYPT(c, BCUR(&b), BCUR(&b), BLEFT(&b));
@@ -522,7 +506,7 @@ static int decrypt(int argc, char *argv[])
     rewind(rfp);
     dstr_reset(&d);
     dstr_ensure(&d, 65536);
-    if (ofp == stdout) printf("DATA\n");
+    if (verb && ofp == stdout) printf("DATA\n");
     for (;;) {
       n = fread(d.buf, 1, d.sz, rfp);
       if (!n) break;
@@ -543,6 +527,7 @@ static int decrypt(int argc, char *argv[])
   GC_DESTROY(cx);
   GM_DESTROY(m);
   freekem(km);
+  if (fp != stdin) fclose(fp);
   if (of) fclose(ofp);
   key_close(&kf);
   dstr_destroy(&d);