2 * PuTTY key generation front end.
11 #define PUTTY_DO_GLOBALS
17 #define WM_DONEKEY (WM_XUSER + 1)
19 #define DEFAULT_KEYSIZE 1024
21 /* ----------------------------------------------------------------------
22 * Progress report code. This is really horrible :-)
24 #define PROGRESSRANGE 65535
30 unsigned startpoint
, total
;
31 unsigned param
, current
, n
; /* if exponential */
32 unsigned mult
; /* if linear */
34 unsigned total
, divisor
, range
;
38 static void progress_update(void *param
, int action
, int phase
, int iprogress
)
40 struct progress
*p
= (struct progress
*) param
;
41 unsigned progress
= iprogress
;
44 if (action
< PROGFN_READY
&& p
->nphases
< phase
)
47 case PROGFN_INITIALISE
:
50 case PROGFN_LIN_PHASE
:
51 p
->phases
[phase
-1].exponential
= 0;
52 p
->phases
[phase
-1].mult
= p
->phases
[phase
].total
/ progress
;
54 case PROGFN_EXP_PHASE
:
55 p
->phases
[phase
-1].exponential
= 1;
56 p
->phases
[phase
-1].param
= 0x10000 + progress
;
57 p
->phases
[phase
-1].current
= p
->phases
[phase
-1].total
;
58 p
->phases
[phase
-1].n
= 0;
60 case PROGFN_PHASE_EXTENT
:
61 p
->phases
[phase
-1].total
= progress
;
67 for (i
= 0; i
< p
->nphases
; i
++) {
68 p
->phases
[i
].startpoint
= total
;
69 total
+= p
->phases
[i
].total
;
72 p
->divisor
= ((p
->total
+ PROGRESSRANGE
- 1) / PROGRESSRANGE
);
73 p
->range
= p
->total
/ p
->divisor
;
74 SendMessage(p
->progbar
, PBM_SETRANGE
, 0, MAKELPARAM(0, p
->range
));
78 if (p
->phases
[phase
-1].exponential
) {
79 while (p
->phases
[phase
-1].n
< progress
) {
80 p
->phases
[phase
-1].n
++;
81 p
->phases
[phase
-1].current
*= p
->phases
[phase
-1].param
;
82 p
->phases
[phase
-1].current
/= 0x10000;
84 position
= (p
->phases
[phase
-1].startpoint
+
85 p
->phases
[phase
-1].total
- p
->phases
[phase
-1].current
);
87 position
= (p
->phases
[phase
-1].startpoint
+
88 progress
* p
->phases
[phase
-1].mult
);
90 SendMessage(p
->progbar
, PBM_SETPOS
, position
/ p
->divisor
, 0);
97 #define PASSPHRASE_MAXLEN 512
99 struct PassphraseProcStruct
{
105 * Dialog-box function for the passphrase box.
107 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
108 WPARAM wParam
, LPARAM lParam
)
110 static char *passphrase
= NULL
;
111 struct PassphraseProcStruct
*p
;
115 SetForegroundWindow(hwnd
);
116 SetWindowPos(hwnd
, HWND_TOP
, 0, 0, 0, 0,
117 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
122 { /* centre the window */
126 hw
= GetDesktopWindow();
127 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
129 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
130 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
131 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
134 p
= (struct PassphraseProcStruct
*) lParam
;
135 passphrase
= p
->passphrase
;
137 SetDlgItemText(hwnd
, 101, p
->comment
);
139 SetDlgItemText(hwnd
, 102, passphrase
);
142 switch (LOWORD(wParam
)) {
152 case 102: /* edit box */
153 if ((HIWORD(wParam
) == EN_CHANGE
) && passphrase
) {
154 GetDlgItemText(hwnd
, 102, passphrase
,
155 PASSPHRASE_MAXLEN
- 1);
156 passphrase
[PASSPHRASE_MAXLEN
- 1] = '\0';
169 * Prompt for a key file. Assumes the filename buffer is of size
172 static int prompt_keyfile(HWND hwnd
, char *dlgtitle
,
173 char *filename
, int save
)
176 memset(&of
, 0, sizeof(of
));
177 #ifdef OPENFILENAME_SIZE_VERSION_400
178 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
180 of
.lStructSize
= sizeof(of
);
183 of
.lpstrFilter
= "All Files\0*\0\0\0";
184 of
.lpstrCustomFilter
= NULL
;
186 of
.lpstrFile
= filename
;
188 of
.nMaxFile
= FILENAME_MAX
;
189 of
.lpstrFileTitle
= NULL
;
190 of
.lpstrInitialDir
= NULL
;
191 of
.lpstrTitle
= dlgtitle
;
194 return GetSaveFileName(&of
);
196 return GetOpenFileName(&of
);
200 * This function is needed to link with the DES code. We need not
201 * have it do anything at all.
203 void logevent(char *msg
)
208 * Dialog-box function for the Licence box.
210 static int CALLBACK
LicenceProc(HWND hwnd
, UINT msg
,
211 WPARAM wParam
, LPARAM lParam
)
218 { /* centre the window */
222 hw
= GetDesktopWindow();
223 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
225 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
226 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
227 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
232 switch (LOWORD(wParam
)) {
246 * Dialog-box function for the About box.
248 static int CALLBACK
AboutProc(HWND hwnd
, UINT msg
,
249 WPARAM wParam
, LPARAM lParam
)
256 { /* centre the window */
260 hw
= GetDesktopWindow();
261 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
263 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
264 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
265 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
268 SetDlgItemText(hwnd
, 100, ver
);
271 switch (LOWORD(wParam
)) {
276 EnableWindow(hwnd
, 0);
277 DialogBox(hinst
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
278 EnableWindow(hwnd
, 1);
279 SetActiveWindow(hwnd
);
291 * Thread to generate a key.
293 struct rsa_key_thread_params
{
294 HWND progressbar
; /* notify this with progress */
295 HWND dialog
; /* notify this on completion */
296 int keysize
; /* bits in key */
299 struct dss_key
*dsskey
;
301 static DWORD WINAPI
generate_rsa_key_thread(void *param
)
303 struct rsa_key_thread_params
*params
=
304 (struct rsa_key_thread_params
*) param
;
305 struct progress prog
;
306 prog
.progbar
= params
->progressbar
;
308 progress_update(&prog
, PROGFN_INITIALISE
, 0, 0);
311 dsa_generate(params
->dsskey
, params
->keysize
, progress_update
, &prog
);
313 rsa_generate(params
->key
, params
->keysize
, progress_update
, &prog
);
315 PostMessage(params
->dialog
, WM_DONEKEY
, 0, 0);
321 struct MainDlgState
{
322 int collecting_entropy
;
323 int generation_thread_exists
;
325 int entropy_got
, entropy_required
, entropy_size
;
328 char **commentptr
; /* points to key.comment or ssh2key.comment */
329 struct ssh2_userkey ssh2key
;
332 struct dss_key dsskey
;
335 static void hidemany(HWND hwnd
, const int *ids
, int hideit
)
338 ShowWindow(GetDlgItem(hwnd
, *ids
++), (hideit ? SW_HIDE
: SW_SHOW
));
342 static void setupbigedit1(HWND hwnd
, int id
, int idstatic
, struct RSAKey
*key
)
347 dec1
= bignum_decimal(key
->exponent
);
348 dec2
= bignum_decimal(key
->modulus
);
349 buffer
= smalloc(strlen(dec1
) + strlen(dec2
) +
350 strlen(key
->comment
) + 30);
351 sprintf(buffer
, "%d %s %s %s",
352 bignum_bitcount(key
->modulus
), dec1
, dec2
, key
->comment
);
353 SetDlgItemText(hwnd
, id
, buffer
);
354 SetDlgItemText(hwnd
, idstatic
,
355 "&Public key for pasting into authorized_keys file:");
361 static void setupbigedit2(HWND hwnd
, int id
, int idstatic
,
362 struct ssh2_userkey
*key
)
364 unsigned char *pub_blob
;
369 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_len
);
370 buffer
= smalloc(strlen(key
->alg
->name
) + 4 * ((pub_len
+ 2) / 3) +
371 strlen(key
->comment
) + 3);
372 strcpy(buffer
, key
->alg
->name
);
373 p
= buffer
+ strlen(buffer
);
376 while (i
< pub_len
) {
377 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
378 base64_encode_atom(pub_blob
+ i
, n
, p
);
383 strcpy(p
, key
->comment
);
384 SetDlgItemText(hwnd
, id
, buffer
);
385 SetDlgItemText(hwnd
, idstatic
, "&Public key for pasting into "
386 "OpenSSH authorized_keys2 file:");
391 static int save_ssh1_pubkey(char *filename
, struct RSAKey
*key
)
396 dec1
= bignum_decimal(key
->exponent
);
397 dec2
= bignum_decimal(key
->modulus
);
398 fp
= fopen(filename
, "wb");
401 fprintf(fp
, "%d %s %s %s\n",
402 bignum_bitcount(key
->modulus
), dec1
, dec2
, key
->comment
);
409 static int save_ssh2_pubkey(char *filename
, struct ssh2_userkey
*key
)
411 unsigned char *pub_blob
;
417 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_len
);
419 fp
= fopen(filename
, "wb");
423 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
425 fprintf(fp
, "Comment: \"");
426 for (p
= key
->comment
; *p
; p
++) {
427 if (*p
== '\\' || *p
== '\"')
435 while (i
< pub_len
) {
437 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
438 base64_encode_atom(pub_blob
+ i
, n
, buf
);
442 if (++column
>= 16) {
450 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
457 * Dialog-box function for the main PuTTYgen dialog box.
459 static int CALLBACK
MainDlgProc(HWND hwnd
, UINT msg
,
460 WPARAM wParam
, LPARAM lParam
)
463 controlidstart
= 100,
469 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
470 IDC_FPSTATIC
, IDC_FINGERPRINT
,
471 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
472 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
473 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
,
475 IDC_GENSTATIC
, IDC_GENERATE
,
476 IDC_LOADSTATIC
, IDC_LOAD
,
477 IDC_SAVESTATIC
, IDC_SAVE
, IDC_SAVEPUB
,
479 IDC_TYPESTATIC
, IDC_KEYSSH1
, IDC_KEYSSH2RSA
, IDC_KEYSSH2DSA
,
480 IDC_BITSSTATIC
, IDC_BITS
,
483 static const int nokey_ids
[] = { IDC_NOKEY
, 0 };
484 static const int generating_ids
[] =
485 { IDC_GENERATING
, IDC_PROGRESS
, 0 };
486 static const int gotkey_ids
[] = {
487 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
488 IDC_FPSTATIC
, IDC_FINGERPRINT
,
489 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
490 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
491 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
, 0
493 static const char generating_msg
[] =
494 "Please wait while a key is generated...";
495 static const char entropy_msg
[] =
496 "Please generate some randomness by moving the mouse over the blank area.";
497 struct MainDlgState
*state
;
504 { /* centre the window */
508 hw
= GetDesktopWindow();
509 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
511 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
512 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
513 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
516 state
= smalloc(sizeof(*state
));
517 state
->generation_thread_exists
= FALSE
;
518 state
->collecting_entropy
= FALSE
;
519 state
->entropy
= NULL
;
520 state
->key_exists
= FALSE
;
521 SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
) state
);
523 struct ctlpos cp
, cp2
;
525 /* Accelerators used: acglops1rbd */
527 ctlposinit(&cp
, hwnd
, 4, 4, 4);
528 bartitle(&cp
, "Public and private key generation for PuTTY",
530 beginbox(&cp
, "Key", IDC_BOX_KEY
);
532 statictext(&cp2
, "No key.", 1, IDC_NOKEY
);
534 statictext(&cp2
, "", 1, IDC_GENERATING
);
535 progressbar(&cp2
, IDC_PROGRESS
);
537 "&Public key for pasting into authorized_keys file:",
538 IDC_PKSTATIC
, IDC_KEYDISPLAY
, 5);
539 SendDlgItemMessage(hwnd
, IDC_KEYDISPLAY
, EM_SETREADONLY
, 1, 0);
540 staticedit(&cp
, "Key fingerprint:", IDC_FPSTATIC
,
541 IDC_FINGERPRINT
, 75);
542 SendDlgItemMessage(hwnd
, IDC_FINGERPRINT
, EM_SETREADONLY
, 1,
544 staticedit(&cp
, "Key &comment:", IDC_COMMENTSTATIC
,
545 IDC_COMMENTEDIT
, 75);
546 staticpassedit(&cp
, "Key p&assphrase:", IDC_PASSPHRASE1STATIC
,
547 IDC_PASSPHRASE1EDIT
, 75);
548 staticpassedit(&cp
, "C&onfirm passphrase:",
549 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
, 75);
551 beginbox(&cp
, "Actions", IDC_BOX_ACTIONS
);
552 staticbtn(&cp
, "Generate a public/private key pair",
553 IDC_GENSTATIC
, "&Generate", IDC_GENERATE
);
554 staticbtn(&cp
, "Load an existing private key file",
555 IDC_LOADSTATIC
, "&Load", IDC_LOAD
);
556 static2btn(&cp
, "Save the generated key", IDC_SAVESTATIC
,
557 "Save p&ublic key", IDC_SAVEPUB
,
558 "&Save private key", IDC_SAVE
);
560 beginbox(&cp
, "Parameters", IDC_BOX_PARAMS
);
561 radioline(&cp
, "Type of key to generate:", IDC_TYPESTATIC
, 3,
562 "SSH&1 (RSA)", IDC_KEYSSH1
,
563 "SSH2 &RSA", IDC_KEYSSH2RSA
,
564 "SSH2 &DSA", IDC_KEYSSH2DSA
, NULL
);
565 staticedit(&cp
, "Number of &bits in a generated key:",
566 IDC_BITSSTATIC
, IDC_BITS
, 20);
569 CheckRadioButton(hwnd
, IDC_KEYSSH1
, IDC_KEYSSH2DSA
, IDC_KEYSSH1
);
570 SetDlgItemInt(hwnd
, IDC_BITS
, DEFAULT_KEYSIZE
, FALSE
);
573 * Initially, hide the progress bar and the key display,
574 * and show the no-key display. Also disable the Save
575 * buttons, because with no key we obviously can't save
578 hidemany(hwnd
, nokey_ids
, FALSE
);
579 hidemany(hwnd
, generating_ids
, TRUE
);
580 hidemany(hwnd
, gotkey_ids
, TRUE
);
581 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
582 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 0);
586 state
= (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
587 if (state
->collecting_entropy
&&
588 state
->entropy
&& state
->entropy_got
< state
->entropy_required
) {
589 state
->entropy
[state
->entropy_got
++] = lParam
;
590 state
->entropy
[state
->entropy_got
++] = GetMessageTime();
591 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
,
592 state
->entropy_got
, 0);
593 if (state
->entropy_got
>= state
->entropy_required
) {
594 struct rsa_key_thread_params
*params
;
598 * Seed the entropy pool
600 random_add_heavynoise(state
->entropy
, state
->entropy_size
);
601 memset(state
->entropy
, 0, state
->entropy_size
);
602 sfree(state
->entropy
);
603 state
->collecting_entropy
= FALSE
;
605 SetDlgItemText(hwnd
, IDC_GENERATING
, generating_msg
);
606 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
607 MAKELPARAM(0, PROGRESSRANGE
));
608 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
610 params
= smalloc(sizeof(*params
));
611 params
->progressbar
= GetDlgItem(hwnd
, IDC_PROGRESS
);
612 params
->dialog
= hwnd
;
613 params
->keysize
= state
->keysize
;
614 params
->is_dsa
= state
->is_dsa
;
615 params
->key
= &state
->key
;
616 params
->dsskey
= &state
->dsskey
;
618 if (!CreateThread(NULL
, 0, generate_rsa_key_thread
,
619 params
, 0, &threadid
)) {
620 MessageBox(hwnd
, "Out of thread resources",
621 "Key generation error",
622 MB_OK
| MB_ICONERROR
);
625 state
->generation_thread_exists
= TRUE
;
631 switch (LOWORD(wParam
)) {
632 case IDC_COMMENTEDIT
:
633 if (HIWORD(wParam
) == EN_CHANGE
) {
634 state
= (struct MainDlgState
*)
635 GetWindowLong(hwnd
, GWL_USERDATA
);
636 if (state
->key_exists
) {
637 HWND editctl
= GetDlgItem(hwnd
, IDC_COMMENTEDIT
);
638 int len
= GetWindowTextLength(editctl
);
639 if (*state
->commentptr
)
640 sfree(*state
->commentptr
);
641 *state
->commentptr
= smalloc(len
+ 1);
642 GetWindowText(editctl
, *state
->commentptr
, len
+ 1);
644 setupbigedit2(hwnd
, IDC_KEYDISPLAY
, IDC_PKSTATIC
,
647 setupbigedit1(hwnd
, IDC_KEYDISPLAY
, IDC_PKSTATIC
,
654 EnableWindow(hwnd
, 0);
655 DialogBox(hinst
, MAKEINTRESOURCE(213), NULL
, AboutProc
);
656 EnableWindow(hwnd
, 1);
657 SetActiveWindow(hwnd
);
661 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
662 if (!state
->generation_thread_exists
) {
664 state
->keysize
= GetDlgItemInt(hwnd
, IDC_BITS
, &ok
, FALSE
);
666 state
->keysize
= DEFAULT_KEYSIZE
;
667 /* If we ever introduce a new key type, check it here! */
668 state
->ssh2
= !IsDlgButtonChecked(hwnd
, IDC_KEYSSH1
);
669 state
->is_dsa
= IsDlgButtonChecked(hwnd
, IDC_KEYSSH2DSA
);
670 if (state
->keysize
< 256) {
671 int ret
= MessageBox(hwnd
,
672 "PuTTYgen will not generate a key"
673 " smaller than 256 bits.\n"
674 "Key length reset to 256. Continue?",
676 MB_ICONWARNING
| MB_OKCANCEL
);
679 state
->keysize
= 256;
680 SetDlgItemInt(hwnd
, IDC_BITS
, 256, FALSE
);
682 hidemany(hwnd
, nokey_ids
, TRUE
);
683 hidemany(hwnd
, generating_ids
, FALSE
);
684 hidemany(hwnd
, gotkey_ids
, TRUE
);
685 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 0);
686 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 0);
687 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
688 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 0);
689 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH1
), 0);
690 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2RSA
), 0);
691 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2DSA
), 0);
692 EnableWindow(GetDlgItem(hwnd
, IDC_BITS
), 0);
693 state
->key_exists
= FALSE
;
694 SetDlgItemText(hwnd
, IDC_GENERATING
, entropy_msg
);
695 state
->collecting_entropy
= TRUE
;
698 * My brief statistical tests on mouse movements
699 * suggest that there are about 2.5 bits of
700 * randomness in the x position, 2.5 in the y
701 * position, and 1.7 in the message time, making
702 * 5.7 bits of unpredictability per mouse movement.
703 * However, other people have told me it's far less
704 * than that, so I'm going to be stupidly cautious
705 * and knock that down to a nice round 2. With this
706 * method, we require two words per mouse movement,
707 * so with 2 bits per mouse movement we expect 2
708 * bits every 2 words.
710 state
->entropy_required
= (state
->keysize
/ 2) * 2;
711 state
->entropy_got
= 0;
712 state
->entropy_size
= (state
->entropy_required
*
713 sizeof(*state
->entropy
));
714 state
->entropy
= smalloc(state
->entropy_size
);
716 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
717 MAKELPARAM(0, state
->entropy_required
));
718 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
723 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
724 if (state
->key_exists
) {
725 char filename
[FILENAME_MAX
];
726 char passphrase
[PASSPHRASE_MAXLEN
];
727 char passphrase2
[PASSPHRASE_MAXLEN
];
728 GetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
729 passphrase
, sizeof(passphrase
));
730 GetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
731 passphrase2
, sizeof(passphrase2
));
732 if (strcmp(passphrase
, passphrase2
)) {
734 "The two passphrases given do not match.",
735 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
740 ret
= MessageBox(hwnd
,
741 "Are you sure you want to save this key\n"
742 "without a passphrase to protect it?",
744 MB_YESNO
| MB_ICONWARNING
);
748 if (prompt_keyfile(hwnd
, "Save private key as:",
751 FILE *fp
= fopen(filename
, "r");
753 char buffer
[FILENAME_MAX
+ 80];
755 sprintf(buffer
, "Overwrite existing file\n%.*s?",
756 FILENAME_MAX
, filename
);
757 ret
= MessageBox(hwnd
, buffer
, "PuTTYgen Warning",
758 MB_YESNO
| MB_ICONWARNING
);
763 ret
= ssh2_save_userkey(filename
, &state
->ssh2key
,
764 *passphrase ? passphrase
:
767 ret
= saversakey(filename
, &state
->key
,
768 *passphrase ? passphrase
: NULL
);
771 MessageBox(hwnd
, "Unable to save key file",
772 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
779 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
780 if (state
->key_exists
) {
781 char filename
[FILENAME_MAX
];
782 if (prompt_keyfile(hwnd
, "Save public key as:",
785 FILE *fp
= fopen(filename
, "r");
787 char buffer
[FILENAME_MAX
+ 80];
789 sprintf(buffer
, "Overwrite existing file\n%.*s?",
790 FILENAME_MAX
, filename
);
791 ret
= MessageBox(hwnd
, buffer
, "PuTTYgen Warning",
792 MB_YESNO
| MB_ICONWARNING
);
797 ret
= save_ssh2_pubkey(filename
, &state
->ssh2key
);
799 ret
= save_ssh1_pubkey(filename
, &state
->key
);
802 MessageBox(hwnd
, "Unable to save key file",
803 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
810 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
811 if (!state
->generation_thread_exists
) {
812 char filename
[FILENAME_MAX
];
813 if (prompt_keyfile(hwnd
, "Load private key:", filename
, 0)) {
814 char passphrase
[PASSPHRASE_MAXLEN
];
819 struct PassphraseProcStruct pps
;
820 struct RSAKey newkey1
;
821 struct ssh2_userkey
*newkey2
= NULL
;
823 ver
= keyfile_version(filename
);
825 MessageBox(NULL
, "Couldn't load private key.",
826 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
832 needs_pass
= rsakey_encrypted(filename
, &comment
);
835 ssh2_userkey_encrypted(filename
, &comment
);
836 pps
.passphrase
= passphrase
;
837 pps
.comment
= comment
;
841 dlgret
= DialogBoxParam(hinst
,
842 MAKEINTRESOURCE(210),
843 NULL
, PassphraseProc
,
853 loadrsakey(filename
, &newkey1
, passphrase
);
856 ssh2_load_userkey(filename
, passphrase
);
857 if (newkey2
== SSH2_WRONG_PASSPHRASE
)
868 MessageBox(NULL
, "Couldn't load private key.",
869 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
870 } else if (ret
== 1) {
871 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
872 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
873 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
874 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 1);
875 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH1
), 1);
876 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2RSA
), 1);
877 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2DSA
), 1);
878 EnableWindow(GetDlgItem(hwnd
, IDC_BITS
), 1);
880 * Now update the key controls with all the
884 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
886 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
893 state
->commentptr
= &state
->key
.comment
;
894 state
->key
= newkey1
;
897 * Set the key fingerprint.
899 savecomment
= state
->key
.comment
;
900 state
->key
.comment
= NULL
;
901 rsa_fingerprint(buf
, sizeof(buf
),
903 state
->key
.comment
= savecomment
;
905 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
907 * Construct a decimal representation
908 * of the key, for pasting into
909 * .ssh/authorized_keys on a Unix box.
911 setupbigedit1(hwnd
, IDC_KEYDISPLAY
,
912 IDC_PKSTATIC
, &state
->key
);
919 &state
->ssh2key
.comment
;
920 state
->ssh2key
= *newkey2
; /* structure copy */
923 savecomment
= state
->ssh2key
.comment
;
924 state
->ssh2key
.comment
= NULL
;
927 fingerprint(state
->ssh2key
.data
);
928 state
->ssh2key
.comment
= savecomment
;
930 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, fp
);
933 setupbigedit2(hwnd
, IDC_KEYDISPLAY
,
934 IDC_PKSTATIC
, &state
->ssh2key
);
936 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
,
940 * Finally, hide the progress bar and show
943 hidemany(hwnd
, nokey_ids
, TRUE
);
944 hidemany(hwnd
, generating_ids
, TRUE
);
945 hidemany(hwnd
, gotkey_ids
, FALSE
);
946 state
->key_exists
= TRUE
;
954 state
= (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
955 state
->generation_thread_exists
= FALSE
;
956 state
->key_exists
= TRUE
;
957 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
958 MAKELPARAM(0, PROGRESSRANGE
));
959 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, PROGRESSRANGE
, 0);
960 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
961 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
962 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
963 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 1);
964 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH1
), 1);
965 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2RSA
), 1);
966 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2DSA
), 1);
967 EnableWindow(GetDlgItem(hwnd
, IDC_BITS
), 1);
970 state
->ssh2key
.data
= &state
->dsskey
;
971 state
->ssh2key
.alg
= &ssh_dss
;
973 state
->ssh2key
.data
= &state
->key
;
974 state
->ssh2key
.alg
= &ssh_rsa
;
976 state
->commentptr
= &state
->ssh2key
.comment
;
978 state
->commentptr
= &state
->key
.comment
;
981 * Invent a comment for the key. We'll do this by including
982 * the date in it. This will be so horrifyingly ugly that
983 * the user will immediately want to change it, which is
986 *state
->commentptr
= smalloc(30);
993 strftime(*state
->commentptr
, 30, "dsa-key-%Y%m%d", tm
);
995 strftime(*state
->commentptr
, 30, "rsa-key-%Y%m%d", tm
);
999 * Now update the key controls with all the key data.
1004 * Blank passphrase, initially. This isn't dangerous,
1005 * because we will warn (Are You Sure?) before allowing
1006 * the user to save an unprotected private key.
1008 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
, "");
1009 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
, "");
1013 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
, *state
->commentptr
);
1015 * Set the key fingerprint.
1017 savecomment
= *state
->commentptr
;
1018 *state
->commentptr
= NULL
;
1021 fp
= state
->ssh2key
.alg
->fingerprint(state
->ssh2key
.data
);
1022 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, fp
);
1026 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
1027 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
1029 *state
->commentptr
= savecomment
;
1031 * Construct a decimal representation of the key, for
1032 * pasting into .ssh/authorized_keys or
1033 * .ssh/authorized_keys2 on a Unix box.
1036 setupbigedit2(hwnd
, IDC_KEYDISPLAY
,
1037 IDC_PKSTATIC
, &state
->ssh2key
);
1039 setupbigedit1(hwnd
, IDC_KEYDISPLAY
,
1040 IDC_PKSTATIC
, &state
->key
);
1044 * Finally, hide the progress bar and show the key data.
1046 hidemany(hwnd
, nokey_ids
, TRUE
);
1047 hidemany(hwnd
, generating_ids
, TRUE
);
1048 hidemany(hwnd
, gotkey_ids
, FALSE
);
1051 state
= (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
1059 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1061 InitCommonControls();
1064 return DialogBox(hinst
, MAKEINTRESOURCE(201), NULL
,
1065 MainDlgProc
) != IDOK
;