コード例 #1
0
        internal static unsafe IntPtr CreateCryptAttributes(CryptographicAttributeObjectCollection attributes)
        {
            if (attributes.Count == 0)
            {
                return(IntPtr.Zero);
            }
            uint num  = 0;
            uint num2 = AlignedLength((uint)Marshal.SizeOf(typeof(I_CRYPT_ATTRIBUTE)));
            uint num3 = AlignedLength((uint)Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)));
            CryptographicAttributeObjectEnumerator enumerator = attributes.GetEnumerator();

            while (enumerator.MoveNext())
            {
                CryptographicAttributeObject current = enumerator.Current;
                num += num2;
                num += AlignedLength((uint)(current.Oid.Value.Length + 1));
                AsnEncodedDataEnumerator enumerator2 = current.Values.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    AsnEncodedData data = enumerator2.Current;
                    num += num3;
                    num += AlignedLength((uint)data.RawData.Length);
                }
            }
            System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)num));
            I_CRYPT_ATTRIBUTE *i_crypt_attributePtr = (I_CRYPT_ATTRIBUTE *)handle.DangerousGetHandle();
            IntPtr             ptr = new IntPtr(((long)handle.DangerousGetHandle()) + (num2 * attributes.Count));
            CryptographicAttributeObjectEnumerator enumerator3 = attributes.GetEnumerator();

            while (enumerator3.MoveNext())
            {
                CryptographicAttributeObject obj3 = enumerator3.Current;
                byte * numPtr = (byte *)ptr;
                byte[] bytes  = new byte[obj3.Oid.Value.Length + 1];
                System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *cryptoapi_blobPtr = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)(numPtr + AlignedLength((uint)bytes.Length));
                i_crypt_attributePtr->pszObjId = (IntPtr)numPtr;
                i_crypt_attributePtr->cValue   = (uint)obj3.Values.Count;
                i_crypt_attributePtr->rgValue  = (IntPtr)cryptoapi_blobPtr;
                Encoding.ASCII.GetBytes(obj3.Oid.Value, 0, obj3.Oid.Value.Length, bytes, 0);
                Marshal.Copy(bytes, 0, i_crypt_attributePtr->pszObjId, bytes.Length);
                IntPtr destination = new IntPtr(((long)((ulong)cryptoapi_blobPtr)) + (obj3.Values.Count * num3));
                AsnEncodedDataEnumerator enumerator4 = obj3.Values.GetEnumerator();
                while (enumerator4.MoveNext())
                {
                    byte[] rawData = enumerator4.Current.RawData;
                    if (rawData.Length > 0)
                    {
                        cryptoapi_blobPtr->cbData = (uint)rawData.Length;
                        cryptoapi_blobPtr->pbData = destination;
                        Marshal.Copy(rawData, 0, destination, rawData.Length);
                        destination = new IntPtr(((long)destination) + AlignedLength((uint)rawData.Length));
                    }
                    cryptoapi_blobPtr++;
                }
                i_crypt_attributePtr++;
                ptr = destination;
            }
            GC.SuppressFinalize(handle);
            return(handle.DangerousGetHandle());
        }
コード例 #2
0
    static void Main()
    {
        //The following example demonstrates the usage the AsnEncodedData classes.
        // Asn encoded data is read from the extensions of an X509 certificate.
        try
        {
            // Open the certificate store.
            X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
            X509Certificate2Collection collection  = (X509Certificate2Collection)store.Certificates;
            X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
            // Select one or more certificates to display extensions information.
            X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Certificate Select", "Select certificates from the following list to get extension information on that certificate", X509SelectionFlag.MultiSelection);

            // Create a new AsnEncodedDataCollection object.
            AsnEncodedDataCollection asncoll = new AsnEncodedDataCollection();
            for (int i = 0; i < scollection.Count; i++)
            {
                // Display certificate information.
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Certificate name: {0}", scollection[i].GetName());
                Console.ResetColor();
                // Display extensions information.
                foreach (X509Extension extension in scollection[i].Extensions)
                {
                    // Create an AsnEncodedData object using the extensions information.
                    AsnEncodedData asndata = new AsnEncodedData(extension.Oid, extension.RawData);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Extension type: {0}", extension.Oid.FriendlyName);
                    Console.WriteLine("Oid value: {0}", asndata.Oid.Value);
                    Console.WriteLine("Raw data length: {0} {1}", asndata.RawData.Length, Environment.NewLine);
                    Console.ResetColor();
                    Console.WriteLine(asndata.Format(true));
                    Console.WriteLine(Environment.NewLine);
                    // Add the AsnEncodedData object to the AsnEncodedDataCollection object.
                    asncoll.Add(asndata);
                }
                Console.WriteLine(Environment.NewLine);
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Number of AsnEncodedData items in the collection: {0} {1}", asncoll.Count, Environment.NewLine);
            Console.ResetColor();

            store.Close();
            //Create an enumerator for moving through the collection.
            AsnEncodedDataEnumerator asne = asncoll.GetEnumerator();
            //You must execute a MoveNext() to get to the first item in the collection.
            asne.MoveNext();
            // Write out AsnEncodedData in the collection.
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("First AsnEncodedData in the collection: {0}", asne.Current.Format(true));
            Console.ResetColor();

            asne.MoveNext();
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("Second AsnEncodedData in the collection: {0}", asne.Current.Format(true));
            Console.ResetColor();
            //Return index in the collection to the beginning.
            asne.Reset();
        }
        catch (CryptographicException)
        {
            Console.WriteLine("Information could not be written out for this certificate.");
        }
    }