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_line() are replaced with the diagnostic
29 * function below, so that I can run tests in an automated
30 * manner and provide their interactive passphrase inputs.
32 * - main() is renamed to cmdgen_main(); at the bottom of the file
33 * I define another main() which calls the former repeatedly to
36 #define get_random_data get_random_data_diagnostic
37 char *get_random_data(int len
)
39 char *buf
= snewn(len
, char);
40 memset(buf
, 'x', len
);
43 #define console_get_line console_get_line_diagnostic
44 int nprompts
, promptsgot
;
45 const char *prompts
[3];
46 int console_get_line(const char *prompt
, char *str
, int maxlen
, int is_pw
)
48 if (promptsgot
< nprompts
) {
49 assert(strlen(prompts
[promptsgot
]) < maxlen
);
50 strcpy(str
, prompts
[promptsgot
++]);
53 promptsgot
++; /* track number of requests anyway */
57 #define main cmdgen_main
64 static void progress_update(void *param
, int action
, int phase
, int iprogress
)
66 struct progress
*p
= (struct progress
*)param
;
67 if (action
!= PROGFN_PROGRESS
)
69 if (phase
> p
->phase
) {
74 p
->current
= iprogress
- 1;
76 p
->current
= iprogress
;
78 while (p
->current
< iprogress
) {
85 static void no_progress(void *param
, int action
, int phase
, int iprogress
)
89 void modalfatalbox(char *p
, ...)
92 fprintf(stderr
, "FATAL ERROR: ");
94 vfprintf(stderr
, p
, ap
);
101 * Stubs to let everything else link sensibly.
103 void log_eventlog(void *handle
, const char *event
)
106 char *x_get_default(const char *key
)
110 void sk_cleanup(void)
114 void showversion(void)
116 char *verstr
= dupstr(ver
);
117 verstr
[0] = tolower(verstr
[0]);
118 printf("PuTTYgen %s\n", verstr
);
125 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
126 " [ -C comment ] [ -P ]\n"
127 " [ -o output-keyfile ] [ -O type | -l | -L"
134 * Help message is an extended version of the usage message. So
135 * start with that, plus a version heading.
140 " -t specify key type when generating (rsa, dsa, rsa1)\n"
141 " -b specify number of bits when generating key\n"
142 " -C change or specify key comment\n"
143 " -P change key passphrase\n"
144 " -O specify output type:\n"
145 " private output PuTTY private key format\n"
146 " private-openssh export OpenSSH private key\n"
147 " private-sshcom export ssh.com private key\n"
148 " public standard / ssh.com public key\n"
149 " public-openssh OpenSSH public key\n"
150 " fingerprint output the key fingerprint\n"
151 " -o specify output file\n"
152 " -l equivalent to `-O fingerprint'\n"
153 " -L equivalent to `-O public-openssh'\n"
154 " -p equivalent to `-O public'\n"
158 static int save_ssh2_pubkey(char *filename
, char *comment
,
159 void *v_pub_blob
, int pub_len
)
161 unsigned char *pub_blob
= (unsigned char *)v_pub_blob
;
167 fp
= fopen(filename
, "wb");
173 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
176 fprintf(fp
, "Comment: \"");
177 for (p
= comment
; *p
; p
++) {
178 if (*p
== '\\' || *p
== '\"')
187 while (i
< pub_len
) {
189 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
190 base64_encode_atom(pub_blob
+ i
, n
, buf
);
194 if (++column
>= 16) {
202 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
208 static int move(char *from
, char *to
)
212 ret
= rename(from
, to
);
215 * This OS may require us to remove the original file first.
218 ret
= rename(from
, to
);
221 perror("puttygen: cannot move new file on to old one");
227 static char *blobfp(char *alg
, int bits
, char *blob
, int bloblen
)
230 unsigned char digest
[16];
231 struct MD5Context md5c
;
235 MD5Update(&md5c
, blob
, bloblen
);
236 MD5Final(digest
, &md5c
);
238 sprintf(buffer
, "%s ", alg
);
240 sprintf(buffer
+ strlen(buffer
), "%d ", bits
);
241 for (i
= 0; i
< 16; i
++)
242 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
245 return dupstr(buffer
);
248 int main(int argc
, char **argv
)
252 enum { NOKEYGEN
, RSA1
, RSA2
, DSA
} keytype
= NOKEYGEN
;
253 char *outfile
= NULL
, *outfiletmp
= NULL
;
254 Filename outfilename
;
255 enum { PRIVATE
, PUBLIC
, PUBLICO
, FP
, OPENSSH
, SSHCOM
} outtype
= PRIVATE
;
257 char *comment
= NULL
, *origcomment
= NULL
;
258 int change_passphrase
= FALSE
;
259 int errs
= FALSE
, nogo
= FALSE
;
260 int intype
= SSH_KEYTYPE_UNOPENABLE
;
262 struct ssh2_userkey
*ssh2key
= NULL
;
263 struct RSAKey
*ssh1key
= NULL
;
264 char *ssh2blob
= NULL
, *ssh2alg
= NULL
;
265 const struct ssh_signkey
*ssh2algf
= NULL
;
267 char *passphrase
= NULL
;
269 progfn_t progressfn
= is_interactive() ? progress_update
: no_progress
;
271 /* ------------------------------------------------------------------
272 * Parse the command line to figure out what we've been asked to do.
276 * If run with no arguments at all, print the usage message and
285 * Parse command line arguments.
302 opt
= p
++; /* opt will have _one_ leading - */
303 while (*p
&& *p
!= '=')
304 p
++; /* find end of option */
310 if (!strcmp(opt
, "-help")) {
313 } else if (!strcmp(opt
, "-version")) {
316 } else if (!strcmp(opt
, "-pgpfp")) {
317 /* support "-pgpfp" for consistency with others */
322 * A sample option requiring an argument:
324 * else if (!strcmp(opt, "-output")) {
326 * errs = TRUE, error(err_optnoarg, opt);
334 "puttygen: no such option `--%s'\n", opt
);
347 * Option requiring no parameter.
359 change_passphrase
= TRUE
;
371 progressfn
= no_progress
;
381 * Option requiring parameter.
387 fprintf(stderr
, "puttygen: option `-%c' expects a"
392 * Now c is the option and p is the parameter.
396 if (!strcmp(p
, "rsa") || !strcmp(p
, "rsa2"))
397 keytype
= RSA2
, sshver
= 2;
398 else if (!strcmp(p
, "rsa1"))
399 keytype
= RSA1
, sshver
= 1;
400 else if (!strcmp(p
, "dsa") || !strcmp(p
, "dss"))
401 keytype
= DSA
, sshver
= 2;
404 "puttygen: unknown key type `%s'\n", p
);
415 if (!strcmp(p
, "public"))
417 else if (!strcmp(p
, "public-openssh"))
419 else if (!strcmp(p
, "private"))
421 else if (!strcmp(p
, "fingerprint"))
423 else if (!strcmp(p
, "private-openssh"))
424 outtype
= OPENSSH
, sshver
= 2;
425 else if (!strcmp(p
, "private-sshcom"))
426 outtype
= SSHCOM
, sshver
= 2;
429 "puttygen: unknown output type `%s'\n", p
);
437 p
= NULL
; /* prevent continued processing */
441 * Unrecognised option.
444 fprintf(stderr
, "puttygen: no such option `-%c'\n", c
);
450 * A non-option argument.
456 fprintf(stderr
, "puttygen: cannot handle more than one"
469 * If run with at least one argument _but_ not the required
470 * ones, print the usage message and return failure.
472 if (!infile
&& keytype
== NOKEYGEN
) {
477 /* ------------------------------------------------------------------
478 * Figure out further details of exactly what we're going to do.
482 * Bomb out if we've been asked to both load and generate a
485 if (keytype
!= NOKEYGEN
&& intype
) {
486 fprintf(stderr
, "puttygen: cannot both load and generate a key\n");
491 * Analyse the type of the input file, in case this affects our
495 infilename
= filename_from_str(infile
);
497 intype
= key_type(&infilename
);
501 * It would be nice here to be able to load _public_
502 * key files, in any of a number of forms, and (a)
503 * convert them to other public key types, (b) print
504 * out their fingerprints. Or, I suppose, for real
505 * orthogonality, (c) change their comment!
507 * In fact this opens some interesting possibilities.
508 * Suppose ssh2_userkey_loadpub() were able to load
509 * public key files as well as extracting the public
510 * key from private ones. And suppose I did the thing
511 * I've been wanting to do, where specifying a
512 * particular private key file for authentication
513 * causes any _other_ key in the agent to be discarded.
514 * Then, if you had an agent forwarded to the machine
515 * you were running Unix PuTTY or Plink on, and you
516 * needed to specify which of the keys in the agent it
517 * should use, you could do that by supplying a
518 * _public_ key file, thus not needing to trust even
519 * your encrypted private key file to the network. Ooh!
522 case SSH_KEYTYPE_UNOPENABLE
:
523 case SSH_KEYTYPE_UNKNOWN
:
524 fprintf(stderr
, "puttygen: unable to load file `%s': %s\n",
525 infile
, key_type_to_str(intype
));
528 case SSH_KEYTYPE_SSH1
:
530 fprintf(stderr
, "puttygen: conversion from SSH-1 to SSH-2 keys"
537 case SSH_KEYTYPE_SSH2
:
538 case SSH_KEYTYPE_OPENSSH
:
539 case SSH_KEYTYPE_SSHCOM
:
541 fprintf(stderr
, "puttygen: conversion from SSH-2 to SSH-1 keys"
551 * Determine the default output file, if none is provided.
553 * This will usually be equal to stdout, except that if the
554 * input and output file formats are the same then the default
555 * output is to overwrite the input.
557 * Also in this code, we bomb out if the input and output file
558 * formats are the same and no other action is performed.
560 if ((intype
== SSH_KEYTYPE_SSH1
&& outtype
== PRIVATE
) ||
561 (intype
== SSH_KEYTYPE_SSH2
&& outtype
== PRIVATE
) ||
562 (intype
== SSH_KEYTYPE_OPENSSH
&& outtype
== OPENSSH
) ||
563 (intype
== SSH_KEYTYPE_SSHCOM
&& outtype
== SSHCOM
)) {
566 outfiletmp
= dupcat(outfile
, ".tmp", NULL
);
569 if (!change_passphrase
&& !comment
) {
570 fprintf(stderr
, "puttygen: this command would perform no useful"
577 * Bomb out rather than automatically choosing to write
578 * a private key file to stdout.
580 if (outtype
==PRIVATE
|| outtype
==OPENSSH
|| outtype
==SSHCOM
) {
581 fprintf(stderr
, "puttygen: need to specify an output file\n");
588 * Figure out whether we need to load the encrypted part of the
589 * key. This will be the case if either (a) we need to write
590 * out a private key format, or (b) the entire input key file
593 if (outtype
== PRIVATE
|| outtype
== OPENSSH
|| outtype
== SSHCOM
||
594 intype
== SSH_KEYTYPE_OPENSSH
|| intype
== SSH_KEYTYPE_SSHCOM
)
595 load_encrypted
= TRUE
;
597 load_encrypted
= FALSE
;
599 /* ------------------------------------------------------------------
600 * Now we're ready to actually do some stuff.
604 * Either load or generate a key.
606 if (keytype
!= NOKEYGEN
) {
608 char default_comment
[80];
610 struct progress prog
;
617 strftime(default_comment
, 30, "dsa-key-%Y%m%d", &tm
);
619 strftime(default_comment
, 30, "rsa-key-%Y%m%d", &tm
);
622 entropy
= get_random_data(bits
/ 8);
623 random_add_heavynoise(entropy
, bits
/ 8);
624 memset(entropy
, 0, bits
/8);
627 if (keytype
== DSA
) {
628 struct dss_key
*dsskey
= snew(struct dss_key
);
629 dsa_generate(dsskey
, bits
, progressfn
, &prog
);
630 ssh2key
= snew(struct ssh2_userkey
);
631 ssh2key
->data
= dsskey
;
632 ssh2key
->alg
= &ssh_dss
;
635 struct RSAKey
*rsakey
= snew(struct RSAKey
);
636 rsa_generate(rsakey
, bits
, progressfn
, &prog
);
637 rsakey
->comment
= NULL
;
638 if (keytype
== RSA1
) {
641 ssh2key
= snew(struct ssh2_userkey
);
642 ssh2key
->data
= rsakey
;
643 ssh2key
->alg
= &ssh_rsa
;
646 progressfn(&prog
, PROGFN_PROGRESS
, INT_MAX
, -1);
649 ssh2key
->comment
= dupstr(default_comment
);
651 ssh1key
->comment
= dupstr(default_comment
);
654 const char *error
= NULL
;
657 assert(infile
!= NULL
);
660 * Find out whether the input key is encrypted.
662 if (intype
== SSH_KEYTYPE_SSH1
)
663 encrypted
= rsakey_encrypted(&infilename
, &origcomment
);
664 else if (intype
== SSH_KEYTYPE_SSH2
)
665 encrypted
= ssh2_userkey_encrypted(&infilename
, &origcomment
);
667 encrypted
= import_encrypted(&infilename
, intype
, &origcomment
);
670 * If so, ask for a passphrase.
672 if (encrypted
&& load_encrypted
) {
673 passphrase
= snewn(512, char);
674 if (!console_get_line("Enter passphrase to load key: ",
675 passphrase
, 512, TRUE
)) {
676 perror("puttygen: unable to read passphrase");
686 case SSH_KEYTYPE_SSH1
:
687 ssh1key
= snew(struct RSAKey
);
688 if (!load_encrypted
) {
693 ret
= rsakey_pubblob(&infilename
, &vblob
, &bloblen
, NULL
,
695 blob
= (char *)vblob
;
697 n
= 4; /* skip modulus bits */
699 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
702 error
= "SSH-1 public key blob was too short";
705 l
= ssh1_read_bignum(blob
+ n
, bloblen
- n
,
708 error
= "SSH-1 public key blob was too short";
712 ssh1key
->comment
= NULL
;
713 ssh1key
->private_exponent
= NULL
;
715 ret
= loadrsakey(&infilename
, ssh1key
, passphrase
, &error
);
720 error
= "unknown error";
723 case SSH_KEYTYPE_SSH2
:
724 if (!load_encrypted
) {
725 ssh2blob
= ssh2_userkey_loadpub(&infilename
, &ssh2alg
,
726 &ssh2bloblen
, NULL
, &error
);
727 ssh2algf
= find_pubkey_alg(ssh2alg
);
729 bits
= ssh2algf
->pubkey_bits(ssh2blob
, ssh2bloblen
);
733 ssh2key
= ssh2_load_userkey(&infilename
, passphrase
, &error
);
735 if ((ssh2key
&& ssh2key
!= SSH2_WRONG_PASSPHRASE
) || ssh2blob
)
738 if (ssh2key
== SSH2_WRONG_PASSPHRASE
)
739 error
= "wrong passphrase";
741 error
= "unknown error";
745 case SSH_KEYTYPE_OPENSSH
:
746 case SSH_KEYTYPE_SSHCOM
:
747 ssh2key
= import_ssh2(&infilename
, intype
, passphrase
, &error
);
749 if (ssh2key
!= SSH2_WRONG_PASSPHRASE
)
752 error
= "wrong passphrase";
754 error
= "unknown error";
762 fprintf(stderr
, "puttygen: error loading `%s': %s\n",
769 * Change the comment if asked to.
774 sfree(ssh1key
->comment
);
775 ssh1key
->comment
= dupstr(comment
);
778 sfree(ssh2key
->comment
);
779 ssh2key
->comment
= dupstr(comment
);
784 * Prompt for a new passphrase if we have been asked to, or if
785 * we have just generated a key.
787 if (change_passphrase
|| keytype
!= NOKEYGEN
) {
791 memset(passphrase
, 0, strlen(passphrase
));
795 passphrase
= snewn(512, char);
796 passphrase2
= snewn(512, char);
797 if (!console_get_line("Enter passphrase to save key: ",
798 passphrase
, 512, TRUE
) ||
799 !console_get_line("Re-enter passphrase to verify: ",
800 passphrase2
, 512, TRUE
)) {
801 perror("puttygen: unable to read new passphrase");
804 if (strcmp(passphrase
, passphrase2
)) {
805 fprintf(stderr
, "puttygen: passphrases do not match\n");
808 memset(passphrase2
, 0, strlen(passphrase2
));
819 * (In the case where outfile and outfiletmp are both NULL,
820 * there is no semantic reason to initialise outfilename at
821 * all; but we have to write _something_ to it or some compiler
822 * will probably complain that it might be used uninitialised.)
825 outfilename
= filename_from_str(outfiletmp
);
827 outfilename
= filename_from_str(outfile ? outfile
: "");
835 ret
= saversakey(&outfilename
, ssh1key
, passphrase
);
837 fprintf(stderr
, "puttygen: unable to save SSH-1 private key\n");
842 ret
= ssh2_save_userkey(&outfilename
, ssh2key
, passphrase
);
844 fprintf(stderr
, "puttygen: unable to save SSH-2 private key\n");
849 if (!move(outfiletmp
, outfile
))
850 return 1; /* rename failed */
863 fp
= f_open(outfilename
, "w");
866 dec1
= bignum_decimal(ssh1key
->exponent
);
867 dec2
= bignum_decimal(ssh1key
->modulus
);
868 fprintf(fp
, "%d %s %s %s\n", bignum_bitcount(ssh1key
->modulus
),
869 dec1
, dec2
, ssh1key
->comment
);
874 } else if (outtype
== PUBLIC
) {
877 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
880 save_ssh2_pubkey(outfile
, ssh2key ? ssh2key
->comment
: origcomment
,
881 ssh2blob
, ssh2bloblen
);
882 } else if (outtype
== PUBLICO
) {
889 ssh2blob
= ssh2key
->alg
->public_blob(ssh2key
->data
,
894 ssh2alg
= ssh2key
->alg
->name
;
897 comment
= ssh2key
->comment
;
899 comment
= origcomment
;
901 buffer
= snewn(strlen(ssh2alg
) +
902 4 * ((ssh2bloblen
+2) / 3) +
903 strlen(comment
) + 3, char);
904 strcpy(buffer
, ssh2alg
);
905 p
= buffer
+ strlen(buffer
);
908 while (i
< ssh2bloblen
) {
909 int n
= (ssh2bloblen
- i
< 3 ? ssh2bloblen
- i
: 3);
910 base64_encode_atom(ssh2blob
+ i
, n
, p
);
921 fp
= f_open(outfilename
, "w");
924 fprintf(fp
, "%s\n", buffer
);
939 fingerprint
= snewn(128, char);
940 rsa_fingerprint(fingerprint
, 128, ssh1key
);
943 fingerprint
= ssh2key
->alg
->fingerprint(ssh2key
->data
);
946 fingerprint
= blobfp(ssh2alg
, bits
, ssh2blob
, ssh2bloblen
);
951 fp
= f_open(outfilename
, "w");
954 fprintf(fp
, "%s\n", fingerprint
);
966 ret
= export_ssh2(&outfilename
, outtype
, ssh2key
, passphrase
);
968 fprintf(stderr
, "puttygen: unable to export key\n");
972 if (!move(outfiletmp
, outfile
))
973 return 1; /* rename failed */
979 memset(passphrase
, 0, strlen(passphrase
));
986 ssh2key
->alg
->freekey(ssh2key
->data
);
1001 void setup_passphrases(char *first
, ...)
1008 prompts
[nprompts
++] = first
;
1009 va_start(ap
, first
);
1010 while ((next
= va_arg(ap
, char *)) != NULL
) {
1011 assert(nprompts
< lenof(prompts
));
1012 prompts
[nprompts
++] = next
;
1018 void test(int retval
, ...)
1025 va_start(ap
, retval
);
1026 while (va_arg(ap
, char *) != NULL
)
1030 argv
= snewn(argc
+1, char *);
1031 va_start(ap
, retval
);
1032 for (i
= 0; i
<= argc
; i
++)
1033 argv
[i
] = va_arg(ap
, char *);
1037 ret
= cmdgen_main(argc
, argv
);
1039 if (ret
!= retval
) {
1040 printf("FAILED retval (exp %d got %d):", retval
, ret
);
1041 for (i
= 0; i
< argc
; i
++)
1042 printf(" %s", argv
[i
]);
1045 } else if (promptsgot
!= nprompts
) {
1046 printf("FAILED nprompts (exp %d got %d):", nprompts
, promptsgot
);
1047 for (i
= 0; i
< argc
; i
++)
1048 printf(" %s", argv
[i
]);
1056 void filecmp(char *file1
, char *file2
, char *fmt
, ...)
1059 * Ideally I should do file comparison myself, to maximise the
1060 * portability of this test suite once this application begins
1061 * running on non-Unix platforms. For the moment, though,
1062 * calling Unix diff is perfectly adequate.
1067 buf
= dupprintf("diff -q '%s' '%s'", file1
, file2
);
1074 printf("FAILED diff (ret=%d): ", ret
);
1087 char *cleanup_fp(char *s
)
1091 if (!strncmp(s
, "ssh-", 4)) {
1092 s
+= strcspn(s
, " \n\t");
1093 s
+= strspn(s
, " \n\t");
1097 s
+= strcspn(s
, " \n\t");
1098 s
+= strspn(s
, " \n\t");
1099 s
+= strcspn(s
, " \n\t");
1101 return dupprintf("%.*s", s
- p
, p
);
1104 char *get_fp(char *filename
)
1107 char buf
[256], *ret
;
1109 fp
= fopen(filename
, "r");
1112 ret
= fgets(buf
, sizeof(buf
), fp
);
1116 return cleanup_fp(buf
);
1119 void check_fp(char *filename
, char *fp
, char *fmt
, ...)
1126 newfp
= get_fp(filename
);
1128 if (!strcmp(fp
, newfp
)) {
1133 printf("FAILED check_fp ['%s' != '%s']: ", newfp
, fp
);
1147 int main(int argc
, char **argv
)
1150 static char *const keytypes
[] = { "rsa1", "dsa", "rsa" };
1153 * Even when this thing is compiled for automatic test mode,
1154 * it's helpful to be able to invoke it with command-line
1155 * options for _manual_ tests.
1158 return cmdgen_main(argc
, argv
);
1162 for (i
= 0; i
< lenof(keytypes
); i
++) {
1163 char filename
[128], osfilename
[128], scfilename
[128];
1164 char pubfilename
[128], tmpfilename1
[128], tmpfilename2
[128];
1167 sprintf(filename
, "test-%s.ppk", keytypes
[i
]);
1168 sprintf(pubfilename
, "test-%s.pub", keytypes
[i
]);
1169 sprintf(osfilename
, "test-%s.os", keytypes
[i
]);
1170 sprintf(scfilename
, "test-%s.sc", keytypes
[i
]);
1171 sprintf(tmpfilename1
, "test-%s.tmp1", keytypes
[i
]);
1172 sprintf(tmpfilename2
, "test-%s.tmp2", keytypes
[i
]);
1175 * Create an encrypted key.
1177 setup_passphrases("sponge", "sponge", NULL
);
1178 test(0, "puttygen", "-t", keytypes
[i
], "-o", filename
, NULL
);
1181 * List the public key in OpenSSH format.
1183 setup_passphrases(NULL
);
1184 test(0, "puttygen", "-L", filename
, "-o", pubfilename
, NULL
);
1188 sprintf(cmdbuf
, "ssh-keygen -l -f '%s' > '%s'",
1189 pubfilename
, tmpfilename1
);
1190 if (system(cmdbuf
) ||
1191 (fp
= get_fp(tmpfilename1
)) == NULL
) {
1192 printf("UNABLE to test fingerprint matching against OpenSSH");
1197 * List the public key in IETF/ssh.com format.
1199 setup_passphrases(NULL
);
1200 test(0, "puttygen", "-p", filename
, NULL
);
1203 * List the fingerprint of the key.
1205 setup_passphrases(NULL
);
1206 test(0, "puttygen", "-l", filename
, "-o", tmpfilename1
, NULL
);
1209 * If we can't test fingerprints against OpenSSH, we
1210 * can at the very least test equality of all the
1211 * fingerprints we generate of this key throughout
1214 fp
= get_fp(tmpfilename1
);
1216 check_fp(tmpfilename1
, fp
, "%s initial fp", keytypes
[i
]);
1220 * Change the comment of the key; this _does_ require a
1221 * passphrase owing to the tamperproofing.
1223 * NOTE: In SSH-1, this only requires a passphrase because
1224 * of inadequacies of the loading and saving mechanisms. In
1225 * _principle_, it should be perfectly possible to modify
1226 * the comment on an SSH-1 key without requiring a
1227 * passphrase; the only reason I can't do it is because my
1228 * loading and saving mechanisms don't include a method of
1229 * loading all the key data without also trying to decrypt
1230 * the private section.
1232 * I don't consider this to be a problem worth solving,
1233 * because (a) to fix it would probably end up bloating
1234 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1235 * it shouldn't be highly significant. If it seriously
1236 * bothers anyone then perhaps I _might_ be persuadable.
1238 setup_passphrases("sponge", NULL
);
1239 test(0, "puttygen", "-C", "new-comment", filename
, NULL
);
1242 * Change the passphrase to nothing.
1244 setup_passphrases("sponge", "", "", NULL
);
1245 test(0, "puttygen", "-P", filename
, NULL
);
1248 * Change the comment of the key again; this time we expect no
1249 * passphrase to be required.
1251 setup_passphrases(NULL
);
1252 test(0, "puttygen", "-C", "new-comment-2", filename
, NULL
);
1255 * Export the private key into OpenSSH format; no passphrase
1256 * should be required since the key is currently unencrypted.
1257 * For RSA1 keys, this should give an error.
1259 setup_passphrases(NULL
);
1260 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1265 * List the fingerprint of the OpenSSH-formatted key.
1267 setup_passphrases(NULL
);
1268 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1269 check_fp(tmpfilename1
, fp
, "%s openssh clear fp", keytypes
[i
]);
1272 * List the public half of the OpenSSH-formatted key in
1275 setup_passphrases(NULL
);
1276 test(0, "puttygen", "-L", osfilename
, NULL
);
1279 * List the public half of the OpenSSH-formatted key in
1280 * IETF/ssh.com format.
1282 setup_passphrases(NULL
);
1283 test(0, "puttygen", "-p", osfilename
, NULL
);
1287 * Export the private key into ssh.com 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-sshcom", "-o", scfilename
,
1297 * List the fingerprint of the ssh.com-formatted key.
1299 setup_passphrases(NULL
);
1300 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1301 check_fp(tmpfilename1
, fp
, "%s ssh.com clear fp", keytypes
[i
]);
1304 * List the public half of the ssh.com-formatted key in
1307 setup_passphrases(NULL
);
1308 test(0, "puttygen", "-L", scfilename
, NULL
);
1311 * List the public half of the ssh.com-formatted key in
1312 * IETF/ssh.com format.
1314 setup_passphrases(NULL
);
1315 test(0, "puttygen", "-p", scfilename
, NULL
);
1320 * Convert from OpenSSH into ssh.com.
1322 setup_passphrases(NULL
);
1323 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1324 "-O", "private-sshcom", NULL
);
1327 * Convert from ssh.com back into a PuTTY key,
1328 * supplying the same comment as we had before we
1329 * started to ensure the comparison works.
1331 setup_passphrases(NULL
);
1332 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1333 "-o", tmpfilename2
, NULL
);
1336 * See if the PuTTY key thus generated is the same as
1339 filecmp(filename
, tmpfilename2
,
1340 "p->o->s->p clear %s", keytypes
[i
]);
1343 * Convert from ssh.com to OpenSSH.
1345 setup_passphrases(NULL
);
1346 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1347 "-O", "private-openssh", NULL
);
1350 * Convert from OpenSSH back into a PuTTY key,
1351 * supplying the same comment as we had before we
1352 * started to ensure the comparison works.
1354 setup_passphrases(NULL
);
1355 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1356 "-o", tmpfilename2
, NULL
);
1359 * See if the PuTTY key thus generated is the same as
1362 filecmp(filename
, tmpfilename2
,
1363 "p->s->o->p clear %s", keytypes
[i
]);
1366 * Finally, do a round-trip conversion between PuTTY
1367 * and ssh.com without involving OpenSSH, to test that
1368 * the key comment is preserved in that case.
1370 setup_passphrases(NULL
);
1371 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1373 setup_passphrases(NULL
);
1374 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1375 filecmp(filename
, tmpfilename2
,
1376 "p->s->p clear %s", keytypes
[i
]);
1380 * Check that mismatched passphrases cause an error.
1382 setup_passphrases("sponge2", "sponge3", NULL
);
1383 test(1, "puttygen", "-P", filename
, NULL
);
1386 * Put a passphrase back on.
1388 setup_passphrases("sponge2", "sponge2", NULL
);
1389 test(0, "puttygen", "-P", filename
, NULL
);
1392 * Export the private key into OpenSSH format, this time
1393 * while encrypted. For RSA1 keys, this should give an
1397 setup_passphrases(NULL
); /* error, hence no passphrase read */
1399 setup_passphrases("sponge2", NULL
);
1400 test((i
==0), "puttygen", "-O", "private-openssh", "-o", osfilename
,
1405 * List the fingerprint of the OpenSSH-formatted key.
1407 setup_passphrases("sponge2", NULL
);
1408 test(0, "puttygen", "-l", osfilename
, "-o", tmpfilename1
, NULL
);
1409 check_fp(tmpfilename1
, fp
, "%s openssh encrypted fp", keytypes
[i
]);
1412 * List the public half of the OpenSSH-formatted key in
1415 setup_passphrases("sponge2", NULL
);
1416 test(0, "puttygen", "-L", osfilename
, NULL
);
1419 * List the public half of the OpenSSH-formatted key in
1420 * IETF/ssh.com format.
1422 setup_passphrases("sponge2", NULL
);
1423 test(0, "puttygen", "-p", osfilename
, NULL
);
1427 * Export the private key into ssh.com format, this time
1428 * while encrypted. For RSA1 keys, this should give an
1432 setup_passphrases(NULL
); /* error, hence no passphrase read */
1434 setup_passphrases("sponge2", NULL
);
1435 test((i
==0), "puttygen", "-O", "private-sshcom", "-o", scfilename
,
1440 * List the fingerprint of the ssh.com-formatted key.
1442 setup_passphrases("sponge2", NULL
);
1443 test(0, "puttygen", "-l", scfilename
, "-o", tmpfilename1
, NULL
);
1444 check_fp(tmpfilename1
, fp
, "%s ssh.com encrypted fp", keytypes
[i
]);
1447 * List the public half of the ssh.com-formatted key in
1450 setup_passphrases("sponge2", NULL
);
1451 test(0, "puttygen", "-L", scfilename
, NULL
);
1454 * List the public half of the ssh.com-formatted key in
1455 * IETF/ssh.com format.
1457 setup_passphrases("sponge2", NULL
);
1458 test(0, "puttygen", "-p", scfilename
, NULL
);
1463 * Convert from OpenSSH into ssh.com.
1465 setup_passphrases("sponge2", NULL
);
1466 test(0, "puttygen", osfilename
, "-o", tmpfilename1
,
1467 "-O", "private-sshcom", NULL
);
1470 * Convert from ssh.com back into a PuTTY key,
1471 * supplying the same comment as we had before we
1472 * started to ensure the comparison works.
1474 setup_passphrases("sponge2", NULL
);
1475 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1476 "-o", tmpfilename2
, NULL
);
1479 * See if the PuTTY key thus generated is the same as
1482 filecmp(filename
, tmpfilename2
,
1483 "p->o->s->p encrypted %s", keytypes
[i
]);
1486 * Convert from ssh.com to OpenSSH.
1488 setup_passphrases("sponge2", NULL
);
1489 test(0, "puttygen", scfilename
, "-o", tmpfilename1
,
1490 "-O", "private-openssh", NULL
);
1493 * Convert from OpenSSH back into a PuTTY key,
1494 * supplying the same comment as we had before we
1495 * started to ensure the comparison works.
1497 setup_passphrases("sponge2", NULL
);
1498 test(0, "puttygen", tmpfilename1
, "-C", "new-comment-2",
1499 "-o", tmpfilename2
, NULL
);
1502 * See if the PuTTY key thus generated is the same as
1505 filecmp(filename
, tmpfilename2
,
1506 "p->s->o->p encrypted %s", keytypes
[i
]);
1509 * Finally, do a round-trip conversion between PuTTY
1510 * and ssh.com without involving OpenSSH, to test that
1511 * the key comment is preserved in that case.
1513 setup_passphrases("sponge2", NULL
);
1514 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1
,
1516 setup_passphrases("sponge2", NULL
);
1517 test(0, "puttygen", tmpfilename1
, "-o", tmpfilename2
, NULL
);
1518 filecmp(filename
, tmpfilename2
,
1519 "p->s->p encrypted %s", keytypes
[i
]);
1523 * Load with the wrong passphrase.
1525 setup_passphrases("sponge8", NULL
);
1526 test(1, "puttygen", "-C", "spurious-new-comment", filename
, NULL
);
1529 * Load a totally bogus file.
1531 setup_passphrases(NULL
);
1532 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename
, NULL
);
1534 printf("%d passes, %d fails\n", passes
, fails
);