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