cmdgen.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. /*
  2. * cmdgen.c - command-line form of PuTTYgen
  3. */
  4. #define PUTTY_DO_GLOBALS
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <limits.h>
  9. #include <assert.h>
  10. #include <time.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include "putty.h"
  14. #include "ssh.h"
  15. #ifdef TEST_CMDGEN
  16. /*
  17. * This section overrides some definitions below for test purposes.
  18. * When compiled with -DTEST_CMDGEN (as cgtest.c will do):
  19. *
  20. * - Calls to get_random_data() are replaced with the diagnostic
  21. * function below (I #define the name so that I can still link
  22. * with the original set of modules without symbol clash), in
  23. * order to avoid depleting the test system's /dev/random
  24. * unnecessarily.
  25. *
  26. * - Calls to console_get_userpass_input() are replaced with the
  27. * diagnostic function below, so that I can run tests in an
  28. * automated manner and provide their interactive passphrase
  29. * inputs.
  30. *
  31. * - main() is renamed to cmdgen_main(); at the bottom of the file
  32. * I define another main() which calls the former repeatedly to
  33. * run tests.
  34. */
  35. #define get_random_data get_random_data_diagnostic
  36. char *get_random_data(int len, const char *device)
  37. {
  38. char *buf = snewn(len, char);
  39. memset(buf, 'x', len);
  40. return buf;
  41. }
  42. #define console_get_userpass_input console_get_userpass_input_diagnostic
  43. int nprompts, promptsgot;
  44. const char *prompts[3];
  45. int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
  46. {
  47. size_t i;
  48. int ret = 1;
  49. for (i = 0; i < p->n_prompts; i++) {
  50. if (promptsgot < nprompts) {
  51. p->prompts[i]->result = dupstr(prompts[promptsgot++]);
  52. } else {
  53. promptsgot++; /* track number of requests anyway */
  54. ret = 0;
  55. }
  56. }
  57. return ret;
  58. }
  59. #define main cmdgen_main
  60. #endif
  61. struct progress {
  62. int phase, current;
  63. };
  64. static void progress_update(void *param, int action, int phase, int iprogress)
  65. {
  66. struct progress *p = (struct progress *)param;
  67. if (action != PROGFN_PROGRESS)
  68. return;
  69. if (phase > p->phase) {
  70. if (p->phase >= 0)
  71. fputc('\n', stderr);
  72. p->phase = phase;
  73. if (iprogress >= 0)
  74. p->current = iprogress - 1;
  75. else
  76. p->current = iprogress;
  77. }
  78. while (p->current < iprogress) {
  79. fputc('+', stdout);
  80. p->current++;
  81. }
  82. fflush(stdout);
  83. }
  84. static void no_progress(void *param, int action, int phase, int iprogress)
  85. {
  86. }
  87. void modalfatalbox(const char *p, ...)
  88. {
  89. va_list ap;
  90. fprintf(stderr, "FATAL ERROR: ");
  91. va_start(ap, p);
  92. vfprintf(stderr, p, ap);
  93. va_end(ap);
  94. fputc('\n', stderr);
  95. cleanup_exit(1);
  96. }
  97. void nonfatal(const char *p, ...)
  98. {
  99. va_list ap;
  100. fprintf(stderr, "ERROR: ");
  101. va_start(ap, p);
  102. vfprintf(stderr, p, ap);
  103. va_end(ap);
  104. fputc('\n', stderr);
  105. }
  106. /*
  107. * Stubs to let everything else link sensibly.
  108. */
  109. void log_eventlog(void *handle, const char *event)
  110. {
  111. }
  112. char *x_get_default(const char *key)
  113. {
  114. return NULL;
  115. }
  116. void sk_cleanup(void)
  117. {
  118. }
  119. void showversion(void)
  120. {
  121. printf("puttygen: %s\n", ver);
  122. }
  123. void usage(int standalone)
  124. {
  125. fprintf(stderr,
  126. "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
  127. " [ -C comment ] [ -P ] [ -q ]\n"
  128. " [ -o output-keyfile ] [ -O type | -l | -L"
  129. " | -p ]\n");
  130. if (standalone)
  131. fprintf(stderr,
  132. "Use \"puttygen --help\" for more detail.\n");
  133. }
  134. void help(void)
  135. {
  136. /*
  137. * Help message is an extended version of the usage message. So
  138. * start with that, plus a version heading.
  139. */
  140. showversion();
  141. usage(FALSE);
  142. fprintf(stderr,
  143. " -t specify key type when generating (ed25519, ecdsa, rsa, "
  144. "dsa, rsa1)\n"
  145. " -b specify number of bits when generating key\n"
  146. " -C change or specify key comment\n"
  147. " -P change key passphrase\n"
  148. " -q quiet: do not display progress bar\n"
  149. " -O specify output type:\n"
  150. " private output PuTTY private key format\n"
  151. " private-openssh export OpenSSH private key\n"
  152. " private-openssh-new export OpenSSH private key "
  153. "(force new file format)\n"
  154. " private-sshcom export ssh.com private key\n"
  155. " public RFC 4716 / ssh.com public key\n"
  156. " public-openssh OpenSSH public key\n"
  157. " fingerprint output the key fingerprint\n"
  158. " -o specify output file\n"
  159. " -l equivalent to `-O fingerprint'\n"
  160. " -L equivalent to `-O public-openssh'\n"
  161. " -p equivalent to `-O public'\n"
  162. " --old-passphrase file\n"
  163. " specify file containing old key passphrase\n"
  164. " --new-passphrase file\n"
  165. " specify file containing new key passphrase\n"
  166. " --random-device device\n"
  167. " specify device to read entropy from (e.g. /dev/urandom)\n"
  168. );
  169. }
  170. static int move(char *from, char *to)
  171. {
  172. int ret;
  173. ret = rename(from, to);
  174. if (ret) {
  175. /*
  176. * This OS may require us to remove the original file first.
  177. */
  178. remove(to);
  179. ret = rename(from, to);
  180. }
  181. if (ret) {
  182. perror("puttygen: cannot move new file on to old one");
  183. return FALSE;
  184. }
  185. return TRUE;
  186. }
  187. static char *readpassphrase(const char *filename)
  188. {
  189. FILE *fp;
  190. char *line;
  191. fp = fopen(filename, "r");
  192. if (!fp) {
  193. fprintf(stderr, "puttygen: cannot open %s: %s\n",
  194. filename, strerror(errno));
  195. return NULL;
  196. }
  197. line = fgetline(fp);
  198. if (line)
  199. line[strcspn(line, "\r\n")] = '\0';
  200. else if (ferror(fp))
  201. fprintf(stderr, "puttygen: error reading from %s: %s\n",
  202. filename, strerror(errno));
  203. else /* empty file */
  204. line = dupstr("");
  205. fclose(fp);
  206. return line;
  207. }
  208. #define DEFAULT_RSADSA_BITS 2048
  209. int main(int argc, char **argv)
  210. {
  211. char *infile = NULL;
  212. Filename *infilename = NULL, *outfilename = NULL;
  213. enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
  214. char *outfile = NULL, *outfiletmp = NULL;
  215. enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
  216. OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
  217. int bits = -1;
  218. char *comment = NULL, *origcomment = NULL;
  219. int change_passphrase = FALSE;
  220. int errs = FALSE, nogo = FALSE;
  221. int intype = SSH_KEYTYPE_UNOPENABLE;
  222. int sshver = 0;
  223. struct ssh2_userkey *ssh2key = NULL;
  224. struct RSAKey *ssh1key = NULL;
  225. unsigned char *ssh2blob = NULL;
  226. char *ssh2alg = NULL;
  227. const struct ssh_signkey *ssh2algf = NULL;
  228. int ssh2bloblen;
  229. char *old_passphrase = NULL, *new_passphrase = NULL;
  230. int load_encrypted;
  231. progfn_t progressfn = is_interactive() ? progress_update : no_progress;
  232. const char *random_device = NULL;
  233. /* ------------------------------------------------------------------
  234. * Parse the command line to figure out what we've been asked to do.
  235. */
  236. /*
  237. * If run with no arguments at all, print the usage message and
  238. * return success.
  239. */
  240. if (argc <= 1) {
  241. usage(TRUE);
  242. return 0;
  243. }
  244. /*
  245. * Parse command line arguments.
  246. */
  247. while (--argc) {
  248. char *p = *++argv;
  249. if (*p == '-') {
  250. /*
  251. * An option.
  252. */
  253. while (p && *++p) {
  254. char c = *p;
  255. switch (c) {
  256. case '-':
  257. /*
  258. * Long option.
  259. */
  260. {
  261. char *opt, *val;
  262. opt = p++; /* opt will have _one_ leading - */
  263. while (*p && *p != '=')
  264. p++; /* find end of option */
  265. if (*p == '=') {
  266. *p++ = '\0';
  267. val = p;
  268. } else
  269. val = NULL;
  270. if (!strcmp(opt, "-help")) {
  271. if (val) {
  272. errs = TRUE;
  273. fprintf(stderr, "puttygen: option `-%s'"
  274. " expects no argument\n", opt);
  275. } else {
  276. help();
  277. nogo = TRUE;
  278. }
  279. } else if (!strcmp(opt, "-version")) {
  280. if (val) {
  281. errs = TRUE;
  282. fprintf(stderr, "puttygen: option `-%s'"
  283. " expects no argument\n", opt);
  284. } else {
  285. showversion();
  286. nogo = TRUE;
  287. }
  288. } else if (!strcmp(opt, "-pgpfp")) {
  289. if (val) {
  290. errs = TRUE;
  291. fprintf(stderr, "puttygen: option `-%s'"
  292. " expects no argument\n", opt);
  293. } else {
  294. /* support --pgpfp for consistency */
  295. pgp_fingerprints();
  296. nogo = TRUE;
  297. }
  298. } else if (!strcmp(opt, "-old-passphrase")) {
  299. if (!val && argc > 1)
  300. --argc, val = *++argv;
  301. if (!val) {
  302. errs = TRUE;
  303. fprintf(stderr, "puttygen: option `-%s'"
  304. " expects an argument\n", opt);
  305. } else {
  306. old_passphrase = readpassphrase(val);
  307. if (!old_passphrase)
  308. errs = TRUE;
  309. }
  310. } else if (!strcmp(opt, "-new-passphrase")) {
  311. if (!val && argc > 1)
  312. --argc, val = *++argv;
  313. if (!val) {
  314. errs = TRUE;
  315. fprintf(stderr, "puttygen: option `-%s'"
  316. " expects an argument\n", opt);
  317. } else {
  318. new_passphrase = readpassphrase(val);
  319. if (!new_passphrase)
  320. errs = TRUE;
  321. }
  322. } else if (!strcmp(opt, "-random-device")) {
  323. if (!val && argc > 1)
  324. --argc, val = *++argv;
  325. if (!val) {
  326. errs = TRUE;
  327. fprintf(stderr, "puttygen: option `-%s'"
  328. " expects an argument\n", opt);
  329. } else {
  330. random_device = val;
  331. }
  332. } else {
  333. errs = TRUE;
  334. fprintf(stderr,
  335. "puttygen: no such option `-%s'\n", opt);
  336. }
  337. }
  338. p = NULL;
  339. break;
  340. case 'h':
  341. case 'V':
  342. case 'P':
  343. case 'l':
  344. case 'L':
  345. case 'p':
  346. case 'q':
  347. /*
  348. * Option requiring no parameter.
  349. */
  350. switch (c) {
  351. case 'h':
  352. help();
  353. nogo = TRUE;
  354. break;
  355. case 'V':
  356. showversion();
  357. nogo = TRUE;
  358. break;
  359. case 'P':
  360. change_passphrase = TRUE;
  361. break;
  362. case 'l':
  363. outtype = FP;
  364. break;
  365. case 'L':
  366. outtype = PUBLICO;
  367. break;
  368. case 'p':
  369. outtype = PUBLIC;
  370. break;
  371. case 'q':
  372. progressfn = no_progress;
  373. break;
  374. }
  375. break;
  376. case 't':
  377. case 'b':
  378. case 'C':
  379. case 'O':
  380. case 'o':
  381. /*
  382. * Option requiring parameter.
  383. */
  384. p++;
  385. if (!*p && argc > 1)
  386. --argc, p = *++argv;
  387. else if (!*p) {
  388. fprintf(stderr, "puttygen: option `-%c' expects a"
  389. " parameter\n", c);
  390. errs = TRUE;
  391. }
  392. /*
  393. * Now c is the option and p is the parameter.
  394. */
  395. switch (c) {
  396. case 't':
  397. if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
  398. keytype = RSA2, sshver = 2;
  399. else if (!strcmp(p, "rsa1"))
  400. keytype = RSA1, sshver = 1;
  401. else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
  402. keytype = DSA, sshver = 2;
  403. else if (!strcmp(p, "ecdsa"))
  404. keytype = ECDSA, sshver = 2;
  405. else if (!strcmp(p, "ed25519"))
  406. keytype = ED25519, sshver = 2;
  407. else {
  408. fprintf(stderr,
  409. "puttygen: unknown key type `%s'\n", p);
  410. errs = TRUE;
  411. }
  412. break;
  413. case 'b':
  414. bits = atoi(p);
  415. break;
  416. case 'C':
  417. comment = p;
  418. break;
  419. case 'O':
  420. if (!strcmp(p, "public"))
  421. outtype = PUBLIC;
  422. else if (!strcmp(p, "public-openssh"))
  423. outtype = PUBLICO;
  424. else if (!strcmp(p, "private"))
  425. outtype = PRIVATE;
  426. else if (!strcmp(p, "fingerprint"))
  427. outtype = FP;
  428. else if (!strcmp(p, "private-openssh"))
  429. outtype = OPENSSH_AUTO, sshver = 2;
  430. else if (!strcmp(p, "private-openssh-new"))
  431. outtype = OPENSSH_NEW, sshver = 2;
  432. else if (!strcmp(p, "private-sshcom"))
  433. outtype = SSHCOM, sshver = 2;
  434. else {
  435. fprintf(stderr,
  436. "puttygen: unknown output type `%s'\n", p);
  437. errs = TRUE;
  438. }
  439. break;
  440. case 'o':
  441. outfile = p;
  442. break;
  443. }
  444. p = NULL; /* prevent continued processing */
  445. break;
  446. default:
  447. /*
  448. * Unrecognised option.
  449. */
  450. errs = TRUE;
  451. fprintf(stderr, "puttygen: no such option `-%c'\n", c);
  452. break;
  453. }
  454. }
  455. } else {
  456. /*
  457. * A non-option argument.
  458. */
  459. if (!infile)
  460. infile = p;
  461. else {
  462. errs = TRUE;
  463. fprintf(stderr, "puttygen: cannot handle more than one"
  464. " input file\n");
  465. }
  466. }
  467. }
  468. if (bits == -1) {
  469. /*
  470. * No explicit key size was specified. Default varies
  471. * depending on key type.
  472. */
  473. switch (keytype) {
  474. case ECDSA:
  475. bits = 384;
  476. break;
  477. case ED25519:
  478. bits = 256;
  479. break;
  480. default:
  481. bits = DEFAULT_RSADSA_BITS;
  482. break;
  483. }
  484. }
  485. if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
  486. fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
  487. errs = TRUE;
  488. }
  489. if (keytype == ED25519 && (bits != 256)) {
  490. fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
  491. errs = TRUE;
  492. }
  493. if (keytype == RSA2 || keytype == RSA1 || keytype == DSA) {
  494. if (bits < 256) {
  495. fprintf(stderr, "puttygen: cannot generate %s keys shorter than"
  496. " 256 bits\n", (keytype == DSA ? "DSA" : "RSA"));
  497. errs = TRUE;
  498. } else if (bits < DEFAULT_RSADSA_BITS) {
  499. fprintf(stderr, "puttygen: warning: %s keys shorter than"
  500. " %d bits are probably not secure\n",
  501. (keytype == DSA ? "DSA" : "RSA"), DEFAULT_RSADSA_BITS);
  502. /* but this is just a warning, so proceed anyway */
  503. }
  504. }
  505. if (errs)
  506. return 1;
  507. if (nogo)
  508. return 0;
  509. /*
  510. * If run with at least one argument _but_ not the required
  511. * ones, print the usage message and return failure.
  512. */
  513. if (!infile && keytype == NOKEYGEN) {
  514. usage(TRUE);
  515. return 1;
  516. }
  517. /* ------------------------------------------------------------------
  518. * Figure out further details of exactly what we're going to do.
  519. */
  520. /*
  521. * Bomb out if we've been asked to both load and generate a
  522. * key.
  523. */
  524. if (keytype != NOKEYGEN && infile) {
  525. fprintf(stderr, "puttygen: cannot both load and generate a key\n");
  526. return 1;
  527. }
  528. /*
  529. * We must save the private part when generating a new key.
  530. */
  531. if (keytype != NOKEYGEN &&
  532. (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
  533. outtype != OPENSSH_NEW && outtype != SSHCOM)) {
  534. fprintf(stderr, "puttygen: this would generate a new key but "
  535. "discard the private part\n");
  536. return 1;
  537. }
  538. /*
  539. * Analyse the type of the input file, in case this affects our
  540. * course of action.
  541. */
  542. if (infile) {
  543. infilename = filename_from_str(infile);
  544. intype = key_type(infilename);
  545. switch (intype) {
  546. case SSH_KEYTYPE_UNOPENABLE:
  547. case SSH_KEYTYPE_UNKNOWN:
  548. fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
  549. infile, key_type_to_str(intype));
  550. return 1;
  551. case SSH_KEYTYPE_SSH1:
  552. case SSH_KEYTYPE_SSH1_PUBLIC:
  553. if (sshver == 2) {
  554. fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
  555. " not supported\n");
  556. return 1;
  557. }
  558. sshver = 1;
  559. break;
  560. case SSH_KEYTYPE_SSH2:
  561. case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
  562. case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
  563. case SSH_KEYTYPE_OPENSSH_PEM:
  564. case SSH_KEYTYPE_OPENSSH_NEW:
  565. case SSH_KEYTYPE_SSHCOM:
  566. if (sshver == 1) {
  567. fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
  568. " not supported\n");
  569. return 1;
  570. }
  571. sshver = 2;
  572. break;
  573. case SSH_KEYTYPE_OPENSSH_AUTO:
  574. default:
  575. assert(0 && "Should never see these types on an input file");
  576. }
  577. }
  578. /*
  579. * Determine the default output file, if none is provided.
  580. *
  581. * This will usually be equal to stdout, except that if the
  582. * input and output file formats are the same then the default
  583. * output is to overwrite the input.
  584. *
  585. * Also in this code, we bomb out if the input and output file
  586. * formats are the same and no other action is performed.
  587. */
  588. if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
  589. (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
  590. (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
  591. (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
  592. (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
  593. if (!outfile) {
  594. outfile = infile;
  595. outfiletmp = dupcat(outfile, ".tmp", NULL);
  596. }
  597. if (!change_passphrase && !comment) {
  598. fprintf(stderr, "puttygen: this command would perform no useful"
  599. " action\n");
  600. return 1;
  601. }
  602. } else {
  603. if (!outfile) {
  604. /*
  605. * Bomb out rather than automatically choosing to write
  606. * a private key file to stdout.
  607. */
  608. if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
  609. outtype == OPENSSH_NEW || outtype == SSHCOM) {
  610. fprintf(stderr, "puttygen: need to specify an output file\n");
  611. return 1;
  612. }
  613. }
  614. }
  615. /*
  616. * Figure out whether we need to load the encrypted part of the
  617. * key. This will be the case if either (a) we need to write
  618. * out a private key format, or (b) the entire input key file
  619. * is encrypted.
  620. */
  621. if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
  622. outtype == OPENSSH_NEW || outtype == SSHCOM ||
  623. intype == SSH_KEYTYPE_OPENSSH_PEM ||
  624. intype == SSH_KEYTYPE_OPENSSH_NEW ||
  625. intype == SSH_KEYTYPE_SSHCOM)
  626. load_encrypted = TRUE;
  627. else
  628. load_encrypted = FALSE;
  629. if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
  630. intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
  631. intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
  632. fprintf(stderr, "puttygen: cannot perform this action on a "
  633. "public-key-only input file\n");
  634. return 1;
  635. }
  636. /* ------------------------------------------------------------------
  637. * Now we're ready to actually do some stuff.
  638. */
  639. /*
  640. * Either load or generate a key.
  641. */
  642. if (keytype != NOKEYGEN) {
  643. char *entropy;
  644. char default_comment[80];
  645. struct tm tm;
  646. struct progress prog;
  647. prog.phase = -1;
  648. prog.current = -1;
  649. tm = ltime();
  650. if (keytype == DSA)
  651. strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
  652. else if (keytype == ECDSA)
  653. strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
  654. else if (keytype == ED25519)
  655. strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
  656. else
  657. strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
  658. random_ref();
  659. entropy = get_random_data(bits / 8, random_device);
  660. if (!entropy) {
  661. fprintf(stderr, "puttygen: failed to collect entropy, "
  662. "could not generate key\n");
  663. return 1;
  664. }
  665. random_add_heavynoise(entropy, bits / 8);
  666. smemclr(entropy, bits/8);
  667. sfree(entropy);
  668. if (keytype == DSA) {
  669. struct dss_key *dsskey = snew(struct dss_key);
  670. dsa_generate(dsskey, bits, progressfn, &prog);
  671. ssh2key = snew(struct ssh2_userkey);
  672. ssh2key->data = dsskey;
  673. ssh2key->alg = &ssh_dss;
  674. ssh1key = NULL;
  675. } else if (keytype == ECDSA) {
  676. struct ec_key *ec = snew(struct ec_key);
  677. ec_generate(ec, bits, progressfn, &prog);
  678. ssh2key = snew(struct ssh2_userkey);
  679. ssh2key->data = ec;
  680. ssh2key->alg = ec->signalg;
  681. ssh1key = NULL;
  682. } else if (keytype == ED25519) {
  683. struct ec_key *ec = snew(struct ec_key);
  684. ec_edgenerate(ec, bits, progressfn, &prog);
  685. ssh2key = snew(struct ssh2_userkey);
  686. ssh2key->data = ec;
  687. ssh2key->alg = &ssh_ecdsa_ed25519;
  688. ssh1key = NULL;
  689. } else {
  690. struct RSAKey *rsakey = snew(struct RSAKey);
  691. rsa_generate(rsakey, bits, progressfn, &prog);
  692. rsakey->comment = NULL;
  693. if (keytype == RSA1) {
  694. ssh1key = rsakey;
  695. } else {
  696. ssh2key = snew(struct ssh2_userkey);
  697. ssh2key->data = rsakey;
  698. ssh2key->alg = &ssh_rsa;
  699. }
  700. }
  701. progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
  702. if (ssh2key)
  703. ssh2key->comment = dupstr(default_comment);
  704. if (ssh1key)
  705. ssh1key->comment = dupstr(default_comment);
  706. } else {
  707. const char *error = NULL;
  708. int encrypted;
  709. assert(infile != NULL);
  710. /*
  711. * Find out whether the input key is encrypted.
  712. */
  713. if (intype == SSH_KEYTYPE_SSH1)
  714. encrypted = rsakey_encrypted(infilename, &origcomment);
  715. else if (intype == SSH_KEYTYPE_SSH2)
  716. encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
  717. else
  718. encrypted = import_encrypted(infilename, intype, &origcomment);
  719. /*
  720. * If so, ask for a passphrase.
  721. */
  722. if (encrypted && load_encrypted) {
  723. if (!old_passphrase) {
  724. prompts_t *p = new_prompts(NULL);
  725. int ret;
  726. p->to_server = FALSE;
  727. p->name = dupstr("SSH key passphrase");
  728. add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
  729. ret = console_get_userpass_input(p, NULL, 0);
  730. assert(ret >= 0);
  731. if (!ret) {
  732. free_prompts(p);
  733. perror("puttygen: unable to read passphrase");
  734. return 1;
  735. } else {
  736. old_passphrase = dupstr(p->prompts[0]->result);
  737. free_prompts(p);
  738. }
  739. }
  740. } else {
  741. old_passphrase = NULL;
  742. }
  743. switch (intype) {
  744. int ret;
  745. case SSH_KEYTYPE_SSH1:
  746. case SSH_KEYTYPE_SSH1_PUBLIC:
  747. ssh1key = snew(struct RSAKey);
  748. if (!load_encrypted) {
  749. void *vblob;
  750. unsigned char *blob;
  751. int n, l, bloblen;
  752. ret = rsakey_pubblob(infilename, &vblob, &bloblen,
  753. &origcomment, &error);
  754. blob = (unsigned char *)vblob;
  755. n = 4; /* skip modulus bits */
  756. l = ssh1_read_bignum(blob + n, bloblen - n,
  757. &ssh1key->exponent);
  758. if (l < 0) {
  759. error = "SSH-1 public key blob was too short";
  760. } else {
  761. n += l;
  762. l = ssh1_read_bignum(blob + n, bloblen - n,
  763. &ssh1key->modulus);
  764. if (l < 0) {
  765. error = "SSH-1 public key blob was too short";
  766. } else
  767. n += l;
  768. }
  769. ssh1key->comment = dupstr(origcomment);
  770. ssh1key->private_exponent = NULL;
  771. ssh1key->p = NULL;
  772. ssh1key->q = NULL;
  773. ssh1key->iqmp = NULL;
  774. } else {
  775. ret = loadrsakey(infilename, ssh1key, old_passphrase, &error);
  776. }
  777. if (ret > 0)
  778. error = NULL;
  779. else if (!error)
  780. error = "unknown error";
  781. break;
  782. case SSH_KEYTYPE_SSH2:
  783. case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
  784. case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
  785. if (!load_encrypted) {
  786. ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
  787. &ssh2bloblen, &origcomment,
  788. &error);
  789. if (ssh2blob) {
  790. ssh2algf = find_pubkey_alg(ssh2alg);
  791. if (ssh2algf)
  792. bits = ssh2algf->pubkey_bits(ssh2algf,
  793. ssh2blob, ssh2bloblen);
  794. else
  795. bits = -1;
  796. }
  797. sfree(ssh2alg);
  798. } else {
  799. ssh2key = ssh2_load_userkey(infilename, old_passphrase,
  800. &error);
  801. }
  802. if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
  803. error = NULL;
  804. else if (!error) {
  805. if (ssh2key == SSH2_WRONG_PASSPHRASE)
  806. error = "wrong passphrase";
  807. else
  808. error = "unknown error";
  809. }
  810. break;
  811. case SSH_KEYTYPE_OPENSSH_PEM:
  812. case SSH_KEYTYPE_OPENSSH_NEW:
  813. case SSH_KEYTYPE_SSHCOM:
  814. ssh2key = import_ssh2(infilename, intype, old_passphrase, &error);
  815. if (ssh2key) {
  816. if (ssh2key != SSH2_WRONG_PASSPHRASE)
  817. error = NULL;
  818. else
  819. error = "wrong passphrase";
  820. } else if (!error)
  821. error = "unknown error";
  822. break;
  823. default:
  824. assert(0);
  825. }
  826. if (error) {
  827. fprintf(stderr, "puttygen: error loading `%s': %s\n",
  828. infile, error);
  829. return 1;
  830. }
  831. }
  832. /*
  833. * Change the comment if asked to.
  834. */
  835. if (comment) {
  836. if (sshver == 1) {
  837. assert(ssh1key);
  838. sfree(ssh1key->comment);
  839. ssh1key->comment = dupstr(comment);
  840. } else {
  841. assert(ssh2key);
  842. sfree(ssh2key->comment);
  843. ssh2key->comment = dupstr(comment);
  844. }
  845. }
  846. /*
  847. * Unless we're changing the passphrase, the old one (if any) is a
  848. * reasonable default.
  849. */
  850. if (!change_passphrase && old_passphrase && !new_passphrase)
  851. new_passphrase = dupstr(old_passphrase);
  852. /*
  853. * Prompt for a new passphrase if we have been asked to, or if
  854. * we have just generated a key.
  855. */
  856. if (!new_passphrase && (change_passphrase || keytype != NOKEYGEN)) {
  857. prompts_t *p = new_prompts(NULL);
  858. int ret;
  859. p->to_server = FALSE;
  860. p->name = dupstr("New SSH key passphrase");
  861. add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
  862. add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
  863. ret = console_get_userpass_input(p, NULL, 0);
  864. assert(ret >= 0);
  865. if (!ret) {
  866. free_prompts(p);
  867. perror("puttygen: unable to read new passphrase");
  868. return 1;
  869. } else {
  870. if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
  871. free_prompts(p);
  872. fprintf(stderr, "puttygen: passphrases do not match\n");
  873. return 1;
  874. }
  875. new_passphrase = dupstr(p->prompts[0]->result);
  876. free_prompts(p);
  877. }
  878. }
  879. if (new_passphrase && !*new_passphrase) {
  880. sfree(new_passphrase);
  881. new_passphrase = NULL;
  882. }
  883. /*
  884. * Write output.
  885. *
  886. * (In the case where outfile and outfiletmp are both NULL,
  887. * there is no semantic reason to initialise outfilename at
  888. * all; but we have to write _something_ to it or some compiler
  889. * will probably complain that it might be used uninitialised.)
  890. */
  891. if (outfiletmp)
  892. outfilename = filename_from_str(outfiletmp);
  893. else
  894. outfilename = filename_from_str(outfile ? outfile : "");
  895. switch (outtype) {
  896. int ret, real_outtype;
  897. case PRIVATE:
  898. if (sshver == 1) {
  899. assert(ssh1key);
  900. ret = saversakey(outfilename, ssh1key, new_passphrase);
  901. if (!ret) {
  902. fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
  903. return 1;
  904. }
  905. } else {
  906. assert(ssh2key);
  907. ret = ssh2_save_userkey(outfilename, ssh2key, new_passphrase);
  908. if (!ret) {
  909. fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
  910. return 1;
  911. }
  912. }
  913. if (outfiletmp) {
  914. if (!move(outfiletmp, outfile))
  915. return 1; /* rename failed */
  916. }
  917. break;
  918. case PUBLIC:
  919. case PUBLICO:
  920. {
  921. FILE *fp;
  922. if (outfile)
  923. fp = f_open(outfilename, "w", FALSE);
  924. else
  925. fp = stdout;
  926. if (sshver == 1) {
  927. ssh1_write_pubkey(fp, ssh1key);
  928. } else {
  929. if (!ssh2blob) {
  930. assert(ssh2key);
  931. ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
  932. &ssh2bloblen);
  933. }
  934. ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
  935. ssh2blob, ssh2bloblen,
  936. (outtype == PUBLIC ?
  937. SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
  938. SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
  939. }
  940. if (outfile)
  941. fclose(fp);
  942. }
  943. break;
  944. case FP:
  945. {
  946. FILE *fp;
  947. char *fingerprint;
  948. if (sshver == 1) {
  949. assert(ssh1key);
  950. fingerprint = snewn(128, char);
  951. rsa_fingerprint(fingerprint, 128, ssh1key);
  952. } else {
  953. if (ssh2key) {
  954. fingerprint = ssh2_fingerprint(ssh2key->alg,
  955. ssh2key->data);
  956. } else {
  957. assert(ssh2blob);
  958. fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
  959. }
  960. }
  961. if (outfile)
  962. fp = f_open(outfilename, "w", FALSE);
  963. else
  964. fp = stdout;
  965. fprintf(fp, "%s\n", fingerprint);
  966. if (outfile)
  967. fclose(fp);
  968. sfree(fingerprint);
  969. }
  970. break;
  971. case OPENSSH_AUTO:
  972. case OPENSSH_NEW:
  973. case SSHCOM:
  974. assert(sshver == 2);
  975. assert(ssh2key);
  976. random_ref(); /* both foreign key types require randomness,
  977. * for IV or padding */
  978. switch (outtype) {
  979. case OPENSSH_AUTO:
  980. real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
  981. break;
  982. case OPENSSH_NEW:
  983. real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
  984. break;
  985. case SSHCOM:
  986. real_outtype = SSH_KEYTYPE_SSHCOM;
  987. break;
  988. default:
  989. assert(0 && "control flow goof");
  990. }
  991. ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
  992. if (!ret) {
  993. fprintf(stderr, "puttygen: unable to export key\n");
  994. return 1;
  995. }
  996. if (outfiletmp) {
  997. if (!move(outfiletmp, outfile))
  998. return 1; /* rename failed */
  999. }
  1000. break;
  1001. }
  1002. if (old_passphrase) {
  1003. smemclr(old_passphrase, strlen(old_passphrase));
  1004. sfree(old_passphrase);
  1005. }
  1006. if (new_passphrase) {
  1007. smemclr(new_passphrase, strlen(new_passphrase));
  1008. sfree(new_passphrase);
  1009. }
  1010. if (ssh1key)
  1011. freersakey(ssh1key);
  1012. if (ssh2key) {
  1013. ssh2key->alg->freekey(ssh2key->data);
  1014. sfree(ssh2key);
  1015. }
  1016. return 0;
  1017. }
  1018. #ifdef TEST_CMDGEN
  1019. #undef main
  1020. #include <stdarg.h>
  1021. int passes, fails;
  1022. void setup_passphrases(char *first, ...)
  1023. {
  1024. va_list ap;
  1025. char *next;
  1026. nprompts = 0;
  1027. if (first) {
  1028. prompts[nprompts++] = first;
  1029. va_start(ap, first);
  1030. while ((next = va_arg(ap, char *)) != NULL) {
  1031. assert(nprompts < lenof(prompts));
  1032. prompts[nprompts++] = next;
  1033. }
  1034. va_end(ap);
  1035. }
  1036. }
  1037. void test(int retval, ...)
  1038. {
  1039. va_list ap;
  1040. int i, argc, ret;
  1041. char **argv;
  1042. argc = 0;
  1043. va_start(ap, retval);
  1044. while (va_arg(ap, char *) != NULL)
  1045. argc++;
  1046. va_end(ap);
  1047. argv = snewn(argc+1, char *);
  1048. va_start(ap, retval);
  1049. for (i = 0; i <= argc; i++)
  1050. argv[i] = va_arg(ap, char *);
  1051. va_end(ap);
  1052. promptsgot = 0;
  1053. ret = cmdgen_main(argc, argv);
  1054. if (ret != retval) {
  1055. printf("FAILED retval (exp %d got %d):", retval, ret);
  1056. for (i = 0; i < argc; i++)
  1057. printf(" %s", argv[i]);
  1058. printf("\n");
  1059. fails++;
  1060. } else if (promptsgot != nprompts) {
  1061. printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
  1062. for (i = 0; i < argc; i++)
  1063. printf(" %s", argv[i]);
  1064. printf("\n");
  1065. fails++;
  1066. } else {
  1067. passes++;
  1068. }
  1069. }
  1070. void filecmp(char *file1, char *file2, char *fmt, ...)
  1071. {
  1072. /*
  1073. * Ideally I should do file comparison myself, to maximise the
  1074. * portability of this test suite once this application begins
  1075. * running on non-Unix platforms. For the moment, though,
  1076. * calling Unix diff is perfectly adequate.
  1077. */
  1078. char *buf;
  1079. int ret;
  1080. buf = dupprintf("diff -q '%s' '%s'", file1, file2);
  1081. ret = system(buf);
  1082. sfree(buf);
  1083. if (ret) {
  1084. va_list ap;
  1085. printf("FAILED diff (ret=%d): ", ret);
  1086. va_start(ap, fmt);
  1087. vprintf(fmt, ap);
  1088. va_end(ap);
  1089. printf("\n");
  1090. fails++;
  1091. } else
  1092. passes++;
  1093. }
  1094. char *cleanup_fp(char *s)
  1095. {
  1096. char *p;
  1097. if (!strncmp(s, "ssh-", 4)) {
  1098. s += strcspn(s, " \n\t");
  1099. s += strspn(s, " \n\t");
  1100. }
  1101. p = s;
  1102. s += strcspn(s, " \n\t");
  1103. s += strspn(s, " \n\t");
  1104. s += strcspn(s, " \n\t");
  1105. return dupprintf("%.*s", (int)(s - p), p);
  1106. }
  1107. char *get_fp(char *filename)
  1108. {
  1109. FILE *fp;
  1110. char buf[256], *ret;
  1111. fp = fopen(filename, "r");
  1112. if (!fp)
  1113. return NULL;
  1114. ret = fgets(buf, sizeof(buf), fp);
  1115. fclose(fp);
  1116. if (!ret)
  1117. return NULL;
  1118. return cleanup_fp(buf);
  1119. }
  1120. void check_fp(char *filename, char *fp, char *fmt, ...)
  1121. {
  1122. char *newfp;
  1123. if (!fp)
  1124. return;
  1125. newfp = get_fp(filename);
  1126. if (!strcmp(fp, newfp)) {
  1127. passes++;
  1128. } else {
  1129. va_list ap;
  1130. printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
  1131. va_start(ap, fmt);
  1132. vprintf(fmt, ap);
  1133. va_end(ap);
  1134. printf("\n");
  1135. fails++;
  1136. }
  1137. sfree(newfp);
  1138. }
  1139. int main(int argc, char **argv)
  1140. {
  1141. int i;
  1142. static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
  1143. /*
  1144. * Even when this thing is compiled for automatic test mode,
  1145. * it's helpful to be able to invoke it with command-line
  1146. * options for _manual_ tests.
  1147. */
  1148. if (argc > 1)
  1149. return cmdgen_main(argc, argv);
  1150. passes = fails = 0;
  1151. for (i = 0; i < lenof(keytypes); i++) {
  1152. char filename[128], osfilename[128], scfilename[128];
  1153. char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
  1154. char *fp;
  1155. sprintf(filename, "test-%s.ppk", keytypes[i]);
  1156. sprintf(pubfilename, "test-%s.pub", keytypes[i]);
  1157. sprintf(osfilename, "test-%s.os", keytypes[i]);
  1158. sprintf(scfilename, "test-%s.sc", keytypes[i]);
  1159. sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
  1160. sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
  1161. /*
  1162. * Create an encrypted key.
  1163. */
  1164. setup_passphrases("sponge", "sponge", NULL);
  1165. test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
  1166. /*
  1167. * List the public key in OpenSSH format.
  1168. */
  1169. setup_passphrases(NULL);
  1170. test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
  1171. {
  1172. char cmdbuf[256];
  1173. fp = NULL;
  1174. sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
  1175. pubfilename, tmpfilename1);
  1176. if (system(cmdbuf) ||
  1177. (fp = get_fp(tmpfilename1)) == NULL) {
  1178. printf("UNABLE to test fingerprint matching against OpenSSH");
  1179. }
  1180. }
  1181. /*
  1182. * List the public key in IETF/ssh.com format.
  1183. */
  1184. setup_passphrases(NULL);
  1185. test(0, "puttygen", "-p", filename, NULL);
  1186. /*
  1187. * List the fingerprint of the key.
  1188. */
  1189. setup_passphrases(NULL);
  1190. test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
  1191. if (!fp) {
  1192. /*
  1193. * If we can't test fingerprints against OpenSSH, we
  1194. * can at the very least test equality of all the
  1195. * fingerprints we generate of this key throughout
  1196. * testing.
  1197. */
  1198. fp = get_fp(tmpfilename1);
  1199. } else {
  1200. check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
  1201. }
  1202. /*
  1203. * Change the comment of the key; this _does_ require a
  1204. * passphrase owing to the tamperproofing.
  1205. *
  1206. * NOTE: In SSH-1, this only requires a passphrase because
  1207. * of inadequacies of the loading and saving mechanisms. In
  1208. * _principle_, it should be perfectly possible to modify
  1209. * the comment on an SSH-1 key without requiring a
  1210. * passphrase; the only reason I can't do it is because my
  1211. * loading and saving mechanisms don't include a method of
  1212. * loading all the key data without also trying to decrypt
  1213. * the private section.
  1214. *
  1215. * I don't consider this to be a problem worth solving,
  1216. * because (a) to fix it would probably end up bloating
  1217. * PuTTY proper, and (b) SSH-1 is on the way out anyway so
  1218. * it shouldn't be highly significant. If it seriously
  1219. * bothers anyone then perhaps I _might_ be persuadable.
  1220. */
  1221. setup_passphrases("sponge", NULL);
  1222. test(0, "puttygen", "-C", "new-comment", filename, NULL);
  1223. /*
  1224. * Change the passphrase to nothing.
  1225. */
  1226. setup_passphrases("sponge", "", "", NULL);
  1227. test(0, "puttygen", "-P", filename, NULL);
  1228. /*
  1229. * Change the comment of the key again; this time we expect no
  1230. * passphrase to be required.
  1231. */
  1232. setup_passphrases(NULL);
  1233. test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
  1234. /*
  1235. * Export the private key into OpenSSH format; no passphrase
  1236. * should be required since the key is currently unencrypted.
  1237. * For RSA1 keys, this should give an error.
  1238. */
  1239. setup_passphrases(NULL);
  1240. test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
  1241. filename, NULL);
  1242. if (i) {
  1243. /*
  1244. * List the fingerprint of the OpenSSH-formatted key.
  1245. */
  1246. setup_passphrases(NULL);
  1247. test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
  1248. check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
  1249. /*
  1250. * List the public half of the OpenSSH-formatted key in
  1251. * OpenSSH format.
  1252. */
  1253. setup_passphrases(NULL);
  1254. test(0, "puttygen", "-L", osfilename, NULL);
  1255. /*
  1256. * List the public half of the OpenSSH-formatted key in
  1257. * IETF/ssh.com format.
  1258. */
  1259. setup_passphrases(NULL);
  1260. test(0, "puttygen", "-p", osfilename, NULL);
  1261. }
  1262. /*
  1263. * Export the private key into ssh.com format; no passphrase
  1264. * should be required since the key is currently unencrypted.
  1265. * For RSA1 keys, this should give an error.
  1266. */
  1267. setup_passphrases(NULL);
  1268. test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
  1269. filename, NULL);
  1270. if (i) {
  1271. /*
  1272. * List the fingerprint of the ssh.com-formatted key.
  1273. */
  1274. setup_passphrases(NULL);
  1275. test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
  1276. check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
  1277. /*
  1278. * List the public half of the ssh.com-formatted key in
  1279. * OpenSSH format.
  1280. */
  1281. setup_passphrases(NULL);
  1282. test(0, "puttygen", "-L", scfilename, NULL);
  1283. /*
  1284. * List the public half of the ssh.com-formatted key in
  1285. * IETF/ssh.com format.
  1286. */
  1287. setup_passphrases(NULL);
  1288. test(0, "puttygen", "-p", scfilename, NULL);
  1289. }
  1290. if (i) {
  1291. /*
  1292. * Convert from OpenSSH into ssh.com.
  1293. */
  1294. setup_passphrases(NULL);
  1295. test(0, "puttygen", osfilename, "-o", tmpfilename1,
  1296. "-O", "private-sshcom", NULL);
  1297. /*
  1298. * Convert from ssh.com back into a PuTTY key,
  1299. * supplying the same comment as we had before we
  1300. * started to ensure the comparison works.
  1301. */
  1302. setup_passphrases(NULL);
  1303. test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
  1304. "-o", tmpfilename2, NULL);
  1305. /*
  1306. * See if the PuTTY key thus generated is the same as
  1307. * the original.
  1308. */
  1309. filecmp(filename, tmpfilename2,
  1310. "p->o->s->p clear %s", keytypes[i]);
  1311. /*
  1312. * Convert from ssh.com to OpenSSH.
  1313. */
  1314. setup_passphrases(NULL);
  1315. test(0, "puttygen", scfilename, "-o", tmpfilename1,
  1316. "-O", "private-openssh", NULL);
  1317. /*
  1318. * Convert from OpenSSH back into a PuTTY key,
  1319. * supplying the same comment as we had before we
  1320. * started to ensure the comparison works.
  1321. */
  1322. setup_passphrases(NULL);
  1323. test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
  1324. "-o", tmpfilename2, NULL);
  1325. /*
  1326. * See if the PuTTY key thus generated is the same as
  1327. * the original.
  1328. */
  1329. filecmp(filename, tmpfilename2,
  1330. "p->s->o->p clear %s", keytypes[i]);
  1331. /*
  1332. * Finally, do a round-trip conversion between PuTTY
  1333. * and ssh.com without involving OpenSSH, to test that
  1334. * the key comment is preserved in that case.
  1335. */
  1336. setup_passphrases(NULL);
  1337. test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
  1338. filename, NULL);
  1339. setup_passphrases(NULL);
  1340. test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
  1341. filecmp(filename, tmpfilename2,
  1342. "p->s->p clear %s", keytypes[i]);
  1343. }
  1344. /*
  1345. * Check that mismatched passphrases cause an error.
  1346. */
  1347. setup_passphrases("sponge2", "sponge3", NULL);
  1348. test(1, "puttygen", "-P", filename, NULL);
  1349. /*
  1350. * Put a passphrase back on.
  1351. */
  1352. setup_passphrases("sponge2", "sponge2", NULL);
  1353. test(0, "puttygen", "-P", filename, NULL);
  1354. /*
  1355. * Export the private key into OpenSSH format, this time
  1356. * while encrypted. For RSA1 keys, this should give an
  1357. * error.
  1358. */
  1359. if (i == 0)
  1360. setup_passphrases(NULL); /* error, hence no passphrase read */
  1361. else
  1362. setup_passphrases("sponge2", NULL);
  1363. test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
  1364. filename, NULL);
  1365. if (i) {
  1366. /*
  1367. * List the fingerprint of the OpenSSH-formatted key.
  1368. */
  1369. setup_passphrases("sponge2", NULL);
  1370. test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
  1371. check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
  1372. /*
  1373. * List the public half of the OpenSSH-formatted key in
  1374. * OpenSSH format.
  1375. */
  1376. setup_passphrases("sponge2", NULL);
  1377. test(0, "puttygen", "-L", osfilename, NULL);
  1378. /*
  1379. * List the public half of the OpenSSH-formatted key in
  1380. * IETF/ssh.com format.
  1381. */
  1382. setup_passphrases("sponge2", NULL);
  1383. test(0, "puttygen", "-p", osfilename, NULL);
  1384. }
  1385. /*
  1386. * Export the private key into ssh.com format, this time
  1387. * while encrypted. For RSA1 keys, this should give an
  1388. * error.
  1389. */
  1390. if (i == 0)
  1391. setup_passphrases(NULL); /* error, hence no passphrase read */
  1392. else
  1393. setup_passphrases("sponge2", NULL);
  1394. test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
  1395. filename, NULL);
  1396. if (i) {
  1397. /*
  1398. * List the fingerprint of the ssh.com-formatted key.
  1399. */
  1400. setup_passphrases("sponge2", NULL);
  1401. test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
  1402. check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
  1403. /*
  1404. * List the public half of the ssh.com-formatted key in
  1405. * OpenSSH format.
  1406. */
  1407. setup_passphrases("sponge2", NULL);
  1408. test(0, "puttygen", "-L", scfilename, NULL);
  1409. /*
  1410. * List the public half of the ssh.com-formatted key in
  1411. * IETF/ssh.com format.
  1412. */
  1413. setup_passphrases("sponge2", NULL);
  1414. test(0, "puttygen", "-p", scfilename, NULL);
  1415. }
  1416. if (i) {
  1417. /*
  1418. * Convert from OpenSSH into ssh.com.
  1419. */
  1420. setup_passphrases("sponge2", NULL);
  1421. test(0, "puttygen", osfilename, "-o", tmpfilename1,
  1422. "-O", "private-sshcom", NULL);
  1423. /*
  1424. * Convert from ssh.com back into a PuTTY key,
  1425. * supplying the same comment as we had before we
  1426. * started to ensure the comparison works.
  1427. */
  1428. setup_passphrases("sponge2", NULL);
  1429. test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
  1430. "-o", tmpfilename2, NULL);
  1431. /*
  1432. * See if the PuTTY key thus generated is the same as
  1433. * the original.
  1434. */
  1435. filecmp(filename, tmpfilename2,
  1436. "p->o->s->p encrypted %s", keytypes[i]);
  1437. /*
  1438. * Convert from ssh.com to OpenSSH.
  1439. */
  1440. setup_passphrases("sponge2", NULL);
  1441. test(0, "puttygen", scfilename, "-o", tmpfilename1,
  1442. "-O", "private-openssh", NULL);
  1443. /*
  1444. * Convert from OpenSSH back into a PuTTY key,
  1445. * supplying the same comment as we had before we
  1446. * started to ensure the comparison works.
  1447. */
  1448. setup_passphrases("sponge2", NULL);
  1449. test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
  1450. "-o", tmpfilename2, NULL);
  1451. /*
  1452. * See if the PuTTY key thus generated is the same as
  1453. * the original.
  1454. */
  1455. filecmp(filename, tmpfilename2,
  1456. "p->s->o->p encrypted %s", keytypes[i]);
  1457. /*
  1458. * Finally, do a round-trip conversion between PuTTY
  1459. * and ssh.com without involving OpenSSH, to test that
  1460. * the key comment is preserved in that case.
  1461. */
  1462. setup_passphrases("sponge2", NULL);
  1463. test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
  1464. filename, NULL);
  1465. setup_passphrases("sponge2", NULL);
  1466. test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
  1467. filecmp(filename, tmpfilename2,
  1468. "p->s->p encrypted %s", keytypes[i]);
  1469. }
  1470. /*
  1471. * Load with the wrong passphrase.
  1472. */
  1473. setup_passphrases("sponge8", NULL);
  1474. test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
  1475. /*
  1476. * Load a totally bogus file.
  1477. */
  1478. setup_passphrases(NULL);
  1479. test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
  1480. }
  1481. printf("%d passes, %d fails\n", passes, fails);
  1482. return 0;
  1483. }
  1484. #endif