/////////////////////////////////////////////////////////////////////// private static bool IsFileTrusted( string fileName, IntPtr fileHandle, bool userInterface, bool userPrompt, bool revocation, bool install, ref int returnValue, ref string error ) { if (String.IsNullOrEmpty(fileName)) { error = "invalid file name"; return(false); } #if WINDOWS if (!VersionOps.IsWindowsOperatingSystem()) { error = "not supported on this operating system"; return(false); } try { UnsafeNativeMethods.WINTRUST_FILE_INFO file = new UnsafeNativeMethods.WINTRUST_FILE_INFO(); file.cbStruct = (uint)Marshal.SizeOf( typeof(UnsafeNativeMethods.WINTRUST_FILE_INFO)); file.pcwszFilePath = fileName; file.hFile = fileHandle; file.pgKnownSubject = IntPtr.Zero; IntPtr pFile = IntPtr.Zero; try { pFile = Marshal.AllocCoTaskMem((int)file.cbStruct); if (pFile != IntPtr.Zero) { Marshal.StructureToPtr(file, pFile, false); UnsafeNativeMethods.WINTRUST_DATA winTrustData = new UnsafeNativeMethods.WINTRUST_DATA(); winTrustData.cbStruct = (uint)Marshal.SizeOf( typeof(UnsafeNativeMethods.WINTRUST_DATA)); winTrustData.pPolicyCallbackData = IntPtr.Zero; winTrustData.pSIPClientData = IntPtr.Zero; winTrustData.dwUIChoice = userInterface && userPrompt ? UnsafeNativeMethods.WTD_UI_ALL : UnsafeNativeMethods.WTD_UI_NONE; winTrustData.fdwRevocationChecks = revocation ? UnsafeNativeMethods.WTD_REVOKE_WHOLECHAIN : UnsafeNativeMethods.WTD_REVOKE_NONE; winTrustData.dwUnionChoice = UnsafeNativeMethods.WTD_CHOICE_FILE; winTrustData.pFile = pFile; winTrustData.dwStateAction = UnsafeNativeMethods.WTD_STATEACTION_IGNORE; winTrustData.hWVTStateData = IntPtr.Zero; winTrustData.pwszURLReference = null; winTrustData.dwProvFlags = UnsafeNativeMethods.WTD_SAFER_FLAG; winTrustData.dwUIContext = install ? UnsafeNativeMethods.WTD_UICONTEXT_INSTALL : UnsafeNativeMethods.WTD_UICONTEXT_EXECUTE; IntPtr hWnd = userInterface ? IntPtr.Zero : INVALID_HANDLE_VALUE; Guid actionId = GetActionId(); returnValue = UnsafeNativeMethods.WinVerifyTrust( hWnd, actionId, ref winTrustData); return(true); } else { error = "out of memory"; } } finally { if (pFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(pFile); pFile = IntPtr.Zero; } } } catch (Exception e) { error = e.ToString(); } #else error = "not implemented"; #endif return(false); }
/////////////////////////////////////////////////////////////////////// private static ReturnCode IsFileTrusted( string fileName, IntPtr fileHandle, bool userInterface, bool userPrompt, bool revocation, bool install, ref int returnValue, ref Result error ) { if (String.IsNullOrEmpty(fileName)) { error = "invalid file name"; return(ReturnCode.Error); } #if WINDOWS if (!PlatformOps.IsWindowsOperatingSystem()) { error = "not supported on this operating system"; return(ReturnCode.Error); } try { UnsafeNativeMethods.WINTRUST_FILE_INFO file = new UnsafeNativeMethods.WINTRUST_FILE_INFO(); file.cbStruct = (uint)Marshal.SizeOf( typeof(UnsafeNativeMethods.WINTRUST_FILE_INFO)); file.pcwszFilePath = fileName; file.hFile = fileHandle; file.pgKnownSubject = IntPtr.Zero; IntPtr pFile = IntPtr.Zero; try { pFile = Marshal.AllocCoTaskMem((int)file.cbStruct); if (pFile != IntPtr.Zero) { Marshal.StructureToPtr(file, pFile, false); UnsafeNativeMethods.WINTRUST_DATA winTrustData = new UnsafeNativeMethods.WINTRUST_DATA(); winTrustData.cbStruct = (uint)Marshal.SizeOf( typeof(UnsafeNativeMethods.WINTRUST_DATA)); winTrustData.pPolicyCallbackData = IntPtr.Zero; winTrustData.pSIPClientData = IntPtr.Zero; winTrustData.dwUIChoice = userInterface && userPrompt ? UnsafeNativeMethods.WTD_UI_ALL : UnsafeNativeMethods.WTD_UI_NONE; winTrustData.fdwRevocationChecks = revocation ? UnsafeNativeMethods.WTD_REVOKE_WHOLECHAIN : UnsafeNativeMethods.WTD_REVOKE_NONE; winTrustData.dwUnionChoice = UnsafeNativeMethods.WTD_CHOICE_FILE; winTrustData.pFile = pFile; winTrustData.dwStateAction = UnsafeNativeMethods.WTD_STATEACTION_IGNORE; winTrustData.hWVTStateData = IntPtr.Zero; winTrustData.pwszURLReference = null; winTrustData.dwProvFlags = UnsafeNativeMethods.WTD_SAFER_FLAG; winTrustData.dwUIContext = install ? UnsafeNativeMethods.WTD_UICONTEXT_INSTALL : UnsafeNativeMethods.WTD_UICONTEXT_EXECUTE; IntPtr hWnd = userInterface ? WindowOps.GetInteractiveHandle() : INVALID_HANDLE_VALUE; Guid actionId = GetActionId(); returnValue = UnsafeNativeMethods.WinVerifyTrust( hWnd, actionId, ref winTrustData); return(ReturnCode.Ok); } else { error = "out of memory"; } } finally { if (pFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(pFile); pFile = IntPtr.Zero; } } } catch (Exception e) { error = e; } #else error = "not implemented"; #endif TraceOps.DebugTrace(String.Format( "IsFileTrusted: file {0} trust failure, " + "userInterface = {1}, revocation = {2}, " + "install = {3}, returnValue = {4}, error = {5}", FormatOps.WrapOrNull(fileName), userInterface, revocation, install, returnValue, FormatOps.WrapOrNull(error)), typeof(SecurityOps).Name, TracePriority.SecurityError); return(ReturnCode.Error); }