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_LIN_PHASE
:
48 p
->phases
[phase
-1].exponential
= 0;
49 p
->phases
[phase
-1].mult
= p
->phases
[phase
].total
/ progress
;
51 case PROGFN_EXP_PHASE
:
52 p
->phases
[phase
-1].exponential
= 1;
53 p
->phases
[phase
-1].param
= 0x10000 + progress
;
54 p
->phases
[phase
-1].current
= p
->phases
[phase
-1].total
;
55 p
->phases
[phase
-1].n
= 0;
57 case PROGFN_PHASE_EXTENT
:
58 p
->phases
[phase
-1].total
= progress
;
64 for (i
= 0; i
< p
->nphases
; i
++) {
65 p
->phases
[i
].startpoint
= total
;
66 total
+= p
->phases
[i
].total
;
69 p
->divisor
= ((p
->total
+ PROGRESSRANGE
- 1) / PROGRESSRANGE
);
70 p
->range
= p
->total
/ p
->divisor
;
71 SendMessage(p
->progbar
, PBM_SETRANGE
, 0, MAKELPARAM(0, p
->range
));
75 if (p
->phases
[phase
-1].exponential
) {
76 while (p
->phases
[phase
-1].n
< progress
) {
77 p
->phases
[phase
-1].n
++;
78 p
->phases
[phase
-1].current
*= p
->phases
[phase
-1].param
;
79 p
->phases
[phase
-1].current
/= 0x10000;
81 position
= (p
->phases
[phase
-1].startpoint
+
82 p
->phases
[phase
-1].total
- p
->phases
[phase
-1].current
);
84 position
= (p
->phases
[phase
-1].startpoint
+
85 progress
* p
->phases
[phase
-1].mult
);
87 SendMessage(p
->progbar
, PBM_SETPOS
, position
/ p
->divisor
, 0);
94 #define PASSPHRASE_MAXLEN 512
96 struct PassphraseProcStruct
{
102 * Dialog-box function for the passphrase box.
104 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
105 WPARAM wParam
, LPARAM lParam
)
107 static char *passphrase
= NULL
;
108 struct PassphraseProcStruct
*p
;
112 SetForegroundWindow(hwnd
);
113 SetWindowPos(hwnd
, HWND_TOP
, 0, 0, 0, 0,
114 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
119 { /* centre the window */
123 hw
= GetDesktopWindow();
124 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
126 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
127 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
128 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
131 p
= (struct PassphraseProcStruct
*) lParam
;
132 passphrase
= p
->passphrase
;
134 SetDlgItemText(hwnd
, 101, p
->comment
);
136 SetDlgItemText(hwnd
, 102, passphrase
);
139 switch (LOWORD(wParam
)) {
149 case 102: /* edit box */
150 if ((HIWORD(wParam
) == EN_CHANGE
) && passphrase
) {
151 GetDlgItemText(hwnd
, 102, passphrase
,
152 PASSPHRASE_MAXLEN
- 1);
153 passphrase
[PASSPHRASE_MAXLEN
- 1] = '\0';
166 * Prompt for a key file. Assumes the filename buffer is of size
169 static int prompt_keyfile(HWND hwnd
, char *dlgtitle
,
170 char *filename
, int save
)
173 memset(&of
, 0, sizeof(of
));
174 #ifdef OPENFILENAME_SIZE_VERSION_400
175 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
177 of
.lStructSize
= sizeof(of
);
180 of
.lpstrFilter
= "All Files\0*\0\0\0";
181 of
.lpstrCustomFilter
= NULL
;
183 of
.lpstrFile
= filename
;
185 of
.nMaxFile
= FILENAME_MAX
;
186 of
.lpstrFileTitle
= NULL
;
187 of
.lpstrInitialDir
= NULL
;
188 of
.lpstrTitle
= dlgtitle
;
191 return GetSaveFileName(&of
);
193 return GetOpenFileName(&of
);
197 * This function is needed to link with the DES code. We need not
198 * have it do anything at all.
200 void logevent(char *msg
)
205 * Dialog-box function for the Licence box.
207 static int CALLBACK
LicenceProc(HWND hwnd
, UINT msg
,
208 WPARAM wParam
, LPARAM lParam
)
215 { /* centre the window */
219 hw
= GetDesktopWindow();
220 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
222 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
223 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
224 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
229 switch (LOWORD(wParam
)) {
243 * Dialog-box function for the About box.
245 static int CALLBACK
AboutProc(HWND hwnd
, UINT msg
,
246 WPARAM wParam
, LPARAM lParam
)
253 { /* centre the window */
257 hw
= GetDesktopWindow();
258 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
260 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
261 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
262 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
265 SetDlgItemText(hwnd
, 100, ver
);
268 switch (LOWORD(wParam
)) {
273 EnableWindow(hwnd
, 0);
274 DialogBox(hinst
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
275 EnableWindow(hwnd
, 1);
276 SetActiveWindow(hwnd
);
288 * Thread to generate a key.
290 struct rsa_key_thread_params
{
291 HWND progressbar
; /* notify this with progress */
292 HWND dialog
; /* notify this on completion */
293 int keysize
; /* bits in key */
296 struct dss_key
*dsskey
;
298 static DWORD WINAPI
generate_rsa_key_thread(void *param
)
300 struct rsa_key_thread_params
*params
=
301 (struct rsa_key_thread_params
*) param
;
302 struct progress prog
;
303 prog
.progbar
= params
->progressbar
;
306 dsa_generate(params
->dsskey
, params
->keysize
, progress_update
, &prog
);
308 rsa_generate(params
->key
, params
->keysize
, progress_update
, &prog
);
310 PostMessage(params
->dialog
, WM_DONEKEY
, 0, 0);
316 struct MainDlgState
{
317 int collecting_entropy
;
318 int generation_thread_exists
;
320 int entropy_got
, entropy_required
, entropy_size
;
323 char **commentptr
; /* points to key.comment or ssh2key.comment */
324 struct ssh2_userkey ssh2key
;
327 struct dss_key dsskey
;
330 static void hidemany(HWND hwnd
, const int *ids
, int hideit
)
333 ShowWindow(GetDlgItem(hwnd
, *ids
++), (hideit ? SW_HIDE
: SW_SHOW
));
337 static void setupbigedit1(HWND hwnd
, int id
, int idstatic
, struct RSAKey
*key
)
342 dec1
= bignum_decimal(key
->exponent
);
343 dec2
= bignum_decimal(key
->modulus
);
344 buffer
= smalloc(strlen(dec1
) + strlen(dec2
) +
345 strlen(key
->comment
) + 30);
346 sprintf(buffer
, "%d %s %s %s",
347 bignum_bitcount(key
->modulus
), dec1
, dec2
, key
->comment
);
348 SetDlgItemText(hwnd
, id
, buffer
);
349 SetDlgItemText(hwnd
, idstatic
,
350 "&Public key for pasting into authorized_keys file:");
356 static void setupbigedit2(HWND hwnd
, int id
, int idstatic
,
357 struct ssh2_userkey
*key
)
359 unsigned char *pub_blob
;
364 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_len
);
365 buffer
= smalloc(strlen(key
->alg
->name
) + 4 * ((pub_len
+ 2) / 3) +
366 strlen(key
->comment
) + 3);
367 strcpy(buffer
, key
->alg
->name
);
368 p
= buffer
+ strlen(buffer
);
371 while (i
< pub_len
) {
372 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
373 base64_encode_atom(pub_blob
+ i
, n
, p
);
378 strcpy(p
, key
->comment
);
379 SetDlgItemText(hwnd
, id
, buffer
);
380 SetDlgItemText(hwnd
, idstatic
, "&Public key for pasting into "
381 "OpenSSH authorized_keys2 file:");
386 static int save_ssh1_pubkey(char *filename
, struct RSAKey
*key
)
391 dec1
= bignum_decimal(key
->exponent
);
392 dec2
= bignum_decimal(key
->modulus
);
393 fp
= fopen(filename
, "wb");
396 fprintf(fp
, "%d %s %s %s\n",
397 bignum_bitcount(key
->modulus
), dec1
, dec2
, key
->comment
);
404 static int save_ssh2_pubkey(char *filename
, struct ssh2_userkey
*key
)
406 unsigned char *pub_blob
;
412 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_len
);
414 fp
= fopen(filename
, "wb");
418 fprintf(fp
, "---- BEGIN SSH2 PUBLIC KEY ----\n");
420 fprintf(fp
, "Comment: \"");
421 for (p
= key
->comment
; *p
; p
++) {
422 if (*p
== '\\' || *p
== '\"')
430 while (i
< pub_len
) {
432 int n
= (pub_len
- i
< 3 ? pub_len
- i
: 3);
433 base64_encode_atom(pub_blob
+ i
, n
, buf
);
437 if (++column
>= 16) {
445 fprintf(fp
, "---- END SSH2 PUBLIC KEY ----\n");
452 * Dialog-box function for the main PuTTYgen dialog box.
454 static int CALLBACK
MainDlgProc(HWND hwnd
, UINT msg
,
455 WPARAM wParam
, LPARAM lParam
)
458 controlidstart
= 100,
464 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
465 IDC_FPSTATIC
, IDC_FINGERPRINT
,
466 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
467 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
468 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
,
470 IDC_GENSTATIC
, IDC_GENERATE
,
471 IDC_LOADSTATIC
, IDC_LOAD
,
472 IDC_SAVESTATIC
, IDC_SAVE
, IDC_SAVEPUB
,
474 IDC_TYPESTATIC
, IDC_KEYSSH1
, IDC_KEYSSH2RSA
, IDC_KEYSSH2DSA
,
475 IDC_BITSSTATIC
, IDC_BITS
,
478 static const int nokey_ids
[] = { IDC_NOKEY
, 0 };
479 static const int generating_ids
[] =
480 { IDC_GENERATING
, IDC_PROGRESS
, 0 };
481 static const int gotkey_ids
[] = {
482 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
483 IDC_FPSTATIC
, IDC_FINGERPRINT
,
484 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
485 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
486 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
, 0
488 static const char generating_msg
[] =
489 "Please wait while a key is generated...";
490 static const char entropy_msg
[] =
491 "Please generate some randomness by moving the mouse over the blank area.";
492 struct MainDlgState
*state
;
499 { /* centre the window */
503 hw
= GetDesktopWindow();
504 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
506 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
507 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
508 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
511 state
= smalloc(sizeof(*state
));
512 state
->generation_thread_exists
= FALSE
;
513 state
->collecting_entropy
= FALSE
;
514 state
->entropy
= NULL
;
515 state
->key_exists
= FALSE
;
516 SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
) state
);
518 struct ctlpos cp
, cp2
;
520 /* Accelerators used: acglops1rbd */
522 ctlposinit(&cp
, hwnd
, 10, 10, 10);
523 bartitle(&cp
, "Public and private key generation for PuTTY",
525 beginbox(&cp
, "Key", IDC_BOX_KEY
);
527 statictext(&cp2
, "No key.", 1, IDC_NOKEY
);
529 statictext(&cp2
, "", 1, IDC_GENERATING
);
530 progressbar(&cp2
, IDC_PROGRESS
);
532 "&Public key for pasting into authorized_keys file:",
533 IDC_PKSTATIC
, IDC_KEYDISPLAY
, 7);
534 SendDlgItemMessage(hwnd
, IDC_KEYDISPLAY
, EM_SETREADONLY
, 1, 0);
535 staticedit(&cp
, "Key fingerprint:", IDC_FPSTATIC
,
536 IDC_FINGERPRINT
, 75);
537 SendDlgItemMessage(hwnd
, IDC_FINGERPRINT
, EM_SETREADONLY
, 1,
539 staticedit(&cp
, "Key &comment:", IDC_COMMENTSTATIC
,
540 IDC_COMMENTEDIT
, 75);
541 staticpassedit(&cp
, "Key p&assphrase:", IDC_PASSPHRASE1STATIC
,
542 IDC_PASSPHRASE1EDIT
, 75);
543 staticpassedit(&cp
, "C&onfirm passphrase:",
544 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
, 75);
546 beginbox(&cp
, "Actions", IDC_BOX_ACTIONS
);
547 staticbtn(&cp
, "Generate a public/private key pair",
548 IDC_GENSTATIC
, "&Generate", IDC_GENERATE
);
549 staticbtn(&cp
, "Load an existing private key file",
550 IDC_LOADSTATIC
, "&Load", IDC_LOAD
);
551 static2btn(&cp
, "Save the generated key", IDC_SAVESTATIC
,
552 "Save p&ublic key", IDC_SAVEPUB
,
553 "&Save private key", IDC_SAVE
);
555 beginbox(&cp
, "Parameters", IDC_BOX_PARAMS
);
556 radioline(&cp
, "Type of key to generate:", IDC_TYPESTATIC
, 3,
557 "SSH&1 (RSA)", IDC_KEYSSH1
,
558 "SSH2 &RSA", IDC_KEYSSH2RSA
,
559 "SSH2 &DSA", IDC_KEYSSH2DSA
, NULL
);
560 staticedit(&cp
, "Number of &bits in a generated key:",
561 IDC_BITSSTATIC
, IDC_BITS
, 20);
564 CheckRadioButton(hwnd
, IDC_KEYSSH1
, IDC_KEYSSH2DSA
, IDC_KEYSSH1
);
565 SetDlgItemInt(hwnd
, IDC_BITS
, DEFAULT_KEYSIZE
, FALSE
);
568 * Initially, hide the progress bar and the key display,
569 * and show the no-key display. Also disable the Save
570 * buttons, because with no key we obviously can't save
573 hidemany(hwnd
, nokey_ids
, FALSE
);
574 hidemany(hwnd
, generating_ids
, TRUE
);
575 hidemany(hwnd
, gotkey_ids
, TRUE
);
576 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
577 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 0);
581 state
= (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
582 if (state
->collecting_entropy
&&
583 state
->entropy
&& state
->entropy_got
< state
->entropy_required
) {
584 state
->entropy
[state
->entropy_got
++] = lParam
;
585 state
->entropy
[state
->entropy_got
++] = GetMessageTime();
586 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
,
587 state
->entropy_got
, 0);
588 if (state
->entropy_got
>= state
->entropy_required
) {
589 struct rsa_key_thread_params
*params
;
593 * Seed the entropy pool
595 random_add_heavynoise(state
->entropy
, state
->entropy_size
);
596 memset(state
->entropy
, 0, state
->entropy_size
);
597 sfree(state
->entropy
);
598 state
->collecting_entropy
= FALSE
;
600 SetDlgItemText(hwnd
, IDC_GENERATING
, generating_msg
);
601 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
602 MAKELPARAM(0, PROGRESSRANGE
));
603 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
605 params
= smalloc(sizeof(*params
));
606 params
->progressbar
= GetDlgItem(hwnd
, IDC_PROGRESS
);
607 params
->dialog
= hwnd
;
608 params
->keysize
= state
->keysize
;
609 params
->is_dsa
= state
->is_dsa
;
610 params
->key
= &state
->key
;
611 params
->dsskey
= &state
->dsskey
;
613 if (!CreateThread(NULL
, 0, generate_rsa_key_thread
,
614 params
, 0, &threadid
)) {
615 MessageBox(hwnd
, "Out of thread resources",
616 "Key generation error",
617 MB_OK
| MB_ICONERROR
);
620 state
->generation_thread_exists
= TRUE
;
626 switch (LOWORD(wParam
)) {
627 case IDC_COMMENTEDIT
:
628 if (HIWORD(wParam
) == EN_CHANGE
) {
629 state
= (struct MainDlgState
*)
630 GetWindowLong(hwnd
, GWL_USERDATA
);
631 if (state
->key_exists
) {
632 HWND editctl
= GetDlgItem(hwnd
, IDC_COMMENTEDIT
);
633 int len
= GetWindowTextLength(editctl
);
634 if (*state
->commentptr
)
635 sfree(*state
->commentptr
);
636 *state
->commentptr
= smalloc(len
+ 1);
637 GetWindowText(editctl
, *state
->commentptr
, len
+ 1);
639 setupbigedit2(hwnd
, IDC_KEYDISPLAY
, IDC_PKSTATIC
,
642 setupbigedit1(hwnd
, IDC_KEYDISPLAY
, IDC_PKSTATIC
,
649 EnableWindow(hwnd
, 0);
650 DialogBox(hinst
, MAKEINTRESOURCE(213), NULL
, AboutProc
);
651 EnableWindow(hwnd
, 1);
652 SetActiveWindow(hwnd
);
656 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
657 if (!state
->generation_thread_exists
) {
659 state
->keysize
= GetDlgItemInt(hwnd
, IDC_BITS
, &ok
, FALSE
);
661 state
->keysize
= DEFAULT_KEYSIZE
;
662 /* If we ever introduce a new key type, check it here! */
663 state
->ssh2
= !IsDlgButtonChecked(hwnd
, IDC_KEYSSH1
);
664 state
->is_dsa
= IsDlgButtonChecked(hwnd
, IDC_KEYSSH2DSA
);
665 if (state
->keysize
< 256) {
666 int ret
= MessageBox(hwnd
,
667 "PuTTYgen will not generate a key"
668 " smaller than 256 bits.\n"
669 "Key length reset to 256. Continue?",
671 MB_ICONWARNING
| MB_OKCANCEL
);
674 state
->keysize
= 256;
675 SetDlgItemInt(hwnd
, IDC_BITS
, 256, FALSE
);
677 hidemany(hwnd
, nokey_ids
, TRUE
);
678 hidemany(hwnd
, generating_ids
, FALSE
);
679 hidemany(hwnd
, gotkey_ids
, TRUE
);
680 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 0);
681 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 0);
682 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
683 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 0);
684 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH1
), 0);
685 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2RSA
), 0);
686 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2DSA
), 0);
687 EnableWindow(GetDlgItem(hwnd
, IDC_BITS
), 0);
688 state
->key_exists
= FALSE
;
689 SetDlgItemText(hwnd
, IDC_GENERATING
, entropy_msg
);
690 state
->collecting_entropy
= TRUE
;
693 * My brief statistical tests on mouse movements
694 * suggest that there are about 2.5 bits of
695 * randomness in the x position, 2.5 in the y
696 * position, and 1.7 in the message time, making
697 * 5.7 bits of unpredictability per mouse movement.
698 * However, other people have told me it's far less
699 * than that, so I'm going to be stupidly cautious
700 * and knock that down to a nice round 2. With this
701 * method, we require two words per mouse movement,
702 * so with 2 bits per mouse movement we expect 2
703 * bits every 2 words.
705 state
->entropy_required
= (state
->keysize
/ 2) * 2;
706 state
->entropy_got
= 0;
707 state
->entropy_size
= (state
->entropy_required
*
708 sizeof(*state
->entropy
));
709 state
->entropy
= smalloc(state
->entropy_size
);
711 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
712 MAKELPARAM(0, state
->entropy_required
));
713 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
718 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
719 if (state
->key_exists
) {
720 char filename
[FILENAME_MAX
];
721 char passphrase
[PASSPHRASE_MAXLEN
];
722 char passphrase2
[PASSPHRASE_MAXLEN
];
723 GetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
724 passphrase
, sizeof(passphrase
));
725 GetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
726 passphrase2
, sizeof(passphrase2
));
727 if (strcmp(passphrase
, passphrase2
)) {
729 "The two passphrases given do not match.",
730 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
735 ret
= MessageBox(hwnd
,
736 "Are you sure you want to save this key\n"
737 "without a passphrase to protect it?",
739 MB_YESNO
| MB_ICONWARNING
);
743 if (prompt_keyfile(hwnd
, "Save private key as:",
746 FILE *fp
= fopen(filename
, "r");
748 char buffer
[FILENAME_MAX
+ 80];
750 sprintf(buffer
, "Overwrite existing file\n%.*s?",
751 FILENAME_MAX
, filename
);
752 ret
= MessageBox(hwnd
, buffer
, "PuTTYgen Warning",
753 MB_YESNO
| MB_ICONWARNING
);
758 ret
= ssh2_save_userkey(filename
, &state
->ssh2key
,
759 *passphrase ? passphrase
:
762 ret
= saversakey(filename
, &state
->key
,
763 *passphrase ? passphrase
: NULL
);
766 MessageBox(hwnd
, "Unable to save key file",
767 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
774 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
775 if (state
->key_exists
) {
776 char filename
[FILENAME_MAX
];
777 if (prompt_keyfile(hwnd
, "Save public key as:",
780 FILE *fp
= fopen(filename
, "r");
782 char buffer
[FILENAME_MAX
+ 80];
784 sprintf(buffer
, "Overwrite existing file\n%.*s?",
785 FILENAME_MAX
, filename
);
786 ret
= MessageBox(hwnd
, buffer
, "PuTTYgen Warning",
787 MB_YESNO
| MB_ICONWARNING
);
792 ret
= save_ssh2_pubkey(filename
, &state
->ssh2key
);
794 ret
= save_ssh1_pubkey(filename
, &state
->key
);
797 MessageBox(hwnd
, "Unable to save key file",
798 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
805 (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
806 if (!state
->generation_thread_exists
) {
807 char filename
[FILENAME_MAX
];
808 if (prompt_keyfile(hwnd
, "Load private key:", filename
, 0)) {
809 char passphrase
[PASSPHRASE_MAXLEN
];
814 struct PassphraseProcStruct pps
;
815 struct RSAKey newkey1
;
816 struct ssh2_userkey
*newkey2
= NULL
;
818 ver
= keyfile_version(filename
);
820 MessageBox(NULL
, "Couldn't load private key.",
821 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
827 needs_pass
= rsakey_encrypted(filename
, &comment
);
830 ssh2_userkey_encrypted(filename
, &comment
);
831 pps
.passphrase
= passphrase
;
832 pps
.comment
= comment
;
836 dlgret
= DialogBoxParam(hinst
,
837 MAKEINTRESOURCE(210),
838 NULL
, PassphraseProc
,
848 loadrsakey(filename
, &newkey1
, passphrase
);
851 ssh2_load_userkey(filename
, passphrase
);
852 if (newkey2
== SSH2_WRONG_PASSPHRASE
)
863 MessageBox(NULL
, "Couldn't load private key.",
864 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
865 } else if (ret
== 1) {
866 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
867 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
868 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
869 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 1);
870 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH1
), 1);
871 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2RSA
), 1);
872 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2DSA
), 1);
873 EnableWindow(GetDlgItem(hwnd
, IDC_BITS
), 1);
875 * Now update the key controls with all the
879 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
881 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
888 state
->commentptr
= &state
->key
.comment
;
889 state
->key
= newkey1
;
892 * Set the key fingerprint.
894 savecomment
= state
->key
.comment
;
895 state
->key
.comment
= NULL
;
896 rsa_fingerprint(buf
, sizeof(buf
),
898 state
->key
.comment
= savecomment
;
900 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
902 * Construct a decimal representation
903 * of the key, for pasting into
904 * .ssh/authorized_keys on a Unix box.
906 setupbigedit1(hwnd
, IDC_KEYDISPLAY
,
907 IDC_PKSTATIC
, &state
->key
);
914 &state
->ssh2key
.comment
;
915 state
->ssh2key
= *newkey2
; /* structure copy */
918 savecomment
= state
->ssh2key
.comment
;
919 state
->ssh2key
.comment
= NULL
;
922 fingerprint(state
->ssh2key
.data
);
923 state
->ssh2key
.comment
= savecomment
;
925 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, fp
);
928 setupbigedit2(hwnd
, IDC_KEYDISPLAY
,
929 IDC_PKSTATIC
, &state
->ssh2key
);
931 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
,
935 * Finally, hide the progress bar and show
938 hidemany(hwnd
, nokey_ids
, TRUE
);
939 hidemany(hwnd
, generating_ids
, TRUE
);
940 hidemany(hwnd
, gotkey_ids
, FALSE
);
941 state
->key_exists
= TRUE
;
949 state
= (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
950 state
->generation_thread_exists
= FALSE
;
951 state
->key_exists
= TRUE
;
952 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
953 MAKELPARAM(0, PROGRESSRANGE
));
954 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, PROGRESSRANGE
, 0);
955 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
956 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
957 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
958 EnableWindow(GetDlgItem(hwnd
, IDC_SAVEPUB
), 1);
959 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH1
), 1);
960 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2RSA
), 1);
961 EnableWindow(GetDlgItem(hwnd
, IDC_KEYSSH2DSA
), 1);
962 EnableWindow(GetDlgItem(hwnd
, IDC_BITS
), 1);
965 state
->ssh2key
.data
= &state
->dsskey
;
966 state
->ssh2key
.alg
= &ssh_dss
;
968 state
->ssh2key
.data
= &state
->key
;
969 state
->ssh2key
.alg
= &ssh_rsa
;
971 state
->commentptr
= &state
->ssh2key
.comment
;
973 state
->commentptr
= &state
->key
.comment
;
976 * Invent a comment for the key. We'll do this by including
977 * the date in it. This will be so horrifyingly ugly that
978 * the user will immediately want to change it, which is
981 *state
->commentptr
= smalloc(30);
988 strftime(*state
->commentptr
, 30, "dsa-key-%Y%m%d", tm
);
990 strftime(*state
->commentptr
, 30, "rsa-key-%Y%m%d", tm
);
994 * Now update the key controls with all the key data.
999 * Blank passphrase, initially. This isn't dangerous,
1000 * because we will warn (Are You Sure?) before allowing
1001 * the user to save an unprotected private key.
1003 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
, "");
1004 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
, "");
1008 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
, *state
->commentptr
);
1010 * Set the key fingerprint.
1012 savecomment
= *state
->commentptr
;
1013 *state
->commentptr
= NULL
;
1016 fp
= state
->ssh2key
.alg
->fingerprint(state
->ssh2key
.data
);
1017 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, fp
);
1021 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
1022 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
1024 *state
->commentptr
= savecomment
;
1026 * Construct a decimal representation of the key, for
1027 * pasting into .ssh/authorized_keys or
1028 * .ssh/authorized_keys2 on a Unix box.
1031 setupbigedit2(hwnd
, IDC_KEYDISPLAY
,
1032 IDC_PKSTATIC
, &state
->ssh2key
);
1034 setupbigedit1(hwnd
, IDC_KEYDISPLAY
,
1035 IDC_PKSTATIC
, &state
->key
);
1039 * Finally, hide the progress bar and show the key data.
1041 hidemany(hwnd
, nokey_ids
, TRUE
);
1042 hidemany(hwnd
, generating_ids
, TRUE
);
1043 hidemany(hwnd
, gotkey_ids
, FALSE
);
1046 state
= (struct MainDlgState
*) GetWindowLong(hwnd
, GWL_USERDATA
);
1054 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1056 InitCommonControls();
1059 return DialogBox(hinst
, MAKEINTRESOURCE(201), NULL
,
1060 MainDlgProc
) != IDOK
;