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

1.130   ! tedu        1: /* $OpenBSD: signify.c,v 1.129 2019/01/17 05:31:28 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:
1.98      tedu       22: #include <limits.h>
1.1       tedu       23: #include <stdint.h>
                     24: #include <fcntl.h>
                     25: #include <string.h>
                     26: #include <stdio.h>
1.34      tedu       27: #include <stdlib.h>
1.96      tedu       28: #include <stddef.h>
                     29: #include <ohash.h>
1.1       tedu       30: #include <err.h>
                     31: #include <unistd.h>
                     32: #include <readpassphrase.h>
                     33: #include <util.h>
                     34: #include <sha2.h>
                     35:
                     36: #include "crypto_api.h"
1.111     espie      37: #include "signify.h"
1.1       tedu       38:
                     39: #define SIGBYTES crypto_sign_ed25519_BYTES
                     40: #define SECRETBYTES crypto_sign_ed25519_SECRETKEYBYTES
                     41: #define PUBLICBYTES crypto_sign_ed25519_PUBLICKEYBYTES
                     42:
                     43: #define PKALG "Ed"
                     44: #define KDFALG "BK"
1.94      tedu       45: #define KEYNUMLEN 8
1.13      tedu       46:
1.18      tedu       47: #define COMMENTHDR "untrusted comment: "
                     48: #define COMMENTHDRLEN 19
                     49: #define COMMENTMAXLEN 1024
1.51      tedu       50: #define VERIFYWITH "verify with "
1.1       tedu       51:
                     52: struct enckey {
                     53:        uint8_t pkalg[2];
                     54:        uint8_t kdfalg[2];
                     55:        uint32_t kdfrounds;
                     56:        uint8_t salt[16];
                     57:        uint8_t checksum[8];
1.94      tedu       58:        uint8_t keynum[KEYNUMLEN];
1.1       tedu       59:        uint8_t seckey[SECRETBYTES];
                     60: };
                     61:
                     62: struct pubkey {
                     63:        uint8_t pkalg[2];
1.94      tedu       64:        uint8_t keynum[KEYNUMLEN];
1.1       tedu       65:        uint8_t pubkey[PUBLICBYTES];
                     66: };
                     67:
                     68: struct sig {
                     69:        uint8_t pkalg[2];
1.94      tedu       70:        uint8_t keynum[KEYNUMLEN];
1.1       tedu       71:        uint8_t sig[SIGBYTES];
                     72: };
                     73:
1.103     tedu       74: static void __dead
1.31      tedu       75: usage(const char *error)
1.1       tedu       76: {
1.31      tedu       77:        if (error)
                     78:                fprintf(stderr, "%s\n", error);
1.9       espie      79:        fprintf(stderr, "usage:"
1.15      espie      80: #ifndef VERIFYONLY
1.47      naddy      81:            "\t%1$s -C [-q] -p pubkey -x sigfile [file ...]\n"
1.31      tedu       82:            "\t%1$s -G [-n] [-c comment] -p pubkey -s seckey\n"
1.111     espie      83:            "\t%1$s -S [-ez] [-x sigfile] -s seckey -m message\n"
1.15      espie      84: #endif
1.112     espie      85:            "\t%1$s -V [-eqz] [-p pubkey] [-t keytype] [-x sigfile] -m message\n",
1.106     tedu       86:            getprogname());
1.1       tedu       87:        exit(1);
                     88: }
                     89:
1.111     espie      90: int
1.61      deraadt    91: xopen(const char *fname, int oflags, mode_t mode)
1.1       tedu       92: {
1.53      tedu       93:        struct stat sb;
1.1       tedu       94:        int fd;
                     95:
1.31      tedu       96:        if (strcmp(fname, "-") == 0) {
1.61      deraadt    97:                if ((oflags & O_WRONLY))
1.31      tedu       98:                        fd = dup(STDOUT_FILENO);
                     99:                else
                    100:                        fd = dup(STDIN_FILENO);
                    101:                if (fd == -1)
                    102:                        err(1, "dup failed");
                    103:        } else {
1.61      deraadt   104:                fd = open(fname, oflags, mode);
1.31      tedu      105:                if (fd == -1)
                    106:                        err(1, "can't open %s for %s", fname,
1.61      deraadt   107:                            (oflags & O_WRONLY) ? "writing" : "reading");
1.31      tedu      108:        }
1.53      tedu      109:        if (fstat(fd, &sb) == -1 || S_ISDIR(sb.st_mode))
1.66      tedu      110:                errx(1, "not a valid file: %s", fname);
1.1       tedu      111:        return fd;
                    112: }
                    113:
1.111     espie     114: void *
1.1       tedu      115: xmalloc(size_t len)
                    116: {
                    117:        void *p;
                    118:
1.89      tedu      119:        if (!(p = malloc(len)))
1.1       tedu      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:        char *commentend, *b64end;
                    129:
                    130:        commentend = strchr(b64, '\n');
                    131:        if (!commentend || commentend - b64 <= COMMENTHDRLEN ||
1.65      tedu      132:            memcmp(b64, COMMENTHDR, COMMENTHDRLEN) != 0)
1.16      tedu      133:                errx(1, "invalid comment in %s; must start with '%s'",
                    134:                    filename, COMMENTHDR);
1.75      tedu      135:        *commentend = '\0';
1.40      deraadt   136:        if (comment) {
                    137:                if (strlcpy(comment, b64 + COMMENTHDRLEN,
                    138:                    COMMENTMAXLEN) >= COMMENTMAXLEN)
1.71      tedu      139:                        errx(1, "comment too long");
1.40      deraadt   140:        }
1.89      tedu      141:        if (!(b64end = strchr(commentend + 1, '\n')))
1.82      tedu      142:                errx(1, "missing new line after base64 in %s", filename);
1.75      tedu      143:        *b64end = '\0';
1.77      tedu      144:        if (b64_pton(commentend + 1, buf, buflen) != buflen)
1.130   ! tedu      145:                errx(1, "unable to parse %s", filename);
1.65      tedu      146:        if (memcmp(buf, PKALG, 2) != 0)
1.16      tedu      147:                errx(1, "unsupported file %s", filename);
                    148:        return b64end - b64 + 1;
                    149: }
                    150:
1.1       tedu      151: static void
1.46      tedu      152: readb64file(const char *filename, void *buf, size_t buflen, char *comment)
1.1       tedu      153: {
                    154:        char b64[2048];
1.10      tedu      155:        int rv, fd;
1.1       tedu      156:
                    157:        fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
1.89      tedu      158:        if ((rv = read(fd, b64, sizeof(b64) - 1)) == -1)
1.7       espie     159:                err(1, "read from %s", filename);
1.75      tedu      160:        b64[rv] = '\0';
1.46      tedu      161:        parseb64file(filename, b64, buf, buflen, comment);
1.41      tedu      162:        explicit_bzero(b64, sizeof(b64));
1.1       tedu      163:        close(fd);
                    164: }
                    165:
1.35      tedu      166: static uint8_t *
1.1       tedu      167: readmsg(const char *filename, unsigned long long *msglenp)
                    168: {
1.49      tedu      169:        unsigned long long msglen = 0;
                    170:        uint8_t *msg = NULL;
1.1       tedu      171:        struct stat sb;
1.49      tedu      172:        ssize_t x, space;
1.1       tedu      173:        int fd;
1.73      tedu      174:        const unsigned long long maxmsgsize = 1UL << 30;
1.1       tedu      175:
                    176:        fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
1.49      tedu      177:        if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode)) {
1.73      tedu      178:                if (sb.st_size > maxmsgsize)
1.49      tedu      179:                        errx(1, "msg too large in %s", filename);
                    180:                space = sb.st_size + 1;
                    181:        } else {
1.97      tedu      182:                space = 64 * 1024 - 1;
1.49      tedu      183:        }
                    184:
1.67      tedu      185:        msg = xmalloc(space + 1);
1.49      tedu      186:        while (1) {
                    187:                if (space == 0) {
1.73      tedu      188:                        if (msglen * 2 > maxmsgsize)
1.67      tedu      189:                                errx(1, "msg too large in %s", filename);
                    190:                        space = msglen;
1.49      tedu      191:                        if (!(msg = realloc(msg, msglen + space + 1)))
1.116     tedu      192:                                err(1, "realloc");
1.49      tedu      193:                }
                    194:                if ((x = read(fd, msg + msglen, space)) == -1)
                    195:                        err(1, "read from %s", filename);
                    196:                if (x == 0)
                    197:                        break;
                    198:                space -= x;
                    199:                msglen += x;
                    200:        }
                    201:
1.75      tedu      202:        msg[msglen] = '\0';
1.1       tedu      203:        close(fd);
                    204:
                    205:        *msglenp = msglen;
                    206:        return msg;
                    207: }
                    208:
1.111     espie     209: void
1.46      tedu      210: writeall(int fd, const void *buf, size_t buflen, const char *filename)
1.1       tedu      211: {
1.11      tedu      212:        ssize_t x;
1.38      espie     213:
1.46      tedu      214:        while (buflen != 0) {
1.89      tedu      215:                if ((x = write(fd, buf, buflen)) == -1)
1.38      espie     216:                        err(1, "write to %s", filename);
1.46      tedu      217:                buflen -= x;
1.61      deraadt   218:                buf = (char *)buf + x;
1.7       espie     219:        }
1.1       tedu      220: }
                    221:
1.17      deraadt   222: #ifndef VERIFYONLY
1.109     tedu      223: static char *
                    224: createheader(const char *comment, const void *buf, size_t buflen)
                    225: {
                    226:        char *header;
                    227:        char b64[1024];
                    228:
                    229:        if (b64_ntop(buf, buflen, b64, sizeof(b64)) == -1)
                    230:                errx(1, "base64 encode failed");
                    231:        if (asprintf(&header, "%s%s\n%s\n", COMMENTHDR, comment, b64) == -1)
                    232:                err(1, "asprintf failed");
                    233:        explicit_bzero(b64, sizeof(b64));
                    234:        return header;
                    235: }
                    236:
1.1       tedu      237: static void
1.109     tedu      238: writekeyfile(const char *filename, const char *comment, const void *buf,
                    239:     size_t buflen, int oflags, mode_t mode)
1.1       tedu      240: {
1.109     tedu      241:        char *header;
                    242:        int fd;
1.1       tedu      243:
1.61      deraadt   244:        fd = xopen(filename, O_CREAT|oflags|O_NOFOLLOW|O_WRONLY, mode);
1.109     tedu      245:        header = createheader(comment, buf, buflen);
1.7       espie     246:        writeall(fd, header, strlen(header), filename);
1.127     deraadt   247:        freezero(header, strlen(header));
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.129     tedu      257:        const char *errstr = NULL;
1.1       tedu      258:
                    259:        if (rounds == 0) {
                    260:                memset(key, 0, keylen);
                    261:                return;
                    262:        }
                    263:
1.50      tedu      264:        if (allowstdin && !isatty(STDIN_FILENO))
1.48      tedu      265:                rppflags |= RPP_STDIN;
                    266:        if (!readpassphrase("passphrase: ", pass, sizeof(pass), rppflags))
1.39      tedu      267:                errx(1, "unable to read passphrase");
1.37      tedu      268:        if (strlen(pass) == 0)
                    269:                errx(1, "please provide a password");
1.70      tedu      270:        if (confirm && !(rppflags & RPP_STDIN)) {
                    271:                char pass2[1024];
                    272:                if (!readpassphrase("confirm passphrase: ", pass2,
                    273:                    sizeof(pass2), rppflags))
1.129     tedu      274:                        errstr = "unable to read passphrase";
                    275:                if (!errstr && strcmp(pass, pass2) != 0)
                    276:                        errstr = "passwords don't match";
1.70      tedu      277:                explicit_bzero(pass2, sizeof(pass2));
                    278:        }
1.129     tedu      279:        if (!errstr && bcrypt_pbkdf(pass, strlen(pass), salt, saltlen, key,
1.1       tedu      280:            keylen, rounds) == -1)
1.129     tedu      281:                errstr = "bcrypt pbkdf";
1.41      tedu      282:        explicit_bzero(pass, sizeof(pass));
1.129     tedu      283:        if (errstr)
                    284:                errx(1, "%s", errstr);
1.1       tedu      285: }
                    286:
                    287: static void
                    288: signmsg(uint8_t *seckey, uint8_t *msg, unsigned long long msglen,
                    289:     uint8_t *sig)
                    290: {
                    291:        unsigned long long siglen;
                    292:        uint8_t *sigbuf;
                    293:
                    294:        sigbuf = xmalloc(msglen + SIGBYTES);
                    295:        crypto_sign_ed25519(sigbuf, &siglen, msg, msglen, seckey);
                    296:        memcpy(sig, sigbuf, SIGBYTES);
                    297:        free(sigbuf);
                    298: }
                    299:
                    300: static void
1.27      tedu      301: generate(const char *pubkeyfile, const char *seckeyfile, int rounds,
                    302:     const char *comment)
1.1       tedu      303: {
                    304:        uint8_t digest[SHA512_DIGEST_LENGTH];
                    305:        struct pubkey pubkey;
                    306:        struct enckey enckey;
                    307:        uint8_t xorkey[sizeof(enckey.seckey)];
1.94      tedu      308:        uint8_t keynum[KEYNUMLEN];
1.27      tedu      309:        char commentbuf[COMMENTMAXLEN];
1.1       tedu      310:        SHA2_CTX ctx;
1.91      tedu      311:        int i, nr;
1.1       tedu      312:
                    313:        crypto_sign_ed25519_keypair(pubkey.pubkey, enckey.seckey);
1.94      tedu      314:        arc4random_buf(keynum, sizeof(keynum));
1.1       tedu      315:
                    316:        SHA512Init(&ctx);
                    317:        SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
                    318:        SHA512Final(digest, &ctx);
                    319:
                    320:        memcpy(enckey.pkalg, PKALG, 2);
                    321:        memcpy(enckey.kdfalg, KDFALG, 2);
                    322:        enckey.kdfrounds = htonl(rounds);
1.94      tedu      323:        memcpy(enckey.keynum, keynum, KEYNUMLEN);
1.1       tedu      324:        arc4random_buf(enckey.salt, sizeof(enckey.salt));
1.70      tedu      325:        kdf(enckey.salt, sizeof(enckey.salt), rounds, 1, 1, xorkey, sizeof(xorkey));
1.1       tedu      326:        memcpy(enckey.checksum, digest, sizeof(enckey.checksum));
                    327:        for (i = 0; i < sizeof(enckey.seckey); i++)
                    328:                enckey.seckey[i] ^= xorkey[i];
1.41      tedu      329:        explicit_bzero(digest, sizeof(digest));
                    330:        explicit_bzero(xorkey, sizeof(xorkey));
1.1       tedu      331:
1.123     tedu      332:        nr = snprintf(commentbuf, sizeof(commentbuf), "%s secret key", comment);
                    333:        if (nr == -1 || nr >= sizeof(commentbuf))
1.71      tedu      334:                errx(1, "comment too long");
1.109     tedu      335:        writekeyfile(seckeyfile, commentbuf, &enckey,
                    336:            sizeof(enckey), O_EXCL, 0600);
1.41      tedu      337:        explicit_bzero(&enckey, sizeof(enckey));
1.1       tedu      338:
                    339:        memcpy(pubkey.pkalg, PKALG, 2);
1.94      tedu      340:        memcpy(pubkey.keynum, keynum, KEYNUMLEN);
1.123     tedu      341:        nr = snprintf(commentbuf, sizeof(commentbuf), "%s public key", comment);
                    342:        if (nr == -1 || nr >= sizeof(commentbuf))
1.71      tedu      343:                errx(1, "comment too long");
1.109     tedu      344:        writekeyfile(pubkeyfile, commentbuf, &pubkey,
                    345:            sizeof(pubkey), O_EXCL, 0666);
1.1       tedu      346: }
                    347:
1.126     espie     348: static const char *
1.125     tedu      349: check_keyname_compliance(const char *pubkeyfile, const char *seckeyfile)
                    350: {
1.126     espie     351:        const char *pos;
1.128     tedu      352:        size_t len;
1.126     espie     353:
                    354:        /* basename may or may not modify input */
                    355:        pos = strrchr(seckeyfile, '/');
                    356:        if (pos != NULL)
1.128     tedu      357:                seckeyfile = pos + 1;
1.126     espie     358:
                    359:        len = strlen(seckeyfile);
1.125     tedu      360:        if (len < 5) /* ?.key */
                    361:                goto bad;
1.126     espie     362:        if (strcmp(seckeyfile + len - 4, ".sec") != 0)
1.125     tedu      363:                goto bad;
1.126     espie     364:        if (pubkeyfile != NULL) {
                    365:                pos = strrchr(pubkeyfile, '/');
                    366:                if (pos != NULL)
1.128     tedu      367:                        pubkeyfile = pos + 1;
1.126     espie     368:
                    369:                if (strlen(pubkeyfile) != len)
                    370:                        goto bad;
                    371:                if (strcmp(pubkeyfile + len - 4, ".pub") != 0)
                    372:                        goto bad;
                    373:                if (strncmp(pubkeyfile, seckeyfile, len - 4) != 0)
                    374:                        goto bad;
                    375:        }
1.125     tedu      376:
1.126     espie     377:        return seckeyfile;
1.125     tedu      378: bad:
                    379:        errx(1, "please use naming scheme of keyname.pub and keyname.sec");
                    380: }
                    381:
1.111     espie     382: uint8_t *
1.110     tedu      383: createsig(const char *seckeyfile, const char *msgfile, uint8_t *msg,
                    384:     unsigned long long msglen)
1.1       tedu      385: {
1.110     tedu      386:        struct enckey enckey;
                    387:        uint8_t xorkey[sizeof(enckey.seckey)];
1.1       tedu      388:        struct sig sig;
1.109     tedu      389:        char *sighdr;
1.1       tedu      390:        uint8_t digest[SHA512_DIGEST_LENGTH];
1.110     tedu      391:        int i, nr, rounds;
1.1       tedu      392:        SHA2_CTX ctx;
1.110     tedu      393:        char comment[COMMENTMAXLEN], sigcomment[COMMENTMAXLEN];
                    394:
                    395:        readb64file(seckeyfile, &enckey, sizeof(enckey), comment);
1.1       tedu      396:
1.126     espie     397:        if (strcmp(seckeyfile, "-") == 0) {
                    398:                nr = snprintf(sigcomment, sizeof(sigcomment),
                    399:                    "signature from %s", comment);
                    400:        } else {
                    401:                const char *keyname = check_keyname_compliance(NULL,
                    402:                    seckeyfile);
1.123     tedu      403:                nr = snprintf(sigcomment, sizeof(sigcomment),
                    404:                    VERIFYWITH "%.*s.pub", (int)strlen(keyname) - 4, keyname);
1.110     tedu      405:        }
1.126     espie     406:        if (nr == -1 || nr >= sizeof(sigcomment))
                    407:                errx(1, "comment too long");
1.110     tedu      408:
                    409:        if (memcmp(enckey.kdfalg, KDFALG, 2) != 0)
1.1       tedu      410:                errx(1, "unsupported KDF");
1.110     tedu      411:        rounds = ntohl(enckey.kdfrounds);
                    412:        kdf(enckey.salt, sizeof(enckey.salt), rounds, strcmp(msgfile, "-") != 0,
1.70      tedu      413:            0, xorkey, sizeof(xorkey));
1.110     tedu      414:        for (i = 0; i < sizeof(enckey.seckey); i++)
                    415:                enckey.seckey[i] ^= xorkey[i];
1.41      tedu      416:        explicit_bzero(xorkey, sizeof(xorkey));
1.1       tedu      417:        SHA512Init(&ctx);
1.110     tedu      418:        SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
1.1       tedu      419:        SHA512Final(digest, &ctx);
1.110     tedu      420:        if (memcmp(enckey.checksum, digest, sizeof(enckey.checksum)) != 0)
1.105     tedu      421:                errx(1, "incorrect passphrase");
1.41      tedu      422:        explicit_bzero(digest, sizeof(digest));
1.1       tedu      423:
1.110     tedu      424:        signmsg(enckey.seckey, msg, msglen, sig.sig);
                    425:        memcpy(sig.keynum, enckey.keynum, KEYNUMLEN);
                    426:        explicit_bzero(&enckey, sizeof(enckey));
1.109     tedu      427:
                    428:        memcpy(sig.pkalg, PKALG, 2);
                    429:
                    430:        sighdr = createheader(sigcomment, &sig, sizeof(sig));
                    431:        return sighdr;
                    432: }
                    433:
                    434: static void
                    435: sign(const char *seckeyfile, const char *msgfile, const char *sigfile,
                    436:     int embedded)
                    437: {
                    438:        uint8_t *msg;
                    439:        char *sighdr;
1.110     tedu      440:        int fd;
1.109     tedu      441:        unsigned long long msglen;
                    442:
                    443:        msg = readmsg(msgfile, &msglen);
                    444:
1.110     tedu      445:        sighdr = createsig(seckeyfile, msgfile, msg, msglen);
1.109     tedu      446:
                    447:        fd = xopen(sigfile, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666);
                    448:        writeall(fd, sighdr, strlen(sighdr), sigfile);
                    449:        free(sighdr);
1.16      tedu      450:        if (embedded)
1.109     tedu      451:                writeall(fd, msg, msglen, sigfile);
                    452:        close(fd);
1.1       tedu      453:
                    454:        free(msg);
                    455: }
1.14      tedu      456: #endif
1.1       tedu      457:
                    458: static void
1.62      tedu      459: verifymsg(struct pubkey *pubkey, uint8_t *msg, unsigned long long msglen,
                    460:     struct sig *sig, int quiet)
1.13      tedu      461: {
                    462:        uint8_t *sigbuf, *dummybuf;
                    463:        unsigned long long siglen, dummylen;
                    464:
1.94      tedu      465:        if (memcmp(pubkey->keynum, sig->keynum, KEYNUMLEN) != 0)
1.62      tedu      466:                errx(1, "verification failed: checked against wrong key");
                    467:
1.13      tedu      468:        siglen = SIGBYTES + msglen;
                    469:        sigbuf = xmalloc(siglen);
                    470:        dummybuf = xmalloc(siglen);
1.62      tedu      471:        memcpy(sigbuf, sig->sig, SIGBYTES);
1.13      tedu      472:        memcpy(sigbuf + SIGBYTES, msg, msglen);
                    473:        if (crypto_sign_ed25519_open(dummybuf, &dummylen, sigbuf, siglen,
1.62      tedu      474:            pubkey->pubkey) == -1)
1.13      tedu      475:                errx(1, "signature verification failed");
1.42      tedu      476:        if (!quiet)
                    477:                printf("Signature Verified\n");
1.13      tedu      478:        free(sigbuf);
                    479:        free(dummybuf);
                    480: }
                    481:
1.107     espie     482: static void
                    483: check_keytype(const char *pubkeyfile, const char *keytype)
                    484: {
1.122     tedu      485:        const char *p;
                    486:        size_t typelen;
                    487:
                    488:        if (!(p = strrchr(pubkeyfile, '-')))
                    489:                goto bad;
                    490:        p++;
                    491:        typelen = strlen(keytype);
                    492:        if (strncmp(p, keytype, typelen) != 0)
                    493:                goto bad;
                    494:        if (strcmp(p + typelen, ".pub") != 0)
                    495:                goto bad;
                    496:        return;
                    497:
                    498: bad:
                    499:        errx(1, "incorrect keytype: %s is not %s", pubkeyfile, keytype);
1.107     espie     500: }
                    501:
1.64      tedu      502: static void
                    503: readpubkey(const char *pubkeyfile, struct pubkey *pubkey,
1.107     espie     504:     const char *sigcomment, const char *keytype)
1.64      tedu      505: {
1.119     tedu      506:        const char *safepath = "/etc/signify";
                    507:        char keypath[1024];
1.64      tedu      508:
                    509:        if (!pubkeyfile) {
1.90      tedu      510:                pubkeyfile = strstr(sigcomment, VERIFYWITH);
1.119     tedu      511:                if (pubkeyfile && strchr(pubkeyfile, '/') == NULL) {
1.64      tedu      512:                        pubkeyfile += strlen(VERIFYWITH);
1.107     espie     513:                        if (keytype)
                    514:                                check_keytype(pubkeyfile, keytype);
1.119     tedu      515:                        if (snprintf(keypath, sizeof(keypath), "%s/%s",
                    516:                            safepath, pubkeyfile) >= sizeof(keypath))
                    517:                                errx(1, "name too long %s", pubkeyfile);
1.121     tedu      518:                        pubkeyfile = keypath;
1.64      tedu      519:                } else
1.66      tedu      520:                        usage("must specify pubkey");
1.64      tedu      521:        }
1.121     tedu      522:        readb64file(pubkeyfile, pubkey, sizeof(*pubkey), NULL);
1.64      tedu      523: }
1.13      tedu      524:
                    525: static void
1.63      tedu      526: verifysimple(const char *pubkeyfile, const char *msgfile, const char *sigfile,
1.107     espie     527:     int quiet, const char *keytype)
1.1       tedu      528: {
1.64      tedu      529:        char sigcomment[COMMENTMAXLEN];
1.1       tedu      530:        struct sig sig;
                    531:        struct pubkey pubkey;
1.63      tedu      532:        unsigned long long msglen;
1.1       tedu      533:        uint8_t *msg;
1.16      tedu      534:
1.63      tedu      535:        msg = readmsg(msgfile, &msglen);
1.1       tedu      536:
1.64      tedu      537:        readb64file(sigfile, &sig, sizeof(sig), sigcomment);
1.107     espie     538:        readpubkey(pubkeyfile, &pubkey, sigcomment, keytype);
1.63      tedu      539:
                    540:        verifymsg(&pubkey, msg, msglen, &sig, quiet);
                    541:
                    542:        free(msg);
                    543: }
                    544:
                    545: static uint8_t *
                    546: verifyembedded(const char *pubkeyfile, const char *sigfile,
1.107     espie     547:     int quiet, unsigned long long *msglenp, const char *keytype)
1.63      tedu      548: {
1.64      tedu      549:        char sigcomment[COMMENTMAXLEN];
1.63      tedu      550:        struct sig sig;
                    551:        struct pubkey pubkey;
                    552:        unsigned long long msglen, siglen;
                    553:        uint8_t *msg;
                    554:
                    555:        msg = readmsg(sigfile, &msglen);
                    556:
1.64      tedu      557:        siglen = parseb64file(sigfile, msg, &sig, sizeof(sig), sigcomment);
1.107     espie     558:        readpubkey(pubkeyfile, &pubkey, sigcomment, keytype);
1.64      tedu      559:
1.63      tedu      560:        msglen -= siglen;
                    561:        memmove(msg, msg + siglen, msglen);
                    562:        msg[msglen] = 0;
1.13      tedu      563:
1.62      tedu      564:        verifymsg(&pubkey, msg, msglen, &sig, quiet);
1.63      tedu      565:
                    566:        *msglenp = msglen;
                    567:        return msg;
                    568: }
                    569:
                    570: static void
                    571: verify(const char *pubkeyfile, const char *msgfile, const char *sigfile,
1.107     espie     572:     int embedded, int quiet, const char *keytype)
1.63      tedu      573: {
                    574:        unsigned long long msglen;
                    575:        uint8_t *msg;
                    576:        int fd;
                    577:
1.16      tedu      578:        if (embedded) {
1.107     espie     579:                msg = verifyembedded(pubkeyfile, sigfile, quiet, &msglen,
                    580:                    keytype);
1.30      tedu      581:                fd = xopen(msgfile, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666);
1.16      tedu      582:                writeall(fd, msg, msglen, msgfile);
1.68      espie     583:                free(msg);
1.16      tedu      584:                close(fd);
1.63      tedu      585:        } else {
1.107     espie     586:                verifysimple(pubkeyfile, msgfile, sigfile, quiet, keytype);
1.16      tedu      587:        }
1.1       tedu      588: }
                    589:
1.42      tedu      590: #ifndef VERIFYONLY
1.83      tedu      591: #define HASHBUFSIZE 224
1.42      tedu      592: struct checksum {
1.98      tedu      593:        char file[PATH_MAX];
1.83      tedu      594:        char hash[HASHBUFSIZE];
1.81      tedu      595:        char algo[32];
1.42      tedu      596: };
                    597:
1.118     deraadt   598: static void *
1.85      espie     599: ecalloc(size_t s1, size_t s2, void *data)
                    600: {
1.87      tedu      601:        void *p;
                    602:
1.89      tedu      603:        if (!(p = calloc(s1, s2)))
1.85      espie     604:                err(1, "calloc");
                    605:        return p;
                    606: }
                    607:
                    608: static void
                    609: efree(void *p, void *data)
                    610: {
                    611:        free(p);
                    612: }
                    613:
1.42      tedu      614: static void
1.85      espie     615: recodehash(char *hash, size_t len)
1.80      tedu      616: {
1.83      tedu      617:        uint8_t data[HASHBUFSIZE / 2];
1.80      tedu      618:        int i, rv;
                    619:
1.85      espie     620:        if (strlen(hash) == len)
1.80      tedu      621:                return;
                    622:        if ((rv = b64_pton(hash, data, sizeof(data))) == -1)
                    623:                errx(1, "invalid base64 encoding");
                    624:        for (i = 0; i < rv; i++)
1.83      tedu      625:                snprintf(hash + i * 2, HASHBUFSIZE - i * 2, "%2.2x", data[i]);
1.80      tedu      626: }
                    627:
1.85      espie     628: static int
                    629: verifychecksum(struct checksum *c, int quiet)
                    630: {
                    631:        char buf[HASHBUFSIZE];
1.87      tedu      632:
1.85      espie     633:        if (strcmp(c->algo, "SHA256") == 0) {
                    634:                recodehash(c->hash, SHA256_DIGEST_STRING_LENGTH-1);
                    635:                if (!SHA256File(c->file, buf))
                    636:                        return 0;
                    637:        } else if (strcmp(c->algo, "SHA512") == 0) {
                    638:                recodehash(c->hash, SHA512_DIGEST_STRING_LENGTH-1);
                    639:                if (!SHA512File(c->file, buf))
                    640:                        return 0;
                    641:        } else {
                    642:                errx(1, "can't handle algorithm %s", c->algo);
                    643:        }
1.105     tedu      644:        if (strcmp(c->hash, buf) != 0)
1.85      espie     645:                return 0;
                    646:        if (!quiet)
                    647:                printf("%s: OK\n", c->file);
                    648:        return 1;
                    649: }
                    650:
1.80      tedu      651: static void
1.45      tedu      652: verifychecksums(char *msg, int argc, char **argv, int quiet)
1.42      tedu      653: {
1.87      tedu      654:        struct ohash_info info = { 0, NULL, ecalloc, efree, NULL };
1.85      espie     655:        struct ohash myh;
                    656:        struct checksum c;
1.88      tedu      657:        char *e, *line, *endline;
1.85      espie     658:        int hasfailed = 0;
1.87      tedu      659:        int i, rv;
1.85      espie     660:        unsigned int slot;
                    661:
1.88      tedu      662:        ohash_init(&myh, 6, &info);
1.85      espie     663:        if (argc) {
                    664:                for (i = 0; i < argc; i++) {
                    665:                        slot = ohash_qlookup(&myh, argv[i]);
                    666:                        e = ohash_find(&myh, slot);
                    667:                        if (e == NULL)
                    668:                                ohash_insert(&myh, slot, argv[i]);
                    669:                }
                    670:        }
1.42      tedu      671:
1.45      tedu      672:        line = msg;
1.42      tedu      673:        while (line && *line) {
                    674:                if ((endline = strchr(line, '\n')))
1.78      tedu      675:                        *endline++ = '\0';
1.99      tedu      676: #if PATH_MAX < 1024 || HASHBUFSIZE < 224
                    677: #error sizes are wrong
                    678: #endif
1.100     tedu      679:                rv = sscanf(line, "%31s (%1023[^)]) = "3s",
1.85      espie     680:                    c.algo, c.file, c.hash);
1.100     tedu      681:                if (rv != 3)
1.42      tedu      682:                        errx(1, "unable to parse checksum line %s", line);
                    683:                line = endline;
1.85      espie     684:                if (argc) {
                    685:                        slot = ohash_qlookup(&myh, c.file);
                    686:                        e = ohash_find(&myh, slot);
                    687:                        if (e != NULL) {
1.87      tedu      688:                                if (verifychecksum(&c, quiet) != 0)
1.85      espie     689:                                        ohash_remove(&myh, slot);
                    690:                        }
1.42      tedu      691:                } else {
1.87      tedu      692:                        if (verifychecksum(&c, quiet) == 0) {
1.88      tedu      693:                                slot = ohash_qlookup(&myh, c.file);
                    694:                                e = ohash_find(&myh, slot);
                    695:                                if (e == NULL) {
                    696:                                        if (!(e = strdup(c.file)))
                    697:                                                err(1, "strdup");
                    698:                                        ohash_insert(&myh, slot, e);
                    699:                                }
1.42      tedu      700:                        }
                    701:                }
1.85      espie     702:        }
1.42      tedu      703:
1.88      tedu      704:        for (e = ohash_first(&myh, &slot); e != NULL; e = ohash_next(&myh, &slot)) {
                    705:                fprintf(stderr, "%s: FAIL\n", e);
                    706:                hasfailed = 1;
                    707:                if (argc == 0)
                    708:                        free(e);
1.42      tedu      709:        }
1.88      tedu      710:        ohash_delete(&myh);
1.43      tedu      711:        if (hasfailed)
1.42      tedu      712:                exit(1);
                    713: }
                    714:
                    715: static void
                    716: check(const char *pubkeyfile, const char *sigfile, int quiet, int argc,
                    717:     char **argv)
                    718: {
1.63      tedu      719:        unsigned long long msglen;
1.42      tedu      720:        uint8_t *msg;
                    721:
1.107     espie     722:        msg = verifyembedded(pubkeyfile, sigfile, quiet, &msglen, NULL);
1.45      tedu      723:        verifychecksums((char *)msg, argc, argv, quiet);
1.42      tedu      724:
1.63      tedu      725:        free(msg);
1.42      tedu      726: }
1.111     espie     727:
                    728: void *
1.118     deraadt   729: verifyzdata(uint8_t *zdata, unsigned long long zdatalen,
1.111     espie     730:     const char *filename, const char *pubkeyfile, const char *keytype)
                    731: {
                    732:        struct sig sig;
                    733:        char sigcomment[COMMENTMAXLEN];
                    734:        unsigned long long siglen;
                    735:        struct pubkey pubkey;
                    736:
                    737:        if (zdatalen < sizeof(sig))
                    738:                errx(1, "signature too short in %s", filename);
1.118     deraadt   739:        siglen = parseb64file(filename, zdata, &sig, sizeof(sig),
1.111     espie     740:            sigcomment);
                    741:        readpubkey(pubkeyfile, &pubkey, sigcomment, keytype);
                    742:        zdata += siglen;
                    743:        zdatalen -= siglen;
                    744:        verifymsg(&pubkey, zdata, zdatalen, &sig, 1);
                    745:        return zdata;
                    746: }
1.42      tedu      747: #endif
                    748:
1.1       tedu      749: int
                    750: main(int argc, char **argv)
                    751: {
1.16      tedu      752:        const char *pubkeyfile = NULL, *seckeyfile = NULL, *msgfile = NULL,
1.1       tedu      753:            *sigfile = NULL;
1.98      tedu      754:        char sigfilebuf[PATH_MAX];
1.27      tedu      755:        const char *comment = "signify";
1.107     espie     756:        char *keytype = NULL;
1.1       tedu      757:        int ch, rounds;
1.16      tedu      758:        int embedded = 0;
1.42      tedu      759:        int quiet = 0;
1.111     espie     760:        int gzip = 0;
1.6       tedu      761:        enum {
                    762:                NONE,
1.42      tedu      763:                CHECK,
1.6       tedu      764:                GENERATE,
                    765:                SIGN,
                    766:                VERIFY
                    767:        } verb = NONE;
                    768:
1.102     deraadt   769:        if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
                    770:                err(1, "pledge");
1.1       tedu      771:
                    772:        rounds = 42;
                    773:
1.111     espie     774:        while ((ch = getopt(argc, argv, "CGSVzc:em:np:qs:t:x:")) != -1) {
1.1       tedu      775:                switch (ch) {
1.14      tedu      776: #ifndef VERIFYONLY
1.42      tedu      777:                case 'C':
                    778:                        if (verb)
                    779:                                usage(NULL);
                    780:                        verb = CHECK;
                    781:                        break;
1.6       tedu      782:                case 'G':
                    783:                        if (verb)
1.31      tedu      784:                                usage(NULL);
1.6       tedu      785:                        verb = GENERATE;
                    786:                        break;
                    787:                case 'S':
                    788:                        if (verb)
1.31      tedu      789:                                usage(NULL);
1.6       tedu      790:                        verb = SIGN;
                    791:                        break;
1.111     espie     792:                case 'z':
                    793:                        gzip = 1;
                    794:                        break;
1.14      tedu      795: #endif
1.6       tedu      796:                case 'V':
                    797:                        if (verb)
1.31      tedu      798:                                usage(NULL);
1.6       tedu      799:                        verb = VERIFY;
                    800:                        break;
1.27      tedu      801:                case 'c':
                    802:                        comment = optarg;
                    803:                        break;
1.16      tedu      804:                case 'e':
                    805:                        embedded = 1;
                    806:                        break;
1.31      tedu      807:                case 'm':
                    808:                        msgfile = optarg;
                    809:                        break;
1.6       tedu      810:                case 'n':
1.1       tedu      811:                        rounds = 0;
                    812:                        break;
1.6       tedu      813:                case 'p':
1.1       tedu      814:                        pubkeyfile = optarg;
                    815:                        break;
1.42      tedu      816:                case 'q':
                    817:                        quiet = 1;
                    818:                        break;
1.6       tedu      819:                case 's':
1.1       tedu      820:                        seckeyfile = optarg;
                    821:                        break;
1.107     espie     822:                case 't':
                    823:                        keytype = optarg;
                    824:                        break;
1.31      tedu      825:                case 'x':
                    826:                        sigfile = optarg;
                    827:                        break;
1.1       tedu      828:                default:
1.31      tedu      829:                        usage(NULL);
1.1       tedu      830:                        break;
                    831:                }
                    832:        }
1.2       tedu      833:        argc -= optind;
1.9       espie     834:        argv += optind;
1.113     tedu      835:
                    836:        if (embedded && gzip)
                    837:                errx(1, "can't combine -e and -z options");
1.104     bluhm     838:
                    839:        if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
                    840:                err(1, "setvbuf");
1.101     tedu      841:
1.114     tedu      842: #ifndef VERIFYONLY
                    843:        if (verb == CHECK) {
1.102     deraadt   844:                if (pledge("stdio rpath", NULL) == -1)
                    845:                        err(1, "pledge");
1.86      tedu      846:                if (!sigfile)
                    847:                        usage("must specify sigfile");
1.42      tedu      848:                check(pubkeyfile, sigfile, quiet, argc, argv);
                    849:                return 0;
                    850:        }
                    851: #endif
                    852:
1.31      tedu      853:        if (argc != 0)
                    854:                usage(NULL);
                    855:
1.36      tedu      856:        if (!sigfile && msgfile) {
1.91      tedu      857:                int nr;
1.36      tedu      858:                if (strcmp(msgfile, "-") == 0)
1.66      tedu      859:                        usage("must specify sigfile with - message");
1.123     tedu      860:                nr = snprintf(sigfilebuf, sizeof(sigfilebuf),
                    861:                    "%s.sig", msgfile);
                    862:                if (nr == -1 || nr >= sizeof(sigfilebuf))
1.36      tedu      863:                        errx(1, "path too long");
                    864:                sigfile = sigfilebuf;
                    865:        }
1.1       tedu      866:
1.36      tedu      867:        switch (verb) {
1.14      tedu      868: #ifndef VERIFYONLY
1.36      tedu      869:        case GENERATE:
1.114     tedu      870:                /* no pledge */
1.31      tedu      871:                if (!pubkeyfile || !seckeyfile)
1.66      tedu      872:                        usage("must specify pubkey and seckey");
1.125     tedu      873:                check_keyname_compliance(pubkeyfile, seckeyfile);
1.27      tedu      874:                generate(pubkeyfile, seckeyfile, rounds, comment);
1.36      tedu      875:                break;
                    876:        case SIGN:
1.114     tedu      877:                /* no pledge */
1.115     tedu      878:                if (gzip) {
1.117     espie     879:                        if (!msgfile || !seckeyfile || !sigfile)
                    880:                                usage("must specify message sigfile seckey");
1.111     espie     881:                        zsign(seckeyfile, msgfile, sigfile);
1.115     tedu      882:                } else {
1.111     espie     883:                        if (!msgfile || !seckeyfile)
                    884:                                usage("must specify message and seckey");
                    885:                        sign(seckeyfile, msgfile, sigfile, embedded);
                    886:                }
1.36      tedu      887:                break;
1.14      tedu      888: #endif
1.36      tedu      889:        case VERIFY:
1.115     tedu      890:                if ((embedded || gzip) &&
                    891:                    (msgfile && strcmp(msgfile, "-") != 0)) {
                    892:                        /* will need to create output file */
1.114     tedu      893:                        if (pledge("stdio rpath wpath cpath", NULL) == -1)
                    894:                                err(1, "pledge");
                    895:                } else {
                    896:                        if (pledge("stdio rpath", NULL) == -1)
                    897:                                err(1, "pledge");
                    898:                }
1.115     tedu      899:                if (gzip) {
1.111     espie     900:                        zverify(pubkeyfile, msgfile, sigfile, keytype);
1.115     tedu      901:                } else {
1.111     espie     902:                        if (!msgfile)
                    903:                                usage("must specify message");
1.118     deraadt   904:                        verify(pubkeyfile, msgfile, sigfile, embedded,
1.111     espie     905:                            quiet, keytype);
                    906:                }
1.36      tedu      907:                break;
                    908:        default:
1.114     tedu      909:                if (pledge("stdio", NULL) == -1)
                    910:                        err(1, "pledge");
1.36      tedu      911:                usage(NULL);
                    912:                break;
1.1       tedu      913:        }
1.9       espie     914:
1.1       tedu      915:        return 0;
                    916: }