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

1.38    ! espie       1: /* $OpenBSD: signify.c,v 1.32 2014/01/13 09:41:16 espie 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.1       tedu       46:
                     47: struct enckey {
                     48:        uint8_t pkalg[2];
                     49:        uint8_t kdfalg[2];
                     50:        uint32_t kdfrounds;
                     51:        uint8_t salt[16];
                     52:        uint8_t checksum[8];
1.13      tedu       53:        uint8_t fingerprint[FPLEN];
1.1       tedu       54:        uint8_t seckey[SECRETBYTES];
                     55: };
                     56:
                     57: struct pubkey {
                     58:        uint8_t pkalg[2];
1.13      tedu       59:        uint8_t fingerprint[FPLEN];
1.1       tedu       60:        uint8_t pubkey[PUBLICBYTES];
                     61: };
                     62:
                     63: struct sig {
                     64:        uint8_t pkalg[2];
1.13      tedu       65:        uint8_t fingerprint[FPLEN];
1.1       tedu       66:        uint8_t sig[SIGBYTES];
                     67: };
                     68:
                     69: extern char *__progname;
                     70:
                     71: static void
1.31      tedu       72: usage(const char *error)
1.1       tedu       73: {
1.31      tedu       74:        if (error)
                     75:                fprintf(stderr, "%s\n", error);
1.9       espie      76:        fprintf(stderr, "usage:"
1.15      espie      77: #ifndef VERIFYONLY
1.31      tedu       78:            "\t%1$s -G [-n] [-c comment] -p pubkey -s seckey\n"
                     79:            "\t%1$s -I [-p pubkey] [-s seckey] [-x sigfile]\n"
                     80:            "\t%1$s -S [-e] [-x sigfile] -s seckey -m message\n"
1.15      espie      81: #endif
1.31      tedu       82:            "\t%1$s -V [-e] [-x sigfile] -p pubkey -m message\n",
1.15      espie      83:            __progname);
1.1       tedu       84:        exit(1);
                     85: }
                     86:
                     87: static int
                     88: xopen(const char *fname, int flags, mode_t mode)
                     89: {
                     90:        int fd;
                     91:
1.31      tedu       92:        if (strcmp(fname, "-") == 0) {
                     93:                if ((flags & O_WRONLY))
                     94:                        fd = dup(STDOUT_FILENO);
                     95:                else
                     96:                        fd = dup(STDIN_FILENO);
                     97:                if (fd == -1)
                     98:                        err(1, "dup failed");
                     99:        } else {
                    100:                fd = open(fname, flags, mode);
                    101:                if (fd == -1)
                    102:                        err(1, "can't open %s for %s", fname,
                    103:                            (flags & O_WRONLY) ? "writing" : "reading");
                    104:        }
1.1       tedu      105:        return fd;
                    106: }
                    107:
                    108: static void *
                    109: xmalloc(size_t len)
                    110: {
                    111:        void *p;
                    112:
                    113:        p = malloc(len);
                    114:        if (!p)
                    115:                err(1, "malloc %zu", len);
                    116:        return p;
                    117: }
                    118:
                    119: static void
1.7       espie     120: readall(int fd, void *buf, size_t len, const char *filename)
1.1       tedu      121: {
1.11      tedu      122:        ssize_t x;
1.38    ! espie     123:
        !           124:        while (len != 0) {
        !           125:                x = read(fd, buf, len);
        !           126:                if (x == -1)
        !           127:                        err(1, "read from %s", filename);
        !           128:                else {
        !           129:                        len -= x;
        !           130:                        buf = (char*)buf + x;
        !           131:                }
1.7       espie     132:        }
1.1       tedu      133: }
                    134:
1.16      tedu      135: static size_t
1.18      tedu      136: parseb64file(const char *filename, char *b64, void *buf, size_t len,
                    137:     char *comment)
1.16      tedu      138: {
                    139:        int rv;
                    140:        char *commentend, *b64end;
                    141:
                    142:        commentend = strchr(b64, '\n');
                    143:        if (!commentend || commentend - b64 <= COMMENTHDRLEN ||
                    144:            memcmp(b64, COMMENTHDR, COMMENTHDRLEN))
                    145:                errx(1, "invalid comment in %s; must start with '%s'",
                    146:                    filename, COMMENTHDR);
1.18      tedu      147:        *commentend = 0;
                    148:        if (comment)
                    149:                strlcpy(comment, b64 + COMMENTHDRLEN, COMMENTMAXLEN);
1.16      tedu      150:        b64end = strchr(commentend + 1, '\n');
                    151:        if (!b64end)
                    152:                errx(1, "missing new line after b64 in %s", filename);
                    153:        *b64end = 0;
                    154:        rv = b64_pton(commentend + 1, buf, len);
                    155:        if (rv != len)
                    156:                errx(1, "invalid b64 encoding in %s", filename);
                    157:        if (memcmp(buf, PKALG, 2))
                    158:                errx(1, "unsupported file %s", filename);
                    159:        return b64end - b64 + 1;
                    160: }
                    161:
1.1       tedu      162: static void
1.18      tedu      163: readb64file(const char *filename, void *buf, size_t len, char *comment)
1.1       tedu      164: {
                    165:        char b64[2048];
1.10      tedu      166:        int rv, fd;
1.1       tedu      167:
                    168:        fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
                    169:        memset(b64, 0, sizeof(b64));
                    170:        rv = read(fd, b64, sizeof(b64) - 1);
                    171:        if (rv == -1)
1.7       espie     172:                err(1, "read from %s", filename);
1.18      tedu      173:        parseb64file(filename, b64, buf, len, comment);
1.1       tedu      174:        memset(b64, 0, sizeof(b64));
                    175:        close(fd);
                    176: }
                    177:
1.35      tedu      178: static uint8_t *
1.1       tedu      179: readmsg(const char *filename, unsigned long long *msglenp)
                    180: {
                    181:        unsigned long long msglen;
                    182:        uint8_t *msg;
                    183:        struct stat sb;
                    184:        int fd;
                    185:
                    186:        fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
1.29      lteo      187:        if (fstat(fd, &sb) == -1)
                    188:                err(1, "fstat on %s", filename);
1.30      tedu      189:        if (!S_ISREG(sb.st_mode))
                    190:                errx(1, "%s must be a regular file", filename);
1.1       tedu      191:        msglen = sb.st_size;
                    192:        if (msglen > (1UL << 30))
                    193:                errx(1, "msg too large in %s", filename);
                    194:        msg = xmalloc(msglen);
1.7       espie     195:        readall(fd, msg, msglen, filename);
1.1       tedu      196:        close(fd);
                    197:
                    198:        *msglenp = msglen;
                    199:        return msg;
                    200: }
                    201:
                    202: static void
1.7       espie     203: writeall(int fd, const void *buf, size_t len, const char *filename)
1.1       tedu      204: {
1.11      tedu      205:        ssize_t x;
1.38    ! espie     206:
        !           207:        while (len != 0) {
        !           208:                x = write(fd, buf, len);
        !           209:                if (x == -1)
        !           210:                        err(1, "write to %s", filename);
        !           211:                else {
        !           212:                        len -= x;
        !           213:                        buf = (char*)buf + x;
        !           214:                }
1.7       espie     215:        }
1.1       tedu      216: }
                    217:
1.17      deraadt   218: #ifndef VERIFYONLY
1.1       tedu      219: static void
1.16      tedu      220: appendall(const char *filename, const void *buf, size_t len)
                    221: {
                    222:        int fd;
                    223:
1.30      tedu      224:        fd = xopen(filename, O_NOFOLLOW | O_WRONLY | O_APPEND, 0);
1.16      tedu      225:        writeall(fd, buf, len, filename);
                    226:        close(fd);
                    227: }
                    228:
                    229: static void
1.1       tedu      230: writeb64file(const char *filename, const char *comment, const void *buf,
1.20      espie     231:     size_t len, int flags, mode_t mode)
1.1       tedu      232: {
                    233:        char header[1024];
                    234:        char b64[1024];
                    235:        int fd, rv;
                    236:
1.30      tedu      237:        fd = xopen(filename, O_CREAT|flags|O_NOFOLLOW|O_WRONLY, mode);
1.31      tedu      238:        snprintf(header, sizeof(header), "%s%s\n", COMMENTHDR, comment);
1.7       espie     239:        writeall(fd, header, strlen(header), filename);
1.8       espie     240:        if ((rv = b64_ntop(buf, len, b64, sizeof(b64)-1)) == -1)
1.1       tedu      241:                errx(1, "b64 encode failed");
1.8       espie     242:        b64[rv++] = '\n';
1.7       espie     243:        writeall(fd, b64, rv, filename);
1.1       tedu      244:        memset(b64, 0, sizeof(b64));
                    245:        close(fd);
                    246: }
                    247:
                    248: static void
                    249: kdf(uint8_t *salt, size_t saltlen, int rounds, uint8_t *key, size_t keylen)
                    250: {
                    251:        char pass[1024];
                    252:
                    253:        if (rounds == 0) {
                    254:                memset(key, 0, keylen);
                    255:                return;
                    256:        }
                    257:
                    258:        if (!readpassphrase("passphrase: ", pass, sizeof(pass), 0))
                    259:                errx(1, "readpassphrase");
1.37      tedu      260:        if (strlen(pass) == 0)
                    261:                errx(1, "please provide a password");
1.1       tedu      262:        if (bcrypt_pbkdf(pass, strlen(pass), salt, saltlen, key,
                    263:            keylen, rounds) == -1)
                    264:                errx(1, "bcrypt pbkdf");
                    265:        memset(pass, 0, sizeof(pass));
                    266: }
                    267:
                    268: static void
                    269: signmsg(uint8_t *seckey, uint8_t *msg, unsigned long long msglen,
                    270:     uint8_t *sig)
                    271: {
                    272:        unsigned long long siglen;
                    273:        uint8_t *sigbuf;
                    274:
                    275:        sigbuf = xmalloc(msglen + SIGBYTES);
                    276:        crypto_sign_ed25519(sigbuf, &siglen, msg, msglen, seckey);
                    277:        memcpy(sig, sigbuf, SIGBYTES);
                    278:        free(sigbuf);
                    279: }
                    280:
                    281: static void
1.27      tedu      282: generate(const char *pubkeyfile, const char *seckeyfile, int rounds,
                    283:     const char *comment)
1.1       tedu      284: {
                    285:        uint8_t digest[SHA512_DIGEST_LENGTH];
                    286:        struct pubkey pubkey;
                    287:        struct enckey enckey;
                    288:        uint8_t xorkey[sizeof(enckey.seckey)];
1.13      tedu      289:        uint8_t fingerprint[FPLEN];
1.27      tedu      290:        char commentbuf[COMMENTMAXLEN];
1.1       tedu      291:        SHA2_CTX ctx;
                    292:        int i;
                    293:
                    294:        crypto_sign_ed25519_keypair(pubkey.pubkey, enckey.seckey);
1.13      tedu      295:        arc4random_buf(fingerprint, sizeof(fingerprint));
1.1       tedu      296:
                    297:        SHA512Init(&ctx);
                    298:        SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
                    299:        SHA512Final(digest, &ctx);
                    300:
                    301:        memcpy(enckey.pkalg, PKALG, 2);
                    302:        memcpy(enckey.kdfalg, KDFALG, 2);
                    303:        enckey.kdfrounds = htonl(rounds);
1.13      tedu      304:        memcpy(enckey.fingerprint, fingerprint, FPLEN);
1.1       tedu      305:        arc4random_buf(enckey.salt, sizeof(enckey.salt));
                    306:        kdf(enckey.salt, sizeof(enckey.salt), rounds, xorkey, sizeof(xorkey));
                    307:        memcpy(enckey.checksum, digest, sizeof(enckey.checksum));
                    308:        for (i = 0; i < sizeof(enckey.seckey); i++)
                    309:                enckey.seckey[i] ^= xorkey[i];
                    310:        memset(digest, 0, sizeof(digest));
                    311:        memset(xorkey, 0, sizeof(xorkey));
                    312:
1.27      tedu      313:        snprintf(commentbuf, sizeof(commentbuf), "%s secret key", comment);
                    314:        writeb64file(seckeyfile, commentbuf, &enckey,
1.20      espie     315:            sizeof(enckey), O_EXCL, 0600);
1.1       tedu      316:        memset(&enckey, 0, sizeof(enckey));
                    317:
                    318:        memcpy(pubkey.pkalg, PKALG, 2);
1.13      tedu      319:        memcpy(pubkey.fingerprint, fingerprint, FPLEN);
1.27      tedu      320:        snprintf(commentbuf, sizeof(commentbuf), "%s public key", comment);
                    321:        writeb64file(pubkeyfile, commentbuf, &pubkey,
1.20      espie     322:            sizeof(pubkey), O_EXCL, 0666);
1.1       tedu      323: }
                    324:
                    325: static void
1.16      tedu      326: sign(const char *seckeyfile, const char *msgfile, const char *sigfile,
                    327:     int embedded)
1.1       tedu      328: {
                    329:        struct sig sig;
                    330:        uint8_t digest[SHA512_DIGEST_LENGTH];
                    331:        struct enckey enckey;
                    332:        uint8_t xorkey[sizeof(enckey.seckey)];
                    333:        uint8_t *msg;
1.18      tedu      334:        char comment[COMMENTMAXLEN], sigcomment[1024];
1.1       tedu      335:        unsigned long long msglen;
                    336:        int i, rounds;
                    337:        SHA2_CTX ctx;
                    338:
1.18      tedu      339:        readb64file(seckeyfile, &enckey, sizeof(enckey), comment);
1.1       tedu      340:
                    341:        if (memcmp(enckey.kdfalg, KDFALG, 2))
                    342:                errx(1, "unsupported KDF");
                    343:        rounds = ntohl(enckey.kdfrounds);
                    344:        kdf(enckey.salt, sizeof(enckey.salt), rounds, xorkey, sizeof(xorkey));
                    345:        for (i = 0; i < sizeof(enckey.seckey); i++)
                    346:                enckey.seckey[i] ^= xorkey[i];
                    347:        memset(xorkey, 0, sizeof(xorkey));
                    348:        SHA512Init(&ctx);
                    349:        SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
                    350:        SHA512Final(digest, &ctx);
                    351:        if (memcmp(enckey.checksum, digest, sizeof(enckey.checksum)))
                    352:            errx(1, "incorrect passphrase");
                    353:        memset(digest, 0, sizeof(digest));
                    354:
1.16      tedu      355:        msg = readmsg(msgfile, &msglen);
1.1       tedu      356:
                    357:        signmsg(enckey.seckey, msg, msglen, sig.sig);
1.13      tedu      358:        memcpy(sig.fingerprint, enckey.fingerprint, FPLEN);
1.1       tedu      359:        memset(&enckey, 0, sizeof(enckey));
                    360:
                    361:        memcpy(sig.pkalg, PKALG, 2);
1.18      tedu      362:        snprintf(sigcomment, sizeof(sigcomment), "signature from %s", comment);
1.20      espie     363:        writeb64file(sigfile, sigcomment, &sig, sizeof(sig), O_TRUNC, 0666);
1.16      tedu      364:        if (embedded)
                    365:                appendall(sigfile, msg, msglen);
1.1       tedu      366:
                    367:        free(msg);
                    368: }
1.22      tedu      369:
                    370: static void
                    371: inspect(const char *seckeyfile, const char *pubkeyfile, const char *sigfile)
                    372: {
                    373:        struct sig sig;
                    374:        struct enckey enckey;
                    375:        struct pubkey pubkey;
                    376:        char fp[(FPLEN + 2) / 3 * 4 + 1];
                    377:
                    378:        if (seckeyfile) {
                    379:                readb64file(seckeyfile, &enckey, sizeof(enckey), NULL);
                    380:                b64_ntop(enckey.fingerprint, FPLEN, fp, sizeof(fp));
                    381:                printf("sec fp: %s\n", fp);
                    382:        }
                    383:        if (pubkeyfile) {
                    384:                readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
                    385:                b64_ntop(pubkey.fingerprint, FPLEN, fp, sizeof(fp));
                    386:                printf("pub fp: %s\n", fp);
                    387:        }
                    388:        if (sigfile) {
                    389:                readb64file(sigfile, &sig, sizeof(sig), NULL);
                    390:                b64_ntop(sig.fingerprint, FPLEN, fp, sizeof(fp));
                    391:                printf("sig fp: %s\n", fp);
                    392:        }
                    393: }
1.14      tedu      394: #endif
1.1       tedu      395:
                    396: static void
1.13      tedu      397: verifymsg(uint8_t *pubkey, uint8_t *msg, unsigned long long msglen,
                    398:     uint8_t *sig)
                    399: {
                    400:        uint8_t *sigbuf, *dummybuf;
                    401:        unsigned long long siglen, dummylen;
                    402:
                    403:        siglen = SIGBYTES + msglen;
                    404:        sigbuf = xmalloc(siglen);
                    405:        dummybuf = xmalloc(siglen);
                    406:        memcpy(sigbuf, sig, SIGBYTES);
                    407:        memcpy(sigbuf + SIGBYTES, msg, msglen);
                    408:        if (crypto_sign_ed25519_open(dummybuf, &dummylen, sigbuf, siglen,
                    409:            pubkey) == -1)
                    410:                errx(1, "signature verification failed");
                    411:        free(sigbuf);
                    412:        free(dummybuf);
                    413: }
                    414:
                    415:
                    416: static void
1.16      tedu      417: verify(const char *pubkeyfile, const char *msgfile, const char *sigfile,
                    418:     int embedded)
1.1       tedu      419: {
                    420:        struct sig sig;
                    421:        struct pubkey pubkey;
1.16      tedu      422:        unsigned long long msglen, siglen = 0;
1.1       tedu      423:        uint8_t *msg;
1.16      tedu      424:        int fd;
                    425:
                    426:        msg = readmsg(embedded ? sigfile : msgfile, &msglen);
1.1       tedu      427:
1.18      tedu      428:        readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
1.16      tedu      429:        if (embedded) {
1.18      tedu      430:                siglen = parseb64file(sigfile, msg, &sig, sizeof(sig), NULL);
1.16      tedu      431:                msg += siglen;
                    432:                msglen -= siglen;
                    433:        } else {
1.18      tedu      434:                readb64file(sigfile, &sig, sizeof(sig), NULL);
1.16      tedu      435:        }
1.13      tedu      436:
1.22      tedu      437:        if (memcmp(pubkey.fingerprint, sig.fingerprint, FPLEN)) {
                    438: #ifndef VERIFYONLY
                    439:                inspect(NULL, pubkeyfile, sigfile);
                    440: #endif
1.13      tedu      441:                errx(1, "verification failed: checked against wrong key");
1.22      tedu      442:        }
1.1       tedu      443:
1.16      tedu      444:        verifymsg(pubkey.pubkey, msg, msglen, sig.sig);
                    445:        if (embedded) {
1.30      tedu      446:                fd = xopen(msgfile, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666);
1.16      tedu      447:                writeall(fd, msg, msglen, msgfile);
                    448:                close(fd);
                    449:        }
1.1       tedu      450:
1.16      tedu      451:        free(msg - siglen);
1.1       tedu      452: }
                    453:
                    454: int
                    455: main(int argc, char **argv)
                    456: {
1.16      tedu      457:        const char *pubkeyfile = NULL, *seckeyfile = NULL, *msgfile = NULL,
1.1       tedu      458:            *sigfile = NULL;
                    459:        char sigfilebuf[1024];
1.27      tedu      460:        const char *comment = "signify";
1.1       tedu      461:        int ch, rounds;
1.16      tedu      462:        int embedded = 0;
1.6       tedu      463:        enum {
                    464:                NONE,
                    465:                GENERATE,
1.22      tedu      466:                INSPECT,
1.6       tedu      467:                SIGN,
                    468:                VERIFY
                    469:        } verb = NONE;
                    470:
1.1       tedu      471:
                    472:        rounds = 42;
                    473:
1.32      espie     474:        while ((ch = getopt(argc, argv, "GISVc:em:np:s:x:")) != -1) {
1.1       tedu      475:                switch (ch) {
1.14      tedu      476: #ifndef VERIFYONLY
1.6       tedu      477:                case 'G':
                    478:                        if (verb)
1.31      tedu      479:                                usage(NULL);
1.6       tedu      480:                        verb = GENERATE;
                    481:                        break;
1.22      tedu      482:                case 'I':
                    483:                        if (verb)
1.31      tedu      484:                                usage(NULL);
1.22      tedu      485:                        verb = INSPECT;
                    486:                        break;
1.6       tedu      487:                case 'S':
                    488:                        if (verb)
1.31      tedu      489:                                usage(NULL);
1.6       tedu      490:                        verb = SIGN;
                    491:                        break;
1.14      tedu      492: #endif
1.6       tedu      493:                case 'V':
                    494:                        if (verb)
1.31      tedu      495:                                usage(NULL);
1.6       tedu      496:                        verb = VERIFY;
                    497:                        break;
1.27      tedu      498:                case 'c':
                    499:                        comment = optarg;
                    500:                        break;
1.16      tedu      501:                case 'e':
                    502:                        embedded = 1;
                    503:                        break;
1.31      tedu      504:                case 'm':
                    505:                        msgfile = optarg;
                    506:                        break;
1.6       tedu      507:                case 'n':
1.1       tedu      508:                        rounds = 0;
                    509:                        break;
1.6       tedu      510:                case 'p':
1.1       tedu      511:                        pubkeyfile = optarg;
                    512:                        break;
1.6       tedu      513:                case 's':
1.1       tedu      514:                        seckeyfile = optarg;
                    515:                        break;
1.31      tedu      516:                case 'x':
                    517:                        sigfile = optarg;
                    518:                        break;
1.1       tedu      519:                default:
1.31      tedu      520:                        usage(NULL);
1.1       tedu      521:                        break;
                    522:                }
                    523:        }
1.2       tedu      524:        argc -= optind;
1.9       espie     525:        argv += optind;
                    526:
1.31      tedu      527:        if (argc != 0)
                    528:                usage(NULL);
                    529:
1.36      tedu      530:        if (!sigfile && msgfile) {
                    531:                if (strcmp(msgfile, "-") == 0)
                    532:                        errx(1, "must specify sigfile with - message");
                    533:                if (snprintf(sigfilebuf, sizeof(sigfilebuf), "%s.sig",
                    534:                    msgfile) >= sizeof(sigfilebuf))
                    535:                        errx(1, "path too long");
                    536:                sigfile = sigfilebuf;
                    537:        }
1.1       tedu      538:
1.36      tedu      539:        switch (verb) {
1.14      tedu      540: #ifndef VERIFYONLY
1.36      tedu      541:        case GENERATE:
1.31      tedu      542:                if (!pubkeyfile || !seckeyfile)
                    543:                        usage("need pubkey and seckey");
1.27      tedu      544:                generate(pubkeyfile, seckeyfile, rounds, comment);
1.36      tedu      545:                break;
                    546:        case INSPECT:
1.22      tedu      547:                inspect(seckeyfile, pubkeyfile, sigfile);
1.36      tedu      548:                break;
                    549:        case SIGN:
                    550:                if (!msgfile || !seckeyfile)
                    551:                        usage("need message and seckey");
                    552:                sign(seckeyfile, msgfile, sigfile, embedded);
                    553:                break;
1.14      tedu      554: #endif
1.36      tedu      555:        case VERIFY:
                    556:                if (!msgfile || !pubkeyfile)
                    557:                        usage("need message and pubkey");
                    558:                verify(pubkeyfile, msgfile, sigfile, embedded);
                    559:                break;
                    560:        default:
                    561:                usage(NULL);
                    562:                break;
1.1       tedu      563:        }
1.9       espie     564:
1.1       tedu      565:        return 0;
                    566: }