2 * PuTTY key generation front end.
13 #define PUTTY_DO_GLOBALS
19 #define WM_DONEKEY (WM_XUSER + 1)
21 #define DEFAULT_KEYSIZE 1024
25 * - test the generated keys for actual working-RSA-key-hood
28 /* ----------------------------------------------------------------------
29 * Progress report code. This is really horrible :-)
31 #define PHASE1TOTAL 0x10000
32 #define PHASE2TOTAL 0x10000
33 #define PHASE3TOTAL 0x04000
35 #define PHASE2START (PHASE1TOTAL)
36 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
37 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
38 #define PROGRESSBIGRANGE 65535
39 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
40 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
42 unsigned phase1param
, phase1current
, phase1n
;
43 unsigned phase2param
, phase2current
, phase2n
;
48 static void progress_update(void *param
, int phase
, int iprogress
) {
49 struct progress
*p
= (struct progress
*)param
;
50 unsigned progress
= iprogress
;
55 p
->phase1param
= 0x10000 + progress
;
56 p
->phase1current
= 0x10000; p
->phase1n
= 0;
59 p
->phase2param
= 0x10000 + progress
;
60 p
->phase2current
= 0x10000; p
->phase2n
= 0;
63 p
->phase3mult
= PHASE3TOTAL
/ progress
;
66 while (p
->phase1n
< progress
) {
68 p
->phase1current
*= p
->phase1param
;
69 p
->phase1current
/= 0x10000;
71 position
= PHASE1START
+ 0x10000 - p
->phase1current
;
74 while (p
->phase2n
< progress
) {
76 p
->phase2current
*= p
->phase2param
;
77 p
->phase2current
/= 0x10000;
79 position
= PHASE2START
+ 0x10000 - p
->phase2current
;
82 position
= PHASE3START
+ progress
* p
->phase3mult
;
86 SendMessage(p
->progbar
, PBM_SETPOS
, position
/ DIVISOR
, 0);
91 #define PASSPHRASE_MAXLEN 512
93 struct PassphraseProcStruct
{
99 * Dialog-box function for the passphrase box.
101 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
102 WPARAM wParam
, LPARAM lParam
) {
103 static char *passphrase
;
104 struct PassphraseProcStruct
*p
;
108 SetForegroundWindow(hwnd
);
109 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
110 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
111 p
= (struct PassphraseProcStruct
*)lParam
;
112 passphrase
= p
->passphrase
;
114 SetDlgItemText(hwnd
, 101, p
->comment
);
118 switch (LOWORD(wParam
)) {
128 case 102: /* edit box */
129 if (HIWORD(wParam
) == EN_CHANGE
) {
130 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
131 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
144 * Prompt for a key file. Assumes the filename buffer is of size
147 static int prompt_keyfile(HWND hwnd
, char *dlgtitle
,
148 char *filename
, int save
) {
150 memset(&of
, 0, sizeof(of
));
151 #ifdef OPENFILENAME_SIZE_VERSION_400
152 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
154 of
.lStructSize
= sizeof(of
);
157 of
.lpstrFilter
= "All Files\0*\0\0\0";
158 of
.lpstrCustomFilter
= NULL
;
160 of
.lpstrFile
= filename
; *filename
= '\0';
161 of
.nMaxFile
= FILENAME_MAX
;
162 of
.lpstrFileTitle
= NULL
;
163 of
.lpstrInitialDir
= NULL
;
164 of
.lpstrTitle
= dlgtitle
;
167 return GetSaveFileName(&of
);
169 return GetOpenFileName(&of
);
173 * This function is needed to link with the DES code. We need not
174 * have it do anything at all.
176 void logevent(char *msg
) {
180 * Dialog-box function for the Licence box.
182 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
183 WPARAM wParam
, LPARAM lParam
) {
188 switch (LOWORD(wParam
)) {
202 * Dialog-box function for the About box.
204 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
205 WPARAM wParam
, LPARAM lParam
) {
208 SetDlgItemText (hwnd
, 100, ver
);
211 switch (LOWORD(wParam
)) {
216 EnableWindow(hwnd
, 0);
217 DialogBox (hinst
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
218 EnableWindow(hwnd
, 1);
219 SetActiveWindow(hwnd
);
231 * Thread to generate a key.
233 struct rsa_key_thread_params
{
234 HWND progressbar
; /* notify this with progress */
235 HWND dialog
; /* notify this on completion */
236 int keysize
; /* bits in key */
240 static DWORD WINAPI
generate_rsa_key_thread(void *param
) {
241 struct rsa_key_thread_params
*params
=
242 (struct rsa_key_thread_params
*)param
;
243 struct progress prog
;
244 prog
.progbar
= params
->progressbar
;
246 rsa_generate(params
->key
, params
->aux
,
247 params
->keysize
, progress_update
, &prog
);
249 PostMessage(params
->dialog
, WM_DONEKEY
, 0, 0);
255 struct MainDlgState
{
256 int collecting_entropy
;
257 int generation_thread_exists
;
259 int entropy_got
, entropy_required
, entropy_size
;
266 static void hidemany(HWND hwnd
, const int *ids
, int hideit
) {
268 ShowWindow(GetDlgItem(hwnd
, *ids
++), (hideit ? SW_HIDE
: SW_SHOW
));
272 static void setupbigedit(HWND hwnd
, int id
, struct RSAKey
*key
) {
276 dec1
= bignum_decimal(key
->exponent
);
277 dec2
= bignum_decimal(key
->modulus
);
278 buffer
= malloc(strlen(dec1
)+strlen(dec2
)+
279 strlen(key
->comment
)+30);
280 sprintf(buffer
, "%d %s %s %s",
281 ssh1_bignum_bitcount(key
->modulus
),
282 dec1
, dec2
, key
->comment
);
283 SetDlgItemText(hwnd
, id
, buffer
);
290 * Dialog-box function for the main PuTTYgen dialog box.
292 static int CALLBACK
MainDlgProc (HWND hwnd
, UINT msg
,
293 WPARAM wParam
, LPARAM lParam
) {
295 controlidstart
= 100,
297 IDC_BOX_KEY
, IDC_BOXT_KEY
,
301 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
302 IDC_FPSTATIC
, IDC_FINGERPRINT
,
303 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
304 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
305 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
,
306 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
,
307 IDC_GENSTATIC
, IDC_GENERATE
,
308 IDC_LOADSTATIC
, IDC_LOAD
,
309 IDC_SAVESTATIC
, IDC_SAVE
,
310 IDC_BOX_PARAMS
, IDC_BOXT_PARAMS
,
311 IDC_BITSSTATIC
, IDC_BITS
,
314 static const int nokey_ids
[] = { IDC_NOKEY
, 0 };
315 static const int generating_ids
[] = { IDC_GENERATING
, IDC_PROGRESS
, 0 };
316 static const int gotkey_ids
[] = {
317 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
318 IDC_FPSTATIC
, IDC_FINGERPRINT
,
319 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
320 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
321 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
, 0 };
322 static const char generating_msg
[] =
323 "Please wait while a key is generated...";
324 static const char entropy_msg
[] =
325 "Please move the mouse in this window to generate randomness";
326 struct MainDlgState
*state
;
330 state
= malloc(sizeof(*state
));
331 state
->generation_thread_exists
= FALSE
;
332 state
->key_exists
= FALSE
;
333 SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
)state
);
335 struct ctlpos cp
, cp2
;
337 /* Accelerators used: acglops */
339 ctlposinit(&cp
, hwnd
, 10, 10, 10);
340 bartitle(&cp
, "Public and private key generation for PuTTY",
343 IDC_BOX_KEY
, IDC_BOXT_KEY
);
345 statictext(&cp2
, "No key.", IDC_NOKEY
);
349 progressbar(&cp2
, IDC_PROGRESS
);
351 "&Public key for pasting into authorized_keys file:",
352 IDC_PKSTATIC
, IDC_KEYDISPLAY
, 7);
353 SendDlgItemMessage(hwnd
, IDC_KEYDISPLAY
, EM_SETREADONLY
, 1, 0);
354 staticedit(&cp
, "Key fingerprint:", IDC_FPSTATIC
,
355 IDC_FINGERPRINT
, 70);
356 SendDlgItemMessage(hwnd
, IDC_FINGERPRINT
, EM_SETREADONLY
, 1, 0);
357 staticedit(&cp
, "Key &comment:", IDC_COMMENTSTATIC
,
358 IDC_COMMENTEDIT
, 70);
359 staticpassedit(&cp
, "Key p&assphrase:", IDC_PASSPHRASE1STATIC
,
360 IDC_PASSPHRASE1EDIT
, 70);
361 staticpassedit(&cp
, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC
,
362 IDC_PASSPHRASE2EDIT
, 70);
364 beginbox(&cp
, "Actions",
365 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
);
366 staticbtn(&cp
, "Generate a public/private key pair",
367 IDC_GENSTATIC
, "&Generate", IDC_GENERATE
);
368 staticbtn(&cp
, "Load an existing private key file",
369 IDC_LOADSTATIC
, "&Load", IDC_LOAD
);
370 staticbtn(&cp
, "Save the generated key to a new file",
371 IDC_SAVESTATIC
, "&Save", IDC_SAVE
);
373 beginbox(&cp
, "Actions",
374 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
);
375 staticedit(&cp
, "Length of generated keys in &bits:",
376 IDC_BITSSTATIC
, IDC_BITS
, 20);
379 SetDlgItemInt(hwnd
, IDC_BITS
, DEFAULT_KEYSIZE
, FALSE
);
382 * Initially, hide the progress bar and the key display,
383 * and show the no-key display. Also disable the Save
384 * button, because with no key we obviously can't save
387 hidemany(hwnd
, nokey_ids
, FALSE
);
388 hidemany(hwnd
, generating_ids
, TRUE
);
389 hidemany(hwnd
, gotkey_ids
, TRUE
);
390 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
394 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
395 if (state
->collecting_entropy
) {
396 state
->entropy
[state
->entropy_got
++] = lParam
;
397 state
->entropy
[state
->entropy_got
++] = GetMessageTime();
398 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
,
399 state
->entropy_got
, 0);
400 if (state
->entropy_got
>= state
->entropy_required
) {
401 struct rsa_key_thread_params
*params
;
405 * Seed the entropy pool
407 random_add_heavynoise(state
->entropy
, state
->entropy_size
);
408 memset(state
->entropy
, 0, state
->entropy_size
);
409 free(state
->entropy
);
411 SetDlgItemText(hwnd
, IDC_GENERATING
, generating_msg
);
412 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
413 MAKELPARAM(0, PROGRESSRANGE
));
414 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
416 params
= malloc(sizeof(*params
));
417 params
->progressbar
= GetDlgItem(hwnd
, IDC_PROGRESS
);
418 params
->dialog
= hwnd
;
419 params
->keysize
= state
->keysize
;
420 params
->key
= &state
->key
;
421 params
->aux
= &state
->aux
;
423 if (!CreateThread(NULL
, 0, generate_rsa_key_thread
,
424 params
, 0, &threadid
)) {
425 MessageBox(hwnd
, "Out of thread resources",
426 "Key generation error",
427 MB_OK
| MB_ICONERROR
);
430 state
->generation_thread_exists
= TRUE
;
431 state
->collecting_entropy
= FALSE
;
437 switch (LOWORD(wParam
)) {
438 case IDC_COMMENTEDIT
:
439 if (HIWORD(wParam
) == EN_CHANGE
) {
440 state
= (struct MainDlgState
*)
441 GetWindowLong(hwnd
, GWL_USERDATA
);
442 if (state
->key_exists
) {
443 HWND editctl
= GetDlgItem(hwnd
, IDC_COMMENTEDIT
);
444 int len
= GetWindowTextLength(editctl
);
445 if (state
->key
.comment
)
446 free(state
->key
.comment
);
447 state
->key
.comment
= malloc(len
+1);
448 GetWindowText(editctl
, state
->key
.comment
, len
+1);
453 EnableWindow(hwnd
, 0);
454 DialogBox (hinst
, MAKEINTRESOURCE(213), NULL
, AboutProc
);
455 EnableWindow(hwnd
, 1);
456 SetActiveWindow(hwnd
);
459 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
460 if (!state
->generation_thread_exists
) {
461 hidemany(hwnd
, nokey_ids
, TRUE
);
462 hidemany(hwnd
, generating_ids
, FALSE
);
463 hidemany(hwnd
, gotkey_ids
, TRUE
);
464 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 0);
465 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 0);
466 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
467 state
->key_exists
= FALSE
;
468 SetDlgItemText(hwnd
, IDC_GENERATING
, entropy_msg
);
469 state
->collecting_entropy
= TRUE
;
472 state
->keysize
= GetDlgItemInt(hwnd
, IDC_BITS
,
474 if (!ok
) state
->keysize
= DEFAULT_KEYSIZE
;
478 * My brief statistical tests on mouse movements
479 * suggest that there are about 5 bits of
480 * randomness in the x position, 5 in the y
481 * position, and 1.7 in the message time, making
482 * 11.7 bits of unpredictability per mouse
483 * movement. However, other people have told me
484 * it's far less than that, so I'm going to be
485 * stupidly cautious and knock that down to a nice
488 state
->entropy_required
= (state
->keysize
/ 4) * 2;
489 state
->entropy_got
= 0;
490 state
->entropy_size
= (state
->entropy_required
*
491 sizeof(*state
->entropy
));
492 state
->entropy
= malloc(state
->entropy_size
);
494 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
495 MAKELPARAM(0, state
->entropy_required
));
496 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
500 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
501 if (state
->key_exists
) {
502 char filename
[FILENAME_MAX
];
503 char passphrase
[PASSPHRASE_MAXLEN
];
504 char passphrase2
[PASSPHRASE_MAXLEN
];
505 GetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
506 passphrase
, sizeof(passphrase
));
507 GetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
508 passphrase2
, sizeof(passphrase2
));
509 if (strcmp(passphrase
, passphrase2
)) {
511 "The two passphrases given do not match.",
513 MB_OK
| MB_ICONERROR
);
518 ret
= MessageBox(hwnd
,
519 "Are you sure you want to save this key\n"
520 "without a passphrase to protect it?",
522 MB_YESNO
| MB_ICONWARNING
);
526 if (prompt_keyfile(hwnd
, "Save private key as:",
529 FILE *fp
= fopen(filename
, "r");
531 char buffer
[FILENAME_MAX
+80];
533 sprintf(buffer
, "Overwrite existing file\n%.*s?",
534 FILENAME_MAX
, filename
);
535 ret
= MessageBox(hwnd
, buffer
, "PuTTYgen Warning",
536 MB_YESNO
| MB_ICONWARNING
);
540 ret
= saversakey(filename
, &state
->key
, &state
->aux
,
541 *passphrase ? passphrase
: NULL
);
543 MessageBox(hwnd
, "Unable to save key file",
545 MB_OK
| MB_ICONERROR
);
551 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
552 if (!state
->generation_thread_exists
) {
553 char filename
[FILENAME_MAX
];
554 if (prompt_keyfile(hwnd
, "Load private key:",
556 char passphrase
[PASSPHRASE_MAXLEN
];
560 struct PassphraseProcStruct pps
;
561 struct RSAKey newkey
;
562 struct RSAAux newaux
;
564 needs_pass
= rsakey_encrypted(filename
, &comment
);
565 pps
.passphrase
= passphrase
;
566 pps
.comment
= comment
;
570 dlgret
= DialogBoxParam(hinst
,
571 MAKEINTRESOURCE(210),
572 NULL
, PassphraseProc
,
580 ret
= loadrsakey(filename
, &newkey
, &newaux
,
583 if (comment
) free(comment
);
585 MessageBox(NULL
, "Couldn't load private key.",
586 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
587 } else if (ret
== 1) {
591 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
592 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
593 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
595 * Now update the key controls with all the
600 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
602 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
604 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
,
607 * Set the key fingerprint.
610 char *savecomment
= state
->key
.comment
;
611 state
->key
.comment
= NULL
;
612 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
613 state
->key
.comment
= savecomment
;
615 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
617 * Construct a decimal representation
618 * of the key, for pasting into
619 * .ssh/authorized_keys on a Unix box.
621 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
624 * Finally, hide the progress bar and show
627 hidemany(hwnd
, nokey_ids
, TRUE
);
628 hidemany(hwnd
, generating_ids
, TRUE
);
629 hidemany(hwnd
, gotkey_ids
, FALSE
);
630 state
->key_exists
= TRUE
;
638 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
639 state
->generation_thread_exists
= FALSE
;
640 state
->key_exists
= TRUE
;
641 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, PROGRESSRANGE
, 0);
642 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
643 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
644 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
646 * Invent a comment for the key. We'll do this by including
647 * the date in it. This will be so horrifyingly ugly that
648 * the user will immediately want to change it, which is
651 state
->key
.comment
= malloc(30);
657 strftime(state
->key
.comment
, 30, "rsa-key-%Y%m%d", tm
);
661 * Now update the key controls with all the key data.
666 * Blank passphrase, initially. This isn't dangerous,
667 * because we will warn (Are You Sure?) before allowing
668 * the user to save an unprotected private key.
670 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
, "");
671 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
, "");
675 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
, state
->key
.comment
);
677 * Set the key fingerprint.
680 char *savecomment
= state
->key
.comment
;
681 state
->key
.comment
= NULL
;
682 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
683 state
->key
.comment
= savecomment
;
685 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
687 * Construct a decimal representation of the key, for
688 * pasting into .ssh/authorized_keys on a Unix box.
690 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
693 * Finally, hide the progress bar and show the key data.
695 hidemany(hwnd
, nokey_ids
, TRUE
);
696 hidemany(hwnd
, generating_ids
, TRUE
);
697 hidemany(hwnd
, gotkey_ids
, FALSE
);
700 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
708 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
709 InitCommonControls();
712 return DialogBox(hinst
, MAKEINTRESOURCE(201), NULL
, MainDlgProc
) != IDOK
;