3 * $Id: dsig.c,v 1.8 2004/04/04 19:42:59 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.8 2004/04/04 19:42:59 mdw
36 * Revision 1.7 2001/02/23 09:04:17 mdw
37 * Add new hash functions. Provide full help for subcommands. Run the
38 * hash function over parts of the header in a canonical order.
40 * Revision 1.6 2000/12/06 20:33:27 mdw
41 * Make flags be macros rather than enumerations, to ensure that they're
44 * Revision 1.5 2000/10/08 12:12:09 mdw
45 * Shut up some warnings.
47 * Revision 1.4 2000/08/04 23:23:44 mdw
48 * Various <ctype.h> fixes.
50 * Revision 1.3 2000/07/15 20:53:23 mdw
51 * More hash functions. Bug fix in getstring.
53 * Revision 1.2 2000/07/01 11:27:22 mdw
54 * Use new PKCS#1 padding functions rather than rolling by hand.
56 * Revision 1.1 2000/06/17 10:54:29 mdw
57 * Program to generate and verify signatures on multiple files.
61 /*----- Header files ------------------------------------------------------*/
71 #include <mLib/alloc.h>
72 #include <mLib/base64.h>
73 #include <mLib/mdwopt.h>
74 #include <mLib/quis.h>
75 #include <mLib/report.h>
89 /*----- Digital signature algorithm ---------------------------------------*/
91 static int dsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
94 key_packstruct ks
[DSA_PRIVFETCHSZ
];
100 kp
= key_fetchinit(dsa_privfetch
, ks
, &dp
);
101 if ((e
= key_fetch(kp
, k
)) != 0) {
105 sz
= mp_octets(dp
.dp
.q
);
107 die(EXIT_FAILURE
, "hash function too wide for this signing key");
109 p
= (octet
*)d
->buf
+ d
->len
;
110 rand_get(RAND_GLOBAL
, p
, sz
);
111 dsa_sign(&dp
.dp
, dp
.x
, m
, msz
, p
, sz
, p
, sz
, p
+ sz
, sz
);
117 static int dsaverify(key
*k
, const void *m
, size_t msz
,
118 const void *s
, size_t ssz
)
121 key_packstruct ks
[DSA_PUBFETCHSZ
];
127 kp
= key_fetchinit(dsa_pubfetch
, ks
, &dp
);
128 if ((e
= key_fetch(kp
, k
)) != 0) {
133 e
= dsa_verify(&dp
.dp
, dp
.y
, m
, msz
, p
, sz
, p
+ sz
, sz
);
138 /*----- RSA signing -------------------------------------------------------*/
140 static int rsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
144 pkcs1 pk
= { 0, 0, 0 };
145 key_packstruct ks
[RSA_PRIVFETCHSZ
];
149 kp
= key_fetchinit(rsa_privfetch
, ks
, &rp
);
150 if ((e
= key_fetch(kp
, k
)) != 0) {
154 rsa_privcreate(&rpc
, &rp
, &rand_global
);
155 if (rsa_sign(&rpc
, m
, msz
, d
, pkcs1_sigencode
, &pk
) < 0)
156 die(EXIT_FAILURE
, "internal error in rsasign (key too small?)");
157 rsa_privdestroy(&rpc
);
162 static int rsaverify(key
*k
, const void *m
, size_t msz
,
163 const void *s
, size_t ssz
)
167 pkcs1 pk
= { 0, 0, 0 };
168 key_packstruct ks
[RSA_PUBFETCHSZ
];
174 kp
= key_fetchinit(rsa_pubfetch
, ks
, &rp
);
175 if ((e
= key_fetch(kp
, k
)) != 0) {
179 rsa_pubcreate(&rpc
, &rp
);
180 if (rsa_verify(&rpc
, s
, ssz
, &d
, pkcs1_sigdecode
, &pk
) > 0 &&
181 msz
== d
.len
&& memcmp(d
.buf
, m
, msz
) == 0)
184 rsa_pubdestroy(&rpc
);
189 /*----- Algorithm choice --------------------------------------------------*/
194 int (*sign
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/, dstr */
*d*/
);
195 int (*verify
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/,
196 const void */
*s*/
, size_t /*ssz*/);
199 static sig sigtab
[] = {
200 { "dsa", "dsig-dsa", dsasign
, dsaverify
},
201 { "rsa", "dsig-rsa", rsasign
, rsaverify
},
205 /* --- @gethash@ --- *
207 * Arguments: @const char *name@ = pointer to name string
209 * Returns: Pointer to appropriate hash class.
211 * Use: Chooses a hash function by name.
214 static const gchash
*gethash(const char *name
)
216 const gchash
*const *g
, *gg
= 0;
217 size_t sz
= strlen(name
);
218 for (g
= ghashtab
; *g
; g
++) {
219 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
220 if ((*g
)->name
[sz
] == 0) {
232 /* --- @getsig@ --- *
234 * Arguments: @const char *name@ = pointer to name string
236 * Returns: Pointer to appropriate signature type.
238 * Use: Chooses a signature algorithm by name.
241 static sig
*getsig(const char *name
)
244 size_t sz
= strlen(name
);
245 for (s
= sigtab
; s
->name
; s
++) {
246 if (strncmp(name
, s
->name
, sz
) == 0) {
247 if (s
->name
[sz
] == 0) {
259 /*----- Data formatting ---------------------------------------------------*/
261 /* --- Binary data structure --- *
263 * The binary format, which is used for hashing and for the optional binary
264 * output, consists of a sequence of tagged blocks. The tag describes the
265 * format and meaining of the following data.
269 /* --- Block tags --- */
271 T_IDENT
= 0, /* An identifying marker */
272 T_SIGALG
, /* Signature algorithm used */
273 T_HASHALG
, /* Hash algorithm used */
274 T_KEYID
, /* Key identifier */
275 T_BEGIN
, /* Begin hashing here */
276 T_COMMENT
= T_BEGIN
, /* A textual comment */
277 T_DATE
, /* Creation date of signature */
278 T_EXPIRE
, /* Expiry date of signature */
279 T_FILE
, /* File and corresponding hash */
280 T_SIGNATURE
, /* Final signature block */
282 /* --- Error messages --- */
290 /* --- Name translation table --- */
292 static const char *tagtab
[] = {
293 "ident:", "sigalg:", "hashalg:", "keyid:",
294 "comment:", "date:", "expires:", "file:",
299 static const char *errtab
[] = {
301 "Unexpected end-of-file",
302 "Binary object too large",
307 /* --- Memory representation of block types --- */
309 typedef struct block
{
310 int tag
; /* Type tag */
311 dstr d
; /* String data */
312 dstr b
; /* Binary data */
313 time_t t
; /* Timestamp */
314 uint32 k
; /* Keyid */
317 /* --- @getstring@ --- *
319 * Arguments: @FILE *fp@ = stream from which to read
320 * @dstr *d@ = destination string
321 * @unsigned raw@ = raw or cooked read
323 * Returns: Zero if OK, nonzero on end-of-file.
325 * Use: Reads a filename (or something similar) from a stream.
328 static int getstring(FILE *fp
, dstr
*d
, unsigned raw
)
333 /* --- Raw: just read exactly what's written up to a null byte --- */
336 if ((ch
= getc(fp
)) == EOF
)
342 if ((ch
= getc(fp
)) == EOF
)
349 /* --- Skip as far as whitespace --- *
351 * Also skip past comments.
359 do ch
= getc(fp
); while (ch
!= '\n' && ch
!= EOF
);
365 /* --- If the character is a quote then read a quoted string --- */
377 /* --- Now read all sorts of interesting things --- */
381 /* --- Handle an escaped thing --- */
388 case 'a': ch
= '\a'; break;
389 case 'b': ch
= '\b'; break;
390 case 'f': ch
= '\f'; break;
391 case 'n': ch
= '\n'; break;
392 case 'r': ch
= '\r'; break;
393 case 't': ch
= '\t'; break;
394 case 'v': ch
= '\v'; break;
401 /* --- If it's a quote or some other end marker then stop --- */
403 if (ch
== q
|| (!q
&& isspace((unsigned char)ch
)))
406 /* --- Otherwise contribute and continue --- */
409 if ((ch
= getc(fp
)) == EOF
)
419 /* --- @putstring@ --- *
421 * Arguments: @FILE *fp@ = stream to write on
422 * @const char *p@ = pointer to text
423 * @unsigned raw@ = whether the string is to be written raw
427 * Use: Emits a string to a stream.
430 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
432 size_t sz
= strlen(p
);
436 /* --- Just write the string null terminated if raw --- */
439 fwrite(p
, 1, sz
+ 1, fp
);
443 /* --- Check for any dodgy characters --- */
446 for (q
= p
; *q
; q
++) {
447 if (isspace((unsigned char)*q
)) {
456 /* --- Emit the string --- */
458 for (q
= p
; *q
; q
++) {
460 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
461 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
462 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
463 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
464 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
465 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
466 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
467 case '`': fputc('\\', fp
); fputc('`', fp
); break;
468 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
469 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
482 /* --- @timestring@ --- *
484 * Arguments: @time_t t@ = a timestamp
485 * @dstr *d@ = a string to write on
489 * Use: Writes a textual representation of the timestamp to the
493 static void timestring(time_t t
, dstr
*d
)
495 if (t
== KEXP_FOREVER
)
498 struct tm
*tm
= localtime(&t
);
500 d
->len
+= strftime(d
->buf
+ d
->len
, 32, "%Y-%m-%d %H:%M:%S %Z", tm
);
505 /* --- @breset@ --- *
507 * Arguments: @block *b@ = block to reset
511 * Use: Resets a block so that more stuff can be put in it.
514 static void breset(block
*b
)
525 * Arguments: @block *b@ = block to initialize
529 * Use: Initializes a block as something to read into.
532 static void binit(block
*b
)
539 /* --- @bdestroy@ --- *
541 * Arguments: @block *b@ = block to destroy
545 * Use: Destroys a block's contents.
548 static void bdestroy(block
*b
)
556 * Arguments: @block *b@ = pointer to block
557 * @FILE *fp@ = stream to read from
558 * @unsigned bin@ = binary switch
560 * Returns: Tag of block, or an error tag.
562 * Use: Reads a block from a stream.
565 static int bget(block
*b
, FILE *fp
, unsigned bin
)
569 /* --- Read the tag --- */
575 if (getstring(fp
, &d
, 0))
577 for (tag
= 0; tagtab
[tag
]; tag
++) {
578 if (strcmp(tagtab
[tag
], d
.buf
) == 0)
585 /* --- Decide what to do next --- */
591 /* --- Reading of strings --- */
597 if (getstring(fp
, &b
->d
, bin
))
601 /* --- Timestamps --- */
607 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
609 b
->t
= ((time_t)(((LOAD32(buf
+ 0) << 16) << 16) & ~MASK32
) |
610 (time_t)LOAD32(buf
+ 4));
612 if (getstring(fp
, &b
->d
, 0))
614 if (strcmp(b
->d
.buf
, "forever") == 0)
616 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
621 /* --- Key ids --- */
626 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
630 if (getstring(fp
, &b
->d
, 0))
632 b
->k
= strtoul(b
->d
.buf
, 0, 16);
636 /* --- Reading of binary data --- */
643 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
649 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
654 if (getstring(fp
, &b
->d
, 0))
657 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
658 base64_decode(&b64
, 0, 0, &b
->b
);
661 if (tag
== T_FILE
&& getstring(fp
, &b
->d
, bin
))
665 /* --- Anything else --- */
676 * Arguments: @block *b@ = pointer to block to emit
677 * @dstr *d@ = output buffer
681 * Use: Encodes a block in a binary format.
684 static void blob(block
*b
, dstr
*d
)
698 STORE32(d
->buf
+ d
->len
, ((b
->t
& ~MASK32
) >> 16) >> 16);
699 STORE32(d
->buf
+ d
->len
+ 4, b
->t
);
704 STORE32(d
->buf
+ d
->len
, b
->k
);
710 STORE16(d
->buf
+ d
->len
, b
->b
.len
);
713 if (b
->tag
== T_FILE
) {
721 /* --- @bwrite@ --- *
723 * Arguments: @block *b@ = pointer to block to write
724 * @FILE *fp@ = stream to write on
728 * Use: Writes a block on a stream in a textual format.
731 static void bwrite(block
*b
, FILE *fp
)
733 fputs(tagtab
[b
->tag
], fp
);
740 putstring(fp
, b
->d
.buf
, 0);
745 timestring(b
->t
, &d
);
746 putstring(fp
, d
.buf
, 0);
750 fprintf(fp
, "%08lx", (unsigned long)b
->k
);
758 base64_encode(&b64
, b
->b
.buf
, b
->b
.len
, &d
);
759 base64_encode(&b64
, 0, 0, &d
);
761 if (b
->tag
== T_FILE
) {
763 putstring(fp
, b
->d
.buf
, 0);
772 * Arguments: @block *b@ = pointer to block to write
773 * @FILE *fp@ = file to write on
774 * @ghash *h@ = pointer to hash function
775 * @unsigned bin@ = binary/text flag
779 * Use: Spits out a block properly.
782 static void bemit(block
*b
, FILE *fp
, ghash
*h
, unsigned bin
)
784 if (h
|| (fp
&& bin
)) {
788 h
->ops
->hash(h
, d
.buf
, d
.len
);
790 fwrite(d
.buf
, d
.len
, 1, fp
);
796 /*----- Static variables --------------------------------------------------*/
798 static const char *keyring
= "keyring";
800 /*----- Other shared functions --------------------------------------------*/
802 /* --- @keyreport@ --- *
804 * Arguments: @const char *file@ = filename containing the error
805 * @int line@ = line number in file
806 * @const char *err@ = error text message
807 * @void *p@ = unimportant pointer
811 * Use: Reports errors during the opening of a key file.
814 static void keyreport(const char *file
, int line
, const char *err
, void *p
)
816 moan("error in keyring `%s' at line `%s': %s", file
, line
, err
);
821 * Arguments: @const gchash *c@ = pointer to hash class
822 * @const char *file@ = file to hash
823 * @void *b@ = pointer to output buffer
825 * Returns: Zero if it worked, or nonzero for a system error.
827 * Use: Hashes a file.
830 static int fhash(const gchash
*c
, const char *file
, void *b
)
832 FILE *fp
= fopen(file
, "rb");
833 ghash
*h
= c
->init();
840 while ((sz
= fread(buf
, 1, sizeof(buf
), fp
)) > 0)
841 h
->ops
->hash(h
, buf
, sz
);
852 * Arguments: @FILE *fp@ = file to write on
853 * @const void *p@ = pointer to data to be written
854 * @size_t sz@ = size of the data to write
858 * Use: Emits a hex dump to a stream.
861 static void fhex(FILE *fp
, const void *p
, size_t sz
)
867 fprintf(fp
, "%02x", *q
++);
875 /*----- Signature generation ----------------------------------------------*/
877 static int sign(int argc
, char *argv
[])
888 const sig
*s
= sigtab
;
889 const gchash
*gch
= &rmd160
;
891 time_t exp
= KEXP_EXPIRE
;
893 const char *ifile
= 0;
894 const char *ofile
= 0;
902 static struct option opts
[] = {
903 { "null", 0, 0, '0' },
904 { "binary", 0, 0, 'b' },
905 { "verbose", 0, 0, 'v' },
906 { "quiet", 0, 0, 'q' },
907 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
908 { "hash", OPTF_ARGREQ
, 0, 'h' },
909 { "comment", OPTF_ARGREQ
, 0, 'c' },
910 { "file", OPTF_ARGREQ
, 0, 'f' },
911 { "output", OPTF_ARGREQ
, 0, 'o' },
912 { "keytype", OPTF_ARGREQ
, 0, 't' },
913 { "keyid", OPTF_ARGREQ
, 0, 'i' },
914 { "key", OPTF_ARGREQ
, 0, 'i' },
915 { "expire", OPTF_ARGREQ
, 0, 'e' },
918 int i
= mdwopt(argc
, argv
, "+0vqb a:h:c: f:o: t:i:k:e:", opts
, 0, 0, 0);
936 if ((s
= getsig(optarg
)) == 0) {
937 die(EXIT_FAILURE
, "unknown or ambiguous signature algorithm `%s'",
942 if ((gch
= gethash(optarg
)) == 0) {
943 die(EXIT_FAILURE
, "unknown or ambiguous hash function `%s'",
964 if (strcmp(optarg
, "forever") == 0)
966 else if ((exp
= get_date(optarg
, 0)) == -1)
967 die(EXIT_FAILURE
, "bad expiry time");
974 if (optind
!= argc
|| (f
& f_bogus
))
975 die(EXIT_FAILURE
, "Usage: sign [-options]");
977 /* --- Locate the signing key --- */
979 if (key_open(&kf
, keyring
, KOPEN_WRITE
, keyreport
, 0))
980 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
982 if ((k
= key_bytag(&kf
, ki
)) == 0)
983 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
987 if ((k
= key_bytype(&kf
, kt
)) == 0)
988 die(EXIT_FAILURE
, "no appropriate key of type `%s'", kt
);
991 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
992 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
996 /* --- Open files --- */
1000 else if ((ifp
= fopen(ifile
, (f
& f_raw
) ?
"rb" : "r")) == 0) {
1001 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
1002 ifile
, strerror(errno
));
1007 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
1008 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
1009 ofile
, strerror(errno
));
1014 /* --- Emit the start of the output --- */
1016 binit(&b
); b
.tag
= T_IDENT
;
1017 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
1018 bemit(&b
, ofp
, 0, f
& f_bin
);
1020 breset(&b
); b
.tag
= T_SIGALG
; DPUTS(&b
.d
, s
->name
);
1021 bemit(&b
, ofp
, h
, f
& f_bin
);
1023 breset(&b
); b
.tag
= T_HASHALG
; DPUTS(&b
.d
, gch
->name
);
1024 bemit(&b
, ofp
, h
, f
& f_bin
);
1026 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
1027 bemit(&b
, ofp
, h
, f
& f_bin
);
1029 /* --- Start hashing, and emit the datestamps and things --- */
1032 time_t now
= time(0);
1034 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, h
, f
& f_bin
);
1035 if (exp
== KEXP_EXPIRE
)
1036 exp
= now
+ 86400 * 28;
1037 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, h
, f
& f_bin
);
1039 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
1040 bemit(&b
, ofp
, h
, f
& f_bin
);
1047 /* --- Now hash the various files --- */
1051 /* --- Stop on an output error --- */
1058 /* --- Read the next filename to hash --- */
1061 if (getstring(ifp
, &b
.d
, f
& f_raw
))
1064 DENSURE(&b
.b
, h
->ops
->c
->hashsz
);
1065 if (fhash(gch
, b
.d
.buf
, b
.b
.buf
)) {
1066 moan("Error reading `%s': %s", b
.d
.buf
, strerror(errno
));
1069 b
.b
.len
+= h
->ops
->c
->hashsz
;
1071 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
1072 fprintf(stderr
, " %s\n", b
.d
.buf
);
1074 bemit(&b
, ofp
, h
, f
& f_bin
);
1078 /* --- Create the signature --- */
1080 if (!(f
& f_bogus
)) {
1082 b
.tag
= T_SIGNATURE
;
1083 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1084 h
->ops
->done(h
, b
.d
.buf
);
1085 if ((e
= s
->sign(k
, b
.d
.buf
, h
->ops
->c
->hashsz
, &b
.b
)) != 0) {
1086 moan("error creating signature: %s", key_strerror(e
));
1089 if (!(f
& f_bogus
)) {
1090 bemit(&b
, ofp
, 0, f
& f_bin
);
1091 key_used(&kf
, k
, exp
);
1095 /* --- Tidy up at the end --- */
1107 if ((e
= key_close(&kf
)) != 0) {
1110 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
1111 keyring
, strerror(errno
));
1113 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
1114 keyring
, strerror(errno
));
1118 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
1119 return (EXIT_SUCCESS
);
1126 /*----- Signature verification --------------------------------------------*/
1128 static int verify(int argc
, char *argv
[])
1139 const gchash
*gch
= &rmd160
;
1146 /* --- Parse the options --- */
1149 static struct option opts
[] = {
1150 { "verbose", 0, 0, 'v' },
1151 { "quiet", 0, 0, 'q' },
1154 int i
= mdwopt(argc
, argv
, "+vq", opts
, 0, 0, 0);
1172 if ((f
& f_bogus
) || argc
> 1)
1173 die(EXIT_FAILURE
, "Usage: verify [-qv] [file]");
1175 /* --- Open the key file, and start reading the input file --- */
1177 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
1178 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
1182 if ((fp
= fopen(argv
[0], "rb")) == 0) {
1183 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1184 argv
[0], strerror(errno
));
1186 if (getc(fp
) == 0) {
1191 if ((fp
= fopen(argv
[0], "r")) == 0) {
1192 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1193 argv
[0], strerror(errno
));
1198 /* --- Read the introductory matter --- */
1203 e
= bget(&b
, fp
, f
& f_bin
);
1205 die(EXIT_FAILURE
, "error reading packet: %s\n", errtab
[-e
]);
1211 printf("INFO ident: `%s'\n", b
.d
.buf
);
1214 if ((s
= getsig(b
.d
.buf
)) == 0) {
1216 printf("FAIL unknown signature algorithm `%s'\n", b
.d
.buf
);
1220 printf("INFO signature algorithm: %s\n", s
->name
);
1223 if ((gch
= gethash(b
.d
.buf
)) == 0) {
1225 printf("FAIL unknown hash function `%s'\n", b
.d
.buf
);
1229 printf("INFO hash function algorithm: %s\n", gch
->name
);
1232 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
1234 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
1239 key_fulltag(k
, &b
.d
);
1240 printf("INFO key: %s\n", b
.d
.buf
);
1244 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
1249 /* --- Initialize the hash function and start reading hashed packets --- */
1255 puts("FAIL no keyid packet found");
1262 breset(&bb
); bb
.tag
= T_SIGALG
; DPUTS(&bb
.d
, s
->name
);
1263 bemit(&bb
, 0, h
, 0);
1264 breset(&bb
); bb
.tag
= T_HASHALG
; DPUTS(&bb
.d
, gch
->name
);
1265 bemit(&bb
, 0, h
, 0);
1266 breset(&bb
); bb
.tag
= T_KEYID
; bb
.k
= k
->id
;
1267 bemit(&bb
, 0, h
, 0);
1275 printf("INFO comment: `%s'\n", b
.d
.buf
);
1281 timestring(b
.t
, &b
.d
);
1282 printf("INFO date: %s\n", b
.d
.buf
);
1287 time_t now
= time(0);
1290 puts("BAD signature has expired");
1295 timestring(b
.t
, &b
.d
);
1296 printf("INFO expires: %s\n", b
.d
.buf
);
1302 DENSURE(&d
, gch
->hashsz
);
1303 if (fhash(gch
, b
.d
.buf
, d
.buf
)) {
1305 printf("BAD error reading file `%s': %s\n",
1306 b
.d
.buf
, strerror(errno
));
1309 } else if (b
.b
.len
!= gch
->hashsz
||
1310 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
1312 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
1314 } else if (verb
> 3) {
1315 fputs("INFO hash: ", stdout
);
1316 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
1317 printf(" %s\n", b
.d
.buf
);
1323 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1324 b
.d
.len
+= h
->ops
->c
->hashsz
;
1325 h
->ops
->done(h
, b
.d
.buf
);
1326 e
= s
->verify(k
, b
.d
.buf
, b
.d
.len
, b
.b
.buf
, b
.b
.len
);
1330 printf("BAD error unpacking key: %s\n", key_strerror(e
));
1332 puts("BAD bad signature");
1335 } else if (verb
> 2)
1336 puts("INFO good signature");
1340 printf("FAIL invalid packet type %i\n", e
);
1345 e
= bget(&b
, fp
, f
& f_bin
);
1348 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
1360 puts("FAIL signature invalid");
1362 puts("OK signature verified");
1364 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
1371 /*----- Main code ---------------------------------------------------------*/
1373 typedef struct cmd
{
1375 int (*func
)(int /*argc*/, char */
*argv*/
[]);
1380 static cmd cmdtab
[] = {
1381 /* { "manifest", manifest, */
1382 /* "manifest [-0] [-o output]" }, */
1385 [-0bqv] [-a alg] [-h hash] [-t keytype] [-i keyid]\n\
1386 [-e expire] [-f file] [-o output]", "\
1389 -0, --null Read null-terminated filenames from stdin.\n\
1390 -b, --binary Produce a binary output file.\n\
1391 -q, --quiet Produce fewer messages while working.\n\
1392 -v, --verbose Produce more messages while working.\n\
1393 -a, --algorithm=SIGALG Use the SIGALG signature algorithm.\n\
1394 -h, --hash=HASHASL Use the HASHALG message digest algorithm.\n\
1395 -c, --comment=COMMENT Include COMMENT in the output file.\n\
1396 -f, --file=FILE Read filenames to hash from FILE.\n\
1397 -o, --output=FILE Write the signed result to FILE.\n\
1398 -t, --keytype=TYPE Use a key with type TYPE to do the signing.\n\
1399 -i, --keyid=ID Use the key with the given ID to do the signing.\n\
1400 -e, --expire=TIME The signature should expire after TIME.\n\
1403 "verify [-qv] [file]", "\
1406 -q, --quiet Produce fewer messages while working.\n\
1407 -v, --verbose Produce more messages while working.\n\
1412 /* --- @findcmd@ --- *
1414 * Arguments: @const char *name@ = a command name
1416 * Returns: Pointer to the command structure.
1418 * Use: Looks up a command by name. If the command isn't found, an
1419 * error is reported and the program is terminated.
1422 static cmd
*findcmd(const char *name
)
1424 cmd
*c
, *chosen
= 0;
1425 size_t sz
= strlen(name
);
1427 for (c
= cmdtab
; c
->name
; c
++) {
1428 if (strncmp(name
, c
->name
, sz
) == 0) {
1429 if (c
->name
[sz
] == 0) {
1433 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1439 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1443 static void version(FILE *fp
)
1445 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1448 static void usage(FILE *fp
)
1450 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1453 static void help(FILE *fp
, char **argv
)
1459 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1469 Create and verify signatures on lists of files.\n\
1471 for (c
= cmdtab
; c
->name
; c
++)
1472 fprintf(fp
, "%s\n", c
->usage
);
1478 * Arguments: @int argc@ = number of command line arguments
1479 * @char *argv[]@ = vector of command line arguments
1481 * Returns: Zero if successful, nonzero otherwise.
1483 * Use: Signs or verifies signatures on lists of files. Useful for
1484 * ensuring that a distribution is unmolested.
1487 int main(int argc
, char *argv
[])
1493 /* --- Initialize the library --- */
1497 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1498 rand_seed(RAND_GLOBAL
, 160);
1500 /* --- Parse options --- */
1503 static struct option opts
[] = {
1504 { "help", 0, 0, 'h' },
1505 { "version", 0, 0, 'v' },
1506 { "usage", 0, 0, 'u' },
1507 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1510 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1515 help(stdout
, argv
+ optind
);
1537 if (f
& f_bogus
|| argc
< 1) {
1542 /* --- Dispatch to the correct subcommand handler --- */
1544 return (findcmd(argv
[0])->func(argc
, argv
));
1549 /*----- That's all, folks -------------------------------------------------*/