Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ICLRStrongName::StrongNameSignatureVerificationEx Method to identify delay signed assemblies

Can anybody please suggest how to use ICLRStrongName::StrongNameSignatureVerificationEx method to identify delay signed assembly. I could not find any example in the internet. I am not understanding how to work with this method.

Please don't refer me any link, I am almost frustrated with different suggestions and different links available in the web. Can anybody please give code sample for the same.

like image 240
Ashok kumar Avatar asked Feb 13 '14 12:02

Ashok kumar


1 Answers

I had the same problem and finally found the time to investigate. I really don't understand why Microsoft did not take the time to write an adequate documentation.

Following link is very helpful and also contains a sample project, even if it also doesn't answer all questions: http://clractivation.codeplex.com/

I'll try to answer all questions with a short code sample. You could use something like this to verify a assembly's strong name:

public bool VerifyStrongName(string assemblyPath, bool force)
{
  if (string.IsNullOrEmpty(assemblyPath))
    throw new ArgumentException(string.Empty, "assemblyPath");

  var host = HostingInteropHelper.GetClrMetaHost<IClrMetaHost>();

  var bufferSize = 100;
  var version = new StringBuilder(bufferSize);
  var result = host.GetVersionFromFile(Assembly.GetExecutingAssembly().Location, version, ref bufferSize);
  if ((HResult)result != HResult.S_OK)
    throw new COMException("Error", result);

  IClrRuntimeInfo info = host.GetRuntime(version.ToString(), new Guid(IID.IClrRuntimeInfo)) as IClrRuntimeInfo;
  ICLRStrongName sn = info.GetInterface(new Guid(CLSID.ClrStrongName), new Guid(IID.IClrStrongName)) as ICLRStrongName;

  var verResult = sn.StrongNameSignatureVerificationEx(assemblyPath, Convert.ToByte(force));
  return Convert.ToBoolean(verResult);
}

You can find all classed and interfaces used in this sample in the ClrActivation sample project.

Additionaly I changed the HRESULT class to be an enum...

  /// <summary>
  /// A set of common, useful HRESULTS, and related functionality
  /// </summary>
  public enum HResult
  {
    /// <summary>
    /// OK/true/Success
    /// </summary>
    S_OK = 0,
    /// <summary>
    /// False
    /// </summary>
    S_FALSE = 1,
    /// <summary>
    /// The method is not implemented
    /// </summary>
    E_NOTIMPL = unchecked((int)0x80004001),
    /// <summary>
    /// The interface is not supported
    /// </summary>
    E_NOINTERFACE = unchecked((int)0x80004002),
    /// <summary>
    /// Bad Pointer
    /// </summary>
    E_POINTER = unchecked((int)0x8004003),
    /// <summary>
    /// General failure HRESULT
    /// </summary>
    E_FAIL = unchecked((int)0x8004005),
    /// <summary>
    /// Invalid Argument
    /// </summary>
    E_INVALIDARG = unchecked((int)0x80070057),
    /// <summary>
    /// Insufficient buffer
    /// </summary>
    ERROR_INSUFFICIENT_BUFFER = unchecked((int)0x8007007A),
    /// <summary>
    /// HRESULT for failure to find or load an appropriate runtime
    /// </summary>
    CLR_E_SHIM_RUNTIMELOAD = unchecked((int)0x80131700),

    SEVERITY = unchecked((int)0x80000000)
  }

...and added the static classed CLSID and IID (took me some time to find the correct CLSID):

  public static class IID
  {
    public const string IClrRuntimeInfo = "BD39D1D2-BA2F-486A-89B0-B4B0CB466891";
    public const string IClrMetaHost = "D332DB9E-B9B3-4125-8207-A14884F53216";
    public const string IClrStrongName = "9FD93CCF-3280-4391-B3A9-96E1CDE77C8D";
    public const string IEnumUnknown = "00000100-0000-0000-C000-000000000046";
  }

  public static class CLSID
  {
    public const string ClrStrongName = "B79B0ACD-F5CD-409b-B5A5-A16244610B92";
  }

I hope I could help you. If you have further questions, don't hesitate to ask.

like image 98
tY Software Avatar answered Oct 23 '22 12:10

tY Software