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