[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.67

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