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) &
237 ~(unsigned long)MASK32
) |
238 (time_t)LOAD32(buf
+ 4));
240 if (getstring(fp
, &b
->d
, GSF_FILE
))
242 if (strcmp(b
->d
.buf
, "forever") == 0)
244 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
249 /* --- Key ids --- */
254 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
258 if (getstring(fp
, &b
->d
, GSF_FILE
))
260 b
->k
= strtoul(b
->d
.buf
, 0, 16);
264 /* --- Reading of binary data --- */
271 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
277 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
282 if (getstring(fp
, &b
->d
, GSF_FILE
))
285 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
286 base64_decode(&b64
, 0, 0, &b
->b
);
290 getstring(fp
, &b
->d
, GSF_FILE
| (bin ? GSF_RAW
: 0)))
294 /* --- Anything else --- */
305 * Arguments: @block *b@ = pointer to block to emit
306 * @dstr *d@ = output buffer
310 * Use: Encodes a block in a binary format.
313 static void blob(block
*b
, dstr
*d
)
325 if (b
->t
== KEXP_FOREVER
) {
326 STORE32(d
->buf
+ d
->len
, 0xffffffff);
327 STORE32(d
->buf
+ d
->len
+ 4, 0xffffffff);
329 STORE32(d
->buf
+ d
->len
,
330 ((b
->t
& ~(unsigned long)MASK32
) >> 16) >> 16);
331 STORE32(d
->buf
+ d
->len
+ 4, b
->t
);
337 STORE32(d
->buf
+ d
->len
, b
->k
);
343 STORE16(d
->buf
+ d
->len
, b
->b
.len
);
346 if (b
->tag
== T_FILE
) {
354 /* --- @bwrite@ --- *
356 * Arguments: @block *b@ = pointer to block to write
357 * @FILE *fp@ = stream to write on
361 * Use: Writes a block on a stream in a textual format.
364 static void bwrite(block
*b
, FILE *fp
)
366 fputs(tagtab
[b
->tag
], fp
);
371 putstring(fp
, b
->d
.buf
, 0);
376 timestring(b
->t
, &d
);
377 putstring(fp
, d
.buf
, 0);
381 fprintf(fp
, "%08lx", (unsigned long)b
->k
);
389 base64_encode(&b64
, b
->b
.buf
, b
->b
.len
, &d
);
390 base64_encode(&b64
, 0, 0, &d
);
392 if (b
->tag
== T_FILE
) {
394 putstring(fp
, b
->d
.buf
, 0);
403 * Arguments: @block *b@ = pointer to block to write
404 * @FILE *fp@ = file to write on
405 * @ghash *h@ = pointer to hash function
406 * @unsigned bin@ = binary/text flag
410 * Use: Spits out a block properly.
413 static void bemit(block
*b
, FILE *fp
, ghash
*h
, unsigned bin
)
415 if (h
|| (fp
&& bin
)) {
419 GH_HASH(h
, d
.buf
, d
.len
);
421 fwrite(d
.buf
, d
.len
, 1, fp
);
427 /*----- Static variables --------------------------------------------------*/
429 static const char *keyring
= "keyring";
431 /*----- Other shared functions --------------------------------------------*/
435 * Arguments: @FILE *fp@ = file to write on
436 * @const void *p@ = pointer to data to be written
437 * @size_t sz@ = size of the data to write
441 * Use: Emits a hex dump to a stream.
444 static void fhex(FILE *fp
, const void *p
, size_t sz
)
450 fprintf(fp
, "%02x", *q
++);
457 /*----- Signature generation ----------------------------------------------*/
459 static int sign(int argc
, char *argv
[])
466 const char *ki
= "dsig";
471 time_t exp
= KEXP_EXPIRE
;
473 const char *ifile
= 0, *hfile
= 0;
474 const char *ofile
= 0;
484 static struct option opts
[] = {
485 { "null", 0, 0, '0' },
486 { "binary", 0, 0, 'b' },
487 { "verbose", 0, 0, 'v' },
488 { "progress", 0, 0, 'p' },
489 { "quiet", 0, 0, 'q' },
490 { "comment", OPTF_ARGREQ
, 0, 'c' },
491 { "file", OPTF_ARGREQ
, 0, 'f' },
492 { "hashes", OPTF_ARGREQ
, 0, 'h' },
493 { "output", OPTF_ARGREQ
, 0, 'o' },
494 { "key", OPTF_ARGREQ
, 0, 'k' },
495 { "expire", OPTF_ARGREQ
, 0, 'e' },
496 { "nocheck", OPTF_ARGREQ
, 0, 'C' },
499 int i
= mdwopt(argc
, argv
, "+0vpqbC" "c:" "f:h:o:" "k:e:",
539 if (strcmp(optarg
, "forever") == 0)
541 else if ((exp
= get_date(optarg
, 0)) == -1)
542 die(EXIT_FAILURE
, "bad expiry time");
549 if (optind
!= argc
|| (f
& f_bogus
))
550 die(EXIT_FAILURE
, "Usage: sign [-OPTIONS]");
552 die(EXIT_FAILURE
, "Inconsistent options `-h' and `-f'");
554 /* --- Locate the signing key --- */
556 if (key_open(&kf
, keyring
, KOPEN_WRITE
, key_moan
, 0))
557 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
558 if ((k
= key_bytag(&kf
, ki
)) == 0)
559 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
561 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
562 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
565 s
= getsig(k
, "dsig", 1);
567 /* --- Check the key --- */
569 if (!(f
& f_nocheck
) && (err
= s
->ops
->check(s
)) != 0)
570 moan("key `%s' fails check: %s", d
.buf
, err
);
572 /* --- Open files --- */
574 if (hfile
) ifile
= hfile
;
575 if (!ifile
|| strcmp(ifile
, "-") == 0)
577 else if ((ifp
= fopen(ifile
, (f
& f_bin
) ?
"rb" : "r")) == 0) {
578 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
579 ifile
, strerror(errno
));
582 if (!ofile
|| strcmp(ofile
, "-") == 0)
584 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
585 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
586 ofile
, strerror(errno
));
589 /* --- Emit the start of the output --- */
591 binit(&b
); b
.tag
= T_IDENT
;
592 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
593 bemit(&b
, ofp
, 0, f
& f_bin
);
595 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
596 bemit(&b
, ofp
, 0, f
& f_bin
);
598 /* --- Start hashing, and emit the datestamps and things --- */
601 time_t now
= time(0);
603 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, s
->h
, f
& f_bin
);
604 if (exp
== KEXP_EXPIRE
)
605 exp
= now
+ 86400 * 28;
606 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, s
->h
, f
& f_bin
);
608 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
609 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
616 /* --- Now hash the various files --- */
621 hfp
.ee
= &encodingtab
[ENC_HEX
];
622 hfp
.gch
= GH_CLASS(s
->h
);
629 DENSURE(&b
.b
, hfp
.gch
->hashsz
);
630 hfp
.hbuf
= (octet
*)b
.b
.buf
;
631 if (ferror(ofp
)) { f
|= f_bogus
; break; }
632 if ((hf
= hfparse(&hfp
)) == HF_EOF
) break;
637 if (hfp
.gch
!= GH_CLASS(s
->h
)) {
638 moan("%s:%d: incorrect hash function `%s' (should be `%s')",
639 hfile
, n
, hfp
.gch
->name
, GH_CLASS(s
->h
)->name
);
644 moan("%s:%d: invalid hash-file line", hfile
, n
);
649 b
.b
.len
+= hfp
.gch
->hashsz
;
650 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
657 /* --- Stop on an output error --- */
664 /* --- Read the next filename to hash --- */
666 fhash_init(&fh
, GH_CLASS(s
->h
), f
| FHF_BINARY
);
668 if (getstring(ifp
, &b
.d
, GSF_FILE
| f
))
671 DENSURE(&b
.b
, GH_CLASS(s
->h
)->hashsz
);
672 if (fhash(&fh
, b
.d
.buf
, b
.b
.buf
)) {
673 moan("error reading `%s': %s", b
.d
.buf
, strerror(errno
));
676 b
.b
.len
+= GH_CLASS(s
->h
)->hashsz
;
678 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
679 fprintf(stderr
, " %s\n", b
.d
.buf
);
681 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
687 /* --- Create the signature --- */
689 if (!(f
& f_bogus
)) {
692 if ((e
= s
->ops
->doit(s
, &b
.b
)) != 0) {
693 moan("error creating signature: %s", key_strerror(e
));
696 if (!(f
& f_bogus
)) {
697 bemit(&b
, ofp
, 0, f
& f_bin
);
698 key_used(&kf
, k
, exp
);
702 /* --- Tidy up at the end --- */
715 if ((e
= key_close(&kf
)) != 0) {
718 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
719 keyring
, strerror(errno
));
721 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
722 keyring
, strerror(errno
));
726 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
727 return (EXIT_SUCCESS
);
734 /*----- Signature verification --------------------------------------------*/
736 static int checkjunk(const char *path
, const struct stat
*st
, void *p
)
738 if (!st
) printf("JUNK (error %s) %s\n", strerror(errno
), path
);
739 else printf("JUNK %s %s\n", describefile(st
), path
);
743 static int verify(int argc
, char *argv
[])
762 /* --- Parse the options --- */
765 static struct option opts
[] = {
766 { "verbose", 0, 0, 'v' },
767 { "progress", 0, 0, 'p' },
768 { "quiet", 0, 0, 'q' },
769 { "nocheck", 0, 0, 'C' },
770 { "junk", 0, 0, 'j' },
773 int i
= mdwopt(argc
, argv
, "+vpqCj", opts
, 0, 0, 0);
800 if ((f
& f_bogus
) || argc
> 1)
801 die(EXIT_FAILURE
, "Usage: verify [-qvC] [FILE]");
803 /* --- Open the key file, and start reading the input file --- */
805 if (key_open(&kf
, keyring
, KOPEN_READ
, key_moan
, 0))
806 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
810 if ((fp
= fopen(argv
[0], "rb")) == 0) {
811 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
812 argv
[0], strerror(errno
));
819 if ((fp
= fopen(argv
[0], "r")) == 0) {
820 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
821 argv
[0], strerror(errno
));
826 /* --- Read the introductory matter --- */
831 e
= bget(&b
, fp
, f
& f_bin
);
833 die(EXIT_FAILURE
, "error reading packet: %s", errtab
[-e
]);
839 printf("INFO ident: `%s'\n", b
.d
.buf
);
842 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
844 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
849 key_fulltag(k
, &b
.d
);
850 printf("INFO key: %s\n", b
.d
.buf
);
854 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
859 /* --- Initialize the hash function and start reading hashed packets --- */
863 puts("FAIL no keyid packet found");
867 s
= getsig(k
, "dsig", 0);
868 if (!(f
& f_nocheck
) && verb
&& (err
= s
->ops
->check(s
)) != 0)
869 printf("WARN public key fails check: %s", err
);
871 fhash_init(&fh
, GH_CLASS(s
->h
), f
| FHF_BINARY
);
876 printf("INFO comment: `%s'\n", b
.d
.buf
);
877 bemit(&b
, 0, s
->h
, 0);
882 timestring(b
.t
, &b
.d
);
883 printf("INFO date: %s\n", b
.d
.buf
);
885 bemit(&b
, 0, s
->h
, 0);
888 time_t now
= time(0);
889 if (b
.t
!= KEXP_FOREVER
&& b
.t
< now
) {
891 puts("BAD signature has expired");
896 timestring(b
.t
, &b
.d
);
897 printf("INFO expires: %s\n", b
.d
.buf
);
899 bemit(&b
, 0, s
->h
, 0);
903 DENSURE(&d
, GH_CLASS(s
->h
)->hashsz
);
904 if (fhash(&fh
, b
.d
.buf
, d
.buf
)) {
906 printf("BAD error reading file `%s': %s\n",
907 b
.d
.buf
, strerror(errno
));
910 } else if (b
.b
.len
!= GH_CLASS(s
->h
)->hashsz
||
911 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
913 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
915 } else if (verb
> 3) {
916 fputs("INFO hash: ", stdout
);
917 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
918 printf(" %s\n", b
.d
.buf
);
920 bemit(&b
, 0, s
->h
, 0);
923 if (s
->ops
->doit(s
, &b
.b
)) {
925 puts("BAD bad signature");
928 puts("INFO good signature");
932 printf("FAIL invalid packet type %i\n", e
);
937 e
= bget(&b
, fp
, f
& f_bin
);
940 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
945 if ((f
& FHF_JUNK
) && fhash_junk(&fh
, checkjunk
, 0))
956 puts("FAIL signature invalid");
958 puts("OK signature verified");
960 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
968 /*----- Main code ---------------------------------------------------------*/
972 listtab[i].name, listtab[i].name) \
973 LI("Signature schemes", sig, \
974 sigtab[i].name, sigtab[i].name) \
975 LI("Hash functions", hash, \
976 ghashtab[i], ghashtab[i]->name)
978 MAKELISTTAB(listtab
, LISTS
)
980 int cmd_show(int argc
, char *argv
[])
982 return (displaylists(listtab
, argv
+ 1));
985 static int cmd_help(int, char **);
987 static cmd cmdtab
[] = {
988 { "help", cmd_help
, "help [COMMAND...]" },
989 { "show", cmd_show
, "show [ITEM...]" },
991 "sign [-0bpqvC] [-c COMMENT] [-k TAG] [-e EXPIRE]\n\t\
992 [-f FILE] [-h FILE] [-o OUTPUT]",
996 -0, --null Read null-terminated filenames from stdin.\n\
997 -b, --binary Produce a binary output file.\n\
998 -q, --quiet Produce fewer messages while working.\n\
999 -v, --verbose Produce more messages while working.\n\
1000 -p, --progress Show progress on large files.\n\
1001 -C, --nocheck Don't check the private key.\n\
1002 -c, --comment=COMMENT Include COMMENT in the output file.\n\
1003 -f, --file=FILE Read filenames to hash from FILE.\n\
1004 -h, --hashes=FILE Read precomputed hashes from FILE.\n\
1005 -o, --output=FILE Write the signed result to FILE.\n\
1006 -k, --key=TAG Use a key named by TAG.\n\
1007 -e, --expire=TIME The signature should expire after TIME.\n\
1010 "verify [-pqvC] [FILE]", "\
1013 -q, --quiet Produce fewer messages while working.\n\
1014 -v, --verbose Produce more messages while working.\n\
1015 -p, --progress Show progress on large files.\n\
1016 -C, --nocheck Don't check the public key.\n\
1021 static int cmd_help(int argc
, char **argv
)
1023 sc_help(cmdtab
, stdout
, argv
+ 1);
1027 void version(FILE *fp
)
1029 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1032 static void usage(FILE *fp
)
1034 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1037 void help_global(FILE *fp
)
1041 Create and verify signatures on lists of files.\n\
1043 Global command-line options:\n\
1045 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
1046 -v, --version Show program version number.\n\
1047 -u, --usage Show a terse usage message.\n\
1049 -k, --keyring=FILE Read keys from FILE.\n",
1055 * Arguments: @int argc@ = number of command line arguments
1056 * @char *argv[]@ = vector of command line arguments
1058 * Returns: Zero if successful, nonzero otherwise.
1060 * Use: Signs or verifies signatures on lists of files. Useful for
1061 * ensuring that a distribution is unmolested.
1064 int main(int argc
, char *argv
[])
1070 /* --- Initialize the library --- */
1074 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1075 rand_seed(RAND_GLOBAL
, 160);
1077 /* --- Parse options --- */
1080 static struct option opts
[] = {
1081 { "help", 0, 0, 'h' },
1082 { "version", 0, 0, 'v' },
1083 { "usage", 0, 0, 'u' },
1084 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1087 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1092 sc_help(cmdtab
, stdout
, argv
+ optind
);
1114 if (f
& f_bogus
|| argc
< 1) {
1119 /* --- Dispatch to the correct subcommand handler --- */
1121 return (findcmd(cmdtab
, argv
[0])->cmd(argc
, argv
));
1126 /*----- That's all, folks -------------------------------------------------*/