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-new export OpenSSH private key "
164 "(force new file format)\n"
165 " private-sshcom export ssh.com private key\n"
166 " public RFC 4716 / ssh.com public key\n"
167 " public-openssh OpenSSH public key\n"
168 " fingerprint output the key fingerprint\n"
169 " -o specify output file\n"
170 " -l equivalent to `-O fingerprint'\n"
171 " -L equivalent to `-O public-openssh'\n"
172 " -p equivalent to `-O public'\n"
176 static int save_ssh2_pubkey(char *filename, char *comment,
177 void *v_pub_blob, int pub_len)
179 unsigned char *pub_blob = (unsigned char *)v_pub_blob;
185 fp = fopen(filename, "wb");
191 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
194 fprintf(fp, "Comment: \"");
195 for (p = comment; *p; p++) {
196 if (*p == '\\' || *p == '\"')
205 while (i < pub_len) {
207 int n = (pub_len - i < 3 ? pub_len - i : 3);
208 base64_encode_atom(pub_blob + i, n, buf);
212 if (++column >= 16) {
220 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
226 static int move(char *from, char *to)
230 ret = rename(from, to);
233 * This OS may require us to remove the original file first.
236 ret = rename(from, to);
239 perror("puttygen: cannot move new file on to old one");
245 static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
248 unsigned char digest[16];
249 struct MD5Context md5c;
253 MD5Update(&md5c, blob, bloblen);
254 MD5Final(digest, &md5c);
256 sprintf(buffer, "%s ", alg);
258 sprintf(buffer + strlen(buffer), "%d ", bits);
259 for (i = 0; i < 16; i++)
260 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
263 return dupstr(buffer);
266 int main(int argc, char **argv)
269 Filename *infilename = NULL, *outfilename = NULL;
270 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
271 char *outfile = NULL, *outfiletmp = NULL;
272 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
273 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
275 char *comment = NULL, *origcomment = NULL;
276 int change_passphrase = FALSE;
277 int errs = FALSE, nogo = FALSE;
278 int intype = SSH_KEYTYPE_UNOPENABLE;
280 struct ssh2_userkey *ssh2key = NULL;
281 struct RSAKey *ssh1key = NULL;
282 unsigned char *ssh2blob = NULL;
283 char *ssh2alg = NULL;
284 const struct ssh_signkey *ssh2algf = NULL;
286 char *passphrase = NULL;
288 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
290 /* ------------------------------------------------------------------
291 * Parse the command line to figure out what we've been asked to do.
295 * If run with no arguments at all, print the usage message and
304 * Parse command line arguments.
321 opt = p++; /* opt will have _one_ leading - */
322 while (*p && *p != '=')
323 p++; /* find end of option */
330 if (!strcmp(opt, "-help")) {
333 fprintf(stderr, "puttygen: option `-%s'"
334 " expects no argument\n", opt);
339 } else if (!strcmp(opt, "-version")) {
342 fprintf(stderr, "puttygen: option `-%s'"
343 " expects no argument\n", opt);
348 } else if (!strcmp(opt, "-pgpfp")) {
351 fprintf(stderr, "puttygen: option `-%s'"
352 " expects no argument\n", opt);
354 /* support --pgpfp for consistency */
360 * For long options requiring an argument, add
361 * code along the lines of
363 * else if (!strcmp(opt, "-output")) {
366 * fprintf(stderr, "puttygen: option `-%s'"
367 * " expects an argument\n", opt);
375 "puttygen: no such option `-%s'\n", opt);
388 * Option requiring no parameter.
400 change_passphrase = TRUE;
412 progressfn = no_progress;
422 * Option requiring parameter.
428 fprintf(stderr, "puttygen: option `-%c' expects a"
433 * Now c is the option and p is the parameter.
437 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
438 keytype = RSA2, sshver = 2;
439 else if (!strcmp(p, "rsa1"))
440 keytype = RSA1, sshver = 1;
441 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
442 keytype = DSA, sshver = 2;
443 else if (!strcmp(p, "ecdsa"))
444 keytype = ECDSA, sshver = 2;
445 else if (!strcmp(p, "ed25519"))
446 keytype = ED25519, 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 outtype = OPENSSH_AUTO, sshver = 2;
470 else if (!strcmp(p, "private-openssh-new"))
471 outtype = OPENSSH_NEW, sshver = 2;
472 else if (!strcmp(p, "private-sshcom"))
473 outtype = SSHCOM, sshver = 2;
476 "puttygen: unknown output type `%s'\n", p);
484 p = NULL; /* prevent continued processing */
488 * Unrecognised option.
491 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
497 * A non-option argument.
503 fprintf(stderr, "puttygen: cannot handle more than one"
511 * No explicit key size was specified. Default varies
512 * depending on key type.
527 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
528 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
532 if (keytype == ED25519 && (bits != 256)) {
533 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
544 * If run with at least one argument _but_ not the required
545 * ones, print the usage message and return failure.
547 if (!infile && keytype == NOKEYGEN) {
552 /* ------------------------------------------------------------------
553 * Figure out further details of exactly what we're going to do.
557 * Bomb out if we've been asked to both load and generate a
560 if (keytype != NOKEYGEN && infile) {
561 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
566 * We must save the private part when generating a new key.
568 if (keytype != NOKEYGEN &&
569 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
570 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
571 fprintf(stderr, "puttygen: this would generate a new key but "
572 "discard the private part\n");
577 * Analyse the type of the input file, in case this affects our
581 infilename = filename_from_str(infile);
583 intype = key_type(infilename);
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:
593 case SSH_KEYTYPE_SSH1_PUBLIC:
595 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
602 case SSH_KEYTYPE_SSH2:
603 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
604 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
605 case SSH_KEYTYPE_OPENSSH_PEM:
606 case SSH_KEYTYPE_OPENSSH_NEW:
607 case SSH_KEYTYPE_SSHCOM:
609 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
616 case SSH_KEYTYPE_OPENSSH_AUTO:
618 assert(0 && "Should never see these types on an input file");
623 * Determine the default output file, if none is provided.
625 * This will usually be equal to stdout, except that if the
626 * input and output file formats are the same then the default
627 * output is to overwrite the input.
629 * Also in this code, we bomb out if the input and output file
630 * formats are the same and no other action is performed.
632 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
633 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
634 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
635 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
636 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
639 outfiletmp = dupcat(outfile, ".tmp", NULL);
642 if (!change_passphrase && !comment) {
643 fprintf(stderr, "puttygen: this command would perform no useful"
650 * Bomb out rather than automatically choosing to write
651 * a private key file to stdout.
653 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
654 outtype == OPENSSH_NEW || outtype == SSHCOM) {
655 fprintf(stderr, "puttygen: need to specify an output file\n");
662 * Figure out whether we need to load the encrypted part of the
663 * key. This will be the case if either (a) we need to write
664 * out a private key format, or (b) the entire input key file
667 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
668 outtype == OPENSSH_NEW || outtype == SSHCOM ||
669 intype == SSH_KEYTYPE_OPENSSH_PEM ||
670 intype == SSH_KEYTYPE_OPENSSH_NEW ||
671 intype == SSH_KEYTYPE_SSHCOM)
672 load_encrypted = TRUE;
674 load_encrypted = FALSE;
676 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
677 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
678 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
679 fprintf(stderr, "puttygen: cannot perform this action on a "
680 "public-key-only input file\n");
684 /* ------------------------------------------------------------------
685 * Now we're ready to actually do some stuff.
689 * Either load or generate a key.
691 if (keytype != NOKEYGEN) {
693 char default_comment[80];
695 struct progress prog;
702 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
703 else if (keytype == ECDSA)
704 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
705 else if (keytype == ED25519)
706 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
708 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
711 entropy = get_random_data(bits / 8);
713 fprintf(stderr, "puttygen: failed to collect entropy, "
714 "could not generate key\n");
717 random_add_heavynoise(entropy, bits / 8);
718 smemclr(entropy, bits/8);
721 if (keytype == DSA) {
722 struct dss_key *dsskey = snew(struct dss_key);
723 dsa_generate(dsskey, bits, progressfn, &prog);
724 ssh2key = snew(struct ssh2_userkey);
725 ssh2key->data = dsskey;
726 ssh2key->alg = &ssh_dss;
728 } else if (keytype == ECDSA) {
729 struct ec_key *ec = snew(struct ec_key);
730 ec_generate(ec, bits, progressfn, &prog);
731 ssh2key = snew(struct ssh2_userkey);
734 ssh2key->alg = &ssh_ecdsa_nistp256;
735 } else if (bits == 384) {
736 ssh2key->alg = &ssh_ecdsa_nistp384;
738 ssh2key->alg = &ssh_ecdsa_nistp521;
741 } else if (keytype == ED25519) {
742 struct ec_key *ec = snew(struct ec_key);
743 ec_edgenerate(ec, bits, progressfn, &prog);
744 ssh2key = snew(struct ssh2_userkey);
746 ssh2key->alg = &ssh_ecdsa_ed25519;
749 struct RSAKey *rsakey = snew(struct RSAKey);
750 rsa_generate(rsakey, bits, progressfn, &prog);
751 rsakey->comment = NULL;
752 if (keytype == RSA1) {
755 ssh2key = snew(struct ssh2_userkey);
756 ssh2key->data = rsakey;
757 ssh2key->alg = &ssh_rsa;
760 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
763 ssh2key->comment = dupstr(default_comment);
765 ssh1key->comment = dupstr(default_comment);
768 const char *error = NULL;
771 assert(infile != NULL);
774 * Find out whether the input key is encrypted.
776 if (intype == SSH_KEYTYPE_SSH1)
777 encrypted = rsakey_encrypted(infilename, &origcomment);
778 else if (intype == SSH_KEYTYPE_SSH2)
779 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
781 encrypted = import_encrypted(infilename, intype, &origcomment);
784 * If so, ask for a passphrase.
786 if (encrypted && load_encrypted) {
787 prompts_t *p = new_prompts(NULL);
789 p->to_server = FALSE;
790 p->name = dupstr("SSH key passphrase");
791 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
792 ret = console_get_userpass_input(p, NULL, 0);
796 perror("puttygen: unable to read passphrase");
799 passphrase = dupstr(p->prompts[0]->result);
809 case SSH_KEYTYPE_SSH1:
810 case SSH_KEYTYPE_SSH1_PUBLIC:
811 ssh1key = snew(struct RSAKey);
812 if (!load_encrypted) {
817 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
818 &origcomment, &error);
819 blob = (unsigned char *)vblob;
821 n = 4; /* skip modulus bits */
823 l = ssh1_read_bignum(blob + n, bloblen - n,
826 error = "SSH-1 public key blob was too short";
829 l = ssh1_read_bignum(blob + n, bloblen - n,
832 error = "SSH-1 public key blob was too short";
836 ssh1key->comment = dupstr(origcomment);
837 ssh1key->private_exponent = NULL;
840 ssh1key->iqmp = NULL;
842 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
847 error = "unknown error";
850 case SSH_KEYTYPE_SSH2:
851 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
852 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
853 if (!load_encrypted) {
854 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
855 &ssh2bloblen, &origcomment,
858 ssh2algf = find_pubkey_alg(ssh2alg);
860 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
865 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
867 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
870 if (ssh2key == SSH2_WRONG_PASSPHRASE)
871 error = "wrong passphrase";
873 error = "unknown error";
877 case SSH_KEYTYPE_OPENSSH_PEM:
878 case SSH_KEYTYPE_OPENSSH_NEW:
879 case SSH_KEYTYPE_SSHCOM:
880 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
882 if (ssh2key != SSH2_WRONG_PASSPHRASE)
885 error = "wrong passphrase";
887 error = "unknown error";
895 fprintf(stderr, "puttygen: error loading `%s': %s\n",
902 * Change the comment if asked to.
907 sfree(ssh1key->comment);
908 ssh1key->comment = dupstr(comment);
911 sfree(ssh2key->comment);
912 ssh2key->comment = dupstr(comment);
917 * Prompt for a new passphrase if we have been asked to, or if
918 * we have just generated a key.
920 if (change_passphrase || keytype != NOKEYGEN) {
921 prompts_t *p = new_prompts(NULL);
924 p->to_server = FALSE;
925 p->name = dupstr("New SSH key passphrase");
926 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
927 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
928 ret = console_get_userpass_input(p, NULL, 0);
932 perror("puttygen: unable to read new passphrase");
935 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
937 fprintf(stderr, "puttygen: passphrases do not match\n");
941 smemclr(passphrase, strlen(passphrase));
944 passphrase = dupstr(p->prompts[0]->result);
956 * (In the case where outfile and outfiletmp are both NULL,
957 * there is no semantic reason to initialise outfilename at
958 * all; but we have to write _something_ to it or some compiler
959 * will probably complain that it might be used uninitialised.)
962 outfilename = filename_from_str(outfiletmp);
964 outfilename = filename_from_str(outfile ? outfile : "");
967 int ret, real_outtype;
972 ret = saversakey(outfilename, ssh1key, passphrase);
974 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
979 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
981 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
986 if (!move(outfiletmp, outfile))
987 return 1; /* rename failed */
1000 fp = f_open(outfilename, "w", FALSE);
1003 dec1 = bignum_decimal(ssh1key->exponent);
1004 dec2 = bignum_decimal(ssh1key->modulus);
1005 fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
1006 dec1, dec2, ssh1key->comment);
1011 } else if (outtype == PUBLIC) {
1014 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1017 save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
1018 ssh2blob, ssh2bloblen);
1019 } else if (outtype == PUBLICO) {
1026 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1031 ssh2alg = ssh2key->alg->name;
1034 comment = ssh2key->comment;
1036 comment = origcomment;
1038 buffer = snewn(strlen(ssh2alg) +
1039 4 * ((ssh2bloblen+2) / 3) +
1040 strlen(comment) + 3, char);
1041 strcpy(buffer, ssh2alg);
1042 p = buffer + strlen(buffer);
1045 while (i < ssh2bloblen) {
1046 int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
1047 base64_encode_atom(ssh2blob + i, n, p);
1058 fp = f_open(outfilename, "w", FALSE);
1061 fprintf(fp, "%s\n", buffer);
1076 fingerprint = snewn(128, char);
1077 rsa_fingerprint(fingerprint, 128, ssh1key);
1080 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
1083 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
1088 fp = f_open(outfilename, "w", FALSE);
1091 fprintf(fp, "%s\n", fingerprint);
1102 assert(sshver == 2);
1104 random_ref(); /* both foreign key types require randomness,
1105 * for IV or padding */
1108 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1111 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1114 real_outtype = SSH_KEYTYPE_SSHCOM;
1117 assert(0 && "control flow goof");
1119 ret = export_ssh2(outfilename, real_outtype, ssh2key, passphrase);
1121 fprintf(stderr, "puttygen: unable to export key\n");
1125 if (!move(outfiletmp, outfile))
1126 return 1; /* rename failed */
1132 smemclr(passphrase, strlen(passphrase));
1137 freersakey(ssh1key);
1139 ssh2key->alg->freekey(ssh2key->data);
1154 void setup_passphrases(char *first, ...)
1161 prompts[nprompts++] = first;
1162 va_start(ap, first);
1163 while ((next = va_arg(ap, char *)) != NULL) {
1164 assert(nprompts < lenof(prompts));
1165 prompts[nprompts++] = next;
1171 void test(int retval, ...)
1178 va_start(ap, retval);
1179 while (va_arg(ap, char *) != NULL)
1183 argv = snewn(argc+1, char *);
1184 va_start(ap, retval);
1185 for (i = 0; i <= argc; i++)
1186 argv[i] = va_arg(ap, char *);
1190 ret = cmdgen_main(argc, argv);
1192 if (ret != retval) {
1193 printf("FAILED retval (exp %d got %d):", retval, ret);
1194 for (i = 0; i < argc; i++)
1195 printf(" %s", argv[i]);
1198 } else if (promptsgot != nprompts) {
1199 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1200 for (i = 0; i < argc; i++)
1201 printf(" %s", argv[i]);
1209 void filecmp(char *file1, char *file2, char *fmt, ...)
1212 * Ideally I should do file comparison myself, to maximise the
1213 * portability of this test suite once this application begins
1214 * running on non-Unix platforms. For the moment, though,
1215 * calling Unix diff is perfectly adequate.
1220 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1227 printf("FAILED diff (ret=%d): ", ret);
1240 char *cleanup_fp(char *s)
1244 if (!strncmp(s, "ssh-", 4)) {
1245 s += strcspn(s, " \n\t");
1246 s += strspn(s, " \n\t");
1250 s += strcspn(s, " \n\t");
1251 s += strspn(s, " \n\t");
1252 s += strcspn(s, " \n\t");
1254 return dupprintf("%.*s", s - p, p);
1257 char *get_fp(char *filename)
1260 char buf[256], *ret;
1262 fp = fopen(filename, "r");
1265 ret = fgets(buf, sizeof(buf), fp);
1269 return cleanup_fp(buf);
1272 void check_fp(char *filename, char *fp, char *fmt, ...)
1279 newfp = get_fp(filename);
1281 if (!strcmp(fp, newfp)) {
1286 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1300 int main(int argc, char **argv)
1303 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1306 * Even when this thing is compiled for automatic test mode,
1307 * it's helpful to be able to invoke it with command-line
1308 * options for _manual_ tests.
1311 return cmdgen_main(argc, argv);
1315 for (i = 0; i < lenof(keytypes); i++) {
1316 char filename[128], osfilename[128], scfilename[128];
1317 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1320 sprintf(filename, "test-%s.ppk", keytypes[i]);
1321 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1322 sprintf(osfilename, "test-%s.os", keytypes[i]);
1323 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1324 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1325 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1328 * Create an encrypted key.
1330 setup_passphrases("sponge", "sponge", NULL);
1331 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1334 * List the public key in OpenSSH format.
1336 setup_passphrases(NULL);
1337 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1341 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1342 pubfilename, tmpfilename1);
1343 if (system(cmdbuf) ||
1344 (fp = get_fp(tmpfilename1)) == NULL) {
1345 printf("UNABLE to test fingerprint matching against OpenSSH");
1350 * List the public key in IETF/ssh.com format.
1352 setup_passphrases(NULL);
1353 test(0, "puttygen", "-p", filename, NULL);
1356 * List the fingerprint of the key.
1358 setup_passphrases(NULL);
1359 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1362 * If we can't test fingerprints against OpenSSH, we
1363 * can at the very least test equality of all the
1364 * fingerprints we generate of this key throughout
1367 fp = get_fp(tmpfilename1);
1369 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1373 * Change the comment of the key; this _does_ require a
1374 * passphrase owing to the tamperproofing.
1376 * NOTE: In SSH-1, this only requires a passphrase because
1377 * of inadequacies of the loading and saving mechanisms. In
1378 * _principle_, it should be perfectly possible to modify
1379 * the comment on an SSH-1 key without requiring a
1380 * passphrase; the only reason I can't do it is because my
1381 * loading and saving mechanisms don't include a method of
1382 * loading all the key data without also trying to decrypt
1383 * the private section.
1385 * I don't consider this to be a problem worth solving,
1386 * because (a) to fix it would probably end up bloating
1387 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1388 * it shouldn't be highly significant. If it seriously
1389 * bothers anyone then perhaps I _might_ be persuadable.
1391 setup_passphrases("sponge", NULL);
1392 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1395 * Change the passphrase to nothing.
1397 setup_passphrases("sponge", "", "", NULL);
1398 test(0, "puttygen", "-P", filename, NULL);
1401 * Change the comment of the key again; this time we expect no
1402 * passphrase to be required.
1404 setup_passphrases(NULL);
1405 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1408 * Export the private key into OpenSSH format; no passphrase
1409 * should be required since the key is currently unencrypted.
1410 * For RSA1 keys, this should give an error.
1412 setup_passphrases(NULL);
1413 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1418 * List the fingerprint of the OpenSSH-formatted key.
1420 setup_passphrases(NULL);
1421 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1422 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1425 * List the public half of the OpenSSH-formatted key in
1428 setup_passphrases(NULL);
1429 test(0, "puttygen", "-L", osfilename, NULL);
1432 * List the public half of the OpenSSH-formatted key in
1433 * IETF/ssh.com format.
1435 setup_passphrases(NULL);
1436 test(0, "puttygen", "-p", osfilename, NULL);
1440 * Export the private key into ssh.com format; no passphrase
1441 * should be required since the key is currently unencrypted.
1442 * For RSA1 keys, this should give an error.
1444 setup_passphrases(NULL);
1445 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1450 * List the fingerprint of the ssh.com-formatted key.
1452 setup_passphrases(NULL);
1453 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1454 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1457 * List the public half of the ssh.com-formatted key in
1460 setup_passphrases(NULL);
1461 test(0, "puttygen", "-L", scfilename, NULL);
1464 * List the public half of the ssh.com-formatted key in
1465 * IETF/ssh.com format.
1467 setup_passphrases(NULL);
1468 test(0, "puttygen", "-p", scfilename, NULL);
1473 * Convert from OpenSSH into ssh.com.
1475 setup_passphrases(NULL);
1476 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1477 "-O", "private-sshcom", NULL);
1480 * Convert from ssh.com back into a PuTTY key,
1481 * supplying the same comment as we had before we
1482 * started to ensure the comparison works.
1484 setup_passphrases(NULL);
1485 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1486 "-o", tmpfilename2, NULL);
1489 * See if the PuTTY key thus generated is the same as
1492 filecmp(filename, tmpfilename2,
1493 "p->o->s->p clear %s", keytypes[i]);
1496 * Convert from ssh.com to OpenSSH.
1498 setup_passphrases(NULL);
1499 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1500 "-O", "private-openssh", NULL);
1503 * Convert from OpenSSH back into a PuTTY key,
1504 * supplying the same comment as we had before we
1505 * started to ensure the comparison works.
1507 setup_passphrases(NULL);
1508 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1509 "-o", tmpfilename2, NULL);
1512 * See if the PuTTY key thus generated is the same as
1515 filecmp(filename, tmpfilename2,
1516 "p->s->o->p clear %s", keytypes[i]);
1519 * Finally, do a round-trip conversion between PuTTY
1520 * and ssh.com without involving OpenSSH, to test that
1521 * the key comment is preserved in that case.
1523 setup_passphrases(NULL);
1524 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1526 setup_passphrases(NULL);
1527 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1528 filecmp(filename, tmpfilename2,
1529 "p->s->p clear %s", keytypes[i]);
1533 * Check that mismatched passphrases cause an error.
1535 setup_passphrases("sponge2", "sponge3", NULL);
1536 test(1, "puttygen", "-P", filename, NULL);
1539 * Put a passphrase back on.
1541 setup_passphrases("sponge2", "sponge2", NULL);
1542 test(0, "puttygen", "-P", filename, NULL);
1545 * Export the private key into OpenSSH format, this time
1546 * while encrypted. For RSA1 keys, this should give an
1550 setup_passphrases(NULL); /* error, hence no passphrase read */
1552 setup_passphrases("sponge2", NULL);
1553 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1558 * List the fingerprint of the OpenSSH-formatted key.
1560 setup_passphrases("sponge2", NULL);
1561 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1562 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1565 * List the public half of the OpenSSH-formatted key in
1568 setup_passphrases("sponge2", NULL);
1569 test(0, "puttygen", "-L", osfilename, NULL);
1572 * List the public half of the OpenSSH-formatted key in
1573 * IETF/ssh.com format.
1575 setup_passphrases("sponge2", NULL);
1576 test(0, "puttygen", "-p", osfilename, NULL);
1580 * Export the private key into ssh.com format, this time
1581 * while encrypted. For RSA1 keys, this should give an
1585 setup_passphrases(NULL); /* error, hence no passphrase read */
1587 setup_passphrases("sponge2", NULL);
1588 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1593 * List the fingerprint of the ssh.com-formatted key.
1595 setup_passphrases("sponge2", NULL);
1596 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1597 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1600 * List the public half of the ssh.com-formatted key in
1603 setup_passphrases("sponge2", NULL);
1604 test(0, "puttygen", "-L", scfilename, NULL);
1607 * List the public half of the ssh.com-formatted key in
1608 * IETF/ssh.com format.
1610 setup_passphrases("sponge2", NULL);
1611 test(0, "puttygen", "-p", scfilename, NULL);
1616 * Convert from OpenSSH into ssh.com.
1618 setup_passphrases("sponge2", NULL);
1619 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1620 "-O", "private-sshcom", NULL);
1623 * Convert from ssh.com back into a PuTTY key,
1624 * supplying the same comment as we had before we
1625 * started to ensure the comparison works.
1627 setup_passphrases("sponge2", NULL);
1628 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1629 "-o", tmpfilename2, NULL);
1632 * See if the PuTTY key thus generated is the same as
1635 filecmp(filename, tmpfilename2,
1636 "p->o->s->p encrypted %s", keytypes[i]);
1639 * Convert from ssh.com to OpenSSH.
1641 setup_passphrases("sponge2", NULL);
1642 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1643 "-O", "private-openssh", NULL);
1646 * Convert from OpenSSH back into a PuTTY key,
1647 * supplying the same comment as we had before we
1648 * started to ensure the comparison works.
1650 setup_passphrases("sponge2", NULL);
1651 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1652 "-o", tmpfilename2, NULL);
1655 * See if the PuTTY key thus generated is the same as
1658 filecmp(filename, tmpfilename2,
1659 "p->s->o->p encrypted %s", keytypes[i]);
1662 * Finally, do a round-trip conversion between PuTTY
1663 * and ssh.com without involving OpenSSH, to test that
1664 * the key comment is preserved in that case.
1666 setup_passphrases("sponge2", NULL);
1667 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1669 setup_passphrases("sponge2", NULL);
1670 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1671 filecmp(filename, tmpfilename2,
1672 "p->s->p encrypted %s", keytypes[i]);
1676 * Load with the wrong passphrase.
1678 setup_passphrases("sponge8", NULL);
1679 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1682 * Load a totally bogus file.
1684 setup_passphrases(NULL);
1685 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1687 printf("%d passes, %d fails\n", passes, fails);