version 1.30, 2014/01/12 21:18:52 |
version 1.31, 2014/01/13 01:40:43 |
|
|
extern char *__progname; |
extern char *__progname; |
|
|
static void |
static void |
usage(void) |
usage(const char *error) |
{ |
{ |
|
if (error) |
|
fprintf(stderr, "%s\n", error); |
fprintf(stderr, "usage:" |
fprintf(stderr, "usage:" |
#ifndef VERIFYONLY |
#ifndef VERIFYONLY |
"\t%1$s -G [-c comment] [-n] -p pubkey -s seckey\n" |
"\t%1$s -G [-n] [-c comment] -p pubkey -s seckey\n" |
"\t%1$s -I [-o sigfile] [-p pubkey] [-s seckey]\n" |
"\t%1$s -I [-p pubkey] [-s seckey] [-x sigfile]\n" |
"\t%1$s -S [-e] [-o sigfile] -s seckey message\n" |
"\t%1$s -S [-e] [-x sigfile] -s seckey -m message\n" |
#endif |
#endif |
"\t%1$s -V [-e] [-o sigfile] -p pubkey message\n", |
"\t%1$s -V [-e] [-x sigfile] -p pubkey -m message\n", |
__progname); |
__progname); |
exit(1); |
exit(1); |
} |
} |
|
|
{ |
{ |
int fd; |
int fd; |
|
|
fd = open(fname, flags, mode); |
if (strcmp(fname, "-") == 0) { |
if (fd == -1) |
if ((flags & O_WRONLY)) |
err(1, "open %s", fname); |
fd = dup(STDOUT_FILENO); |
|
else |
|
fd = dup(STDIN_FILENO); |
|
if (fd == -1) |
|
err(1, "dup failed"); |
|
} else { |
|
fd = open(fname, flags, mode); |
|
if (fd == -1) |
|
err(1, "can't open %s for %s", fname, |
|
(flags & O_WRONLY) ? "writing" : "reading"); |
|
} |
return fd; |
return fd; |
} |
} |
|
|
|
|
int fd, rv; |
int fd, rv; |
|
|
fd = xopen(filename, O_CREAT|flags|O_NOFOLLOW|O_WRONLY, mode); |
fd = xopen(filename, O_CREAT|flags|O_NOFOLLOW|O_WRONLY, mode); |
snprintf(header, sizeof(header), "%s%s\n", COMMENTHDR, |
snprintf(header, sizeof(header), "%s%s\n", COMMENTHDR, comment); |
comment); |
|
writeall(fd, header, strlen(header), filename); |
writeall(fd, header, strlen(header), filename); |
if ((rv = b64_ntop(buf, len, b64, sizeof(b64)-1)) == -1) |
if ((rv = b64_ntop(buf, len, b64, sizeof(b64)-1)) == -1) |
errx(1, "b64 encode failed"); |
errx(1, "b64 encode failed"); |
|
|
|
|
rounds = 42; |
rounds = 42; |
|
|
while ((ch = getopt(argc, argv, "GISVc:eno:p:s:")) != -1) { |
while ((ch = getopt(argc, argv, "GISVc:em:n:p:s:x:")) != -1) { |
switch (ch) { |
switch (ch) { |
#ifndef VERIFYONLY |
#ifndef VERIFYONLY |
case 'G': |
case 'G': |
if (verb) |
if (verb) |
usage(); |
usage(NULL); |
verb = GENERATE; |
verb = GENERATE; |
break; |
break; |
case 'I': |
case 'I': |
if (verb) |
if (verb) |
usage(); |
usage(NULL); |
verb = INSPECT; |
verb = INSPECT; |
break; |
break; |
case 'S': |
case 'S': |
if (verb) |
if (verb) |
usage(); |
usage(NULL); |
verb = SIGN; |
verb = SIGN; |
break; |
break; |
#endif |
#endif |
case 'V': |
case 'V': |
if (verb) |
if (verb) |
usage(); |
usage(NULL); |
verb = VERIFY; |
verb = VERIFY; |
break; |
break; |
case 'c': |
case 'c': |
|
|
case 'e': |
case 'e': |
embedded = 1; |
embedded = 1; |
break; |
break; |
|
case 'm': |
|
msgfile = optarg; |
|
break; |
case 'n': |
case 'n': |
rounds = 0; |
rounds = 0; |
break; |
break; |
case 'o': |
|
sigfile = optarg; |
|
break; |
|
case 'p': |
case 'p': |
pubkeyfile = optarg; |
pubkeyfile = optarg; |
break; |
break; |
case 's': |
case 's': |
seckeyfile = optarg; |
seckeyfile = optarg; |
break; |
break; |
|
case 'x': |
|
sigfile = optarg; |
|
break; |
default: |
default: |
usage(); |
usage(NULL); |
break; |
break; |
} |
} |
} |
} |
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
#ifdef VERIFYONLY |
if (argc != 0) |
if (verb != VERIFY) |
usage(NULL); |
#else |
|
if (verb == NONE) |
if (verb == NONE) |
#endif |
usage(NULL); |
usage(); |
|
|
|
#ifndef VERIFYONLY |
#ifndef VERIFYONLY |
if (verb == GENERATE) { |
if (verb == GENERATE) { |
if (!pubkeyfile || !seckeyfile || argc != 0) |
if (!pubkeyfile || !seckeyfile) |
usage(); |
usage("need pubkey and seckey"); |
generate(pubkeyfile, seckeyfile, rounds, comment); |
generate(pubkeyfile, seckeyfile, rounds, comment); |
} else if (verb == INSPECT) { |
} else if (verb == INSPECT) { |
if (argc != 0) |
|
usage(); |
|
inspect(seckeyfile, pubkeyfile, sigfile); |
inspect(seckeyfile, pubkeyfile, sigfile); |
} else |
} else |
#endif |
#endif |
{ |
{ |
if (argc != 1) |
if (!msgfile) |
usage(); |
usage("need message"); |
|
|
msgfile = argv[0]; |
|
|
|
if (!sigfile) { |
if (!sigfile) { |
|
if (strcmp(msgfile, "-") == 0) |
|
errx(1, "must specify sigfile with - message"); |
if (snprintf(sigfilebuf, sizeof(sigfilebuf), "%s.sig", |
if (snprintf(sigfilebuf, sizeof(sigfilebuf), "%s.sig", |
msgfile) >= sizeof(sigfilebuf)) |
msgfile) >= sizeof(sigfilebuf)) |
errx(1, "path too long"); |
errx(1, "path too long"); |
|
|
#ifndef VERIFYONLY |
#ifndef VERIFYONLY |
if (verb == SIGN) { |
if (verb == SIGN) { |
if (!seckeyfile) |
if (!seckeyfile) |
usage(); |
usage("need seckey"); |
sign(seckeyfile, msgfile, sigfile, embedded); |
sign(seckeyfile, msgfile, sigfile, embedded); |
} else |
} else |
#endif |
#endif |
if (verb == VERIFY) { |
if (verb == VERIFY) { |
if (!pubkeyfile) |
if (!pubkeyfile) |
usage(); |
usage("need pubkey"); |
verify(pubkeyfile, msgfile, sigfile, embedded); |
verify(pubkeyfile, msgfile, sigfile, embedded); |
} |
} |
} |
} |