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 | |
32874aea |
41 | static void progress_update(void *param, int phase, int iprogress) |
42 | { |
43 | struct progress *p = (struct progress *) param; |
6e522441 |
44 | unsigned progress = iprogress; |
45 | int position; |
46 | |
47 | switch (phase) { |
48 | case -1: |
32874aea |
49 | p->phase1param = 0x10000 + progress; |
50 | p->phase1current = 0x10000; |
51 | p->phase1n = 0; |
52 | return; |
6e522441 |
53 | case -2: |
32874aea |
54 | p->phase2param = 0x10000 + progress; |
55 | p->phase2current = 0x10000; |
56 | p->phase2n = 0; |
57 | return; |
6e522441 |
58 | case -3: |
32874aea |
59 | p->phase3mult = PHASE3TOTAL / progress; |
60 | return; |
6e522441 |
61 | case 1: |
32874aea |
62 | while (p->phase1n < progress) { |
63 | p->phase1n++; |
64 | p->phase1current *= p->phase1param; |
65 | p->phase1current /= 0x10000; |
66 | } |
67 | position = PHASE1START + 0x10000 - p->phase1current; |
68 | break; |
6e522441 |
69 | case 2: |
32874aea |
70 | while (p->phase2n < progress) { |
71 | p->phase2n++; |
72 | p->phase2current *= p->phase2param; |
73 | p->phase2current /= 0x10000; |
74 | } |
75 | position = PHASE2START + 0x10000 - p->phase2current; |
76 | break; |
6e522441 |
77 | case 3: |
32874aea |
78 | position = PHASE3START + progress * p->phase3mult; |
79 | break; |
2d466ffd |
80 | default: |
81 | /* |
82 | * Shouldn't happen, but having a default clause placates |
83 | * gcc -Wall, which would otherwise complain that |
84 | * `position' might be used uninitialised. |
85 | */ |
86 | return; |
6e522441 |
87 | } |
88 | |
89 | SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0); |
90 | } |
91 | |
92 | extern char ver[]; |
93 | |
94 | #define PASSPHRASE_MAXLEN 512 |
95 | |
96 | struct PassphraseProcStruct { |
97 | char *passphrase; |
98 | char *comment; |
99 | }; |
100 | |
101 | /* |
102 | * Dialog-box function for the passphrase box. |
103 | */ |
104 | static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, |
32874aea |
105 | WPARAM wParam, LPARAM lParam) |
106 | { |
8e1feb27 |
107 | static char *passphrase = NULL; |
6e522441 |
108 | struct PassphraseProcStruct *p; |
109 | |
110 | switch (msg) { |
111 | case WM_INITDIALOG: |
32874aea |
112 | SetForegroundWindow(hwnd); |
113 | SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, |
114 | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); |
65a22376 |
115 | |
116 | /* |
117 | * Centre the window. |
118 | */ |
119 | { /* centre the window */ |
120 | RECT rs, rd; |
121 | HWND hw; |
122 | |
123 | hw = GetDesktopWindow(); |
32874aea |
124 | if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) |
125 | MoveWindow(hwnd, |
126 | (rs.right + rs.left + rd.left - rd.right) / 2, |
127 | (rs.bottom + rs.top + rd.top - rd.bottom) / 2, |
128 | rd.right - rd.left, rd.bottom - rd.top, TRUE); |
65a22376 |
129 | } |
130 | |
32874aea |
131 | p = (struct PassphraseProcStruct *) lParam; |
132 | passphrase = p->passphrase; |
133 | if (p->comment) |
134 | SetDlgItemText(hwnd, 101, p->comment); |
135 | *passphrase = 0; |
136 | SetDlgItemText(hwnd, 102, passphrase); |
137 | return 0; |
6e522441 |
138 | case WM_COMMAND: |
139 | switch (LOWORD(wParam)) { |
140 | case IDOK: |
141 | if (*passphrase) |
32874aea |
142 | EndDialog(hwnd, 1); |
6e522441 |
143 | else |
32874aea |
144 | MessageBeep(0); |
6e522441 |
145 | return 0; |
146 | case IDCANCEL: |
32874aea |
147 | EndDialog(hwnd, 0); |
6e522441 |
148 | return 0; |
32874aea |
149 | case 102: /* edit box */ |
8e1feb27 |
150 | if ((HIWORD(wParam) == EN_CHANGE) && passphrase) { |
32874aea |
151 | GetDlgItemText(hwnd, 102, passphrase, |
152 | PASSPHRASE_MAXLEN - 1); |
153 | passphrase[PASSPHRASE_MAXLEN - 1] = '\0'; |
154 | } |
155 | return 0; |
6e522441 |
156 | } |
157 | return 0; |
158 | case WM_CLOSE: |
32874aea |
159 | EndDialog(hwnd, 0); |
6e522441 |
160 | return 0; |
161 | } |
162 | return 0; |
163 | } |
164 | |
165 | /* |
166 | * Prompt for a key file. Assumes the filename buffer is of size |
167 | * FILENAME_MAX. |
168 | */ |
169 | static int prompt_keyfile(HWND hwnd, char *dlgtitle, |
32874aea |
170 | char *filename, int save) |
171 | { |
6e522441 |
172 | OPENFILENAME of; |
173 | memset(&of, 0, sizeof(of)); |
174 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
175 | of.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
176 | #else |
177 | of.lStructSize = sizeof(of); |
178 | #endif |
179 | of.hwndOwner = hwnd; |
180 | of.lpstrFilter = "All Files\0*\0\0\0"; |
181 | of.lpstrCustomFilter = NULL; |
182 | of.nFilterIndex = 1; |
32874aea |
183 | of.lpstrFile = filename; |
184 | *filename = '\0'; |
6e522441 |
185 | of.nMaxFile = FILENAME_MAX; |
186 | of.lpstrFileTitle = NULL; |
187 | of.lpstrInitialDir = NULL; |
188 | of.lpstrTitle = dlgtitle; |
189 | of.Flags = 0; |
190 | if (save) |
32874aea |
191 | return GetSaveFileName(&of); |
6e522441 |
192 | else |
32874aea |
193 | return GetOpenFileName(&of); |
6e522441 |
194 | } |
195 | |
196 | /* |
197 | * This function is needed to link with the DES code. We need not |
198 | * have it do anything at all. |
199 | */ |
32874aea |
200 | void logevent(char *msg) |
201 | { |
6e522441 |
202 | } |
203 | |
204 | /* |
205 | * Dialog-box function for the Licence box. |
206 | */ |
32874aea |
207 | static int CALLBACK LicenceProc(HWND hwnd, UINT msg, |
208 | WPARAM wParam, LPARAM lParam) |
209 | { |
6e522441 |
210 | switch (msg) { |
211 | case WM_INITDIALOG: |
65a22376 |
212 | /* |
213 | * Centre the window. |
214 | */ |
215 | { /* centre the window */ |
216 | RECT rs, rd; |
217 | HWND hw; |
218 | |
219 | hw = GetDesktopWindow(); |
32874aea |
220 | if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) |
221 | MoveWindow(hwnd, |
222 | (rs.right + rs.left + rd.left - rd.right) / 2, |
223 | (rs.bottom + rs.top + rd.top - rd.bottom) / 2, |
224 | rd.right - rd.left, rd.bottom - rd.top, TRUE); |
65a22376 |
225 | } |
226 | |
6e522441 |
227 | return 1; |
228 | case WM_COMMAND: |
229 | switch (LOWORD(wParam)) { |
230 | case IDOK: |
32874aea |
231 | EndDialog(hwnd, 1); |
6e522441 |
232 | return 0; |
233 | } |
234 | return 0; |
235 | case WM_CLOSE: |
236 | EndDialog(hwnd, 1); |
237 | return 0; |
238 | } |
239 | return 0; |
240 | } |
241 | |
242 | /* |
243 | * Dialog-box function for the About box. |
244 | */ |
32874aea |
245 | static int CALLBACK AboutProc(HWND hwnd, UINT msg, |
246 | WPARAM wParam, LPARAM lParam) |
247 | { |
6e522441 |
248 | switch (msg) { |
249 | case WM_INITDIALOG: |
65a22376 |
250 | /* |
251 | * Centre the window. |
252 | */ |
253 | { /* centre the window */ |
254 | RECT rs, rd; |
255 | HWND hw; |
256 | |
257 | hw = GetDesktopWindow(); |
32874aea |
258 | if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) |
259 | MoveWindow(hwnd, |
260 | (rs.right + rs.left + rd.left - rd.right) / 2, |
261 | (rs.bottom + rs.top + rd.top - rd.bottom) / 2, |
262 | rd.right - rd.left, rd.bottom - rd.top, TRUE); |
65a22376 |
263 | } |
264 | |
32874aea |
265 | SetDlgItemText(hwnd, 100, ver); |
6e522441 |
266 | return 1; |
267 | case WM_COMMAND: |
268 | switch (LOWORD(wParam)) { |
269 | case IDOK: |
32874aea |
270 | EndDialog(hwnd, 1); |
6e522441 |
271 | return 0; |
272 | case 101: |
273 | EnableWindow(hwnd, 0); |
32874aea |
274 | DialogBox(hinst, MAKEINTRESOURCE(214), NULL, LicenceProc); |
6e522441 |
275 | EnableWindow(hwnd, 1); |
32874aea |
276 | SetActiveWindow(hwnd); |
6e522441 |
277 | return 0; |
278 | } |
279 | return 0; |
280 | case WM_CLOSE: |
32874aea |
281 | EndDialog(hwnd, 1); |
6e522441 |
282 | return 0; |
283 | } |
284 | return 0; |
285 | } |
286 | |
287 | /* |
288 | * Thread to generate a key. |
289 | */ |
290 | struct rsa_key_thread_params { |
32874aea |
291 | HWND progressbar; /* notify this with progress */ |
292 | HWND dialog; /* notify this on completion */ |
0c2986d0 |
293 | int keysize; /* bits in key */ |
6e522441 |
294 | struct RSAKey *key; |
6e522441 |
295 | }; |
32874aea |
296 | static DWORD WINAPI generate_rsa_key_thread(void *param) |
297 | { |
6e522441 |
298 | struct rsa_key_thread_params *params = |
32874aea |
299 | (struct rsa_key_thread_params *) param; |
6e522441 |
300 | struct progress prog; |
301 | prog.progbar = params->progressbar; |
302 | |
65a22376 |
303 | rsa_generate(params->key, params->keysize, progress_update, &prog); |
6e522441 |
304 | |
305 | PostMessage(params->dialog, WM_DONEKEY, 0, 0); |
306 | |
dcbde236 |
307 | sfree(params); |
6e522441 |
308 | return 0; |
309 | } |
310 | |
311 | struct MainDlgState { |
312 | int collecting_entropy; |
313 | int generation_thread_exists; |
314 | int key_exists; |
315 | int entropy_got, entropy_required, entropy_size; |
0c2986d0 |
316 | int keysize; |
65a22376 |
317 | int ssh2; |
318 | char **commentptr; /* points to key.comment or ssh2key.comment */ |
319 | struct ssh2_userkey ssh2key; |
6e522441 |
320 | unsigned *entropy; |
321 | struct RSAKey key; |
6e522441 |
322 | }; |
323 | |
32874aea |
324 | static void hidemany(HWND hwnd, const int *ids, int hideit) |
325 | { |
6e522441 |
326 | while (*ids) { |
32874aea |
327 | ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW)); |
6e522441 |
328 | } |
329 | } |
330 | |
32874aea |
331 | static void setupbigedit1(HWND hwnd, int id, struct RSAKey *key) |
332 | { |
6e522441 |
333 | char *buffer; |
334 | char *dec1, *dec2; |
335 | |
336 | dec1 = bignum_decimal(key->exponent); |
337 | dec2 = bignum_decimal(key->modulus); |
32874aea |
338 | buffer = smalloc(strlen(dec1) + strlen(dec2) + |
339 | strlen(key->comment) + 30); |
6e522441 |
340 | sprintf(buffer, "%d %s %s %s", |
32874aea |
341 | bignum_bitcount(key->modulus), dec1, dec2, key->comment); |
6e522441 |
342 | SetDlgItemText(hwnd, id, buffer); |
dcbde236 |
343 | sfree(dec1); |
344 | sfree(dec2); |
345 | sfree(buffer); |
6e522441 |
346 | } |
347 | |
32874aea |
348 | static void setupbigedit2(HWND hwnd, int id, struct ssh2_userkey *key) |
349 | { |
65a22376 |
350 | unsigned char *pub_blob; |
351 | char *buffer, *p; |
44fb739e |
352 | int pub_len; |
65a22376 |
353 | int i; |
354 | |
355 | pub_blob = key->alg->public_blob(key->data, &pub_len); |
32874aea |
356 | buffer = smalloc(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) + |
65a22376 |
357 | strlen(key->comment) + 3); |
358 | strcpy(buffer, key->alg->name); |
359 | p = buffer + strlen(buffer); |
360 | *p++ = ' '; |
361 | i = 0; |
362 | while (i < pub_len) { |
32874aea |
363 | int n = (pub_len - i < 3 ? pub_len - i : 3); |
364 | base64_encode_atom(pub_blob + i, n, p); |
65a22376 |
365 | i += n; |
366 | p += 4; |
367 | } |
368 | *p++ = ' '; |
369 | strcpy(p, key->comment); |
370 | SetDlgItemText(hwnd, id, buffer); |
371 | sfree(pub_blob); |
32874aea |
372 | sfree(buffer); |
65a22376 |
373 | } |
374 | |
6e522441 |
375 | /* |
376 | * Dialog-box function for the main PuTTYgen dialog box. |
377 | */ |
32874aea |
378 | static int CALLBACK MainDlgProc(HWND hwnd, UINT msg, |
379 | WPARAM wParam, LPARAM lParam) |
380 | { |
6e522441 |
381 | enum { |
32874aea |
382 | controlidstart = 100, |
383 | IDC_TITLE, |
384 | IDC_BOX_KEY, |
385 | IDC_NOKEY, |
386 | IDC_GENERATING, |
387 | IDC_PROGRESS, |
388 | IDC_PKSTATIC, IDC_KEYDISPLAY, |
389 | IDC_FPSTATIC, IDC_FINGERPRINT, |
390 | IDC_COMMENTSTATIC, IDC_COMMENTEDIT, |
391 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
392 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, |
393 | IDC_BOX_ACTIONS, |
394 | IDC_GENSTATIC, IDC_GENERATE, |
395 | IDC_LOADSTATIC, IDC_LOAD, |
396 | IDC_SAVESTATIC, IDC_SAVE, |
397 | IDC_BOX_PARAMS, |
65a22376 |
398 | IDC_TYPESTATIC, IDC_KEYSSH1, IDC_KEYSSH2RSA, |
32874aea |
399 | IDC_BITSSTATIC, IDC_BITS, |
400 | IDC_ABOUT, |
6e522441 |
401 | }; |
402 | static const int nokey_ids[] = { IDC_NOKEY, 0 }; |
32874aea |
403 | static const int generating_ids[] = |
404 | { IDC_GENERATING, IDC_PROGRESS, 0 }; |
6e522441 |
405 | static const int gotkey_ids[] = { |
32874aea |
406 | IDC_PKSTATIC, IDC_KEYDISPLAY, |
407 | IDC_FPSTATIC, IDC_FINGERPRINT, |
408 | IDC_COMMENTSTATIC, IDC_COMMENTEDIT, |
409 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
410 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 |
411 | }; |
6e522441 |
412 | static const char generating_msg[] = |
32874aea |
413 | "Please wait while a key is generated..."; |
6e522441 |
414 | static const char entropy_msg[] = |
32874aea |
415 | "Please generate some randomness by moving the mouse over the blank area."; |
6e522441 |
416 | struct MainDlgState *state; |
417 | |
418 | switch (msg) { |
419 | case WM_INITDIALOG: |
65a22376 |
420 | /* |
421 | * Centre the window. |
422 | */ |
423 | { /* centre the window */ |
424 | RECT rs, rd; |
425 | HWND hw; |
426 | |
427 | hw = GetDesktopWindow(); |
32874aea |
428 | if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) |
429 | MoveWindow(hwnd, |
430 | (rs.right + rs.left + rd.left - rd.right) / 2, |
431 | (rs.bottom + rs.top + rd.top - rd.bottom) / 2, |
432 | rd.right - rd.left, rd.bottom - rd.top, TRUE); |
65a22376 |
433 | } |
434 | |
32874aea |
435 | state = smalloc(sizeof(*state)); |
436 | state->generation_thread_exists = FALSE; |
437 | state->collecting_entropy = FALSE; |
438 | state->entropy = NULL; |
439 | state->key_exists = FALSE; |
440 | SetWindowLong(hwnd, GWL_USERDATA, (LONG) state); |
441 | { |
442 | struct ctlpos cp, cp2; |
6e522441 |
443 | |
a5470c60 |
444 | /* Accelerators used: acglops */ |
445 | |
32874aea |
446 | ctlposinit(&cp, hwnd, 10, 10, 10); |
447 | bartitle(&cp, "Public and private key generation for PuTTY", |
448 | IDC_TITLE); |
449 | beginbox(&cp, "Key", IDC_BOX_KEY); |
450 | cp2 = cp; |
451 | statictext(&cp2, "No key.", IDC_NOKEY); |
452 | cp2 = cp; |
453 | statictext(&cp2, "", IDC_GENERATING); |
454 | progressbar(&cp2, IDC_PROGRESS); |
455 | bigeditctrl(&cp, |
456 | "&Public key for pasting into authorized_keys file:", |
457 | IDC_PKSTATIC, IDC_KEYDISPLAY, 7); |
458 | SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0); |
459 | staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC, |
460 | IDC_FINGERPRINT, 75); |
461 | SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, |
462 | 0); |
463 | staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC, |
464 | IDC_COMMENTEDIT, 75); |
465 | staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC, |
466 | IDC_PASSPHRASE1EDIT, 75); |
467 | staticpassedit(&cp, "C&onfirm passphrase:", |
468 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 75); |
469 | endbox(&cp); |
470 | beginbox(&cp, "Actions", IDC_BOX_ACTIONS); |
471 | staticbtn(&cp, "Generate a public/private key pair", |
472 | IDC_GENSTATIC, "&Generate", IDC_GENERATE); |
473 | staticbtn(&cp, "Load an existing private key file", |
474 | IDC_LOADSTATIC, "&Load", IDC_LOAD); |
475 | staticbtn(&cp, "Save the generated key to a new file", |
476 | IDC_SAVESTATIC, "&Save", IDC_SAVE); |
477 | endbox(&cp); |
478 | beginbox(&cp, "Parameters", IDC_BOX_PARAMS); |
65a22376 |
479 | radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 2, |
32874aea |
480 | "SSH&1 (RSA)", IDC_KEYSSH1, |
481 | "SSH2 &RSA", IDC_KEYSSH2RSA, NULL); |
482 | staticedit(&cp, "Number of &bits in a generated key:", |
0c2986d0 |
483 | IDC_BITSSTATIC, IDC_BITS, 20); |
32874aea |
484 | endbox(&cp); |
485 | } |
65a22376 |
486 | CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2RSA, IDC_KEYSSH1); |
0c2986d0 |
487 | SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE); |
488 | |
32874aea |
489 | /* |
490 | * Initially, hide the progress bar and the key display, |
491 | * and show the no-key display. Also disable the Save |
492 | * button, because with no key we obviously can't save |
493 | * anything. |
494 | */ |
495 | hidemany(hwnd, nokey_ids, FALSE); |
496 | hidemany(hwnd, generating_ids, TRUE); |
497 | hidemany(hwnd, gotkey_ids, TRUE); |
498 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0); |
6e522441 |
499 | |
500 | return 1; |
501 | case WM_MOUSEMOVE: |
32874aea |
502 | state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
503 | if (state->collecting_entropy && |
504 | state->entropy && state->entropy_got < state->entropy_required) { |
505 | state->entropy[state->entropy_got++] = lParam; |
506 | state->entropy[state->entropy_got++] = GetMessageTime(); |
507 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, |
508 | state->entropy_got, 0); |
509 | if (state->entropy_got >= state->entropy_required) { |
510 | struct rsa_key_thread_params *params; |
511 | DWORD threadid; |
512 | |
513 | /* |
514 | * Seed the entropy pool |
515 | */ |
516 | random_add_heavynoise(state->entropy, state->entropy_size); |
517 | memset(state->entropy, 0, state->entropy_size); |
518 | sfree(state->entropy); |
519 | state->collecting_entropy = FALSE; |
520 | |
521 | SetDlgItemText(hwnd, IDC_GENERATING, generating_msg); |
522 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, |
523 | MAKELPARAM(0, PROGRESSRANGE)); |
524 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
525 | |
526 | params = smalloc(sizeof(*params)); |
527 | params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS); |
528 | params->dialog = hwnd; |
0c2986d0 |
529 | params->keysize = state->keysize; |
32874aea |
530 | params->key = &state->key; |
531 | |
532 | if (!CreateThread(NULL, 0, generate_rsa_key_thread, |
533 | params, 0, &threadid)) { |
534 | MessageBox(hwnd, "Out of thread resources", |
535 | "Key generation error", |
536 | MB_OK | MB_ICONERROR); |
537 | sfree(params); |
538 | } else { |
539 | state->generation_thread_exists = TRUE; |
540 | } |
541 | } |
542 | } |
543 | break; |
6e522441 |
544 | case WM_COMMAND: |
545 | switch (LOWORD(wParam)) { |
546 | case IDC_COMMENTEDIT: |
547 | if (HIWORD(wParam) == EN_CHANGE) { |
32874aea |
548 | state = (struct MainDlgState *) |
549 | GetWindowLong(hwnd, GWL_USERDATA); |
550 | if (state->key_exists) { |
551 | HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT); |
552 | int len = GetWindowTextLength(editctl); |
553 | if (*state->commentptr) |
554 | sfree(*state->commentptr); |
555 | *state->commentptr = smalloc(len + 1); |
556 | GetWindowText(editctl, *state->commentptr, len + 1); |
2e8324fc |
557 | if (state->ssh2) { |
32874aea |
558 | setupbigedit2(hwnd, IDC_KEYDISPLAY, |
559 | &state->ssh2key); |
2e8324fc |
560 | } else { |
561 | setupbigedit1(hwnd, IDC_KEYDISPLAY, &state->key); |
562 | } |
563 | } |
564 | } |
6e522441 |
565 | break; |
566 | case IDC_ABOUT: |
567 | EnableWindow(hwnd, 0); |
32874aea |
568 | DialogBox(hinst, MAKEINTRESOURCE(213), NULL, AboutProc); |
6e522441 |
569 | EnableWindow(hwnd, 1); |
32874aea |
570 | SetActiveWindow(hwnd); |
6e522441 |
571 | return 0; |
32874aea |
572 | case IDC_GENERATE: |
573 | state = |
574 | (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
575 | if (!state->generation_thread_exists) { |
576 | BOOL ok; |
577 | state->keysize = GetDlgItemInt(hwnd, IDC_BITS, &ok, FALSE); |
578 | if (!ok) |
579 | state->keysize = DEFAULT_KEYSIZE; |
65a22376 |
580 | /* If we ever introduce a new key type, check it here! */ |
581 | state->ssh2 = !IsDlgButtonChecked(hwnd, IDC_KEYSSH1); |
32874aea |
582 | if (state->keysize < 256) { |
583 | int ret = MessageBox(hwnd, |
584 | "PuTTYgen will not generate a key" |
585 | " smaller than 256 bits.\n" |
586 | "Key length reset to 256. Continue?", |
587 | "PuTTYgen Warning", |
588 | MB_ICONWARNING | MB_OKCANCEL); |
589 | if (ret != IDOK) |
590 | break; |
591 | state->keysize = 256; |
592 | SetDlgItemInt(hwnd, IDC_BITS, 256, FALSE); |
593 | } |
594 | hidemany(hwnd, nokey_ids, TRUE); |
595 | hidemany(hwnd, generating_ids, FALSE); |
596 | hidemany(hwnd, gotkey_ids, TRUE); |
597 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0); |
598 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0); |
599 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0); |
600 | state->key_exists = FALSE; |
601 | SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg); |
602 | state->collecting_entropy = TRUE; |
603 | |
604 | /* |
605 | * My brief statistical tests on mouse movements |
606 | * suggest that there are about 2.5 bits of |
607 | * randomness in the x position, 2.5 in the y |
608 | * position, and 1.7 in the message time, making |
609 | * 5.7 bits of unpredictability per mouse movement. |
610 | * However, other people have told me it's far less |
611 | * than that, so I'm going to be stupidly cautious |
612 | * and knock that down to a nice round 2. With this |
613 | * method, we require two words per mouse movement, |
614 | * so with 2 bits per mouse movement we expect 2 |
615 | * bits every 2 words. |
616 | */ |
617 | state->entropy_required = (state->keysize / 2) * 2; |
618 | state->entropy_got = 0; |
619 | state->entropy_size = (state->entropy_required * |
620 | sizeof(*state->entropy)); |
621 | state->entropy = smalloc(state->entropy_size); |
622 | |
623 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, |
624 | MAKELPARAM(0, state->entropy_required)); |
625 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
626 | } |
627 | break; |
628 | case IDC_SAVE: |
629 | state = |
630 | (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
631 | if (state->key_exists) { |
632 | char filename[FILENAME_MAX]; |
633 | char passphrase[PASSPHRASE_MAXLEN]; |
634 | char passphrase2[PASSPHRASE_MAXLEN]; |
635 | GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, |
636 | passphrase, sizeof(passphrase)); |
637 | GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, |
638 | passphrase2, sizeof(passphrase2)); |
a5470c60 |
639 | if (strcmp(passphrase, passphrase2)) { |
32874aea |
640 | MessageBox(hwnd, |
a5470c60 |
641 | "The two passphrases given do not match.", |
32874aea |
642 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
a5470c60 |
643 | break; |
644 | } |
32874aea |
645 | if (!*passphrase) { |
646 | int ret; |
647 | ret = MessageBox(hwnd, |
648 | "Are you sure you want to save this key\n" |
649 | "without a passphrase to protect it?", |
650 | "PuTTYgen Warning", |
651 | MB_YESNO | MB_ICONWARNING); |
652 | if (ret != IDYES) |
653 | break; |
654 | } |
655 | if (prompt_keyfile(hwnd, "Save private key as:", |
656 | filename, 1)) { |
bdea0743 |
657 | int ret; |
18790478 |
658 | FILE *fp = fopen(filename, "r"); |
659 | if (fp) { |
32874aea |
660 | char buffer[FILENAME_MAX + 80]; |
18790478 |
661 | fclose(fp); |
662 | sprintf(buffer, "Overwrite existing file\n%.*s?", |
663 | FILENAME_MAX, filename); |
664 | ret = MessageBox(hwnd, buffer, "PuTTYgen Warning", |
665 | MB_YESNO | MB_ICONWARNING); |
666 | if (ret != IDYES) |
667 | break; |
668 | } |
65a22376 |
669 | if (state->ssh2) { |
670 | ret = ssh2_save_userkey(filename, &state->ssh2key, |
32874aea |
671 | *passphrase ? passphrase : |
672 | NULL); |
65a22376 |
673 | } else { |
674 | ret = saversakey(filename, &state->key, |
675 | *passphrase ? passphrase : NULL); |
676 | } |
bdea0743 |
677 | if (ret <= 0) { |
678 | MessageBox(hwnd, "Unable to save key file", |
32874aea |
679 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
bdea0743 |
680 | } |
32874aea |
681 | } |
682 | } |
683 | break; |
684 | case IDC_LOAD: |
685 | state = |
686 | (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
687 | if (!state->generation_thread_exists) { |
688 | char filename[FILENAME_MAX]; |
689 | if (prompt_keyfile(hwnd, "Load private key:", filename, 0)) { |
690 | char passphrase[PASSPHRASE_MAXLEN]; |
691 | int needs_pass; |
65a22376 |
692 | int ver; |
32874aea |
693 | int ret; |
694 | char *comment; |
695 | struct PassphraseProcStruct pps; |
696 | struct RSAKey newkey1; |
2d466ffd |
697 | struct ssh2_userkey *newkey2 = NULL; |
65a22376 |
698 | |
699 | ver = keyfile_version(filename); |
700 | if (ver == 0) { |
32874aea |
701 | MessageBox(NULL, "Couldn't load private key.", |
702 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
65a22376 |
703 | break; |
704 | } |
6e522441 |
705 | |
65a22376 |
706 | comment = NULL; |
707 | if (ver == 1) |
708 | needs_pass = rsakey_encrypted(filename, &comment); |
709 | else |
32874aea |
710 | needs_pass = |
711 | ssh2_userkey_encrypted(filename, &comment); |
712 | pps.passphrase = passphrase; |
713 | pps.comment = comment; |
714 | do { |
715 | if (needs_pass) { |
716 | int dlgret; |
717 | dlgret = DialogBoxParam(hinst, |
718 | MAKEINTRESOURCE(210), |
719 | NULL, PassphraseProc, |
720 | (LPARAM) & pps); |
721 | if (!dlgret) { |
722 | ret = -2; |
723 | break; |
724 | } |
725 | } else |
726 | *passphrase = '\0'; |
65a22376 |
727 | if (ver == 1) |
32874aea |
728 | ret = |
729 | loadrsakey(filename, &newkey1, passphrase); |
65a22376 |
730 | else { |
32874aea |
731 | newkey2 = |
732 | ssh2_load_userkey(filename, passphrase); |
65a22376 |
733 | if (newkey2 == SSH2_WRONG_PASSPHRASE) |
734 | ret = -1; |
735 | else if (!newkey2) |
736 | ret = 0; |
737 | else |
738 | ret = 1; |
739 | } |
32874aea |
740 | } while (ret == -1); |
741 | if (comment) |
742 | sfree(comment); |
743 | if (ret == 0) { |
744 | MessageBox(NULL, "Couldn't load private key.", |
745 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
746 | } else if (ret == 1) { |
747 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); |
748 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); |
749 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); |
750 | /* |
751 | * Now update the key controls with all the |
752 | * key data. |
753 | */ |
754 | { |
755 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, |
756 | passphrase); |
757 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, |
758 | passphrase); |
65a22376 |
759 | if (ver == 1) { |
760 | char buf[128]; |
761 | char *savecomment; |
762 | |
763 | state->ssh2 = FALSE; |
764 | state->commentptr = &state->key.comment; |
765 | state->key = newkey1; |
766 | |
767 | /* |
768 | * Set the key fingerprint. |
769 | */ |
770 | savecomment = state->key.comment; |
32874aea |
771 | state->key.comment = NULL; |
772 | rsa_fingerprint(buf, sizeof(buf), |
773 | &state->key); |
774 | state->key.comment = savecomment; |
65a22376 |
775 | |
776 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
777 | /* |
778 | * Construct a decimal representation |
779 | * of the key, for pasting into |
780 | * .ssh/authorized_keys on a Unix box. |
781 | */ |
32874aea |
782 | setupbigedit1(hwnd, IDC_KEYDISPLAY, |
783 | &state->key); |
65a22376 |
784 | } else { |
785 | char *fp; |
786 | char *savecomment; |
787 | |
788 | state->ssh2 = TRUE; |
32874aea |
789 | state->commentptr = |
790 | &state->ssh2key.comment; |
791 | state->ssh2key = *newkey2; /* structure copy */ |
65a22376 |
792 | sfree(newkey2); |
793 | |
794 | savecomment = state->ssh2key.comment; |
32874aea |
795 | state->ssh2key.comment = NULL; |
796 | fp = |
797 | state->ssh2key.alg-> |
798 | fingerprint(state->ssh2key.data); |
799 | state->ssh2key.comment = savecomment; |
65a22376 |
800 | |
801 | SetDlgItemText(hwnd, IDC_FINGERPRINT, fp); |
802 | sfree(fp); |
803 | |
32874aea |
804 | setupbigedit2(hwnd, IDC_KEYDISPLAY, |
805 | &state->ssh2key); |
65a22376 |
806 | } |
32874aea |
807 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, |
808 | *state->commentptr); |
809 | } |
810 | /* |
811 | * Finally, hide the progress bar and show |
812 | * the key data. |
813 | */ |
814 | hidemany(hwnd, nokey_ids, TRUE); |
815 | hidemany(hwnd, generating_ids, TRUE); |
816 | hidemany(hwnd, gotkey_ids, FALSE); |
817 | state->key_exists = TRUE; |
818 | } |
819 | } |
820 | } |
821 | break; |
6e522441 |
822 | } |
823 | return 0; |
824 | case WM_DONEKEY: |
32874aea |
825 | state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
826 | state->generation_thread_exists = FALSE; |
827 | state->key_exists = TRUE; |
828 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, |
829 | 0); |
830 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); |
831 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); |
832 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); |
3b4c5899 |
833 | if (state->ssh2) { |
834 | state->ssh2key.data = &state->key; |
835 | state->ssh2key.alg = &ssh_rsa; |
65a22376 |
836 | state->commentptr = &state->ssh2key.comment; |
3b4c5899 |
837 | } else { |
65a22376 |
838 | state->commentptr = &state->key.comment; |
3b4c5899 |
839 | } |
32874aea |
840 | /* |
841 | * Invent a comment for the key. We'll do this by including |
842 | * the date in it. This will be so horrifyingly ugly that |
843 | * the user will immediately want to change it, which is |
844 | * what we want :-) |
845 | */ |
846 | *state->commentptr = smalloc(30); |
847 | { |
848 | time_t t; |
849 | struct tm *tm; |
850 | time(&t); |
851 | tm = localtime(&t); |
852 | strftime(*state->commentptr, 30, "rsa-key-%Y%m%d", tm); |
853 | } |
854 | |
855 | /* |
856 | * Now update the key controls with all the key data. |
857 | */ |
858 | { |
65a22376 |
859 | char *savecomment; |
32874aea |
860 | /* |
861 | * Blank passphrase, initially. This isn't dangerous, |
862 | * because we will warn (Are You Sure?) before allowing |
863 | * the user to save an unprotected private key. |
864 | */ |
865 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, ""); |
866 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, ""); |
867 | /* |
868 | * Set the comment. |
869 | */ |
870 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, *state->commentptr); |
871 | /* |
872 | * Set the key fingerprint. |
873 | */ |
65a22376 |
874 | savecomment = *state->commentptr; |
875 | *state->commentptr = NULL; |
32874aea |
876 | if (state->ssh2) { |
65a22376 |
877 | char *fp; |
32874aea |
878 | fp = state->ssh2key.alg->fingerprint(state->ssh2key.data); |
65a22376 |
879 | SetDlgItemText(hwnd, IDC_FINGERPRINT, fp); |
880 | sfree(fp); |
881 | } else { |
882 | char buf[128]; |
32874aea |
883 | rsa_fingerprint(buf, sizeof(buf), &state->key); |
65a22376 |
884 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
32874aea |
885 | } |
65a22376 |
886 | *state->commentptr = savecomment; |
32874aea |
887 | /* |
888 | * Construct a decimal representation of the key, for |
889 | * pasting into .ssh/authorized_keys on a Unix box. |
890 | */ |
65a22376 |
891 | if (state->ssh2) { |
892 | setupbigedit2(hwnd, IDC_KEYDISPLAY, &state->ssh2key); |
893 | } else { |
894 | setupbigedit1(hwnd, IDC_KEYDISPLAY, &state->key); |
895 | } |
32874aea |
896 | } |
897 | /* |
898 | * Finally, hide the progress bar and show the key data. |
899 | */ |
900 | hidemany(hwnd, nokey_ids, TRUE); |
901 | hidemany(hwnd, generating_ids, TRUE); |
902 | hidemany(hwnd, gotkey_ids, FALSE); |
903 | break; |
6e522441 |
904 | case WM_CLOSE: |
32874aea |
905 | state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
906 | sfree(state); |
907 | EndDialog(hwnd, 1); |
6e522441 |
908 | return 0; |
909 | } |
910 | return 0; |
911 | } |
912 | |
32874aea |
913 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) |
914 | { |
8b8cf871 |
915 | InitCommonControls(); |
6e522441 |
916 | hinst = inst; |
917 | random_init(); |
32874aea |
918 | return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, |
919 | MainDlgProc) != IDOK; |
6e522441 |
920 | } |