How to Demand several StrongNameIdentityPermissions “at the same time” in 1.0 and 1.1.

Problem Statement:

Code Access Security provides developers with numerous ways of protecting their methods from unauthorized or untrusted callers, including usage of caller's StrongName signature to identify it.

So if one would like to make sure that all the callers of some method are signed with particular key [what is almost equivalent to being shipped by particular publisher], he/she would probably do something like this:

[method: StrongNameIdentityPermissionAttribute(SecurityAction.Demand, PublicKey = "0x002400...")]

public int ProtectedMethod()





However, in practice this approach is not feasible, because almost always there are some other callers on the stack. Even though they may be considered valid, [e.g., code from System.dll, or unmanaged IE "frame"], they possess different public keys and thus fail the Demand.

So typically people use LinkDemand instead of normal Demand, what obviously gives much lesser protection as:

a) LinkDemand checks only immediate caller, thus opening the door for luring attack;

b) It is enforced during Jit-time only.

Additionally, it quite poorly solves the problem of handling several "valid" callers. In CLR version 2.0 there is a workaround for this, but in earlier versions people have to create a LinkDemand-protected wrapper for each caller key per each protected method. Such a hassle!

Proposed Solution:

However, even CLR 1.0 posesses classes that may be used to emulate full Demand functionality. The sample below shows how to do it.

[Disclaimer: although I verified that it builds and runs for me, I do not accept any responsibility for consequences of using this code or any parts of it]

Note, that you may want to replace hardcoded public keys in the sample. Use "sn.exe -Tp" to extract them from assemblies, and "sn.exe -tp" from key files.



// Comment this out if you don't want detailed output:

#define VERBOSE

using System;

using System.Security;

using System.Security.Permissions;

using System.Security.Policy;

using System.Reflection;

using System.Diagnostics;


This sample demonstrates how you can effectively check that all the callers of your method belong to a set of assemblies signed with StrongName keys you consider valid.

Effectively, this works pretty much as regular Demand for StrongNameIdentityPermission. However, regular Demand allows to query callers for one key only at a time, so if at least one of the callers posesses different key, normal Demand fails. This demo allows you circumvent this problem and "Demand" a set of keys at a time.

The idea put into it is very simple: using StackTrace class, we query all our callers, get their assembly Evidence, extract StrongName keys from them and then compare with the list of "good" keys.

This approach may not work if:

1. This code is not granted SecurityPermissionFlag.ControlEvidence that is needed to enumerate other's Evidence, and that in fact is quite powerful permission. However, if this sample is used to protect something so thoroughly, it may be already doing something that requires FullTrust.

2. Callers have ControlEvidence. In this case, they can spoof this mechanism, although it's quite not straighforward.

3. Machine has StrongName Verification skip entires that could be used to spoof the sample, too. But having such entries is asking for troubles anyway, so don't ever create them if you can avoid it.

Main method to use is EmulateSNDemand that takes a set of string representation of "good" keys and returns true if all callers have keys from this set, and fals if at least one caller did not have a key from this set.

Eugene V. Bobukh,



[assembly: System.Reflection.AssemblyKeyFile("ST.snk")]

[assembly: AllowPartiallyTrustedCallersAttribute()]

[assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum, ControlEvidence = true)]

public class DemoClass


// Helper function.

private static bool IsInSet(StrongName SN, string[] GoodKeys)


string Key;

if (null == SN) Key = null;

else Key = SN.PublicKey.ToString();

foreach (string s in GoodKeys)


if ((null != s)&&(null != Key))


if (Key.ToUpper() == s.ToUpper()) return true;


else if (s == Key) return true;


return false;




Returns true if all caller's keys belong to passed set of GoodKeys, and fails otherwise. If you want to allow non-signed callers, add null to GoodKeys array. Second parameter shows how many stack frames to skip. You can use it if you don't want to bother checking your own key, for example. Zero means "check everyone". */


public static bool EmulateSNDemand(string[] GoodKeys, int nFramesToSkip)


StackTrace st = new StackTrace();

int Frames = st.FrameCount;

Console.WriteLine("Frames: {0}", Frames);

for (int i = nFramesToSkip; i < Frames; i++)


StackFrame sf = st.GetFrame(i);

MethodBase mb = sf.GetMethod();

Type t = mb.DeclaringType;

Assembly Asm = t.Assembly;

Evidence Ev = Asm.Evidence;


Console.WriteLine("===================================");Console.WriteLine("Looking at the caller #{0} in the stack", i);

Console.WriteLine("Calling method: {0}", mb);

Console.WriteLine("Class that contains it: {0}", t);

Console.WriteLine("Calling Assembly: {0}", Asm);


bool bKeyFound = false;

foreach (object o in Ev)


if (o is StrongName)


bKeyFound = true;


Console.WriteLine("Caller has StrongName:\r\n{0}", ((StrongName) o).PublicKey.ToString());


if (false == IsInSet((StrongName) o, GoodKeys))



Console.WriteLine("This key is not \"good\"");


return false;

// why bother further if at least one caller is "bad"?



else Console.WriteLine("This key is \"good\"");




/* Finished searching. It appears that caller did not have a strongname, but let's check if we allow it: */

if (false == IsInSet((StrongName) null, GoodKeys))



Console.WriteLine("Caller does not have a key, and this is not allowed");


return false;



if (false == bKeyFound) Console.WriteLine("Caller does not have a key, but we allowed this");



// If we are here, we've checked all the callers, and all they were "good", so return true:

return true;



/* This is a "protected" method that supposely wraps another, private method and calls it only if EmulateSNDemand() check passes. */


public static string ProtectionSample()


// Use sn.exe to extract these values from assemblies or key pairs:

string[] GoodKeys = {

/* Our's key. We can omit putting it here and set  nFramesToSkip to 2 for not checking our own key it will prevent anyone from reverse-calling us. However, now we don't do it: */







/* Key 1 */







/* Key 2*/







// For demo purposes, we allow nonsigned callers, too.




if (true == EmulateSNDemand(GoodKeys, 0)) return "\r\nAll callers have \"good\" keys";

return "\r\nSome of the callers do not appear to have \"good\" keys";



Comments (5)

  1. Are you sure that this works? The PublicKey.ToString does not work as you would expect. I blogged about it here:

  2. Eugene Bobukh says:

    Well, at least it used to work 🙂 I guess the reason for confusion is that I’m using not the PublicKey [which is essentially just a byte array in your sample, right?] but StrongNamePublicKeyBlob.ToString() here — it’s different. Hope that helps 🙂

Skip to main content