puttygen.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  1. /*
  2. * PuTTY key generation front end (Windows).
  3. */
  4. #include <time.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include "putty.h"
  9. #include "ssh.h"
  10. #include "sshkeygen.h"
  11. #include "licence.h"
  12. #include "security-api.h"
  13. #include "puttygen-rc.h"
  14. #include <commctrl.h>
  15. #ifdef MSVC4
  16. #define ICON_BIG 1
  17. #endif
  18. #define WM_DONEKEY (WM_APP + 1)
  19. #define DEFAULT_KEY_BITS 2048
  20. #define DEFAULT_ECCURVE_INDEX 0
  21. #define DEFAULT_EDCURVE_INDEX 0
  22. static char *cmdline_keyfile = NULL;
  23. static ptrlen cmdline_demo_keystr;
  24. static const char *demo_screenshot_filename = NULL;
  25. /*
  26. * Print a modal (Really Bad) message box and perform a fatal exit.
  27. */
  28. void modalfatalbox(const char *fmt, ...)
  29. {
  30. va_list ap;
  31. char *stuff;
  32. va_start(ap, fmt);
  33. stuff = dupvprintf(fmt, ap);
  34. va_end(ap);
  35. MessageBox(NULL, stuff, "PuTTYgen Fatal Error",
  36. MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
  37. sfree(stuff);
  38. exit(1);
  39. }
  40. /*
  41. * Print a non-fatal message box and do not exit.
  42. */
  43. void nonfatal(const char *fmt, ...)
  44. {
  45. va_list ap;
  46. char *stuff;
  47. va_start(ap, fmt);
  48. stuff = dupvprintf(fmt, ap);
  49. va_end(ap);
  50. MessageBox(NULL, stuff, "PuTTYgen Error",
  51. MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
  52. sfree(stuff);
  53. }
  54. /* ----------------------------------------------------------------------
  55. * ProgressReceiver implementation.
  56. */
  57. #define PROGRESSRANGE 65535
  58. struct progressphase {
  59. double startpoint, total;
  60. /* For exponential phases */
  61. double exp_probability, exp_current_value;
  62. };
  63. struct progress {
  64. size_t nphases, phasessize;
  65. struct progressphase *phases, *currphase;
  66. double scale;
  67. HWND progbar;
  68. ProgressReceiver rec;
  69. };
  70. static ProgressPhase win_progress_add_linear(
  71. ProgressReceiver *prog, double overall_cost) {
  72. struct progress *p = container_of(prog, struct progress, rec);
  73. sgrowarray(p->phases, p->phasessize, p->nphases);
  74. int phase = p->nphases++;
  75. p->phases[phase].total = overall_cost;
  76. ProgressPhase ph = { .n = phase };
  77. return ph;
  78. }
  79. static ProgressPhase win_progress_add_probabilistic(
  80. ProgressReceiver *prog, double cost_per_attempt, double probability) {
  81. struct progress *p = container_of(prog, struct progress, rec);
  82. sgrowarray(p->phases, p->phasessize, p->nphases);
  83. int phase = p->nphases++;
  84. p->phases[phase].exp_probability = 1.0 - probability;
  85. p->phases[phase].exp_current_value = 1.0;
  86. /* Expected number of attempts = 1 / probability of attempt succeeding */
  87. p->phases[phase].total = cost_per_attempt / probability;
  88. ProgressPhase ph = { .n = phase };
  89. return ph;
  90. }
  91. static void win_progress_ready(ProgressReceiver *prog)
  92. {
  93. struct progress *p = container_of(prog, struct progress, rec);
  94. double total = 0;
  95. for (int i = 0; i < p->nphases; i++) {
  96. p->phases[i].startpoint = total;
  97. total += p->phases[i].total;
  98. }
  99. p->scale = PROGRESSRANGE / total;
  100. SendMessage(p->progbar, PBM_SETRANGE, 0, MAKELPARAM(0, PROGRESSRANGE));
  101. }
  102. static void win_progress_start_phase(ProgressReceiver *prog,
  103. ProgressPhase phase)
  104. {
  105. struct progress *p = container_of(prog, struct progress, rec);
  106. assert(phase.n < p->nphases);
  107. p->currphase = &p->phases[phase.n];
  108. }
  109. static void win_progress_update(struct progress *p, double phasepos)
  110. {
  111. double position = (p->currphase->startpoint +
  112. p->currphase->total * phasepos);
  113. position *= p->scale;
  114. if (position < 0)
  115. position = 0;
  116. if (position > PROGRESSRANGE)
  117. position = PROGRESSRANGE;
  118. SendMessage(p->progbar, PBM_SETPOS, (WPARAM)position, 0);
  119. }
  120. static void win_progress_report(ProgressReceiver *prog, double progress)
  121. {
  122. struct progress *p = container_of(prog, struct progress, rec);
  123. win_progress_update(p, progress);
  124. }
  125. static void win_progress_report_attempt(ProgressReceiver *prog)
  126. {
  127. struct progress *p = container_of(prog, struct progress, rec);
  128. p->currphase->exp_current_value *= p->currphase->exp_probability;
  129. win_progress_update(p, 1.0 - p->currphase->exp_current_value);
  130. }
  131. static void win_progress_report_phase_complete(ProgressReceiver *prog)
  132. {
  133. struct progress *p = container_of(prog, struct progress, rec);
  134. win_progress_update(p, 1.0);
  135. }
  136. static const ProgressReceiverVtable win_progress_vt = {
  137. .add_linear = win_progress_add_linear,
  138. .add_probabilistic = win_progress_add_probabilistic,
  139. .ready = win_progress_ready,
  140. .start_phase = win_progress_start_phase,
  141. .report = win_progress_report,
  142. .report_attempt = win_progress_report_attempt,
  143. .report_phase_complete = win_progress_report_phase_complete,
  144. };
  145. static void win_progress_initialise(struct progress *p)
  146. {
  147. p->nphases = p->phasessize = 0;
  148. p->phases = p->currphase = NULL;
  149. p->rec.vt = &win_progress_vt;
  150. }
  151. static void win_progress_cleanup(struct progress *p)
  152. {
  153. sfree(p->phases);
  154. }
  155. struct PassphraseProcStruct {
  156. char **passphrase;
  157. char *comment;
  158. };
  159. /*
  160. * Dialog-box function for the passphrase box.
  161. */
  162. static INT_PTR CALLBACK PassphraseProc(HWND hwnd, UINT msg,
  163. WPARAM wParam, LPARAM lParam)
  164. {
  165. static char **passphrase = NULL;
  166. struct PassphraseProcStruct *p;
  167. switch (msg) {
  168. case WM_INITDIALOG:
  169. SetForegroundWindow(hwnd);
  170. SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
  171. SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  172. /*
  173. * Centre the window.
  174. */
  175. { /* centre the window */
  176. RECT rs, rd;
  177. HWND hw;
  178. hw = GetDesktopWindow();
  179. if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
  180. MoveWindow(hwnd,
  181. (rs.right + rs.left + rd.left - rd.right) / 2,
  182. (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
  183. rd.right - rd.left, rd.bottom - rd.top, true);
  184. }
  185. p = (struct PassphraseProcStruct *) lParam;
  186. passphrase = p->passphrase;
  187. if (p->comment)
  188. SetDlgItemText(hwnd, 101, p->comment);
  189. burnstr(*passphrase);
  190. *passphrase = dupstr("");
  191. SetDlgItemText(hwnd, 102, *passphrase);
  192. return 0;
  193. case WM_COMMAND:
  194. switch (LOWORD(wParam)) {
  195. case IDOK:
  196. if (*passphrase)
  197. EndDialog(hwnd, 1);
  198. else
  199. MessageBeep(0);
  200. return 0;
  201. case IDCANCEL:
  202. EndDialog(hwnd, 0);
  203. return 0;
  204. case 102: /* edit box */
  205. if ((HIWORD(wParam) == EN_CHANGE) && passphrase) {
  206. burnstr(*passphrase);
  207. *passphrase = GetDlgItemText_alloc(hwnd, 102);
  208. }
  209. return 0;
  210. }
  211. return 0;
  212. case WM_CLOSE:
  213. EndDialog(hwnd, 0);
  214. return 0;
  215. }
  216. return 0;
  217. }
  218. static void try_get_dlg_item_uint32(HWND hwnd, int id, uint32_t *out)
  219. {
  220. char buf[128];
  221. if (!GetDlgItemText(hwnd, id, buf, sizeof(buf)))
  222. return;
  223. if (!*buf)
  224. return;
  225. char *end;
  226. unsigned long val = strtoul(buf, &end, 10);
  227. if (*end)
  228. return;
  229. if ((val >> 16) >> 16)
  230. return;
  231. *out = val;
  232. }
  233. static ppk_save_parameters save_params;
  234. struct PPKParams {
  235. ppk_save_parameters params;
  236. uint32_t time_passes, time_ms;
  237. };
  238. /*
  239. * Dialog-box function for the passphrase box.
  240. */
  241. static INT_PTR CALLBACK PPKParamsProc(HWND hwnd, UINT msg,
  242. WPARAM wParam, LPARAM lParam)
  243. {
  244. struct PPKParams *pp;
  245. char *buf;
  246. if (msg == WM_INITDIALOG) {
  247. pp = (struct PPKParams *)lParam;
  248. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pp);
  249. } else {
  250. pp = (struct PPKParams *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  251. }
  252. switch (msg) {
  253. case WM_INITDIALOG:
  254. SetForegroundWindow(hwnd);
  255. SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
  256. SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  257. if (has_help())
  258. SetWindowLongPtr(hwnd, GWL_EXSTYLE,
  259. GetWindowLongPtr(hwnd, GWL_EXSTYLE) |
  260. WS_EX_CONTEXTHELP);
  261. /*
  262. * Centre the window.
  263. */
  264. { /* centre the window */
  265. RECT rs, rd;
  266. HWND hw;
  267. hw = GetDesktopWindow();
  268. if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
  269. MoveWindow(hwnd,
  270. (rs.right + rs.left + rd.left - rd.right) / 2,
  271. (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
  272. rd.right - rd.left, rd.bottom - rd.top, true);
  273. }
  274. CheckRadioButton(hwnd, IDC_PPKVER_2, IDC_PPKVER_3,
  275. IDC_PPKVER_2 + (pp->params.fmt_version - 2));
  276. CheckRadioButton(
  277. hwnd, IDC_KDF_ARGON2ID, IDC_KDF_ARGON2D,
  278. (pp->params.argon2_flavour == Argon2id ? IDC_KDF_ARGON2ID :
  279. pp->params.argon2_flavour == Argon2i ? IDC_KDF_ARGON2I :
  280. /* pp->params.argon2_flavour == Argon2d ? */ IDC_KDF_ARGON2D));
  281. buf = dupprintf("%"PRIu32, pp->params.argon2_mem);
  282. SetDlgItemText(hwnd, IDC_ARGON2_MEM, buf);
  283. sfree(buf);
  284. if (pp->params.argon2_passes_auto) {
  285. CheckRadioButton(hwnd, IDC_PPK_AUTO_YES, IDC_PPK_AUTO_NO,
  286. IDC_PPK_AUTO_YES);
  287. buf = dupprintf("%"PRIu32, pp->time_ms);
  288. SetDlgItemText(hwnd, IDC_ARGON2_TIME, buf);
  289. sfree(buf);
  290. } else {
  291. CheckRadioButton(hwnd, IDC_PPK_AUTO_YES, IDC_PPK_AUTO_NO,
  292. IDC_PPK_AUTO_NO);
  293. buf = dupprintf("%"PRIu32, pp->time_passes);
  294. SetDlgItemText(hwnd, IDC_ARGON2_TIME, buf);
  295. sfree(buf);
  296. }
  297. buf = dupprintf("%"PRIu32, pp->params.argon2_parallelism);
  298. SetDlgItemText(hwnd, IDC_ARGON2_PARALLEL, buf);
  299. sfree(buf);
  300. return 0;
  301. case WM_COMMAND:
  302. switch (LOWORD(wParam)) {
  303. case IDOK:
  304. EndDialog(hwnd, 1);
  305. return 0;
  306. case IDCANCEL:
  307. EndDialog(hwnd, 0);
  308. return 0;
  309. case IDC_PPKVER_2:
  310. pp->params.fmt_version = 2;
  311. return 0;
  312. case IDC_PPKVER_3:
  313. pp->params.fmt_version = 3;
  314. return 0;
  315. case IDC_KDF_ARGON2ID:
  316. pp->params.argon2_flavour = Argon2id;
  317. return 0;
  318. case IDC_KDF_ARGON2I:
  319. pp->params.argon2_flavour = Argon2i;
  320. return 0;
  321. case IDC_KDF_ARGON2D:
  322. pp->params.argon2_flavour = Argon2d;
  323. return 0;
  324. case IDC_ARGON2_MEM:
  325. try_get_dlg_item_uint32(hwnd, IDC_ARGON2_MEM,
  326. &pp->params.argon2_mem);
  327. return 0;
  328. case IDC_PPK_AUTO_YES:
  329. pp->params.argon2_passes_auto = true;
  330. buf = dupprintf("%"PRIu32, pp->time_ms);
  331. SetDlgItemText(hwnd, IDC_ARGON2_TIME, buf);
  332. sfree(buf);
  333. return 0;
  334. case IDC_PPK_AUTO_NO:
  335. pp->params.argon2_passes_auto = false;
  336. buf = dupprintf("%"PRIu32, pp->time_passes);
  337. SetDlgItemText(hwnd, IDC_ARGON2_TIME, buf);
  338. sfree(buf);
  339. return 0;
  340. case IDC_ARGON2_TIME:
  341. try_get_dlg_item_uint32(hwnd, IDC_ARGON2_TIME,
  342. pp->params.argon2_passes_auto ?
  343. &pp->time_ms : &pp->time_passes);
  344. return 0;
  345. case IDC_ARGON2_PARALLEL:
  346. try_get_dlg_item_uint32(hwnd, IDC_ARGON2_PARALLEL,
  347. &pp->params.argon2_parallelism);
  348. return 0;
  349. }
  350. return 0;
  351. case WM_HELP: {
  352. int id = ((LPHELPINFO)lParam)->iCtrlId;
  353. const char *topic = NULL;
  354. switch (id) {
  355. case IDC_PPKVER_STATIC:
  356. case IDC_PPKVER_2:
  357. case IDC_PPKVER_3:
  358. topic = WINHELP_CTX_puttygen_ppkver; break;
  359. case IDC_KDF_STATIC:
  360. case IDC_KDF_ARGON2ID:
  361. case IDC_KDF_ARGON2I:
  362. case IDC_KDF_ARGON2D:
  363. case IDC_ARGON2_MEM_STATIC:
  364. case IDC_ARGON2_MEM:
  365. case IDC_ARGON2_MEM_STATIC2:
  366. case IDC_ARGON2_TIME_STATIC:
  367. case IDC_ARGON2_TIME:
  368. case IDC_PPK_AUTO_YES:
  369. case IDC_PPK_AUTO_NO:
  370. case IDC_ARGON2_PARALLEL_STATIC:
  371. case IDC_ARGON2_PARALLEL:
  372. topic = WINHELP_CTX_puttygen_kdfparam; break;
  373. }
  374. if (topic) {
  375. launch_help(hwnd, topic);
  376. } else {
  377. MessageBeep(0);
  378. }
  379. break;
  380. }
  381. case WM_CLOSE:
  382. EndDialog(hwnd, 0);
  383. return 0;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * Prompt for a key file. Assumes the filename buffer is of size
  389. * FILENAME_MAX.
  390. */
  391. static bool prompt_keyfile(HWND hwnd, char *dlgtitle,
  392. char *filename, bool save, bool ppk)
  393. {
  394. OPENFILENAME of;
  395. memset(&of, 0, sizeof(of));
  396. of.hwndOwner = hwnd;
  397. if (ppk) {
  398. of.lpstrFilter = "PuTTY Private Key Files (*.ppk)\0*.ppk\0"
  399. "All Files (*.*)\0*\0\0\0";
  400. of.lpstrDefExt = ".ppk";
  401. } else {
  402. of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
  403. }
  404. of.lpstrCustomFilter = NULL;
  405. of.nFilterIndex = 1;
  406. of.lpstrFile = filename;
  407. *filename = '\0';
  408. of.nMaxFile = FILENAME_MAX;
  409. of.lpstrFileTitle = NULL;
  410. of.lpstrTitle = dlgtitle;
  411. of.Flags = 0;
  412. return request_file(NULL, &of, false, save);
  413. }
  414. /*
  415. * Dialog-box function for the Licence box.
  416. */
  417. static INT_PTR CALLBACK LicenceProc(HWND hwnd, UINT msg,
  418. WPARAM wParam, LPARAM lParam)
  419. {
  420. switch (msg) {
  421. case WM_INITDIALOG: {
  422. /*
  423. * Centre the window.
  424. */
  425. RECT rs, rd;
  426. HWND hw;
  427. hw = GetDesktopWindow();
  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);
  433. SetDlgItemText(hwnd, 1000, LICENCE_TEXT("\r\n\r\n"));
  434. return 1;
  435. }
  436. case WM_COMMAND:
  437. switch (LOWORD(wParam)) {
  438. case IDOK:
  439. case IDCANCEL:
  440. EndDialog(hwnd, 1);
  441. return 0;
  442. }
  443. return 0;
  444. case WM_CLOSE:
  445. EndDialog(hwnd, 1);
  446. return 0;
  447. }
  448. return 0;
  449. }
  450. /*
  451. * Dialog-box function for the About box.
  452. */
  453. static INT_PTR CALLBACK AboutProc(HWND hwnd, UINT msg,
  454. WPARAM wParam, LPARAM lParam)
  455. {
  456. switch (msg) {
  457. case WM_INITDIALOG:
  458. /*
  459. * Centre the window.
  460. */
  461. { /* centre the window */
  462. RECT rs, rd;
  463. HWND hw;
  464. hw = GetDesktopWindow();
  465. if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
  466. MoveWindow(hwnd,
  467. (rs.right + rs.left + rd.left - rd.right) / 2,
  468. (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
  469. rd.right - rd.left, rd.bottom - rd.top, true);
  470. }
  471. {
  472. char *buildinfo_text = buildinfo("\r\n");
  473. char *text = dupprintf(
  474. "PuTTYgen\r\n\r\n%s\r\n\r\n%s\r\n\r\n%s",
  475. ver, buildinfo_text,
  476. "\251 " SHORT_COPYRIGHT_DETAILS ". All rights reserved.");
  477. sfree(buildinfo_text);
  478. SetDlgItemText(hwnd, 1000, text);
  479. MakeDlgItemBorderless(hwnd, 1000);
  480. sfree(text);
  481. }
  482. return 1;
  483. case WM_COMMAND:
  484. switch (LOWORD(wParam)) {
  485. case IDOK:
  486. case IDCANCEL:
  487. EndDialog(hwnd, 1);
  488. return 0;
  489. case 101:
  490. EnableWindow(hwnd, 0);
  491. DialogBox(hinst, MAKEINTRESOURCE(214), hwnd, LicenceProc);
  492. EnableWindow(hwnd, 1);
  493. SetActiveWindow(hwnd);
  494. return 0;
  495. case 102:
  496. /* Load web browser */
  497. ShellExecute(hwnd, "open",
  498. "https://www.chiark.greenend.org.uk/~sgtatham/putty/",
  499. 0, 0, SW_SHOWDEFAULT);
  500. return 0;
  501. }
  502. return 0;
  503. case WM_CLOSE:
  504. EndDialog(hwnd, 1);
  505. return 0;
  506. }
  507. return 0;
  508. }
  509. typedef enum {RSA, DSA, ECDSA, EDDSA} keytype;
  510. /*
  511. * Thread to generate a key.
  512. */
  513. struct rsa_key_thread_params {
  514. HWND progressbar; /* notify this with progress */
  515. HWND dialog; /* notify this on completion */
  516. int key_bits; /* bits in key modulus (RSA, DSA) */
  517. int curve_bits; /* bits in elliptic curve (ECDSA) */
  518. keytype keytype;
  519. const PrimeGenerationPolicy *primepolicy;
  520. bool rsa_strong;
  521. union {
  522. RSAKey *key;
  523. struct dsa_key *dsakey;
  524. struct ecdsa_key *eckey;
  525. struct eddsa_key *edkey;
  526. };
  527. };
  528. static DWORD WINAPI generate_key_thread(void *param)
  529. {
  530. struct rsa_key_thread_params *params =
  531. (struct rsa_key_thread_params *) param;
  532. struct progress prog;
  533. prog.progbar = params->progressbar;
  534. win_progress_initialise(&prog);
  535. PrimeGenerationContext *pgc = primegen_new_context(params->primepolicy);
  536. if (params->keytype == DSA)
  537. dsa_generate(params->dsakey, params->key_bits, pgc, &prog.rec);
  538. else if (params->keytype == ECDSA)
  539. ecdsa_generate(params->eckey, params->curve_bits);
  540. else if (params->keytype == EDDSA)
  541. eddsa_generate(params->edkey, params->curve_bits);
  542. else
  543. rsa_generate(params->key, params->key_bits, params->rsa_strong,
  544. pgc, &prog.rec);
  545. primegen_free_context(pgc);
  546. PostMessage(params->dialog, WM_DONEKEY, 0, 0);
  547. win_progress_cleanup(&prog);
  548. sfree(params);
  549. return 0;
  550. }
  551. struct InitialParams {
  552. int keybutton;
  553. int primepolicybutton;
  554. bool rsa_strong;
  555. FingerprintType fptype;
  556. int keybits;
  557. int eccurve_index, edcurve_index;
  558. };
  559. struct MainDlgState {
  560. bool generation_thread_exists;
  561. bool key_exists;
  562. int entropy_got, entropy_required;
  563. strbuf *entropy;
  564. ULONG entropy_prev_msgtime;
  565. int key_bits, curve_bits;
  566. bool ssh2;
  567. keytype keytype;
  568. const PrimeGenerationPolicy *primepolicy;
  569. bool rsa_strong;
  570. FingerprintType fptype;
  571. char **commentptr; /* points to key.comment or ssh2key.comment */
  572. ssh2_userkey ssh2key;
  573. union {
  574. RSAKey key;
  575. struct dsa_key dsakey;
  576. struct ecdsa_key eckey;
  577. struct eddsa_key edkey;
  578. };
  579. HMENU filemenu, keymenu, cvtmenu;
  580. };
  581. /*
  582. * Rate limit for incrementing the entropy_got counter.
  583. *
  584. * Some pointing devices (e.g. gaming mice) can be set to send
  585. * mouse-movement events at an extremely high sample rate like 1kHz.
  586. * In that situation, there's likely to be a strong correlation
  587. * between the contents of successive movement events, so you have to
  588. * regard the mouse movements as containing less entropy each.
  589. *
  590. * A reasonably simple approach to this is to continue to buffer all
  591. * mouse data, but limit the rate at which we increment the counter
  592. * for how much entropy we think we've collected. That way, the user
  593. * still has to spend time wiggling the mouse back and forth in a way
  594. * that varies with muscle motions and introduces randomness.
  595. */
  596. #define ENTROPY_RATE_LIMIT 10 /* in units of GetMessageTime(), i.e. ms */
  597. static void hidemany(HWND hwnd, const int *ids, bool hideit)
  598. {
  599. while (*ids) {
  600. ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
  601. }
  602. }
  603. enum {
  604. controlidstart = 100,
  605. IDC_QUIT,
  606. IDC_TITLE,
  607. IDC_BOX_KEY,
  608. IDC_NOKEY,
  609. IDC_GENERATING,
  610. IDC_PROGRESS,
  611. IDC_PKSTATIC, IDC_KEYDISPLAY,
  612. IDC_CERTSTATIC, IDC_CERTMOREINFO,
  613. IDC_FPSTATIC, IDC_FINGERPRINT,
  614. IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
  615. IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
  616. IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT,
  617. IDC_BOX_ACTIONS,
  618. IDC_GENSTATIC, IDC_GENERATE,
  619. IDC_LOADSTATIC, IDC_LOAD,
  620. IDC_SAVESTATIC, IDC_SAVE, IDC_SAVEPUB,
  621. IDC_BOX_PARAMS,
  622. IDC_TYPESTATIC, IDC_KEYSSH1, IDC_KEYSSH2RSA, IDC_KEYSSH2DSA,
  623. IDC_KEYSSH2ECDSA, IDC_KEYSSH2EDDSA,
  624. IDC_PRIMEGEN_PROB, IDC_PRIMEGEN_MAURER_SIMPLE, IDC_PRIMEGEN_MAURER_COMPLEX,
  625. IDC_RSA_STRONG,
  626. IDC_FPTYPE_SHA256, IDC_FPTYPE_MD5,
  627. IDC_PPK_PARAMS,
  628. IDC_BITSSTATIC, IDC_BITS,
  629. IDC_ECCURVESTATIC, IDC_ECCURVE,
  630. IDC_EDCURVESTATIC, IDC_EDCURVE,
  631. IDC_NOTHINGSTATIC,
  632. IDC_ABOUT,
  633. IDC_GIVEHELP,
  634. IDC_IMPORT,
  635. IDC_EXPORT_OPENSSH_AUTO, IDC_EXPORT_OPENSSH_NEW,
  636. IDC_EXPORT_SSHCOM,
  637. IDC_ADDCERT, IDC_REMCERT,
  638. };
  639. static void setupbigedit1(HWND hwnd, RSAKey *key)
  640. {
  641. ShowWindow(GetDlgItem(hwnd, IDC_CERTSTATIC), SW_HIDE);
  642. ShowWindow(GetDlgItem(hwnd, IDC_CERTMOREINFO), SW_HIDE);
  643. ShowWindow(GetDlgItem(hwnd, IDC_PKSTATIC), SW_SHOW);
  644. ShowWindow(GetDlgItem(hwnd, IDC_KEYDISPLAY), SW_SHOW);
  645. SetDlgItemText(hwnd, IDC_PKSTATIC,
  646. "&Public key for pasting into authorized_keys file:");
  647. char *buffer = ssh1_pubkey_str(key);
  648. SetDlgItemText(hwnd, IDC_KEYDISPLAY, buffer);
  649. sfree(buffer);
  650. }
  651. static void setupbigedit2(HWND hwnd, ssh2_userkey *key)
  652. {
  653. if (ssh_key_alg(key->key)->is_certificate) {
  654. ShowWindow(GetDlgItem(hwnd, IDC_CERTSTATIC), SW_SHOW);
  655. ShowWindow(GetDlgItem(hwnd, IDC_CERTMOREINFO), SW_SHOW);
  656. ShowWindow(GetDlgItem(hwnd, IDC_PKSTATIC), SW_HIDE);
  657. ShowWindow(GetDlgItem(hwnd, IDC_KEYDISPLAY), SW_HIDE);
  658. SetDlgItemText(hwnd, IDC_CERTSTATIC,
  659. "This public key contains an OpenSSH certificate.");
  660. } else {
  661. ShowWindow(GetDlgItem(hwnd, IDC_CERTSTATIC), SW_HIDE);
  662. ShowWindow(GetDlgItem(hwnd, IDC_CERTMOREINFO), SW_HIDE);
  663. ShowWindow(GetDlgItem(hwnd, IDC_PKSTATIC), SW_SHOW);
  664. ShowWindow(GetDlgItem(hwnd, IDC_KEYDISPLAY), SW_SHOW);
  665. SetDlgItemText(hwnd, IDC_PKSTATIC, "&Public key for pasting into "
  666. "OpenSSH authorized_keys file:");
  667. char *buffer = ssh2_pubkey_openssh_str(key);
  668. SetDlgItemText(hwnd, IDC_KEYDISPLAY, buffer);
  669. sfree(buffer);
  670. }
  671. }
  672. /*
  673. * Warn about the obsolescent key file format.
  674. */
  675. void old_keyfile_warning(void)
  676. {
  677. static const char mbtitle[] = "PuTTY Key File Warning";
  678. static const char message[] =
  679. "You are loading an SSH-2 private key which has an\n"
  680. "old version of the file format. This means your key\n"
  681. "file is not fully tamperproof. Future versions of\n"
  682. "PuTTY may stop supporting this private key format,\n"
  683. "so we recommend you convert your key to the new\n"
  684. "format.\n"
  685. "\n"
  686. "Once the key is loaded into PuTTYgen, you can perform\n"
  687. "this conversion simply by saving it again.";
  688. MessageBox(NULL, message, mbtitle, MB_OK);
  689. }
  690. static const int nokey_ids[] = { IDC_NOKEY, 0 };
  691. static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 };
  692. static const int gotkey_ids_unconditional[] = {
  693. IDC_FPSTATIC, IDC_FINGERPRINT,
  694. IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
  695. IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
  696. IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0
  697. };
  698. static const int gotkey_ids_conditional[] = {
  699. IDC_PKSTATIC, IDC_KEYDISPLAY,
  700. IDC_CERTSTATIC, IDC_CERTMOREINFO,
  701. };
  702. /*
  703. * Small UI helper function to switch the state of the main dialog
  704. * by enabling and disabling controls and menu items.
  705. */
  706. void ui_set_state(HWND hwnd, struct MainDlgState *state, int status)
  707. {
  708. int type;
  709. switch (status) {
  710. case 0: /* no key */
  711. hidemany(hwnd, nokey_ids, false);
  712. hidemany(hwnd, generating_ids, true);
  713. hidemany(hwnd, gotkey_ids_unconditional, true);
  714. hidemany(hwnd, gotkey_ids_conditional, true);
  715. EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
  716. EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
  717. EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
  718. EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 0);
  719. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 1);
  720. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 1);
  721. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 1);
  722. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2ECDSA), 1);
  723. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2EDDSA), 1);
  724. EnableWindow(GetDlgItem(hwnd, IDC_BITS), 1);
  725. EnableMenuItem(state->filemenu, IDC_LOAD, MF_ENABLED|MF_BYCOMMAND);
  726. EnableMenuItem(state->filemenu, IDC_SAVE, MF_GRAYED|MF_BYCOMMAND);
  727. EnableMenuItem(state->filemenu, IDC_SAVEPUB, MF_GRAYED|MF_BYCOMMAND);
  728. EnableMenuItem(state->keymenu, IDC_GENERATE, MF_ENABLED|MF_BYCOMMAND);
  729. EnableMenuItem(state->keymenu, IDC_KEYSSH1, MF_ENABLED|MF_BYCOMMAND);
  730. EnableMenuItem(state->keymenu, IDC_KEYSSH2RSA, MF_ENABLED|MF_BYCOMMAND);
  731. EnableMenuItem(state->keymenu, IDC_KEYSSH2DSA, MF_ENABLED|MF_BYCOMMAND);
  732. EnableMenuItem(state->keymenu, IDC_KEYSSH2ECDSA,
  733. MF_ENABLED|MF_BYCOMMAND);
  734. EnableMenuItem(state->keymenu, IDC_KEYSSH2EDDSA,
  735. MF_ENABLED|MF_BYCOMMAND);
  736. EnableMenuItem(state->cvtmenu, IDC_IMPORT, MF_ENABLED|MF_BYCOMMAND);
  737. EnableMenuItem(state->cvtmenu, IDC_EXPORT_OPENSSH_AUTO,
  738. MF_GRAYED|MF_BYCOMMAND);
  739. EnableMenuItem(state->cvtmenu, IDC_EXPORT_OPENSSH_NEW,
  740. MF_GRAYED|MF_BYCOMMAND);
  741. EnableMenuItem(state->cvtmenu, IDC_EXPORT_SSHCOM,
  742. MF_GRAYED|MF_BYCOMMAND);
  743. EnableMenuItem(state->keymenu, IDC_ADDCERT, MF_GRAYED|MF_BYCOMMAND);
  744. EnableMenuItem(state->keymenu, IDC_REMCERT, MF_GRAYED|MF_BYCOMMAND);
  745. break;
  746. case 1: /* generating key */
  747. hidemany(hwnd, nokey_ids, true);
  748. hidemany(hwnd, generating_ids, false);
  749. hidemany(hwnd, gotkey_ids_unconditional, true);
  750. hidemany(hwnd, gotkey_ids_conditional, true);
  751. EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
  752. EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
  753. EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
  754. EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 0);
  755. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 0);
  756. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 0);
  757. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 0);
  758. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2ECDSA), 0);
  759. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2EDDSA), 0);
  760. EnableWindow(GetDlgItem(hwnd, IDC_BITS), 0);
  761. EnableMenuItem(state->filemenu, IDC_LOAD, MF_GRAYED|MF_BYCOMMAND);
  762. EnableMenuItem(state->filemenu, IDC_SAVE, MF_GRAYED|MF_BYCOMMAND);
  763. EnableMenuItem(state->filemenu, IDC_SAVEPUB, MF_GRAYED|MF_BYCOMMAND);
  764. EnableMenuItem(state->keymenu, IDC_GENERATE, MF_GRAYED|MF_BYCOMMAND);
  765. EnableMenuItem(state->keymenu, IDC_KEYSSH1, MF_GRAYED|MF_BYCOMMAND);
  766. EnableMenuItem(state->keymenu, IDC_KEYSSH2RSA, MF_GRAYED|MF_BYCOMMAND);
  767. EnableMenuItem(state->keymenu, IDC_KEYSSH2DSA, MF_GRAYED|MF_BYCOMMAND);
  768. EnableMenuItem(state->keymenu, IDC_KEYSSH2ECDSA,
  769. MF_GRAYED|MF_BYCOMMAND);
  770. EnableMenuItem(state->keymenu, IDC_KEYSSH2EDDSA,
  771. MF_GRAYED|MF_BYCOMMAND);
  772. EnableMenuItem(state->cvtmenu, IDC_IMPORT, MF_GRAYED|MF_BYCOMMAND);
  773. EnableMenuItem(state->cvtmenu, IDC_EXPORT_OPENSSH_AUTO,
  774. MF_GRAYED|MF_BYCOMMAND);
  775. EnableMenuItem(state->cvtmenu, IDC_EXPORT_OPENSSH_NEW,
  776. MF_GRAYED|MF_BYCOMMAND);
  777. EnableMenuItem(state->cvtmenu, IDC_EXPORT_SSHCOM,
  778. MF_GRAYED|MF_BYCOMMAND);
  779. EnableMenuItem(state->keymenu, IDC_ADDCERT, MF_GRAYED|MF_BYCOMMAND);
  780. EnableMenuItem(state->keymenu, IDC_REMCERT, MF_GRAYED|MF_BYCOMMAND);
  781. break;
  782. case 2:
  783. hidemany(hwnd, nokey_ids, true);
  784. hidemany(hwnd, generating_ids, true);
  785. hidemany(hwnd, gotkey_ids_unconditional, false);
  786. // gotkey_ids_conditional will be unhidden by setupbigedit2
  787. EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
  788. EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
  789. EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
  790. EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 1);
  791. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 1);
  792. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 1);
  793. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 1);
  794. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2ECDSA), 1);
  795. EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2EDDSA), 1);
  796. EnableWindow(GetDlgItem(hwnd, IDC_BITS), 1);
  797. EnableMenuItem(state->filemenu, IDC_LOAD, MF_ENABLED|MF_BYCOMMAND);
  798. EnableMenuItem(state->filemenu, IDC_SAVE, MF_ENABLED|MF_BYCOMMAND);
  799. EnableMenuItem(state->filemenu, IDC_SAVEPUB, MF_ENABLED|MF_BYCOMMAND);
  800. EnableMenuItem(state->keymenu, IDC_GENERATE, MF_ENABLED|MF_BYCOMMAND);
  801. EnableMenuItem(state->keymenu, IDC_KEYSSH1, MF_ENABLED|MF_BYCOMMAND);
  802. EnableMenuItem(state->keymenu, IDC_KEYSSH2RSA,MF_ENABLED|MF_BYCOMMAND);
  803. EnableMenuItem(state->keymenu, IDC_KEYSSH2DSA,MF_ENABLED|MF_BYCOMMAND);
  804. EnableMenuItem(state->keymenu, IDC_KEYSSH2ECDSA,
  805. MF_ENABLED|MF_BYCOMMAND);
  806. EnableMenuItem(state->keymenu, IDC_KEYSSH2EDDSA,
  807. MF_ENABLED|MF_BYCOMMAND);
  808. EnableMenuItem(state->cvtmenu, IDC_IMPORT, MF_ENABLED|MF_BYCOMMAND);
  809. /*
  810. * Enable export menu items if and only if the key type
  811. * supports this kind of export.
  812. */
  813. type = state->ssh2 ? SSH_KEYTYPE_SSH2 : SSH_KEYTYPE_SSH1;
  814. #define do_export_menuitem(x,y) \
  815. EnableMenuItem(state->cvtmenu, x, MF_BYCOMMAND | \
  816. (import_target_type(y)==type?MF_ENABLED:MF_GRAYED))
  817. do_export_menuitem(IDC_EXPORT_OPENSSH_AUTO, SSH_KEYTYPE_OPENSSH_AUTO);
  818. do_export_menuitem(IDC_EXPORT_OPENSSH_NEW, SSH_KEYTYPE_OPENSSH_NEW);
  819. do_export_menuitem(IDC_EXPORT_SSHCOM, SSH_KEYTYPE_SSHCOM);
  820. #undef do_export_menuitem
  821. /*
  822. * Enable certificate menu items similarly.
  823. */
  824. {
  825. bool add_cert_allowed = false, rem_cert_allowed = false;
  826. if (state->ssh2 && state->ssh2key.key) {
  827. const ssh_keyalg *alg = ssh_key_alg(state->ssh2key.key);
  828. if (alg->is_certificate) {
  829. /* If there's a certificate, we can remove it */
  830. rem_cert_allowed = true;
  831. /* And reset to the base algorithm for the next check */
  832. alg = alg->base_alg;
  833. }
  834. /* Now, do we have any certified version of this alg? */
  835. for (size_t i = 0; i < n_keyalgs; i++) {
  836. if (all_keyalgs[i]->base_alg == alg) {
  837. add_cert_allowed = true;
  838. break;
  839. }
  840. }
  841. }
  842. EnableMenuItem(state->keymenu, IDC_ADDCERT, MF_BYCOMMAND |
  843. (add_cert_allowed ? MF_ENABLED : MF_GRAYED));
  844. EnableMenuItem(state->keymenu, IDC_REMCERT, MF_BYCOMMAND |
  845. (rem_cert_allowed ? MF_ENABLED : MF_GRAYED));
  846. }
  847. break;
  848. }
  849. }
  850. /*
  851. * Helper functions to set the key type, taking care of keeping the
  852. * menu and radio button selections in sync and also showing/hiding
  853. * the appropriate size/curve control for the current key type.
  854. */
  855. void ui_update_key_type_ctrls(HWND hwnd)
  856. {
  857. enum { BITS, ECCURVE, EDCURVE, NOTHING } which;
  858. static const int bits_ids[] = {
  859. IDC_BITSSTATIC, IDC_BITS, 0
  860. };
  861. static const int eccurve_ids[] = {
  862. IDC_ECCURVESTATIC, IDC_ECCURVE, 0
  863. };
  864. static const int edcurve_ids[] = {
  865. IDC_EDCURVESTATIC, IDC_EDCURVE, 0
  866. };
  867. static const int nothing_ids[] = {
  868. IDC_NOTHINGSTATIC, 0
  869. };
  870. if (IsDlgButtonChecked(hwnd, IDC_KEYSSH1) ||
  871. IsDlgButtonChecked(hwnd, IDC_KEYSSH2RSA) ||
  872. IsDlgButtonChecked(hwnd, IDC_KEYSSH2DSA)) {
  873. which = BITS;
  874. } else if (IsDlgButtonChecked(hwnd, IDC_KEYSSH2ECDSA)) {
  875. which = ECCURVE;
  876. } else if (IsDlgButtonChecked(hwnd, IDC_KEYSSH2EDDSA)) {
  877. which = EDCURVE;
  878. } else {
  879. /* Currently not used since Ed25519 stopped being the only
  880. * thing in its class, but I'll keep it here in case it comes
  881. * in useful again */
  882. which = NOTHING;
  883. }
  884. hidemany(hwnd, bits_ids, which != BITS);
  885. hidemany(hwnd, eccurve_ids, which != ECCURVE);
  886. hidemany(hwnd, edcurve_ids, which != EDCURVE);
  887. hidemany(hwnd, nothing_ids, which != NOTHING);
  888. }
  889. void ui_set_key_type(HWND hwnd, struct MainDlgState *state, int button)
  890. {
  891. CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2EDDSA, button);
  892. CheckMenuRadioItem(state->keymenu, IDC_KEYSSH1, IDC_KEYSSH2EDDSA,
  893. button, MF_BYCOMMAND);
  894. ui_update_key_type_ctrls(hwnd);
  895. }
  896. void ui_set_primepolicy(HWND hwnd, struct MainDlgState *state, int option)
  897. {
  898. CheckMenuRadioItem(state->keymenu, IDC_PRIMEGEN_PROB,
  899. IDC_PRIMEGEN_MAURER_COMPLEX, option, MF_BYCOMMAND);
  900. switch (option) {
  901. case IDC_PRIMEGEN_PROB:
  902. state->primepolicy = &primegen_probabilistic;
  903. break;
  904. case IDC_PRIMEGEN_MAURER_SIMPLE:
  905. state->primepolicy = &primegen_provable_maurer_simple;
  906. break;
  907. case IDC_PRIMEGEN_MAURER_COMPLEX:
  908. state->primepolicy = &primegen_provable_maurer_complex;
  909. break;
  910. }
  911. }
  912. void ui_set_rsa_strong(HWND hwnd, struct MainDlgState *state, bool enable)
  913. {
  914. state->rsa_strong = enable;
  915. CheckMenuItem(state->keymenu, IDC_RSA_STRONG,
  916. (enable ? MF_CHECKED : 0) | MF_BYCOMMAND);
  917. }
  918. static FingerprintType idc_to_fptype(int option)
  919. {
  920. switch (option) {
  921. case IDC_FPTYPE_SHA256:
  922. return SSH_FPTYPE_SHA256;
  923. case IDC_FPTYPE_MD5:
  924. return SSH_FPTYPE_MD5;
  925. default:
  926. unreachable("bad control id in idc_to_fptype");
  927. }
  928. }
  929. static int fptype_to_idc(FingerprintType fptype)
  930. {
  931. switch (fptype) {
  932. case SSH_FPTYPE_SHA256:
  933. return IDC_FPTYPE_SHA256;
  934. case SSH_FPTYPE_MD5:
  935. return IDC_FPTYPE_MD5;
  936. default:
  937. unreachable("bad fptype in fptype_to_idc");
  938. }
  939. }
  940. void ui_set_fptype(HWND hwnd, struct MainDlgState *state, int option)
  941. {
  942. CheckMenuRadioItem(state->keymenu, IDC_FPTYPE_SHA256,
  943. IDC_FPTYPE_MD5, option, MF_BYCOMMAND);
  944. state->fptype = idc_to_fptype(option);
  945. if (state->key_exists && state->ssh2) {
  946. char *fp = ssh2_fingerprint(state->ssh2key.key, state->fptype);
  947. SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
  948. sfree(fp);
  949. }
  950. }
  951. static void update_ui_after_ssh2_pubkey_change(
  952. HWND hwnd, struct MainDlgState *state)
  953. {
  954. /* Smaller version of update_ui_after_load which doesn't need to
  955. * be told things like the passphrase, which we aren't changing
  956. * anyway */
  957. char *savecomment = state->ssh2key.comment;
  958. state->ssh2key.comment = NULL;
  959. char *fp = ssh2_fingerprint(state->ssh2key.key, state->fptype);
  960. state->ssh2key.comment = savecomment;
  961. SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
  962. sfree(fp);
  963. setupbigedit2(hwnd, &state->ssh2key);
  964. }
  965. static void update_ui_after_load(HWND hwnd, struct MainDlgState *state,
  966. const char *passphrase, int type,
  967. RSAKey *newkey1, ssh2_userkey *newkey2)
  968. {
  969. SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, passphrase);
  970. SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, passphrase);
  971. if (type == SSH_KEYTYPE_SSH1) {
  972. char *fingerprint, *savecomment;
  973. state->ssh2 = false;
  974. state->commentptr = &state->key.comment;
  975. state->key = *newkey1; /* structure copy */
  976. /*
  977. * Set the key fingerprint.
  978. */
  979. savecomment = state->key.comment;
  980. state->key.comment = NULL;
  981. fingerprint = rsa_ssh1_fingerprint(&state->key);
  982. state->key.comment = savecomment;
  983. SetDlgItemText(hwnd, IDC_FINGERPRINT, fingerprint);
  984. sfree(fingerprint);
  985. /*
  986. * Construct a decimal representation of the key, for pasting
  987. * into .ssh/authorized_keys on a Unix box.
  988. */
  989. setupbigedit1(hwnd, &state->key);
  990. } else {
  991. state->ssh2 = true;
  992. state->commentptr = &state->ssh2key.comment;
  993. state->ssh2key = *newkey2; /* structure copy */
  994. sfree(newkey2);
  995. update_ui_after_ssh2_pubkey_change(hwnd, state);
  996. }
  997. SetDlgItemText(hwnd, IDC_COMMENTEDIT,
  998. *state->commentptr);
  999. /*
  1000. * Finally, hide the progress bar and show the key data.
  1001. */
  1002. ui_set_state(hwnd, state, 2);
  1003. state->key_exists = true;
  1004. }
  1005. void load_key_file(HWND hwnd, struct MainDlgState *state,
  1006. Filename *filename, bool was_import_cmd)
  1007. {
  1008. char *passphrase;
  1009. bool needs_pass;
  1010. int type, realtype;
  1011. int ret;
  1012. const char *errmsg = NULL;
  1013. char *comment;
  1014. RSAKey newkey1;
  1015. ssh2_userkey *newkey2 = NULL;
  1016. type = realtype = key_type(filename);
  1017. if (type != SSH_KEYTYPE_SSH1 &&
  1018. type != SSH_KEYTYPE_SSH2 &&
  1019. !import_possible(type)) {
  1020. char *msg = dupprintf("Couldn't load private key (%s)",
  1021. key_type_to_str(type));
  1022. message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
  1023. false, HELPCTXID(errors_cantloadkey));
  1024. sfree(msg);
  1025. return;
  1026. }
  1027. if (type != SSH_KEYTYPE_SSH1 &&
  1028. type != SSH_KEYTYPE_SSH2) {
  1029. realtype = type;
  1030. type = import_target_type(type);
  1031. }
  1032. comment = NULL;
  1033. passphrase = NULL;
  1034. if (realtype == SSH_KEYTYPE_SSH1)
  1035. needs_pass = rsa1_encrypted_f(filename, &comment);
  1036. else if (realtype == SSH_KEYTYPE_SSH2)
  1037. needs_pass = ppk_encrypted_f(filename, &comment);
  1038. else
  1039. needs_pass = import_encrypted(filename, realtype, &comment);
  1040. do {
  1041. burnstr(passphrase);
  1042. passphrase = NULL;
  1043. if (needs_pass) {
  1044. int dlgret;
  1045. struct PassphraseProcStruct pps;
  1046. pps.passphrase = &passphrase;
  1047. pps.comment = comment;
  1048. dlgret = DialogBoxParam(hinst,
  1049. MAKEINTRESOURCE(210),
  1050. NULL, PassphraseProc,
  1051. (LPARAM) &pps);
  1052. if (!dlgret) {
  1053. ret = -2;
  1054. break;
  1055. }
  1056. assert(passphrase != NULL);
  1057. } else
  1058. passphrase = dupstr("");
  1059. if (type == SSH_KEYTYPE_SSH1) {
  1060. if (realtype == type)
  1061. ret = rsa1_load_f(filename, &newkey1, passphrase, &errmsg);
  1062. else
  1063. ret = import_ssh1(filename, realtype, &newkey1,
  1064. passphrase, &errmsg);
  1065. } else {
  1066. if (realtype == type)
  1067. newkey2 = ppk_load_f(filename, passphrase, &errmsg);
  1068. else
  1069. newkey2 = import_ssh2(filename, realtype, passphrase, &errmsg);
  1070. if (newkey2 == SSH2_WRONG_PASSPHRASE)
  1071. ret = -1;
  1072. else if (!newkey2)
  1073. ret = 0;
  1074. else
  1075. ret = 1;
  1076. }
  1077. } while (ret == -1);
  1078. if (comment)
  1079. sfree(comment);
  1080. if (ret == 0) {
  1081. char *msg = dupprintf("Couldn't load private key (%s)", errmsg);
  1082. message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
  1083. false, HELPCTXID(errors_cantloadkey));
  1084. sfree(msg);
  1085. } else if (ret == 1) {
  1086. /*
  1087. * Now update the key controls with all the
  1088. * key data.
  1089. */
  1090. update_ui_after_load(hwnd, state, passphrase, type, &newkey1, newkey2);
  1091. /*
  1092. * If the user has imported a foreign key
  1093. * using the Load command, let them know.
  1094. * If they've used the Import command, be
  1095. * silent.
  1096. */
  1097. if (realtype != type && !was_import_cmd) {
  1098. char msg[512];
  1099. sprintf(msg, "Successfully imported foreign key\n"
  1100. "(%s).\n"
  1101. "To use this key with PuTTY, you need to\n"
  1102. "use the \"Save private key\" command to\n"
  1103. "save it in PuTTY's own format.",
  1104. key_type_to_str(realtype));
  1105. MessageBox(NULL, msg, "PuTTYgen Notice",
  1106. MB_OK | MB_ICONINFORMATION);
  1107. }
  1108. }
  1109. burnstr(passphrase);
  1110. }
  1111. void add_certificate(HWND hwnd, struct MainDlgState *state,
  1112. Filename *filename)
  1113. {
  1114. int type = key_type(filename);
  1115. if (type != SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 &&
  1116. type != SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH) {
  1117. char *msg = dupprintf("Couldn't load certificate (%s)",
  1118. key_type_to_str(type));
  1119. message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
  1120. false, HELPCTXID(errors_cantloadkey));
  1121. sfree(msg);
  1122. return;
  1123. }
  1124. char *algname = NULL;
  1125. char *comment = NULL;
  1126. const char *error = NULL;
  1127. strbuf *pub = strbuf_new();
  1128. if (!ppk_loadpub_f(filename, &algname, BinarySink_UPCAST(pub), &comment,
  1129. &error)) {
  1130. char *msg = dupprintf("Couldn't load certificate (%s)", error);
  1131. message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
  1132. false, HELPCTXID(errors_cantloadkey));
  1133. sfree(msg);
  1134. strbuf_free(pub);
  1135. return;
  1136. }
  1137. sfree(comment);
  1138. const ssh_keyalg *alg = find_pubkey_alg(algname);
  1139. if (!alg) {
  1140. char *msg = dupprintf("Couldn't load certificate (unsupported "
  1141. "algorithm name '%s')", algname);
  1142. message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
  1143. false, HELPCTXID(errors_cantloadkey));
  1144. sfree(msg);
  1145. sfree(algname);
  1146. strbuf_free(pub);
  1147. return;
  1148. }
  1149. sfree(algname);
  1150. /* Check the two public keys match apart from certificates */
  1151. strbuf *old_basepub = strbuf_new();
  1152. ssh_key_public_blob(ssh_key_base_key(state->ssh2key.key),
  1153. BinarySink_UPCAST(old_basepub));
  1154. ssh_key *new_pubkey = ssh_key_new_pub(alg, ptrlen_from_strbuf(pub));
  1155. strbuf *new_basepub = strbuf_new();
  1156. ssh_key_public_blob(ssh_key_base_key(new_pubkey),
  1157. BinarySink_UPCAST(new_basepub));
  1158. ssh_key_free(new_pubkey);
  1159. bool match = ptrlen_eq_ptrlen(ptrlen_from_strbuf(old_basepub),
  1160. ptrlen_from_strbuf(new_basepub));
  1161. strbuf_free(old_basepub);
  1162. strbuf_free(new_basepub);
  1163. if (!match) {
  1164. char *msg = dupprintf("Certificate is for a different public key");
  1165. message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
  1166. false, HELPCTXID(errors_cantloadkey));
  1167. sfree(msg);
  1168. strbuf_free(pub);
  1169. return;
  1170. }
  1171. strbuf *priv = strbuf_new_nm();
  1172. ssh_key_private_blob(state->ssh2key.key, BinarySink_UPCAST(priv));
  1173. ssh_key *newkey = ssh_key_new_priv(
  1174. alg, ptrlen_from_strbuf(pub), ptrlen_from_strbuf(priv));
  1175. strbuf_free(pub);
  1176. strbuf_free(priv);
  1177. if (!newkey) {
  1178. char *msg = dupprintf("Couldn't combine certificate with key");
  1179. message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
  1180. false, HELPCTXID(errors_cantloadkey));
  1181. sfree(msg);
  1182. return;
  1183. }
  1184. ssh_key_free(state->ssh2key.key);
  1185. state->ssh2key.key = newkey;
  1186. update_ui_after_ssh2_pubkey_change(hwnd, state);
  1187. ui_set_state(hwnd, state, 2);
  1188. }
  1189. void remove_certificate(HWND hwnd, struct MainDlgState *state)
  1190. {
  1191. ssh_key *newkey = ssh_key_clone(ssh_key_base_key(state->ssh2key.key));
  1192. ssh_key_free(state->ssh2key.key);
  1193. state->ssh2key.key = newkey;
  1194. update_ui_after_ssh2_pubkey_change(hwnd, state);
  1195. ui_set_state(hwnd, state, 2);
  1196. }
  1197. static void start_generating_key(HWND hwnd, struct MainDlgState *state)
  1198. {
  1199. static const char generating_msg[] =
  1200. "Please wait while a key is generated...";
  1201. struct rsa_key_thread_params *params;
  1202. DWORD threadid;
  1203. SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
  1204. SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
  1205. MAKELPARAM(0, PROGRESSRANGE));
  1206. SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
  1207. params = snew(struct rsa_key_thread_params);
  1208. params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
  1209. params->dialog = hwnd;
  1210. params->key_bits = state->key_bits;
  1211. params->curve_bits = state->curve_bits;
  1212. params->keytype = state->keytype;
  1213. params->primepolicy = state->primepolicy;
  1214. params->rsa_strong = state->rsa_strong;
  1215. params->key = &state->key;
  1216. params->dsakey = &state->dsakey;
  1217. HANDLE hThread = CreateThread(NULL, 0, generate_key_thread,
  1218. params, 0, &threadid);
  1219. if (!hThread) {
  1220. MessageBox(hwnd, "Out of thread resources",
  1221. "Key generation error",
  1222. MB_OK | MB_ICONERROR);
  1223. sfree(params);
  1224. } else {
  1225. CloseHandle(hThread); /* we don't need the thread handle */
  1226. state->generation_thread_exists = true;
  1227. }
  1228. }
  1229. /*
  1230. * Dialog-box function and context structure for the 'Certificate
  1231. * info' button.
  1232. */
  1233. struct certinfo_dialog_ctx {
  1234. SeatDialogText *text;
  1235. };
  1236. static INT_PTR CertInfoProc(HWND hwnd, UINT msg, WPARAM wParam,
  1237. LPARAM lParam, void *vctx)
  1238. {
  1239. struct certinfo_dialog_ctx *ctx = (struct certinfo_dialog_ctx *)vctx;
  1240. switch (msg) {
  1241. case WM_INITDIALOG: {
  1242. int index = 100, y = 12;
  1243. WPARAM font = SendMessage(hwnd, WM_GETFONT, 0, 0);
  1244. const char *key = NULL;
  1245. for (SeatDialogTextItem *item = ctx->text->items,
  1246. *end = item + ctx->text->nitems; item < end; item++) {
  1247. switch (item->type) {
  1248. case SDT_MORE_INFO_KEY:
  1249. key = item->text;
  1250. break;
  1251. case SDT_MORE_INFO_VALUE_SHORT:
  1252. case SDT_MORE_INFO_VALUE_BLOB: {
  1253. RECT rk, rv;
  1254. DWORD editstyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP |
  1255. ES_AUTOHSCROLL | ES_READONLY;
  1256. if (item->type == SDT_MORE_INFO_VALUE_BLOB) {
  1257. rk.left = 12;
  1258. rk.right = 286;
  1259. rk.top = y;
  1260. rk.bottom = 8;
  1261. y += 10;
  1262. editstyle |= ES_MULTILINE;
  1263. rv.left = 12;
  1264. rv.right = 286;
  1265. rv.top = y;
  1266. rv.bottom = 64;
  1267. y += 68;
  1268. } else {
  1269. rk.left = 12;
  1270. rk.right = 130;
  1271. rk.top = y+2;
  1272. rk.bottom = 8;
  1273. rv.left = 150;
  1274. rv.right = 298;
  1275. rv.top = y;
  1276. rv.bottom = 12;
  1277. y += 16;
  1278. }
  1279. MapDialogRect(hwnd, &rk);
  1280. HWND ctl = CreateWindowEx(
  1281. 0, "STATIC", key, WS_CHILD | WS_VISIBLE,
  1282. rk.left, rk.top, rk.right, rk.bottom,
  1283. hwnd, (HMENU)(ULONG_PTR)index++, hinst, NULL);
  1284. SendMessage(ctl, WM_SETFONT, font, MAKELPARAM(true, 0));
  1285. MapDialogRect(hwnd, &rv);
  1286. ctl = CreateWindowEx(
  1287. WS_EX_CLIENTEDGE, "EDIT", item->text, editstyle,
  1288. rv.left, rv.top, rv.right, rv.bottom,
  1289. hwnd, (HMENU)(ULONG_PTR)index++, hinst, NULL);
  1290. SendMessage(ctl, WM_SETFONT, font, MAKELPARAM(true, 0));
  1291. break;
  1292. }
  1293. default:
  1294. break;
  1295. }
  1296. }
  1297. /*
  1298. * Now resize the overall window, and move the Close button at
  1299. * the bottom.
  1300. */
  1301. RECT r;
  1302. r.left = 176;
  1303. r.top = y + 10;
  1304. r.right = r.bottom = 0;
  1305. MapDialogRect(hwnd, &r);
  1306. HWND ctl = GetDlgItem(hwnd, IDOK);
  1307. SetWindowPos(ctl, NULL, r.left, r.top, 0, 0,
  1308. SWP_NOSIZE | SWP_NOREDRAW | SWP_NOZORDER);
  1309. r.left = r.top = r.right = 0;
  1310. r.bottom = 300;
  1311. MapDialogRect(hwnd, &r);
  1312. int oldheight = r.bottom;
  1313. r.left = r.top = r.right = 0;
  1314. r.bottom = y + 30;
  1315. MapDialogRect(hwnd, &r);
  1316. int newheight = r.bottom;
  1317. GetWindowRect(hwnd, &r);
  1318. SetWindowPos(hwnd, NULL, 0, 0, r.right - r.left,
  1319. r.bottom - r.top + newheight - oldheight,
  1320. SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
  1321. ShowWindow(hwnd, SW_SHOWNORMAL);
  1322. return 1;
  1323. }
  1324. case WM_COMMAND:
  1325. switch (LOWORD(wParam)) {
  1326. case IDOK:
  1327. ShinyEndDialog(hwnd, 0);
  1328. return 0;
  1329. }
  1330. return 0;
  1331. case WM_CLOSE:
  1332. ShinyEndDialog(hwnd, 0);
  1333. return 0;
  1334. }
  1335. return 0;
  1336. }
  1337. /*
  1338. * Dialog-box function for the main PuTTYgen dialog box.
  1339. */
  1340. static INT_PTR CALLBACK MainDlgProc(HWND hwnd, UINT msg,
  1341. WPARAM wParam, LPARAM lParam)
  1342. {
  1343. const int DEMO_SCREENSHOT_TIMER_ID = 1230;
  1344. static const char entropy_msg[] =
  1345. "Please generate some randomness by moving the mouse over the blank area.";
  1346. struct MainDlgState *state;
  1347. switch (msg) {
  1348. case WM_INITDIALOG:
  1349. if (has_help())
  1350. SetWindowLongPtr(hwnd, GWL_EXSTYLE,
  1351. GetWindowLongPtr(hwnd, GWL_EXSTYLE) |
  1352. WS_EX_CONTEXTHELP);
  1353. else {
  1354. /*
  1355. * If we add a Help button, this is where we destroy it
  1356. * if the help file isn't present.
  1357. */
  1358. }
  1359. SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,
  1360. (LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(200)));
  1361. state = snew(struct MainDlgState);
  1362. state->generation_thread_exists = false;
  1363. state->entropy = NULL;
  1364. state->key_exists = false;
  1365. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) state);
  1366. {
  1367. HMENU menu, menu1;
  1368. menu = CreateMenu();
  1369. menu1 = CreateMenu();
  1370. AppendMenu(menu1, MF_ENABLED, IDC_LOAD, "&Load private key");
  1371. AppendMenu(menu1, MF_ENABLED, IDC_SAVEPUB, "Save p&ublic key");
  1372. AppendMenu(menu1, MF_ENABLED, IDC_SAVE, "&Save private key");
  1373. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1374. AppendMenu(menu1, MF_ENABLED, IDC_QUIT, "E&xit");
  1375. AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1, "&File");
  1376. state->filemenu = menu1;
  1377. menu1 = CreateMenu();
  1378. AppendMenu(menu1, MF_ENABLED, IDC_GENERATE, "&Generate key pair");
  1379. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1380. AppendMenu(menu1, MF_ENABLED, IDC_ADDCERT,
  1381. "Add &certificate to key");
  1382. AppendMenu(menu1, MF_ENABLED, IDC_REMCERT,
  1383. "Remove certificate from key");
  1384. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1385. AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH1, "SSH-&1 key (RSA)");
  1386. AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2RSA, "SSH-2 &RSA key");
  1387. AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2DSA, "SSH-2 &DSA key");
  1388. AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2ECDSA, "SSH-2 &ECDSA key");
  1389. AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2EDDSA, "SSH-2 EdD&SA key");
  1390. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1391. AppendMenu(menu1, MF_ENABLED, IDC_PRIMEGEN_PROB,
  1392. "Use probable primes (fast)");
  1393. AppendMenu(menu1, MF_ENABLED, IDC_PRIMEGEN_MAURER_SIMPLE,
  1394. "Use proven primes (slower)");
  1395. AppendMenu(menu1, MF_ENABLED, IDC_PRIMEGEN_MAURER_COMPLEX,
  1396. "Use proven primes with even distribution (slowest)");
  1397. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1398. AppendMenu(menu1, MF_ENABLED, IDC_RSA_STRONG,
  1399. "Use \"strong\" primes as RSA key factors");
  1400. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1401. AppendMenu(menu1, MF_ENABLED, IDC_PPK_PARAMS,
  1402. "Parameters for saving key files...");
  1403. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1404. AppendMenu(menu1, MF_ENABLED, IDC_FPTYPE_SHA256,
  1405. "Show fingerprint as SHA256");
  1406. AppendMenu(menu1, MF_ENABLED, IDC_FPTYPE_MD5,
  1407. "Show fingerprint as MD5");
  1408. AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1, "&Key");
  1409. state->keymenu = menu1;
  1410. menu1 = CreateMenu();
  1411. AppendMenu(menu1, MF_ENABLED, IDC_IMPORT, "&Import key");
  1412. AppendMenu(menu1, MF_SEPARATOR, 0, 0);
  1413. AppendMenu(menu1, MF_ENABLED, IDC_EXPORT_OPENSSH_AUTO,
  1414. "Export &OpenSSH key");
  1415. AppendMenu(menu1, MF_ENABLED, IDC_EXPORT_OPENSSH_NEW,
  1416. "Export &OpenSSH key (force new file format)");
  1417. AppendMenu(menu1, MF_ENABLED, IDC_EXPORT_SSHCOM,
  1418. "Export &ssh.com key");
  1419. AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1,
  1420. "Con&versions");
  1421. state->cvtmenu = menu1;
  1422. menu1 = CreateMenu();
  1423. AppendMenu(menu1, MF_ENABLED, IDC_ABOUT, "&About");
  1424. if (has_help())
  1425. AppendMenu(menu1, MF_ENABLED, IDC_GIVEHELP, "&Help");
  1426. AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1, "&Help");
  1427. SetMenu(hwnd, menu);
  1428. }
  1429. /*
  1430. * Centre the window.
  1431. */
  1432. { /* centre the window */
  1433. RECT rs, rd;
  1434. HWND hw;
  1435. hw = GetDesktopWindow();
  1436. if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
  1437. MoveWindow(hwnd,
  1438. (rs.right + rs.left + rd.left - rd.right) / 2,
  1439. (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
  1440. rd.right - rd.left, rd.bottom - rd.top, true);
  1441. }
  1442. {
  1443. struct ctlpos cp, cp2;
  1444. int ymax;
  1445. /* Accelerators used: acglops1rbvde */
  1446. ctlposinit(&cp, hwnd, 4, 4, 4);
  1447. beginbox(&cp, "Key", IDC_BOX_KEY);
  1448. cp2 = cp;
  1449. statictext(&cp2, "No key.", 1, IDC_NOKEY);
  1450. cp2 = cp;
  1451. statictext(&cp2, "", 1, IDC_GENERATING);
  1452. progressbar(&cp2, IDC_PROGRESS);
  1453. cp2 = cp;
  1454. bigeditctrl(&cp2, NULL, -1, IDC_CERTSTATIC, 3);
  1455. {
  1456. HWND child = GetDlgItem(hwnd, IDC_CERTSTATIC);
  1457. LONG_PTR style = GetWindowLongPtr(child, GWL_STYLE);
  1458. style &= ~WS_VSCROLL;
  1459. SetWindowLongPtr(child, GWL_STYLE, style);
  1460. SendMessage(child, EM_SETREADONLY, true, 0);
  1461. }
  1462. MakeDlgItemBorderless(hwnd, IDC_CERTSTATIC);
  1463. cp2.xoff = cp2.width = cp2.width / 3;
  1464. button(&cp2, "Certificate info...", IDC_CERTMOREINFO, false);
  1465. bigeditctrl(&cp,
  1466. "&Public key for pasting into authorized_keys file:",
  1467. IDC_PKSTATIC, IDC_KEYDISPLAY, 5);
  1468. SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
  1469. staticedit(&cp, "Key f&ingerprint:", IDC_FPSTATIC,
  1470. IDC_FINGERPRINT, 82);
  1471. SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1,
  1472. 0);
  1473. staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
  1474. IDC_COMMENTEDIT, 82);
  1475. staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
  1476. IDC_PASSPHRASE1EDIT, 82);
  1477. staticpassedit(&cp, "C&onfirm passphrase:",
  1478. IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 82);
  1479. endbox(&cp);
  1480. beginbox(&cp, "Actions", IDC_BOX_ACTIONS);
  1481. staticbtn(&cp, "Generate a public/private key pair",
  1482. IDC_GENSTATIC, "&Generate", IDC_GENERATE);
  1483. staticbtn(&cp, "Load an existing private key file",
  1484. IDC_LOADSTATIC, "&Load", IDC_LOAD);
  1485. static2btn(&cp, "Save the generated key", IDC_SAVESTATIC,
  1486. "Save p&ublic key", IDC_SAVEPUB,
  1487. "&Save private key", IDC_SAVE);
  1488. endbox(&cp);
  1489. beginbox(&cp, "Parameters", IDC_BOX_PARAMS);
  1490. radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 5,
  1491. "&RSA", IDC_KEYSSH2RSA,
  1492. "&DSA", IDC_KEYSSH2DSA,
  1493. "&ECDSA", IDC_KEYSSH2ECDSA,
  1494. "EdD&SA", IDC_KEYSSH2EDDSA,
  1495. "SSH-&1 (RSA)", IDC_KEYSSH1,
  1496. NULL);
  1497. cp2 = cp;
  1498. staticedit(&cp2, "Number of &bits in a generated key:",
  1499. IDC_BITSSTATIC, IDC_BITS, 20);
  1500. ymax = cp2.ypos;
  1501. cp2 = cp;
  1502. staticddl(&cp2, "Cur&ve to use for generating this key:",
  1503. IDC_ECCURVESTATIC, IDC_ECCURVE, 30);
  1504. SendDlgItemMessage(hwnd, IDC_ECCURVE, CB_RESETCONTENT, 0, 0);
  1505. {
  1506. int i, bits;
  1507. const struct ec_curve *curve;
  1508. const ssh_keyalg *alg;
  1509. for (i = 0; i < n_ec_nist_curve_lengths; i++) {
  1510. bits = ec_nist_curve_lengths[i];
  1511. ec_nist_alg_and_curve_by_bits(bits, &curve, &alg);
  1512. SendDlgItemMessage(hwnd, IDC_ECCURVE, CB_ADDSTRING, 0,
  1513. (LPARAM)curve->textname);
  1514. }
  1515. }
  1516. ymax = ymax > cp2.ypos ? ymax : cp2.ypos;
  1517. cp2 = cp;
  1518. staticddl(&cp2, "Cur&ve to use for generating this key:",
  1519. IDC_EDCURVESTATIC, IDC_EDCURVE, 30);
  1520. SendDlgItemMessage(hwnd, IDC_EDCURVE, CB_RESETCONTENT, 0, 0);
  1521. {
  1522. int i, bits;
  1523. const struct ec_curve *curve;
  1524. const ssh_keyalg *alg;
  1525. for (i = 0; i < n_ec_ed_curve_lengths; i++) {
  1526. bits = ec_ed_curve_lengths[i];
  1527. ec_ed_alg_and_curve_by_bits(bits, &curve, &alg);
  1528. char *desc = dupprintf("%s (%d bits)",
  1529. curve->textname, bits);
  1530. SendDlgItemMessage(hwnd, IDC_EDCURVE, CB_ADDSTRING, 0,
  1531. (LPARAM)desc);
  1532. sfree(desc);
  1533. }
  1534. }
  1535. ymax = ymax > cp2.ypos ? ymax : cp2.ypos;
  1536. cp2 = cp;
  1537. statictext(&cp2, "(nothing to configure for this key type)",
  1538. 1, IDC_NOTHINGSTATIC);
  1539. ymax = ymax > cp2.ypos ? ymax : cp2.ypos;
  1540. cp.ypos = ymax;
  1541. endbox(&cp);
  1542. }
  1543. struct InitialParams *params = (struct InitialParams *)lParam;
  1544. ui_set_key_type(hwnd, state, params->keybutton);
  1545. ui_set_primepolicy(hwnd, state, params->primepolicybutton);
  1546. ui_set_rsa_strong(hwnd, state, params->rsa_strong);
  1547. ui_set_fptype(hwnd, state, fptype_to_idc(params->fptype));
  1548. SetDlgItemInt(hwnd, IDC_BITS, params->keybits, false);
  1549. SendDlgItemMessage(hwnd, IDC_ECCURVE, CB_SETCURSEL,
  1550. params->eccurve_index, 0);
  1551. SendDlgItemMessage(hwnd, IDC_EDCURVE, CB_SETCURSEL,
  1552. params->edcurve_index, 0);
  1553. /*
  1554. * Initially, hide the progress bar and the key display,
  1555. * and show the no-key display. Also disable the Save
  1556. * buttons, because with no key we obviously can't save
  1557. * anything.
  1558. */
  1559. ui_set_state(hwnd, state, 0);
  1560. /*
  1561. * Load a key file if one was provided on the command line.
  1562. */
  1563. if (cmdline_keyfile) {
  1564. Filename *fn = filename_from_str(cmdline_keyfile);
  1565. load_key_file(hwnd, state, fn, false);
  1566. filename_free(fn);
  1567. } else if (cmdline_demo_keystr.ptr) {
  1568. BinarySource src[1];
  1569. BinarySource_BARE_INIT_PL(src, cmdline_demo_keystr);
  1570. const char *errmsg;
  1571. ssh2_userkey *k = ppk_load_s(src, NULL, &errmsg);
  1572. assert(!errmsg);
  1573. update_ui_after_load(hwnd, state, "demo passphrase",
  1574. SSH_KEYTYPE_SSH2, NULL, k);
  1575. SetTimer(hwnd, DEMO_SCREENSHOT_TIMER_ID, TICKSPERSEC, NULL);
  1576. }
  1577. return 1;
  1578. case WM_TIMER:
  1579. if ((UINT_PTR)wParam == DEMO_SCREENSHOT_TIMER_ID) {
  1580. KillTimer(hwnd, DEMO_SCREENSHOT_TIMER_ID);
  1581. char *err = save_screenshot(hwnd, demo_screenshot_filename);
  1582. if (err) {
  1583. MessageBox(hwnd, err, "Demo screenshot failure",
  1584. MB_OK | MB_ICONERROR);
  1585. sfree(err);
  1586. }
  1587. EndDialog(hwnd, 0);
  1588. }
  1589. return 0;
  1590. case WM_MOUSEMOVE:
  1591. state = (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1592. if (state->entropy && state->entropy_got < state->entropy_required) {
  1593. ULONG msgtime = GetMessageTime();
  1594. put_uint32(state->entropy, lParam);
  1595. put_uint32(state->entropy, msgtime);
  1596. if (msgtime - state->entropy_prev_msgtime > ENTROPY_RATE_LIMIT) {
  1597. state->entropy_got += 2;
  1598. state->entropy_prev_msgtime = msgtime;
  1599. }
  1600. SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
  1601. state->entropy_got, 0);
  1602. if (state->entropy_got >= state->entropy_required) {
  1603. /*
  1604. * Seed the entropy pool
  1605. */
  1606. random_reseed(ptrlen_from_strbuf(state->entropy));
  1607. strbuf_free(state->entropy);
  1608. state->entropy = NULL;
  1609. start_generating_key(hwnd, state);
  1610. }
  1611. }
  1612. break;
  1613. case WM_COMMAND:
  1614. switch (LOWORD(wParam)) {
  1615. case IDC_KEYSSH1:
  1616. case IDC_KEYSSH2RSA:
  1617. case IDC_KEYSSH2DSA:
  1618. case IDC_KEYSSH2ECDSA:
  1619. case IDC_KEYSSH2EDDSA: {
  1620. state = (struct MainDlgState *)
  1621. GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1622. ui_set_key_type(hwnd, state, LOWORD(wParam));
  1623. break;
  1624. }
  1625. case IDC_PRIMEGEN_PROB:
  1626. case IDC_PRIMEGEN_MAURER_SIMPLE:
  1627. case IDC_PRIMEGEN_MAURER_COMPLEX: {
  1628. state = (struct MainDlgState *)
  1629. GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1630. ui_set_primepolicy(hwnd, state, LOWORD(wParam));
  1631. break;
  1632. }
  1633. case IDC_FPTYPE_SHA256:
  1634. case IDC_FPTYPE_MD5: {
  1635. state = (struct MainDlgState *)
  1636. GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1637. ui_set_fptype(hwnd, state, LOWORD(wParam));
  1638. break;
  1639. }
  1640. case IDC_RSA_STRONG: {
  1641. state = (struct MainDlgState *)
  1642. GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1643. ui_set_rsa_strong(hwnd, state, !state->rsa_strong);
  1644. break;
  1645. }
  1646. case IDC_PPK_PARAMS: {
  1647. struct PPKParams pp[1];
  1648. pp->params = save_params;
  1649. if (pp->params.argon2_passes_auto) {
  1650. pp->time_ms = pp->params.argon2_milliseconds;
  1651. pp->time_passes = 13;
  1652. } else {
  1653. pp->time_ms = 100;
  1654. pp->time_passes = pp->params.argon2_passes;
  1655. }
  1656. int dlgret = DialogBoxParam(hinst, MAKEINTRESOURCE(215),
  1657. NULL, PPKParamsProc, (LPARAM)pp);
  1658. if (dlgret) {
  1659. if (pp->params.argon2_passes_auto) {
  1660. pp->params.argon2_milliseconds = pp->time_ms;
  1661. } else {
  1662. pp->params.argon2_passes = pp->time_passes;
  1663. }
  1664. save_params = pp->params;
  1665. }
  1666. break;
  1667. }
  1668. case IDC_QUIT:
  1669. PostMessage(hwnd, WM_CLOSE, 0, 0);
  1670. break;
  1671. case IDC_COMMENTEDIT:
  1672. if (HIWORD(wParam) == EN_CHANGE) {
  1673. state = (struct MainDlgState *)
  1674. GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1675. if (state->key_exists) {
  1676. HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
  1677. int len = GetWindowTextLength(editctl);
  1678. if (*state->commentptr)
  1679. sfree(*state->commentptr);
  1680. *state->commentptr = snewn(len + 1, char);
  1681. GetWindowText(editctl, *state->commentptr, len + 1);
  1682. if (state->ssh2) {
  1683. setupbigedit2(hwnd, &state->ssh2key);
  1684. } else {
  1685. setupbigedit1(hwnd, &state->key);
  1686. }
  1687. }
  1688. }
  1689. break;
  1690. case IDC_ABOUT:
  1691. EnableWindow(hwnd, 0);
  1692. DialogBox(hinst, MAKEINTRESOURCE(213), hwnd, AboutProc);
  1693. EnableWindow(hwnd, 1);
  1694. SetActiveWindow(hwnd);
  1695. return 0;
  1696. case IDC_GIVEHELP:
  1697. if (HIWORD(wParam) == BN_CLICKED ||
  1698. HIWORD(wParam) == BN_DOUBLECLICKED) {
  1699. launch_help(hwnd, WINHELP_CTX_puttygen_general);
  1700. }
  1701. return 0;
  1702. case IDC_GENERATE:
  1703. if (HIWORD(wParam) != BN_CLICKED &&
  1704. HIWORD(wParam) != BN_DOUBLECLICKED)
  1705. break;
  1706. state =
  1707. (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1708. if (!state->generation_thread_exists) {
  1709. unsigned raw_entropy_required;
  1710. unsigned char *raw_entropy_buf;
  1711. BOOL ok;
  1712. state->key_bits = GetDlgItemInt(hwnd, IDC_BITS, &ok, false);
  1713. if (!ok)
  1714. state->key_bits = DEFAULT_KEY_BITS;
  1715. state->ssh2 = true;
  1716. if (IsDlgButtonChecked(hwnd, IDC_KEYSSH1)) {
  1717. state->ssh2 = false;
  1718. state->keytype = RSA;
  1719. } else if (IsDlgButtonChecked(hwnd, IDC_KEYSSH2RSA)) {
  1720. state->keytype = RSA;
  1721. } else if (IsDlgButtonChecked(hwnd, IDC_KEYSSH2DSA)) {
  1722. state->keytype = DSA;
  1723. } else if (IsDlgButtonChecked(hwnd, IDC_KEYSSH2ECDSA)) {
  1724. state->keytype = ECDSA;
  1725. int curveindex = SendDlgItemMessage(hwnd, IDC_ECCURVE,
  1726. CB_GETCURSEL, 0, 0);
  1727. assert(curveindex >= 0);
  1728. assert(curveindex < n_ec_nist_curve_lengths);
  1729. state->curve_bits = ec_nist_curve_lengths[curveindex];
  1730. } else if (IsDlgButtonChecked(hwnd, IDC_KEYSSH2EDDSA)) {
  1731. state->keytype = EDDSA;
  1732. int curveindex = SendDlgItemMessage(hwnd, IDC_EDCURVE,
  1733. CB_GETCURSEL, 0, 0);
  1734. assert(curveindex >= 0);
  1735. assert(curveindex < n_ec_ed_curve_lengths);
  1736. state->curve_bits = ec_ed_curve_lengths[curveindex];
  1737. } else {
  1738. /* Somehow, no button was checked */
  1739. break;
  1740. }
  1741. if ((state->keytype == RSA || state->keytype == DSA) &&
  1742. state->key_bits < 256) {
  1743. char *message = dupprintf(
  1744. "PuTTYgen will not generate a key smaller than 256"
  1745. " bits.\nKey length reset to default %d. Continue?",
  1746. DEFAULT_KEY_BITS);
  1747. int ret = MessageBox(hwnd, message, "PuTTYgen Warning",
  1748. MB_ICONWARNING | MB_OKCANCEL);
  1749. sfree(message);
  1750. if (ret != IDOK)
  1751. break;
  1752. state->key_bits = DEFAULT_KEY_BITS;
  1753. SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEY_BITS, false);
  1754. } else if ((state->keytype == RSA || state->keytype == DSA) &&
  1755. state->key_bits < DEFAULT_KEY_BITS) {
  1756. char *message = dupprintf(
  1757. "Keys shorter than %d bits are not recommended. "
  1758. "Really generate this key?", DEFAULT_KEY_BITS);
  1759. int ret = MessageBox(hwnd, message, "PuTTYgen Warning",
  1760. MB_ICONWARNING | MB_OKCANCEL);
  1761. sfree(message);
  1762. if (ret != IDOK)
  1763. break;
  1764. }
  1765. if (state->keytype == RSA || state->keytype == DSA)
  1766. raw_entropy_required = (state->key_bits / 2) * 2;
  1767. else if (state->keytype == ECDSA || state->keytype == EDDSA)
  1768. raw_entropy_required = (state->curve_bits / 2) * 2;
  1769. else
  1770. unreachable("we must have initialised keytype by now");
  1771. /* Bound the entropy collection above by the amount of
  1772. * data we can actually fit into the PRNG. Any more
  1773. * than that and it's doing no more good. */
  1774. if (raw_entropy_required > random_seed_bits())
  1775. raw_entropy_required = random_seed_bits();
  1776. raw_entropy_buf = snewn(raw_entropy_required, unsigned char);
  1777. if (win_read_random(raw_entropy_buf, raw_entropy_required)) {
  1778. /*
  1779. * If we can get entropy from CryptGenRandom, use
  1780. * it. But CryptGenRandom isn't a kernel-level
  1781. * CPRNG (according to Wikipedia), and papers have
  1782. * been published cryptanalysing it. So we'll
  1783. * still do manual entropy collection; we'll just
  1784. * do it _as well_ as this.
  1785. */
  1786. random_reseed(
  1787. make_ptrlen(raw_entropy_buf, raw_entropy_required));
  1788. }
  1789. /*
  1790. * Manual entropy input, by making the user wave the
  1791. * mouse over the window a lot.
  1792. *
  1793. * My brief statistical tests on mouse movements
  1794. * suggest that there are about 2.5 bits of randomness
  1795. * in the x position, 2.5 in the y position, and 1.7
  1796. * in the message time, making 5.7 bits of
  1797. * unpredictability per mouse movement. However, other
  1798. * people have told me it's far less than that, so I'm
  1799. * going to be stupidly cautious and knock that down
  1800. * to a nice round 2. With this method, we require two
  1801. * words per mouse movement, so with 2 bits per mouse
  1802. * movement we expect 2 bits every 2 words, i.e. the
  1803. * number of _words_ of mouse data we want to collect
  1804. * is just the same as the number of _bits_ of entropy
  1805. * we want.
  1806. */
  1807. state->entropy_required = raw_entropy_required;
  1808. ui_set_state(hwnd, state, 1);
  1809. SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
  1810. state->key_exists = false;
  1811. state->entropy_got = 0;
  1812. state->entropy = strbuf_new_nm();
  1813. state->entropy_prev_msgtime = GetMessageTime();
  1814. SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
  1815. MAKELPARAM(0, state->entropy_required));
  1816. SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
  1817. smemclr(raw_entropy_buf, raw_entropy_required);
  1818. sfree(raw_entropy_buf);
  1819. }
  1820. break;
  1821. case IDC_SAVE:
  1822. case IDC_EXPORT_OPENSSH_AUTO:
  1823. case IDC_EXPORT_OPENSSH_NEW:
  1824. case IDC_EXPORT_SSHCOM:
  1825. if (HIWORD(wParam) != BN_CLICKED)
  1826. break;
  1827. state =
  1828. (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1829. if (state->key_exists) {
  1830. char filename[FILENAME_MAX];
  1831. char *passphrase, *passphrase2;
  1832. int type, realtype;
  1833. if (state->ssh2)
  1834. realtype = SSH_KEYTYPE_SSH2;
  1835. else
  1836. realtype = SSH_KEYTYPE_SSH1;
  1837. if (LOWORD(wParam) == IDC_EXPORT_OPENSSH_AUTO)
  1838. type = SSH_KEYTYPE_OPENSSH_AUTO;
  1839. else if (LOWORD(wParam) == IDC_EXPORT_OPENSSH_NEW)
  1840. type = SSH_KEYTYPE_OPENSSH_NEW;
  1841. else if (LOWORD(wParam) == IDC_EXPORT_SSHCOM)
  1842. type = SSH_KEYTYPE_SSHCOM;
  1843. else
  1844. type = realtype;
  1845. if (type != realtype &&
  1846. import_target_type(type) != realtype) {
  1847. char msg[256];
  1848. sprintf(msg, "Cannot export an SSH-%d key in an SSH-%d"
  1849. " format", (state->ssh2 ? 2 : 1),
  1850. (state->ssh2 ? 1 : 2));
  1851. MessageBox(hwnd, msg,
  1852. "PuTTYgen Error", MB_OK | MB_ICONERROR);
  1853. break;
  1854. }
  1855. passphrase = GetDlgItemText_alloc(hwnd, IDC_PASSPHRASE1EDIT);
  1856. passphrase2 = GetDlgItemText_alloc(hwnd, IDC_PASSPHRASE2EDIT);
  1857. if (strcmp(passphrase, passphrase2)) {
  1858. MessageBox(hwnd,
  1859. "The two passphrases given do not match.",
  1860. "PuTTYgen Error", MB_OK | MB_ICONERROR);
  1861. burnstr(passphrase);
  1862. burnstr(passphrase2);
  1863. break;
  1864. }
  1865. burnstr(passphrase2);
  1866. if (!*passphrase) {
  1867. int ret;
  1868. ret = MessageBox(hwnd,
  1869. "Are you sure you want to save this key\n"
  1870. "without a passphrase to protect it?",
  1871. "PuTTYgen Warning",
  1872. MB_YESNO | MB_ICONWARNING);
  1873. if (ret != IDYES) {
  1874. burnstr(passphrase);
  1875. break;
  1876. }
  1877. }
  1878. if (prompt_keyfile(hwnd, "Save private key as:",
  1879. filename, true, (type == realtype))) {
  1880. int ret;
  1881. FILE *fp = fopen(filename, "r");
  1882. if (fp) {
  1883. char *buffer;
  1884. fclose(fp);
  1885. buffer = dupprintf("Overwrite existing file\n%s?",
  1886. filename);
  1887. ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
  1888. MB_YESNO | MB_ICONWARNING);
  1889. sfree(buffer);
  1890. if (ret != IDYES) {
  1891. burnstr(passphrase);
  1892. break;
  1893. }
  1894. }
  1895. if (state->ssh2) {
  1896. Filename *fn = filename_from_str(filename);
  1897. if (type != realtype)
  1898. ret = export_ssh2(fn, type, &state->ssh2key,
  1899. *passphrase ? passphrase : NULL);
  1900. else
  1901. ret = ppk_save_f(fn, &state->ssh2key,
  1902. *passphrase ? passphrase : NULL,
  1903. &save_params);
  1904. filename_free(fn);
  1905. } else {
  1906. Filename *fn = filename_from_str(filename);
  1907. if (type != realtype)
  1908. ret = export_ssh1(fn, type, &state->key,
  1909. *passphrase ? passphrase : NULL);
  1910. else
  1911. ret = rsa1_save_f(fn, &state->key,
  1912. *passphrase ? passphrase : NULL);
  1913. filename_free(fn);
  1914. }
  1915. if (ret <= 0) {
  1916. MessageBox(hwnd, "Unable to save key file",
  1917. "PuTTYgen Error", MB_OK | MB_ICONERROR);
  1918. }
  1919. }
  1920. burnstr(passphrase);
  1921. }
  1922. break;
  1923. case IDC_SAVEPUB:
  1924. if (HIWORD(wParam) != BN_CLICKED)
  1925. break;
  1926. state =
  1927. (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1928. if (state->key_exists) {
  1929. char filename[FILENAME_MAX];
  1930. if (prompt_keyfile(hwnd, "Save public key as:",
  1931. filename, true, false)) {
  1932. int ret;
  1933. FILE *fp = fopen(filename, "r");
  1934. if (fp) {
  1935. char *buffer;
  1936. fclose(fp);
  1937. buffer = dupprintf("Overwrite existing file\n%s?",
  1938. filename);
  1939. ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
  1940. MB_YESNO | MB_ICONWARNING);
  1941. sfree(buffer);
  1942. if (ret != IDYES)
  1943. break;
  1944. }
  1945. fp = fopen(filename, "w");
  1946. if (!fp) {
  1947. MessageBox(hwnd, "Unable to open key file",
  1948. "PuTTYgen Error", MB_OK | MB_ICONERROR);
  1949. } else {
  1950. if (state->ssh2) {
  1951. strbuf *blob = strbuf_new();
  1952. ssh_key_public_blob(
  1953. state->ssh2key.key, BinarySink_UPCAST(blob));
  1954. ssh2_write_pubkey(fp, state->ssh2key.comment,
  1955. blob->u, blob->len,
  1956. SSH_KEYTYPE_SSH2_PUBLIC_RFC4716);
  1957. strbuf_free(blob);
  1958. } else {
  1959. ssh1_write_pubkey(fp, &state->key);
  1960. }
  1961. if (fclose(fp) < 0) {
  1962. MessageBox(hwnd, "Unable to save key file",
  1963. "PuTTYgen Error", MB_OK | MB_ICONERROR);
  1964. }
  1965. }
  1966. }
  1967. }
  1968. break;
  1969. case IDC_LOAD:
  1970. case IDC_IMPORT:
  1971. if (HIWORD(wParam) != BN_CLICKED)
  1972. break;
  1973. state =
  1974. (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1975. if (!state->generation_thread_exists) {
  1976. char filename[FILENAME_MAX];
  1977. if (prompt_keyfile(hwnd, "Load private key:", filename, false,
  1978. LOWORD(wParam) == IDC_LOAD)) {
  1979. Filename *fn = filename_from_str(filename);
  1980. load_key_file(hwnd, state, fn, LOWORD(wParam) != IDC_LOAD);
  1981. filename_free(fn);
  1982. }
  1983. }
  1984. break;
  1985. case IDC_ADDCERT:
  1986. if (HIWORD(wParam) != BN_CLICKED)
  1987. break;
  1988. state =
  1989. (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1990. if (state->key_exists && !state->generation_thread_exists) {
  1991. char filename[FILENAME_MAX];
  1992. if (prompt_keyfile(hwnd, "Load certificate:", filename, false,
  1993. false)) {
  1994. Filename *fn = filename_from_str(filename);
  1995. add_certificate(hwnd, state, fn);
  1996. filename_free(fn);
  1997. }
  1998. }
  1999. break;
  2000. case IDC_REMCERT:
  2001. if (HIWORD(wParam) != BN_CLICKED)
  2002. break;
  2003. state =
  2004. (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  2005. if (state->key_exists && !state->generation_thread_exists) {
  2006. remove_certificate(hwnd, state);
  2007. }
  2008. break;
  2009. case IDC_CERTMOREINFO: {
  2010. if (HIWORD(wParam) != BN_CLICKED)
  2011. break;
  2012. state =
  2013. (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  2014. if (!state->key_exists || !state->ssh2 || !state->ssh2key.key)
  2015. break;
  2016. if (!ssh_key_alg(state->ssh2key.key)->is_certificate)
  2017. break;
  2018. struct certinfo_dialog_ctx ctx[1];
  2019. ctx->text = ssh_key_cert_info(state->ssh2key.key);
  2020. ShinyDialogBox(hinst, MAKEINTRESOURCE(216),
  2021. "PuTTYgenCertInfo", hwnd, CertInfoProc, ctx);
  2022. seat_dialog_text_free(ctx->text);
  2023. break;
  2024. }
  2025. }
  2026. return 0;
  2027. case WM_DONEKEY:
  2028. state = (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  2029. state->generation_thread_exists = false;
  2030. state->key_exists = true;
  2031. SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
  2032. MAKELPARAM(0, PROGRESSRANGE));
  2033. SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
  2034. if (state->ssh2) {
  2035. if (state->keytype == DSA) {
  2036. state->ssh2key.key = &state->dsakey.sshk;
  2037. } else if (state->keytype == ECDSA) {
  2038. state->ssh2key.key = &state->eckey.sshk;
  2039. } else if (state->keytype == EDDSA) {
  2040. state->ssh2key.key = &state->edkey.sshk;
  2041. } else {
  2042. state->ssh2key.key = &state->key.sshk;
  2043. }
  2044. state->commentptr = &state->ssh2key.comment;
  2045. } else {
  2046. state->commentptr = &state->key.comment;
  2047. }
  2048. /*
  2049. * Invent a comment for the key. We'll do this by including
  2050. * the date in it. This will be so horrifyingly ugly that
  2051. * the user will immediately want to change it, which is
  2052. * what we want :-)
  2053. */
  2054. *state->commentptr = snewn(30, char);
  2055. {
  2056. struct tm tm;
  2057. tm = ltime();
  2058. if (state->keytype == DSA)
  2059. strftime(*state->commentptr, 30, "dsa-key-%Y%m%d", &tm);
  2060. else if (state->keytype == ECDSA)
  2061. strftime(*state->commentptr, 30, "ecdsa-key-%Y%m%d", &tm);
  2062. else if (state->keytype == EDDSA)
  2063. strftime(*state->commentptr, 30, "eddsa-key-%Y%m%d", &tm);
  2064. else
  2065. strftime(*state->commentptr, 30, "rsa-key-%Y%m%d", &tm);
  2066. }
  2067. /*
  2068. * Now update the key controls with all the key data.
  2069. */
  2070. {
  2071. char *fp, *savecomment;
  2072. /*
  2073. * Blank passphrase, initially. This isn't dangerous,
  2074. * because we will warn (Are You Sure?) before allowing
  2075. * the user to save an unprotected private key.
  2076. */
  2077. SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
  2078. SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
  2079. /*
  2080. * Set the comment.
  2081. */
  2082. SetDlgItemText(hwnd, IDC_COMMENTEDIT, *state->commentptr);
  2083. /*
  2084. * Set the key fingerprint.
  2085. */
  2086. savecomment = *state->commentptr;
  2087. *state->commentptr = NULL;
  2088. if (state->ssh2)
  2089. fp = ssh2_fingerprint(state->ssh2key.key, state->fptype);
  2090. else
  2091. fp = rsa_ssh1_fingerprint(&state->key);
  2092. SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
  2093. sfree(fp);
  2094. *state->commentptr = savecomment;
  2095. /*
  2096. * Construct a decimal representation of the key, for
  2097. * pasting into .ssh/authorized_keys or
  2098. * .ssh/authorized_keys2 on a Unix box.
  2099. */
  2100. if (state->ssh2) {
  2101. setupbigedit2(hwnd, &state->ssh2key);
  2102. } else {
  2103. setupbigedit1(hwnd, &state->key);
  2104. }
  2105. }
  2106. /*
  2107. * Finally, hide the progress bar and show the key data.
  2108. */
  2109. ui_set_state(hwnd, state, 2);
  2110. break;
  2111. case WM_HELP: {
  2112. int id = ((LPHELPINFO)lParam)->iCtrlId;
  2113. const char *topic = NULL;
  2114. switch (id) {
  2115. case IDC_GENERATING:
  2116. case IDC_PROGRESS:
  2117. case IDC_GENSTATIC:
  2118. case IDC_GENERATE:
  2119. topic = WINHELP_CTX_puttygen_generate; break;
  2120. case IDC_PKSTATIC:
  2121. case IDC_KEYDISPLAY:
  2122. topic = WINHELP_CTX_puttygen_pastekey; break;
  2123. case IDC_FPSTATIC:
  2124. case IDC_FINGERPRINT:
  2125. topic = WINHELP_CTX_puttygen_fingerprint; break;
  2126. case IDC_COMMENTSTATIC:
  2127. case IDC_COMMENTEDIT:
  2128. topic = WINHELP_CTX_puttygen_comment; break;
  2129. case IDC_PASSPHRASE1STATIC:
  2130. case IDC_PASSPHRASE1EDIT:
  2131. case IDC_PASSPHRASE2STATIC:
  2132. case IDC_PASSPHRASE2EDIT:
  2133. topic = WINHELP_CTX_puttygen_passphrase; break;
  2134. case IDC_LOADSTATIC:
  2135. case IDC_LOAD:
  2136. topic = WINHELP_CTX_puttygen_load; break;
  2137. case IDC_SAVESTATIC:
  2138. case IDC_SAVE:
  2139. topic = WINHELP_CTX_puttygen_savepriv; break;
  2140. case IDC_SAVEPUB:
  2141. topic = WINHELP_CTX_puttygen_savepub; break;
  2142. case IDC_TYPESTATIC:
  2143. case IDC_KEYSSH1:
  2144. case IDC_KEYSSH2RSA:
  2145. case IDC_KEYSSH2DSA:
  2146. case IDC_KEYSSH2ECDSA:
  2147. case IDC_KEYSSH2EDDSA:
  2148. topic = WINHELP_CTX_puttygen_keytype; break;
  2149. case IDC_BITSSTATIC:
  2150. case IDC_BITS:
  2151. topic = WINHELP_CTX_puttygen_bits; break;
  2152. case IDC_IMPORT:
  2153. case IDC_EXPORT_OPENSSH_AUTO:
  2154. case IDC_EXPORT_OPENSSH_NEW:
  2155. case IDC_EXPORT_SSHCOM:
  2156. topic = WINHELP_CTX_puttygen_conversions; break;
  2157. }
  2158. if (topic) {
  2159. launch_help(hwnd, topic);
  2160. } else {
  2161. MessageBeep(0);
  2162. }
  2163. break;
  2164. }
  2165. case WM_CLOSE:
  2166. state = (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  2167. sfree(state);
  2168. quit_help(hwnd);
  2169. EndDialog(hwnd, 1);
  2170. return 0;
  2171. }
  2172. return 0;
  2173. }
  2174. void cleanup_exit(int code)
  2175. {
  2176. shutdown_help();
  2177. exit(code);
  2178. }
  2179. HINSTANCE hinst;
  2180. static NORETURN void opt_error(const char *fmt, ...)
  2181. {
  2182. va_list ap;
  2183. va_start(ap, fmt);
  2184. char *msg = dupvprintf(fmt, ap);
  2185. va_end(ap);
  2186. MessageBox(NULL, msg, "PuTTYgen command line error", MB_ICONERROR | MB_OK);
  2187. exit(1);
  2188. }
  2189. int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
  2190. {
  2191. int argc;
  2192. char **argv;
  2193. int ret;
  2194. struct InitialParams params[1];
  2195. dll_hijacking_protection();
  2196. init_common_controls();
  2197. hinst = inst;
  2198. /*
  2199. * See if we can find our Help file.
  2200. */
  2201. init_help();
  2202. params->keybutton = IDC_KEYSSH2RSA;
  2203. params->primepolicybutton = IDC_PRIMEGEN_PROB;
  2204. params->rsa_strong = false;
  2205. params->fptype = SSH_FPTYPE_DEFAULT;
  2206. params->keybits = DEFAULT_KEY_BITS;
  2207. params->eccurve_index = DEFAULT_ECCURVE_INDEX;
  2208. params->edcurve_index = DEFAULT_EDCURVE_INDEX;
  2209. save_params = ppk_save_default_parameters;
  2210. split_into_argv(cmdline, false, &argc, &argv, NULL);
  2211. int argbits = -1;
  2212. AuxMatchOpt amo = aux_match_opt_init(argc, argv, 0, opt_error);
  2213. while (!aux_match_done(&amo)) {
  2214. char *val;
  2215. #define match_opt(...) aux_match_opt( \
  2216. &amo, NULL, __VA_ARGS__, (const char *)NULL)
  2217. #define match_optval(...) aux_match_opt( \
  2218. &amo, &val, __VA_ARGS__, (const char *)NULL)
  2219. if (aux_match_arg(&amo, &val)) {
  2220. if (!cmdline_keyfile) {
  2221. /*
  2222. * Assume the first argument to be a private key file, and
  2223. * attempt to load it.
  2224. */
  2225. cmdline_keyfile = val;
  2226. continue;
  2227. } else {
  2228. opt_error("unexpected extra argument '%s'\n", val);
  2229. }
  2230. } else if (match_opt("-pgpfp")) {
  2231. pgp_fingerprints_msgbox(NULL);
  2232. return 1;
  2233. } else if (match_opt("-restrict-acl", "-restrict_acl",
  2234. "-restrictacl")) {
  2235. restrict_process_acl();
  2236. } else if (match_optval("-t")) {
  2237. if (!strcmp(val, "rsa") || !strcmp(val, "rsa2")) {
  2238. params->keybutton = IDC_KEYSSH2RSA;
  2239. } else if (!strcmp(val, "rsa1")) {
  2240. params->keybutton = IDC_KEYSSH1;
  2241. } else if (!strcmp(val, "dsa") || !strcmp(val, "dss")) {
  2242. params->keybutton = IDC_KEYSSH2DSA;
  2243. } else if (!strcmp(val, "ecdsa")) {
  2244. params->keybutton = IDC_KEYSSH2ECDSA;
  2245. } else if (!strcmp(val, "eddsa")) {
  2246. params->keybutton = IDC_KEYSSH2EDDSA;
  2247. } else if (!strcmp(val, "ed25519")) {
  2248. params->keybutton = IDC_KEYSSH2EDDSA;
  2249. argbits = 255;
  2250. } else if (!strcmp(val, "ed448")) {
  2251. params->keybutton = IDC_KEYSSH2EDDSA;
  2252. argbits = 448;
  2253. } else {
  2254. opt_error("unknown key type '%s'\n", val);
  2255. }
  2256. } else if (match_optval("-b")) {
  2257. argbits = atoi(val);
  2258. } else if (match_optval("-E")) {
  2259. if (!strcmp(val, "md5"))
  2260. params->fptype = SSH_FPTYPE_MD5;
  2261. else if (!strcmp(val, "sha256"))
  2262. params->fptype = SSH_FPTYPE_SHA256;
  2263. else
  2264. opt_error("unknown fingerprint type '%s'\n", val);
  2265. } else if (match_optval("-primes")) {
  2266. if (!strcmp(val, "probable") ||
  2267. !strcmp(val, "probabilistic")) {
  2268. params->primepolicybutton = IDC_PRIMEGEN_PROB;
  2269. } else if (!strcmp(val, "provable") ||
  2270. !strcmp(val, "proven") ||
  2271. !strcmp(val, "simple") ||
  2272. !strcmp(val, "maurer-simple")) {
  2273. params->primepolicybutton = IDC_PRIMEGEN_MAURER_SIMPLE;
  2274. } else if (!strcmp(val, "provable-even") ||
  2275. !strcmp(val, "proven-even") ||
  2276. !strcmp(val, "even") ||
  2277. !strcmp(val, "complex") ||
  2278. !strcmp(val, "maurer-complex")) {
  2279. params->primepolicybutton = IDC_PRIMEGEN_MAURER_COMPLEX;
  2280. } else {
  2281. opt_error("unrecognised prime-generation mode '%s'\n", val);
  2282. }
  2283. } else if (match_opt("-strong-rsa")) {
  2284. params->rsa_strong = true;
  2285. } else if (match_optval("-ppk-param", "-ppk-params")) {
  2286. char *nextval;
  2287. for (; val; val = nextval) {
  2288. nextval = strchr(val, ',');
  2289. if (nextval)
  2290. *nextval++ = '\0';
  2291. char *optvalue = strchr(val, '=');
  2292. if (!optvalue)
  2293. opt_error("PPK parameter '%s' expected a value\n", val);
  2294. *optvalue++ = '\0';
  2295. /* Non-numeric options */
  2296. if (!strcmp(val, "kdf")) {
  2297. if (!strcmp(optvalue, "Argon2id") ||
  2298. !strcmp(optvalue, "argon2id")) {
  2299. save_params.argon2_flavour = Argon2id;
  2300. } else if (!strcmp(optvalue, "Argon2i") ||
  2301. !strcmp(optvalue, "argon2i")) {
  2302. save_params.argon2_flavour = Argon2i;
  2303. } else if (!strcmp(optvalue, "Argon2d") ||
  2304. !strcmp(optvalue, "argon2d")) {
  2305. save_params.argon2_flavour = Argon2d;
  2306. } else {
  2307. opt_error("unrecognised kdf '%s'\n", optvalue);
  2308. }
  2309. continue;
  2310. }
  2311. char *end;
  2312. unsigned long n = strtoul(optvalue, &end, 0);
  2313. if (!*optvalue || *end)
  2314. opt_error("value '%s' for PPK parameter '%s': expected a "
  2315. "number\n", optvalue, val);
  2316. if (!strcmp(val, "version")) {
  2317. save_params.fmt_version = n;
  2318. } else if (!strcmp(val, "memory") ||
  2319. !strcmp(val, "mem")) {
  2320. save_params.argon2_mem = n;
  2321. } else if (!strcmp(val, "time")) {
  2322. save_params.argon2_passes_auto = true;
  2323. save_params.argon2_milliseconds = n;
  2324. } else if (!strcmp(val, "passes")) {
  2325. save_params.argon2_passes_auto = false;
  2326. save_params.argon2_passes = n;
  2327. } else if (!strcmp(val, "parallelism") ||
  2328. !strcmp(val, "parallel")) {
  2329. save_params.argon2_parallelism = n;
  2330. } else {
  2331. opt_error("unrecognised PPK parameter '%s'\n", val);
  2332. }
  2333. }
  2334. } else if (match_optval("-demo-screenshot")) {
  2335. demo_screenshot_filename = val;
  2336. cmdline_demo_keystr = PTRLEN_LITERAL(
  2337. "PuTTY-User-Key-File-3: ssh-ed25519\n"
  2338. "Encryption: none\n"
  2339. "Comment: ed25519-key-20220402\n"
  2340. "Public-Lines: 2\n"
  2341. "AAAAC3NzaC1lZDI1NTE5AAAAILzuIFwZ"
  2342. "8ZhgOlilcSb+9zPuCf/DmKJiloVlmWGy\n"
  2343. "xa/F\n"
  2344. "Private-Lines: 1\n"
  2345. "AAAAIPca6vLwtB2NJhZUpABQISR0gcQH8jjQLta19VyzA3wc\n"
  2346. "Private-MAC: 1159e9628259b35933b397379bbe8a14"
  2347. "a1f1d97fe91e446e45a9581a3408b70e\n");
  2348. params->keybutton = IDC_KEYSSH2EDDSA;
  2349. argbits = 255;
  2350. } else {
  2351. opt_error("unrecognised option '%s'\n", amo.argv[amo.index]);
  2352. }
  2353. }
  2354. /* Translate argbits into eccurve_index and edcurve_index */
  2355. if (argbits > 0) {
  2356. switch (params->keybutton) {
  2357. case IDC_KEYSSH2RSA:
  2358. case IDC_KEYSSH1:
  2359. case IDC_KEYSSH2DSA:
  2360. params->keybits = argbits;
  2361. break;
  2362. case IDC_KEYSSH2ECDSA: {
  2363. bool found = false;
  2364. for (int j = 0; j < n_ec_nist_curve_lengths; j++)
  2365. if (argbits == ec_nist_curve_lengths[j]) {
  2366. params->eccurve_index = j;
  2367. found = true;
  2368. break;
  2369. }
  2370. if (!found)
  2371. opt_error("unsupported ECDSA bit length %d", argbits);
  2372. break;
  2373. }
  2374. case IDC_KEYSSH2EDDSA: {
  2375. bool found = false;
  2376. for (int j = 0; j < n_ec_ed_curve_lengths; j++)
  2377. if (argbits == ec_ed_curve_lengths[j]) {
  2378. params->edcurve_index = j;
  2379. found = true;
  2380. break;
  2381. }
  2382. if (!found)
  2383. opt_error("unsupported EDDSA bit length %d", argbits);
  2384. break;
  2385. }
  2386. }
  2387. }
  2388. random_setup_special();
  2389. ret = DialogBoxParam(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc,
  2390. (LPARAM)params) != IDOK;
  2391. cleanup_exit(ret);
  2392. return ret; /* just in case optimiser complains */
  2393. }