Annotation of src/usr.bin/signify/signify.c, Revision 1.51
1.51 ! tedu 1: /* $OpenBSD: signify.c,v 1.50 2014/03/07 19:53:33 tedu Exp $ */
1.1 tedu 2: /*
3: * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/stat.h>
18:
19: #include <netinet/in.h>
20: #include <resolv.h>
21:
22: #include <stdint.h>
23: #include <fcntl.h>
24: #include <string.h>
25: #include <stdio.h>
1.34 tedu 26: #include <stdlib.h>
1.1 tedu 27: #include <err.h>
28: #include <unistd.h>
29: #include <readpassphrase.h>
30: #include <util.h>
31: #include <sha2.h>
32:
33: #include "crypto_api.h"
34:
35: #define SIGBYTES crypto_sign_ed25519_BYTES
36: #define SECRETBYTES crypto_sign_ed25519_SECRETKEYBYTES
37: #define PUBLICBYTES crypto_sign_ed25519_PUBLICKEYBYTES
38:
39: #define PKALG "Ed"
40: #define KDFALG "BK"
1.13 tedu 41: #define FPLEN 8
42:
1.18 tedu 43: #define COMMENTHDR "untrusted comment: "
44: #define COMMENTHDRLEN 19
45: #define COMMENTMAXLEN 1024
1.51 ! tedu 46: #define VERIFYWITH "verify with "
1.1 tedu 47:
48: struct enckey {
49: uint8_t pkalg[2];
50: uint8_t kdfalg[2];
51: uint32_t kdfrounds;
52: uint8_t salt[16];
53: uint8_t checksum[8];
1.13 tedu 54: uint8_t fingerprint[FPLEN];
1.1 tedu 55: uint8_t seckey[SECRETBYTES];
56: };
57:
58: struct pubkey {
59: uint8_t pkalg[2];
1.13 tedu 60: uint8_t fingerprint[FPLEN];
1.1 tedu 61: uint8_t pubkey[PUBLICBYTES];
62: };
63:
64: struct sig {
65: uint8_t pkalg[2];
1.13 tedu 66: uint8_t fingerprint[FPLEN];
1.1 tedu 67: uint8_t sig[SIGBYTES];
68: };
69:
70: extern char *__progname;
71:
72: static void
1.31 tedu 73: usage(const char *error)
1.1 tedu 74: {
1.31 tedu 75: if (error)
76: fprintf(stderr, "%s\n", error);
1.9 espie 77: fprintf(stderr, "usage:"
1.15 espie 78: #ifndef VERIFYONLY
1.47 naddy 79: "\t%1$s -C [-q] -p pubkey -x sigfile [file ...]\n"
1.31 tedu 80: "\t%1$s -G [-n] [-c comment] -p pubkey -s seckey\n"
81: "\t%1$s -I [-p pubkey] [-s seckey] [-x sigfile]\n"
82: "\t%1$s -S [-e] [-x sigfile] -s seckey -m message\n"
1.15 espie 83: #endif
1.42 tedu 84: "\t%1$s -V [-eq] [-x sigfile] -p pubkey -m message\n",
1.15 espie 85: __progname);
1.1 tedu 86: exit(1);
87: }
88:
89: static int
90: xopen(const char *fname, int flags, mode_t mode)
91: {
92: int fd;
93:
1.31 tedu 94: if (strcmp(fname, "-") == 0) {
95: if ((flags & O_WRONLY))
96: fd = dup(STDOUT_FILENO);
97: else
98: fd = dup(STDIN_FILENO);
99: if (fd == -1)
100: err(1, "dup failed");
101: } else {
102: fd = open(fname, flags, mode);
103: if (fd == -1)
104: err(1, "can't open %s for %s", fname,
105: (flags & O_WRONLY) ? "writing" : "reading");
106: }
1.1 tedu 107: return fd;
108: }
109:
110: static void *
111: xmalloc(size_t len)
112: {
113: void *p;
114:
115: p = malloc(len);
116: if (!p)
117: err(1, "malloc %zu", len);
118: return p;
119: }
120:
1.16 tedu 121: static size_t
1.46 tedu 122: parseb64file(const char *filename, char *b64, void *buf, size_t buflen,
1.18 tedu 123: char *comment)
1.16 tedu 124: {
125: int rv;
126: char *commentend, *b64end;
127:
128: commentend = strchr(b64, '\n');
129: if (!commentend || commentend - b64 <= COMMENTHDRLEN ||
130: memcmp(b64, COMMENTHDR, COMMENTHDRLEN))
131: errx(1, "invalid comment in %s; must start with '%s'",
132: filename, COMMENTHDR);
1.18 tedu 133: *commentend = 0;
1.40 deraadt 134: if (comment) {
135: if (strlcpy(comment, b64 + COMMENTHDRLEN,
136: COMMENTMAXLEN) >= COMMENTMAXLEN)
137: err(1, "comment too long");
138: }
1.16 tedu 139: b64end = strchr(commentend + 1, '\n');
140: if (!b64end)
141: errx(1, "missing new line after b64 in %s", filename);
142: *b64end = 0;
1.46 tedu 143: rv = b64_pton(commentend + 1, buf, buflen);
144: if (rv != buflen)
1.16 tedu 145: errx(1, "invalid b64 encoding in %s", filename);
146: if (memcmp(buf, PKALG, 2))
147: errx(1, "unsupported file %s", filename);
148: return b64end - b64 + 1;
149: }
150:
1.1 tedu 151: static void
1.46 tedu 152: readb64file(const char *filename, void *buf, size_t buflen, char *comment)
1.1 tedu 153: {
154: char b64[2048];
1.10 tedu 155: int rv, fd;
1.1 tedu 156:
157: fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
158: memset(b64, 0, sizeof(b64));
159: rv = read(fd, b64, sizeof(b64) - 1);
160: if (rv == -1)
1.7 espie 161: err(1, "read from %s", filename);
1.46 tedu 162: parseb64file(filename, b64, buf, buflen, comment);
1.41 tedu 163: explicit_bzero(b64, sizeof(b64));
1.1 tedu 164: close(fd);
165: }
166:
1.35 tedu 167: static uint8_t *
1.1 tedu 168: readmsg(const char *filename, unsigned long long *msglenp)
169: {
1.49 tedu 170: unsigned long long msglen = 0;
171: uint8_t *msg = NULL;
1.1 tedu 172: struct stat sb;
1.49 tedu 173: ssize_t x, space;
1.1 tedu 174: int fd;
1.49 tedu 175: const size_t chunklen = 64 * 1024;
1.1 tedu 176:
177: fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
1.49 tedu 178: if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode)) {
179: if (sb.st_size > (1UL << 30))
180: errx(1, "msg too large in %s", filename);
181: space = sb.st_size + 1;
182: msg = xmalloc(space + 1);
183: } else {
184: space = 0;
185: }
186:
187: while (1) {
188: if (space == 0) {
189: space = chunklen;
190: if (!(msg = realloc(msg, msglen + space + 1)))
191: errx(1, "realloc");
192: }
193: if ((x = read(fd, msg + msglen, space)) == -1)
194: err(1, "read from %s", filename);
195: if (x == 0)
196: break;
197: space -= x;
198: msglen += x;
199: if (msglen > (1UL << 30))
200: errx(1, "msg too large in %s", filename);
201: }
202:
1.45 tedu 203: msg[msglen] = 0;
1.1 tedu 204: close(fd);
205:
206: *msglenp = msglen;
207: return msg;
208: }
209:
210: static void
1.46 tedu 211: writeall(int fd, const void *buf, size_t buflen, const char *filename)
1.1 tedu 212: {
1.11 tedu 213: ssize_t x;
1.38 espie 214:
1.46 tedu 215: while (buflen != 0) {
216: x = write(fd, buf, buflen);
1.38 espie 217: if (x == -1)
218: err(1, "write to %s", filename);
1.46 tedu 219: buflen -= x;
1.44 tedu 220: buf = (char*)buf + x;
1.7 espie 221: }
1.1 tedu 222: }
223:
1.17 deraadt 224: #ifndef VERIFYONLY
1.1 tedu 225: static void
1.46 tedu 226: appendall(const char *filename, const void *buf, size_t buflen)
1.16 tedu 227: {
228: int fd;
229:
1.30 tedu 230: fd = xopen(filename, O_NOFOLLOW | O_WRONLY | O_APPEND, 0);
1.46 tedu 231: writeall(fd, buf, buflen, filename);
1.16 tedu 232: close(fd);
233: }
234:
235: static void
1.1 tedu 236: writeb64file(const char *filename, const char *comment, const void *buf,
1.46 tedu 237: size_t buflen, int flags, mode_t mode)
1.1 tedu 238: {
239: char header[1024];
240: char b64[1024];
241: int fd, rv;
242:
1.30 tedu 243: fd = xopen(filename, O_CREAT|flags|O_NOFOLLOW|O_WRONLY, mode);
1.40 deraadt 244: if (snprintf(header, sizeof(header), "%s%s\n",
245: COMMENTHDR, comment) >= sizeof(header))
246: err(1, "comment too long");
1.7 espie 247: writeall(fd, header, strlen(header), filename);
1.46 tedu 248: if ((rv = b64_ntop(buf, buflen, b64, sizeof(b64)-1)) == -1)
1.1 tedu 249: errx(1, "b64 encode failed");
1.8 espie 250: b64[rv++] = '\n';
1.7 espie 251: writeall(fd, b64, rv, filename);
1.41 tedu 252: explicit_bzero(b64, sizeof(b64));
1.1 tedu 253: close(fd);
254: }
255:
256: static void
1.50 tedu 257: kdf(uint8_t *salt, size_t saltlen, int rounds, int allowstdin,
258: uint8_t *key, size_t keylen)
1.1 tedu 259: {
260: char pass[1024];
1.48 tedu 261: int rppflags = RPP_ECHO_OFF;
1.1 tedu 262:
263: if (rounds == 0) {
264: memset(key, 0, keylen);
265: return;
266: }
267:
1.50 tedu 268: if (allowstdin && !isatty(STDIN_FILENO))
1.48 tedu 269: rppflags |= RPP_STDIN;
270: if (!readpassphrase("passphrase: ", pass, sizeof(pass), rppflags))
1.39 tedu 271: errx(1, "unable to read passphrase");
1.37 tedu 272: if (strlen(pass) == 0)
273: errx(1, "please provide a password");
1.1 tedu 274: if (bcrypt_pbkdf(pass, strlen(pass), salt, saltlen, key,
275: keylen, rounds) == -1)
276: errx(1, "bcrypt pbkdf");
1.41 tedu 277: explicit_bzero(pass, sizeof(pass));
1.1 tedu 278: }
279:
280: static void
281: signmsg(uint8_t *seckey, uint8_t *msg, unsigned long long msglen,
282: uint8_t *sig)
283: {
284: unsigned long long siglen;
285: uint8_t *sigbuf;
286:
287: sigbuf = xmalloc(msglen + SIGBYTES);
288: crypto_sign_ed25519(sigbuf, &siglen, msg, msglen, seckey);
289: memcpy(sig, sigbuf, SIGBYTES);
290: free(sigbuf);
291: }
292:
293: static void
1.27 tedu 294: generate(const char *pubkeyfile, const char *seckeyfile, int rounds,
295: const char *comment)
1.1 tedu 296: {
297: uint8_t digest[SHA512_DIGEST_LENGTH];
298: struct pubkey pubkey;
299: struct enckey enckey;
300: uint8_t xorkey[sizeof(enckey.seckey)];
1.13 tedu 301: uint8_t fingerprint[FPLEN];
1.27 tedu 302: char commentbuf[COMMENTMAXLEN];
1.1 tedu 303: SHA2_CTX ctx;
304: int i;
305:
306: crypto_sign_ed25519_keypair(pubkey.pubkey, enckey.seckey);
1.13 tedu 307: arc4random_buf(fingerprint, sizeof(fingerprint));
1.1 tedu 308:
309: SHA512Init(&ctx);
310: SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
311: SHA512Final(digest, &ctx);
312:
313: memcpy(enckey.pkalg, PKALG, 2);
314: memcpy(enckey.kdfalg, KDFALG, 2);
315: enckey.kdfrounds = htonl(rounds);
1.13 tedu 316: memcpy(enckey.fingerprint, fingerprint, FPLEN);
1.1 tedu 317: arc4random_buf(enckey.salt, sizeof(enckey.salt));
1.50 tedu 318: kdf(enckey.salt, sizeof(enckey.salt), rounds, 1, xorkey, sizeof(xorkey));
1.1 tedu 319: memcpy(enckey.checksum, digest, sizeof(enckey.checksum));
320: for (i = 0; i < sizeof(enckey.seckey); i++)
321: enckey.seckey[i] ^= xorkey[i];
1.41 tedu 322: explicit_bzero(digest, sizeof(digest));
323: explicit_bzero(xorkey, sizeof(xorkey));
1.1 tedu 324:
1.40 deraadt 325: if (snprintf(commentbuf, sizeof(commentbuf), "%s secret key",
326: comment) >= sizeof(commentbuf))
327: err(1, "comment too long");
1.27 tedu 328: writeb64file(seckeyfile, commentbuf, &enckey,
1.20 espie 329: sizeof(enckey), O_EXCL, 0600);
1.41 tedu 330: explicit_bzero(&enckey, sizeof(enckey));
1.1 tedu 331:
332: memcpy(pubkey.pkalg, PKALG, 2);
1.13 tedu 333: memcpy(pubkey.fingerprint, fingerprint, FPLEN);
1.40 deraadt 334: if (snprintf(commentbuf, sizeof(commentbuf), "%s public key",
335: comment) >= sizeof(commentbuf))
336: err(1, "comment too long");
1.27 tedu 337: writeb64file(pubkeyfile, commentbuf, &pubkey,
1.20 espie 338: sizeof(pubkey), O_EXCL, 0666);
1.1 tedu 339: }
340:
341: static void
1.16 tedu 342: sign(const char *seckeyfile, const char *msgfile, const char *sigfile,
343: int embedded)
1.1 tedu 344: {
345: struct sig sig;
346: uint8_t digest[SHA512_DIGEST_LENGTH];
347: struct enckey enckey;
348: uint8_t xorkey[sizeof(enckey.seckey)];
349: uint8_t *msg;
1.51 ! tedu 350: char comment[COMMENTMAXLEN], sigcomment[COMMENTMAXLEN];
! 351: char *secname;
1.1 tedu 352: unsigned long long msglen;
353: int i, rounds;
354: SHA2_CTX ctx;
355:
1.18 tedu 356: readb64file(seckeyfile, &enckey, sizeof(enckey), comment);
1.1 tedu 357:
358: if (memcmp(enckey.kdfalg, KDFALG, 2))
359: errx(1, "unsupported KDF");
360: rounds = ntohl(enckey.kdfrounds);
1.50 tedu 361: kdf(enckey.salt, sizeof(enckey.salt), rounds, strcmp(msgfile, "-") != 0,
362: xorkey, sizeof(xorkey));
1.1 tedu 363: for (i = 0; i < sizeof(enckey.seckey); i++)
364: enckey.seckey[i] ^= xorkey[i];
1.41 tedu 365: explicit_bzero(xorkey, sizeof(xorkey));
1.1 tedu 366: SHA512Init(&ctx);
367: SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
368: SHA512Final(digest, &ctx);
369: if (memcmp(enckey.checksum, digest, sizeof(enckey.checksum)))
370: errx(1, "incorrect passphrase");
1.41 tedu 371: explicit_bzero(digest, sizeof(digest));
1.1 tedu 372:
1.16 tedu 373: msg = readmsg(msgfile, &msglen);
1.1 tedu 374:
375: signmsg(enckey.seckey, msg, msglen, sig.sig);
1.13 tedu 376: memcpy(sig.fingerprint, enckey.fingerprint, FPLEN);
1.41 tedu 377: explicit_bzero(&enckey, sizeof(enckey));
1.1 tedu 378:
379: memcpy(sig.pkalg, PKALG, 2);
1.51 ! tedu 380: if ((secname = strstr(seckeyfile, ".sec")) && strlen(secname) == 4) {
! 381: if (snprintf(sigcomment, sizeof(sigcomment), VERIFYWITH "%.*s.pub",
! 382: (int)strlen(seckeyfile) - 4, seckeyfile) >= sizeof(sigcomment));
! 383: } else {
! 384: if (snprintf(sigcomment, sizeof(sigcomment), "signature from %s",
! 385: comment) >= sizeof(sigcomment))
! 386: err(1, "comment too long");
! 387: }
1.20 espie 388: writeb64file(sigfile, sigcomment, &sig, sizeof(sig), O_TRUNC, 0666);
1.16 tedu 389: if (embedded)
390: appendall(sigfile, msg, msglen);
1.1 tedu 391:
392: free(msg);
393: }
1.22 tedu 394:
395: static void
396: inspect(const char *seckeyfile, const char *pubkeyfile, const char *sigfile)
397: {
398: struct sig sig;
399: struct enckey enckey;
400: struct pubkey pubkey;
401: char fp[(FPLEN + 2) / 3 * 4 + 1];
402:
403: if (seckeyfile) {
404: readb64file(seckeyfile, &enckey, sizeof(enckey), NULL);
405: b64_ntop(enckey.fingerprint, FPLEN, fp, sizeof(fp));
406: printf("sec fp: %s\n", fp);
407: }
408: if (pubkeyfile) {
409: readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
410: b64_ntop(pubkey.fingerprint, FPLEN, fp, sizeof(fp));
411: printf("pub fp: %s\n", fp);
412: }
413: if (sigfile) {
414: readb64file(sigfile, &sig, sizeof(sig), NULL);
415: b64_ntop(sig.fingerprint, FPLEN, fp, sizeof(fp));
416: printf("sig fp: %s\n", fp);
417: }
418: }
1.14 tedu 419: #endif
1.1 tedu 420:
421: static void
1.13 tedu 422: verifymsg(uint8_t *pubkey, uint8_t *msg, unsigned long long msglen,
1.42 tedu 423: uint8_t *sig, int quiet)
1.13 tedu 424: {
425: uint8_t *sigbuf, *dummybuf;
426: unsigned long long siglen, dummylen;
427:
428: siglen = SIGBYTES + msglen;
429: sigbuf = xmalloc(siglen);
430: dummybuf = xmalloc(siglen);
431: memcpy(sigbuf, sig, SIGBYTES);
432: memcpy(sigbuf + SIGBYTES, msg, msglen);
433: if (crypto_sign_ed25519_open(dummybuf, &dummylen, sigbuf, siglen,
434: pubkey) == -1)
435: errx(1, "signature verification failed");
1.42 tedu 436: if (!quiet)
437: printf("Signature Verified\n");
1.13 tedu 438: free(sigbuf);
439: free(dummybuf);
440: }
441:
442:
443: static void
1.16 tedu 444: verify(const char *pubkeyfile, const char *msgfile, const char *sigfile,
1.42 tedu 445: int embedded, int quiet)
1.1 tedu 446: {
1.51 ! tedu 447: char comment[COMMENTMAXLEN];
1.1 tedu 448: struct sig sig;
449: struct pubkey pubkey;
1.16 tedu 450: unsigned long long msglen, siglen = 0;
1.1 tedu 451: uint8_t *msg;
1.16 tedu 452: int fd;
453:
454: msg = readmsg(embedded ? sigfile : msgfile, &msglen);
1.1 tedu 455:
1.16 tedu 456: if (embedded) {
1.51 ! tedu 457: siglen = parseb64file(sigfile, msg, &sig, sizeof(sig), comment);
1.16 tedu 458: msg += siglen;
459: msglen -= siglen;
460: } else {
1.51 ! tedu 461: readb64file(sigfile, &sig, sizeof(sig), comment);
! 462: }
! 463: if (!pubkeyfile) {
! 464: if ((pubkeyfile = strstr(comment, VERIFYWITH)))
! 465: pubkeyfile += strlen(VERIFYWITH);
! 466: else
! 467: usage("need pubkey");
1.16 tedu 468: }
1.51 ! tedu 469: readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
1.13 tedu 470:
1.22 tedu 471: if (memcmp(pubkey.fingerprint, sig.fingerprint, FPLEN)) {
472: #ifndef VERIFYONLY
473: inspect(NULL, pubkeyfile, sigfile);
474: #endif
1.13 tedu 475: errx(1, "verification failed: checked against wrong key");
1.22 tedu 476: }
1.1 tedu 477:
1.42 tedu 478: verifymsg(pubkey.pubkey, msg, msglen, sig.sig, quiet);
1.16 tedu 479: if (embedded) {
1.30 tedu 480: fd = xopen(msgfile, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666);
1.16 tedu 481: writeall(fd, msg, msglen, msgfile);
482: close(fd);
483: }
1.1 tedu 484:
1.16 tedu 485: free(msg - siglen);
1.1 tedu 486: }
487:
1.42 tedu 488: #ifndef VERIFYONLY
489: struct checksum {
490: char file[1024];
491: char hash[1024];
492: char algo[256];
493: };
494:
495: static void
1.45 tedu 496: verifychecksums(char *msg, int argc, char **argv, int quiet)
1.42 tedu 497: {
498: char buf[1024];
1.45 tedu 499: char *line, *endline;
1.42 tedu 500: struct checksum *checksums = NULL, *c = NULL;
501: int nchecksums = 0;
1.43 tedu 502: int i, j, uselist, count, hasfailed;
1.42 tedu 503: int *failures;
504:
1.45 tedu 505: line = msg;
1.42 tedu 506: while (line && *line) {
507: if (!(checksums = realloc(checksums,
508: sizeof(*c) * (nchecksums + 1))))
509: err(1, "realloc");
510: c = &checksums[nchecksums++];
511: if ((endline = strchr(line, '\n')))
512: *endline++ = 0;
513: if (sscanf(line, "%255s %1023s = %1023s",
514: c->algo, buf, c->hash) != 3 ||
515: buf[0] != '(' || buf[strlen(buf) - 1] != ')')
516: errx(1, "unable to parse checksum line %s", line);
517: buf[strlen(buf) - 1] = 0;
518: strlcpy(c->file, buf + 1, sizeof(c->file));
519: line = endline;
520: }
521:
522: if (argc) {
523: uselist = 0;
524: count = argc;
525: } else {
526: uselist = 1;
527: count = nchecksums;
528: }
1.43 tedu 529: if (!(failures = calloc(count, sizeof(int))))
530: err(1, "calloc");
1.42 tedu 531: for (i = 0; i < count; i++) {
532: if (uselist) {
533: c = &checksums[i];
534: } else {
535: for (j = 0; j < nchecksums; j++) {
536: c = &checksums[j];
537: if (strcmp(c->file, argv[i]) == 0)
538: break;
539: }
540: if (j == nchecksums) {
541: failures[i] = 1;
542: continue;
543: }
544: }
545:
546: if (strcmp(c->algo, "SHA256") == 0) {
547: if (!SHA256File(c->file, buf)) {
548: failures[i] = 1;
549: continue;
550: }
551: } else if (strcmp(c->algo, "SHA512") == 0) {
552: if (!SHA512File(c->file, buf)) {
553: failures[i] = 1;
554: continue;
555: }
556: } else {
557: errx(1, "can't handle algorithm %s", c->algo);
558: }
559: if (strcmp(c->hash, buf) != 0) {
560: failures[i] = 1;
561: continue;
562: }
563: if (!quiet)
564: printf("%s: OK\n", c->file);
565: }
1.43 tedu 566: hasfailed = 0;
1.42 tedu 567: for (i = 0; i < count; i++) {
568: if (failures[i]) {
569: fprintf(stderr, "%s: FAIL\n",
570: uselist ? checksums[i].file : argv[i]);
1.43 tedu 571: hasfailed = 1;
1.42 tedu 572: }
573: }
1.43 tedu 574: if (hasfailed)
1.42 tedu 575: exit(1);
576: free(checksums);
1.43 tedu 577: free(failures);
1.42 tedu 578: }
579:
580: static void
581: check(const char *pubkeyfile, const char *sigfile, int quiet, int argc,
582: char **argv)
583: {
1.51 ! tedu 584: char comment[COMMENTMAXLEN];
1.42 tedu 585: struct sig sig;
586: struct pubkey pubkey;
587: unsigned long long msglen, siglen;
588: uint8_t *msg;
589:
590: msg = readmsg(sigfile, &msglen);
591:
1.51 ! tedu 592: siglen = parseb64file(sigfile, msg, &sig, sizeof(sig), NULL);
! 593: if (!pubkeyfile) {
! 594: if ((pubkeyfile = strstr(comment, VERIFYWITH)))
! 595: pubkeyfile += strlen(VERIFYWITH);
! 596: else
! 597: usage("need pubkey");
! 598: }
1.42 tedu 599: readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
600: msg += siglen;
601: msglen -= siglen;
602:
603: if (memcmp(pubkey.fingerprint, sig.fingerprint, FPLEN)) {
604: #ifndef VERIFYONLY
605: inspect(NULL, pubkeyfile, sigfile);
606: #endif
607: errx(1, "verification failed: checked against wrong key");
608: }
609:
610: verifymsg(pubkey.pubkey, msg, msglen, sig.sig, quiet);
1.45 tedu 611: verifychecksums((char *)msg, argc, argv, quiet);
1.42 tedu 612:
613: free(msg - siglen);
614: }
615: #endif
616:
1.1 tedu 617: int
618: main(int argc, char **argv)
619: {
1.16 tedu 620: const char *pubkeyfile = NULL, *seckeyfile = NULL, *msgfile = NULL,
1.1 tedu 621: *sigfile = NULL;
622: char sigfilebuf[1024];
1.27 tedu 623: const char *comment = "signify";
1.1 tedu 624: int ch, rounds;
1.16 tedu 625: int embedded = 0;
1.42 tedu 626: int quiet = 0;
1.6 tedu 627: enum {
628: NONE,
1.42 tedu 629: CHECK,
1.6 tedu 630: GENERATE,
1.22 tedu 631: INSPECT,
1.6 tedu 632: SIGN,
633: VERIFY
634: } verb = NONE;
635:
1.1 tedu 636:
637: rounds = 42;
638:
1.42 tedu 639: while ((ch = getopt(argc, argv, "CGISVc:em:np:qs:x:")) != -1) {
1.1 tedu 640: switch (ch) {
1.14 tedu 641: #ifndef VERIFYONLY
1.42 tedu 642: case 'C':
643: if (verb)
644: usage(NULL);
645: verb = CHECK;
646: break;
1.6 tedu 647: case 'G':
648: if (verb)
1.31 tedu 649: usage(NULL);
1.6 tedu 650: verb = GENERATE;
651: break;
1.22 tedu 652: case 'I':
653: if (verb)
1.31 tedu 654: usage(NULL);
1.22 tedu 655: verb = INSPECT;
656: break;
1.6 tedu 657: case 'S':
658: if (verb)
1.31 tedu 659: usage(NULL);
1.6 tedu 660: verb = SIGN;
661: break;
1.14 tedu 662: #endif
1.6 tedu 663: case 'V':
664: if (verb)
1.31 tedu 665: usage(NULL);
1.6 tedu 666: verb = VERIFY;
667: break;
1.27 tedu 668: case 'c':
669: comment = optarg;
670: break;
1.16 tedu 671: case 'e':
672: embedded = 1;
673: break;
1.31 tedu 674: case 'm':
675: msgfile = optarg;
676: break;
1.6 tedu 677: case 'n':
1.1 tedu 678: rounds = 0;
679: break;
1.6 tedu 680: case 'p':
1.1 tedu 681: pubkeyfile = optarg;
682: break;
1.42 tedu 683: case 'q':
684: quiet = 1;
685: break;
1.6 tedu 686: case 's':
1.1 tedu 687: seckeyfile = optarg;
688: break;
1.31 tedu 689: case 'x':
690: sigfile = optarg;
691: break;
1.1 tedu 692: default:
1.31 tedu 693: usage(NULL);
1.1 tedu 694: break;
695: }
696: }
1.2 tedu 697: argc -= optind;
1.9 espie 698: argv += optind;
699:
1.42 tedu 700: #ifndef VERIFYONLY
701: if (verb == CHECK) {
702: if (!pubkeyfile || !sigfile)
703: usage("need pubkey and sigfile");
704: check(pubkeyfile, sigfile, quiet, argc, argv);
705: return 0;
706: }
707: #endif
708:
709: quiet = 1; /* retain quiet default for 5.5 release */
710:
1.31 tedu 711: if (argc != 0)
712: usage(NULL);
713:
1.36 tedu 714: if (!sigfile && msgfile) {
715: if (strcmp(msgfile, "-") == 0)
716: errx(1, "must specify sigfile with - message");
717: if (snprintf(sigfilebuf, sizeof(sigfilebuf), "%s.sig",
718: msgfile) >= sizeof(sigfilebuf))
719: errx(1, "path too long");
720: sigfile = sigfilebuf;
721: }
1.1 tedu 722:
1.36 tedu 723: switch (verb) {
1.14 tedu 724: #ifndef VERIFYONLY
1.36 tedu 725: case GENERATE:
1.31 tedu 726: if (!pubkeyfile || !seckeyfile)
727: usage("need pubkey and seckey");
1.27 tedu 728: generate(pubkeyfile, seckeyfile, rounds, comment);
1.36 tedu 729: break;
730: case INSPECT:
1.22 tedu 731: inspect(seckeyfile, pubkeyfile, sigfile);
1.36 tedu 732: break;
733: case SIGN:
734: if (!msgfile || !seckeyfile)
735: usage("need message and seckey");
736: sign(seckeyfile, msgfile, sigfile, embedded);
737: break;
1.14 tedu 738: #endif
1.36 tedu 739: case VERIFY:
1.51 ! tedu 740: if (!msgfile)
! 741: usage("need message");
1.42 tedu 742: verify(pubkeyfile, msgfile, sigfile, embedded, quiet);
1.36 tedu 743: break;
744: default:
745: usage(NULL);
746: break;
1.1 tedu 747: }
1.9 espie 748:
1.1 tedu 749: return 0;
750: }