[DllImport("mscorsn.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.U1)]
static extern bool StrongNameSignatureVerificationEx(
[MarshalAs(UnmanagedType.LPWStr)]string wszFilePath,
[MarshalAs(UnmanagedType.U1)]bool fForceVerification,
[MarshalAs(UnmanagedType.U1)]ref bool pfWasVerified);
None.
This function is exported from mscorsn.dll in v1.0 and v1.1 of the .NET framework, but it will be moved to mscorwks.dll in v2.0. There is a shim in mscoree.dll which will redirect to the appropriate implementation dll, which has the same name.
The fForceVerification / pfWasVerified parameters can be confusion. A good blog entry to read, which explains how these work can be found here: http://blogs.msdn.com/shawnfa/archive/2004/06/07/150378.aspx
See the supporting function below for the code to the CheckToken() function - it verifies that an assembly contains the correct token data.
// check the signature first
bool notForced = false;
bool verified = StrongNameSignatureVerificationEx(assembly, false, ref notForced);
Console.WriteLine("Verified: {0}\nForced: {1}", verified, !notForced);
// check to see if it is a Microsoft assembly
byte[] msClrToken = new byte[] { 0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 };
byte[] msFxToken = new byte[] { 0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a };
bool isMsAsm = CheckToken(assembly, msClrToken) || CheckToken(assembly, msFxToken);
if(isMsAsm && verified && notForced)
Console.WriteLine("Microsoft signed assembly");
else if(isMsAsm && verified && !notForced)
Console.WriteLine("Microsoft delay signed assembly");
else if(isMsAsm && !verified)
Console.WriteLine("Microsoft assembly, modified since signing");
else
Console.WriteLine("Not a Microsoft assembly");
/// <summary>
/// Check an assembly to see if it has the given public key token
/// </summary>
/// <remarks>
/// Does not check to make sure the assembly's signature is valid.
/// Loads the assembly in the LoadFrom context.
/// </remarks>
/// <param name='assembly'>Path to the assembly to check</param>
/// <param name='expectedToken'>Token to search for</param>
/// <exception cref='System.ArgumentNullException'>If assembly or expectedToken are null</exception>
/// <returns>true if the assembly was signed with a key that has this token, false otherwise</returns>
public static bool CheckToken(string assembly, byte[] expectedToken)
{
if (assembly == null)
throw new ArgumentNullException("assembly");
if (expectedToken == null)
throw new ArgumentNullException("expectedToken");
try
{
// Get the public key token of the given assembly
Assembly asm = Assembly.LoadFrom(assembly);
byte[] asmToken = asm.GetName().GetPublicKeyToken();
// Compare it to the given token
if (asmToken.Length != expectedToken.Length)
return false;
for (int i = 0; i < asmToken.Length; i++)
if (asmToken[i] != expectedToken[i])
return false;
return true;
}
catch (System.IO.FileNotFoundException)
{
// couldn't find the assembly
return false;
}
catch (BadImageFormatException)
{
// the given file couldn't get through the loader
return false;
}
}
None