/////////////////////////////////////////////////////////////////////// #region Native Wrapper Methods private static bool IsOpen( ref bool isOpen, ref string error ) { try { if (VersionOps.IsWindowsOperatingSystem()) { isOpen = UnsafeNativeMethods.GetConsoleWindow() != IntPtr.Zero; return(true); } else { error = "not implemented"; } } catch (Exception e) { error = e.ToString(); } return(false); }
/////////////////////////////////////////////////////////////////////// private static bool Close( ref string error ) { try { if (VersionOps.IsWindowsOperatingSystem()) { if (UnsafeNativeMethods.FreeConsole()) { return(true); } else { error = "failed to free console"; } } else { error = "not implemented"; } } catch (Exception e) { error = e.ToString(); } return(false); }
/////////////////////////////////////////////////////////////////////// private static bool Attach( ref string error ) { try { if (VersionOps.IsWindowsOperatingSystem()) { int processId = UnsafeNativeMethods.ATTACH_PARENT_PROCESS; if (UnsafeNativeMethods.AttachConsole(processId)) { return(true); } else { error = "failed to attach console"; } } else { error = "not implemented"; } } catch (Exception e) { error = e.ToString(); } return(false); }
/////////////////////////////////////////////////////////////////////// #region Private Methods #if !DEBUG private static bool IsStrongNameSigned( string fileName, bool force, ref bool returnValue, ref bool verified, ref string error ) { if (String.IsNullOrEmpty(fileName)) { error = "invalid file name"; return(false); } #if !MONO if (!VersionOps.IsWindowsOperatingSystem()) { error = "not supported on this operating system"; return(false); } try { returnValue = UnsafeNativeMethods.StrongNameSignatureVerificationEx( fileName, force, ref verified); return(true); } catch (Exception e) { error = e.ToString(); } #else error = "not implemented"; #endif return(false); }
/////////////////////////////////////////////////////////////////////// public bool VerifyFile( Configuration configuration, string fileName, bool strongName ) { try { if (!File.Exists(fileName)) { Trace(configuration, String.Format( "File \"{0}\" does not exist.", fileName), TraceCategory); return(false); } /////////////////////////////////////////////////////////////// string error = null; if (strongName) { #if NATIVE && WINDOWS if (VersionOps.IsWindowsOperatingSystem() && !StrongNameEx.IsStrongNameSigned( configuration, fileName, true, ref error)) { Trace(configuration, String.Format( "Assembly in file \"{0}\" is not signed.", fileName), TraceCategory); Trace(configuration, String.Format( "Assembly signature error: {0}", error), TraceCategory); return(false); } #endif /////////////////////////////////////////////////////////// AssemblyName assemblyName = AssemblyName.GetAssemblyName(fileName); if (assemblyName == null) { Trace(configuration, String.Format( "Assembly in file \"{0}\" has no name.", fileName), TraceCategory); return(false); } byte[] filePublicKeyToken = assemblyName.GetPublicKeyToken(); if (!GenericOps <byte> .Equals( filePublicKeyToken, publicKeyToken)) { Trace(configuration, String.Format( "Assembly in file \"{0}\" has incorrect " + "public key token \"{1}\".", fileName, FormatOps.ToHexString(filePublicKeyToken)), TraceCategory); return(false); } } /////////////////////////////////////////////////////////////// byte[] hash = null; if (FileOps.Hash( configuration, "md5", fileName, ref hash, ref error)) { if (!GenericOps <byte> .Equals(hash, md5Hash)) { Trace(configuration, String.Format( "File \"{0}\" MD5 hash mismatch, got: {1}.", fileName, FormatOps.ToHexString(hash)), TraceCategory); return(false); } } else { Trace(configuration, error, TraceCategory); return(false); } /////////////////////////////////////////////////////////////// if (FileOps.Hash( configuration, "sha1", fileName, ref hash, ref error)) { if (!GenericOps <byte> .Equals(hash, sha1Hash)) { Trace(configuration, String.Format( "File \"{0}\" SHA1 hash mismatch, got: {1}.", fileName, FormatOps.ToHexString(hash)), TraceCategory); return(false); } } else { Trace(configuration, error, TraceCategory); return(false); } /////////////////////////////////////////////////////////////// if (FileOps.Hash( configuration, "sha512", fileName, ref hash, ref error)) { if (!GenericOps <byte> .Equals(hash, sha512Hash)) { Trace(configuration, String.Format( "File \"{0}\" SHA512 hash mismatch, got: {1}.", fileName, FormatOps.ToHexString(hash)), TraceCategory); return(false); } } else { Trace(configuration, error, TraceCategory); return(false); } return(true); } catch (Exception e) { Trace(configuration, e, TraceCategory); } return(false); }
/////////////////////////////////////////////////////////////////////// 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); }