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