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
);
106 * Stubs to let everything else link sensibly.
108 void log_eventlog(void *handle
, const char *event
)
111 char *x_get_default(const char *key
)
115 void sk_cleanup(void)
119 void showversion(void)
121 char *verstr
= dupstr(ver
);
122 verstr
[0] = tolower(verstr
[0]);
123 printf("PuTTYgen %s\n", verstr
);
127 void usage(int standalone
)
130 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
131 " [ -C comment ] [ -P ] [ -q ]\n"
132 " [ -o output-keyfile ] [ -O type | -l | -L"
136 "Use \"puttygen --help\" for more detail.\n");
142 * Help message is an extended version of the usage message. So
143 * start with that, plus a version heading.
148 " -t specify key type when generating (rsa, dsa, rsa1)\n"
149 " -b specify number of bits when generating key\n"
150 " -C change or specify key comment\n"
151 " -P change key passphrase\n"
152 " -q quiet: do not display progress bar\n"
153 " -O specify output type:\n"
154 " private output PuTTY private key format\n"
155 " private-openssh export OpenSSH private key\n"
156 " private-sshcom export ssh.com private key\n"
157 " public standard / ssh.com public key\n"
158 " public-openssh OpenSSH public key\n"
159 " fingerprint output the key fingerprint\n"
160 " -o specify output file\n"
161 " -l equivalent to `-O fingerprint'\n"
162 " -L equivalent to `-O public-openssh'\n"
163 " -p equivalent to `-O public'\n"
167 static int save_ssh2_pubkey(char *filename
, char *comment
,
168 void *v_pub_blob
, int pub_len
)
170 unsigned char *pub_blob
= (unsigned char *)v_pub_blob
;
176 fp
= fopen(filename
, "wb");
182 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
185 fprintf(fp
, "Comment: \"");
186 for (p
= comment
; *p
; p
++) {
187 if (*p
== '\\' || *p
== '\"')
196 while (i
< pub_len
) {
198 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
199 base64_encode_atom(pub_blob
+ i
, n
, buf
);
203 if (++column
>= 16) {
211 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
217 static int move(char *from
, char *to
)
221 ret
= rename(from
, to
);
224 * This OS may require us to remove the original file first.
227 ret
= rename(from
, to
);
230 perror("puttygen: cannot move new file on to old one");
236 static char *blobfp(char *alg
, int bits
, unsigned char *blob
, int bloblen
)
239 unsigned char digest
[16];
240 struct MD5Context md5c
;
244 MD5Update(&md5c
, blob
, bloblen
);
245 MD5Final(digest
, &md5c
);
247 sprintf(buffer
, "%s ", alg
);
249 sprintf(buffer
+ strlen(buffer
), "%d ", bits
);
250 for (i
= 0; i
< 16; i
++)
251 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
254 return dupstr(buffer
);
257 int main(int argc
, char **argv
)
261 enum { NOKEYGEN
, RSA1
, RSA2
, DSA
} keytype
= NOKEYGEN
;
262 char *outfile
= NULL
, *outfiletmp
= NULL
;
263 Filename outfilename
;
264 enum { PRIVATE
, PUBLIC
, PUBLICO
, FP
, OPENSSH
, SSHCOM
} outtype
= PRIVATE
;
266 char *comment
= NULL
, *origcomment
= NULL
;
267 int change_passphrase
= FALSE
;
268 int errs
= FALSE
, nogo
= FALSE
;
269 int intype
= SSH_KEYTYPE_UNOPENABLE
;
271 struct ssh2_userkey
*ssh2key
= NULL
;
272 struct RSAKey
*ssh1key
= NULL
;
273 unsigned char *ssh2blob
= NULL
;
274 char *ssh2alg
= NULL
;
275 const struct ssh_signkey
*ssh2algf
= NULL
;
277 char *passphrase
= NULL
;
279 progfn_t progressfn
= is_interactive() ? progress_update
: no_progress
;
281 /* ------------------------------------------------------------------
282 * Parse the command line to figure out what we've been asked to do.
286 * If run with no arguments at all, print the usage message and
295 * Parse command line arguments.
312 opt
= p
++; /* opt will have _one_ leading - */
313 while (*p
&& *p
!= '=')
314 p
++; /* find end of option */
320 if (!strcmp(opt
, "-help")) {
323 } else if (!strcmp(opt
, "-version")) {
326 } else if (!strcmp(opt
, "-pgpfp")) {
327 /* support "-pgpfp" for consistency with others */
332 * A sample option requiring an argument:
334 * else if (!strcmp(opt, "-output")) {
336 * errs = TRUE, error(err_optnoarg, opt);
344 "puttygen: no such option `-%s'\n", opt
);
357 * Option requiring no parameter.
369 change_passphrase
= TRUE
;
381 progressfn
= no_progress
;
391 * Option requiring parameter.
397 fprintf(stderr
, "puttygen: option `-%c' expects a"
402 * Now c is the option and p is the parameter.
406 if (!strcmp(p
, "rsa") || !strcmp(p
, "rsa2"))
407 keytype
= RSA2
, sshver
= 2;
408 else if (!strcmp(p
, "rsa1"))
409 keytype
= RSA1
, sshver
= 1;
410 else if (!strcmp(p
, "dsa") || !strcmp(p
, "dss"))
411 keytype
= DSA
, sshver
= 2;
414 "puttygen: unknown key type `%s'\n", p
);
425 if (!strcmp(p
, "public"))
427 else if (!strcmp(p
, "public-openssh"))
429 else if (!strcmp(p
, "private"))
431 else if (!strcmp(p
, "fingerprint"))
433 else if (!strcmp(p
, "private-openssh"))
434 outtype
= OPENSSH
, sshver
= 2;
435 else if (!strcmp(p
, "private-sshcom"))
436 outtype
= SSHCOM
, sshver
= 2;
439 "puttygen: unknown output type `%s'\n", p
);
447 p
= NULL
; /* prevent continued processing */
451 * Unrecognised option.
454 fprintf(stderr
, "puttygen: no such option `-%c'\n", c
);
460 * A non-option argument.
466 fprintf(stderr
, "puttygen: cannot handle more than one"
479 * If run with at least one argument _but_ not the required
480 * ones, print the usage message and return failure.
482 if (!infile
&& keytype
== NOKEYGEN
) {
487 /* ------------------------------------------------------------------
488 * Figure out further details of exactly what we're going to do.
492 * Bomb out if we've been asked to both load and generate a
495 if (keytype
!= NOKEYGEN
&& infile
) {
496 fprintf(stderr
, "puttygen: cannot both load and generate a key\n");
501 * We must save the private part when generating a new key.
503 if (keytype
!= NOKEYGEN
&&
504 (outtype
!= PRIVATE
&& outtype
!= OPENSSH
&& outtype
!= SSHCOM
)) {
505 fprintf(stderr
, "puttygen: this would generate a new key but "
506 "discard the private part\n");
511 * Analyse the type of the input file, in case this affects our
515 infilename
= filename_from_str(infile
);
517 intype
= key_type(&infilename
);
521 * It would be nice here to be able to load _public_
522 * key files, in any of a number of forms, and (a)
523 * convert them to other public key types, (b) print
524 * out their fingerprints. Or, I suppose, for real
525 * orthogonality, (c) change their comment!
527 * In fact this opens some interesting possibilities.
528 * Suppose ssh2_userkey_loadpub() were able to load
529 * public key files as well as extracting the public
530 * key from private ones. And suppose I did the thing
531 * I've been wanting to do, where specifying a
532 * particular private key file for authentication
533 * causes any _other_ key in the agent to be discarded.
534 * Then, if you had an agent forwarded to the machine
535 * you were running Unix PuTTY or Plink on, and you
536 * needed to specify which of the keys in the agent it
537 * should use, you could do that by supplying a
538 * _public_ key file, thus not needing to trust even
539 * your encrypted private key file to the network. Ooh!
542 case SSH_KEYTYPE_UNOPENABLE
:
543 case SSH_KEYTYPE_UNKNOWN
:
544 fprintf(stderr
, "puttygen: unable to load file `%s': %s\n",
545 infile
, key_type_to_str(intype
));
548 case SSH_KEYTYPE_SSH1
:
550 fprintf(stderr
, "puttygen: conversion from SSH-1 to SSH-2 keys"
557 case SSH_KEYTYPE_SSH2
:
558 case SSH_KEYTYPE_OPENSSH
:
559 case SSH_KEYTYPE_SSHCOM
:
561 fprintf(stderr
, "puttygen: conversion from SSH-2 to SSH-1 keys"
571 * Determine the default output file, if none is provided.
573 * This will usually be equal to stdout, except that if the
574 * input and output file formats are the same then the default
575 * output is to overwrite the input.
577 * Also in this code, we bomb out if the input and output file
578 * formats are the same and no other action is performed.
580 if ((intype
== SSH_KEYTYPE_SSH1
&& outtype
== PRIVATE
) ||
581 (intype
== SSH_KEYTYPE_SSH2
&& outtype
== PRIVATE
) ||
582 (intype
== SSH_KEYTYPE_OPENSSH
&& outtype
== OPENSSH
) ||
583 (intype
== SSH_KEYTYPE_SSHCOM
&& outtype
== SSHCOM
)) {
586 outfiletmp
= dupcat(outfile
, ".tmp", NULL
);
589 if (!change_passphrase
&& !comment
) {
590 fprintf(stderr
, "puttygen: this command would perform no useful"
597 * Bomb out rather than automatically choosing to write
598 * a private key file to stdout.
600 if (outtype
==PRIVATE
|| outtype
==OPENSSH
|| outtype
==SSHCOM
) {
601 fprintf(stderr
, "puttygen: need to specify an output file\n");
608 * Figure out whether we need to load the encrypted part of the
609 * key. This will be the case if either (a) we need to write
610 * out a private key format, or (b) the entire input key file
613 if (outtype
== PRIVATE
|| outtype
== OPENSSH
|| outtype
== SSHCOM
||
614 intype
== SSH_KEYTYPE_OPENSSH
|| intype
== SSH_KEYTYPE_SSHCOM
)
615 load_encrypted
= TRUE
;
617 load_encrypted
= FALSE
;
619 /* ------------------------------------------------------------------
620 * Now we're ready to actually do some stuff.
624 * Either load or generate a key.
626 if (keytype
!= NOKEYGEN
) {
628 char default_comment
[80];
630 struct progress prog
;
637 strftime(default_comment
, 30, "dsa-key-%Y%m%d", &tm
);
639 strftime(default_comment
, 30, "rsa-key-%Y%m%d", &tm
);
642 entropy
= get_random_data(bits
/ 8);
643 random_add_heavynoise(entropy
, bits
/ 8);
644 memset(entropy
, 0, bits
/8);
647 if (keytype
== DSA
) {
648 struct dss_key
*dsskey
= snew(struct dss_key
);
649 dsa_generate(dsskey
, bits
, progressfn
, &prog
);
650 ssh2key
= snew(struct ssh2_userkey
);
651 ssh2key
->data
= dsskey
;
652 ssh2key
->alg
= &ssh_dss
;
655 struct RSAKey
*rsakey
= snew(struct RSAKey
);
656 rsa_generate(rsakey
, bits
, progressfn
, &prog
);
657 rsakey
->comment
= NULL
;
658 if (keytype
== RSA1
) {
661 ssh2key
= snew(struct ssh2_userkey
);
662 ssh2key
->data
= rsakey
;
663 ssh2key
->alg
= &ssh_rsa
;
666 progressfn(&prog
, PROGFN_PROGRESS
, INT_MAX
, -1);
669 ssh2key
->comment
= dupstr(default_comment
);
671 ssh1key
->comment
= dupstr(default_comment
);
674 const char *error
= NULL
;
677 assert(infile
!= NULL
);
680 * Find out whether the input key is encrypted.
682 if (intype
== SSH_KEYTYPE_SSH1
)
683 encrypted
= rsakey_encrypted(&infilename
, &origcomment
);
684 else if (intype
== SSH_KEYTYPE_SSH2
)
685 encrypted
= ssh2_userkey_encrypted(&infilename
, &origcomment
);
687 encrypted
= import_encrypted(&infilename
, intype
, &origcomment
);
690 * If so, ask for a passphrase.
692 if (encrypted
&& load_encrypted
) {
693 prompts_t
*p
= new_prompts(NULL
);
695 p
->to_server
= FALSE
;
696 p
->name
= dupstr("SSH key passphrase");
697 add_prompt(p
, dupstr("Enter passphrase to load key: "), FALSE
, 512);
698 ret
= console_get_userpass_input(p
, NULL
, 0);
702 perror("puttygen: unable to read passphrase");
705 passphrase
= dupstr(p
->prompts
[0]->result
);
715 case SSH_KEYTYPE_SSH1
:
716 ssh1key
= snew(struct RSAKey
);
717 if (!load_encrypted
) {
722 ret
= rsakey_pubblob(&infilename
, &vblob
, &bloblen
,
723 &origcomment
, &error
);
724 blob
= (unsigned char *)vblob
;
726 n
= 4; /* skip modulus bits */
728 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
731 error
= "SSH-1 public key blob was too short";
734 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
737 error
= "SSH-1 public key blob was too short";
741 ssh1key
->comment
= dupstr(origcomment
);
742 ssh1key
->private_exponent
= NULL
;
744 ret
= loadrsakey(&infilename
, ssh1key
, passphrase
, &error
);
749 error
= "unknown error";
752 case SSH_KEYTYPE_SSH2
:
753 if (!load_encrypted
) {
754 ssh2blob
= ssh2_userkey_loadpub(&infilename
, &ssh2alg
,
755 &ssh2bloblen
, NULL
, &error
);
756 ssh2algf
= find_pubkey_alg(ssh2alg
);
758 bits
= ssh2algf
->pubkey_bits(ssh2blob
, ssh2bloblen
);
762 ssh2key
= ssh2_load_userkey(&infilename
, passphrase
, &error
);
764 if ((ssh2key
&& ssh2key
!= SSH2_WRONG_PASSPHRASE
) || ssh2blob
)
767 if (ssh2key
== SSH2_WRONG_PASSPHRASE
)
768 error
= "wrong passphrase";
770 error
= "unknown error";
774 case SSH_KEYTYPE_OPENSSH
:
775 case SSH_KEYTYPE_SSHCOM
:
776 ssh2key
= import_ssh2(&infilename
, intype
, passphrase
, &error
);
778 if (ssh2key
!= SSH2_WRONG_PASSPHRASE
)
781 error
= "wrong passphrase";
783 error
= "unknown error";
791 fprintf(stderr
, "puttygen: error loading `%s': %s\n",
798 * Change the comment if asked to.
803 sfree(ssh1key
->comment
);
804 ssh1key
->comment
= dupstr(comment
);
807 sfree(ssh2key
->comment
);
808 ssh2key
->comment
= dupstr(comment
);
813 * Prompt for a new passphrase if we have been asked to, or if
814 * we have just generated a key.
816 if (change_passphrase
|| keytype
!= NOKEYGEN
) {
817 prompts_t
*p
= new_prompts(NULL
);
820 p
->to_server
= FALSE
;
821 p
->name
= dupstr("New SSH key passphrase");
822 add_prompt(p
, dupstr("Enter passphrase to save key: "), FALSE
, 512);
823 add_prompt(p
, dupstr("Re-enter passphrase to verify: "), FALSE
, 512);
824 ret
= console_get_userpass_input(p
, NULL
, 0);
828 perror("puttygen: unable to read new passphrase");
831 if (strcmp(p
->prompts
[0]->result
, p
->prompts
[1]->result
)) {
833 fprintf(stderr
, "puttygen: passphrases do not match\n");
837 memset(passphrase
, 0, strlen(passphrase
));
840 passphrase
= dupstr(p
->prompts
[0]->result
);
852 * (In the case where outfile and outfiletmp are both NULL,
853 * there is no semantic reason to initialise outfilename at
854 * all; but we have to write _something_ to it or some compiler
855 * will probably complain that it might be used uninitialised.)
858 outfilename
= filename_from_str(outfiletmp
);
860 outfilename
= filename_from_str(outfile ? outfile
: "");
868 ret
= saversakey(&outfilename
, ssh1key
, passphrase
);
870 fprintf(stderr
, "puttygen: unable to save SSH-1 private key\n");
875 ret
= ssh2_save_userkey(&outfilename
, ssh2key
, passphrase
);
877 fprintf(stderr
, "puttygen: unable to save SSH-2 private key\n");
882 if (!move(outfiletmp
, outfile
))
883 return 1; /* rename failed */
896 fp
= f_open(outfilename
, "w", FALSE
);
899 dec1
= bignum_decimal(ssh1key
->exponent
);
900 dec2
= bignum_decimal(ssh1key
->modulus
);
901 fprintf(fp
, "%d %s %s %s\n", bignum_bitcount(ssh1key
->modulus
),
902 dec1
, dec2
, ssh1key
->comment
);
907 } else if (outtype
== PUBLIC
) {
910 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
913 save_ssh2_pubkey(outfile
, ssh2key ? ssh2key
->comment
: origcomment
,
914 ssh2blob
, ssh2bloblen
);
915 } else if (outtype
== PUBLICO
) {
922 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
927 ssh2alg
= ssh2key
->alg
->name
;
930 comment
= ssh2key
->comment
;
932 comment
= origcomment
;
934 buffer
= snewn(strlen(ssh2alg
) +
935 4 * ((ssh2bloblen
+2) / 3) +
936 strlen(comment
) + 3, char);
937 strcpy(buffer
, ssh2alg
);
938 p
= buffer
+ strlen(buffer
);
941 while (i
< ssh2bloblen
) {
942 int n
= (ssh2bloblen
- i
< 3 ? ssh2bloblen
- i
: 3);
943 base64_encode_atom(ssh2blob
+ i
, n
, p
);
954 fp
= f_open(outfilename
, "w", FALSE
);
957 fprintf(fp
, "%s\n", buffer
);
972 fingerprint
= snewn(128, char);
973 rsa_fingerprint(fingerprint
, 128, ssh1key
);
976 fingerprint
= ssh2key
->alg
->fingerprint(ssh2key
->data
);
979 fingerprint
= blobfp(ssh2alg
, bits
, ssh2blob
, ssh2bloblen
);
984 fp
= f_open(outfilename
, "w", FALSE
);
987 fprintf(fp
, "%s\n", fingerprint
);
999 ret
= export_ssh2(&outfilename
, outtype
, ssh2key
, passphrase
);
1001 fprintf(stderr
, "puttygen: unable to export key\n");
1005 if (!move(outfiletmp
, outfile
))
1006 return 1; /* rename failed */
1012 memset(passphrase
, 0, strlen(passphrase
));
1017 freersakey(ssh1key
);
1019 ssh2key
->alg
->freekey(ssh2key
->data
);
1034 void setup_passphrases(char *first
, ...)
1041 prompts
[nprompts
++] = first
;
1042 va_start(ap
, first
);
1043 while ((next
= va_arg(ap
, char *)) != NULL
) {
1044 assert(nprompts
< lenof(prompts
));
1045 prompts
[nprompts
++] = next
;
1051 void test(int retval
, ...)
1058 va_start(ap
, retval
);
1059 while (va_arg(ap
, char *) != NULL
)
1063 argv
= snewn(argc
+1, char *);
1064 va_start(ap
, retval
);
1065 for (i
= 0; i
<= argc
; i
++)
1066 argv
[i
] = va_arg(ap
, char *);
1070 ret
= cmdgen_main(argc
, argv
);
1072 if (ret
!= retval
) {
1073 printf("FAILED retval (exp %d got %d):", retval
, ret
);
1074 for (i
= 0; i
< argc
; i
++)
1075 printf(" %s", argv
[i
]);
1078 } else if (promptsgot
!= nprompts
) {
1079 printf("FAILED nprompts (exp %d got %d):", nprompts
, promptsgot
);
1080 for (i
= 0; i
< argc
; i
++)
1081 printf(" %s", argv
[i
]);
1089 void filecmp(char *file1
, char *file2
, char *fmt
, ...)
1092 * Ideally I should do file comparison myself, to maximise the
1093 * portability of this test suite once this application begins
1094 * running on non-Unix platforms. For the moment, though,
1095 * calling Unix diff is perfectly adequate.
1100 buf
= dupprintf("diff -q '%s' '%s'", file1
, file2
);
1107 printf("FAILED diff (ret=%d): ", ret
);
1120 char *cleanup_fp(char *s
)
1124 if (!strncmp(s
, "ssh-", 4)) {
1125 s
+= strcspn(s
, " \n\t");
1126 s
+= strspn(s
, " \n\t");
1130 s
+= strcspn(s
, " \n\t");
1131 s
+= strspn(s
, " \n\t");
1132 s
+= strcspn(s
, " \n\t");
1134 return dupprintf("%.*s", s
- p
, p
);
1137 char *get_fp(char *filename
)
1140 char buf
[256], *ret
;
1142 fp
= fopen(filename
, "r");
1145 ret
= fgets(buf
, sizeof(buf
), fp
);
1149 return cleanup_fp(buf
);
1152 void check_fp(char *filename
, char *fp
, char *fmt
, ...)
1159 newfp
= get_fp(filename
);
1161 if (!strcmp(fp
, newfp
)) {
1166 printf("FAILED check_fp ['%s' != '%s']: ", newfp
, fp
);
1180 int main(int argc
, char **argv
)
1183 static char *const keytypes
[] = { "rsa1", "dsa", "rsa" };
1186 * Even when this thing is compiled for automatic test mode,
1187 * it's helpful to be able to invoke it with command-line
1188 * options for _manual_ tests.
1191 return cmdgen_main(argc
, argv
);
1195 for (i
= 0; i
< lenof(keytypes
); i
++) {
1196 char filename
[128], osfilename
[128], scfilename
[128];
1197 char pubfilename
[128], tmpfilename1
[128], tmpfilename2
[128];
1200 sprintf(filename
, "test-%s.ppk", keytypes
[i
]);
1201 sprintf(pubfilename
, "test-%s.pub", keytypes
[i
]);
1202 sprintf(osfilename
, "test-%s.os", keytypes
[i
]);
1203 sprintf(scfilename
, "test-%s.sc", keytypes
[i
]);
1204 sprintf(tmpfilename1
, "test-%s.tmp1", keytypes
[i
]);
1205 sprintf(tmpfilename2
, "test-%s.tmp2", keytypes
[i
]);
1208 * Create an encrypted key.
1210 setup_passphrases("sponge", "sponge", NULL
);
1211 test(0, "puttygen", "-t", keytypes
[i
], "-o", filename
, NULL
);
1214 * List the public key in OpenSSH format.
1216 setup_passphrases(NULL
);
1217 test(0, "puttygen", "-L", filename
, "-o", pubfilename
, NULL
);
1221 sprintf(cmdbuf
, "ssh-keygen -l -f '%s' > '%s'",
1222 pubfilename
, tmpfilename1
);
1223 if (system(cmdbuf
) ||
1224 (fp
= get_fp(tmpfilename1
)) == NULL
) {
1225 printf("UNABLE to test fingerprint matching against OpenSSH");
1230 * List the public key in IETF/ssh.com format.
1232 setup_passphrases(NULL
);
1233 test(0, "puttygen", "-p", filename
, NULL
);
1236 * List the fingerprint of the key.
1238 setup_passphrases(NULL
);
1239 test(0, "puttygen", "-l", filename
, "-o", tmpfilename1
, NULL
);
1242 * If we can't test fingerprints against OpenSSH, we
1243 * can at the very least test equality of all the
1244 * fingerprints we generate of this key throughout
1247 fp
= get_fp(tmpfilename1
);
1249 check_fp(tmpfilename1
, fp
, "%s initial fp", keytypes
[i
]);
1253 * Change the comment of the key; this _does_ require a
1254 * passphrase owing to the tamperproofing.
1256 * NOTE: In SSH-1, this only requires a passphrase because
1257 * of inadequacies of the loading and saving mechanisms. In
1258 * _principle_, it should be perfectly possible to modify
1259 * the comment on an SSH-1 key without requiring a
1260 * passphrase; the only reason I can't do it is because my
1261 * loading and saving mechanisms don't include a method of
1262 * loading all the key data without also trying to decrypt
1263 * the private section.
1265 * I don't consider this to be a problem worth solving,
1266 * because (a) to fix it would probably end up bloating
1267 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1268 * it shouldn't be highly significant. If it seriously
1269 * bothers anyone then perhaps I _might_ be persuadable.
1271 setup_passphrases("sponge", NULL
);
1272 test(0, "puttygen", "-C", "new-comment", filename
, NULL
);
1275 * Change the passphrase to nothing.
1277 setup_passphrases("sponge", "", "", NULL
);
1278 test(0, "puttygen", "-P", filename
, NULL
);
1281 * Change the comment of the key again; this time we expect no
1282 * passphrase to be required.
1284 setup_passphrases(NULL
);
1285 test(0, "puttygen", "-C", "new-comment-2", filename
, NULL
);
1288 * Export the private key into OpenSSH format; no passphrase
1289 * should be required since the key is currently unencrypted.
1290 * For RSA1 keys, this should give an error.
1292 setup_passphrases(NULL
);
1293 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1298 * List the fingerprint of the OpenSSH-formatted key.
1300 setup_passphrases(NULL
);
1301 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1302 check_fp(tmpfilename1
, fp
, "%s openssh clear fp", keytypes
[i
]);
1305 * List the public half of the OpenSSH-formatted key in
1308 setup_passphrases(NULL
);
1309 test(0, "puttygen", "-L", osfilename
, NULL
);
1312 * List the public half of the OpenSSH-formatted key in
1313 * IETF/ssh.com format.
1315 setup_passphrases(NULL
);
1316 test(0, "puttygen", "-p", osfilename
, NULL
);
1320 * Export the private key into ssh.com format; no passphrase
1321 * should be required since the key is currently unencrypted.
1322 * For RSA1 keys, this should give an error.
1324 setup_passphrases(NULL
);
1325 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1330 * List the fingerprint of the ssh.com-formatted key.
1332 setup_passphrases(NULL
);
1333 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1334 check_fp(tmpfilename1
, fp
, "%s ssh.com clear fp", keytypes
[i
]);
1337 * List the public half of the ssh.com-formatted key in
1340 setup_passphrases(NULL
);
1341 test(0, "puttygen", "-L", scfilename
, NULL
);
1344 * List the public half of the ssh.com-formatted key in
1345 * IETF/ssh.com format.
1347 setup_passphrases(NULL
);
1348 test(0, "puttygen", "-p", scfilename
, NULL
);
1353 * Convert from OpenSSH into ssh.com.
1355 setup_passphrases(NULL
);
1356 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1357 "-O", "private-sshcom", NULL
);
1360 * Convert from ssh.com back into a PuTTY key,
1361 * supplying the same comment as we had before we
1362 * started to ensure the comparison works.
1364 setup_passphrases(NULL
);
1365 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1366 "-o", tmpfilename2
, NULL
);
1369 * See if the PuTTY key thus generated is the same as
1372 filecmp(filename
, tmpfilename2
,
1373 "p->o->s->p clear %s", keytypes
[i
]);
1376 * Convert from ssh.com to OpenSSH.
1378 setup_passphrases(NULL
);
1379 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1380 "-O", "private-openssh", NULL
);
1383 * Convert from OpenSSH back into a PuTTY key,
1384 * supplying the same comment as we had before we
1385 * started to ensure the comparison works.
1387 setup_passphrases(NULL
);
1388 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1389 "-o", tmpfilename2
, NULL
);
1392 * See if the PuTTY key thus generated is the same as
1395 filecmp(filename
, tmpfilename2
,
1396 "p->s->o->p clear %s", keytypes
[i
]);
1399 * Finally, do a round-trip conversion between PuTTY
1400 * and ssh.com without involving OpenSSH, to test that
1401 * the key comment is preserved in that case.
1403 setup_passphrases(NULL
);
1404 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1406 setup_passphrases(NULL
);
1407 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1408 filecmp(filename
, tmpfilename2
,
1409 "p->s->p clear %s", keytypes
[i
]);
1413 * Check that mismatched passphrases cause an error.
1415 setup_passphrases("sponge2", "sponge3", NULL
);
1416 test(1, "puttygen", "-P", filename
, NULL
);
1419 * Put a passphrase back on.
1421 setup_passphrases("sponge2", "sponge2", NULL
);
1422 test(0, "puttygen", "-P", filename
, NULL
);
1425 * Export the private key into OpenSSH format, this time
1426 * while encrypted. For RSA1 keys, this should give an
1430 setup_passphrases(NULL
); /* error, hence no passphrase read */
1432 setup_passphrases("sponge2", NULL
);
1433 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1438 * List the fingerprint of the OpenSSH-formatted key.
1440 setup_passphrases("sponge2", NULL
);
1441 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1442 check_fp(tmpfilename1
, fp
, "%s openssh encrypted fp", keytypes
[i
]);
1445 * List the public half of the OpenSSH-formatted key in
1448 setup_passphrases("sponge2", NULL
);
1449 test(0, "puttygen", "-L", osfilename
, NULL
);
1452 * List the public half of the OpenSSH-formatted key in
1453 * IETF/ssh.com format.
1455 setup_passphrases("sponge2", NULL
);
1456 test(0, "puttygen", "-p", osfilename
, NULL
);
1460 * Export the private key into ssh.com format, this time
1461 * while encrypted. For RSA1 keys, this should give an
1465 setup_passphrases(NULL
); /* error, hence no passphrase read */
1467 setup_passphrases("sponge2", NULL
);
1468 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1473 * List the fingerprint of the ssh.com-formatted key.
1475 setup_passphrases("sponge2", NULL
);
1476 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1477 check_fp(tmpfilename1
, fp
, "%s ssh.com encrypted fp", keytypes
[i
]);
1480 * List the public half of the ssh.com-formatted key in
1483 setup_passphrases("sponge2", NULL
);
1484 test(0, "puttygen", "-L", scfilename
, NULL
);
1487 * List the public half of the ssh.com-formatted key in
1488 * IETF/ssh.com format.
1490 setup_passphrases("sponge2", NULL
);
1491 test(0, "puttygen", "-p", scfilename
, NULL
);
1496 * Convert from OpenSSH into ssh.com.
1498 setup_passphrases("sponge2", NULL
);
1499 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1500 "-O", "private-sshcom", NULL
);
1503 * Convert from ssh.com 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("sponge2", 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->o->s->p encrypted %s", keytypes
[i
]);
1519 * Convert from ssh.com to OpenSSH.
1521 setup_passphrases("sponge2", NULL
);
1522 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1523 "-O", "private-openssh", NULL
);
1526 * Convert from OpenSSH back into a PuTTY key,
1527 * supplying the same comment as we had before we
1528 * started to ensure the comparison works.
1530 setup_passphrases("sponge2", NULL
);
1531 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1532 "-o", tmpfilename2
, NULL
);
1535 * See if the PuTTY key thus generated is the same as
1538 filecmp(filename
, tmpfilename2
,
1539 "p->s->o->p encrypted %s", keytypes
[i
]);
1542 * Finally, do a round-trip conversion between PuTTY
1543 * and ssh.com without involving OpenSSH, to test that
1544 * the key comment is preserved in that case.
1546 setup_passphrases("sponge2", NULL
);
1547 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1549 setup_passphrases("sponge2", NULL
);
1550 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1551 filecmp(filename
, tmpfilename2
,
1552 "p->s->p encrypted %s", keytypes
[i
]);
1556 * Load with the wrong passphrase.
1558 setup_passphrases("sponge8", NULL
);
1559 test(1, "puttygen", "-C", "spurious-new-comment", filename
, NULL
);
1562 * Load a totally bogus file.
1564 setup_passphrases(NULL
);
1565 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename
, NULL
);
1567 printf("%d passes, %d fails\n", passes
, fails
);