5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
41 #include <mLib/base64.h>
42 #include <mLib/dstr.h>
43 #include <mLib/mdwopt.h>
44 #include <mLib/quis.h>
45 #include <mLib/report.h>
59 /*----- Static variables --------------------------------------------------*/
61 static const char *keyring = "keyring";
63 /*----- Data formats ------------------------------------------------------*
65 * Our crypto stuff is split into three parts: a header describing the key,
66 * the message itself, and the signature. In a detached signature, the
67 * message is separate, and the header and signature are combined into one
68 * encoded chunk. In an attached signature, the message is included. There
69 * are two forms of message: binary and text. Binary messages are divided
70 * into chunks, each preceded by a 2-octet length, and terminated by a
71 * zero-length chunk. Text messages are byte-stuffed, as for RFC821, and
72 * trailing whitespace before a newline is ignored.
75 typedef struct sigmsg {
76 unsigned f; /* Flags */
80 #define F_HASHMASK (F_BINARY)
81 uint32 keyid; /* Key identifier */
82 time_t t; /* When the signature was made */
83 dstr kh; /* Key fingerprint (sanity check) */
84 dstr sig; /* Signature */
85 sig *s; /* Signature algorithm */
94 #define F_NOCHECK 1024u
95 #define F_PROGRESS 2048u
97 /*----- Chunk I/O ---------------------------------------------------------*/
99 static void chunk_write(enc *e, const void *p, size_t n)
105 if (e->ops->write(e, b, 2) || e->ops->write(e, p, n))
106 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
109 static size_t chunk_read(enc *e, void *p)
114 if (e->ops->read(e, b, 2) != 2)
117 if (n && e->ops->read(e, p, n) != n)
122 if (!errno) die(EXIT_FAILURE, "unexpected end-of-file on input");
123 else die(EXIT_FAILURE, "error reading input: %s", strerror(errno));
127 /*----- Message canonification --------------------------------------------*/
129 #define MSGBUFSZ 65536
130 #define MSGBUFTHRESH 32768
132 typedef struct msgcanon {
137 size_t (*read)(struct msgcanon *, void *);
138 void (*write)(struct msgcanon *, const void *, size_t);
139 void (*close)(struct msgcanon *);
142 #define MC_INIT { 0 }
144 static size_t textread(msgcanon *m, void *bp)
146 size_t n = 0, nsp = 0;
151 if (f & F_EOF) return (0);
153 if ((ch = getc(m->fp)) == EOF) goto eof;
154 if (!(f & (F_DETACH | F_MIDLINE)) && ch == '.') {
156 if (ch == '\n') goto eof;
160 if (n >= MSGBUFTHRESH) goto full;
163 } else if (isspace(ch)) {
165 if (n >= MSGBUFSZ) goto full;
170 if (n >= MSGBUFTHRESH) goto full;
181 if (m->f & F_PROGRESS) fprogress_update(&m->ff, n);
185 static void textwrite(msgcanon *m, const void *bp, size_t n)
187 const char *p = bp, *l = p + n;
191 if (!(f & (F_MIDLINE | F_DETACH)) &&
192 (*p == '.' || *p == '-'))
194 if (*p == '\n') f &= ~F_MIDLINE;
202 static size_t binreadembed(msgcanon *m, void *bp)
204 size_t n = chunk_read(m->e, bp);
205 if (m->f & F_PROGRESS) fprogress_update(&m->ff, n);
209 static size_t binreaddetach(msgcanon *m, void *bp)
211 size_t n = fread(bp, 1, MSGBUFSZ - 1, m->fp);
212 if (m->f & F_PROGRESS) fprogress_update(&m->ff, n);
216 static void binwriteembed(msgcanon *m, const void *bp, size_t n)
217 { chunk_write(m->e, bp, n); }
219 static void nullwrite(msgcanon *m, const void *bp, size_t n) { ; }
221 static void mcsetup_readfile(msgcanon *m, unsigned f, const char *fn)
223 m->f = F_DETACH | (f & (F_BINARY | F_PROGRESS));
224 if (!fn || strcmp(fn, "-") == 0) {
227 } else if ((m->fp = fopen(fn, (f & F_BINARY) ? "rb" : "r")) == 0)
228 die(EXIT_FAILURE, "couldn't open file `%s': %s", fn, strerror(errno));
229 if (m->f & F_PROGRESS) {
230 if (fprogress_init(&m->ff, fn, m->fp)) {
231 die(EXIT_FAILURE, "failed to initialize progress indicator: %s",
235 m->read = (f & F_BINARY) ? binreaddetach : textread;
238 static void mcsetup_writenull(msgcanon *m) { m->write = nullwrite; }
240 static void mcsetup_read(msgcanon *m, unsigned f, enc **ee,
241 const char *fn, const char *dfn)
245 m->f = f | F_NOCLOSE;
247 if (dfn && !(f & F_DETACH)) die(EXIT_FAILURE, "signature is not detached");
249 if (!(f & F_DETACH)) {
253 m->read = binreadembed;
254 if (m->f & F_PROGRESS) {
255 if (fprogress_init(&m->ff, fn, m->fp)) {
256 die(EXIT_FAILURE, "failed to initialize progress indicator: %s",
261 m->read = binreaddetach;
262 if (!dfn || strcmp(dfn, "-") == 0)
264 else if ((m->fp = fopen(dfn, "rb")) == 0)
265 die(EXIT_FAILURE, "can't open `%s': %s", dfn, strerror(errno));
268 if (m->f & F_PROGRESS) {
269 if (fprogress_init(&m->ff, dfn, m->fp)) {
270 die(EXIT_FAILURE, "failed to initialize progress indicator: %s",
277 if (!(f & F_DETACH)) {
278 if (e->ops->decdone(e) || ferror(e->fp))
279 die(EXIT_FAILURE, "error at end of signature header");
283 if (m->f & F_PROGRESS) {
284 if (fprogress_init(&m->ff, fn, m->fp)) {
285 die(EXIT_FAILURE, "failed to initialize progress indicator: %s",
290 if (!dfn || strcmp(dfn, "-") == 0)
292 else if ((m->fp = fopen(dfn, "r")) == 0)
293 die(EXIT_FAILURE, "can't read file `%s': %s", dfn, strerror(errno));
296 if (m->f & F_PROGRESS) {
297 if (fprogress_init(&m->ff, dfn, m->fp)) {
298 die(EXIT_FAILURE, "failed to initialize progress indicator: %s",
306 static void mcsetup_write(msgcanon *m, unsigned f, enc **ee)
310 m->f = f | F_NOCLOSE;
313 m->write = nullwrite;
314 else if (f & F_BINARY) {
318 m->write = binwriteembed;
320 if (e->ops->encdone(e) || ferror(e->fp))
321 die(EXIT_FAILURE, "error at end of signature header");
325 m->write = textwrite;
329 static void mc_endread(msgcanon *m, const encops *eops, enc **ee)
331 if (!(m->f & F_DETACH)) {
335 *ee = initdec(eops, m->fp, checkbdry, "CATSIGN SIGNATURE");
337 if (m->fp && !(m->f & F_NOCLOSE)) {
338 if (ferror(m->fp) || fclose(m->fp))
339 die(EXIT_FAILURE, "error closing message file: %s", strerror(errno));
341 if (m->f & F_PROGRESS) fprogress_done(&m->ff);
344 static void mc_endwrite(msgcanon *m, const encops *eops, enc **ee)
346 if (!(m->f & F_BINARY)) {
347 if (m->f & F_MIDLINE) putc('\n', m->fp);
348 if (!(m->f & F_DETACH)) {
349 putc('.', m->fp); putc('\n', m->fp);
350 *ee = initenc(eops, m->fp, "CATSIGN SIGNATURE");
352 } else if (!(m->f & F_DETACH)) {
353 chunk_write(m->e, 0, 0);
356 if (m->fp && !(m->f & F_NOCLOSE)) {
357 if (fflush(m->fp) || ferror(m->fp) || fclose(m->fp))
358 die(EXIT_FAILURE, "error closing message file: %s", strerror(errno));
362 /*----- Signature reading and writing -------------------------------------*/
364 static void sig_init(sigmsg *s, unsigned f, uint32 keyid)
366 s->f = f & F_SIGMASK;
371 dstr_create(&s->sig);
374 static void sig_destroy(sigmsg *s)
376 dstr_destroy(&s->kh);
377 dstr_destroy(&s->sig);
378 if (s->s) freesig(s->s);
381 static void sigtobuffer(sigmsg *s, buf *b, int hashp)
386 if (hashp) buf_putu16(b, s->f & F_HASHMASK);
387 else buf_putu16(b, s->f & F_SIGMASK);
388 buf_putu32(b, s->keyid);
389 buf_putu32(b, HI64(t));
390 buf_putu32(b, LO64(t));
391 buf_putdstr16(b, &s->kh);
395 static void dohash(ghash *h, const void *p, size_t n)
397 /* trace_block(1, "hashing", p, n); */
401 static void sig_hash(sigmsg *s)
406 buf_init(&b, bb, sizeof(bb));
407 sigtobuffer(s, &b, 1);
408 dohash(s->s->h, BBASE(&b), BLEN(&b));
411 static void keyhash(key *k, sig *s, dstr *d)
419 key_fingerprint(k, h, &kf);
420 dstr_ensure(d, GH_CLASS(h)->hashsz);
421 GH_DONE(h, d->buf + d->len);
422 d->len += GH_CLASS(h)->hashsz;
426 static void sig_writeheader(enc *e, sigmsg *s)
431 buf_init(&b, bb, sizeof(bb));
432 sigtobuffer(s, &b, 0);
433 chunk_write(e, BBASE(&b), BLEN(&b));
436 static void sig_writesig(enc *e, sigmsg *s)
437 { chunk_write(e, s->sig.buf, s->sig.len); }
439 static void diechoke(const char *m, void *p)
440 { die(EXIT_FAILURE, "%s%s%s", p, p ? ": " : "", m); }
442 static void sig_readheader(enc *e, sigmsg *s,
443 void (*choke)(const char *, void *), void *p)
452 n = chunk_read(e, bb);
454 if (buf_getu16(&b, &f)) choke("missing flags", p);
455 if (buf_getu32(&b, &x)) choke("missing keyid", p);
457 if (buf_getu32(&b, &x) || buf_getu32(&b, &y))
458 choke("missing datestamp", p);
459 SET64(t, x, y); s->t = GET64(time_t, t);
460 if (buf_getdstr16(&b, &s->kh))
461 choke("missing key hash", p);
463 choke("junk at end", p);
466 static void sig_readsig(enc *e, sigmsg *s)
471 n = chunk_read(e, bb);
472 dstr_putm(&s->sig, bb, n);
475 /*----- Signing -----------------------------------------------------------*/
477 static int sign(int argc, char *argv[])
479 const char *ef = "binary", *fn = 0, *of = 0, *kn = "ccsig", *err;
490 msgcanon mc_in = MC_INIT, mc_out = MC_INIT;
494 static const struct option opt[] = {
495 { "armour", 0, 0, 'a' },
496 { "armor", 0, 0, 'a' },
497 { "binary", 0, 0, 'b' },
498 { "detach", 0, 0, 'd' },
499 { "key", OPTF_ARGREQ, 0, 'k' },
500 { "format", OPTF_ARGREQ, 0, 'f' },
501 { "output", OPTF_ARGREQ, 0, 'o' },
502 { "progress", 0, 0, 'p' },
503 { "text", 0, 0, 't' },
504 { "nocheck", 0, 0, 'C' },
507 i = mdwopt(argc, argv, "k:f:o:abdptC", opt, 0, 0, 0);
510 case 'k': kn = optarg; break;
511 case 'f': ef = optarg; break;
512 case 'o': of = optarg; break;
513 case 'a': ef = "pem"; break;
514 case 't': f &= ~F_BINARY; break;
515 case 'b': f |= F_BINARY; break;
516 case 'd': f |= F_DETACH; break;
517 case 'C': f |= F_NOCHECK; break;
518 case 'p': f |= F_PROGRESS; break;
519 default: f |= F_BOGUS; break;
522 if (argc - optind > 1 || (f & F_BOGUS))
523 die(EXIT_FAILURE, "Usage: sign [-OPTIONS] [FILE]");
525 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
526 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
527 if ((k = key_bytag(&kf, kn)) == 0)
528 die(EXIT_FAILURE, "key `%s' not found", kn);
530 if ((eo = getenc(ef)) == 0)
531 die(EXIT_FAILURE, "encoding `%s' not found", ef);
533 fn = (optind >= argc) ? 0 : argv[optind++];
535 if (!of || strcmp(of, "-") == 0)
537 else if ((ofp = fopen(of, eo->wmode)) == 0) {
538 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
539 ofp, strerror(errno));
542 /* --- Start the work --- */
544 sig_init(&s, f, k->id);
547 s.s = getsig(k, "ccsig", 1);
548 if (!(f & F_NOCHECK) && (err = s.s->ops->check(s.s)) != 0)
549 moan("key %s fails check: %s", d.buf, err);
550 keyhash(k, s.s, &s.kh);
552 (f & F_DETACH) ? "CATSIGN SIGNATURE" :
553 (f & F_BINARY) ? "CATSIGN MESSAGE" :
554 "CATSIGN MESSAGE HEADER");
555 sig_writeheader(e, &s);
557 /* --- Hash the message --- */
559 mcsetup_readfile(&mc_in, f, fn);
560 mcsetup_write(&mc_out, f, &e);
563 n = mc_in.read(&mc_in, bb);
565 dohash(s.s->h, bb, n);
566 mc_out.write(&mc_out, bb, n);
568 mc_endread(&mc_in, 0, 0);
569 mc_endwrite(&mc_out, eo, &e);
571 /* --- Write the signature --- */
573 if (s.s->ops->doit(s.s, &s.sig))
574 die(EXIT_FAILURE, "signature failed");
577 if (fflush(ofp) || ferror(ofp) || fclose(ofp))
578 die(EXIT_FAILURE, "error writing signature: %s", strerror(errno));
580 /* --- All done --- */
589 /*----- Verifying ---------------------------------------------------------*/
591 typedef struct vrfctx {
597 static int vrfbdry(const char *b, void *p)
601 if (strcmp(b, "CATSIGN MESSAGE") == 0) {
603 v->m |= F_BINARY | F_DETACH;
605 } else if (strcmp(b, "CATSIGN MESSAGE HEADER") == 0) {
606 v->m |= F_BINARY | F_DETACH;
608 } else if (strcmp(b, "CATSIGN SIGNATURE") == 0) {
616 static void vrfchoke(const char *m, void *p)
619 if (v->verb) printf("FAIL %s: %s\n", v->what, m);
623 static int verify(int argc, char *argv[])
625 const char *ef = "binary", *of = 0, *fn, *dfn = 0, *kn = 0, *err;
626 vrfctx v = { 0, 0, 1 };
630 FILE *fp, *ofp = 0, *rfp = 0;
637 dstr d = DSTR_INIT, dd = DSTR_INIT;
639 msgcanon mc_in = MC_INIT;
643 static const struct option opt[] = {
644 { "armour", 0, 0, 'a' },
645 { "armor", 0, 0, 'a' },
646 { "buffer", 0, 0, 'b' },
647 { "key", OPTF_ARGREQ, 0, 'k' },
648 { "format", OPTF_ARGREQ, 0, 'f' },
649 { "output", OPTF_ARGREQ, 0, 'o' },
650 { "progress", 0, 0, 'p' },
651 { "quiet", 0, 0, 'q' },
652 { "utc", 0, 0, 'u' },
653 { "fresh-time", 0, 0, 't' },
654 { "gmt", 0, 0, 'u' },
655 { "verbose", 0, 0, 'v' },
656 { "nocheck", 0, 0, 'C' },
659 i = mdwopt(argc, argv, "k:f:o:abpqt:uvC", opt, 0, 0, 0);
662 case 'a': ef = "pem"; break;
663 case 'b': v.f |= F_BUFFER; break;
664 case 'k': kn = optarg; break;
665 case 'f': ef = optarg; break;
666 case 'o': of = optarg; break;
667 case 'u': v.f |= F_UTC; break;
668 case 'C': v.f |= F_NOCHECK; break;
670 if (strcmp(optarg, "always") == 0) t_fresh = 0;
671 else if ((t_fresh = get_date(optarg, 0)) < 0)
672 die(EXIT_FAILURE, "bad freshness time");
674 case 'q': if (v.verb > 0) v.verb--; break;
675 case 'v': if (v.verb < 10) v.verb++; break;
676 case 'p': v.f |= F_PROGRESS; break;
677 default: v.f |= F_BOGUS; break;
680 if (argc - optind > 2 || (v.f & F_BOGUS))
681 die(EXIT_FAILURE, "Usage: verify [-OPTIONS] [FILE [MESSAGE]]");
683 if ((eo = getenc(ef)) == 0)
684 die(EXIT_FAILURE, "encoding `%s' not found", ef);
686 fn = optind < argc ? argv[optind++] : "-";
687 if (strcmp(fn, "-") == 0)
689 else if ((fp = fopen(fn, eo->rmode)) == 0) {
690 die(EXIT_FAILURE, "couldn't open file `%s': %s",
691 fn, strerror(errno));
694 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
695 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
696 if (kn && (kk = key_bytag(&kf, kn)) == 0)
697 die(EXIT_FAILURE, "key `%s' not found", kn);
699 e = initdec(eo, fp, vrfbdry, &v);
701 /* --- Read the header chunk --- */
703 v.what = "malformed header";
704 sig_readheader(e, &s, vrfchoke, &v);
706 if (((s.f ^ v.f) & v.m) != 0) {
707 if (v.verb) printf("FAIL boundary string inconsistent with contents\n");
712 if ((k = key_byid(&kf, s.keyid)) == 0) {
713 if (v.verb) printf("FAIL key id %08lx not found\n",
714 (unsigned long)s.keyid);
717 if (kk && k->id != kk->id) {
719 dstr_reset(&d); key_fulltag(k, &d);
720 dstr_reset(&dd); key_fulltag(kk, &dd);
721 printf("FAIL signing key is %s; expected key %s\n", d.buf, dd.buf);
726 s.s = getsig(k, "ccsig", 0);
727 dstr_reset(&d); key_fulltag(k, &d);
728 if (!(v.f & F_NOCHECK) && v.verb && (err = s.s->ops->check(s.s)) != 0)
729 printf("WARN verification key %s fails check: %s\n", d.buf, err);
731 dstr_reset(&dd); keyhash(k, s.s, &dd);
732 if (dd.len != s.kh.len || memcmp(dd.buf, s.kh.buf, dd.len) != 0) {
733 if (v.verb) printf("FAIL key hash mismatch\n");
737 /* --- Now a merry dance --- */
742 dfn = argv[optind++];
743 mcsetup_read(&mc_in, v.f, &e, fn, dfn);
745 if (!of && (v.f & F_DETACH)) {
748 } else if (!of || strcmp(of, "-") == 0) {
752 if (of && !(v.f & F_BUFFER)) {
753 if ((ofp = fopen(of, (v.f & F_BINARY) ? "wb" : "w")) == 0) {
754 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
755 of, strerror(errno));
758 } else if ((rfp = tmpfile()) == 0)
759 die(EXIT_FAILURE, "couldn't create temporary file: %s", strerror(errno));
761 /* --- Read the message and verify the signature --- */
765 n = mc_in.read(&mc_in, bb);
767 dohash(s.s->h, bb, n);
768 if (rfp) fwrite(bb, 1, n, rfp);
770 mc_endread(&mc_in, eo, &e);
771 if (!(v.f & F_DETACH))
773 if (rfp && (ferror(rfp) || fflush(rfp))) {
774 if (v.verb) printf("FAIL error writing message: %s\n", strerror(errno));
778 /* --- Check the signature --- */
780 if (s.s->ops->doit(s.s, &s.sig)) {
781 if (v.verb) printf("FAIL signature verification failed\n");
784 if (t_fresh && s.t < t_fresh) {
785 if (v.verb) printf("FAIL signature is stale\n");
789 if (v.verb) printf("FAIL signature timestamp in the future\n");
793 tm = (v.f & F_UTC) ? gmtime(&s.t) : localtime(&s.t);
794 strftime(bb, sizeof(bb), "%Y-%m-%d %H:%M:%S %Z", tm);
795 printf("INFO good-signature %s\n", d.buf);
796 printf("INFO date %s\n", bb);
799 /* --- Unbuffer buffered input --- */
801 if (v.f & F_BUFFER) {
802 if (!ofp && (ofp = fopen(of, "wb")) == 0) {
803 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
804 of, strerror(errno));
807 if (v.f & F_PROGRESS) fprogress_init(&ff, "copying buffer", rfp);
808 if (v.verb && ofp == stdout) printf("DATA\n");
810 n = fread(bb, 1, sizeof(bb), rfp);
812 if (v.f & F_PROGRESS) fprogress_update(&ff, n);
813 if (fwrite(bb, 1, n, ofp) < n) {
814 if (v.f & F_PROGRESS) fprogress_done(&ff);
815 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
818 if (v.f & F_PROGRESS) fprogress_done(&ff);
819 if (ferror(rfp) || fclose(rfp))
820 die(EXIT_FAILURE, "error unbuffering output: %s", strerror(errno));
822 if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp)))
823 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
825 /* --- Tidy up --- */
828 if (v.verb && ofp != stdout)
829 printf("OK verified successfully\n");
838 /*----- Reformatting ------------------------------------------------------*/
840 static int format(int argc, char *argv[])
842 const char *ief = "binary", *oef = "binary";
843 const char *fn, *dfn = 0, *of = 0, *mf = 0;
845 FILE *fp, *ofp = 0, *mfp = 0;
848 msgcanon mc_in = MC_INIT, mc_out = MC_INIT;
850 vrfctx v = { 0, 0, 1 };
851 unsigned f = 0, fm = ~F_SIGMASK, sf;
852 const encops *ieo, *oeo;
856 static const struct option opt[] = {
857 { "armour-in", 0, 0, 'a' },
858 { "armor-in", 0, 0, 'a' },
859 { "armour-out", 0, 0, 'A' },
860 { "armor-out", 0, 0, 'A' },
861 { "detach", 0, 0, 'D' },
862 { "embed", 0, 0, 'E' },
863 { "format-in", OPTF_ARGREQ, 0, 'f' },
864 { "format-out", OPTF_ARGREQ, 0, 'F' },
865 { "message", OPTF_ARGREQ, 0, 'm' },
866 { "output", OPTF_ARGREQ, 0, 'o' },
867 { "progress", 0, 0, 'p' },
870 i = mdwopt(argc, argv, "f:F:m:o:apADE", opt, 0, 0, 0);
873 case 'a': ief = "pem"; break;
874 case 'A': oef = "pem"; break;
875 case 'f': ief = optarg; break;
876 case 'F': oef = optarg; break;
877 case 'D': f |= F_DETACH; fm |= F_DETACH; break;
878 case 'E': f &= ~F_DETACH; fm |= F_DETACH; break;
879 case 'm': mf = optarg; break;
880 case 'o': of = optarg; break;
881 case 'p': f |= F_PROGRESS; break;
882 default: f |= F_BOGUS; break;
886 if (argc - optind > 2 || (f & F_BOGUS))
887 die(EXIT_FAILURE, "Usage: format [-OPTIONS] [FILE [MESSAGE]]");
889 if ((ieo = getenc(ief)) == 0)
890 die(EXIT_FAILURE, "encoding `%s' not found", ief);
891 if ((oeo = getenc(oef)) == 0)
892 die(EXIT_FAILURE, "encoding `%s' not found", oef);
894 fn = optind < argc ? argv[optind++] : "-";
895 if (strcmp(fn, "-") == 0)
897 else if ((fp = fopen(fn, ieo->rmode)) == 0) {
898 die(EXIT_FAILURE, "couldn't open file `%s': %s",
899 fn, strerror(errno));
903 dfn = argv[optind++];
905 ie = initdec(ieo, fp, vrfbdry, &v);
907 /* --- Read the header chunk --- */
909 sig_readheader(ie, &s, diechoke, "malformed header");
911 if (((s.f ^ v.f) & v.m) != 0)
912 moan("boundary string inconsistent with contents (ignoring)");
914 mcsetup_read(&mc_in, s.f, &ie, fn, dfn);
916 /* --- Prepare the output stuff --- */
918 if (!of && !mf) of = "-";
920 f = (f & fm) | (s.f & ~fm);
927 mcsetup_writenull(&mc_out);
929 if (strcmp(of, "-") == 0)
931 else if ((ofp = fopen(of, oeo->wmode)) == 0) {
932 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
933 of, strerror(errno));
935 oe = initenc(oeo, ofp,
936 (f & F_DETACH) ? "CATSIGN SIGNATURE" :
937 (f & F_BINARY) ? "CATSIGN MESSAGE" :
938 "CATSIGN MESSAGE HEADER");
939 sig_writeheader(oe, &s);
940 mcsetup_write(&mc_out, f, &oe);
944 if (strcmp(mf, "-") == 0)
946 else if ((mfp = fopen(mf, (f & F_BINARY) ? "wb" : "w")) == 0) {
947 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
948 mf, strerror(errno));
952 /* --- Wade through the message body --- */
955 n = mc_in.read(&mc_in, bb);
957 mc_out.write(&mc_out, bb, n);
958 if (mfp) fwrite(bb, 1, n, mfp);
960 mc_endread(&mc_in, ieo, &ie);
961 if (of) mc_endwrite(&mc_out, oeo, &oe);
963 /* --- Write the signature --- */
965 if (!(sf & F_DETACH))
968 sig_writesig(oe, &s);
969 oe->ops->encdone(oe);
972 /* --- All done --- */
974 ie->ops->decdone(ie);
975 if (ferror(fp) || fclose(fp))
976 die(EXIT_FAILURE, "error reading input signature: %s", strerror(errno));
977 if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp)))
978 die(EXIT_FAILURE, "error writing output signature: %s", strerror(errno));
979 if (mfp && (fflush(mfp) || ferror(mfp) || fclose(mfp)))
980 die(EXIT_FAILURE, "error writing output message: %s", strerror(errno));
987 static void infochoke(const char *m, void *p)
990 printf("BAD %s: %s\n", v->what, m);
994 static void infokeyreport(const char *file, int line,
995 const char *err, void *p)
998 static int info(int argc, char *argv[])
1000 const char *ef = "binary";
1001 vrfctx v = { 0, 0, 1 };
1014 static const struct option opt[] = {
1015 { "armour", 0, 0, 'a' },
1016 { "armor", 0, 0, 'a' },
1017 { "format", OPTF_ARGREQ, 0, 'f' },
1018 { "gmt", 0, 0, 'u' },
1019 { "utc", 0, 0, 'u' },
1022 i = mdwopt(argc, argv, "f:au", opt, 0, 0, 0);
1025 case 'a': ef = "pem"; break;
1026 case 'f': ef = optarg; break;
1027 case 'u': v.f |= F_UTC; break;
1028 default: v.f |= F_BOGUS; break;
1031 if (argc - optind > 1 || (v.f & F_BOGUS))
1032 die(EXIT_FAILURE, "Usage: info [-OPTIONS] [FILE]");
1034 if ((eo = getenc(ef)) == 0)
1035 die(EXIT_FAILURE, "encoding `%s' not found", ef);
1039 else if (strcmp(argv[optind], "-") == 0) {
1042 } else if ((fp = fopen(argv[optind], eo->rmode)) == 0) {
1043 die(EXIT_FAILURE, "couldn't open file `%s': %s",
1044 argv[optind], strerror(errno));
1048 if (key_open(&kf, keyring, KOPEN_READ, infokeyreport, 0)) {
1049 printf("NOTE can't open keyring `%s'\n", keyring);
1052 e = initdec(eo, fp, vrfbdry, &v);
1054 v.what = "malformed header";
1055 sig_readheader(e, &s, infochoke, &v);
1057 printf("INFO flags %sdetach %sbinary\n",
1058 (s.f & F_DETACH) ? "" : "!",
1059 (s.f & F_BINARY) ? "" : "!");
1061 if (((s.f ^ v.f) & v.m) != 0) {
1062 printf("WARN boundary string inconsistent with contents\n");
1063 printf("INFO expected-flags");
1064 if (v.m & F_DETACH) printf(" %sdetach", (v.f & F_DETACH) ? "" : "!");
1065 if (v.m & F_BINARY) printf(" %sbinary", (v.f & F_BINARY) ? "" : "!");
1070 tm = (v.f & F_UTC) ? gmtime(&s.t) : localtime(&s.t);
1071 strftime(bb, sizeof(bb), "%Y-%m-%d %H:%M:%S %Z", tm);
1072 printf("INFO date %s\n", bb);
1074 if (keyring && (k = key_byid(&kf, s.keyid)) != 0) {
1075 dstr_reset(&d); key_fulltag(k, &d);
1076 printf("INFO key %s\n", d.buf);
1078 printf("INFO unknown-key %08lx\n", (unsigned long)s.keyid);
1080 if (keyring) key_close(&kf);
1086 /*----- Main code ---------------------------------------------------------*/
1090 listtab[i].name, listtab[i].name) \
1091 LI("Signature schemes", sig, \
1092 sigtab[i].name, sigtab[i].name) \
1093 LI("Encodings", enc, \
1094 enctab[i].name, enctab[i].name) \
1095 LI("Hash functions", hash, \
1096 ghashtab[i], ghashtab[i]->name)
1098 MAKELISTTAB(listtab, LISTS)
1100 int cmd_show(int argc, char *argv[])
1102 return (displaylists(listtab, argv + 1));
1105 static int cmd_help(int, char **);
1107 static cmd cmdtab[] = {
1108 { "help", cmd_help, "help [COMMAND...]" },
1109 { "show", cmd_show, "show [ITEM...]" },
1113 "sign [-adptC] [-k TAG] [-f FORMAT] [-o OUTPUT] [FILE]", "\
1116 -a, --armour Same as `-f pem'.\n\
1117 -b, --binary Treat the input message as binary data.\n\
1118 -d, --detach Produce a detached signature.\n\
1119 -f, --format=FORMAT Encode as FORMAT.\n\
1120 -k, --key=TAG Use public encryption key named by TAG.\n\
1121 -o, --output=FILE Write output to FILE.\n\
1122 -p, --progress Show progress on large files.\n\
1123 -t, --text Canonify input message as a text file.\n\
1124 -C, --nocheck Don't check the private key.\n\
1127 "verify [-abpquvC] [-f FORMAT] [-k TAG] [-o OUTPUT]\n\t\
1128 [FILE [MESSAGE]]", "\
1131 -a, --armour Same as `-f pem'.\n\
1132 -b, --buffer Buffer message until signature is verified.\n\
1133 -f, --format=FORMAT Decode as FORMAT.\n\
1134 -k, --key=TAG Require that the message be signed by key TAG.\n\
1135 -o, --output=FILE Write message to FILE.\n\
1136 -p, --progress Show progress on large files.\n\
1137 -q, --quiet Produce fewer messages.\n\
1138 -t, --freshtime=TIME Only accept signatures made after this time.\n\
1139 -u, --utc Show dates in UTC rather than local time.\n\
1140 -v, --verbose Produce more verbose messages.\n\
1141 -C, --nocheck Don't check the public key.\n\
1144 "info [-au] [-f FORMAT] [FILE]", "\
1147 -a, --armour Same as `-f pem'.\n\
1148 -f, --format=FORMAT Decode as FORMAT.\n\
1149 -u, --utc Show dates in UTC rather than local time.\n\
1152 "format [-apuADE] [-f FORMAT] [-F format] [-m FILE] [-o FILE]\n\t\
1153 [FILE [MESSAGE]]", "\
1156 -a, --armour-in Same as `-f pem'.\n\
1157 -A, --armour-out Same as `-F pem'.\n\
1158 -D, --detach Create detached signature.\n\
1159 -E, --embed Create signature with embedded message.\n\
1160 -f, --format-in=FORMAT Decode input as FORMAT.\n\
1161 -F, --format-out=FORMAT Encode output as FORMAT.\n\
1162 -m, --message=FILE Write message to FILE.\n\
1163 -o, --output=FILE Write new signature to FILE.\n\
1164 -p, --progress Show progress on large files.\n\
1167 }; /* " Emacs seems confused. */
1169 static int cmd_help(int argc, char **argv)
1171 sc_help(cmdtab, stdout, argv + 1);
1175 void version(FILE *fp)
1177 pquis(fp, "$, Catacomb version " VERSION "\n");
1180 static void usage(FILE *fp)
1182 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1185 void help_global(FILE *fp)
1189 Sign and verify data.\n\
1191 Global command-line options:\n\
1193 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1194 -v, --version Show program version number.\n\
1195 -u, --usage Show a terse usage message.\n\
1197 -k, --keyring=FILE Read keys from FILE.\n",
1203 * Arguments: @int argc@ = number of command line arguments
1204 * @char *argv[]@ = vector of command line arguments
1206 * Returns: Zero if successful, nonzero otherwise.
1208 * Use: Encrypts or decrypts files.
1211 int main(int argc, char *argv[])
1217 /* --- Initialize the library --- */
1221 rand_noisesrc(RAND_GLOBAL, &noise_source);
1222 rand_seed(RAND_GLOBAL, 160);
1223 /* trace_on(stderr, 1); */
1225 /* --- Parse options --- */
1228 static struct option opts[] = {
1229 { "help", 0, 0, 'h' },
1230 { "version", 0, 0, 'v' },
1231 { "usage", 0, 0, 'u' },
1232 { "keyring", OPTF_ARGREQ, 0, 'k' },
1235 int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0);
1240 sc_help(cmdtab, stdout, argv + optind);
1262 if (f & f_bogus || argc < 1) {
1267 /* --- Dispatch to the correct subcommand handler --- */
1269 return (findcmd(cmdtab, argv[0])->cmd(argc, argv));
1274 /*----- That's all, folks -------------------------------------------------*/