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((unsigned char)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
)
260 Filename
*infilename
= NULL
, *outfilename
= NULL
;
261 enum { NOKEYGEN
, RSA1
, RSA2
, DSA
} keytype
= NOKEYGEN
;
262 char *outfile
= NULL
, *outfiletmp
= NULL
;
263 enum { PRIVATE
, PUBLIC
, PUBLICO
, FP
, OPENSSH
, SSHCOM
} outtype
= PRIVATE
;
265 char *comment
= NULL
, *origcomment
= NULL
;
266 int change_passphrase
= FALSE
;
267 int errs
= FALSE
, nogo
= FALSE
;
268 int intype
= SSH_KEYTYPE_UNOPENABLE
;
270 struct ssh2_userkey
*ssh2key
= NULL
;
271 struct RSAKey
*ssh1key
= NULL
;
272 unsigned char *ssh2blob
= NULL
;
273 char *ssh2alg
= NULL
;
274 const struct ssh_signkey
*ssh2algf
= NULL
;
276 char *passphrase
= NULL
;
278 progfn_t progressfn
= is_interactive() ? progress_update
: no_progress
;
280 /* ------------------------------------------------------------------
281 * Parse the command line to figure out what we've been asked to do.
285 * If run with no arguments at all, print the usage message and
294 * Parse command line arguments.
311 opt
= p
++; /* opt will have _one_ leading - */
312 while (*p
&& *p
!= '=')
313 p
++; /* find end of option */
320 if (!strcmp(opt
, "-help")) {
323 fprintf(stderr
, "puttygen: option `-%s'"
324 " expects no argument\n", opt
);
329 } else if (!strcmp(opt
, "-version")) {
332 fprintf(stderr
, "puttygen: option `-%s'"
333 " expects no argument\n", opt
);
338 } else if (!strcmp(opt
, "-pgpfp")) {
341 fprintf(stderr
, "puttygen: option `-%s'"
342 " expects no argument\n", opt
);
344 /* support --pgpfp for consistency */
350 * For long options requiring an argument, add
351 * code along the lines of
353 * else if (!strcmp(opt, "-output")) {
356 * fprintf(stderr, "puttygen: option `-%s'"
357 * " expects an argument\n", opt);
365 "puttygen: no such option `-%s'\n", opt
);
378 * Option requiring no parameter.
390 change_passphrase
= TRUE
;
402 progressfn
= no_progress
;
412 * Option requiring parameter.
418 fprintf(stderr
, "puttygen: option `-%c' expects a"
423 * Now c is the option and p is the parameter.
427 if (!strcmp(p
, "rsa") || !strcmp(p
, "rsa2"))
428 keytype
= RSA2
, sshver
= 2;
429 else if (!strcmp(p
, "rsa1"))
430 keytype
= RSA1
, sshver
= 1;
431 else if (!strcmp(p
, "dsa") || !strcmp(p
, "dss"))
432 keytype
= DSA
, sshver
= 2;
435 "puttygen: unknown key type `%s'\n", p
);
446 if (!strcmp(p
, "public"))
448 else if (!strcmp(p
, "public-openssh"))
450 else if (!strcmp(p
, "private"))
452 else if (!strcmp(p
, "fingerprint"))
454 else if (!strcmp(p
, "private-openssh"))
455 outtype
= OPENSSH
, sshver
= 2;
456 else if (!strcmp(p
, "private-sshcom"))
457 outtype
= SSHCOM
, sshver
= 2;
460 "puttygen: unknown output type `%s'\n", p
);
468 p
= NULL
; /* prevent continued processing */
472 * Unrecognised option.
475 fprintf(stderr
, "puttygen: no such option `-%c'\n", c
);
481 * A non-option argument.
487 fprintf(stderr
, "puttygen: cannot handle more than one"
500 * If run with at least one argument _but_ not the required
501 * ones, print the usage message and return failure.
503 if (!infile
&& keytype
== NOKEYGEN
) {
508 /* ------------------------------------------------------------------
509 * Figure out further details of exactly what we're going to do.
513 * Bomb out if we've been asked to both load and generate a
516 if (keytype
!= NOKEYGEN
&& infile
) {
517 fprintf(stderr
, "puttygen: cannot both load and generate a key\n");
522 * We must save the private part when generating a new key.
524 if (keytype
!= NOKEYGEN
&&
525 (outtype
!= PRIVATE
&& outtype
!= OPENSSH
&& outtype
!= SSHCOM
)) {
526 fprintf(stderr
, "puttygen: this would generate a new key but "
527 "discard the private part\n");
532 * Analyse the type of the input file, in case this affects our
536 infilename
= filename_from_str(infile
);
538 intype
= key_type(infilename
);
542 * It would be nice here to be able to load _public_
543 * key files, in any of a number of forms, and (a)
544 * convert them to other public key types, (b) print
545 * out their fingerprints. Or, I suppose, for real
546 * orthogonality, (c) change their comment!
548 * In fact this opens some interesting possibilities.
549 * Suppose ssh2_userkey_loadpub() were able to load
550 * public key files as well as extracting the public
551 * key from private ones. And suppose I did the thing
552 * I've been wanting to do, where specifying a
553 * particular private key file for authentication
554 * causes any _other_ key in the agent to be discarded.
555 * Then, if you had an agent forwarded to the machine
556 * you were running Unix PuTTY or Plink on, and you
557 * needed to specify which of the keys in the agent it
558 * should use, you could do that by supplying a
559 * _public_ key file, thus not needing to trust even
560 * your encrypted private key file to the network. Ooh!
563 case SSH_KEYTYPE_UNOPENABLE
:
564 case SSH_KEYTYPE_UNKNOWN
:
565 fprintf(stderr
, "puttygen: unable to load file `%s': %s\n",
566 infile
, key_type_to_str(intype
));
569 case SSH_KEYTYPE_SSH1
:
571 fprintf(stderr
, "puttygen: conversion from SSH-1 to SSH-2 keys"
578 case SSH_KEYTYPE_SSH2
:
579 case SSH_KEYTYPE_OPENSSH
:
580 case SSH_KEYTYPE_SSHCOM
:
582 fprintf(stderr
, "puttygen: conversion from SSH-2 to SSH-1 keys"
592 * Determine the default output file, if none is provided.
594 * This will usually be equal to stdout, except that if the
595 * input and output file formats are the same then the default
596 * output is to overwrite the input.
598 * Also in this code, we bomb out if the input and output file
599 * formats are the same and no other action is performed.
601 if ((intype
== SSH_KEYTYPE_SSH1
&& outtype
== PRIVATE
) ||
602 (intype
== SSH_KEYTYPE_SSH2
&& outtype
== PRIVATE
) ||
603 (intype
== SSH_KEYTYPE_OPENSSH
&& outtype
== OPENSSH
) ||
604 (intype
== SSH_KEYTYPE_SSHCOM
&& outtype
== SSHCOM
)) {
607 outfiletmp
= dupcat(outfile
, ".tmp", NULL
);
610 if (!change_passphrase
&& !comment
) {
611 fprintf(stderr
, "puttygen: this command would perform no useful"
618 * Bomb out rather than automatically choosing to write
619 * a private key file to stdout.
621 if (outtype
==PRIVATE
|| outtype
==OPENSSH
|| outtype
==SSHCOM
) {
622 fprintf(stderr
, "puttygen: need to specify an output file\n");
629 * Figure out whether we need to load the encrypted part of the
630 * key. This will be the case if either (a) we need to write
631 * out a private key format, or (b) the entire input key file
634 if (outtype
== PRIVATE
|| outtype
== OPENSSH
|| outtype
== SSHCOM
||
635 intype
== SSH_KEYTYPE_OPENSSH
|| intype
== SSH_KEYTYPE_SSHCOM
)
636 load_encrypted
= TRUE
;
638 load_encrypted
= FALSE
;
640 /* ------------------------------------------------------------------
641 * Now we're ready to actually do some stuff.
645 * Either load or generate a key.
647 if (keytype
!= NOKEYGEN
) {
649 char default_comment
[80];
651 struct progress prog
;
658 strftime(default_comment
, 30, "dsa-key-%Y%m%d", &tm
);
660 strftime(default_comment
, 30, "rsa-key-%Y%m%d", &tm
);
663 entropy
= get_random_data(bits
/ 8);
665 fprintf(stderr
, "puttygen: failed to collect entropy, "
666 "could not generate key\n");
669 random_add_heavynoise(entropy
, bits
/ 8);
670 smemclr(entropy
, bits
/8);
673 if (keytype
== DSA
) {
674 struct dss_key
*dsskey
= snew(struct dss_key
);
675 dsa_generate(dsskey
, bits
, progressfn
, &prog
);
676 ssh2key
= snew(struct ssh2_userkey
);
677 ssh2key
->data
= dsskey
;
678 ssh2key
->alg
= &ssh_dss
;
681 struct RSAKey
*rsakey
= snew(struct RSAKey
);
682 rsa_generate(rsakey
, bits
, progressfn
, &prog
);
683 rsakey
->comment
= NULL
;
684 if (keytype
== RSA1
) {
687 ssh2key
= snew(struct ssh2_userkey
);
688 ssh2key
->data
= rsakey
;
689 ssh2key
->alg
= &ssh_rsa
;
692 progressfn(&prog
, PROGFN_PROGRESS
, INT_MAX
, -1);
695 ssh2key
->comment
= dupstr(default_comment
);
697 ssh1key
->comment
= dupstr(default_comment
);
700 const char *error
= NULL
;
703 assert(infile
!= NULL
);
706 * Find out whether the input key is encrypted.
708 if (intype
== SSH_KEYTYPE_SSH1
)
709 encrypted
= rsakey_encrypted(infilename
, &origcomment
);
710 else if (intype
== SSH_KEYTYPE_SSH2
)
711 encrypted
= ssh2_userkey_encrypted(infilename
, &origcomment
);
713 encrypted
= import_encrypted(infilename
, intype
, &origcomment
);
716 * If so, ask for a passphrase.
718 if (encrypted
&& load_encrypted
) {
719 prompts_t
*p
= new_prompts(NULL
);
721 p
->to_server
= FALSE
;
722 p
->name
= dupstr("SSH key passphrase");
723 add_prompt(p
, dupstr("Enter passphrase to load key: "), FALSE
);
724 ret
= console_get_userpass_input(p
, NULL
, 0);
728 perror("puttygen: unable to read passphrase");
731 passphrase
= dupstr(p
->prompts
[0]->result
);
741 case SSH_KEYTYPE_SSH1
:
742 ssh1key
= snew(struct RSAKey
);
743 if (!load_encrypted
) {
748 ret
= rsakey_pubblob(infilename
, &vblob
, &bloblen
,
749 &origcomment
, &error
);
750 blob
= (unsigned char *)vblob
;
752 n
= 4; /* skip modulus bits */
754 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
757 error
= "SSH-1 public key blob was too short";
760 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
763 error
= "SSH-1 public key blob was too short";
767 ssh1key
->comment
= dupstr(origcomment
);
768 ssh1key
->private_exponent
= NULL
;
770 ret
= loadrsakey(infilename
, ssh1key
, passphrase
, &error
);
775 error
= "unknown error";
778 case SSH_KEYTYPE_SSH2
:
779 if (!load_encrypted
) {
780 ssh2blob
= ssh2_userkey_loadpub(infilename
, &ssh2alg
,
781 &ssh2bloblen
, NULL
, &error
);
782 ssh2algf
= find_pubkey_alg(ssh2alg
);
784 bits
= ssh2algf
->pubkey_bits(ssh2blob
, ssh2bloblen
);
788 ssh2key
= ssh2_load_userkey(infilename
, passphrase
, &error
);
790 if ((ssh2key
&& ssh2key
!= SSH2_WRONG_PASSPHRASE
) || ssh2blob
)
793 if (ssh2key
== SSH2_WRONG_PASSPHRASE
)
794 error
= "wrong passphrase";
796 error
= "unknown error";
800 case SSH_KEYTYPE_OPENSSH
:
801 case SSH_KEYTYPE_SSHCOM
:
802 ssh2key
= import_ssh2(infilename
, intype
, passphrase
, &error
);
804 if (ssh2key
!= SSH2_WRONG_PASSPHRASE
)
807 error
= "wrong passphrase";
809 error
= "unknown error";
817 fprintf(stderr
, "puttygen: error loading `%s': %s\n",
824 * Change the comment if asked to.
829 sfree(ssh1key
->comment
);
830 ssh1key
->comment
= dupstr(comment
);
833 sfree(ssh2key
->comment
);
834 ssh2key
->comment
= dupstr(comment
);
839 * Prompt for a new passphrase if we have been asked to, or if
840 * we have just generated a key.
842 if (change_passphrase
|| keytype
!= NOKEYGEN
) {
843 prompts_t
*p
= new_prompts(NULL
);
846 p
->to_server
= FALSE
;
847 p
->name
= dupstr("New SSH key passphrase");
848 add_prompt(p
, dupstr("Enter passphrase to save key: "), FALSE
);
849 add_prompt(p
, dupstr("Re-enter passphrase to verify: "), FALSE
);
850 ret
= console_get_userpass_input(p
, NULL
, 0);
854 perror("puttygen: unable to read new passphrase");
857 if (strcmp(p
->prompts
[0]->result
, p
->prompts
[1]->result
)) {
859 fprintf(stderr
, "puttygen: passphrases do not match\n");
863 smemclr(passphrase
, strlen(passphrase
));
866 passphrase
= dupstr(p
->prompts
[0]->result
);
878 * (In the case where outfile and outfiletmp are both NULL,
879 * there is no semantic reason to initialise outfilename at
880 * all; but we have to write _something_ to it or some compiler
881 * will probably complain that it might be used uninitialised.)
884 outfilename
= filename_from_str(outfiletmp
);
886 outfilename
= filename_from_str(outfile ? outfile
: "");
894 ret
= saversakey(outfilename
, ssh1key
, passphrase
);
896 fprintf(stderr
, "puttygen: unable to save SSH-1 private key\n");
901 ret
= ssh2_save_userkey(outfilename
, ssh2key
, passphrase
);
903 fprintf(stderr
, "puttygen: unable to save SSH-2 private key\n");
908 if (!move(outfiletmp
, outfile
))
909 return 1; /* rename failed */
922 fp
= f_open(outfilename
, "w", FALSE
);
925 dec1
= bignum_decimal(ssh1key
->exponent
);
926 dec2
= bignum_decimal(ssh1key
->modulus
);
927 fprintf(fp
, "%d %s %s %s\n", bignum_bitcount(ssh1key
->modulus
),
928 dec1
, dec2
, ssh1key
->comment
);
933 } else if (outtype
== PUBLIC
) {
936 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
939 save_ssh2_pubkey(outfile
, ssh2key ? ssh2key
->comment
: origcomment
,
940 ssh2blob
, ssh2bloblen
);
941 } else if (outtype
== PUBLICO
) {
948 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
953 ssh2alg
= ssh2key
->alg
->name
;
956 comment
= ssh2key
->comment
;
958 comment
= origcomment
;
960 buffer
= snewn(strlen(ssh2alg
) +
961 4 * ((ssh2bloblen
+2) / 3) +
962 strlen(comment
) + 3, char);
963 strcpy(buffer
, ssh2alg
);
964 p
= buffer
+ strlen(buffer
);
967 while (i
< ssh2bloblen
) {
968 int n
= (ssh2bloblen
- i
< 3 ? ssh2bloblen
- i
: 3);
969 base64_encode_atom(ssh2blob
+ i
, n
, p
);
980 fp
= f_open(outfilename
, "w", FALSE
);
983 fprintf(fp
, "%s\n", buffer
);
998 fingerprint
= snewn(128, char);
999 rsa_fingerprint(fingerprint
, 128, ssh1key
);
1002 fingerprint
= ssh2key
->alg
->fingerprint(ssh2key
->data
);
1005 fingerprint
= blobfp(ssh2alg
, bits
, ssh2blob
, ssh2bloblen
);
1010 fp
= f_open(outfilename
, "w", FALSE
);
1013 fprintf(fp
, "%s\n", fingerprint
);
1023 assert(sshver
== 2);
1025 ret
= export_ssh2(outfilename
, outtype
, ssh2key
, passphrase
);
1027 fprintf(stderr
, "puttygen: unable to export key\n");
1031 if (!move(outfiletmp
, outfile
))
1032 return 1; /* rename failed */
1038 smemclr(passphrase
, strlen(passphrase
));
1043 freersakey(ssh1key
);
1045 ssh2key
->alg
->freekey(ssh2key
->data
);
1060 void setup_passphrases(char *first
, ...)
1067 prompts
[nprompts
++] = first
;
1068 va_start(ap
, first
);
1069 while ((next
= va_arg(ap
, char *)) != NULL
) {
1070 assert(nprompts
< lenof(prompts
));
1071 prompts
[nprompts
++] = next
;
1077 void test(int retval
, ...)
1084 va_start(ap
, retval
);
1085 while (va_arg(ap
, char *) != NULL
)
1089 argv
= snewn(argc
+1, char *);
1090 va_start(ap
, retval
);
1091 for (i
= 0; i
<= argc
; i
++)
1092 argv
[i
] = va_arg(ap
, char *);
1096 ret
= cmdgen_main(argc
, argv
);
1098 if (ret
!= retval
) {
1099 printf("FAILED retval (exp %d got %d):", retval
, ret
);
1100 for (i
= 0; i
< argc
; i
++)
1101 printf(" %s", argv
[i
]);
1104 } else if (promptsgot
!= nprompts
) {
1105 printf("FAILED nprompts (exp %d got %d):", nprompts
, promptsgot
);
1106 for (i
= 0; i
< argc
; i
++)
1107 printf(" %s", argv
[i
]);
1115 void filecmp(char *file1
, char *file2
, char *fmt
, ...)
1118 * Ideally I should do file comparison myself, to maximise the
1119 * portability of this test suite once this application begins
1120 * running on non-Unix platforms. For the moment, though,
1121 * calling Unix diff is perfectly adequate.
1126 buf
= dupprintf("diff -q '%s' '%s'", file1
, file2
);
1133 printf("FAILED diff (ret=%d): ", ret
);
1146 char *cleanup_fp(char *s
)
1150 if (!strncmp(s
, "ssh-", 4)) {
1151 s
+= strcspn(s
, " \n\t");
1152 s
+= strspn(s
, " \n\t");
1156 s
+= strcspn(s
, " \n\t");
1157 s
+= strspn(s
, " \n\t");
1158 s
+= strcspn(s
, " \n\t");
1160 return dupprintf("%.*s", s
- p
, p
);
1163 char *get_fp(char *filename
)
1166 char buf
[256], *ret
;
1168 fp
= fopen(filename
, "r");
1171 ret
= fgets(buf
, sizeof(buf
), fp
);
1175 return cleanup_fp(buf
);
1178 void check_fp(char *filename
, char *fp
, char *fmt
, ...)
1185 newfp
= get_fp(filename
);
1187 if (!strcmp(fp
, newfp
)) {
1192 printf("FAILED check_fp ['%s' != '%s']: ", newfp
, fp
);
1206 int main(int argc
, char **argv
)
1209 static char *const keytypes
[] = { "rsa1", "dsa", "rsa" };
1212 * Even when this thing is compiled for automatic test mode,
1213 * it's helpful to be able to invoke it with command-line
1214 * options for _manual_ tests.
1217 return cmdgen_main(argc
, argv
);
1221 for (i
= 0; i
< lenof(keytypes
); i
++) {
1222 char filename
[128], osfilename
[128], scfilename
[128];
1223 char pubfilename
[128], tmpfilename1
[128], tmpfilename2
[128];
1226 sprintf(filename
, "test-%s.ppk", keytypes
[i
]);
1227 sprintf(pubfilename
, "test-%s.pub", keytypes
[i
]);
1228 sprintf(osfilename
, "test-%s.os", keytypes
[i
]);
1229 sprintf(scfilename
, "test-%s.sc", keytypes
[i
]);
1230 sprintf(tmpfilename1
, "test-%s.tmp1", keytypes
[i
]);
1231 sprintf(tmpfilename2
, "test-%s.tmp2", keytypes
[i
]);
1234 * Create an encrypted key.
1236 setup_passphrases("sponge", "sponge", NULL
);
1237 test(0, "puttygen", "-t", keytypes
[i
], "-o", filename
, NULL
);
1240 * List the public key in OpenSSH format.
1242 setup_passphrases(NULL
);
1243 test(0, "puttygen", "-L", filename
, "-o", pubfilename
, NULL
);
1247 sprintf(cmdbuf
, "ssh-keygen -l -f '%s' > '%s'",
1248 pubfilename
, tmpfilename1
);
1249 if (system(cmdbuf
) ||
1250 (fp
= get_fp(tmpfilename1
)) == NULL
) {
1251 printf("UNABLE to test fingerprint matching against OpenSSH");
1256 * List the public key in IETF/ssh.com format.
1258 setup_passphrases(NULL
);
1259 test(0, "puttygen", "-p", filename
, NULL
);
1262 * List the fingerprint of the key.
1264 setup_passphrases(NULL
);
1265 test(0, "puttygen", "-l", filename
, "-o", tmpfilename1
, NULL
);
1268 * If we can't test fingerprints against OpenSSH, we
1269 * can at the very least test equality of all the
1270 * fingerprints we generate of this key throughout
1273 fp
= get_fp(tmpfilename1
);
1275 check_fp(tmpfilename1
, fp
, "%s initial fp", keytypes
[i
]);
1279 * Change the comment of the key; this _does_ require a
1280 * passphrase owing to the tamperproofing.
1282 * NOTE: In SSH-1, this only requires a passphrase because
1283 * of inadequacies of the loading and saving mechanisms. In
1284 * _principle_, it should be perfectly possible to modify
1285 * the comment on an SSH-1 key without requiring a
1286 * passphrase; the only reason I can't do it is because my
1287 * loading and saving mechanisms don't include a method of
1288 * loading all the key data without also trying to decrypt
1289 * the private section.
1291 * I don't consider this to be a problem worth solving,
1292 * because (a) to fix it would probably end up bloating
1293 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1294 * it shouldn't be highly significant. If it seriously
1295 * bothers anyone then perhaps I _might_ be persuadable.
1297 setup_passphrases("sponge", NULL
);
1298 test(0, "puttygen", "-C", "new-comment", filename
, NULL
);
1301 * Change the passphrase to nothing.
1303 setup_passphrases("sponge", "", "", NULL
);
1304 test(0, "puttygen", "-P", filename
, NULL
);
1307 * Change the comment of the key again; this time we expect no
1308 * passphrase to be required.
1310 setup_passphrases(NULL
);
1311 test(0, "puttygen", "-C", "new-comment-2", filename
, NULL
);
1314 * Export the private key into OpenSSH format; no passphrase
1315 * should be required since the key is currently unencrypted.
1316 * For RSA1 keys, this should give an error.
1318 setup_passphrases(NULL
);
1319 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1324 * List the fingerprint of the OpenSSH-formatted key.
1326 setup_passphrases(NULL
);
1327 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1328 check_fp(tmpfilename1
, fp
, "%s openssh clear fp", keytypes
[i
]);
1331 * List the public half of the OpenSSH-formatted key in
1334 setup_passphrases(NULL
);
1335 test(0, "puttygen", "-L", osfilename
, NULL
);
1338 * List the public half of the OpenSSH-formatted key in
1339 * IETF/ssh.com format.
1341 setup_passphrases(NULL
);
1342 test(0, "puttygen", "-p", osfilename
, NULL
);
1346 * Export the private key into ssh.com format; no passphrase
1347 * should be required since the key is currently unencrypted.
1348 * For RSA1 keys, this should give an error.
1350 setup_passphrases(NULL
);
1351 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1356 * List the fingerprint of the ssh.com-formatted key.
1358 setup_passphrases(NULL
);
1359 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1360 check_fp(tmpfilename1
, fp
, "%s ssh.com clear fp", keytypes
[i
]);
1363 * List the public half of the ssh.com-formatted key in
1366 setup_passphrases(NULL
);
1367 test(0, "puttygen", "-L", scfilename
, NULL
);
1370 * List the public half of the ssh.com-formatted key in
1371 * IETF/ssh.com format.
1373 setup_passphrases(NULL
);
1374 test(0, "puttygen", "-p", scfilename
, NULL
);
1379 * Convert from OpenSSH into ssh.com.
1381 setup_passphrases(NULL
);
1382 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1383 "-O", "private-sshcom", NULL
);
1386 * Convert from ssh.com back into a PuTTY key,
1387 * supplying the same comment as we had before we
1388 * started to ensure the comparison works.
1390 setup_passphrases(NULL
);
1391 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1392 "-o", tmpfilename2
, NULL
);
1395 * See if the PuTTY key thus generated is the same as
1398 filecmp(filename
, tmpfilename2
,
1399 "p->o->s->p clear %s", keytypes
[i
]);
1402 * Convert from ssh.com to OpenSSH.
1404 setup_passphrases(NULL
);
1405 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1406 "-O", "private-openssh", NULL
);
1409 * Convert from OpenSSH back into a PuTTY key,
1410 * supplying the same comment as we had before we
1411 * started to ensure the comparison works.
1413 setup_passphrases(NULL
);
1414 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1415 "-o", tmpfilename2
, NULL
);
1418 * See if the PuTTY key thus generated is the same as
1421 filecmp(filename
, tmpfilename2
,
1422 "p->s->o->p clear %s", keytypes
[i
]);
1425 * Finally, do a round-trip conversion between PuTTY
1426 * and ssh.com without involving OpenSSH, to test that
1427 * the key comment is preserved in that case.
1429 setup_passphrases(NULL
);
1430 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1432 setup_passphrases(NULL
);
1433 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1434 filecmp(filename
, tmpfilename2
,
1435 "p->s->p clear %s", keytypes
[i
]);
1439 * Check that mismatched passphrases cause an error.
1441 setup_passphrases("sponge2", "sponge3", NULL
);
1442 test(1, "puttygen", "-P", filename
, NULL
);
1445 * Put a passphrase back on.
1447 setup_passphrases("sponge2", "sponge2", NULL
);
1448 test(0, "puttygen", "-P", filename
, NULL
);
1451 * Export the private key into OpenSSH format, this time
1452 * while encrypted. For RSA1 keys, this should give an
1456 setup_passphrases(NULL
); /* error, hence no passphrase read */
1458 setup_passphrases("sponge2", NULL
);
1459 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1464 * List the fingerprint of the OpenSSH-formatted key.
1466 setup_passphrases("sponge2", NULL
);
1467 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1468 check_fp(tmpfilename1
, fp
, "%s openssh encrypted fp", keytypes
[i
]);
1471 * List the public half of the OpenSSH-formatted key in
1474 setup_passphrases("sponge2", NULL
);
1475 test(0, "puttygen", "-L", osfilename
, NULL
);
1478 * List the public half of the OpenSSH-formatted key in
1479 * IETF/ssh.com format.
1481 setup_passphrases("sponge2", NULL
);
1482 test(0, "puttygen", "-p", osfilename
, NULL
);
1486 * Export the private key into ssh.com format, this time
1487 * while encrypted. For RSA1 keys, this should give an
1491 setup_passphrases(NULL
); /* error, hence no passphrase read */
1493 setup_passphrases("sponge2", NULL
);
1494 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1499 * List the fingerprint of the ssh.com-formatted key.
1501 setup_passphrases("sponge2", NULL
);
1502 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1503 check_fp(tmpfilename1
, fp
, "%s ssh.com encrypted fp", keytypes
[i
]);
1506 * List the public half of the ssh.com-formatted key in
1509 setup_passphrases("sponge2", NULL
);
1510 test(0, "puttygen", "-L", scfilename
, NULL
);
1513 * List the public half of the ssh.com-formatted key in
1514 * IETF/ssh.com format.
1516 setup_passphrases("sponge2", NULL
);
1517 test(0, "puttygen", "-p", scfilename
, NULL
);
1522 * Convert from OpenSSH into ssh.com.
1524 setup_passphrases("sponge2", NULL
);
1525 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1526 "-O", "private-sshcom", NULL
);
1529 * Convert from ssh.com back into a PuTTY key,
1530 * supplying the same comment as we had before we
1531 * started to ensure the comparison works.
1533 setup_passphrases("sponge2", NULL
);
1534 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1535 "-o", tmpfilename2
, NULL
);
1538 * See if the PuTTY key thus generated is the same as
1541 filecmp(filename
, tmpfilename2
,
1542 "p->o->s->p encrypted %s", keytypes
[i
]);
1545 * Convert from ssh.com to OpenSSH.
1547 setup_passphrases("sponge2", NULL
);
1548 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1549 "-O", "private-openssh", NULL
);
1552 * Convert from OpenSSH back into a PuTTY key,
1553 * supplying the same comment as we had before we
1554 * started to ensure the comparison works.
1556 setup_passphrases("sponge2", NULL
);
1557 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1558 "-o", tmpfilename2
, NULL
);
1561 * See if the PuTTY key thus generated is the same as
1564 filecmp(filename
, tmpfilename2
,
1565 "p->s->o->p encrypted %s", keytypes
[i
]);
1568 * Finally, do a round-trip conversion between PuTTY
1569 * and ssh.com without involving OpenSSH, to test that
1570 * the key comment is preserved in that case.
1572 setup_passphrases("sponge2", NULL
);
1573 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1575 setup_passphrases("sponge2", NULL
);
1576 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1577 filecmp(filename
, tmpfilename2
,
1578 "p->s->p encrypted %s", keytypes
[i
]);
1582 * Load with the wrong passphrase.
1584 setup_passphrases("sponge8", NULL
);
1585 test(1, "puttygen", "-C", "spurious-new-comment", filename
, NULL
);
1588 * Load a totally bogus file.
1590 setup_passphrases(NULL
);
1591 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename
, NULL
);
1593 printf("%d passes, %d fails\n", passes
, fails
);