4bedc99b |
1 | /* -*-c-*- |
2 | * |
c65df279 |
3 | * $Id$ |
4bedc99b |
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 | |
4bedc99b |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
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 | |
64 | enum { |
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 | |
86 | static const char *tagtab[] = { |
4e6e0188 |
87 | "ident:", "keyid:", |
4bedc99b |
88 | "comment:", "date:", "expires:", "file:", |
89 | "signature:", |
90 | 0 |
91 | }; |
92 | |
93 | static 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 | |
103 | typedef 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 | |
111 | /* --- @getstring@ --- * |
112 | * |
113 | * Arguments: @FILE *fp@ = stream from which to read |
114 | * @dstr *d@ = destination string |
115 | * @unsigned raw@ = raw or cooked read |
116 | * |
117 | * Returns: Zero if OK, nonzero on end-of-file. |
118 | * |
119 | * Use: Reads a filename (or something similar) from a stream. |
120 | */ |
121 | |
122 | static int getstring(FILE *fp, dstr *d, unsigned raw) |
123 | { |
124 | int ch; |
125 | int q = 0; |
126 | |
127 | /* --- Raw: just read exactly what's written up to a null byte --- */ |
128 | |
129 | if (raw) { |
130 | if ((ch = getc(fp)) == EOF) |
131 | return (EOF); |
132 | for (;;) { |
133 | if (!ch) |
134 | break; |
135 | DPUTC(d, ch); |
136 | if ((ch = getc(fp)) == EOF) |
137 | break; |
138 | } |
139 | DPUTZ(d); |
140 | return (0); |
141 | } |
142 | |
143 | /* --- Skip as far as whitespace --- * |
144 | * |
145 | * Also skip past comments. |
146 | */ |
147 | |
148 | again: |
149 | ch = getc(fp); |
d470270a |
150 | while (isspace(ch)) |
4bedc99b |
151 | ch = getc(fp); |
152 | if (ch == '#') { |
153 | do ch = getc(fp); while (ch != '\n' && ch != EOF); |
154 | goto again; |
155 | } |
156 | if (ch == EOF) |
157 | return (EOF); |
158 | |
159 | /* --- If the character is a quote then read a quoted string --- */ |
160 | |
161 | switch (ch) { |
162 | case '`': |
163 | ch = '\''; |
164 | case '\'': |
165 | case '\"': |
166 | q = ch; |
167 | ch = getc(fp); |
168 | break; |
169 | } |
170 | |
171 | /* --- Now read all sorts of interesting things --- */ |
172 | |
173 | for (;;) { |
174 | |
175 | /* --- Handle an escaped thing --- */ |
176 | |
177 | if (ch == '\\') { |
178 | ch = getc(fp); |
179 | if (ch == EOF) |
180 | break; |
181 | switch (ch) { |
365c07f5 |
182 | case 'a': ch = '\a'; break; |
4bedc99b |
183 | case 'b': ch = '\b'; break; |
184 | case 'f': ch = '\f'; break; |
185 | case 'n': ch = '\n'; break; |
186 | case 'r': ch = '\r'; break; |
187 | case 't': ch = '\t'; break; |
188 | case 'v': ch = '\v'; break; |
189 | } |
190 | DPUTC(d, ch); |
365c07f5 |
191 | ch = getc(fp); |
4bedc99b |
192 | continue; |
193 | } |
194 | |
195 | /* --- If it's a quote or some other end marker then stop --- */ |
196 | |
197 | if (ch == q || (!q && isspace((unsigned char)ch))) |
198 | break; |
199 | |
200 | /* --- Otherwise contribute and continue --- */ |
201 | |
202 | DPUTC(d, ch); |
203 | if ((ch = getc(fp)) == EOF) |
204 | break; |
205 | } |
206 | |
207 | /* --- Done --- */ |
208 | |
209 | DPUTZ(d); |
210 | return (0); |
211 | } |
212 | |
213 | /* --- @putstring@ --- * |
214 | * |
215 | * Arguments: @FILE *fp@ = stream to write on |
216 | * @const char *p@ = pointer to text |
217 | * @unsigned raw@ = whether the string is to be written raw |
218 | * |
219 | * Returns: --- |
220 | * |
221 | * Use: Emits a string to a stream. |
222 | */ |
223 | |
224 | static void putstring(FILE *fp, const char *p, unsigned raw) |
225 | { |
226 | size_t sz = strlen(p); |
227 | unsigned qq; |
228 | const char *q; |
229 | |
230 | /* --- Just write the string null terminated if raw --- */ |
231 | |
232 | if (raw) { |
233 | fwrite(p, 1, sz + 1, fp); |
234 | return; |
235 | } |
236 | |
237 | /* --- Check for any dodgy characters --- */ |
238 | |
239 | qq = 0; |
240 | for (q = p; *q; q++) { |
241 | if (isspace((unsigned char)*q)) { |
242 | qq = '\"'; |
243 | break; |
244 | } |
245 | } |
246 | |
247 | if (qq) |
248 | putc(qq, fp); |
249 | |
250 | /* --- Emit the string --- */ |
251 | |
252 | for (q = p; *q; q++) { |
253 | switch (*q) { |
254 | case '\a': fputc('\\', fp); fputc('a', fp); break; |
255 | case '\b': fputc('\\', fp); fputc('b', fp); break; |
256 | case '\f': fputc('\\', fp); fputc('f', fp); break; |
257 | case '\n': fputc('\\', fp); fputc('n', fp); break; |
258 | case '\r': fputc('\\', fp); fputc('r', fp); break; |
259 | case '\t': fputc('\\', fp); fputc('t', fp); break; |
260 | case '\v': fputc('\\', fp); fputc('v', fp); break; |
261 | case '`': fputc('\\', fp); fputc('`', fp); break; |
262 | case '\'': fputc('\\', fp); fputc('\'', fp); break; |
263 | case '\"': fputc('\\', fp); fputc('\"', fp); break; |
264 | default: |
265 | putc(*q, fp); |
266 | break; |
267 | } |
268 | } |
269 | |
270 | /* --- Done --- */ |
271 | |
272 | if (qq) |
273 | putc(qq, fp); |
274 | } |
275 | |
276 | /* --- @timestring@ --- * |
277 | * |
278 | * Arguments: @time_t t@ = a timestamp |
279 | * @dstr *d@ = a string to write on |
280 | * |
281 | * Returns: --- |
282 | * |
283 | * Use: Writes a textual representation of the timestamp to the |
284 | * string. |
285 | */ |
286 | |
287 | static void timestring(time_t t, dstr *d) |
288 | { |
289 | if (t == KEXP_FOREVER) |
290 | DPUTS(d, "forever"); |
291 | else { |
292 | struct tm *tm = localtime(&t); |
293 | DENSURE(d, 32); |
294 | d->len += strftime(d->buf + d->len, 32, "%Y-%m-%d %H:%M:%S %Z", tm); |
295 | DPUTZ(d); |
296 | } |
297 | } |
298 | |
299 | /* --- @breset@ --- * |
300 | * |
301 | * Arguments: @block *b@ = block to reset |
302 | * |
303 | * Returns: --- |
304 | * |
305 | * Use: Resets a block so that more stuff can be put in it. |
306 | */ |
307 | |
308 | static void breset(block *b) |
309 | { |
310 | b->tag = 0; |
311 | DRESET(&b->d); |
312 | DRESET(&b->b); |
313 | b->k = 0; |
314 | b->t = KEXP_EXPIRE; |
315 | } |
316 | |
317 | /* --- @binit@ --- * |
318 | * |
319 | * Arguments: @block *b@ = block to initialize |
320 | * |
321 | * Returns: --- |
322 | * |
323 | * Use: Initializes a block as something to read into. |
324 | */ |
325 | |
326 | static void binit(block *b) |
327 | { |
328 | dstr_create(&b->d); |
329 | dstr_create(&b->b); |
330 | breset(b); |
331 | } |
332 | |
333 | /* --- @bdestroy@ --- * |
334 | * |
335 | * Arguments: @block *b@ = block to destroy |
336 | * |
337 | * Returns: --- |
338 | * |
339 | * Use: Destroys a block's contents. |
340 | */ |
341 | |
342 | static void bdestroy(block *b) |
343 | { |
344 | dstr_destroy(&b->d); |
345 | dstr_destroy(&b->b); |
346 | } |
347 | |
348 | /* --- @bget@ --- * |
349 | * |
350 | * Arguments: @block *b@ = pointer to block |
351 | * @FILE *fp@ = stream to read from |
352 | * @unsigned bin@ = binary switch |
353 | * |
354 | * Returns: Tag of block, or an error tag. |
355 | * |
356 | * Use: Reads a block from a stream. |
357 | */ |
358 | |
359 | static int bget(block *b, FILE *fp, unsigned bin) |
360 | { |
361 | int tag; |
362 | |
363 | /* --- Read the tag --- */ |
364 | |
365 | if (bin) |
366 | tag = getc(fp); |
367 | else { |
368 | dstr d = DSTR_INIT; |
369 | if (getstring(fp, &d, 0)) |
370 | return (E_EOF); |
371 | for (tag = 0; tagtab[tag]; tag++) { |
372 | if (strcmp(tagtab[tag], d.buf) == 0) |
373 | goto done; |
374 | } |
375 | return (E_TAG); |
376 | done:; |
377 | } |
378 | |
379 | /* --- Decide what to do next --- */ |
380 | |
381 | breset(b); |
382 | b->tag = tag; |
383 | switch (tag) { |
384 | |
385 | /* --- Reading of strings --- */ |
386 | |
387 | case T_IDENT: |
388 | case T_COMMENT: |
4bedc99b |
389 | if (getstring(fp, &b->d, bin)) |
390 | return (E_EOF); |
391 | break; |
392 | |
393 | /* --- Timestamps --- */ |
394 | |
395 | case T_DATE: |
396 | case T_EXPIRE: |
397 | if (bin) { |
398 | octet buf[8]; |
399 | if (fread(buf, sizeof(buf), 1, fp) < 1) |
400 | return (E_EOF); |
401 | b->t = ((time_t)(((LOAD32(buf + 0) << 16) << 16) & ~MASK32) | |
402 | (time_t)LOAD32(buf + 4)); |
403 | } else { |
404 | if (getstring(fp, &b->d, 0)) |
405 | return (E_EOF); |
406 | if (strcmp(b->d.buf, "forever") == 0) |
407 | b->t = KEXP_FOREVER; |
408 | else if ((b->t = get_date(b->d.buf, 0)) == -1) |
409 | return (E_DATE); |
410 | } |
411 | break; |
412 | |
413 | /* --- Key ids --- */ |
414 | |
415 | case T_KEYID: |
416 | if (bin) { |
417 | octet buf[4]; |
418 | if (fread(buf, sizeof(buf), 1, fp) < 1) |
419 | return (E_EOF); |
420 | b->k = LOAD32(buf); |
421 | } else { |
422 | if (getstring(fp, &b->d, 0)) |
423 | return (E_EOF); |
424 | b->k = strtoul(b->d.buf, 0, 16); |
425 | } |
426 | break; |
427 | |
428 | /* --- Reading of binary data --- */ |
429 | |
430 | case T_FILE: |
431 | case T_SIGNATURE: |
432 | if (bin) { |
433 | octet buf[2]; |
434 | uint32 sz; |
435 | if (fread(buf, sizeof(buf), 1, fp) < 1) |
436 | return (E_EOF); |
437 | sz = LOAD16(buf); |
438 | if (sz > 4096) |
439 | return (E_BIN); |
440 | DENSURE(&b->b, sz); |
441 | if (fread(b->b.buf + b->b.len, 1, sz, fp) < sz) |
442 | return (E_EOF); |
443 | b->b.len += sz; |
444 | } else { |
445 | base64_ctx b64; |
446 | if (getstring(fp, &b->d, 0)) |
447 | return (E_EOF); |
448 | base64_init(&b64); |
449 | base64_decode(&b64, b->d.buf, b->d.len, &b->b); |
450 | base64_decode(&b64, 0, 0, &b->b); |
451 | DRESET(&b->d); |
452 | } |
453 | if (tag == T_FILE && getstring(fp, &b->d, bin)) |
454 | return (E_EOF); |
455 | break; |
456 | |
457 | /* --- Anything else --- */ |
458 | |
459 | default: |
460 | return (E_TAG); |
461 | } |
462 | |
463 | return (tag); |
464 | } |
465 | |
466 | /* --- @blob@ --- * |
467 | * |
468 | * Arguments: @block *b@ = pointer to block to emit |
469 | * @dstr *d@ = output buffer |
470 | * |
471 | * Returns: --- |
472 | * |
473 | * Use: Encodes a block in a binary format. |
474 | */ |
475 | |
476 | static void blob(block *b, dstr *d) |
477 | { |
478 | DPUTC(d, b->tag); |
479 | switch (b->tag) { |
480 | case T_IDENT: |
4bedc99b |
481 | case T_COMMENT: |
482 | DPUTD(d, &b->d); |
483 | DPUTC(d, 0); |
484 | break; |
485 | case T_DATE: |
486 | case T_EXPIRE: |
487 | DENSURE(d, 8); |
c65df279 |
488 | if (b->t == KEXP_FOREVER) { |
489 | STORE32(d->buf + d->len, 0xffffffff); |
490 | STORE32(d->buf + d->len + 4, 0xffffffff); |
491 | } else { |
492 | STORE32(d->buf + d->len, ((b->t & ~MASK32) >> 16) >> 16); |
493 | STORE32(d->buf + d->len + 4, b->t); |
494 | } |
4bedc99b |
495 | d->len += 8; |
496 | break; |
497 | case T_KEYID: |
498 | DENSURE(d, 4); |
499 | STORE32(d->buf + d->len, b->k); |
500 | d->len += 4; |
501 | break; |
502 | case T_FILE: |
503 | case T_SIGNATURE: |
504 | DENSURE(d, 2); |
505 | STORE16(d->buf + d->len, b->b.len); |
506 | d->len += 2; |
507 | DPUTD(d, &b->b); |
508 | if (b->tag == T_FILE) { |
509 | DPUTD(d, &b->d); |
510 | DPUTC(d, 0); |
511 | } |
512 | break; |
513 | } |
514 | } |
515 | |
516 | /* --- @bwrite@ --- * |
517 | * |
518 | * Arguments: @block *b@ = pointer to block to write |
519 | * @FILE *fp@ = stream to write on |
520 | * |
521 | * Returns: --- |
522 | * |
523 | * Use: Writes a block on a stream in a textual format. |
524 | */ |
525 | |
526 | static void bwrite(block *b, FILE *fp) |
527 | { |
528 | fputs(tagtab[b->tag], fp); |
529 | putc(' ', fp); |
530 | switch (b->tag) { |
531 | case T_IDENT: |
4bedc99b |
532 | case T_COMMENT: |
533 | putstring(fp, b->d.buf, 0); |
534 | break; |
535 | case T_DATE: |
536 | case T_EXPIRE: { |
537 | dstr d = DSTR_INIT; |
538 | timestring(b->t, &d); |
539 | putstring(fp, d.buf, 0); |
540 | dstr_destroy(&d); |
541 | } break; |
542 | case T_KEYID: |
543 | fprintf(fp, "%08lx", (unsigned long)b->k); |
544 | break; |
545 | case T_FILE: |
546 | case T_SIGNATURE: { |
547 | dstr d = DSTR_INIT; |
548 | base64_ctx b64; |
549 | base64_init(&b64); |
550 | b64.maxline = 0; |
551 | base64_encode(&b64, b->b.buf, b->b.len, &d); |
552 | base64_encode(&b64, 0, 0, &d); |
553 | dstr_write(&d, fp); |
554 | if (b->tag == T_FILE) { |
555 | putc(' ', fp); |
556 | putstring(fp, b->d.buf, 0); |
557 | } |
558 | } break; |
559 | } |
560 | putc('\n', fp); |
561 | } |
562 | |
563 | /* --- @bemit@ --- * |
564 | * |
565 | * Arguments: @block *b@ = pointer to block to write |
566 | * @FILE *fp@ = file to write on |
567 | * @ghash *h@ = pointer to hash function |
568 | * @unsigned bin@ = binary/text flag |
569 | * |
570 | * Returns: --- |
571 | * |
572 | * Use: Spits out a block properly. |
573 | */ |
574 | |
575 | static void bemit(block *b, FILE *fp, ghash *h, unsigned bin) |
576 | { |
577 | if (h || (fp && bin)) { |
578 | dstr d = DSTR_INIT; |
579 | blob(b, &d); |
580 | if (h) |
4e6e0188 |
581 | GH_HASH(h, d.buf, d.len); |
4bedc99b |
582 | if (fp && bin) |
583 | fwrite(d.buf, d.len, 1, fp); |
584 | } |
585 | if (fp && !bin) |
586 | bwrite(b, fp); |
587 | } |
588 | |
589 | /*----- Static variables --------------------------------------------------*/ |
590 | |
591 | static const char *keyring = "keyring"; |
592 | |
593 | /*----- Other shared functions --------------------------------------------*/ |
594 | |
4bedc99b |
595 | /* --- @fhash@ --- * |
596 | * |
597 | * Arguments: @const gchash *c@ = pointer to hash class |
598 | * @const char *file@ = file to hash |
e45700eb |
599 | * @void *b@ = pointer to output buffer |
4bedc99b |
600 | * |
601 | * Returns: Zero if it worked, or nonzero for a system error. |
602 | * |
603 | * Use: Hashes a file. |
604 | */ |
605 | |
e45700eb |
606 | static int fhash(const gchash *c, const char *file, void *b) |
4bedc99b |
607 | { |
608 | FILE *fp = fopen(file, "rb"); |
4e6e0188 |
609 | ghash *h = GH_INIT(c); |
4bedc99b |
610 | char buf[4096]; |
611 | size_t sz; |
612 | int rc = 0; |
613 | |
614 | if (!fp) |
615 | return (-1); |
616 | while ((sz = fread(buf, 1, sizeof(buf), fp)) > 0) |
4e6e0188 |
617 | GH_HASH(h, buf, sz); |
4bedc99b |
618 | if (ferror(fp)) |
619 | rc = -1; |
4e6e0188 |
620 | GH_DONE(h, b); |
621 | GH_DESTROY(h); |
4bedc99b |
622 | fclose(fp); |
623 | return (rc); |
624 | } |
625 | |
626 | /* --- @fhex@ --- * |
627 | * |
628 | * Arguments: @FILE *fp@ = file to write on |
629 | * @const void *p@ = pointer to data to be written |
630 | * @size_t sz@ = size of the data to write |
631 | * |
632 | * Returns: --- |
633 | * |
634 | * Use: Emits a hex dump to a stream. |
635 | */ |
636 | |
637 | static void fhex(FILE *fp, const void *p, size_t sz) |
638 | { |
639 | const octet *q = p; |
640 | if (!sz) |
641 | return; |
642 | for (;;) { |
643 | fprintf(fp, "%02x", *q++); |
644 | sz--; |
645 | if (!sz) |
646 | break; |
4bedc99b |
647 | } |
648 | } |
649 | |
650 | /*----- Signature generation ----------------------------------------------*/ |
651 | |
652 | static int sign(int argc, char *argv[]) |
653 | { |
16efd15b |
654 | #define f_raw 1u |
655 | #define f_bin 2u |
656 | #define f_bogus 4u |
4bedc99b |
657 | |
658 | unsigned f = 0; |
4e6e0188 |
659 | const char *ki = "dsig"; |
4bedc99b |
660 | key_file kf; |
661 | key *k; |
4e6e0188 |
662 | sig *s; |
4bedc99b |
663 | time_t exp = KEXP_EXPIRE; |
664 | unsigned verb = 0; |
665 | const char *ifile = 0; |
666 | const char *ofile = 0; |
667 | const char *c = 0; |
5c3f75ec |
668 | const char *err; |
4bedc99b |
669 | FILE *ifp, *ofp; |
670 | dstr d = DSTR_INIT; |
671 | block b; |
672 | int e; |
673 | |
674 | for (;;) { |
675 | static struct option opts[] = { |
676 | { "null", 0, 0, '0' }, |
677 | { "binary", 0, 0, 'b' }, |
678 | { "verbose", 0, 0, 'v' }, |
679 | { "quiet", 0, 0, 'q' }, |
4bedc99b |
680 | { "comment", OPTF_ARGREQ, 0, 'c' }, |
681 | { "file", OPTF_ARGREQ, 0, 'f' }, |
682 | { "output", OPTF_ARGREQ, 0, 'o' }, |
4e6e0188 |
683 | { "key", OPTF_ARGREQ, 0, 'k' }, |
4bedc99b |
684 | { "expire", OPTF_ARGREQ, 0, 'e' }, |
685 | { 0, 0, 0, 0 } |
686 | }; |
5c3f75ec |
687 | int i = mdwopt(argc, argv, "+0vqb" "c:" "f:o:" "k:e:", opts, 0, 0, 0); |
4bedc99b |
688 | if (i < 0) |
689 | break; |
690 | switch (i) { |
691 | case '0': |
692 | f |= f_raw; |
693 | break; |
694 | case 'b': |
695 | f |= f_bin; |
696 | break; |
697 | case 'v': |
698 | verb++; |
699 | break; |
700 | case 'q': |
701 | if (verb > 0) |
702 | verb--; |
703 | break; |
4bedc99b |
704 | case 'c': |
705 | c = optarg; |
706 | break; |
707 | case 'f': |
708 | ifile = optarg; |
709 | break; |
710 | case 'o': |
711 | ofile = optarg; |
712 | break; |
4bedc99b |
713 | case 'k': |
714 | ki = optarg; |
715 | break; |
716 | case 'e': |
717 | if (strcmp(optarg, "forever") == 0) |
718 | exp = KEXP_FOREVER; |
719 | else if ((exp = get_date(optarg, 0)) == -1) |
720 | die(EXIT_FAILURE, "bad expiry time"); |
721 | break; |
722 | default: |
723 | f |= f_bogus; |
724 | break; |
725 | } |
726 | } |
727 | if (optind != argc || (f & f_bogus)) |
c65df279 |
728 | die(EXIT_FAILURE, "Usage: sign [-OPTIONS]"); |
4bedc99b |
729 | |
730 | /* --- Locate the signing key --- */ |
731 | |
213e565f |
732 | if (key_open(&kf, keyring, KOPEN_WRITE, key_moan, 0)) |
4bedc99b |
733 | die(EXIT_FAILURE, "couldn't open keyring `%s'", keyring); |
4e6e0188 |
734 | if ((k = key_bytag(&kf, ki)) == 0) |
735 | die(EXIT_FAILURE, "couldn't find key `%s'", ki); |
4bedc99b |
736 | key_fulltag(k, &d); |
737 | if (exp == KEXP_FOREVER && k->exp != KEXP_FOREVER) { |
738 | die(EXIT_FAILURE, "key `%s' expires: can't create nonexpiring signature", |
739 | d.buf); |
740 | } |
5c3f75ec |
741 | s = getsig(k, "dsig", 1); |
742 | |
743 | /* --- Check the key --- */ |
744 | |
745 | if ((err = s->ops->check(s)) != 0) |
746 | moan("key `%s' fails check: %s", d.buf, err); |
4bedc99b |
747 | |
748 | /* --- Open files --- */ |
749 | |
750 | if (!ifile) |
751 | ifp = stdin; |
752 | else if ((ifp = fopen(ifile, (f & f_raw) ? "rb" : "r")) == 0) { |
753 | die(EXIT_FAILURE, "couldn't open input file `%s': %s", |
754 | ifile, strerror(errno)); |
755 | } |
756 | |
757 | if (!ofile) |
758 | ofp = stdout; |
759 | else if ((ofp = fopen(ofile, (f & f_bin) ? "wb" : "w")) == 0) { |
760 | die(EXIT_FAILURE, "couldn't open output file `%s': %s", |
761 | ofile, strerror(errno)); |
762 | } |
763 | |
764 | /* --- Emit the start of the output --- */ |
765 | |
766 | binit(&b); b.tag = T_IDENT; |
767 | dstr_putf(&b.d, "%s, Catacomb version " VERSION, QUIS); |
768 | bemit(&b, ofp, 0, f & f_bin); |
769 | |
4bedc99b |
770 | breset(&b); b.tag = T_KEYID; b.k = k->id; |
4e6e0188 |
771 | bemit(&b, ofp, 0, f & f_bin); |
4bedc99b |
772 | |
773 | /* --- Start hashing, and emit the datestamps and things --- */ |
774 | |
775 | { |
776 | time_t now = time(0); |
777 | |
4e6e0188 |
778 | breset(&b); b.tag = T_DATE; b.t = now; bemit(&b, ofp, s->h, f & f_bin); |
4bedc99b |
779 | if (exp == KEXP_EXPIRE) |
780 | exp = now + 86400 * 28; |
4e6e0188 |
781 | breset(&b); b.tag = T_EXPIRE; b.t = exp; bemit(&b, ofp, s->h, f & f_bin); |
4bedc99b |
782 | if (c) { |
783 | breset(&b); b.tag = T_COMMENT; DPUTS(&b.d, c); |
4e6e0188 |
784 | bemit(&b, ofp, s->h, f & f_bin); |
4bedc99b |
785 | } |
786 | |
787 | if (!(f & f_bin)) |
788 | putc('\n', ofp); |
789 | } |
790 | |
791 | /* --- Now hash the various files --- */ |
792 | |
793 | for (;;) { |
794 | |
795 | /* --- Stop on an output error --- */ |
796 | |
797 | if (ferror(ofp)) { |
798 | f |= f_bogus; |
799 | break; |
800 | } |
801 | |
802 | /* --- Read the next filename to hash --- */ |
803 | |
804 | breset(&b); |
805 | if (getstring(ifp, &b.d, f & f_raw)) |
806 | break; |
807 | b.tag = T_FILE; |
4e6e0188 |
808 | DENSURE(&b.b, GH_CLASS(s->h)->hashsz); |
809 | if (fhash(GH_CLASS(s->h), b.d.buf, b.b.buf)) { |
4bedc99b |
810 | moan("Error reading `%s': %s", b.d.buf, strerror(errno)); |
811 | f |= f_bogus; |
812 | } else { |
4e6e0188 |
813 | b.b.len += GH_CLASS(s->h)->hashsz; |
4bedc99b |
814 | if (verb) { |
815 | fhex(stderr, b.b.buf, b.b.len); |
816 | fprintf(stderr, " %s\n", b.d.buf); |
817 | } |
4e6e0188 |
818 | bemit(&b, ofp, s->h, f & f_bin); |
4bedc99b |
819 | } |
820 | } |
821 | |
822 | /* --- Create the signature --- */ |
823 | |
824 | if (!(f & f_bogus)) { |
825 | breset(&b); |
826 | b.tag = T_SIGNATURE; |
4e6e0188 |
827 | if ((e = s->ops->doit(s, &b.b)) != 0) { |
4bedc99b |
828 | moan("error creating signature: %s", key_strerror(e)); |
829 | f |= f_bogus; |
830 | } |
831 | if (!(f & f_bogus)) { |
832 | bemit(&b, ofp, 0, f & f_bin); |
833 | key_used(&kf, k, exp); |
834 | } |
835 | } |
836 | |
837 | /* --- Tidy up at the end --- */ |
838 | |
4e6e0188 |
839 | freesig(s); |
4bedc99b |
840 | bdestroy(&b); |
841 | if (ifile) |
842 | fclose(ifp); |
843 | if (ofile) { |
844 | if (fclose(ofp)) |
845 | f |= f_bogus; |
846 | } else { |
847 | if (fflush(ofp)) |
848 | f |= f_bogus; |
849 | } |
850 | if ((e = key_close(&kf)) != 0) { |
851 | switch (e) { |
852 | case KWRITE_FAIL: |
853 | die(EXIT_FAILURE, "couldn't write file `%s': %s", |
854 | keyring, strerror(errno)); |
855 | case KWRITE_BROKEN: |
856 | die(EXIT_FAILURE, "keyring file `%s' broken: %s (repair manually)", |
857 | keyring, strerror(errno)); |
858 | } |
859 | } |
860 | if (f & f_bogus) |
861 | die(EXIT_FAILURE, "error(s) occurred while creating signature"); |
862 | return (EXIT_SUCCESS); |
16efd15b |
863 | |
864 | #undef f_raw |
865 | #undef f_bin |
866 | #undef f_bogus |
4bedc99b |
867 | } |
868 | |
869 | /*----- Signature verification --------------------------------------------*/ |
870 | |
871 | static int verify(int argc, char *argv[]) |
872 | { |
16efd15b |
873 | #define f_bogus 1u |
874 | #define f_bin 2u |
875 | #define f_ok 4u |
4bedc99b |
876 | |
877 | unsigned f = 0; |
878 | unsigned verb = 1; |
879 | key_file kf; |
880 | key *k = 0; |
4e6e0188 |
881 | sig *s; |
4bedc99b |
882 | dstr d = DSTR_INIT; |
5c3f75ec |
883 | const char *err; |
4bedc99b |
884 | FILE *fp; |
885 | block b; |
886 | int e; |
887 | |
888 | /* --- Parse the options --- */ |
889 | |
890 | for (;;) { |
891 | static struct option opts[] = { |
892 | { "verbose", 0, 0, 'v' }, |
893 | { "quiet", 0, 0, 'q' }, |
894 | { 0, 0, 0, 0 } |
895 | }; |
896 | int i = mdwopt(argc, argv, "+vq", opts, 0, 0, 0); |
897 | if (i < 0) |
898 | break; |
899 | switch (i) { |
900 | case 'v': |
901 | verb++; |
902 | break; |
903 | case 'q': |
904 | if (verb) |
905 | verb--; |
906 | break; |
907 | default: |
908 | f |= f_bogus; |
909 | break; |
910 | } |
911 | } |
912 | argc -= optind; |
913 | argv += optind; |
914 | if ((f & f_bogus) || argc > 1) |
c65df279 |
915 | die(EXIT_FAILURE, "Usage: verify [-qv] [FILE]"); |
4bedc99b |
916 | |
917 | /* --- Open the key file, and start reading the input file --- */ |
918 | |
213e565f |
919 | if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0)) |
4bedc99b |
920 | die(EXIT_FAILURE, "couldn't open keyring `%s'\n", keyring); |
921 | if (argc < 1) |
922 | fp = stdin; |
923 | else { |
924 | if ((fp = fopen(argv[0], "rb")) == 0) { |
925 | die(EXIT_FAILURE, "couldn't open file `%s': %s\n", |
926 | argv[0], strerror(errno)); |
927 | } |
928 | if (getc(fp) == 0) { |
929 | ungetc(0, fp); |
930 | f |= f_bin; |
931 | } else { |
932 | fclose(fp); |
933 | if ((fp = fopen(argv[0], "r")) == 0) { |
934 | die(EXIT_FAILURE, "couldn't open file `%s': %s\n", |
935 | argv[0], strerror(errno)); |
936 | } |
937 | } |
938 | } |
939 | |
940 | /* --- Read the introductory matter --- */ |
941 | |
942 | binit(&b); |
4e6e0188 |
943 | for (;;) { |
4bedc99b |
944 | breset(&b); |
945 | e = bget(&b, fp, f & f_bin); |
946 | if (e < 0) |
4e6e0188 |
947 | die(EXIT_FAILURE, "error reading packet: %s", errtab[-e]); |
4bedc99b |
948 | if (e >= T_BEGIN) |
949 | break; |
950 | switch (e) { |
951 | case T_IDENT: |
952 | if (verb > 2) |
953 | printf("INFO ident: `%s'\n", b.d.buf); |
954 | break; |
4bedc99b |
955 | case T_KEYID: |
956 | if ((k = key_byid(&kf, b.k)) == 0) { |
957 | if (verb) |
958 | printf("FAIL key %08lx not found\n", (unsigned long)b.k); |
959 | exit(EXIT_FAILURE); |
960 | } |
961 | if (verb > 2) { |
962 | DRESET(&b.d); |
963 | key_fulltag(k, &b.d); |
964 | printf("INFO key: %s\n", b.d.buf); |
965 | } |
966 | break; |
967 | default: |
968 | die(EXIT_FAILURE, "(internal) unknown packet type\n"); |
969 | break; |
970 | } |
971 | } |
972 | |
973 | /* --- Initialize the hash function and start reading hashed packets --- */ |
974 | |
4bedc99b |
975 | if (!k) { |
976 | if (verb) |
977 | puts("FAIL no keyid packet found"); |
978 | exit(EXIT_FAILURE); |
979 | } |
bd5efd74 |
980 | |
5c3f75ec |
981 | s = getsig(k, "dsig", 0); |
982 | if (verb && (err = s->ops->check(s)) != 0) |
983 | printf("WARN public key fails check: %s", err); |
984 | |
4bedc99b |
985 | for (;;) { |
986 | switch (e) { |
987 | case T_COMMENT: |
988 | if (verb > 1) |
989 | printf("INFO comment: `%s'\n", b.d.buf); |
4e6e0188 |
990 | bemit(&b, 0, s->h, 0); |
4bedc99b |
991 | break; |
992 | case T_DATE: |
993 | if (verb > 2) { |
994 | DRESET(&b.d); |
995 | timestring(b.t, &b.d); |
996 | printf("INFO date: %s\n", b.d.buf); |
997 | } |
4e6e0188 |
998 | bemit(&b, 0, s->h, 0); |
4bedc99b |
999 | break; |
1000 | case T_EXPIRE: { |
1001 | time_t now = time(0); |
c65df279 |
1002 | if (b.t != KEXP_FOREVER && b.t < now) { |
4bedc99b |
1003 | if (verb > 1) |
1004 | puts("BAD signature has expired"); |
1005 | f |= f_bogus; |
1006 | } |
1007 | if (verb > 2) { |
1008 | DRESET(&b.d); |
1009 | timestring(b.t, &b.d); |
1010 | printf("INFO expires: %s\n", b.d.buf); |
1011 | } |
4e6e0188 |
1012 | bemit(&b, 0, s->h, 0); |
4bedc99b |
1013 | } break; |
1014 | case T_FILE: |
1015 | DRESET(&d); |
4e6e0188 |
1016 | DENSURE(&d, GH_CLASS(s->h)->hashsz); |
1017 | if (fhash(GH_CLASS(s->h), b.d.buf, d.buf)) { |
4bedc99b |
1018 | if (verb > 1) { |
1019 | printf("BAD error reading file `%s': %s\n", |
1020 | b.d.buf, strerror(errno)); |
1021 | } |
1022 | f |= f_bogus; |
4e6e0188 |
1023 | } else if (b.b.len != GH_CLASS(s->h)->hashsz || |
4bedc99b |
1024 | memcmp(d.buf, b.b.buf, b.b.len) != 0) { |
1025 | if (verb > 1) |
1026 | printf("BAD file `%s' has incorrect hash\n", b.d.buf); |
1027 | f |= f_bogus; |
1028 | } else if (verb > 3) { |
1029 | fputs("INFO hash: ", stdout); |
1030 | fhex(stdout, b.b.buf, b.b.len); |
1031 | printf(" %s\n", b.d.buf); |
1032 | } |
4e6e0188 |
1033 | bemit(&b, 0, s->h, 0); |
4bedc99b |
1034 | break; |
1035 | case T_SIGNATURE: |
4e6e0188 |
1036 | if (s->ops->doit(s, &b.b)) { |
1037 | if (verb > 1) |
1038 | puts("BAD bad signature"); |
4bedc99b |
1039 | f |= f_bogus; |
1040 | } else if (verb > 2) |
1041 | puts("INFO good signature"); |
1042 | goto done; |
1043 | default: |
1044 | if (verb) |
1045 | printf("FAIL invalid packet type %i\n", e); |
1046 | exit(EXIT_FAILURE); |
1047 | break; |
1048 | } |
1049 | breset(&b); |
1050 | e = bget(&b, fp, f & f_bin); |
1051 | if (e < 0) { |
1052 | if (verb) |
1053 | printf("FAIL error reading packet: %s\n", errtab[-e]); |
1054 | exit(EXIT_FAILURE); |
1055 | } |
1056 | } |
1057 | done: |
1058 | bdestroy(&b); |
1059 | dstr_destroy(&d); |
4e6e0188 |
1060 | freesig(s); |
4bedc99b |
1061 | key_close(&kf); |
1062 | if (fp != stdin) |
1063 | fclose(fp); |
1064 | if (verb) { |
1065 | if (f & f_bogus) |
1066 | puts("FAIL signature invalid"); |
1067 | else |
1068 | puts("OK signature verified"); |
1069 | } |
1070 | return (f & f_bogus ? EXIT_FAILURE : EXIT_SUCCESS); |
16efd15b |
1071 | |
1072 | #undef f_bogus |
1073 | #undef f_bin |
1074 | #undef f_ok |
4bedc99b |
1075 | } |
1076 | |
1077 | /*----- Main code ---------------------------------------------------------*/ |
1078 | |
c65df279 |
1079 | #define LISTS(LI) \ |
1080 | LI("Lists", list, \ |
1081 | listtab[i].name, listtab[i].name) \ |
1082 | LI("Signature schemes", sig, \ |
1083 | sigtab[i].name, sigtab[i].name) \ |
1084 | LI("Hash functions", hash, \ |
1085 | ghashtab[i], ghashtab[i]->name) |
1086 | |
1087 | MAKELISTTAB(listtab, LISTS) |
1088 | |
1089 | int cmd_show(int argc, char *argv[]) |
1090 | { |
1091 | return (displaylists(listtab, argv + 1)); |
1092 | } |
1093 | |
1094 | static int cmd_help(int, char **); |
4bedc99b |
1095 | |
1096 | static cmd cmdtab[] = { |
c65df279 |
1097 | { "help", cmd_help, "help [COMMAND...]" }, |
1098 | { "show", cmd_show, "show [ITEM...]" }, |
4bedc99b |
1099 | { "sign", sign, |
c65df279 |
1100 | "sign [-0bqv] [-c COMMENT] [-k TAG] [-e EXPIRE]\n\t\ |
1101 | [-f FILE] [-o OUTPUT]", |
5c3f75ec |
1102 | "\ |
bd5efd74 |
1103 | Options:\n\ |
1104 | \n\ |
1105 | -0, --null Read null-terminated filenames from stdin.\n\ |
1106 | -b, --binary Produce a binary output file.\n\ |
1107 | -q, --quiet Produce fewer messages while working.\n\ |
1108 | -v, --verbose Produce more messages while working.\n\ |
bd5efd74 |
1109 | -c, --comment=COMMENT Include COMMENT in the output file.\n\ |
1110 | -f, --file=FILE Read filenames to hash from FILE.\n\ |
1111 | -o, --output=FILE Write the signed result to FILE.\n\ |
4e6e0188 |
1112 | -k, --key=TAG Use a key named by TAG.\n\ |
bd5efd74 |
1113 | -e, --expire=TIME The signature should expire after TIME.\n\ |
1114 | " }, |
4bedc99b |
1115 | { "verify", verify, |
c65df279 |
1116 | "verify [-qv] [FILE]", "\ |
bd5efd74 |
1117 | Options:\n\ |
1118 | \n\ |
1119 | -q, --quiet Produce fewer messages while working.\n\ |
1120 | -v, --verbose Produce more messages while working.\n\ |
1121 | " }, |
4bedc99b |
1122 | { 0, 0, 0 } |
1123 | }; |
1124 | |
c65df279 |
1125 | static int cmd_help(int argc, char **argv) |
bd5efd74 |
1126 | { |
c65df279 |
1127 | sc_help(cmdtab, stdout, argv + 1); |
1128 | return (0); |
bd5efd74 |
1129 | } |
1130 | |
c65df279 |
1131 | void version(FILE *fp) |
4bedc99b |
1132 | { |
1133 | pquis(fp, "$, Catacomb version " VERSION "\n"); |
1134 | } |
1135 | |
1136 | static void usage(FILE *fp) |
1137 | { |
c65df279 |
1138 | pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n"); |
4bedc99b |
1139 | } |
1140 | |
c65df279 |
1141 | void help_global(FILE *fp) |
4bedc99b |
1142 | { |
c65df279 |
1143 | usage(fp); |
1144 | fputs("\n\ |
4bedc99b |
1145 | Create and verify signatures on lists of files.\n\ |
c65df279 |
1146 | \n\ |
1147 | Global command-line options:\n\ |
1148 | \n\ |
1149 | -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\ |
1150 | -v, --version Show program version number.\n\ |
1151 | -u, --usage Show a terse usage message.\n\ |
1152 | \n\ |
1153 | -k, --keyring=FILE Read keys from FILE.\n", |
1154 | fp); |
4bedc99b |
1155 | } |
1156 | |
1157 | /* --- @main@ --- * |
1158 | * |
1159 | * Arguments: @int argc@ = number of command line arguments |
1160 | * @char *argv[]@ = vector of command line arguments |
1161 | * |
1162 | * Returns: Zero if successful, nonzero otherwise. |
1163 | * |
1164 | * Use: Signs or verifies signatures on lists of files. Useful for |
1165 | * ensuring that a distribution is unmolested. |
1166 | */ |
1167 | |
1168 | int main(int argc, char *argv[]) |
1169 | { |
1170 | unsigned f = 0; |
4bedc99b |
1171 | |
16efd15b |
1172 | #define f_bogus 1u |
4bedc99b |
1173 | |
1174 | /* --- Initialize the library --- */ |
1175 | |
1176 | ego(argv[0]); |
1177 | sub_init(); |
1178 | rand_noisesrc(RAND_GLOBAL, &noise_source); |
1179 | rand_seed(RAND_GLOBAL, 160); |
1180 | |
1181 | /* --- Parse options --- */ |
1182 | |
1183 | for (;;) { |
1184 | static struct option opts[] = { |
1185 | { "help", 0, 0, 'h' }, |
1186 | { "version", 0, 0, 'v' }, |
1187 | { "usage", 0, 0, 'u' }, |
1188 | { "keyring", OPTF_ARGREQ, 0, 'k' }, |
1189 | { 0, 0, 0, 0 } |
1190 | }; |
1191 | int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0); |
1192 | if (i < 0) |
1193 | break; |
1194 | switch (i) { |
1195 | case 'h': |
c65df279 |
1196 | sc_help(cmdtab, stdout, argv + optind); |
4bedc99b |
1197 | exit(0); |
1198 | break; |
1199 | case 'v': |
1200 | version(stdout); |
1201 | exit(0); |
1202 | break; |
1203 | case 'u': |
1204 | usage(stdout); |
1205 | exit(0); |
1206 | case 'k': |
1207 | keyring = optarg; |
1208 | break; |
1209 | default: |
1210 | f |= f_bogus; |
1211 | break; |
1212 | } |
1213 | } |
1214 | |
1215 | argc -= optind; |
1216 | argv += optind; |
1217 | optind = 0; |
1218 | if (f & f_bogus || argc < 1) { |
1219 | usage(stderr); |
1220 | exit(EXIT_FAILURE); |
1221 | } |
1222 | |
1223 | /* --- Dispatch to the correct subcommand handler --- */ |
1224 | |
c65df279 |
1225 | return (findcmd(cmdtab, argv[0])->cmd(argc, argv)); |
16efd15b |
1226 | |
1227 | #undef f_bogus |
4bedc99b |
1228 | } |
1229 | |
1230 | /*----- That's all, folks -------------------------------------------------*/ |