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
);
130 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
131 " [ -C comment ] [ -P ]\n"
132 " [ -o output-keyfile ] [ -O type | -l | -L"
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 " -O specify output type:\n"
150 " private output PuTTY private key format\n"
151 " private-openssh export OpenSSH private key\n"
152 " private-sshcom export ssh.com private key\n"
153 " public standard / ssh.com public key\n"
154 " public-openssh OpenSSH public key\n"
155 " fingerprint output the key fingerprint\n"
156 " -o specify output file\n"
157 " -l equivalent to `-O fingerprint'\n"
158 " -L equivalent to `-O public-openssh'\n"
159 " -p equivalent to `-O public'\n"
163 static int save_ssh2_pubkey(char *filename
, char *comment
,
164 void *v_pub_blob
, int pub_len
)
166 unsigned char *pub_blob
= (unsigned char *)v_pub_blob
;
172 fp
= fopen(filename
, "wb");
178 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
181 fprintf(fp
, "Comment: \"");
182 for (p
= comment
; *p
; p
++) {
183 if (*p
== '\\' || *p
== '\"')
192 while (i
< pub_len
) {
194 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
195 base64_encode_atom(pub_blob
+ i
, n
, buf
);
199 if (++column
>= 16) {
207 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
213 static int move(char *from
, char *to
)
217 ret
= rename(from
, to
);
220 * This OS may require us to remove the original file first.
223 ret
= rename(from
, to
);
226 perror("puttygen: cannot move new file on to old one");
232 static char *blobfp(char *alg
, int bits
, char *blob
, int bloblen
)
235 unsigned char digest
[16];
236 struct MD5Context md5c
;
240 MD5Update(&md5c
, blob
, bloblen
);
241 MD5Final(digest
, &md5c
);
243 sprintf(buffer
, "%s ", alg
);
245 sprintf(buffer
+ strlen(buffer
), "%d ", bits
);
246 for (i
= 0; i
< 16; i
++)
247 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
250 return dupstr(buffer
);
253 int main(int argc
, char **argv
)
257 enum { NOKEYGEN
, RSA1
, RSA2
, DSA
} keytype
= NOKEYGEN
;
258 char *outfile
= NULL
, *outfiletmp
= NULL
;
259 Filename outfilename
;
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 char *ssh2blob
= NULL
, *ssh2alg
= NULL
;
270 const struct ssh_signkey
*ssh2algf
= NULL
;
272 char *passphrase
= NULL
;
274 progfn_t progressfn
= is_interactive() ? progress_update
: no_progress
;
276 /* ------------------------------------------------------------------
277 * Parse the command line to figure out what we've been asked to do.
281 * If run with no arguments at all, print the usage message and
290 * Parse command line arguments.
307 opt
= p
++; /* opt will have _one_ leading - */
308 while (*p
&& *p
!= '=')
309 p
++; /* find end of option */
315 if (!strcmp(opt
, "-help")) {
318 } else if (!strcmp(opt
, "-version")) {
321 } else if (!strcmp(opt
, "-pgpfp")) {
322 /* support "-pgpfp" for consistency with others */
327 * A sample option requiring an argument:
329 * else if (!strcmp(opt, "-output")) {
331 * errs = TRUE, error(err_optnoarg, opt);
339 "puttygen: no such option `--%s'\n", opt
);
352 * Option requiring no parameter.
364 change_passphrase
= TRUE
;
376 progressfn
= no_progress
;
386 * Option requiring parameter.
392 fprintf(stderr
, "puttygen: option `-%c' expects a"
397 * Now c is the option and p is the parameter.
401 if (!strcmp(p
, "rsa") || !strcmp(p
, "rsa2"))
402 keytype
= RSA2
, sshver
= 2;
403 else if (!strcmp(p
, "rsa1"))
404 keytype
= RSA1
, sshver
= 1;
405 else if (!strcmp(p
, "dsa") || !strcmp(p
, "dss"))
406 keytype
= DSA
, sshver
= 2;
409 "puttygen: unknown key type `%s'\n", p
);
420 if (!strcmp(p
, "public"))
422 else if (!strcmp(p
, "public-openssh"))
424 else if (!strcmp(p
, "private"))
426 else if (!strcmp(p
, "fingerprint"))
428 else if (!strcmp(p
, "private-openssh"))
429 outtype
= OPENSSH
, sshver
= 2;
430 else if (!strcmp(p
, "private-sshcom"))
431 outtype
= SSHCOM
, sshver
= 2;
434 "puttygen: unknown output type `%s'\n", p
);
442 p
= NULL
; /* prevent continued processing */
446 * Unrecognised option.
449 fprintf(stderr
, "puttygen: no such option `-%c'\n", c
);
455 * A non-option argument.
461 fprintf(stderr
, "puttygen: cannot handle more than one"
474 * If run with at least one argument _but_ not the required
475 * ones, print the usage message and return failure.
477 if (!infile
&& keytype
== NOKEYGEN
) {
482 /* ------------------------------------------------------------------
483 * Figure out further details of exactly what we're going to do.
487 * Bomb out if we've been asked to both load and generate a
490 if (keytype
!= NOKEYGEN
&& intype
) {
491 fprintf(stderr
, "puttygen: cannot both load and generate a key\n");
496 * Analyse the type of the input file, in case this affects our
500 infilename
= filename_from_str(infile
);
502 intype
= key_type(&infilename
);
506 * It would be nice here to be able to load _public_
507 * key files, in any of a number of forms, and (a)
508 * convert them to other public key types, (b) print
509 * out their fingerprints. Or, I suppose, for real
510 * orthogonality, (c) change their comment!
512 * In fact this opens some interesting possibilities.
513 * Suppose ssh2_userkey_loadpub() were able to load
514 * public key files as well as extracting the public
515 * key from private ones. And suppose I did the thing
516 * I've been wanting to do, where specifying a
517 * particular private key file for authentication
518 * causes any _other_ key in the agent to be discarded.
519 * Then, if you had an agent forwarded to the machine
520 * you were running Unix PuTTY or Plink on, and you
521 * needed to specify which of the keys in the agent it
522 * should use, you could do that by supplying a
523 * _public_ key file, thus not needing to trust even
524 * your encrypted private key file to the network. Ooh!
527 case SSH_KEYTYPE_UNOPENABLE
:
528 case SSH_KEYTYPE_UNKNOWN
:
529 fprintf(stderr
, "puttygen: unable to load file `%s': %s\n",
530 infile
, key_type_to_str(intype
));
533 case SSH_KEYTYPE_SSH1
:
535 fprintf(stderr
, "puttygen: conversion from SSH-1 to SSH-2 keys"
542 case SSH_KEYTYPE_SSH2
:
543 case SSH_KEYTYPE_OPENSSH
:
544 case SSH_KEYTYPE_SSHCOM
:
546 fprintf(stderr
, "puttygen: conversion from SSH-2 to SSH-1 keys"
556 * Determine the default output file, if none is provided.
558 * This will usually be equal to stdout, except that if the
559 * input and output file formats are the same then the default
560 * output is to overwrite the input.
562 * Also in this code, we bomb out if the input and output file
563 * formats are the same and no other action is performed.
565 if ((intype
== SSH_KEYTYPE_SSH1
&& outtype
== PRIVATE
) ||
566 (intype
== SSH_KEYTYPE_SSH2
&& outtype
== PRIVATE
) ||
567 (intype
== SSH_KEYTYPE_OPENSSH
&& outtype
== OPENSSH
) ||
568 (intype
== SSH_KEYTYPE_SSHCOM
&& outtype
== SSHCOM
)) {
571 outfiletmp
= dupcat(outfile
, ".tmp", NULL
);
574 if (!change_passphrase
&& !comment
) {
575 fprintf(stderr
, "puttygen: this command would perform no useful"
582 * Bomb out rather than automatically choosing to write
583 * a private key file to stdout.
585 if (outtype
==PRIVATE
|| outtype
==OPENSSH
|| outtype
==SSHCOM
) {
586 fprintf(stderr
, "puttygen: need to specify an output file\n");
593 * Figure out whether we need to load the encrypted part of the
594 * key. This will be the case if either (a) we need to write
595 * out a private key format, or (b) the entire input key file
598 if (outtype
== PRIVATE
|| outtype
== OPENSSH
|| outtype
== SSHCOM
||
599 intype
== SSH_KEYTYPE_OPENSSH
|| intype
== SSH_KEYTYPE_SSHCOM
)
600 load_encrypted
= TRUE
;
602 load_encrypted
= FALSE
;
604 /* ------------------------------------------------------------------
605 * Now we're ready to actually do some stuff.
609 * Either load or generate a key.
611 if (keytype
!= NOKEYGEN
) {
613 char default_comment
[80];
615 struct progress prog
;
622 strftime(default_comment
, 30, "dsa-key-%Y%m%d", &tm
);
624 strftime(default_comment
, 30, "rsa-key-%Y%m%d", &tm
);
627 entropy
= get_random_data(bits
/ 8);
628 random_add_heavynoise(entropy
, bits
/ 8);
629 memset(entropy
, 0, bits
/8);
632 if (keytype
== DSA
) {
633 struct dss_key
*dsskey
= snew(struct dss_key
);
634 dsa_generate(dsskey
, bits
, progressfn
, &prog
);
635 ssh2key
= snew(struct ssh2_userkey
);
636 ssh2key
->data
= dsskey
;
637 ssh2key
->alg
= &ssh_dss
;
640 struct RSAKey
*rsakey
= snew(struct RSAKey
);
641 rsa_generate(rsakey
, bits
, progressfn
, &prog
);
642 rsakey
->comment
= NULL
;
643 if (keytype
== RSA1
) {
646 ssh2key
= snew(struct ssh2_userkey
);
647 ssh2key
->data
= rsakey
;
648 ssh2key
->alg
= &ssh_rsa
;
651 progressfn(&prog
, PROGFN_PROGRESS
, INT_MAX
, -1);
654 ssh2key
->comment
= dupstr(default_comment
);
656 ssh1key
->comment
= dupstr(default_comment
);
659 const char *error
= NULL
;
662 assert(infile
!= NULL
);
665 * Find out whether the input key is encrypted.
667 if (intype
== SSH_KEYTYPE_SSH1
)
668 encrypted
= rsakey_encrypted(&infilename
, &origcomment
);
669 else if (intype
== SSH_KEYTYPE_SSH2
)
670 encrypted
= ssh2_userkey_encrypted(&infilename
, &origcomment
);
672 encrypted
= import_encrypted(&infilename
, intype
, &origcomment
);
675 * If so, ask for a passphrase.
677 if (encrypted
&& load_encrypted
) {
678 prompts_t
*p
= new_prompts(NULL
);
680 p
->to_server
= FALSE
;
681 p
->name
= dupstr("SSH key passphrase");
682 add_prompt(p
, dupstr("Enter passphrase to load key: "), FALSE
, 512);
683 ret
= console_get_userpass_input(p
, NULL
, 0);
687 perror("puttygen: unable to read passphrase");
690 passphrase
= dupstr(p
->prompts
[0]->result
);
700 case SSH_KEYTYPE_SSH1
:
701 ssh1key
= snew(struct RSAKey
);
702 if (!load_encrypted
) {
707 ret
= rsakey_pubblob(&infilename
, &vblob
, &bloblen
,
708 &origcomment
, &error
);
709 blob
= (char *)vblob
;
711 n
= 4; /* skip modulus bits */
713 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
716 error
= "SSH-1 public key blob was too short";
719 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
722 error
= "SSH-1 public key blob was too short";
726 ssh1key
->comment
= dupstr(origcomment
);
727 ssh1key
->private_exponent
= NULL
;
729 ret
= loadrsakey(&infilename
, ssh1key
, passphrase
, &error
);
734 error
= "unknown error";
737 case SSH_KEYTYPE_SSH2
:
738 if (!load_encrypted
) {
739 ssh2blob
= ssh2_userkey_loadpub(&infilename
, &ssh2alg
,
740 &ssh2bloblen
, NULL
, &error
);
741 ssh2algf
= find_pubkey_alg(ssh2alg
);
743 bits
= ssh2algf
->pubkey_bits(ssh2blob
, ssh2bloblen
);
747 ssh2key
= ssh2_load_userkey(&infilename
, passphrase
, &error
);
749 if ((ssh2key
&& ssh2key
!= SSH2_WRONG_PASSPHRASE
) || ssh2blob
)
752 if (ssh2key
== SSH2_WRONG_PASSPHRASE
)
753 error
= "wrong passphrase";
755 error
= "unknown error";
759 case SSH_KEYTYPE_OPENSSH
:
760 case SSH_KEYTYPE_SSHCOM
:
761 ssh2key
= import_ssh2(&infilename
, intype
, passphrase
, &error
);
763 if (ssh2key
!= SSH2_WRONG_PASSPHRASE
)
766 error
= "wrong passphrase";
768 error
= "unknown error";
776 fprintf(stderr
, "puttygen: error loading `%s': %s\n",
783 * Change the comment if asked to.
788 sfree(ssh1key
->comment
);
789 ssh1key
->comment
= dupstr(comment
);
792 sfree(ssh2key
->comment
);
793 ssh2key
->comment
= dupstr(comment
);
798 * Prompt for a new passphrase if we have been asked to, or if
799 * we have just generated a key.
801 if (change_passphrase
|| keytype
!= NOKEYGEN
) {
802 prompts_t
*p
= new_prompts(NULL
);
805 p
->to_server
= FALSE
;
806 p
->name
= dupstr("New SSH key passphrase");
807 add_prompt(p
, dupstr("Enter passphrase to save key: "), FALSE
, 512);
808 add_prompt(p
, dupstr("Re-enter passphrase to verify: "), FALSE
, 512);
809 ret
= console_get_userpass_input(p
, NULL
, 0);
813 perror("puttygen: unable to read new passphrase");
816 if (strcmp(p
->prompts
[0]->result
, p
->prompts
[1]->result
)) {
818 fprintf(stderr
, "puttygen: passphrases do not match\n");
822 memset(passphrase
, 0, strlen(passphrase
));
825 passphrase
= dupstr(p
->prompts
[0]->result
);
837 * (In the case where outfile and outfiletmp are both NULL,
838 * there is no semantic reason to initialise outfilename at
839 * all; but we have to write _something_ to it or some compiler
840 * will probably complain that it might be used uninitialised.)
843 outfilename
= filename_from_str(outfiletmp
);
845 outfilename
= filename_from_str(outfile ? outfile
: "");
853 ret
= saversakey(&outfilename
, ssh1key
, passphrase
);
855 fprintf(stderr
, "puttygen: unable to save SSH-1 private key\n");
860 ret
= ssh2_save_userkey(&outfilename
, ssh2key
, passphrase
);
862 fprintf(stderr
, "puttygen: unable to save SSH-2 private key\n");
867 if (!move(outfiletmp
, outfile
))
868 return 1; /* rename failed */
881 fp
= f_open(outfilename
, "w");
884 dec1
= bignum_decimal(ssh1key
->exponent
);
885 dec2
= bignum_decimal(ssh1key
->modulus
);
886 fprintf(fp
, "%d %s %s %s\n", bignum_bitcount(ssh1key
->modulus
),
887 dec1
, dec2
, ssh1key
->comment
);
892 } else if (outtype
== PUBLIC
) {
895 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
898 save_ssh2_pubkey(outfile
, ssh2key ? ssh2key
->comment
: origcomment
,
899 ssh2blob
, ssh2bloblen
);
900 } else if (outtype
== PUBLICO
) {
907 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
912 ssh2alg
= ssh2key
->alg
->name
;
915 comment
= ssh2key
->comment
;
917 comment
= origcomment
;
919 buffer
= snewn(strlen(ssh2alg
) +
920 4 * ((ssh2bloblen
+2) / 3) +
921 strlen(comment
) + 3, char);
922 strcpy(buffer
, ssh2alg
);
923 p
= buffer
+ strlen(buffer
);
926 while (i
< ssh2bloblen
) {
927 int n
= (ssh2bloblen
- i
< 3 ? ssh2bloblen
- i
: 3);
928 base64_encode_atom(ssh2blob
+ i
, n
, p
);
939 fp
= f_open(outfilename
, "w");
942 fprintf(fp
, "%s\n", buffer
);
957 fingerprint
= snewn(128, char);
958 rsa_fingerprint(fingerprint
, 128, ssh1key
);
961 fingerprint
= ssh2key
->alg
->fingerprint(ssh2key
->data
);
964 fingerprint
= blobfp(ssh2alg
, bits
, ssh2blob
, ssh2bloblen
);
969 fp
= f_open(outfilename
, "w");
972 fprintf(fp
, "%s\n", fingerprint
);
984 ret
= export_ssh2(&outfilename
, outtype
, ssh2key
, passphrase
);
986 fprintf(stderr
, "puttygen: unable to export key\n");
990 if (!move(outfiletmp
, outfile
))
991 return 1; /* rename failed */
997 memset(passphrase
, 0, strlen(passphrase
));
1002 freersakey(ssh1key
);
1004 ssh2key
->alg
->freekey(ssh2key
->data
);
1019 void setup_passphrases(char *first
, ...)
1026 prompts
[nprompts
++] = first
;
1027 va_start(ap
, first
);
1028 while ((next
= va_arg(ap
, char *)) != NULL
) {
1029 assert(nprompts
< lenof(prompts
));
1030 prompts
[nprompts
++] = next
;
1036 void test(int retval
, ...)
1043 va_start(ap
, retval
);
1044 while (va_arg(ap
, char *) != NULL
)
1048 argv
= snewn(argc
+1, char *);
1049 va_start(ap
, retval
);
1050 for (i
= 0; i
<= argc
; i
++)
1051 argv
[i
] = va_arg(ap
, char *);
1055 ret
= cmdgen_main(argc
, argv
);
1057 if (ret
!= retval
) {
1058 printf("FAILED retval (exp %d got %d):", retval
, ret
);
1059 for (i
= 0; i
< argc
; i
++)
1060 printf(" %s", argv
[i
]);
1063 } else if (promptsgot
!= nprompts
) {
1064 printf("FAILED nprompts (exp %d got %d):", nprompts
, promptsgot
);
1065 for (i
= 0; i
< argc
; i
++)
1066 printf(" %s", argv
[i
]);
1074 void filecmp(char *file1
, char *file2
, char *fmt
, ...)
1077 * Ideally I should do file comparison myself, to maximise the
1078 * portability of this test suite once this application begins
1079 * running on non-Unix platforms. For the moment, though,
1080 * calling Unix diff is perfectly adequate.
1085 buf
= dupprintf("diff -q '%s' '%s'", file1
, file2
);
1092 printf("FAILED diff (ret=%d): ", ret
);
1105 char *cleanup_fp(char *s
)
1109 if (!strncmp(s
, "ssh-", 4)) {
1110 s
+= strcspn(s
, " \n\t");
1111 s
+= strspn(s
, " \n\t");
1115 s
+= strcspn(s
, " \n\t");
1116 s
+= strspn(s
, " \n\t");
1117 s
+= strcspn(s
, " \n\t");
1119 return dupprintf("%.*s", s
- p
, p
);
1122 char *get_fp(char *filename
)
1125 char buf
[256], *ret
;
1127 fp
= fopen(filename
, "r");
1130 ret
= fgets(buf
, sizeof(buf
), fp
);
1134 return cleanup_fp(buf
);
1137 void check_fp(char *filename
, char *fp
, char *fmt
, ...)
1144 newfp
= get_fp(filename
);
1146 if (!strcmp(fp
, newfp
)) {
1151 printf("FAILED check_fp ['%s' != '%s']: ", newfp
, fp
);
1165 int main(int argc
, char **argv
)
1168 static char *const keytypes
[] = { "rsa1", "dsa", "rsa" };
1171 * Even when this thing is compiled for automatic test mode,
1172 * it's helpful to be able to invoke it with command-line
1173 * options for _manual_ tests.
1176 return cmdgen_main(argc
, argv
);
1180 for (i
= 0; i
< lenof(keytypes
); i
++) {
1181 char filename
[128], osfilename
[128], scfilename
[128];
1182 char pubfilename
[128], tmpfilename1
[128], tmpfilename2
[128];
1185 sprintf(filename
, "test-%s.ppk", keytypes
[i
]);
1186 sprintf(pubfilename
, "test-%s.pub", keytypes
[i
]);
1187 sprintf(osfilename
, "test-%s.os", keytypes
[i
]);
1188 sprintf(scfilename
, "test-%s.sc", keytypes
[i
]);
1189 sprintf(tmpfilename1
, "test-%s.tmp1", keytypes
[i
]);
1190 sprintf(tmpfilename2
, "test-%s.tmp2", keytypes
[i
]);
1193 * Create an encrypted key.
1195 setup_passphrases("sponge", "sponge", NULL
);
1196 test(0, "puttygen", "-t", keytypes
[i
], "-o", filename
, NULL
);
1199 * List the public key in OpenSSH format.
1201 setup_passphrases(NULL
);
1202 test(0, "puttygen", "-L", filename
, "-o", pubfilename
, NULL
);
1206 sprintf(cmdbuf
, "ssh-keygen -l -f '%s' > '%s'",
1207 pubfilename
, tmpfilename1
);
1208 if (system(cmdbuf
) ||
1209 (fp
= get_fp(tmpfilename1
)) == NULL
) {
1210 printf("UNABLE to test fingerprint matching against OpenSSH");
1215 * List the public key in IETF/ssh.com format.
1217 setup_passphrases(NULL
);
1218 test(0, "puttygen", "-p", filename
, NULL
);
1221 * List the fingerprint of the key.
1223 setup_passphrases(NULL
);
1224 test(0, "puttygen", "-l", filename
, "-o", tmpfilename1
, NULL
);
1227 * If we can't test fingerprints against OpenSSH, we
1228 * can at the very least test equality of all the
1229 * fingerprints we generate of this key throughout
1232 fp
= get_fp(tmpfilename1
);
1234 check_fp(tmpfilename1
, fp
, "%s initial fp", keytypes
[i
]);
1238 * Change the comment of the key; this _does_ require a
1239 * passphrase owing to the tamperproofing.
1241 * NOTE: In SSH-1, this only requires a passphrase because
1242 * of inadequacies of the loading and saving mechanisms. In
1243 * _principle_, it should be perfectly possible to modify
1244 * the comment on an SSH-1 key without requiring a
1245 * passphrase; the only reason I can't do it is because my
1246 * loading and saving mechanisms don't include a method of
1247 * loading all the key data without also trying to decrypt
1248 * the private section.
1250 * I don't consider this to be a problem worth solving,
1251 * because (a) to fix it would probably end up bloating
1252 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1253 * it shouldn't be highly significant. If it seriously
1254 * bothers anyone then perhaps I _might_ be persuadable.
1256 setup_passphrases("sponge", NULL
);
1257 test(0, "puttygen", "-C", "new-comment", filename
, NULL
);
1260 * Change the passphrase to nothing.
1262 setup_passphrases("sponge", "", "", NULL
);
1263 test(0, "puttygen", "-P", filename
, NULL
);
1266 * Change the comment of the key again; this time we expect no
1267 * passphrase to be required.
1269 setup_passphrases(NULL
);
1270 test(0, "puttygen", "-C", "new-comment-2", filename
, NULL
);
1273 * Export the private key into OpenSSH format; no passphrase
1274 * should be required since the key is currently unencrypted.
1275 * For RSA1 keys, this should give an error.
1277 setup_passphrases(NULL
);
1278 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1283 * List the fingerprint of the OpenSSH-formatted key.
1285 setup_passphrases(NULL
);
1286 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1287 check_fp(tmpfilename1
, fp
, "%s openssh clear fp", keytypes
[i
]);
1290 * List the public half of the OpenSSH-formatted key in
1293 setup_passphrases(NULL
);
1294 test(0, "puttygen", "-L", osfilename
, NULL
);
1297 * List the public half of the OpenSSH-formatted key in
1298 * IETF/ssh.com format.
1300 setup_passphrases(NULL
);
1301 test(0, "puttygen", "-p", osfilename
, NULL
);
1305 * Export the private key into ssh.com format; no passphrase
1306 * should be required since the key is currently unencrypted.
1307 * For RSA1 keys, this should give an error.
1309 setup_passphrases(NULL
);
1310 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1315 * List the fingerprint of the ssh.com-formatted key.
1317 setup_passphrases(NULL
);
1318 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1319 check_fp(tmpfilename1
, fp
, "%s ssh.com clear fp", keytypes
[i
]);
1322 * List the public half of the ssh.com-formatted key in
1325 setup_passphrases(NULL
);
1326 test(0, "puttygen", "-L", scfilename
, NULL
);
1329 * List the public half of the ssh.com-formatted key in
1330 * IETF/ssh.com format.
1332 setup_passphrases(NULL
);
1333 test(0, "puttygen", "-p", scfilename
, NULL
);
1338 * Convert from OpenSSH into ssh.com.
1340 setup_passphrases(NULL
);
1341 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1342 "-O", "private-sshcom", NULL
);
1345 * Convert from ssh.com back into a PuTTY key,
1346 * supplying the same comment as we had before we
1347 * started to ensure the comparison works.
1349 setup_passphrases(NULL
);
1350 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1351 "-o", tmpfilename2
, NULL
);
1354 * See if the PuTTY key thus generated is the same as
1357 filecmp(filename
, tmpfilename2
,
1358 "p->o->s->p clear %s", keytypes
[i
]);
1361 * Convert from ssh.com to OpenSSH.
1363 setup_passphrases(NULL
);
1364 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1365 "-O", "private-openssh", NULL
);
1368 * Convert from OpenSSH back into a PuTTY key,
1369 * supplying the same comment as we had before we
1370 * started to ensure the comparison works.
1372 setup_passphrases(NULL
);
1373 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1374 "-o", tmpfilename2
, NULL
);
1377 * See if the PuTTY key thus generated is the same as
1380 filecmp(filename
, tmpfilename2
,
1381 "p->s->o->p clear %s", keytypes
[i
]);
1384 * Finally, do a round-trip conversion between PuTTY
1385 * and ssh.com without involving OpenSSH, to test that
1386 * the key comment is preserved in that case.
1388 setup_passphrases(NULL
);
1389 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1391 setup_passphrases(NULL
);
1392 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1393 filecmp(filename
, tmpfilename2
,
1394 "p->s->p clear %s", keytypes
[i
]);
1398 * Check that mismatched passphrases cause an error.
1400 setup_passphrases("sponge2", "sponge3", NULL
);
1401 test(1, "puttygen", "-P", filename
, NULL
);
1404 * Put a passphrase back on.
1406 setup_passphrases("sponge2", "sponge2", NULL
);
1407 test(0, "puttygen", "-P", filename
, NULL
);
1410 * Export the private key into OpenSSH format, this time
1411 * while encrypted. For RSA1 keys, this should give an
1415 setup_passphrases(NULL
); /* error, hence no passphrase read */
1417 setup_passphrases("sponge2", NULL
);
1418 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1423 * List the fingerprint of the OpenSSH-formatted key.
1425 setup_passphrases("sponge2", NULL
);
1426 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1427 check_fp(tmpfilename1
, fp
, "%s openssh encrypted fp", keytypes
[i
]);
1430 * List the public half of the OpenSSH-formatted key in
1433 setup_passphrases("sponge2", NULL
);
1434 test(0, "puttygen", "-L", osfilename
, NULL
);
1437 * List the public half of the OpenSSH-formatted key in
1438 * IETF/ssh.com format.
1440 setup_passphrases("sponge2", NULL
);
1441 test(0, "puttygen", "-p", osfilename
, NULL
);
1445 * Export the private key into ssh.com format, this time
1446 * while encrypted. For RSA1 keys, this should give an
1450 setup_passphrases(NULL
); /* error, hence no passphrase read */
1452 setup_passphrases("sponge2", NULL
);
1453 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1458 * List the fingerprint of the ssh.com-formatted key.
1460 setup_passphrases("sponge2", NULL
);
1461 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1462 check_fp(tmpfilename1
, fp
, "%s ssh.com encrypted fp", keytypes
[i
]);
1465 * List the public half of the ssh.com-formatted key in
1468 setup_passphrases("sponge2", NULL
);
1469 test(0, "puttygen", "-L", scfilename
, NULL
);
1472 * List the public half of the ssh.com-formatted key in
1473 * IETF/ssh.com format.
1475 setup_passphrases("sponge2", NULL
);
1476 test(0, "puttygen", "-p", scfilename
, NULL
);
1481 * Convert from OpenSSH into ssh.com.
1483 setup_passphrases("sponge2", NULL
);
1484 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1485 "-O", "private-sshcom", NULL
);
1488 * Convert from ssh.com back into a PuTTY key,
1489 * supplying the same comment as we had before we
1490 * started to ensure the comparison works.
1492 setup_passphrases("sponge2", NULL
);
1493 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1494 "-o", tmpfilename2
, NULL
);
1497 * See if the PuTTY key thus generated is the same as
1500 filecmp(filename
, tmpfilename2
,
1501 "p->o->s->p encrypted %s", keytypes
[i
]);
1504 * Convert from ssh.com to OpenSSH.
1506 setup_passphrases("sponge2", NULL
);
1507 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1508 "-O", "private-openssh", NULL
);
1511 * Convert from OpenSSH back into a PuTTY key,
1512 * supplying the same comment as we had before we
1513 * started to ensure the comparison works.
1515 setup_passphrases("sponge2", NULL
);
1516 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1517 "-o", tmpfilename2
, NULL
);
1520 * See if the PuTTY key thus generated is the same as
1523 filecmp(filename
, tmpfilename2
,
1524 "p->s->o->p encrypted %s", keytypes
[i
]);
1527 * Finally, do a round-trip conversion between PuTTY
1528 * and ssh.com without involving OpenSSH, to test that
1529 * the key comment is preserved in that case.
1531 setup_passphrases("sponge2", NULL
);
1532 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1534 setup_passphrases("sponge2", NULL
);
1535 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1536 filecmp(filename
, tmpfilename2
,
1537 "p->s->p encrypted %s", keytypes
[i
]);
1541 * Load with the wrong passphrase.
1543 setup_passphrases("sponge8", NULL
);
1544 test(1, "puttygen", "-C", "spurious-new-comment", filename
, NULL
);
1547 * Load a totally bogus file.
1549 setup_passphrases(NULL
);
1550 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename
, NULL
);
1552 printf("%d passes, %d fails\n", passes
, fails
);