//package IM;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.*;
import java.security.spec.RSAPrivateKeySpec;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAMultiPrimePrivateCrtKey;


class Setup {

    public static void main(String args[]) {
        try {
            String privatefile = "ca.priv";
            String publicfile = "ca.pub";

            PublicKey publicKey = null;
            PrivateKey privateKey = null;
            try {
                KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
                keyGen.initialize(1024);
                KeyPair keypair = keyGen.generateKeyPair();

                //RSAPrivateKeySpec priv = (RSAPrivateKeySpec) keypair.getPrivate();
                //RSAMultiPrimePrivateCrtKey temp = (RSAMultiPrimePrivateCrtKey)priv;
                //System.out.println("P: " + temp.getPrimeExponentP());
                //System.out.println("Modulus: " + priv.getModulus());
                privateKey = keypair.getPrivate();
                publicKey = keypair.getPublic();
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }

            //generate ca's private key and public key.


            FileOutputStream fos = new FileOutputStream(privatefile);
            ObjectOutputStream objout = new ObjectOutputStream(fos);
            objout.writeObject(privateKey);
            objout.close();
            fos.close();


            fos = new FileOutputStream(publicfile);
            objout = new ObjectOutputStream(fos);
            objout.writeObject(publicKey);
            objout.close();
            fos.close();

            setupRevocationList(privateKey);

        } catch (Exception e) {

            System.out.println(e);
            e.printStackTrace();
            System.out.print("Error\n");
        }
    }

    public static void setupRevocationList(PrivateKey privateKey) {
        try {
            Signature signature = Signature.getInstance(Helper.RSA_SIGN);
            Serializable revocationList = new String[0];
            SignedObject signedRlist = new SignedObject(revocationList, privateKey, signature);


            FileOutputStream fos = new FileOutputStream(Helper.R_LIST);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(signedRlist);


        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
            System.out.print("Error\n");
        }

    }
}
