using System;

namespace test
{
	using net.catcert.psis;
	using System.Web;
    using System.Security;
    using System.Security.Cryptography.X509Certificates;

	public class ValidacioCertificatSSL
	{
		public ValidacioCertificatSSL()
		{
		}

		static void Main()  
		{
			// Utilitats
			Utils utils = new Utils();

            X509Certificate2 p12client = null;
            /*
            X509Store store = new X509Store("truststore", StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadWrite);
          
            X509Certificate2 certPSIS = new X509Certificate2("C:/Documents and Settings/jriudavets/Mis documentos/Visual Studio 2005/Projects/TestPSIS/TestPSIS/bin/Release/psis.cer");
            X509Certificate2 ECACC = new X509Certificate2("C:/Documents and Settings/jriudavets/Mis documentos/Visual Studio 2005/Projects/TestPSIS/TestPSIS/bin/Release/EC-ACC.cer");
            X509Certificate2 ECGENCAT = new X509Certificate2("C:/Documents and Settings/jriudavets/Mis documentos/Visual Studio 2005/Projects/TestPSIS/TestPSIS/bin/Release/EC-GENCAT.cer");
            X509Certificate2 ECSAFP = new X509Certificate2("C:/Documents and Settings/jriudavets/Mis documentos/Visual Studio 2005/Projects/TestPSIS/TestPSIS/bin/Release/EC-SAFP.cer");


            X509Certificate2Collection collection = new X509Certificate2Collection();
            collection.Add(certPSIS);
            collection.Add(ECACC);
            collection.Add(ECGENCAT);
            collection.Add(ECSAFP);

            store.AddRange(collection);

            X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
            Console.WriteLine("Store name: {0}", store.Name);
            Console.WriteLine("Store location: {0}", store.Location);
            foreach (X509Certificate2 x509 in storecollection)
            {
                Console.WriteLine("certificate name: {0}", x509.Subject);
            }

            store.Close();
            */
            //X509Store keystore = new X509Store("keystore", StoreLocation.CurrentUser);
            //keystore.Open(OpenFlags.MaxAllowed);
            p12client = new X509Certificate2("C:/Documents and Settings/jriudavets/Mis documentos/Visual Studio 2005/Projects/TestPSIS/TestPSIS/bin/Release/psisauth.p12", "sIikZSmz");
            //keystore.Add(p12client);
         /*
            X509Certificate2Collection storecollectionp12 = (X509Certificate2Collection)keystore.Certificates;
            Console.WriteLine("KeyStore name: {0}", keystore.Name);
            Console.WriteLine("KeyStore location: {0}", keystore.Location);
            foreach (X509Certificate2 x509 in storecollectionp12)
            {
                Console.WriteLine("certificate name: {0}", x509.Subject);
            }

            keystore.Close();

            */




			// Dades de prova
			byte[] certificate = utils.Base64File("C:/Documents and Settings/jriudavets/Mis documentos/Visual Studio 2005/Projects/TestPSIS/TestPSIS/bin/Release/certificate.dat");

			// Inicialització del client
			digitalSignatureService proxy = new digitalSignatureService();

			// Indicar l’adreça del servidor
            proxy.Url = "https://psisbeta.catcert.net/psis/catcert-test/dss-secure";
            proxy.ClientCertificates.Add(p12client);
     

			// Composició del missatge
			X509DataType x509DataType = new X509DataType();

			Object[] certificates = new Object[1];
			certificates[0] = certificate;

			ItemsChoiceType[] certificateType = new ItemsChoiceType[1];
			certificateType[0] = ItemsChoiceType.X509Certificate;

			x509DataType = new X509DataType();
			x509DataType.Items = certificates;
			x509DataType.ItemsElementName = certificateType;

			SignatureObjectTypeOther other = new SignatureObjectTypeOther();
			other.X509Data = x509DataType;

			SignatureObjectType signature = new SignatureObjectType();
			signature.Item = other;

			/*
			 * Peticio d'informacio del Certificat (X509CertificateInfo)
			 *	- GivenName
			 *	- Surname
			 *	- KeyOwnerNif
			 *	- SerialNumber
			 *	- IssuerCA
			 */
			OptionalInputs optInputs = new OptionalInputs();
			ItemsChoiceType6[] types6 = new ItemsChoiceType6[1];
			Object[] objects = new Object[1];

			optInputs.ItemsElementName = types6;
			optInputs.Items = objects;

			// Colleccio dels attributs a demanar
			string[] attributeDesignators = new string[]
			{
				// GivenName
				"urn:oasis:names:tc:dss:1.0:profiles:XSS:certificateAttributes:SubjectDistinguishedName:givenName",

				// Surname
				"urn:oasis:names:tc:dss:1.0:profiles:XSS:certificateAttributes:SubjectDistinguishedName:surname",

				// KeyOwnerNIF
				"urn:catcert:psis:certificateAttributes:KeyOwnerNIF",

				// SerialNumber
				"urn:oasis:names:tc:dss:1.0:profiles:XSS:certificateAttributes:SerialNumber",

				// IssuerCA
				"urn:catcert:psis:certificateAttributes:issuerCA",
			};

			AttributeDesignator[] ads = new AttributeDesignator[ attributeDesignators.Length ];
			types6[0] = ItemsChoiceType6.ReturnX509CertificateInfo;
			objects[0] = ads;

			for( int i = 0; i < attributeDesignators.Length; i++ )
			{
				string attributeDesignator = attributeDesignators[i];

				AttributeDesignator ad = new AttributeDesignator();
				ad.Name = attributeDesignator;

				ads[i] = ad;
			}

			// Creació del missatge DSS
			VerifyRequest requestDocument = new VerifyRequest();

			requestDocument.SignatureObject = signature;
			requestDocument.OptionalInputs = optInputs;

			/*
			 * !!! ATENCIO !!!
			 *	Especificar el profile XSS 
			 */
			requestDocument.Profile = "urn:oasis:names:tc:dss:1.0:profiles:XSS";


			// Visualització de la petició
			Console.WriteLine(utils.MessageToString(requestDocument));

			// Execució del servei
			VerifyResponse responseDocument = proxy.verify (requestDocument);

			// Visualització de la resposta
			Console.WriteLine(utils.MessageToString(responseDocument));


			foreach( object o in responseDocument.OptionalOutputs.Items )
			{
				if ( o.GetType() == typeof(X509CertificateInfo) ) 
				{
					X509CertificateInfo ci = (X509CertificateInfo)o;

					foreach( Attribute a in ci.Attribute )
					{
						Console.WriteLine("Valors de l'atribut {0}", a.Name );

						if ( a.AttributeValue != null )
						{
							/*
							 * !!! ATENCIO !!!
							 *		Els atributs poden ser multivaluats
							 */
							for( int i = 0; i < a.AttributeValue.Length; i++ )
							{
								Console.WriteLine( "\t[{0}] : {1}", i, a.AttributeValue[i] );
							}
						}

						Console.WriteLine("\t---");
					}
				}
			}
		}
	}
}
