3 * $Id: dsig.c,v 1.7 2001/02/23 09:04:17 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.7 2001/02/23 09:04:17 mdw
34 * Add new hash functions. Provide full help for subcommands. Run the
35 * hash function over parts of the header in a canonical order.
37 * Revision 1.6 2000/12/06 20:33:27 mdw
38 * Make flags be macros rather than enumerations, to ensure that they're
41 * Revision 1.5 2000/10/08 12:12:09 mdw
42 * Shut up some warnings.
44 * Revision 1.4 2000/08/04 23:23:44 mdw
45 * Various <ctype.h> fixes.
47 * Revision 1.3 2000/07/15 20:53:23 mdw
48 * More hash functions. Bug fix in getstring.
50 * Revision 1.2 2000/07/01 11:27:22 mdw
51 * Use new PKCS#1 padding functions rather than rolling by hand.
53 * Revision 1.1 2000/06/17 10:54:29 mdw
54 * Program to generate and verify signatures on multiple files.
58 /*----- Header files ------------------------------------------------------*/
68 #include <mLib/alloc.h>
69 #include <mLib/base64.h>
70 #include <mLib/mdwopt.h>
71 #include <mLib/quis.h>
72 #include <mLib/report.h>
99 /*----- Digital signature algorithm ---------------------------------------*/
101 static int dsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
104 key_packstruct ks
[DSA_PRIVFETCHSZ
];
110 kp
= key_fetchinit(dsa_privfetch
, ks
, &dp
);
111 if ((e
= key_fetch(kp
, k
)) != 0) {
115 sz
= mp_octets(dp
.dp
.q
);
117 die(EXIT_FAILURE
, "hash function too wide for this signing key");
119 p
= (octet
*)d
->buf
+ d
->len
;
120 rand_get(RAND_GLOBAL
, p
, sz
);
121 dsa_sign(&dp
.dp
, dp
.x
, m
, msz
, p
, sz
, p
, sz
, p
+ sz
, sz
);
127 static int dsaverify(key
*k
, const void *m
, size_t msz
,
128 const void *s
, size_t ssz
)
131 key_packstruct ks
[DSA_PUBFETCHSZ
];
137 kp
= key_fetchinit(dsa_pubfetch
, ks
, &dp
);
138 if ((e
= key_fetch(kp
, k
)) != 0) {
143 e
= dsa_verify(&dp
.dp
, dp
.y
, m
, msz
, p
, sz
, p
+ sz
, sz
);
148 /*----- RSA signing -------------------------------------------------------*/
150 static int rsasign(key
*k
, const void *m
, size_t msz
, dstr
*d
)
154 pkcs1 pk
= { 0, 0, 0 };
155 key_packstruct ks
[RSA_PRIVFETCHSZ
];
159 kp
= key_fetchinit(rsa_privfetch
, ks
, &rp
);
160 if ((e
= key_fetch(kp
, k
)) != 0) {
164 rsa_privcreate(&rpc
, &rp
, &rand_global
);
165 if (rsa_sign(&rpc
, m
, msz
, d
, pkcs1_sigencode
, &pk
) < 0)
166 die(EXIT_FAILURE
, "internal error in rsasign (key too small?)");
167 rsa_privdestroy(&rpc
);
172 static int rsaverify(key
*k
, const void *m
, size_t msz
,
173 const void *s
, size_t ssz
)
177 pkcs1 pk
= { 0, 0, 0 };
178 key_packstruct ks
[RSA_PUBFETCHSZ
];
184 kp
= key_fetchinit(rsa_pubfetch
, ks
, &rp
);
185 if ((e
= key_fetch(kp
, k
)) != 0) {
189 rsa_pubcreate(&rpc
, &rp
);
190 if (rsa_verify(&rpc
, s
, ssz
, &d
, pkcs1_sigdecode
, &pk
) > 0 &&
191 msz
== d
.len
&& memcmp(d
.buf
, m
, msz
) == 0)
194 rsa_pubdestroy(&rpc
);
199 /*----- Algorithm choice --------------------------------------------------*/
204 int (*sign
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/, dstr */
*d*/
);
205 int (*verify
)(key */
*k*/
, const void */
*m*/
, size_t /*msz*/,
206 const void */
*s*/
, size_t /*ssz*/);
209 static const gchash
*hashtab
[] = {
210 &rmd160
, &tiger
, &sha
, &sha256
, &sha384
, &sha512
,
211 &rmd128
, &rmd256
, &rmd320
, &md5
, &md4
, &md2
, 0 };
212 static sig sigtab
[] = {
213 { "dsa", "dsig-dsa", dsasign
, dsaverify
},
214 { "rsa", "dsig-rsa", rsasign
, rsaverify
},
218 /* --- @gethash@ --- *
220 * Arguments: @const char *name@ = pointer to name string
222 * Returns: Pointer to appropriate hash class.
224 * Use: Chooses a hash function by name.
227 static const gchash
*gethash(const char *name
)
229 const gchash
**g
, *gg
= 0;
230 size_t sz
= strlen(name
);
231 for (g
= hashtab
; *g
; g
++) {
232 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
233 if ((*g
)->name
[sz
] == 0) {
245 /* --- @getsig@ --- *
247 * Arguments: @const char *name@ = pointer to name string
249 * Returns: Pointer to appropriate signature type.
251 * Use: Chooses a signature algorithm by name.
254 static sig
*getsig(const char *name
)
257 size_t sz
= strlen(name
);
258 for (s
= sigtab
; s
->name
; s
++) {
259 if (strncmp(name
, s
->name
, sz
) == 0) {
260 if (s
->name
[sz
] == 0) {
272 /*----- Data formatting ---------------------------------------------------*/
274 /* --- Binary data structure --- *
276 * The binary format, which is used for hashing and for the optional binary
277 * output, consists of a sequence of tagged blocks. The tag describes the
278 * format and meaining of the following data.
282 /* --- Block tags --- */
284 T_IDENT
= 0, /* An identifying marker */
285 T_SIGALG
, /* Signature algorithm used */
286 T_HASHALG
, /* Hash algorithm used */
287 T_KEYID
, /* Key identifier */
288 T_BEGIN
, /* Begin hashing here */
289 T_COMMENT
= T_BEGIN
, /* A textual comment */
290 T_DATE
, /* Creation date of signature */
291 T_EXPIRE
, /* Expiry date of signature */
292 T_FILE
, /* File and corresponding hash */
293 T_SIGNATURE
, /* Final signature block */
295 /* --- Error messages --- */
303 /* --- Name translation table --- */
305 static const char *tagtab
[] = {
306 "ident:", "sigalg:", "hashalg:", "keyid:",
307 "comment:", "date:", "expires:", "file:",
312 static const char *errtab
[] = {
314 "Unexpected end-of-file",
315 "Binary object too large",
320 /* --- Memory representation of block types --- */
322 typedef struct block
{
323 int tag
; /* Type tag */
324 dstr d
; /* String data */
325 dstr b
; /* Binary data */
326 time_t t
; /* Timestamp */
327 uint32 k
; /* Keyid */
330 /* --- @getstring@ --- *
332 * Arguments: @FILE *fp@ = stream from which to read
333 * @dstr *d@ = destination string
334 * @unsigned raw@ = raw or cooked read
336 * Returns: Zero if OK, nonzero on end-of-file.
338 * Use: Reads a filename (or something similar) from a stream.
341 static int getstring(FILE *fp
, dstr
*d
, unsigned raw
)
346 /* --- Raw: just read exactly what's written up to a null byte --- */
349 if ((ch
= getc(fp
)) == EOF
)
355 if ((ch
= getc(fp
)) == EOF
)
362 /* --- Skip as far as whitespace --- *
364 * Also skip past comments.
372 do ch
= getc(fp
); while (ch
!= '\n' && ch
!= EOF
);
378 /* --- If the character is a quote then read a quoted string --- */
390 /* --- Now read all sorts of interesting things --- */
394 /* --- Handle an escaped thing --- */
401 case 'a': ch
= '\a'; break;
402 case 'b': ch
= '\b'; break;
403 case 'f': ch
= '\f'; break;
404 case 'n': ch
= '\n'; break;
405 case 'r': ch
= '\r'; break;
406 case 't': ch
= '\t'; break;
407 case 'v': ch
= '\v'; break;
414 /* --- If it's a quote or some other end marker then stop --- */
416 if (ch
== q
|| (!q
&& isspace((unsigned char)ch
)))
419 /* --- Otherwise contribute and continue --- */
422 if ((ch
= getc(fp
)) == EOF
)
432 /* --- @putstring@ --- *
434 * Arguments: @FILE *fp@ = stream to write on
435 * @const char *p@ = pointer to text
436 * @unsigned raw@ = whether the string is to be written raw
440 * Use: Emits a string to a stream.
443 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
445 size_t sz
= strlen(p
);
449 /* --- Just write the string null terminated if raw --- */
452 fwrite(p
, 1, sz
+ 1, fp
);
456 /* --- Check for any dodgy characters --- */
459 for (q
= p
; *q
; q
++) {
460 if (isspace((unsigned char)*q
)) {
469 /* --- Emit the string --- */
471 for (q
= p
; *q
; q
++) {
473 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
474 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
475 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
476 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
477 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
478 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
479 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
480 case '`': fputc('\\', fp
); fputc('`', fp
); break;
481 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
482 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
495 /* --- @timestring@ --- *
497 * Arguments: @time_t t@ = a timestamp
498 * @dstr *d@ = a string to write on
502 * Use: Writes a textual representation of the timestamp to the
506 static void timestring(time_t t
, dstr
*d
)
508 if (t
== KEXP_FOREVER
)
511 struct tm
*tm
= localtime(&t
);
513 d
->len
+= strftime(d
->buf
+ d
->len
, 32, "%Y-%m-%d %H:%M:%S %Z", tm
);
518 /* --- @breset@ --- *
520 * Arguments: @block *b@ = block to reset
524 * Use: Resets a block so that more stuff can be put in it.
527 static void breset(block
*b
)
538 * Arguments: @block *b@ = block to initialize
542 * Use: Initializes a block as something to read into.
545 static void binit(block
*b
)
552 /* --- @bdestroy@ --- *
554 * Arguments: @block *b@ = block to destroy
558 * Use: Destroys a block's contents.
561 static void bdestroy(block
*b
)
569 * Arguments: @block *b@ = pointer to block
570 * @FILE *fp@ = stream to read from
571 * @unsigned bin@ = binary switch
573 * Returns: Tag of block, or an error tag.
575 * Use: Reads a block from a stream.
578 static int bget(block
*b
, FILE *fp
, unsigned bin
)
582 /* --- Read the tag --- */
588 if (getstring(fp
, &d
, 0))
590 for (tag
= 0; tagtab
[tag
]; tag
++) {
591 if (strcmp(tagtab
[tag
], d
.buf
) == 0)
598 /* --- Decide what to do next --- */
604 /* --- Reading of strings --- */
610 if (getstring(fp
, &b
->d
, bin
))
614 /* --- Timestamps --- */
620 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
622 b
->t
= ((time_t)(((LOAD32(buf
+ 0) << 16) << 16) & ~MASK32
) |
623 (time_t)LOAD32(buf
+ 4));
625 if (getstring(fp
, &b
->d
, 0))
627 if (strcmp(b
->d
.buf
, "forever") == 0)
629 else if ((b
->t
= get_date(b
->d
.buf
, 0)) == -1)
634 /* --- Key ids --- */
639 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
643 if (getstring(fp
, &b
->d
, 0))
645 b
->k
= strtoul(b
->d
.buf
, 0, 16);
649 /* --- Reading of binary data --- */
656 if (fread(buf
, sizeof(buf
), 1, fp
) < 1)
662 if (fread(b
->b
.buf
+ b
->b
.len
, 1, sz
, fp
) < sz
)
667 if (getstring(fp
, &b
->d
, 0))
670 base64_decode(&b64
, b
->d
.buf
, b
->d
.len
, &b
->b
);
671 base64_decode(&b64
, 0, 0, &b
->b
);
674 if (tag
== T_FILE
&& getstring(fp
, &b
->d
, bin
))
678 /* --- Anything else --- */
689 * Arguments: @block *b@ = pointer to block to emit
690 * @dstr *d@ = output buffer
694 * Use: Encodes a block in a binary format.
697 static void blob(block
*b
, dstr
*d
)
711 STORE32(d
->buf
+ d
->len
, ((b
->t
& ~MASK32
) >> 16) >> 16);
712 STORE32(d
->buf
+ d
->len
+ 4, b
->t
);
717 STORE32(d
->buf
+ d
->len
, b
->k
);
723 STORE16(d
->buf
+ d
->len
, b
->b
.len
);
726 if (b
->tag
== T_FILE
) {
734 /* --- @bwrite@ --- *
736 * Arguments: @block *b@ = pointer to block to write
737 * @FILE *fp@ = stream to write on
741 * Use: Writes a block on a stream in a textual format.
744 static void bwrite(block
*b
, FILE *fp
)
746 fputs(tagtab
[b
->tag
], fp
);
753 putstring(fp
, b
->d
.buf
, 0);
758 timestring(b
->t
, &d
);
759 putstring(fp
, d
.buf
, 0);
763 fprintf(fp
, "%08lx", (unsigned long)b
->k
);
771 base64_encode(&b64
, b
->b
.buf
, b
->b
.len
, &d
);
772 base64_encode(&b64
, 0, 0, &d
);
774 if (b
->tag
== T_FILE
) {
776 putstring(fp
, b
->d
.buf
, 0);
785 * Arguments: @block *b@ = pointer to block to write
786 * @FILE *fp@ = file to write on
787 * @ghash *h@ = pointer to hash function
788 * @unsigned bin@ = binary/text flag
792 * Use: Spits out a block properly.
795 static void bemit(block
*b
, FILE *fp
, ghash
*h
, unsigned bin
)
797 if (h
|| (fp
&& bin
)) {
801 h
->ops
->hash(h
, d
.buf
, d
.len
);
803 fwrite(d
.buf
, d
.len
, 1, fp
);
809 /*----- Static variables --------------------------------------------------*/
811 static const char *keyring
= "keyring";
813 /*----- Other shared functions --------------------------------------------*/
815 /* --- @keyreport@ --- *
817 * Arguments: @const char *file@ = filename containing the error
818 * @int line@ = line number in file
819 * @const char *err@ = error text message
820 * @void *p@ = unimportant pointer
824 * Use: Reports errors during the opening of a key file.
827 static void keyreport(const char *file
, int line
, const char *err
, void *p
)
829 moan("error in keyring `%s' at line `%s': %s", file
, line
, err
);
834 * Arguments: @const gchash *c@ = pointer to hash class
835 * @const char *file@ = file to hash
836 * @void *b@ = pointer to output buffer
838 * Returns: Zero if it worked, or nonzero for a system error.
840 * Use: Hashes a file.
843 static int fhash(const gchash
*c
, const char *file
, void *b
)
845 FILE *fp
= fopen(file
, "rb");
846 ghash
*h
= c
->init();
853 while ((sz
= fread(buf
, 1, sizeof(buf
), fp
)) > 0)
854 h
->ops
->hash(h
, buf
, sz
);
865 * Arguments: @FILE *fp@ = file to write on
866 * @const void *p@ = pointer to data to be written
867 * @size_t sz@ = size of the data to write
871 * Use: Emits a hex dump to a stream.
874 static void fhex(FILE *fp
, const void *p
, size_t sz
)
880 fprintf(fp
, "%02x", *q
++);
888 /*----- Signature generation ----------------------------------------------*/
890 static int sign(int argc
, char *argv
[])
901 const sig
*s
= sigtab
;
902 const gchash
*gch
= &rmd160
;
904 time_t exp
= KEXP_EXPIRE
;
906 const char *ifile
= 0;
907 const char *ofile
= 0;
915 static struct option opts
[] = {
916 { "null", 0, 0, '0' },
917 { "binary", 0, 0, 'b' },
918 { "verbose", 0, 0, 'v' },
919 { "quiet", 0, 0, 'q' },
920 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
921 { "hash", OPTF_ARGREQ
, 0, 'h' },
922 { "comment", OPTF_ARGREQ
, 0, 'c' },
923 { "file", OPTF_ARGREQ
, 0, 'f' },
924 { "output", OPTF_ARGREQ
, 0, 'o' },
925 { "keytype", OPTF_ARGREQ
, 0, 't' },
926 { "keyid", OPTF_ARGREQ
, 0, 'i' },
927 { "key", OPTF_ARGREQ
, 0, 'i' },
928 { "expire", OPTF_ARGREQ
, 0, 'e' },
931 int i
= mdwopt(argc
, argv
, "+0vqb a:h:c: f:o: t:i:k:e:", opts
, 0, 0, 0);
949 if ((s
= getsig(optarg
)) == 0) {
950 die(EXIT_FAILURE
, "unknown or ambiguous signature algorithm `%s'",
955 if ((gch
= gethash(optarg
)) == 0) {
956 die(EXIT_FAILURE
, "unknown or ambiguous hash function `%s'",
977 if (strcmp(optarg
, "forever") == 0)
979 else if ((exp
= get_date(optarg
, 0)) == -1)
980 die(EXIT_FAILURE
, "bad expiry time");
987 if (optind
!= argc
|| (f
& f_bogus
))
988 die(EXIT_FAILURE
, "Usage: sign [-options]");
990 /* --- Locate the signing key --- */
992 if (key_open(&kf
, keyring
, KOPEN_WRITE
, keyreport
, 0))
993 die(EXIT_FAILURE
, "couldn't open keyring `%s'", keyring
);
995 if ((k
= key_bytag(&kf
, ki
)) == 0)
996 die(EXIT_FAILURE
, "couldn't find key `%s'", ki
);
1000 if ((k
= key_bytype(&kf
, kt
)) == 0)
1001 die(EXIT_FAILURE
, "no appropriate key of type `%s'", kt
);
1004 if (exp
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
) {
1005 die(EXIT_FAILURE
, "key `%s' expires: can't create nonexpiring signature",
1009 /* --- Open files --- */
1013 else if ((ifp
= fopen(ifile
, (f
& f_raw
) ?
"rb" : "r")) == 0) {
1014 die(EXIT_FAILURE
, "couldn't open input file `%s': %s",
1015 ifile
, strerror(errno
));
1020 else if ((ofp
= fopen(ofile
, (f
& f_bin
) ?
"wb" : "w")) == 0) {
1021 die(EXIT_FAILURE
, "couldn't open output file `%s': %s",
1022 ofile
, strerror(errno
));
1027 /* --- Emit the start of the output --- */
1029 binit(&b
); b
.tag
= T_IDENT
;
1030 dstr_putf(&b
.d
, "%s, Catacomb version " VERSION
, QUIS
);
1031 bemit(&b
, ofp
, 0, f
& f_bin
);
1033 breset(&b
); b
.tag
= T_SIGALG
; DPUTS(&b
.d
, s
->name
);
1034 bemit(&b
, ofp
, h
, f
& f_bin
);
1036 breset(&b
); b
.tag
= T_HASHALG
; DPUTS(&b
.d
, gch
->name
);
1037 bemit(&b
, ofp
, h
, f
& f_bin
);
1039 breset(&b
); b
.tag
= T_KEYID
; b
.k
= k
->id
;
1040 bemit(&b
, ofp
, h
, f
& f_bin
);
1042 /* --- Start hashing, and emit the datestamps and things --- */
1045 time_t now
= time(0);
1047 breset(&b
); b
.tag
= T_DATE
; b
.t
= now
; bemit(&b
, ofp
, h
, f
& f_bin
);
1048 if (exp
== KEXP_EXPIRE
)
1049 exp
= now
+ 86400 * 28;
1050 breset(&b
); b
.tag
= T_EXPIRE
; b
.t
= exp
; bemit(&b
, ofp
, h
, f
& f_bin
);
1052 breset(&b
); b
.tag
= T_COMMENT
; DPUTS(&b
.d
, c
);
1053 bemit(&b
, ofp
, h
, f
& f_bin
);
1060 /* --- Now hash the various files --- */
1064 /* --- Stop on an output error --- */
1071 /* --- Read the next filename to hash --- */
1074 if (getstring(ifp
, &b
.d
, f
& f_raw
))
1077 DENSURE(&b
.b
, h
->ops
->c
->hashsz
);
1078 if (fhash(gch
, b
.d
.buf
, b
.b
.buf
)) {
1079 moan("Error reading `%s': %s", b
.d
.buf
, strerror(errno
));
1082 b
.b
.len
+= h
->ops
->c
->hashsz
;
1084 fhex(stderr
, b
.b
.buf
, b
.b
.len
);
1085 fprintf(stderr
, " %s\n", b
.d
.buf
);
1087 bemit(&b
, ofp
, h
, f
& f_bin
);
1091 /* --- Create the signature --- */
1093 if (!(f
& f_bogus
)) {
1095 b
.tag
= T_SIGNATURE
;
1096 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1097 h
->ops
->done(h
, b
.d
.buf
);
1098 if ((e
= s
->sign(k
, b
.d
.buf
, h
->ops
->c
->hashsz
, &b
.b
)) != 0) {
1099 moan("error creating signature: %s", key_strerror(e
));
1102 if (!(f
& f_bogus
)) {
1103 bemit(&b
, ofp
, 0, f
& f_bin
);
1104 key_used(&kf
, k
, exp
);
1108 /* --- Tidy up at the end --- */
1120 if ((e
= key_close(&kf
)) != 0) {
1123 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
1124 keyring
, strerror(errno
));
1126 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
1127 keyring
, strerror(errno
));
1131 die(EXIT_FAILURE
, "error(s) occurred while creating signature");
1132 return (EXIT_SUCCESS
);
1139 /*----- Signature verification --------------------------------------------*/
1141 static int verify(int argc
, char *argv
[])
1152 const gchash
*gch
= &rmd160
;
1159 /* --- Parse the options --- */
1162 static struct option opts
[] = {
1163 { "verbose", 0, 0, 'v' },
1164 { "quiet", 0, 0, 'q' },
1167 int i
= mdwopt(argc
, argv
, "+vq", opts
, 0, 0, 0);
1185 if ((f
& f_bogus
) || argc
> 1)
1186 die(EXIT_FAILURE
, "Usage: verify [-qv] [file]");
1188 /* --- Open the key file, and start reading the input file --- */
1190 if (key_open(&kf
, keyring
, KOPEN_READ
, keyreport
, 0))
1191 die(EXIT_FAILURE
, "couldn't open keyring `%s'\n", keyring
);
1195 if ((fp
= fopen(argv
[0], "rb")) == 0) {
1196 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1197 argv
[0], strerror(errno
));
1199 if (getc(fp
) == 0) {
1204 if ((fp
= fopen(argv
[0], "r")) == 0) {
1205 die(EXIT_FAILURE
, "couldn't open file `%s': %s\n",
1206 argv
[0], strerror(errno
));
1211 /* --- Read the introductory matter --- */
1216 e
= bget(&b
, fp
, f
& f_bin
);
1218 die(EXIT_FAILURE
, "error reading packet: %s\n", errtab
[-e
]);
1224 printf("INFO ident: `%s'\n", b
.d
.buf
);
1227 if ((s
= getsig(b
.d
.buf
)) == 0) {
1229 printf("FAIL unknown signature algorithm `%s'\n", b
.d
.buf
);
1233 printf("INFO signature algorithm: %s\n", s
->name
);
1236 if ((gch
= gethash(b
.d
.buf
)) == 0) {
1238 printf("FAIL unknown hash function `%s'\n", b
.d
.buf
);
1242 printf("INFO hash function algorithm: %s\n", gch
->name
);
1245 if ((k
= key_byid(&kf
, b
.k
)) == 0) {
1247 printf("FAIL key %08lx not found\n", (unsigned long)b
.k
);
1252 key_fulltag(k
, &b
.d
);
1253 printf("INFO key: %s\n", b
.d
.buf
);
1257 die(EXIT_FAILURE
, "(internal) unknown packet type\n");
1262 /* --- Initialize the hash function and start reading hashed packets --- */
1268 puts("FAIL no keyid packet found");
1275 breset(&bb
); bb
.tag
= T_SIGALG
; DPUTS(&bb
.d
, s
->name
);
1276 bemit(&bb
, 0, h
, 0);
1277 breset(&bb
); bb
.tag
= T_HASHALG
; DPUTS(&bb
.d
, gch
->name
);
1278 bemit(&bb
, 0, h
, 0);
1279 breset(&bb
); bb
.tag
= T_KEYID
; bb
.k
= k
->id
;
1280 bemit(&bb
, 0, h
, 0);
1288 printf("INFO comment: `%s'\n", b
.d
.buf
);
1294 timestring(b
.t
, &b
.d
);
1295 printf("INFO date: %s\n", b
.d
.buf
);
1300 time_t now
= time(0);
1303 puts("BAD signature has expired");
1308 timestring(b
.t
, &b
.d
);
1309 printf("INFO expires: %s\n", b
.d
.buf
);
1315 DENSURE(&d
, gch
->hashsz
);
1316 if (fhash(gch
, b
.d
.buf
, d
.buf
)) {
1318 printf("BAD error reading file `%s': %s\n",
1319 b
.d
.buf
, strerror(errno
));
1322 } else if (b
.b
.len
!= gch
->hashsz
||
1323 memcmp(d
.buf
, b
.b
.buf
, b
.b
.len
) != 0) {
1325 printf("BAD file `%s' has incorrect hash\n", b
.d
.buf
);
1327 } else if (verb
> 3) {
1328 fputs("INFO hash: ", stdout
);
1329 fhex(stdout
, b
.b
.buf
, b
.b
.len
);
1330 printf(" %s\n", b
.d
.buf
);
1336 DENSURE(&b
.d
, h
->ops
->c
->hashsz
);
1337 b
.d
.len
+= h
->ops
->c
->hashsz
;
1338 h
->ops
->done(h
, b
.d
.buf
);
1339 e
= s
->verify(k
, b
.d
.buf
, b
.d
.len
, b
.b
.buf
, b
.b
.len
);
1343 printf("BAD error unpacking key: %s\n", key_strerror(e
));
1345 puts("BAD bad signature");
1348 } else if (verb
> 2)
1349 puts("INFO good signature");
1353 printf("FAIL invalid packet type %i\n", e
);
1358 e
= bget(&b
, fp
, f
& f_bin
);
1361 printf("FAIL error reading packet: %s\n", errtab
[-e
]);
1373 puts("FAIL signature invalid");
1375 puts("OK signature verified");
1377 return (f
& f_bogus ? EXIT_FAILURE
: EXIT_SUCCESS
);
1384 /*----- Main code ---------------------------------------------------------*/
1386 typedef struct cmd
{
1388 int (*func
)(int /*argc*/, char */
*argv*/
[]);
1393 static cmd cmdtab
[] = {
1394 /* { "manifest", manifest, */
1395 /* "manifest [-0] [-o output]" }, */
1398 [-0bqv] [-a alg] [-h hash] [-t keytype] [-i keyid]\n\
1399 [-e expire] [-f file] [-o output]", "\
1402 -0, --null Read null-terminated filenames from stdin.\n\
1403 -b, --binary Produce a binary output file.\n\
1404 -q, --quiet Produce fewer messages while working.\n\
1405 -v, --verbose Produce more messages while working.\n\
1406 -a, --algorithm=SIGALG Use the SIGALG signature algorithm.\n\
1407 -h, --hash=HASHASL Use the HASHALG message digest algorithm.\n\
1408 -c, --comment=COMMENT Include COMMENT in the output file.\n\
1409 -f, --file=FILE Read filenames to hash from FILE.\n\
1410 -o, --output=FILE Write the signed result to FILE.\n\
1411 -t, --keytype=TYPE Use a key with type TYPE to do the signing.\n\
1412 -i, --keyid=ID Use the key with the given ID to do the signing.\n\
1413 -e, --expire=TIME The signature should expire after TIME.\n\
1416 "verify [-qv] [file]", "\
1419 -q, --quiet Produce fewer messages while working.\n\
1420 -v, --verbose Produce more messages while working.\n\
1425 /* --- @findcmd@ --- *
1427 * Arguments: @const char *name@ = a command name
1429 * Returns: Pointer to the command structure.
1431 * Use: Looks up a command by name. If the command isn't found, an
1432 * error is reported and the program is terminated.
1435 static cmd
*findcmd(const char *name
)
1437 cmd
*c
, *chosen
= 0;
1438 size_t sz
= strlen(name
);
1440 for (c
= cmdtab
; c
->name
; c
++) {
1441 if (strncmp(name
, c
->name
, sz
) == 0) {
1442 if (c
->name
[sz
] == 0) {
1446 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1452 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1456 static void version(FILE *fp
)
1458 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1461 static void usage(FILE *fp
)
1463 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1466 static void help(FILE *fp
, char **argv
)
1472 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1482 Create and verify signatures on lists of files.\n\
1484 for (c
= cmdtab
; c
->name
; c
++)
1485 fprintf(fp
, "%s\n", c
->usage
);
1491 * Arguments: @int argc@ = number of command line arguments
1492 * @char *argv[]@ = vector of command line arguments
1494 * Returns: Zero if successful, nonzero otherwise.
1496 * Use: Signs or verifies signatures on lists of files. Useful for
1497 * ensuring that a distribution is unmolested.
1500 int main(int argc
, char *argv
[])
1506 /* --- Initialize the library --- */
1510 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1511 rand_seed(RAND_GLOBAL
, 160);
1513 /* --- Parse options --- */
1516 static struct option opts
[] = {
1517 { "help", 0, 0, 'h' },
1518 { "version", 0, 0, 'v' },
1519 { "usage", 0, 0, 'u' },
1520 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1523 int i
= mdwopt(argc
, argv
, "+hvu k:", opts
, 0, 0, 0);
1528 help(stdout
, argv
+ optind
);
1550 if (f
& f_bogus
|| argc
< 1) {
1555 /* --- Dispatch to the correct subcommand handler --- */
1557 return (findcmd(argv
[0])->func(argc
, argv
));
1562 /*----- That's all, folks -------------------------------------------------*/