2 * cmdgen.c - command-line form of PuTTYgen
\r
5 #define PUTTY_DO_GLOBALS
\r
19 * This section overrides some definitions below for test purposes.
\r
20 * When compiled with -DTEST_CMDGEN:
\r
22 * - Calls to get_random_data() are replaced with the diagnostic
\r
23 * function below (I #define the name so that I can still link
\r
24 * with the original set of modules without symbol clash), in
\r
25 * order to avoid depleting the test system's /dev/random
\r
28 * - Calls to console_get_userpass_input() are replaced with the
\r
29 * diagnostic function below, so that I can run tests in an
\r
30 * automated manner and provide their interactive passphrase
\r
33 * - main() is renamed to cmdgen_main(); at the bottom of the file
\r
34 * I define another main() which calls the former repeatedly to
\r
37 #define get_random_data get_random_data_diagnostic
\r
38 char *get_random_data(int len)
\r
40 char *buf = snewn(len, char);
\r
41 memset(buf, 'x', len);
\r
44 #define console_get_userpass_input console_get_userpass_input_diagnostic
\r
45 int nprompts, promptsgot;
\r
46 const char *prompts[3];
\r
47 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
\r
51 for (i = 0; i < p->n_prompts; i++) {
\r
52 if (promptsgot < nprompts) {
\r
53 assert(strlen(prompts[promptsgot]) < p->prompts[i]->result_len);
\r
54 strcpy(p->prompts[i]->result, prompts[promptsgot++]);
\r
56 promptsgot++; /* track number of requests anyway */
\r
62 #define main cmdgen_main
\r
69 static void progress_update(void *param, int action, int phase, int iprogress)
\r
71 struct progress *p = (struct progress *)param;
\r
72 if (action != PROGFN_PROGRESS)
\r
74 if (phase > p->phase) {
\r
76 fputc('\n', stderr);
\r
79 p->current = iprogress - 1;
\r
81 p->current = iprogress;
\r
83 while (p->current < iprogress) {
\r
90 static void no_progress(void *param, int action, int phase, int iprogress)
\r
94 void modalfatalbox(char *p, ...)
\r
97 fprintf(stderr, "FATAL ERROR: ");
\r
99 vfprintf(stderr, p, ap);
\r
101 fputc('\n', stderr);
\r
106 * Stubs to let everything else link sensibly.
\r
108 void log_eventlog(void *handle, const char *event)
\r
111 char *x_get_default(const char *key)
\r
115 void sk_cleanup(void)
\r
119 void showversion(void)
\r
121 char *verstr = dupstr(ver);
\r
122 verstr[0] = tolower((unsigned char)verstr[0]);
\r
123 printf("PuTTYgen %s\n", verstr);
\r
127 void usage(int standalone)
\r
130 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
\r
131 " [ -C comment ] [ -P ] [ -q ]\n"
\r
132 " [ -o output-keyfile ] [ -O type | -l | -L"
\r
136 "Use \"puttygen --help\" for more detail.\n");
\r
142 * Help message is an extended version of the usage message. So
\r
143 * start with that, plus a version heading.
\r
148 " -t specify key type when generating (rsa, dsa, rsa1)\n"
\r
149 " -b specify number of bits when generating key\n"
\r
150 " -C change or specify key comment\n"
\r
151 " -P change key passphrase\n"
\r
152 " -q quiet: do not display progress bar\n"
\r
153 " -O specify output type:\n"
\r
154 " private output PuTTY private key format\n"
\r
155 " private-openssh export OpenSSH private key\n"
\r
156 " private-sshcom export ssh.com private key\n"
\r
157 " public standard / ssh.com public key\n"
\r
158 " public-openssh OpenSSH public key\n"
\r
159 " fingerprint output the key fingerprint\n"
\r
160 " -o specify output file\n"
\r
161 " -l equivalent to `-O fingerprint'\n"
\r
162 " -L equivalent to `-O public-openssh'\n"
\r
163 " -p equivalent to `-O public'\n"
\r
167 static int save_ssh2_pubkey(char *filename, char *comment,
\r
168 void *v_pub_blob, int pub_len)
\r
170 unsigned char *pub_blob = (unsigned char *)v_pub_blob;
\r
176 fp = fopen(filename, "wb");
\r
182 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
\r
185 fprintf(fp, "Comment: \"");
\r
186 for (p = comment; *p; p++) {
\r
187 if (*p == '\\' || *p == '\"')
\r
191 fprintf(fp, "\"\n");
\r
196 while (i < pub_len) {
\r
198 int n = (pub_len - i < 3 ? pub_len - i : 3);
\r
199 base64_encode_atom(pub_blob + i, n, buf);
\r
203 if (++column >= 16) {
\r
211 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
\r
217 static int move(char *from, char *to)
\r
221 ret = rename(from, to);
\r
224 * This OS may require us to remove the original file first.
\r
227 ret = rename(from, to);
\r
230 perror("puttygen: cannot move new file on to old one");
\r
236 static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
\r
239 unsigned char digest[16];
\r
240 struct MD5Context md5c;
\r
244 MD5Update(&md5c, blob, bloblen);
\r
245 MD5Final(digest, &md5c);
\r
247 sprintf(buffer, "%s ", alg);
\r
249 sprintf(buffer + strlen(buffer), "%d ", bits);
\r
250 for (i = 0; i < 16; i++)
\r
251 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
\r
254 return dupstr(buffer);
\r
257 int main(int argc, char **argv)
\r
259 char *infile = NULL;
\r
260 Filename infilename;
\r
261 enum { NOKEYGEN, RSA1, RSA2, DSA } keytype = NOKEYGEN;
\r
262 char *outfile = NULL, *outfiletmp = NULL;
\r
263 Filename outfilename;
\r
264 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH, SSHCOM } outtype = PRIVATE;
\r
266 char *comment = NULL, *origcomment = NULL;
\r
267 int change_passphrase = FALSE;
\r
268 int errs = FALSE, nogo = FALSE;
\r
269 int intype = SSH_KEYTYPE_UNOPENABLE;
\r
271 struct ssh2_userkey *ssh2key = NULL;
\r
272 struct RSAKey *ssh1key = NULL;
\r
273 unsigned char *ssh2blob = NULL;
\r
274 char *ssh2alg = NULL;
\r
275 const struct ssh_signkey *ssh2algf = NULL;
\r
277 char *passphrase = NULL;
\r
278 int load_encrypted;
\r
279 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
\r
281 /* ------------------------------------------------------------------
\r
282 * Parse the command line to figure out what we've been asked to do.
\r
286 * If run with no arguments at all, print the usage message and
\r
295 * Parse command line arguments.
\r
303 while (p && *++p) {
\r
312 opt = p++; /* opt will have _one_ leading - */
\r
313 while (*p && *p != '=')
\r
314 p++; /* find end of option */
\r
321 if (!strcmp(opt, "-help")) {
\r
324 fprintf(stderr, "puttygen: option `-%s'"
\r
325 " expects no argument\n", opt);
\r
330 } else if (!strcmp(opt, "-version")) {
\r
333 fprintf(stderr, "puttygen: option `-%s'"
\r
334 " expects no argument\n", opt);
\r
339 } else if (!strcmp(opt, "-pgpfp")) {
\r
342 fprintf(stderr, "puttygen: option `-%s'"
\r
343 " expects no argument\n", opt);
\r
345 /* support --pgpfp for consistency */
\r
346 pgp_fingerprints();
\r
351 * For long options requiring an argument, add
\r
352 * code along the lines of
\r
354 * else if (!strcmp(opt, "-output")) {
\r
357 * fprintf(stderr, "puttygen: option `-%s'"
\r
358 * " expects an argument\n", opt);
\r
366 "puttygen: no such option `-%s'\n", opt);
\r
379 * Option requiring no parameter.
\r
391 change_passphrase = TRUE;
\r
403 progressfn = no_progress;
\r
413 * Option requiring parameter.
\r
416 if (!*p && argc > 1)
\r
417 --argc, p = *++argv;
\r
419 fprintf(stderr, "puttygen: option `-%c' expects a"
\r
420 " parameter\n", c);
\r
424 * Now c is the option and p is the parameter.
\r
428 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
\r
429 keytype = RSA2, sshver = 2;
\r
430 else if (!strcmp(p, "rsa1"))
\r
431 keytype = RSA1, sshver = 1;
\r
432 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
\r
433 keytype = DSA, sshver = 2;
\r
436 "puttygen: unknown key type `%s'\n", p);
\r
447 if (!strcmp(p, "public"))
\r
449 else if (!strcmp(p, "public-openssh"))
\r
451 else if (!strcmp(p, "private"))
\r
453 else if (!strcmp(p, "fingerprint"))
\r
455 else if (!strcmp(p, "private-openssh"))
\r
456 outtype = OPENSSH, sshver = 2;
\r
457 else if (!strcmp(p, "private-sshcom"))
\r
458 outtype = SSHCOM, sshver = 2;
\r
461 "puttygen: unknown output type `%s'\n", p);
\r
469 p = NULL; /* prevent continued processing */
\r
473 * Unrecognised option.
\r
476 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
\r
482 * A non-option argument.
\r
488 fprintf(stderr, "puttygen: cannot handle more than one"
\r
501 * If run with at least one argument _but_ not the required
\r
502 * ones, print the usage message and return failure.
\r
504 if (!infile && keytype == NOKEYGEN) {
\r
509 /* ------------------------------------------------------------------
\r
510 * Figure out further details of exactly what we're going to do.
\r
514 * Bomb out if we've been asked to both load and generate a
\r
517 if (keytype != NOKEYGEN && infile) {
\r
518 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
\r
523 * We must save the private part when generating a new key.
\r
525 if (keytype != NOKEYGEN &&
\r
526 (outtype != PRIVATE && outtype != OPENSSH && outtype != SSHCOM)) {
\r
527 fprintf(stderr, "puttygen: this would generate a new key but "
\r
528 "discard the private part\n");
\r
533 * Analyse the type of the input file, in case this affects our
\r
534 * course of action.
\r
537 infilename = filename_from_str(infile);
\r
539 intype = key_type(&infilename);
\r
543 * It would be nice here to be able to load _public_
\r
544 * key files, in any of a number of forms, and (a)
\r
545 * convert them to other public key types, (b) print
\r
546 * out their fingerprints. Or, I suppose, for real
\r
547 * orthogonality, (c) change their comment!
\r
549 * In fact this opens some interesting possibilities.
\r
550 * Suppose ssh2_userkey_loadpub() were able to load
\r
551 * public key files as well as extracting the public
\r
552 * key from private ones. And suppose I did the thing
\r
553 * I've been wanting to do, where specifying a
\r
554 * particular private key file for authentication
\r
555 * causes any _other_ key in the agent to be discarded.
\r
556 * Then, if you had an agent forwarded to the machine
\r
557 * you were running Unix PuTTY or Plink on, and you
\r
558 * needed to specify which of the keys in the agent it
\r
559 * should use, you could do that by supplying a
\r
560 * _public_ key file, thus not needing to trust even
\r
561 * your encrypted private key file to the network. Ooh!
\r
564 case SSH_KEYTYPE_UNOPENABLE:
\r
565 case SSH_KEYTYPE_UNKNOWN:
\r
566 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
\r
567 infile, key_type_to_str(intype));
\r
570 case SSH_KEYTYPE_SSH1:
\r
572 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
\r
573 " not supported\n");
\r
579 case SSH_KEYTYPE_SSH2:
\r
580 case SSH_KEYTYPE_OPENSSH:
\r
581 case SSH_KEYTYPE_SSHCOM:
\r
583 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
\r
584 " not supported\n");
\r
593 * Determine the default output file, if none is provided.
\r
595 * This will usually be equal to stdout, except that if the
\r
596 * input and output file formats are the same then the default
\r
597 * output is to overwrite the input.
\r
599 * Also in this code, we bomb out if the input and output file
\r
600 * formats are the same and no other action is performed.
\r
602 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
\r
603 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
\r
604 (intype == SSH_KEYTYPE_OPENSSH && outtype == OPENSSH) ||
\r
605 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
\r
608 outfiletmp = dupcat(outfile, ".tmp", NULL);
\r
611 if (!change_passphrase && !comment) {
\r
612 fprintf(stderr, "puttygen: this command would perform no useful"
\r
619 * Bomb out rather than automatically choosing to write
\r
620 * a private key file to stdout.
\r
622 if (outtype==PRIVATE || outtype==OPENSSH || outtype==SSHCOM) {
\r
623 fprintf(stderr, "puttygen: need to specify an output file\n");
\r
630 * Figure out whether we need to load the encrypted part of the
\r
631 * key. This will be the case if either (a) we need to write
\r
632 * out a private key format, or (b) the entire input key file
\r
635 if (outtype == PRIVATE || outtype == OPENSSH || outtype == SSHCOM ||
\r
636 intype == SSH_KEYTYPE_OPENSSH || intype == SSH_KEYTYPE_SSHCOM)
\r
637 load_encrypted = TRUE;
\r
639 load_encrypted = FALSE;
\r
641 /* ------------------------------------------------------------------
\r
642 * Now we're ready to actually do some stuff.
\r
646 * Either load or generate a key.
\r
648 if (keytype != NOKEYGEN) {
\r
650 char default_comment[80];
\r
652 struct progress prog;
\r
658 if (keytype == DSA)
\r
659 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
\r
661 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
\r
664 entropy = get_random_data(bits / 8);
\r
666 fprintf(stderr, "puttygen: failed to collect entropy, "
\r
667 "could not generate key\n");
\r
670 random_add_heavynoise(entropy, bits / 8);
\r
671 memset(entropy, 0, bits/8);
\r
674 if (keytype == DSA) {
\r
675 struct dss_key *dsskey = snew(struct dss_key);
\r
676 dsa_generate(dsskey, bits, progressfn, &prog);
\r
677 ssh2key = snew(struct ssh2_userkey);
\r
678 ssh2key->data = dsskey;
\r
679 ssh2key->alg = &ssh_dss;
\r
682 struct RSAKey *rsakey = snew(struct RSAKey);
\r
683 rsa_generate(rsakey, bits, progressfn, &prog);
\r
684 rsakey->comment = NULL;
\r
685 if (keytype == RSA1) {
\r
688 ssh2key = snew(struct ssh2_userkey);
\r
689 ssh2key->data = rsakey;
\r
690 ssh2key->alg = &ssh_rsa;
\r
693 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
\r
696 ssh2key->comment = dupstr(default_comment);
\r
698 ssh1key->comment = dupstr(default_comment);
\r
701 const char *error = NULL;
\r
704 assert(infile != NULL);
\r
707 * Find out whether the input key is encrypted.
\r
709 if (intype == SSH_KEYTYPE_SSH1)
\r
710 encrypted = rsakey_encrypted(&infilename, &origcomment);
\r
711 else if (intype == SSH_KEYTYPE_SSH2)
\r
712 encrypted = ssh2_userkey_encrypted(&infilename, &origcomment);
\r
714 encrypted = import_encrypted(&infilename, intype, &origcomment);
\r
717 * If so, ask for a passphrase.
\r
719 if (encrypted && load_encrypted) {
\r
720 prompts_t *p = new_prompts(NULL);
\r
722 p->to_server = FALSE;
\r
723 p->name = dupstr("SSH key passphrase");
\r
724 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE, 512);
\r
725 ret = console_get_userpass_input(p, NULL, 0);
\r
729 perror("puttygen: unable to read passphrase");
\r
732 passphrase = dupstr(p->prompts[0]->result);
\r
742 case SSH_KEYTYPE_SSH1:
\r
743 ssh1key = snew(struct RSAKey);
\r
744 if (!load_encrypted) {
\r
746 unsigned char *blob;
\r
749 ret = rsakey_pubblob(&infilename, &vblob, &bloblen,
\r
750 &origcomment, &error);
\r
751 blob = (unsigned char *)vblob;
\r
753 n = 4; /* skip modulus bits */
\r
755 l = ssh1_read_bignum(blob + n, bloblen - n,
\r
756 &ssh1key->exponent);
\r
758 error = "SSH-1 public key blob was too short";
\r
761 l = ssh1_read_bignum(blob + n, bloblen - n,
\r
762 &ssh1key->modulus);
\r
764 error = "SSH-1 public key blob was too short";
\r
768 ssh1key->comment = dupstr(origcomment);
\r
769 ssh1key->private_exponent = NULL;
\r
771 ret = loadrsakey(&infilename, ssh1key, passphrase, &error);
\r
776 error = "unknown error";
\r
779 case SSH_KEYTYPE_SSH2:
\r
780 if (!load_encrypted) {
\r
781 ssh2blob = ssh2_userkey_loadpub(&infilename, &ssh2alg,
\r
782 &ssh2bloblen, NULL, &error);
\r
783 ssh2algf = find_pubkey_alg(ssh2alg);
\r
785 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
\r
789 ssh2key = ssh2_load_userkey(&infilename, passphrase, &error);
\r
791 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
\r
794 if (ssh2key == SSH2_WRONG_PASSPHRASE)
\r
795 error = "wrong passphrase";
\r
797 error = "unknown error";
\r
801 case SSH_KEYTYPE_OPENSSH:
\r
802 case SSH_KEYTYPE_SSHCOM:
\r
803 ssh2key = import_ssh2(&infilename, intype, passphrase, &error);
\r
805 if (ssh2key != SSH2_WRONG_PASSPHRASE)
\r
808 error = "wrong passphrase";
\r
810 error = "unknown error";
\r
818 fprintf(stderr, "puttygen: error loading `%s': %s\n",
\r
825 * Change the comment if asked to.
\r
830 sfree(ssh1key->comment);
\r
831 ssh1key->comment = dupstr(comment);
\r
834 sfree(ssh2key->comment);
\r
835 ssh2key->comment = dupstr(comment);
\r
840 * Prompt for a new passphrase if we have been asked to, or if
\r
841 * we have just generated a key.
\r
843 if (change_passphrase || keytype != NOKEYGEN) {
\r
844 prompts_t *p = new_prompts(NULL);
\r
847 p->to_server = FALSE;
\r
848 p->name = dupstr("New SSH key passphrase");
\r
849 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE, 512);
\r
850 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE, 512);
\r
851 ret = console_get_userpass_input(p, NULL, 0);
\r
855 perror("puttygen: unable to read new passphrase");
\r
858 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
\r
860 fprintf(stderr, "puttygen: passphrases do not match\n");
\r
864 memset(passphrase, 0, strlen(passphrase));
\r
867 passphrase = dupstr(p->prompts[0]->result);
\r
869 if (!*passphrase) {
\r
879 * (In the case where outfile and outfiletmp are both NULL,
\r
880 * there is no semantic reason to initialise outfilename at
\r
881 * all; but we have to write _something_ to it or some compiler
\r
882 * will probably complain that it might be used uninitialised.)
\r
885 outfilename = filename_from_str(outfiletmp);
\r
887 outfilename = filename_from_str(outfile ? outfile : "");
\r
895 ret = saversakey(&outfilename, ssh1key, passphrase);
\r
897 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
\r
902 ret = ssh2_save_userkey(&outfilename, ssh2key, passphrase);
\r
904 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
\r
909 if (!move(outfiletmp, outfile))
\r
910 return 1; /* rename failed */
\r
923 fp = f_open(outfilename, "w", FALSE);
\r
926 dec1 = bignum_decimal(ssh1key->exponent);
\r
927 dec2 = bignum_decimal(ssh1key->modulus);
\r
928 fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
\r
929 dec1, dec2, ssh1key->comment);
\r
934 } else if (outtype == PUBLIC) {
\r
937 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
\r
940 save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
\r
941 ssh2blob, ssh2bloblen);
\r
942 } else if (outtype == PUBLICO) {
\r
949 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
\r
954 ssh2alg = ssh2key->alg->name;
\r
957 comment = ssh2key->comment;
\r
959 comment = origcomment;
\r
961 buffer = snewn(strlen(ssh2alg) +
\r
962 4 * ((ssh2bloblen+2) / 3) +
\r
963 strlen(comment) + 3, char);
\r
964 strcpy(buffer, ssh2alg);
\r
965 p = buffer + strlen(buffer);
\r
968 while (i < ssh2bloblen) {
\r
969 int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
\r
970 base64_encode_atom(ssh2blob + i, n, p);
\r
976 strcpy(p, comment);
\r
981 fp = f_open(outfilename, "w", FALSE);
\r
984 fprintf(fp, "%s\n", buffer);
\r
999 fingerprint = snewn(128, char);
\r
1000 rsa_fingerprint(fingerprint, 128, ssh1key);
\r
1003 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
\r
1006 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
\r
1011 fp = f_open(outfilename, "w", FALSE);
\r
1014 fprintf(fp, "%s\n", fingerprint);
\r
1018 sfree(fingerprint);
\r
1024 assert(sshver == 2);
\r
1026 ret = export_ssh2(&outfilename, outtype, ssh2key, passphrase);
\r
1028 fprintf(stderr, "puttygen: unable to export key\n");
\r
1032 if (!move(outfiletmp, outfile))
\r
1033 return 1; /* rename failed */
\r
1039 memset(passphrase, 0, strlen(passphrase));
\r
1040 sfree(passphrase);
\r
1044 freersakey(ssh1key);
\r
1046 ssh2key->alg->freekey(ssh2key->data);
\r
1053 #ifdef TEST_CMDGEN
\r
1057 #include <stdarg.h>
\r
1059 int passes, fails;
\r
1061 void setup_passphrases(char *first, ...)
\r
1068 prompts[nprompts++] = first;
\r
1069 va_start(ap, first);
\r
1070 while ((next = va_arg(ap, char *)) != NULL) {
\r
1071 assert(nprompts < lenof(prompts));
\r
1072 prompts[nprompts++] = next;
\r
1078 void test(int retval, ...)
\r
1085 va_start(ap, retval);
\r
1086 while (va_arg(ap, char *) != NULL)
\r
1090 argv = snewn(argc+1, char *);
\r
1091 va_start(ap, retval);
\r
1092 for (i = 0; i <= argc; i++)
\r
1093 argv[i] = va_arg(ap, char *);
\r
1097 ret = cmdgen_main(argc, argv);
\r
1099 if (ret != retval) {
\r
1100 printf("FAILED retval (exp %d got %d):", retval, ret);
\r
1101 for (i = 0; i < argc; i++)
\r
1102 printf(" %s", argv[i]);
\r
1105 } else if (promptsgot != nprompts) {
\r
1106 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
\r
1107 for (i = 0; i < argc; i++)
\r
1108 printf(" %s", argv[i]);
\r
1116 void filecmp(char *file1, char *file2, char *fmt, ...)
\r
1119 * Ideally I should do file comparison myself, to maximise the
\r
1120 * portability of this test suite once this application begins
\r
1121 * running on non-Unix platforms. For the moment, though,
\r
1122 * calling Unix diff is perfectly adequate.
\r
1127 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
\r
1128 ret = system(buf);
\r
1134 printf("FAILED diff (ret=%d): ", ret);
\r
1136 va_start(ap, fmt);
\r
1147 char *cleanup_fp(char *s)
\r
1151 if (!strncmp(s, "ssh-", 4)) {
\r
1152 s += strcspn(s, " \n\t");
\r
1153 s += strspn(s, " \n\t");
\r
1157 s += strcspn(s, " \n\t");
\r
1158 s += strspn(s, " \n\t");
\r
1159 s += strcspn(s, " \n\t");
\r
1161 return dupprintf("%.*s", s - p, p);
\r
1164 char *get_fp(char *filename)
\r
1167 char buf[256], *ret;
\r
1169 fp = fopen(filename, "r");
\r
1172 ret = fgets(buf, sizeof(buf), fp);
\r
1176 return cleanup_fp(buf);
\r
1179 void check_fp(char *filename, char *fp, char *fmt, ...)
\r
1186 newfp = get_fp(filename);
\r
1188 if (!strcmp(fp, newfp)) {
\r
1193 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
\r
1195 va_start(ap, fmt);
\r
1207 int main(int argc, char **argv)
\r
1210 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
\r
1213 * Even when this thing is compiled for automatic test mode,
\r
1214 * it's helpful to be able to invoke it with command-line
\r
1215 * options for _manual_ tests.
\r
1218 return cmdgen_main(argc, argv);
\r
1220 passes = fails = 0;
\r
1222 for (i = 0; i < lenof(keytypes); i++) {
\r
1223 char filename[128], osfilename[128], scfilename[128];
\r
1224 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
\r
1227 sprintf(filename, "test-%s.ppk", keytypes[i]);
\r
1228 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
\r
1229 sprintf(osfilename, "test-%s.os", keytypes[i]);
\r
1230 sprintf(scfilename, "test-%s.sc", keytypes[i]);
\r
1231 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
\r
1232 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
\r
1235 * Create an encrypted key.
\r
1237 setup_passphrases("sponge", "sponge", NULL);
\r
1238 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
\r
1241 * List the public key in OpenSSH format.
\r
1243 setup_passphrases(NULL);
\r
1244 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
\r
1248 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
\r
1249 pubfilename, tmpfilename1);
\r
1250 if (system(cmdbuf) ||
\r
1251 (fp = get_fp(tmpfilename1)) == NULL) {
\r
1252 printf("UNABLE to test fingerprint matching against OpenSSH");
\r
1257 * List the public key in IETF/ssh.com format.
\r
1259 setup_passphrases(NULL);
\r
1260 test(0, "puttygen", "-p", filename, NULL);
\r
1263 * List the fingerprint of the key.
\r
1265 setup_passphrases(NULL);
\r
1266 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
\r
1269 * If we can't test fingerprints against OpenSSH, we
\r
1270 * can at the very least test equality of all the
\r
1271 * fingerprints we generate of this key throughout
\r
1274 fp = get_fp(tmpfilename1);
\r
1276 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
\r
1280 * Change the comment of the key; this _does_ require a
\r
1281 * passphrase owing to the tamperproofing.
\r
1283 * NOTE: In SSH-1, this only requires a passphrase because
\r
1284 * of inadequacies of the loading and saving mechanisms. In
\r
1285 * _principle_, it should be perfectly possible to modify
\r
1286 * the comment on an SSH-1 key without requiring a
\r
1287 * passphrase; the only reason I can't do it is because my
\r
1288 * loading and saving mechanisms don't include a method of
\r
1289 * loading all the key data without also trying to decrypt
\r
1290 * the private section.
\r
1292 * I don't consider this to be a problem worth solving,
\r
1293 * because (a) to fix it would probably end up bloating
\r
1294 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
\r
1295 * it shouldn't be highly significant. If it seriously
\r
1296 * bothers anyone then perhaps I _might_ be persuadable.
\r
1298 setup_passphrases("sponge", NULL);
\r
1299 test(0, "puttygen", "-C", "new-comment", filename, NULL);
\r
1302 * Change the passphrase to nothing.
\r
1304 setup_passphrases("sponge", "", "", NULL);
\r
1305 test(0, "puttygen", "-P", filename, NULL);
\r
1308 * Change the comment of the key again; this time we expect no
\r
1309 * passphrase to be required.
\r
1311 setup_passphrases(NULL);
\r
1312 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
\r
1315 * Export the private key into OpenSSH format; no passphrase
\r
1316 * should be required since the key is currently unencrypted.
\r
1317 * For RSA1 keys, this should give an error.
\r
1319 setup_passphrases(NULL);
\r
1320 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
\r
1325 * List the fingerprint of the OpenSSH-formatted key.
\r
1327 setup_passphrases(NULL);
\r
1328 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
\r
1329 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
\r
1332 * List the public half of the OpenSSH-formatted key in
\r
1335 setup_passphrases(NULL);
\r
1336 test(0, "puttygen", "-L", osfilename, NULL);
\r
1339 * List the public half of the OpenSSH-formatted key in
\r
1340 * IETF/ssh.com format.
\r
1342 setup_passphrases(NULL);
\r
1343 test(0, "puttygen", "-p", osfilename, NULL);
\r
1347 * Export the private key into ssh.com format; no passphrase
\r
1348 * should be required since the key is currently unencrypted.
\r
1349 * For RSA1 keys, this should give an error.
\r
1351 setup_passphrases(NULL);
\r
1352 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
\r
1357 * List the fingerprint of the ssh.com-formatted key.
\r
1359 setup_passphrases(NULL);
\r
1360 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
\r
1361 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
\r
1364 * List the public half of the ssh.com-formatted key in
\r
1367 setup_passphrases(NULL);
\r
1368 test(0, "puttygen", "-L", scfilename, NULL);
\r
1371 * List the public half of the ssh.com-formatted key in
\r
1372 * IETF/ssh.com format.
\r
1374 setup_passphrases(NULL);
\r
1375 test(0, "puttygen", "-p", scfilename, NULL);
\r
1380 * Convert from OpenSSH into ssh.com.
\r
1382 setup_passphrases(NULL);
\r
1383 test(0, "puttygen", osfilename, "-o", tmpfilename1,
\r
1384 "-O", "private-sshcom", NULL);
\r
1387 * Convert from ssh.com back into a PuTTY key,
\r
1388 * supplying the same comment as we had before we
\r
1389 * started to ensure the comparison works.
\r
1391 setup_passphrases(NULL);
\r
1392 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
\r
1393 "-o", tmpfilename2, NULL);
\r
1396 * See if the PuTTY key thus generated is the same as
\r
1399 filecmp(filename, tmpfilename2,
\r
1400 "p->o->s->p clear %s", keytypes[i]);
\r
1403 * Convert from ssh.com to OpenSSH.
\r
1405 setup_passphrases(NULL);
\r
1406 test(0, "puttygen", scfilename, "-o", tmpfilename1,
\r
1407 "-O", "private-openssh", NULL);
\r
1410 * Convert from OpenSSH back into a PuTTY key,
\r
1411 * supplying the same comment as we had before we
\r
1412 * started to ensure the comparison works.
\r
1414 setup_passphrases(NULL);
\r
1415 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
\r
1416 "-o", tmpfilename2, NULL);
\r
1419 * See if the PuTTY key thus generated is the same as
\r
1422 filecmp(filename, tmpfilename2,
\r
1423 "p->s->o->p clear %s", keytypes[i]);
\r
1426 * Finally, do a round-trip conversion between PuTTY
\r
1427 * and ssh.com without involving OpenSSH, to test that
\r
1428 * the key comment is preserved in that case.
\r
1430 setup_passphrases(NULL);
\r
1431 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
\r
1433 setup_passphrases(NULL);
\r
1434 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
\r
1435 filecmp(filename, tmpfilename2,
\r
1436 "p->s->p clear %s", keytypes[i]);
\r
1440 * Check that mismatched passphrases cause an error.
\r
1442 setup_passphrases("sponge2", "sponge3", NULL);
\r
1443 test(1, "puttygen", "-P", filename, NULL);
\r
1446 * Put a passphrase back on.
\r
1448 setup_passphrases("sponge2", "sponge2", NULL);
\r
1449 test(0, "puttygen", "-P", filename, NULL);
\r
1452 * Export the private key into OpenSSH format, this time
\r
1453 * while encrypted. For RSA1 keys, this should give an
\r
1457 setup_passphrases(NULL); /* error, hence no passphrase read */
\r
1459 setup_passphrases("sponge2", NULL);
\r
1460 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
\r
1465 * List the fingerprint of the OpenSSH-formatted key.
\r
1467 setup_passphrases("sponge2", NULL);
\r
1468 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
\r
1469 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
\r
1472 * List the public half of the OpenSSH-formatted key in
\r
1475 setup_passphrases("sponge2", NULL);
\r
1476 test(0, "puttygen", "-L", osfilename, NULL);
\r
1479 * List the public half of the OpenSSH-formatted key in
\r
1480 * IETF/ssh.com format.
\r
1482 setup_passphrases("sponge2", NULL);
\r
1483 test(0, "puttygen", "-p", osfilename, NULL);
\r
1487 * Export the private key into ssh.com format, this time
\r
1488 * while encrypted. For RSA1 keys, this should give an
\r
1492 setup_passphrases(NULL); /* error, hence no passphrase read */
\r
1494 setup_passphrases("sponge2", NULL);
\r
1495 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
\r
1500 * List the fingerprint of the ssh.com-formatted key.
\r
1502 setup_passphrases("sponge2", NULL);
\r
1503 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
\r
1504 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
\r
1507 * List the public half of the ssh.com-formatted key in
\r
1510 setup_passphrases("sponge2", NULL);
\r
1511 test(0, "puttygen", "-L", scfilename, NULL);
\r
1514 * List the public half of the ssh.com-formatted key in
\r
1515 * IETF/ssh.com format.
\r
1517 setup_passphrases("sponge2", NULL);
\r
1518 test(0, "puttygen", "-p", scfilename, NULL);
\r
1523 * Convert from OpenSSH into ssh.com.
\r
1525 setup_passphrases("sponge2", NULL);
\r
1526 test(0, "puttygen", osfilename, "-o", tmpfilename1,
\r
1527 "-O", "private-sshcom", NULL);
\r
1530 * Convert from ssh.com back into a PuTTY key,
\r
1531 * supplying the same comment as we had before we
\r
1532 * started to ensure the comparison works.
\r
1534 setup_passphrases("sponge2", NULL);
\r
1535 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
\r
1536 "-o", tmpfilename2, NULL);
\r
1539 * See if the PuTTY key thus generated is the same as
\r
1542 filecmp(filename, tmpfilename2,
\r
1543 "p->o->s->p encrypted %s", keytypes[i]);
\r
1546 * Convert from ssh.com to OpenSSH.
\r
1548 setup_passphrases("sponge2", NULL);
\r
1549 test(0, "puttygen", scfilename, "-o", tmpfilename1,
\r
1550 "-O", "private-openssh", NULL);
\r
1553 * Convert from OpenSSH back into a PuTTY key,
\r
1554 * supplying the same comment as we had before we
\r
1555 * started to ensure the comparison works.
\r
1557 setup_passphrases("sponge2", NULL);
\r
1558 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
\r
1559 "-o", tmpfilename2, NULL);
\r
1562 * See if the PuTTY key thus generated is the same as
\r
1565 filecmp(filename, tmpfilename2,
\r
1566 "p->s->o->p encrypted %s", keytypes[i]);
\r
1569 * Finally, do a round-trip conversion between PuTTY
\r
1570 * and ssh.com without involving OpenSSH, to test that
\r
1571 * the key comment is preserved in that case.
\r
1573 setup_passphrases("sponge2", NULL);
\r
1574 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
\r
1576 setup_passphrases("sponge2", NULL);
\r
1577 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
\r
1578 filecmp(filename, tmpfilename2,
\r
1579 "p->s->p encrypted %s", keytypes[i]);
\r
1583 * Load with the wrong passphrase.
\r
1585 setup_passphrases("sponge8", NULL);
\r
1586 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
\r
1589 * Load a totally bogus file.
\r
1591 setup_passphrases(NULL);
\r
1592 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
\r
1594 printf("%d passes, %d fails\n", passes, fails);
\r