2 * PuTTY key generation front end.
13 #define PUTTY_DO_GLOBALS
19 #define WM_DONEKEY (WM_XUSER + 1)
21 #define DEFAULT_KEYSIZE 1024
23 /* ----------------------------------------------------------------------
24 * Progress report code. This is really horrible :-)
26 #define PHASE1TOTAL 0x10000
27 #define PHASE2TOTAL 0x10000
28 #define PHASE3TOTAL 0x04000
30 #define PHASE2START (PHASE1TOTAL)
31 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
32 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
33 #define PROGRESSBIGRANGE 65535
34 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
35 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
37 unsigned phase1param
, phase1current
, phase1n
;
38 unsigned phase2param
, phase2current
, phase2n
;
43 static void progress_update(void *param
, int phase
, int iprogress
) {
44 struct progress
*p
= (struct progress
*)param
;
45 unsigned progress
= iprogress
;
50 p
->phase1param
= 0x10000 + progress
;
51 p
->phase1current
= 0x10000; p
->phase1n
= 0;
54 p
->phase2param
= 0x10000 + progress
;
55 p
->phase2current
= 0x10000; p
->phase2n
= 0;
58 p
->phase3mult
= PHASE3TOTAL
/ progress
;
61 while (p
->phase1n
< progress
) {
63 p
->phase1current
*= p
->phase1param
;
64 p
->phase1current
/= 0x10000;
66 position
= PHASE1START
+ 0x10000 - p
->phase1current
;
69 while (p
->phase2n
< progress
) {
71 p
->phase2current
*= p
->phase2param
;
72 p
->phase2current
/= 0x10000;
74 position
= PHASE2START
+ 0x10000 - p
->phase2current
;
77 position
= PHASE3START
+ progress
* p
->phase3mult
;
81 SendMessage(p
->progbar
, PBM_SETPOS
, position
/ DIVISOR
, 0);
86 #define PASSPHRASE_MAXLEN 512
88 struct PassphraseProcStruct
{
94 * Dialog-box function for the passphrase box.
96 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
97 WPARAM wParam
, LPARAM lParam
) {
98 static char *passphrase
;
99 struct PassphraseProcStruct
*p
;
103 SetForegroundWindow(hwnd
);
104 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
105 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
106 p
= (struct PassphraseProcStruct
*)lParam
;
107 passphrase
= p
->passphrase
;
109 SetDlgItemText(hwnd
, 101, p
->comment
);
113 switch (LOWORD(wParam
)) {
123 case 102: /* edit box */
124 if (HIWORD(wParam
) == EN_CHANGE
) {
125 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
126 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
139 * Prompt for a key file. Assumes the filename buffer is of size
142 static int prompt_keyfile(HWND hwnd
, char *dlgtitle
,
143 char *filename
, int save
) {
145 memset(&of
, 0, sizeof(of
));
146 #ifdef OPENFILENAME_SIZE_VERSION_400
147 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
149 of
.lStructSize
= sizeof(of
);
152 of
.lpstrFilter
= "All Files\0*\0\0\0";
153 of
.lpstrCustomFilter
= NULL
;
155 of
.lpstrFile
= filename
; *filename
= '\0';
156 of
.nMaxFile
= FILENAME_MAX
;
157 of
.lpstrFileTitle
= NULL
;
158 of
.lpstrInitialDir
= NULL
;
159 of
.lpstrTitle
= dlgtitle
;
162 return GetSaveFileName(&of
);
164 return GetOpenFileName(&of
);
168 * This function is needed to link with the DES code. We need not
169 * have it do anything at all.
171 void logevent(char *msg
) {
175 * Dialog-box function for the Licence box.
177 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
178 WPARAM wParam
, LPARAM lParam
) {
183 switch (LOWORD(wParam
)) {
197 * Dialog-box function for the About box.
199 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
200 WPARAM wParam
, LPARAM lParam
) {
203 SetDlgItemText (hwnd
, 100, ver
);
206 switch (LOWORD(wParam
)) {
211 EnableWindow(hwnd
, 0);
212 DialogBox (hinst
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
213 EnableWindow(hwnd
, 1);
214 SetActiveWindow(hwnd
);
226 * Thread to generate a key.
228 struct rsa_key_thread_params
{
229 HWND progressbar
; /* notify this with progress */
230 HWND dialog
; /* notify this on completion */
231 int keysize
; /* bits in key */
235 static DWORD WINAPI
generate_rsa_key_thread(void *param
) {
236 struct rsa_key_thread_params
*params
=
237 (struct rsa_key_thread_params
*)param
;
238 struct progress prog
;
239 prog
.progbar
= params
->progressbar
;
241 rsa_generate(params
->key
, params
->aux
,
242 params
->keysize
, progress_update
, &prog
);
244 PostMessage(params
->dialog
, WM_DONEKEY
, 0, 0);
250 struct MainDlgState
{
251 int collecting_entropy
;
252 int generation_thread_exists
;
254 int entropy_got
, entropy_required
, entropy_size
;
261 static void hidemany(HWND hwnd
, const int *ids
, int hideit
) {
263 ShowWindow(GetDlgItem(hwnd
, *ids
++), (hideit ? SW_HIDE
: SW_SHOW
));
267 static void setupbigedit(HWND hwnd
, int id
, struct RSAKey
*key
) {
271 dec1
= bignum_decimal(key
->exponent
);
272 dec2
= bignum_decimal(key
->modulus
);
273 buffer
= malloc(strlen(dec1
)+strlen(dec2
)+
274 strlen(key
->comment
)+30);
275 sprintf(buffer
, "%d %s %s %s",
276 ssh1_bignum_bitcount(key
->modulus
),
277 dec1
, dec2
, key
->comment
);
278 SetDlgItemText(hwnd
, id
, buffer
);
285 * Dialog-box function for the main PuTTYgen dialog box.
287 static int CALLBACK
MainDlgProc (HWND hwnd
, UINT msg
,
288 WPARAM wParam
, LPARAM lParam
) {
290 controlidstart
= 100,
292 IDC_BOX_KEY
, IDC_BOXT_KEY
,
296 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
297 IDC_FPSTATIC
, IDC_FINGERPRINT
,
298 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
299 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
300 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
,
301 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
,
302 IDC_GENSTATIC
, IDC_GENERATE
,
303 IDC_LOADSTATIC
, IDC_LOAD
,
304 IDC_SAVESTATIC
, IDC_SAVE
,
305 IDC_BOX_PARAMS
, IDC_BOXT_PARAMS
,
306 IDC_BITSSTATIC
, IDC_BITS
,
309 static const int nokey_ids
[] = { IDC_NOKEY
, 0 };
310 static const int generating_ids
[] = { IDC_GENERATING
, IDC_PROGRESS
, 0 };
311 static const int gotkey_ids
[] = {
312 IDC_PKSTATIC
, IDC_KEYDISPLAY
,
313 IDC_FPSTATIC
, IDC_FINGERPRINT
,
314 IDC_COMMENTSTATIC
, IDC_COMMENTEDIT
,
315 IDC_PASSPHRASE1STATIC
, IDC_PASSPHRASE1EDIT
,
316 IDC_PASSPHRASE2STATIC
, IDC_PASSPHRASE2EDIT
, 0 };
317 static const char generating_msg
[] =
318 "Please wait while a key is generated...";
319 static const char entropy_msg
[] =
320 "Please generate some randomness by moving the mouse over the blank area.";
321 struct MainDlgState
*state
;
325 state
= malloc(sizeof(*state
));
326 state
->generation_thread_exists
= FALSE
;
327 state
->key_exists
= FALSE
;
328 SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
)state
);
330 struct ctlpos cp
, cp2
;
332 /* Accelerators used: acglops */
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_PASSPHRASE1STATIC
,
355 IDC_PASSPHRASE1EDIT
, 70);
356 staticpassedit(&cp
, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC
,
357 IDC_PASSPHRASE2EDIT
, 70);
359 beginbox(&cp
, "Actions",
360 IDC_BOX_ACTIONS
, IDC_BOXT_ACTIONS
);
361 staticbtn(&cp
, "Generate a public/private key pair",
362 IDC_GENSTATIC
, "&Generate", IDC_GENERATE
);
363 staticbtn(&cp
, "Load an existing private key file",
364 IDC_LOADSTATIC
, "&Load", IDC_LOAD
);
365 staticbtn(&cp
, "Save the generated key to a new file",
366 IDC_SAVESTATIC
, "&Save", IDC_SAVE
);
368 beginbox(&cp
, "Parameters",
369 IDC_BOX_PARAMS
, IDC_BOXT_PARAMS
);
370 staticedit(&cp
, "Number of &bits in a generated key:",
371 IDC_BITSSTATIC
, IDC_BITS
, 20);
374 SetDlgItemInt(hwnd
, IDC_BITS
, DEFAULT_KEYSIZE
, FALSE
);
377 * Initially, hide the progress bar and the key display,
378 * and show the no-key display. Also disable the Save
379 * button, because with no key we obviously can't save
382 hidemany(hwnd
, nokey_ids
, FALSE
);
383 hidemany(hwnd
, generating_ids
, TRUE
);
384 hidemany(hwnd
, gotkey_ids
, TRUE
);
385 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
389 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
390 if (state
->collecting_entropy
) {
391 state
->entropy
[state
->entropy_got
++] = lParam
;
392 state
->entropy
[state
->entropy_got
++] = GetMessageTime();
393 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
,
394 state
->entropy_got
, 0);
395 if (state
->entropy_got
>= state
->entropy_required
) {
396 struct rsa_key_thread_params
*params
;
400 * Seed the entropy pool
402 random_add_heavynoise(state
->entropy
, state
->entropy_size
);
403 memset(state
->entropy
, 0, state
->entropy_size
);
404 free(state
->entropy
);
406 SetDlgItemText(hwnd
, IDC_GENERATING
, generating_msg
);
407 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
408 MAKELPARAM(0, PROGRESSRANGE
));
409 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
411 params
= malloc(sizeof(*params
));
412 params
->progressbar
= GetDlgItem(hwnd
, IDC_PROGRESS
);
413 params
->dialog
= hwnd
;
414 params
->keysize
= state
->keysize
;
415 params
->key
= &state
->key
;
416 params
->aux
= &state
->aux
;
418 if (!CreateThread(NULL
, 0, generate_rsa_key_thread
,
419 params
, 0, &threadid
)) {
420 MessageBox(hwnd
, "Out of thread resources",
421 "Key generation error",
422 MB_OK
| MB_ICONERROR
);
425 state
->generation_thread_exists
= TRUE
;
426 state
->collecting_entropy
= FALSE
;
432 switch (LOWORD(wParam
)) {
433 case IDC_COMMENTEDIT
:
434 if (HIWORD(wParam
) == EN_CHANGE
) {
435 state
= (struct MainDlgState
*)
436 GetWindowLong(hwnd
, GWL_USERDATA
);
437 if (state
->key_exists
) {
438 HWND editctl
= GetDlgItem(hwnd
, IDC_COMMENTEDIT
);
439 int len
= GetWindowTextLength(editctl
);
440 if (state
->key
.comment
)
441 free(state
->key
.comment
);
442 state
->key
.comment
= malloc(len
+1);
443 GetWindowText(editctl
, state
->key
.comment
, len
+1);
448 EnableWindow(hwnd
, 0);
449 DialogBox (hinst
, MAKEINTRESOURCE(213), NULL
, AboutProc
);
450 EnableWindow(hwnd
, 1);
451 SetActiveWindow(hwnd
);
454 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
455 if (!state
->generation_thread_exists
) {
456 hidemany(hwnd
, nokey_ids
, TRUE
);
457 hidemany(hwnd
, generating_ids
, FALSE
);
458 hidemany(hwnd
, gotkey_ids
, TRUE
);
459 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 0);
460 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 0);
461 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 0);
462 state
->key_exists
= FALSE
;
463 SetDlgItemText(hwnd
, IDC_GENERATING
, entropy_msg
);
464 state
->collecting_entropy
= TRUE
;
467 state
->keysize
= GetDlgItemInt(hwnd
, IDC_BITS
,
469 if (!ok
) state
->keysize
= DEFAULT_KEYSIZE
;
473 * My brief statistical tests on mouse movements
474 * suggest that there are about 2.5 bits of
475 * randomness in the x position, 2.5 in the y
476 * position, and 1.7 in the message time, making
477 * 5.7 bits of unpredictability per mouse movement.
478 * However, other people have told me it's far less
479 * than that, so I'm going to be stupidly cautious
480 * and knock that down to a nice round 2. With this
481 * method, we require two words per mouse movement,
482 * so with 2 bits per mouse movement we expect 2
483 * bits every 2 words.
485 state
->entropy_required
= (state
->keysize
/2) * 2;
486 state
->entropy_got
= 0;
487 state
->entropy_size
= (state
->entropy_required
*
488 sizeof(*state
->entropy
));
489 state
->entropy
= malloc(state
->entropy_size
);
491 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETRANGE
, 0,
492 MAKELPARAM(0, state
->entropy_required
));
493 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
497 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
498 if (state
->key_exists
) {
499 char filename
[FILENAME_MAX
];
500 char passphrase
[PASSPHRASE_MAXLEN
];
501 char passphrase2
[PASSPHRASE_MAXLEN
];
502 GetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
503 passphrase
, sizeof(passphrase
));
504 GetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
505 passphrase2
, sizeof(passphrase2
));
506 if (strcmp(passphrase
, passphrase2
)) {
508 "The two passphrases given do not match.",
510 MB_OK
| MB_ICONERROR
);
515 ret
= MessageBox(hwnd
,
516 "Are you sure you want to save this key\n"
517 "without a passphrase to protect it?",
519 MB_YESNO
| MB_ICONWARNING
);
523 if (prompt_keyfile(hwnd
, "Save private key as:",
526 FILE *fp
= fopen(filename
, "r");
528 char buffer
[FILENAME_MAX
+80];
530 sprintf(buffer
, "Overwrite existing file\n%.*s?",
531 FILENAME_MAX
, filename
);
532 ret
= MessageBox(hwnd
, buffer
, "PuTTYgen Warning",
533 MB_YESNO
| MB_ICONWARNING
);
537 ret
= saversakey(filename
, &state
->key
, &state
->aux
,
538 *passphrase ? passphrase
: NULL
);
540 MessageBox(hwnd
, "Unable to save key file",
542 MB_OK
| MB_ICONERROR
);
548 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
549 if (!state
->generation_thread_exists
) {
550 char filename
[FILENAME_MAX
];
551 if (prompt_keyfile(hwnd
, "Load private key:",
553 char passphrase
[PASSPHRASE_MAXLEN
];
557 struct PassphraseProcStruct pps
;
558 struct RSAKey newkey
;
559 struct RSAAux newaux
;
561 needs_pass
= rsakey_encrypted(filename
, &comment
);
562 pps
.passphrase
= passphrase
;
563 pps
.comment
= comment
;
567 dlgret
= DialogBoxParam(hinst
,
568 MAKEINTRESOURCE(210),
569 NULL
, PassphraseProc
,
577 ret
= loadrsakey(filename
, &newkey
, &newaux
,
580 if (comment
) free(comment
);
582 MessageBox(NULL
, "Couldn't load private key.",
583 "PuTTYgen Error", MB_OK
| MB_ICONERROR
);
584 } else if (ret
== 1) {
588 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
589 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
590 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
592 * Now update the key controls with all the
597 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
,
599 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
,
601 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
,
604 * Set the key fingerprint.
607 char *savecomment
= state
->key
.comment
;
608 state
->key
.comment
= NULL
;
609 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
610 state
->key
.comment
= savecomment
;
612 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
614 * Construct a decimal representation
615 * of the key, for pasting into
616 * .ssh/authorized_keys on a Unix box.
618 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
621 * Finally, hide the progress bar and show
624 hidemany(hwnd
, nokey_ids
, TRUE
);
625 hidemany(hwnd
, generating_ids
, TRUE
);
626 hidemany(hwnd
, gotkey_ids
, FALSE
);
627 state
->key_exists
= TRUE
;
635 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
636 state
->generation_thread_exists
= FALSE
;
637 state
->key_exists
= TRUE
;
638 SendDlgItemMessage(hwnd
, IDC_PROGRESS
, PBM_SETPOS
, PROGRESSRANGE
, 0);
639 EnableWindow(GetDlgItem(hwnd
, IDC_GENERATE
), 1);
640 EnableWindow(GetDlgItem(hwnd
, IDC_LOAD
), 1);
641 EnableWindow(GetDlgItem(hwnd
, IDC_SAVE
), 1);
643 * Invent a comment for the key. We'll do this by including
644 * the date in it. This will be so horrifyingly ugly that
645 * the user will immediately want to change it, which is
648 state
->key
.comment
= malloc(30);
654 strftime(state
->key
.comment
, 30, "rsa-key-%Y%m%d", tm
);
658 * Now update the key controls with all the key data.
663 * Blank passphrase, initially. This isn't dangerous,
664 * because we will warn (Are You Sure?) before allowing
665 * the user to save an unprotected private key.
667 SetDlgItemText(hwnd
, IDC_PASSPHRASE1EDIT
, "");
668 SetDlgItemText(hwnd
, IDC_PASSPHRASE2EDIT
, "");
672 SetDlgItemText(hwnd
, IDC_COMMENTEDIT
, state
->key
.comment
);
674 * Set the key fingerprint.
677 char *savecomment
= state
->key
.comment
;
678 state
->key
.comment
= NULL
;
679 rsa_fingerprint(buf
, sizeof(buf
), &state
->key
);
680 state
->key
.comment
= savecomment
;
682 SetDlgItemText(hwnd
, IDC_FINGERPRINT
, buf
);
684 * Construct a decimal representation of the key, for
685 * pasting into .ssh/authorized_keys on a Unix box.
687 setupbigedit(hwnd
, IDC_KEYDISPLAY
, &state
->key
);
690 * Finally, hide the progress bar and show the key data.
692 hidemany(hwnd
, nokey_ids
, TRUE
);
693 hidemany(hwnd
, generating_ids
, TRUE
);
694 hidemany(hwnd
, gotkey_ids
, FALSE
);
697 state
= (struct MainDlgState
*)GetWindowLong(hwnd
, GWL_USERDATA
);
705 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
706 InitCommonControls();
709 return DialogBox(hinst
, MAKEINTRESOURCE(201), NULL
, MainDlgProc
) != IDOK
;