7 * (c) 2005 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
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
96 /*----- Chunk I/O ---------------------------------------------------------*/
98 static void chunk_write(enc *e, const void *p, size_t n)
104 if (e->ops->write(e, b, 2) || e->ops->write(e, p, n))
105 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
108 static size_t chunk_read(enc *e, void *p)
113 if (e->ops->read(e, b, 2) != 2)
116 if (n && e->ops->read(e, p, n) != n)
121 if (!errno) die(EXIT_FAILURE, "unexpected end-of-file on input");
122 else die(EXIT_FAILURE, "error reading input: %s", strerror(errno));
126 /*----- Message canonification --------------------------------------------*/
128 #define MSGBUFSZ 65536
129 #define MSGBUFTHRESH 32768
131 typedef struct msgcanon {
135 size_t (*read)(struct msgcanon *, void *);
136 void (*write)(struct msgcanon *, const void *, size_t);
137 void (*close)(struct msgcanon *);
140 #define MC_INIT { 0 }
142 static size_t textread(msgcanon *m, void *bp)
144 size_t n = 0, nsp = 0;
149 if (f & F_EOF) return (0);
151 if ((ch = getc(m->fp)) == EOF) goto eof;
152 if (!(f & (F_DETACH | F_MIDLINE)) && ch == '.') {
154 if (ch == '\n') goto eof;
158 if (n >= MSGBUFTHRESH) goto full;
161 } else if (isspace(ch)) {
163 if (n >= MSGBUFSZ) goto full;
168 if (n >= MSGBUFTHRESH) goto full;
182 static void textwrite(msgcanon *m, const void *bp, size_t n)
184 const char *p = bp, *l = p + n;
188 if (!(f & (F_MIDLINE | F_DETACH)) &&
189 (*p == '.' || *p == '-'))
191 if (*p == '\n') f &= ~F_MIDLINE;
199 static size_t binreadembed(msgcanon *m, void *bp)
200 { return (chunk_read(m->e, bp)); }
201 static size_t binreaddetach(msgcanon *m, void *bp)
202 { return (fread(bp, 1, MSGBUFSZ, m->fp)); }
204 static void binwriteembed(msgcanon *m, const void *bp, size_t n)
205 { chunk_write(m->e, bp, n); }
207 static void nullwrite(msgcanon *m, const void *bp, size_t n) { ; }
209 static void mcsetup_readfile(msgcanon *m, unsigned f, const char *fn)
211 m->f = F_DETACH | (f & F_BINARY);
212 if (!fn || strcmp(fn, "-") == 0) {
215 } else if ((m->fp = fopen(fn, (f & F_BINARY) ? "rb" : "r")) == 0)
216 die(EXIT_FAILURE, "couldn't open file `%s': %s", fn, strerror(errno));
217 m->read = (f & F_BINARY) ? binreaddetach : textread;
220 static void mcsetup_writenull(msgcanon *m) { m->write = nullwrite; }
222 static void mcsetup_read(msgcanon *m, unsigned f, enc **ee, const char *dfn)
226 m->f = f | F_NOCLOSE;
228 if (dfn && !(f & F_DETACH)) die(EXIT_FAILURE, "signature is not detached");
230 if (!(f & F_DETACH)) {
234 m->read = binreadembed;
236 m->read = binreaddetach;
237 if (!dfn || strcmp(dfn, "-") == 0)
239 else if ((m->fp = fopen(dfn, "rb")) == 0)
240 die(EXIT_FAILURE, "can't open `%s': %s", dfn, strerror(errno));
246 if (!(f & F_DETACH)) {
247 if (e->ops->decdone(e) || ferror(e->fp))
248 die(EXIT_FAILURE, "error at end of signature header");
252 } else if (!dfn || strcmp(dfn, "-") == 0)
254 else if ((m->fp = fopen(dfn, "r")) == 0)
255 die(EXIT_FAILURE, "can't read file `%s': %s", dfn, strerror(errno));
261 static void mcsetup_write(msgcanon *m, unsigned f, enc **ee)
265 m->f = f | F_NOCLOSE;
268 m->write = nullwrite;
269 else if (f & F_BINARY) {
273 m->write = binwriteembed;
275 if (e->ops->encdone(e) || ferror(e->fp))
276 die(EXIT_FAILURE, "error at end of signature header");
280 m->write = textwrite;
284 static void mc_endread(msgcanon *m, const encops *eops, enc **ee)
286 if (!(m->f & F_DETACH)) {
290 *ee = initdec(eops, m->fp, checkbdry, "CATSIGN SIGNATURE");
292 if (m->fp && !(m->f & F_NOCLOSE)) {
293 if (ferror(m->fp) || fclose(m->fp))
294 die(EXIT_FAILURE, "error closing message file: %s", strerror(errno));
298 static void mc_endwrite(msgcanon *m, const encops *eops, enc **ee)
300 if (!(m->f & F_BINARY)) {
301 if (m->f & F_MIDLINE) putc('\n', m->fp);
302 if (!(m->f & F_DETACH)) {
303 putc('.', m->fp); putc('\n', m->fp);
304 *ee = initenc(eops, m->fp, "CATSIGN SIGNATURE");
306 } else if (!(m->f & F_DETACH)) {
307 chunk_write(m->e, 0, 0);
310 if (m->fp && !(m->f & F_NOCLOSE)) {
311 if (fflush(m->fp) || ferror(m->fp) || fclose(m->fp))
312 die(EXIT_FAILURE, "error closing message file: %s", strerror(errno));
316 /*----- Signature reading and writing -------------------------------------*/
318 static void sig_init(sigmsg *s, unsigned f, uint32 keyid)
320 s->f = f & F_SIGMASK;
325 dstr_create(&s->sig);
328 static void sig_destroy(sigmsg *s)
330 dstr_destroy(&s->kh);
331 dstr_destroy(&s->sig);
332 if (s->s) freesig(s->s);
335 static void sigtobuffer(sigmsg *s, buf *b, int hashp)
340 if (hashp) buf_putu16(b, s->f & F_HASHMASK);
341 else buf_putu16(b, s->f & F_SIGMASK);
342 buf_putu32(b, s->keyid);
343 buf_putu32(b, HI64(t));
344 buf_putu32(b, LO64(t));
345 buf_putdstr16(b, &s->kh);
349 static void dohash(ghash *h, const void *p, size_t n)
351 /* trace_block(1, "hashing", p, n); */
355 static void sig_hash(sigmsg *s)
360 buf_init(&b, bb, sizeof(bb));
361 sigtobuffer(s, &b, 1);
362 dohash(s->s->h, BBASE(&b), BLEN(&b));
365 static void keyhash(key *k, sig *s, dstr *d)
370 h = GH_INIT(GH_CLASS(s->h));
373 key_fingerprint(k, h, &kf);
374 dstr_ensure(d, GH_CLASS(h)->hashsz);
375 GH_DONE(h, d->buf + d->len);
376 d->len += GH_CLASS(h)->hashsz;
380 static void sig_writeheader(enc *e, sigmsg *s)
385 buf_init(&b, bb, sizeof(bb));
386 sigtobuffer(s, &b, 0);
387 chunk_write(e, BBASE(&b), BLEN(&b));
390 static void sig_writesig(enc *e, sigmsg *s)
391 { chunk_write(e, s->sig.buf, s->sig.len); }
393 static void diechoke(const char *m, void *p)
394 { die(EXIT_FAILURE, "%s%s%s", p, p ? ": " : "", m); }
396 static void sig_readheader(enc *e, sigmsg *s,
397 void (*choke)(const char *, void *), void *p)
406 n = chunk_read(e, bb);
408 if (buf_getu16(&b, &f)) choke("missing flags", p);
409 if (buf_getu32(&b, &x)) choke("missing keyid", p);
411 if (buf_getu32(&b, &x) || buf_getu32(&b, &y))
412 choke("missing datestamp", p);
413 SET64(t, x, y); s->t = GET64(time_t, t);
414 if (buf_getdstr16(&b, &s->kh))
415 choke("missing key hash", p);
417 choke("junk at end", p);
420 static void sig_readsig(enc *e, sigmsg *s)
425 n = chunk_read(e, bb);
426 dstr_putm(&s->sig, bb, n);
429 /*----- Signing -----------------------------------------------------------*/
431 static int sign(int argc, char *argv[])
433 const char *ef = "binary", *fn = 0, *of = 0, *kn = "ccsig", *err;
444 msgcanon mc_in = MC_INIT, mc_out = MC_INIT;
448 static const struct option opt[] = {
449 { "armour", 0, 0, 'a' },
450 { "armor", 0, 0, 'a' },
451 { "binary", 0, 0, 'b' },
452 { "detach", 0, 0, 'd' },
453 { "key", OPTF_ARGREQ, 0, 'k' },
454 { "format", OPTF_ARGREQ, 0, 'f' },
455 { "output", OPTF_ARGREQ, 0, 'o' },
456 { "text", 0, 0, 't' },
457 { "nocheck", 0, 0, 'C' },
460 i = mdwopt(argc, argv, "k:f:o:abdtC", opt, 0, 0, 0);
463 case 'k': kn = optarg; break;
464 case 'f': ef = optarg; break;
465 case 'o': of = optarg; break;
466 case 'a': ef = "pem"; break;
467 case 't': f &= ~F_BINARY; break;
468 case 'b': f |= F_BINARY; break;
469 case 'd': f |= F_DETACH; break;
470 case 'C': f |= F_NOCHECK; break;
471 default: f |= F_BOGUS; break;
474 if (argc - optind > 1 || (f & F_BOGUS))
475 die(EXIT_FAILURE, "Usage: sign [-OPTIONS] [FILE]");
477 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
478 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
479 if ((k = key_bytag(&kf, kn)) == 0)
480 die(EXIT_FAILURE, "key `%s' not found", kn);
482 if ((eo = getenc(ef)) == 0)
483 die(EXIT_FAILURE, "encoding `%s' not found", ef);
485 fn = (optind >= argc) ? 0 : argv[optind++];
487 if (!of || strcmp(of, "-") == 0)
489 else if ((ofp = fopen(of, eo->wmode)) == 0) {
490 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
491 ofp, strerror(errno));
494 /* --- Start the work --- */
496 sig_init(&s, f, k->id);
499 s.s = getsig(k, "ccsig", 1);
500 if (!(f & F_NOCHECK) && (err = s.s->ops->check(s.s)) != 0)
501 moan("key %s fails check: %s", d.buf, err);
502 keyhash(k, s.s, &s.kh);
504 (f & F_DETACH) ? "CATSIGN SIGNATURE" :
505 (f & F_BINARY) ? "CATSIGN MESSAGE" :
506 "CATSIGN MESSAGE HEADER");
507 sig_writeheader(e, &s);
509 /* --- Hash the message --- */
511 mcsetup_readfile(&mc_in, f, fn);
512 mcsetup_write(&mc_out, f, &e);
515 n = mc_in.read(&mc_in, bb);
517 dohash(s.s->h, bb, n);
518 mc_out.write(&mc_out, bb, n);
520 mc_endread(&mc_in, 0, 0);
521 mc_endwrite(&mc_out, eo, &e);
523 /* --- Write the signature --- */
525 if (s.s->ops->doit(s.s, &s.sig))
526 die(EXIT_FAILURE, "signature failed");
529 if (fflush(ofp) || ferror(ofp) || fclose(ofp))
530 die(EXIT_FAILURE, "error writing signature: %s", strerror(errno));
532 /* --- All done --- */
541 /*----- Verifying ---------------------------------------------------------*/
543 typedef struct vrfctx {
549 static int vrfbdry(const char *b, void *p)
553 if (strcmp(b, "CATSIGN MESSAGE") == 0) {
555 v->m |= F_BINARY | F_DETACH;
557 } else if (strcmp(b, "CATSIGN MESSAGE HEADER") == 0) {
558 v->m |= F_BINARY | F_DETACH;
560 } else if (strcmp(b, "CATSIGN SIGNATURE") == 0) {
568 static void vrfchoke(const char *m, void *p)
571 if (v->verb) printf("FAIL %s: %s\n", v->what, m);
575 static int verify(int argc, char *argv[])
577 const char *ef = "binary", *of = 0, *dfn = 0, *kn = 0, *err;
578 vrfctx v = { 0, 0, 1 };
582 FILE *fp, *ofp = 0, *rfp = 0;
588 dstr d = DSTR_INIT, dd = DSTR_INIT;
590 msgcanon mc_in = MC_INIT;
594 static const struct option opt[] = {
595 { "armour", 0, 0, 'a' },
596 { "armor", 0, 0, 'a' },
597 { "buffer", 0, 0, 'b' },
598 { "key", OPTF_ARGREQ, 0, 'k' },
599 { "format", OPTF_ARGREQ, 0, 'f' },
600 { "output", OPTF_ARGREQ, 0, 'o' },
601 { "quiet", 0, 0, 'q' },
602 { "utc", 0, 0, 'u' },
603 { "fresh-time", 0, 0, 't' },
604 { "gmt", 0, 0, 'u' },
605 { "verbose", 0, 0, 'v' },
606 { "nocheck", 0, 0, 'C' },
609 i = mdwopt(argc, argv, "k:f:o:abqt:uvC", opt, 0, 0, 0);
612 case 'a': ef = "pem"; break;
613 case 'b': v.f |= F_BUFFER; break;
614 case 'k': kn = optarg; break;
615 case 'f': ef = optarg; break;
616 case 'o': of = optarg; break;
617 case 'u': v.f |= F_UTC; break;
618 case 'C': v.f |= F_NOCHECK; break;
620 if (strcmp(optarg, "always") == 0) t_fresh = 0;
621 else if ((t_fresh = get_date(optarg, 0)) < 0)
622 die(EXIT_FAILURE, "bad freshness time");
624 case 'q': if (v.verb > 0) v.verb--; break;
625 case 'v': if (v.verb < 10) v.verb++; break;
626 default: v.f |= F_BOGUS; break;
629 if (argc - optind > 2 || (v.f & F_BOGUS))
630 die(EXIT_FAILURE, "Usage: verify [-OPTIONS] [FILE [MESSAGE]]");
632 if ((eo = getenc(ef)) == 0)
633 die(EXIT_FAILURE, "encoding `%s' not found", ef);
637 else if (strcmp(argv[optind], "-") == 0) {
640 } else if ((fp = fopen(argv[optind], eo->rmode)) == 0) {
641 die(EXIT_FAILURE, "couldn't open file `%s': %s",
642 argv[optind], strerror(errno));
646 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
647 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
648 if (kn && (kk = key_bytag(&kf, kn)) == 0)
649 die(EXIT_FAILURE, "key `%s' not found", kn);
651 e = initdec(eo, fp, vrfbdry, &v);
653 /* --- Read the header chunk --- */
655 v.what = "malformed header";
656 sig_readheader(e, &s, vrfchoke, &v);
658 if (((s.f ^ v.f) & v.m) != 0) {
659 if (v.verb) printf("FAIL boundary string inconsistent with contents\n");
664 if ((k = key_byid(&kf, s.keyid)) == 0) {
665 if (v.verb) printf("FAIL key id %08lx not found\n",
666 (unsigned long)s.keyid);
669 if (kk && k->id != kk->id) {
671 dstr_reset(&d); key_fulltag(k, &d);
672 dstr_reset(&dd); key_fulltag(kk, &dd);
673 printf("FAIL signing key is %s; expected key %s\n", d.buf, dd.buf);
678 s.s = getsig(k, "ccsig", 0);
679 dstr_reset(&d); key_fulltag(k, &d);
680 if (!(v.f & F_NOCHECK) && v.verb && (err = s.s->ops->check(s.s)) != 0)
681 printf("WARN verification key %s fails check: %s\n", d.buf, err);
683 dstr_reset(&dd); keyhash(k, s.s, &dd);
684 if (dd.len != s.kh.len || memcmp(dd.buf, s.kh.buf, dd.len) != 0) {
685 if (v.verb) printf("FAIL key hash mismatch\n");
689 /* --- Now a merry dance --- */
694 dfn = argv[optind++];
695 mcsetup_read(&mc_in, v.f, &e, dfn);
697 if (!of && (v.f & F_DETACH)) {
700 } else if (!of || strcmp(of, "-") == 0) {
704 if (of && !(v.f & F_BUFFER)) {
705 if ((ofp = fopen(of, (v.f & F_BINARY) ? "wb" : "w")) == 0) {
706 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
707 of, strerror(errno));
710 } else if ((rfp = tmpfile()) == 0)
711 die(EXIT_FAILURE, "couldn't create temporary file: %s", strerror(errno));
713 /* --- Read the message and verify the signature --- */
717 n = mc_in.read(&mc_in, bb);
719 dohash(s.s->h, bb, n);
720 if (rfp) fwrite(bb, 1, n, rfp);
722 mc_endread(&mc_in, eo, &e);
723 if (!(v.f & F_DETACH))
725 if (rfp && (ferror(rfp) || fflush(rfp))) {
726 if (v.verb) printf("FAIL error writing message: %s\n", strerror(errno));
730 /* --- Check the signature --- */
732 if (s.s->ops->doit(s.s, &s.sig)) {
733 if (v.verb) printf("FAIL signature verification failed\n");
736 if (t_fresh && s.t < t_fresh) {
737 if (v.verb) printf("FAIL signature is stale\n");
741 if (v.verb) printf("FAIL signature timestamp in the future\n");
745 tm = (v.f & F_UTC) ? gmtime(&s.t) : localtime(&s.t);
746 strftime(bb, sizeof(bb), "%Y-%m-%d %H:%M:%S %Z", tm);
747 printf("INFO good-signature %s\n", d.buf);
748 printf("INFO date %s\n", bb);
751 /* --- Unbuffer buffered input --- */
753 if (v.f & F_BUFFER) {
754 if (!ofp && (ofp = fopen(of, "wb")) == 0) {
755 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
756 of, strerror(errno));
759 if (v.verb && ofp == stdout) printf("DATA\n");
761 n = fread(bb, 1, sizeof(bb), rfp);
763 if (fwrite(bb, 1, n, ofp) < n)
764 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
766 if (ferror(rfp) || fclose(rfp))
767 die(EXIT_FAILURE, "error unbuffering output: %s", strerror(errno));
769 if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp)))
770 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
772 /* --- Tidy up --- */
775 if (v.verb && ofp != stdout)
776 printf("OK verified successfully\n");
785 /*----- Reformatting ------------------------------------------------------*/
787 static int format(int argc, char *argv[])
789 const char *ief = "binary", *oef = "binary";
790 const char *dfn = 0, *of = 0, *mf = 0;
792 FILE *fp, *ofp = 0, *mfp = 0;
795 msgcanon mc_in = MC_INIT, mc_out = MC_INIT;
797 vrfctx v = { 0, 0, 1 };
798 unsigned f = 0, fm = ~F_SIGMASK, sf;
799 const encops *ieo, *oeo;
803 static const struct option opt[] = {
804 { "armour-in", 0, 0, 'a' },
805 { "armor-in", 0, 0, 'a' },
806 { "armour-out", 0, 0, 'A' },
807 { "armor-out", 0, 0, 'A' },
808 { "detach", 0, 0, 'D' },
809 { "embed", 0, 0, 'E' },
810 { "format-in", OPTF_ARGREQ, 0, 'f' },
811 { "format-out", OPTF_ARGREQ, 0, 'F' },
812 { "message", OPTF_ARGREQ, 0, 'm' },
813 { "output", OPTF_ARGREQ, 0, 'o' },
816 i = mdwopt(argc, argv, "f:F:m:o:aADE", opt, 0, 0, 0);
819 case 'a': ief = "pem"; break;
820 case 'A': oef = "pem"; break;
821 case 'f': ief = optarg; break;
822 case 'F': oef = optarg; break;
823 case 'D': f |= F_DETACH; fm |= F_DETACH; break;
824 case 'E': f &= ~F_DETACH; fm |= F_DETACH; break;
825 case 'm': mf = optarg; break;
826 case 'o': of = optarg; break;
827 default: f |= F_BOGUS; break;
831 if (argc - optind > 2 || (f & F_BOGUS))
832 die(EXIT_FAILURE, "Usage: format [-OPTIONS] [FILE [MESSAGE]]");
834 if ((ieo = getenc(ief)) == 0)
835 die(EXIT_FAILURE, "encoding `%s' not found", ief);
836 if ((oeo = getenc(oef)) == 0)
837 die(EXIT_FAILURE, "encoding `%s' not found", oef);
841 else if (strcmp(argv[optind], "-") == 0) {
844 } else if ((fp = fopen(argv[optind], ieo->rmode)) == 0) {
845 die(EXIT_FAILURE, "couldn't open file `%s': %s",
846 argv[optind], strerror(errno));
851 dfn = argv[optind++];
853 ie = initdec(ieo, fp, vrfbdry, &v);
855 /* --- Read the header chunk --- */
857 sig_readheader(ie, &s, diechoke, "malformed header");
859 if (((s.f ^ v.f) & v.m) != 0)
860 moan("boundary string inconsistent with contents (ignoring)");
862 mcsetup_read(&mc_in, s.f, &ie, dfn);
864 /* --- Prepare the output stuff --- */
866 if (!of && !mf) of = "-";
868 f = (f & fm) | (s.f & ~fm);
875 mcsetup_writenull(&mc_out);
877 if (strcmp(of, "-") == 0)
879 else if ((ofp = fopen(of, oeo->wmode)) == 0) {
880 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
881 of, strerror(errno));
883 oe = initenc(oeo, ofp,
884 (f & F_DETACH) ? "CATSIGN SIGNATURE" :
885 (f & F_BINARY) ? "CATSIGN MESSAGE" :
886 "CATSIGN MESSAGE HEADER");
887 sig_writeheader(oe, &s);
888 mcsetup_write(&mc_out, f, &oe);
892 if (strcmp(mf, "-") == 0)
894 else if ((mfp = fopen(mf, (f & F_BINARY) ? "wb" : "w")) == 0) {
895 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
896 mf, strerror(errno));
900 /* --- Wade through the message body --- */
903 n = mc_in.read(&mc_in, bb);
905 mc_out.write(&mc_out, bb, n);
906 if (mfp) fwrite(bb, 1, n, mfp);
908 mc_endread(&mc_in, ieo, &ie);
909 if (of) mc_endwrite(&mc_out, oeo, &oe);
911 /* --- Write the signature --- */
913 if (!(sf & F_DETACH))
916 sig_writesig(oe, &s);
917 oe->ops->encdone(oe);
920 /* --- All done --- */
922 ie->ops->decdone(ie);
923 if (ferror(fp) || fclose(fp))
924 die(EXIT_FAILURE, "error reading input signature: %s", strerror(errno));
925 if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp)))
926 die(EXIT_FAILURE, "error writing output signature: %s", strerror(errno));
927 if (mfp && (fflush(mfp) || ferror(mfp) || fclose(mfp)))
928 die(EXIT_FAILURE, "error writing output message: %s", strerror(errno));
935 static void infochoke(const char *m, void *p)
938 printf("BAD %s: %s\n", v->what, m);
942 static void infokeyreport(const char *file, int line,
943 const char *err, void *p)
946 static int info(int argc, char *argv[])
948 const char *ef = "binary";
949 vrfctx v = { 0, 0, 1 };
962 static const struct option opt[] = {
963 { "armour", 0, 0, 'a' },
964 { "armor", 0, 0, 'a' },
965 { "format", OPTF_ARGREQ, 0, 'f' },
966 { "gmt", 0, 0, 'u' },
967 { "utc", 0, 0, 'u' },
970 i = mdwopt(argc, argv, "f:au", opt, 0, 0, 0);
973 case 'a': ef = "pem"; break;
974 case 'f': ef = optarg; break;
975 case 'u': v.f |= F_UTC; break;
976 default: v.f |= F_BOGUS; break;
979 if (argc - optind > 1 || (v.f & F_BOGUS))
980 die(EXIT_FAILURE, "Usage: info [-OPTIONS] [FILE]");
982 if ((eo = getenc(ef)) == 0)
983 die(EXIT_FAILURE, "encoding `%s' not found", ef);
987 else if (strcmp(argv[optind], "-") == 0) {
990 } else if ((fp = fopen(argv[optind], eo->rmode)) == 0) {
991 die(EXIT_FAILURE, "couldn't open file `%s': %s",
992 argv[optind], strerror(errno));
996 if (key_open(&kf, keyring, KOPEN_READ, infokeyreport, 0)) {
997 printf("NOTE can't open keyring `%s'\n", keyring);
1000 e = initdec(eo, fp, vrfbdry, &v);
1002 v.what = "malformed header";
1003 sig_readheader(e, &s, infochoke, &v);
1005 printf("INFO flags %sdetach %sbinary\n",
1006 (s.f & F_DETACH) ? "" : "!",
1007 (s.f & F_BINARY) ? "" : "!");
1009 if (((s.f ^ v.f) & v.m) != 0) {
1010 printf("WARN boundary string inconsistent with contents\n");
1011 printf("INFO expected-flags");
1012 if (v.m & F_DETACH) printf(" %sdetach", (v.f & F_DETACH) ? "" : "!");
1013 if (v.m & F_BINARY) printf(" %sbinary", (v.f & F_BINARY) ? "" : "!");
1018 tm = (v.f & F_UTC) ? gmtime(&s.t) : localtime(&s.t);
1019 strftime(bb, sizeof(bb), "%Y-%m-%d %H:%M:%S %Z", tm);
1020 printf("INFO date %s\n", bb);
1022 if (keyring && (k = key_byid(&kf, s.keyid)) != 0) {
1023 dstr_reset(&d); key_fulltag(k, &d);
1024 printf("INFO key %s\n", d.buf);
1026 printf("INFO unknown-key %08lx\n", (unsigned long)s.keyid);
1028 if (keyring) key_close(&kf);
1034 /*----- Main code ---------------------------------------------------------*/
1038 listtab[i].name, listtab[i].name) \
1039 LI("Signature schemes", sig, \
1040 sigtab[i].name, sigtab[i].name) \
1041 LI("Encodings", enc, \
1042 enctab[i].name, enctab[i].name) \
1043 LI("Hash functions", hash, \
1044 ghashtab[i], ghashtab[i]->name)
1046 MAKELISTTAB(listtab, LISTS)
1048 int cmd_show(int argc, char *argv[])
1050 return (displaylists(listtab, argv + 1));
1053 static int cmd_help(int, char **);
1055 static cmd cmdtab[] = {
1056 { "help", cmd_help, "help [COMMAND...]" },
1057 { "show", cmd_show, "show [ITEM...]" },
1061 "sign [-adtC] [-k TAG] [-f FORMAT] [-o OUTPUT] [FILE]", "\
1064 -a, --armour Same as `-f pem'.\n\
1065 -b, --binary Treat the input message as binary data.\n\
1066 -d, --detach Produce a detached signature.\n\
1067 -f, --format=FORMAT Encode as FORMAT.\n\
1068 -k, --key=TAG Use public encryption key named by TAG.\n\
1069 -o, --output=FILE Write output to FILE.\n\
1070 -t, --text Canonify input message as a text file.\n\
1071 -C, --nocheck Don't check the private key.\n\
1074 "verify [-abquvC] [-f FORMAT] [-k TAG] [-o OUTPUT]\n\t\
1075 [FILE [MESSAGE]]", "\
1078 -a, --armour Same as `-f pem'.\n\
1079 -b, --buffer Buffer message until signature is verified.\n\
1080 -f, --format=FORMAT Decode as FORMAT.\n\
1081 -k, --key=TAG Require that the message be signed by key TAG.\n\
1082 -o, --output=FILE Write message to FILE.\n\
1083 -q, --quiet Produce fewer messages.\n\
1084 -t, --freshtime=TIME Only accept signatures made after this time.\n\
1085 -u, --utc Show dates in UTC rather than local time.\n\
1086 -v, --verbose Produce more verbose messages.\n\
1087 -C, --nocheck Don't check the public key.\n\
1090 "info [-au] [-f FORMAT] [FILE]", "\
1093 -a, --armour Same as `-f pem'.\n\
1094 -f, --format=FORMAT Decode as FORMAT.\n\
1095 -u, --utc Show dates in UTC rather than local time.\n\
1098 "format [-auADE] [-f FORMAT] [-F format] [-m FILE] [-o FILE]\n\t\
1099 [FILE [MESSAGE]]", "\
1102 -a, --armour-in Same as `-f pem'.\n\
1103 -A, --armour-out Same as `-F pem'.\n\
1104 -D, --detach Create detached signature.\n\
1105 -E, --embed Create signature with embedded message.\n\
1106 -f, --format-in=FORMAT Decode input as FORMAT.\n\
1107 -F, --format-out=FORMAT Encode output as FORMAT.\n\
1108 -m, --message=FILE Write message to FILE.\n\
1109 -o, --output=FILE Write new signature to FILE.\n\
1112 }; /* " Emacs seems confused. */
1114 static int cmd_help(int argc, char **argv)
1116 sc_help(cmdtab, stdout, argv + 1);
1120 void version(FILE *fp)
1122 pquis(fp, "$, Catacomb version " VERSION "\n");
1125 static void usage(FILE *fp)
1127 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1130 void help_global(FILE *fp)
1134 Sign and verify data.\n\
1136 Global command-line options:\n\
1138 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1139 -v, --version Show program version number.\n\
1140 -u, --usage Show a terse usage message.\n\
1142 -k, --keyring=FILE Read keys from FILE.\n",
1148 * Arguments: @int argc@ = number of command line arguments
1149 * @char *argv[]@ = vector of command line arguments
1151 * Returns: Zero if successful, nonzero otherwise.
1153 * Use: Encrypts or decrypts files.
1156 int main(int argc, char *argv[])
1162 /* --- Initialize the library --- */
1166 rand_noisesrc(RAND_GLOBAL, &noise_source);
1167 rand_seed(RAND_GLOBAL, 160);
1168 /* trace_on(stderr, 1); */
1170 /* --- Parse options --- */
1173 static struct option opts[] = {
1174 { "help", 0, 0, 'h' },
1175 { "version", 0, 0, 'v' },
1176 { "usage", 0, 0, 'u' },
1177 { "keyring", OPTF_ARGREQ, 0, 'k' },
1180 int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0);
1185 sc_help(cmdtab, stdout, argv + optind);
1207 if (f & f_bogus || argc < 1) {
1212 /* --- Dispatch to the correct subcommand handler --- */
1214 return (findcmd(cmdtab, argv[0])->cmd(argc, argv));
1219 /*----- That's all, folks -------------------------------------------------*/