Hash utilities: maintain a hash state object, not a bundle of arguments.
[u/mdw/catacomb] / dsig.c
CommitLineData
4bedc99b 1/* -*-c-*-
2 *
c65df279 3 * $Id$
4bedc99b 4 *
5 * Verify signatures on distribuitions of files
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
4bedc99b 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.
45c0fd36 18 *
4bedc99b 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.
45c0fd36 23 *
4bedc99b 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
4bedc99b 30/*----- Header files ------------------------------------------------------*/
31
cd6eca43
MW
32#define _FILE_OFFSET_BITS 64
33
4bedc99b 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
4bedc99b 49#include "getdate.h"
5c3f75ec 50#include "rand.h"
4bedc99b 51#include "ghash.h"
52#include "key.h"
53#include "key-data.h"
4bedc99b 54#include "noise.h"
5c3f75ec 55#include "cc.h"
4bedc99b 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
66enum {
67 /* --- Block tags --- */
68
69 T_IDENT = 0, /* An identifying marker */
4bedc99b 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
88static const char *tagtab[] = {
4e6e0188 89 "ident:", "keyid:",
4bedc99b 90 "comment:", "date:", "expires:", "file:",
91 "signature:",
92 0
93};
94
95static 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
105typedef 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
4bedc99b 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
124static 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
145static 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
163static 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
179static 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
196static 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;
18b3351a 206 if (getstring(fp, &d, GSF_FILE))
4bedc99b 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:
18b3351a 226 if (getstring(fp, &b->d, GSF_FILE | (bin ? GSF_RAW : 0)))
4bedc99b 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 {
18b3351a 241 if (getstring(fp, &b->d, GSF_FILE))
4bedc99b 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 {
18b3351a 259 if (getstring(fp, &b->d, GSF_FILE))
4bedc99b 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;
18b3351a 283 if (getstring(fp, &b->d, GSF_FILE))
4bedc99b 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 }
18b3351a
MW
290 if (tag == T_FILE &&
291 getstring(fp, &b->d, GSF_FILE | (bin ? GSF_RAW : 0)))
4bedc99b 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
314static void blob(block *b, dstr *d)
315{
316 DPUTC(d, b->tag);
317 switch (b->tag) {
318 case T_IDENT:
4bedc99b 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);
c65df279 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 }
4bedc99b 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
364static void bwrite(block *b, FILE *fp)
365{
366 fputs(tagtab[b->tag], fp);
367 putc(' ', fp);
368 switch (b->tag) {
369 case T_IDENT:
4bedc99b 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
413static 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)
4e6e0188 419 GH_HASH(h, d.buf, d.len);
4bedc99b 420 if (fp && bin)
421 fwrite(d.buf, d.len, 1, fp);
422 }
423 if (fp && !bin)
424 bwrite(b, fp);
425}
45c0fd36 426
4bedc99b 427/*----- Static variables --------------------------------------------------*/
428
429static const char *keyring = "keyring";
430
431/*----- Other shared functions --------------------------------------------*/
432
4bedc99b 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
444static 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;
4bedc99b 454 }
45c0fd36 455}
4bedc99b 456
457/*----- Signature generation ----------------------------------------------*/
458
459static int sign(int argc, char *argv[])
460{
18b3351a 461#define f_bogus 1u
16efd15b 462#define f_bin 2u
18b3351a 463#define f_nocheck 4u
4bedc99b 464
465 unsigned f = 0;
4e6e0188 466 const char *ki = "dsig";
4bedc99b 467 key_file kf;
468 key *k;
4e6e0188 469 sig *s;
07290a45 470 fhashstate fh;
4bedc99b 471 time_t exp = KEXP_EXPIRE;
472 unsigned verb = 0;
95d92463 473 const char *ifile = 0, *hfile = 0;
4bedc99b 474 const char *ofile = 0;
475 const char *c = 0;
5c3f75ec 476 const char *err;
4bedc99b 477 FILE *ifp, *ofp;
478 dstr d = DSTR_INIT;
95d92463 479 hfpctx hfp;
4bedc99b 480 block b;
95d92463 481 int e, hf, n;
4bedc99b 482
483 for (;;) {
484 static struct option opts[] = {
485 { "null", 0, 0, '0' },
486 { "binary", 0, 0, 'b' },
487 { "verbose", 0, 0, 'v' },
cd6eca43 488 { "progress", 0, 0, 'p' },
4bedc99b 489 { "quiet", 0, 0, 'q' },
4bedc99b 490 { "comment", OPTF_ARGREQ, 0, 'c' },
491 { "file", OPTF_ARGREQ, 0, 'f' },
95d92463 492 { "hashes", OPTF_ARGREQ, 0, 'h' },
4bedc99b 493 { "output", OPTF_ARGREQ, 0, 'o' },
4e6e0188 494 { "key", OPTF_ARGREQ, 0, 'k' },
4bedc99b 495 { "expire", OPTF_ARGREQ, 0, 'e' },
946c3f72 496 { "nocheck", OPTF_ARGREQ, 0, 'C' },
4bedc99b 497 { 0, 0, 0, 0 }
498 };
95d92463
MW
499 int i = mdwopt(argc, argv, "+0vpqbC" "c:" "f:h:o:" "k:e:",
500 opts, 0, 0, 0);
4bedc99b 501 if (i < 0)
502 break;
503 switch (i) {
504 case '0':
18b3351a 505 f |= GSF_RAW;
4bedc99b 506 break;
507 case 'b':
508 f |= f_bin;
509 break;
510 case 'v':
511 verb++;
512 break;
cd6eca43
MW
513 case 'p':
514 f |= FHF_PROGRESS;
515 break;
4bedc99b 516 case 'q':
517 if (verb > 0)
518 verb--;
519 break;
946c3f72 520 case 'C':
521 f |= f_nocheck;
522 break;
4bedc99b 523 case 'c':
524 c = optarg;
525 break;
526 case 'f':
527 ifile = optarg;
528 break;
95d92463
MW
529 case 'h':
530 hfile = optarg;
531 break;
4bedc99b 532 case 'o':
533 ofile = optarg;
534 break;
4bedc99b 535 case 'k':
536 ki = optarg;
537 break;
538 case 'e':
539 if (strcmp(optarg, "forever") == 0)
540 exp = KEXP_FOREVER;
541 else if ((exp = get_date(optarg, 0)) == -1)
542 die(EXIT_FAILURE, "bad expiry time");
543 break;
544 default:
545 f |= f_bogus;
546 break;
547 }
548 }
549 if (optind != argc || (f & f_bogus))
c65df279 550 die(EXIT_FAILURE, "Usage: sign [-OPTIONS]");
95d92463
MW
551 if (hfile && ifile)
552 die(EXIT_FAILURE, "Inconsistent options `-h' and `-f'");
4bedc99b 553
554 /* --- Locate the signing key --- */
555
213e565f 556 if (key_open(&kf, keyring, KOPEN_WRITE, key_moan, 0))
4bedc99b 557 die(EXIT_FAILURE, "couldn't open keyring `%s'", keyring);
4e6e0188 558 if ((k = key_bytag(&kf, ki)) == 0)
559 die(EXIT_FAILURE, "couldn't find key `%s'", ki);
4bedc99b 560 key_fulltag(k, &d);
561 if (exp == KEXP_FOREVER && k->exp != KEXP_FOREVER) {
562 die(EXIT_FAILURE, "key `%s' expires: can't create nonexpiring signature",
563 d.buf);
564 }
5c3f75ec 565 s = getsig(k, "dsig", 1);
566
567 /* --- Check the key --- */
568
946c3f72 569 if (!(f & f_nocheck) && (err = s->ops->check(s)) != 0)
5c3f75ec 570 moan("key `%s' fails check: %s", d.buf, err);
4bedc99b 571
572 /* --- Open files --- */
573
95d92463 574 if (hfile) ifile = hfile;
b92aaf61 575 if (!ifile || strcmp(ifile, "-") == 0)
4bedc99b 576 ifp = stdin;
18b3351a 577 else if ((ifp = fopen(ifile, (f & f_bin) ? "rb" : "r")) == 0) {
4bedc99b 578 die(EXIT_FAILURE, "couldn't open input file `%s': %s",
579 ifile, strerror(errno));
580 }
581
b92aaf61 582 if (!ofile || strcmp(ofile, "-") == 0)
4bedc99b 583 ofp = stdout;
584 else if ((ofp = fopen(ofile, (f & f_bin) ? "wb" : "w")) == 0) {
585 die(EXIT_FAILURE, "couldn't open output file `%s': %s",
586 ofile, strerror(errno));
587 }
588
589 /* --- Emit the start of the output --- */
590
591 binit(&b); b.tag = T_IDENT;
592 dstr_putf(&b.d, "%s, Catacomb version " VERSION, QUIS);
593 bemit(&b, ofp, 0, f & f_bin);
45c0fd36 594
4bedc99b 595 breset(&b); b.tag = T_KEYID; b.k = k->id;
4e6e0188 596 bemit(&b, ofp, 0, f & f_bin);
4bedc99b 597
598 /* --- Start hashing, and emit the datestamps and things --- */
599
600 {
601 time_t now = time(0);
602
4e6e0188 603 breset(&b); b.tag = T_DATE; b.t = now; bemit(&b, ofp, s->h, f & f_bin);
4bedc99b 604 if (exp == KEXP_EXPIRE)
605 exp = now + 86400 * 28;
4e6e0188 606 breset(&b); b.tag = T_EXPIRE; b.t = exp; bemit(&b, ofp, s->h, f & f_bin);
4bedc99b 607 if (c) {
608 breset(&b); b.tag = T_COMMENT; DPUTS(&b.d, c);
4e6e0188 609 bemit(&b, ofp, s->h, f & f_bin);
4bedc99b 610 }
611
612 if (!(f & f_bin))
613 putc('\n', ofp);
614 }
615
616 /* --- Now hash the various files --- */
617
95d92463
MW
618 if (hfile) {
619 hfp.f = f;
620 hfp.fp = ifp;
621 hfp.ee = &encodingtab[ENC_HEX];
622 hfp.gch = GH_CLASS(s->h);
623 hfp.dline = &d;
624 hfp.dfile = &b.d;
625
626 n = 0;
627 for (;;) {
628 breset(&b);
629 DENSURE(&b.b, hfp.gch->hashsz);
630 hfp.hbuf = (octet *)b.b.buf;
631 if (ferror(ofp)) { f |= f_bogus; break; }
632 if ((hf = hfparse(&hfp)) == HF_EOF) break;
633 n++;
634
635 switch (hf) {
636 case HF_HASH:
637 if (hfp.gch != GH_CLASS(s->h)) {
638 moan("%s:%d: incorrect hash function `%s' (should be `%s')",
639 hfile, n, hfp.gch->name, GH_CLASS(s->h)->name);
640 f |= f_bogus;
641 }
642 break;
643 case HF_BAD:
644 moan("%s:%d: invalid hash-file line", hfile, n);
645 f |= f_bogus;
646 break;
647 case HF_FILE:
648 b.tag = T_FILE;
649 b.b.len += hfp.gch->hashsz;
650 bemit(&b, ofp, s->h, f & f_bin);
651 break;
652 }
653 }
654 } else {
655 for (;;) {
4bedc99b 656
95d92463 657 /* --- Stop on an output error --- */
4bedc99b 658
95d92463
MW
659 if (ferror(ofp)) {
660 f |= f_bogus;
661 break;
662 }
4bedc99b 663
95d92463 664 /* --- Read the next filename to hash --- */
4bedc99b 665
07290a45 666 fhash_init(&fh, GH_CLASS(s->h), f | FHF_BINARY);
95d92463
MW
667 breset(&b);
668 if (getstring(ifp, &b.d, GSF_FILE | f))
669 break;
670 b.tag = T_FILE;
671 DENSURE(&b.b, GH_CLASS(s->h)->hashsz);
07290a45 672 if (fhash(&fh, b.d.buf, b.b.buf)) {
95d92463
MW
673 moan("error reading `%s': %s", b.d.buf, strerror(errno));
674 f |= f_bogus;
675 } else {
676 b.b.len += GH_CLASS(s->h)->hashsz;
677 if (verb) {
678 fhex(stderr, b.b.buf, b.b.len);
679 fprintf(stderr, " %s\n", b.d.buf);
680 }
681 bemit(&b, ofp, s->h, f & f_bin);
4bedc99b 682 }
07290a45 683 fhash_free(&fh);
4bedc99b 684 }
685 }
686
687 /* --- Create the signature --- */
688
689 if (!(f & f_bogus)) {
690 breset(&b);
691 b.tag = T_SIGNATURE;
4e6e0188 692 if ((e = s->ops->doit(s, &b.b)) != 0) {
4bedc99b 693 moan("error creating signature: %s", key_strerror(e));
694 f |= f_bogus;
695 }
696 if (!(f & f_bogus)) {
697 bemit(&b, ofp, 0, f & f_bin);
698 key_used(&kf, k, exp);
699 }
700 }
701
702 /* --- Tidy up at the end --- */
703
4e6e0188 704 freesig(s);
4bedc99b 705 bdestroy(&b);
706 if (ifile)
707 fclose(ifp);
708 if (ofile) {
709 if (fclose(ofp))
710 f |= f_bogus;
711 } else {
712 if (fflush(ofp))
713 f |= f_bogus;
714 }
715 if ((e = key_close(&kf)) != 0) {
716 switch (e) {
717 case KWRITE_FAIL:
718 die(EXIT_FAILURE, "couldn't write file `%s': %s",
719 keyring, strerror(errno));
720 case KWRITE_BROKEN:
721 die(EXIT_FAILURE, "keyring file `%s' broken: %s (repair manually)",
722 keyring, strerror(errno));
723 }
724 }
725 if (f & f_bogus)
726 die(EXIT_FAILURE, "error(s) occurred while creating signature");
727 return (EXIT_SUCCESS);
16efd15b 728
16efd15b 729#undef f_bin
730#undef f_bogus
946c3f72 731#undef f_nocheck
4bedc99b 732}
733
734/*----- Signature verification --------------------------------------------*/
735
736static int verify(int argc, char *argv[])
737{
16efd15b 738#define f_bogus 1u
739#define f_bin 2u
740#define f_ok 4u
946c3f72 741#define f_nocheck 8u
4bedc99b 742
743 unsigned f = 0;
744 unsigned verb = 1;
745 key_file kf;
746 key *k = 0;
4e6e0188 747 sig *s;
4bedc99b 748 dstr d = DSTR_INIT;
5c3f75ec 749 const char *err;
07290a45 750 fhashstate fh;
4bedc99b 751 FILE *fp;
752 block b;
753 int e;
754
755 /* --- Parse the options --- */
756
757 for (;;) {
758 static struct option opts[] = {
759 { "verbose", 0, 0, 'v' },
cd6eca43 760 { "progress", 0, 0, 'p' },
4bedc99b 761 { "quiet", 0, 0, 'q' },
946c3f72 762 { "nocheck", 0, 0, 'C' },
4bedc99b 763 { 0, 0, 0, 0 }
764 };
cd6eca43 765 int i = mdwopt(argc, argv, "+vpqC", opts, 0, 0, 0);
4bedc99b 766 if (i < 0)
767 break;
768 switch (i) {
769 case 'v':
770 verb++;
771 break;
cd6eca43
MW
772 case 'p':
773 f |= FHF_PROGRESS;
774 break;
4bedc99b 775 case 'q':
776 if (verb)
777 verb--;
778 break;
946c3f72 779 case 'C':
780 f |= f_nocheck;
781 break;
4bedc99b 782 default:
783 f |= f_bogus;
784 break;
785 }
786 }
787 argc -= optind;
788 argv += optind;
789 if ((f & f_bogus) || argc > 1)
946c3f72 790 die(EXIT_FAILURE, "Usage: verify [-qvC] [FILE]");
4bedc99b 791
792 /* --- Open the key file, and start reading the input file --- */
793
213e565f 794 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
4bedc99b 795 die(EXIT_FAILURE, "couldn't open keyring `%s'\n", keyring);
796 if (argc < 1)
797 fp = stdin;
798 else {
799 if ((fp = fopen(argv[0], "rb")) == 0) {
800 die(EXIT_FAILURE, "couldn't open file `%s': %s\n",
801 argv[0], strerror(errno));
802 }
803 if (getc(fp) == 0) {
804 ungetc(0, fp);
805 f |= f_bin;
806 } else {
807 fclose(fp);
808 if ((fp = fopen(argv[0], "r")) == 0) {
809 die(EXIT_FAILURE, "couldn't open file `%s': %s\n",
810 argv[0], strerror(errno));
811 }
812 }
813 }
814
815 /* --- Read the introductory matter --- */
816
817 binit(&b);
4e6e0188 818 for (;;) {
4bedc99b 819 breset(&b);
820 e = bget(&b, fp, f & f_bin);
821 if (e < 0)
4e6e0188 822 die(EXIT_FAILURE, "error reading packet: %s", errtab[-e]);
4bedc99b 823 if (e >= T_BEGIN)
824 break;
825 switch (e) {
826 case T_IDENT:
827 if (verb > 2)
828 printf("INFO ident: `%s'\n", b.d.buf);
829 break;
4bedc99b 830 case T_KEYID:
831 if ((k = key_byid(&kf, b.k)) == 0) {
832 if (verb)
833 printf("FAIL key %08lx not found\n", (unsigned long)b.k);
834 exit(EXIT_FAILURE);
835 }
836 if (verb > 2) {
837 DRESET(&b.d);
838 key_fulltag(k, &b.d);
839 printf("INFO key: %s\n", b.d.buf);
840 }
841 break;
842 default:
843 die(EXIT_FAILURE, "(internal) unknown packet type\n");
844 break;
845 }
846 }
847
848 /* --- Initialize the hash function and start reading hashed packets --- */
849
4bedc99b 850 if (!k) {
851 if (verb)
852 puts("FAIL no keyid packet found");
853 exit(EXIT_FAILURE);
854 }
bd5efd74 855
5c3f75ec 856 s = getsig(k, "dsig", 0);
946c3f72 857 if (!(f & f_nocheck) && verb && (err = s->ops->check(s)) != 0)
5c3f75ec 858 printf("WARN public key fails check: %s", err);
859
07290a45 860 fhash_init(&fh, GH_CLASS(s->h), f | FHF_BINARY);
4bedc99b 861 for (;;) {
862 switch (e) {
863 case T_COMMENT:
864 if (verb > 1)
865 printf("INFO comment: `%s'\n", b.d.buf);
4e6e0188 866 bemit(&b, 0, s->h, 0);
4bedc99b 867 break;
868 case T_DATE:
869 if (verb > 2) {
870 DRESET(&b.d);
871 timestring(b.t, &b.d);
872 printf("INFO date: %s\n", b.d.buf);
873 }
4e6e0188 874 bemit(&b, 0, s->h, 0);
4bedc99b 875 break;
876 case T_EXPIRE: {
877 time_t now = time(0);
c65df279 878 if (b.t != KEXP_FOREVER && b.t < now) {
4bedc99b 879 if (verb > 1)
880 puts("BAD signature has expired");
881 f |= f_bogus;
882 }
883 if (verb > 2) {
884 DRESET(&b.d);
885 timestring(b.t, &b.d);
886 printf("INFO expires: %s\n", b.d.buf);
887 }
4e6e0188 888 bemit(&b, 0, s->h, 0);
4bedc99b 889 } break;
890 case T_FILE:
891 DRESET(&d);
4e6e0188 892 DENSURE(&d, GH_CLASS(s->h)->hashsz);
07290a45 893 if (fhash(&fh, b.d.buf, d.buf)) {
4bedc99b 894 if (verb > 1) {
895 printf("BAD error reading file `%s': %s\n",
896 b.d.buf, strerror(errno));
897 }
898 f |= f_bogus;
4e6e0188 899 } else if (b.b.len != GH_CLASS(s->h)->hashsz ||
4bedc99b 900 memcmp(d.buf, b.b.buf, b.b.len) != 0) {
901 if (verb > 1)
902 printf("BAD file `%s' has incorrect hash\n", b.d.buf);
903 f |= f_bogus;
904 } else if (verb > 3) {
905 fputs("INFO hash: ", stdout);
906 fhex(stdout, b.b.buf, b.b.len);
907 printf(" %s\n", b.d.buf);
908 }
4e6e0188 909 bemit(&b, 0, s->h, 0);
4bedc99b 910 break;
911 case T_SIGNATURE:
4e6e0188 912 if (s->ops->doit(s, &b.b)) {
913 if (verb > 1)
914 puts("BAD bad signature");
4bedc99b 915 f |= f_bogus;
916 } else if (verb > 2)
917 puts("INFO good signature");
918 goto done;
919 default:
920 if (verb)
921 printf("FAIL invalid packet type %i\n", e);
922 exit(EXIT_FAILURE);
923 break;
924 }
925 breset(&b);
926 e = bget(&b, fp, f & f_bin);
927 if (e < 0) {
928 if (verb)
929 printf("FAIL error reading packet: %s\n", errtab[-e]);
930 exit(EXIT_FAILURE);
931 }
932 }
933done:
07290a45 934 fhash_free(&fh);
4bedc99b 935 bdestroy(&b);
936 dstr_destroy(&d);
4e6e0188 937 freesig(s);
4bedc99b 938 key_close(&kf);
939 if (fp != stdin)
940 fclose(fp);
941 if (verb) {
942 if (f & f_bogus)
943 puts("FAIL signature invalid");
944 else
945 puts("OK signature verified");
946 }
947 return (f & f_bogus ? EXIT_FAILURE : EXIT_SUCCESS);
16efd15b 948
949#undef f_bogus
950#undef f_bin
951#undef f_ok
946c3f72 952#undef f_nocheck
4bedc99b 953}
954
955/*----- Main code ---------------------------------------------------------*/
956
c65df279 957#define LISTS(LI) \
958 LI("Lists", list, \
959 listtab[i].name, listtab[i].name) \
960 LI("Signature schemes", sig, \
961 sigtab[i].name, sigtab[i].name) \
962 LI("Hash functions", hash, \
963 ghashtab[i], ghashtab[i]->name)
964
965MAKELISTTAB(listtab, LISTS)
966
967int cmd_show(int argc, char *argv[])
968{
969 return (displaylists(listtab, argv + 1));
970}
971
972static int cmd_help(int, char **);
4bedc99b 973
974static cmd cmdtab[] = {
c65df279 975 { "help", cmd_help, "help [COMMAND...]" },
976 { "show", cmd_show, "show [ITEM...]" },
4bedc99b 977 { "sign", sign,
cd6eca43 978 "sign [-0bpqvC] [-c COMMENT] [-k TAG] [-e EXPIRE]\n\t\
95d92463 979[-f FILE] [-h FILE] [-o OUTPUT]",
5c3f75ec 980 "\
bd5efd74 981Options:\n\
982\n\
983-0, --null Read null-terminated filenames from stdin.\n\
984-b, --binary Produce a binary output file.\n\
985-q, --quiet Produce fewer messages while working.\n\
986-v, --verbose Produce more messages while working.\n\
cd6eca43 987-p, --progress Show progress on large files.\n\
946c3f72 988-C, --nocheck Don't check the private key.\n\
bd5efd74 989-c, --comment=COMMENT Include COMMENT in the output file.\n\
990-f, --file=FILE Read filenames to hash from FILE.\n\
95d92463 991-h, --hashes=FILE Read precomputed hashes from FILE.\n\
bd5efd74 992-o, --output=FILE Write the signed result to FILE.\n\
4e6e0188 993-k, --key=TAG Use a key named by TAG.\n\
bd5efd74 994-e, --expire=TIME The signature should expire after TIME.\n\
995" },
4bedc99b 996 { "verify", verify,
cd6eca43 997 "verify [-pqvC] [FILE]", "\
bd5efd74 998Options:\n\
999\n\
1000-q, --quiet Produce fewer messages while working.\n\
1001-v, --verbose Produce more messages while working.\n\
cd6eca43 1002-p, --progress Show progress on large files.\n\
946c3f72 1003-C, --nocheck Don't check the public key.\n\
bd5efd74 1004" },
4bedc99b 1005 { 0, 0, 0 }
1006};
1007
c65df279 1008static int cmd_help(int argc, char **argv)
bd5efd74 1009{
c65df279 1010 sc_help(cmdtab, stdout, argv + 1);
1011 return (0);
bd5efd74 1012}
1013
c65df279 1014void version(FILE *fp)
4bedc99b 1015{
1016 pquis(fp, "$, Catacomb version " VERSION "\n");
1017}
1018
1019static void usage(FILE *fp)
1020{
c65df279 1021 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
4bedc99b 1022}
1023
c65df279 1024void help_global(FILE *fp)
4bedc99b 1025{
c65df279 1026 usage(fp);
1027 fputs("\n\
4bedc99b 1028Create and verify signatures on lists of files.\n\
c65df279 1029\n\
1030Global command-line options:\n\
1031\n\
1032-h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1033-v, --version Show program version number.\n\
1034-u, --usage Show a terse usage message.\n\
1035\n\
1036-k, --keyring=FILE Read keys from FILE.\n",
1037 fp);
4bedc99b 1038}
1039
1040/* --- @main@ --- *
1041 *
1042 * Arguments: @int argc@ = number of command line arguments
1043 * @char *argv[]@ = vector of command line arguments
1044 *
1045 * Returns: Zero if successful, nonzero otherwise.
1046 *
1047 * Use: Signs or verifies signatures on lists of files. Useful for
1048 * ensuring that a distribution is unmolested.
1049 */
1050
1051int main(int argc, char *argv[])
1052{
1053 unsigned f = 0;
4bedc99b 1054
16efd15b 1055#define f_bogus 1u
4bedc99b 1056
1057 /* --- Initialize the library --- */
1058
1059 ego(argv[0]);
1060 sub_init();
1061 rand_noisesrc(RAND_GLOBAL, &noise_source);
1062 rand_seed(RAND_GLOBAL, 160);
1063
1064 /* --- Parse options --- */
1065
1066 for (;;) {
1067 static struct option opts[] = {
1068 { "help", 0, 0, 'h' },
1069 { "version", 0, 0, 'v' },
1070 { "usage", 0, 0, 'u' },
1071 { "keyring", OPTF_ARGREQ, 0, 'k' },
1072 { 0, 0, 0, 0 }
1073 };
1074 int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0);
1075 if (i < 0)
1076 break;
1077 switch (i) {
1078 case 'h':
c65df279 1079 sc_help(cmdtab, stdout, argv + optind);
4bedc99b 1080 exit(0);
1081 break;
1082 case 'v':
1083 version(stdout);
1084 exit(0);
1085 break;
1086 case 'u':
1087 usage(stdout);
1088 exit(0);
1089 case 'k':
1090 keyring = optarg;
1091 break;
1092 default:
1093 f |= f_bogus;
1094 break;
1095 }
1096 }
1097
1098 argc -= optind;
1099 argv += optind;
1100 optind = 0;
1101 if (f & f_bogus || argc < 1) {
1102 usage(stderr);
1103 exit(EXIT_FAILURE);
1104 }
1105
1106 /* --- Dispatch to the correct subcommand handler --- */
1107
c65df279 1108 return (findcmd(cmdtab, argv[0])->cmd(argc, argv));
16efd15b 1109
1110#undef f_bogus
4bedc99b 1111}
1112
1113/*----- That's all, folks -------------------------------------------------*/