key-data.c (key_struct{set,steal}): Assert no other references.
[u/mdw/catacomb] / dsig.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Verify signatures on distribuitions of files
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #define _FILE_OFFSET_BITS 64
33
34 #include "config.h"
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <mLib/alloc.h>
43 #include <mLib/base64.h>
44 #include <mLib/mdwopt.h>
45 #include <mLib/quis.h>
46 #include <mLib/report.h>
47 #include <mLib/sub.h>
48
49 #include "getdate.h"
50 #include "rand.h"
51 #include "ghash.h"
52 #include "key.h"
53 #include "key-data.h"
54 #include "noise.h"
55 #include "cc.h"
56
57 /*----- Data formatting ---------------------------------------------------*/
58
59 /* --- Binary data structure --- *
60 *
61 * The binary format, which is used for hashing and for the optional binary
62 * output, consists of a sequence of tagged blocks. The tag describes the
63 * format and meaining of the following data.
64 */
65
66 enum {
67 /* --- Block tags --- */
68
69 T_IDENT = 0, /* An identifying marker */
70 T_KEYID, /* Key identifier */
71 T_BEGIN, /* Begin hashing here */
72 T_COMMENT = T_BEGIN, /* A textual comment */
73 T_DATE, /* Creation date of signature */
74 T_EXPIRE, /* Expiry date of signature */
75 T_FILE, /* File and corresponding hash */
76 T_SIGNATURE, /* Final signature block */
77
78 /* --- Error messages --- */
79
80 E_EOF = -1,
81 E_BIN = -2,
82 E_TAG = -3,
83 E_DATE = -4
84 };
85
86 /* --- Name translation table --- */
87
88 static const char *tagtab[] = {
89 "ident:", "keyid:",
90 "comment:", "date:", "expires:", "file:",
91 "signature:",
92 0
93 };
94
95 static const char *errtab[] = {
96 "Off-by-one bug",
97 "Unexpected end-of-file",
98 "Binary object too large",
99 "Unrecognized tag",
100 "Bad date string"
101 };
102
103 /* --- Memory representation of block types --- */
104
105 typedef struct block {
106 int tag; /* Type tag */
107 dstr d; /* String data */
108 dstr b; /* Binary data */
109 time_t t; /* Timestamp */
110 uint32 k; /* Keyid */
111 } block;
112
113 /* --- @timestring@ --- *
114 *
115 * Arguments: @time_t t@ = a timestamp
116 * @dstr *d@ = a string to write on
117 *
118 * Returns: ---
119 *
120 * Use: Writes a textual representation of the timestamp to the
121 * string.
122 */
123
124 static void timestring(time_t t, dstr *d)
125 {
126 if (t == KEXP_FOREVER)
127 DPUTS(d, "forever");
128 else {
129 struct tm *tm = localtime(&t);
130 DENSURE(d, 32);
131 d->len += strftime(d->buf + d->len, 32, "%Y-%m-%d %H:%M:%S %Z", tm);
132 DPUTZ(d);
133 }
134 }
135
136 /* --- @breset@ --- *
137 *
138 * Arguments: @block *b@ = block to reset
139 *
140 * Returns: ---
141 *
142 * Use: Resets a block so that more stuff can be put in it.
143 */
144
145 static void breset(block *b)
146 {
147 b->tag = 0;
148 DRESET(&b->d);
149 DRESET(&b->b);
150 b->k = 0;
151 b->t = KEXP_EXPIRE;
152 }
153
154 /* --- @binit@ --- *
155 *
156 * Arguments: @block *b@ = block to initialize
157 *
158 * Returns: ---
159 *
160 * Use: Initializes a block as something to read into.
161 */
162
163 static void binit(block *b)
164 {
165 dstr_create(&b->d);
166 dstr_create(&b->b);
167 breset(b);
168 }
169
170 /* --- @bdestroy@ --- *
171 *
172 * Arguments: @block *b@ = block to destroy
173 *
174 * Returns: ---
175 *
176 * Use: Destroys a block's contents.
177 */
178
179 static void bdestroy(block *b)
180 {
181 dstr_destroy(&b->d);
182 dstr_destroy(&b->b);
183 }
184
185 /* --- @bget@ --- *
186 *
187 * Arguments: @block *b@ = pointer to block
188 * @FILE *fp@ = stream to read from
189 * @unsigned bin@ = binary switch
190 *
191 * Returns: Tag of block, or an error tag.
192 *
193 * Use: Reads a block from a stream.
194 */
195
196 static int bget(block *b, FILE *fp, unsigned bin)
197 {
198 int tag;
199
200 /* --- Read the tag --- */
201
202 if (bin)
203 tag = getc(fp);
204 else {
205 dstr d = DSTR_INIT;
206 if (getstring(fp, &d, GSF_FILE))
207 return (E_EOF);
208 for (tag = 0; tagtab[tag]; tag++) {
209 if (strcmp(tagtab[tag], d.buf) == 0)
210 goto done;
211 }
212 return (E_TAG);
213 done:;
214 }
215
216 /* --- Decide what to do next --- */
217
218 breset(b);
219 b->tag = tag;
220 switch (tag) {
221
222 /* --- Reading of strings --- */
223
224 case T_IDENT:
225 case T_COMMENT:
226 if (getstring(fp, &b->d, GSF_FILE | (bin ? GSF_RAW : 0)))
227 return (E_EOF);
228 break;
229
230 /* --- Timestamps --- */
231
232 case T_DATE:
233 case T_EXPIRE:
234 if (bin) {
235 octet buf[8];
236 if (fread(buf, sizeof(buf), 1, fp) < 1)
237 return (E_EOF);
238 b->t = ((time_t)(((LOAD32(buf + 0) << 16) << 16) & ~MASK32) |
239 (time_t)LOAD32(buf + 4));
240 } else {
241 if (getstring(fp, &b->d, GSF_FILE))
242 return (E_EOF);
243 if (strcmp(b->d.buf, "forever") == 0)
244 b->t = KEXP_FOREVER;
245 else if ((b->t = get_date(b->d.buf, 0)) == -1)
246 return (E_DATE);
247 }
248 break;
249
250 /* --- Key ids --- */
251
252 case T_KEYID:
253 if (bin) {
254 octet buf[4];
255 if (fread(buf, sizeof(buf), 1, fp) < 1)
256 return (E_EOF);
257 b->k = LOAD32(buf);
258 } else {
259 if (getstring(fp, &b->d, GSF_FILE))
260 return (E_EOF);
261 b->k = strtoul(b->d.buf, 0, 16);
262 }
263 break;
264
265 /* --- Reading of binary data --- */
266
267 case T_FILE:
268 case T_SIGNATURE:
269 if (bin) {
270 octet buf[2];
271 uint32 sz;
272 if (fread(buf, sizeof(buf), 1, fp) < 1)
273 return (E_EOF);
274 sz = LOAD16(buf);
275 if (sz > 4096)
276 return (E_BIN);
277 DENSURE(&b->b, sz);
278 if (fread(b->b.buf + b->b.len, 1, sz, fp) < sz)
279 return (E_EOF);
280 b->b.len += sz;
281 } else {
282 base64_ctx b64;
283 if (getstring(fp, &b->d, GSF_FILE))
284 return (E_EOF);
285 base64_init(&b64);
286 base64_decode(&b64, b->d.buf, b->d.len, &b->b);
287 base64_decode(&b64, 0, 0, &b->b);
288 DRESET(&b->d);
289 }
290 if (tag == T_FILE &&
291 getstring(fp, &b->d, GSF_FILE | (bin ? GSF_RAW : 0)))
292 return (E_EOF);
293 break;
294
295 /* --- Anything else --- */
296
297 default:
298 return (E_TAG);
299 }
300
301 return (tag);
302 }
303
304 /* --- @blob@ --- *
305 *
306 * Arguments: @block *b@ = pointer to block to emit
307 * @dstr *d@ = output buffer
308 *
309 * Returns: ---
310 *
311 * Use: Encodes a block in a binary format.
312 */
313
314 static void blob(block *b, dstr *d)
315 {
316 DPUTC(d, b->tag);
317 switch (b->tag) {
318 case T_IDENT:
319 case T_COMMENT:
320 DPUTD(d, &b->d);
321 DPUTC(d, 0);
322 break;
323 case T_DATE:
324 case T_EXPIRE:
325 DENSURE(d, 8);
326 if (b->t == KEXP_FOREVER) {
327 STORE32(d->buf + d->len, 0xffffffff);
328 STORE32(d->buf + d->len + 4, 0xffffffff);
329 } else {
330 STORE32(d->buf + d->len, ((b->t & ~MASK32) >> 16) >> 16);
331 STORE32(d->buf + d->len + 4, b->t);
332 }
333 d->len += 8;
334 break;
335 case T_KEYID:
336 DENSURE(d, 4);
337 STORE32(d->buf + d->len, b->k);
338 d->len += 4;
339 break;
340 case T_FILE:
341 case T_SIGNATURE:
342 DENSURE(d, 2);
343 STORE16(d->buf + d->len, b->b.len);
344 d->len += 2;
345 DPUTD(d, &b->b);
346 if (b->tag == T_FILE) {
347 DPUTD(d, &b->d);
348 DPUTC(d, 0);
349 }
350 break;
351 }
352 }
353
354 /* --- @bwrite@ --- *
355 *
356 * Arguments: @block *b@ = pointer to block to write
357 * @FILE *fp@ = stream to write on
358 *
359 * Returns: ---
360 *
361 * Use: Writes a block on a stream in a textual format.
362 */
363
364 static void bwrite(block *b, FILE *fp)
365 {
366 fputs(tagtab[b->tag], fp);
367 putc(' ', fp);
368 switch (b->tag) {
369 case T_IDENT:
370 case T_COMMENT:
371 putstring(fp, b->d.buf, 0);
372 break;
373 case T_DATE:
374 case T_EXPIRE: {
375 dstr d = DSTR_INIT;
376 timestring(b->t, &d);
377 putstring(fp, d.buf, 0);
378 dstr_destroy(&d);
379 } break;
380 case T_KEYID:
381 fprintf(fp, "%08lx", (unsigned long)b->k);
382 break;
383 case T_FILE:
384 case T_SIGNATURE: {
385 dstr d = DSTR_INIT;
386 base64_ctx b64;
387 base64_init(&b64);
388 b64.maxline = 0;
389 base64_encode(&b64, b->b.buf, b->b.len, &d);
390 base64_encode(&b64, 0, 0, &d);
391 dstr_write(&d, fp);
392 if (b->tag == T_FILE) {
393 putc(' ', fp);
394 putstring(fp, b->d.buf, 0);
395 }
396 } break;
397 }
398 putc('\n', fp);
399 }
400
401 /* --- @bemit@ --- *
402 *
403 * Arguments: @block *b@ = pointer to block to write
404 * @FILE *fp@ = file to write on
405 * @ghash *h@ = pointer to hash function
406 * @unsigned bin@ = binary/text flag
407 *
408 * Returns: ---
409 *
410 * Use: Spits out a block properly.
411 */
412
413 static void bemit(block *b, FILE *fp, ghash *h, unsigned bin)
414 {
415 if (h || (fp && bin)) {
416 dstr d = DSTR_INIT;
417 blob(b, &d);
418 if (h)
419 GH_HASH(h, d.buf, d.len);
420 if (fp && bin)
421 fwrite(d.buf, d.len, 1, fp);
422 }
423 if (fp && !bin)
424 bwrite(b, fp);
425 }
426
427 /*----- Static variables --------------------------------------------------*/
428
429 static const char *keyring = "keyring";
430
431 /*----- Other shared functions --------------------------------------------*/
432
433 /* --- @fhex@ --- *
434 *
435 * Arguments: @FILE *fp@ = file to write on
436 * @const void *p@ = pointer to data to be written
437 * @size_t sz@ = size of the data to write
438 *
439 * Returns: ---
440 *
441 * Use: Emits a hex dump to a stream.
442 */
443
444 static void fhex(FILE *fp, const void *p, size_t sz)
445 {
446 const octet *q = p;
447 if (!sz)
448 return;
449 for (;;) {
450 fprintf(fp, "%02x", *q++);
451 sz--;
452 if (!sz)
453 break;
454 }
455 }
456
457 /*----- Signature generation ----------------------------------------------*/
458
459 static int sign(int argc, char *argv[])
460 {
461 #define f_bogus 1u
462 #define f_bin 2u
463 #define f_nocheck 4u
464
465 unsigned f = 0;
466 const char *ki = "dsig";
467 key_file kf;
468 key *k;
469 sig *s;
470 time_t exp = KEXP_EXPIRE;
471 unsigned verb = 0;
472 const char *ifile = 0, *hfile = 0;
473 const char *ofile = 0;
474 const char *c = 0;
475 const char *err;
476 FILE *ifp, *ofp;
477 dstr d = DSTR_INIT;
478 hfpctx hfp;
479 block b;
480 int e, hf, n;
481
482 for (;;) {
483 static struct option opts[] = {
484 { "null", 0, 0, '0' },
485 { "binary", 0, 0, 'b' },
486 { "verbose", 0, 0, 'v' },
487 { "progress", 0, 0, 'p' },
488 { "quiet", 0, 0, 'q' },
489 { "comment", OPTF_ARGREQ, 0, 'c' },
490 { "file", OPTF_ARGREQ, 0, 'f' },
491 { "hashes", OPTF_ARGREQ, 0, 'h' },
492 { "output", OPTF_ARGREQ, 0, 'o' },
493 { "key", OPTF_ARGREQ, 0, 'k' },
494 { "expire", OPTF_ARGREQ, 0, 'e' },
495 { "nocheck", OPTF_ARGREQ, 0, 'C' },
496 { 0, 0, 0, 0 }
497 };
498 int i = mdwopt(argc, argv, "+0vpqbC" "c:" "f:h:o:" "k:e:",
499 opts, 0, 0, 0);
500 if (i < 0)
501 break;
502 switch (i) {
503 case '0':
504 f |= GSF_RAW;
505 break;
506 case 'b':
507 f |= f_bin;
508 break;
509 case 'v':
510 verb++;
511 break;
512 case 'p':
513 f |= FHF_PROGRESS;
514 break;
515 case 'q':
516 if (verb > 0)
517 verb--;
518 break;
519 case 'C':
520 f |= f_nocheck;
521 break;
522 case 'c':
523 c = optarg;
524 break;
525 case 'f':
526 ifile = optarg;
527 break;
528 case 'h':
529 hfile = optarg;
530 break;
531 case 'o':
532 ofile = optarg;
533 break;
534 case 'k':
535 ki = optarg;
536 break;
537 case 'e':
538 if (strcmp(optarg, "forever") == 0)
539 exp = KEXP_FOREVER;
540 else if ((exp = get_date(optarg, 0)) == -1)
541 die(EXIT_FAILURE, "bad expiry time");
542 break;
543 default:
544 f |= f_bogus;
545 break;
546 }
547 }
548 if (optind != argc || (f & f_bogus))
549 die(EXIT_FAILURE, "Usage: sign [-OPTIONS]");
550 if (hfile && ifile)
551 die(EXIT_FAILURE, "Inconsistent options `-h' and `-f'");
552
553 /* --- Locate the signing key --- */
554
555 if (key_open(&kf, keyring, KOPEN_WRITE, key_moan, 0))
556 die(EXIT_FAILURE, "couldn't open keyring `%s'", keyring);
557 if ((k = key_bytag(&kf, ki)) == 0)
558 die(EXIT_FAILURE, "couldn't find key `%s'", ki);
559 key_fulltag(k, &d);
560 if (exp == KEXP_FOREVER && k->exp != KEXP_FOREVER) {
561 die(EXIT_FAILURE, "key `%s' expires: can't create nonexpiring signature",
562 d.buf);
563 }
564 s = getsig(k, "dsig", 1);
565
566 /* --- Check the key --- */
567
568 if (!(f & f_nocheck) && (err = s->ops->check(s)) != 0)
569 moan("key `%s' fails check: %s", d.buf, err);
570
571 /* --- Open files --- */
572
573 if (hfile) ifile = hfile;
574 if (!ifile || strcmp(ifile, "-") == 0)
575 ifp = stdin;
576 else if ((ifp = fopen(ifile, (f & f_bin) ? "rb" : "r")) == 0) {
577 die(EXIT_FAILURE, "couldn't open input file `%s': %s",
578 ifile, strerror(errno));
579 }
580
581 if (!ofile || strcmp(ofile, "-") == 0)
582 ofp = stdout;
583 else if ((ofp = fopen(ofile, (f & f_bin) ? "wb" : "w")) == 0) {
584 die(EXIT_FAILURE, "couldn't open output file `%s': %s",
585 ofile, strerror(errno));
586 }
587
588 /* --- Emit the start of the output --- */
589
590 binit(&b); b.tag = T_IDENT;
591 dstr_putf(&b.d, "%s, Catacomb version " VERSION, QUIS);
592 bemit(&b, ofp, 0, f & f_bin);
593
594 breset(&b); b.tag = T_KEYID; b.k = k->id;
595 bemit(&b, ofp, 0, f & f_bin);
596
597 /* --- Start hashing, and emit the datestamps and things --- */
598
599 {
600 time_t now = time(0);
601
602 breset(&b); b.tag = T_DATE; b.t = now; bemit(&b, ofp, s->h, f & f_bin);
603 if (exp == KEXP_EXPIRE)
604 exp = now + 86400 * 28;
605 breset(&b); b.tag = T_EXPIRE; b.t = exp; bemit(&b, ofp, s->h, f & f_bin);
606 if (c) {
607 breset(&b); b.tag = T_COMMENT; DPUTS(&b.d, c);
608 bemit(&b, ofp, s->h, f & f_bin);
609 }
610
611 if (!(f & f_bin))
612 putc('\n', ofp);
613 }
614
615 /* --- Now hash the various files --- */
616
617 if (hfile) {
618 hfp.f = f;
619 hfp.fp = ifp;
620 hfp.ee = &encodingtab[ENC_HEX];
621 hfp.gch = GH_CLASS(s->h);
622 hfp.dline = &d;
623 hfp.dfile = &b.d;
624
625 n = 0;
626 for (;;) {
627 breset(&b);
628 DENSURE(&b.b, hfp.gch->hashsz);
629 hfp.hbuf = (octet *)b.b.buf;
630 if (ferror(ofp)) { f |= f_bogus; break; }
631 if ((hf = hfparse(&hfp)) == HF_EOF) break;
632 n++;
633
634 switch (hf) {
635 case HF_HASH:
636 if (hfp.gch != GH_CLASS(s->h)) {
637 moan("%s:%d: incorrect hash function `%s' (should be `%s')",
638 hfile, n, hfp.gch->name, GH_CLASS(s->h)->name);
639 f |= f_bogus;
640 }
641 break;
642 case HF_BAD:
643 moan("%s:%d: invalid hash-file line", hfile, n);
644 f |= f_bogus;
645 break;
646 case HF_FILE:
647 b.tag = T_FILE;
648 b.b.len += hfp.gch->hashsz;
649 bemit(&b, ofp, s->h, f & f_bin);
650 break;
651 }
652 }
653 } else {
654 for (;;) {
655
656 /* --- Stop on an output error --- */
657
658 if (ferror(ofp)) {
659 f |= f_bogus;
660 break;
661 }
662
663 /* --- Read the next filename to hash --- */
664
665 breset(&b);
666 if (getstring(ifp, &b.d, GSF_FILE | f))
667 break;
668 b.tag = T_FILE;
669 DENSURE(&b.b, GH_CLASS(s->h)->hashsz);
670 if (fhash(GH_CLASS(s->h), f | FHF_BINARY, b.d.buf, b.b.buf)) {
671 moan("error reading `%s': %s", b.d.buf, strerror(errno));
672 f |= f_bogus;
673 } else {
674 b.b.len += GH_CLASS(s->h)->hashsz;
675 if (verb) {
676 fhex(stderr, b.b.buf, b.b.len);
677 fprintf(stderr, " %s\n", b.d.buf);
678 }
679 bemit(&b, ofp, s->h, f & f_bin);
680 }
681 }
682 }
683
684 /* --- Create the signature --- */
685
686 if (!(f & f_bogus)) {
687 breset(&b);
688 b.tag = T_SIGNATURE;
689 if ((e = s->ops->doit(s, &b.b)) != 0) {
690 moan("error creating signature: %s", key_strerror(e));
691 f |= f_bogus;
692 }
693 if (!(f & f_bogus)) {
694 bemit(&b, ofp, 0, f & f_bin);
695 key_used(&kf, k, exp);
696 }
697 }
698
699 /* --- Tidy up at the end --- */
700
701 freesig(s);
702 bdestroy(&b);
703 if (ifile)
704 fclose(ifp);
705 if (ofile) {
706 if (fclose(ofp))
707 f |= f_bogus;
708 } else {
709 if (fflush(ofp))
710 f |= f_bogus;
711 }
712 if ((e = key_close(&kf)) != 0) {
713 switch (e) {
714 case KWRITE_FAIL:
715 die(EXIT_FAILURE, "couldn't write file `%s': %s",
716 keyring, strerror(errno));
717 case KWRITE_BROKEN:
718 die(EXIT_FAILURE, "keyring file `%s' broken: %s (repair manually)",
719 keyring, strerror(errno));
720 }
721 }
722 if (f & f_bogus)
723 die(EXIT_FAILURE, "error(s) occurred while creating signature");
724 return (EXIT_SUCCESS);
725
726 #undef f_bin
727 #undef f_bogus
728 #undef f_nocheck
729 }
730
731 /*----- Signature verification --------------------------------------------*/
732
733 static int verify(int argc, char *argv[])
734 {
735 #define f_bogus 1u
736 #define f_bin 2u
737 #define f_ok 4u
738 #define f_nocheck 8u
739
740 unsigned f = 0;
741 unsigned verb = 1;
742 key_file kf;
743 key *k = 0;
744 sig *s;
745 dstr d = DSTR_INIT;
746 const char *err;
747 FILE *fp;
748 block b;
749 int e;
750
751 /* --- Parse the options --- */
752
753 for (;;) {
754 static struct option opts[] = {
755 { "verbose", 0, 0, 'v' },
756 { "progress", 0, 0, 'p' },
757 { "quiet", 0, 0, 'q' },
758 { "nocheck", 0, 0, 'C' },
759 { 0, 0, 0, 0 }
760 };
761 int i = mdwopt(argc, argv, "+vpqC", opts, 0, 0, 0);
762 if (i < 0)
763 break;
764 switch (i) {
765 case 'v':
766 verb++;
767 break;
768 case 'p':
769 f |= FHF_PROGRESS;
770 break;
771 case 'q':
772 if (verb)
773 verb--;
774 break;
775 case 'C':
776 f |= f_nocheck;
777 break;
778 default:
779 f |= f_bogus;
780 break;
781 }
782 }
783 argc -= optind;
784 argv += optind;
785 if ((f & f_bogus) || argc > 1)
786 die(EXIT_FAILURE, "Usage: verify [-qvC] [FILE]");
787
788 /* --- Open the key file, and start reading the input file --- */
789
790 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
791 die(EXIT_FAILURE, "couldn't open keyring `%s'\n", keyring);
792 if (argc < 1)
793 fp = stdin;
794 else {
795 if ((fp = fopen(argv[0], "rb")) == 0) {
796 die(EXIT_FAILURE, "couldn't open file `%s': %s\n",
797 argv[0], strerror(errno));
798 }
799 if (getc(fp) == 0) {
800 ungetc(0, fp);
801 f |= f_bin;
802 } else {
803 fclose(fp);
804 if ((fp = fopen(argv[0], "r")) == 0) {
805 die(EXIT_FAILURE, "couldn't open file `%s': %s\n",
806 argv[0], strerror(errno));
807 }
808 }
809 }
810
811 /* --- Read the introductory matter --- */
812
813 binit(&b);
814 for (;;) {
815 breset(&b);
816 e = bget(&b, fp, f & f_bin);
817 if (e < 0)
818 die(EXIT_FAILURE, "error reading packet: %s", errtab[-e]);
819 if (e >= T_BEGIN)
820 break;
821 switch (e) {
822 case T_IDENT:
823 if (verb > 2)
824 printf("INFO ident: `%s'\n", b.d.buf);
825 break;
826 case T_KEYID:
827 if ((k = key_byid(&kf, b.k)) == 0) {
828 if (verb)
829 printf("FAIL key %08lx not found\n", (unsigned long)b.k);
830 exit(EXIT_FAILURE);
831 }
832 if (verb > 2) {
833 DRESET(&b.d);
834 key_fulltag(k, &b.d);
835 printf("INFO key: %s\n", b.d.buf);
836 }
837 break;
838 default:
839 die(EXIT_FAILURE, "(internal) unknown packet type\n");
840 break;
841 }
842 }
843
844 /* --- Initialize the hash function and start reading hashed packets --- */
845
846 if (!k) {
847 if (verb)
848 puts("FAIL no keyid packet found");
849 exit(EXIT_FAILURE);
850 }
851
852 s = getsig(k, "dsig", 0);
853 if (!(f & f_nocheck) && verb && (err = s->ops->check(s)) != 0)
854 printf("WARN public key fails check: %s", err);
855
856 for (;;) {
857 switch (e) {
858 case T_COMMENT:
859 if (verb > 1)
860 printf("INFO comment: `%s'\n", b.d.buf);
861 bemit(&b, 0, s->h, 0);
862 break;
863 case T_DATE:
864 if (verb > 2) {
865 DRESET(&b.d);
866 timestring(b.t, &b.d);
867 printf("INFO date: %s\n", b.d.buf);
868 }
869 bemit(&b, 0, s->h, 0);
870 break;
871 case T_EXPIRE: {
872 time_t now = time(0);
873 if (b.t != KEXP_FOREVER && b.t < now) {
874 if (verb > 1)
875 puts("BAD signature has expired");
876 f |= f_bogus;
877 }
878 if (verb > 2) {
879 DRESET(&b.d);
880 timestring(b.t, &b.d);
881 printf("INFO expires: %s\n", b.d.buf);
882 }
883 bemit(&b, 0, s->h, 0);
884 } break;
885 case T_FILE:
886 DRESET(&d);
887 DENSURE(&d, GH_CLASS(s->h)->hashsz);
888 if (fhash(GH_CLASS(s->h), f | FHF_BINARY, b.d.buf, d.buf)) {
889 if (verb > 1) {
890 printf("BAD error reading file `%s': %s\n",
891 b.d.buf, strerror(errno));
892 }
893 f |= f_bogus;
894 } else if (b.b.len != GH_CLASS(s->h)->hashsz ||
895 memcmp(d.buf, b.b.buf, b.b.len) != 0) {
896 if (verb > 1)
897 printf("BAD file `%s' has incorrect hash\n", b.d.buf);
898 f |= f_bogus;
899 } else if (verb > 3) {
900 fputs("INFO hash: ", stdout);
901 fhex(stdout, b.b.buf, b.b.len);
902 printf(" %s\n", b.d.buf);
903 }
904 bemit(&b, 0, s->h, 0);
905 break;
906 case T_SIGNATURE:
907 if (s->ops->doit(s, &b.b)) {
908 if (verb > 1)
909 puts("BAD bad signature");
910 f |= f_bogus;
911 } else if (verb > 2)
912 puts("INFO good signature");
913 goto done;
914 default:
915 if (verb)
916 printf("FAIL invalid packet type %i\n", e);
917 exit(EXIT_FAILURE);
918 break;
919 }
920 breset(&b);
921 e = bget(&b, fp, f & f_bin);
922 if (e < 0) {
923 if (verb)
924 printf("FAIL error reading packet: %s\n", errtab[-e]);
925 exit(EXIT_FAILURE);
926 }
927 }
928 done:
929 bdestroy(&b);
930 dstr_destroy(&d);
931 freesig(s);
932 key_close(&kf);
933 if (fp != stdin)
934 fclose(fp);
935 if (verb) {
936 if (f & f_bogus)
937 puts("FAIL signature invalid");
938 else
939 puts("OK signature verified");
940 }
941 return (f & f_bogus ? EXIT_FAILURE : EXIT_SUCCESS);
942
943 #undef f_bogus
944 #undef f_bin
945 #undef f_ok
946 #undef f_nocheck
947 }
948
949 /*----- Main code ---------------------------------------------------------*/
950
951 #define LISTS(LI) \
952 LI("Lists", list, \
953 listtab[i].name, listtab[i].name) \
954 LI("Signature schemes", sig, \
955 sigtab[i].name, sigtab[i].name) \
956 LI("Hash functions", hash, \
957 ghashtab[i], ghashtab[i]->name)
958
959 MAKELISTTAB(listtab, LISTS)
960
961 int cmd_show(int argc, char *argv[])
962 {
963 return (displaylists(listtab, argv + 1));
964 }
965
966 static int cmd_help(int, char **);
967
968 static cmd cmdtab[] = {
969 { "help", cmd_help, "help [COMMAND...]" },
970 { "show", cmd_show, "show [ITEM...]" },
971 { "sign", sign,
972 "sign [-0bpqvC] [-c COMMENT] [-k TAG] [-e EXPIRE]\n\t\
973 [-f FILE] [-h FILE] [-o OUTPUT]",
974 "\
975 Options:\n\
976 \n\
977 -0, --null Read null-terminated filenames from stdin.\n\
978 -b, --binary Produce a binary output file.\n\
979 -q, --quiet Produce fewer messages while working.\n\
980 -v, --verbose Produce more messages while working.\n\
981 -p, --progress Show progress on large files.\n\
982 -C, --nocheck Don't check the private key.\n\
983 -c, --comment=COMMENT Include COMMENT in the output file.\n\
984 -f, --file=FILE Read filenames to hash from FILE.\n\
985 -h, --hashes=FILE Read precomputed hashes from FILE.\n\
986 -o, --output=FILE Write the signed result to FILE.\n\
987 -k, --key=TAG Use a key named by TAG.\n\
988 -e, --expire=TIME The signature should expire after TIME.\n\
989 " },
990 { "verify", verify,
991 "verify [-pqvC] [FILE]", "\
992 Options:\n\
993 \n\
994 -q, --quiet Produce fewer messages while working.\n\
995 -v, --verbose Produce more messages while working.\n\
996 -p, --progress Show progress on large files.\n\
997 -C, --nocheck Don't check the public key.\n\
998 " },
999 { 0, 0, 0 }
1000 };
1001
1002 static int cmd_help(int argc, char **argv)
1003 {
1004 sc_help(cmdtab, stdout, argv + 1);
1005 return (0);
1006 }
1007
1008 void version(FILE *fp)
1009 {
1010 pquis(fp, "$, Catacomb version " VERSION "\n");
1011 }
1012
1013 static void usage(FILE *fp)
1014 {
1015 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1016 }
1017
1018 void help_global(FILE *fp)
1019 {
1020 usage(fp);
1021 fputs("\n\
1022 Create and verify signatures on lists of files.\n\
1023 \n\
1024 Global command-line options:\n\
1025 \n\
1026 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1027 -v, --version Show program version number.\n\
1028 -u, --usage Show a terse usage message.\n\
1029 \n\
1030 -k, --keyring=FILE Read keys from FILE.\n",
1031 fp);
1032 }
1033
1034 /* --- @main@ --- *
1035 *
1036 * Arguments: @int argc@ = number of command line arguments
1037 * @char *argv[]@ = vector of command line arguments
1038 *
1039 * Returns: Zero if successful, nonzero otherwise.
1040 *
1041 * Use: Signs or verifies signatures on lists of files. Useful for
1042 * ensuring that a distribution is unmolested.
1043 */
1044
1045 int main(int argc, char *argv[])
1046 {
1047 unsigned f = 0;
1048
1049 #define f_bogus 1u
1050
1051 /* --- Initialize the library --- */
1052
1053 ego(argv[0]);
1054 sub_init();
1055 rand_noisesrc(RAND_GLOBAL, &noise_source);
1056 rand_seed(RAND_GLOBAL, 160);
1057
1058 /* --- Parse options --- */
1059
1060 for (;;) {
1061 static struct option opts[] = {
1062 { "help", 0, 0, 'h' },
1063 { "version", 0, 0, 'v' },
1064 { "usage", 0, 0, 'u' },
1065 { "keyring", OPTF_ARGREQ, 0, 'k' },
1066 { 0, 0, 0, 0 }
1067 };
1068 int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0);
1069 if (i < 0)
1070 break;
1071 switch (i) {
1072 case 'h':
1073 sc_help(cmdtab, stdout, argv + optind);
1074 exit(0);
1075 break;
1076 case 'v':
1077 version(stdout);
1078 exit(0);
1079 break;
1080 case 'u':
1081 usage(stdout);
1082 exit(0);
1083 case 'k':
1084 keyring = optarg;
1085 break;
1086 default:
1087 f |= f_bogus;
1088 break;
1089 }
1090 }
1091
1092 argc -= optind;
1093 argv += optind;
1094 optind = 0;
1095 if (f & f_bogus || argc < 1) {
1096 usage(stderr);
1097 exit(EXIT_FAILURE);
1098 }
1099
1100 /* --- Dispatch to the correct subcommand handler --- */
1101
1102 return (findcmd(cmdtab, argv[0])->cmd(argc, argv));
1103
1104 #undef f_bogus
1105 }
1106
1107 /*----- That's all, folks -------------------------------------------------*/