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