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 */
95 /*----- Chunk I/O ---------------------------------------------------------*/
97 static void chunk_write(enc
*e
, const void *p
, size_t n
)
103 if (e
->ops
->write(e
, b
, 2) || e
->ops
->write(e
, p
, n
))
104 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
107 static size_t chunk_read(enc
*e
, void *p
)
112 if (e
->ops
->read(e
, b
, 2) != 2)
115 if (n
&& e
->ops
->read(e
, p
, n
) != n
)
120 if (!errno
) die(EXIT_FAILURE
, "unexpected end-of-file on input");
121 else die(EXIT_FAILURE
, "error reading input: %s", strerror(errno
));
125 /*----- Message canonification --------------------------------------------*/
127 #define MSGBUFSZ 65536
128 #define MSGBUFTHRESH 32768
130 typedef struct msgcanon
{
134 size_t (*read
)(struct msgcanon
*, void *);
135 void (*write
)(struct msgcanon
*, const void *, size_t);
136 void (*close
)(struct msgcanon
*);
139 #define MC_INIT { 0 }
141 static size_t textread(msgcanon
*m
, void *bp
)
143 size_t n
= 0, nsp
= 0;
148 if (f
& F_EOF
) return (0);
150 if ((ch
= getc(m
->fp
)) == EOF
) goto eof
;
151 if (!(f
& (F_DETACH
| F_MIDLINE
)) && ch
== '.') {
153 if (ch
== '\n') goto eof
;
157 if (n
>= MSGBUFTHRESH
) goto full
;
160 } else if (isspace(ch
)) {
162 if (n
>= MSGBUFSZ
) goto full
;
167 if (n
>= MSGBUFTHRESH
) goto full
;
181 static void textwrite(msgcanon
*m
, const void *bp
, size_t n
)
183 const char *p
= bp
, *l
= p
+ n
;
187 if (!(f
& (F_MIDLINE
| F_DETACH
)) &&
188 (*p
== '.' || *p
== '-'))
190 if (*p
== '\n') f
&= ~F_MIDLINE
;
198 static size_t binreadembed(msgcanon
*m
, void *bp
)
199 { return (chunk_read(m
->e
, bp
)); }
200 static size_t binreaddetach(msgcanon
*m
, void *bp
)
201 { return (fread(bp
, 1, MSGBUFSZ
, m
->fp
)); }
203 static void binwriteembed(msgcanon
*m
, const void *bp
, size_t n
)
204 { chunk_write(m
->e
, bp
, n
); }
206 static void nullwrite(msgcanon
*m
, const void *bp
, size_t n
) { ; }
208 static void mcsetup_readfile(msgcanon
*m
, unsigned f
, const char *fn
)
210 m
->f
= F_DETACH
| (f
& F_BINARY
);
211 if (!fn
|| strcmp(fn
, "-") == 0) {
214 } else if ((m
->fp
= fopen(fn
, (f
& F_BINARY
) ?
"rb" : "r")) == 0)
215 die(EXIT_FAILURE
, "couldn't open file `%s': %s", fn
, strerror(errno
));
216 m
->read
= (f
& F_BINARY
) ? binreaddetach
: textread
;
219 static void mcsetup_writenull(msgcanon
*m
) { m
->write
= nullwrite
; }
221 static void mcsetup_read(msgcanon
*m
, unsigned f
, enc
**ee
, const char *dfn
)
225 m
->f
= f
| F_NOCLOSE
;
227 if (dfn
&& !(f
& F_DETACH
)) die(EXIT_FAILURE
, "signature is not detached");
229 if (!(f
& F_DETACH
)) {
233 m
->read
= binreadembed
;
235 m
->read
= binreaddetach
;
236 if (!dfn
|| strcmp(dfn
, "-") == 0)
238 else if ((m
->fp
= fopen(dfn
, "rb")) == 0)
239 die(EXIT_FAILURE
, "can't open `%s': %s", dfn
, strerror(errno
));
245 if (!(f
& F_DETACH
)) {
246 if (e
->ops
->decdone(e
) || ferror(e
->fp
))
247 die(EXIT_FAILURE
, "error at end of signature header");
251 } else if (!dfn
|| strcmp(dfn
, "-") == 0)
253 else if ((m
->fp
= fopen(dfn
, "r")) == 0)
254 die(EXIT_FAILURE
, "can't read file `%s': %s", dfn
, strerror(errno
));
260 static void mcsetup_write(msgcanon
*m
, unsigned f
, enc
**ee
)
264 m
->f
= f
| F_NOCLOSE
;
267 m
->write
= nullwrite
;
268 else if (f
& F_BINARY
) {
272 m
->write
= binwriteembed
;
274 if (e
->ops
->encdone(e
) || ferror(e
->fp
))
275 die(EXIT_FAILURE
, "error at end of signature header");
279 m
->write
= textwrite
;
283 static void mc_endread(msgcanon
*m
, const encops
*eops
, enc
**ee
)
285 if (!(m
->f
& F_DETACH
)) {
289 *ee
= initdec(eops
, m
->fp
, checkbdry
, "CATSIGN SIGNATURE");
291 if (m
->fp
&& !(m
->f
& F_NOCLOSE
)) {
292 if (ferror(m
->fp
) || fclose(m
->fp
))
293 die(EXIT_FAILURE
, "error closing message file: %s", strerror(errno
));
297 static void mc_endwrite(msgcanon
*m
, const encops
*eops
, enc
**ee
)
299 if (!(m
->f
& F_BINARY
)) {
300 if (m
->f
& F_MIDLINE
) putc('\n', m
->fp
);
301 if (!(m
->f
& F_DETACH
)) {
302 putc('.', m
->fp
); putc('\n', m
->fp
);
303 *ee
= initenc(eops
, m
->fp
, "CATSIGN SIGNATURE");
305 } else if (!(m
->f
& F_DETACH
)) {
306 chunk_write(m
->e
, 0, 0);
309 if (m
->fp
&& !(m
->f
& F_NOCLOSE
)) {
310 if (fflush(m
->fp
) || ferror(m
->fp
) || fclose(m
->fp
))
311 die(EXIT_FAILURE
, "error closing message file: %s", strerror(errno
));
315 /*----- Signature reading and writing -------------------------------------*/
317 static void sig_init(sigmsg
*s
, unsigned f
, uint32 keyid
)
319 s
->f
= f
& F_SIGMASK
;
324 dstr_create(&s
->sig
);
327 static void sig_destroy(sigmsg
*s
)
329 dstr_destroy(&s
->kh
);
330 dstr_destroy(&s
->sig
);
331 if (s
->s
) freesig(s
->s
);
334 static void sigtobuffer(sigmsg
*s
, buf
*b
, int hashp
)
339 if (hashp
) buf_putu16(b
, s
->f
& F_HASHMASK
);
340 else buf_putu16(b
, s
->f
& F_SIGMASK
);
341 buf_putu32(b
, s
->keyid
);
342 buf_putu32(b
, HI64(t
));
343 buf_putu32(b
, LO64(t
));
344 buf_putdstr16(b
, &s
->kh
);
348 static void dohash(ghash
*h
, const void *p
, size_t n
)
350 /* trace_block(1, "hashing", p, n); */
354 static void sig_hash(sigmsg
*s
)
359 buf_init(&b
, bb
, sizeof(bb
));
360 sigtobuffer(s
, &b
, 1);
361 dohash(s
->s
->h
, BBASE(&b
), BLEN(&b
));
364 static void keyhash(key
*k
, sig
*s
, dstr
*d
)
369 h
= GH_INIT(GH_CLASS(s
->h
));
372 key_fingerprint(k
, h
, &kf
);
373 dstr_ensure(d
, GH_CLASS(h
)->hashsz
);
374 GH_DONE(h
, d
->buf
+ d
->len
);
375 d
->len
+= GH_CLASS(h
)->hashsz
;
379 static void sig_writeheader(enc
*e
, sigmsg
*s
)
384 buf_init(&b
, bb
, sizeof(bb
));
385 sigtobuffer(s
, &b
, 0);
386 chunk_write(e
, BBASE(&b
), BLEN(&b
));
389 static void sig_writesig(enc
*e
, sigmsg
*s
)
390 { chunk_write(e
, s
->sig
.buf
, s
->sig
.len
); }
392 static void diechoke(const char *m
, void *p
)
393 { die(EXIT_FAILURE
, "%s%s%s", p
, p ?
": " : "", m
); }
395 static void sig_readheader(enc
*e
, sigmsg
*s
,
396 void (*choke
)(const char *, void *), void *p
)
405 n
= chunk_read(e
, bb
);
407 if (buf_getu16(&b
, &f
)) choke("missing flags", p
);
408 if (buf_getu32(&b
, &x
)) choke("missing keyid", p
);
410 if (buf_getu32(&b
, &x
) || buf_getu32(&b
, &y
))
411 choke("missing datestamp", p
);
412 SET64(t
, x
, y
); s
->t
= GET64(time_t, t
);
413 if (buf_getdstr16(&b
, &s
->kh
))
414 choke("missing key hash", p
);
416 choke("junk at end", p
);
419 static void sig_readsig(enc
*e
, sigmsg
*s
)
424 n
= chunk_read(e
, bb
);
425 dstr_putm(&s
->sig
, bb
, n
);
428 /*----- Signing -----------------------------------------------------------*/
430 static int sign(int argc
, char *argv
[])
432 const char *ef
= "binary", *fn
= 0, *of
= 0, *kn
= "ccsig", *err
;
443 msgcanon mc_in
= MC_INIT
, mc_out
= MC_INIT
;
447 static const struct option opt
[] = {
448 { "armour", 0, 0, 'a' },
449 { "armor", 0, 0, 'a' },
450 { "binary", 0, 0, 'b' },
451 { "detach", 0, 0, 'd' },
452 { "key", OPTF_ARGREQ
, 0, 'k' },
453 { "format", OPTF_ARGREQ
, 0, 'f' },
454 { "output", OPTF_ARGREQ
, 0, 'o' },
455 { "text", 0, 0, 't' },
458 i
= mdwopt(argc
, argv
, "k:f:o:abdt", opt
, 0, 0, 0);
461 case 'k': kn
= optarg
; break;
462 case 'f': ef
= optarg
; break;
463 case 'o': of
= optarg
; break;
464 case 'a': ef
= "pem"; break;
465 case 't': f
&= ~F_BINARY
; break;
466 case 'b': f
|= F_BINARY
; break;
467 case 'd': f
|= F_DETACH
; break;
468 default: f
|= F_BOGUS
; break;
471 if (argc
- optind
> 1 || (f
& F_BOGUS
))
472 die(EXIT_FAILURE
, "Usage: sign [-OPTIONS] [FILE]");
474 if (key_open(&kf
, keyring
, KOPEN_READ
, key_moan
, 0))
475 die(EXIT_FAILURE
, "can't open keyring `%s'", keyring
);
476 if ((k
= key_bytag(&kf
, kn
)) == 0)
477 die(EXIT_FAILURE
, "key `%s' not found", kn
);
479 if ((eo
= getenc(ef
)) == 0)
480 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
482 fn
= (optind
>= argc
) ?
0 : argv
[optind
++];
484 if (!of
|| strcmp(of
, "-") == 0)
486 else if ((ofp
= fopen(of
, eo
->wmode
)) == 0) {
487 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
488 ofp
, strerror(errno
));
491 /* --- Start the work --- */
493 sig_init(&s
, f
, k
->id
);
496 s
.s
= getsig(k
, "ccsig", 1);
497 if ((err
= s
.s
->ops
->check(s
.s
)) != 0)
498 moan("key %s fails check: %s", d
.buf
, err
);
499 keyhash(k
, s
.s
, &s
.kh
);
501 (f
& F_DETACH
) ?
"CATSIGN SIGNATURE" :
502 (f
& F_BINARY
) ?
"CATSIGN MESSAGE" :
503 "CATSIGN MESSAGE HEADER");
504 sig_writeheader(e
, &s
);
506 /* --- Hash the message --- */
508 mcsetup_readfile(&mc_in
, f
, fn
);
509 mcsetup_write(&mc_out
, f
, &e
);
512 n
= mc_in
.read(&mc_in
, bb
);
514 dohash(s
.s
->h
, bb
, n
);
515 mc_out
.write(&mc_out
, bb
, n
);
517 mc_endread(&mc_in
, 0, 0);
518 mc_endwrite(&mc_out
, eo
, &e
);
520 /* --- Write the signature --- */
522 if (s
.s
->ops
->doit(s
.s
, &s
.sig
))
523 die(EXIT_FAILURE
, "signature failed");
526 if (fflush(ofp
) || ferror(ofp
) || fclose(ofp
))
527 die(EXIT_FAILURE
, "error writing signature: %s", strerror(errno
));
529 /* --- All done --- */
538 /*----- Verifying ---------------------------------------------------------*/
540 typedef struct vrfctx
{
546 static int vrfbdry(const char *b
, void *p
)
550 if (strcmp(b
, "CATSIGN MESSAGE") == 0) {
552 v
->m
|= F_BINARY
| F_DETACH
;
554 } else if (strcmp(b
, "CATSIGN MESSAGE HEADER") == 0) {
555 v
->m
|= F_BINARY
| F_DETACH
;
557 } else if (strcmp(b
, "CATSIGN SIGNATURE") == 0) {
565 static void vrfchoke(const char *m
, void *p
)
568 if (v
->verb
) printf("FAIL %s: %s\n", v
->what
, m
);
572 static int verify(int argc
, char *argv
[])
574 const char *ef
= "binary", *of
= 0, *dfn
= 0, *kn
= 0, *err
;
575 vrfctx v
= { 0, 0, 1 };
579 FILE *fp
, *ofp
= 0, *rfp
= 0;
585 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
587 msgcanon mc_in
= MC_INIT
;
591 static const struct option opt
[] = {
592 { "armour", 0, 0, 'a' },
593 { "armor", 0, 0, 'a' },
594 { "buffer", 0, 0, 'b' },
595 { "key", OPTF_ARGREQ
, 0, 'k' },
596 { "format", OPTF_ARGREQ
, 0, 'f' },
597 { "output", OPTF_ARGREQ
, 0, 'o' },
598 { "quiet", 0, 0, 'q' },
599 { "utc", 0, 0, 'u' },
600 { "fresh-time", 0, 0, 't' },
601 { "gmt", 0, 0, 'u' },
602 { "verbose", 0, 0, 'v' },
605 i
= mdwopt(argc
, argv
, "k:f:o:abqt:uv", opt
, 0, 0, 0);
608 case 'a': ef
= "pem"; break;
609 case 'b': v
.f
|= F_BUFFER
; break;
610 case 'k': kn
= optarg
; break;
611 case 'f': ef
= optarg
; break;
612 case 'o': of
= optarg
; break;
613 case 'u': v
.f
|= F_UTC
; break;
615 if (strcmp(optarg
, "always") == 0) t_fresh
= 0;
616 else if ((t_fresh
= get_date(optarg
, 0)) < 0)
617 die(EXIT_FAILURE
, "bad freshness time");
619 case 'q': if (v
.verb
> 0) v
.verb
--; break;
620 case 'v': if (v
.verb
< 10) v
.verb
++; break;
621 default: v
.f
|= F_BOGUS
; break;
624 if (argc
- optind
> 2 || (v
.f
& F_BOGUS
))
625 die(EXIT_FAILURE
, "Usage: verify [-OPTIONS] [FILE [MESSAGE]]");
627 if ((eo
= getenc(ef
)) == 0)
628 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
632 else if (strcmp(argv
[optind
], "-") == 0) {
635 } else if ((fp
= fopen(argv
[optind
], eo
->rmode
)) == 0) {
636 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
637 argv
[optind
], strerror(errno
));
641 if (key_open(&kf
, keyring
, KOPEN_READ
, key_moan
, 0))
642 die(EXIT_FAILURE
, "can't open keyring `%s'", keyring
);
643 if (kn
&& (kk
= key_bytag(&kf
, kn
)) == 0)
644 die(EXIT_FAILURE
, "key `%s' not found", kn
);
646 e
= initdec(eo
, fp
, vrfbdry
, &v
);
648 /* --- Read the header chunk --- */
650 v
.what
= "malformed header";
651 sig_readheader(e
, &s
, vrfchoke
, &v
);
653 if (((s
.f
^ v
.f
) & v
.m
) != 0) {
654 if (v
.verb
) printf("FAIL boundary string inconsistent with contents\n");
659 if ((k
= key_byid(&kf
, s
.keyid
)) == 0) {
660 if (v
.verb
) printf("FAIL key id %08lx not found\n",
661 (unsigned long)s
.keyid
);
664 if (kk
&& k
->id
!= kk
->id
) {
666 dstr_reset(&d
); key_fulltag(k
, &d
);
667 dstr_reset(&dd
); key_fulltag(kk
, &dd
);
668 printf("FAIL signing key is %s; expected key %s\n", d
.buf
, dd
.buf
);
673 s
.s
= getsig(k
, "ccsig", 0);
674 dstr_reset(&d
); key_fulltag(k
, &d
);
675 if (v
.verb
&& (err
= s
.s
->ops
->check(s
.s
)) != 0)
676 printf("WARN verification key %s fails check: %s\n", d
.buf
, err
);
678 dstr_reset(&dd
); keyhash(k
, s
.s
, &dd
);
679 if (dd
.len
!= s
.kh
.len
|| memcmp(dd
.buf
, s
.kh
.buf
, dd
.len
) != 0) {
680 if (v
.verb
) printf("FAIL key hash mismatch\n");
684 /* --- Now a merry dance --- */
689 dfn
= argv
[optind
++];
690 mcsetup_read(&mc_in
, v
.f
, &e
, dfn
);
692 if (!of
&& (v
.f
& F_DETACH
)) {
695 } else if (!of
|| strcmp(of
, "-") == 0) {
699 if (of
&& !(v
.f
& F_BUFFER
)) {
700 if ((ofp
= fopen(of
, (v
.f
& F_BINARY
) ?
"wb" : "w")) == 0) {
701 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
702 of
, strerror(errno
));
705 } else if ((rfp
= tmpfile()) == 0)
706 die(EXIT_FAILURE
, "couldn't create temporary file: %s", strerror(errno
));
708 /* --- Read the message and verify the signature --- */
712 n
= mc_in
.read(&mc_in
, bb
);
714 dohash(s
.s
->h
, bb
, n
);
715 if (rfp
) fwrite(bb
, 1, n
, rfp
);
717 mc_endread(&mc_in
, eo
, &e
);
718 if (!(v
.f
& F_DETACH
))
720 if (rfp
&& (ferror(rfp
) || fflush(rfp
))) {
721 if (v
.verb
) printf("FAIL error writing message: %s\n", strerror(errno
));
725 /* --- Check the signature --- */
727 if (s
.s
->ops
->doit(s
.s
, &s
.sig
)) {
728 if (v
.verb
) printf("FAIL signature verification failed\n");
731 if (t_fresh
&& s
.t
< t_fresh
) {
732 if (v
.verb
) printf("FAIL signature is stale\n");
736 if (v
.verb
) printf("FAIL signature timestamp in the future\n");
740 tm
= (v
.f
& F_UTC
) ?
gmtime(&s
.t
) : localtime(&s
.t
);
741 strftime(bb
, sizeof(bb
), "%Y-%m-%d %H:%M:%S %Z", tm
);
742 printf("INFO good-signature %s\n", d
.buf
);
743 printf("INFO date %s\n", bb
);
746 /* --- Unbuffer buffered input --- */
748 if (v
.f
& F_BUFFER
) {
749 if (!ofp
&& (ofp
= fopen(of
, "wb")) == 0) {
750 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
751 of
, strerror(errno
));
754 if (v
.verb
&& ofp
== stdout
) printf("DATA\n");
756 n
= fread(bb
, 1, sizeof(bb
), rfp
);
758 if (fwrite(bb
, 1, n
, ofp
) < n
)
759 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
761 if (ferror(rfp
) || fclose(rfp
))
762 die(EXIT_FAILURE
, "error unbuffering output: %s", strerror(errno
));
764 if (ofp
&& (fflush(ofp
) || ferror(ofp
) || fclose(ofp
)))
765 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
767 /* --- Tidy up --- */
770 if (v
.verb
&& ofp
!= stdout
)
771 printf("OK verified successfully\n");
780 /*----- Reformatting ------------------------------------------------------*/
782 static int format(int argc
, char *argv
[])
784 const char *ief
= "binary", *oef
= "binary";
785 const char *dfn
= 0, *of
= 0, *mf
= 0;
787 FILE *fp
, *ofp
= 0, *mfp
= 0;
790 msgcanon mc_in
= MC_INIT
, mc_out
= MC_INIT
;
792 vrfctx v
= { 0, 0, 1 };
793 unsigned f
= 0, fm
= ~F_SIGMASK
, sf
;
794 const encops
*ieo
, *oeo
;
798 static const struct option opt
[] = {
799 { "armour-in", 0, 0, 'a' },
800 { "armor-in", 0, 0, 'a' },
801 { "armour-out", 0, 0, 'A' },
802 { "armor-out", 0, 0, 'A' },
803 { "detach", 0, 0, 'D' },
804 { "embed", 0, 0, 'E' },
805 { "format-in", OPTF_ARGREQ
, 0, 'f' },
806 { "format-out", OPTF_ARGREQ
, 0, 'F' },
807 { "message", OPTF_ARGREQ
, 0, 'm' },
808 { "output", OPTF_ARGREQ
, 0, 'o' },
811 i
= mdwopt(argc
, argv
, "f:F:m:o:aADE", opt
, 0, 0, 0);
814 case 'a': ief
= "pem"; break;
815 case 'A': oef
= "pem"; break;
816 case 'f': ief
= optarg
; break;
817 case 'F': oef
= optarg
; break;
818 case 'D': f
|= F_DETACH
; fm
|= F_DETACH
; break;
819 case 'E': f
&= ~F_DETACH
; fm
|= F_DETACH
; break;
820 case 'm': mf
= optarg
; break;
821 case 'o': of
= optarg
; break;
822 default: f
|= F_BOGUS
; break;
826 if (argc
- optind
> 2 || (f
& F_BOGUS
))
827 die(EXIT_FAILURE
, "Usage: format [-OPTIONS] [FILE [MESSAGE]]");
829 if ((ieo
= getenc(ief
)) == 0)
830 die(EXIT_FAILURE
, "encoding `%s' not found", ief
);
831 if ((oeo
= getenc(oef
)) == 0)
832 die(EXIT_FAILURE
, "encoding `%s' not found", oef
);
836 else if (strcmp(argv
[optind
], "-") == 0) {
839 } else if ((fp
= fopen(argv
[optind
], ieo
->rmode
)) == 0) {
840 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
841 argv
[optind
], strerror(errno
));
846 dfn
= argv
[optind
++];
848 ie
= initdec(ieo
, fp
, vrfbdry
, &v
);
850 /* --- Read the header chunk --- */
852 sig_readheader(ie
, &s
, diechoke
, "malformed header");
854 if (((s
.f
^ v
.f
) & v
.m
) != 0)
855 moan("boundary string inconsistent with contents (ignoring)");
857 mcsetup_read(&mc_in
, s
.f
, &ie
, dfn
);
859 /* --- Prepare the output stuff --- */
861 if (!of
&& !mf
) of
= "-";
863 f
= (f
& fm
) | (s
.f
& ~fm
);
870 mcsetup_writenull(&mc_out
);
872 if (strcmp(of
, "-") == 0)
874 else if ((ofp
= fopen(of
, oeo
->wmode
)) == 0) {
875 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
876 of
, strerror(errno
));
878 oe
= initenc(oeo
, ofp
,
879 (f
& F_DETACH
) ?
"CATSIGN SIGNATURE" :
880 (f
& F_BINARY
) ?
"CATSIGN MESSAGE" :
881 "CATSIGN MESSAGE HEADER");
882 sig_writeheader(oe
, &s
);
883 mcsetup_write(&mc_out
, f
, &oe
);
887 if (strcmp(mf
, "-") == 0)
889 else if ((mfp
= fopen(mf
, (f
& F_BINARY
) ?
"wb" : "w")) == 0) {
890 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
891 mf
, strerror(errno
));
895 /* --- Wade through the message body --- */
898 n
= mc_in
.read(&mc_in
, bb
);
900 mc_out
.write(&mc_out
, bb
, n
);
901 if (mfp
) fwrite(bb
, 1, n
, mfp
);
903 mc_endread(&mc_in
, ieo
, &ie
);
904 if (of
) mc_endwrite(&mc_out
, oeo
, &oe
);
906 /* --- Write the signature --- */
908 if (!(sf
& F_DETACH
))
911 sig_writesig(oe
, &s
);
912 oe
->ops
->encdone(oe
);
915 /* --- All done --- */
917 ie
->ops
->decdone(ie
);
918 if (ferror(fp
) || fclose(fp
))
919 die(EXIT_FAILURE
, "error reading input signature: %s", strerror(errno
));
920 if (ofp
&& (fflush(ofp
) || ferror(ofp
) || fclose(ofp
)))
921 die(EXIT_FAILURE
, "error writing output signature: %s", strerror(errno
));
922 if (mfp
&& (fflush(mfp
) || ferror(mfp
) || fclose(mfp
)))
923 die(EXIT_FAILURE
, "error writing output message: %s", strerror(errno
));
930 static void infochoke(const char *m
, void *p
)
933 printf("BAD %s: %s\n", v
->what
, m
);
937 static void infokeyreport(const char *file
, int line
,
938 const char *err
, void *p
)
941 static int info(int argc
, char *argv
[])
943 const char *ef
= "binary";
944 vrfctx v
= { 0, 0, 1 };
957 static const struct option opt
[] = {
958 { "armour", 0, 0, 'a' },
959 { "armor", 0, 0, 'a' },
960 { "format", OPTF_ARGREQ
, 0, 'f' },
961 { "gmt", 0, 0, 'u' },
962 { "utc", 0, 0, 'u' },
965 i
= mdwopt(argc
, argv
, "f:au", opt
, 0, 0, 0);
968 case 'a': ef
= "pem"; break;
969 case 'f': ef
= optarg
; break;
970 case 'u': v
.f
|= F_UTC
; break;
971 default: v
.f
|= F_BOGUS
; break;
974 if (argc
- optind
> 1 || (v
.f
& F_BOGUS
))
975 die(EXIT_FAILURE
, "Usage: info [-OPTIONS] [FILE]");
977 if ((eo
= getenc(ef
)) == 0)
978 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
982 else if (strcmp(argv
[optind
], "-") == 0) {
985 } else if ((fp
= fopen(argv
[optind
], eo
->rmode
)) == 0) {
986 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
987 argv
[optind
], strerror(errno
));
991 if (key_open(&kf
, keyring
, KOPEN_READ
, infokeyreport
, 0)) {
992 printf("NOTE can't open keyring `%s'\n", keyring
);
995 e
= initdec(eo
, fp
, vrfbdry
, &v
);
997 v
.what
= "malformed header";
998 sig_readheader(e
, &s
, infochoke
, &v
);
1000 printf("INFO flags %sdetach %sbinary\n",
1001 (s
.f
& F_DETACH
) ?
"" : "!",
1002 (s
.f
& F_BINARY
) ?
"" : "!");
1004 if (((s
.f
^ v
.f
) & v
.m
) != 0) {
1005 printf("WARN boundary string inconsistent with contents\n");
1006 printf("INFO expected-flags");
1007 if (v
.m
& F_DETACH
) printf(" %sdetach", (v
.f
& F_DETACH
) ?
"" : "!");
1008 if (v
.m
& F_BINARY
) printf(" %sbinary", (v
.f
& F_BINARY
) ?
"" : "!");
1013 tm
= (v
.f
& F_UTC
) ?
gmtime(&s
.t
) : localtime(&s
.t
);
1014 strftime(bb
, sizeof(bb
), "%Y-%m-%d %H:%M:%S %Z", tm
);
1015 printf("INFO date %s\n", bb
);
1017 if (keyring
&& (k
= key_byid(&kf
, s
.keyid
)) != 0) {
1018 dstr_reset(&d
); key_fulltag(k
, &d
);
1019 printf("INFO key %s\n", d
.buf
);
1021 printf("INFO unknown-key %08lx\n", (unsigned long)s
.keyid
);
1023 if (keyring
) key_close(&kf
);
1029 /*----- Main code ---------------------------------------------------------*/
1033 listtab[i].name, listtab[i].name) \
1034 LI("Signature schemes", sig, \
1035 sigtab[i].name, sigtab[i].name) \
1036 LI("Encodings", enc, \
1037 enctab[i].name, enctab[i].name) \
1038 LI("Hash functions", hash, \
1039 ghashtab[i], ghashtab[i]->name)
1041 MAKELISTTAB(listtab
, LISTS
)
1043 int cmd_show(int argc
, char *argv
[])
1045 return (displaylists(listtab
, argv
+ 1));
1048 static int cmd_help(int, char **);
1050 static cmd cmdtab
[] = {
1051 { "help", cmd_help
, "help [COMMAND...]" },
1052 { "show", cmd_show
, "show [ITEM...]" },
1056 "sign [-adt] [-k TAG] [-f FORMAT] [-o OUTPUT] [FILE]", "\
1059 -a, --armour Same as `-f pem'.\n\
1060 -b, --binary Treat the input message as binary data.\n\
1061 -d, --detach Produce a detached signature.\n\
1062 -f, --format=FORMAT Encode as FORMAT.\n\
1063 -k, --key=TAG Use public encryption key named by TAG.\n\
1064 -o, --output=FILE Write output to FILE.\n\
1065 -t, --text Canonify input message as a text file.\n\
1068 "verify [-abquv] [-f FORMAT] [-k TAG] [-o OUTPUT]\n\t\
1069 [FILE [MESSAGE]]", "\
1072 -a, --armour Same as `-f pem'.\n\
1073 -b, --buffer Buffer message until signature is verified.\n\
1074 -f, --format=FORMAT Decode as FORMAT.\n\
1075 -k, --key=TAG Require that the message be signed by key TAG.\n\
1076 -o, --output=FILE Write message to FILE.\n\
1077 -q, --quiet Produce fewer messages.\n\
1078 -t, --freshtime=TIME Only accept signatures made after this time.\n\
1079 -u, --utc Show dates in UTC rather than local time.\n\
1080 -v, --verbose Produce more verbose messages.\n\
1083 "info [-au] [-f FORMAT] [FILE]", "\
1086 -a, --armour Same as `-f pem'.\n\
1087 -f, --format=FORMAT Decode as FORMAT.\n\
1088 -u, --utc Show dates in UTC rather than local time.\n\
1091 "format [-auADE] [-f FORMAT] [-F format] [-m FILE] [-o FILE]\n\t\
1092 [FILE [MESSAGE]]", "\
1095 -a, --armour-in Same as `-f pem'.\n\
1096 -A, --armour-out Same as `-F pem'.\n\
1097 -D, --detach Create detached signature.\n\
1098 -E, --embed Create signature with embedded message.\n\
1099 -f, --format-in=FORMAT Decode input as FORMAT.\n\
1100 -F, --format-out=FORMAT Encode output as FORMAT.\n\
1101 -m, --message=FILE Write message to FILE.\n\
1102 -o, --output=FILE Write new signature to FILE.\n\
1105 }; /* " Emacs seems confused. */
1107 static int cmd_help(int argc
, char **argv
)
1109 sc_help(cmdtab
, stdout
, argv
+ 1);
1113 void version(FILE *fp
)
1115 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1118 static void usage(FILE *fp
)
1120 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1123 void help_global(FILE *fp
)
1127 Sign and verify data.\n\
1129 Global command-line options:\n\
1131 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1132 -v, --version Show program version number.\n\
1133 -u, --usage Show a terse usage message.\n\
1135 -k, --keyring=FILE Read keys from FILE.\n",
1141 * Arguments: @int argc@ = number of command line arguments
1142 * @char *argv[]@ = vector of command line arguments
1144 * Returns: Zero if successful, nonzero otherwise.
1146 * Use: Encrypts or decrypts files.
1149 int main(int argc
, char *argv
[])
1155 /* --- Initialize the library --- */
1159 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1160 rand_seed(RAND_GLOBAL
, 160);
1161 /* trace_on(stderr, 1); */
1163 /* --- Parse options --- */
1166 static struct option opts
[] = {
1167 { "help", 0, 0, 'h' },
1168 { "version", 0, 0, 'v' },
1169 { "usage", 0, 0, 'u' },
1170 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1173 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1178 sc_help(cmdtab
, stdout
, argv
+ optind
);
1200 if (f
& f_bogus
|| argc
< 1) {
1205 /* --- Dispatch to the correct subcommand handler --- */
1207 return (findcmd(cmdtab
, argv
[0])->cmd(argc
, argv
));
1212 /*----- That's all, folks -------------------------------------------------*/