3 * $Id: dsig.c,v 1.1 2000/06/17 10:54:29 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.1 2000/06/17 10:54:29 mdw
34 * Program to generate and verify signatures on multiple files.
38 /*----- Header files ------------------------------------------------------*/
48 #include <mLib/alloc.h>
49 #include <mLib/base64.h>
50 #include <mLib/mdwopt.h>
51 #include <mLib/quis.h>
52 #include <mLib/report.h>
67 /*----- Digital signature algorithm ---------------------------------------*/
69 static int dsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
72 key_packstruct ks
[DSA_PRIVFETCHSZ
];
78 kp
= key_fetchinit(dsa_privfetch
, ks
, &dp
);
79 if ((e
= key_fetch(kp
, k
)) != 0) {
83 sz
= mp_octets(dp
.dp
.q
);
86 rand_get(RAND_GLOBAL
, p
, sz
);
87 dsa_sign(&dp
.dp
, dp
.x
, m
, msz
, p
, sz
, p
, sz
, p
+ sz
, sz
);
93 static int dsaverify(key
*k
, const void *m
, size_t msz
,
94 const void *s
, size_t ssz
)
97 key_packstruct ks
[DSA_PUBFETCHSZ
];
102 kp
= key_fetchinit(dsa_pubfetch
, ks
, &dp
);
103 if ((e
= key_fetch(kp
, k
)) != 0) {
108 e
= dsa_verify(&dp
.dp
, dp
.y
, m
, msz
, s
, sz
, s
+ sz
, sz
);
113 /*----- RSA signing -------------------------------------------------------*/
115 static int rsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
118 key_packstruct ks
[RSA_PRIVFETCHSZ
];
125 kp
= key_fetchinit(rsa_privfetch
, ks
, &rp
);
126 if ((e
= key_fetch(kp
, k
)) != 0) {
130 sz
= mp_octets(rp
.n
);
132 die(EXIT_FAILURE
, "key too small (help!)");
135 memcpy(p
+ sz
- msz
, m
, msz
);
138 memset(p
+ 2, 0xff, sz
- msz
- 3);
140 memcpy(p
+ sz
- msz
, m
, msz
);
141 x
= mp_loadb(MP_NEW
, p
, sz
);
142 x
= rsa_decrypt(&rp
, x
, x
, &rand_global
);
150 static int rsaverify(key
*k
, const void *m
, size_t msz
,
151 const void *s
, size_t ssz
)
154 key_packstruct ks
[RSA_PUBFETCHSZ
];
164 kp
= key_fetchinit(rsa_pubfetch
, ks
, &rp
);
165 if ((e
= key_fetch(kp
, k
)) != 0) {
169 x
= mp_loadb(MP_NEW
, s
, ssz
);
170 mpmont_create(&mm
, rp
.n
);
171 x
= mpmont_exp(&mm
, x
, x
, rp
.e
);
173 sz
= mp_octets(rp
.n
);
178 if (msz
+ 11 >= sz
|| p
[0] != 0 || p
[1] != 1 || p
[sz
- msz
- 1] != 0)
180 for (i
= 2; i
< sz
- msz
- 1; i
++) {
184 if (memcmp(m
, p
+ sz
- msz
, msz
) != 0)
192 /*----- Algorithm choice --------------------------------------------------*/
197 int (*sign
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/, dstr */
*d*/
);
198 int (*verify
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/,
199 const void */
*s*/
, size_t /*ssz*/);
202 static const gchash
*hashtab
[] = { &rmd160
, &sha
, &md5
, 0 };
203 static sig sigtab
[] = {
204 { "dsa", "dsig-dsa", dsasign
, dsaverify
},
205 { "rsa", "dsig-rsa", rsasign
, rsaverify
},
209 /* --- @gethash@ --- *
211 * Arguments: @const char *name@ = pointer to name string
213 * Returns: Pointer to appropriate hash class.
215 * Use: Chooses a hash function by name.
218 static const gchash
*gethash(const char *name
)
220 const gchash
**g
, *gg
= 0;
221 size_t sz
= strlen(name
);
222 for (g
= hashtab
; *g
; g
++) {
223 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
224 if ((*g
)->name
[sz
] == 0) {
236 /* --- @getsig@ --- *
238 * Arguments: @const char *name@ = pointer to name string
240 * Returns: Pointer to appropriate signature type.
242 * Use: Chooses a signature algorithm by name.
245 static sig
*getsig(const char *name
)
248 size_t sz
= strlen(name
);
249 for (s
= sigtab
; s
->name
; s
++) {
250 if (strncmp(name
, s
->name
, sz
) == 0) {
251 if (s
->name
[sz
] == 0) {
263 /*----- Data formatting ---------------------------------------------------*/
265 /* --- Binary data structure --- *
267 * The binary format, which is used for hashing and for the optional binary
268 * output, consists of a sequence of tagged blocks. The tag describes the
269 * format and meaining of the following data.
273 /* --- Block tags --- */
275 T_IDENT
= 0, /* An identifying marker */
276 T_SIGALG
, /* Signature algorithm used */
277 T_HASHALG
, /* Hash algorithm used */
278 T_KEYID
, /* Key identifier */
279 T_BEGIN
, /* Begin hashing here */
280 T_COMMENT
= T_BEGIN
, /* A textual comment */
281 T_DATE
, /* Creation date of signature */
282 T_EXPIRE
, /* Expiry date of signature */
283 T_FILE
, /* File and corresponding hash */
284 T_SIGNATURE
, /* Final signature block */
286 /* --- Error messages --- */
294 /* --- Name translation table --- */
296 static const char *tagtab
[] = {
297 "ident:", "sigalg:", "hashalg:", "keyid:",
298 "comment:", "date:", "expires:", "file:",
303 static const char *errtab
[] = {
305 "Unexpected end-of-file",
306 "Binary object too large",
311 /* --- Memory representation of block types --- */
313 typedef struct block
{
314 int tag
; /* Type tag */
315 dstr d
; /* String data */
316 dstr b
; /* Binary data */
317 time_t t
; /* Timestamp */
318 uint32 k
; /* Keyid */
321 /* --- @getstring@ --- *
323 * Arguments: @FILE *fp@ = stream from which to read
324 * @dstr *d@ = destination string
325 * @unsigned raw@ = raw or cooked read
327 * Returns: Zero if OK, nonzero on end-of-file.
329 * Use: Reads a filename (or something similar) from a stream.
332 static int getstring(FILE *fp
, dstr
*d
, unsigned raw
)
337 /* --- Raw: just read exactly what's written up to a null byte --- */
340 if ((ch
= getc(fp
)) == EOF
)
346 if ((ch
= getc(fp
)) == EOF
)
353 /* --- Skip as far as whitespace --- *
355 * Also skip past comments.
360 while (isspace((unsigned char)ch
))
363 do ch
= getc(fp
); while (ch
!= '\n' && ch
!= EOF
);
369 /* --- If the character is a quote then read a quoted string --- */
381 /* --- Now read all sorts of interesting things --- */
385 /* --- Handle an escaped thing --- */
392 case 'a': ch
= '\n'; break;
393 case 'b': ch
= '\b'; break;
394 case 'f': ch
= '\f'; break;
395 case 'n': ch
= '\n'; break;
396 case 'r': ch
= '\r'; break;
397 case 't': ch
= '\t'; break;
398 case 'v': ch
= '\v'; break;
404 /* --- If it's a quote or some other end marker then stop --- */
406 if (ch
== q
|| (!q
&& isspace((unsigned char)ch
)))
409 /* --- Otherwise contribute and continue --- */
412 if ((ch
= getc(fp
)) == EOF
)
422 /* --- @putstring@ --- *
424 * Arguments: @FILE *fp@ = stream to write on
425 * @const char *p@ = pointer to text
426 * @unsigned raw@ = whether the string is to be written raw
430 * Use: Emits a string to a stream.
433 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
435 size_t sz
= strlen(p
);
439 /* --- Just write the string null terminated if raw --- */
442 fwrite(p
, 1, sz
+ 1, fp
);
446 /* --- Check for any dodgy characters --- */
449 for (q
= p
; *q
; q
++) {
450 if (isspace((unsigned char)*q
)) {
459 /* --- Emit the string --- */
461 for (q
= p
; *q
; q
++) {
463 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
464 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
465 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
466 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
467 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
468 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
469 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
470 case '`': fputc('\\', fp
); fputc('`', fp
); break;
471 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
472 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
485 /* --- @timestring@ --- *
487 * Arguments: @time_t t@ = a timestamp
488 * @dstr *d@ = a string to write on
492 * Use: Writes a textual representation of the timestamp to the
496 static void timestring(time_t t
, dstr
*d
)
498 if (t
== KEXP_FOREVER
)
501 struct tm
*tm
= localtime(&t
);
503 d
->len
+= strftime(d
->buf
+ d
->len
, 32, "%Y-%m-%d %H:%M:%S %Z", tm
);
508 /* --- @breset@ --- *
510 * Arguments: @block *b@ = block to reset
514 * Use: Resets a block so that more stuff can be put in it.
517 static void breset(block
*b
)
528 * Arguments: @block *b@ = block to initialize
532 * Use: Initializes a block as something to read into.
535 static void binit(block
*b
)
542 /* --- @bdestroy@ --- *
544 * Arguments: @block *b@ = block to destroy
548 * Use: Destroys a block's contents.
551 static void bdestroy(block
*b
)
559 * Arguments: @block *b@ = pointer to block
560 * @FILE *fp@ = stream to read from
561 * @unsigned bin@ = binary switch
563 * Returns: Tag of block, or an error tag.
565 * Use: Reads a block from a stream.
568 static int bget(block
*b
, FILE *fp
, unsigned bin
)
572 /* --- Read the tag --- */
578 if (getstring(fp
, &d
, 0))
580 for (tag
= 0; tagtab
[tag
]; tag
++) {
581 if (strcmp(tagtab
[tag
], d
.buf
) == 0)
588 /* --- Decide what to do next --- */
594 /* --- Reading of strings --- */
600 if (getstring(fp
, &b
->d
, bin
))
604 /* --- Timestamps --- */
610 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
612 b
->t
= ((time_t)(((LOAD32(buf
+ 0) << 16) << 16) & ~MASK32
) |
613 (time_t)LOAD32(buf
+ 4));
615 if (getstring(fp
, &b
->d
, 0))
617 if (strcmp(b
->d
.buf
, "forever") == 0)
619 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
624 /* --- Key ids --- */
629 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
633 if (getstring(fp
, &b
->d
, 0))
635 b
->k
= strtoul(b
->d
.buf
, 0, 16);
639 /* --- Reading of binary data --- */
646 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
652 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
657 if (getstring(fp
, &b
->d
, 0))
660 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
661 base64_decode(&b64
, 0, 0, &b
->b
);
664 if (tag
== T_FILE
&& getstring(fp
, &b
->d
, bin
))
668 /* --- Anything else --- */
679 * Arguments: @block *b@ = pointer to block to emit
680 * @dstr *d@ = output buffer
684 * Use: Encodes a block in a binary format.
687 static void blob(block
*b
, dstr
*d
)
701 STORE32(d
->buf
+ d
->len
, ((b
->t
& ~MASK32
) >> 16) >> 16);
702 STORE32(d
->buf
+ d
->len
+ 4, b
->t
);
707 STORE32(d
->buf
+ d
->len
, b
->k
);
713 STORE16(d
->buf
+ d
->len
, b
->b
.len
);
716 if (b
->tag
== T_FILE
) {
724 /* --- @bwrite@ --- *
726 * Arguments: @block *b@ = pointer to block to write
727 * @FILE *fp@ = stream to write on
731 * Use: Writes a block on a stream in a textual format.
734 static void bwrite(block
*b
, FILE *fp
)
736 fputs(tagtab
[b
->tag
], fp
);
743 putstring(fp
, b
->d
.buf
, 0);
748 timestring(b
->t
, &d
);
749 putstring(fp
, d
.buf
, 0);
753 fprintf(fp
, "%08lx", (unsigned long)b
->k
);
761 base64_encode(&b64
, b
->b
.buf
, b
->b
.len
, &d
);
762 base64_encode(&b64
, 0, 0, &d
);
764 if (b
->tag
== T_FILE
) {
766 putstring(fp
, b
->d
.buf
, 0);
775 * Arguments: @block *b@ = pointer to block to write
776 * @FILE *fp@ = file to write on
777 * @ghash *h@ = pointer to hash function
778 * @unsigned bin@ = binary/text flag
782 * Use: Spits out a block properly.
785 static void bemit(block
*b
, FILE *fp
, ghash
*h
, unsigned bin
)
787 if (h
|| (fp
&& bin
)) {
791 h
->ops
->hash(h
, d
.buf
, d
.len
);
793 fwrite(d
.buf
, d
.len
, 1, fp
);
799 /*----- Static variables --------------------------------------------------*/
801 static const char *keyring
= "keyring";
803 /*----- Other shared functions --------------------------------------------*/
805 /* --- @keyreport@ --- *
807 * Arguments: @const char *file@ = filename containing the error
808 * @int line@ = line number in file
809 * @const char *err@ = error text message
810 * @void *p@ = unimportant pointer
814 * Use: Reports errors during the opening of a key file.
817 static void keyreport(const char *file
, int line
, const char *err
, void *p
)
819 moan("error in keyring `%s' at line `%s': %s", file
, line
, err
);
824 * Arguments: @const gchash *c@ = pointer to hash class
825 * @const char *file@ = file to hash
826 * @octet *b@ = pointer to output buffer
828 * Returns: Zero if it worked, or nonzero for a system error.
830 * Use: Hashes a file.
833 static int fhash(const gchash
*c
, const char *file
, octet
*b
)
835 FILE *fp
= fopen(file
, "rb");
836 ghash
*h
= c
->init();
843 while ((sz
= fread(buf
, 1, sizeof(buf
), fp
)) > 0)
844 h
->ops
->hash(h
, buf
, sz
);
855 * Arguments: @FILE *fp@ = file to write on
856 * @const void *p@ = pointer to data to be written
857 * @size_t sz@ = size of the data to write
861 * Use: Emits a hex dump to a stream.
864 static void fhex(FILE *fp
, const void *p
, size_t sz
)
870 fprintf(fp
, "%02x", *q
++);
878 /*----- Signature generation ----------------------------------------------*/
880 static int sign(int argc
, char *argv
[])
893 const sig
*s
= sigtab
;
894 const gchash
*gch
= &rmd160
;
896 time_t exp
= KEXP_EXPIRE
;
898 const char *ifile
= 0;
899 const char *ofile
= 0;
907 static struct option opts
[] = {
908 { "null", 0, 0, '0' },
909 { "binary", 0, 0, 'b' },
910 { "verbose", 0, 0, 'v' },
911 { "quiet", 0, 0, 'q' },
912 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
913 { "hash", OPTF_ARGREQ
, 0, 'h' },
914 { "comment", OPTF_ARGREQ
, 0, 'c' },
915 { "file", OPTF_ARGREQ
, 0, 'f' },
916 { "output", OPTF_ARGREQ
, 0, 'o' },
917 { "keytype", OPTF_ARGREQ
, 0, 't' },
918 { "keyid", OPTF_ARGREQ
, 0, 'i' },
919 { "key", OPTF_ARGREQ
, 0, 'i' },
920 { "expire", OPTF_ARGREQ
, 0, 'e' },
923 int i
= mdwopt(argc
, argv
, "+0vqb a:h:c: f:o: t:i:k:e:", opts
, 0, 0, 0);
941 if ((s
= getsig(optarg
)) == 0) {
942 die(EXIT_FAILURE
, "unknown or ambiguous signature algorithm `%s'",
947 if ((gch
= gethash(optarg
)) == 0) {
948 die(EXIT_FAILURE
, "unknown or ambiguous hash function `%s'",
969 if (strcmp(optarg
, "forever") == 0)
971 else if ((exp
= get_date(optarg
, 0)) == -1)
972 die(EXIT_FAILURE
, "bad expiry time");
979 if (optind
!= argc
|| (f
& f_bogus
))
980 die(EXIT_FAILURE
, "Usage: sign [-options]");
982 /* --- Locate the signing key --- */
984 if (key_open(&kf
, keyring
, KOPEN_WRITE
, keyreport
, 0))
985 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
987 if ((k
= key_bytag(&kf
, ki
)) == 0)
988 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
992 if ((k
= key_bytype(&kf
, kt
)) == 0)
993 die(EXIT_FAILURE
, "no appropriate key of type `%s'", kt
);
996 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
997 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
1001 /* --- Open files --- */
1005 else if ((ifp
= fopen(ifile
, (f
& f_raw
) ?
"rb" : "r")) == 0) {
1006 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
1007 ifile
, strerror(errno
));
1012 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
1013 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
1014 ofile
, strerror(errno
));
1017 /* --- Emit the start of the output --- */
1019 binit(&b
); b
.tag
= T_IDENT
;
1020 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
1021 bemit(&b
, ofp
, 0, f
& f_bin
);
1023 breset(&b
); b
.tag
= T_SIGALG
; DPUTS(&b
.d
, s
->name
);
1024 bemit(&b
, ofp
, 0, f
& f_bin
);
1026 breset(&b
); b
.tag
= T_HASHALG
; DPUTS(&b
.d
, gch
->name
);
1027 bemit(&b
, ofp
, 0, f
& f_bin
);
1029 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
1030 bemit(&b
, ofp
, 0, f
& f_bin
);
1032 /* --- Start hashing, and emit the datestamps and things --- */
1035 time_t now
= time(0);
1038 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, h
, f
& f_bin
);
1039 if (exp
== KEXP_EXPIRE
)
1040 exp
= now
+ 86400 * 28;
1041 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, h
, f
& f_bin
);
1043 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
1044 bemit(&b
, ofp
, h
, f
& f_bin
);
1051 /* --- Now hash the various files --- */
1055 /* --- Stop on an output error --- */
1062 /* --- Read the next filename to hash --- */
1065 if (getstring(ifp
, &b
.d
, f
& f_raw
))
1068 DENSURE(&b
.b
, h
->ops
->c
->hashsz
);
1069 if (fhash(gch
, b
.d
.buf
, b
.b
.buf
)) {
1070 moan("Error reading `%s': %s", b
.d
.buf
, strerror(errno
));
1073 b
.b
.len
+= h
->ops
->c
->hashsz
;
1075 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
1076 fprintf(stderr
, " %s\n", b
.d
.buf
);
1078 bemit(&b
, ofp
, h
, f
& f_bin
);
1082 /* --- Create the signature --- */
1084 if (!(f
& f_bogus
)) {
1086 b
.tag
= T_SIGNATURE
;
1087 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1088 h
->ops
->done(h
, b
.d
.buf
);
1089 if ((e
= s
->sign(k
, b
.d
.buf
, h
->ops
->c
->hashsz
, &b
.b
)) != 0) {
1090 moan("error creating signature: %s", key_strerror(e
));
1093 if (!(f
& f_bogus
)) {
1094 bemit(&b
, ofp
, 0, f
& f_bin
);
1095 key_used(&kf
, k
, exp
);
1099 /* --- Tidy up at the end --- */
1111 if ((e
= key_close(&kf
)) != 0) {
1114 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
1115 keyring
, strerror(errno
));
1117 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
1118 keyring
, strerror(errno
));
1122 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
1123 return (EXIT_SUCCESS
);
1126 /*----- Signature verification --------------------------------------------*/
1128 static int verify(int argc
, char *argv
[])
1141 const gchash
*gch
= &rmd160
;
1148 /* --- Parse the options --- */
1151 static struct option opts
[] = {
1152 { "verbose", 0, 0, 'v' },
1153 { "quiet", 0, 0, 'q' },
1156 int i
= mdwopt(argc
, argv
, "+vq", opts
, 0, 0, 0);
1174 if ((f
& f_bogus
) || argc
> 1)
1175 die(EXIT_FAILURE
, "Usage: verify [-qv] [file]");
1177 /* --- Open the key file, and start reading the input file --- */
1179 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
1180 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
1184 if ((fp
= fopen(argv
[0], "rb")) == 0) {
1185 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1186 argv
[0], strerror(errno
));
1188 if (getc(fp
) == 0) {
1193 if ((fp
= fopen(argv
[0], "r")) == 0) {
1194 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1195 argv
[0], strerror(errno
));
1200 /* --- Read the introductory matter --- */
1205 e
= bget(&b
, fp
, f
& f_bin
);
1207 die(EXIT_FAILURE
, "error reading packet: %s\n", errtab
[-e
]);
1213 printf("INFO ident: `%s'\n", b
.d
.buf
);
1216 if ((s
= getsig(b
.d
.buf
)) == 0) {
1218 printf("FAIL unknown signature algorithm `%s'\n", b
.d
.buf
);
1222 printf("INFO signature algorithm: %s\n", s
->name
);
1225 if ((gch
= gethash(b
.d
.buf
)) == 0) {
1227 printf("FAIL unknown hash function `%s'\n", b
.d
.buf
);
1231 printf("INFO hash function algorithm: %s\n", gch
->name
);
1234 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
1236 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
1241 key_fulltag(k
, &b
.d
);
1242 printf("INFO key: %s\n", b
.d
.buf
);
1246 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
1251 /* --- Initialize the hash function and start reading hashed packets --- */
1256 puts("FAIL no keyid packet found");
1263 printf("INFO comment: `%s'\n", b
.d
.buf
);
1269 timestring(b
.t
, &b
.d
);
1270 printf("INFO date: %s\n", b
.d
.buf
);
1275 time_t now
= time(0);
1278 puts("BAD signature has expired");
1283 timestring(b
.t
, &b
.d
);
1284 printf("INFO expires: %s\n", b
.d
.buf
);
1290 DENSURE(&d
, gch
->hashsz
);
1291 if (fhash(gch
, b
.d
.buf
, d
.buf
)) {
1293 printf("BAD error reading file `%s': %s\n",
1294 b
.d
.buf
, strerror(errno
));
1297 } else if (b
.b
.len
!= gch
->hashsz
||
1298 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
1300 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
1302 } else if (verb
> 3) {
1303 fputs("INFO hash: ", stdout
);
1304 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
1305 printf(" %s\n", b
.d
.buf
);
1311 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1312 b
.d
.len
+= h
->ops
->c
->hashsz
;
1313 h
->ops
->done(h
, b
.d
.buf
);
1314 e
= s
->verify(k
, b
.d
.buf
, b
.d
.len
, b
.b
.buf
, b
.b
.len
);
1318 printf("BAD error unpacking key: %s\n", key_strerror(e
));
1320 puts("BAD bad signature");
1323 } else if (verb
> 2)
1324 puts("INFO good signature");
1328 printf("FAIL invalid packet type %i\n", e
);
1333 e
= bget(&b
, fp
, f
& f_bin
);
1336 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
1348 puts("FAIL signature invalid");
1350 puts("OK signature verified");
1352 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
1355 /*----- Main code ---------------------------------------------------------*/
1357 typedef struct cmd
{
1359 int (*func
)(int /*argc*/, char */
*argv*/
[]);
1363 static cmd cmdtab
[] = {
1364 /* { "manifest", manifest, */
1365 /* "manifest [-0] [-o output]" }, */
1368 [-0v] [-a alg] [-h hash] [-t keytype] [-i keyid]\n\
1369 [-e expire] [-f file] [-o output]" },
1371 "verify [-qv] [file]" },
1375 static void version(FILE *fp
)
1377 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1380 static void usage(FILE *fp
)
1382 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1385 static void help(FILE *fp
)
1392 Create and verify signatures on lists of files.\n\
1394 for (c
= cmdtab
; c
->name
; c
++)
1395 fprintf(fp
, "%s\n", c
->help
);
1400 * Arguments: @int argc@ = number of command line arguments
1401 * @char *argv[]@ = vector of command line arguments
1403 * Returns: Zero if successful, nonzero otherwise.
1405 * Use: Signs or verifies signatures on lists of files. Useful for
1406 * ensuring that a distribution is unmolested.
1409 int main(int argc
, char *argv
[])
1412 cmd
*c
= 0, *cc
= 0;
1419 /* --- Initialize the library --- */
1423 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1424 rand_seed(RAND_GLOBAL
, 160);
1426 /* --- Parse options --- */
1429 static struct option opts
[] = {
1430 { "help", 0, 0, 'h' },
1431 { "version", 0, 0, 'v' },
1432 { "usage", 0, 0, 'u' },
1433 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1436 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1463 if (f
& f_bogus
|| argc
< 1) {
1468 /* --- Dispatch to the correct subcommand handler --- */
1470 n
= strlen(argv
[0]);
1471 for (c
= cmdtab
; c
->name
; c
++) {
1472 if (strncmp(argv
[0], c
->name
, n
) == 0) {
1473 if (c
->name
[n
] == 0) {
1477 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1483 die(EXIT_FAILURE
, "unknown command `%s'", argv
[0]);
1484 return (cc
->func(argc
, argv
));
1487 /*----- That's all, folks -------------------------------------------------*/