2707f4558b5271de7600157b6e7e957e981459a8
5 * Verify signatures on distribuitions of files
7 * (c) 2000 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
42 #include <mLib/alloc.h>
43 #include <mLib/base64.h>
44 #include <mLib/mdwopt.h>
45 #include <mLib/quis.h>
46 #include <mLib/report.h>
57 /*----- Data formatting ---------------------------------------------------*/
59 /* --- Binary data structure --- *
61 * The binary format, which is used for hashing and for the optional binary
62 * output, consists of a sequence of tagged blocks. The tag describes the
63 * format and meaining of the following data.
67 /* --- Block tags --- */
69 T_IDENT
= 0, /* An identifying marker */
70 T_KEYID
, /* Key identifier */
71 T_BEGIN
, /* Begin hashing here */
72 T_COMMENT
= T_BEGIN
, /* A textual comment */
73 T_DATE
, /* Creation date of signature */
74 T_EXPIRE
, /* Expiry date of signature */
75 T_FILE
, /* File and corresponding hash */
76 T_SIGNATURE
, /* Final signature block */
78 /* --- Error messages --- */
86 /* --- Name translation table --- */
88 static const char *tagtab
[] = {
90 "comment:", "date:", "expires:", "file:",
95 static const char *errtab
[] = {
97 "Unexpected end-of-file",
98 "Binary object too large",
103 /* --- Memory representation of block types --- */
105 typedef struct block
{
106 int tag
; /* Type tag */
107 dstr d
; /* String data */
108 dstr b
; /* Binary data */
109 time_t t
; /* Timestamp */
110 uint32 k
; /* Keyid */
113 /* --- @timestring@ --- *
115 * Arguments: @time_t t@ = a timestamp
116 * @dstr *d@ = a string to write on
120 * Use: Writes a textual representation of the timestamp to the
124 static void timestring(time_t t
, dstr
*d
)
126 if (t
== KEXP_FOREVER
)
129 struct tm
*tm
= localtime(&t
);
131 d
->len
+= strftime(d
->buf
+ d
->len
, 32, "%Y-%m-%d %H:%M:%S %Z", tm
);
136 /* --- @breset@ --- *
138 * Arguments: @block *b@ = block to reset
142 * Use: Resets a block so that more stuff can be put in it.
145 static void breset(block
*b
)
156 * Arguments: @block *b@ = block to initialize
160 * Use: Initializes a block as something to read into.
163 static void binit(block
*b
)
170 /* --- @bdestroy@ --- *
172 * Arguments: @block *b@ = block to destroy
176 * Use: Destroys a block's contents.
179 static void bdestroy(block
*b
)
187 * Arguments: @block *b@ = pointer to block
188 * @FILE *fp@ = stream to read from
189 * @unsigned bin@ = binary switch
191 * Returns: Tag of block, or an error tag.
193 * Use: Reads a block from a stream.
196 static int bget(block
*b
, FILE *fp
, unsigned bin
)
200 /* --- Read the tag --- */
206 if (getstring(fp
, &d
, GSF_FILE
))
208 for (tag
= 0; tagtab
[tag
]; tag
++) {
209 if (strcmp(tagtab
[tag
], d
.buf
) == 0)
216 /* --- Decide what to do next --- */
222 /* --- Reading of strings --- */
226 if (getstring(fp
, &b
->d
, GSF_FILE
| (bin ? GSF_RAW
: 0)))
230 /* --- Timestamps --- */
236 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
238 b
->t
= ((time_t)(((LOAD32(buf
+ 0) << 16) << 16) & ~MASK32
) |
239 (time_t)LOAD32(buf
+ 4));
241 if (getstring(fp
, &b
->d
, GSF_FILE
))
243 if (strcmp(b
->d
.buf
, "forever") == 0)
245 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
250 /* --- Key ids --- */
255 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
259 if (getstring(fp
, &b
->d
, GSF_FILE
))
261 b
->k
= strtoul(b
->d
.buf
, 0, 16);
265 /* --- Reading of binary data --- */
272 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
278 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
283 if (getstring(fp
, &b
->d
, GSF_FILE
))
286 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
287 base64_decode(&b64
, 0, 0, &b
->b
);
291 getstring(fp
, &b
->d
, GSF_FILE
| (bin ? GSF_RAW
: 0)))
295 /* --- Anything else --- */
306 * Arguments: @block *b@ = pointer to block to emit
307 * @dstr *d@ = output buffer
311 * Use: Encodes a block in a binary format.
314 static void blob(block
*b
, dstr
*d
)
326 if (b
->t
== KEXP_FOREVER
) {
327 STORE32(d
->buf
+ d
->len
, 0xffffffff);
328 STORE32(d
->buf
+ d
->len
+ 4, 0xffffffff);
330 STORE32(d
->buf
+ d
->len
, ((b
->t
& ~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";
470 time_t exp
= KEXP_EXPIRE
;
472 const char *ifile
= 0;
473 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 { "output", OPTF_ARGREQ
, 0, 'o' },
491 { "key", OPTF_ARGREQ
, 0, 'k' },
492 { "expire", OPTF_ARGREQ
, 0, 'e' },
493 { "nocheck", OPTF_ARGREQ
, 0, 'C' },
496 int i
= mdwopt(argc
, argv
, "+0vpqbC" "c:" "f:o:" "k:e:", opts
, 0, 0, 0);
532 if (strcmp(optarg
, "forever") == 0)
534 else if ((exp
= get_date(optarg
, 0)) == -1)
535 die(EXIT_FAILURE
, "bad expiry time");
542 if (optind
!= argc
|| (f
& f_bogus
))
543 die(EXIT_FAILURE
, "Usage: sign [-OPTIONS]");
545 /* --- Locate the signing key --- */
547 if (key_open(&kf
, keyring
, KOPEN_WRITE
, key_moan
, 0))
548 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
549 if ((k
= key_bytag(&kf
, ki
)) == 0)
550 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
552 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
553 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
556 s
= getsig(k
, "dsig", 1);
558 /* --- Check the key --- */
560 if (!(f
& f_nocheck
) && (err
= s
->ops
->check(s
)) != 0)
561 moan("key `%s' fails check: %s", d
.buf
, err
);
563 /* --- Open files --- */
565 if (!ifile
|| strcmp(ifile
, "-") == 0)
567 else if ((ifp
= fopen(ifile
, (f
& f_bin
) ?
"rb" : "r")) == 0) {
568 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
569 ifile
, strerror(errno
));
572 if (!ofile
|| strcmp(ofile
, "-") == 0)
574 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
575 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
576 ofile
, strerror(errno
));
579 /* --- Emit the start of the output --- */
581 binit(&b
); b
.tag
= T_IDENT
;
582 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
583 bemit(&b
, ofp
, 0, f
& f_bin
);
585 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
586 bemit(&b
, ofp
, 0, f
& f_bin
);
588 /* --- Start hashing, and emit the datestamps and things --- */
591 time_t now
= time(0);
593 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, s
->h
, f
& f_bin
);
594 if (exp
== KEXP_EXPIRE
)
595 exp
= now
+ 86400 * 28;
596 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, s
->h
, f
& f_bin
);
598 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
599 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
606 /* --- Now hash the various files --- */
610 /* --- Stop on an output error --- */
617 /* --- Read the next filename to hash --- */
620 if (getstring(ifp
, &b
.d
, GSF_FILE
| f
))
623 DENSURE(&b
.b
, GH_CLASS(s
->h
)->hashsz
);
624 if (fhash(GH_CLASS(s
->h
), f
| FHF_BINARY
, b
.d
.buf
, b
.b
.buf
)) {
625 moan("Error reading `%s': %s", b
.d
.buf
, strerror(errno
));
628 b
.b
.len
+= GH_CLASS(s
->h
)->hashsz
;
630 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
631 fprintf(stderr
, " %s\n", b
.d
.buf
);
633 bemit(&b
, ofp
, s
->h
, f
& f_bin
);
637 /* --- Create the signature --- */
639 if (!(f
& f_bogus
)) {
642 if ((e
= s
->ops
->doit(s
, &b
.b
)) != 0) {
643 moan("error creating signature: %s", key_strerror(e
));
646 if (!(f
& f_bogus
)) {
647 bemit(&b
, ofp
, 0, f
& f_bin
);
648 key_used(&kf
, k
, exp
);
652 /* --- Tidy up at the end --- */
665 if ((e
= key_close(&kf
)) != 0) {
668 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
669 keyring
, strerror(errno
));
671 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
672 keyring
, strerror(errno
));
676 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
677 return (EXIT_SUCCESS
);
684 /*----- Signature verification --------------------------------------------*/
686 static int verify(int argc
, char *argv
[])
704 /* --- Parse the options --- */
707 static struct option opts
[] = {
708 { "verbose", 0, 0, 'v' },
709 { "progress", 0, 0, 'p' },
710 { "quiet", 0, 0, 'q' },
711 { "nocheck", 0, 0, 'C' },
714 int i
= mdwopt(argc
, argv
, "+vpqC", opts
, 0, 0, 0);
738 if ((f
& f_bogus
) || argc
> 1)
739 die(EXIT_FAILURE
, "Usage: verify [-qvC] [FILE]");
741 /* --- Open the key file, and start reading the input file --- */
743 if (key_open(&kf
, keyring
, KOPEN_READ
, key_moan
, 0))
744 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
748 if ((fp
= fopen(argv
[0], "rb")) == 0) {
749 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
750 argv
[0], strerror(errno
));
757 if ((fp
= fopen(argv
[0], "r")) == 0) {
758 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
759 argv
[0], strerror(errno
));
764 /* --- Read the introductory matter --- */
769 e
= bget(&b
, fp
, f
& f_bin
);
771 die(EXIT_FAILURE
, "error reading packet: %s", errtab
[-e
]);
777 printf("INFO ident: `%s'\n", b
.d
.buf
);
780 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
782 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
787 key_fulltag(k
, &b
.d
);
788 printf("INFO key: %s\n", b
.d
.buf
);
792 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
797 /* --- Initialize the hash function and start reading hashed packets --- */
801 puts("FAIL no keyid packet found");
805 s
= getsig(k
, "dsig", 0);
806 if (!(f
& f_nocheck
) && verb
&& (err
= s
->ops
->check(s
)) != 0)
807 printf("WARN public key fails check: %s", err
);
813 printf("INFO comment: `%s'\n", b
.d
.buf
);
814 bemit(&b
, 0, s
->h
, 0);
819 timestring(b
.t
, &b
.d
);
820 printf("INFO date: %s\n", b
.d
.buf
);
822 bemit(&b
, 0, s
->h
, 0);
825 time_t now
= time(0);
826 if (b
.t
!= KEXP_FOREVER
&& b
.t
< now
) {
828 puts("BAD signature has expired");
833 timestring(b
.t
, &b
.d
);
834 printf("INFO expires: %s\n", b
.d
.buf
);
836 bemit(&b
, 0, s
->h
, 0);
840 DENSURE(&d
, GH_CLASS(s
->h
)->hashsz
);
841 if (fhash(GH_CLASS(s
->h
), f
| FHF_BINARY
, b
.d
.buf
, d
.buf
)) {
843 printf("BAD error reading file `%s': %s\n",
844 b
.d
.buf
, strerror(errno
));
847 } else if (b
.b
.len
!= GH_CLASS(s
->h
)->hashsz
||
848 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
850 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
852 } else if (verb
> 3) {
853 fputs("INFO hash: ", stdout
);
854 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
855 printf(" %s\n", b
.d
.buf
);
857 bemit(&b
, 0, s
->h
, 0);
860 if (s
->ops
->doit(s
, &b
.b
)) {
862 puts("BAD bad signature");
865 puts("INFO good signature");
869 printf("FAIL invalid packet type %i\n", e
);
874 e
= bget(&b
, fp
, f
& f_bin
);
877 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
890 puts("FAIL signature invalid");
892 puts("OK signature verified");
894 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
902 /*----- Main code ---------------------------------------------------------*/
906 listtab[i].name, listtab[i].name) \
907 LI("Signature schemes", sig, \
908 sigtab[i].name, sigtab[i].name) \
909 LI("Hash functions", hash, \
910 ghashtab[i], ghashtab[i]->name)
912 MAKELISTTAB(listtab
, LISTS
)
914 int cmd_show(int argc
, char *argv
[])
916 return (displaylists(listtab
, argv
+ 1));
919 static int cmd_help(int, char **);
921 static cmd cmdtab
[] = {
922 { "help", cmd_help
, "help [COMMAND...]" },
923 { "show", cmd_show
, "show [ITEM...]" },
925 "sign [-0bpqvC] [-c COMMENT] [-k TAG] [-e EXPIRE]\n\t\
926 [-f FILE] [-o OUTPUT]",
930 -0, --null Read null-terminated filenames from stdin.\n\
931 -b, --binary Produce a binary output file.\n\
932 -q, --quiet Produce fewer messages while working.\n\
933 -v, --verbose Produce more messages while working.\n\
934 -p, --progress Show progress on large files.\n\
935 -C, --nocheck Don't check the private key.\n\
936 -c, --comment=COMMENT Include COMMENT in the output file.\n\
937 -f, --file=FILE Read filenames to hash from FILE.\n\
938 -o, --output=FILE Write the signed result to FILE.\n\
939 -k, --key=TAG Use a key named by TAG.\n\
940 -e, --expire=TIME The signature should expire after TIME.\n\
943 "verify [-pqvC] [FILE]", "\
946 -q, --quiet Produce fewer messages while working.\n\
947 -v, --verbose Produce more messages while working.\n\
948 -p, --progress Show progress on large files.\n\
949 -C, --nocheck Don't check the public key.\n\
954 static int cmd_help(int argc
, char **argv
)
956 sc_help(cmdtab
, stdout
, argv
+ 1);
960 void version(FILE *fp
)
962 pquis(fp
, "$, Catacomb version " VERSION
"\n");
965 static void usage(FILE *fp
)
967 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
970 void help_global(FILE *fp
)
974 Create and verify signatures on lists of files.\n\
976 Global command-line options:\n\
978 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
979 -v, --version Show program version number.\n\
980 -u, --usage Show a terse usage message.\n\
982 -k, --keyring=FILE Read keys from FILE.\n",
988 * Arguments: @int argc@ = number of command line arguments
989 * @char *argv[]@ = vector of command line arguments
991 * Returns: Zero if successful, nonzero otherwise.
993 * Use: Signs or verifies signatures on lists of files. Useful for
994 * ensuring that a distribution is unmolested.
997 int main(int argc
, char *argv
[])
1003 /* --- Initialize the library --- */
1007 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1008 rand_seed(RAND_GLOBAL
, 160);
1010 /* --- Parse options --- */
1013 static struct option opts
[] = {
1014 { "help", 0, 0, 'h' },
1015 { "version", 0, 0, 'v' },
1016 { "usage", 0, 0, 'u' },
1017 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1020 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1025 sc_help(cmdtab
, stdout
, argv
+ optind
);
1047 if (f
& f_bogus
|| argc
< 1) {
1052 /* --- Dispatch to the correct subcommand handler --- */
1054 return (findcmd(cmdtab
, argv
[0])->cmd(argc
, argv
));
1059 /*----- That's all, folks -------------------------------------------------*/