2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
21 * This section overrides some definitions below for test purposes.
22 * When compiled with -DTEST_CMDGEN:
24 * - Calls to get_random_data() are replaced with the diagnostic
25 * function below (I #define the name so that I can still link
26 * with the original set of modules without symbol clash), in
27 * order to avoid depleting the test system's /dev/random
30 * - Calls to console_get_userpass_input() are replaced with the
31 * diagnostic function below, so that I can run tests in an
32 * automated manner and provide their interactive passphrase
35 * - main() is renamed to cmdgen_main(); at the bottom of the file
36 * I define another main() which calls the former repeatedly to
39 #define get_random_data get_random_data_diagnostic
40 char *get_random_data(int len)
42 char *buf = snewn(len, char);
43 memset(buf, 'x', len);
46 #define console_get_userpass_input console_get_userpass_input_diagnostic
47 int nprompts, promptsgot;
48 const char *prompts[3];
49 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
53 for (i = 0; i < p->n_prompts; i++) {
54 if (promptsgot < nprompts) {
55 assert(strlen(prompts[promptsgot]) < p->prompts[i]->result_len);
56 strcpy(p->prompts[i]->result, prompts[promptsgot++]);
58 promptsgot++; /* track number of requests anyway */
64 #define main cmdgen_main
71 static void progress_update(void *param, int action, int phase, int iprogress)
73 struct progress *p = (struct progress *)param;
74 if (action != PROGFN_PROGRESS)
76 if (phase > p->phase) {
81 p->current = iprogress - 1;
83 p->current = iprogress;
85 while (p->current < iprogress) {
92 static void no_progress(void *param, int action, int phase, int iprogress)
96 void modalfatalbox(const char *p, ...)
99 fprintf(stderr, "FATAL ERROR: ");
101 vfprintf(stderr, p, ap);
107 void nonfatal(const char *p, ...)
110 fprintf(stderr, "ERROR: ");
112 vfprintf(stderr, p, ap);
118 * Stubs to let everything else link sensibly.
120 void log_eventlog(void *handle, const char *event)
123 char *x_get_default(const char *key)
127 void sk_cleanup(void)
131 void showversion(void)
133 printf("puttygen: %s\n", ver);
136 void usage(int standalone)
139 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
140 " [ -C comment ] [ -P ] [ -q ]\n"
141 " [ -o output-keyfile ] [ -O type | -l | -L"
145 "Use \"puttygen --help\" for more detail.\n");
151 * Help message is an extended version of the usage message. So
152 * start with that, plus a version heading.
157 " -t specify key type when generating (ed25519, ecdsa, rsa, "
159 " -b specify number of bits when generating key\n"
160 " -C change or specify key comment\n"
161 " -P change key passphrase\n"
162 " -q quiet: do not display progress bar\n"
163 " -O specify output type:\n"
164 " private output PuTTY private key format\n"
165 " private-openssh export OpenSSH private key\n"
166 " private-openssh-new export OpenSSH private key "
167 "(force new file format)\n"
168 " private-sshcom export ssh.com private key\n"
169 " public RFC 4716 / ssh.com public key\n"
170 " public-openssh OpenSSH public key\n"
171 " fingerprint output the key fingerprint\n"
172 " -o specify output file\n"
173 " -l equivalent to `-O fingerprint'\n"
174 " -L equivalent to `-O public-openssh'\n"
175 " -p equivalent to `-O public'\n"
176 " --old-passphrase file\n"
177 " specify file containing old key passphrase\n"
178 " --new-passphrase file\n"
179 " specify file containing new key passphrase\n"
183 static int move(char *from, char *to)
187 ret = rename(from, to);
190 * This OS may require us to remove the original file first.
193 ret = rename(from, to);
196 perror("puttygen: cannot move new file on to old one");
202 static char *readpassphrase(const char *filename)
207 fp = fopen(filename, "r");
209 fprintf(stderr, "puttygen: cannot open %s: %s\n",
210 filename, strerror(errno));
215 line[strcspn(line, "\r\n")] = '\0';
217 fprintf(stderr, "puttygen: error reading from %s: %s\n",
218 filename, strerror(errno));
219 else /* empty file */
225 int main(int argc, char **argv)
228 Filename *infilename = NULL, *outfilename = NULL;
229 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
230 char *outfile = NULL, *outfiletmp = NULL;
231 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
232 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
234 char *comment = NULL, *origcomment = NULL;
235 int change_passphrase = FALSE;
236 int errs = FALSE, nogo = FALSE;
237 int intype = SSH_KEYTYPE_UNOPENABLE;
239 struct ssh2_userkey *ssh2key = NULL;
240 struct RSAKey *ssh1key = NULL;
241 unsigned char *ssh2blob = NULL;
242 char *ssh2alg = NULL;
243 const struct ssh_signkey *ssh2algf = NULL;
245 char *old_passphrase = NULL, *new_passphrase = NULL;
247 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
249 /* ------------------------------------------------------------------
250 * Parse the command line to figure out what we've been asked to do.
254 * If run with no arguments at all, print the usage message and
263 * Parse command line arguments.
280 opt = p++; /* opt will have _one_ leading - */
281 while (*p && *p != '=')
282 p++; /* find end of option */
289 if (!strcmp(opt, "-help")) {
292 fprintf(stderr, "puttygen: option `-%s'"
293 " expects no argument\n", opt);
298 } else if (!strcmp(opt, "-version")) {
301 fprintf(stderr, "puttygen: option `-%s'"
302 " expects no argument\n", opt);
307 } else if (!strcmp(opt, "-pgpfp")) {
310 fprintf(stderr, "puttygen: option `-%s'"
311 " expects no argument\n", opt);
313 /* support --pgpfp for consistency */
317 } else if (!strcmp(opt, "-old-passphrase")) {
318 if (!val && argc > 1)
319 --argc, val = *++argv;
322 fprintf(stderr, "puttygen: option `-%s'"
323 " expects an argument\n", opt);
325 old_passphrase = readpassphrase(val);
329 } else if (!strcmp(opt, "-new-passphrase")) {
330 if (!val && argc > 1)
331 --argc, val = *++argv;
334 fprintf(stderr, "puttygen: option `-%s'"
335 " expects an argument\n", opt);
337 new_passphrase = readpassphrase(val);
344 "puttygen: no such option `-%s'\n", opt);
357 * Option requiring no parameter.
369 change_passphrase = TRUE;
381 progressfn = no_progress;
391 * Option requiring parameter.
397 fprintf(stderr, "puttygen: option `-%c' expects a"
402 * Now c is the option and p is the parameter.
406 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
407 keytype = RSA2, sshver = 2;
408 else if (!strcmp(p, "rsa1"))
409 keytype = RSA1, sshver = 1;
410 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
411 keytype = DSA, sshver = 2;
412 else if (!strcmp(p, "ecdsa"))
413 keytype = ECDSA, sshver = 2;
414 else if (!strcmp(p, "ed25519"))
415 keytype = ED25519, sshver = 2;
418 "puttygen: unknown key type `%s'\n", p);
429 if (!strcmp(p, "public"))
431 else if (!strcmp(p, "public-openssh"))
433 else if (!strcmp(p, "private"))
435 else if (!strcmp(p, "fingerprint"))
437 else if (!strcmp(p, "private-openssh"))
438 outtype = OPENSSH_AUTO, sshver = 2;
439 else if (!strcmp(p, "private-openssh-new"))
440 outtype = OPENSSH_NEW, sshver = 2;
441 else if (!strcmp(p, "private-sshcom"))
442 outtype = SSHCOM, sshver = 2;
445 "puttygen: unknown output type `%s'\n", p);
453 p = NULL; /* prevent continued processing */
457 * Unrecognised option.
460 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
466 * A non-option argument.
472 fprintf(stderr, "puttygen: cannot handle more than one"
480 * No explicit key size was specified. Default varies
481 * depending on key type.
496 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
497 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
501 if (keytype == ED25519 && (bits != 256)) {
502 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
513 * If run with at least one argument _but_ not the required
514 * ones, print the usage message and return failure.
516 if (!infile && keytype == NOKEYGEN) {
521 /* ------------------------------------------------------------------
522 * Figure out further details of exactly what we're going to do.
526 * Bomb out if we've been asked to both load and generate a
529 if (keytype != NOKEYGEN && infile) {
530 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
535 * We must save the private part when generating a new key.
537 if (keytype != NOKEYGEN &&
538 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
539 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
540 fprintf(stderr, "puttygen: this would generate a new key but "
541 "discard the private part\n");
546 * Analyse the type of the input file, in case this affects our
550 infilename = filename_from_str(infile);
552 intype = key_type(infilename);
555 case SSH_KEYTYPE_UNOPENABLE:
556 case SSH_KEYTYPE_UNKNOWN:
557 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
558 infile, key_type_to_str(intype));
561 case SSH_KEYTYPE_SSH1:
562 case SSH_KEYTYPE_SSH1_PUBLIC:
564 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
571 case SSH_KEYTYPE_SSH2:
572 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
573 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
574 case SSH_KEYTYPE_OPENSSH_PEM:
575 case SSH_KEYTYPE_OPENSSH_NEW:
576 case SSH_KEYTYPE_SSHCOM:
578 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
585 case SSH_KEYTYPE_OPENSSH_AUTO:
587 assert(0 && "Should never see these types on an input file");
592 * Determine the default output file, if none is provided.
594 * This will usually be equal to stdout, except that if the
595 * input and output file formats are the same then the default
596 * output is to overwrite the input.
598 * Also in this code, we bomb out if the input and output file
599 * formats are the same and no other action is performed.
601 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
602 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
603 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
604 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
605 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
608 outfiletmp = dupcat(outfile, ".tmp", NULL);
611 if (!change_passphrase && !comment) {
612 fprintf(stderr, "puttygen: this command would perform no useful"
619 * Bomb out rather than automatically choosing to write
620 * a private key file to stdout.
622 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
623 outtype == OPENSSH_NEW || outtype == SSHCOM) {
624 fprintf(stderr, "puttygen: need to specify an output file\n");
631 * Figure out whether we need to load the encrypted part of the
632 * key. This will be the case if either (a) we need to write
633 * out a private key format, or (b) the entire input key file
636 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
637 outtype == OPENSSH_NEW || outtype == SSHCOM ||
638 intype == SSH_KEYTYPE_OPENSSH_PEM ||
639 intype == SSH_KEYTYPE_OPENSSH_NEW ||
640 intype == SSH_KEYTYPE_SSHCOM)
641 load_encrypted = TRUE;
643 load_encrypted = FALSE;
645 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
646 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
647 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
648 fprintf(stderr, "puttygen: cannot perform this action on a "
649 "public-key-only input file\n");
653 /* ------------------------------------------------------------------
654 * Now we're ready to actually do some stuff.
658 * Either load or generate a key.
660 if (keytype != NOKEYGEN) {
662 char default_comment[80];
664 struct progress prog;
671 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
672 else if (keytype == ECDSA)
673 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
674 else if (keytype == ED25519)
675 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
677 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
680 entropy = get_random_data(bits / 8);
682 fprintf(stderr, "puttygen: failed to collect entropy, "
683 "could not generate key\n");
686 random_add_heavynoise(entropy, bits / 8);
687 smemclr(entropy, bits/8);
690 if (keytype == DSA) {
691 struct dss_key *dsskey = snew(struct dss_key);
692 dsa_generate(dsskey, bits, progressfn, &prog);
693 ssh2key = snew(struct ssh2_userkey);
694 ssh2key->data = dsskey;
695 ssh2key->alg = &ssh_dss;
697 } else if (keytype == ECDSA) {
698 struct ec_key *ec = snew(struct ec_key);
699 ec_generate(ec, bits, progressfn, &prog);
700 ssh2key = snew(struct ssh2_userkey);
702 ssh2key->alg = ec->signalg;
704 } else if (keytype == ED25519) {
705 struct ec_key *ec = snew(struct ec_key);
706 ec_edgenerate(ec, bits, progressfn, &prog);
707 ssh2key = snew(struct ssh2_userkey);
709 ssh2key->alg = &ssh_ecdsa_ed25519;
712 struct RSAKey *rsakey = snew(struct RSAKey);
713 rsa_generate(rsakey, bits, progressfn, &prog);
714 rsakey->comment = NULL;
715 if (keytype == RSA1) {
718 ssh2key = snew(struct ssh2_userkey);
719 ssh2key->data = rsakey;
720 ssh2key->alg = &ssh_rsa;
723 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
726 ssh2key->comment = dupstr(default_comment);
728 ssh1key->comment = dupstr(default_comment);
731 const char *error = NULL;
734 assert(infile != NULL);
737 * Find out whether the input key is encrypted.
739 if (intype == SSH_KEYTYPE_SSH1)
740 encrypted = rsakey_encrypted(infilename, &origcomment);
741 else if (intype == SSH_KEYTYPE_SSH2)
742 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
744 encrypted = import_encrypted(infilename, intype, &origcomment);
747 * If so, ask for a passphrase.
749 if (encrypted && load_encrypted) {
750 if (!old_passphrase) {
751 prompts_t *p = new_prompts(NULL);
753 p->to_server = FALSE;
754 p->name = dupstr("SSH key passphrase");
755 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
756 ret = console_get_userpass_input(p, NULL, 0);
760 perror("puttygen: unable to read passphrase");
763 old_passphrase = dupstr(p->prompts[0]->result);
768 old_passphrase = NULL;
774 case SSH_KEYTYPE_SSH1:
775 case SSH_KEYTYPE_SSH1_PUBLIC:
776 ssh1key = snew(struct RSAKey);
777 if (!load_encrypted) {
782 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
783 &origcomment, &error);
784 blob = (unsigned char *)vblob;
786 n = 4; /* skip modulus bits */
788 l = ssh1_read_bignum(blob + n, bloblen - n,
791 error = "SSH-1 public key blob was too short";
794 l = ssh1_read_bignum(blob + n, bloblen - n,
797 error = "SSH-1 public key blob was too short";
801 ssh1key->comment = dupstr(origcomment);
802 ssh1key->private_exponent = NULL;
805 ssh1key->iqmp = NULL;
807 ret = loadrsakey(infilename, ssh1key, old_passphrase, &error);
812 error = "unknown error";
815 case SSH_KEYTYPE_SSH2:
816 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
817 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
818 if (!load_encrypted) {
819 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
820 &ssh2bloblen, &origcomment,
823 ssh2algf = find_pubkey_alg(ssh2alg);
825 bits = ssh2algf->pubkey_bits(ssh2algf,
826 ssh2blob, ssh2bloblen);
832 ssh2key = ssh2_load_userkey(infilename, old_passphrase,
835 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
838 if (ssh2key == SSH2_WRONG_PASSPHRASE)
839 error = "wrong passphrase";
841 error = "unknown error";
845 case SSH_KEYTYPE_OPENSSH_PEM:
846 case SSH_KEYTYPE_OPENSSH_NEW:
847 case SSH_KEYTYPE_SSHCOM:
848 ssh2key = import_ssh2(infilename, intype, old_passphrase, &error);
850 if (ssh2key != SSH2_WRONG_PASSPHRASE)
853 error = "wrong passphrase";
855 error = "unknown error";
863 fprintf(stderr, "puttygen: error loading `%s': %s\n",
870 * Change the comment if asked to.
875 sfree(ssh1key->comment);
876 ssh1key->comment = dupstr(comment);
879 sfree(ssh2key->comment);
880 ssh2key->comment = dupstr(comment);
885 * Unless we're changing the passphrase, the old one (if any) is a
886 * reasonable default.
888 if (!change_passphrase && old_passphrase && !new_passphrase)
889 new_passphrase = dupstr(old_passphrase);
892 * Prompt for a new passphrase if we have been asked to, or if
893 * we have just generated a key.
895 if (!new_passphrase && (change_passphrase || keytype != NOKEYGEN)) {
896 prompts_t *p = new_prompts(NULL);
899 p->to_server = FALSE;
900 p->name = dupstr("New SSH key passphrase");
901 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
902 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
903 ret = console_get_userpass_input(p, NULL, 0);
907 perror("puttygen: unable to read new passphrase");
910 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
912 fprintf(stderr, "puttygen: passphrases do not match\n");
915 new_passphrase = dupstr(p->prompts[0]->result);
919 if (new_passphrase && !*new_passphrase) {
920 sfree(new_passphrase);
921 new_passphrase = NULL;
927 * (In the case where outfile and outfiletmp are both NULL,
928 * there is no semantic reason to initialise outfilename at
929 * all; but we have to write _something_ to it or some compiler
930 * will probably complain that it might be used uninitialised.)
933 outfilename = filename_from_str(outfiletmp);
935 outfilename = filename_from_str(outfile ? outfile : "");
938 int ret, real_outtype;
943 ret = saversakey(outfilename, ssh1key, new_passphrase);
945 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
950 ret = ssh2_save_userkey(outfilename, ssh2key, new_passphrase);
952 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
957 if (!move(outfiletmp, outfile))
958 return 1; /* rename failed */
968 fp = f_open(outfilename, "w", FALSE);
973 ssh1_write_pubkey(fp, ssh1key);
977 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
981 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
982 ssh2blob, ssh2bloblen,
984 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
985 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
1000 fingerprint = snewn(128, char);
1001 rsa_fingerprint(fingerprint, 128, ssh1key);
1004 fingerprint = ssh2_fingerprint(ssh2key->alg,
1008 fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
1013 fp = f_open(outfilename, "w", FALSE);
1016 fprintf(fp, "%s\n", fingerprint);
1027 assert(sshver == 2);
1029 random_ref(); /* both foreign key types require randomness,
1030 * for IV or padding */
1033 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1036 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1039 real_outtype = SSH_KEYTYPE_SSHCOM;
1042 assert(0 && "control flow goof");
1044 ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
1046 fprintf(stderr, "puttygen: unable to export key\n");
1050 if (!move(outfiletmp, outfile))
1051 return 1; /* rename failed */
1056 if (old_passphrase) {
1057 smemclr(old_passphrase, strlen(old_passphrase));
1058 sfree(old_passphrase);
1060 if (new_passphrase) {
1061 smemclr(new_passphrase, strlen(new_passphrase));
1062 sfree(new_passphrase);
1066 freersakey(ssh1key);
1068 ssh2key->alg->freekey(ssh2key->data);
1083 void setup_passphrases(char *first, ...)
1090 prompts[nprompts++] = first;
1091 va_start(ap, first);
1092 while ((next = va_arg(ap, char *)) != NULL) {
1093 assert(nprompts < lenof(prompts));
1094 prompts[nprompts++] = next;
1100 void test(int retval, ...)
1107 va_start(ap, retval);
1108 while (va_arg(ap, char *) != NULL)
1112 argv = snewn(argc+1, char *);
1113 va_start(ap, retval);
1114 for (i = 0; i <= argc; i++)
1115 argv[i] = va_arg(ap, char *);
1119 ret = cmdgen_main(argc, argv);
1121 if (ret != retval) {
1122 printf("FAILED retval (exp %d got %d):", retval, ret);
1123 for (i = 0; i < argc; i++)
1124 printf(" %s", argv[i]);
1127 } else if (promptsgot != nprompts) {
1128 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1129 for (i = 0; i < argc; i++)
1130 printf(" %s", argv[i]);
1138 void filecmp(char *file1, char *file2, char *fmt, ...)
1141 * Ideally I should do file comparison myself, to maximise the
1142 * portability of this test suite once this application begins
1143 * running on non-Unix platforms. For the moment, though,
1144 * calling Unix diff is perfectly adequate.
1149 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1156 printf("FAILED diff (ret=%d): ", ret);
1169 char *cleanup_fp(char *s)
1173 if (!strncmp(s, "ssh-", 4)) {
1174 s += strcspn(s, " \n\t");
1175 s += strspn(s, " \n\t");
1179 s += strcspn(s, " \n\t");
1180 s += strspn(s, " \n\t");
1181 s += strcspn(s, " \n\t");
1183 return dupprintf("%.*s", s - p, p);
1186 char *get_fp(char *filename)
1189 char buf[256], *ret;
1191 fp = fopen(filename, "r");
1194 ret = fgets(buf, sizeof(buf), fp);
1198 return cleanup_fp(buf);
1201 void check_fp(char *filename, char *fp, char *fmt, ...)
1208 newfp = get_fp(filename);
1210 if (!strcmp(fp, newfp)) {
1215 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1229 int main(int argc, char **argv)
1232 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1235 * Even when this thing is compiled for automatic test mode,
1236 * it's helpful to be able to invoke it with command-line
1237 * options for _manual_ tests.
1240 return cmdgen_main(argc, argv);
1244 for (i = 0; i < lenof(keytypes); i++) {
1245 char filename[128], osfilename[128], scfilename[128];
1246 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1249 sprintf(filename, "test-%s.ppk", keytypes[i]);
1250 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1251 sprintf(osfilename, "test-%s.os", keytypes[i]);
1252 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1253 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1254 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1257 * Create an encrypted key.
1259 setup_passphrases("sponge", "sponge", NULL);
1260 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1263 * List the public key in OpenSSH format.
1265 setup_passphrases(NULL);
1266 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1270 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1271 pubfilename, tmpfilename1);
1272 if (system(cmdbuf) ||
1273 (fp = get_fp(tmpfilename1)) == NULL) {
1274 printf("UNABLE to test fingerprint matching against OpenSSH");
1279 * List the public key in IETF/ssh.com format.
1281 setup_passphrases(NULL);
1282 test(0, "puttygen", "-p", filename, NULL);
1285 * List the fingerprint of the key.
1287 setup_passphrases(NULL);
1288 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1291 * If we can't test fingerprints against OpenSSH, we
1292 * can at the very least test equality of all the
1293 * fingerprints we generate of this key throughout
1296 fp = get_fp(tmpfilename1);
1298 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1302 * Change the comment of the key; this _does_ require a
1303 * passphrase owing to the tamperproofing.
1305 * NOTE: In SSH-1, this only requires a passphrase because
1306 * of inadequacies of the loading and saving mechanisms. In
1307 * _principle_, it should be perfectly possible to modify
1308 * the comment on an SSH-1 key without requiring a
1309 * passphrase; the only reason I can't do it is because my
1310 * loading and saving mechanisms don't include a method of
1311 * loading all the key data without also trying to decrypt
1312 * the private section.
1314 * I don't consider this to be a problem worth solving,
1315 * because (a) to fix it would probably end up bloating
1316 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1317 * it shouldn't be highly significant. If it seriously
1318 * bothers anyone then perhaps I _might_ be persuadable.
1320 setup_passphrases("sponge", NULL);
1321 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1324 * Change the passphrase to nothing.
1326 setup_passphrases("sponge", "", "", NULL);
1327 test(0, "puttygen", "-P", filename, NULL);
1330 * Change the comment of the key again; this time we expect no
1331 * passphrase to be required.
1333 setup_passphrases(NULL);
1334 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1337 * Export the private key into OpenSSH format; no passphrase
1338 * should be required since the key is currently unencrypted.
1339 * For RSA1 keys, this should give an error.
1341 setup_passphrases(NULL);
1342 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1347 * List the fingerprint of the OpenSSH-formatted key.
1349 setup_passphrases(NULL);
1350 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1351 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1354 * List the public half of the OpenSSH-formatted key in
1357 setup_passphrases(NULL);
1358 test(0, "puttygen", "-L", osfilename, NULL);
1361 * List the public half of the OpenSSH-formatted key in
1362 * IETF/ssh.com format.
1364 setup_passphrases(NULL);
1365 test(0, "puttygen", "-p", osfilename, NULL);
1369 * Export the private key into ssh.com format; no passphrase
1370 * should be required since the key is currently unencrypted.
1371 * For RSA1 keys, this should give an error.
1373 setup_passphrases(NULL);
1374 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1379 * List the fingerprint of the ssh.com-formatted key.
1381 setup_passphrases(NULL);
1382 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1383 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1386 * List the public half of the ssh.com-formatted key in
1389 setup_passphrases(NULL);
1390 test(0, "puttygen", "-L", scfilename, NULL);
1393 * List the public half of the ssh.com-formatted key in
1394 * IETF/ssh.com format.
1396 setup_passphrases(NULL);
1397 test(0, "puttygen", "-p", scfilename, NULL);
1402 * Convert from OpenSSH into ssh.com.
1404 setup_passphrases(NULL);
1405 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1406 "-O", "private-sshcom", NULL);
1409 * Convert from ssh.com back into a PuTTY key,
1410 * supplying the same comment as we had before we
1411 * started to ensure the comparison works.
1413 setup_passphrases(NULL);
1414 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1415 "-o", tmpfilename2, NULL);
1418 * See if the PuTTY key thus generated is the same as
1421 filecmp(filename, tmpfilename2,
1422 "p->o->s->p clear %s", keytypes[i]);
1425 * Convert from ssh.com to OpenSSH.
1427 setup_passphrases(NULL);
1428 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1429 "-O", "private-openssh", NULL);
1432 * Convert from OpenSSH back into a PuTTY key,
1433 * supplying the same comment as we had before we
1434 * started to ensure the comparison works.
1436 setup_passphrases(NULL);
1437 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1438 "-o", tmpfilename2, NULL);
1441 * See if the PuTTY key thus generated is the same as
1444 filecmp(filename, tmpfilename2,
1445 "p->s->o->p clear %s", keytypes[i]);
1448 * Finally, do a round-trip conversion between PuTTY
1449 * and ssh.com without involving OpenSSH, to test that
1450 * the key comment is preserved in that case.
1452 setup_passphrases(NULL);
1453 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1455 setup_passphrases(NULL);
1456 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1457 filecmp(filename, tmpfilename2,
1458 "p->s->p clear %s", keytypes[i]);
1462 * Check that mismatched passphrases cause an error.
1464 setup_passphrases("sponge2", "sponge3", NULL);
1465 test(1, "puttygen", "-P", filename, NULL);
1468 * Put a passphrase back on.
1470 setup_passphrases("sponge2", "sponge2", NULL);
1471 test(0, "puttygen", "-P", filename, NULL);
1474 * Export the private key into OpenSSH format, this time
1475 * while encrypted. For RSA1 keys, this should give an
1479 setup_passphrases(NULL); /* error, hence no passphrase read */
1481 setup_passphrases("sponge2", NULL);
1482 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1487 * List the fingerprint of the OpenSSH-formatted key.
1489 setup_passphrases("sponge2", NULL);
1490 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1491 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1494 * List the public half of the OpenSSH-formatted key in
1497 setup_passphrases("sponge2", NULL);
1498 test(0, "puttygen", "-L", osfilename, NULL);
1501 * List the public half of the OpenSSH-formatted key in
1502 * IETF/ssh.com format.
1504 setup_passphrases("sponge2", NULL);
1505 test(0, "puttygen", "-p", osfilename, NULL);
1509 * Export the private key into ssh.com format, this time
1510 * while encrypted. For RSA1 keys, this should give an
1514 setup_passphrases(NULL); /* error, hence no passphrase read */
1516 setup_passphrases("sponge2", NULL);
1517 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1522 * List the fingerprint of the ssh.com-formatted key.
1524 setup_passphrases("sponge2", NULL);
1525 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1526 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1529 * List the public half of the ssh.com-formatted key in
1532 setup_passphrases("sponge2", NULL);
1533 test(0, "puttygen", "-L", scfilename, NULL);
1536 * List the public half of the ssh.com-formatted key in
1537 * IETF/ssh.com format.
1539 setup_passphrases("sponge2", NULL);
1540 test(0, "puttygen", "-p", scfilename, NULL);
1545 * Convert from OpenSSH into ssh.com.
1547 setup_passphrases("sponge2", NULL);
1548 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1549 "-O", "private-sshcom", NULL);
1552 * Convert from ssh.com back into a PuTTY key,
1553 * supplying the same comment as we had before we
1554 * started to ensure the comparison works.
1556 setup_passphrases("sponge2", NULL);
1557 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1558 "-o", tmpfilename2, NULL);
1561 * See if the PuTTY key thus generated is the same as
1564 filecmp(filename, tmpfilename2,
1565 "p->o->s->p encrypted %s", keytypes[i]);
1568 * Convert from ssh.com to OpenSSH.
1570 setup_passphrases("sponge2", NULL);
1571 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1572 "-O", "private-openssh", NULL);
1575 * Convert from OpenSSH back into a PuTTY key,
1576 * supplying the same comment as we had before we
1577 * started to ensure the comparison works.
1579 setup_passphrases("sponge2", NULL);
1580 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1581 "-o", tmpfilename2, NULL);
1584 * See if the PuTTY key thus generated is the same as
1587 filecmp(filename, tmpfilename2,
1588 "p->s->o->p encrypted %s", keytypes[i]);
1591 * Finally, do a round-trip conversion between PuTTY
1592 * and ssh.com without involving OpenSSH, to test that
1593 * the key comment is preserved in that case.
1595 setup_passphrases("sponge2", NULL);
1596 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1598 setup_passphrases("sponge2", NULL);
1599 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1600 filecmp(filename, tmpfilename2,
1601 "p->s->p encrypted %s", keytypes[i]);
1605 * Load with the wrong passphrase.
1607 setup_passphrases("sponge8", NULL);
1608 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1611 * Load a totally bogus file.
1613 setup_passphrases(NULL);
1614 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1616 printf("%d passes, %d fails\n", passes, fails);