2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
19 * This section overrides some definitions below for test purposes.
20 * When compiled with -DTEST_CMDGEN:
22 * - Calls to get_random_data() are replaced with the diagnostic
23 * function below (I #define the name so that I can still link
24 * with the original set of modules without symbol clash), in
25 * order to avoid depleting the test system's /dev/random
28 * - Calls to console_get_userpass_input() are replaced with the
29 * diagnostic function below, so that I can run tests in an
30 * automated manner and provide their interactive passphrase
33 * - main() is renamed to cmdgen_main(); at the bottom of the file
34 * I define another main() which calls the former repeatedly to
37 #define get_random_data get_random_data_diagnostic
38 char *get_random_data(int len)
40 char *buf = snewn(len, char);
41 memset(buf, 'x', len);
44 #define console_get_userpass_input console_get_userpass_input_diagnostic
45 int nprompts, promptsgot;
46 const char *prompts[3];
47 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
51 for (i = 0; i < p->n_prompts; i++) {
52 if (promptsgot < nprompts) {
53 assert(strlen(prompts[promptsgot]) < p->prompts[i]->result_len);
54 strcpy(p->prompts[i]->result, prompts[promptsgot++]);
56 promptsgot++; /* track number of requests anyway */
62 #define main cmdgen_main
69 static void progress_update(void *param, int action, int phase, int iprogress)
71 struct progress *p = (struct progress *)param;
72 if (action != PROGFN_PROGRESS)
74 if (phase > p->phase) {
79 p->current = iprogress - 1;
81 p->current = iprogress;
83 while (p->current < iprogress) {
90 static void no_progress(void *param, int action, int phase, int iprogress)
94 void modalfatalbox(char *p, ...)
97 fprintf(stderr, "FATAL ERROR: ");
99 vfprintf(stderr, p, ap);
105 void nonfatal(char *p, ...)
108 fprintf(stderr, "ERROR: ");
110 vfprintf(stderr, p, ap);
116 * Stubs to let everything else link sensibly.
118 void log_eventlog(void *handle, const char *event)
121 char *x_get_default(const char *key)
125 void sk_cleanup(void)
129 void showversion(void)
131 printf("puttygen: %s\n", ver);
134 void usage(int standalone)
137 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
138 " [ -C comment ] [ -P ] [ -q ]\n"
139 " [ -o output-keyfile ] [ -O type | -l | -L"
143 "Use \"puttygen --help\" for more detail.\n");
149 * Help message is an extended version of the usage message. So
150 * start with that, plus a version heading.
155 " -t specify key type when generating (rsa, dsa, rsa1)\n"
156 " -b specify number of bits when generating key\n"
157 " -C change or specify key comment\n"
158 " -P change key passphrase\n"
159 " -q quiet: do not display progress bar\n"
160 " -O specify output type:\n"
161 " private output PuTTY private key format\n"
162 " private-openssh export OpenSSH private key\n"
163 " private-openssh-pem export OpenSSH private key "
164 "(force old PEM format)\n"
165 " private-openssh-new export OpenSSH private key "
166 "(force new format)\n"
167 " private-sshcom export ssh.com private key\n"
168 " public standard / ssh.com public key\n"
169 " public-openssh OpenSSH public key\n"
170 " fingerprint output the key fingerprint\n"
171 " -o specify output file\n"
172 " -l equivalent to `-O fingerprint'\n"
173 " -L equivalent to `-O public-openssh'\n"
174 " -p equivalent to `-O public'\n"
178 static int save_ssh2_pubkey(char *filename, char *comment,
179 void *v_pub_blob, int pub_len)
181 unsigned char *pub_blob = (unsigned char *)v_pub_blob;
187 fp = fopen(filename, "wb");
193 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
196 fprintf(fp, "Comment: \"");
197 for (p = comment; *p; p++) {
198 if (*p == '\\' || *p == '\"')
207 while (i < pub_len) {
209 int n = (pub_len - i < 3 ? pub_len - i : 3);
210 base64_encode_atom(pub_blob + i, n, buf);
214 if (++column >= 16) {
222 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
228 static int move(char *from, char *to)
232 ret = rename(from, to);
235 * This OS may require us to remove the original file first.
238 ret = rename(from, to);
241 perror("puttygen: cannot move new file on to old one");
247 static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
250 unsigned char digest[16];
251 struct MD5Context md5c;
255 MD5Update(&md5c, blob, bloblen);
256 MD5Final(digest, &md5c);
258 sprintf(buffer, "%s ", alg);
260 sprintf(buffer + strlen(buffer), "%d ", bits);
261 for (i = 0; i < 16; i++)
262 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
265 return dupstr(buffer);
268 int main(int argc, char **argv)
271 Filename *infilename = NULL, *outfilename = NULL;
272 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA } keytype = NOKEYGEN;
273 char *outfile = NULL, *outfiletmp = NULL;
274 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_PEM,
275 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
277 char *comment = NULL, *origcomment = NULL;
278 int change_passphrase = FALSE;
279 int errs = FALSE, nogo = FALSE;
280 int intype = SSH_KEYTYPE_UNOPENABLE;
282 struct ssh2_userkey *ssh2key = NULL;
283 struct RSAKey *ssh1key = NULL;
284 unsigned char *ssh2blob = NULL;
285 char *ssh2alg = NULL;
286 const struct ssh_signkey *ssh2algf = NULL;
288 char *passphrase = NULL;
290 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
292 /* ------------------------------------------------------------------
293 * Parse the command line to figure out what we've been asked to do.
297 * If run with no arguments at all, print the usage message and
306 * Parse command line arguments.
323 opt = p++; /* opt will have _one_ leading - */
324 while (*p && *p != '=')
325 p++; /* find end of option */
332 if (!strcmp(opt, "-help")) {
335 fprintf(stderr, "puttygen: option `-%s'"
336 " expects no argument\n", opt);
341 } else if (!strcmp(opt, "-version")) {
344 fprintf(stderr, "puttygen: option `-%s'"
345 " expects no argument\n", opt);
350 } else if (!strcmp(opt, "-pgpfp")) {
353 fprintf(stderr, "puttygen: option `-%s'"
354 " expects no argument\n", opt);
356 /* support --pgpfp for consistency */
362 * For long options requiring an argument, add
363 * code along the lines of
365 * else if (!strcmp(opt, "-output")) {
368 * fprintf(stderr, "puttygen: option `-%s'"
369 * " expects an argument\n", opt);
377 "puttygen: no such option `-%s'\n", opt);
390 * Option requiring no parameter.
402 change_passphrase = TRUE;
414 progressfn = no_progress;
424 * Option requiring parameter.
430 fprintf(stderr, "puttygen: option `-%c' expects a"
435 * Now c is the option and p is the parameter.
439 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
440 keytype = RSA2, sshver = 2;
441 else if (!strcmp(p, "rsa1"))
442 keytype = RSA1, sshver = 1;
443 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
444 keytype = DSA, sshver = 2;
445 else if (!strcmp(p, "ecdsa"))
446 keytype = ECDSA, sshver = 2;
449 "puttygen: unknown key type `%s'\n", p);
460 if (!strcmp(p, "public"))
462 else if (!strcmp(p, "public-openssh"))
464 else if (!strcmp(p, "private"))
466 else if (!strcmp(p, "fingerprint"))
468 else if (!strcmp(p, "private-openssh") ||
469 !strcmp(p, "private-openssh-pem"))
470 outtype = OPENSSH_PEM, sshver = 2;
471 else if (!strcmp(p, "private-openssh-new"))
472 outtype = OPENSSH_NEW, sshver = 2;
473 else if (!strcmp(p, "private-sshcom"))
474 outtype = SSHCOM, sshver = 2;
477 "puttygen: unknown output type `%s'\n", p);
485 p = NULL; /* prevent continued processing */
489 * Unrecognised option.
492 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
498 * A non-option argument.
504 fprintf(stderr, "puttygen: cannot handle more than one"
512 * No explicit key size was specified. Default varies
513 * depending on key type.
525 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
526 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
537 * If run with at least one argument _but_ not the required
538 * ones, print the usage message and return failure.
540 if (!infile && keytype == NOKEYGEN) {
545 /* ------------------------------------------------------------------
546 * Figure out further details of exactly what we're going to do.
550 * Bomb out if we've been asked to both load and generate a
553 if (keytype != NOKEYGEN && infile) {
554 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
559 * We must save the private part when generating a new key.
561 if (keytype != NOKEYGEN &&
562 (outtype != PRIVATE && outtype != OPENSSH_PEM &&
563 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
564 fprintf(stderr, "puttygen: this would generate a new key but "
565 "discard the private part\n");
570 * Analyse the type of the input file, in case this affects our
574 infilename = filename_from_str(infile);
576 intype = key_type(infilename);
580 * It would be nice here to be able to load _public_
581 * key files, in any of a number of forms, and (a)
582 * convert them to other public key types, (b) print
583 * out their fingerprints. Or, I suppose, for real
584 * orthogonality, (c) change their comment!
586 * In fact this opens some interesting possibilities.
587 * Suppose ssh2_userkey_loadpub() were able to load
588 * public key files as well as extracting the public
589 * key from private ones. And suppose I did the thing
590 * I've been wanting to do, where specifying a
591 * particular private key file for authentication
592 * causes any _other_ key in the agent to be discarded.
593 * Then, if you had an agent forwarded to the machine
594 * you were running Unix PuTTY or Plink on, and you
595 * needed to specify which of the keys in the agent it
596 * should use, you could do that by supplying a
597 * _public_ key file, thus not needing to trust even
598 * your encrypted private key file to the network. Ooh!
601 case SSH_KEYTYPE_UNOPENABLE:
602 case SSH_KEYTYPE_UNKNOWN:
603 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
604 infile, key_type_to_str(intype));
607 case SSH_KEYTYPE_SSH1:
609 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
616 case SSH_KEYTYPE_SSH2:
617 case SSH_KEYTYPE_OPENSSH_PEM:
618 case SSH_KEYTYPE_OPENSSH_NEW:
619 case SSH_KEYTYPE_SSHCOM:
621 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
631 * Determine the default output file, if none is provided.
633 * This will usually be equal to stdout, except that if the
634 * input and output file formats are the same then the default
635 * output is to overwrite the input.
637 * Also in this code, we bomb out if the input and output file
638 * formats are the same and no other action is performed.
640 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
641 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
642 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_PEM) ||
643 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
644 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
647 outfiletmp = dupcat(outfile, ".tmp", NULL);
650 if (!change_passphrase && !comment) {
651 fprintf(stderr, "puttygen: this command would perform no useful"
658 * Bomb out rather than automatically choosing to write
659 * a private key file to stdout.
661 if (outtype == PRIVATE || outtype == OPENSSH_PEM ||
662 outtype == OPENSSH_NEW || outtype == SSHCOM) {
663 fprintf(stderr, "puttygen: need to specify an output file\n");
670 * Figure out whether we need to load the encrypted part of the
671 * key. This will be the case if either (a) we need to write
672 * out a private key format, or (b) the entire input key file
675 if (outtype == PRIVATE || outtype == OPENSSH_PEM ||
676 outtype == OPENSSH_NEW || outtype == SSHCOM ||
677 intype == SSH_KEYTYPE_OPENSSH_PEM ||
678 intype == SSH_KEYTYPE_OPENSSH_NEW ||
679 intype == SSH_KEYTYPE_SSHCOM)
680 load_encrypted = TRUE;
682 load_encrypted = FALSE;
684 /* ------------------------------------------------------------------
685 * Now we're ready to actually do some stuff.
689 * Either load or generate a key.
691 if (keytype != NOKEYGEN) {
693 char default_comment[80];
695 struct progress prog;
702 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
703 else if (keytype == ECDSA)
704 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
706 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
709 entropy = get_random_data(bits / 8);
711 fprintf(stderr, "puttygen: failed to collect entropy, "
712 "could not generate key\n");
715 random_add_heavynoise(entropy, bits / 8);
716 smemclr(entropy, bits/8);
719 if (keytype == DSA) {
720 struct dss_key *dsskey = snew(struct dss_key);
721 dsa_generate(dsskey, bits, progressfn, &prog);
722 ssh2key = snew(struct ssh2_userkey);
723 ssh2key->data = dsskey;
724 ssh2key->alg = &ssh_dss;
726 } else if (keytype == ECDSA) {
727 struct ec_key *ec = snew(struct ec_key);
728 ec_generate(ec, bits, progressfn, &prog);
729 ssh2key = snew(struct ssh2_userkey);
732 ssh2key->alg = &ssh_ecdsa_nistp256;
733 } else if (bits == 384) {
734 ssh2key->alg = &ssh_ecdsa_nistp384;
736 ssh2key->alg = &ssh_ecdsa_nistp521;
740 struct RSAKey *rsakey = snew(struct RSAKey);
741 rsa_generate(rsakey, bits, progressfn, &prog);
742 rsakey->comment = NULL;
743 if (keytype == RSA1) {
746 ssh2key = snew(struct ssh2_userkey);
747 ssh2key->data = rsakey;
748 ssh2key->alg = &ssh_rsa;
751 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
754 ssh2key->comment = dupstr(default_comment);
756 ssh1key->comment = dupstr(default_comment);
759 const char *error = NULL;
762 assert(infile != NULL);
765 * Find out whether the input key is encrypted.
767 if (intype == SSH_KEYTYPE_SSH1)
768 encrypted = rsakey_encrypted(infilename, &origcomment);
769 else if (intype == SSH_KEYTYPE_SSH2)
770 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
772 encrypted = import_encrypted(infilename, intype, &origcomment);
775 * If so, ask for a passphrase.
777 if (encrypted && load_encrypted) {
778 prompts_t *p = new_prompts(NULL);
780 p->to_server = FALSE;
781 p->name = dupstr("SSH key passphrase");
782 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
783 ret = console_get_userpass_input(p, NULL, 0);
787 perror("puttygen: unable to read passphrase");
790 passphrase = dupstr(p->prompts[0]->result);
800 case SSH_KEYTYPE_SSH1:
801 ssh1key = snew(struct RSAKey);
802 if (!load_encrypted) {
807 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
808 &origcomment, &error);
809 blob = (unsigned char *)vblob;
811 n = 4; /* skip modulus bits */
813 l = ssh1_read_bignum(blob + n, bloblen - n,
816 error = "SSH-1 public key blob was too short";
819 l = ssh1_read_bignum(blob + n, bloblen - n,
822 error = "SSH-1 public key blob was too short";
826 ssh1key->comment = dupstr(origcomment);
827 ssh1key->private_exponent = NULL;
830 ssh1key->iqmp = NULL;
832 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
837 error = "unknown error";
840 case SSH_KEYTYPE_SSH2:
841 if (!load_encrypted) {
842 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
843 &ssh2bloblen, NULL, &error);
845 ssh2algf = find_pubkey_alg(ssh2alg);
847 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
852 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
854 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
857 if (ssh2key == SSH2_WRONG_PASSPHRASE)
858 error = "wrong passphrase";
860 error = "unknown error";
864 case SSH_KEYTYPE_OPENSSH_PEM:
865 case SSH_KEYTYPE_OPENSSH_NEW:
866 case SSH_KEYTYPE_SSHCOM:
867 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
869 if (ssh2key != SSH2_WRONG_PASSPHRASE)
872 error = "wrong passphrase";
874 error = "unknown error";
882 fprintf(stderr, "puttygen: error loading `%s': %s\n",
889 * Change the comment if asked to.
894 sfree(ssh1key->comment);
895 ssh1key->comment = dupstr(comment);
898 sfree(ssh2key->comment);
899 ssh2key->comment = dupstr(comment);
904 * Prompt for a new passphrase if we have been asked to, or if
905 * we have just generated a key.
907 if (change_passphrase || keytype != NOKEYGEN) {
908 prompts_t *p = new_prompts(NULL);
911 p->to_server = FALSE;
912 p->name = dupstr("New SSH key passphrase");
913 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
914 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
915 ret = console_get_userpass_input(p, NULL, 0);
919 perror("puttygen: unable to read new passphrase");
922 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
924 fprintf(stderr, "puttygen: passphrases do not match\n");
928 smemclr(passphrase, strlen(passphrase));
931 passphrase = dupstr(p->prompts[0]->result);
943 * (In the case where outfile and outfiletmp are both NULL,
944 * there is no semantic reason to initialise outfilename at
945 * all; but we have to write _something_ to it or some compiler
946 * will probably complain that it might be used uninitialised.)
949 outfilename = filename_from_str(outfiletmp);
951 outfilename = filename_from_str(outfile ? outfile : "");
954 int ret, real_outtype;
959 ret = saversakey(outfilename, ssh1key, passphrase);
961 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
966 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
968 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
973 if (!move(outfiletmp, outfile))
974 return 1; /* rename failed */
987 fp = f_open(outfilename, "w", FALSE);
990 dec1 = bignum_decimal(ssh1key->exponent);
991 dec2 = bignum_decimal(ssh1key->modulus);
992 fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
993 dec1, dec2, ssh1key->comment);
998 } else if (outtype == PUBLIC) {
1001 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1004 save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
1005 ssh2blob, ssh2bloblen);
1006 } else if (outtype == PUBLICO) {
1013 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1018 ssh2alg = ssh2key->alg->name;
1021 comment = ssh2key->comment;
1023 comment = origcomment;
1025 buffer = snewn(strlen(ssh2alg) +
1026 4 * ((ssh2bloblen+2) / 3) +
1027 strlen(comment) + 3, char);
1028 strcpy(buffer, ssh2alg);
1029 p = buffer + strlen(buffer);
1032 while (i < ssh2bloblen) {
1033 int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
1034 base64_encode_atom(ssh2blob + i, n, p);
1045 fp = f_open(outfilename, "w", FALSE);
1048 fprintf(fp, "%s\n", buffer);
1063 fingerprint = snewn(128, char);
1064 rsa_fingerprint(fingerprint, 128, ssh1key);
1067 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
1070 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
1075 fp = f_open(outfilename, "w", FALSE);
1078 fprintf(fp, "%s\n", fingerprint);
1089 assert(sshver == 2);
1091 random_ref(); /* both foreign key types require randomness,
1092 * for IV or padding */
1095 real_outtype = SSH_KEYTYPE_OPENSSH_PEM;
1098 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1101 real_outtype = SSH_KEYTYPE_SSHCOM;
1104 assert(0 && "control flow goof");
1106 ret = export_ssh2(outfilename, real_outtype, ssh2key, passphrase);
1108 fprintf(stderr, "puttygen: unable to export key\n");
1112 if (!move(outfiletmp, outfile))
1113 return 1; /* rename failed */
1119 smemclr(passphrase, strlen(passphrase));
1124 freersakey(ssh1key);
1126 ssh2key->alg->freekey(ssh2key->data);
1141 void setup_passphrases(char *first, ...)
1148 prompts[nprompts++] = first;
1149 va_start(ap, first);
1150 while ((next = va_arg(ap, char *)) != NULL) {
1151 assert(nprompts < lenof(prompts));
1152 prompts[nprompts++] = next;
1158 void test(int retval, ...)
1165 va_start(ap, retval);
1166 while (va_arg(ap, char *) != NULL)
1170 argv = snewn(argc+1, char *);
1171 va_start(ap, retval);
1172 for (i = 0; i <= argc; i++)
1173 argv[i] = va_arg(ap, char *);
1177 ret = cmdgen_main(argc, argv);
1179 if (ret != retval) {
1180 printf("FAILED retval (exp %d got %d):", retval, ret);
1181 for (i = 0; i < argc; i++)
1182 printf(" %s", argv[i]);
1185 } else if (promptsgot != nprompts) {
1186 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1187 for (i = 0; i < argc; i++)
1188 printf(" %s", argv[i]);
1196 void filecmp(char *file1, char *file2, char *fmt, ...)
1199 * Ideally I should do file comparison myself, to maximise the
1200 * portability of this test suite once this application begins
1201 * running on non-Unix platforms. For the moment, though,
1202 * calling Unix diff is perfectly adequate.
1207 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1214 printf("FAILED diff (ret=%d): ", ret);
1227 char *cleanup_fp(char *s)
1231 if (!strncmp(s, "ssh-", 4)) {
1232 s += strcspn(s, " \n\t");
1233 s += strspn(s, " \n\t");
1237 s += strcspn(s, " \n\t");
1238 s += strspn(s, " \n\t");
1239 s += strcspn(s, " \n\t");
1241 return dupprintf("%.*s", s - p, p);
1244 char *get_fp(char *filename)
1247 char buf[256], *ret;
1249 fp = fopen(filename, "r");
1252 ret = fgets(buf, sizeof(buf), fp);
1256 return cleanup_fp(buf);
1259 void check_fp(char *filename, char *fp, char *fmt, ...)
1266 newfp = get_fp(filename);
1268 if (!strcmp(fp, newfp)) {
1273 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1287 int main(int argc, char **argv)
1290 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1293 * Even when this thing is compiled for automatic test mode,
1294 * it's helpful to be able to invoke it with command-line
1295 * options for _manual_ tests.
1298 return cmdgen_main(argc, argv);
1302 for (i = 0; i < lenof(keytypes); i++) {
1303 char filename[128], osfilename[128], scfilename[128];
1304 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1307 sprintf(filename, "test-%s.ppk", keytypes[i]);
1308 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1309 sprintf(osfilename, "test-%s.os", keytypes[i]);
1310 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1311 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1312 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1315 * Create an encrypted key.
1317 setup_passphrases("sponge", "sponge", NULL);
1318 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1321 * List the public key in OpenSSH format.
1323 setup_passphrases(NULL);
1324 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1328 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1329 pubfilename, tmpfilename1);
1330 if (system(cmdbuf) ||
1331 (fp = get_fp(tmpfilename1)) == NULL) {
1332 printf("UNABLE to test fingerprint matching against OpenSSH");
1337 * List the public key in IETF/ssh.com format.
1339 setup_passphrases(NULL);
1340 test(0, "puttygen", "-p", filename, NULL);
1343 * List the fingerprint of the key.
1345 setup_passphrases(NULL);
1346 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1349 * If we can't test fingerprints against OpenSSH, we
1350 * can at the very least test equality of all the
1351 * fingerprints we generate of this key throughout
1354 fp = get_fp(tmpfilename1);
1356 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1360 * Change the comment of the key; this _does_ require a
1361 * passphrase owing to the tamperproofing.
1363 * NOTE: In SSH-1, this only requires a passphrase because
1364 * of inadequacies of the loading and saving mechanisms. In
1365 * _principle_, it should be perfectly possible to modify
1366 * the comment on an SSH-1 key without requiring a
1367 * passphrase; the only reason I can't do it is because my
1368 * loading and saving mechanisms don't include a method of
1369 * loading all the key data without also trying to decrypt
1370 * the private section.
1372 * I don't consider this to be a problem worth solving,
1373 * because (a) to fix it would probably end up bloating
1374 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1375 * it shouldn't be highly significant. If it seriously
1376 * bothers anyone then perhaps I _might_ be persuadable.
1378 setup_passphrases("sponge", NULL);
1379 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1382 * Change the passphrase to nothing.
1384 setup_passphrases("sponge", "", "", NULL);
1385 test(0, "puttygen", "-P", filename, NULL);
1388 * Change the comment of the key again; this time we expect no
1389 * passphrase to be required.
1391 setup_passphrases(NULL);
1392 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1395 * Export the private key into OpenSSH format; no passphrase
1396 * should be required since the key is currently unencrypted.
1397 * For RSA1 keys, this should give an error.
1399 setup_passphrases(NULL);
1400 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1405 * List the fingerprint of the OpenSSH-formatted key.
1407 setup_passphrases(NULL);
1408 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1409 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1412 * List the public half of the OpenSSH-formatted key in
1415 setup_passphrases(NULL);
1416 test(0, "puttygen", "-L", osfilename, NULL);
1419 * List the public half of the OpenSSH-formatted key in
1420 * IETF/ssh.com format.
1422 setup_passphrases(NULL);
1423 test(0, "puttygen", "-p", osfilename, NULL);
1427 * Export the private key into ssh.com format; no passphrase
1428 * should be required since the key is currently unencrypted.
1429 * For RSA1 keys, this should give an error.
1431 setup_passphrases(NULL);
1432 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1437 * List the fingerprint of the ssh.com-formatted key.
1439 setup_passphrases(NULL);
1440 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1441 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1444 * List the public half of the ssh.com-formatted key in
1447 setup_passphrases(NULL);
1448 test(0, "puttygen", "-L", scfilename, NULL);
1451 * List the public half of the ssh.com-formatted key in
1452 * IETF/ssh.com format.
1454 setup_passphrases(NULL);
1455 test(0, "puttygen", "-p", scfilename, NULL);
1460 * Convert from OpenSSH into ssh.com.
1462 setup_passphrases(NULL);
1463 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1464 "-O", "private-sshcom", NULL);
1467 * Convert from ssh.com back into a PuTTY key,
1468 * supplying the same comment as we had before we
1469 * started to ensure the comparison works.
1471 setup_passphrases(NULL);
1472 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1473 "-o", tmpfilename2, NULL);
1476 * See if the PuTTY key thus generated is the same as
1479 filecmp(filename, tmpfilename2,
1480 "p->o->s->p clear %s", keytypes[i]);
1483 * Convert from ssh.com to OpenSSH.
1485 setup_passphrases(NULL);
1486 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1487 "-O", "private-openssh", NULL);
1490 * Convert from OpenSSH back into a PuTTY key,
1491 * supplying the same comment as we had before we
1492 * started to ensure the comparison works.
1494 setup_passphrases(NULL);
1495 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1496 "-o", tmpfilename2, NULL);
1499 * See if the PuTTY key thus generated is the same as
1502 filecmp(filename, tmpfilename2,
1503 "p->s->o->p clear %s", keytypes[i]);
1506 * Finally, do a round-trip conversion between PuTTY
1507 * and ssh.com without involving OpenSSH, to test that
1508 * the key comment is preserved in that case.
1510 setup_passphrases(NULL);
1511 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1513 setup_passphrases(NULL);
1514 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1515 filecmp(filename, tmpfilename2,
1516 "p->s->p clear %s", keytypes[i]);
1520 * Check that mismatched passphrases cause an error.
1522 setup_passphrases("sponge2", "sponge3", NULL);
1523 test(1, "puttygen", "-P", filename, NULL);
1526 * Put a passphrase back on.
1528 setup_passphrases("sponge2", "sponge2", NULL);
1529 test(0, "puttygen", "-P", filename, NULL);
1532 * Export the private key into OpenSSH format, this time
1533 * while encrypted. For RSA1 keys, this should give an
1537 setup_passphrases(NULL); /* error, hence no passphrase read */
1539 setup_passphrases("sponge2", NULL);
1540 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1545 * List the fingerprint of the OpenSSH-formatted key.
1547 setup_passphrases("sponge2", NULL);
1548 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1549 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1552 * List the public half of the OpenSSH-formatted key in
1555 setup_passphrases("sponge2", NULL);
1556 test(0, "puttygen", "-L", osfilename, NULL);
1559 * List the public half of the OpenSSH-formatted key in
1560 * IETF/ssh.com format.
1562 setup_passphrases("sponge2", NULL);
1563 test(0, "puttygen", "-p", osfilename, NULL);
1567 * Export the private key into ssh.com format, this time
1568 * while encrypted. For RSA1 keys, this should give an
1572 setup_passphrases(NULL); /* error, hence no passphrase read */
1574 setup_passphrases("sponge2", NULL);
1575 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1580 * List the fingerprint of the ssh.com-formatted key.
1582 setup_passphrases("sponge2", NULL);
1583 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1584 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1587 * List the public half of the ssh.com-formatted key in
1590 setup_passphrases("sponge2", NULL);
1591 test(0, "puttygen", "-L", scfilename, NULL);
1594 * List the public half of the ssh.com-formatted key in
1595 * IETF/ssh.com format.
1597 setup_passphrases("sponge2", NULL);
1598 test(0, "puttygen", "-p", scfilename, NULL);
1603 * Convert from OpenSSH into ssh.com.
1605 setup_passphrases("sponge2", NULL);
1606 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1607 "-O", "private-sshcom", NULL);
1610 * Convert from ssh.com back into a PuTTY key,
1611 * supplying the same comment as we had before we
1612 * started to ensure the comparison works.
1614 setup_passphrases("sponge2", NULL);
1615 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1616 "-o", tmpfilename2, NULL);
1619 * See if the PuTTY key thus generated is the same as
1622 filecmp(filename, tmpfilename2,
1623 "p->o->s->p encrypted %s", keytypes[i]);
1626 * Convert from ssh.com to OpenSSH.
1628 setup_passphrases("sponge2", NULL);
1629 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1630 "-O", "private-openssh", NULL);
1633 * Convert from OpenSSH back into a PuTTY key,
1634 * supplying the same comment as we had before we
1635 * started to ensure the comparison works.
1637 setup_passphrases("sponge2", NULL);
1638 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1639 "-o", tmpfilename2, NULL);
1642 * See if the PuTTY key thus generated is the same as
1645 filecmp(filename, tmpfilename2,
1646 "p->s->o->p encrypted %s", keytypes[i]);
1649 * Finally, do a round-trip conversion between PuTTY
1650 * and ssh.com without involving OpenSSH, to test that
1651 * the key comment is preserved in that case.
1653 setup_passphrases("sponge2", NULL);
1654 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1656 setup_passphrases("sponge2", NULL);
1657 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1658 filecmp(filename, tmpfilename2,
1659 "p->s->p encrypted %s", keytypes[i]);
1663 * Load with the wrong passphrase.
1665 setup_passphrases("sponge8", NULL);
1666 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1669 * Load a totally bogus file.
1671 setup_passphrases(NULL);
1672 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1674 printf("%d passes, %d fails\n", passes, fails);