3 * $Id: dsig.c,v 1.5 2000/10/08 12:12:09 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.5 2000/10/08 12:12:09 mdw
34 * Shut up some warnings.
36 * Revision 1.4 2000/08/04 23:23:44 mdw
37 * Various <ctype.h> fixes.
39 * Revision 1.3 2000/07/15 20:53:23 mdw
40 * More hash functions. Bug fix in getstring.
42 * Revision 1.2 2000/07/01 11:27:22 mdw
43 * Use new PKCS#1 padding functions rather than rolling by hand.
45 * Revision 1.1 2000/06/17 10:54:29 mdw
46 * Program to generate and verify signatures on multiple files.
50 /*----- Header files ------------------------------------------------------*/
60 #include <mLib/alloc.h>
61 #include <mLib/base64.h>
62 #include <mLib/mdwopt.h>
63 #include <mLib/quis.h>
64 #include <mLib/report.h>
87 /*----- Digital signature algorithm ---------------------------------------*/
89 static int dsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
92 key_packstruct ks
[DSA_PRIVFETCHSZ
];
98 kp
= key_fetchinit(dsa_privfetch
, ks
, &dp
);
99 if ((e
= key_fetch(kp
, k
)) != 0) {
103 sz
= mp_octets(dp
.dp
.q
);
105 die(EXIT_FAILURE
, "hash function too wide for this signing key");
107 p
= (octet
*)d
->buf
+ d
->len
;
108 rand_get(RAND_GLOBAL
, p
, sz
);
109 dsa_sign(&dp
.dp
, dp
.x
, m
, msz
, p
, sz
, p
, sz
, p
+ sz
, sz
);
115 static int dsaverify(key
*k
, const void *m
, size_t msz
,
116 const void *s
, size_t ssz
)
119 key_packstruct ks
[DSA_PUBFETCHSZ
];
125 kp
= key_fetchinit(dsa_pubfetch
, ks
, &dp
);
126 if ((e
= key_fetch(kp
, k
)) != 0) {
131 e
= dsa_verify(&dp
.dp
, dp
.y
, m
, msz
, p
, sz
, p
+ sz
, sz
);
136 /*----- RSA signing -------------------------------------------------------*/
138 static int rsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
142 pkcs1 pk
= { 0, 0, 0 };
143 key_packstruct ks
[RSA_PRIVFETCHSZ
];
147 kp
= key_fetchinit(rsa_privfetch
, ks
, &rp
);
148 if ((e
= key_fetch(kp
, k
)) != 0) {
152 rsa_privcreate(&rpc
, &rp
, &rand_global
);
153 if (rsa_sign(&rpc
, m
, msz
, d
, pkcs1_sigencode
, &pk
) < 0)
154 die(EXIT_FAILURE
, "internal error in rsasign (key too small?)");
155 rsa_privdestroy(&rpc
);
160 static int rsaverify(key
*k
, const void *m
, size_t msz
,
161 const void *s
, size_t ssz
)
165 pkcs1 pk
= { 0, 0, 0 };
166 key_packstruct ks
[RSA_PUBFETCHSZ
];
172 kp
= key_fetchinit(rsa_pubfetch
, ks
, &rp
);
173 if ((e
= key_fetch(kp
, k
)) != 0) {
177 rsa_pubcreate(&rpc
, &rp
);
178 if (rsa_verify(&rpc
, s
, ssz
, &d
, pkcs1_sigdecode
, &pk
) > 0 &&
179 msz
== d
.len
&& memcmp(d
.buf
, m
, msz
) == 0)
182 rsa_pubdestroy(&rpc
);
187 /*----- Algorithm choice --------------------------------------------------*/
192 int (*sign
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/, dstr */
*d*/
);
193 int (*verify
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/,
194 const void */
*s*/
, size_t /*ssz*/);
197 static const gchash
*hashtab
[] = {
198 &rmd160
, &tiger
, &sha
, &rmd128
, &rmd256
, &rmd320
, &md5
, &md4
, 0 };
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
**g
, *gg
= 0;
217 size_t sz
= strlen(name
);
218 for (g
= hashtab
; *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
[])
890 const sig
*s
= sigtab
;
891 const gchash
*gch
= &rmd160
;
893 time_t exp
= KEXP_EXPIRE
;
895 const char *ifile
= 0;
896 const char *ofile
= 0;
904 static struct option opts
[] = {
905 { "null", 0, 0, '0' },
906 { "binary", 0, 0, 'b' },
907 { "verbose", 0, 0, 'v' },
908 { "quiet", 0, 0, 'q' },
909 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
910 { "hash", OPTF_ARGREQ
, 0, 'h' },
911 { "comment", OPTF_ARGREQ
, 0, 'c' },
912 { "file", OPTF_ARGREQ
, 0, 'f' },
913 { "output", OPTF_ARGREQ
, 0, 'o' },
914 { "keytype", OPTF_ARGREQ
, 0, 't' },
915 { "keyid", OPTF_ARGREQ
, 0, 'i' },
916 { "key", OPTF_ARGREQ
, 0, 'i' },
917 { "expire", OPTF_ARGREQ
, 0, 'e' },
920 int i
= mdwopt(argc
, argv
, "+0vqb a:h:c: f:o: t:i:k:e:", opts
, 0, 0, 0);
938 if ((s
= getsig(optarg
)) == 0) {
939 die(EXIT_FAILURE
, "unknown or ambiguous signature algorithm `%s'",
944 if ((gch
= gethash(optarg
)) == 0) {
945 die(EXIT_FAILURE
, "unknown or ambiguous hash function `%s'",
966 if (strcmp(optarg
, "forever") == 0)
968 else if ((exp
= get_date(optarg
, 0)) == -1)
969 die(EXIT_FAILURE
, "bad expiry time");
976 if (optind
!= argc
|| (f
& f_bogus
))
977 die(EXIT_FAILURE
, "Usage: sign [-options]");
979 /* --- Locate the signing key --- */
981 if (key_open(&kf
, keyring
, KOPEN_WRITE
, keyreport
, 0))
982 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
984 if ((k
= key_bytag(&kf
, ki
)) == 0)
985 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
989 if ((k
= key_bytype(&kf
, kt
)) == 0)
990 die(EXIT_FAILURE
, "no appropriate key of type `%s'", kt
);
993 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
994 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
998 /* --- Open files --- */
1002 else if ((ifp
= fopen(ifile
, (f
& f_raw
) ?
"rb" : "r")) == 0) {
1003 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
1004 ifile
, strerror(errno
));
1009 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
1010 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
1011 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
, 0, f
& f_bin
);
1023 breset(&b
); b
.tag
= T_HASHALG
; DPUTS(&b
.d
, gch
->name
);
1024 bemit(&b
, ofp
, 0, f
& f_bin
);
1026 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
1027 bemit(&b
, ofp
, 0, f
& f_bin
);
1029 /* --- Start hashing, and emit the datestamps and things --- */
1032 time_t now
= time(0);
1035 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, h
, f
& f_bin
);
1036 if (exp
== KEXP_EXPIRE
)
1037 exp
= now
+ 86400 * 28;
1038 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, h
, f
& f_bin
);
1040 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
1041 bemit(&b
, ofp
, h
, f
& f_bin
);
1048 /* --- Now hash the various files --- */
1052 /* --- Stop on an output error --- */
1059 /* --- Read the next filename to hash --- */
1062 if (getstring(ifp
, &b
.d
, f
& f_raw
))
1065 DENSURE(&b
.b
, h
->ops
->c
->hashsz
);
1066 if (fhash(gch
, b
.d
.buf
, b
.b
.buf
)) {
1067 moan("Error reading `%s': %s", b
.d
.buf
, strerror(errno
));
1070 b
.b
.len
+= h
->ops
->c
->hashsz
;
1072 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
1073 fprintf(stderr
, " %s\n", b
.d
.buf
);
1075 bemit(&b
, ofp
, h
, f
& f_bin
);
1079 /* --- Create the signature --- */
1081 if (!(f
& f_bogus
)) {
1083 b
.tag
= T_SIGNATURE
;
1084 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1085 h
->ops
->done(h
, b
.d
.buf
);
1086 if ((e
= s
->sign(k
, b
.d
.buf
, h
->ops
->c
->hashsz
, &b
.b
)) != 0) {
1087 moan("error creating signature: %s", key_strerror(e
));
1090 if (!(f
& f_bogus
)) {
1091 bemit(&b
, ofp
, 0, f
& f_bin
);
1092 key_used(&kf
, k
, exp
);
1096 /* --- Tidy up at the end --- */
1108 if ((e
= key_close(&kf
)) != 0) {
1111 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
1112 keyring
, strerror(errno
));
1114 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
1115 keyring
, strerror(errno
));
1119 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
1120 return (EXIT_SUCCESS
);
1123 /*----- Signature verification --------------------------------------------*/
1125 static int verify(int argc
, char *argv
[])
1138 const gchash
*gch
= &rmd160
;
1145 /* --- Parse the options --- */
1148 static struct option opts
[] = {
1149 { "verbose", 0, 0, 'v' },
1150 { "quiet", 0, 0, 'q' },
1153 int i
= mdwopt(argc
, argv
, "+vq", opts
, 0, 0, 0);
1171 if ((f
& f_bogus
) || argc
> 1)
1172 die(EXIT_FAILURE
, "Usage: verify [-qv] [file]");
1174 /* --- Open the key file, and start reading the input file --- */
1176 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
1177 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
1181 if ((fp
= fopen(argv
[0], "rb")) == 0) {
1182 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1183 argv
[0], strerror(errno
));
1185 if (getc(fp
) == 0) {
1190 if ((fp
= fopen(argv
[0], "r")) == 0) {
1191 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1192 argv
[0], strerror(errno
));
1197 /* --- Read the introductory matter --- */
1202 e
= bget(&b
, fp
, f
& f_bin
);
1204 die(EXIT_FAILURE
, "error reading packet: %s\n", errtab
[-e
]);
1210 printf("INFO ident: `%s'\n", b
.d
.buf
);
1213 if ((s
= getsig(b
.d
.buf
)) == 0) {
1215 printf("FAIL unknown signature algorithm `%s'\n", b
.d
.buf
);
1219 printf("INFO signature algorithm: %s\n", s
->name
);
1222 if ((gch
= gethash(b
.d
.buf
)) == 0) {
1224 printf("FAIL unknown hash function `%s'\n", b
.d
.buf
);
1228 printf("INFO hash function algorithm: %s\n", gch
->name
);
1231 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
1233 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
1238 key_fulltag(k
, &b
.d
);
1239 printf("INFO key: %s\n", b
.d
.buf
);
1243 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
1248 /* --- Initialize the hash function and start reading hashed packets --- */
1253 puts("FAIL no keyid packet found");
1260 printf("INFO comment: `%s'\n", b
.d
.buf
);
1266 timestring(b
.t
, &b
.d
);
1267 printf("INFO date: %s\n", b
.d
.buf
);
1272 time_t now
= time(0);
1275 puts("BAD signature has expired");
1280 timestring(b
.t
, &b
.d
);
1281 printf("INFO expires: %s\n", b
.d
.buf
);
1287 DENSURE(&d
, gch
->hashsz
);
1288 if (fhash(gch
, b
.d
.buf
, d
.buf
)) {
1290 printf("BAD error reading file `%s': %s\n",
1291 b
.d
.buf
, strerror(errno
));
1294 } else if (b
.b
.len
!= gch
->hashsz
||
1295 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
1297 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
1299 } else if (verb
> 3) {
1300 fputs("INFO hash: ", stdout
);
1301 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
1302 printf(" %s\n", b
.d
.buf
);
1308 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1309 b
.d
.len
+= h
->ops
->c
->hashsz
;
1310 h
->ops
->done(h
, b
.d
.buf
);
1311 e
= s
->verify(k
, b
.d
.buf
, b
.d
.len
, b
.b
.buf
, b
.b
.len
);
1315 printf("BAD error unpacking key: %s\n", key_strerror(e
));
1317 puts("BAD bad signature");
1320 } else if (verb
> 2)
1321 puts("INFO good signature");
1325 printf("FAIL invalid packet type %i\n", e
);
1330 e
= bget(&b
, fp
, f
& f_bin
);
1333 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
1345 puts("FAIL signature invalid");
1347 puts("OK signature verified");
1349 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
1352 /*----- Main code ---------------------------------------------------------*/
1354 typedef struct cmd
{
1356 int (*func
)(int /*argc*/, char */
*argv*/
[]);
1360 static cmd cmdtab
[] = {
1361 /* { "manifest", manifest, */
1362 /* "manifest [-0] [-o output]" }, */
1365 [-0v] [-a alg] [-h hash] [-t keytype] [-i keyid]\n\
1366 [-e expire] [-f file] [-o output]" },
1368 "verify [-qv] [file]" },
1372 static void version(FILE *fp
)
1374 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1377 static void usage(FILE *fp
)
1379 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1382 static void help(FILE *fp
)
1389 Create and verify signatures on lists of files.\n\
1391 for (c
= cmdtab
; c
->name
; c
++)
1392 fprintf(fp
, "%s\n", c
->help
);
1397 * Arguments: @int argc@ = number of command line arguments
1398 * @char *argv[]@ = vector of command line arguments
1400 * Returns: Zero if successful, nonzero otherwise.
1402 * Use: Signs or verifies signatures on lists of files. Useful for
1403 * ensuring that a distribution is unmolested.
1406 int main(int argc
, char *argv
[])
1409 cmd
*c
= 0, *cc
= 0;
1416 /* --- Initialize the library --- */
1420 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1421 rand_seed(RAND_GLOBAL
, 160);
1423 /* --- Parse options --- */
1426 static struct option opts
[] = {
1427 { "help", 0, 0, 'h' },
1428 { "version", 0, 0, 'v' },
1429 { "usage", 0, 0, 'u' },
1430 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1433 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1460 if (f
& f_bogus
|| argc
< 1) {
1465 /* --- Dispatch to the correct subcommand handler --- */
1467 n
= strlen(argv
[0]);
1468 for (c
= cmdtab
; c
->name
; c
++) {
1469 if (strncmp(argv
[0], c
->name
, n
) == 0) {
1470 if (c
->name
[n
] == 0) {
1474 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1480 die(EXIT_FAILURE
, "unknown command `%s'", argv
[0]);
1481 return (cc
->func(argc
, argv
));
1484 /*----- That's all, folks -------------------------------------------------*/