Annotation of src/usr.bin/signify/signify.c, Revision 1.63
1.63 ! tedu 1: /* $OpenBSD: signify.c,v 1.60 2014/03/17 00:01:58 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
1.61 deraadt 90: xopen(const char *fname, int oflags, mode_t mode)
1.1 tedu 91: {
1.53 tedu 92: struct stat sb;
1.1 tedu 93: int fd;
94:
1.31 tedu 95: if (strcmp(fname, "-") == 0) {
1.61 deraadt 96: if ((oflags & O_WRONLY))
1.31 tedu 97: fd = dup(STDOUT_FILENO);
98: else
99: fd = dup(STDIN_FILENO);
100: if (fd == -1)
101: err(1, "dup failed");
102: } else {
1.61 deraadt 103: fd = open(fname, oflags, mode);
1.31 tedu 104: if (fd == -1)
105: err(1, "can't open %s for %s", fname,
1.61 deraadt 106: (oflags & O_WRONLY) ? "writing" : "reading");
1.31 tedu 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.61 deraadt 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.61 deraadt 230: size_t buflen, const void *msg, size_t msglen, int oflags, mode_t mode)
1.1 tedu 231: {
232: char header[1024];
233: char b64[1024];
234: int fd, rv;
235:
1.61 deraadt 236: fd = xopen(filename, O_CREAT|oflags|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.62 tedu 421: verifymsg(struct pubkey *pubkey, uint8_t *msg, unsigned long long msglen,
422: struct sig *sig, int quiet)
1.13 tedu 423: {
424: uint8_t *sigbuf, *dummybuf;
425: unsigned long long siglen, dummylen;
426:
1.62 tedu 427: if (memcmp(pubkey->fingerprint, sig->fingerprint, FPLEN))
428: errx(1, "verification failed: checked against wrong key");
429:
1.13 tedu 430: siglen = SIGBYTES + msglen;
431: sigbuf = xmalloc(siglen);
432: dummybuf = xmalloc(siglen);
1.62 tedu 433: memcpy(sigbuf, sig->sig, SIGBYTES);
1.13 tedu 434: memcpy(sigbuf + SIGBYTES, msg, msglen);
435: if (crypto_sign_ed25519_open(dummybuf, &dummylen, sigbuf, siglen,
1.62 tedu 436: pubkey->pubkey) == -1)
1.13 tedu 437: errx(1, "signature verification failed");
1.42 tedu 438: if (!quiet)
439: printf("Signature Verified\n");
1.13 tedu 440: free(sigbuf);
441: free(dummybuf);
442: }
443:
444:
445: static void
1.63 ! tedu 446: verifysimple(const char *pubkeyfile, const char *msgfile, const char *sigfile,
! 447: int quiet)
1.1 tedu 448: {
1.51 tedu 449: char comment[COMMENTMAXLEN];
1.1 tedu 450: struct sig sig;
451: struct pubkey pubkey;
1.63 ! tedu 452: unsigned long long msglen;
1.1 tedu 453: uint8_t *msg;
1.16 tedu 454:
1.63 ! tedu 455: msg = readmsg(msgfile, &msglen);
1.1 tedu 456:
1.63 ! tedu 457: readb64file(sigfile, &sig, sizeof(sig), comment);
! 458: if (!pubkeyfile) {
! 459: if ((pubkeyfile = strstr(comment, VERIFYWITH))) {
! 460: pubkeyfile += strlen(VERIFYWITH);
! 461: if (strncmp(pubkeyfile, "/etc/signify/", 13) != 0 ||
! 462: strstr(pubkeyfile, "/../") != NULL)
! 463: errx(1, "untrusted path %s", pubkeyfile);
! 464: } else
! 465: usage("need pubkey");
1.51 tedu 466: }
1.63 ! tedu 467: readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
! 468:
! 469: verifymsg(&pubkey, msg, msglen, &sig, quiet);
! 470:
! 471: free(msg);
! 472: }
! 473:
! 474: static uint8_t *
! 475: verifyembedded(const char *pubkeyfile, const char *sigfile,
! 476: int quiet, unsigned long long *msglenp)
! 477: {
! 478: char comment[COMMENTMAXLEN];
! 479: struct sig sig;
! 480: struct pubkey pubkey;
! 481: unsigned long long msglen, siglen;
! 482: uint8_t *msg;
! 483:
! 484: msg = readmsg(sigfile, &msglen);
! 485:
! 486: siglen = parseb64file(sigfile, msg, &sig, sizeof(sig), comment);
! 487: msglen -= siglen;
! 488: memmove(msg, msg + siglen, msglen);
! 489: msg[msglen] = 0;
1.51 tedu 490: if (!pubkeyfile) {
1.52 tedu 491: if ((pubkeyfile = strstr(comment, VERIFYWITH))) {
1.51 tedu 492: pubkeyfile += strlen(VERIFYWITH);
1.56 tedu 493: if (strncmp(pubkeyfile, "/etc/signify/", 13) != 0 ||
1.54 tedu 494: strstr(pubkeyfile, "/../") != NULL)
1.52 tedu 495: errx(1, "untrusted path %s", pubkeyfile);
496: } else
1.51 tedu 497: usage("need pubkey");
1.16 tedu 498: }
1.51 tedu 499: readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
1.13 tedu 500:
1.62 tedu 501: verifymsg(&pubkey, msg, msglen, &sig, quiet);
1.63 ! tedu 502:
! 503: *msglenp = msglen;
! 504: return msg;
! 505: }
! 506:
! 507: static void
! 508: verify(const char *pubkeyfile, const char *msgfile, const char *sigfile,
! 509: int embedded, int quiet)
! 510: {
! 511: unsigned long long msglen;
! 512: uint8_t *msg;
! 513: int fd;
! 514:
1.16 tedu 515: if (embedded) {
1.63 ! tedu 516: msg = verifyembedded(pubkeyfile, sigfile, quiet, &msglen);
1.30 tedu 517: fd = xopen(msgfile, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666);
1.16 tedu 518: writeall(fd, msg, msglen, msgfile);
519: close(fd);
1.63 ! tedu 520: } else {
! 521: verifysimple(pubkeyfile, msgfile, sigfile, quiet);
1.16 tedu 522: }
1.1 tedu 523: }
524:
1.42 tedu 525: #ifndef VERIFYONLY
526: struct checksum {
527: char file[1024];
528: char hash[1024];
529: char algo[256];
530: };
531:
532: static void
1.45 tedu 533: verifychecksums(char *msg, int argc, char **argv, int quiet)
1.42 tedu 534: {
535: char buf[1024];
1.45 tedu 536: char *line, *endline;
1.42 tedu 537: struct checksum *checksums = NULL, *c = NULL;
538: int nchecksums = 0;
1.43 tedu 539: int i, j, uselist, count, hasfailed;
1.42 tedu 540: int *failures;
541:
1.45 tedu 542: line = msg;
1.42 tedu 543: while (line && *line) {
544: if (!(checksums = realloc(checksums,
545: sizeof(*c) * (nchecksums + 1))))
546: err(1, "realloc");
547: c = &checksums[nchecksums++];
548: if ((endline = strchr(line, '\n')))
549: *endline++ = 0;
550: if (sscanf(line, "%255s %1023s = %1023s",
551: c->algo, buf, c->hash) != 3 ||
552: buf[0] != '(' || buf[strlen(buf) - 1] != ')')
553: errx(1, "unable to parse checksum line %s", line);
554: buf[strlen(buf) - 1] = 0;
555: strlcpy(c->file, buf + 1, sizeof(c->file));
556: line = endline;
557: }
558:
559: if (argc) {
560: uselist = 0;
561: count = argc;
562: } else {
563: uselist = 1;
564: count = nchecksums;
565: }
1.43 tedu 566: if (!(failures = calloc(count, sizeof(int))))
567: err(1, "calloc");
1.42 tedu 568: for (i = 0; i < count; i++) {
569: if (uselist) {
570: c = &checksums[i];
571: } else {
572: for (j = 0; j < nchecksums; j++) {
573: c = &checksums[j];
574: if (strcmp(c->file, argv[i]) == 0)
575: break;
576: }
577: if (j == nchecksums) {
578: failures[i] = 1;
579: continue;
580: }
581: }
582:
583: if (strcmp(c->algo, "SHA256") == 0) {
584: if (!SHA256File(c->file, buf)) {
585: failures[i] = 1;
586: continue;
587: }
588: } else if (strcmp(c->algo, "SHA512") == 0) {
589: if (!SHA512File(c->file, buf)) {
590: failures[i] = 1;
591: continue;
592: }
593: } else {
594: errx(1, "can't handle algorithm %s", c->algo);
595: }
596: if (strcmp(c->hash, buf) != 0) {
597: failures[i] = 1;
598: continue;
599: }
600: if (!quiet)
601: printf("%s: OK\n", c->file);
602: }
1.43 tedu 603: hasfailed = 0;
1.42 tedu 604: for (i = 0; i < count; i++) {
605: if (failures[i]) {
606: fprintf(stderr, "%s: FAIL\n",
607: uselist ? checksums[i].file : argv[i]);
1.43 tedu 608: hasfailed = 1;
1.42 tedu 609: }
610: }
1.43 tedu 611: if (hasfailed)
1.42 tedu 612: exit(1);
613: free(checksums);
1.43 tedu 614: free(failures);
1.42 tedu 615: }
616:
617: static void
618: check(const char *pubkeyfile, const char *sigfile, int quiet, int argc,
619: char **argv)
620: {
1.63 ! tedu 621: unsigned long long msglen;
1.42 tedu 622: uint8_t *msg;
623:
1.63 ! tedu 624: msg = verifyembedded(pubkeyfile, sigfile, quiet, &msglen);
1.45 tedu 625: verifychecksums((char *)msg, argc, argv, quiet);
1.42 tedu 626:
1.63 ! tedu 627: free(msg);
1.42 tedu 628: }
629: #endif
630:
1.1 tedu 631: int
632: main(int argc, char **argv)
633: {
1.16 tedu 634: const char *pubkeyfile = NULL, *seckeyfile = NULL, *msgfile = NULL,
1.1 tedu 635: *sigfile = NULL;
636: char sigfilebuf[1024];
1.27 tedu 637: const char *comment = "signify";
1.1 tedu 638: int ch, rounds;
1.16 tedu 639: int embedded = 0;
1.42 tedu 640: int quiet = 0;
1.6 tedu 641: enum {
642: NONE,
1.42 tedu 643: CHECK,
1.6 tedu 644: GENERATE,
1.22 tedu 645: INSPECT,
1.6 tedu 646: SIGN,
647: VERIFY
648: } verb = NONE;
649:
1.1 tedu 650:
651: rounds = 42;
652:
1.42 tedu 653: while ((ch = getopt(argc, argv, "CGISVc:em:np:qs:x:")) != -1) {
1.1 tedu 654: switch (ch) {
1.14 tedu 655: #ifndef VERIFYONLY
1.42 tedu 656: case 'C':
657: if (verb)
658: usage(NULL);
659: verb = CHECK;
660: break;
1.6 tedu 661: case 'G':
662: if (verb)
1.31 tedu 663: usage(NULL);
1.6 tedu 664: verb = GENERATE;
665: break;
1.22 tedu 666: case 'I':
667: if (verb)
1.31 tedu 668: usage(NULL);
1.22 tedu 669: verb = INSPECT;
670: break;
1.6 tedu 671: case 'S':
672: if (verb)
1.31 tedu 673: usage(NULL);
1.6 tedu 674: verb = SIGN;
675: break;
1.14 tedu 676: #endif
1.6 tedu 677: case 'V':
678: if (verb)
1.31 tedu 679: usage(NULL);
1.6 tedu 680: verb = VERIFY;
681: break;
1.27 tedu 682: case 'c':
683: comment = optarg;
684: break;
1.16 tedu 685: case 'e':
686: embedded = 1;
687: break;
1.31 tedu 688: case 'm':
689: msgfile = optarg;
690: break;
1.6 tedu 691: case 'n':
1.1 tedu 692: rounds = 0;
693: break;
1.6 tedu 694: case 'p':
1.1 tedu 695: pubkeyfile = optarg;
696: break;
1.42 tedu 697: case 'q':
698: quiet = 1;
699: break;
1.6 tedu 700: case 's':
1.1 tedu 701: seckeyfile = optarg;
702: break;
1.31 tedu 703: case 'x':
704: sigfile = optarg;
705: break;
1.1 tedu 706: default:
1.31 tedu 707: usage(NULL);
1.1 tedu 708: break;
709: }
710: }
1.2 tedu 711: argc -= optind;
1.9 espie 712: argv += optind;
713:
1.42 tedu 714: #ifndef VERIFYONLY
715: if (verb == CHECK) {
716: if (!pubkeyfile || !sigfile)
717: usage("need pubkey and sigfile");
718: check(pubkeyfile, sigfile, quiet, argc, argv);
719: return 0;
720: }
721: #endif
722:
723: quiet = 1; /* retain quiet default for 5.5 release */
724:
1.31 tedu 725: if (argc != 0)
726: usage(NULL);
727:
1.36 tedu 728: if (!sigfile && msgfile) {
729: if (strcmp(msgfile, "-") == 0)
730: errx(1, "must specify sigfile with - message");
731: if (snprintf(sigfilebuf, sizeof(sigfilebuf), "%s.sig",
732: msgfile) >= sizeof(sigfilebuf))
733: errx(1, "path too long");
734: sigfile = sigfilebuf;
735: }
1.1 tedu 736:
1.36 tedu 737: switch (verb) {
1.14 tedu 738: #ifndef VERIFYONLY
1.36 tedu 739: case GENERATE:
1.31 tedu 740: if (!pubkeyfile || !seckeyfile)
741: usage("need pubkey and seckey");
1.27 tedu 742: generate(pubkeyfile, seckeyfile, rounds, comment);
1.36 tedu 743: break;
744: case INSPECT:
1.22 tedu 745: inspect(seckeyfile, pubkeyfile, sigfile);
1.36 tedu 746: break;
747: case SIGN:
748: if (!msgfile || !seckeyfile)
749: usage("need message and seckey");
750: sign(seckeyfile, msgfile, sigfile, embedded);
751: break;
1.14 tedu 752: #endif
1.36 tedu 753: case VERIFY:
1.51 tedu 754: if (!msgfile)
755: usage("need message");
1.42 tedu 756: verify(pubkeyfile, msgfile, sigfile, embedded, quiet);
1.36 tedu 757: break;
758: default:
759: usage(NULL);
760: break;
1.1 tedu 761: }
1.9 espie 762:
1.1 tedu 763: return 0;
764: }