public static bool CheckSignature(IntPtr ownerWindow, string filename, bool enableUi, out string errorMessage) { using (var wtd = new WinTrustData(filename) { UIChoice = enableUi ? WinTrustDataUIChoice.All : WinTrustDataUIChoice.None, UIContext = WinTrustDataUIContext.Execute, RevocationChecks = WinTrustDataRevocationChecks.WholeChain, StateAction = WinTrustDataStateAction.Ignore, ProvFlags = WinTrustDataProvFlags.RevocationCheckChain }) { var trustResult = WinTrust.WinVerifyTrust( ownerWindow, new Guid(WinTrust.WINTRUST_ACTION_GENERIC_VERIFY_V2), wtd ); if (trustResult == WinVerifyTrustResult.Success) { errorMessage = null; return(true); } else { var sb = new StringBuilder(1024); var charCount = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, IntPtr.Zero, (uint)trustResult, 0, sb, (uint)sb.Capacity, IntPtr.Zero ); errorMessage = sb.ToString(0, (int)charCount); return(false); } } }
public static bool HasValidSignature(string fileName) { try { if (_isValidCache.Contains(fileName)) { return(true); } var wtd = new WinTrustData(fileName); var guidAction = new Guid(WINTRUST_ACTION_GENERIC_VERIFY_V2); WinVerifyTrustResult result = WinTrust.WinVerifyTrust(INVALID_HANDLE_VALUE, guidAction, wtd); bool ret = (result == WinVerifyTrustResult.Success); if (ret) { _isValidCache.Add(fileName); } #if COAPP_ENGINE_CORE var response = Event <GetResponseInterface> .RaiseFirst(); if (response != null) { response.SignatureValidation(fileName, ret, ret ? Verifier.GetPublisherInformation(fileName)["PublisherName"] : null); } #endif return(ret); } catch (Exception) { return(false); } }
public static uint IsSigned(string path) { WinTrustFileInfo fileInfo = new WinTrustFileInfo() { cbStruct = (uint)Marshal.SizeOf(typeof(WinTrustFileInfo)), pcwszFilePath = Path.GetFullPath(path), hFile = IntPtr.Zero, pgKnownSubject = IntPtr.Zero }; WinTrustData data = new WinTrustData() { cbStruct = (uint)Marshal.SizeOf(typeof(WinTrustData)), dwProvFlags = Convert.ToUInt32(Provider.WTD_SAFER_FLAG), dwStateAction = Convert.ToUInt32(StateAction.WTD_STATEACTION_IGNORE), dwUIChoice = Convert.ToUInt32(UIChoice.WTD_UI_NONE), dwUIContext = 0, dwUnionChoice = Convert.ToUInt32(UnionChoice.WTD_CHOICE_FILE), fdwRevocationChecks = Convert.ToUInt32(RevocationChecks.WTD_REVOKE_NONE), hWVTStateData = IntPtr.Zero, pFile = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WinTrustFileInfo))), pPolicyCallbackData = IntPtr.Zero, pSIPClientData = IntPtr.Zero, pwszURLReference = IntPtr.Zero }; // Potential memory leak. Need to investigate Marshal.StructureToPtr(fileInfo, data.pFile, false); IntPtr pGuid = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid))); IntPtr pData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WinTrustData))); Marshal.StructureToPtr(data, pData, true); Marshal.StructureToPtr(WinTrust.WINTRUST_ACTION_GENERIC_VERIFY_V2, pGuid, true); uint result = WinTrust.WinVerifyTrust(IntPtr.Zero, pGuid, pData); Marshal.FreeHGlobal(pGuid); Marshal.FreeHGlobal(pData); return(result); }