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 /*----- Utilities ---------------------------------------------------------*/
61 /* --- @keyreport@ --- *
63 * Arguments: @const char *file@ = filename containing the error
64 * @int line@ = line number in file
65 * @const char *err@ = error text message
66 * @void *p@ = unimportant pointer
70 * Use: Reports errors during the opening of a key file.
73 static void keyreport(const char *file
, int line
, const char *err
, void *p
)
75 moan("error in keyring `%s' at line `%s': %s", file
, line
, err
);
78 /*----- Static variables --------------------------------------------------*/
80 static const char *keyring
= "keyring";
82 /*----- Data formats ------------------------------------------------------*
84 * Our crypto stuff is split into three parts: a header describing the key,
85 * the message itself, and the signature. In a detached signature, the
86 * message is separate, and the header and signature are combined into one
87 * encoded chunk. In an attached signature, the message is included. There
88 * are two forms of message: binary and text. Binary messages are divided
89 * into chunks, each preceded by a 2-octet length, and terminated by a
90 * zero-length chunk. Text messages are byte-stuffed, as for RFC821, and
91 * trailing whitespace before a newline is ignored.
94 typedef struct sigmsg
{
95 unsigned f
; /* Flags */
99 #define F_HASHMASK (F_BINARY)
100 uint32 keyid
; /* Key identifier */
101 time_t t
; /* When the signature was made */
102 dstr kh
; /* Key fingerprint (sanity check) */
103 dstr sig
; /* Signature */
104 sig
*s
; /* Signature algorithm */
107 #define F_MIDLINE 16u
109 #define F_NOCLOSE 64u
111 #define F_BUFFER 256u
114 /*----- Chunk I/O ---------------------------------------------------------*/
116 static void chunk_write(enc
*e
, const void *p
, size_t n
)
122 if (e
->ops
->write(e
, b
, 2) || e
->ops
->write(e
, p
, n
))
123 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
126 static size_t chunk_read(enc
*e
, void *p
)
131 if (e
->ops
->read(e
, b
, 2) != 2)
134 if (n
&& e
->ops
->read(e
, p
, n
) != n
)
139 if (!errno
) die(EXIT_FAILURE
, "unexpected end-of-file on input");
140 else die(EXIT_FAILURE
, "error reading input: %s", strerror(errno
));
144 /*----- Message canonification --------------------------------------------*/
146 #define MSGBUFSZ 65536
147 #define MSGBUFTHRESH 32768
149 typedef struct msgcanon
{
153 size_t (*read
)(struct msgcanon
*, void *);
154 void (*write
)(struct msgcanon
*, const void *, size_t);
155 void (*close
)(struct msgcanon
*);
158 #define MC_INIT { 0 }
160 static size_t textread(msgcanon
*m
, void *bp
)
162 size_t n
= 0, nsp
= 0;
167 if (f
& F_EOF
) return (0);
169 if ((ch
= getc(m
->fp
)) == EOF
) goto eof
;
170 if (!(f
& (F_DETACH
| F_MIDLINE
)) && ch
== '.') {
172 if (ch
== '\n') goto eof
;
176 if (n
>= MSGBUFTHRESH
) goto full
;
179 } else if (isspace(ch
)) {
181 if (n
>= MSGBUFSZ
) goto full
;
186 if (n
>= MSGBUFTHRESH
) goto full
;
200 static void textwrite(msgcanon
*m
, const void *bp
, size_t n
)
202 const char *p
= bp
, *l
= p
+ n
;
206 if (!(f
& (F_MIDLINE
| F_DETACH
)) &&
207 (*p
== '.' || *p
== '-'))
209 if (*p
== '\n') f
&= ~F_MIDLINE
;
217 static size_t binreadembed(msgcanon
*m
, void *bp
)
218 { return (chunk_read(m
->e
, bp
)); }
219 static size_t binreaddetach(msgcanon
*m
, void *bp
)
220 { return (fread(bp
, 1, MSGBUFSZ
, m
->fp
)); }
222 static void binwriteembed(msgcanon
*m
, const void *bp
, size_t n
)
223 { chunk_write(m
->e
, bp
, n
); }
225 static void nullwrite(msgcanon
*m
, const void *bp
, size_t n
) { ; }
227 static void mcsetup_readfile(msgcanon
*m
, unsigned f
, const char *fn
)
229 m
->f
= F_DETACH
| (f
& F_BINARY
);
230 if (!fn
|| strcmp(fn
, "-") == 0) {
233 } else if ((m
->fp
= fopen(fn
, (f
& F_BINARY
) ?
"rb" : "r")) == 0)
234 die(EXIT_FAILURE
, "couldn't open file `%s': %s", fn
, strerror(errno
));
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
, const char *dfn
)
244 m
->f
= f
| F_NOCLOSE
;
246 if (dfn
&& !(f
& F_DETACH
)) die(EXIT_FAILURE
, "signature is not detached");
248 if (!(f
& F_DETACH
)) {
252 m
->read
= binreadembed
;
254 m
->read
= binreaddetach
;
255 if (!dfn
|| strcmp(dfn
, "-") == 0)
257 else if ((m
->fp
= fopen(dfn
, "rb")) == 0)
258 die(EXIT_FAILURE
, "can't open `%s': %s", dfn
, strerror(errno
));
264 if (!(f
& F_DETACH
)) {
265 if (e
->ops
->decdone(e
) || ferror(e
->fp
))
266 die(EXIT_FAILURE
, "error at end of signature header");
270 } else if (!dfn
|| strcmp(dfn
, "-") == 0)
272 else if ((m
->fp
= fopen(dfn
, "r")) == 0)
273 die(EXIT_FAILURE
, "can't read file `%s': %s", dfn
, strerror(errno
));
279 static void mcsetup_write(msgcanon
*m
, unsigned f
, enc
**ee
)
283 m
->f
= f
| F_NOCLOSE
;
286 m
->write
= nullwrite
;
287 else if (f
& F_BINARY
) {
291 m
->write
= binwriteembed
;
293 if (e
->ops
->encdone(e
) || ferror(e
->fp
))
294 die(EXIT_FAILURE
, "error at end of signature header");
298 m
->write
= textwrite
;
302 static void mc_endread(msgcanon
*m
, const encops
*eops
, enc
**ee
)
304 if (!(m
->f
& F_DETACH
)) {
308 *ee
= initdec(eops
, m
->fp
, checkbdry
, "CATSIGN SIGNATURE");
310 if (m
->fp
&& !(m
->f
& F_NOCLOSE
)) {
311 if (ferror(m
->fp
) || fclose(m
->fp
))
312 die(EXIT_FAILURE
, "error closing message file: %s", strerror(errno
));
316 static void mc_endwrite(msgcanon
*m
, const encops
*eops
, enc
**ee
)
318 if (!(m
->f
& F_BINARY
)) {
319 if (m
->f
& F_MIDLINE
) putc('\n', m
->fp
);
320 if (!(m
->f
& F_DETACH
)) {
321 putc('.', m
->fp
); putc('\n', m
->fp
);
322 *ee
= initenc(eops
, m
->fp
, "CATSIGN SIGNATURE");
324 } else if (!(m
->f
& F_DETACH
)) {
325 chunk_write(m
->e
, 0, 0);
328 if (m
->fp
&& !(m
->f
& F_NOCLOSE
)) {
329 if (fflush(m
->fp
) || ferror(m
->fp
) || fclose(m
->fp
))
330 die(EXIT_FAILURE
, "error closing message file: %s", strerror(errno
));
334 /*----- Signature reading and writing -------------------------------------*/
336 static void sig_init(sigmsg
*s
, unsigned f
, uint32 keyid
)
338 s
->f
= f
& F_SIGMASK
;
343 dstr_create(&s
->sig
);
346 static void sig_destroy(sigmsg
*s
)
348 dstr_destroy(&s
->kh
);
349 dstr_destroy(&s
->sig
);
350 if (s
->s
) freesig(s
->s
);
353 static void sigtobuffer(sigmsg
*s
, buf
*b
, int hashp
)
358 if (hashp
) buf_putu16(b
, s
->f
& F_HASHMASK
);
359 else buf_putu16(b
, s
->f
& F_SIGMASK
);
360 buf_putu32(b
, s
->keyid
);
361 buf_putu32(b
, HI64(t
));
362 buf_putu32(b
, LO64(t
));
363 buf_putdstr16(b
, &s
->kh
);
367 static void dohash(ghash
*h
, const void *p
, size_t n
)
369 /* trace_block(1, "hashing", p, n); */
373 static void sig_hash(sigmsg
*s
)
378 buf_init(&b
, bb
, sizeof(bb
));
379 sigtobuffer(s
, &b
, 1);
380 dohash(s
->s
->h
, BBASE(&b
), BLEN(&b
));
383 static void keyhash(key
*k
, sig
*s
, dstr
*d
)
388 h
= GH_INIT(GH_CLASS(s
->h
));
391 key_fingerprint(k
, h
, &kf
);
392 dstr_ensure(d
, GH_CLASS(h
)->hashsz
);
393 GH_DONE(h
, d
->buf
+ d
->len
);
394 d
->len
+= GH_CLASS(h
)->hashsz
;
398 static void sig_writeheader(enc
*e
, sigmsg
*s
)
403 buf_init(&b
, bb
, sizeof(bb
));
404 sigtobuffer(s
, &b
, 0);
405 chunk_write(e
, BBASE(&b
), BLEN(&b
));
408 static void sig_writesig(enc
*e
, sigmsg
*s
)
409 { chunk_write(e
, s
->sig
.buf
, s
->sig
.len
); }
411 static void diechoke(const char *m
, void *p
)
412 { die(EXIT_FAILURE
, "%s%s%s", p
, p ?
": " : "", m
); }
414 static void sig_readheader(enc
*e
, sigmsg
*s
,
415 void (*choke
)(const char *, void *), void *p
)
424 n
= chunk_read(e
, bb
);
426 if (buf_getu16(&b
, &f
)) choke("missing flags", p
);
427 if (buf_getu32(&b
, &x
)) choke("missing keyid", p
);
429 if (buf_getu32(&b
, &x
) || buf_getu32(&b
, &y
))
430 choke("missing datestamp", p
);
431 SET64(t
, x
, y
); s
->t
= GET64(time_t, t
);
432 if (buf_getdstr16(&b
, &s
->kh
))
433 choke("missing key hash", p
);
435 choke("junk at end", p
);
438 static void sig_readsig(enc
*e
, sigmsg
*s
)
443 n
= chunk_read(e
, bb
);
444 dstr_putm(&s
->sig
, bb
, n
);
447 /*----- Signing -----------------------------------------------------------*/
449 static int sign(int argc
, char *argv
[])
451 const char *ef
= "binary", *fn
= 0, *of
= 0, *kn
= "ccsig", *err
;
462 msgcanon mc_in
= MC_INIT
, mc_out
= MC_INIT
;
466 static const struct option opt
[] = {
467 { "armour", 0, 0, 'a' },
468 { "armor", 0, 0, 'a' },
469 { "binary", 0, 0, 'b' },
470 { "detach", 0, 0, 'd' },
471 { "key", OPTF_ARGREQ
, 0, 'k' },
472 { "format", OPTF_ARGREQ
, 0, 'f' },
473 { "output", OPTF_ARGREQ
, 0, 'o' },
474 { "text", 0, 0, 't' },
477 i
= mdwopt(argc
, argv
, "k:f:o:abdt", opt
, 0, 0, 0);
480 case 'k': kn
= optarg
; break;
481 case 'f': ef
= optarg
; break;
482 case 'o': of
= optarg
; break;
483 case 'a': ef
= "pem"; break;
484 case 't': f
&= ~F_BINARY
; break;
485 case 'b': f
|= F_BINARY
; break;
486 case 'd': f
|= F_DETACH
; break;
487 default: f
|= F_BOGUS
; break;
490 if (argc
- optind
> 1 || (f
& F_BOGUS
))
491 die(EXIT_FAILURE
, "Usage: sign [-OPTIONS] [FILE]");
493 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
494 die(EXIT_FAILURE
, "can't open keyring `%s'", keyring
);
495 if ((k
= key_bytag(&kf
, kn
)) == 0)
496 die(EXIT_FAILURE
, "key `%s' not found", kn
);
498 if ((eo
= getenc(ef
)) == 0)
499 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
501 fn
= (optind
>= argc
) ?
0 : argv
[optind
++];
503 if (!of
|| strcmp(of
, "-") == 0)
505 else if ((ofp
= fopen(of
, eo
->wmode
)) == 0) {
506 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
507 ofp
, strerror(errno
));
510 /* --- Start the work --- */
512 sig_init(&s
, f
, k
->id
);
515 s
.s
= getsig(k
, "ccsig", 1);
516 if ((err
= s
.s
->ops
->check(s
.s
)) != 0)
517 moan("key %s fails check: %s", d
.buf
, err
);
518 keyhash(k
, s
.s
, &s
.kh
);
520 (f
& F_DETACH
) ?
"CATSIGN SIGNATURE" :
521 (f
& F_BINARY
) ?
"CATSIGN MESSAGE" :
522 "CATSIGN MESSAGE HEADER");
523 sig_writeheader(e
, &s
);
525 /* --- Hash the message --- */
527 mcsetup_readfile(&mc_in
, f
, fn
);
528 mcsetup_write(&mc_out
, f
, &e
);
531 n
= mc_in
.read(&mc_in
, bb
);
533 dohash(s
.s
->h
, bb
, n
);
534 mc_out
.write(&mc_out
, bb
, n
);
536 mc_endread(&mc_in
, 0, 0);
537 mc_endwrite(&mc_out
, eo
, &e
);
539 /* --- Write the signature --- */
541 if (s
.s
->ops
->doit(s
.s
, &s
.sig
))
542 die(EXIT_FAILURE
, "signature failed");
545 if (fflush(ofp
) || ferror(ofp
) || fclose(ofp
))
546 die(EXIT_FAILURE
, "error writing signature: %s", strerror(errno
));
548 /* --- All done --- */
557 /*----- Verifying ---------------------------------------------------------*/
559 typedef struct vrfctx
{
565 static int vrfbdry(const char *b
, void *p
)
569 if (strcmp(b
, "CATSIGN MESSAGE") == 0) {
571 v
->m
|= F_BINARY
| F_DETACH
;
573 } else if (strcmp(b
, "CATSIGN MESSAGE HEADER") == 0) {
574 v
->m
|= F_BINARY
| F_DETACH
;
576 } else if (strcmp(b
, "CATSIGN SIGNATURE") == 0) {
584 static void vrfchoke(const char *m
, void *p
)
587 if (v
->verb
) printf("FAIL %s: %s\n", v
->what
, m
);
591 static int verify(int argc
, char *argv
[])
593 const char *ef
= "binary", *of
= 0, *dfn
= 0, *kn
= 0, *err
;
594 vrfctx v
= { 0, 0, 1 };
598 FILE *fp
, *ofp
= 0, *rfp
= 0;
604 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
606 msgcanon mc_in
= MC_INIT
;
610 static const struct option opt
[] = {
611 { "armour", 0, 0, 'a' },
612 { "armor", 0, 0, 'a' },
613 { "buffer", 0, 0, 'b' },
614 { "key", OPTF_ARGREQ
, 0, 'k' },
615 { "format", OPTF_ARGREQ
, 0, 'f' },
616 { "output", OPTF_ARGREQ
, 0, 'o' },
617 { "quiet", 0, 0, 'q' },
618 { "utc", 0, 0, 'u' },
619 { "fresh-time", 0, 0, 't' },
620 { "gmt", 0, 0, 'u' },
621 { "verbose", 0, 0, 'v' },
624 i
= mdwopt(argc
, argv
, "k:f:o:abqt:uv", opt
, 0, 0, 0);
627 case 'a': ef
= "pem"; break;
628 case 'b': v
.f
|= F_BUFFER
; break;
629 case 'k': kn
= optarg
; break;
630 case 'f': ef
= optarg
; break;
631 case 'o': of
= optarg
; break;
632 case 'u': v
.f
|= F_UTC
; break;
634 if (strcmp(optarg
, "always") == 0) t_fresh
= 0;
635 else if ((t_fresh
= get_date(optarg
, 0)) < 0)
636 die(EXIT_FAILURE
, "bad freshness time");
638 case 'q': if (v
.verb
> 0) v
.verb
--; break;
639 case 'v': if (v
.verb
< 10) v
.verb
++; break;
640 default: v
.f
|= F_BOGUS
; break;
643 if (argc
- optind
> 2 || (v
.f
& F_BOGUS
))
644 die(EXIT_FAILURE
, "Usage: verify [-OPTIONS] [FILE [MESSAGE]]");
646 if ((eo
= getenc(ef
)) == 0)
647 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
651 else if (strcmp(argv
[optind
], "-") == 0) {
654 } else if ((fp
= fopen(argv
[optind
], eo
->rmode
)) == 0) {
655 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
656 argv
[optind
], strerror(errno
));
660 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
661 die(EXIT_FAILURE
, "can't open keyring `%s'", keyring
);
662 if (kn
&& (kk
= key_bytag(&kf
, kn
)) == 0)
663 die(EXIT_FAILURE
, "key `%s' not found", kn
);
665 e
= initdec(eo
, fp
, vrfbdry
, &v
);
667 /* --- Read the header chunk --- */
669 v
.what
= "malformed header";
670 sig_readheader(e
, &s
, vrfchoke
, &v
);
672 if (((s
.f
^ v
.f
) & v
.m
) != 0) {
673 if (v
.verb
) printf("FAIL boundary string inconsistent with contents\n");
678 if ((k
= key_byid(&kf
, s
.keyid
)) == 0) {
679 if (v
.verb
) printf("FAIL key id %08lx not found\n",
680 (unsigned long)s
.keyid
);
683 if (kk
&& k
->id
!= kk
->id
) {
685 dstr_reset(&d
); key_fulltag(k
, &d
);
686 dstr_reset(&dd
); key_fulltag(kk
, &dd
);
687 printf("FAIL signing key is %s; expected key %s\n", d
.buf
, dd
.buf
);
692 s
.s
= getsig(k
, "ccsig", 0);
693 dstr_reset(&d
); key_fulltag(k
, &d
);
694 if (v
.verb
&& (err
= s
.s
->ops
->check(s
.s
)) != 0)
695 printf("WARN verification key %s fails check: %s\n", d
.buf
, err
);
697 dstr_reset(&dd
); keyhash(k
, s
.s
, &dd
);
698 if (dd
.len
!= s
.kh
.len
|| memcmp(dd
.buf
, s
.kh
.buf
, dd
.len
) != 0) {
699 if (v
.verb
) printf("FAIL key hash mismatch\n");
703 /* --- Now a merry dance --- */
708 dfn
= argv
[optind
++];
709 mcsetup_read(&mc_in
, v
.f
, &e
, dfn
);
711 if (!of
&& (v
.f
& F_DETACH
)) {
714 } else if (!of
|| strcmp(of
, "-") == 0) {
718 if (of
&& !(v
.f
& F_BUFFER
)) {
719 if ((ofp
= fopen(of
, (v
.f
& F_BINARY
) ?
"wb" : "w")) == 0) {
720 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
721 of
, strerror(errno
));
724 } else if ((rfp
= tmpfile()) == 0)
725 die(EXIT_FAILURE
, "couldn't create temporary file: %s", strerror(errno
));
727 /* --- Read the message and verify the signature --- */
731 n
= mc_in
.read(&mc_in
, bb
);
733 dohash(s
.s
->h
, bb
, n
);
734 if (rfp
) fwrite(bb
, 1, n
, rfp
);
736 mc_endread(&mc_in
, eo
, &e
);
737 if (!(v
.f
& F_DETACH
))
739 if (rfp
&& (ferror(rfp
) || fflush(rfp
))) {
740 if (v
.verb
) printf("FAIL error writing message: %s\n", strerror(errno
));
744 /* --- Check the signature --- */
746 if (s
.s
->ops
->doit(s
.s
, &s
.sig
)) {
747 if (v
.verb
) printf("FAIL signature verification failed\n");
750 if (t_fresh
&& s
.t
< t_fresh
) {
751 if (v
.verb
) printf("FAIL signature is stale\n");
755 if (v
.verb
) printf("FAIL signature timestamp in the future\n");
759 tm
= (v
.f
& F_UTC
) ?
gmtime(&s
.t
) : localtime(&s
.t
);
760 strftime(bb
, sizeof(bb
), "%Y-%m-%d %H:%M:%S %Z", tm
);
761 printf("INFO good-signature %s\n", d
.buf
);
762 printf("INFO date %s\n", bb
);
765 /* --- Unbuffer buffered input --- */
767 if (v
.f
& F_BUFFER
) {
768 if (!ofp
&& (ofp
= fopen(of
, "wb")) == 0) {
769 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
770 of
, strerror(errno
));
773 if (v
.verb
&& ofp
== stdout
) printf("DATA\n");
775 n
= fread(bb
, 1, sizeof(bb
), rfp
);
777 if (fwrite(bb
, 1, n
, ofp
) < n
)
778 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
780 if (ferror(rfp
) || fclose(rfp
))
781 die(EXIT_FAILURE
, "error unbuffering output: %s", strerror(errno
));
783 if (ofp
&& (fflush(ofp
) || ferror(ofp
) || fclose(ofp
)))
784 die(EXIT_FAILURE
, "error writing output: %s", strerror(errno
));
786 /* --- Tidy up --- */
789 if (v
.verb
&& ofp
!= stdout
)
790 printf("OK verified successfully\n");
799 /*----- Reformatting ------------------------------------------------------*/
801 static int format(int argc
, char *argv
[])
803 const char *ief
= "binary", *oef
= "binary";
804 const char *dfn
= 0, *of
= 0, *mf
= 0;
806 FILE *fp
, *ofp
= 0, *mfp
= 0;
809 msgcanon mc_in
= MC_INIT
, mc_out
= MC_INIT
;
811 vrfctx v
= { 0, 0, 1 };
812 unsigned f
= 0, fm
= ~F_SIGMASK
, sf
;
813 const encops
*ieo
, *oeo
;
817 static const struct option opt
[] = {
818 { "armour-in", 0, 0, 'a' },
819 { "armor-in", 0, 0, 'a' },
820 { "armour-out", 0, 0, 'A' },
821 { "armor-out", 0, 0, 'A' },
822 { "detach", 0, 0, 'D' },
823 { "embed", 0, 0, 'E' },
824 { "format-in", OPTF_ARGREQ
, 0, 'f' },
825 { "format-out", OPTF_ARGREQ
, 0, 'F' },
826 { "message", OPTF_ARGREQ
, 0, 'm' },
827 { "output", OPTF_ARGREQ
, 0, 'o' },
830 i
= mdwopt(argc
, argv
, "f:F:m:o:aADE", opt
, 0, 0, 0);
833 case 'a': ief
= "pem"; break;
834 case 'A': oef
= "pem"; break;
835 case 'f': ief
= optarg
; break;
836 case 'F': oef
= optarg
; break;
837 case 'D': f
|= F_DETACH
; fm
|= F_DETACH
; break;
838 case 'E': f
&= ~F_DETACH
; fm
|= F_DETACH
; break;
839 case 'm': mf
= optarg
; break;
840 case 'o': of
= optarg
; break;
841 default: f
|= F_BOGUS
; break;
845 if (argc
- optind
> 2 || (f
& F_BOGUS
))
846 die(EXIT_FAILURE
, "Usage: format [-OPTIONS] [FILE [MESSAGE]]");
848 if ((ieo
= getenc(ief
)) == 0)
849 die(EXIT_FAILURE
, "encoding `%s' not found", ief
);
850 if ((oeo
= getenc(oef
)) == 0)
851 die(EXIT_FAILURE
, "encoding `%s' not found", oef
);
855 else if (strcmp(argv
[optind
], "-") == 0) {
858 } else if ((fp
= fopen(argv
[optind
], ieo
->rmode
)) == 0) {
859 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
860 argv
[optind
], strerror(errno
));
865 dfn
= argv
[optind
++];
867 ie
= initdec(ieo
, fp
, vrfbdry
, &v
);
869 /* --- Read the header chunk --- */
871 sig_readheader(ie
, &s
, diechoke
, "malformed header");
873 if (((s
.f
^ v
.f
) & v
.m
) != 0)
874 moan("boundary string inconsistent with contents (ignoring)");
876 mcsetup_read(&mc_in
, s
.f
, &ie
, dfn
);
878 /* --- Prepare the output stuff --- */
880 if (!of
&& !mf
) of
= "-";
882 f
= (f
& fm
) | (s
.f
& ~fm
);
889 mcsetup_writenull(&mc_out
);
891 if (strcmp(of
, "-") == 0)
893 else if ((ofp
= fopen(of
, oeo
->wmode
)) == 0) {
894 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
895 of
, strerror(errno
));
897 oe
= initenc(oeo
, ofp
,
898 (f
& F_DETACH
) ?
"CATSIGN SIGNATURE" :
899 (f
& F_BINARY
) ?
"CATSIGN MESSAGE" :
900 "CATSIGN MESSAGE HEADER");
901 sig_writeheader(oe
, &s
);
902 mcsetup_write(&mc_out
, f
, &oe
);
906 if (strcmp(mf
, "-") == 0)
908 else if ((mfp
= fopen(mf
, (f
& F_BINARY
) ?
"wb" : "w")) == 0) {
909 die(EXIT_FAILURE
, "couldn't open file `%s' for output: %s",
910 mf
, strerror(errno
));
914 /* --- Wade through the message body --- */
917 n
= mc_in
.read(&mc_in
, bb
);
919 mc_out
.write(&mc_out
, bb
, n
);
920 if (mfp
) fwrite(bb
, 1, n
, mfp
);
922 mc_endread(&mc_in
, ieo
, &ie
);
923 if (of
) mc_endwrite(&mc_out
, oeo
, &oe
);
925 /* --- Write the signature --- */
927 if (!(sf
& F_DETACH
))
930 sig_writesig(oe
, &s
);
931 oe
->ops
->encdone(oe
);
934 /* --- All done --- */
936 ie
->ops
->decdone(ie
);
937 if (ferror(fp
) || fclose(fp
))
938 die(EXIT_FAILURE
, "error reading input signature: %s", strerror(errno
));
939 if (ofp
&& (fflush(ofp
) || ferror(ofp
) || fclose(ofp
)))
940 die(EXIT_FAILURE
, "error writing output signature: %s", strerror(errno
));
941 if (mfp
&& (fflush(mfp
) || ferror(mfp
) || fclose(mfp
)))
942 die(EXIT_FAILURE
, "error writing output message: %s", strerror(errno
));
949 static void infochoke(const char *m
, void *p
)
952 printf("BAD %s: %s\n", v
->what
, m
);
956 static void infokeyreport(const char *file
, int line
,
957 const char *err
, void *p
)
960 static int info(int argc
, char *argv
[])
962 const char *ef
= "binary";
963 vrfctx v
= { 0, 0, 1 };
976 static const struct option opt
[] = {
977 { "armour", 0, 0, 'a' },
978 { "armor", 0, 0, 'a' },
979 { "format", OPTF_ARGREQ
, 0, 'f' },
980 { "gmt", 0, 0, 'u' },
981 { "utc", 0, 0, 'u' },
984 i
= mdwopt(argc
, argv
, "f:au", opt
, 0, 0, 0);
987 case 'a': ef
= "pem"; break;
988 case 'f': ef
= optarg
; break;
989 case 'u': v
.f
|= F_UTC
; break;
990 default: v
.f
|= F_BOGUS
; break;
993 if (argc
- optind
> 1 || (v
.f
& F_BOGUS
))
994 die(EXIT_FAILURE
, "Usage: info [-OPTIONS] [FILE]");
996 if ((eo
= getenc(ef
)) == 0)
997 die(EXIT_FAILURE
, "encoding `%s' not found", ef
);
1001 else if (strcmp(argv
[optind
], "-") == 0) {
1004 } else if ((fp
= fopen(argv
[optind
], eo
->rmode
)) == 0) {
1005 die(EXIT_FAILURE
, "couldn't open file `%s': %s",
1006 argv
[optind
], strerror(errno
));
1010 if (key_open(&kf
, keyring
, KOPEN_READ
, infokeyreport
, 0)) {
1011 printf("NOTE can't open keyring `%s'\n", keyring
);
1014 e
= initdec(eo
, fp
, vrfbdry
, &v
);
1016 v
.what
= "malformed header";
1017 sig_readheader(e
, &s
, infochoke
, &v
);
1019 printf("INFO flags %sdetach %sbinary\n",
1020 (s
.f
& F_DETACH
) ?
"" : "!",
1021 (s
.f
& F_BINARY
) ?
"" : "!");
1023 if (((s
.f
^ v
.f
) & v
.m
) != 0) {
1024 printf("WARN boundary string inconsistent with contents\n");
1025 printf("INFO expected-flags");
1026 if (v
.m
& F_DETACH
) printf(" %sdetach", (v
.f
& F_DETACH
) ?
"" : "!");
1027 if (v
.m
& F_BINARY
) printf(" %sbinary", (v
.f
& F_BINARY
) ?
"" : "!");
1032 tm
= (v
.f
& F_UTC
) ?
gmtime(&s
.t
) : localtime(&s
.t
);
1033 strftime(bb
, sizeof(bb
), "%Y-%m-%d %H:%M:%S %Z", tm
);
1034 printf("INFO date %s\n", bb
);
1036 if (keyring
&& (k
= key_byid(&kf
, s
.keyid
)) != 0) {
1037 dstr_reset(&d
); key_fulltag(k
, &d
);
1038 printf("INFO key %s\n", d
.buf
);
1040 printf("INFO unknown-key %08lx\n", (unsigned long)s
.keyid
);
1042 if (keyring
) key_close(&kf
);
1048 /*----- Main code ---------------------------------------------------------*/
1052 listtab[i].name, listtab[i].name) \
1053 LI("Signature schemes", sig, \
1054 sigtab[i].name, sigtab[i].name) \
1055 LI("Encodings", enc, \
1056 enctab[i].name, enctab[i].name) \
1057 LI("Hash functions", hash, \
1058 ghashtab[i], ghashtab[i]->name)
1060 MAKELISTTAB(listtab
, LISTS
)
1062 int cmd_show(int argc
, char *argv
[])
1064 return (displaylists(listtab
, argv
+ 1));
1067 static int cmd_help(int, char **);
1069 static cmd cmdtab
[] = {
1070 { "help", cmd_help
, "help [COMMAND...]" },
1071 { "show", cmd_show
, "show [ITEM...]" },
1075 "sign [-adt] [-k TAG] [-f FORMAT] [-o OUTPUT] [FILE]", "\
1078 -a, --armour Same as `-f pem'.\n\
1079 -b, --binary Treat the input message as binary data.\n\
1080 -d, --detach Produce a detached signature.\n\
1081 -f, --format=FORMAT Encode as FORMAT.\n\
1082 -k, --key=TAG Use public encryption key named by TAG.\n\
1083 -o, --output=FILE Write output to FILE.\n\
1084 -t, --text Canonify input message as a text file.\n\
1087 "verify [-abquv] [-f FORMAT] [-k TAG] [-o OUTPUT]\n\t\
1088 [FILE [MESSAGE]]", "\
1091 -a, --armour Same as `-f pem'.\n\
1092 -b, --buffer Buffer message until signature is verified.\n\
1093 -f, --format=FORMAT Decode as FORMAT.\n\
1094 -k, --key=TAG Require that the message be signed by key TAG.\n\
1095 -o, --output=FILE Write message to FILE.\n\
1096 -q, --quiet Produce fewer messages.\n\
1097 -t, --freshtime=TIME Only accept signatures made after this time.\n\
1098 -u, --utc Show dates in UTC rather than local time.\n\
1099 -v, --verbose Produce more verbose messages.\n\
1102 "info [-au] [-f FORMAT] [FILE]", "\
1105 -a, --armour Same as `-f pem'.\n\
1106 -f, --format=FORMAT Decode as FORMAT.\n\
1107 -u, --utc Show dates in UTC rather than local time.\n\
1110 "format [-auADE] [-f FORMAT] [-F format] [-m FILE] [-o FILE]\n\t\
1111 [FILE [MESSAGE]]", "\
1114 -a, --armour-in Same as `-f pem'.\n\
1115 -A, --armour-out Same as `-F pem'.\n\
1116 -D, --detach Create detached signature.\n\
1117 -E, --embed Create signature with embedded message.\n\
1118 -f, --format-in=FORMAT Decode input as FORMAT.\n\
1119 -F, --format-out=FORMAT Encode output as FORMAT.\n\
1120 -m, --message=FILE Write message to FILE.\n\
1121 -o, --output=FILE Write new signature to FILE.\n\
1124 }; /* " Emacs seems confused. */
1126 static int cmd_help(int argc
, char **argv
)
1128 sc_help(cmdtab
, stdout
, argv
+ 1);
1132 void version(FILE *fp
)
1134 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1137 static void usage(FILE *fp
)
1139 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1142 void help_global(FILE *fp
)
1146 Sign and verify data.\n\
1148 Global command-line options:\n\
1150 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1151 -v, --version Show program version number.\n\
1152 -u, --usage Show a terse usage message.\n\
1154 -k, --keyring=FILE Read keys from FILE.\n",
1160 * Arguments: @int argc@ = number of command line arguments
1161 * @char *argv[]@ = vector of command line arguments
1163 * Returns: Zero if successful, nonzero otherwise.
1165 * Use: Encrypts or decrypts files.
1168 int main(int argc
, char *argv
[])
1174 /* --- Initialize the library --- */
1178 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1179 rand_seed(RAND_GLOBAL
, 160);
1180 /* trace_on(stderr, 1); */
1182 /* --- Parse options --- */
1185 static struct option opts
[] = {
1186 { "help", 0, 0, 'h' },
1187 { "version", 0, 0, 'v' },
1188 { "usage", 0, 0, 'u' },
1189 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1192 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1197 sc_help(cmdtab
, stdout
, argv
+ optind
);
1219 if (f
& f_bogus
|| argc
< 1) {
1224 /* --- Dispatch to the correct subcommand handler --- */
1226 return (findcmd(cmdtab
, argv
[0])->cmd(argc
, argv
));
1231 /*----- That's all, folks -------------------------------------------------*/