Exemplo n.º 1
0
        static void TestCrypto32()
        {
            IntPtr hCertCntxt = IntPtr.Zero;
            IntPtr hStore     = IntPtr.Zero;

            hStore = Crypto32.CertOpenStore(Crypto32.CERT_STORE_PROV_SYSTEM,
                                            Crypto32.MY_ENCODING_TYPE,
                                            IntPtr.Zero,
                                            Crypto32.CERT_SYSTEM_STORE_CURRENT_USER,
                                            MY);

            Console.WriteLine("Store Handle:\t0x{0:X}", hStore.ToInt64());

            String sha1Hex = "3bd3f17836bd00f8a756e6c53fca48539da2f042";

            // Convert to bin
            int tam = sha1Hex.Length / 2;

            byte[] sha1Bin = new byte[tam];

            int aux = 0;

            for (int i = 0; i < tam; ++i)
            {
                String str = sha1Hex.Substring(aux, 2);
                sha1Bin[i] = (byte)Convert.ToInt32(str, 16);
                aux        = aux + 2;
            }

            Crypto32.CRYPTOAPI_BLOB cryptBlob;
            cryptBlob.cbData = sha1Bin.Length;
            GCHandle h1 = default(GCHandle);
            GCHandle h2 = default(GCHandle);

            try
            {
                h1 = GCHandle.Alloc(sha1Bin, GCHandleType.Pinned);
                cryptBlob.pbData = h1.AddrOfPinnedObject();
                h2         = GCHandle.Alloc(cryptBlob, GCHandleType.Pinned);
                hCertCntxt = Crypto32.CertFindCertificateInStore(
                    hStore,
                    Crypto32.MY_ENCODING_TYPE,
                    0,
                    Crypto32.CERT_FIND_SHA1_HASH,
                    h2.AddrOfPinnedObject(),
                    IntPtr.Zero);
            }
            finally
            {
                if (h1 != default(GCHandle))
                {
                    h1.Free();
                }
                if (h2 != default(GCHandle))
                {
                    h2.Free();
                }
            }

            if (hCertCntxt != IntPtr.Zero)
            {  //use certcontext from managed code
                Console.WriteLine("CertContext:\t0x{0:X}", hCertCntxt.ToInt64());
                X509Certificate foundcert = new X509Certificate(hCertCntxt);
                Console.WriteLine("\nFound certificate with Thumbprint \"{0}\"", sha1Hex);
                Console.WriteLine("SubjectName:\t{0}", foundcert.GetName());
                Console.WriteLine("Serial No:\t{0}", foundcert.GetSerialNumberString());
                Console.WriteLine("HashString:\t{0}", foundcert.GetCertHashString());
            }
            else
            {
                Console.WriteLine("Could not find certificate containing Thumbprint \"{0}\"", sha1Hex);
            }

            if (hCertCntxt != IntPtr.Zero)
            {
                Crypto32.CertFreeCertificateContext(hCertCntxt);
            }
            if (hStore != IntPtr.Zero)
            {
                Crypto32.CertCloseStore(hStore, 0);
            }

            //IntPtr hSysStore = IntPtr.Zero;
            //IntPtr hCertCntxt = IntPtr.Zero;

            //hSysStore = Crypto32.CertOpenSystemStore(IntPtr.Zero, MY);
            //Console.WriteLine("Store Handle:\t0x{0:X}", hSysStore.ToInt32());

            //if (hSysStore != IntPtr.Zero)
            //{
            //    hCertCntxt = Crypto32.CertFindCertificateInStore(
            //        hSysStore,
            //        MY_ENCODING_TYPE,
            //        0,
            //        CERT_FIND_SUBJECT_STR,
            //        lpszCertSubject,
            //        IntPtr.Zero);

            //    if (hCertCntxt != IntPtr.Zero)
            //    {  //use certcontext from managed code
            //        Console.WriteLine("CertContext:\t0x{0:X}", hCertCntxt.ToInt32());
            //        X509Certificate foundcert = new X509Certificate(hCertCntxt);
            //        Console.WriteLine("\nFound certificate with SubjectName string \"{0}\"", lpszCertSubject);
            //        Console.WriteLine("SubjectName:\t{0}", foundcert.GetName());
            //        Console.WriteLine("Serial No:\t{0}", foundcert.GetSerialNumberString());
            //        Console.WriteLine("HashString:\t{0}", foundcert.GetCertHashString());
            //    }
            //    else
            //        Console.WriteLine("Could not find SubjectName containing string \"{0}\"", lpszCertSubject);
            //}
            ////-------  Clean Up  -----------
            //if (hCertCntxt != IntPtr.Zero)
            //    Crypto32.CertFreeCertificateContext(hCertCntxt);
            //if (hSysStore != IntPtr.Zero)
            //    Crypto32.CertCloseStore(hSysStore, 0);
        }
Exemplo n.º 2
0
        static X509Certificate GetCert()
        {
            IntPtr hCertCntxt = IntPtr.Zero;
            IntPtr hStore     = IntPtr.Zero;

            hStore = Crypto32.CertOpenStore(Crypto32.CERT_STORE_PROV_SYSTEM,
                                            Crypto32.MY_ENCODING_TYPE,
                                            IntPtr.Zero,
                                            Crypto32.CERT_SYSTEM_STORE_CURRENT_USER,
                                            MY);

            String sha1Hex = "3bd3f17836bd00f8a756e6c53fca48539da2f042";

            // Convert to bin
            int tam = sha1Hex.Length / 2;

            byte[] sha1Bin = new byte[tam];

            int aux = 0;

            for (int i = 0; i < tam; ++i)
            {
                String str = sha1Hex.Substring(aux, 2);
                sha1Bin[i] = (byte)Convert.ToInt32(str, 16);
                aux        = aux + 2;
            }

            Crypto32.CRYPTOAPI_BLOB cryptBlob;
            cryptBlob.cbData = sha1Bin.Length;
            GCHandle h1 = default(GCHandle);
            GCHandle h2 = default(GCHandle);

            try
            {
                h1 = GCHandle.Alloc(sha1Bin, GCHandleType.Pinned);
                cryptBlob.pbData = h1.AddrOfPinnedObject();
                h2         = GCHandle.Alloc(cryptBlob, GCHandleType.Pinned);
                hCertCntxt = Crypto32.CertFindCertificateInStore(
                    hStore,
                    Crypto32.MY_ENCODING_TYPE,
                    0,
                    Crypto32.CERT_FIND_SHA1_HASH,
                    h2.AddrOfPinnedObject(),
                    IntPtr.Zero);
            }
            finally
            {
                if (h1 != default(GCHandle))
                {
                    h1.Free();
                }
                if (h2 != default(GCHandle))
                {
                    h2.Free();
                }
            }

            X509Certificate cert = null;

            if (hCertCntxt != IntPtr.Zero)
            {
                cert = new X509Certificate(hCertCntxt);
            }

            if (hCertCntxt != IntPtr.Zero)
            {
                Crypto32.CertFreeCertificateContext(hCertCntxt);
            }
            if (hStore != IntPtr.Zero)
            {
                Crypto32.CertCloseStore(hStore, 0);
            }

            return(cert);
        }