// Path: src/main/java/com/reachcall/util/ImportKey.java
// public class ImportKey {
//     /**
//      * <p>Creates an InputStream from a file, and fills it with the complete
//      * file. Thus, available() on the returned InputStream will return the
//      * full number of bytes the file contains</p>
//      * @param fname The filename
//      * @return The filled InputStream
//      * @exception IOException, if the Streams couldn't be created.
//      **/
//     private static InputStream fullStream(String fname)
//         throws IOException {
//         FileInputStream fis = new FileInputStream(fname);
//         DataInputStream dis = new DataInputStream(fis);
//         byte[] bytes = new byte[dis.available()];
//         dis.readFully(bytes);
// 
//         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
// 
//         return bais;
//     }
// 
//     /**
//      * <p>Takes two file names for a key and the certificate for the key,
//      * and imports those into a keystore. Optionally it takes an alias
//      * for the key.
//      * <p>The first argument is the filename for the key. The key should be
//      * in PKCS8-format.
//      * <p>The second argument is the filename for the certificate for the key.
//      * <p>If a third argument is given it is used as the alias. If missing,
//      * the key is imported with the alias importkey
//      * <p>The name of the keystore file can be controlled by setting
//      * the keystore property (java -Dkeystore=mykeystore). If no name
//      * is given, the file is named <code>keystore.ImportKey</code>
//      * and placed in your home directory.
//      * @param args [0] Name of the key file, [1] Name of the certificate file
//      * [2] Alias for the key.
//      **/
//     public static void main(String[] args) {
//         // change this if you want another password by default
//         String keypass = "importkey";
// 
//         // change this if you want another alias by default
//         String defaultalias = "importkey";
// 
//         // change this if you want another keystorefile by default
//         String keystorename = System.getProperty("keystore");
// 
//         if (keystorename == null) {
//             keystorename = System.getProperty("user.home") + System.getProperty("file.separator") +
//                 "keystore.ImportKey"; // especially this ;-)
//         }
// 
//         // parsing command line input
//         String keyfile = "";
//         String certfile = "";
// 
//         if ((args.length < 2) || (args.length > 3)) {
//             System.out.println("Usage: java comu.ImportKey keyfile certfile [alias]");
//             System.exit(0);
//         } else {
//             keyfile = args[0];
//             certfile = args[1];
// 
//             if (args.length > 2) {
//                 defaultalias = args[2];
//             }
//         }
//         importKey(keypass, keystorename, keyfile, certfile, defaultalias);
//     }
// 
//     public static void importKey(String keypass, String keystorename, String keyfile, String certfile, String defaultalias) {
//         try {
//             // initializing and clearing keystore 
//             KeyStore ks = KeyStore.getInstance("JKS", "SUN");
//             ks.load(null, keypass.toCharArray());
//             System.out.println("Using keystore-file : " + keystorename);
//             ks.store(new FileOutputStream(keystorename), keypass.toCharArray());
//             ks.load(new FileInputStream(keystorename), keypass.toCharArray());
// 
//             // loading Key
//             InputStream fl = fullStream(keyfile);
//             byte[] key = new byte[fl.available()];
//             KeyFactory kf = KeyFactory.getInstance("RSA");
//             fl.read(key, 0, fl.available());
//             fl.close();
// 
//             PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(key);
//             PrivateKey ff = kf.generatePrivate(keysp);
// 
//             // loading CertificateChain
//             CertificateFactory cf = CertificateFactory.getInstance("X.509");
//             InputStream certstream = fullStream(certfile);
// 
//             Collection c = cf.generateCertificates(certstream);
//             Certificate[] certs = new Certificate[c.toArray().length];
// 
//             if (c.size() == 1) {
//                 certstream = fullStream(certfile);
//                 System.out.println("One certificate, no chain.");
// 
//                 Certificate cert = cf.generateCertificate(certstream);
//                 certs[0] = cert;
//             } else {
//                 System.out.println("Certificate chain length: " + c.size());
//                 certs = (Certificate[]) c.toArray();
//             }
// 
//             // storing keystore
//             ks.setKeyEntry(defaultalias, ff, keypass.toCharArray(), certs);
//             System.out.println("Key and certificate stored.");
//             System.out.println("Alias:" + defaultalias + "  Password:" + keypass);
//             ks.store(new FileOutputStream(keystorename), keypass.toCharArray());
//         } catch (Exception ex) {
//             ex.printStackTrace();
//         }
//     }
// }


import com.reachcall.util.ImportKey;
import java.io.File;
import java.io.IOException;
import com.beust.jcommander.JCommander;
                "-outform", "DER");
        b.inheritIO();
        Process proc = b.start();
       
        int exitCode = proc.waitFor();
        if(exitCode != 0){
            throw new RuntimeException("Conversion of key to der failed. Exit code: "+0);
        }
        return result;
    }
    
    private static File certPemToDer(File certPem) throws IOException, InterruptedException{
        File result = File.createTempFile("certFile", "der");
        result.deleteOnExit();
        //openssl x509 -in cert.pem -inform PEM -out cert.der -outform DE
        ProcessBuilder b = new ProcessBuilder("openssl",
                "x509", "-in", certPem.getAbsolutePath(),
                "-inform", "PEM", "-out", result.getAbsolutePath(),
                "-outform", "DER");
        b.inheritIO();
        Process proc = b.start();
       
        int exitCode = proc.waitFor();
        if(exitCode != 0){
            throw new RuntimeException("Conversion of certificate to der failed. Exit code: "+0);
        }
        return result;
    }
    
    private static void doImport(File keystoreFile, String keystorePassword, String alias, File keyFile, File certFile){
// Path: src/main/java/com/reachcall/util/ImportKey.java
// public class ImportKey {
//     /**
//      * <p>Creates an InputStream from a file, and fills it with the complete
//      * file. Thus, available() on the returned InputStream will return the
//      * full number of bytes the file contains</p>
//      * @param fname The filename
//      * @return The filled InputStream
//      * @exception IOException, if the Streams couldn't be created.
//      **/
//     private static InputStream fullStream(String fname)
//         throws IOException {
//         FileInputStream fis = new FileInputStream(fname);
//         DataInputStream dis = new DataInputStream(fis);
//         byte[] bytes = new byte[dis.available()];
//         dis.readFully(bytes);
// 
//         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
// 
//         return bais;
//     }
// 
//     /**
//      * <p>Takes two file names for a key and the certificate for the key,
//      * and imports those into a keystore. Optionally it takes an alias
//      * for the key.
//      * <p>The first argument is the filename for the key. The key should be
//      * in PKCS8-format.
//      * <p>The second argument is the filename for the certificate for the key.
//      * <p>If a third argument is given it is used as the alias. If missing,
//      * the key is imported with the alias importkey
//      * <p>The name of the keystore file can be controlled by setting
//      * the keystore property (java -Dkeystore=mykeystore). If no name
//      * is given, the file is named <code>keystore.ImportKey</code>
//      * and placed in your home directory.
//      * @param args [0] Name of the key file, [1] Name of the certificate file
//      * [2] Alias for the key.
//      **/
//     public static void main(String[] args) {
//         // change this if you want another password by default
//         String keypass = "importkey";
// 
//         // change this if you want another alias by default
//         String defaultalias = "importkey";
// 
//         // change this if you want another keystorefile by default
//         String keystorename = System.getProperty("keystore");
// 
//         if (keystorename == null) {
//             keystorename = System.getProperty("user.home") + System.getProperty("file.separator") +
//                 "keystore.ImportKey"; // especially this ;-)
//         }
// 
//         // parsing command line input
//         String keyfile = "";
//         String certfile = "";
// 
//         if ((args.length < 2) || (args.length > 3)) {
//             System.out.println("Usage: java comu.ImportKey keyfile certfile [alias]");
//             System.exit(0);
//         } else {
//             keyfile = args[0];
//             certfile = args[1];
// 
//             if (args.length > 2) {
//                 defaultalias = args[2];
//             }
//         }
//         importKey(keypass, keystorename, keyfile, certfile, defaultalias);
//     }
// 
//     public static void importKey(String keypass, String keystorename, String keyfile, String certfile, String defaultalias) {
//         try {
//             // initializing and clearing keystore 
//             KeyStore ks = KeyStore.getInstance("JKS", "SUN");
//             ks.load(null, keypass.toCharArray());
//             System.out.println("Using keystore-file : " + keystorename);
//             ks.store(new FileOutputStream(keystorename), keypass.toCharArray());
//             ks.load(new FileInputStream(keystorename), keypass.toCharArray());
// 
//             // loading Key
//             InputStream fl = fullStream(keyfile);
//             byte[] key = new byte[fl.available()];
//             KeyFactory kf = KeyFactory.getInstance("RSA");
//             fl.read(key, 0, fl.available());
//             fl.close();
// 
//             PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(key);
//             PrivateKey ff = kf.generatePrivate(keysp);
// 
//             // loading CertificateChain
//             CertificateFactory cf = CertificateFactory.getInstance("X.509");
//             InputStream certstream = fullStream(certfile);
// 
//             Collection c = cf.generateCertificates(certstream);
//             Certificate[] certs = new Certificate[c.toArray().length];
// 
//             if (c.size() == 1) {
//                 certstream = fullStream(certfile);
//                 System.out.println("One certificate, no chain.");
// 
//                 Certificate cert = cf.generateCertificate(certstream);
//                 certs[0] = cert;
//             } else {
//                 System.out.println("Certificate chain length: " + c.size());
//                 certs = (Certificate[]) c.toArray();
//             }
// 
//             // storing keystore
//             ks.setKeyEntry(defaultalias, ff, keypass.toCharArray(), certs);
//             System.out.println("Key and certificate stored.");
//             System.out.println("Alias:" + defaultalias + "  Password:" + keypass);
//             ks.store(new FileOutputStream(keystorename), keypass.toCharArray());
//         } catch (Exception ex) {
//             ex.printStackTrace();
//         }
//     }
// }


// Path: src/main/java/com/reachcall/pretty/TLSSetup.java
import com.reachcall.util.ImportKey;
import java.io.File;
import java.io.IOException;
import com.beust.jcommander.JCommander;

                "-outform", "DER");
        b.inheritIO();
        Process proc = b.start();
       
        int exitCode = proc.waitFor();
        if(exitCode != 0){
            throw new RuntimeException("Conversion of key to der failed. Exit code: "+0);
        }
        return result;
    }
    
    private static File certPemToDer(File certPem) throws IOException, InterruptedException{
        File result = File.createTempFile("certFile", "der");
        result.deleteOnExit();
        //openssl x509 -in cert.pem -inform PEM -out cert.der -outform DE
        ProcessBuilder b = new ProcessBuilder("openssl",
                "x509", "-in", certPem.getAbsolutePath(),
                "-inform", "PEM", "-out", result.getAbsolutePath(),
                "-outform", "DER");
        b.inheritIO();
        Process proc = b.start();
       
        int exitCode = proc.waitFor();
        if(exitCode != 0){
            throw new RuntimeException("Conversion of certificate to der failed. Exit code: "+0);
        }
        return result;
    }
    
    private static void doImport(File keystoreFile, String keystorePassword, String alias, File keyFile, File certFile){
        ImportKey.importKey(keystorePassword, keystoreFile.getAbsolutePath(),keyFile.getAbsolutePath(), certFile.getAbsolutePath(), alias );