[BACK]Return to signify.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / signify

Annotation of src/usr.bin/signify/signify.c, Revision 1.75

1.75    ! tedu        1: /* $OpenBSD: signify.c,v 1.74 2014/05/06 23:19:46 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))
1.66      tedu      109:                errx(1, "not a valid 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 ||
1.65      tedu      133:            memcmp(b64, COMMENTHDR, COMMENTHDRLEN) != 0)
1.16      tedu      134:                errx(1, "invalid comment in %s; must start with '%s'",
                    135:                    filename, COMMENTHDR);
1.75    ! tedu      136:        *commentend = '\0';
1.40      deraadt   137:        if (comment) {
                    138:                if (strlcpy(comment, b64 + COMMENTHDRLEN,
                    139:                    COMMENTMAXLEN) >= COMMENTMAXLEN)
1.71      tedu      140:                        errx(1, "comment too long");
1.40      deraadt   141:        }
1.16      tedu      142:        b64end = strchr(commentend + 1, '\n');
                    143:        if (!b64end)
                    144:                errx(1, "missing new line after b64 in %s", filename);
1.75    ! tedu      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);
1.65      tedu      149:        if (memcmp(buf, PKALG, 2) != 0)
1.16      tedu      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:        rv = read(fd, b64, sizeof(b64) - 1);
                    162:        if (rv == -1)
1.7       espie     163:                err(1, "read from %s", filename);
1.75    ! tedu      164:        b64[rv] = '\0';
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.73      tedu      178:        const unsigned long long maxmsgsize = 1UL << 30;
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)) {
1.73      tedu      182:                if (sb.st_size > maxmsgsize)
1.49      tedu      183:                        errx(1, "msg too large in %s", filename);
                    184:                space = sb.st_size + 1;
                    185:        } else {
1.67      tedu      186:                space = 64 * 1024;
1.49      tedu      187:        }
                    188:
1.67      tedu      189:        msg = xmalloc(space + 1);
1.49      tedu      190:        while (1) {
                    191:                if (space == 0) {
1.73      tedu      192:                        if (msglen * 2 > maxmsgsize)
1.67      tedu      193:                                errx(1, "msg too large in %s", filename);
                    194:                        space = msglen;
1.49      tedu      195:                        if (!(msg = realloc(msg, msglen + space + 1)))
                    196:                                errx(1, "realloc");
                    197:                }
                    198:                if ((x = read(fd, msg + msglen, space)) == -1)
                    199:                        err(1, "read from %s", filename);
                    200:                if (x == 0)
                    201:                        break;
                    202:                space -= x;
                    203:                msglen += x;
                    204:        }
                    205:
1.75    ! 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))
1.71      tedu      239:                errx(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.70      tedu      252: kdf(uint8_t *salt, size_t saltlen, int rounds, int allowstdin, int confirm,
1.50      tedu      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.70      tedu      269:        if (confirm && !(rppflags & RPP_STDIN)) {
                    270:                char pass2[1024];
                    271:                if (!readpassphrase("confirm passphrase: ", pass2,
                    272:                    sizeof(pass2), rppflags))
                    273:                        errx(1, "unable to read passphrase");
                    274:                if (strcmp(pass, pass2) != 0)
                    275:                        errx(1, "passwords don't match");
                    276:                explicit_bzero(pass2, sizeof(pass2));
                    277:        }
1.1       tedu      278:        if (bcrypt_pbkdf(pass, strlen(pass), salt, saltlen, key,
                    279:            keylen, rounds) == -1)
                    280:                errx(1, "bcrypt pbkdf");
1.41      tedu      281:        explicit_bzero(pass, sizeof(pass));
1.1       tedu      282: }
                    283:
                    284: static void
                    285: signmsg(uint8_t *seckey, uint8_t *msg, unsigned long long msglen,
                    286:     uint8_t *sig)
                    287: {
                    288:        unsigned long long siglen;
                    289:        uint8_t *sigbuf;
                    290:
                    291:        sigbuf = xmalloc(msglen + SIGBYTES);
                    292:        crypto_sign_ed25519(sigbuf, &siglen, msg, msglen, seckey);
                    293:        memcpy(sig, sigbuf, SIGBYTES);
                    294:        free(sigbuf);
                    295: }
                    296:
                    297: static void
1.27      tedu      298: generate(const char *pubkeyfile, const char *seckeyfile, int rounds,
                    299:     const char *comment)
1.1       tedu      300: {
                    301:        uint8_t digest[SHA512_DIGEST_LENGTH];
                    302:        struct pubkey pubkey;
                    303:        struct enckey enckey;
                    304:        uint8_t xorkey[sizeof(enckey.seckey)];
1.13      tedu      305:        uint8_t fingerprint[FPLEN];
1.27      tedu      306:        char commentbuf[COMMENTMAXLEN];
1.1       tedu      307:        SHA2_CTX ctx;
                    308:        int i;
                    309:
                    310:        crypto_sign_ed25519_keypair(pubkey.pubkey, enckey.seckey);
1.13      tedu      311:        arc4random_buf(fingerprint, sizeof(fingerprint));
1.1       tedu      312:
                    313:        SHA512Init(&ctx);
                    314:        SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
                    315:        SHA512Final(digest, &ctx);
                    316:
                    317:        memcpy(enckey.pkalg, PKALG, 2);
                    318:        memcpy(enckey.kdfalg, KDFALG, 2);
                    319:        enckey.kdfrounds = htonl(rounds);
1.13      tedu      320:        memcpy(enckey.fingerprint, fingerprint, FPLEN);
1.1       tedu      321:        arc4random_buf(enckey.salt, sizeof(enckey.salt));
1.70      tedu      322:        kdf(enckey.salt, sizeof(enckey.salt), rounds, 1, 1, xorkey, sizeof(xorkey));
1.1       tedu      323:        memcpy(enckey.checksum, digest, sizeof(enckey.checksum));
                    324:        for (i = 0; i < sizeof(enckey.seckey); i++)
                    325:                enckey.seckey[i] ^= xorkey[i];
1.41      tedu      326:        explicit_bzero(digest, sizeof(digest));
                    327:        explicit_bzero(xorkey, sizeof(xorkey));
1.1       tedu      328:
1.40      deraadt   329:        if (snprintf(commentbuf, sizeof(commentbuf), "%s secret key",
                    330:            comment) >= sizeof(commentbuf))
1.71      tedu      331:                errx(1, "comment too long");
1.27      tedu      332:        writeb64file(seckeyfile, commentbuf, &enckey,
1.58      tedu      333:            sizeof(enckey), NULL, 0, O_EXCL, 0600);
1.41      tedu      334:        explicit_bzero(&enckey, sizeof(enckey));
1.1       tedu      335:
                    336:        memcpy(pubkey.pkalg, PKALG, 2);
1.13      tedu      337:        memcpy(pubkey.fingerprint, fingerprint, FPLEN);
1.40      deraadt   338:        if (snprintf(commentbuf, sizeof(commentbuf), "%s public key",
                    339:            comment) >= sizeof(commentbuf))
1.71      tedu      340:                errx(1, "comment too long");
1.27      tedu      341:        writeb64file(pubkeyfile, commentbuf, &pubkey,
1.58      tedu      342:            sizeof(pubkey), NULL, 0, O_EXCL, 0666);
1.1       tedu      343: }
                    344:
                    345: static void
1.16      tedu      346: sign(const char *seckeyfile, const char *msgfile, const char *sigfile,
                    347:     int embedded)
1.1       tedu      348: {
                    349:        struct sig sig;
                    350:        uint8_t digest[SHA512_DIGEST_LENGTH];
                    351:        struct enckey enckey;
                    352:        uint8_t xorkey[sizeof(enckey.seckey)];
                    353:        uint8_t *msg;
1.51      tedu      354:        char comment[COMMENTMAXLEN], sigcomment[COMMENTMAXLEN];
                    355:        char *secname;
1.1       tedu      356:        unsigned long long msglen;
                    357:        int i, rounds;
                    358:        SHA2_CTX ctx;
                    359:
1.18      tedu      360:        readb64file(seckeyfile, &enckey, sizeof(enckey), comment);
1.1       tedu      361:
1.65      tedu      362:        if (memcmp(enckey.kdfalg, KDFALG, 2) != 0)
1.1       tedu      363:                errx(1, "unsupported KDF");
                    364:        rounds = ntohl(enckey.kdfrounds);
1.50      tedu      365:        kdf(enckey.salt, sizeof(enckey.salt), rounds, strcmp(msgfile, "-") != 0,
1.70      tedu      366:            0, xorkey, sizeof(xorkey));
1.1       tedu      367:        for (i = 0; i < sizeof(enckey.seckey); i++)
                    368:                enckey.seckey[i] ^= xorkey[i];
1.41      tedu      369:        explicit_bzero(xorkey, sizeof(xorkey));
1.1       tedu      370:        SHA512Init(&ctx);
                    371:        SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
                    372:        SHA512Final(digest, &ctx);
1.65      tedu      373:        if (memcmp(enckey.checksum, digest, sizeof(enckey.checksum)) != 0)
1.1       tedu      374:            errx(1, "incorrect passphrase");
1.41      tedu      375:        explicit_bzero(digest, sizeof(digest));
1.1       tedu      376:
1.16      tedu      377:        msg = readmsg(msgfile, &msglen);
1.1       tedu      378:
                    379:        signmsg(enckey.seckey, msg, msglen, sig.sig);
1.13      tedu      380:        memcpy(sig.fingerprint, enckey.fingerprint, FPLEN);
1.41      tedu      381:        explicit_bzero(&enckey, sizeof(enckey));
1.1       tedu      382:
                    383:        memcpy(sig.pkalg, PKALG, 2);
1.51      tedu      384:        if ((secname = strstr(seckeyfile, ".sec")) && strlen(secname) == 4) {
                    385:                if (snprintf(sigcomment, sizeof(sigcomment), VERIFYWITH "%.*s.pub",
1.57      tedu      386:                    (int)strlen(seckeyfile) - 4, seckeyfile) >= sizeof(sigcomment))
1.71      tedu      387:                        errx(1, "comment too long");
1.51      tedu      388:        } else {
                    389:                if (snprintf(sigcomment, sizeof(sigcomment), "signature from %s",
                    390:                    comment) >= sizeof(sigcomment))
1.71      tedu      391:                        errx(1, "comment too long");
1.51      tedu      392:        }
1.16      tedu      393:        if (embedded)
1.58      tedu      394:                writeb64file(sigfile, sigcomment, &sig, sizeof(sig), msg,
                    395:                    msglen, O_TRUNC, 0666);
                    396:        else
                    397:                writeb64file(sigfile, sigcomment, &sig, sizeof(sig), NULL,
                    398:                    0, O_TRUNC, 0666);
1.1       tedu      399:
                    400:        free(msg);
                    401: }
1.22      tedu      402:
                    403: static void
                    404: inspect(const char *seckeyfile, const char *pubkeyfile, const char *sigfile)
                    405: {
                    406:        struct sig sig;
                    407:        struct enckey enckey;
                    408:        struct pubkey pubkey;
                    409:        char fp[(FPLEN + 2) / 3 * 4 + 1];
                    410:
                    411:        if (seckeyfile) {
                    412:                readb64file(seckeyfile, &enckey, sizeof(enckey), NULL);
                    413:                b64_ntop(enckey.fingerprint, FPLEN, fp, sizeof(fp));
                    414:                printf("sec fp: %s\n", fp);
                    415:        }
                    416:        if (pubkeyfile) {
                    417:                readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
                    418:                b64_ntop(pubkey.fingerprint, FPLEN, fp, sizeof(fp));
                    419:                printf("pub fp: %s\n", fp);
                    420:        }
                    421:        if (sigfile) {
                    422:                readb64file(sigfile, &sig, sizeof(sig), NULL);
                    423:                b64_ntop(sig.fingerprint, FPLEN, fp, sizeof(fp));
                    424:                printf("sig fp: %s\n", fp);
                    425:        }
                    426: }
1.14      tedu      427: #endif
1.1       tedu      428:
                    429: static void
1.62      tedu      430: verifymsg(struct pubkey *pubkey, uint8_t *msg, unsigned long long msglen,
                    431:     struct sig *sig, int quiet)
1.13      tedu      432: {
                    433:        uint8_t *sigbuf, *dummybuf;
                    434:        unsigned long long siglen, dummylen;
                    435:
1.65      tedu      436:        if (memcmp(pubkey->fingerprint, sig->fingerprint, FPLEN) != 0)
1.62      tedu      437:                errx(1, "verification failed: checked against wrong key");
                    438:
1.13      tedu      439:        siglen = SIGBYTES + msglen;
                    440:        sigbuf = xmalloc(siglen);
                    441:        dummybuf = xmalloc(siglen);
1.62      tedu      442:        memcpy(sigbuf, sig->sig, SIGBYTES);
1.13      tedu      443:        memcpy(sigbuf + SIGBYTES, msg, msglen);
                    444:        if (crypto_sign_ed25519_open(dummybuf, &dummylen, sigbuf, siglen,
1.62      tedu      445:            pubkey->pubkey) == -1)
1.13      tedu      446:                errx(1, "signature verification failed");
1.42      tedu      447:        if (!quiet)
                    448:                printf("Signature Verified\n");
1.13      tedu      449:        free(sigbuf);
                    450:        free(dummybuf);
                    451: }
                    452:
1.64      tedu      453: static void
                    454: readpubkey(const char *pubkeyfile, struct pubkey *pubkey,
                    455:     const char *sigcomment)
                    456: {
                    457:
                    458:        if (!pubkeyfile) {
                    459:                if ((pubkeyfile = strstr(sigcomment, 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
1.66      tedu      465:                        usage("must specify pubkey");
1.64      tedu      466:        }
                    467:        readb64file(pubkeyfile, pubkey, sizeof(*pubkey), NULL);
                    468: }
1.13      tedu      469:
                    470: static void
1.63      tedu      471: verifysimple(const char *pubkeyfile, const char *msgfile, const char *sigfile,
                    472:     int quiet)
1.1       tedu      473: {
1.64      tedu      474:        char sigcomment[COMMENTMAXLEN];
1.1       tedu      475:        struct sig sig;
                    476:        struct pubkey pubkey;
1.63      tedu      477:        unsigned long long msglen;
1.1       tedu      478:        uint8_t *msg;
1.16      tedu      479:
1.63      tedu      480:        msg = readmsg(msgfile, &msglen);
1.1       tedu      481:
1.64      tedu      482:        readb64file(sigfile, &sig, sizeof(sig), sigcomment);
                    483:        readpubkey(pubkeyfile, &pubkey, sigcomment);
1.63      tedu      484:
                    485:        verifymsg(&pubkey, msg, msglen, &sig, quiet);
                    486:
                    487:        free(msg);
                    488: }
                    489:
                    490: static uint8_t *
                    491: verifyembedded(const char *pubkeyfile, const char *sigfile,
                    492:     int quiet, unsigned long long *msglenp)
                    493: {
1.64      tedu      494:        char sigcomment[COMMENTMAXLEN];
1.63      tedu      495:        struct sig sig;
                    496:        struct pubkey pubkey;
                    497:        unsigned long long msglen, siglen;
                    498:        uint8_t *msg;
                    499:
                    500:        msg = readmsg(sigfile, &msglen);
                    501:
1.64      tedu      502:        siglen = parseb64file(sigfile, msg, &sig, sizeof(sig), sigcomment);
                    503:        readpubkey(pubkeyfile, &pubkey, sigcomment);
                    504:
1.63      tedu      505:        msglen -= siglen;
                    506:        memmove(msg, msg + siglen, msglen);
                    507:        msg[msglen] = 0;
1.13      tedu      508:
1.62      tedu      509:        verifymsg(&pubkey, msg, msglen, &sig, quiet);
1.63      tedu      510:
                    511:        *msglenp = msglen;
                    512:        return msg;
                    513: }
                    514:
                    515: static void
                    516: verify(const char *pubkeyfile, const char *msgfile, const char *sigfile,
                    517:     int embedded, int quiet)
                    518: {
                    519:        unsigned long long msglen;
                    520:        uint8_t *msg;
                    521:        int fd;
                    522:
1.16      tedu      523:        if (embedded) {
1.63      tedu      524:                msg = verifyembedded(pubkeyfile, sigfile, quiet, &msglen);
1.30      tedu      525:                fd = xopen(msgfile, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666);
1.16      tedu      526:                writeall(fd, msg, msglen, msgfile);
1.68      espie     527:                free(msg);
1.16      tedu      528:                close(fd);
1.63      tedu      529:        } else {
                    530:                verifysimple(pubkeyfile, msgfile, sigfile, quiet);
1.16      tedu      531:        }
1.1       tedu      532: }
                    533:
1.42      tedu      534: #ifndef VERIFYONLY
                    535: struct checksum {
                    536:        char file[1024];
                    537:        char hash[1024];
                    538:        char algo[256];
                    539: };
                    540:
                    541: static void
1.45      tedu      542: verifychecksums(char *msg, int argc, char **argv, int quiet)
1.42      tedu      543: {
                    544:        char buf[1024];
1.45      tedu      545:        char *line, *endline;
1.42      tedu      546:        struct checksum *checksums = NULL, *c = NULL;
                    547:        int nchecksums = 0;
1.43      tedu      548:        int i, j, uselist, count, hasfailed;
1.42      tedu      549:        int *failures;
                    550:
1.45      tedu      551:        line = msg;
1.42      tedu      552:        while (line && *line) {
1.72      tedu      553:                if (!(checksums = reallocarray(checksums,
                    554:                    nchecksums + 1, sizeof(*checksums))))
1.42      tedu      555:                        err(1, "realloc");
                    556:                c = &checksums[nchecksums++];
                    557:                if ((endline = strchr(line, '\n')))
                    558:                        *endline++ = 0;
                    559:                if (sscanf(line, "%255s %1023s = %1023s",
                    560:                    c->algo, buf, c->hash) != 3 ||
                    561:                    buf[0] != '(' || buf[strlen(buf) - 1] != ')')
                    562:                        errx(1, "unable to parse checksum line %s", line);
                    563:                buf[strlen(buf) - 1] = 0;
                    564:                strlcpy(c->file, buf + 1, sizeof(c->file));
                    565:                line = endline;
                    566:        }
                    567:
                    568:        if (argc) {
                    569:                uselist = 0;
                    570:                count = argc;
                    571:        } else {
                    572:                uselist = 1;
                    573:                count = nchecksums;
                    574:        }
1.74      tedu      575:        if (!(failures = calloc(count, sizeof(*failures))))
1.43      tedu      576:                err(1, "calloc");
1.42      tedu      577:        for (i = 0; i < count; i++) {
                    578:                if (uselist) {
                    579:                        c = &checksums[i];
                    580:                } else {
                    581:                        for (j = 0; j < nchecksums; j++) {
                    582:                                c = &checksums[j];
                    583:                                if (strcmp(c->file, argv[i]) == 0)
                    584:                                        break;
                    585:                        }
                    586:                        if (j == nchecksums) {
                    587:                                failures[i] = 1;
                    588:                                continue;
                    589:                        }
                    590:                }
                    591:
                    592:                if (strcmp(c->algo, "SHA256") == 0) {
                    593:                        if (!SHA256File(c->file, buf)) {
                    594:                                failures[i] = 1;
                    595:                                continue;
                    596:                        }
                    597:                } else if (strcmp(c->algo, "SHA512") == 0) {
                    598:                        if (!SHA512File(c->file, buf)) {
                    599:                                failures[i] = 1;
                    600:                                continue;
                    601:                        }
                    602:                } else {
                    603:                        errx(1, "can't handle algorithm %s", c->algo);
                    604:                }
                    605:                if (strcmp(c->hash, buf) != 0) {
                    606:                        failures[i] = 1;
                    607:                        continue;
                    608:                }
                    609:                if (!quiet)
                    610:                        printf("%s: OK\n", c->file);
                    611:        }
1.43      tedu      612:        hasfailed = 0;
1.42      tedu      613:        for (i = 0; i < count; i++) {
                    614:                if (failures[i]) {
                    615:                        fprintf(stderr, "%s: FAIL\n",
                    616:                            uselist ? checksums[i].file : argv[i]);
1.43      tedu      617:                        hasfailed = 1;
1.42      tedu      618:                }
                    619:        }
1.43      tedu      620:        if (hasfailed)
1.42      tedu      621:                exit(1);
                    622:        free(checksums);
1.43      tedu      623:        free(failures);
1.42      tedu      624: }
                    625:
                    626: static void
                    627: check(const char *pubkeyfile, const char *sigfile, int quiet, int argc,
                    628:     char **argv)
                    629: {
1.63      tedu      630:        unsigned long long msglen;
1.42      tedu      631:        uint8_t *msg;
                    632:
1.63      tedu      633:        msg = verifyembedded(pubkeyfile, sigfile, quiet, &msglen);
1.45      tedu      634:        verifychecksums((char *)msg, argc, argv, quiet);
1.42      tedu      635:
1.63      tedu      636:        free(msg);
1.42      tedu      637: }
                    638: #endif
                    639:
1.1       tedu      640: int
                    641: main(int argc, char **argv)
                    642: {
1.16      tedu      643:        const char *pubkeyfile = NULL, *seckeyfile = NULL, *msgfile = NULL,
1.1       tedu      644:            *sigfile = NULL;
                    645:        char sigfilebuf[1024];
1.27      tedu      646:        const char *comment = "signify";
1.1       tedu      647:        int ch, rounds;
1.16      tedu      648:        int embedded = 0;
1.42      tedu      649:        int quiet = 0;
1.6       tedu      650:        enum {
                    651:                NONE,
1.42      tedu      652:                CHECK,
1.6       tedu      653:                GENERATE,
1.22      tedu      654:                INSPECT,
1.6       tedu      655:                SIGN,
                    656:                VERIFY
                    657:        } verb = NONE;
                    658:
1.1       tedu      659:
                    660:        rounds = 42;
                    661:
1.42      tedu      662:        while ((ch = getopt(argc, argv, "CGISVc:em:np:qs:x:")) != -1) {
1.1       tedu      663:                switch (ch) {
1.14      tedu      664: #ifndef VERIFYONLY
1.42      tedu      665:                case 'C':
                    666:                        if (verb)
                    667:                                usage(NULL);
                    668:                        verb = CHECK;
                    669:                        break;
1.6       tedu      670:                case 'G':
                    671:                        if (verb)
1.31      tedu      672:                                usage(NULL);
1.6       tedu      673:                        verb = GENERATE;
                    674:                        break;
1.22      tedu      675:                case 'I':
                    676:                        if (verb)
1.31      tedu      677:                                usage(NULL);
1.22      tedu      678:                        verb = INSPECT;
                    679:                        break;
1.6       tedu      680:                case 'S':
                    681:                        if (verb)
1.31      tedu      682:                                usage(NULL);
1.6       tedu      683:                        verb = SIGN;
                    684:                        break;
1.14      tedu      685: #endif
1.6       tedu      686:                case 'V':
                    687:                        if (verb)
1.31      tedu      688:                                usage(NULL);
1.6       tedu      689:                        verb = VERIFY;
                    690:                        break;
1.27      tedu      691:                case 'c':
                    692:                        comment = optarg;
                    693:                        break;
1.16      tedu      694:                case 'e':
                    695:                        embedded = 1;
                    696:                        break;
1.31      tedu      697:                case 'm':
                    698:                        msgfile = optarg;
                    699:                        break;
1.6       tedu      700:                case 'n':
1.1       tedu      701:                        rounds = 0;
                    702:                        break;
1.6       tedu      703:                case 'p':
1.1       tedu      704:                        pubkeyfile = optarg;
                    705:                        break;
1.42      tedu      706:                case 'q':
                    707:                        quiet = 1;
                    708:                        break;
1.6       tedu      709:                case 's':
1.1       tedu      710:                        seckeyfile = optarg;
                    711:                        break;
1.31      tedu      712:                case 'x':
                    713:                        sigfile = optarg;
                    714:                        break;
1.1       tedu      715:                default:
1.31      tedu      716:                        usage(NULL);
1.1       tedu      717:                        break;
                    718:                }
                    719:        }
1.2       tedu      720:        argc -= optind;
1.9       espie     721:        argv += optind;
                    722:
1.42      tedu      723: #ifndef VERIFYONLY
                    724:        if (verb == CHECK) {
                    725:                if (!pubkeyfile || !sigfile)
1.66      tedu      726:                        usage("must specify pubkey and sigfile");
1.42      tedu      727:                check(pubkeyfile, sigfile, quiet, argc, argv);
                    728:                return 0;
                    729:        }
                    730: #endif
                    731:
1.31      tedu      732:        if (argc != 0)
                    733:                usage(NULL);
                    734:
1.36      tedu      735:        if (!sigfile && msgfile) {
                    736:                if (strcmp(msgfile, "-") == 0)
1.66      tedu      737:                        usage("must specify sigfile with - message");
1.36      tedu      738:                if (snprintf(sigfilebuf, sizeof(sigfilebuf), "%s.sig",
                    739:                    msgfile) >= sizeof(sigfilebuf))
                    740:                        errx(1, "path too long");
                    741:                sigfile = sigfilebuf;
                    742:        }
1.1       tedu      743:
1.36      tedu      744:        switch (verb) {
1.14      tedu      745: #ifndef VERIFYONLY
1.36      tedu      746:        case GENERATE:
1.31      tedu      747:                if (!pubkeyfile || !seckeyfile)
1.66      tedu      748:                        usage("must specify pubkey and seckey");
1.27      tedu      749:                generate(pubkeyfile, seckeyfile, rounds, comment);
1.36      tedu      750:                break;
                    751:        case INSPECT:
1.22      tedu      752:                inspect(seckeyfile, pubkeyfile, sigfile);
1.36      tedu      753:                break;
                    754:        case SIGN:
                    755:                if (!msgfile || !seckeyfile)
1.66      tedu      756:                        usage("must specify message and seckey");
1.36      tedu      757:                sign(seckeyfile, msgfile, sigfile, embedded);
                    758:                break;
1.14      tedu      759: #endif
1.36      tedu      760:        case VERIFY:
1.51      tedu      761:                if (!msgfile)
1.66      tedu      762:                        usage("must specify message");
1.42      tedu      763:                verify(pubkeyfile, msgfile, sigfile, embedded, quiet);
1.36      tedu      764:                break;
                    765:        default:
                    766:                usage(NULL);
                    767:                break;
1.1       tedu      768:        }
1.9       espie     769:
1.1       tedu      770:        return 0;
                    771: }