6e522441 |
1 | /* |
2 | * PuTTY key generation front end. |
3 | */ |
4 | |
5 | #include <windows.h> |
6 | #include <commctrl.h> |
7 | #include <time.h> |
8 | #ifndef NO_SECURITY |
9 | #include <aclapi.h> |
10 | #endif |
11 | #include <stdio.h> |
12 | |
13 | #define PUTTY_DO_GLOBALS |
14 | |
15 | #include "putty.h" |
16 | #include "ssh.h" |
17 | #include "winstuff.h" |
18 | |
19 | #define WM_DONEKEY (WM_XUSER + 1) |
20 | |
0c2986d0 |
21 | #define DEFAULT_KEYSIZE 1024 |
6e522441 |
22 | |
6e522441 |
23 | /* ---------------------------------------------------------------------- |
24 | * Progress report code. This is really horrible :-) |
25 | */ |
26 | #define PHASE1TOTAL 0x10000 |
27 | #define PHASE2TOTAL 0x10000 |
28 | #define PHASE3TOTAL 0x04000 |
29 | #define PHASE1START 0 |
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) |
36 | struct progress { |
37 | unsigned phase1param, phase1current, phase1n; |
38 | unsigned phase2param, phase2current, phase2n; |
39 | unsigned phase3mult; |
40 | HWND progbar; |
41 | }; |
42 | |
43 | static void progress_update(void *param, int phase, int iprogress) { |
44 | struct progress *p = (struct progress *)param; |
45 | unsigned progress = iprogress; |
46 | int position; |
47 | |
48 | switch (phase) { |
49 | case -1: |
50 | p->phase1param = 0x10000 + progress; |
51 | p->phase1current = 0x10000; p->phase1n = 0; |
52 | return; |
53 | case -2: |
54 | p->phase2param = 0x10000 + progress; |
55 | p->phase2current = 0x10000; p->phase2n = 0; |
56 | return; |
57 | case -3: |
58 | p->phase3mult = PHASE3TOTAL / progress; |
59 | return; |
60 | case 1: |
61 | while (p->phase1n < progress) { |
62 | p->phase1n++; |
63 | p->phase1current *= p->phase1param; |
64 | p->phase1current /= 0x10000; |
65 | } |
66 | position = PHASE1START + 0x10000 - p->phase1current; |
67 | break; |
68 | case 2: |
69 | while (p->phase2n < progress) { |
70 | p->phase2n++; |
71 | p->phase2current *= p->phase2param; |
72 | p->phase2current /= 0x10000; |
73 | } |
74 | position = PHASE2START + 0x10000 - p->phase2current; |
75 | break; |
76 | case 3: |
77 | position = PHASE3START + progress * p->phase3mult; |
78 | break; |
79 | } |
80 | |
81 | SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0); |
82 | } |
83 | |
84 | extern char ver[]; |
85 | |
86 | #define PASSPHRASE_MAXLEN 512 |
87 | |
88 | struct PassphraseProcStruct { |
89 | char *passphrase; |
90 | char *comment; |
91 | }; |
92 | |
93 | /* |
94 | * Dialog-box function for the passphrase box. |
95 | */ |
96 | static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, |
97 | WPARAM wParam, LPARAM lParam) { |
98 | static char *passphrase; |
99 | struct PassphraseProcStruct *p; |
100 | |
101 | switch (msg) { |
102 | case WM_INITDIALOG: |
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; |
108 | if (p->comment) |
109 | SetDlgItemText(hwnd, 101, p->comment); |
110 | *passphrase = 0; |
111 | return 0; |
112 | case WM_COMMAND: |
113 | switch (LOWORD(wParam)) { |
114 | case IDOK: |
115 | if (*passphrase) |
116 | EndDialog (hwnd, 1); |
117 | else |
118 | MessageBeep (0); |
119 | return 0; |
120 | case IDCANCEL: |
121 | EndDialog (hwnd, 0); |
122 | return 0; |
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'; |
127 | } |
128 | return 0; |
129 | } |
130 | return 0; |
131 | case WM_CLOSE: |
132 | EndDialog (hwnd, 0); |
133 | return 0; |
134 | } |
135 | return 0; |
136 | } |
137 | |
138 | /* |
139 | * Prompt for a key file. Assumes the filename buffer is of size |
140 | * FILENAME_MAX. |
141 | */ |
142 | static int prompt_keyfile(HWND hwnd, char *dlgtitle, |
143 | char *filename, int save) { |
144 | OPENFILENAME of; |
145 | memset(&of, 0, sizeof(of)); |
146 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
147 | of.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
148 | #else |
149 | of.lStructSize = sizeof(of); |
150 | #endif |
151 | of.hwndOwner = hwnd; |
152 | of.lpstrFilter = "All Files\0*\0\0\0"; |
153 | of.lpstrCustomFilter = NULL; |
154 | of.nFilterIndex = 1; |
155 | of.lpstrFile = filename; *filename = '\0'; |
156 | of.nMaxFile = FILENAME_MAX; |
157 | of.lpstrFileTitle = NULL; |
158 | of.lpstrInitialDir = NULL; |
159 | of.lpstrTitle = dlgtitle; |
160 | of.Flags = 0; |
161 | if (save) |
162 | return GetSaveFileName(&of); |
163 | else |
164 | return GetOpenFileName(&of); |
165 | } |
166 | |
167 | /* |
168 | * This function is needed to link with the DES code. We need not |
169 | * have it do anything at all. |
170 | */ |
171 | void logevent(char *msg) { |
172 | } |
173 | |
174 | /* |
175 | * Dialog-box function for the Licence box. |
176 | */ |
177 | static int CALLBACK LicenceProc (HWND hwnd, UINT msg, |
178 | WPARAM wParam, LPARAM lParam) { |
179 | switch (msg) { |
180 | case WM_INITDIALOG: |
181 | return 1; |
182 | case WM_COMMAND: |
183 | switch (LOWORD(wParam)) { |
184 | case IDOK: |
185 | EndDialog(hwnd, 1); |
186 | return 0; |
187 | } |
188 | return 0; |
189 | case WM_CLOSE: |
190 | EndDialog(hwnd, 1); |
191 | return 0; |
192 | } |
193 | return 0; |
194 | } |
195 | |
196 | /* |
197 | * Dialog-box function for the About box. |
198 | */ |
199 | static int CALLBACK AboutProc (HWND hwnd, UINT msg, |
200 | WPARAM wParam, LPARAM lParam) { |
201 | switch (msg) { |
202 | case WM_INITDIALOG: |
203 | SetDlgItemText (hwnd, 100, ver); |
204 | return 1; |
205 | case WM_COMMAND: |
206 | switch (LOWORD(wParam)) { |
207 | case IDOK: |
208 | EndDialog(hwnd, 1); |
209 | return 0; |
210 | case 101: |
211 | EnableWindow(hwnd, 0); |
212 | DialogBox (hinst, MAKEINTRESOURCE(214), NULL, LicenceProc); |
213 | EnableWindow(hwnd, 1); |
214 | SetActiveWindow(hwnd); |
215 | return 0; |
216 | } |
217 | return 0; |
218 | case WM_CLOSE: |
219 | EndDialog(hwnd, 1); |
220 | return 0; |
221 | } |
222 | return 0; |
223 | } |
224 | |
225 | /* |
226 | * Thread to generate a key. |
227 | */ |
228 | struct rsa_key_thread_params { |
229 | HWND progressbar; /* notify this with progress */ |
230 | HWND dialog; /* notify this on completion */ |
0c2986d0 |
231 | int keysize; /* bits in key */ |
6e522441 |
232 | struct RSAKey *key; |
233 | struct RSAAux *aux; |
234 | }; |
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; |
240 | |
0c2986d0 |
241 | rsa_generate(params->key, params->aux, |
242 | params->keysize, progress_update, &prog); |
6e522441 |
243 | |
244 | PostMessage(params->dialog, WM_DONEKEY, 0, 0); |
245 | |
246 | free(params); |
247 | return 0; |
248 | } |
249 | |
250 | struct MainDlgState { |
251 | int collecting_entropy; |
252 | int generation_thread_exists; |
253 | int key_exists; |
254 | int entropy_got, entropy_required, entropy_size; |
0c2986d0 |
255 | int keysize; |
6e522441 |
256 | unsigned *entropy; |
257 | struct RSAKey key; |
258 | struct RSAAux aux; |
259 | }; |
260 | |
261 | static void hidemany(HWND hwnd, const int *ids, int hideit) { |
262 | while (*ids) { |
263 | ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW)); |
264 | } |
265 | } |
266 | |
267 | static void setupbigedit(HWND hwnd, int id, struct RSAKey *key) { |
268 | char *buffer; |
269 | char *dec1, *dec2; |
270 | |
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); |
279 | free(dec1); |
280 | free(dec2); |
281 | free(buffer); |
282 | } |
283 | |
284 | /* |
285 | * Dialog-box function for the main PuTTYgen dialog box. |
286 | */ |
287 | static int CALLBACK MainDlgProc (HWND hwnd, UINT msg, |
288 | WPARAM wParam, LPARAM lParam) { |
289 | enum { |
290 | controlidstart = 100, |
291 | IDC_TITLE, |
292 | IDC_BOX_KEY, IDC_BOXT_KEY, |
293 | IDC_NOKEY, |
294 | IDC_GENERATING, |
295 | IDC_PROGRESS, |
296 | IDC_PKSTATIC, IDC_KEYDISPLAY, |
297 | IDC_FPSTATIC, IDC_FINGERPRINT, |
298 | IDC_COMMENTSTATIC, IDC_COMMENTEDIT, |
a5470c60 |
299 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
300 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, |
6e522441 |
301 | IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS, |
302 | IDC_GENSTATIC, IDC_GENERATE, |
303 | IDC_LOADSTATIC, IDC_LOAD, |
304 | IDC_SAVESTATIC, IDC_SAVE, |
0c2986d0 |
305 | IDC_BOX_PARAMS, IDC_BOXT_PARAMS, |
306 | IDC_BITSSTATIC, IDC_BITS, |
6e522441 |
307 | IDC_ABOUT, |
308 | }; |
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, |
a5470c60 |
315 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
316 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 }; |
6e522441 |
317 | static const char generating_msg[] = |
318 | "Please wait while a key is generated..."; |
319 | static const char entropy_msg[] = |
47c98e43 |
320 | "Please generate some randomness by moving the mouse over the blank area."; |
6e522441 |
321 | struct MainDlgState *state; |
322 | |
323 | switch (msg) { |
324 | case WM_INITDIALOG: |
325 | state = malloc(sizeof(*state)); |
326 | state->generation_thread_exists = FALSE; |
327 | state->key_exists = FALSE; |
328 | SetWindowLong(hwnd, GWL_USERDATA, (LONG)state); |
329 | { |
330 | struct ctlpos cp, cp2; |
331 | |
a5470c60 |
332 | /* Accelerators used: acglops */ |
333 | |
6e522441 |
334 | ctlposinit(&cp, hwnd, 10, 10, 10); |
335 | bartitle(&cp, "Public and private key generation for PuTTY", |
336 | IDC_TITLE); |
337 | beginbox(&cp, "Key", |
338 | IDC_BOX_KEY, IDC_BOXT_KEY); |
339 | cp2 = cp; |
340 | statictext(&cp2, "No key.", IDC_NOKEY); |
341 | cp2 = cp; |
342 | statictext(&cp2, "", |
343 | IDC_GENERATING); |
344 | progressbar(&cp2, IDC_PROGRESS); |
345 | bigeditctrl(&cp, |
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); |
a5470c60 |
354 | staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC, |
355 | IDC_PASSPHRASE1EDIT, 70); |
356 | staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC, |
357 | IDC_PASSPHRASE2EDIT, 70); |
6e522441 |
358 | endbox(&cp); |
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); |
367 | endbox(&cp); |
ec68f104 |
368 | beginbox(&cp, "Parameters", |
369 | IDC_BOX_PARAMS, IDC_BOXT_PARAMS); |
47c98e43 |
370 | staticedit(&cp, "Number of &bits in a generated key:", |
0c2986d0 |
371 | IDC_BITSSTATIC, IDC_BITS, 20); |
372 | endbox(&cp); |
6e522441 |
373 | } |
0c2986d0 |
374 | SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE); |
375 | |
6e522441 |
376 | /* |
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 |
380 | * anything. |
381 | */ |
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); |
386 | |
387 | return 1; |
388 | case WM_MOUSEMOVE: |
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; |
397 | DWORD threadid; |
398 | |
399 | /* |
400 | * Seed the entropy pool |
401 | */ |
402 | random_add_heavynoise(state->entropy, state->entropy_size); |
403 | memset(state->entropy, 0, state->entropy_size); |
404 | free(state->entropy); |
405 | |
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); |
410 | |
411 | params = malloc(sizeof(*params)); |
412 | params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS); |
413 | params->dialog = hwnd; |
0c2986d0 |
414 | params->keysize = state->keysize; |
6e522441 |
415 | params->key = &state->key; |
416 | params->aux = &state->aux; |
417 | |
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); |
423 | free(params); |
424 | } else { |
425 | state->generation_thread_exists = TRUE; |
426 | state->collecting_entropy = FALSE; |
427 | } |
428 | } |
429 | } |
430 | break; |
431 | case WM_COMMAND: |
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); |
444 | } |
445 | } |
446 | break; |
447 | case IDC_ABOUT: |
448 | EnableWindow(hwnd, 0); |
449 | DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc); |
450 | EnableWindow(hwnd, 1); |
451 | SetActiveWindow(hwnd); |
452 | return 0; |
453 | case IDC_GENERATE: |
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; |
0c2986d0 |
465 | { |
466 | BOOL ok; |
467 | state->keysize = GetDlgItemInt(hwnd, IDC_BITS, |
468 | &ok, FALSE); |
469 | if (!ok) state->keysize = DEFAULT_KEYSIZE; |
470 | } |
6e522441 |
471 | |
472 | /* |
473 | * My brief statistical tests on mouse movements |
47c98e43 |
474 | * suggest that there are about 2.5 bits of |
475 | * randomness in the x position, 2.5 in the y |
6e522441 |
476 | * position, and 1.7 in the message time, making |
47c98e43 |
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. |
6e522441 |
484 | */ |
47c98e43 |
485 | state->entropy_required = (state->keysize/2) * 2; |
6e522441 |
486 | state->entropy_got = 0; |
487 | state->entropy_size = (state->entropy_required * |
488 | sizeof(*state->entropy)); |
489 | state->entropy = malloc(state->entropy_size); |
490 | |
491 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, |
492 | MAKELPARAM(0, state->entropy_required)); |
493 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
494 | } |
495 | break; |
496 | case IDC_SAVE: |
497 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
498 | if (state->key_exists) { |
499 | char filename[FILENAME_MAX]; |
500 | char passphrase[PASSPHRASE_MAXLEN]; |
a5470c60 |
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)) { |
507 | MessageBox(hwnd, |
508 | "The two passphrases given do not match.", |
509 | "PuTTYgen Error", |
510 | MB_OK | MB_ICONERROR); |
511 | break; |
512 | } |
6e522441 |
513 | if (!*passphrase) { |
514 | int ret; |
515 | ret = MessageBox(hwnd, |
516 | "Are you sure you want to save this key\n" |
517 | "without a passphrase to protect it?", |
518 | "PuTTYgen Warning", |
519 | MB_YESNO | MB_ICONWARNING); |
520 | if (ret != IDYES) |
521 | break; |
522 | } |
523 | if (prompt_keyfile(hwnd, "Save private key as:", |
524 | filename, 1)) { |
bdea0743 |
525 | int ret; |
18790478 |
526 | FILE *fp = fopen(filename, "r"); |
527 | if (fp) { |
18790478 |
528 | char buffer[FILENAME_MAX+80]; |
529 | fclose(fp); |
530 | sprintf(buffer, "Overwrite existing file\n%.*s?", |
531 | FILENAME_MAX, filename); |
532 | ret = MessageBox(hwnd, buffer, "PuTTYgen Warning", |
533 | MB_YESNO | MB_ICONWARNING); |
534 | if (ret != IDYES) |
535 | break; |
536 | } |
bdea0743 |
537 | ret = saversakey(filename, &state->key, &state->aux, |
538 | *passphrase ? passphrase : NULL); |
539 | if (ret <= 0) { |
540 | MessageBox(hwnd, "Unable to save key file", |
541 | "PuTTYgen Error", |
542 | MB_OK | MB_ICONERROR); |
543 | } |
6e522441 |
544 | } |
545 | } |
546 | break; |
547 | case IDC_LOAD: |
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:", |
552 | filename, 0)) { |
553 | char passphrase[PASSPHRASE_MAXLEN]; |
554 | int needs_pass; |
555 | int ret; |
556 | char *comment; |
557 | struct PassphraseProcStruct pps; |
558 | struct RSAKey newkey; |
559 | struct RSAAux newaux; |
560 | |
561 | needs_pass = rsakey_encrypted(filename, &comment); |
562 | pps.passphrase = passphrase; |
563 | pps.comment = comment; |
564 | do { |
565 | if (needs_pass) { |
566 | int dlgret; |
567 | dlgret = DialogBoxParam(hinst, |
568 | MAKEINTRESOURCE(210), |
569 | NULL, PassphraseProc, |
570 | (LPARAM)&pps); |
571 | if (!dlgret) { |
572 | ret = -2; |
573 | break; |
574 | } |
575 | } else |
576 | *passphrase = '\0'; |
577 | ret = loadrsakey(filename, &newkey, &newaux, |
578 | passphrase); |
579 | } while (ret == -1); |
580 | if (comment) free(comment); |
581 | if (ret == 0) { |
582 | MessageBox(NULL, "Couldn't load private key.", |
583 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
584 | } else if (ret == 1) { |
585 | state->key = newkey; |
586 | state->aux = newaux; |
587 | |
588 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); |
589 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); |
590 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); |
591 | /* |
592 | * Now update the key controls with all the |
593 | * key data. |
594 | */ |
595 | { |
596 | char buf[128]; |
a5470c60 |
597 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, |
598 | passphrase); |
599 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, |
6e522441 |
600 | passphrase); |
601 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, |
602 | state->key.comment); |
603 | /* |
604 | * Set the key fingerprint. |
605 | */ |
606 | { |
607 | char *savecomment = state->key.comment; |
608 | state->key.comment = NULL; |
609 | rsa_fingerprint(buf, sizeof(buf), &state->key); |
610 | state->key.comment = savecomment; |
611 | } |
612 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
613 | /* |
614 | * Construct a decimal representation |
615 | * of the key, for pasting into |
616 | * .ssh/authorized_keys on a Unix box. |
617 | */ |
618 | setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key); |
619 | } |
620 | /* |
621 | * Finally, hide the progress bar and show |
622 | * the key data. |
623 | */ |
624 | hidemany(hwnd, nokey_ids, TRUE); |
625 | hidemany(hwnd, generating_ids, TRUE); |
626 | hidemany(hwnd, gotkey_ids, FALSE); |
627 | state->key_exists = TRUE; |
628 | } |
629 | } |
630 | } |
631 | break; |
632 | } |
633 | return 0; |
634 | case WM_DONEKEY: |
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); |
642 | /* |
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 |
646 | * what we want :-) |
647 | */ |
648 | state->key.comment = malloc(30); |
649 | { |
650 | time_t t; |
651 | struct tm *tm; |
652 | time(&t); |
653 | tm = localtime(&t); |
654 | strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm); |
655 | } |
656 | |
657 | /* |
658 | * Now update the key controls with all the key data. |
659 | */ |
660 | { |
661 | char buf[128]; |
662 | /* |
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. |
666 | */ |
a5470c60 |
667 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, ""); |
668 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, ""); |
6e522441 |
669 | /* |
670 | * Set the comment. |
671 | */ |
672 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment); |
673 | /* |
674 | * Set the key fingerprint. |
675 | */ |
676 | { |
677 | char *savecomment = state->key.comment; |
678 | state->key.comment = NULL; |
679 | rsa_fingerprint(buf, sizeof(buf), &state->key); |
680 | state->key.comment = savecomment; |
681 | } |
682 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
683 | /* |
684 | * Construct a decimal representation of the key, for |
685 | * pasting into .ssh/authorized_keys on a Unix box. |
686 | */ |
687 | setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key); |
688 | } |
689 | /* |
690 | * Finally, hide the progress bar and show the key data. |
691 | */ |
692 | hidemany(hwnd, nokey_ids, TRUE); |
693 | hidemany(hwnd, generating_ids, TRUE); |
694 | hidemany(hwnd, gotkey_ids, FALSE); |
695 | break; |
696 | case WM_CLOSE: |
697 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
698 | free(state); |
699 | EndDialog(hwnd, 1); |
700 | return 0; |
701 | } |
702 | return 0; |
703 | } |
704 | |
705 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { |
8b8cf871 |
706 | InitCommonControls(); |
6e522441 |
707 | hinst = inst; |
708 | random_init(); |
709 | return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK; |
710 | } |