| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Text; |
|---|
| 25 | using System.ComponentModel; |
|---|
| 26 | using System.Security.Principal; |
|---|
| 27 | using System.Runtime.InteropServices; |
|---|
| 28 | using Microsoft.Win32.SafeHandles; |
|---|
| 29 | |
|---|
| 30 | namespace Eraser.Util |
|---|
| 31 | { |
|---|
| 32 | public static class Security |
|---|
| 33 | { |
|---|
| 34 | /// <summary> |
|---|
| 35 | /// Checks whether the current process is running with administrative |
|---|
| 36 | /// privileges. |
|---|
| 37 | /// </summary> |
|---|
| 38 | /// <returns>True if the user is an administrator. This only returns |
|---|
| 39 | /// true under Vista when UAC is enabled and the process is elevated.</returns> |
|---|
| 40 | public static bool IsAdministrator() |
|---|
| 41 | { |
|---|
| 42 | WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); |
|---|
| 43 | return principal.IsInRole(WindowsBuiltInRole.Administrator); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /// <summary> |
|---|
| 47 | /// Verifies the Authenticode signature in a file. |
|---|
| 48 | /// </summary> |
|---|
| 49 | /// <param name="pathToFile">The file to verify.</param> |
|---|
| 50 | /// <returns>True if the file contains a valid Authenticode certificate.</returns> |
|---|
| 51 | public static bool VerifyAuthenticode(string pathToFile) |
|---|
| 52 | { |
|---|
| 53 | IntPtr unionPointer = IntPtr.Zero; |
|---|
| 54 | |
|---|
| 55 | try |
|---|
| 56 | { |
|---|
| 57 | NativeMethods.WINTRUST_FILE_INFO fileinfo = new NativeMethods.WINTRUST_FILE_INFO(); |
|---|
| 58 | fileinfo.cbStruct = (uint)Marshal.SizeOf(typeof(NativeMethods.WINTRUST_FILE_INFO)); |
|---|
| 59 | fileinfo.pcwszFilePath = pathToFile; |
|---|
| 60 | |
|---|
| 61 | NativeMethods.WINTRUST_DATA data = new NativeMethods.WINTRUST_DATA(); |
|---|
| 62 | data.cbStruct = (uint)Marshal.SizeOf(typeof(NativeMethods.WINTRUST_DATA)); |
|---|
| 63 | data.dwUIChoice = NativeMethods.WINTRUST_DATA.UIChoices.WTD_UI_NONE; |
|---|
| 64 | data.fdwRevocationChecks = NativeMethods.WINTRUST_DATA.RevocationChecks.WTD_REVOKE_NONE; |
|---|
| 65 | data.dwUnionChoice = NativeMethods.WINTRUST_DATA.UnionChoices.WTD_CHOICE_FILE; |
|---|
| 66 | unionPointer = data.pUnion = Marshal.AllocHGlobal((int)fileinfo.cbStruct); |
|---|
| 67 | Marshal.StructureToPtr(fileinfo, data.pUnion, false); |
|---|
| 68 | |
|---|
| 69 | Guid guid = NativeMethods.WINTRUST_ACTION_GENERIC_VERIFY_V2; |
|---|
| 70 | return NativeMethods.WinVerifyTrust(IntPtr.Zero, ref guid, ref data) == 0; |
|---|
| 71 | } |
|---|
| 72 | finally |
|---|
| 73 | { |
|---|
| 74 | if (unionPointer != IntPtr.Zero) |
|---|
| 75 | Marshal.FreeHGlobal(unionPointer); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /// <summary> |
|---|
| 80 | /// Gets a value indicating whether the assembly manifest at the supplied |
|---|
| 81 | /// path contains a strong name signature. |
|---|
| 82 | /// </summary> |
|---|
| 83 | /// <param name="assemblyPath">The path to the portable executable (.exe or |
|---|
| 84 | /// .dll) file for the assembly to be verified.</param> |
|---|
| 85 | /// <returns>True if the verification was successful; otherwise, false.</returns> |
|---|
| 86 | /// <remarks>VerifyStrongName is a utility function to check the validity |
|---|
| 87 | /// of an assembly, taking into account registry settings.</remarks> |
|---|
| 88 | public static bool VerifyStrongName(string assemblyPath) |
|---|
| 89 | { |
|---|
| 90 | bool wasVerified = false; |
|---|
| 91 | return NativeMethods.StrongNameSignatureVerificationEx(assemblyPath, false, |
|---|
| 92 | out wasVerified) && wasVerified; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | } |
|---|