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 printf("puttygen: %s\n", ver
);
124 void usage(int standalone
)
127 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
128 " [ -C comment ] [ -P ] [ -q ]\n"
129 " [ -o output-keyfile ] [ -O type | -l | -L"
133 "Use \"puttygen --help\" for more detail.\n");
139 * Help message is an extended version of the usage message. So
140 * start with that, plus a version heading.
145 " -t specify key type when generating (rsa, dsa, rsa1)\n"
146 " -b specify number of bits when generating key\n"
147 " -C change or specify key comment\n"
148 " -P change key passphrase\n"
149 " -q quiet: do not display progress bar\n"
150 " -O specify output type:\n"
151 " private output PuTTY private key format\n"
152 " private-openssh export OpenSSH private key\n"
153 " private-sshcom export ssh.com private key\n"
154 " public standard / ssh.com public key\n"
155 " public-openssh OpenSSH public key\n"
156 " fingerprint output the key fingerprint\n"
157 " -o specify output file\n"
158 " -l equivalent to `-O fingerprint'\n"
159 " -L equivalent to `-O public-openssh'\n"
160 " -p equivalent to `-O public'\n"
164 static int save_ssh2_pubkey(char *filename
, char *comment
,
165 void *v_pub_blob
, int pub_len
)
167 unsigned char *pub_blob
= (unsigned char *)v_pub_blob
;
173 fp
= fopen(filename
, "wb");
179 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
182 fprintf(fp
, "Comment: \"");
183 for (p
= comment
; *p
; p
++) {
184 if (*p
== '\\' || *p
== '\"')
193 while (i
< pub_len
) {
195 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
196 base64_encode_atom(pub_blob
+ i
, n
, buf
);
200 if (++column
>= 16) {
208 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
214 static int move(char *from
, char *to
)
218 ret
= rename(from
, to
);
221 * This OS may require us to remove the original file first.
224 ret
= rename(from
, to
);
227 perror("puttygen: cannot move new file on to old one");
233 static char *blobfp(char *alg
, int bits
, unsigned char *blob
, int bloblen
)
236 unsigned char digest
[16];
237 struct MD5Context md5c
;
241 MD5Update(&md5c
, blob
, bloblen
);
242 MD5Final(digest
, &md5c
);
244 sprintf(buffer
, "%s ", alg
);
246 sprintf(buffer
+ strlen(buffer
), "%d ", bits
);
247 for (i
= 0; i
< 16; i
++)
248 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
251 return dupstr(buffer
);
254 int main(int argc
, char **argv
)
257 Filename
*infilename
= NULL
, *outfilename
= NULL
;
258 enum { NOKEYGEN
, RSA1
, RSA2
, DSA
} keytype
= NOKEYGEN
;
259 char *outfile
= NULL
, *outfiletmp
= NULL
;
260 enum { PRIVATE
, PUBLIC
, PUBLICO
, FP
, OPENSSH
, SSHCOM
} outtype
= PRIVATE
;
262 char *comment
= NULL
, *origcomment
= NULL
;
263 int change_passphrase
= FALSE
;
264 int errs
= FALSE
, nogo
= FALSE
;
265 int intype
= SSH_KEYTYPE_UNOPENABLE
;
267 struct ssh2_userkey
*ssh2key
= NULL
;
268 struct RSAKey
*ssh1key
= NULL
;
269 unsigned char *ssh2blob
= NULL
;
270 char *ssh2alg
= NULL
;
271 const struct ssh_signkey
*ssh2algf
= NULL
;
273 char *passphrase
= NULL
;
275 progfn_t progressfn
= is_interactive() ? progress_update
: no_progress
;
277 /* ------------------------------------------------------------------
278 * Parse the command line to figure out what we've been asked to do.
282 * If run with no arguments at all, print the usage message and
291 * Parse command line arguments.
308 opt
= p
++; /* opt will have _one_ leading - */
309 while (*p
&& *p
!= '=')
310 p
++; /* find end of option */
317 if (!strcmp(opt
, "-help")) {
320 fprintf(stderr
, "puttygen: option `-%s'"
321 " expects no argument\n", opt
);
326 } else if (!strcmp(opt
, "-version")) {
329 fprintf(stderr
, "puttygen: option `-%s'"
330 " expects no argument\n", opt
);
335 } else if (!strcmp(opt
, "-pgpfp")) {
338 fprintf(stderr
, "puttygen: option `-%s'"
339 " expects no argument\n", opt
);
341 /* support --pgpfp for consistency */
347 * For long options requiring an argument, add
348 * code along the lines of
350 * else if (!strcmp(opt, "-output")) {
353 * fprintf(stderr, "puttygen: option `-%s'"
354 * " expects an argument\n", opt);
362 "puttygen: no such option `-%s'\n", opt
);
375 * Option requiring no parameter.
387 change_passphrase
= TRUE
;
399 progressfn
= no_progress
;
409 * Option requiring parameter.
415 fprintf(stderr
, "puttygen: option `-%c' expects a"
420 * Now c is the option and p is the parameter.
424 if (!strcmp(p
, "rsa") || !strcmp(p
, "rsa2"))
425 keytype
= RSA2
, sshver
= 2;
426 else if (!strcmp(p
, "rsa1"))
427 keytype
= RSA1
, sshver
= 1;
428 else if (!strcmp(p
, "dsa") || !strcmp(p
, "dss"))
429 keytype
= DSA
, sshver
= 2;
432 "puttygen: unknown key type `%s'\n", p
);
443 if (!strcmp(p
, "public"))
445 else if (!strcmp(p
, "public-openssh"))
447 else if (!strcmp(p
, "private"))
449 else if (!strcmp(p
, "fingerprint"))
451 else if (!strcmp(p
, "private-openssh"))
452 outtype
= OPENSSH
, sshver
= 2;
453 else if (!strcmp(p
, "private-sshcom"))
454 outtype
= SSHCOM
, sshver
= 2;
457 "puttygen: unknown output type `%s'\n", p
);
465 p
= NULL
; /* prevent continued processing */
469 * Unrecognised option.
472 fprintf(stderr
, "puttygen: no such option `-%c'\n", c
);
478 * A non-option argument.
484 fprintf(stderr
, "puttygen: cannot handle more than one"
497 * If run with at least one argument _but_ not the required
498 * ones, print the usage message and return failure.
500 if (!infile
&& keytype
== NOKEYGEN
) {
505 /* ------------------------------------------------------------------
506 * Figure out further details of exactly what we're going to do.
510 * Bomb out if we've been asked to both load and generate a
513 if (keytype
!= NOKEYGEN
&& infile
) {
514 fprintf(stderr
, "puttygen: cannot both load and generate a key\n");
519 * We must save the private part when generating a new key.
521 if (keytype
!= NOKEYGEN
&&
522 (outtype
!= PRIVATE
&& outtype
!= OPENSSH
&& outtype
!= SSHCOM
)) {
523 fprintf(stderr
, "puttygen: this would generate a new key but "
524 "discard the private part\n");
529 * Analyse the type of the input file, in case this affects our
533 infilename
= filename_from_str(infile
);
535 intype
= key_type(infilename
);
539 * It would be nice here to be able to load _public_
540 * key files, in any of a number of forms, and (a)
541 * convert them to other public key types, (b) print
542 * out their fingerprints. Or, I suppose, for real
543 * orthogonality, (c) change their comment!
545 * In fact this opens some interesting possibilities.
546 * Suppose ssh2_userkey_loadpub() were able to load
547 * public key files as well as extracting the public
548 * key from private ones. And suppose I did the thing
549 * I've been wanting to do, where specifying a
550 * particular private key file for authentication
551 * causes any _other_ key in the agent to be discarded.
552 * Then, if you had an agent forwarded to the machine
553 * you were running Unix PuTTY or Plink on, and you
554 * needed to specify which of the keys in the agent it
555 * should use, you could do that by supplying a
556 * _public_ key file, thus not needing to trust even
557 * your encrypted private key file to the network. Ooh!
560 case SSH_KEYTYPE_UNOPENABLE
:
561 case SSH_KEYTYPE_UNKNOWN
:
562 fprintf(stderr
, "puttygen: unable to load file `%s': %s\n",
563 infile
, key_type_to_str(intype
));
566 case SSH_KEYTYPE_SSH1
:
568 fprintf(stderr
, "puttygen: conversion from SSH-1 to SSH-2 keys"
575 case SSH_KEYTYPE_SSH2
:
576 case SSH_KEYTYPE_OPENSSH
:
577 case SSH_KEYTYPE_SSHCOM
:
579 fprintf(stderr
, "puttygen: conversion from SSH-2 to SSH-1 keys"
589 * Determine the default output file, if none is provided.
591 * This will usually be equal to stdout, except that if the
592 * input and output file formats are the same then the default
593 * output is to overwrite the input.
595 * Also in this code, we bomb out if the input and output file
596 * formats are the same and no other action is performed.
598 if ((intype
== SSH_KEYTYPE_SSH1
&& outtype
== PRIVATE
) ||
599 (intype
== SSH_KEYTYPE_SSH2
&& outtype
== PRIVATE
) ||
600 (intype
== SSH_KEYTYPE_OPENSSH
&& outtype
== OPENSSH
) ||
601 (intype
== SSH_KEYTYPE_SSHCOM
&& outtype
== SSHCOM
)) {
604 outfiletmp
= dupcat(outfile
, ".tmp", NULL
);
607 if (!change_passphrase
&& !comment
) {
608 fprintf(stderr
, "puttygen: this command would perform no useful"
615 * Bomb out rather than automatically choosing to write
616 * a private key file to stdout.
618 if (outtype
==PRIVATE
|| outtype
==OPENSSH
|| outtype
==SSHCOM
) {
619 fprintf(stderr
, "puttygen: need to specify an output file\n");
626 * Figure out whether we need to load the encrypted part of the
627 * key. This will be the case if either (a) we need to write
628 * out a private key format, or (b) the entire input key file
631 if (outtype
== PRIVATE
|| outtype
== OPENSSH
|| outtype
== SSHCOM
||
632 intype
== SSH_KEYTYPE_OPENSSH
|| intype
== SSH_KEYTYPE_SSHCOM
)
633 load_encrypted
= TRUE
;
635 load_encrypted
= FALSE
;
637 /* ------------------------------------------------------------------
638 * Now we're ready to actually do some stuff.
642 * Either load or generate a key.
644 if (keytype
!= NOKEYGEN
) {
646 char default_comment
[80];
648 struct progress prog
;
655 strftime(default_comment
, 30, "dsa-key-%Y%m%d", &tm
);
657 strftime(default_comment
, 30, "rsa-key-%Y%m%d", &tm
);
660 entropy
= get_random_data(bits
/ 8);
662 fprintf(stderr
, "puttygen: failed to collect entropy, "
663 "could not generate key\n");
666 random_add_heavynoise(entropy
, bits
/ 8);
667 smemclr(entropy
, bits
/8);
670 if (keytype
== DSA
) {
671 struct dss_key
*dsskey
= snew(struct dss_key
);
672 dsa_generate(dsskey
, bits
, progressfn
, &prog
);
673 ssh2key
= snew(struct ssh2_userkey
);
674 ssh2key
->data
= dsskey
;
675 ssh2key
->alg
= &ssh_dss
;
678 struct RSAKey
*rsakey
= snew(struct RSAKey
);
679 rsa_generate(rsakey
, bits
, progressfn
, &prog
);
680 rsakey
->comment
= NULL
;
681 if (keytype
== RSA1
) {
684 ssh2key
= snew(struct ssh2_userkey
);
685 ssh2key
->data
= rsakey
;
686 ssh2key
->alg
= &ssh_rsa
;
689 progressfn(&prog
, PROGFN_PROGRESS
, INT_MAX
, -1);
692 ssh2key
->comment
= dupstr(default_comment
);
694 ssh1key
->comment
= dupstr(default_comment
);
697 const char *error
= NULL
;
700 assert(infile
!= NULL
);
703 * Find out whether the input key is encrypted.
705 if (intype
== SSH_KEYTYPE_SSH1
)
706 encrypted
= rsakey_encrypted(infilename
, &origcomment
);
707 else if (intype
== SSH_KEYTYPE_SSH2
)
708 encrypted
= ssh2_userkey_encrypted(infilename
, &origcomment
);
710 encrypted
= import_encrypted(infilename
, intype
, &origcomment
);
713 * If so, ask for a passphrase.
715 if (encrypted
&& load_encrypted
) {
716 prompts_t
*p
= new_prompts(NULL
);
718 p
->to_server
= FALSE
;
719 p
->name
= dupstr("SSH key passphrase");
720 add_prompt(p
, dupstr("Enter passphrase to load key: "), FALSE
);
721 ret
= console_get_userpass_input(p
, NULL
, 0);
725 perror("puttygen: unable to read passphrase");
728 passphrase
= dupstr(p
->prompts
[0]->result
);
738 case SSH_KEYTYPE_SSH1
:
739 ssh1key
= snew(struct RSAKey
);
740 if (!load_encrypted
) {
745 ret
= rsakey_pubblob(infilename
, &vblob
, &bloblen
,
746 &origcomment
, &error
);
747 blob
= (unsigned char *)vblob
;
749 n
= 4; /* skip modulus bits */
751 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
754 error
= "SSH-1 public key blob was too short";
757 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
760 error
= "SSH-1 public key blob was too short";
764 ssh1key
->comment
= dupstr(origcomment
);
765 ssh1key
->private_exponent
= NULL
;
767 ret
= loadrsakey(infilename
, ssh1key
, passphrase
, &error
);
772 error
= "unknown error";
775 case SSH_KEYTYPE_SSH2
:
776 if (!load_encrypted
) {
777 ssh2blob
= ssh2_userkey_loadpub(infilename
, &ssh2alg
,
778 &ssh2bloblen
, NULL
, &error
);
779 ssh2algf
= find_pubkey_alg(ssh2alg
);
781 bits
= ssh2algf
->pubkey_bits(ssh2blob
, ssh2bloblen
);
785 ssh2key
= ssh2_load_userkey(infilename
, passphrase
, &error
);
787 if ((ssh2key
&& ssh2key
!= SSH2_WRONG_PASSPHRASE
) || ssh2blob
)
790 if (ssh2key
== SSH2_WRONG_PASSPHRASE
)
791 error
= "wrong passphrase";
793 error
= "unknown error";
797 case SSH_KEYTYPE_OPENSSH
:
798 case SSH_KEYTYPE_SSHCOM
:
799 ssh2key
= import_ssh2(infilename
, intype
, passphrase
, &error
);
801 if (ssh2key
!= SSH2_WRONG_PASSPHRASE
)
804 error
= "wrong passphrase";
806 error
= "unknown error";
814 fprintf(stderr
, "puttygen: error loading `%s': %s\n",
821 * Change the comment if asked to.
826 sfree(ssh1key
->comment
);
827 ssh1key
->comment
= dupstr(comment
);
830 sfree(ssh2key
->comment
);
831 ssh2key
->comment
= dupstr(comment
);
836 * Prompt for a new passphrase if we have been asked to, or if
837 * we have just generated a key.
839 if (change_passphrase
|| keytype
!= NOKEYGEN
) {
840 prompts_t
*p
= new_prompts(NULL
);
843 p
->to_server
= FALSE
;
844 p
->name
= dupstr("New SSH key passphrase");
845 add_prompt(p
, dupstr("Enter passphrase to save key: "), FALSE
);
846 add_prompt(p
, dupstr("Re-enter passphrase to verify: "), FALSE
);
847 ret
= console_get_userpass_input(p
, NULL
, 0);
851 perror("puttygen: unable to read new passphrase");
854 if (strcmp(p
->prompts
[0]->result
, p
->prompts
[1]->result
)) {
856 fprintf(stderr
, "puttygen: passphrases do not match\n");
860 smemclr(passphrase
, strlen(passphrase
));
863 passphrase
= dupstr(p
->prompts
[0]->result
);
875 * (In the case where outfile and outfiletmp are both NULL,
876 * there is no semantic reason to initialise outfilename at
877 * all; but we have to write _something_ to it or some compiler
878 * will probably complain that it might be used uninitialised.)
881 outfilename
= filename_from_str(outfiletmp
);
883 outfilename
= filename_from_str(outfile ? outfile
: "");
891 ret
= saversakey(outfilename
, ssh1key
, passphrase
);
893 fprintf(stderr
, "puttygen: unable to save SSH-1 private key\n");
898 ret
= ssh2_save_userkey(outfilename
, ssh2key
, passphrase
);
900 fprintf(stderr
, "puttygen: unable to save SSH-2 private key\n");
905 if (!move(outfiletmp
, outfile
))
906 return 1; /* rename failed */
919 fp
= f_open(outfilename
, "w", FALSE
);
922 dec1
= bignum_decimal(ssh1key
->exponent
);
923 dec2
= bignum_decimal(ssh1key
->modulus
);
924 fprintf(fp
, "%d %s %s %s\n", bignum_bitcount(ssh1key
->modulus
),
925 dec1
, dec2
, ssh1key
->comment
);
930 } else if (outtype
== PUBLIC
) {
933 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
936 save_ssh2_pubkey(outfile
, ssh2key ? ssh2key
->comment
: origcomment
,
937 ssh2blob
, ssh2bloblen
);
938 } else if (outtype
== PUBLICO
) {
945 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
950 ssh2alg
= ssh2key
->alg
->name
;
953 comment
= ssh2key
->comment
;
955 comment
= origcomment
;
957 buffer
= snewn(strlen(ssh2alg
) +
958 4 * ((ssh2bloblen
+2) / 3) +
959 strlen(comment
) + 3, char);
960 strcpy(buffer
, ssh2alg
);
961 p
= buffer
+ strlen(buffer
);
964 while (i
< ssh2bloblen
) {
965 int n
= (ssh2bloblen
- i
< 3 ? ssh2bloblen
- i
: 3);
966 base64_encode_atom(ssh2blob
+ i
, n
, p
);
977 fp
= f_open(outfilename
, "w", FALSE
);
980 fprintf(fp
, "%s\n", buffer
);
995 fingerprint
= snewn(128, char);
996 rsa_fingerprint(fingerprint
, 128, ssh1key
);
999 fingerprint
= ssh2key
->alg
->fingerprint(ssh2key
->data
);
1002 fingerprint
= blobfp(ssh2alg
, bits
, ssh2blob
, ssh2bloblen
);
1007 fp
= f_open(outfilename
, "w", FALSE
);
1010 fprintf(fp
, "%s\n", fingerprint
);
1020 assert(sshver
== 2);
1022 ret
= export_ssh2(outfilename
, outtype
, ssh2key
, passphrase
);
1024 fprintf(stderr
, "puttygen: unable to export key\n");
1028 if (!move(outfiletmp
, outfile
))
1029 return 1; /* rename failed */
1035 smemclr(passphrase
, strlen(passphrase
));
1040 freersakey(ssh1key
);
1042 ssh2key
->alg
->freekey(ssh2key
->data
);
1057 void setup_passphrases(char *first
, ...)
1064 prompts
[nprompts
++] = first
;
1065 va_start(ap
, first
);
1066 while ((next
= va_arg(ap
, char *)) != NULL
) {
1067 assert(nprompts
< lenof(prompts
));
1068 prompts
[nprompts
++] = next
;
1074 void test(int retval
, ...)
1081 va_start(ap
, retval
);
1082 while (va_arg(ap
, char *) != NULL
)
1086 argv
= snewn(argc
+1, char *);
1087 va_start(ap
, retval
);
1088 for (i
= 0; i
<= argc
; i
++)
1089 argv
[i
] = va_arg(ap
, char *);
1093 ret
= cmdgen_main(argc
, argv
);
1095 if (ret
!= retval
) {
1096 printf("FAILED retval (exp %d got %d):", retval
, ret
);
1097 for (i
= 0; i
< argc
; i
++)
1098 printf(" %s", argv
[i
]);
1101 } else if (promptsgot
!= nprompts
) {
1102 printf("FAILED nprompts (exp %d got %d):", nprompts
, promptsgot
);
1103 for (i
= 0; i
< argc
; i
++)
1104 printf(" %s", argv
[i
]);
1112 void filecmp(char *file1
, char *file2
, char *fmt
, ...)
1115 * Ideally I should do file comparison myself, to maximise the
1116 * portability of this test suite once this application begins
1117 * running on non-Unix platforms. For the moment, though,
1118 * calling Unix diff is perfectly adequate.
1123 buf
= dupprintf("diff -q '%s' '%s'", file1
, file2
);
1130 printf("FAILED diff (ret=%d): ", ret
);
1143 char *cleanup_fp(char *s
)
1147 if (!strncmp(s
, "ssh-", 4)) {
1148 s
+= strcspn(s
, " \n\t");
1149 s
+= strspn(s
, " \n\t");
1153 s
+= strcspn(s
, " \n\t");
1154 s
+= strspn(s
, " \n\t");
1155 s
+= strcspn(s
, " \n\t");
1157 return dupprintf("%.*s", s
- p
, p
);
1160 char *get_fp(char *filename
)
1163 char buf
[256], *ret
;
1165 fp
= fopen(filename
, "r");
1168 ret
= fgets(buf
, sizeof(buf
), fp
);
1172 return cleanup_fp(buf
);
1175 void check_fp(char *filename
, char *fp
, char *fmt
, ...)
1182 newfp
= get_fp(filename
);
1184 if (!strcmp(fp
, newfp
)) {
1189 printf("FAILED check_fp ['%s' != '%s']: ", newfp
, fp
);
1203 int main(int argc
, char **argv
)
1206 static char *const keytypes
[] = { "rsa1", "dsa", "rsa" };
1209 * Even when this thing is compiled for automatic test mode,
1210 * it's helpful to be able to invoke it with command-line
1211 * options for _manual_ tests.
1214 return cmdgen_main(argc
, argv
);
1218 for (i
= 0; i
< lenof(keytypes
); i
++) {
1219 char filename
[128], osfilename
[128], scfilename
[128];
1220 char pubfilename
[128], tmpfilename1
[128], tmpfilename2
[128];
1223 sprintf(filename
, "test-%s.ppk", keytypes
[i
]);
1224 sprintf(pubfilename
, "test-%s.pub", keytypes
[i
]);
1225 sprintf(osfilename
, "test-%s.os", keytypes
[i
]);
1226 sprintf(scfilename
, "test-%s.sc", keytypes
[i
]);
1227 sprintf(tmpfilename1
, "test-%s.tmp1", keytypes
[i
]);
1228 sprintf(tmpfilename2
, "test-%s.tmp2", keytypes
[i
]);
1231 * Create an encrypted key.
1233 setup_passphrases("sponge", "sponge", NULL
);
1234 test(0, "puttygen", "-t", keytypes
[i
], "-o", filename
, NULL
);
1237 * List the public key in OpenSSH format.
1239 setup_passphrases(NULL
);
1240 test(0, "puttygen", "-L", filename
, "-o", pubfilename
, NULL
);
1244 sprintf(cmdbuf
, "ssh-keygen -l -f '%s' > '%s'",
1245 pubfilename
, tmpfilename1
);
1246 if (system(cmdbuf
) ||
1247 (fp
= get_fp(tmpfilename1
)) == NULL
) {
1248 printf("UNABLE to test fingerprint matching against OpenSSH");
1253 * List the public key in IETF/ssh.com format.
1255 setup_passphrases(NULL
);
1256 test(0, "puttygen", "-p", filename
, NULL
);
1259 * List the fingerprint of the key.
1261 setup_passphrases(NULL
);
1262 test(0, "puttygen", "-l", filename
, "-o", tmpfilename1
, NULL
);
1265 * If we can't test fingerprints against OpenSSH, we
1266 * can at the very least test equality of all the
1267 * fingerprints we generate of this key throughout
1270 fp
= get_fp(tmpfilename1
);
1272 check_fp(tmpfilename1
, fp
, "%s initial fp", keytypes
[i
]);
1276 * Change the comment of the key; this _does_ require a
1277 * passphrase owing to the tamperproofing.
1279 * NOTE: In SSH-1, this only requires a passphrase because
1280 * of inadequacies of the loading and saving mechanisms. In
1281 * _principle_, it should be perfectly possible to modify
1282 * the comment on an SSH-1 key without requiring a
1283 * passphrase; the only reason I can't do it is because my
1284 * loading and saving mechanisms don't include a method of
1285 * loading all the key data without also trying to decrypt
1286 * the private section.
1288 * I don't consider this to be a problem worth solving,
1289 * because (a) to fix it would probably end up bloating
1290 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1291 * it shouldn't be highly significant. If it seriously
1292 * bothers anyone then perhaps I _might_ be persuadable.
1294 setup_passphrases("sponge", NULL
);
1295 test(0, "puttygen", "-C", "new-comment", filename
, NULL
);
1298 * Change the passphrase to nothing.
1300 setup_passphrases("sponge", "", "", NULL
);
1301 test(0, "puttygen", "-P", filename
, NULL
);
1304 * Change the comment of the key again; this time we expect no
1305 * passphrase to be required.
1307 setup_passphrases(NULL
);
1308 test(0, "puttygen", "-C", "new-comment-2", filename
, NULL
);
1311 * Export the private key into OpenSSH format; no passphrase
1312 * should be required since the key is currently unencrypted.
1313 * For RSA1 keys, this should give an error.
1315 setup_passphrases(NULL
);
1316 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1321 * List the fingerprint of the OpenSSH-formatted key.
1323 setup_passphrases(NULL
);
1324 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1325 check_fp(tmpfilename1
, fp
, "%s openssh clear fp", keytypes
[i
]);
1328 * List the public half of the OpenSSH-formatted key in
1331 setup_passphrases(NULL
);
1332 test(0, "puttygen", "-L", osfilename
, NULL
);
1335 * List the public half of the OpenSSH-formatted key in
1336 * IETF/ssh.com format.
1338 setup_passphrases(NULL
);
1339 test(0, "puttygen", "-p", osfilename
, NULL
);
1343 * Export the private key into ssh.com format; no passphrase
1344 * should be required since the key is currently unencrypted.
1345 * For RSA1 keys, this should give an error.
1347 setup_passphrases(NULL
);
1348 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1353 * List the fingerprint of the ssh.com-formatted key.
1355 setup_passphrases(NULL
);
1356 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1357 check_fp(tmpfilename1
, fp
, "%s ssh.com clear fp", keytypes
[i
]);
1360 * List the public half of the ssh.com-formatted key in
1363 setup_passphrases(NULL
);
1364 test(0, "puttygen", "-L", scfilename
, NULL
);
1367 * List the public half of the ssh.com-formatted key in
1368 * IETF/ssh.com format.
1370 setup_passphrases(NULL
);
1371 test(0, "puttygen", "-p", scfilename
, NULL
);
1376 * Convert from OpenSSH into ssh.com.
1378 setup_passphrases(NULL
);
1379 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1380 "-O", "private-sshcom", NULL
);
1383 * Convert from ssh.com 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->o->s->p clear %s", keytypes
[i
]);
1399 * Convert from ssh.com to OpenSSH.
1401 setup_passphrases(NULL
);
1402 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1403 "-O", "private-openssh", NULL
);
1406 * Convert from OpenSSH back into a PuTTY key,
1407 * supplying the same comment as we had before we
1408 * started to ensure the comparison works.
1410 setup_passphrases(NULL
);
1411 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1412 "-o", tmpfilename2
, NULL
);
1415 * See if the PuTTY key thus generated is the same as
1418 filecmp(filename
, tmpfilename2
,
1419 "p->s->o->p clear %s", keytypes
[i
]);
1422 * Finally, do a round-trip conversion between PuTTY
1423 * and ssh.com without involving OpenSSH, to test that
1424 * the key comment is preserved in that case.
1426 setup_passphrases(NULL
);
1427 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1429 setup_passphrases(NULL
);
1430 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1431 filecmp(filename
, tmpfilename2
,
1432 "p->s->p clear %s", keytypes
[i
]);
1436 * Check that mismatched passphrases cause an error.
1438 setup_passphrases("sponge2", "sponge3", NULL
);
1439 test(1, "puttygen", "-P", filename
, NULL
);
1442 * Put a passphrase back on.
1444 setup_passphrases("sponge2", "sponge2", NULL
);
1445 test(0, "puttygen", "-P", filename
, NULL
);
1448 * Export the private key into OpenSSH format, this time
1449 * while encrypted. For RSA1 keys, this should give an
1453 setup_passphrases(NULL
); /* error, hence no passphrase read */
1455 setup_passphrases("sponge2", NULL
);
1456 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1461 * List the fingerprint of the OpenSSH-formatted key.
1463 setup_passphrases("sponge2", NULL
);
1464 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1465 check_fp(tmpfilename1
, fp
, "%s openssh encrypted fp", keytypes
[i
]);
1468 * List the public half of the OpenSSH-formatted key in
1471 setup_passphrases("sponge2", NULL
);
1472 test(0, "puttygen", "-L", osfilename
, NULL
);
1475 * List the public half of the OpenSSH-formatted key in
1476 * IETF/ssh.com format.
1478 setup_passphrases("sponge2", NULL
);
1479 test(0, "puttygen", "-p", osfilename
, NULL
);
1483 * Export the private key into ssh.com format, this time
1484 * while encrypted. For RSA1 keys, this should give an
1488 setup_passphrases(NULL
); /* error, hence no passphrase read */
1490 setup_passphrases("sponge2", NULL
);
1491 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1496 * List the fingerprint of the ssh.com-formatted key.
1498 setup_passphrases("sponge2", NULL
);
1499 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1500 check_fp(tmpfilename1
, fp
, "%s ssh.com encrypted fp", keytypes
[i
]);
1503 * List the public half of the ssh.com-formatted key in
1506 setup_passphrases("sponge2", NULL
);
1507 test(0, "puttygen", "-L", scfilename
, NULL
);
1510 * List the public half of the ssh.com-formatted key in
1511 * IETF/ssh.com format.
1513 setup_passphrases("sponge2", NULL
);
1514 test(0, "puttygen", "-p", scfilename
, NULL
);
1519 * Convert from OpenSSH into ssh.com.
1521 setup_passphrases("sponge2", NULL
);
1522 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1523 "-O", "private-sshcom", NULL
);
1526 * Convert from ssh.com 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->o->s->p encrypted %s", keytypes
[i
]);
1542 * Convert from ssh.com to OpenSSH.
1544 setup_passphrases("sponge2", NULL
);
1545 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1546 "-O", "private-openssh", NULL
);
1549 * Convert from OpenSSH back into a PuTTY key,
1550 * supplying the same comment as we had before we
1551 * started to ensure the comparison works.
1553 setup_passphrases("sponge2", NULL
);
1554 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1555 "-o", tmpfilename2
, NULL
);
1558 * See if the PuTTY key thus generated is the same as
1561 filecmp(filename
, tmpfilename2
,
1562 "p->s->o->p encrypted %s", keytypes
[i
]);
1565 * Finally, do a round-trip conversion between PuTTY
1566 * and ssh.com without involving OpenSSH, to test that
1567 * the key comment is preserved in that case.
1569 setup_passphrases("sponge2", NULL
);
1570 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1572 setup_passphrases("sponge2", NULL
);
1573 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1574 filecmp(filename
, tmpfilename2
,
1575 "p->s->p encrypted %s", keytypes
[i
]);
1579 * Load with the wrong passphrase.
1581 setup_passphrases("sponge8", NULL
);
1582 test(1, "puttygen", "-C", "spurious-new-comment", filename
, NULL
);
1585 * Load a totally bogus file.
1587 setup_passphrases(NULL
);
1588 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename
, NULL
);
1590 printf("%d passes, %d fails\n", passes
, fails
);