3 * $Id: dsig.c,v 1.2 2000/07/01 11:27:22 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.2 2000/07/01 11:27:22 mdw
34 * Use new PKCS#1 padding functions rather than rolling by hand.
36 * Revision 1.1 2000/06/17 10:54:29 mdw
37 * Program to generate and verify signatures on multiple files.
41 /*----- Header files ------------------------------------------------------*/
51 #include <mLib/alloc.h>
52 #include <mLib/base64.h>
53 #include <mLib/mdwopt.h>
54 #include <mLib/quis.h>
55 #include <mLib/report.h>
71 /*----- Digital signature algorithm ---------------------------------------*/
73 static int dsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
76 key_packstruct ks
[DSA_PRIVFETCHSZ
];
82 kp
= key_fetchinit(dsa_privfetch
, ks
, &dp
);
83 if ((e
= key_fetch(kp
, k
)) != 0) {
87 sz
= mp_octets(dp
.dp
.q
);
90 rand_get(RAND_GLOBAL
, p
, sz
);
91 dsa_sign(&dp
.dp
, dp
.x
, m
, msz
, p
, sz
, p
, sz
, p
+ sz
, sz
);
97 static int dsaverify(key
*k
, const void *m
, size_t msz
,
98 const void *s
, size_t ssz
)
101 key_packstruct ks
[DSA_PUBFETCHSZ
];
106 kp
= key_fetchinit(dsa_pubfetch
, ks
, &dp
);
107 if ((e
= key_fetch(kp
, k
)) != 0) {
112 e
= dsa_verify(&dp
.dp
, dp
.y
, m
, msz
, s
, sz
, s
+ sz
, sz
);
117 /*----- RSA signing -------------------------------------------------------*/
119 static int rsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
123 pkcs1 pk
= { 0, 0, 0 };
124 key_packstruct ks
[RSA_PRIVFETCHSZ
];
128 kp
= key_fetchinit(rsa_privfetch
, ks
, &rp
);
129 if ((e
= key_fetch(kp
, k
)) != 0) {
133 rsa_privcreate(&rpc
, &rp
, &rand_global
);
134 if (rsa_sign(&rpc
, m
, msz
, d
, pkcs1_sigencode
, &pk
) < 0)
135 die(EXIT_FAILURE
, "internal error in rsasign (key too small?)");
136 rsa_privdestroy(&rpc
);
141 static int rsaverify(key
*k
, const void *m
, size_t msz
,
142 const void *s
, size_t ssz
)
146 pkcs1 pk
= { 0, 0, 0 };
147 key_packstruct ks
[RSA_PUBFETCHSZ
];
153 kp
= key_fetchinit(rsa_pubfetch
, ks
, &rp
);
154 if ((e
= key_fetch(kp
, k
)) != 0) {
158 rsa_pubcreate(&rpc
, &rp
);
159 if (rsa_verify(&rpc
, s
, ssz
, &d
, pkcs1_sigdecode
, &pk
) > 0 &&
160 msz
== d
.len
&& memcmp(d
.buf
, m
, msz
) == 0)
163 rsa_pubdestroy(&rpc
);
168 /*----- Algorithm choice --------------------------------------------------*/
173 int (*sign
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/, dstr */
*d*/
);
174 int (*verify
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/,
175 const void */
*s*/
, size_t /*ssz*/);
178 static const gchash
*hashtab
[] = { &rmd160
, &sha
, &md5
, 0 };
179 static sig sigtab
[] = {
180 { "dsa", "dsig-dsa", dsasign
, dsaverify
},
181 { "rsa", "dsig-rsa", rsasign
, rsaverify
},
185 /* --- @gethash@ --- *
187 * Arguments: @const char *name@ = pointer to name string
189 * Returns: Pointer to appropriate hash class.
191 * Use: Chooses a hash function by name.
194 static const gchash
*gethash(const char *name
)
196 const gchash
**g
, *gg
= 0;
197 size_t sz
= strlen(name
);
198 for (g
= hashtab
; *g
; g
++) {
199 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
200 if ((*g
)->name
[sz
] == 0) {
212 /* --- @getsig@ --- *
214 * Arguments: @const char *name@ = pointer to name string
216 * Returns: Pointer to appropriate signature type.
218 * Use: Chooses a signature algorithm by name.
221 static sig
*getsig(const char *name
)
224 size_t sz
= strlen(name
);
225 for (s
= sigtab
; s
->name
; s
++) {
226 if (strncmp(name
, s
->name
, sz
) == 0) {
227 if (s
->name
[sz
] == 0) {
239 /*----- Data formatting ---------------------------------------------------*/
241 /* --- Binary data structure --- *
243 * The binary format, which is used for hashing and for the optional binary
244 * output, consists of a sequence of tagged blocks. The tag describes the
245 * format and meaining of the following data.
249 /* --- Block tags --- */
251 T_IDENT
= 0, /* An identifying marker */
252 T_SIGALG
, /* Signature algorithm used */
253 T_HASHALG
, /* Hash algorithm used */
254 T_KEYID
, /* Key identifier */
255 T_BEGIN
, /* Begin hashing here */
256 T_COMMENT
= T_BEGIN
, /* A textual comment */
257 T_DATE
, /* Creation date of signature */
258 T_EXPIRE
, /* Expiry date of signature */
259 T_FILE
, /* File and corresponding hash */
260 T_SIGNATURE
, /* Final signature block */
262 /* --- Error messages --- */
270 /* --- Name translation table --- */
272 static const char *tagtab
[] = {
273 "ident:", "sigalg:", "hashalg:", "keyid:",
274 "comment:", "date:", "expires:", "file:",
279 static const char *errtab
[] = {
281 "Unexpected end-of-file",
282 "Binary object too large",
287 /* --- Memory representation of block types --- */
289 typedef struct block
{
290 int tag
; /* Type tag */
291 dstr d
; /* String data */
292 dstr b
; /* Binary data */
293 time_t t
; /* Timestamp */
294 uint32 k
; /* Keyid */
297 /* --- @getstring@ --- *
299 * Arguments: @FILE *fp@ = stream from which to read
300 * @dstr *d@ = destination string
301 * @unsigned raw@ = raw or cooked read
303 * Returns: Zero if OK, nonzero on end-of-file.
305 * Use: Reads a filename (or something similar) from a stream.
308 static int getstring(FILE *fp
, dstr
*d
, unsigned raw
)
313 /* --- Raw: just read exactly what's written up to a null byte --- */
316 if ((ch
= getc(fp
)) == EOF
)
322 if ((ch
= getc(fp
)) == EOF
)
329 /* --- Skip as far as whitespace --- *
331 * Also skip past comments.
336 while (isspace((unsigned char)ch
))
339 do ch
= getc(fp
); while (ch
!= '\n' && ch
!= EOF
);
345 /* --- If the character is a quote then read a quoted string --- */
357 /* --- Now read all sorts of interesting things --- */
361 /* --- Handle an escaped thing --- */
368 case 'a': ch
= '\n'; break;
369 case 'b': ch
= '\b'; break;
370 case 'f': ch
= '\f'; break;
371 case 'n': ch
= '\n'; break;
372 case 'r': ch
= '\r'; break;
373 case 't': ch
= '\t'; break;
374 case 'v': ch
= '\v'; break;
380 /* --- If it's a quote or some other end marker then stop --- */
382 if (ch
== q
|| (!q
&& isspace((unsigned char)ch
)))
385 /* --- Otherwise contribute and continue --- */
388 if ((ch
= getc(fp
)) == EOF
)
398 /* --- @putstring@ --- *
400 * Arguments: @FILE *fp@ = stream to write on
401 * @const char *p@ = pointer to text
402 * @unsigned raw@ = whether the string is to be written raw
406 * Use: Emits a string to a stream.
409 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
411 size_t sz
= strlen(p
);
415 /* --- Just write the string null terminated if raw --- */
418 fwrite(p
, 1, sz
+ 1, fp
);
422 /* --- Check for any dodgy characters --- */
425 for (q
= p
; *q
; q
++) {
426 if (isspace((unsigned char)*q
)) {
435 /* --- Emit the string --- */
437 for (q
= p
; *q
; q
++) {
439 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
440 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
441 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
442 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
443 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
444 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
445 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
446 case '`': fputc('\\', fp
); fputc('`', fp
); break;
447 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
448 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
461 /* --- @timestring@ --- *
463 * Arguments: @time_t t@ = a timestamp
464 * @dstr *d@ = a string to write on
468 * Use: Writes a textual representation of the timestamp to the
472 static void timestring(time_t t
, dstr
*d
)
474 if (t
== KEXP_FOREVER
)
477 struct tm
*tm
= localtime(&t
);
479 d
->len
+= strftime(d
->buf
+ d
->len
, 32, "%Y-%m-%d %H:%M:%S %Z", tm
);
484 /* --- @breset@ --- *
486 * Arguments: @block *b@ = block to reset
490 * Use: Resets a block so that more stuff can be put in it.
493 static void breset(block
*b
)
504 * Arguments: @block *b@ = block to initialize
508 * Use: Initializes a block as something to read into.
511 static void binit(block
*b
)
518 /* --- @bdestroy@ --- *
520 * Arguments: @block *b@ = block to destroy
524 * Use: Destroys a block's contents.
527 static void bdestroy(block
*b
)
535 * Arguments: @block *b@ = pointer to block
536 * @FILE *fp@ = stream to read from
537 * @unsigned bin@ = binary switch
539 * Returns: Tag of block, or an error tag.
541 * Use: Reads a block from a stream.
544 static int bget(block
*b
, FILE *fp
, unsigned bin
)
548 /* --- Read the tag --- */
554 if (getstring(fp
, &d
, 0))
556 for (tag
= 0; tagtab
[tag
]; tag
++) {
557 if (strcmp(tagtab
[tag
], d
.buf
) == 0)
564 /* --- Decide what to do next --- */
570 /* --- Reading of strings --- */
576 if (getstring(fp
, &b
->d
, bin
))
580 /* --- Timestamps --- */
586 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
588 b
->t
= ((time_t)(((LOAD32(buf
+ 0) << 16) << 16) & ~MASK32
) |
589 (time_t)LOAD32(buf
+ 4));
591 if (getstring(fp
, &b
->d
, 0))
593 if (strcmp(b
->d
.buf
, "forever") == 0)
595 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
600 /* --- Key ids --- */
605 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
609 if (getstring(fp
, &b
->d
, 0))
611 b
->k
= strtoul(b
->d
.buf
, 0, 16);
615 /* --- Reading of binary data --- */
622 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
628 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
633 if (getstring(fp
, &b
->d
, 0))
636 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
637 base64_decode(&b64
, 0, 0, &b
->b
);
640 if (tag
== T_FILE
&& getstring(fp
, &b
->d
, bin
))
644 /* --- Anything else --- */
655 * Arguments: @block *b@ = pointer to block to emit
656 * @dstr *d@ = output buffer
660 * Use: Encodes a block in a binary format.
663 static void blob(block
*b
, dstr
*d
)
677 STORE32(d
->buf
+ d
->len
, ((b
->t
& ~MASK32
) >> 16) >> 16);
678 STORE32(d
->buf
+ d
->len
+ 4, b
->t
);
683 STORE32(d
->buf
+ d
->len
, b
->k
);
689 STORE16(d
->buf
+ d
->len
, b
->b
.len
);
692 if (b
->tag
== T_FILE
) {
700 /* --- @bwrite@ --- *
702 * Arguments: @block *b@ = pointer to block to write
703 * @FILE *fp@ = stream to write on
707 * Use: Writes a block on a stream in a textual format.
710 static void bwrite(block
*b
, FILE *fp
)
712 fputs(tagtab
[b
->tag
], fp
);
719 putstring(fp
, b
->d
.buf
, 0);
724 timestring(b
->t
, &d
);
725 putstring(fp
, d
.buf
, 0);
729 fprintf(fp
, "%08lx", (unsigned long)b
->k
);
737 base64_encode(&b64
, b
->b
.buf
, b
->b
.len
, &d
);
738 base64_encode(&b64
, 0, 0, &d
);
740 if (b
->tag
== T_FILE
) {
742 putstring(fp
, b
->d
.buf
, 0);
751 * Arguments: @block *b@ = pointer to block to write
752 * @FILE *fp@ = file to write on
753 * @ghash *h@ = pointer to hash function
754 * @unsigned bin@ = binary/text flag
758 * Use: Spits out a block properly.
761 static void bemit(block
*b
, FILE *fp
, ghash
*h
, unsigned bin
)
763 if (h
|| (fp
&& bin
)) {
767 h
->ops
->hash(h
, d
.buf
, d
.len
);
769 fwrite(d
.buf
, d
.len
, 1, fp
);
775 /*----- Static variables --------------------------------------------------*/
777 static const char *keyring
= "keyring";
779 /*----- Other shared functions --------------------------------------------*/
781 /* --- @keyreport@ --- *
783 * Arguments: @const char *file@ = filename containing the error
784 * @int line@ = line number in file
785 * @const char *err@ = error text message
786 * @void *p@ = unimportant pointer
790 * Use: Reports errors during the opening of a key file.
793 static void keyreport(const char *file
, int line
, const char *err
, void *p
)
795 moan("error in keyring `%s' at line `%s': %s", file
, line
, err
);
800 * Arguments: @const gchash *c@ = pointer to hash class
801 * @const char *file@ = file to hash
802 * @octet *b@ = pointer to output buffer
804 * Returns: Zero if it worked, or nonzero for a system error.
806 * Use: Hashes a file.
809 static int fhash(const gchash
*c
, const char *file
, octet
*b
)
811 FILE *fp
= fopen(file
, "rb");
812 ghash
*h
= c
->init();
819 while ((sz
= fread(buf
, 1, sizeof(buf
), fp
)) > 0)
820 h
->ops
->hash(h
, buf
, sz
);
831 * Arguments: @FILE *fp@ = file to write on
832 * @const void *p@ = pointer to data to be written
833 * @size_t sz@ = size of the data to write
837 * Use: Emits a hex dump to a stream.
840 static void fhex(FILE *fp
, const void *p
, size_t sz
)
846 fprintf(fp
, "%02x", *q
++);
854 /*----- Signature generation ----------------------------------------------*/
856 static int sign(int argc
, char *argv
[])
869 const sig
*s
= sigtab
;
870 const gchash
*gch
= &rmd160
;
872 time_t exp
= KEXP_EXPIRE
;
874 const char *ifile
= 0;
875 const char *ofile
= 0;
883 static struct option opts
[] = {
884 { "null", 0, 0, '0' },
885 { "binary", 0, 0, 'b' },
886 { "verbose", 0, 0, 'v' },
887 { "quiet", 0, 0, 'q' },
888 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
889 { "hash", OPTF_ARGREQ
, 0, 'h' },
890 { "comment", OPTF_ARGREQ
, 0, 'c' },
891 { "file", OPTF_ARGREQ
, 0, 'f' },
892 { "output", OPTF_ARGREQ
, 0, 'o' },
893 { "keytype", OPTF_ARGREQ
, 0, 't' },
894 { "keyid", OPTF_ARGREQ
, 0, 'i' },
895 { "key", OPTF_ARGREQ
, 0, 'i' },
896 { "expire", OPTF_ARGREQ
, 0, 'e' },
899 int i
= mdwopt(argc
, argv
, "+0vqb a:h:c: f:o: t:i:k:e:", opts
, 0, 0, 0);
917 if ((s
= getsig(optarg
)) == 0) {
918 die(EXIT_FAILURE
, "unknown or ambiguous signature algorithm `%s'",
923 if ((gch
= gethash(optarg
)) == 0) {
924 die(EXIT_FAILURE
, "unknown or ambiguous hash function `%s'",
945 if (strcmp(optarg
, "forever") == 0)
947 else if ((exp
= get_date(optarg
, 0)) == -1)
948 die(EXIT_FAILURE
, "bad expiry time");
955 if (optind
!= argc
|| (f
& f_bogus
))
956 die(EXIT_FAILURE
, "Usage: sign [-options]");
958 /* --- Locate the signing key --- */
960 if (key_open(&kf
, keyring
, KOPEN_WRITE
, keyreport
, 0))
961 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
963 if ((k
= key_bytag(&kf
, ki
)) == 0)
964 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
968 if ((k
= key_bytype(&kf
, kt
)) == 0)
969 die(EXIT_FAILURE
, "no appropriate key of type `%s'", kt
);
972 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
973 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
977 /* --- Open files --- */
981 else if ((ifp
= fopen(ifile
, (f
& f_raw
) ?
"rb" : "r")) == 0) {
982 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
983 ifile
, strerror(errno
));
988 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
989 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
990 ofile
, strerror(errno
));
993 /* --- Emit the start of the output --- */
995 binit(&b
); b
.tag
= T_IDENT
;
996 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
997 bemit(&b
, ofp
, 0, f
& f_bin
);
999 breset(&b
); b
.tag
= T_SIGALG
; DPUTS(&b
.d
, s
->name
);
1000 bemit(&b
, ofp
, 0, f
& f_bin
);
1002 breset(&b
); b
.tag
= T_HASHALG
; DPUTS(&b
.d
, gch
->name
);
1003 bemit(&b
, ofp
, 0, f
& f_bin
);
1005 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
1006 bemit(&b
, ofp
, 0, f
& f_bin
);
1008 /* --- Start hashing, and emit the datestamps and things --- */
1011 time_t now
= time(0);
1014 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, h
, f
& f_bin
);
1015 if (exp
== KEXP_EXPIRE
)
1016 exp
= now
+ 86400 * 28;
1017 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, h
, f
& f_bin
);
1019 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
1020 bemit(&b
, ofp
, h
, f
& f_bin
);
1027 /* --- Now hash the various files --- */
1031 /* --- Stop on an output error --- */
1038 /* --- Read the next filename to hash --- */
1041 if (getstring(ifp
, &b
.d
, f
& f_raw
))
1044 DENSURE(&b
.b
, h
->ops
->c
->hashsz
);
1045 if (fhash(gch
, b
.d
.buf
, b
.b
.buf
)) {
1046 moan("Error reading `%s': %s", b
.d
.buf
, strerror(errno
));
1049 b
.b
.len
+= h
->ops
->c
->hashsz
;
1051 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
1052 fprintf(stderr
, " %s\n", b
.d
.buf
);
1054 bemit(&b
, ofp
, h
, f
& f_bin
);
1058 /* --- Create the signature --- */
1060 if (!(f
& f_bogus
)) {
1062 b
.tag
= T_SIGNATURE
;
1063 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1064 h
->ops
->done(h
, b
.d
.buf
);
1065 if ((e
= s
->sign(k
, b
.d
.buf
, h
->ops
->c
->hashsz
, &b
.b
)) != 0) {
1066 moan("error creating signature: %s", key_strerror(e
));
1069 if (!(f
& f_bogus
)) {
1070 bemit(&b
, ofp
, 0, f
& f_bin
);
1071 key_used(&kf
, k
, exp
);
1075 /* --- Tidy up at the end --- */
1087 if ((e
= key_close(&kf
)) != 0) {
1090 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
1091 keyring
, strerror(errno
));
1093 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
1094 keyring
, strerror(errno
));
1098 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
1099 return (EXIT_SUCCESS
);
1102 /*----- Signature verification --------------------------------------------*/
1104 static int verify(int argc
, char *argv
[])
1117 const gchash
*gch
= &rmd160
;
1124 /* --- Parse the options --- */
1127 static struct option opts
[] = {
1128 { "verbose", 0, 0, 'v' },
1129 { "quiet", 0, 0, 'q' },
1132 int i
= mdwopt(argc
, argv
, "+vq", opts
, 0, 0, 0);
1150 if ((f
& f_bogus
) || argc
> 1)
1151 die(EXIT_FAILURE
, "Usage: verify [-qv] [file]");
1153 /* --- Open the key file, and start reading the input file --- */
1155 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
1156 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
1160 if ((fp
= fopen(argv
[0], "rb")) == 0) {
1161 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1162 argv
[0], strerror(errno
));
1164 if (getc(fp
) == 0) {
1169 if ((fp
= fopen(argv
[0], "r")) == 0) {
1170 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1171 argv
[0], strerror(errno
));
1176 /* --- Read the introductory matter --- */
1181 e
= bget(&b
, fp
, f
& f_bin
);
1183 die(EXIT_FAILURE
, "error reading packet: %s\n", errtab
[-e
]);
1189 printf("INFO ident: `%s'\n", b
.d
.buf
);
1192 if ((s
= getsig(b
.d
.buf
)) == 0) {
1194 printf("FAIL unknown signature algorithm `%s'\n", b
.d
.buf
);
1198 printf("INFO signature algorithm: %s\n", s
->name
);
1201 if ((gch
= gethash(b
.d
.buf
)) == 0) {
1203 printf("FAIL unknown hash function `%s'\n", b
.d
.buf
);
1207 printf("INFO hash function algorithm: %s\n", gch
->name
);
1210 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
1212 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
1217 key_fulltag(k
, &b
.d
);
1218 printf("INFO key: %s\n", b
.d
.buf
);
1222 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
1227 /* --- Initialize the hash function and start reading hashed packets --- */
1232 puts("FAIL no keyid packet found");
1239 printf("INFO comment: `%s'\n", b
.d
.buf
);
1245 timestring(b
.t
, &b
.d
);
1246 printf("INFO date: %s\n", b
.d
.buf
);
1251 time_t now
= time(0);
1254 puts("BAD signature has expired");
1259 timestring(b
.t
, &b
.d
);
1260 printf("INFO expires: %s\n", b
.d
.buf
);
1266 DENSURE(&d
, gch
->hashsz
);
1267 if (fhash(gch
, b
.d
.buf
, d
.buf
)) {
1269 printf("BAD error reading file `%s': %s\n",
1270 b
.d
.buf
, strerror(errno
));
1273 } else if (b
.b
.len
!= gch
->hashsz
||
1274 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
1276 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
1278 } else if (verb
> 3) {
1279 fputs("INFO hash: ", stdout
);
1280 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
1281 printf(" %s\n", b
.d
.buf
);
1287 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1288 b
.d
.len
+= h
->ops
->c
->hashsz
;
1289 h
->ops
->done(h
, b
.d
.buf
);
1290 e
= s
->verify(k
, b
.d
.buf
, b
.d
.len
, b
.b
.buf
, b
.b
.len
);
1294 printf("BAD error unpacking key: %s\n", key_strerror(e
));
1296 puts("BAD bad signature");
1299 } else if (verb
> 2)
1300 puts("INFO good signature");
1304 printf("FAIL invalid packet type %i\n", e
);
1309 e
= bget(&b
, fp
, f
& f_bin
);
1312 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
1324 puts("FAIL signature invalid");
1326 puts("OK signature verified");
1328 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
1331 /*----- Main code ---------------------------------------------------------*/
1333 typedef struct cmd
{
1335 int (*func
)(int /*argc*/, char */
*argv*/
[]);
1339 static cmd cmdtab
[] = {
1340 /* { "manifest", manifest, */
1341 /* "manifest [-0] [-o output]" }, */
1344 [-0v] [-a alg] [-h hash] [-t keytype] [-i keyid]\n\
1345 [-e expire] [-f file] [-o output]" },
1347 "verify [-qv] [file]" },
1351 static void version(FILE *fp
)
1353 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1356 static void usage(FILE *fp
)
1358 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1361 static void help(FILE *fp
)
1368 Create and verify signatures on lists of files.\n\
1370 for (c
= cmdtab
; c
->name
; c
++)
1371 fprintf(fp
, "%s\n", c
->help
);
1376 * Arguments: @int argc@ = number of command line arguments
1377 * @char *argv[]@ = vector of command line arguments
1379 * Returns: Zero if successful, nonzero otherwise.
1381 * Use: Signs or verifies signatures on lists of files. Useful for
1382 * ensuring that a distribution is unmolested.
1385 int main(int argc
, char *argv
[])
1388 cmd
*c
= 0, *cc
= 0;
1395 /* --- Initialize the library --- */
1399 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1400 rand_seed(RAND_GLOBAL
, 160);
1402 /* --- Parse options --- */
1405 static struct option opts
[] = {
1406 { "help", 0, 0, 'h' },
1407 { "version", 0, 0, 'v' },
1408 { "usage", 0, 0, 'u' },
1409 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1412 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1439 if (f
& f_bogus
|| argc
< 1) {
1444 /* --- Dispatch to the correct subcommand handler --- */
1446 n
= strlen(argv
[0]);
1447 for (c
= cmdtab
; c
->name
; c
++) {
1448 if (strncmp(argv
[0], c
->name
, n
) == 0) {
1449 if (c
->name
[n
] == 0) {
1453 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1459 die(EXIT_FAILURE
, "unknown command `%s'", argv
[0]);
1460 return (cc
->func(argc
, argv
));
1463 /*----- That's all, folks -------------------------------------------------*/