+++ /dev/null
-/*\r
- * cmdgen.c - command-line form of PuTTYgen\r
- */\r
-\r
-#define PUTTY_DO_GLOBALS\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <ctype.h>\r
-#include <limits.h>\r
-#include <assert.h>\r
-#include <time.h>\r
-\r
-#include "putty.h"\r
-#include "ssh.h"\r
-\r
-#ifdef TEST_CMDGEN\r
-/*\r
- * This section overrides some definitions below for test purposes.\r
- * When compiled with -DTEST_CMDGEN:\r
- * \r
- * - Calls to get_random_data() are replaced with the diagnostic\r
- * function below (I #define the name so that I can still link\r
- * with the original set of modules without symbol clash), in\r
- * order to avoid depleting the test system's /dev/random\r
- * unnecessarily.\r
- * \r
- * - Calls to console_get_userpass_input() are replaced with the\r
- * diagnostic function below, so that I can run tests in an\r
- * automated manner and provide their interactive passphrase\r
- * inputs.\r
- * \r
- * - main() is renamed to cmdgen_main(); at the bottom of the file\r
- * I define another main() which calls the former repeatedly to\r
- * run tests.\r
- */\r
-#define get_random_data get_random_data_diagnostic\r
-char *get_random_data(int len)\r
-{\r
- char *buf = snewn(len, char);\r
- memset(buf, 'x', len);\r
- return buf;\r
-}\r
-#define console_get_userpass_input console_get_userpass_input_diagnostic\r
-int nprompts, promptsgot;\r
-const char *prompts[3];\r
-int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)\r
-{\r
- size_t i;\r
- int ret = 1;\r
- for (i = 0; i < p->n_prompts; i++) {\r
- if (promptsgot < nprompts) {\r
- assert(strlen(prompts[promptsgot]) < p->prompts[i]->result_len);\r
- strcpy(p->prompts[i]->result, prompts[promptsgot++]);\r
- } else {\r
- promptsgot++; /* track number of requests anyway */\r
- ret = 0;\r
- }\r
- }\r
- return ret;\r
-}\r
-#define main cmdgen_main\r
-#endif\r
-\r
-struct progress {\r
- int phase, current;\r
-};\r
-\r
-static void progress_update(void *param, int action, int phase, int iprogress)\r
-{\r
- struct progress *p = (struct progress *)param;\r
- if (action != PROGFN_PROGRESS)\r
- return;\r
- if (phase > p->phase) {\r
- if (p->phase >= 0)\r
- fputc('\n', stderr);\r
- p->phase = phase;\r
- if (iprogress >= 0)\r
- p->current = iprogress - 1;\r
- else\r
- p->current = iprogress;\r
- }\r
- while (p->current < iprogress) {\r
- fputc('+', stdout);\r
- p->current++;\r
- }\r
- fflush(stdout);\r
-}\r
-\r
-static void no_progress(void *param, int action, int phase, int iprogress)\r
-{\r
-}\r
-\r
-void modalfatalbox(char *p, ...)\r
-{\r
- va_list ap;\r
- fprintf(stderr, "FATAL ERROR: ");\r
- va_start(ap, p);\r
- vfprintf(stderr, p, ap);\r
- va_end(ap);\r
- fputc('\n', stderr);\r
- cleanup_exit(1);\r
-}\r
-\r
-/*\r
- * Stubs to let everything else link sensibly.\r
- */\r
-void log_eventlog(void *handle, const char *event)\r
-{\r
-}\r
-char *x_get_default(const char *key)\r
-{\r
- return NULL;\r
-}\r
-void sk_cleanup(void)\r
-{\r
-}\r
-\r
-void showversion(void)\r
-{\r
- char *verstr = dupstr(ver);\r
- verstr[0] = tolower((unsigned char)verstr[0]);\r
- printf("PuTTYgen %s\n", verstr);\r
- sfree(verstr);\r
-}\r
-\r
-void usage(int standalone)\r
-{\r
- fprintf(stderr,\r
- "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"\r
- " [ -C comment ] [ -P ] [ -q ]\n"\r
- " [ -o output-keyfile ] [ -O type | -l | -L"\r
- " | -p ]\n");\r
- if (standalone)\r
- fprintf(stderr,\r
- "Use \"puttygen --help\" for more detail.\n");\r
-}\r
-\r
-void help(void)\r
-{\r
- /*\r
- * Help message is an extended version of the usage message. So\r
- * start with that, plus a version heading.\r
- */\r
- showversion();\r
- usage(FALSE);\r
- fprintf(stderr,\r
- " -t specify key type when generating (rsa, dsa, rsa1)\n"\r
- " -b specify number of bits when generating key\n"\r
- " -C change or specify key comment\n"\r
- " -P change key passphrase\n"\r
- " -q quiet: do not display progress bar\n"\r
- " -O specify output type:\n"\r
- " private output PuTTY private key format\n"\r
- " private-openssh export OpenSSH private key\n"\r
- " private-sshcom export ssh.com private key\n"\r
- " public standard / ssh.com public key\n"\r
- " public-openssh OpenSSH public key\n"\r
- " fingerprint output the key fingerprint\n"\r
- " -o specify output file\n"\r
- " -l equivalent to `-O fingerprint'\n"\r
- " -L equivalent to `-O public-openssh'\n"\r
- " -p equivalent to `-O public'\n"\r
- );\r
-}\r
-\r
-static int save_ssh2_pubkey(char *filename, char *comment,\r
- void *v_pub_blob, int pub_len)\r
-{\r
- unsigned char *pub_blob = (unsigned char *)v_pub_blob;\r
- char *p;\r
- int i, column;\r
- FILE *fp;\r
-\r
- if (filename) {\r
- fp = fopen(filename, "wb");\r
- if (!fp)\r
- return 0;\r
- } else\r
- fp = stdout;\r
-\r
- fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");\r
-\r
- if (comment) {\r
- fprintf(fp, "Comment: \"");\r
- for (p = comment; *p; p++) {\r
- if (*p == '\\' || *p == '\"')\r
- fputc('\\', fp);\r
- fputc(*p, fp);\r
- }\r
- fprintf(fp, "\"\n");\r
- }\r
-\r
- i = 0;\r
- column = 0;\r
- while (i < pub_len) {\r
- char buf[5];\r
- int n = (pub_len - i < 3 ? pub_len - i : 3);\r
- base64_encode_atom(pub_blob + i, n, buf);\r
- i += n;\r
- buf[4] = '\0';\r
- fputs(buf, fp);\r
- if (++column >= 16) {\r
- fputc('\n', fp);\r
- column = 0;\r
- }\r
- }\r
- if (column > 0)\r
- fputc('\n', fp);\r
- \r
- fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");\r
- if (filename)\r
- fclose(fp);\r
- return 1;\r
-}\r
-\r
-static int move(char *from, char *to)\r
-{\r
- int ret;\r
-\r
- ret = rename(from, to);\r
- if (ret) {\r
- /*\r
- * This OS may require us to remove the original file first.\r
- */\r
- remove(to);\r
- ret = rename(from, to);\r
- }\r
- if (ret) {\r
- perror("puttygen: cannot move new file on to old one");\r
- return FALSE;\r
- }\r
- return TRUE;\r
-}\r
-\r
-static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)\r
-{\r
- char buffer[128];\r
- unsigned char digest[16];\r
- struct MD5Context md5c;\r
- int i;\r
-\r
- MD5Init(&md5c);\r
- MD5Update(&md5c, blob, bloblen);\r
- MD5Final(digest, &md5c);\r
-\r
- sprintf(buffer, "%s ", alg);\r
- if (bits > 0)\r
- sprintf(buffer + strlen(buffer), "%d ", bits);\r
- for (i = 0; i < 16; i++)\r
- sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",\r
- digest[i]);\r
-\r
- return dupstr(buffer);\r
-}\r
-\r
-int main(int argc, char **argv)\r
-{\r
- char *infile = NULL;\r
- Filename infilename;\r
- enum { NOKEYGEN, RSA1, RSA2, DSA } keytype = NOKEYGEN; \r
- char *outfile = NULL, *outfiletmp = NULL;\r
- Filename outfilename;\r
- enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH, SSHCOM } outtype = PRIVATE;\r
- int bits = 1024;\r
- char *comment = NULL, *origcomment = NULL;\r
- int change_passphrase = FALSE;\r
- int errs = FALSE, nogo = FALSE;\r
- int intype = SSH_KEYTYPE_UNOPENABLE;\r
- int sshver = 0;\r
- struct ssh2_userkey *ssh2key = NULL;\r
- struct RSAKey *ssh1key = NULL;\r
- unsigned char *ssh2blob = NULL;\r
- char *ssh2alg = NULL;\r
- const struct ssh_signkey *ssh2algf = NULL;\r
- int ssh2bloblen;\r
- char *passphrase = NULL;\r
- int load_encrypted;\r
- progfn_t progressfn = is_interactive() ? progress_update : no_progress;\r
-\r
- /* ------------------------------------------------------------------\r
- * Parse the command line to figure out what we've been asked to do.\r
- */\r
-\r
- /*\r
- * If run with no arguments at all, print the usage message and\r
- * return success.\r
- */\r
- if (argc <= 1) {\r
- usage(TRUE);\r
- return 0;\r
- }\r
-\r
- /*\r
- * Parse command line arguments.\r
- */\r
- while (--argc) {\r
- char *p = *++argv;\r
- if (*p == '-') {\r
- /*\r
- * An option.\r
- */\r
- while (p && *++p) {\r
- char c = *p;\r
- switch (c) {\r
- case '-':\r
- /*\r
- * Long option.\r
- */\r
- {\r
- char *opt, *val;\r
- opt = p++; /* opt will have _one_ leading - */\r
- while (*p && *p != '=')\r
- p++; /* find end of option */\r
- if (*p == '=') {\r
- *p++ = '\0';\r
- val = p;\r
- } else\r
- val = NULL;\r
-\r
- if (!strcmp(opt, "-help")) {\r
- if (val) {\r
- errs = TRUE;\r
- fprintf(stderr, "puttygen: option `-%s'"\r
- " expects no argument\n", opt);\r
- } else {\r
- help();\r
- nogo = TRUE;\r
- }\r
- } else if (!strcmp(opt, "-version")) {\r
- if (val) {\r
- errs = TRUE;\r
- fprintf(stderr, "puttygen: option `-%s'"\r
- " expects no argument\n", opt);\r
- } else {\r
- showversion();\r
- nogo = TRUE;\r
- }\r
- } else if (!strcmp(opt, "-pgpfp")) {\r
- if (val) {\r
- errs = TRUE;\r
- fprintf(stderr, "puttygen: option `-%s'"\r
- " expects no argument\n", opt);\r
- } else {\r
- /* support --pgpfp for consistency */\r
- pgp_fingerprints();\r
- nogo = TRUE;\r
- }\r
- }\r
- /*\r
- * For long options requiring an argument, add\r
- * code along the lines of\r
- * \r
- * else if (!strcmp(opt, "-output")) {\r
- * if (!val) {\r
- * errs = TRUE;\r
- * fprintf(stderr, "puttygen: option `-%s'"\r
- * " expects an argument\n", opt);\r
- * } else\r
- * ofile = val;\r
- * }\r
- */\r
- else {\r
- errs = TRUE;\r
- fprintf(stderr,\r
- "puttygen: no such option `-%s'\n", opt);\r
- }\r
- }\r
- p = NULL;\r
- break;\r
- case 'h':\r
- case 'V':\r
- case 'P':\r
- case 'l':\r
- case 'L':\r
- case 'p':\r
- case 'q':\r
- /*\r
- * Option requiring no parameter.\r
- */\r
- switch (c) {\r
- case 'h':\r
- help();\r
- nogo = TRUE;\r
- break;\r
- case 'V':\r
- showversion();\r
- nogo = TRUE;\r
- break;\r
- case 'P':\r
- change_passphrase = TRUE;\r
- break;\r
- case 'l':\r
- outtype = FP;\r
- break;\r
- case 'L':\r
- outtype = PUBLICO;\r
- break;\r
- case 'p':\r
- outtype = PUBLIC;\r
- break;\r
- case 'q':\r
- progressfn = no_progress;\r
- break;\r
- }\r
- break;\r
- case 't':\r
- case 'b':\r
- case 'C':\r
- case 'O':\r
- case 'o':\r
- /*\r
- * Option requiring parameter.\r
- */\r
- p++;\r
- if (!*p && argc > 1)\r
- --argc, p = *++argv;\r
- else if (!*p) {\r
- fprintf(stderr, "puttygen: option `-%c' expects a"\r
- " parameter\n", c);\r
- errs = TRUE;\r
- }\r
- /*\r
- * Now c is the option and p is the parameter.\r
- */\r
- switch (c) {\r
- case 't':\r
- if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))\r
- keytype = RSA2, sshver = 2;\r
- else if (!strcmp(p, "rsa1"))\r
- keytype = RSA1, sshver = 1;\r
- else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))\r
- keytype = DSA, sshver = 2;\r
- else {\r
- fprintf(stderr,\r
- "puttygen: unknown key type `%s'\n", p);\r
- errs = TRUE;\r
- }\r
- break;\r
- case 'b':\r
- bits = atoi(p);\r
- break;\r
- case 'C':\r
- comment = p;\r
- break;\r
- case 'O':\r
- if (!strcmp(p, "public"))\r
- outtype = PUBLIC;\r
- else if (!strcmp(p, "public-openssh"))\r
- outtype = PUBLICO;\r
- else if (!strcmp(p, "private"))\r
- outtype = PRIVATE;\r
- else if (!strcmp(p, "fingerprint"))\r
- outtype = FP;\r
- else if (!strcmp(p, "private-openssh"))\r
- outtype = OPENSSH, sshver = 2;\r
- else if (!strcmp(p, "private-sshcom"))\r
- outtype = SSHCOM, sshver = 2;\r
- else {\r
- fprintf(stderr,\r
- "puttygen: unknown output type `%s'\n", p);\r
- errs = TRUE;\r
- }\r
- break;\r
- case 'o':\r
- outfile = p;\r
- break;\r
- }\r
- p = NULL; /* prevent continued processing */\r
- break;\r
- default:\r
- /*\r
- * Unrecognised option.\r
- */\r
- errs = TRUE;\r
- fprintf(stderr, "puttygen: no such option `-%c'\n", c);\r
- break;\r
- }\r
- }\r
- } else {\r
- /*\r
- * A non-option argument.\r
- */\r
- if (!infile)\r
- infile = p;\r
- else {\r
- errs = TRUE;\r
- fprintf(stderr, "puttygen: cannot handle more than one"\r
- " input file\n");\r
- }\r
- }\r
- }\r
-\r
- if (errs)\r
- return 1;\r
-\r
- if (nogo)\r
- return 0;\r
-\r
- /*\r
- * If run with at least one argument _but_ not the required\r
- * ones, print the usage message and return failure.\r
- */\r
- if (!infile && keytype == NOKEYGEN) {\r
- usage(TRUE);\r
- return 1;\r
- }\r
-\r
- /* ------------------------------------------------------------------\r
- * Figure out further details of exactly what we're going to do.\r
- */\r
-\r
- /*\r
- * Bomb out if we've been asked to both load and generate a\r
- * key.\r
- */\r
- if (keytype != NOKEYGEN && infile) {\r
- fprintf(stderr, "puttygen: cannot both load and generate a key\n");\r
- return 1;\r
- }\r
-\r
- /* \r
- * We must save the private part when generating a new key.\r
- */\r
- if (keytype != NOKEYGEN &&\r
- (outtype != PRIVATE && outtype != OPENSSH && outtype != SSHCOM)) {\r
- fprintf(stderr, "puttygen: this would generate a new key but "\r
- "discard the private part\n");\r
- return 1;\r
- }\r
-\r
- /*\r
- * Analyse the type of the input file, in case this affects our\r
- * course of action.\r
- */\r
- if (infile) {\r
- infilename = filename_from_str(infile);\r
-\r
- intype = key_type(&infilename);\r
-\r
- switch (intype) {\r
- /*\r
- * It would be nice here to be able to load _public_\r
- * key files, in any of a number of forms, and (a)\r
- * convert them to other public key types, (b) print\r
- * out their fingerprints. Or, I suppose, for real\r
- * orthogonality, (c) change their comment!\r
- * \r
- * In fact this opens some interesting possibilities.\r
- * Suppose ssh2_userkey_loadpub() were able to load\r
- * public key files as well as extracting the public\r
- * key from private ones. And suppose I did the thing\r
- * I've been wanting to do, where specifying a\r
- * particular private key file for authentication\r
- * causes any _other_ key in the agent to be discarded.\r
- * Then, if you had an agent forwarded to the machine\r
- * you were running Unix PuTTY or Plink on, and you\r
- * needed to specify which of the keys in the agent it\r
- * should use, you could do that by supplying a\r
- * _public_ key file, thus not needing to trust even\r
- * your encrypted private key file to the network. Ooh!\r
- */\r
-\r
- case SSH_KEYTYPE_UNOPENABLE:\r
- case SSH_KEYTYPE_UNKNOWN:\r
- fprintf(stderr, "puttygen: unable to load file `%s': %s\n",\r
- infile, key_type_to_str(intype));\r
- return 1;\r
-\r
- case SSH_KEYTYPE_SSH1:\r
- if (sshver == 2) {\r
- fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"\r
- " not supported\n");\r
- return 1;\r
- }\r
- sshver = 1;\r
- break;\r
-\r
- case SSH_KEYTYPE_SSH2:\r
- case SSH_KEYTYPE_OPENSSH:\r
- case SSH_KEYTYPE_SSHCOM:\r
- if (sshver == 1) {\r
- fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"\r
- " not supported\n");\r
- return 1;\r
- }\r
- sshver = 2;\r
- break;\r
- }\r
- }\r
-\r
- /*\r
- * Determine the default output file, if none is provided.\r
- * \r
- * This will usually be equal to stdout, except that if the\r
- * input and output file formats are the same then the default\r
- * output is to overwrite the input.\r
- * \r
- * Also in this code, we bomb out if the input and output file\r
- * formats are the same and no other action is performed.\r
- */\r
- if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||\r
- (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||\r
- (intype == SSH_KEYTYPE_OPENSSH && outtype == OPENSSH) ||\r
- (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {\r
- if (!outfile) {\r
- outfile = infile;\r
- outfiletmp = dupcat(outfile, ".tmp", NULL);\r
- }\r
-\r
- if (!change_passphrase && !comment) {\r
- fprintf(stderr, "puttygen: this command would perform no useful"\r
- " action\n");\r
- return 1;\r
- }\r
- } else {\r
- if (!outfile) {\r
- /*\r
- * Bomb out rather than automatically choosing to write\r
- * a private key file to stdout.\r
- */\r
- if (outtype==PRIVATE || outtype==OPENSSH || outtype==SSHCOM) {\r
- fprintf(stderr, "puttygen: need to specify an output file\n");\r
- return 1;\r
- }\r
- }\r
- }\r
-\r
- /*\r
- * Figure out whether we need to load the encrypted part of the\r
- * key. This will be the case if either (a) we need to write\r
- * out a private key format, or (b) the entire input key file\r
- * is encrypted.\r
- */\r
- if (outtype == PRIVATE || outtype == OPENSSH || outtype == SSHCOM ||\r
- intype == SSH_KEYTYPE_OPENSSH || intype == SSH_KEYTYPE_SSHCOM)\r
- load_encrypted = TRUE;\r
- else\r
- load_encrypted = FALSE;\r
-\r
- /* ------------------------------------------------------------------\r
- * Now we're ready to actually do some stuff.\r
- */\r
-\r
- /*\r
- * Either load or generate a key.\r
- */\r
- if (keytype != NOKEYGEN) {\r
- char *entropy;\r
- char default_comment[80];\r
- struct tm tm;\r
- struct progress prog;\r
-\r
- prog.phase = -1;\r
- prog.current = -1;\r
-\r
- tm = ltime();\r
- if (keytype == DSA)\r
- strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);\r
- else\r
- strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);\r
-\r
- random_ref();\r
- entropy = get_random_data(bits / 8);\r
- if (!entropy) {\r
- fprintf(stderr, "puttygen: failed to collect entropy, "\r
- "could not generate key\n");\r
- return 1;\r
- }\r
- random_add_heavynoise(entropy, bits / 8);\r
- memset(entropy, 0, bits/8);\r
- sfree(entropy);\r
-\r
- if (keytype == DSA) {\r
- struct dss_key *dsskey = snew(struct dss_key);\r
- dsa_generate(dsskey, bits, progressfn, &prog);\r
- ssh2key = snew(struct ssh2_userkey);\r
- ssh2key->data = dsskey;\r
- ssh2key->alg = &ssh_dss;\r
- ssh1key = NULL;\r
- } else {\r
- struct RSAKey *rsakey = snew(struct RSAKey);\r
- rsa_generate(rsakey, bits, progressfn, &prog);\r
- rsakey->comment = NULL;\r
- if (keytype == RSA1) {\r
- ssh1key = rsakey;\r
- } else {\r
- ssh2key = snew(struct ssh2_userkey);\r
- ssh2key->data = rsakey;\r
- ssh2key->alg = &ssh_rsa;\r
- }\r
- }\r
- progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);\r
-\r
- if (ssh2key)\r
- ssh2key->comment = dupstr(default_comment);\r
- if (ssh1key)\r
- ssh1key->comment = dupstr(default_comment);\r
-\r
- } else {\r
- const char *error = NULL;\r
- int encrypted;\r
-\r
- assert(infile != NULL);\r
-\r
- /*\r
- * Find out whether the input key is encrypted.\r
- */\r
- if (intype == SSH_KEYTYPE_SSH1)\r
- encrypted = rsakey_encrypted(&infilename, &origcomment);\r
- else if (intype == SSH_KEYTYPE_SSH2)\r
- encrypted = ssh2_userkey_encrypted(&infilename, &origcomment);\r
- else\r
- encrypted = import_encrypted(&infilename, intype, &origcomment);\r
-\r
- /*\r
- * If so, ask for a passphrase.\r
- */\r
- if (encrypted && load_encrypted) {\r
- prompts_t *p = new_prompts(NULL);\r
- int ret;\r
- p->to_server = FALSE;\r
- p->name = dupstr("SSH key passphrase");\r
- add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE, 512);\r
- ret = console_get_userpass_input(p, NULL, 0);\r
- assert(ret >= 0);\r
- if (!ret) {\r
- free_prompts(p);\r
- perror("puttygen: unable to read passphrase");\r
- return 1;\r
- } else {\r
- passphrase = dupstr(p->prompts[0]->result);\r
- free_prompts(p);\r
- }\r
- } else {\r
- passphrase = NULL;\r
- }\r
-\r
- switch (intype) {\r
- int ret;\r
-\r
- case SSH_KEYTYPE_SSH1:\r
- ssh1key = snew(struct RSAKey);\r
- if (!load_encrypted) {\r
- void *vblob;\r
- unsigned char *blob;\r
- int n, l, bloblen;\r
-\r
- ret = rsakey_pubblob(&infilename, &vblob, &bloblen,\r
- &origcomment, &error);\r
- blob = (unsigned char *)vblob;\r
-\r
- n = 4; /* skip modulus bits */\r
- \r
- l = ssh1_read_bignum(blob + n, bloblen - n,\r
- &ssh1key->exponent);\r
- if (l < 0) {\r
- error = "SSH-1 public key blob was too short";\r
- } else {\r
- n += l;\r
- l = ssh1_read_bignum(blob + n, bloblen - n,\r
- &ssh1key->modulus);\r
- if (l < 0) {\r
- error = "SSH-1 public key blob was too short";\r
- } else\r
- n += l;\r
- }\r
- ssh1key->comment = dupstr(origcomment);\r
- ssh1key->private_exponent = NULL;\r
- } else {\r
- ret = loadrsakey(&infilename, ssh1key, passphrase, &error);\r
- }\r
- if (ret > 0)\r
- error = NULL;\r
- else if (!error)\r
- error = "unknown error";\r
- break;\r
-\r
- case SSH_KEYTYPE_SSH2:\r
- if (!load_encrypted) {\r
- ssh2blob = ssh2_userkey_loadpub(&infilename, &ssh2alg,\r
- &ssh2bloblen, NULL, &error);\r
- ssh2algf = find_pubkey_alg(ssh2alg);\r
- if (ssh2algf)\r
- bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);\r
- else\r
- bits = -1;\r
- } else {\r
- ssh2key = ssh2_load_userkey(&infilename, passphrase, &error);\r
- }\r
- if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)\r
- error = NULL;\r
- else if (!error) {\r
- if (ssh2key == SSH2_WRONG_PASSPHRASE)\r
- error = "wrong passphrase";\r
- else\r
- error = "unknown error";\r
- }\r
- break;\r
-\r
- case SSH_KEYTYPE_OPENSSH:\r
- case SSH_KEYTYPE_SSHCOM:\r
- ssh2key = import_ssh2(&infilename, intype, passphrase, &error);\r
- if (ssh2key) {\r
- if (ssh2key != SSH2_WRONG_PASSPHRASE)\r
- error = NULL;\r
- else\r
- error = "wrong passphrase";\r
- } else if (!error)\r
- error = "unknown error";\r
- break;\r
-\r
- default:\r
- assert(0);\r
- }\r
-\r
- if (error) {\r
- fprintf(stderr, "puttygen: error loading `%s': %s\n",\r
- infile, error);\r
- return 1;\r
- }\r
- }\r
-\r
- /*\r
- * Change the comment if asked to.\r
- */\r
- if (comment) {\r
- if (sshver == 1) {\r
- assert(ssh1key);\r
- sfree(ssh1key->comment);\r
- ssh1key->comment = dupstr(comment);\r
- } else {\r
- assert(ssh2key);\r
- sfree(ssh2key->comment);\r
- ssh2key->comment = dupstr(comment);\r
- }\r
- }\r
-\r
- /*\r
- * Prompt for a new passphrase if we have been asked to, or if\r
- * we have just generated a key.\r
- */\r
- if (change_passphrase || keytype != NOKEYGEN) {\r
- prompts_t *p = new_prompts(NULL);\r
- int ret;\r
-\r
- p->to_server = FALSE;\r
- p->name = dupstr("New SSH key passphrase");\r
- add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE, 512);\r
- add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE, 512);\r
- ret = console_get_userpass_input(p, NULL, 0);\r
- assert(ret >= 0);\r
- if (!ret) {\r
- free_prompts(p);\r
- perror("puttygen: unable to read new passphrase");\r
- return 1;\r
- } else {\r
- if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {\r
- free_prompts(p);\r
- fprintf(stderr, "puttygen: passphrases do not match\n");\r
- return 1;\r
- }\r
- if (passphrase) {\r
- memset(passphrase, 0, strlen(passphrase));\r
- sfree(passphrase);\r
- }\r
- passphrase = dupstr(p->prompts[0]->result);\r
- free_prompts(p);\r
- if (!*passphrase) {\r
- sfree(passphrase);\r
- passphrase = NULL;\r
- }\r
- }\r
- }\r
-\r
- /*\r
- * Write output.\r
- * \r
- * (In the case where outfile and outfiletmp are both NULL,\r
- * there is no semantic reason to initialise outfilename at\r
- * all; but we have to write _something_ to it or some compiler\r
- * will probably complain that it might be used uninitialised.)\r
- */\r
- if (outfiletmp)\r
- outfilename = filename_from_str(outfiletmp);\r
- else\r
- outfilename = filename_from_str(outfile ? outfile : "");\r
-\r
- switch (outtype) {\r
- int ret;\r
-\r
- case PRIVATE:\r
- if (sshver == 1) {\r
- assert(ssh1key);\r
- ret = saversakey(&outfilename, ssh1key, passphrase);\r
- if (!ret) {\r
- fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");\r
- return 1;\r
- }\r
- } else {\r
- assert(ssh2key);\r
- ret = ssh2_save_userkey(&outfilename, ssh2key, passphrase);\r
- if (!ret) {\r
- fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");\r
- return 1;\r
- }\r
- }\r
- if (outfiletmp) {\r
- if (!move(outfiletmp, outfile))\r
- return 1; /* rename failed */\r
- }\r
- break;\r
-\r
- case PUBLIC:\r
- case PUBLICO:\r
- if (sshver == 1) {\r
- FILE *fp;\r
- char *dec1, *dec2;\r
-\r
- assert(ssh1key);\r
-\r
- if (outfile)\r
- fp = f_open(outfilename, "w", FALSE);\r
- else\r
- fp = stdout;\r
- dec1 = bignum_decimal(ssh1key->exponent);\r
- dec2 = bignum_decimal(ssh1key->modulus);\r
- fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),\r
- dec1, dec2, ssh1key->comment);\r
- sfree(dec1);\r
- sfree(dec2);\r
- if (outfile)\r
- fclose(fp);\r
- } else if (outtype == PUBLIC) {\r
- if (!ssh2blob) {\r
- assert(ssh2key);\r
- ssh2blob = ssh2key->alg->public_blob(ssh2key->data,\r
- &ssh2bloblen);\r
- }\r
- save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,\r
- ssh2blob, ssh2bloblen);\r
- } else if (outtype == PUBLICO) {\r
- char *buffer, *p;\r
- int i;\r
- FILE *fp;\r
-\r
- if (!ssh2blob) {\r
- assert(ssh2key);\r
- ssh2blob = ssh2key->alg->public_blob(ssh2key->data,\r
- &ssh2bloblen);\r
- }\r
- if (!ssh2alg) {\r
- assert(ssh2key);\r
- ssh2alg = ssh2key->alg->name;\r
- }\r
- if (ssh2key)\r
- comment = ssh2key->comment;\r
- else\r
- comment = origcomment;\r
-\r
- buffer = snewn(strlen(ssh2alg) +\r
- 4 * ((ssh2bloblen+2) / 3) +\r
- strlen(comment) + 3, char);\r
- strcpy(buffer, ssh2alg);\r
- p = buffer + strlen(buffer);\r
- *p++ = ' ';\r
- i = 0;\r
- while (i < ssh2bloblen) {\r
- int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);\r
- base64_encode_atom(ssh2blob + i, n, p);\r
- i += n;\r
- p += 4;\r
- }\r
- if (*comment) {\r
- *p++ = ' ';\r
- strcpy(p, comment);\r
- } else\r
- *p++ = '\0';\r
-\r
- if (outfile)\r
- fp = f_open(outfilename, "w", FALSE);\r
- else\r
- fp = stdout;\r
- fprintf(fp, "%s\n", buffer);\r
- if (outfile)\r
- fclose(fp);\r
-\r
- sfree(buffer);\r
- }\r
- break;\r
-\r
- case FP:\r
- {\r
- FILE *fp;\r
- char *fingerprint;\r
-\r
- if (sshver == 1) {\r
- assert(ssh1key);\r
- fingerprint = snewn(128, char);\r
- rsa_fingerprint(fingerprint, 128, ssh1key);\r
- } else {\r
- if (ssh2key) {\r
- fingerprint = ssh2key->alg->fingerprint(ssh2key->data);\r
- } else {\r
- assert(ssh2blob);\r
- fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);\r
- }\r
- }\r
-\r
- if (outfile)\r
- fp = f_open(outfilename, "w", FALSE);\r
- else\r
- fp = stdout;\r
- fprintf(fp, "%s\n", fingerprint);\r
- if (outfile)\r
- fclose(fp);\r
-\r
- sfree(fingerprint);\r
- }\r
- break;\r
- \r
- case OPENSSH:\r
- case SSHCOM:\r
- assert(sshver == 2);\r
- assert(ssh2key);\r
- ret = export_ssh2(&outfilename, outtype, ssh2key, passphrase);\r
- if (!ret) {\r
- fprintf(stderr, "puttygen: unable to export key\n");\r
- return 1;\r
- }\r
- if (outfiletmp) {\r
- if (!move(outfiletmp, outfile))\r
- return 1; /* rename failed */\r
- }\r
- break;\r
- }\r
-\r
- if (passphrase) {\r
- memset(passphrase, 0, strlen(passphrase));\r
- sfree(passphrase);\r
- }\r
-\r
- if (ssh1key)\r
- freersakey(ssh1key);\r
- if (ssh2key) {\r
- ssh2key->alg->freekey(ssh2key->data);\r
- sfree(ssh2key);\r
- }\r
-\r
- return 0;\r
-}\r
-\r
-#ifdef TEST_CMDGEN\r
-\r
-#undef main\r
-\r
-#include <stdarg.h>\r
-\r
-int passes, fails;\r
-\r
-void setup_passphrases(char *first, ...)\r
-{\r
- va_list ap;\r
- char *next;\r
-\r
- nprompts = 0;\r
- if (first) {\r
- prompts[nprompts++] = first;\r
- va_start(ap, first);\r
- while ((next = va_arg(ap, char *)) != NULL) {\r
- assert(nprompts < lenof(prompts));\r
- prompts[nprompts++] = next;\r
- }\r
- va_end(ap);\r
- }\r
-}\r
-\r
-void test(int retval, ...)\r
-{\r
- va_list ap;\r
- int i, argc, ret;\r
- char **argv;\r
-\r
- argc = 0;\r
- va_start(ap, retval);\r
- while (va_arg(ap, char *) != NULL)\r
- argc++;\r
- va_end(ap);\r
-\r
- argv = snewn(argc+1, char *);\r
- va_start(ap, retval);\r
- for (i = 0; i <= argc; i++)\r
- argv[i] = va_arg(ap, char *);\r
- va_end(ap);\r
-\r
- promptsgot = 0;\r
- ret = cmdgen_main(argc, argv);\r
-\r
- if (ret != retval) {\r
- printf("FAILED retval (exp %d got %d):", retval, ret);\r
- for (i = 0; i < argc; i++)\r
- printf(" %s", argv[i]);\r
- printf("\n");\r
- fails++;\r
- } else if (promptsgot != nprompts) {\r
- printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);\r
- for (i = 0; i < argc; i++)\r
- printf(" %s", argv[i]);\r
- printf("\n");\r
- fails++;\r
- } else {\r
- passes++;\r
- }\r
-}\r
-\r
-void filecmp(char *file1, char *file2, char *fmt, ...)\r
-{\r
- /*\r
- * Ideally I should do file comparison myself, to maximise the\r
- * portability of this test suite once this application begins\r
- * running on non-Unix platforms. For the moment, though,\r
- * calling Unix diff is perfectly adequate.\r
- */\r
- char *buf;\r
- int ret;\r
-\r
- buf = dupprintf("diff -q '%s' '%s'", file1, file2);\r
- ret = system(buf);\r
- sfree(buf);\r
-\r
- if (ret) {\r
- va_list ap;\r
-\r
- printf("FAILED diff (ret=%d): ", ret);\r
-\r
- va_start(ap, fmt);\r
- vprintf(fmt, ap);\r
- va_end(ap);\r
-\r
- printf("\n");\r
-\r
- fails++;\r
- } else\r
- passes++;\r
-}\r
-\r
-char *cleanup_fp(char *s)\r
-{\r
- char *p;\r
-\r
- if (!strncmp(s, "ssh-", 4)) {\r
- s += strcspn(s, " \n\t");\r
- s += strspn(s, " \n\t");\r
- }\r
-\r
- p = s;\r
- s += strcspn(s, " \n\t");\r
- s += strspn(s, " \n\t");\r
- s += strcspn(s, " \n\t");\r
-\r
- return dupprintf("%.*s", s - p, p);\r
-}\r
-\r
-char *get_fp(char *filename)\r
-{\r
- FILE *fp;\r
- char buf[256], *ret;\r
-\r
- fp = fopen(filename, "r");\r
- if (!fp)\r
- return NULL;\r
- ret = fgets(buf, sizeof(buf), fp);\r
- fclose(fp);\r
- if (!ret)\r
- return NULL;\r
- return cleanup_fp(buf);\r
-}\r
-\r
-void check_fp(char *filename, char *fp, char *fmt, ...)\r
-{\r
- char *newfp;\r
-\r
- if (!fp)\r
- return;\r
-\r
- newfp = get_fp(filename);\r
-\r
- if (!strcmp(fp, newfp)) {\r
- passes++;\r
- } else {\r
- va_list ap;\r
-\r
- printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);\r
-\r
- va_start(ap, fmt);\r
- vprintf(fmt, ap);\r
- va_end(ap);\r
-\r
- printf("\n");\r
-\r
- fails++;\r
- }\r
-\r
- sfree(newfp);\r
-}\r
-\r
-int main(int argc, char **argv)\r
-{\r
- int i;\r
- static char *const keytypes[] = { "rsa1", "dsa", "rsa" };\r
-\r
- /*\r
- * Even when this thing is compiled for automatic test mode,\r
- * it's helpful to be able to invoke it with command-line\r
- * options for _manual_ tests.\r
- */\r
- if (argc > 1)\r
- return cmdgen_main(argc, argv);\r
-\r
- passes = fails = 0;\r
-\r
- for (i = 0; i < lenof(keytypes); i++) {\r
- char filename[128], osfilename[128], scfilename[128];\r
- char pubfilename[128], tmpfilename1[128], tmpfilename2[128];\r
- char *fp;\r
-\r
- sprintf(filename, "test-%s.ppk", keytypes[i]);\r
- sprintf(pubfilename, "test-%s.pub", keytypes[i]);\r
- sprintf(osfilename, "test-%s.os", keytypes[i]);\r
- sprintf(scfilename, "test-%s.sc", keytypes[i]);\r
- sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);\r
- sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);\r
-\r
- /*\r
- * Create an encrypted key.\r
- */\r
- setup_passphrases("sponge", "sponge", NULL);\r
- test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);\r
-\r
- /*\r
- * List the public key in OpenSSH format.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);\r
- {\r
- char cmdbuf[256];\r
- fp = NULL;\r
- sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",\r
- pubfilename, tmpfilename1);\r
- if (system(cmdbuf) ||\r
- (fp = get_fp(tmpfilename1)) == NULL) {\r
- printf("UNABLE to test fingerprint matching against OpenSSH");\r
- }\r
- }\r
-\r
- /*\r
- * List the public key in IETF/ssh.com format.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-p", filename, NULL);\r
-\r
- /*\r
- * List the fingerprint of the key.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);\r
- if (!fp) {\r
- /*\r
- * If we can't test fingerprints against OpenSSH, we\r
- * can at the very least test equality of all the\r
- * fingerprints we generate of this key throughout\r
- * testing.\r
- */\r
- fp = get_fp(tmpfilename1);\r
- } else {\r
- check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);\r
- }\r
-\r
- /*\r
- * Change the comment of the key; this _does_ require a\r
- * passphrase owing to the tamperproofing.\r
- * \r
- * NOTE: In SSH-1, this only requires a passphrase because\r
- * of inadequacies of the loading and saving mechanisms. In\r
- * _principle_, it should be perfectly possible to modify\r
- * the comment on an SSH-1 key without requiring a\r
- * passphrase; the only reason I can't do it is because my\r
- * loading and saving mechanisms don't include a method of\r
- * loading all the key data without also trying to decrypt\r
- * the private section.\r
- * \r
- * I don't consider this to be a problem worth solving,\r
- * because (a) to fix it would probably end up bloating\r
- * PuTTY proper, and (b) SSH-1 is on the way out anyway so\r
- * it shouldn't be highly significant. If it seriously\r
- * bothers anyone then perhaps I _might_ be persuadable.\r
- */\r
- setup_passphrases("sponge", NULL);\r
- test(0, "puttygen", "-C", "new-comment", filename, NULL);\r
-\r
- /*\r
- * Change the passphrase to nothing.\r
- */\r
- setup_passphrases("sponge", "", "", NULL);\r
- test(0, "puttygen", "-P", filename, NULL);\r
-\r
- /*\r
- * Change the comment of the key again; this time we expect no\r
- * passphrase to be required.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-C", "new-comment-2", filename, NULL);\r
-\r
- /*\r
- * Export the private key into OpenSSH format; no passphrase\r
- * should be required since the key is currently unencrypted.\r
- * For RSA1 keys, this should give an error.\r
- */\r
- setup_passphrases(NULL);\r
- test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,\r
- filename, NULL);\r
-\r
- if (i) {\r
- /*\r
- * List the fingerprint of the OpenSSH-formatted key.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);\r
- check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);\r
-\r
- /*\r
- * List the public half of the OpenSSH-formatted key in\r
- * OpenSSH format.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-L", osfilename, NULL);\r
-\r
- /*\r
- * List the public half of the OpenSSH-formatted key in\r
- * IETF/ssh.com format.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-p", osfilename, NULL);\r
- }\r
-\r
- /*\r
- * Export the private key into ssh.com format; no passphrase\r
- * should be required since the key is currently unencrypted.\r
- * For RSA1 keys, this should give an error.\r
- */\r
- setup_passphrases(NULL);\r
- test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,\r
- filename, NULL);\r
-\r
- if (i) {\r
- /*\r
- * List the fingerprint of the ssh.com-formatted key.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);\r
- check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);\r
-\r
- /*\r
- * List the public half of the ssh.com-formatted key in\r
- * OpenSSH format.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-L", scfilename, NULL);\r
-\r
- /*\r
- * List the public half of the ssh.com-formatted key in\r
- * IETF/ssh.com format.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-p", scfilename, NULL);\r
- }\r
-\r
- if (i) {\r
- /*\r
- * Convert from OpenSSH into ssh.com.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", osfilename, "-o", tmpfilename1,\r
- "-O", "private-sshcom", NULL);\r
-\r
- /*\r
- * Convert from ssh.com back into a PuTTY key,\r
- * supplying the same comment as we had before we\r
- * started to ensure the comparison works.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",\r
- "-o", tmpfilename2, NULL);\r
-\r
- /*\r
- * See if the PuTTY key thus generated is the same as\r
- * the original.\r
- */\r
- filecmp(filename, tmpfilename2,\r
- "p->o->s->p clear %s", keytypes[i]);\r
-\r
- /*\r
- * Convert from ssh.com to OpenSSH.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", scfilename, "-o", tmpfilename1,\r
- "-O", "private-openssh", NULL);\r
-\r
- /*\r
- * Convert from OpenSSH back into a PuTTY key,\r
- * supplying the same comment as we had before we\r
- * started to ensure the comparison works.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",\r
- "-o", tmpfilename2, NULL);\r
-\r
- /*\r
- * See if the PuTTY key thus generated is the same as\r
- * the original.\r
- */\r
- filecmp(filename, tmpfilename2,\r
- "p->s->o->p clear %s", keytypes[i]);\r
-\r
- /*\r
- * Finally, do a round-trip conversion between PuTTY\r
- * and ssh.com without involving OpenSSH, to test that\r
- * the key comment is preserved in that case.\r
- */\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,\r
- filename, NULL);\r
- setup_passphrases(NULL);\r
- test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);\r
- filecmp(filename, tmpfilename2,\r
- "p->s->p clear %s", keytypes[i]);\r
- }\r
-\r
- /*\r
- * Check that mismatched passphrases cause an error.\r
- */\r
- setup_passphrases("sponge2", "sponge3", NULL);\r
- test(1, "puttygen", "-P", filename, NULL);\r
-\r
- /*\r
- * Put a passphrase back on.\r
- */\r
- setup_passphrases("sponge2", "sponge2", NULL);\r
- test(0, "puttygen", "-P", filename, NULL);\r
-\r
- /*\r
- * Export the private key into OpenSSH format, this time\r
- * while encrypted. For RSA1 keys, this should give an\r
- * error.\r
- */\r
- if (i == 0)\r
- setup_passphrases(NULL); /* error, hence no passphrase read */\r
- else\r
- setup_passphrases("sponge2", NULL);\r
- test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,\r
- filename, NULL);\r
-\r
- if (i) {\r
- /*\r
- * List the fingerprint of the OpenSSH-formatted key.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);\r
- check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);\r
-\r
- /*\r
- * List the public half of the OpenSSH-formatted key in\r
- * OpenSSH format.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", "-L", osfilename, NULL);\r
-\r
- /*\r
- * List the public half of the OpenSSH-formatted key in\r
- * IETF/ssh.com format.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", "-p", osfilename, NULL);\r
- }\r
-\r
- /*\r
- * Export the private key into ssh.com format, this time\r
- * while encrypted. For RSA1 keys, this should give an\r
- * error.\r
- */\r
- if (i == 0)\r
- setup_passphrases(NULL); /* error, hence no passphrase read */\r
- else\r
- setup_passphrases("sponge2", NULL);\r
- test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,\r
- filename, NULL);\r
-\r
- if (i) {\r
- /*\r
- * List the fingerprint of the ssh.com-formatted key.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);\r
- check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);\r
-\r
- /*\r
- * List the public half of the ssh.com-formatted key in\r
- * OpenSSH format.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", "-L", scfilename, NULL);\r
-\r
- /*\r
- * List the public half of the ssh.com-formatted key in\r
- * IETF/ssh.com format.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", "-p", scfilename, NULL);\r
- }\r
-\r
- if (i) {\r
- /*\r
- * Convert from OpenSSH into ssh.com.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", osfilename, "-o", tmpfilename1,\r
- "-O", "private-sshcom", NULL);\r
-\r
- /*\r
- * Convert from ssh.com back into a PuTTY key,\r
- * supplying the same comment as we had before we\r
- * started to ensure the comparison works.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",\r
- "-o", tmpfilename2, NULL);\r
-\r
- /*\r
- * See if the PuTTY key thus generated is the same as\r
- * the original.\r
- */\r
- filecmp(filename, tmpfilename2,\r
- "p->o->s->p encrypted %s", keytypes[i]);\r
-\r
- /*\r
- * Convert from ssh.com to OpenSSH.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", scfilename, "-o", tmpfilename1,\r
- "-O", "private-openssh", NULL);\r
-\r
- /*\r
- * Convert from OpenSSH back into a PuTTY key,\r
- * supplying the same comment as we had before we\r
- * started to ensure the comparison works.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",\r
- "-o", tmpfilename2, NULL);\r
-\r
- /*\r
- * See if the PuTTY key thus generated is the same as\r
- * the original.\r
- */\r
- filecmp(filename, tmpfilename2,\r
- "p->s->o->p encrypted %s", keytypes[i]);\r
-\r
- /*\r
- * Finally, do a round-trip conversion between PuTTY\r
- * and ssh.com without involving OpenSSH, to test that\r
- * the key comment is preserved in that case.\r
- */\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,\r
- filename, NULL);\r
- setup_passphrases("sponge2", NULL);\r
- test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);\r
- filecmp(filename, tmpfilename2,\r
- "p->s->p encrypted %s", keytypes[i]);\r
- }\r
-\r
- /*\r
- * Load with the wrong passphrase.\r
- */\r
- setup_passphrases("sponge8", NULL);\r
- test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);\r
-\r
- /*\r
- * Load a totally bogus file.\r
- */\r
- setup_passphrases(NULL);\r
- test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);\r
- }\r
- printf("%d passes, %d fails\n", passes, fails);\r
- return 0;\r
-}\r
-\r
-#endif\r