package com.greenautomation.pkeytool;

import java.io.*;
import java.security.*;
import java.util.Map;

/**
 * The pkeytool -exportkey command.<p>
 *
 * Copyright 2003 Green Automation, Inc.<p>
 *
 * This file is part of pkeytool.<p>
 *
 * pkeytool is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.<p>
 *
 * pkeytool is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.<p>
 *
 * You should have received a copy of the GNU General Public License
 * along with pkeytool; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author David Green &lt;green@couchpotato.net&gt;
 * @version $Id: ExportKeyCommand.java,v 1.2 2004/08/04 19:17:10 green Exp $
 */
public class ExportKeyCommand implements Command {

    public void run(Map args) throws Exception {
        boolean rfc = args.get("-rfc") != null;
        KeyStore keystore = Utils.openKeyStore(args, true, false);
        String alias = (String) args.get("-alias");
        if (alias == null) {
            alias = "mykey";
        }
        Key key;
        String pass = (String) args.get("-keypass");
        if (pass == null) {
            // try -storepass
            pass = (String) args.get("-storepass"); // should not return null
            char[] ch = pass.toCharArray();
            try {
                key = keystore.getKey(alias, ch);
            }
            catch (UnrecoverableKeyException ex) {
                // prompt for password
                pass = Utils.promptFor("Enter key password for <" + alias + ">", 3);
                ch = pass.toCharArray();
                key = keystore.getKey(alias, ch);
            }
        }
        else {
            char[] ch = pass.toCharArray();
            key = keystore.getKey(alias, ch);
        }
        String filename = (String) args.get("-file");
        OutputStream out;
        PrintStream ps;
        if (filename != null) {
            out = new FileOutputStream(filename);
            ps = new PrintStream(out, true);
        }
        else {
            out = null;
            ps = System.out;
        }
        if (rfc) {
            Utils.writeArmored(ps, key.getEncoded(), "PRIVATE KEY", 76);
        }
        else {
            ps.write(key.getEncoded());
            ps.flush();
        }
        if (out != null) {
            out.close();
        }
    }

    public String getName() {
        return "-exportkey";
    }

    public String[] getParamsWithArgs() {
        return new String[] {
            "-keystore", "-storepass", "-alias", "-keypass", "-file", "-storetype"
        };
    }

    public String[] getParamsWithoutArgs() {
        return new String[] { "-rfc" };
    }

    public void printHelp(PrintStream s) {
        s.println("-exportkey   [-keystore <keystore>] [-storepass <storepass>]");
        s.println("             [-alias <alias>] [-keypass <keypass>]");
        s.println("             [-file <file>] [-rfc] [-storetype <storetype>]");
    }

}