3 * Verify signatures on distribuitions of files
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
40 #include <mLib/alloc.h>
41 #include <mLib/base64.h>
42 #include <mLib/mdwopt.h>
43 #include <mLib/quis.h>
44 #include <mLib/report.h>
55 /*----- Data formatting ---------------------------------------------------*/
57 /* --- Binary data structure --- *
59 * The binary format, which is used for hashing and for the optional binary
60 * output, consists of a sequence of tagged blocks. The tag describes the
61 * format and meaining of the following data.
65 /* --- Block tags --- */
67 T_IDENT
= 0, /* An identifying marker */
68 T_KEYID
, /* Key identifier */
69 T_BEGIN
, /* Begin hashing here */
70 T_COMMENT
= T_BEGIN
, /* A textual comment */
71 T_DATE
, /* Creation date of signature */
72 T_EXPIRE
, /* Expiry date of signature */
73 T_FILE
, /* File and corresponding hash */
74 T_SIGNATURE
, /* Final signature block */
76 /* --- Error messages --- */
84 /* --- Name translation table --- */
86 static const char *tagtab
[] = {
88 "comment:", "date:", "expires:", "file:",
93 static const char *errtab
[] = {
95 "Unexpected end-of-file",
96 "Binary object too large",
101 /* --- Memory representation of block types --- */
103 typedef struct block
{
104 int tag
; /* Type tag */
105 dstr d
; /* String data */
106 dstr b
; /* Binary data */
107 time_t t
; /* Timestamp */
108 uint32 k
; /* Keyid */
111 /* --- @timestring@ --- *
113 * Arguments: @time_t t@ = a timestamp
114 * @dstr *d@ = a string to write on
118 * Use: Writes a textual representation of the timestamp to the
122 static void timestring(time_t t
, dstr
*d
)
124 if (t
== KEXP_FOREVER
)
127 struct tm
*tm
= localtime(&t
);
129 d
->len
+= strftime(d
->buf
+ d
->len
, 32, "%Y-%m-%d %H:%M:%S %Z", tm
);
134 /* --- @breset@ --- *
136 * Arguments: @block *b@ = block to reset
140 * Use: Resets a block so that more stuff can be put in it.
143 static void breset(block
*b
)
154 * Arguments: @block *b@ = block to initialize
158 * Use: Initializes a block as something to read into.
161 static void binit(block
*b
)
168 /* --- @bdestroy@ --- *
170 * Arguments: @block *b@ = block to destroy
174 * Use: Destroys a block's contents.
177 static void bdestroy(block
*b
)
185 * Arguments: @block *b@ = pointer to block
186 * @FILE *fp@ = stream to read from
187 * @unsigned bin@ = binary switch
189 * Returns: Tag of block, or an error tag.
191 * Use: Reads a block from a stream.
194 static int bget(block
*b
, FILE *fp
, unsigned bin
)
198 /* --- Read the tag --- */
204 if (getstring(fp
, &d
, GSF_FILE
))
206 for (tag
= 0; tagtab
[tag
]; tag
++) {
207 if (strcmp(tagtab
[tag
], d
.buf
) == 0)
214 /* --- Decide what to do next --- */
220 /* --- Reading of strings --- */
224 if (getstring(fp
, &b
->d
, GSF_FILE
| (bin ? GSF_RAW
: 0)))
228 /* --- Timestamps --- */
234 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
236 b
->t
= ((time_t)(((LOAD32(buf
+ 0) << 16) << 16) & ~MASK32
) |
237 (time_t)LOAD32(buf
+ 4));
239 if (getstring(fp
, &b
->d
, GSF_FILE
))
241 if (strcmp(b
->d
.buf
, "forever") == 0)
243 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
248 /* --- Key ids --- */
253 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
257 if (getstring(fp
, &b
->d
, GSF_FILE
))
259 b
->k
= strtoul(b
->d
.buf
, 0, 16);
263 /* --- Reading of binary data --- */
270 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
276 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
281 if (getstring(fp
, &b
->d
, GSF_FILE
))
284 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
285 base64_decode(&b64
, 0, 0, &b
->b
);
289 getstring(fp
, &b
->d
, GSF_FILE
| (bin ? GSF_RAW
: 0)))
293 /* --- Anything else --- */
304 * Arguments: @block *b@ = pointer to block to emit
305 * @dstr *d@ = output buffer
309 * Use: Encodes a block in a binary format.
312 static void blob(block
*b
, dstr
*d
)
324 if (b
->t
== KEXP_FOREVER
) {
325 STORE32(d
->buf
+ d
->len
, 0xffffffff);
326 STORE32(d
->buf
+ d
->len
+ 4, 0xffffffff);
328 STORE32(d
->buf
+ d
->len
, ((b
->t
& ~MASK32
) >> 16) >> 16);
329 STORE32(d
->buf
+ d
->len
+ 4, b
->t
);
335 STORE32(d
->buf
+ d
->len
, b
->k
);
341 STORE16(d
->buf
+ d
->len
, b
->b
.len
);
344 if (b
->tag
== T_FILE
) {
352 /* --- @bwrite@ --- *
354 * Arguments: @block *b@ = pointer to block to write
355 * @FILE *fp@ = stream to write on
359 * Use: Writes a block on a stream in a textual format.
362 static void bwrite(block
*b
, FILE *fp
)
364 fputs(tagtab
[b
->tag
], fp
);
369 putstring(fp
, b
->d
.buf
, 0);
374 timestring(b
->t
, &d
);
375 putstring(fp
, d
.buf
, 0);
379 fprintf(fp
, "%08lx", (unsigned long)b
->k
);
387 base64_encode(&b64
, b
->b
.buf
, b
->b
.len
, &d
);
388 base64_encode(&b64
, 0, 0, &d
);
390 if (b
->tag
== T_FILE
) {
392 putstring(fp
, b
->d
.buf
, 0);
401 * Arguments: @block *b@ = pointer to block to write
402 * @FILE *fp@ = file to write on
403 * @ghash *h@ = pointer to hash function
404 * @unsigned bin@ = binary/text flag
408 * Use: Spits out a block properly.
411 static void bemit(block
*b
, FILE *fp
, ghash
*h
, unsigned bin
)
413 if (h
|| (fp
&& bin
)) {
417 GH_HASH(h
, d
.buf
, d
.len
);
419 fwrite(d
.buf
, d
.len
, 1, fp
);
425 /*----- Static variables --------------------------------------------------*/
427 static const char *keyring
= "keyring";
429 /*----- Other shared functions --------------------------------------------*/
433 * Arguments: @FILE *fp@ = file to write on
434 * @const void *p@ = pointer to data to be written
435 * @size_t sz@ = size of the data to write
439 * Use: Emits a hex dump to a stream.
442 static void fhex(FILE *fp
, const void *p
, size_t sz
)
448 fprintf(fp
, "%02x", *q
++);
455 /*----- Signature generation ----------------------------------------------*/
457 static int sign(int argc
, char *argv
[])
464 const char *ki
= "dsig";
469 time_t exp
= KEXP_EXPIRE
;
471 const char *ifile
= 0, *hfile
= 0;
472 const char *ofile
= 0;
482 static struct option opts
[] = {
483 { "null", 0, 0, '0' },
484 { "binary", 0, 0, 'b' },
485 { "verbose", 0, 0, 'v' },
486 { "progress", 0, 0, 'p' },
487 { "quiet", 0, 0, 'q' },
488 { "comment", OPTF_ARGREQ
, 0, 'c' },
489 { "file", OPTF_ARGREQ
, 0, 'f' },
490 { "hashes", OPTF_ARGREQ
, 0, 'h' },
491 { "output", OPTF_ARGREQ
, 0, 'o' },
492 { "key", OPTF_ARGREQ
, 0, 'k' },
493 { "expire", OPTF_ARGREQ
, 0, 'e' },
494 { "nocheck", OPTF_ARGREQ
, 0, 'C' },
497 int i
= mdwopt(argc
, argv
, "+0vpqbC" "c:" "f:h:o:" "k:e:",
537 if (strcmp(optarg
, "forever") == 0)
539 else if ((exp
= get_date(optarg
, 0)) == -1)
540 die(EXIT_FAILURE
, "bad expiry time");
547 if (optind
!= argc
|| (f
& f_bogus
))
548 die(EXIT_FAILURE
, "Usage: sign [-OPTIONS]");
550 die(EXIT_FAILURE
, "Inconsistent options `-h' and `-f'");
552 /* --- Locate the signing key --- */
554 if (key_open(&kf
, keyring
, KOPEN_WRITE
, key_moan
, 0))
555 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
556 if ((k
= key_bytag(&kf
, ki
)) == 0)
557 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
559 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
560 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
563 s
= getsig(k
, "dsig", 1);
565 /* --- Check the key --- */
567 if (!(f
& f_nocheck
) && (err
= s
->ops
->check(s
)) != 0)
568 moan("key `%s' fails check: %s", d
.buf
, err
);
570 /* --- Open files --- */
572 if (hfile
) ifile
= hfile
;
573 if (!ifile
|| strcmp(ifile
, "-") == 0)
575 else if ((ifp
= fopen(ifile
, (f
& f_bin
) ?
"rb" : "r")) == 0) {
576 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
577 ifile
, strerror(errno
));
580 if (!ofile
|| strcmp(ofile
, "-") == 0)
582 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
583 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
584 ofile
, strerror(errno
));
587 /* --- Emit the start of the output --- */
589 binit(&b
); b
.tag
= T_IDENT
;
590 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
591 bemit(&b
, ofp
, 0, f
& f_bin
);
593 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
594 bemit(&b
, ofp
, 0, f
& f_bin
);
596 /* --- Start hashing, and emit the datestamps and things --- */
599 time_t now
= time(0);
601 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, s
->h
, f
& f_bin
);
602 if (exp
== KEXP_EXPIRE
)
603 exp
= now
+ 86400 * 28;
604 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, s
->h
, f
& f_bin
);
606 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
607 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
614 /* --- Now hash the various files --- */
619 hfp
.ee
= &encodingtab
[ENC_HEX
];
620 hfp
.gch
= GH_CLASS(s
->h
);
627 DENSURE(&b
.b
, hfp
.gch
->hashsz
);
628 hfp
.hbuf
= (octet
*)b
.b
.buf
;
629 if (ferror(ofp
)) { f
|= f_bogus
; break; }
630 if ((hf
= hfparse(&hfp
)) == HF_EOF
) break;
635 if (hfp
.gch
!= GH_CLASS(s
->h
)) {
636 moan("%s:%d: incorrect hash function `%s' (should be `%s')",
637 hfile
, n
, hfp
.gch
->name
, GH_CLASS(s
->h
)->name
);
642 moan("%s:%d: invalid hash-file line", hfile
, n
);
647 b
.b
.len
+= hfp
.gch
->hashsz
;
648 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
655 /* --- Stop on an output error --- */
662 /* --- Read the next filename to hash --- */
664 fhash_init(&fh
, GH_CLASS(s
->h
), f
| FHF_BINARY
);
666 if (getstring(ifp
, &b
.d
, GSF_FILE
| f
))
669 DENSURE(&b
.b
, GH_CLASS(s
->h
)->hashsz
);
670 if (fhash(&fh
, b
.d
.buf
, b
.b
.buf
)) {
671 moan("error reading `%s': %s", b
.d
.buf
, strerror(errno
));
674 b
.b
.len
+= GH_CLASS(s
->h
)->hashsz
;
676 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
677 fprintf(stderr
, " %s\n", b
.d
.buf
);
679 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
685 /* --- Create the signature --- */
687 if (!(f
& f_bogus
)) {
690 if ((e
= s
->ops
->doit(s
, &b
.b
)) != 0) {
691 moan("error creating signature: %s", key_strerror(e
));
694 if (!(f
& f_bogus
)) {
695 bemit(&b
, ofp
, 0, f
& f_bin
);
696 key_used(&kf
, k
, exp
);
700 /* --- Tidy up at the end --- */
713 if ((e
= key_close(&kf
)) != 0) {
716 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
717 keyring
, strerror(errno
));
719 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
720 keyring
, strerror(errno
));
724 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
725 return (EXIT_SUCCESS
);
732 /*----- Signature verification --------------------------------------------*/
734 static int checkjunk(const char *path
, const struct stat
*st
, void *p
)
736 if (!st
) printf("JUNK (error %s) %s\n", strerror(errno
), path
);
737 else printf("JUNK %s %s\n", describefile(st
), path
);
741 static int verify(int argc
, char *argv
[])
760 /* --- Parse the options --- */
763 static struct option opts
[] = {
764 { "verbose", 0, 0, 'v' },
765 { "progress", 0, 0, 'p' },
766 { "quiet", 0, 0, 'q' },
767 { "nocheck", 0, 0, 'C' },
768 { "junk", 0, 0, 'j' },
771 int i
= mdwopt(argc
, argv
, "+vpqCj", opts
, 0, 0, 0);
798 if ((f
& f_bogus
) || argc
> 1)
799 die(EXIT_FAILURE
, "Usage: verify [-qvC] [FILE]");
801 /* --- Open the key file, and start reading the input file --- */
803 if (key_open(&kf
, keyring
, KOPEN_READ
, key_moan
, 0))
804 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
808 if ((fp
= fopen(argv
[0], "rb")) == 0) {
809 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
810 argv
[0], strerror(errno
));
817 if ((fp
= fopen(argv
[0], "r")) == 0) {
818 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
819 argv
[0], strerror(errno
));
824 /* --- Read the introductory matter --- */
829 e
= bget(&b
, fp
, f
& f_bin
);
831 die(EXIT_FAILURE
, "error reading packet: %s", errtab
[-e
]);
837 printf("INFO ident: `%s'\n", b
.d
.buf
);
840 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
842 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
847 key_fulltag(k
, &b
.d
);
848 printf("INFO key: %s\n", b
.d
.buf
);
852 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
857 /* --- Initialize the hash function and start reading hashed packets --- */
861 puts("FAIL no keyid packet found");
865 s
= getsig(k
, "dsig", 0);
866 if (!(f
& f_nocheck
) && verb
&& (err
= s
->ops
->check(s
)) != 0)
867 printf("WARN public key fails check: %s", err
);
869 fhash_init(&fh
, GH_CLASS(s
->h
), f
| FHF_BINARY
);
874 printf("INFO comment: `%s'\n", b
.d
.buf
);
875 bemit(&b
, 0, s
->h
, 0);
880 timestring(b
.t
, &b
.d
);
881 printf("INFO date: %s\n", b
.d
.buf
);
883 bemit(&b
, 0, s
->h
, 0);
886 time_t now
= time(0);
887 if (b
.t
!= KEXP_FOREVER
&& b
.t
< now
) {
889 puts("BAD signature has expired");
894 timestring(b
.t
, &b
.d
);
895 printf("INFO expires: %s\n", b
.d
.buf
);
897 bemit(&b
, 0, s
->h
, 0);
901 DENSURE(&d
, GH_CLASS(s
->h
)->hashsz
);
902 if (fhash(&fh
, b
.d
.buf
, d
.buf
)) {
904 printf("BAD error reading file `%s': %s\n",
905 b
.d
.buf
, strerror(errno
));
908 } else if (b
.b
.len
!= GH_CLASS(s
->h
)->hashsz
||
909 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
911 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
913 } else if (verb
> 3) {
914 fputs("INFO hash: ", stdout
);
915 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
916 printf(" %s\n", b
.d
.buf
);
918 bemit(&b
, 0, s
->h
, 0);
921 if (s
->ops
->doit(s
, &b
.b
)) {
923 puts("BAD bad signature");
926 puts("INFO good signature");
930 printf("FAIL invalid packet type %i\n", e
);
935 e
= bget(&b
, fp
, f
& f_bin
);
938 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
943 if ((f
& FHF_JUNK
) && fhash_junk(&fh
, checkjunk
, 0))
954 puts("FAIL signature invalid");
956 puts("OK signature verified");
958 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
966 /*----- Main code ---------------------------------------------------------*/
970 listtab[i].name, listtab[i].name) \
971 LI("Signature schemes", sig, \
972 sigtab[i].name, sigtab[i].name) \
973 LI("Hash functions", hash, \
974 ghashtab[i], ghashtab[i]->name)
976 MAKELISTTAB(listtab
, LISTS
)
978 int cmd_show(int argc
, char *argv
[])
980 return (displaylists(listtab
, argv
+ 1));
983 static int cmd_help(int, char **);
985 static cmd cmdtab
[] = {
986 { "help", cmd_help
, "help [COMMAND...]" },
987 { "show", cmd_show
, "show [ITEM...]" },
989 "sign [-0bpqvC] [-c COMMENT] [-k TAG] [-e EXPIRE]\n\t\
990 [-f FILE] [-h FILE] [-o OUTPUT]",
994 -0, --null Read null-terminated filenames from stdin.\n\
995 -b, --binary Produce a binary output file.\n\
996 -q, --quiet Produce fewer messages while working.\n\
997 -v, --verbose Produce more messages while working.\n\
998 -p, --progress Show progress on large files.\n\
999 -C, --nocheck Don't check the private key.\n\
1000 -c, --comment=COMMENT Include COMMENT in the output file.\n\
1001 -f, --file=FILE Read filenames to hash from FILE.\n\
1002 -h, --hashes=FILE Read precomputed hashes from FILE.\n\
1003 -o, --output=FILE Write the signed result to FILE.\n\
1004 -k, --key=TAG Use a key named by TAG.\n\
1005 -e, --expire=TIME The signature should expire after TIME.\n\
1008 "verify [-pqvC] [FILE]", "\
1011 -q, --quiet Produce fewer messages while working.\n\
1012 -v, --verbose Produce more messages while working.\n\
1013 -p, --progress Show progress on large files.\n\
1014 -C, --nocheck Don't check the public key.\n\
1019 static int cmd_help(int argc
, char **argv
)
1021 sc_help(cmdtab
, stdout
, argv
+ 1);
1025 void version(FILE *fp
)
1027 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1030 static void usage(FILE *fp
)
1032 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1035 void help_global(FILE *fp
)
1039 Create and verify signatures on lists of files.\n\
1041 Global command-line options:\n\
1043 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1044 -v, --version Show program version number.\n\
1045 -u, --usage Show a terse usage message.\n\
1047 -k, --keyring=FILE Read keys from FILE.\n",
1053 * Arguments: @int argc@ = number of command line arguments
1054 * @char *argv[]@ = vector of command line arguments
1056 * Returns: Zero if successful, nonzero otherwise.
1058 * Use: Signs or verifies signatures on lists of files. Useful for
1059 * ensuring that a distribution is unmolested.
1062 int main(int argc
, char *argv
[])
1068 /* --- Initialize the library --- */
1072 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1073 rand_seed(RAND_GLOBAL
, 160);
1075 /* --- Parse options --- */
1078 static struct option opts
[] = {
1079 { "help", 0, 0, 'h' },
1080 { "version", 0, 0, 'v' },
1081 { "usage", 0, 0, 'u' },
1082 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1085 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1090 sc_help(cmdtab
, stdout
, argv
+ optind
);
1112 if (f
& f_bogus
|| argc
< 1) {
1117 /* --- Dispatch to the correct subcommand handler --- */
1119 return (findcmd(cmdtab
, argv
[0])->cmd(argc
, argv
));
1124 /*----- That's all, folks -------------------------------------------------*/