3 * $Id: dsig.c,v 1.4 2000/08/04 23:23:44 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.4 2000/08/04 23:23:44 mdw
34 * Various <ctype.h> fixes.
36 * Revision 1.3 2000/07/15 20:53:23 mdw
37 * More hash functions. Bug fix in getstring.
39 * Revision 1.2 2000/07/01 11:27:22 mdw
40 * Use new PKCS#1 padding functions rather than rolling by hand.
42 * Revision 1.1 2000/06/17 10:54:29 mdw
43 * Program to generate and verify signatures on multiple files.
47 /*----- Header files ------------------------------------------------------*/
57 #include <mLib/alloc.h>
58 #include <mLib/base64.h>
59 #include <mLib/mdwopt.h>
60 #include <mLib/quis.h>
61 #include <mLib/report.h>
84 /*----- Digital signature algorithm ---------------------------------------*/
86 static int dsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
89 key_packstruct ks
[DSA_PRIVFETCHSZ
];
95 kp
= key_fetchinit(dsa_privfetch
, ks
, &dp
);
96 if ((e
= key_fetch(kp
, k
)) != 0) {
100 sz
= mp_octets(dp
.dp
.q
);
102 die(EXIT_FAILURE
, "hash function too wide for this signing key");
105 rand_get(RAND_GLOBAL
, p
, sz
);
106 dsa_sign(&dp
.dp
, dp
.x
, m
, msz
, p
, sz
, p
, sz
, p
+ sz
, sz
);
112 static int dsaverify(key
*k
, const void *m
, size_t msz
,
113 const void *s
, size_t ssz
)
116 key_packstruct ks
[DSA_PUBFETCHSZ
];
121 kp
= key_fetchinit(dsa_pubfetch
, ks
, &dp
);
122 if ((e
= key_fetch(kp
, k
)) != 0) {
127 e
= dsa_verify(&dp
.dp
, dp
.y
, m
, msz
, s
, sz
, s
+ sz
, sz
);
132 /*----- RSA signing -------------------------------------------------------*/
134 static int rsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
138 pkcs1 pk
= { 0, 0, 0 };
139 key_packstruct ks
[RSA_PRIVFETCHSZ
];
143 kp
= key_fetchinit(rsa_privfetch
, ks
, &rp
);
144 if ((e
= key_fetch(kp
, k
)) != 0) {
148 rsa_privcreate(&rpc
, &rp
, &rand_global
);
149 if (rsa_sign(&rpc
, m
, msz
, d
, pkcs1_sigencode
, &pk
) < 0)
150 die(EXIT_FAILURE
, "internal error in rsasign (key too small?)");
151 rsa_privdestroy(&rpc
);
156 static int rsaverify(key
*k
, const void *m
, size_t msz
,
157 const void *s
, size_t ssz
)
161 pkcs1 pk
= { 0, 0, 0 };
162 key_packstruct ks
[RSA_PUBFETCHSZ
];
168 kp
= key_fetchinit(rsa_pubfetch
, ks
, &rp
);
169 if ((e
= key_fetch(kp
, k
)) != 0) {
173 rsa_pubcreate(&rpc
, &rp
);
174 if (rsa_verify(&rpc
, s
, ssz
, &d
, pkcs1_sigdecode
, &pk
) > 0 &&
175 msz
== d
.len
&& memcmp(d
.buf
, m
, msz
) == 0)
178 rsa_pubdestroy(&rpc
);
183 /*----- Algorithm choice --------------------------------------------------*/
188 int (*sign
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/, dstr */
*d*/
);
189 int (*verify
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/,
190 const void */
*s*/
, size_t /*ssz*/);
193 static const gchash
*hashtab
[] = {
194 &rmd160
, &tiger
, &sha
, &rmd128
, &rmd256
, &rmd320
, &md5
, &md4
, 0 };
195 static sig sigtab
[] = {
196 { "dsa", "dsig-dsa", dsasign
, dsaverify
},
197 { "rsa", "dsig-rsa", rsasign
, rsaverify
},
201 /* --- @gethash@ --- *
203 * Arguments: @const char *name@ = pointer to name string
205 * Returns: Pointer to appropriate hash class.
207 * Use: Chooses a hash function by name.
210 static const gchash
*gethash(const char *name
)
212 const gchash
**g
, *gg
= 0;
213 size_t sz
= strlen(name
);
214 for (g
= hashtab
; *g
; g
++) {
215 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
216 if ((*g
)->name
[sz
] == 0) {
228 /* --- @getsig@ --- *
230 * Arguments: @const char *name@ = pointer to name string
232 * Returns: Pointer to appropriate signature type.
234 * Use: Chooses a signature algorithm by name.
237 static sig
*getsig(const char *name
)
240 size_t sz
= strlen(name
);
241 for (s
= sigtab
; s
->name
; s
++) {
242 if (strncmp(name
, s
->name
, sz
) == 0) {
243 if (s
->name
[sz
] == 0) {
255 /*----- Data formatting ---------------------------------------------------*/
257 /* --- Binary data structure --- *
259 * The binary format, which is used for hashing and for the optional binary
260 * output, consists of a sequence of tagged blocks. The tag describes the
261 * format and meaining of the following data.
265 /* --- Block tags --- */
267 T_IDENT
= 0, /* An identifying marker */
268 T_SIGALG
, /* Signature algorithm used */
269 T_HASHALG
, /* Hash algorithm used */
270 T_KEYID
, /* Key identifier */
271 T_BEGIN
, /* Begin hashing here */
272 T_COMMENT
= T_BEGIN
, /* A textual comment */
273 T_DATE
, /* Creation date of signature */
274 T_EXPIRE
, /* Expiry date of signature */
275 T_FILE
, /* File and corresponding hash */
276 T_SIGNATURE
, /* Final signature block */
278 /* --- Error messages --- */
286 /* --- Name translation table --- */
288 static const char *tagtab
[] = {
289 "ident:", "sigalg:", "hashalg:", "keyid:",
290 "comment:", "date:", "expires:", "file:",
295 static const char *errtab
[] = {
297 "Unexpected end-of-file",
298 "Binary object too large",
303 /* --- Memory representation of block types --- */
305 typedef struct block
{
306 int tag
; /* Type tag */
307 dstr d
; /* String data */
308 dstr b
; /* Binary data */
309 time_t t
; /* Timestamp */
310 uint32 k
; /* Keyid */
313 /* --- @getstring@ --- *
315 * Arguments: @FILE *fp@ = stream from which to read
316 * @dstr *d@ = destination string
317 * @unsigned raw@ = raw or cooked read
319 * Returns: Zero if OK, nonzero on end-of-file.
321 * Use: Reads a filename (or something similar) from a stream.
324 static int getstring(FILE *fp
, dstr
*d
, unsigned raw
)
329 /* --- Raw: just read exactly what's written up to a null byte --- */
332 if ((ch
= getc(fp
)) == EOF
)
338 if ((ch
= getc(fp
)) == EOF
)
345 /* --- Skip as far as whitespace --- *
347 * Also skip past comments.
355 do ch
= getc(fp
); while (ch
!= '\n' && ch
!= EOF
);
361 /* --- If the character is a quote then read a quoted string --- */
373 /* --- Now read all sorts of interesting things --- */
377 /* --- Handle an escaped thing --- */
384 case 'a': ch
= '\a'; break;
385 case 'b': ch
= '\b'; break;
386 case 'f': ch
= '\f'; break;
387 case 'n': ch
= '\n'; break;
388 case 'r': ch
= '\r'; break;
389 case 't': ch
= '\t'; break;
390 case 'v': ch
= '\v'; break;
397 /* --- If it's a quote or some other end marker then stop --- */
399 if (ch
== q
|| (!q
&& isspace((unsigned char)ch
)))
402 /* --- Otherwise contribute and continue --- */
405 if ((ch
= getc(fp
)) == EOF
)
415 /* --- @putstring@ --- *
417 * Arguments: @FILE *fp@ = stream to write on
418 * @const char *p@ = pointer to text
419 * @unsigned raw@ = whether the string is to be written raw
423 * Use: Emits a string to a stream.
426 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
428 size_t sz
= strlen(p
);
432 /* --- Just write the string null terminated if raw --- */
435 fwrite(p
, 1, sz
+ 1, fp
);
439 /* --- Check for any dodgy characters --- */
442 for (q
= p
; *q
; q
++) {
443 if (isspace((unsigned char)*q
)) {
452 /* --- Emit the string --- */
454 for (q
= p
; *q
; q
++) {
456 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
457 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
458 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
459 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
460 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
461 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
462 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
463 case '`': fputc('\\', fp
); fputc('`', fp
); break;
464 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
465 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
478 /* --- @timestring@ --- *
480 * Arguments: @time_t t@ = a timestamp
481 * @dstr *d@ = a string to write on
485 * Use: Writes a textual representation of the timestamp to the
489 static void timestring(time_t t
, dstr
*d
)
491 if (t
== KEXP_FOREVER
)
494 struct tm
*tm
= localtime(&t
);
496 d
->len
+= strftime(d
->buf
+ d
->len
, 32, "%Y-%m-%d %H:%M:%S %Z", tm
);
501 /* --- @breset@ --- *
503 * Arguments: @block *b@ = block to reset
507 * Use: Resets a block so that more stuff can be put in it.
510 static void breset(block
*b
)
521 * Arguments: @block *b@ = block to initialize
525 * Use: Initializes a block as something to read into.
528 static void binit(block
*b
)
535 /* --- @bdestroy@ --- *
537 * Arguments: @block *b@ = block to destroy
541 * Use: Destroys a block's contents.
544 static void bdestroy(block
*b
)
552 * Arguments: @block *b@ = pointer to block
553 * @FILE *fp@ = stream to read from
554 * @unsigned bin@ = binary switch
556 * Returns: Tag of block, or an error tag.
558 * Use: Reads a block from a stream.
561 static int bget(block
*b
, FILE *fp
, unsigned bin
)
565 /* --- Read the tag --- */
571 if (getstring(fp
, &d
, 0))
573 for (tag
= 0; tagtab
[tag
]; tag
++) {
574 if (strcmp(tagtab
[tag
], d
.buf
) == 0)
581 /* --- Decide what to do next --- */
587 /* --- Reading of strings --- */
593 if (getstring(fp
, &b
->d
, bin
))
597 /* --- Timestamps --- */
603 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
605 b
->t
= ((time_t)(((LOAD32(buf
+ 0) << 16) << 16) & ~MASK32
) |
606 (time_t)LOAD32(buf
+ 4));
608 if (getstring(fp
, &b
->d
, 0))
610 if (strcmp(b
->d
.buf
, "forever") == 0)
612 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
617 /* --- Key ids --- */
622 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
626 if (getstring(fp
, &b
->d
, 0))
628 b
->k
= strtoul(b
->d
.buf
, 0, 16);
632 /* --- Reading of binary data --- */
639 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
645 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
650 if (getstring(fp
, &b
->d
, 0))
653 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
654 base64_decode(&b64
, 0, 0, &b
->b
);
657 if (tag
== T_FILE
&& getstring(fp
, &b
->d
, bin
))
661 /* --- Anything else --- */
672 * Arguments: @block *b@ = pointer to block to emit
673 * @dstr *d@ = output buffer
677 * Use: Encodes a block in a binary format.
680 static void blob(block
*b
, dstr
*d
)
694 STORE32(d
->buf
+ d
->len
, ((b
->t
& ~MASK32
) >> 16) >> 16);
695 STORE32(d
->buf
+ d
->len
+ 4, b
->t
);
700 STORE32(d
->buf
+ d
->len
, b
->k
);
706 STORE16(d
->buf
+ d
->len
, b
->b
.len
);
709 if (b
->tag
== T_FILE
) {
717 /* --- @bwrite@ --- *
719 * Arguments: @block *b@ = pointer to block to write
720 * @FILE *fp@ = stream to write on
724 * Use: Writes a block on a stream in a textual format.
727 static void bwrite(block
*b
, FILE *fp
)
729 fputs(tagtab
[b
->tag
], fp
);
736 putstring(fp
, b
->d
.buf
, 0);
741 timestring(b
->t
, &d
);
742 putstring(fp
, d
.buf
, 0);
746 fprintf(fp
, "%08lx", (unsigned long)b
->k
);
754 base64_encode(&b64
, b
->b
.buf
, b
->b
.len
, &d
);
755 base64_encode(&b64
, 0, 0, &d
);
757 if (b
->tag
== T_FILE
) {
759 putstring(fp
, b
->d
.buf
, 0);
768 * Arguments: @block *b@ = pointer to block to write
769 * @FILE *fp@ = file to write on
770 * @ghash *h@ = pointer to hash function
771 * @unsigned bin@ = binary/text flag
775 * Use: Spits out a block properly.
778 static void bemit(block
*b
, FILE *fp
, ghash
*h
, unsigned bin
)
780 if (h
|| (fp
&& bin
)) {
784 h
->ops
->hash(h
, d
.buf
, d
.len
);
786 fwrite(d
.buf
, d
.len
, 1, fp
);
792 /*----- Static variables --------------------------------------------------*/
794 static const char *keyring
= "keyring";
796 /*----- Other shared functions --------------------------------------------*/
798 /* --- @keyreport@ --- *
800 * Arguments: @const char *file@ = filename containing the error
801 * @int line@ = line number in file
802 * @const char *err@ = error text message
803 * @void *p@ = unimportant pointer
807 * Use: Reports errors during the opening of a key file.
810 static void keyreport(const char *file
, int line
, const char *err
, void *p
)
812 moan("error in keyring `%s' at line `%s': %s", file
, line
, err
);
817 * Arguments: @const gchash *c@ = pointer to hash class
818 * @const char *file@ = file to hash
819 * @octet *b@ = pointer to output buffer
821 * Returns: Zero if it worked, or nonzero for a system error.
823 * Use: Hashes a file.
826 static int fhash(const gchash
*c
, const char *file
, octet
*b
)
828 FILE *fp
= fopen(file
, "rb");
829 ghash
*h
= c
->init();
836 while ((sz
= fread(buf
, 1, sizeof(buf
), fp
)) > 0)
837 h
->ops
->hash(h
, buf
, sz
);
848 * Arguments: @FILE *fp@ = file to write on
849 * @const void *p@ = pointer to data to be written
850 * @size_t sz@ = size of the data to write
854 * Use: Emits a hex dump to a stream.
857 static void fhex(FILE *fp
, const void *p
, size_t sz
)
863 fprintf(fp
, "%02x", *q
++);
871 /*----- Signature generation ----------------------------------------------*/
873 static int sign(int argc
, char *argv
[])
886 const sig
*s
= sigtab
;
887 const gchash
*gch
= &rmd160
;
889 time_t exp
= KEXP_EXPIRE
;
891 const char *ifile
= 0;
892 const char *ofile
= 0;
900 static struct option opts
[] = {
901 { "null", 0, 0, '0' },
902 { "binary", 0, 0, 'b' },
903 { "verbose", 0, 0, 'v' },
904 { "quiet", 0, 0, 'q' },
905 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
906 { "hash", OPTF_ARGREQ
, 0, 'h' },
907 { "comment", OPTF_ARGREQ
, 0, 'c' },
908 { "file", OPTF_ARGREQ
, 0, 'f' },
909 { "output", OPTF_ARGREQ
, 0, 'o' },
910 { "keytype", OPTF_ARGREQ
, 0, 't' },
911 { "keyid", OPTF_ARGREQ
, 0, 'i' },
912 { "key", OPTF_ARGREQ
, 0, 'i' },
913 { "expire", OPTF_ARGREQ
, 0, 'e' },
916 int i
= mdwopt(argc
, argv
, "+0vqb a:h:c: f:o: t:i:k:e:", opts
, 0, 0, 0);
934 if ((s
= getsig(optarg
)) == 0) {
935 die(EXIT_FAILURE
, "unknown or ambiguous signature algorithm `%s'",
940 if ((gch
= gethash(optarg
)) == 0) {
941 die(EXIT_FAILURE
, "unknown or ambiguous hash function `%s'",
962 if (strcmp(optarg
, "forever") == 0)
964 else if ((exp
= get_date(optarg
, 0)) == -1)
965 die(EXIT_FAILURE
, "bad expiry time");
972 if (optind
!= argc
|| (f
& f_bogus
))
973 die(EXIT_FAILURE
, "Usage: sign [-options]");
975 /* --- Locate the signing key --- */
977 if (key_open(&kf
, keyring
, KOPEN_WRITE
, keyreport
, 0))
978 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
980 if ((k
= key_bytag(&kf
, ki
)) == 0)
981 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
985 if ((k
= key_bytype(&kf
, kt
)) == 0)
986 die(EXIT_FAILURE
, "no appropriate key of type `%s'", kt
);
989 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
990 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
994 /* --- Open files --- */
998 else if ((ifp
= fopen(ifile
, (f
& f_raw
) ?
"rb" : "r")) == 0) {
999 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
1000 ifile
, strerror(errno
));
1005 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
1006 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
1007 ofile
, strerror(errno
));
1010 /* --- Emit the start of the output --- */
1012 binit(&b
); b
.tag
= T_IDENT
;
1013 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
1014 bemit(&b
, ofp
, 0, f
& f_bin
);
1016 breset(&b
); b
.tag
= T_SIGALG
; DPUTS(&b
.d
, s
->name
);
1017 bemit(&b
, ofp
, 0, f
& f_bin
);
1019 breset(&b
); b
.tag
= T_HASHALG
; DPUTS(&b
.d
, gch
->name
);
1020 bemit(&b
, ofp
, 0, f
& f_bin
);
1022 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
1023 bemit(&b
, ofp
, 0, f
& f_bin
);
1025 /* --- Start hashing, and emit the datestamps and things --- */
1028 time_t now
= time(0);
1031 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, h
, f
& f_bin
);
1032 if (exp
== KEXP_EXPIRE
)
1033 exp
= now
+ 86400 * 28;
1034 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, h
, f
& f_bin
);
1036 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
1037 bemit(&b
, ofp
, h
, f
& f_bin
);
1044 /* --- Now hash the various files --- */
1048 /* --- Stop on an output error --- */
1055 /* --- Read the next filename to hash --- */
1058 if (getstring(ifp
, &b
.d
, f
& f_raw
))
1061 DENSURE(&b
.b
, h
->ops
->c
->hashsz
);
1062 if (fhash(gch
, b
.d
.buf
, b
.b
.buf
)) {
1063 moan("Error reading `%s': %s", b
.d
.buf
, strerror(errno
));
1066 b
.b
.len
+= h
->ops
->c
->hashsz
;
1068 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
1069 fprintf(stderr
, " %s\n", b
.d
.buf
);
1071 bemit(&b
, ofp
, h
, f
& f_bin
);
1075 /* --- Create the signature --- */
1077 if (!(f
& f_bogus
)) {
1079 b
.tag
= T_SIGNATURE
;
1080 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1081 h
->ops
->done(h
, b
.d
.buf
);
1082 if ((e
= s
->sign(k
, b
.d
.buf
, h
->ops
->c
->hashsz
, &b
.b
)) != 0) {
1083 moan("error creating signature: %s", key_strerror(e
));
1086 if (!(f
& f_bogus
)) {
1087 bemit(&b
, ofp
, 0, f
& f_bin
);
1088 key_used(&kf
, k
, exp
);
1092 /* --- Tidy up at the end --- */
1104 if ((e
= key_close(&kf
)) != 0) {
1107 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
1108 keyring
, strerror(errno
));
1110 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
1111 keyring
, strerror(errno
));
1115 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
1116 return (EXIT_SUCCESS
);
1119 /*----- Signature verification --------------------------------------------*/
1121 static int verify(int argc
, char *argv
[])
1134 const gchash
*gch
= &rmd160
;
1141 /* --- Parse the options --- */
1144 static struct option opts
[] = {
1145 { "verbose", 0, 0, 'v' },
1146 { "quiet", 0, 0, 'q' },
1149 int i
= mdwopt(argc
, argv
, "+vq", opts
, 0, 0, 0);
1167 if ((f
& f_bogus
) || argc
> 1)
1168 die(EXIT_FAILURE
, "Usage: verify [-qv] [file]");
1170 /* --- Open the key file, and start reading the input file --- */
1172 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
1173 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
1177 if ((fp
= fopen(argv
[0], "rb")) == 0) {
1178 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1179 argv
[0], strerror(errno
));
1181 if (getc(fp
) == 0) {
1186 if ((fp
= fopen(argv
[0], "r")) == 0) {
1187 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1188 argv
[0], strerror(errno
));
1193 /* --- Read the introductory matter --- */
1198 e
= bget(&b
, fp
, f
& f_bin
);
1200 die(EXIT_FAILURE
, "error reading packet: %s\n", errtab
[-e
]);
1206 printf("INFO ident: `%s'\n", b
.d
.buf
);
1209 if ((s
= getsig(b
.d
.buf
)) == 0) {
1211 printf("FAIL unknown signature algorithm `%s'\n", b
.d
.buf
);
1215 printf("INFO signature algorithm: %s\n", s
->name
);
1218 if ((gch
= gethash(b
.d
.buf
)) == 0) {
1220 printf("FAIL unknown hash function `%s'\n", b
.d
.buf
);
1224 printf("INFO hash function algorithm: %s\n", gch
->name
);
1227 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
1229 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
1234 key_fulltag(k
, &b
.d
);
1235 printf("INFO key: %s\n", b
.d
.buf
);
1239 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
1244 /* --- Initialize the hash function and start reading hashed packets --- */
1249 puts("FAIL no keyid packet found");
1256 printf("INFO comment: `%s'\n", b
.d
.buf
);
1262 timestring(b
.t
, &b
.d
);
1263 printf("INFO date: %s\n", b
.d
.buf
);
1268 time_t now
= time(0);
1271 puts("BAD signature has expired");
1276 timestring(b
.t
, &b
.d
);
1277 printf("INFO expires: %s\n", b
.d
.buf
);
1283 DENSURE(&d
, gch
->hashsz
);
1284 if (fhash(gch
, b
.d
.buf
, d
.buf
)) {
1286 printf("BAD error reading file `%s': %s\n",
1287 b
.d
.buf
, strerror(errno
));
1290 } else if (b
.b
.len
!= gch
->hashsz
||
1291 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
1293 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
1295 } else if (verb
> 3) {
1296 fputs("INFO hash: ", stdout
);
1297 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
1298 printf(" %s\n", b
.d
.buf
);
1304 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1305 b
.d
.len
+= h
->ops
->c
->hashsz
;
1306 h
->ops
->done(h
, b
.d
.buf
);
1307 e
= s
->verify(k
, b
.d
.buf
, b
.d
.len
, b
.b
.buf
, b
.b
.len
);
1311 printf("BAD error unpacking key: %s\n", key_strerror(e
));
1313 puts("BAD bad signature");
1316 } else if (verb
> 2)
1317 puts("INFO good signature");
1321 printf("FAIL invalid packet type %i\n", e
);
1326 e
= bget(&b
, fp
, f
& f_bin
);
1329 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
1341 puts("FAIL signature invalid");
1343 puts("OK signature verified");
1345 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
1348 /*----- Main code ---------------------------------------------------------*/
1350 typedef struct cmd
{
1352 int (*func
)(int /*argc*/, char */
*argv*/
[]);
1356 static cmd cmdtab
[] = {
1357 /* { "manifest", manifest, */
1358 /* "manifest [-0] [-o output]" }, */
1361 [-0v] [-a alg] [-h hash] [-t keytype] [-i keyid]\n\
1362 [-e expire] [-f file] [-o output]" },
1364 "verify [-qv] [file]" },
1368 static void version(FILE *fp
)
1370 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1373 static void usage(FILE *fp
)
1375 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1378 static void help(FILE *fp
)
1385 Create and verify signatures on lists of files.\n\
1387 for (c
= cmdtab
; c
->name
; c
++)
1388 fprintf(fp
, "%s\n", c
->help
);
1393 * Arguments: @int argc@ = number of command line arguments
1394 * @char *argv[]@ = vector of command line arguments
1396 * Returns: Zero if successful, nonzero otherwise.
1398 * Use: Signs or verifies signatures on lists of files. Useful for
1399 * ensuring that a distribution is unmolested.
1402 int main(int argc
, char *argv
[])
1405 cmd
*c
= 0, *cc
= 0;
1412 /* --- Initialize the library --- */
1416 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1417 rand_seed(RAND_GLOBAL
, 160);
1419 /* --- Parse options --- */
1422 static struct option opts
[] = {
1423 { "help", 0, 0, 'h' },
1424 { "version", 0, 0, 'v' },
1425 { "usage", 0, 0, 'u' },
1426 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1429 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1456 if (f
& f_bogus
|| argc
< 1) {
1461 /* --- Dispatch to the correct subcommand handler --- */
1463 n
= strlen(argv
[0]);
1464 for (c
= cmdtab
; c
->name
; c
++) {
1465 if (strncmp(argv
[0], c
->name
, n
) == 0) {
1466 if (c
->name
[n
] == 0) {
1470 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1476 die(EXIT_FAILURE
, "unknown command `%s'", argv
[0]);
1477 return (cc
->func(argc
, argv
));
1480 /*----- That's all, folks -------------------------------------------------*/