示例#1
0
        /////////////////////////////////////////////////////
        //                                                 //
        // LoadPFX()                                       //
        //                                                 //
        /////////////////////////////////////////////////////
        //Description:  Uses unmanaged CAPI calls to load
        //              PFX data from a PFX file.
        //
        //Returns:      none
        /////////////////////////////////////////////////////
        internal static bool LoadPFX(string filename, ref CwAgent.Win32Helper.CRYPT_DATA_BLOB ppfx)
        {
            //load the bytes from this file and validate it is a PFX file
            Stream stream = null;

            byte[] pfxdata = null;

            try
            {
                stream = new FileStream(filename, FileMode.Open);
                int datalen = (int)stream.Length;
                pfxdata = new byte[datalen];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(pfxdata, 0, datalen);
                stream.Close();
            }
            catch (Exception)
            {
                return(false);
            }

            if (pfxdata == null || pfxdata.Length == 0)
            {
                return(false);
            }

            //set data in pfx structure
            ppfx.cbData = pfxdata.Length;
            ppfx.pbData = Marshal.AllocHGlobal(pfxdata.Length);
            Marshal.Copy(pfxdata, 0, ppfx.pbData, pfxdata.Length);

            return(true);
        }
示例#2
0
        /////////////////////////////////////////////////////
        //                                                 //
        // GetX509StoreHandleFromPFX()                     //
        //                                                 //
        /////////////////////////////////////////////////////
        //Description:  Uses unmanaged CAPI calls to import a
        //              PKCS-12/PFX crypto file and returns
        //              the embedded certificate.
        //
        //              Note:  this function throws an exception
        //              if the PFX file contains > 1 cert.
        //
        //Returns:      void
        /////////////////////////////////////////////////////
        internal static IntPtr GetX509StoreHandleFromPFX(string filename, string password)
        {
            IntPtr hMemStore = IntPtr.Zero;

            //get pfx from data in file
            CwAgent.Win32Helper.CRYPT_DATA_BLOB ppfx = new CwAgent.Win32Helper.CRYPT_DATA_BLOB();
            if (!LoadPFX(filename, ref ppfx))
            {
                throw new Exception("Failed to load data from PFX file.");
            }

            //try to import to memory store
            hMemStore = CwAgent.Win32Helper.PFXImportCertStore(ref ppfx, password, (uint)CwAgent.Win32Helper.CRYPT_USER_KEYSET);
            password  = null; //mark for garbage collection

            if (hMemStore == IntPtr.Zero)
            {
                Marshal.FreeHGlobal(ppfx.pbData);
                throw new Exception("Failed to import PFX certificate store:  " + CwAgent.Win32Helper.GetLastError32());
            }

            Marshal.FreeHGlobal(ppfx.pbData);

            return(hMemStore);
        }
示例#3
0
        /////////////////////////////////////////////////////
        //                                                 //
        // IsValidPFXPassword()                            //
        //                                                 //
        /////////////////////////////////////////////////////
        //Description:  Uses unmanaged CAPI calls to validate
        //              a given password for the PFX file.
        //
        //Returns:      true if valid
        /////////////////////////////////////////////////////
        internal static bool IsValidPFXPassword(string filename, string pwd)
        {
            bool ret = false;

            CwAgent.Win32Helper.CRYPT_DATA_BLOB ppfx = new CwAgent.Win32Helper.CRYPT_DATA_BLOB();

            if (LoadPFX(filename, ref ppfx))
            {
                if (CwAgent.Win32Helper.PFXVerifyPassword(ref ppfx, pwd, 0))
                {
                    ret = true;
                }
            }

            if (ppfx.pbData != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(ppfx.pbData);
            }

            return(ret);
        }
示例#4
0
        /////////////////////////////////////////////////////
        //                                                 //
        // IsValidPFXPassword()                            //
        //                                                 //
        /////////////////////////////////////////////////////
        //Description:  Uses unmanaged CAPI calls to validate
        //              a given password for the PFX file.
        //
        //Returns:      true if valid
        /////////////////////////////////////////////////////
        internal static bool IsValidPFXPassword(string filename, string pwd)
        {
            bool ret = false;
            CwAgent.Win32Helper.CRYPT_DATA_BLOB ppfx = new CwAgent.Win32Helper.CRYPT_DATA_BLOB();

            if (LoadPFX(filename, ref ppfx))
                if (CwAgent.Win32Helper.PFXVerifyPassword(ref ppfx,pwd,0))
                    ret = true;

            if (ppfx.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(ppfx.pbData);

            return ret;
        }
示例#5
0
        /////////////////////////////////////////////////////
        //                                                 //
        // GetX509StoreHandleFromPFX()                     //
        //                                                 //
        /////////////////////////////////////////////////////
        //Description:  Uses unmanaged CAPI calls to import a
        //              PKCS-12/PFX crypto file and returns
        //              the embedded certificate.
        //
        //              Note:  this function throws an exception
        //              if the PFX file contains > 1 cert.
        //
        //Returns:      void
        /////////////////////////////////////////////////////
        internal static IntPtr GetX509StoreHandleFromPFX(string filename, string password)
        {
            IntPtr hMemStore = IntPtr.Zero;

            //get pfx from data in file
            CwAgent.Win32Helper.CRYPT_DATA_BLOB ppfx = new CwAgent.Win32Helper.CRYPT_DATA_BLOB();
            if (!LoadPFX(filename, ref ppfx))
                throw new Exception("Failed to load data from PFX file.");

            //try to import to memory store
            hMemStore = CwAgent.Win32Helper.PFXImportCertStore(ref ppfx, password, (uint)CwAgent.Win32Helper.CRYPT_USER_KEYSET);
            password = null; //mark for garbage collection

            if (hMemStore == IntPtr.Zero)
            {
                Marshal.FreeHGlobal(ppfx.pbData);
                throw new Exception("Failed to import PFX certificate store:  " + CwAgent.Win32Helper.GetLastError32());
            }

            Marshal.FreeHGlobal(ppfx.pbData);

            return hMemStore;
        }