2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
19 * This section overrides some definitions below for test purposes.
20 * When compiled with -DTEST_CMDGEN:
22 * - Calls to get_random_data() are replaced with the diagnostic
23 * function below (I #define the name so that I can still link
24 * with the original set of modules without symbol clash), in
25 * order to avoid depleting the test system's /dev/random
28 * - Calls to console_get_userpass_input() are replaced with the
29 * diagnostic function below, so that I can run tests in an
30 * automated manner and provide their interactive passphrase
33 * - main() is renamed to cmdgen_main(); at the bottom of the file
34 * I define another main() which calls the former repeatedly to
37 #define get_random_data get_random_data_diagnostic
38 char *get_random_data(int len
)
40 char *buf
= snewn(len
, char);
41 memset(buf
, 'x', len
);
44 #define console_get_userpass_input console_get_userpass_input_diagnostic
45 int nprompts
, promptsgot
;
46 const char *prompts
[3];
47 int console_get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
51 for (i
= 0; i
< p
->n_prompts
; i
++) {
52 if (promptsgot
< nprompts
) {
53 assert(strlen(prompts
[promptsgot
]) < p
->prompts
[i
]->result_len
);
54 strcpy(p
->prompts
[i
]->result
, prompts
[promptsgot
++]);
56 promptsgot
++; /* track number of requests anyway */
62 #define main cmdgen_main
69 static void progress_update(void *param
, int action
, int phase
, int iprogress
)
71 struct progress
*p
= (struct progress
*)param
;
72 if (action
!= PROGFN_PROGRESS
)
74 if (phase
> p
->phase
) {
79 p
->current
= iprogress
- 1;
81 p
->current
= iprogress
;
83 while (p
->current
< iprogress
) {
90 static void no_progress(void *param
, int action
, int phase
, int iprogress
)
94 void modalfatalbox(char *p
, ...)
97 fprintf(stderr
, "FATAL ERROR: ");
99 vfprintf(stderr
, p
, ap
);
106 * Stubs to let everything else link sensibly.
108 void log_eventlog(void *handle
, const char *event
)
111 char *x_get_default(const char *key
)
115 void sk_cleanup(void)
119 void showversion(void)
121 char *verstr
= dupstr(ver
);
122 verstr
[0] = tolower(verstr
[0]);
123 printf("PuTTYgen %s\n", verstr
);
127 void usage(int standalone
)
130 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
131 " [ -C comment ] [ -P ] [ -q ]\n"
132 " [ -o output-keyfile ] [ -O type | -l | -L"
136 "Use \"puttygen --help\" for more detail.\n");
142 * Help message is an extended version of the usage message. So
143 * start with that, plus a version heading.
148 " -t specify key type when generating (rsa, dsa, rsa1)\n"
149 " -b specify number of bits when generating key\n"
150 " -C change or specify key comment\n"
151 " -P change key passphrase\n"
152 " -q quiet: do not display progress bar\n"
153 " -O specify output type:\n"
154 " private output PuTTY private key format\n"
155 " private-openssh export OpenSSH private key\n"
156 " private-sshcom export ssh.com private key\n"
157 " public standard / ssh.com public key\n"
158 " public-openssh OpenSSH public key\n"
159 " fingerprint output the key fingerprint\n"
160 " -o specify output file\n"
161 " -l equivalent to `-O fingerprint'\n"
162 " -L equivalent to `-O public-openssh'\n"
163 " -p equivalent to `-O public'\n"
167 static int save_ssh2_pubkey(char *filename
, char *comment
,
168 void *v_pub_blob
, int pub_len
)
170 unsigned char *pub_blob
= (unsigned char *)v_pub_blob
;
176 fp
= fopen(filename
, "wb");
182 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
185 fprintf(fp
, "Comment: \"");
186 for (p
= comment
; *p
; p
++) {
187 if (*p
== '\\' || *p
== '\"')
196 while (i
< pub_len
) {
198 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
199 base64_encode_atom(pub_blob
+ i
, n
, buf
);
203 if (++column
>= 16) {
211 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
217 static int move(char *from
, char *to
)
221 ret
= rename(from
, to
);
224 * This OS may require us to remove the original file first.
227 ret
= rename(from
, to
);
230 perror("puttygen: cannot move new file on to old one");
236 static char *blobfp(char *alg
, int bits
, char *blob
, int bloblen
)
239 unsigned char digest
[16];
240 struct MD5Context md5c
;
244 MD5Update(&md5c
, blob
, bloblen
);
245 MD5Final(digest
, &md5c
);
247 sprintf(buffer
, "%s ", alg
);
249 sprintf(buffer
+ strlen(buffer
), "%d ", bits
);
250 for (i
= 0; i
< 16; i
++)
251 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
254 return dupstr(buffer
);
257 int main(int argc
, char **argv
)
261 enum { NOKEYGEN
, RSA1
, RSA2
, DSA
} keytype
= NOKEYGEN
;
262 char *outfile
= NULL
, *outfiletmp
= NULL
;
263 Filename outfilename
;
264 enum { PRIVATE
, PUBLIC
, PUBLICO
, FP
, OPENSSH
, SSHCOM
} outtype
= PRIVATE
;
266 char *comment
= NULL
, *origcomment
= NULL
;
267 int change_passphrase
= FALSE
;
268 int errs
= FALSE
, nogo
= FALSE
;
269 int intype
= SSH_KEYTYPE_UNOPENABLE
;
271 struct ssh2_userkey
*ssh2key
= NULL
;
272 struct RSAKey
*ssh1key
= NULL
;
273 char *ssh2blob
= NULL
, *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 */
319 if (!strcmp(opt
, "-help")) {
322 } else if (!strcmp(opt
, "-version")) {
325 } else if (!strcmp(opt
, "-pgpfp")) {
326 /* support "-pgpfp" for consistency with others */
331 * A sample option requiring an argument:
333 * else if (!strcmp(opt, "-output")) {
335 * errs = TRUE, error(err_optnoarg, opt);
343 "puttygen: no such option `-%s'\n", opt
);
356 * Option requiring no parameter.
368 change_passphrase
= TRUE
;
380 progressfn
= no_progress
;
390 * Option requiring parameter.
396 fprintf(stderr
, "puttygen: option `-%c' expects a"
401 * Now c is the option and p is the parameter.
405 if (!strcmp(p
, "rsa") || !strcmp(p
, "rsa2"))
406 keytype
= RSA2
, sshver
= 2;
407 else if (!strcmp(p
, "rsa1"))
408 keytype
= RSA1
, sshver
= 1;
409 else if (!strcmp(p
, "dsa") || !strcmp(p
, "dss"))
410 keytype
= DSA
, sshver
= 2;
413 "puttygen: unknown key type `%s'\n", p
);
424 if (!strcmp(p
, "public"))
426 else if (!strcmp(p
, "public-openssh"))
428 else if (!strcmp(p
, "private"))
430 else if (!strcmp(p
, "fingerprint"))
432 else if (!strcmp(p
, "private-openssh"))
433 outtype
= OPENSSH
, sshver
= 2;
434 else if (!strcmp(p
, "private-sshcom"))
435 outtype
= SSHCOM
, sshver
= 2;
438 "puttygen: unknown output type `%s'\n", p
);
446 p
= NULL
; /* prevent continued processing */
450 * Unrecognised option.
453 fprintf(stderr
, "puttygen: no such option `-%c'\n", c
);
459 * A non-option argument.
465 fprintf(stderr
, "puttygen: cannot handle more than one"
478 * If run with at least one argument _but_ not the required
479 * ones, print the usage message and return failure.
481 if (!infile
&& keytype
== NOKEYGEN
) {
486 /* ------------------------------------------------------------------
487 * Figure out further details of exactly what we're going to do.
491 * Bomb out if we've been asked to both load and generate a
494 if (keytype
!= NOKEYGEN
&& infile
) {
495 fprintf(stderr
, "puttygen: cannot both load and generate a key\n");
500 * We must save the private part when generating a new key.
502 if (keytype
!= NOKEYGEN
&&
503 (outtype
!= PRIVATE
&& outtype
!= OPENSSH
&& outtype
!= SSHCOM
)) {
504 fprintf(stderr
, "puttygen: this would generate a new key but "
505 "discard the private part\n");
510 * Analyse the type of the input file, in case this affects our
514 infilename
= filename_from_str(infile
);
516 intype
= key_type(&infilename
);
520 * It would be nice here to be able to load _public_
521 * key files, in any of a number of forms, and (a)
522 * convert them to other public key types, (b) print
523 * out their fingerprints. Or, I suppose, for real
524 * orthogonality, (c) change their comment!
526 * In fact this opens some interesting possibilities.
527 * Suppose ssh2_userkey_loadpub() were able to load
528 * public key files as well as extracting the public
529 * key from private ones. And suppose I did the thing
530 * I've been wanting to do, where specifying a
531 * particular private key file for authentication
532 * causes any _other_ key in the agent to be discarded.
533 * Then, if you had an agent forwarded to the machine
534 * you were running Unix PuTTY or Plink on, and you
535 * needed to specify which of the keys in the agent it
536 * should use, you could do that by supplying a
537 * _public_ key file, thus not needing to trust even
538 * your encrypted private key file to the network. Ooh!
541 case SSH_KEYTYPE_UNOPENABLE
:
542 case SSH_KEYTYPE_UNKNOWN
:
543 fprintf(stderr
, "puttygen: unable to load file `%s': %s\n",
544 infile
, key_type_to_str(intype
));
547 case SSH_KEYTYPE_SSH1
:
549 fprintf(stderr
, "puttygen: conversion from SSH-1 to SSH-2 keys"
556 case SSH_KEYTYPE_SSH2
:
557 case SSH_KEYTYPE_OPENSSH
:
558 case SSH_KEYTYPE_SSHCOM
:
560 fprintf(stderr
, "puttygen: conversion from SSH-2 to SSH-1 keys"
570 * Determine the default output file, if none is provided.
572 * This will usually be equal to stdout, except that if the
573 * input and output file formats are the same then the default
574 * output is to overwrite the input.
576 * Also in this code, we bomb out if the input and output file
577 * formats are the same and no other action is performed.
579 if ((intype
== SSH_KEYTYPE_SSH1
&& outtype
== PRIVATE
) ||
580 (intype
== SSH_KEYTYPE_SSH2
&& outtype
== PRIVATE
) ||
581 (intype
== SSH_KEYTYPE_OPENSSH
&& outtype
== OPENSSH
) ||
582 (intype
== SSH_KEYTYPE_SSHCOM
&& outtype
== SSHCOM
)) {
585 outfiletmp
= dupcat(outfile
, ".tmp", NULL
);
588 if (!change_passphrase
&& !comment
) {
589 fprintf(stderr
, "puttygen: this command would perform no useful"
596 * Bomb out rather than automatically choosing to write
597 * a private key file to stdout.
599 if (outtype
==PRIVATE
|| outtype
==OPENSSH
|| outtype
==SSHCOM
) {
600 fprintf(stderr
, "puttygen: need to specify an output file\n");
607 * Figure out whether we need to load the encrypted part of the
608 * key. This will be the case if either (a) we need to write
609 * out a private key format, or (b) the entire input key file
612 if (outtype
== PRIVATE
|| outtype
== OPENSSH
|| outtype
== SSHCOM
||
613 intype
== SSH_KEYTYPE_OPENSSH
|| intype
== SSH_KEYTYPE_SSHCOM
)
614 load_encrypted
= TRUE
;
616 load_encrypted
= FALSE
;
618 /* ------------------------------------------------------------------
619 * Now we're ready to actually do some stuff.
623 * Either load or generate a key.
625 if (keytype
!= NOKEYGEN
) {
627 char default_comment
[80];
629 struct progress prog
;
636 strftime(default_comment
, 30, "dsa-key-%Y%m%d", &tm
);
638 strftime(default_comment
, 30, "rsa-key-%Y%m%d", &tm
);
641 entropy
= get_random_data(bits
/ 8);
642 random_add_heavynoise(entropy
, bits
/ 8);
643 memset(entropy
, 0, bits
/8);
646 if (keytype
== DSA
) {
647 struct dss_key
*dsskey
= snew(struct dss_key
);
648 dsa_generate(dsskey
, bits
, progressfn
, &prog
);
649 ssh2key
= snew(struct ssh2_userkey
);
650 ssh2key
->data
= dsskey
;
651 ssh2key
->alg
= &ssh_dss
;
654 struct RSAKey
*rsakey
= snew(struct RSAKey
);
655 rsa_generate(rsakey
, bits
, progressfn
, &prog
);
656 rsakey
->comment
= NULL
;
657 if (keytype
== RSA1
) {
660 ssh2key
= snew(struct ssh2_userkey
);
661 ssh2key
->data
= rsakey
;
662 ssh2key
->alg
= &ssh_rsa
;
665 progressfn(&prog
, PROGFN_PROGRESS
, INT_MAX
, -1);
668 ssh2key
->comment
= dupstr(default_comment
);
670 ssh1key
->comment
= dupstr(default_comment
);
673 const char *error
= NULL
;
676 assert(infile
!= NULL
);
679 * Find out whether the input key is encrypted.
681 if (intype
== SSH_KEYTYPE_SSH1
)
682 encrypted
= rsakey_encrypted(&infilename
, &origcomment
);
683 else if (intype
== SSH_KEYTYPE_SSH2
)
684 encrypted
= ssh2_userkey_encrypted(&infilename
, &origcomment
);
686 encrypted
= import_encrypted(&infilename
, intype
, &origcomment
);
689 * If so, ask for a passphrase.
691 if (encrypted
&& load_encrypted
) {
692 prompts_t
*p
= new_prompts(NULL
);
694 p
->to_server
= FALSE
;
695 p
->name
= dupstr("SSH key passphrase");
696 add_prompt(p
, dupstr("Enter passphrase to load key: "), FALSE
, 512);
697 ret
= console_get_userpass_input(p
, NULL
, 0);
701 perror("puttygen: unable to read passphrase");
704 passphrase
= dupstr(p
->prompts
[0]->result
);
714 case SSH_KEYTYPE_SSH1
:
715 ssh1key
= snew(struct RSAKey
);
716 if (!load_encrypted
) {
721 ret
= rsakey_pubblob(&infilename
, &vblob
, &bloblen
,
722 &origcomment
, &error
);
723 blob
= (char *)vblob
;
725 n
= 4; /* skip modulus bits */
727 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
730 error
= "SSH-1 public key blob was too short";
733 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
736 error
= "SSH-1 public key blob was too short";
740 ssh1key
->comment
= dupstr(origcomment
);
741 ssh1key
->private_exponent
= NULL
;
743 ret
= loadrsakey(&infilename
, ssh1key
, passphrase
, &error
);
748 error
= "unknown error";
751 case SSH_KEYTYPE_SSH2
:
752 if (!load_encrypted
) {
753 ssh2blob
= ssh2_userkey_loadpub(&infilename
, &ssh2alg
,
754 &ssh2bloblen
, NULL
, &error
);
755 ssh2algf
= find_pubkey_alg(ssh2alg
);
757 bits
= ssh2algf
->pubkey_bits(ssh2blob
, ssh2bloblen
);
761 ssh2key
= ssh2_load_userkey(&infilename
, passphrase
, &error
);
763 if ((ssh2key
&& ssh2key
!= SSH2_WRONG_PASSPHRASE
) || ssh2blob
)
766 if (ssh2key
== SSH2_WRONG_PASSPHRASE
)
767 error
= "wrong passphrase";
769 error
= "unknown error";
773 case SSH_KEYTYPE_OPENSSH
:
774 case SSH_KEYTYPE_SSHCOM
:
775 ssh2key
= import_ssh2(&infilename
, intype
, passphrase
, &error
);
777 if (ssh2key
!= SSH2_WRONG_PASSPHRASE
)
780 error
= "wrong passphrase";
782 error
= "unknown error";
790 fprintf(stderr
, "puttygen: error loading `%s': %s\n",
797 * Change the comment if asked to.
802 sfree(ssh1key
->comment
);
803 ssh1key
->comment
= dupstr(comment
);
806 sfree(ssh2key
->comment
);
807 ssh2key
->comment
= dupstr(comment
);
812 * Prompt for a new passphrase if we have been asked to, or if
813 * we have just generated a key.
815 if (change_passphrase
|| keytype
!= NOKEYGEN
) {
816 prompts_t
*p
= new_prompts(NULL
);
819 p
->to_server
= FALSE
;
820 p
->name
= dupstr("New SSH key passphrase");
821 add_prompt(p
, dupstr("Enter passphrase to save key: "), FALSE
, 512);
822 add_prompt(p
, dupstr("Re-enter passphrase to verify: "), FALSE
, 512);
823 ret
= console_get_userpass_input(p
, NULL
, 0);
827 perror("puttygen: unable to read new passphrase");
830 if (strcmp(p
->prompts
[0]->result
, p
->prompts
[1]->result
)) {
832 fprintf(stderr
, "puttygen: passphrases do not match\n");
836 memset(passphrase
, 0, strlen(passphrase
));
839 passphrase
= dupstr(p
->prompts
[0]->result
);
851 * (In the case where outfile and outfiletmp are both NULL,
852 * there is no semantic reason to initialise outfilename at
853 * all; but we have to write _something_ to it or some compiler
854 * will probably complain that it might be used uninitialised.)
857 outfilename
= filename_from_str(outfiletmp
);
859 outfilename
= filename_from_str(outfile ? outfile
: "");
867 ret
= saversakey(&outfilename
, ssh1key
, passphrase
);
869 fprintf(stderr
, "puttygen: unable to save SSH-1 private key\n");
874 ret
= ssh2_save_userkey(&outfilename
, ssh2key
, passphrase
);
876 fprintf(stderr
, "puttygen: unable to save SSH-2 private key\n");
881 if (!move(outfiletmp
, outfile
))
882 return 1; /* rename failed */
895 fp
= f_open(outfilename
, "w");
898 dec1
= bignum_decimal(ssh1key
->exponent
);
899 dec2
= bignum_decimal(ssh1key
->modulus
);
900 fprintf(fp
, "%d %s %s %s\n", bignum_bitcount(ssh1key
->modulus
),
901 dec1
, dec2
, ssh1key
->comment
);
906 } else if (outtype
== PUBLIC
) {
909 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
912 save_ssh2_pubkey(outfile
, ssh2key ? ssh2key
->comment
: origcomment
,
913 ssh2blob
, ssh2bloblen
);
914 } else if (outtype
== PUBLICO
) {
921 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
926 ssh2alg
= ssh2key
->alg
->name
;
929 comment
= ssh2key
->comment
;
931 comment
= origcomment
;
933 buffer
= snewn(strlen(ssh2alg
) +
934 4 * ((ssh2bloblen
+2) / 3) +
935 strlen(comment
) + 3, char);
936 strcpy(buffer
, ssh2alg
);
937 p
= buffer
+ strlen(buffer
);
940 while (i
< ssh2bloblen
) {
941 int n
= (ssh2bloblen
- i
< 3 ? ssh2bloblen
- i
: 3);
942 base64_encode_atom(ssh2blob
+ i
, n
, p
);
953 fp
= f_open(outfilename
, "w");
956 fprintf(fp
, "%s\n", buffer
);
971 fingerprint
= snewn(128, char);
972 rsa_fingerprint(fingerprint
, 128, ssh1key
);
975 fingerprint
= ssh2key
->alg
->fingerprint(ssh2key
->data
);
978 fingerprint
= blobfp(ssh2alg
, bits
, ssh2blob
, ssh2bloblen
);
983 fp
= f_open(outfilename
, "w");
986 fprintf(fp
, "%s\n", fingerprint
);
998 ret
= export_ssh2(&outfilename
, outtype
, ssh2key
, passphrase
);
1000 fprintf(stderr
, "puttygen: unable to export key\n");
1004 if (!move(outfiletmp
, outfile
))
1005 return 1; /* rename failed */
1011 memset(passphrase
, 0, strlen(passphrase
));
1016 freersakey(ssh1key
);
1018 ssh2key
->alg
->freekey(ssh2key
->data
);
1033 void setup_passphrases(char *first
, ...)
1040 prompts
[nprompts
++] = first
;
1041 va_start(ap
, first
);
1042 while ((next
= va_arg(ap
, char *)) != NULL
) {
1043 assert(nprompts
< lenof(prompts
));
1044 prompts
[nprompts
++] = next
;
1050 void test(int retval
, ...)
1057 va_start(ap
, retval
);
1058 while (va_arg(ap
, char *) != NULL
)
1062 argv
= snewn(argc
+1, char *);
1063 va_start(ap
, retval
);
1064 for (i
= 0; i
<= argc
; i
++)
1065 argv
[i
] = va_arg(ap
, char *);
1069 ret
= cmdgen_main(argc
, argv
);
1071 if (ret
!= retval
) {
1072 printf("FAILED retval (exp %d got %d):", retval
, ret
);
1073 for (i
= 0; i
< argc
; i
++)
1074 printf(" %s", argv
[i
]);
1077 } else if (promptsgot
!= nprompts
) {
1078 printf("FAILED nprompts (exp %d got %d):", nprompts
, promptsgot
);
1079 for (i
= 0; i
< argc
; i
++)
1080 printf(" %s", argv
[i
]);
1088 void filecmp(char *file1
, char *file2
, char *fmt
, ...)
1091 * Ideally I should do file comparison myself, to maximise the
1092 * portability of this test suite once this application begins
1093 * running on non-Unix platforms. For the moment, though,
1094 * calling Unix diff is perfectly adequate.
1099 buf
= dupprintf("diff -q '%s' '%s'", file1
, file2
);
1106 printf("FAILED diff (ret=%d): ", ret
);
1119 char *cleanup_fp(char *s
)
1123 if (!strncmp(s
, "ssh-", 4)) {
1124 s
+= strcspn(s
, " \n\t");
1125 s
+= strspn(s
, " \n\t");
1129 s
+= strcspn(s
, " \n\t");
1130 s
+= strspn(s
, " \n\t");
1131 s
+= strcspn(s
, " \n\t");
1133 return dupprintf("%.*s", s
- p
, p
);
1136 char *get_fp(char *filename
)
1139 char buf
[256], *ret
;
1141 fp
= fopen(filename
, "r");
1144 ret
= fgets(buf
, sizeof(buf
), fp
);
1148 return cleanup_fp(buf
);
1151 void check_fp(char *filename
, char *fp
, char *fmt
, ...)
1158 newfp
= get_fp(filename
);
1160 if (!strcmp(fp
, newfp
)) {
1165 printf("FAILED check_fp ['%s' != '%s']: ", newfp
, fp
);
1179 int main(int argc
, char **argv
)
1182 static char *const keytypes
[] = { "rsa1", "dsa", "rsa" };
1185 * Even when this thing is compiled for automatic test mode,
1186 * it's helpful to be able to invoke it with command-line
1187 * options for _manual_ tests.
1190 return cmdgen_main(argc
, argv
);
1194 for (i
= 0; i
< lenof(keytypes
); i
++) {
1195 char filename
[128], osfilename
[128], scfilename
[128];
1196 char pubfilename
[128], tmpfilename1
[128], tmpfilename2
[128];
1199 sprintf(filename
, "test-%s.ppk", keytypes
[i
]);
1200 sprintf(pubfilename
, "test-%s.pub", keytypes
[i
]);
1201 sprintf(osfilename
, "test-%s.os", keytypes
[i
]);
1202 sprintf(scfilename
, "test-%s.sc", keytypes
[i
]);
1203 sprintf(tmpfilename1
, "test-%s.tmp1", keytypes
[i
]);
1204 sprintf(tmpfilename2
, "test-%s.tmp2", keytypes
[i
]);
1207 * Create an encrypted key.
1209 setup_passphrases("sponge", "sponge", NULL
);
1210 test(0, "puttygen", "-t", keytypes
[i
], "-o", filename
, NULL
);
1213 * List the public key in OpenSSH format.
1215 setup_passphrases(NULL
);
1216 test(0, "puttygen", "-L", filename
, "-o", pubfilename
, NULL
);
1220 sprintf(cmdbuf
, "ssh-keygen -l -f '%s' > '%s'",
1221 pubfilename
, tmpfilename1
);
1222 if (system(cmdbuf
) ||
1223 (fp
= get_fp(tmpfilename1
)) == NULL
) {
1224 printf("UNABLE to test fingerprint matching against OpenSSH");
1229 * List the public key in IETF/ssh.com format.
1231 setup_passphrases(NULL
);
1232 test(0, "puttygen", "-p", filename
, NULL
);
1235 * List the fingerprint of the key.
1237 setup_passphrases(NULL
);
1238 test(0, "puttygen", "-l", filename
, "-o", tmpfilename1
, NULL
);
1241 * If we can't test fingerprints against OpenSSH, we
1242 * can at the very least test equality of all the
1243 * fingerprints we generate of this key throughout
1246 fp
= get_fp(tmpfilename1
);
1248 check_fp(tmpfilename1
, fp
, "%s initial fp", keytypes
[i
]);
1252 * Change the comment of the key; this _does_ require a
1253 * passphrase owing to the tamperproofing.
1255 * NOTE: In SSH-1, this only requires a passphrase because
1256 * of inadequacies of the loading and saving mechanisms. In
1257 * _principle_, it should be perfectly possible to modify
1258 * the comment on an SSH-1 key without requiring a
1259 * passphrase; the only reason I can't do it is because my
1260 * loading and saving mechanisms don't include a method of
1261 * loading all the key data without also trying to decrypt
1262 * the private section.
1264 * I don't consider this to be a problem worth solving,
1265 * because (a) to fix it would probably end up bloating
1266 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1267 * it shouldn't be highly significant. If it seriously
1268 * bothers anyone then perhaps I _might_ be persuadable.
1270 setup_passphrases("sponge", NULL
);
1271 test(0, "puttygen", "-C", "new-comment", filename
, NULL
);
1274 * Change the passphrase to nothing.
1276 setup_passphrases("sponge", "", "", NULL
);
1277 test(0, "puttygen", "-P", filename
, NULL
);
1280 * Change the comment of the key again; this time we expect no
1281 * passphrase to be required.
1283 setup_passphrases(NULL
);
1284 test(0, "puttygen", "-C", "new-comment-2", filename
, NULL
);
1287 * Export the private key into OpenSSH format; no passphrase
1288 * should be required since the key is currently unencrypted.
1289 * For RSA1 keys, this should give an error.
1291 setup_passphrases(NULL
);
1292 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1297 * List the fingerprint of the OpenSSH-formatted key.
1299 setup_passphrases(NULL
);
1300 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1301 check_fp(tmpfilename1
, fp
, "%s openssh clear fp", keytypes
[i
]);
1304 * List the public half of the OpenSSH-formatted key in
1307 setup_passphrases(NULL
);
1308 test(0, "puttygen", "-L", osfilename
, NULL
);
1311 * List the public half of the OpenSSH-formatted key in
1312 * IETF/ssh.com format.
1314 setup_passphrases(NULL
);
1315 test(0, "puttygen", "-p", osfilename
, NULL
);
1319 * Export the private key into ssh.com format; no passphrase
1320 * should be required since the key is currently unencrypted.
1321 * For RSA1 keys, this should give an error.
1323 setup_passphrases(NULL
);
1324 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1329 * List the fingerprint of the ssh.com-formatted key.
1331 setup_passphrases(NULL
);
1332 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1333 check_fp(tmpfilename1
, fp
, "%s ssh.com clear fp", keytypes
[i
]);
1336 * List the public half of the ssh.com-formatted key in
1339 setup_passphrases(NULL
);
1340 test(0, "puttygen", "-L", scfilename
, NULL
);
1343 * List the public half of the ssh.com-formatted key in
1344 * IETF/ssh.com format.
1346 setup_passphrases(NULL
);
1347 test(0, "puttygen", "-p", scfilename
, NULL
);
1352 * Convert from OpenSSH into ssh.com.
1354 setup_passphrases(NULL
);
1355 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1356 "-O", "private-sshcom", NULL
);
1359 * Convert from ssh.com back into a PuTTY key,
1360 * supplying the same comment as we had before we
1361 * started to ensure the comparison works.
1363 setup_passphrases(NULL
);
1364 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1365 "-o", tmpfilename2
, NULL
);
1368 * See if the PuTTY key thus generated is the same as
1371 filecmp(filename
, tmpfilename2
,
1372 "p->o->s->p clear %s", keytypes
[i
]);
1375 * Convert from ssh.com to OpenSSH.
1377 setup_passphrases(NULL
);
1378 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1379 "-O", "private-openssh", NULL
);
1382 * Convert from OpenSSH back into a PuTTY key,
1383 * supplying the same comment as we had before we
1384 * started to ensure the comparison works.
1386 setup_passphrases(NULL
);
1387 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1388 "-o", tmpfilename2
, NULL
);
1391 * See if the PuTTY key thus generated is the same as
1394 filecmp(filename
, tmpfilename2
,
1395 "p->s->o->p clear %s", keytypes
[i
]);
1398 * Finally, do a round-trip conversion between PuTTY
1399 * and ssh.com without involving OpenSSH, to test that
1400 * the key comment is preserved in that case.
1402 setup_passphrases(NULL
);
1403 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1405 setup_passphrases(NULL
);
1406 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1407 filecmp(filename
, tmpfilename2
,
1408 "p->s->p clear %s", keytypes
[i
]);
1412 * Check that mismatched passphrases cause an error.
1414 setup_passphrases("sponge2", "sponge3", NULL
);
1415 test(1, "puttygen", "-P", filename
, NULL
);
1418 * Put a passphrase back on.
1420 setup_passphrases("sponge2", "sponge2", NULL
);
1421 test(0, "puttygen", "-P", filename
, NULL
);
1424 * Export the private key into OpenSSH format, this time
1425 * while encrypted. For RSA1 keys, this should give an
1429 setup_passphrases(NULL
); /* error, hence no passphrase read */
1431 setup_passphrases("sponge2", NULL
);
1432 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1437 * List the fingerprint of the OpenSSH-formatted key.
1439 setup_passphrases("sponge2", NULL
);
1440 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1441 check_fp(tmpfilename1
, fp
, "%s openssh encrypted fp", keytypes
[i
]);
1444 * List the public half of the OpenSSH-formatted key in
1447 setup_passphrases("sponge2", NULL
);
1448 test(0, "puttygen", "-L", osfilename
, NULL
);
1451 * List the public half of the OpenSSH-formatted key in
1452 * IETF/ssh.com format.
1454 setup_passphrases("sponge2", NULL
);
1455 test(0, "puttygen", "-p", osfilename
, NULL
);
1459 * Export the private key into ssh.com format, this time
1460 * while encrypted. For RSA1 keys, this should give an
1464 setup_passphrases(NULL
); /* error, hence no passphrase read */
1466 setup_passphrases("sponge2", NULL
);
1467 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1472 * List the fingerprint of the ssh.com-formatted key.
1474 setup_passphrases("sponge2", NULL
);
1475 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1476 check_fp(tmpfilename1
, fp
, "%s ssh.com encrypted fp", keytypes
[i
]);
1479 * List the public half of the ssh.com-formatted key in
1482 setup_passphrases("sponge2", NULL
);
1483 test(0, "puttygen", "-L", scfilename
, NULL
);
1486 * List the public half of the ssh.com-formatted key in
1487 * IETF/ssh.com format.
1489 setup_passphrases("sponge2", NULL
);
1490 test(0, "puttygen", "-p", scfilename
, NULL
);
1495 * Convert from OpenSSH into ssh.com.
1497 setup_passphrases("sponge2", NULL
);
1498 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1499 "-O", "private-sshcom", NULL
);
1502 * Convert from ssh.com back into a PuTTY key,
1503 * supplying the same comment as we had before we
1504 * started to ensure the comparison works.
1506 setup_passphrases("sponge2", NULL
);
1507 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1508 "-o", tmpfilename2
, NULL
);
1511 * See if the PuTTY key thus generated is the same as
1514 filecmp(filename
, tmpfilename2
,
1515 "p->o->s->p encrypted %s", keytypes
[i
]);
1518 * Convert from ssh.com to OpenSSH.
1520 setup_passphrases("sponge2", NULL
);
1521 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1522 "-O", "private-openssh", NULL
);
1525 * Convert from OpenSSH back into a PuTTY key,
1526 * supplying the same comment as we had before we
1527 * started to ensure the comparison works.
1529 setup_passphrases("sponge2", NULL
);
1530 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1531 "-o", tmpfilename2
, NULL
);
1534 * See if the PuTTY key thus generated is the same as
1537 filecmp(filename
, tmpfilename2
,
1538 "p->s->o->p encrypted %s", keytypes
[i
]);
1541 * Finally, do a round-trip conversion between PuTTY
1542 * and ssh.com without involving OpenSSH, to test that
1543 * the key comment is preserved in that case.
1545 setup_passphrases("sponge2", NULL
);
1546 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1548 setup_passphrases("sponge2", NULL
);
1549 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1550 filecmp(filename
, tmpfilename2
,
1551 "p->s->p encrypted %s", keytypes
[i
]);
1555 * Load with the wrong passphrase.
1557 setup_passphrases("sponge8", NULL
);
1558 test(1, "puttygen", "-C", "spurious-new-comment", filename
, NULL
);
1561 * Load a totally bogus file.
1563 setup_passphrases(NULL
);
1564 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename
, NULL
);
1566 printf("%d passes, %d fails\n", passes
, fails
);