package testgenerator;
import iaik.pkcs.pkcs12.CertificateBag;
import iaik.pkcs.pkcs12.KeyBag;
import iaik.pkcs.pkcs12.PKCS12;
import iaik.security.ecc.ecdsa.ECPrivateKey;
import iaik.security.ecc.ecdsa.ECPublicKey;
import iaik.security.ecc.math.ecgroup.ECPoint;

import java.io.FileInputStream;


/**
 * @author stephan
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class TestPKCS12 {

/*
	public static void main(String[] args) throws Exception {
		
//		String file = "C:/Daten/MOAPhase2/additionalFiles/keys/TestUser.p12";
//		String password = "testuser";
//		String file = "C:\\Certs\\grill(1234).p12";
//		String password = "1234";

		// SL eCard Demo P12 Files
		String file = "C:\\Daten\\MOAPhase2\\Keys\\sl2002mmdd.ecard.sicher(ego).p12";
		String password = "ego";
//		String file = "C:\\Daten\\MOAPhase2\\Keys\\sl2002mmdd.ecard.normal(ego).p12";
//		String password = "ego";

		// SL Personalausweis Demo P12 Files
//		String file = "C:\\Daten\\MOAPhase2\\Keys\\sicher-demo(buergerkarte).p12";
//		String password = "buergerkarte";
//		String file = "C:\\Daten\\MOAPhase2\\Keys\\normal().p12";
//		String password = "";
		
		IAIK.addAsJDK14Provider();

		FileInputStream pkcs12FIS = new FileInputStream(file);
		PKCS12 pkcs12 = new PKCS12(pkcs12FIS);
		if (!pkcs12.verify(password.toCharArray()))
		{
			System.out.println("could not PKCS12.verify file " + file + " with password " + password);
		} else
		{
			System.out.println("could PKCS12.verify file " + file + " with password " + password);
		}
			
		pkcs12.decrypt(password.toCharArray());
		System.out.println("could PKCS12.decrypt file " + file + " with password " + password);
		
		KeyBag[] keyBags = pkcs12.getKeyBags();
		System.out.println("PKCS12.getKeyBags(): " + keyBags.length + " KeyBags found");
		
		RSAPrivateKey privKey = (RSAPrivateKey) keyBags[0].getPrivateKey();
		System.out.println("getFriendlyName(): " + keyBags[0].getFriendlyName() );		
		
		X509Certificate[] certBag = CertificateBag.getCertificates(pkcs12.getCertificateBags());
		System.out.println("PKCS12.getCertificates(): " + certBag.length + " certificates found");
		
		System.out.println("certBag[0]: " + certBag[0]);

	}
*/

   public static void main(String[] args) throws Exception {
   		// von Wolfgang Bauer
       iaik.security.provider.IAIK.addAsProvider();
       iaik.security.ecc.provider.ECCProvider.addAsProvider();
	
	   final String FILENAME = "sl2002mmdd.ecard.sicher.p12";
	   final char[] PASSWORD = "ego".toCharArray();
//	   public final static String FILENAME = "sl2002mmdd.ecard.sicher.p12";
//	   public final static char[] PASSWORD = "ego".toCharArray();
	
	     System.out.println("Checking");
	     FileInputStream fis = new FileInputStream(FILENAME);
	     PKCS12 p12 = new PKCS12(fis);
	     p12.decrypt(PASSWORD);
	     CertificateBag[] cb = p12.getCertificateBags();
	
	     KeyBag kb = p12.getKeyBag();
	     ECPrivateKey pk = (ECPrivateKey) kb.getPrivateKey();
	
	     ECPoint p = pk.getParameter().getG();
	     p.multiply(pk.getS());
	
	     ECPoint p2 = ((ECPublicKey)cb[0].getCertificate().getPublicKey()).getW();
	     if (p.equals(p2) ) {
	       System.out.println("Check OK");
	     } else {
	       System.out.println("Nix wars");
	     }
	     cb[0].getCertificate().verify();
	
	}



}