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