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 ]\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 " -O specify output type:\n"
153 " private output PuTTY private key format\n"
154 " private-openssh export OpenSSH private key\n"
155 " private-sshcom export ssh.com private key\n"
156 " public standard / ssh.com public key\n"
157 " public-openssh OpenSSH public key\n"
158 " fingerprint output the key fingerprint\n"
159 " -o specify output file\n"
160 " -l equivalent to `-O fingerprint'\n"
161 " -L equivalent to `-O public-openssh'\n"
162 " -p equivalent to `-O public'\n"
166 static int save_ssh2_pubkey(char *filename
, char *comment
,
167 void *v_pub_blob
, int pub_len
)
169 unsigned char *pub_blob
= (unsigned char *)v_pub_blob
;
175 fp
= fopen(filename
, "wb");
181 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
184 fprintf(fp
, "Comment: \"");
185 for (p
= comment
; *p
; p
++) {
186 if (*p
== '\\' || *p
== '\"')
195 while (i
< pub_len
) {
197 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
198 base64_encode_atom(pub_blob
+ i
, n
, buf
);
202 if (++column
>= 16) {
210 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
216 static int move(char *from
, char *to
)
220 ret
= rename(from
, to
);
223 * This OS may require us to remove the original file first.
226 ret
= rename(from
, to
);
229 perror("puttygen: cannot move new file on to old one");
235 static char *blobfp(char *alg
, int bits
, char *blob
, int bloblen
)
238 unsigned char digest
[16];
239 struct MD5Context md5c
;
243 MD5Update(&md5c
, blob
, bloblen
);
244 MD5Final(digest
, &md5c
);
246 sprintf(buffer
, "%s ", alg
);
248 sprintf(buffer
+ strlen(buffer
), "%d ", bits
);
249 for (i
= 0; i
< 16; i
++)
250 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
253 return dupstr(buffer
);
256 int main(int argc
, char **argv
)
260 enum { NOKEYGEN
, RSA1
, RSA2
, DSA
} keytype
= NOKEYGEN
;
261 char *outfile
= NULL
, *outfiletmp
= NULL
;
262 Filename outfilename
;
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 char *ssh2blob
= NULL
, *ssh2alg
= NULL
;
273 const struct ssh_signkey
*ssh2algf
= NULL
;
275 char *passphrase
= NULL
;
277 progfn_t progressfn
= is_interactive() ? progress_update
: no_progress
;
279 /* ------------------------------------------------------------------
280 * Parse the command line to figure out what we've been asked to do.
284 * If run with no arguments at all, print the usage message and
293 * Parse command line arguments.
310 opt
= p
++; /* opt will have _one_ leading - */
311 while (*p
&& *p
!= '=')
312 p
++; /* find end of option */
318 if (!strcmp(opt
, "-help")) {
321 } else if (!strcmp(opt
, "-version")) {
324 } else if (!strcmp(opt
, "-pgpfp")) {
325 /* support "-pgpfp" for consistency with others */
330 * A sample option requiring an argument:
332 * else if (!strcmp(opt, "-output")) {
334 * errs = TRUE, error(err_optnoarg, opt);
342 "puttygen: no such option `--%s'\n", opt
);
355 * Option requiring no parameter.
367 change_passphrase
= TRUE
;
379 progressfn
= no_progress
;
389 * Option requiring parameter.
395 fprintf(stderr
, "puttygen: option `-%c' expects a"
400 * Now c is the option and p is the parameter.
404 if (!strcmp(p
, "rsa") || !strcmp(p
, "rsa2"))
405 keytype
= RSA2
, sshver
= 2;
406 else if (!strcmp(p
, "rsa1"))
407 keytype
= RSA1
, sshver
= 1;
408 else if (!strcmp(p
, "dsa") || !strcmp(p
, "dss"))
409 keytype
= DSA
, sshver
= 2;
412 "puttygen: unknown key type `%s'\n", p
);
423 if (!strcmp(p
, "public"))
425 else if (!strcmp(p
, "public-openssh"))
427 else if (!strcmp(p
, "private"))
429 else if (!strcmp(p
, "fingerprint"))
431 else if (!strcmp(p
, "private-openssh"))
432 outtype
= OPENSSH
, sshver
= 2;
433 else if (!strcmp(p
, "private-sshcom"))
434 outtype
= SSHCOM
, sshver
= 2;
437 "puttygen: unknown output type `%s'\n", p
);
445 p
= NULL
; /* prevent continued processing */
449 * Unrecognised option.
452 fprintf(stderr
, "puttygen: no such option `-%c'\n", c
);
458 * A non-option argument.
464 fprintf(stderr
, "puttygen: cannot handle more than one"
477 * If run with at least one argument _but_ not the required
478 * ones, print the usage message and return failure.
480 if (!infile
&& keytype
== NOKEYGEN
) {
485 /* ------------------------------------------------------------------
486 * Figure out further details of exactly what we're going to do.
490 * Bomb out if we've been asked to both load and generate a
493 if (keytype
!= NOKEYGEN
&& intype
) {
494 fprintf(stderr
, "puttygen: cannot both load and generate a key\n");
499 * Analyse the type of the input file, in case this affects our
503 infilename
= filename_from_str(infile
);
505 intype
= key_type(&infilename
);
509 * It would be nice here to be able to load _public_
510 * key files, in any of a number of forms, and (a)
511 * convert them to other public key types, (b) print
512 * out their fingerprints. Or, I suppose, for real
513 * orthogonality, (c) change their comment!
515 * In fact this opens some interesting possibilities.
516 * Suppose ssh2_userkey_loadpub() were able to load
517 * public key files as well as extracting the public
518 * key from private ones. And suppose I did the thing
519 * I've been wanting to do, where specifying a
520 * particular private key file for authentication
521 * causes any _other_ key in the agent to be discarded.
522 * Then, if you had an agent forwarded to the machine
523 * you were running Unix PuTTY or Plink on, and you
524 * needed to specify which of the keys in the agent it
525 * should use, you could do that by supplying a
526 * _public_ key file, thus not needing to trust even
527 * your encrypted private key file to the network. Ooh!
530 case SSH_KEYTYPE_UNOPENABLE
:
531 case SSH_KEYTYPE_UNKNOWN
:
532 fprintf(stderr
, "puttygen: unable to load file `%s': %s\n",
533 infile
, key_type_to_str(intype
));
536 case SSH_KEYTYPE_SSH1
:
538 fprintf(stderr
, "puttygen: conversion from SSH-1 to SSH-2 keys"
545 case SSH_KEYTYPE_SSH2
:
546 case SSH_KEYTYPE_OPENSSH
:
547 case SSH_KEYTYPE_SSHCOM
:
549 fprintf(stderr
, "puttygen: conversion from SSH-2 to SSH-1 keys"
559 * Determine the default output file, if none is provided.
561 * This will usually be equal to stdout, except that if the
562 * input and output file formats are the same then the default
563 * output is to overwrite the input.
565 * Also in this code, we bomb out if the input and output file
566 * formats are the same and no other action is performed.
568 if ((intype
== SSH_KEYTYPE_SSH1
&& outtype
== PRIVATE
) ||
569 (intype
== SSH_KEYTYPE_SSH2
&& outtype
== PRIVATE
) ||
570 (intype
== SSH_KEYTYPE_OPENSSH
&& outtype
== OPENSSH
) ||
571 (intype
== SSH_KEYTYPE_SSHCOM
&& outtype
== SSHCOM
)) {
574 outfiletmp
= dupcat(outfile
, ".tmp", NULL
);
577 if (!change_passphrase
&& !comment
) {
578 fprintf(stderr
, "puttygen: this command would perform no useful"
585 * Bomb out rather than automatically choosing to write
586 * a private key file to stdout.
588 if (outtype
==PRIVATE
|| outtype
==OPENSSH
|| outtype
==SSHCOM
) {
589 fprintf(stderr
, "puttygen: need to specify an output file\n");
596 * Figure out whether we need to load the encrypted part of the
597 * key. This will be the case if either (a) we need to write
598 * out a private key format, or (b) the entire input key file
601 if (outtype
== PRIVATE
|| outtype
== OPENSSH
|| outtype
== SSHCOM
||
602 intype
== SSH_KEYTYPE_OPENSSH
|| intype
== SSH_KEYTYPE_SSHCOM
)
603 load_encrypted
= TRUE
;
605 load_encrypted
= FALSE
;
607 /* ------------------------------------------------------------------
608 * Now we're ready to actually do some stuff.
612 * Either load or generate a key.
614 if (keytype
!= NOKEYGEN
) {
616 char default_comment
[80];
618 struct progress prog
;
625 strftime(default_comment
, 30, "dsa-key-%Y%m%d", &tm
);
627 strftime(default_comment
, 30, "rsa-key-%Y%m%d", &tm
);
630 entropy
= get_random_data(bits
/ 8);
631 random_add_heavynoise(entropy
, bits
/ 8);
632 memset(entropy
, 0, bits
/8);
635 if (keytype
== DSA
) {
636 struct dss_key
*dsskey
= snew(struct dss_key
);
637 dsa_generate(dsskey
, bits
, progressfn
, &prog
);
638 ssh2key
= snew(struct ssh2_userkey
);
639 ssh2key
->data
= dsskey
;
640 ssh2key
->alg
= &ssh_dss
;
643 struct RSAKey
*rsakey
= snew(struct RSAKey
);
644 rsa_generate(rsakey
, bits
, progressfn
, &prog
);
645 rsakey
->comment
= NULL
;
646 if (keytype
== RSA1
) {
649 ssh2key
= snew(struct ssh2_userkey
);
650 ssh2key
->data
= rsakey
;
651 ssh2key
->alg
= &ssh_rsa
;
654 progressfn(&prog
, PROGFN_PROGRESS
, INT_MAX
, -1);
657 ssh2key
->comment
= dupstr(default_comment
);
659 ssh1key
->comment
= dupstr(default_comment
);
662 const char *error
= NULL
;
665 assert(infile
!= NULL
);
668 * Find out whether the input key is encrypted.
670 if (intype
== SSH_KEYTYPE_SSH1
)
671 encrypted
= rsakey_encrypted(&infilename
, &origcomment
);
672 else if (intype
== SSH_KEYTYPE_SSH2
)
673 encrypted
= ssh2_userkey_encrypted(&infilename
, &origcomment
);
675 encrypted
= import_encrypted(&infilename
, intype
, &origcomment
);
678 * If so, ask for a passphrase.
680 if (encrypted
&& load_encrypted
) {
681 prompts_t
*p
= new_prompts(NULL
);
683 p
->to_server
= FALSE
;
684 p
->name
= dupstr("SSH key passphrase");
685 add_prompt(p
, dupstr("Enter passphrase to load key: "), FALSE
, 512);
686 ret
= console_get_userpass_input(p
, NULL
, 0);
690 perror("puttygen: unable to read passphrase");
693 passphrase
= dupstr(p
->prompts
[0]->result
);
703 case SSH_KEYTYPE_SSH1
:
704 ssh1key
= snew(struct RSAKey
);
705 if (!load_encrypted
) {
710 ret
= rsakey_pubblob(&infilename
, &vblob
, &bloblen
,
711 &origcomment
, &error
);
712 blob
= (char *)vblob
;
714 n
= 4; /* skip modulus bits */
716 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
719 error
= "SSH-1 public key blob was too short";
722 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
725 error
= "SSH-1 public key blob was too short";
729 ssh1key
->comment
= dupstr(origcomment
);
730 ssh1key
->private_exponent
= NULL
;
732 ret
= loadrsakey(&infilename
, ssh1key
, passphrase
, &error
);
737 error
= "unknown error";
740 case SSH_KEYTYPE_SSH2
:
741 if (!load_encrypted
) {
742 ssh2blob
= ssh2_userkey_loadpub(&infilename
, &ssh2alg
,
743 &ssh2bloblen
, NULL
, &error
);
744 ssh2algf
= find_pubkey_alg(ssh2alg
);
746 bits
= ssh2algf
->pubkey_bits(ssh2blob
, ssh2bloblen
);
750 ssh2key
= ssh2_load_userkey(&infilename
, passphrase
, &error
);
752 if ((ssh2key
&& ssh2key
!= SSH2_WRONG_PASSPHRASE
) || ssh2blob
)
755 if (ssh2key
== SSH2_WRONG_PASSPHRASE
)
756 error
= "wrong passphrase";
758 error
= "unknown error";
762 case SSH_KEYTYPE_OPENSSH
:
763 case SSH_KEYTYPE_SSHCOM
:
764 ssh2key
= import_ssh2(&infilename
, intype
, passphrase
, &error
);
766 if (ssh2key
!= SSH2_WRONG_PASSPHRASE
)
769 error
= "wrong passphrase";
771 error
= "unknown error";
779 fprintf(stderr
, "puttygen: error loading `%s': %s\n",
786 * Change the comment if asked to.
791 sfree(ssh1key
->comment
);
792 ssh1key
->comment
= dupstr(comment
);
795 sfree(ssh2key
->comment
);
796 ssh2key
->comment
= dupstr(comment
);
801 * Prompt for a new passphrase if we have been asked to, or if
802 * we have just generated a key.
804 if (change_passphrase
|| keytype
!= NOKEYGEN
) {
805 prompts_t
*p
= new_prompts(NULL
);
808 p
->to_server
= FALSE
;
809 p
->name
= dupstr("New SSH key passphrase");
810 add_prompt(p
, dupstr("Enter passphrase to save key: "), FALSE
, 512);
811 add_prompt(p
, dupstr("Re-enter passphrase to verify: "), FALSE
, 512);
812 ret
= console_get_userpass_input(p
, NULL
, 0);
816 perror("puttygen: unable to read new passphrase");
819 if (strcmp(p
->prompts
[0]->result
, p
->prompts
[1]->result
)) {
821 fprintf(stderr
, "puttygen: passphrases do not match\n");
825 memset(passphrase
, 0, strlen(passphrase
));
828 passphrase
= dupstr(p
->prompts
[0]->result
);
840 * (In the case where outfile and outfiletmp are both NULL,
841 * there is no semantic reason to initialise outfilename at
842 * all; but we have to write _something_ to it or some compiler
843 * will probably complain that it might be used uninitialised.)
846 outfilename
= filename_from_str(outfiletmp
);
848 outfilename
= filename_from_str(outfile ? outfile
: "");
856 ret
= saversakey(&outfilename
, ssh1key
, passphrase
);
858 fprintf(stderr
, "puttygen: unable to save SSH-1 private key\n");
863 ret
= ssh2_save_userkey(&outfilename
, ssh2key
, passphrase
);
865 fprintf(stderr
, "puttygen: unable to save SSH-2 private key\n");
870 if (!move(outfiletmp
, outfile
))
871 return 1; /* rename failed */
884 fp
= f_open(outfilename
, "w");
887 dec1
= bignum_decimal(ssh1key
->exponent
);
888 dec2
= bignum_decimal(ssh1key
->modulus
);
889 fprintf(fp
, "%d %s %s %s\n", bignum_bitcount(ssh1key
->modulus
),
890 dec1
, dec2
, ssh1key
->comment
);
895 } else if (outtype
== PUBLIC
) {
898 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
901 save_ssh2_pubkey(outfile
, ssh2key ? ssh2key
->comment
: origcomment
,
902 ssh2blob
, ssh2bloblen
);
903 } else if (outtype
== PUBLICO
) {
910 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
915 ssh2alg
= ssh2key
->alg
->name
;
918 comment
= ssh2key
->comment
;
920 comment
= origcomment
;
922 buffer
= snewn(strlen(ssh2alg
) +
923 4 * ((ssh2bloblen
+2) / 3) +
924 strlen(comment
) + 3, char);
925 strcpy(buffer
, ssh2alg
);
926 p
= buffer
+ strlen(buffer
);
929 while (i
< ssh2bloblen
) {
930 int n
= (ssh2bloblen
- i
< 3 ? ssh2bloblen
- i
: 3);
931 base64_encode_atom(ssh2blob
+ i
, n
, p
);
942 fp
= f_open(outfilename
, "w");
945 fprintf(fp
, "%s\n", buffer
);
960 fingerprint
= snewn(128, char);
961 rsa_fingerprint(fingerprint
, 128, ssh1key
);
964 fingerprint
= ssh2key
->alg
->fingerprint(ssh2key
->data
);
967 fingerprint
= blobfp(ssh2alg
, bits
, ssh2blob
, ssh2bloblen
);
972 fp
= f_open(outfilename
, "w");
975 fprintf(fp
, "%s\n", fingerprint
);
987 ret
= export_ssh2(&outfilename
, outtype
, ssh2key
, passphrase
);
989 fprintf(stderr
, "puttygen: unable to export key\n");
993 if (!move(outfiletmp
, outfile
))
994 return 1; /* rename failed */
1000 memset(passphrase
, 0, strlen(passphrase
));
1005 freersakey(ssh1key
);
1007 ssh2key
->alg
->freekey(ssh2key
->data
);
1022 void setup_passphrases(char *first
, ...)
1029 prompts
[nprompts
++] = first
;
1030 va_start(ap
, first
);
1031 while ((next
= va_arg(ap
, char *)) != NULL
) {
1032 assert(nprompts
< lenof(prompts
));
1033 prompts
[nprompts
++] = next
;
1039 void test(int retval
, ...)
1046 va_start(ap
, retval
);
1047 while (va_arg(ap
, char *) != NULL
)
1051 argv
= snewn(argc
+1, char *);
1052 va_start(ap
, retval
);
1053 for (i
= 0; i
<= argc
; i
++)
1054 argv
[i
] = va_arg(ap
, char *);
1058 ret
= cmdgen_main(argc
, argv
);
1060 if (ret
!= retval
) {
1061 printf("FAILED retval (exp %d got %d):", retval
, ret
);
1062 for (i
= 0; i
< argc
; i
++)
1063 printf(" %s", argv
[i
]);
1066 } else if (promptsgot
!= nprompts
) {
1067 printf("FAILED nprompts (exp %d got %d):", nprompts
, promptsgot
);
1068 for (i
= 0; i
< argc
; i
++)
1069 printf(" %s", argv
[i
]);
1077 void filecmp(char *file1
, char *file2
, char *fmt
, ...)
1080 * Ideally I should do file comparison myself, to maximise the
1081 * portability of this test suite once this application begins
1082 * running on non-Unix platforms. For the moment, though,
1083 * calling Unix diff is perfectly adequate.
1088 buf
= dupprintf("diff -q '%s' '%s'", file1
, file2
);
1095 printf("FAILED diff (ret=%d): ", ret
);
1108 char *cleanup_fp(char *s
)
1112 if (!strncmp(s
, "ssh-", 4)) {
1113 s
+= strcspn(s
, " \n\t");
1114 s
+= strspn(s
, " \n\t");
1118 s
+= strcspn(s
, " \n\t");
1119 s
+= strspn(s
, " \n\t");
1120 s
+= strcspn(s
, " \n\t");
1122 return dupprintf("%.*s", s
- p
, p
);
1125 char *get_fp(char *filename
)
1128 char buf
[256], *ret
;
1130 fp
= fopen(filename
, "r");
1133 ret
= fgets(buf
, sizeof(buf
), fp
);
1137 return cleanup_fp(buf
);
1140 void check_fp(char *filename
, char *fp
, char *fmt
, ...)
1147 newfp
= get_fp(filename
);
1149 if (!strcmp(fp
, newfp
)) {
1154 printf("FAILED check_fp ['%s' != '%s']: ", newfp
, fp
);
1168 int main(int argc
, char **argv
)
1171 static char *const keytypes
[] = { "rsa1", "dsa", "rsa" };
1174 * Even when this thing is compiled for automatic test mode,
1175 * it's helpful to be able to invoke it with command-line
1176 * options for _manual_ tests.
1179 return cmdgen_main(argc
, argv
);
1183 for (i
= 0; i
< lenof(keytypes
); i
++) {
1184 char filename
[128], osfilename
[128], scfilename
[128];
1185 char pubfilename
[128], tmpfilename1
[128], tmpfilename2
[128];
1188 sprintf(filename
, "test-%s.ppk", keytypes
[i
]);
1189 sprintf(pubfilename
, "test-%s.pub", keytypes
[i
]);
1190 sprintf(osfilename
, "test-%s.os", keytypes
[i
]);
1191 sprintf(scfilename
, "test-%s.sc", keytypes
[i
]);
1192 sprintf(tmpfilename1
, "test-%s.tmp1", keytypes
[i
]);
1193 sprintf(tmpfilename2
, "test-%s.tmp2", keytypes
[i
]);
1196 * Create an encrypted key.
1198 setup_passphrases("sponge", "sponge", NULL
);
1199 test(0, "puttygen", "-t", keytypes
[i
], "-o", filename
, NULL
);
1202 * List the public key in OpenSSH format.
1204 setup_passphrases(NULL
);
1205 test(0, "puttygen", "-L", filename
, "-o", pubfilename
, NULL
);
1209 sprintf(cmdbuf
, "ssh-keygen -l -f '%s' > '%s'",
1210 pubfilename
, tmpfilename1
);
1211 if (system(cmdbuf
) ||
1212 (fp
= get_fp(tmpfilename1
)) == NULL
) {
1213 printf("UNABLE to test fingerprint matching against OpenSSH");
1218 * List the public key in IETF/ssh.com format.
1220 setup_passphrases(NULL
);
1221 test(0, "puttygen", "-p", filename
, NULL
);
1224 * List the fingerprint of the key.
1226 setup_passphrases(NULL
);
1227 test(0, "puttygen", "-l", filename
, "-o", tmpfilename1
, NULL
);
1230 * If we can't test fingerprints against OpenSSH, we
1231 * can at the very least test equality of all the
1232 * fingerprints we generate of this key throughout
1235 fp
= get_fp(tmpfilename1
);
1237 check_fp(tmpfilename1
, fp
, "%s initial fp", keytypes
[i
]);
1241 * Change the comment of the key; this _does_ require a
1242 * passphrase owing to the tamperproofing.
1244 * NOTE: In SSH-1, this only requires a passphrase because
1245 * of inadequacies of the loading and saving mechanisms. In
1246 * _principle_, it should be perfectly possible to modify
1247 * the comment on an SSH-1 key without requiring a
1248 * passphrase; the only reason I can't do it is because my
1249 * loading and saving mechanisms don't include a method of
1250 * loading all the key data without also trying to decrypt
1251 * the private section.
1253 * I don't consider this to be a problem worth solving,
1254 * because (a) to fix it would probably end up bloating
1255 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1256 * it shouldn't be highly significant. If it seriously
1257 * bothers anyone then perhaps I _might_ be persuadable.
1259 setup_passphrases("sponge", NULL
);
1260 test(0, "puttygen", "-C", "new-comment", filename
, NULL
);
1263 * Change the passphrase to nothing.
1265 setup_passphrases("sponge", "", "", NULL
);
1266 test(0, "puttygen", "-P", filename
, NULL
);
1269 * Change the comment of the key again; this time we expect no
1270 * passphrase to be required.
1272 setup_passphrases(NULL
);
1273 test(0, "puttygen", "-C", "new-comment-2", filename
, NULL
);
1276 * Export the private key into OpenSSH format; no passphrase
1277 * should be required since the key is currently unencrypted.
1278 * For RSA1 keys, this should give an error.
1280 setup_passphrases(NULL
);
1281 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1286 * List the fingerprint of the OpenSSH-formatted key.
1288 setup_passphrases(NULL
);
1289 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1290 check_fp(tmpfilename1
, fp
, "%s openssh clear fp", keytypes
[i
]);
1293 * List the public half of the OpenSSH-formatted key in
1296 setup_passphrases(NULL
);
1297 test(0, "puttygen", "-L", osfilename
, NULL
);
1300 * List the public half of the OpenSSH-formatted key in
1301 * IETF/ssh.com format.
1303 setup_passphrases(NULL
);
1304 test(0, "puttygen", "-p", osfilename
, NULL
);
1308 * Export the private key into ssh.com format; no passphrase
1309 * should be required since the key is currently unencrypted.
1310 * For RSA1 keys, this should give an error.
1312 setup_passphrases(NULL
);
1313 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1318 * List the fingerprint of the ssh.com-formatted key.
1320 setup_passphrases(NULL
);
1321 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1322 check_fp(tmpfilename1
, fp
, "%s ssh.com clear fp", keytypes
[i
]);
1325 * List the public half of the ssh.com-formatted key in
1328 setup_passphrases(NULL
);
1329 test(0, "puttygen", "-L", scfilename
, NULL
);
1332 * List the public half of the ssh.com-formatted key in
1333 * IETF/ssh.com format.
1335 setup_passphrases(NULL
);
1336 test(0, "puttygen", "-p", scfilename
, NULL
);
1341 * Convert from OpenSSH into ssh.com.
1343 setup_passphrases(NULL
);
1344 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1345 "-O", "private-sshcom", NULL
);
1348 * Convert from ssh.com back into a PuTTY key,
1349 * supplying the same comment as we had before we
1350 * started to ensure the comparison works.
1352 setup_passphrases(NULL
);
1353 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1354 "-o", tmpfilename2
, NULL
);
1357 * See if the PuTTY key thus generated is the same as
1360 filecmp(filename
, tmpfilename2
,
1361 "p->o->s->p clear %s", keytypes
[i
]);
1364 * Convert from ssh.com to OpenSSH.
1366 setup_passphrases(NULL
);
1367 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1368 "-O", "private-openssh", NULL
);
1371 * Convert from OpenSSH back into a PuTTY key,
1372 * supplying the same comment as we had before we
1373 * started to ensure the comparison works.
1375 setup_passphrases(NULL
);
1376 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1377 "-o", tmpfilename2
, NULL
);
1380 * See if the PuTTY key thus generated is the same as
1383 filecmp(filename
, tmpfilename2
,
1384 "p->s->o->p clear %s", keytypes
[i
]);
1387 * Finally, do a round-trip conversion between PuTTY
1388 * and ssh.com without involving OpenSSH, to test that
1389 * the key comment is preserved in that case.
1391 setup_passphrases(NULL
);
1392 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1394 setup_passphrases(NULL
);
1395 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1396 filecmp(filename
, tmpfilename2
,
1397 "p->s->p clear %s", keytypes
[i
]);
1401 * Check that mismatched passphrases cause an error.
1403 setup_passphrases("sponge2", "sponge3", NULL
);
1404 test(1, "puttygen", "-P", filename
, NULL
);
1407 * Put a passphrase back on.
1409 setup_passphrases("sponge2", "sponge2", NULL
);
1410 test(0, "puttygen", "-P", filename
, NULL
);
1413 * Export the private key into OpenSSH format, this time
1414 * while encrypted. For RSA1 keys, this should give an
1418 setup_passphrases(NULL
); /* error, hence no passphrase read */
1420 setup_passphrases("sponge2", NULL
);
1421 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1426 * List the fingerprint of the OpenSSH-formatted key.
1428 setup_passphrases("sponge2", NULL
);
1429 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1430 check_fp(tmpfilename1
, fp
, "%s openssh encrypted fp", keytypes
[i
]);
1433 * List the public half of the OpenSSH-formatted key in
1436 setup_passphrases("sponge2", NULL
);
1437 test(0, "puttygen", "-L", osfilename
, NULL
);
1440 * List the public half of the OpenSSH-formatted key in
1441 * IETF/ssh.com format.
1443 setup_passphrases("sponge2", NULL
);
1444 test(0, "puttygen", "-p", osfilename
, NULL
);
1448 * Export the private key into ssh.com 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-sshcom", "-o", scfilename
,
1461 * List the fingerprint of the ssh.com-formatted key.
1463 setup_passphrases("sponge2", NULL
);
1464 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1465 check_fp(tmpfilename1
, fp
, "%s ssh.com encrypted fp", keytypes
[i
]);
1468 * List the public half of the ssh.com-formatted key in
1471 setup_passphrases("sponge2", NULL
);
1472 test(0, "puttygen", "-L", scfilename
, NULL
);
1475 * List the public half of the ssh.com-formatted key in
1476 * IETF/ssh.com format.
1478 setup_passphrases("sponge2", NULL
);
1479 test(0, "puttygen", "-p", scfilename
, NULL
);
1484 * Convert from OpenSSH into ssh.com.
1486 setup_passphrases("sponge2", NULL
);
1487 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1488 "-O", "private-sshcom", NULL
);
1491 * Convert from ssh.com back into a PuTTY key,
1492 * supplying the same comment as we had before we
1493 * started to ensure the comparison works.
1495 setup_passphrases("sponge2", NULL
);
1496 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1497 "-o", tmpfilename2
, NULL
);
1500 * See if the PuTTY key thus generated is the same as
1503 filecmp(filename
, tmpfilename2
,
1504 "p->o->s->p encrypted %s", keytypes
[i
]);
1507 * Convert from ssh.com to OpenSSH.
1509 setup_passphrases("sponge2", NULL
);
1510 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1511 "-O", "private-openssh", NULL
);
1514 * Convert from OpenSSH back into a PuTTY key,
1515 * supplying the same comment as we had before we
1516 * started to ensure the comparison works.
1518 setup_passphrases("sponge2", NULL
);
1519 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1520 "-o", tmpfilename2
, NULL
);
1523 * See if the PuTTY key thus generated is the same as
1526 filecmp(filename
, tmpfilename2
,
1527 "p->s->o->p encrypted %s", keytypes
[i
]);
1530 * Finally, do a round-trip conversion between PuTTY
1531 * and ssh.com without involving OpenSSH, to test that
1532 * the key comment is preserved in that case.
1534 setup_passphrases("sponge2", NULL
);
1535 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1537 setup_passphrases("sponge2", NULL
);
1538 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1539 filecmp(filename
, tmpfilename2
,
1540 "p->s->p encrypted %s", keytypes
[i
]);
1544 * Load with the wrong passphrase.
1546 setup_passphrases("sponge8", NULL
);
1547 test(1, "puttygen", "-C", "spurious-new-comment", filename
, NULL
);
1550 * Load a totally bogus file.
1552 setup_passphrases(NULL
);
1553 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename
, NULL
);
1555 printf("%d passes, %d fails\n", passes
, fails
);