package com.greenautomation.pkeytool;

import java.io.*;
import java.security.*;
import java.security.spec.*;
import java.security.cert.*;
import java.security.cert.Certificate;
import java.util.Map;

/**
 * The pkeytool -importkey 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: ImportKeyCommand.java,v 1.2 2004/08/04 19:09:12 green Exp $
 */
public class ImportKeyCommand implements Command {

    public void run(Map args) throws Exception {
        String keyfile = (String) args.get("-keyfile");
        if (keyfile == null) {
            throw new PKeyToolException("Must specify -keyfile");
        }
        String certfile = (String) args.get("-certfile");
        if (certfile == null) {
            throw new PKeyToolException("Must specify -certfile");
        }
        KeyStore keystore = Utils.openKeyStore(args, true, true);
        String alias = (String) args.get("-alias");
        if (alias == null) {
            alias = "mykey";
        }
        String pass = (String) args.get("-keypass");
        if (pass == null) {
            System.err.println("Enter key password for <" + alias + ">");
            pass = Utils.promptFor(   "        (RETURN if same as keystore password):  ");
            if (pass.length() == 0) {
                pass = (String) args.get("-storepass"); // should not be null
            }
        }
        char[] passChars = pass.toCharArray();

        FileInputStream in = new FileInputStream(keyfile);
        byte[] encodedKey = Utils.readArmored(in, "PRIVATE KEY");

        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(encodedKey);
        KeyFactory factory = KeyFactory.getInstance("DSA");
        PrivateKey key;
        try {
            key = factory.generatePrivate(spec);
        }
        catch (InvalidKeySpecException ex) {
            // might be an RSA key
            factory = KeyFactory.getInstance("RSA");
            key = factory.generatePrivate(spec);
        }

        in = new FileInputStream(certfile);
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        Certificate cert = certFactory.generateCertificate(in);

        keystore.setKeyEntry(alias, key, passChars, new Certificate[] {cert});
        Utils.saveKeyStore(keystore, args);
    }

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

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

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

    public void printHelp(PrintStream s) {
        s.println("-importkey   -keyfile <keyfile> -certfile <certfile>");
        s.println("             [-keystore <keystore>] [-storepass <storepass>]");
        s.println("             [-alias <alias>] [-keypass <keypass>] [-storetype <storetype>]");
    }

}