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