X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=cmdgen.c;h=12e1ac9e19e31822e5d16f8095e982c21f37990b;hb=9dd9860cc84f82309de64c33e2813c6e9dc60749;hp=73383163063b2c52f1ebf9d3d688a304f2dcb41e;hpb=76a4b576e58fa3b245b02f9fbb052e4651acbfa9;p=PuTTY.git diff --git a/cmdgen.c b/cmdgen.c index 73383163..12e1ac9e 100644 --- a/cmdgen.c +++ b/cmdgen.c @@ -91,7 +91,7 @@ static void no_progress(void *param, int action, int phase, int iprogress) { } -void modalfatalbox(char *p, ...) +void modalfatalbox(const char *p, ...) { va_list ap; fprintf(stderr, "FATAL ERROR: "); @@ -102,7 +102,7 @@ void modalfatalbox(char *p, ...) cleanup_exit(1); } -void nonfatal(char *p, ...) +void nonfatal(const char *p, ...) { va_list ap; fprintf(stderr, "ERROR: "); @@ -152,7 +152,8 @@ void help(void) showversion(); usage(FALSE); fprintf(stderr, - " -t specify key type when generating (rsa, dsa, rsa1)\n" + " -t specify key type when generating (ed25519, ecdsa, 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" @@ -160,12 +161,10 @@ void help(void) " -O specify output type:\n" " private output PuTTY private key format\n" " private-openssh export OpenSSH private key\n" - " private-openssh-pem export OpenSSH private key " - "(force old PEM format)\n" " private-openssh-new export OpenSSH private key " - "(force new format)\n" + "(force new file format)\n" " private-sshcom export ssh.com private key\n" - " public standard / ssh.com public key\n" + " public RFC 4716 / ssh.com public key\n" " public-openssh OpenSSH public key\n" " fingerprint output the key fingerprint\n" " -o specify output file\n" @@ -175,56 +174,6 @@ void help(void) ); } -static int save_ssh2_pubkey(char *filename, char *comment, - void *v_pub_blob, int pub_len) -{ - unsigned char *pub_blob = (unsigned char *)v_pub_blob; - char *p; - int i, column; - FILE *fp; - - if (filename) { - fp = fopen(filename, "wb"); - if (!fp) - return 0; - } else - fp = stdout; - - fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n"); - - if (comment) { - fprintf(fp, "Comment: \""); - for (p = comment; *p; p++) { - if (*p == '\\' || *p == '\"') - fputc('\\', fp); - fputc(*p, fp); - } - fprintf(fp, "\"\n"); - } - - i = 0; - column = 0; - while (i < pub_len) { - char buf[5]; - int n = (pub_len - i < 3 ? pub_len - i : 3); - base64_encode_atom(pub_blob + i, n, buf); - i += n; - buf[4] = '\0'; - fputs(buf, fp); - if (++column >= 16) { - fputc('\n', fp); - column = 0; - } - } - if (column > 0) - fputc('\n', fp); - - fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n"); - if (filename) - fclose(fp); - return 1; -} - static int move(char *from, char *to) { int ret; @@ -244,34 +193,13 @@ static int move(char *from, char *to) return TRUE; } -static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen) -{ - char buffer[128]; - unsigned char digest[16]; - struct MD5Context md5c; - int i; - - MD5Init(&md5c); - MD5Update(&md5c, blob, bloblen); - MD5Final(digest, &md5c); - - sprintf(buffer, "%s ", alg); - if (bits > 0) - sprintf(buffer + strlen(buffer), "%d ", bits); - for (i = 0; i < 16; i++) - sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", - digest[i]); - - return dupstr(buffer); -} - int main(int argc, char **argv) { char *infile = NULL; Filename *infilename = NULL, *outfilename = NULL; enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN; char *outfile = NULL, *outfiletmp = NULL; - enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_PEM, + enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO, OPENSSH_NEW, SSHCOM } outtype = PRIVATE; int bits = -1; char *comment = NULL, *origcomment = NULL; @@ -467,9 +395,8 @@ int main(int argc, char **argv) outtype = PRIVATE; else if (!strcmp(p, "fingerprint")) outtype = FP; - else if (!strcmp(p, "private-openssh") || - !strcmp(p, "private-openssh-pem")) - outtype = OPENSSH_PEM, sshver = 2; + else if (!strcmp(p, "private-openssh")) + outtype = OPENSSH_AUTO, sshver = 2; else if (!strcmp(p, "private-openssh-new")) outtype = OPENSSH_NEW, sshver = 2; else if (!strcmp(p, "private-sshcom")) @@ -569,7 +496,7 @@ int main(int argc, char **argv) * We must save the private part when generating a new key. */ if (keytype != NOKEYGEN && - (outtype != PRIVATE && outtype != OPENSSH_PEM && + (outtype != PRIVATE && outtype != OPENSSH_AUTO && outtype != OPENSSH_NEW && outtype != SSHCOM)) { fprintf(stderr, "puttygen: this would generate a new key but " "discard the private part\n"); @@ -586,28 +513,6 @@ int main(int argc, char **argv) intype = key_type(infilename); switch (intype) { - /* - * It would be nice here to be able to load _public_ - * key files, in any of a number of forms, and (a) - * convert them to other public key types, (b) print - * out their fingerprints. Or, I suppose, for real - * orthogonality, (c) change their comment! - * - * In fact this opens some interesting possibilities. - * Suppose ssh2_userkey_loadpub() were able to load - * public key files as well as extracting the public - * key from private ones. And suppose I did the thing - * I've been wanting to do, where specifying a - * particular private key file for authentication - * causes any _other_ key in the agent to be discarded. - * Then, if you had an agent forwarded to the machine - * you were running Unix PuTTY or Plink on, and you - * needed to specify which of the keys in the agent it - * should use, you could do that by supplying a - * _public_ key file, thus not needing to trust even - * your encrypted private key file to the network. Ooh! - */ - case SSH_KEYTYPE_UNOPENABLE: case SSH_KEYTYPE_UNKNOWN: fprintf(stderr, "puttygen: unable to load file `%s': %s\n", @@ -615,6 +520,7 @@ int main(int argc, char **argv) return 1; case SSH_KEYTYPE_SSH1: + case SSH_KEYTYPE_SSH1_PUBLIC: if (sshver == 2) { fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys" " not supported\n"); @@ -624,6 +530,8 @@ int main(int argc, char **argv) break; case SSH_KEYTYPE_SSH2: + case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716: + case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH: case SSH_KEYTYPE_OPENSSH_PEM: case SSH_KEYTYPE_OPENSSH_NEW: case SSH_KEYTYPE_SSHCOM: @@ -634,6 +542,10 @@ int main(int argc, char **argv) } sshver = 2; break; + + case SSH_KEYTYPE_OPENSSH_AUTO: + default: + assert(0 && "Should never see these types on an input file"); } } @@ -649,7 +561,7 @@ int main(int argc, char **argv) */ if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) || (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) || - (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_PEM) || + (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) || (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) || (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) { if (!outfile) { @@ -668,7 +580,7 @@ int main(int argc, char **argv) * Bomb out rather than automatically choosing to write * a private key file to stdout. */ - if (outtype == PRIVATE || outtype == OPENSSH_PEM || + if (outtype == PRIVATE || outtype == OPENSSH_AUTO || outtype == OPENSSH_NEW || outtype == SSHCOM) { fprintf(stderr, "puttygen: need to specify an output file\n"); return 1; @@ -682,7 +594,7 @@ int main(int argc, char **argv) * out a private key format, or (b) the entire input key file * is encrypted. */ - if (outtype == PRIVATE || outtype == OPENSSH_PEM || + if (outtype == PRIVATE || outtype == OPENSSH_AUTO || outtype == OPENSSH_NEW || outtype == SSHCOM || intype == SSH_KEYTYPE_OPENSSH_PEM || intype == SSH_KEYTYPE_OPENSSH_NEW || @@ -691,6 +603,14 @@ int main(int argc, char **argv) else load_encrypted = FALSE; + if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC || + intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 || + intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) { + fprintf(stderr, "puttygen: cannot perform this action on a " + "public-key-only input file\n"); + return 1; + } + /* ------------------------------------------------------------------ * Now we're ready to actually do some stuff. */ @@ -740,13 +660,7 @@ int main(int argc, char **argv) ec_generate(ec, bits, progressfn, &prog); ssh2key = snew(struct ssh2_userkey); ssh2key->data = ec; - if (bits == 256) { - ssh2key->alg = &ssh_ecdsa_nistp256; - } else if (bits == 384) { - ssh2key->alg = &ssh_ecdsa_nistp384; - } else { - ssh2key->alg = &ssh_ecdsa_nistp521; - } + ssh2key->alg = ec->signalg; ssh1key = NULL; } else if (keytype == ED25519) { struct ec_key *ec = snew(struct ec_key); @@ -817,6 +731,7 @@ int main(int argc, char **argv) int ret; case SSH_KEYTYPE_SSH1: + case SSH_KEYTYPE_SSH1_PUBLIC: ssh1key = snew(struct RSAKey); if (!load_encrypted) { void *vblob; @@ -857,16 +772,21 @@ int main(int argc, char **argv) break; case SSH_KEYTYPE_SSH2: + case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716: + case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH: if (!load_encrypted) { ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg, - &ssh2bloblen, NULL, &error); + &ssh2bloblen, &origcomment, + &error); if (ssh2blob) { ssh2algf = find_pubkey_alg(ssh2alg); if (ssh2algf) - bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen); + bits = ssh2algf->pubkey_bits(ssh2algf, + ssh2blob, ssh2bloblen); else bits = -1; } + sfree(ssh2alg); } else { ssh2key = ssh2_load_userkey(infilename, passphrase, &error); } @@ -996,80 +916,33 @@ int main(int argc, char **argv) case PUBLIC: case PUBLICO: - if (sshver == 1) { - FILE *fp; - char *dec1, *dec2; + { + FILE *fp; - assert(ssh1key); + if (outfile) + fp = f_open(outfilename, "w", FALSE); + else + fp = stdout; - if (outfile) - fp = f_open(outfilename, "w", FALSE); - else - fp = stdout; - dec1 = bignum_decimal(ssh1key->exponent); - dec2 = bignum_decimal(ssh1key->modulus); - fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus), - dec1, dec2, ssh1key->comment); - sfree(dec1); - sfree(dec2); - if (outfile) - fclose(fp); - } else if (outtype == PUBLIC) { - if (!ssh2blob) { - assert(ssh2key); - ssh2blob = ssh2key->alg->public_blob(ssh2key->data, - &ssh2bloblen); - } - save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment, - ssh2blob, ssh2bloblen); - } else if (outtype == PUBLICO) { - char *buffer, *p; - int i; - FILE *fp; + if (sshver == 1) { + ssh1_write_pubkey(fp, ssh1key); + } else { + if (!ssh2blob) { + assert(ssh2key); + ssh2blob = ssh2key->alg->public_blob(ssh2key->data, + &ssh2bloblen); + } - if (!ssh2blob) { - assert(ssh2key); - ssh2blob = ssh2key->alg->public_blob(ssh2key->data, - &ssh2bloblen); - } - if (!ssh2alg) { - assert(ssh2key); - ssh2alg = ssh2key->alg->name; - } - if (ssh2key) - comment = ssh2key->comment; - else - comment = origcomment; - - buffer = snewn(strlen(ssh2alg) + - 4 * ((ssh2bloblen+2) / 3) + - strlen(comment) + 3, char); - strcpy(buffer, ssh2alg); - p = buffer + strlen(buffer); - *p++ = ' '; - i = 0; - while (i < ssh2bloblen) { - int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3); - base64_encode_atom(ssh2blob + i, n, p); - i += n; - p += 4; - } - if (*comment) { - *p++ = ' '; - strcpy(p, comment); - } else - *p++ = '\0'; + ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment, + ssh2blob, ssh2bloblen, + (outtype == PUBLIC ? + SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 : + SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)); + } - if (outfile) - fp = f_open(outfilename, "w", FALSE); - else - fp = stdout; - fprintf(fp, "%s\n", buffer); if (outfile) fclose(fp); - - sfree(buffer); - } + } break; case FP: @@ -1083,10 +956,11 @@ int main(int argc, char **argv) rsa_fingerprint(fingerprint, 128, ssh1key); } else { if (ssh2key) { - fingerprint = ssh2key->alg->fingerprint(ssh2key->data); + fingerprint = ssh2_fingerprint(ssh2key->alg, + ssh2key->data); } else { assert(ssh2blob); - fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen); + fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen); } } @@ -1102,7 +976,7 @@ int main(int argc, char **argv) } break; - case OPENSSH_PEM: + case OPENSSH_AUTO: case OPENSSH_NEW: case SSHCOM: assert(sshver == 2); @@ -1110,8 +984,8 @@ int main(int argc, char **argv) random_ref(); /* both foreign key types require randomness, * for IV or padding */ switch (outtype) { - case OPENSSH_PEM: - real_outtype = SSH_KEYTYPE_OPENSSH_PEM; + case OPENSSH_AUTO: + real_outtype = SSH_KEYTYPE_OPENSSH_AUTO; break; case OPENSSH_NEW: real_outtype = SSH_KEYTYPE_OPENSSH_NEW;