2 * PuTTY key generation front end.
13 #define PUTTY_DO_GLOBALS
19 #define WM_DONEKEY (WM_XUSER + 1)
25 * - check the return value from saversakey()
26 * - test the generated keys for actual working-RSA-key-hood
30 /* ----------------------------------------------------------------------
31 * Progress report code. This is really horrible :-)
33 #define PHASE1TOTAL 0x10000
34 #define PHASE2TOTAL 0x10000
35 #define PHASE3TOTAL 0x04000
37 #define PHASE2START (PHASE1TOTAL)
38 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
39 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
40 #define PROGRESSBIGRANGE 65535
41 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
42 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
44 unsigned phase1param
, phase1current
, phase1n
;
45 unsigned phase2param
, phase2current
, phase2n
;
50 static void progress_update(void *param
, int phase
, int iprogress
) {
51 struct progress
*p
= (struct progress
*)param
;
52 unsigned progress
= iprogress
;
57 p
->phase1param
= 0x10000 + progress
;
58 p
->phase1current
= 0x10000; p
->phase1n
= 0;
61 p
->phase2param
= 0x10000 + progress
;
62 p
->phase2current
= 0x10000; p
->phase2n
= 0;
65 p
->phase3mult
= PHASE3TOTAL
/ progress
;
68 while (p
->phase1n
< progress
) {
70 p
->phase1current
*= p
->phase1param
;
71 p
->phase1current
/= 0x10000;
73 position
= PHASE1START
+ 0x10000 - p
->phase1current
;
76 while (p
->phase2n
< progress
) {
78 p
->phase2current
*= p
->phase2param
;
79 p
->phase2current
/= 0x10000;
81 position
= PHASE2START
+ 0x10000 - p
->phase2current
;
84 position
= PHASE3START
+ progress
* p
->phase3mult
;
88 SendMessage(p
->progbar
, PBM_SETPOS
, position
/ DIVISOR
, 0);
93 #define PASSPHRASE_MAXLEN 512
95 struct PassphraseProcStruct
{
101 * Dialog-box function for the passphrase box.
103 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
104 WPARAM wParam
, LPARAM lParam
) {
105 static char *passphrase
;
106 struct PassphraseProcStruct
*p
;
110 SetForegroundWindow(hwnd
);
111 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
112 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
113 p
= (struct PassphraseProcStruct
*)lParam
;
114 passphrase
= p
->passphrase
;
116 SetDlgItemText(hwnd
, 101, p
->comment
);
120 switch (LOWORD(wParam
)) {
130 case 102: /* edit box */
131 if (HIWORD(wParam
) == EN_CHANGE
) {
132 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
133 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
146 * Prompt for a key file. Assumes the filename buffer is of size
149 static int prompt_keyfile(HWND hwnd
, char *dlgtitle
,
150 char *filename
, int save
) {
152 memset(&of
, 0, sizeof(of
));
153 #ifdef OPENFILENAME_SIZE_VERSION_400
154 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
156 of
.lStructSize
= sizeof(of
);
159 of
.lpstrFilter
= "All Files\0*\0\0\0";
160 of
.lpstrCustomFilter
= NULL
;
162 of
.lpstrFile
= filename
; *filename
= '\0';
163 of
.nMaxFile
= FILENAME_MAX
;
164 of
.lpstrFileTitle
= NULL
;
165 of
.lpstrInitialDir
= NULL
;
166 of
.lpstrTitle
= dlgtitle
;
169 return GetSaveFileName(&of
);
171 return GetOpenFileName(&of
);
175 * This function is needed to link with the DES code. We need not
176 * have it do anything at all.
178 void logevent(char *msg
) {
182 * Dialog-box function for the Licence box.
184 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
185 WPARAM wParam
, LPARAM lParam
) {
190 switch (LOWORD(wParam
)) {
204 * Dialog-box function for the About box.
206 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
207 WPARAM wParam
, LPARAM lParam
) {
210 SetDlgItemText (hwnd
, 100, ver
);
213 switch (LOWORD(wParam
)) {
218 EnableWindow(hwnd
, 0);
219 DialogBox (hinst
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
220 EnableWindow(hwnd
, 1);
221 SetActiveWindow(hwnd
);
233 * Thread to generate a key.
235 struct rsa_key_thread_params
{
236 HWND progressbar
; /* notify this with progress */
237 HWND dialog
; /* notify this on completion */
241 static DWORD WINAPI
generate_rsa_key_thread(void *param
) {
242 struct rsa_key_thread_params
*params
=
243 (struct rsa_key_thread_params
*)param
;
244 struct progress prog
;
245 prog
.progbar
= params
->progressbar
;
247 rsa_generate(params
->key
, params
->aux
, 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
;
265 static void hidemany(HWND hwnd
, const int *ids
, int hideit
) {
267 ShowWindow(GetDlgItem(hwnd
, *ids
++), (hideit ? SW_HIDE
: SW_SHOW
));
271 static void setupbigedit(HWND hwnd
, int id
, struct RSAKey
*key
) {
275 dec1
= bignum_decimal(key
->exponent
);
276 dec2
= bignum_decimal(key
->modulus
);
277 buffer
= malloc(strlen(dec1
)+strlen(dec2
)+
278 strlen(key
->comment
)+30);
279 sprintf(buffer
, "%d %s %s %s",
280 ssh1_bignum_bitcount(key
->modulus
),
281 dec1
, dec2
, key
->comment
);
282 SetDlgItemText(hwnd
, id
, buffer
);
289 * Dialog-box function for the main PuTTYgen dialog box.
291 static int CALLBACK
MainDlgProc (HWND hwnd
, UINT msg
,
292 WPARAM wParam
, LPARAM lParam
) {
294 controlidstart
= 100,
296 IDC_BOX_KEY
, IDC_BOXT_KEY
,
300 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
301 IDC_FPSTATIC
, IDC_FINGERPRINT
,
302 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
303 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
304 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
,
305 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
,
306 IDC_GENSTATIC
, IDC_GENERATE
,
307 IDC_LOADSTATIC
, IDC_LOAD
,
308 IDC_SAVESTATIC
, IDC_SAVE
,
311 static const int nokey_ids
[] = { IDC_NOKEY
, 0 };
312 static const int generating_ids
[] = { IDC_GENERATING
, IDC_PROGRESS
, 0 };
313 static const int gotkey_ids
[] = {
314 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
315 IDC_FPSTATIC
, IDC_FINGERPRINT
,
316 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
317 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
318 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
, 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 /* Accelerators used: acglops */
336 ctlposinit(&cp
, hwnd
, 10, 10, 10);
337 bartitle(&cp
, "Public and private key generation for PuTTY",
340 IDC_BOX_KEY
, IDC_BOXT_KEY
);
342 statictext(&cp2
, "No key.", IDC_NOKEY
);
346 progressbar(&cp2
, IDC_PROGRESS
);
348 "&Public key for pasting into authorized_keys file:",
349 IDC_PKSTATIC
, IDC_KEYDISPLAY
, 7);
350 SendDlgItemMessage(hwnd
, IDC_KEYDISPLAY
, EM_SETREADONLY
, 1, 0);
351 staticedit(&cp
, "Key fingerprint:", IDC_FPSTATIC
,
352 IDC_FINGERPRINT
, 70);
353 SendDlgItemMessage(hwnd
, IDC_FINGERPRINT
, EM_SETREADONLY
, 1, 0);
354 staticedit(&cp
, "Key &comment:", IDC_COMMENTSTATIC
,
355 IDC_COMMENTEDIT
, 70);
356 staticpassedit(&cp
, "Key p&assphrase:", IDC_PASSPHRASE1STATIC
,
357 IDC_PASSPHRASE1EDIT
, 70);
358 staticpassedit(&cp
, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC
,
359 IDC_PASSPHRASE2EDIT
, 70);
361 beginbox(&cp
, "Actions",
362 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
);
363 staticbtn(&cp
, "Generate a public/private key pair",
364 IDC_GENSTATIC
, "&Generate", IDC_GENERATE
);
365 staticbtn(&cp
, "Load an existing private key file",
366 IDC_LOADSTATIC
, "&Load", IDC_LOAD
);
367 staticbtn(&cp
, "Save the generated key to a new file",
368 IDC_SAVESTATIC
, "&Save", IDC_SAVE
);
372 * Initially, hide the progress bar and the key display,
373 * and show the no-key display. Also disable the Save
374 * button, because with no key we obviously can't save
377 hidemany(hwnd
, nokey_ids
, FALSE
);
378 hidemany(hwnd
, generating_ids
, TRUE
);
379 hidemany(hwnd
, gotkey_ids
, TRUE
);
380 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
384 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
385 if (state
->collecting_entropy
) {
386 state
->entropy
[state
->entropy_got
++] = lParam
;
387 state
->entropy
[state
->entropy_got
++] = GetMessageTime();
388 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
,
389 state
->entropy_got
, 0);
390 if (state
->entropy_got
>= state
->entropy_required
) {
391 struct rsa_key_thread_params
*params
;
395 * Seed the entropy pool
397 random_add_heavynoise(state
->entropy
, state
->entropy_size
);
398 memset(state
->entropy
, 0, state
->entropy_size
);
399 free(state
->entropy
);
401 SetDlgItemText(hwnd
, IDC_GENERATING
, generating_msg
);
402 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
403 MAKELPARAM(0, PROGRESSRANGE
));
404 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
406 params
= malloc(sizeof(*params
));
407 params
->progressbar
= GetDlgItem(hwnd
, IDC_PROGRESS
);
408 params
->dialog
= hwnd
;
409 params
->key
= &state
->key
;
410 params
->aux
= &state
->aux
;
412 if (!CreateThread(NULL
, 0, generate_rsa_key_thread
,
413 params
, 0, &threadid
)) {
414 MessageBox(hwnd
, "Out of thread resources",
415 "Key generation error",
416 MB_OK
| MB_ICONERROR
);
419 state
->generation_thread_exists
= TRUE
;
420 state
->collecting_entropy
= FALSE
;
426 switch (LOWORD(wParam
)) {
427 case IDC_COMMENTEDIT
:
428 if (HIWORD(wParam
) == EN_CHANGE
) {
429 state
= (struct MainDlgState
*)
430 GetWindowLong(hwnd
, GWL_USERDATA
);
431 if (state
->key_exists
) {
432 HWND editctl
= GetDlgItem(hwnd
, IDC_COMMENTEDIT
);
433 int len
= GetWindowTextLength(editctl
);
434 if (state
->key
.comment
)
435 free(state
->key
.comment
);
436 state
->key
.comment
= malloc(len
+1);
437 GetWindowText(editctl
, state
->key
.comment
, len
+1);
442 EnableWindow(hwnd
, 0);
443 DialogBox (hinst
, MAKEINTRESOURCE(213), NULL
, AboutProc
);
444 EnableWindow(hwnd
, 1);
445 SetActiveWindow(hwnd
);
448 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
449 if (!state
->generation_thread_exists
) {
450 hidemany(hwnd
, nokey_ids
, TRUE
);
451 hidemany(hwnd
, generating_ids
, FALSE
);
452 hidemany(hwnd
, gotkey_ids
, TRUE
);
453 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 0);
454 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 0);
455 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
456 state
->key_exists
= FALSE
;
457 SetDlgItemText(hwnd
, IDC_GENERATING
, entropy_msg
);
458 state
->collecting_entropy
= TRUE
;
461 * My brief statistical tests on mouse movements
462 * suggest that there are about 5 bits of
463 * randomness in the x position, 5 in the y
464 * position, and 1.7 in the message time, making
465 * 11.7 bits of unpredictability per mouse
466 * movement. However, other people have told me
467 * it's far less than that, so I'm going to be
468 * stupidly cautious and knock that down to a nice
471 state
->entropy_required
= (KEYSIZE
/ 4) * 2;
472 state
->entropy_got
= 0;
473 state
->entropy_size
= (state
->entropy_required
*
474 sizeof(*state
->entropy
));
475 state
->entropy
= malloc(state
->entropy_size
);
477 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
478 MAKELPARAM(0, state
->entropy_required
));
479 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
483 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
484 if (state
->key_exists
) {
485 char filename
[FILENAME_MAX
];
486 char passphrase
[PASSPHRASE_MAXLEN
];
487 char passphrase2
[PASSPHRASE_MAXLEN
];
488 GetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
489 passphrase
, sizeof(passphrase
));
490 GetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
491 passphrase2
, sizeof(passphrase2
));
492 if (strcmp(passphrase
, passphrase2
)) {
494 "The two passphrases given do not match.",
496 MB_OK
| MB_ICONERROR
);
501 ret
= MessageBox(hwnd
,
502 "Are you sure you want to save this key\n"
503 "without a passphrase to protect it?",
505 MB_YESNO
| MB_ICONWARNING
);
509 if (prompt_keyfile(hwnd
, "Save private key as:",
511 FILE *fp
= fopen(filename
, "r");
514 char buffer
[FILENAME_MAX
+80];
516 sprintf(buffer
, "Overwrite existing file\n%.*s?",
517 FILENAME_MAX
, filename
);
518 ret
= MessageBox(hwnd
, buffer
, "PuTTYgen Warning",
519 MB_YESNO
| MB_ICONWARNING
);
523 saversakey(filename
, &state
->key
, &state
->aux
,
524 *passphrase ? passphrase
: NULL
);
525 /* FIXME: check return value */
530 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
531 if (!state
->generation_thread_exists
) {
532 char filename
[FILENAME_MAX
];
533 if (prompt_keyfile(hwnd
, "Load private key:",
535 char passphrase
[PASSPHRASE_MAXLEN
];
539 struct PassphraseProcStruct pps
;
540 struct RSAKey newkey
;
541 struct RSAAux newaux
;
543 needs_pass
= rsakey_encrypted(filename
, &comment
);
544 pps
.passphrase
= passphrase
;
545 pps
.comment
= comment
;
549 dlgret
= DialogBoxParam(hinst
,
550 MAKEINTRESOURCE(210),
551 NULL
, PassphraseProc
,
559 ret
= loadrsakey(filename
, &newkey
, &newaux
,
562 if (comment
) free(comment
);
564 MessageBox(NULL
, "Couldn't load private key.",
565 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
566 } else if (ret
== 1) {
570 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
571 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
572 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
574 * Now update the key controls with all the
579 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
581 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
583 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
,
586 * Set the key fingerprint.
589 char *savecomment
= state
->key
.comment
;
590 state
->key
.comment
= NULL
;
591 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
592 state
->key
.comment
= savecomment
;
594 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
596 * Construct a decimal representation
597 * of the key, for pasting into
598 * .ssh/authorized_keys on a Unix box.
600 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
603 * Finally, hide the progress bar and show
606 hidemany(hwnd
, nokey_ids
, TRUE
);
607 hidemany(hwnd
, generating_ids
, TRUE
);
608 hidemany(hwnd
, gotkey_ids
, FALSE
);
609 state
->key_exists
= TRUE
;
617 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
618 state
->generation_thread_exists
= FALSE
;
619 state
->key_exists
= TRUE
;
620 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, PROGRESSRANGE
, 0);
621 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
622 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
623 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
625 * Invent a comment for the key. We'll do this by including
626 * the date in it. This will be so horrifyingly ugly that
627 * the user will immediately want to change it, which is
630 state
->key
.comment
= malloc(30);
636 strftime(state
->key
.comment
, 30, "rsa-key-%Y%m%d", tm
);
640 * Now update the key controls with all the key data.
645 * Blank passphrase, initially. This isn't dangerous,
646 * because we will warn (Are You Sure?) before allowing
647 * the user to save an unprotected private key.
649 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
, "");
650 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
, "");
654 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
, state
->key
.comment
);
656 * Set the key fingerprint.
659 char *savecomment
= state
->key
.comment
;
660 state
->key
.comment
= NULL
;
661 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
662 state
->key
.comment
= savecomment
;
664 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
666 * Construct a decimal representation of the key, for
667 * pasting into .ssh/authorized_keys on a Unix box.
669 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
672 * Finally, hide the progress bar and show the key data.
674 hidemany(hwnd
, nokey_ids
, TRUE
);
675 hidemany(hwnd
, generating_ids
, TRUE
);
676 hidemany(hwnd
, gotkey_ids
, FALSE
);
679 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
687 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
688 InitCommonControls();
691 return DialogBox(hinst
, MAKEINTRESOURCE(201), NULL
, MainDlgProc
) != IDOK
;