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"
510 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
511 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
522 * If run with at least one argument _but_ not the required
523 * ones, print the usage message and return failure.
525 if (!infile && keytype == NOKEYGEN) {
530 /* ------------------------------------------------------------------
531 * Figure out further details of exactly what we're going to do.
535 * Bomb out if we've been asked to both load and generate a
538 if (keytype != NOKEYGEN && infile) {
539 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
544 * We must save the private part when generating a new key.
546 if (keytype != NOKEYGEN &&
547 (outtype != PRIVATE && outtype != OPENSSH_PEM &&
548 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
549 fprintf(stderr, "puttygen: this would generate a new key but "
550 "discard the private part\n");
555 * Analyse the type of the input file, in case this affects our
559 infilename = filename_from_str(infile);
561 intype = key_type(infilename);
565 * It would be nice here to be able to load _public_
566 * key files, in any of a number of forms, and (a)
567 * convert them to other public key types, (b) print
568 * out their fingerprints. Or, I suppose, for real
569 * orthogonality, (c) change their comment!
571 * In fact this opens some interesting possibilities.
572 * Suppose ssh2_userkey_loadpub() were able to load
573 * public key files as well as extracting the public
574 * key from private ones. And suppose I did the thing
575 * I've been wanting to do, where specifying a
576 * particular private key file for authentication
577 * causes any _other_ key in the agent to be discarded.
578 * Then, if you had an agent forwarded to the machine
579 * you were running Unix PuTTY or Plink on, and you
580 * needed to specify which of the keys in the agent it
581 * should use, you could do that by supplying a
582 * _public_ key file, thus not needing to trust even
583 * your encrypted private key file to the network. Ooh!
586 case SSH_KEYTYPE_UNOPENABLE:
587 case SSH_KEYTYPE_UNKNOWN:
588 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
589 infile, key_type_to_str(intype));
592 case SSH_KEYTYPE_SSH1:
594 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
601 case SSH_KEYTYPE_SSH2:
602 case SSH_KEYTYPE_OPENSSH_PEM:
603 case SSH_KEYTYPE_OPENSSH_NEW:
604 case SSH_KEYTYPE_SSHCOM:
606 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
616 * Determine the default output file, if none is provided.
618 * This will usually be equal to stdout, except that if the
619 * input and output file formats are the same then the default
620 * output is to overwrite the input.
622 * Also in this code, we bomb out if the input and output file
623 * formats are the same and no other action is performed.
625 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
626 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
627 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_PEM) ||
628 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
629 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
632 outfiletmp = dupcat(outfile, ".tmp", NULL);
635 if (!change_passphrase && !comment) {
636 fprintf(stderr, "puttygen: this command would perform no useful"
643 * Bomb out rather than automatically choosing to write
644 * a private key file to stdout.
646 if (outtype == PRIVATE || outtype == OPENSSH_PEM ||
647 outtype == OPENSSH_NEW || outtype == SSHCOM) {
648 fprintf(stderr, "puttygen: need to specify an output file\n");
655 * Figure out whether we need to load the encrypted part of the
656 * key. This will be the case if either (a) we need to write
657 * out a private key format, or (b) the entire input key file
660 if (outtype == PRIVATE || outtype == OPENSSH_PEM ||
661 outtype == OPENSSH_NEW || outtype == SSHCOM ||
662 intype == SSH_KEYTYPE_OPENSSH_PEM ||
663 intype == SSH_KEYTYPE_OPENSSH_NEW ||
664 intype == SSH_KEYTYPE_SSHCOM)
665 load_encrypted = TRUE;
667 load_encrypted = FALSE;
669 /* ------------------------------------------------------------------
670 * Now we're ready to actually do some stuff.
674 * Either load or generate a key.
676 if (keytype != NOKEYGEN) {
678 char default_comment[80];
680 struct progress prog;
687 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
688 else if (keytype == ECDSA)
689 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
691 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
694 entropy = get_random_data(bits / 8);
696 fprintf(stderr, "puttygen: failed to collect entropy, "
697 "could not generate key\n");
700 random_add_heavynoise(entropy, bits / 8);
701 smemclr(entropy, bits/8);
704 if (keytype == DSA) {
705 struct dss_key *dsskey = snew(struct dss_key);
706 dsa_generate(dsskey, bits, progressfn, &prog);
707 ssh2key = snew(struct ssh2_userkey);
708 ssh2key->data = dsskey;
709 ssh2key->alg = &ssh_dss;
711 } else if (keytype == ECDSA) {
712 struct ec_key *ec = snew(struct ec_key);
713 ec_generate(ec, bits, progressfn, &prog);
714 ssh2key = snew(struct ssh2_userkey);
717 ssh2key->alg = &ssh_ecdsa_nistp256;
718 } else if (bits == 384) {
719 ssh2key->alg = &ssh_ecdsa_nistp384;
721 ssh2key->alg = &ssh_ecdsa_nistp521;
725 struct RSAKey *rsakey = snew(struct RSAKey);
726 rsa_generate(rsakey, bits, progressfn, &prog);
727 rsakey->comment = NULL;
728 if (keytype == RSA1) {
731 ssh2key = snew(struct ssh2_userkey);
732 ssh2key->data = rsakey;
733 ssh2key->alg = &ssh_rsa;
736 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
739 ssh2key->comment = dupstr(default_comment);
741 ssh1key->comment = dupstr(default_comment);
744 const char *error = NULL;
747 assert(infile != NULL);
750 * Find out whether the input key is encrypted.
752 if (intype == SSH_KEYTYPE_SSH1)
753 encrypted = rsakey_encrypted(infilename, &origcomment);
754 else if (intype == SSH_KEYTYPE_SSH2)
755 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
757 encrypted = import_encrypted(infilename, intype, &origcomment);
760 * If so, ask for a passphrase.
762 if (encrypted && load_encrypted) {
763 prompts_t *p = new_prompts(NULL);
765 p->to_server = FALSE;
766 p->name = dupstr("SSH key passphrase");
767 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
768 ret = console_get_userpass_input(p, NULL, 0);
772 perror("puttygen: unable to read passphrase");
775 passphrase = dupstr(p->prompts[0]->result);
785 case SSH_KEYTYPE_SSH1:
786 ssh1key = snew(struct RSAKey);
787 if (!load_encrypted) {
792 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
793 &origcomment, &error);
794 blob = (unsigned char *)vblob;
796 n = 4; /* skip modulus bits */
798 l = ssh1_read_bignum(blob + n, bloblen - n,
801 error = "SSH-1 public key blob was too short";
804 l = ssh1_read_bignum(blob + n, bloblen - n,
807 error = "SSH-1 public key blob was too short";
811 ssh1key->comment = dupstr(origcomment);
812 ssh1key->private_exponent = NULL;
815 ssh1key->iqmp = NULL;
817 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
822 error = "unknown error";
825 case SSH_KEYTYPE_SSH2:
826 if (!load_encrypted) {
827 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
828 &ssh2bloblen, NULL, &error);
830 ssh2algf = find_pubkey_alg(ssh2alg);
832 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
837 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
839 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
842 if (ssh2key == SSH2_WRONG_PASSPHRASE)
843 error = "wrong passphrase";
845 error = "unknown error";
849 case SSH_KEYTYPE_OPENSSH_PEM:
850 case SSH_KEYTYPE_OPENSSH_NEW:
851 case SSH_KEYTYPE_SSHCOM:
852 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
854 if (ssh2key != SSH2_WRONG_PASSPHRASE)
857 error = "wrong passphrase";
859 error = "unknown error";
867 fprintf(stderr, "puttygen: error loading `%s': %s\n",
874 * Change the comment if asked to.
879 sfree(ssh1key->comment);
880 ssh1key->comment = dupstr(comment);
883 sfree(ssh2key->comment);
884 ssh2key->comment = dupstr(comment);
889 * Prompt for a new passphrase if we have been asked to, or if
890 * we have just generated a key.
892 if (change_passphrase || keytype != NOKEYGEN) {
893 prompts_t *p = new_prompts(NULL);
896 p->to_server = FALSE;
897 p->name = dupstr("New SSH key passphrase");
898 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
899 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
900 ret = console_get_userpass_input(p, NULL, 0);
904 perror("puttygen: unable to read new passphrase");
907 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
909 fprintf(stderr, "puttygen: passphrases do not match\n");
913 smemclr(passphrase, strlen(passphrase));
916 passphrase = dupstr(p->prompts[0]->result);
928 * (In the case where outfile and outfiletmp are both NULL,
929 * there is no semantic reason to initialise outfilename at
930 * all; but we have to write _something_ to it or some compiler
931 * will probably complain that it might be used uninitialised.)
934 outfilename = filename_from_str(outfiletmp);
936 outfilename = filename_from_str(outfile ? outfile : "");
939 int ret, real_outtype;
944 ret = saversakey(outfilename, ssh1key, passphrase);
946 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
951 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
953 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
958 if (!move(outfiletmp, outfile))
959 return 1; /* rename failed */
972 fp = f_open(outfilename, "w", FALSE);
975 dec1 = bignum_decimal(ssh1key->exponent);
976 dec2 = bignum_decimal(ssh1key->modulus);
977 fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
978 dec1, dec2, ssh1key->comment);
983 } else if (outtype == PUBLIC) {
986 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
989 save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
990 ssh2blob, ssh2bloblen);
991 } else if (outtype == PUBLICO) {
998 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1003 ssh2alg = ssh2key->alg->name;
1006 comment = ssh2key->comment;
1008 comment = origcomment;
1010 buffer = snewn(strlen(ssh2alg) +
1011 4 * ((ssh2bloblen+2) / 3) +
1012 strlen(comment) + 3, char);
1013 strcpy(buffer, ssh2alg);
1014 p = buffer + strlen(buffer);
1017 while (i < ssh2bloblen) {
1018 int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
1019 base64_encode_atom(ssh2blob + i, n, p);
1030 fp = f_open(outfilename, "w", FALSE);
1033 fprintf(fp, "%s\n", buffer);
1048 fingerprint = snewn(128, char);
1049 rsa_fingerprint(fingerprint, 128, ssh1key);
1052 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
1055 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
1060 fp = f_open(outfilename, "w", FALSE);
1063 fprintf(fp, "%s\n", fingerprint);
1074 assert(sshver == 2);
1076 random_ref(); /* both foreign key types require randomness,
1077 * for IV or padding */
1080 real_outtype = SSH_KEYTYPE_OPENSSH_PEM;
1083 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1086 real_outtype = SSH_KEYTYPE_SSHCOM;
1089 assert(0 && "control flow goof");
1091 ret = export_ssh2(outfilename, real_outtype, ssh2key, passphrase);
1093 fprintf(stderr, "puttygen: unable to export key\n");
1097 if (!move(outfiletmp, outfile))
1098 return 1; /* rename failed */
1104 smemclr(passphrase, strlen(passphrase));
1109 freersakey(ssh1key);
1111 ssh2key->alg->freekey(ssh2key->data);
1126 void setup_passphrases(char *first, ...)
1133 prompts[nprompts++] = first;
1134 va_start(ap, first);
1135 while ((next = va_arg(ap, char *)) != NULL) {
1136 assert(nprompts < lenof(prompts));
1137 prompts[nprompts++] = next;
1143 void test(int retval, ...)
1150 va_start(ap, retval);
1151 while (va_arg(ap, char *) != NULL)
1155 argv = snewn(argc+1, char *);
1156 va_start(ap, retval);
1157 for (i = 0; i <= argc; i++)
1158 argv[i] = va_arg(ap, char *);
1162 ret = cmdgen_main(argc, argv);
1164 if (ret != retval) {
1165 printf("FAILED retval (exp %d got %d):", retval, ret);
1166 for (i = 0; i < argc; i++)
1167 printf(" %s", argv[i]);
1170 } else if (promptsgot != nprompts) {
1171 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1172 for (i = 0; i < argc; i++)
1173 printf(" %s", argv[i]);
1181 void filecmp(char *file1, char *file2, char *fmt, ...)
1184 * Ideally I should do file comparison myself, to maximise the
1185 * portability of this test suite once this application begins
1186 * running on non-Unix platforms. For the moment, though,
1187 * calling Unix diff is perfectly adequate.
1192 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1199 printf("FAILED diff (ret=%d): ", ret);
1212 char *cleanup_fp(char *s)
1216 if (!strncmp(s, "ssh-", 4)) {
1217 s += strcspn(s, " \n\t");
1218 s += strspn(s, " \n\t");
1222 s += strcspn(s, " \n\t");
1223 s += strspn(s, " \n\t");
1224 s += strcspn(s, " \n\t");
1226 return dupprintf("%.*s", s - p, p);
1229 char *get_fp(char *filename)
1232 char buf[256], *ret;
1234 fp = fopen(filename, "r");
1237 ret = fgets(buf, sizeof(buf), fp);
1241 return cleanup_fp(buf);
1244 void check_fp(char *filename, char *fp, char *fmt, ...)
1251 newfp = get_fp(filename);
1253 if (!strcmp(fp, newfp)) {
1258 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1272 int main(int argc, char **argv)
1275 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1278 * Even when this thing is compiled for automatic test mode,
1279 * it's helpful to be able to invoke it with command-line
1280 * options for _manual_ tests.
1283 return cmdgen_main(argc, argv);
1287 for (i = 0; i < lenof(keytypes); i++) {
1288 char filename[128], osfilename[128], scfilename[128];
1289 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1292 sprintf(filename, "test-%s.ppk", keytypes[i]);
1293 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1294 sprintf(osfilename, "test-%s.os", keytypes[i]);
1295 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1296 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1297 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1300 * Create an encrypted key.
1302 setup_passphrases("sponge", "sponge", NULL);
1303 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1306 * List the public key in OpenSSH format.
1308 setup_passphrases(NULL);
1309 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1313 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1314 pubfilename, tmpfilename1);
1315 if (system(cmdbuf) ||
1316 (fp = get_fp(tmpfilename1)) == NULL) {
1317 printf("UNABLE to test fingerprint matching against OpenSSH");
1322 * List the public key in IETF/ssh.com format.
1324 setup_passphrases(NULL);
1325 test(0, "puttygen", "-p", filename, NULL);
1328 * List the fingerprint of the key.
1330 setup_passphrases(NULL);
1331 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1334 * If we can't test fingerprints against OpenSSH, we
1335 * can at the very least test equality of all the
1336 * fingerprints we generate of this key throughout
1339 fp = get_fp(tmpfilename1);
1341 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1345 * Change the comment of the key; this _does_ require a
1346 * passphrase owing to the tamperproofing.
1348 * NOTE: In SSH-1, this only requires a passphrase because
1349 * of inadequacies of the loading and saving mechanisms. In
1350 * _principle_, it should be perfectly possible to modify
1351 * the comment on an SSH-1 key without requiring a
1352 * passphrase; the only reason I can't do it is because my
1353 * loading and saving mechanisms don't include a method of
1354 * loading all the key data without also trying to decrypt
1355 * the private section.
1357 * I don't consider this to be a problem worth solving,
1358 * because (a) to fix it would probably end up bloating
1359 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1360 * it shouldn't be highly significant. If it seriously
1361 * bothers anyone then perhaps I _might_ be persuadable.
1363 setup_passphrases("sponge", NULL);
1364 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1367 * Change the passphrase to nothing.
1369 setup_passphrases("sponge", "", "", NULL);
1370 test(0, "puttygen", "-P", filename, NULL);
1373 * Change the comment of the key again; this time we expect no
1374 * passphrase to be required.
1376 setup_passphrases(NULL);
1377 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1380 * Export the private key into OpenSSH format; no passphrase
1381 * should be required since the key is currently unencrypted.
1382 * For RSA1 keys, this should give an error.
1384 setup_passphrases(NULL);
1385 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1390 * List the fingerprint of the OpenSSH-formatted key.
1392 setup_passphrases(NULL);
1393 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1394 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1397 * List the public half of the OpenSSH-formatted key in
1400 setup_passphrases(NULL);
1401 test(0, "puttygen", "-L", osfilename, NULL);
1404 * List the public half of the OpenSSH-formatted key in
1405 * IETF/ssh.com format.
1407 setup_passphrases(NULL);
1408 test(0, "puttygen", "-p", osfilename, NULL);
1412 * Export the private key into ssh.com format; no passphrase
1413 * should be required since the key is currently unencrypted.
1414 * For RSA1 keys, this should give an error.
1416 setup_passphrases(NULL);
1417 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1422 * List the fingerprint of the ssh.com-formatted key.
1424 setup_passphrases(NULL);
1425 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1426 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1429 * List the public half of the ssh.com-formatted key in
1432 setup_passphrases(NULL);
1433 test(0, "puttygen", "-L", scfilename, NULL);
1436 * List the public half of the ssh.com-formatted key in
1437 * IETF/ssh.com format.
1439 setup_passphrases(NULL);
1440 test(0, "puttygen", "-p", scfilename, NULL);
1445 * Convert from OpenSSH into ssh.com.
1447 setup_passphrases(NULL);
1448 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1449 "-O", "private-sshcom", NULL);
1452 * Convert from ssh.com back into a PuTTY key,
1453 * supplying the same comment as we had before we
1454 * started to ensure the comparison works.
1456 setup_passphrases(NULL);
1457 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1458 "-o", tmpfilename2, NULL);
1461 * See if the PuTTY key thus generated is the same as
1464 filecmp(filename, tmpfilename2,
1465 "p->o->s->p clear %s", keytypes[i]);
1468 * Convert from ssh.com to OpenSSH.
1470 setup_passphrases(NULL);
1471 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1472 "-O", "private-openssh", NULL);
1475 * Convert from OpenSSH back into a PuTTY key,
1476 * supplying the same comment as we had before we
1477 * started to ensure the comparison works.
1479 setup_passphrases(NULL);
1480 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1481 "-o", tmpfilename2, NULL);
1484 * See if the PuTTY key thus generated is the same as
1487 filecmp(filename, tmpfilename2,
1488 "p->s->o->p clear %s", keytypes[i]);
1491 * Finally, do a round-trip conversion between PuTTY
1492 * and ssh.com without involving OpenSSH, to test that
1493 * the key comment is preserved in that case.
1495 setup_passphrases(NULL);
1496 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1498 setup_passphrases(NULL);
1499 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1500 filecmp(filename, tmpfilename2,
1501 "p->s->p clear %s", keytypes[i]);
1505 * Check that mismatched passphrases cause an error.
1507 setup_passphrases("sponge2", "sponge3", NULL);
1508 test(1, "puttygen", "-P", filename, NULL);
1511 * Put a passphrase back on.
1513 setup_passphrases("sponge2", "sponge2", NULL);
1514 test(0, "puttygen", "-P", filename, NULL);
1517 * Export the private key into OpenSSH format, this time
1518 * while encrypted. For RSA1 keys, this should give an
1522 setup_passphrases(NULL); /* error, hence no passphrase read */
1524 setup_passphrases("sponge2", NULL);
1525 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1530 * List the fingerprint of the OpenSSH-formatted key.
1532 setup_passphrases("sponge2", NULL);
1533 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1534 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1537 * List the public half of the OpenSSH-formatted key in
1540 setup_passphrases("sponge2", NULL);
1541 test(0, "puttygen", "-L", osfilename, NULL);
1544 * List the public half of the OpenSSH-formatted key in
1545 * IETF/ssh.com format.
1547 setup_passphrases("sponge2", NULL);
1548 test(0, "puttygen", "-p", osfilename, NULL);
1552 * Export the private key into ssh.com format, this time
1553 * while encrypted. For RSA1 keys, this should give an
1557 setup_passphrases(NULL); /* error, hence no passphrase read */
1559 setup_passphrases("sponge2", NULL);
1560 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1565 * List the fingerprint of the ssh.com-formatted key.
1567 setup_passphrases("sponge2", NULL);
1568 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1569 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1572 * List the public half of the ssh.com-formatted key in
1575 setup_passphrases("sponge2", NULL);
1576 test(0, "puttygen", "-L", scfilename, NULL);
1579 * List the public half of the ssh.com-formatted key in
1580 * IETF/ssh.com format.
1582 setup_passphrases("sponge2", NULL);
1583 test(0, "puttygen", "-p", scfilename, NULL);
1588 * Convert from OpenSSH into ssh.com.
1590 setup_passphrases("sponge2", NULL);
1591 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1592 "-O", "private-sshcom", NULL);
1595 * Convert from ssh.com back into a PuTTY key,
1596 * supplying the same comment as we had before we
1597 * started to ensure the comparison works.
1599 setup_passphrases("sponge2", NULL);
1600 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1601 "-o", tmpfilename2, NULL);
1604 * See if the PuTTY key thus generated is the same as
1607 filecmp(filename, tmpfilename2,
1608 "p->o->s->p encrypted %s", keytypes[i]);
1611 * Convert from ssh.com to OpenSSH.
1613 setup_passphrases("sponge2", NULL);
1614 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1615 "-O", "private-openssh", NULL);
1618 * Convert from OpenSSH back into a PuTTY key,
1619 * supplying the same comment as we had before we
1620 * started to ensure the comparison works.
1622 setup_passphrases("sponge2", NULL);
1623 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1624 "-o", tmpfilename2, NULL);
1627 * See if the PuTTY key thus generated is the same as
1630 filecmp(filename, tmpfilename2,
1631 "p->s->o->p encrypted %s", keytypes[i]);
1634 * Finally, do a round-trip conversion between PuTTY
1635 * and ssh.com without involving OpenSSH, to test that
1636 * the key comment is preserved in that case.
1638 setup_passphrases("sponge2", NULL);
1639 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1641 setup_passphrases("sponge2", NULL);
1642 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1643 filecmp(filename, tmpfilename2,
1644 "p->s->p encrypted %s", keytypes[i]);
1648 * Load with the wrong passphrase.
1650 setup_passphrases("sponge8", NULL);
1651 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1654 * Load a totally bogus file.
1656 setup_passphrases(NULL);
1657 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1659 printf("%d passes, %d fails\n", passes, fails);