Skip to content
Snippets Groups Projects
cmdgen.c 36.9 KiB
Newer Older
        if (outfile) {
          fp = f_open(outfilename, "w", false);
          if (!fp) {
            fprintf(stderr, "unable to open output file\n");
            exit(1);
          }
        } else {
          fp = stdout;
        fprintf(fp, "%s\n", fingerprint);
        if (outfile)
            fclose(fp);

        sfree(fingerprint);
        assert(sshver == 2);
        assert(ssh2key);
        random_ref(); /* both foreign key types require randomness,
                       * for IV or padding */
        switch (outtype) {
          case OPENSSH_AUTO:
            real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
            break;
          case OPENSSH_NEW:
            real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
            break;
          case SSHCOM:
            real_outtype = SSH_KEYTYPE_SSHCOM;
            break;
            unreachable("control flow goof");
        ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
        if (!ret) {
            fprintf(stderr, "puttygen: unable to export key\n");
            RETURN(1);
        }
        if (outfiletmp) {
            if (!move(outfiletmp, outfile))
                RETURN(1);              /* rename failed */

      case TEXT: {
        key_components *kc;
        if (sshver == 1) {
            assert(ssh1key);
            kc = rsa_components(ssh1key);
        } else {
            if (ssh2key) {
                kc = ssh_key_components(ssh2key->key);
            } else {
                assert(ssh2blob);

                BinarySource src[1];
                BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(ssh2blob));
                ptrlen algname = get_string(src);
                const ssh_keyalg *alg = find_pubkey_alg_len(algname);
                if (!alg) {
                    fprintf(stderr, "puttygen: cannot extract key components "
                            "from public key of unknown type '%.*s'\n",
                            PTRLEN_PRINTF(algname));
                    RETURN(1);
                }
                ssh_key *sk = ssh_key_new_pub(
                    alg, ptrlen_from_strbuf(ssh2blob));
                kc = ssh_key_components(sk);
                ssh_key_free(sk);
            }
        }

        FILE *fp;
        if (outfile) {
            fp = f_open(outfilename, "w", false);
            if (!fp) {
                fprintf(stderr, "unable to open output file\n");
                exit(1);
            }
        } else {
            fp = stdout;
        }

        for (size_t i = 0; i < kc->ncomponents; i++) {
            if (kc->components[i].is_mp_int) {
                char *hex = mp_get_hex(kc->components[i].mp);
                fprintf(fp, "%s=0x%s\n", kc->components[i].name, hex);
                smemclr(hex, strlen(hex));
                sfree(hex);
            } else {
                fprintf(fp, "%s=\"", kc->components[i].name);
                write_c_string_literal(fp, ptrlen_from_asciz(
                                           kc->components[i].text));
                fputs("\"\n", fp);
            }
        }

        if (outfile)
            fclose(fp);
        key_components_free(kc);
        break;
      }
        smemclr(old_passphrase, strlen(old_passphrase));
        sfree(old_passphrase);
        smemclr(new_passphrase, strlen(new_passphrase));
        sfree(new_passphrase);
    if (ssh1key) {
        freersakey(ssh1key);
        sfree(ssh1key);
    if (ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) {
        sfree(ssh2key->comment);
        if (ssh2key->key)
            ssh_key_free(ssh2key->key);
    if (ssh2blob)
        strbuf_free(ssh2blob);
Simon Tatham's avatar
Simon Tatham committed
    sfree(origcomment);
    if (infilename)
        filename_free(infilename);
    if (infile_lf)
        lf_free(infile_lf);
    if (outfilename)
        filename_free(outfilename);
    sfree(outfiletmp);
    return exit_status;