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

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