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