Commit | Line | Data |
---|---|---|
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 | ||
66 | enum { | |
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 | ||
88 | static const char *tagtab[] = { | |
4e6e0188 | 89 | "ident:", "keyid:", |
4bedc99b | 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 | ||
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 | ||
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; | |
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 | ||
314 | static 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 | ||
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: | |
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 | ||
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) | |
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 | ||
429 | static 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 | ||
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; | |
4bedc99b | 454 | } |
45c0fd36 | 455 | } |
4bedc99b | 456 | |
457 | /*----- Signature generation ----------------------------------------------*/ | |
458 | ||
459 | static 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 | ||
f5e91c02 MW |
736 | static int checkjunk(const char *path, const struct stat *st, void *p) |
737 | { | |
738 | if (!st) printf("JUNK (error %s) %s\n", strerror(errno), path); | |
739 | else printf("JUNK %s %s\n", describefile(st), path); | |
740 | return (0); | |
741 | } | |
742 | ||
4bedc99b | 743 | static int verify(int argc, char *argv[]) |
744 | { | |
16efd15b | 745 | #define f_bogus 1u |
746 | #define f_bin 2u | |
747 | #define f_ok 4u | |
946c3f72 | 748 | #define f_nocheck 8u |
4bedc99b | 749 | |
750 | unsigned f = 0; | |
751 | unsigned verb = 1; | |
752 | key_file kf; | |
753 | key *k = 0; | |
4e6e0188 | 754 | sig *s; |
4bedc99b | 755 | dstr d = DSTR_INIT; |
5c3f75ec | 756 | const char *err; |
07290a45 | 757 | fhashstate fh; |
4bedc99b | 758 | FILE *fp; |
759 | block b; | |
760 | int e; | |
761 | ||
762 | /* --- Parse the options --- */ | |
763 | ||
764 | for (;;) { | |
765 | static struct option opts[] = { | |
766 | { "verbose", 0, 0, 'v' }, | |
cd6eca43 | 767 | { "progress", 0, 0, 'p' }, |
4bedc99b | 768 | { "quiet", 0, 0, 'q' }, |
946c3f72 | 769 | { "nocheck", 0, 0, 'C' }, |
f5e91c02 | 770 | { "junk", 0, 0, 'j' }, |
4bedc99b | 771 | { 0, 0, 0, 0 } |
772 | }; | |
f5e91c02 | 773 | int i = mdwopt(argc, argv, "+vpqCj", opts, 0, 0, 0); |
4bedc99b | 774 | if (i < 0) |
775 | break; | |
776 | switch (i) { | |
777 | case 'v': | |
778 | verb++; | |
779 | break; | |
cd6eca43 MW |
780 | case 'p': |
781 | f |= FHF_PROGRESS; | |
782 | break; | |
4bedc99b | 783 | case 'q': |
784 | if (verb) | |
785 | verb--; | |
786 | break; | |
946c3f72 | 787 | case 'C': |
788 | f |= f_nocheck; | |
789 | break; | |
f5e91c02 MW |
790 | case 'j': |
791 | f |= FHF_JUNK; | |
792 | break; | |
4bedc99b | 793 | default: |
794 | f |= f_bogus; | |
795 | break; | |
796 | } | |
797 | } | |
798 | argc -= optind; | |
799 | argv += optind; | |
800 | if ((f & f_bogus) || argc > 1) | |
946c3f72 | 801 | die(EXIT_FAILURE, "Usage: verify [-qvC] [FILE]"); |
4bedc99b | 802 | |
803 | /* --- Open the key file, and start reading the input file --- */ | |
804 | ||
213e565f | 805 | if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0)) |
4bedc99b | 806 | die(EXIT_FAILURE, "couldn't open keyring `%s'\n", keyring); |
807 | if (argc < 1) | |
808 | fp = stdin; | |
809 | else { | |
810 | if ((fp = fopen(argv[0], "rb")) == 0) { | |
811 | die(EXIT_FAILURE, "couldn't open file `%s': %s\n", | |
812 | argv[0], strerror(errno)); | |
813 | } | |
814 | if (getc(fp) == 0) { | |
815 | ungetc(0, fp); | |
816 | f |= f_bin; | |
817 | } else { | |
818 | fclose(fp); | |
819 | if ((fp = fopen(argv[0], "r")) == 0) { | |
820 | die(EXIT_FAILURE, "couldn't open file `%s': %s\n", | |
821 | argv[0], strerror(errno)); | |
822 | } | |
823 | } | |
824 | } | |
825 | ||
826 | /* --- Read the introductory matter --- */ | |
827 | ||
828 | binit(&b); | |
4e6e0188 | 829 | for (;;) { |
4bedc99b | 830 | breset(&b); |
831 | e = bget(&b, fp, f & f_bin); | |
832 | if (e < 0) | |
4e6e0188 | 833 | die(EXIT_FAILURE, "error reading packet: %s", errtab[-e]); |
4bedc99b | 834 | if (e >= T_BEGIN) |
835 | break; | |
836 | switch (e) { | |
837 | case T_IDENT: | |
838 | if (verb > 2) | |
839 | printf("INFO ident: `%s'\n", b.d.buf); | |
840 | break; | |
4bedc99b | 841 | case T_KEYID: |
842 | if ((k = key_byid(&kf, b.k)) == 0) { | |
843 | if (verb) | |
844 | printf("FAIL key %08lx not found\n", (unsigned long)b.k); | |
845 | exit(EXIT_FAILURE); | |
846 | } | |
847 | if (verb > 2) { | |
848 | DRESET(&b.d); | |
849 | key_fulltag(k, &b.d); | |
850 | printf("INFO key: %s\n", b.d.buf); | |
851 | } | |
852 | break; | |
853 | default: | |
854 | die(EXIT_FAILURE, "(internal) unknown packet type\n"); | |
855 | break; | |
856 | } | |
857 | } | |
858 | ||
859 | /* --- Initialize the hash function and start reading hashed packets --- */ | |
860 | ||
4bedc99b | 861 | if (!k) { |
862 | if (verb) | |
863 | puts("FAIL no keyid packet found"); | |
864 | exit(EXIT_FAILURE); | |
865 | } | |
bd5efd74 | 866 | |
5c3f75ec | 867 | s = getsig(k, "dsig", 0); |
946c3f72 | 868 | if (!(f & f_nocheck) && verb && (err = s->ops->check(s)) != 0) |
5c3f75ec | 869 | printf("WARN public key fails check: %s", err); |
870 | ||
07290a45 | 871 | fhash_init(&fh, GH_CLASS(s->h), f | FHF_BINARY); |
4bedc99b | 872 | for (;;) { |
873 | switch (e) { | |
874 | case T_COMMENT: | |
875 | if (verb > 1) | |
876 | printf("INFO comment: `%s'\n", b.d.buf); | |
4e6e0188 | 877 | bemit(&b, 0, s->h, 0); |
4bedc99b | 878 | break; |
879 | case T_DATE: | |
880 | if (verb > 2) { | |
881 | DRESET(&b.d); | |
882 | timestring(b.t, &b.d); | |
883 | printf("INFO date: %s\n", b.d.buf); | |
884 | } | |
4e6e0188 | 885 | bemit(&b, 0, s->h, 0); |
4bedc99b | 886 | break; |
887 | case T_EXPIRE: { | |
888 | time_t now = time(0); | |
c65df279 | 889 | if (b.t != KEXP_FOREVER && b.t < now) { |
4bedc99b | 890 | if (verb > 1) |
891 | puts("BAD signature has expired"); | |
892 | f |= f_bogus; | |
893 | } | |
894 | if (verb > 2) { | |
895 | DRESET(&b.d); | |
896 | timestring(b.t, &b.d); | |
897 | printf("INFO expires: %s\n", b.d.buf); | |
898 | } | |
4e6e0188 | 899 | bemit(&b, 0, s->h, 0); |
4bedc99b | 900 | } break; |
901 | case T_FILE: | |
902 | DRESET(&d); | |
4e6e0188 | 903 | DENSURE(&d, GH_CLASS(s->h)->hashsz); |
07290a45 | 904 | if (fhash(&fh, b.d.buf, d.buf)) { |
4bedc99b | 905 | if (verb > 1) { |
906 | printf("BAD error reading file `%s': %s\n", | |
907 | b.d.buf, strerror(errno)); | |
908 | } | |
909 | f |= f_bogus; | |
4e6e0188 | 910 | } else if (b.b.len != GH_CLASS(s->h)->hashsz || |
4bedc99b | 911 | memcmp(d.buf, b.b.buf, b.b.len) != 0) { |
912 | if (verb > 1) | |
913 | printf("BAD file `%s' has incorrect hash\n", b.d.buf); | |
914 | f |= f_bogus; | |
915 | } else if (verb > 3) { | |
916 | fputs("INFO hash: ", stdout); | |
917 | fhex(stdout, b.b.buf, b.b.len); | |
918 | printf(" %s\n", b.d.buf); | |
919 | } | |
4e6e0188 | 920 | bemit(&b, 0, s->h, 0); |
4bedc99b | 921 | break; |
922 | case T_SIGNATURE: | |
4e6e0188 | 923 | if (s->ops->doit(s, &b.b)) { |
924 | if (verb > 1) | |
925 | puts("BAD bad signature"); | |
4bedc99b | 926 | f |= f_bogus; |
927 | } else if (verb > 2) | |
928 | puts("INFO good signature"); | |
929 | goto done; | |
930 | default: | |
931 | if (verb) | |
932 | printf("FAIL invalid packet type %i\n", e); | |
933 | exit(EXIT_FAILURE); | |
934 | break; | |
935 | } | |
936 | breset(&b); | |
937 | e = bget(&b, fp, f & f_bin); | |
938 | if (e < 0) { | |
939 | if (verb) | |
940 | printf("FAIL error reading packet: %s\n", errtab[-e]); | |
941 | exit(EXIT_FAILURE); | |
942 | } | |
943 | } | |
944 | done: | |
f5e91c02 MW |
945 | if ((f & FHF_JUNK) && fhash_junk(&fh, checkjunk, 0)) |
946 | f |= f_bogus; | |
07290a45 | 947 | fhash_free(&fh); |
4bedc99b | 948 | bdestroy(&b); |
949 | dstr_destroy(&d); | |
4e6e0188 | 950 | freesig(s); |
4bedc99b | 951 | key_close(&kf); |
952 | if (fp != stdin) | |
953 | fclose(fp); | |
954 | if (verb) { | |
955 | if (f & f_bogus) | |
956 | puts("FAIL signature invalid"); | |
957 | else | |
958 | puts("OK signature verified"); | |
959 | } | |
960 | return (f & f_bogus ? EXIT_FAILURE : EXIT_SUCCESS); | |
16efd15b | 961 | |
962 | #undef f_bogus | |
963 | #undef f_bin | |
964 | #undef f_ok | |
946c3f72 | 965 | #undef f_nocheck |
4bedc99b | 966 | } |
967 | ||
968 | /*----- Main code ---------------------------------------------------------*/ | |
969 | ||
c65df279 | 970 | #define LISTS(LI) \ |
971 | LI("Lists", list, \ | |
972 | listtab[i].name, listtab[i].name) \ | |
973 | LI("Signature schemes", sig, \ | |
974 | sigtab[i].name, sigtab[i].name) \ | |
975 | LI("Hash functions", hash, \ | |
976 | ghashtab[i], ghashtab[i]->name) | |
977 | ||
978 | MAKELISTTAB(listtab, LISTS) | |
979 | ||
980 | int cmd_show(int argc, char *argv[]) | |
981 | { | |
982 | return (displaylists(listtab, argv + 1)); | |
983 | } | |
984 | ||
985 | static int cmd_help(int, char **); | |
4bedc99b | 986 | |
987 | static cmd cmdtab[] = { | |
c65df279 | 988 | { "help", cmd_help, "help [COMMAND...]" }, |
989 | { "show", cmd_show, "show [ITEM...]" }, | |
4bedc99b | 990 | { "sign", sign, |
cd6eca43 | 991 | "sign [-0bpqvC] [-c COMMENT] [-k TAG] [-e EXPIRE]\n\t\ |
95d92463 | 992 | [-f FILE] [-h FILE] [-o OUTPUT]", |
5c3f75ec | 993 | "\ |
bd5efd74 | 994 | Options:\n\ |
995 | \n\ | |
996 | -0, --null Read null-terminated filenames from stdin.\n\ | |
997 | -b, --binary Produce a binary output file.\n\ | |
998 | -q, --quiet Produce fewer messages while working.\n\ | |
999 | -v, --verbose Produce more messages while working.\n\ | |
cd6eca43 | 1000 | -p, --progress Show progress on large files.\n\ |
946c3f72 | 1001 | -C, --nocheck Don't check the private key.\n\ |
bd5efd74 | 1002 | -c, --comment=COMMENT Include COMMENT in the output file.\n\ |
1003 | -f, --file=FILE Read filenames to hash from FILE.\n\ | |
95d92463 | 1004 | -h, --hashes=FILE Read precomputed hashes from FILE.\n\ |
bd5efd74 | 1005 | -o, --output=FILE Write the signed result to FILE.\n\ |
4e6e0188 | 1006 | -k, --key=TAG Use a key named by TAG.\n\ |
bd5efd74 | 1007 | -e, --expire=TIME The signature should expire after TIME.\n\ |
1008 | " }, | |
4bedc99b | 1009 | { "verify", verify, |
cd6eca43 | 1010 | "verify [-pqvC] [FILE]", "\ |
bd5efd74 | 1011 | Options:\n\ |
1012 | \n\ | |
1013 | -q, --quiet Produce fewer messages while working.\n\ | |
1014 | -v, --verbose Produce more messages while working.\n\ | |
cd6eca43 | 1015 | -p, --progress Show progress on large files.\n\ |
946c3f72 | 1016 | -C, --nocheck Don't check the public key.\n\ |
bd5efd74 | 1017 | " }, |
4bedc99b | 1018 | { 0, 0, 0 } |
1019 | }; | |
1020 | ||
c65df279 | 1021 | static int cmd_help(int argc, char **argv) |
bd5efd74 | 1022 | { |
c65df279 | 1023 | sc_help(cmdtab, stdout, argv + 1); |
1024 | return (0); | |
bd5efd74 | 1025 | } |
1026 | ||
c65df279 | 1027 | void version(FILE *fp) |
4bedc99b | 1028 | { |
1029 | pquis(fp, "$, Catacomb version " VERSION "\n"); | |
1030 | } | |
1031 | ||
1032 | static void usage(FILE *fp) | |
1033 | { | |
c65df279 | 1034 | pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n"); |
4bedc99b | 1035 | } |
1036 | ||
c65df279 | 1037 | void help_global(FILE *fp) |
4bedc99b | 1038 | { |
c65df279 | 1039 | usage(fp); |
1040 | fputs("\n\ | |
4bedc99b | 1041 | Create and verify signatures on lists of files.\n\ |
c65df279 | 1042 | \n\ |
1043 | Global command-line options:\n\ | |
1044 | \n\ | |
1045 | -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\ | |
1046 | -v, --version Show program version number.\n\ | |
1047 | -u, --usage Show a terse usage message.\n\ | |
1048 | \n\ | |
1049 | -k, --keyring=FILE Read keys from FILE.\n", | |
1050 | fp); | |
4bedc99b | 1051 | } |
1052 | ||
1053 | /* --- @main@ --- * | |
1054 | * | |
1055 | * Arguments: @int argc@ = number of command line arguments | |
1056 | * @char *argv[]@ = vector of command line arguments | |
1057 | * | |
1058 | * Returns: Zero if successful, nonzero otherwise. | |
1059 | * | |
1060 | * Use: Signs or verifies signatures on lists of files. Useful for | |
1061 | * ensuring that a distribution is unmolested. | |
1062 | */ | |
1063 | ||
1064 | int main(int argc, char *argv[]) | |
1065 | { | |
1066 | unsigned f = 0; | |
4bedc99b | 1067 | |
16efd15b | 1068 | #define f_bogus 1u |
4bedc99b | 1069 | |
1070 | /* --- Initialize the library --- */ | |
1071 | ||
1072 | ego(argv[0]); | |
1073 | sub_init(); | |
1074 | rand_noisesrc(RAND_GLOBAL, &noise_source); | |
1075 | rand_seed(RAND_GLOBAL, 160); | |
1076 | ||
1077 | /* --- Parse options --- */ | |
1078 | ||
1079 | for (;;) { | |
1080 | static struct option opts[] = { | |
1081 | { "help", 0, 0, 'h' }, | |
1082 | { "version", 0, 0, 'v' }, | |
1083 | { "usage", 0, 0, 'u' }, | |
1084 | { "keyring", OPTF_ARGREQ, 0, 'k' }, | |
1085 | { 0, 0, 0, 0 } | |
1086 | }; | |
1087 | int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0); | |
1088 | if (i < 0) | |
1089 | break; | |
1090 | switch (i) { | |
1091 | case 'h': | |
c65df279 | 1092 | sc_help(cmdtab, stdout, argv + optind); |
4bedc99b | 1093 | exit(0); |
1094 | break; | |
1095 | case 'v': | |
1096 | version(stdout); | |
1097 | exit(0); | |
1098 | break; | |
1099 | case 'u': | |
1100 | usage(stdout); | |
1101 | exit(0); | |
1102 | case 'k': | |
1103 | keyring = optarg; | |
1104 | break; | |
1105 | default: | |
1106 | f |= f_bogus; | |
1107 | break; | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | argc -= optind; | |
1112 | argv += optind; | |
1113 | optind = 0; | |
1114 | if (f & f_bogus || argc < 1) { | |
1115 | usage(stderr); | |
1116 | exit(EXIT_FAILURE); | |
1117 | } | |
1118 | ||
1119 | /* --- Dispatch to the correct subcommand handler --- */ | |
1120 | ||
c65df279 | 1121 | return (findcmd(cmdtab, argv[0])->cmd(argc, argv)); |
16efd15b | 1122 | |
1123 | #undef f_bogus | |
4bedc99b | 1124 | } |
1125 | ||
1126 | /*----- That's all, folks -------------------------------------------------*/ |