2 * PuTTY key generation front end.
13 #define PUTTY_DO_GLOBALS
19 #define WM_DONEKEY (WM_XUSER + 1)
25 * - have some means of verifying passphrase changes against typos
26 * - prompt before overwriting an existing file
27 * - check the return value from saversakey()
28 * - test the generated keys for actual working-RSA-key-hood
32 /* ----------------------------------------------------------------------
33 * Progress report code. This is really horrible :-)
35 #define PHASE1TOTAL 0x10000
36 #define PHASE2TOTAL 0x10000
37 #define PHASE3TOTAL 0x04000
39 #define PHASE2START (PHASE1TOTAL)
40 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
41 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
42 #define PROGRESSBIGRANGE 65535
43 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
44 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
46 unsigned phase1param
, phase1current
, phase1n
;
47 unsigned phase2param
, phase2current
, phase2n
;
52 static void progress_update(void *param
, int phase
, int iprogress
) {
53 struct progress
*p
= (struct progress
*)param
;
54 unsigned progress
= iprogress
;
59 p
->phase1param
= 0x10000 + progress
;
60 p
->phase1current
= 0x10000; p
->phase1n
= 0;
63 p
->phase2param
= 0x10000 + progress
;
64 p
->phase2current
= 0x10000; p
->phase2n
= 0;
67 p
->phase3mult
= PHASE3TOTAL
/ progress
;
70 while (p
->phase1n
< progress
) {
72 p
->phase1current
*= p
->phase1param
;
73 p
->phase1current
/= 0x10000;
75 position
= PHASE1START
+ 0x10000 - p
->phase1current
;
78 while (p
->phase2n
< progress
) {
80 p
->phase2current
*= p
->phase2param
;
81 p
->phase2current
/= 0x10000;
83 position
= PHASE2START
+ 0x10000 - p
->phase2current
;
86 position
= PHASE3START
+ progress
* p
->phase3mult
;
90 SendMessage(p
->progbar
, PBM_SETPOS
, position
/ DIVISOR
, 0);
95 #define PASSPHRASE_MAXLEN 512
97 struct PassphraseProcStruct
{
103 * Dialog-box function for the passphrase box.
105 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
106 WPARAM wParam
, LPARAM lParam
) {
107 static char *passphrase
;
108 struct PassphraseProcStruct
*p
;
112 SetForegroundWindow(hwnd
);
113 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
114 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
115 p
= (struct PassphraseProcStruct
*)lParam
;
116 passphrase
= p
->passphrase
;
118 SetDlgItemText(hwnd
, 101, p
->comment
);
122 switch (LOWORD(wParam
)) {
132 case 102: /* edit box */
133 if (HIWORD(wParam
) == EN_CHANGE
) {
134 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
135 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
148 * Prompt for a key file. Assumes the filename buffer is of size
151 static int prompt_keyfile(HWND hwnd
, char *dlgtitle
,
152 char *filename
, int save
) {
154 memset(&of
, 0, sizeof(of
));
155 #ifdef OPENFILENAME_SIZE_VERSION_400
156 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
158 of
.lStructSize
= sizeof(of
);
161 of
.lpstrFilter
= "All Files\0*\0\0\0";
162 of
.lpstrCustomFilter
= NULL
;
164 of
.lpstrFile
= filename
; *filename
= '\0';
165 of
.nMaxFile
= FILENAME_MAX
;
166 of
.lpstrFileTitle
= NULL
;
167 of
.lpstrInitialDir
= NULL
;
168 of
.lpstrTitle
= dlgtitle
;
171 return GetSaveFileName(&of
);
173 return GetOpenFileName(&of
);
177 * This function is needed to link with the DES code. We need not
178 * have it do anything at all.
180 void logevent(char *msg
) {
184 * Dialog-box function for the Licence box.
186 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
187 WPARAM wParam
, LPARAM lParam
) {
192 switch (LOWORD(wParam
)) {
206 * Dialog-box function for the About box.
208 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
209 WPARAM wParam
, LPARAM lParam
) {
212 SetDlgItemText (hwnd
, 100, ver
);
215 switch (LOWORD(wParam
)) {
220 EnableWindow(hwnd
, 0);
221 DialogBox (hinst
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
222 EnableWindow(hwnd
, 1);
223 SetActiveWindow(hwnd
);
235 * Thread to generate a key.
237 struct rsa_key_thread_params
{
238 HWND progressbar
; /* notify this with progress */
239 HWND dialog
; /* notify this on completion */
243 static DWORD WINAPI
generate_rsa_key_thread(void *param
) {
244 struct rsa_key_thread_params
*params
=
245 (struct rsa_key_thread_params
*)param
;
246 struct progress prog
;
247 prog
.progbar
= params
->progressbar
;
249 rsa_generate(params
->key
, params
->aux
, KEYSIZE
, progress_update
, &prog
);
251 PostMessage(params
->dialog
, WM_DONEKEY
, 0, 0);
257 struct MainDlgState
{
258 int collecting_entropy
;
259 int generation_thread_exists
;
261 int entropy_got
, entropy_required
, entropy_size
;
267 static void hidemany(HWND hwnd
, const int *ids
, int hideit
) {
269 ShowWindow(GetDlgItem(hwnd
, *ids
++), (hideit ? SW_HIDE
: SW_SHOW
));
273 static void setupbigedit(HWND hwnd
, int id
, struct RSAKey
*key
) {
277 dec1
= bignum_decimal(key
->exponent
);
278 dec2
= bignum_decimal(key
->modulus
);
279 buffer
= malloc(strlen(dec1
)+strlen(dec2
)+
280 strlen(key
->comment
)+30);
281 sprintf(buffer
, "%d %s %s %s",
282 ssh1_bignum_bitcount(key
->modulus
),
283 dec1
, dec2
, key
->comment
);
284 SetDlgItemText(hwnd
, id
, buffer
);
291 * Dialog-box function for the main PuTTYgen dialog box.
293 static int CALLBACK
MainDlgProc (HWND hwnd
, UINT msg
,
294 WPARAM wParam
, LPARAM lParam
) {
296 controlidstart
= 100,
298 IDC_BOX_KEY
, IDC_BOXT_KEY
,
302 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
303 IDC_FPSTATIC
, IDC_FINGERPRINT
,
304 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
305 IDC_PASSPHRASESTATIC
, IDC_PASSPHRASEEDIT
,
306 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
,
307 IDC_GENSTATIC
, IDC_GENERATE
,
308 IDC_LOADSTATIC
, IDC_LOAD
,
309 IDC_SAVESTATIC
, IDC_SAVE
,
312 static const int nokey_ids
[] = { IDC_NOKEY
, 0 };
313 static const int generating_ids
[] = { IDC_GENERATING
, IDC_PROGRESS
, 0 };
314 static const int gotkey_ids
[] = {
315 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
316 IDC_FPSTATIC
, IDC_FINGERPRINT
,
317 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
318 IDC_PASSPHRASESTATIC
, IDC_PASSPHRASEEDIT
, 0 };
319 static const char generating_msg
[] =
320 "Please wait while a key is generated...";
321 static const char entropy_msg
[] =
322 "Please move the mouse in this window to generate randomness";
323 struct MainDlgState
*state
;
327 state
= malloc(sizeof(*state
));
328 state
->generation_thread_exists
= FALSE
;
329 state
->key_exists
= FALSE
;
330 SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
)state
);
332 struct ctlpos cp
, cp2
;
334 ctlposinit(&cp
, hwnd
, 10, 10, 10);
335 bartitle(&cp
, "Public and private key generation for PuTTY",
338 IDC_BOX_KEY
, IDC_BOXT_KEY
);
340 statictext(&cp2
, "No key.", IDC_NOKEY
);
344 progressbar(&cp2
, IDC_PROGRESS
);
346 "&Public key for pasting into authorized_keys file:",
347 IDC_PKSTATIC
, IDC_KEYDISPLAY
, 7);
348 SendDlgItemMessage(hwnd
, IDC_KEYDISPLAY
, EM_SETREADONLY
, 1, 0);
349 staticedit(&cp
, "Key fingerprint:", IDC_FPSTATIC
,
350 IDC_FINGERPRINT
, 70);
351 SendDlgItemMessage(hwnd
, IDC_FINGERPRINT
, EM_SETREADONLY
, 1, 0);
352 staticedit(&cp
, "Key &comment:", IDC_COMMENTSTATIC
,
353 IDC_COMMENTEDIT
, 70);
354 staticpassedit(&cp
, "Key p&assphrase:", IDC_PASSPHRASESTATIC
,
355 IDC_PASSPHRASEEDIT
, 70);
357 beginbox(&cp
, "Actions",
358 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
);
359 staticbtn(&cp
, "Generate a public/private key pair",
360 IDC_GENSTATIC
, "&Generate", IDC_GENERATE
);
361 staticbtn(&cp
, "Load an existing private key file",
362 IDC_LOADSTATIC
, "&Load", IDC_LOAD
);
363 staticbtn(&cp
, "Save the generated key to a new file",
364 IDC_SAVESTATIC
, "&Save", IDC_SAVE
);
368 * Initially, hide the progress bar and the key display,
369 * and show the no-key display. Also disable the Save
370 * button, because with no key we obviously can't save
373 hidemany(hwnd
, nokey_ids
, FALSE
);
374 hidemany(hwnd
, generating_ids
, TRUE
);
375 hidemany(hwnd
, gotkey_ids
, TRUE
);
376 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
380 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
381 if (state
->collecting_entropy
) {
382 state
->entropy
[state
->entropy_got
++] = lParam
;
383 state
->entropy
[state
->entropy_got
++] = GetMessageTime();
384 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
,
385 state
->entropy_got
, 0);
386 if (state
->entropy_got
>= state
->entropy_required
) {
387 struct rsa_key_thread_params
*params
;
391 * Seed the entropy pool
393 random_add_heavynoise(state
->entropy
, state
->entropy_size
);
394 memset(state
->entropy
, 0, state
->entropy_size
);
395 free(state
->entropy
);
397 SetDlgItemText(hwnd
, IDC_GENERATING
, generating_msg
);
398 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
399 MAKELPARAM(0, PROGRESSRANGE
));
400 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
402 params
= malloc(sizeof(*params
));
403 params
->progressbar
= GetDlgItem(hwnd
, IDC_PROGRESS
);
404 params
->dialog
= hwnd
;
405 params
->key
= &state
->key
;
406 params
->aux
= &state
->aux
;
408 if (!CreateThread(NULL
, 0, generate_rsa_key_thread
,
409 params
, 0, &threadid
)) {
410 MessageBox(hwnd
, "Out of thread resources",
411 "Key generation error",
412 MB_OK
| MB_ICONERROR
);
415 state
->generation_thread_exists
= TRUE
;
416 state
->collecting_entropy
= FALSE
;
422 switch (LOWORD(wParam
)) {
423 case IDC_COMMENTEDIT
:
424 if (HIWORD(wParam
) == EN_CHANGE
) {
425 state
= (struct MainDlgState
*)
426 GetWindowLong(hwnd
, GWL_USERDATA
);
427 if (state
->key_exists
) {
428 HWND editctl
= GetDlgItem(hwnd
, IDC_COMMENTEDIT
);
429 int len
= GetWindowTextLength(editctl
);
430 if (state
->key
.comment
)
431 free(state
->key
.comment
);
432 state
->key
.comment
= malloc(len
+1);
433 GetWindowText(editctl
, state
->key
.comment
, len
+1);
438 EnableWindow(hwnd
, 0);
439 DialogBox (hinst
, MAKEINTRESOURCE(213), NULL
, AboutProc
);
440 EnableWindow(hwnd
, 1);
441 SetActiveWindow(hwnd
);
444 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
445 if (!state
->generation_thread_exists
) {
446 hidemany(hwnd
, nokey_ids
, TRUE
);
447 hidemany(hwnd
, generating_ids
, FALSE
);
448 hidemany(hwnd
, gotkey_ids
, TRUE
);
449 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 0);
450 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 0);
451 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
452 state
->key_exists
= FALSE
;
453 SetDlgItemText(hwnd
, IDC_GENERATING
, entropy_msg
);
454 state
->collecting_entropy
= TRUE
;
457 * My brief statistical tests on mouse movements
458 * suggest that there are about 5 bits of
459 * randomness in the x position, 5 in the y
460 * position, and 1.7 in the message time, making
461 * 11.7 bits of unpredictability per mouse
462 * movement. However, other people have told me
463 * it's far less than that, so I'm going to be
464 * stupidly cautious and knock that down to a nice
467 state
->entropy_required
= (KEYSIZE
/ 4) * 2;
468 state
->entropy_got
= 0;
469 state
->entropy_size
= (state
->entropy_required
*
470 sizeof(*state
->entropy
));
471 state
->entropy
= malloc(state
->entropy_size
);
473 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
474 MAKELPARAM(0, state
->entropy_required
));
475 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
479 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
480 if (state
->key_exists
) {
481 char filename
[FILENAME_MAX
];
482 char passphrase
[PASSPHRASE_MAXLEN
];
483 GetDlgItemText(hwnd
, IDC_PASSPHRASEEDIT
,
484 passphrase
, sizeof(passphrase
)-1);
487 ret
= MessageBox(hwnd
,
488 "Are you sure you want to save this key\n"
489 "without a passphrase to protect it?",
491 MB_YESNO
| MB_ICONWARNING
);
495 if (prompt_keyfile(hwnd
, "Save private key as:",
497 /* FIXME: prompt before overwriting */
498 saversakey(filename
, &state
->key
, &state
->aux
,
499 *passphrase ? passphrase
: NULL
);
500 /* FIXME: check return value */
505 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
506 if (!state
->generation_thread_exists
) {
507 char filename
[FILENAME_MAX
];
508 if (prompt_keyfile(hwnd
, "Load private key:",
510 char passphrase
[PASSPHRASE_MAXLEN
];
514 struct PassphraseProcStruct pps
;
515 struct RSAKey newkey
;
516 struct RSAAux newaux
;
518 needs_pass
= rsakey_encrypted(filename
, &comment
);
519 pps
.passphrase
= passphrase
;
520 pps
.comment
= comment
;
524 dlgret
= DialogBoxParam(hinst
,
525 MAKEINTRESOURCE(210),
526 NULL
, PassphraseProc
,
534 ret
= loadrsakey(filename
, &newkey
, &newaux
,
537 if (comment
) free(comment
);
539 MessageBox(NULL
, "Couldn't load private key.",
540 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
541 } else if (ret
== 1) {
545 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
546 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
547 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
549 * Now update the key controls with all the
554 SetDlgItemText(hwnd
, IDC_PASSPHRASEEDIT
,
556 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
,
559 * Set the key fingerprint.
562 char *savecomment
= state
->key
.comment
;
563 state
->key
.comment
= NULL
;
564 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
565 state
->key
.comment
= savecomment
;
567 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
569 * Construct a decimal representation
570 * of the key, for pasting into
571 * .ssh/authorized_keys on a Unix box.
573 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
576 * Finally, hide the progress bar and show
579 hidemany(hwnd
, nokey_ids
, TRUE
);
580 hidemany(hwnd
, generating_ids
, TRUE
);
581 hidemany(hwnd
, gotkey_ids
, FALSE
);
582 state
->key_exists
= TRUE
;
590 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
591 state
->generation_thread_exists
= FALSE
;
592 state
->key_exists
= TRUE
;
593 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, PROGRESSRANGE
, 0);
594 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
595 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
596 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
598 * Invent a comment for the key. We'll do this by including
599 * the date in it. This will be so horrifyingly ugly that
600 * the user will immediately want to change it, which is
603 state
->key
.comment
= malloc(30);
609 strftime(state
->key
.comment
, 30, "rsa-key-%Y%m%d", tm
);
613 * Now update the key controls with all the key data.
618 * Blank passphrase, initially. This isn't dangerous,
619 * because we will warn (Are You Sure?) before allowing
620 * the user to save an unprotected private key.
622 SetDlgItemText(hwnd
, IDC_PASSPHRASEEDIT
, "");
626 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
, state
->key
.comment
);
628 * Set the key fingerprint.
631 char *savecomment
= state
->key
.comment
;
632 state
->key
.comment
= NULL
;
633 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
634 state
->key
.comment
= savecomment
;
636 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
638 * Construct a decimal representation of the key, for
639 * pasting into .ssh/authorized_keys on a Unix box.
641 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
644 * Finally, hide the progress bar and show the key data.
646 hidemany(hwnd
, nokey_ids
, TRUE
);
647 hidemany(hwnd
, generating_ids
, TRUE
);
648 hidemany(hwnd
, gotkey_ids
, FALSE
);
651 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
659 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
662 return DialogBox(hinst
, MAKEINTRESOURCE(201), NULL
, MainDlgProc
) != IDOK
;