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