X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/edd0cb8aef57080ae884e06731a7892ca8cdba44..acab36bc0e2b03bc71745a2e1e6cc64bebdf3c86:/cmdgen.c diff --git a/cmdgen.c b/cmdgen.c index 36da55ee..9aa585f3 100644 --- a/cmdgen.c +++ b/cmdgen.c @@ -124,13 +124,16 @@ void showversion(void) sfree(verstr); } -void usage(void) +void usage(int standalone) { fprintf(stderr, "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n" - " [ -C comment ] [ -P ]\n" + " [ -C comment ] [ -P ] [ -q ]\n" " [ -o output-keyfile ] [ -O type | -l | -L" " | -p ]\n"); + if (standalone) + fprintf(stderr, + "Use \"puttygen --help\" for more detail.\n"); } void help(void) @@ -140,12 +143,13 @@ void help(void) * start with that, plus a version heading. */ showversion(); - usage(); + usage(FALSE); fprintf(stderr, " -t specify key type when generating (rsa, dsa, rsa1)\n" " -b specify number of bits when generating key\n" " -C change or specify key comment\n" " -P change key passphrase\n" + " -q quiet: do not display progress bar\n" " -O specify output type:\n" " private output PuTTY private key format\n" " private-openssh export OpenSSH private key\n" @@ -229,7 +233,7 @@ static int move(char *from, char *to) return TRUE; } -static char *blobfp(char *alg, int bits, char *blob, int bloblen) +static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen) { char buffer[128]; unsigned char digest[16]; @@ -266,7 +270,8 @@ int main(int argc, char **argv) int sshver = 0; struct ssh2_userkey *ssh2key = NULL; struct RSAKey *ssh1key = NULL; - char *ssh2blob = NULL, *ssh2alg = NULL; + unsigned char *ssh2blob = NULL; + char *ssh2alg = NULL; const struct ssh_signkey *ssh2algf = NULL; int ssh2bloblen; char *passphrase = NULL; @@ -282,7 +287,7 @@ int main(int argc, char **argv) * return success. */ if (argc <= 1) { - usage(); + usage(TRUE); return 0; } @@ -336,7 +341,7 @@ int main(int argc, char **argv) else { errs = TRUE; fprintf(stderr, - "puttygen: no such option `--%s'\n", opt); + "puttygen: no such option `-%s'\n", opt); } } p = NULL; @@ -475,7 +480,7 @@ int main(int argc, char **argv) * ones, print the usage message and return failure. */ if (!infile && keytype == NOKEYGEN) { - usage(); + usage(TRUE); return 1; } @@ -487,11 +492,21 @@ int main(int argc, char **argv) * Bomb out if we've been asked to both load and generate a * key. */ - if (keytype != NOKEYGEN && intype) { + if (keytype != NOKEYGEN && infile) { fprintf(stderr, "puttygen: cannot both load and generate a key\n"); return 1; } + /* + * We must save the private part when generating a new key. + */ + if (keytype != NOKEYGEN && + (outtype != PRIVATE && outtype != OPENSSH && outtype != SSHCOM)) { + fprintf(stderr, "puttygen: this would generate a new key but " + "discard the private part\n"); + return 1; + } + /* * Analyse the type of the input file, in case this affects our * course of action. @@ -625,6 +640,11 @@ int main(int argc, char **argv) random_ref(); entropy = get_random_data(bits / 8); + if (!entropy) { + fprintf(stderr, "puttygen: failed to collect entropy, " + "could not generate key\n"); + return 1; + } random_add_heavynoise(entropy, bits / 8); memset(entropy, 0, bits/8); sfree(entropy); @@ -701,12 +721,12 @@ int main(int argc, char **argv) ssh1key = snew(struct RSAKey); if (!load_encrypted) { void *vblob; - char *blob; + unsigned char *blob; int n, l, bloblen; ret = rsakey_pubblob(&infilename, &vblob, &bloblen, &origcomment, &error); - blob = (char *)vblob; + blob = (unsigned char *)vblob; n = 4; /* skip modulus bits */ @@ -878,7 +898,7 @@ int main(int argc, char **argv) assert(ssh1key); if (outfile) - fp = f_open(outfilename, "w"); + fp = f_open(outfilename, "w", FALSE); else fp = stdout; dec1 = bignum_decimal(ssh1key->exponent); @@ -936,7 +956,7 @@ int main(int argc, char **argv) *p++ = '\0'; if (outfile) - fp = f_open(outfilename, "w"); + fp = f_open(outfilename, "w", FALSE); else fp = stdout; fprintf(fp, "%s\n", buffer); @@ -966,7 +986,7 @@ int main(int argc, char **argv) } if (outfile) - fp = f_open(outfilename, "w"); + fp = f_open(outfilename, "w", FALSE); else fp = stdout; fprintf(fp, "%s\n", fingerprint);