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 ------------------------------------------------------*/
32 #define _FILE_OFFSET_BITS 64
43 #include <mLib/base64.h>
44 #include <mLib/dstr.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
61 /*----- Static variables --------------------------------------------------*/
63 static const char *keyring
= "keyring";
65 /*----- Data formats ------------------------------------------------------*
67 * Our crypto stuff is split into three parts: a header describing the key,
68 * the message itself, and the signature. In a detached signature, the
69 * message is separate, and the header and signature are combined into one
70 * encoded chunk. In an attached signature, the message is included. There
71 * are two forms of message: binary and text. Binary messages are divided
72 * into chunks, each preceded by a 2-octet length, and terminated by a
73 * zero-length chunk. Text messages are byte-stuffed, as for RFC821, and
74 * trailing whitespace before a newline is ignored.
77 typedef struct sigmsg
{
78 unsigned f
; /* Flags */
82 #define F_HASHMASK (F_BINARY)
83 uint32 keyid
; /* Key identifier */
84 time_t t
; /* When the signature was made */
85 dstr kh
; /* Key fingerprint (sanity check) */
86 dstr sig
; /* Signature */
87 sig
*s
; /* Signature algorithm */
96 #define F_NOCHECK 1024u
97 #define F_PROGRESS 2048u
99 /*----- Chunk I/O ---------------------------------------------------------*/
101 static void chunk_write(enc
*e
, const void *p
, size_t n
)
107 if (e
->ops
->write(e
, b
, 2) || e
->ops
->write(e
, p
, n
))
108 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
111 static size_t chunk_read(enc
*e
, void *p
)
116 if (e
->ops
->read(e
, b
, 2) != 2)
119 if (n
&& e
->ops
->read(e
, p
, n
) != n
)
124 if (!errno
) die(EXIT_FAILURE
, "unexpected end-of-file on input");
125 else die(EXIT_FAILURE
, "error reading input: %s", strerror(errno
));
129 /*----- Message canonification --------------------------------------------*/
131 #define MSGBUFSZ 65536
132 #define MSGBUFTHRESH 32768
134 typedef struct msgcanon
{
139 size_t (*read
)(struct msgcanon
*, void *);
140 void (*write
)(struct msgcanon
*, const void *, size_t);
141 void (*close
)(struct msgcanon
*);
144 #define MC_INIT { 0 }
146 static size_t textread(msgcanon
*m
, void *bp
)
148 size_t n
= 0, nsp
= 0;
153 if (f
& F_EOF
) return (0);
155 if ((ch
= getc(m
->fp
)) == EOF
) goto eof
;
156 if (!(f
& (F_DETACH
| F_MIDLINE
)) && ch
== '.') {
158 if (ch
== '\n') goto eof
;
162 if (n
>= MSGBUFTHRESH
) goto full
;
165 } else if (isspace(ch
)) {
167 if (n
>= MSGBUFSZ
) goto full
;
172 if (n
>= MSGBUFTHRESH
) goto full
;
183 if (m
->f
& F_PROGRESS
) fprogress_update(&m
->ff
, n
);
187 static void textwrite(msgcanon
*m
, const void *bp
, size_t n
)
189 const char *p
= bp
, *l
= p
+ n
;
193 if (!(f
& (F_MIDLINE
| F_DETACH
)) &&
194 (*p
== '.' || *p
== '-'))
196 if (*p
== '\n') f
&= ~F_MIDLINE
;
204 static size_t binreadembed(msgcanon
*m
, void *bp
)
206 size_t n
= chunk_read(m
->e
, bp
);
207 if (m
->f
& F_PROGRESS
) fprogress_update(&m
->ff
, n
);
211 static size_t binreaddetach(msgcanon
*m
, void *bp
)
213 size_t n
= fread(bp
, 1, MSGBUFSZ
- 1, m
->fp
);
214 if (m
->f
& F_PROGRESS
) fprogress_update(&m
->ff
, n
);
218 static void binwriteembed(msgcanon
*m
, const void *bp
, size_t n
)
219 { chunk_write(m
->e
, bp
, n
); }
221 static void nullwrite(msgcanon
*m
, const void *bp
, size_t n
) { ; }
223 static void mcsetup_readfile(msgcanon
*m
, unsigned f
, const char *fn
)
225 m
->f
= F_DETACH
| (f
& (F_BINARY
| F_PROGRESS
));
226 if (!fn
|| strcmp(fn
, "-") == 0) {
229 } else if ((m
->fp
= fopen(fn
, (f
& F_BINARY
) ?
"rb" : "r")) == 0)
230 die(EXIT_FAILURE
, "couldn't open file `%s': %s", fn
, strerror(errno
));
231 if (m
->f
& F_PROGRESS
) {
232 if (fprogress_init(&m
->ff
, fn
, m
->fp
)) {
233 die(EXIT_FAILURE
, "failed to initialize progress indicator: %s",
237 m
->read
= (f
& F_BINARY
) ? binreaddetach
: textread
;
240 static void mcsetup_writenull(msgcanon
*m
) { m
->write
= nullwrite
; }
242 static void mcsetup_read(msgcanon
*m
, unsigned f
, enc
**ee
,
243 const char *fn
, const char *dfn
)
247 m
->f
= f
| F_NOCLOSE
;
249 if (dfn
&& !(f
& F_DETACH
)) die(EXIT_FAILURE
, "signature is not detached");
251 if (!(f
& F_DETACH
)) {
255 m
->read
= binreadembed
;
256 if (m
->f
& F_PROGRESS
) {
257 if (fprogress_init(&m
->ff
, fn
, m
->fp
)) {
258 die(EXIT_FAILURE
, "failed to initialize progress indicator: %s",
263 m
->read
= binreaddetach
;
264 if (!dfn
|| strcmp(dfn
, "-") == 0)
266 else if ((m
->fp
= fopen(dfn
, "rb")) == 0)
267 die(EXIT_FAILURE
, "can't open `%s': %s", dfn
, strerror(errno
));
270 if (m
->f
& F_PROGRESS
) {
271 if (fprogress_init(&m
->ff
, dfn
, m
->fp
)) {
272 die(EXIT_FAILURE
, "failed to initialize progress indicator: %s",
279 if (!(f
& F_DETACH
)) {
280 if (e
->ops
->decdone(e
) || ferror(e
->fp
))
281 die(EXIT_FAILURE
, "error at end of signature header");
285 if (m
->f
& F_PROGRESS
) {
286 if (fprogress_init(&m
->ff
, fn
, m
->fp
)) {
287 die(EXIT_FAILURE
, "failed to initialize progress indicator: %s",
292 if (!dfn
|| strcmp(dfn
, "-") == 0)
294 else if ((m
->fp
= fopen(dfn
, "r")) == 0)
295 die(EXIT_FAILURE
, "can't read file `%s': %s", dfn
, strerror(errno
));
298 if (m
->f
& F_PROGRESS
) {
299 if (fprogress_init(&m
->ff
, dfn
, m
->fp
)) {
300 die(EXIT_FAILURE
, "failed to initialize progress indicator: %s",
308 static void mcsetup_write(msgcanon
*m
, unsigned f
, enc
**ee
)
312 m
->f
= f
| F_NOCLOSE
;
315 m
->write
= nullwrite
;
316 else if (f
& F_BINARY
) {
320 m
->write
= binwriteembed
;
322 if (e
->ops
->encdone(e
) || ferror(e
->fp
))
323 die(EXIT_FAILURE
, "error at end of signature header");
327 m
->write
= textwrite
;
331 static void mc_endread(msgcanon
*m
, const encops
*eops
, enc
**ee
)
333 if (!(m
->f
& F_DETACH
)) {
337 *ee
= initdec(eops
, m
->fp
, checkbdry
, "CATSIGN SIGNATURE");
339 if (m
->fp
&& !(m
->f
& F_NOCLOSE
)) {
340 if (ferror(m
->fp
) || fclose(m
->fp
))
341 die(EXIT_FAILURE
, "error closing message file: %s", strerror(errno
));
343 if (m
->f
& F_PROGRESS
) fprogress_done(&m
->ff
);
346 static void mc_endwrite(msgcanon
*m
, const encops
*eops
, enc
**ee
)
348 if (!(m
->f
& F_BINARY
)) {
349 if (m
->f
& F_MIDLINE
) putc('\n', m
->fp
);
350 if (!(m
->f
& F_DETACH
)) {
351 putc('.', m
->fp
); putc('\n', m
->fp
);
352 *ee
= initenc(eops
, m
->fp
, "CATSIGN SIGNATURE");
354 } else if (!(m
->f
& F_DETACH
)) {
355 chunk_write(m
->e
, 0, 0);
358 if (m
->fp
&& !(m
->f
& F_NOCLOSE
)) {
359 if (fflush(m
->fp
) || ferror(m
->fp
) || fclose(m
->fp
))
360 die(EXIT_FAILURE
, "error closing message file: %s", strerror(errno
));
364 /*----- Signature reading and writing -------------------------------------*/
366 static void sig_init(sigmsg
*s
, unsigned f
, uint32 keyid
)
368 s
->f
= f
& F_SIGMASK
;
373 dstr_create(&s
->sig
);
376 static void sig_destroy(sigmsg
*s
)
378 dstr_destroy(&s
->kh
);
379 dstr_destroy(&s
->sig
);
380 if (s
->s
) freesig(s
->s
);
383 static void sigtobuffer(sigmsg
*s
, buf
*b
, int hashp
)
388 if (hashp
) buf_putu16(b
, s
->f
& F_HASHMASK
);
389 else buf_putu16(b
, s
->f
& F_SIGMASK
);
390 buf_putu32(b
, s
->keyid
);
391 buf_putu32(b
, HI64(t
));
392 buf_putu32(b
, LO64(t
));
393 buf_putdstr16(b
, &s
->kh
);
397 static void dohash(ghash
*h
, const void *p
, size_t n
)
399 /* trace_block(1, "hashing", p, n); */
403 static void sig_hash(sigmsg
*s
)
408 buf_init(&b
, bb
, sizeof(bb
));
409 sigtobuffer(s
, &b
, 1);
410 dohash(s
->s
->h
, BBASE(&b
), BLEN(&b
));
413 static void keyhash(key
*k
, sig
*s
, dstr
*d
)
421 key_fingerprint(k
, h
, &kf
);
422 dstr_ensure(d
, GH_CLASS(h
)->hashsz
);
423 GH_DONE(h
, d
->buf
+ d
->len
);
424 d
->len
+= GH_CLASS(h
)->hashsz
;
428 static void sig_writeheader(enc
*e
, sigmsg
*s
)
433 buf_init(&b
, bb
, sizeof(bb
));
434 sigtobuffer(s
, &b
, 0);
435 chunk_write(e
, BBASE(&b
), BLEN(&b
));
438 static void sig_writesig(enc
*e
, sigmsg
*s
)
439 { chunk_write(e
, s
->sig
.buf
, s
->sig
.len
); }
441 static void diechoke(const char *m
, void *p
)
442 { die(EXIT_FAILURE
, "%s%s%s", p
, p ?
": " : "", m
); }
444 static void sig_readheader(enc
*e
, sigmsg
*s
,
445 void (*choke
)(const char *, void *), void *p
)
454 n
= chunk_read(e
, bb
);
456 if (buf_getu16(&b
, &f
)) choke("missing flags", p
);
457 if (buf_getu32(&b
, &x
)) choke("missing keyid", p
);
459 if (buf_getu32(&b
, &x
) || buf_getu32(&b
, &y
))
460 choke("missing datestamp", p
);
461 SET64(t
, x
, y
); s
->t
= GET64(time_t, t
);
462 if (buf_getdstr16(&b
, &s
->kh
))
463 choke("missing key hash", p
);
465 choke("junk at end", p
);
468 static void sig_readsig(enc
*e
, sigmsg
*s
)
473 n
= chunk_read(e
, bb
);
474 dstr_putm(&s
->sig
, bb
, n
);
477 /*----- Signing -----------------------------------------------------------*/
479 static int sign(int argc
, char *argv
[])
481 const char *ef
= "binary", *fn
= 0, *of
= 0, *kn
= "ccsig", *err
;
492 msgcanon mc_in
= MC_INIT
, mc_out
= MC_INIT
;
496 static const struct option opt
[] = {
497 { "armour", 0, 0, 'a' },
498 { "armor", 0, 0, 'a' },
499 { "binary", 0, 0, 'b' },
500 { "detach", 0, 0, 'd' },
501 { "key", OPTF_ARGREQ
, 0, 'k' },
502 { "format", OPTF_ARGREQ
, 0, 'f' },
503 { "output", OPTF_ARGREQ
, 0, 'o' },
504 { "progress", 0, 0, 'p' },
505 { "text", 0, 0, 't' },
506 { "nocheck", 0, 0, 'C' },
509 i
= mdwopt(argc
, argv
, "k:f:o:abdptC", opt
, 0, 0, 0);
512 case 'k': kn
= optarg
; break;
513 case 'f': ef
= optarg
; break;
514 case 'o': of
= optarg
; break;
515 case 'a': ef
= "pem"; break;
516 case 't': f
&= ~F_BINARY
; break;
517 case 'b': f
|= F_BINARY
; break;
518 case 'd': f
|= F_DETACH
; break;
519 case 'C': f
|= F_NOCHECK
; break;
520 case 'p': f
|= F_PROGRESS
; break;
521 default: f
|= F_BOGUS
; break;
524 if (argc
- optind
> 1 || (f
& F_BOGUS
))
525 die(EXIT_FAILURE
, "Usage: sign [-OPTIONS] [FILE]");
527 if (key_open(&kf
, keyring
, KOPEN_READ
, key_moan
, 0))
528 die(EXIT_FAILURE
, "can't open keyring `%s'", keyring
);
529 if ((k
= key_bytag(&kf
, kn
)) == 0)
530 die(EXIT_FAILURE
, "key `%s' not found", kn
);
532 if ((eo
= getenc(ef
)) == 0)
533 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
535 fn
= (optind
>= argc
) ?
0 : argv
[optind
++];
537 if (!of
|| strcmp(of
, "-") == 0)
539 else if ((ofp
= fopen(of
, eo
->wmode
)) == 0) {
540 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
541 ofp
, strerror(errno
));
544 /* --- Start the work --- */
546 sig_init(&s
, f
, k
->id
);
549 s
.s
= getsig(k
, "ccsig", 1);
550 if (!(f
& F_NOCHECK
) && (err
= s
.s
->ops
->check(s
.s
)) != 0)
551 moan("key %s fails check: %s", d
.buf
, err
);
552 keyhash(k
, s
.s
, &s
.kh
);
554 (f
& F_DETACH
) ?
"CATSIGN SIGNATURE" :
555 (f
& F_BINARY
) ?
"CATSIGN MESSAGE" :
556 "CATSIGN MESSAGE HEADER");
557 sig_writeheader(e
, &s
);
559 /* --- Hash the message --- */
561 mcsetup_readfile(&mc_in
, f
, fn
);
562 mcsetup_write(&mc_out
, f
, &e
);
565 n
= mc_in
.read(&mc_in
, bb
);
567 dohash(s
.s
->h
, bb
, n
);
568 mc_out
.write(&mc_out
, bb
, n
);
570 mc_endread(&mc_in
, 0, 0);
571 mc_endwrite(&mc_out
, eo
, &e
);
573 /* --- Write the signature --- */
575 if (s
.s
->ops
->doit(s
.s
, &s
.sig
))
576 die(EXIT_FAILURE
, "signature failed");
579 if (fflush(ofp
) || ferror(ofp
) || fclose(ofp
))
580 die(EXIT_FAILURE
, "error writing signature: %s", strerror(errno
));
582 /* --- All done --- */
591 /*----- Verifying ---------------------------------------------------------*/
593 typedef struct vrfctx
{
599 static int vrfbdry(const char *b
, void *p
)
603 if (strcmp(b
, "CATSIGN MESSAGE") == 0) {
605 v
->m
|= F_BINARY
| F_DETACH
;
607 } else if (strcmp(b
, "CATSIGN MESSAGE HEADER") == 0) {
608 v
->m
|= F_BINARY
| F_DETACH
;
610 } else if (strcmp(b
, "CATSIGN SIGNATURE") == 0) {
618 static void vrfchoke(const char *m
, void *p
)
621 if (v
->verb
) printf("FAIL %s: %s\n", v
->what
, m
);
625 static int verify(int argc
, char *argv
[])
627 const char *ef
= "binary", *of
= 0, *fn
, *dfn
= 0, *kn
= 0, *err
;
628 vrfctx v
= { 0, 0, 1 };
632 FILE *fp
, *ofp
= 0, *rfp
= 0;
639 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
641 msgcanon mc_in
= MC_INIT
;
645 static const struct option opt
[] = {
646 { "armour", 0, 0, 'a' },
647 { "armor", 0, 0, 'a' },
648 { "buffer", 0, 0, 'b' },
649 { "key", OPTF_ARGREQ
, 0, 'k' },
650 { "format", OPTF_ARGREQ
, 0, 'f' },
651 { "output", OPTF_ARGREQ
, 0, 'o' },
652 { "progress", 0, 0, 'p' },
653 { "quiet", 0, 0, 'q' },
654 { "utc", 0, 0, 'u' },
655 { "fresh-time", 0, 0, 't' },
656 { "gmt", 0, 0, 'u' },
657 { "verbose", 0, 0, 'v' },
658 { "nocheck", 0, 0, 'C' },
661 i
= mdwopt(argc
, argv
, "k:f:o:abpqt:uvC", opt
, 0, 0, 0);
664 case 'a': ef
= "pem"; break;
665 case 'b': v
.f
|= F_BUFFER
; break;
666 case 'k': kn
= optarg
; break;
667 case 'f': ef
= optarg
; break;
668 case 'o': of
= optarg
; break;
669 case 'u': v
.f
|= F_UTC
; break;
670 case 'C': v
.f
|= F_NOCHECK
; break;
672 if (strcmp(optarg
, "always") == 0) t_fresh
= 0;
673 else if ((t_fresh
= get_date(optarg
, 0)) < 0)
674 die(EXIT_FAILURE
, "bad freshness time");
676 case 'q': if (v
.verb
> 0) v
.verb
--; break;
677 case 'v': if (v
.verb
< 10) v
.verb
++; break;
678 case 'p': v
.f
|= F_PROGRESS
; break;
679 default: v
.f
|= F_BOGUS
; break;
682 if (argc
- optind
> 2 || (v
.f
& F_BOGUS
))
683 die(EXIT_FAILURE
, "Usage: verify [-OPTIONS] [FILE [MESSAGE]]");
685 if ((eo
= getenc(ef
)) == 0)
686 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
688 fn
= optind
< argc ? argv
[optind
++] : "-";
689 if (strcmp(fn
, "-") == 0)
691 else if ((fp
= fopen(fn
, eo
->rmode
)) == 0) {
692 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
693 fn
, strerror(errno
));
696 if (key_open(&kf
, keyring
, KOPEN_READ
, key_moan
, 0))
697 die(EXIT_FAILURE
, "can't open keyring `%s'", keyring
);
698 if (kn
&& (kk
= key_bytag(&kf
, kn
)) == 0)
699 die(EXIT_FAILURE
, "key `%s' not found", kn
);
701 e
= initdec(eo
, fp
, vrfbdry
, &v
);
703 /* --- Read the header chunk --- */
705 v
.what
= "malformed header";
706 sig_readheader(e
, &s
, vrfchoke
, &v
);
708 if (((s
.f
^ v
.f
) & v
.m
) != 0) {
709 if (v
.verb
) printf("FAIL boundary string inconsistent with contents\n");
714 if ((k
= key_byid(&kf
, s
.keyid
)) == 0) {
715 if (v
.verb
) printf("FAIL key id %08lx not found\n",
716 (unsigned long)s
.keyid
);
719 if (kk
&& k
->id
!= kk
->id
) {
721 dstr_reset(&d
); key_fulltag(k
, &d
);
722 dstr_reset(&dd
); key_fulltag(kk
, &dd
);
723 printf("FAIL signing key is %s; expected key %s\n", d
.buf
, dd
.buf
);
728 s
.s
= getsig(k
, "ccsig", 0);
729 dstr_reset(&d
); key_fulltag(k
, &d
);
730 if (!(v
.f
& F_NOCHECK
) && v
.verb
&& (err
= s
.s
->ops
->check(s
.s
)) != 0)
731 printf("WARN verification key %s fails check: %s\n", d
.buf
, err
);
733 dstr_reset(&dd
); keyhash(k
, s
.s
, &dd
);
734 if (dd
.len
!= s
.kh
.len
|| memcmp(dd
.buf
, s
.kh
.buf
, dd
.len
) != 0) {
735 if (v
.verb
) printf("FAIL key hash mismatch\n");
739 /* --- Now a merry dance --- */
744 dfn
= argv
[optind
++];
745 mcsetup_read(&mc_in
, v
.f
, &e
, fn
, dfn
);
747 if (!of
&& (v
.f
& F_DETACH
)) {
750 } else if (!of
|| strcmp(of
, "-") == 0) {
754 if (of
&& !(v
.f
& F_BUFFER
)) {
755 if ((ofp
= fopen(of
, (v
.f
& F_BINARY
) ?
"wb" : "w")) == 0) {
756 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
757 of
, strerror(errno
));
760 } else if ((rfp
= tmpfile()) == 0)
761 die(EXIT_FAILURE
, "couldn't create temporary file: %s", strerror(errno
));
763 /* --- Read the message and verify the signature --- */
767 n
= mc_in
.read(&mc_in
, bb
);
769 dohash(s
.s
->h
, bb
, n
);
770 if (rfp
) fwrite(bb
, 1, n
, rfp
);
772 mc_endread(&mc_in
, eo
, &e
);
773 if (!(v
.f
& F_DETACH
))
775 if (rfp
&& (ferror(rfp
) || fflush(rfp
))) {
776 if (v
.verb
) printf("FAIL error writing message: %s\n", strerror(errno
));
780 /* --- Check the signature --- */
782 if (s
.s
->ops
->doit(s
.s
, &s
.sig
)) {
783 if (v
.verb
) printf("FAIL signature verification failed\n");
786 if (t_fresh
&& s
.t
< t_fresh
) {
787 if (v
.verb
) printf("FAIL signature is stale\n");
791 if (v
.verb
) printf("FAIL signature timestamp in the future\n");
795 tm
= (v
.f
& F_UTC
) ?
gmtime(&s
.t
) : localtime(&s
.t
);
796 strftime(bb
, sizeof(bb
), "%Y-%m-%d %H:%M:%S %Z", tm
);
797 printf("INFO good-signature %s\n", d
.buf
);
798 printf("INFO date %s\n", bb
);
801 /* --- Unbuffer buffered input --- */
803 if (v
.f
& F_BUFFER
) {
804 if (!ofp
&& (ofp
= fopen(of
, "wb")) == 0) {
805 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
806 of
, strerror(errno
));
809 if (v
.f
& F_PROGRESS
) fprogress_init(&ff
, "copying buffer", rfp
);
810 if (v
.verb
&& ofp
== stdout
) printf("DATA\n");
812 n
= fread(bb
, 1, sizeof(bb
), rfp
);
814 if (v
.f
& F_PROGRESS
) fprogress_update(&ff
, n
);
815 if (fwrite(bb
, 1, n
, ofp
) < n
) {
816 if (v
.f
& F_PROGRESS
) fprogress_done(&ff
);
817 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
820 if (v
.f
& F_PROGRESS
) fprogress_done(&ff
);
821 if (ferror(rfp
) || fclose(rfp
))
822 die(EXIT_FAILURE
, "error unbuffering output: %s", strerror(errno
));
824 if (ofp
&& (fflush(ofp
) || ferror(ofp
) || fclose(ofp
)))
825 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
827 /* --- Tidy up --- */
830 if (v
.verb
&& ofp
!= stdout
)
831 printf("OK verified successfully\n");
840 /*----- Reformatting ------------------------------------------------------*/
842 static int format(int argc
, char *argv
[])
844 const char *ief
= "binary", *oef
= "binary";
845 const char *fn
, *dfn
= 0, *of
= 0, *mf
= 0;
847 FILE *fp
, *ofp
= 0, *mfp
= 0;
850 msgcanon mc_in
= MC_INIT
, mc_out
= MC_INIT
;
852 vrfctx v
= { 0, 0, 1 };
853 unsigned f
= 0, fm
= ~F_SIGMASK
, sf
;
854 const encops
*ieo
, *oeo
;
858 static const struct option opt
[] = {
859 { "armour-in", 0, 0, 'a' },
860 { "armor-in", 0, 0, 'a' },
861 { "armour-out", 0, 0, 'A' },
862 { "armor-out", 0, 0, 'A' },
863 { "detach", 0, 0, 'D' },
864 { "embed", 0, 0, 'E' },
865 { "format-in", OPTF_ARGREQ
, 0, 'f' },
866 { "format-out", OPTF_ARGREQ
, 0, 'F' },
867 { "message", OPTF_ARGREQ
, 0, 'm' },
868 { "output", OPTF_ARGREQ
, 0, 'o' },
869 { "progress", 0, 0, 'p' },
872 i
= mdwopt(argc
, argv
, "f:F:m:o:apADE", opt
, 0, 0, 0);
875 case 'a': ief
= "pem"; break;
876 case 'A': oef
= "pem"; break;
877 case 'f': ief
= optarg
; break;
878 case 'F': oef
= optarg
; break;
879 case 'D': f
|= F_DETACH
; fm
|= F_DETACH
; break;
880 case 'E': f
&= ~F_DETACH
; fm
|= F_DETACH
; break;
881 case 'm': mf
= optarg
; break;
882 case 'o': of
= optarg
; break;
883 case 'p': f
|= F_PROGRESS
; break;
884 default: f
|= F_BOGUS
; break;
888 if (argc
- optind
> 2 || (f
& F_BOGUS
))
889 die(EXIT_FAILURE
, "Usage: format [-OPTIONS] [FILE [MESSAGE]]");
891 if ((ieo
= getenc(ief
)) == 0)
892 die(EXIT_FAILURE
, "encoding `%s' not found", ief
);
893 if ((oeo
= getenc(oef
)) == 0)
894 die(EXIT_FAILURE
, "encoding `%s' not found", oef
);
896 fn
= optind
< argc ? argv
[optind
++] : "-";
897 if (strcmp(fn
, "-") == 0)
899 else if ((fp
= fopen(fn
, ieo
->rmode
)) == 0) {
900 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
901 fn
, strerror(errno
));
905 dfn
= argv
[optind
++];
907 ie
= initdec(ieo
, fp
, vrfbdry
, &v
);
909 /* --- Read the header chunk --- */
911 sig_readheader(ie
, &s
, diechoke
, "malformed header");
913 if (((s
.f
^ v
.f
) & v
.m
) != 0)
914 moan("boundary string inconsistent with contents (ignoring)");
916 mcsetup_read(&mc_in
, s
.f
, &ie
, fn
, dfn
);
918 /* --- Prepare the output stuff --- */
920 if (!of
&& !mf
) of
= "-";
922 f
= (f
& fm
) | (s
.f
& ~fm
);
929 mcsetup_writenull(&mc_out
);
931 if (strcmp(of
, "-") == 0)
933 else if ((ofp
= fopen(of
, oeo
->wmode
)) == 0) {
934 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
935 of
, strerror(errno
));
937 oe
= initenc(oeo
, ofp
,
938 (f
& F_DETACH
) ?
"CATSIGN SIGNATURE" :
939 (f
& F_BINARY
) ?
"CATSIGN MESSAGE" :
940 "CATSIGN MESSAGE HEADER");
941 sig_writeheader(oe
, &s
);
942 mcsetup_write(&mc_out
, f
, &oe
);
946 if (strcmp(mf
, "-") == 0)
948 else if ((mfp
= fopen(mf
, (f
& F_BINARY
) ?
"wb" : "w")) == 0) {
949 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
950 mf
, strerror(errno
));
954 /* --- Wade through the message body --- */
957 n
= mc_in
.read(&mc_in
, bb
);
959 mc_out
.write(&mc_out
, bb
, n
);
960 if (mfp
) fwrite(bb
, 1, n
, mfp
);
962 mc_endread(&mc_in
, ieo
, &ie
);
963 if (of
) mc_endwrite(&mc_out
, oeo
, &oe
);
965 /* --- Write the signature --- */
967 if (!(sf
& F_DETACH
))
970 sig_writesig(oe
, &s
);
971 oe
->ops
->encdone(oe
);
974 /* --- All done --- */
976 ie
->ops
->decdone(ie
);
977 if (ferror(fp
) || fclose(fp
))
978 die(EXIT_FAILURE
, "error reading input signature: %s", strerror(errno
));
979 if (ofp
&& (fflush(ofp
) || ferror(ofp
) || fclose(ofp
)))
980 die(EXIT_FAILURE
, "error writing output signature: %s", strerror(errno
));
981 if (mfp
&& (fflush(mfp
) || ferror(mfp
) || fclose(mfp
)))
982 die(EXIT_FAILURE
, "error writing output message: %s", strerror(errno
));
989 static void infochoke(const char *m
, void *p
)
992 printf("BAD %s: %s\n", v
->what
, m
);
996 static void infokeyreport(const char *file
, int line
,
997 const char *err
, void *p
)
1000 static int info(int argc
, char *argv
[])
1002 const char *ef
= "binary";
1003 vrfctx v
= { 0, 0, 1 };
1016 static const struct option opt
[] = {
1017 { "armour", 0, 0, 'a' },
1018 { "armor", 0, 0, 'a' },
1019 { "format", OPTF_ARGREQ
, 0, 'f' },
1020 { "gmt", 0, 0, 'u' },
1021 { "utc", 0, 0, 'u' },
1024 i
= mdwopt(argc
, argv
, "f:au", opt
, 0, 0, 0);
1027 case 'a': ef
= "pem"; break;
1028 case 'f': ef
= optarg
; break;
1029 case 'u': v
.f
|= F_UTC
; break;
1030 default: v
.f
|= F_BOGUS
; break;
1033 if (argc
- optind
> 1 || (v
.f
& F_BOGUS
))
1034 die(EXIT_FAILURE
, "Usage: info [-OPTIONS] [FILE]");
1036 if ((eo
= getenc(ef
)) == 0)
1037 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
1041 else if (strcmp(argv
[optind
], "-") == 0) {
1044 } else if ((fp
= fopen(argv
[optind
], eo
->rmode
)) == 0) {
1045 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
1046 argv
[optind
], strerror(errno
));
1050 if (key_open(&kf
, keyring
, KOPEN_READ
, infokeyreport
, 0)) {
1051 printf("NOTE can't open keyring `%s'\n", keyring
);
1054 e
= initdec(eo
, fp
, vrfbdry
, &v
);
1056 v
.what
= "malformed header";
1057 sig_readheader(e
, &s
, infochoke
, &v
);
1059 printf("INFO flags %sdetach %sbinary\n",
1060 (s
.f
& F_DETACH
) ?
"" : "!",
1061 (s
.f
& F_BINARY
) ?
"" : "!");
1063 if (((s
.f
^ v
.f
) & v
.m
) != 0) {
1064 printf("WARN boundary string inconsistent with contents\n");
1065 printf("INFO expected-flags");
1066 if (v
.m
& F_DETACH
) printf(" %sdetach", (v
.f
& F_DETACH
) ?
"" : "!");
1067 if (v
.m
& F_BINARY
) printf(" %sbinary", (v
.f
& F_BINARY
) ?
"" : "!");
1072 tm
= (v
.f
& F_UTC
) ?
gmtime(&s
.t
) : localtime(&s
.t
);
1073 strftime(bb
, sizeof(bb
), "%Y-%m-%d %H:%M:%S %Z", tm
);
1074 printf("INFO date %s\n", bb
);
1076 if (keyring
&& (k
= key_byid(&kf
, s
.keyid
)) != 0) {
1077 dstr_reset(&d
); key_fulltag(k
, &d
);
1078 printf("INFO key %s\n", d
.buf
);
1080 printf("INFO unknown-key %08lx\n", (unsigned long)s
.keyid
);
1082 if (keyring
) key_close(&kf
);
1088 /*----- Main code ---------------------------------------------------------*/
1092 listtab[i].name, listtab[i].name) \
1093 LI("Signature schemes", sig, \
1094 sigtab[i].name, sigtab[i].name) \
1095 LI("Encodings", enc, \
1096 enctab[i].name, enctab[i].name) \
1097 LI("Hash functions", hash, \
1098 ghashtab[i], ghashtab[i]->name)
1100 MAKELISTTAB(listtab
, LISTS
)
1102 int cmd_show(int argc
, char *argv
[])
1104 return (displaylists(listtab
, argv
+ 1));
1107 static int cmd_help(int, char **);
1109 static cmd cmdtab
[] = {
1110 { "help", cmd_help
, "help [COMMAND...]" },
1111 { "show", cmd_show
, "show [ITEM...]" },
1115 "sign [-adptC] [-k TAG] [-f FORMAT] [-o OUTPUT] [FILE]", "\
1118 -a, --armour Same as `-f pem'.\n\
1119 -b, --binary Treat the input message as binary data.\n\
1120 -d, --detach Produce a detached signature.\n\
1121 -f, --format=FORMAT Encode as FORMAT.\n\
1122 -k, --key=TAG Use public encryption key named by TAG.\n\
1123 -o, --output=FILE Write output to FILE.\n\
1124 -p, --progress Show progress on large files.\n\
1125 -t, --text Canonify input message as a text file.\n\
1126 -C, --nocheck Don't check the private key.\n\
1129 "verify [-abpquvC] [-f FORMAT] [-k TAG] [-o OUTPUT]\n\t\
1130 [FILE [MESSAGE]]", "\
1133 -a, --armour Same as `-f pem'.\n\
1134 -b, --buffer Buffer message until signature is verified.\n\
1135 -f, --format=FORMAT Decode as FORMAT.\n\
1136 -k, --key=TAG Require that the message be signed by key TAG.\n\
1137 -o, --output=FILE Write message to FILE.\n\
1138 -p, --progress Show progress on large files.\n\
1139 -q, --quiet Produce fewer messages.\n\
1140 -t, --freshtime=TIME Only accept signatures made after this time.\n\
1141 -u, --utc Show dates in UTC rather than local time.\n\
1142 -v, --verbose Produce more verbose messages.\n\
1143 -C, --nocheck Don't check the public key.\n\
1146 "info [-au] [-f FORMAT] [FILE]", "\
1149 -a, --armour Same as `-f pem'.\n\
1150 -f, --format=FORMAT Decode as FORMAT.\n\
1151 -u, --utc Show dates in UTC rather than local time.\n\
1154 "format [-apuADE] [-f FORMAT] [-F format] [-m FILE] [-o FILE]\n\t\
1155 [FILE [MESSAGE]]", "\
1158 -a, --armour-in Same as `-f pem'.\n\
1159 -A, --armour-out Same as `-F pem'.\n\
1160 -D, --detach Create detached signature.\n\
1161 -E, --embed Create signature with embedded message.\n\
1162 -f, --format-in=FORMAT Decode input as FORMAT.\n\
1163 -F, --format-out=FORMAT Encode output as FORMAT.\n\
1164 -m, --message=FILE Write message to FILE.\n\
1165 -o, --output=FILE Write new signature to FILE.\n\
1166 -p, --progress Show progress on large files.\n\
1169 }; /* " Emacs seems confused. */
1171 static int cmd_help(int argc
, char **argv
)
1173 sc_help(cmdtab
, stdout
, argv
+ 1);
1177 void version(FILE *fp
)
1179 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1182 static void usage(FILE *fp
)
1184 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1187 void help_global(FILE *fp
)
1191 Sign and verify data.\n\
1193 Global command-line options:\n\
1195 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1196 -v, --version Show program version number.\n\
1197 -u, --usage Show a terse usage message.\n\
1199 -k, --keyring=FILE Read keys from FILE.\n",
1205 * Arguments: @int argc@ = number of command line arguments
1206 * @char *argv[]@ = vector of command line arguments
1208 * Returns: Zero if successful, nonzero otherwise.
1210 * Use: Encrypts or decrypts files.
1213 int main(int argc
, char *argv
[])
1219 /* --- Initialize the library --- */
1223 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1224 rand_seed(RAND_GLOBAL
, 160);
1225 /* trace_on(stderr, 1); */
1227 /* --- Parse options --- */
1230 static struct option opts
[] = {
1231 { "help", 0, 0, 'h' },
1232 { "version", 0, 0, 'v' },
1233 { "usage", 0, 0, 'u' },
1234 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1237 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1242 sc_help(cmdtab
, stdout
, argv
+ optind
);
1264 if (f
& f_bogus
|| argc
< 1) {
1269 /* --- Dispatch to the correct subcommand handler --- */
1271 return (findcmd(cmdtab
, argv
[0])->cmd(argc
, argv
));
1276 /*----- That's all, folks -------------------------------------------------*/