New tests.
[mono.git] / mcs / class / corlib / System.Security / SecurityManager.cs
index 48d085cec1a9de6da7cccb85222fbd5e980643df..26da6e305bb0493d4810c97efc59dbb71cc59185 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+#if !MOONLIGHT
+
 using System.Collections;
+using System.Diagnostics;
 using System.Globalization;
 using System.IO;
 using System.Reflection;
@@ -50,13 +53,13 @@ namespace System.Security {
                public RuntimeDeclSecurityEntry choice;
        }
 
-       public sealed class SecurityManager {
-
+       [ComVisible (true)]
+       public static class SecurityManager {
                private static object _lockObject;
                private static ArrayList _hierarchy;
-               private static PermissionSet _fullTrust; // for [AllowPartiallyTrustedCallers]
                private static IPermission _unmanagedCode;
                private static Hashtable _declsecCache;
+               private static PolicyLevel _level;
 
                static SecurityManager () 
                {
@@ -65,10 +68,6 @@ namespace System.Security {
                        _lockObject = new object ();
                }
 
-               private SecurityManager ()
-               {
-               }
-
                // properties
 
                extern public static bool CheckExecutionRights {
@@ -76,30 +75,44 @@ namespace System.Security {
                        get;
 
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                       [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+                       [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                        set;
                }
 
+               [Obsolete ("The security manager cannot be turned off on MS runtime")]
                extern public static bool SecurityEnabled {
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
                        get;
 
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                       [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+                       [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                        set;
                }
 
+               internal static bool CheckElevatedPermissions ()
+               {
+                       return true; // always true outside Moonlight
+               }
+
+               [Conditional ("MOONLIGHT")]
+               internal static void EnsureElevatedPermissions ()
+               {
+                       // do nothing outside of Moonlight
+               }
+
                // methods
 
-#if NET_2_0
-               [MonoTODO]
+               // NOTE: This method doesn't show in the class library status page because
+               // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
+               // But it's there!
+               // FIXME works for fulltrust (empty), documentation doesn't really make sense, type wise
+               [MonoTODO ("CAS support is experimental (and unsupported). This method only works in FullTrust.")]
                [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey = "0x00000000000000000400000000000000")]
                public static void GetZoneAndOrigin (out ArrayList zone, out ArrayList origin) 
                {
                        zone = new ArrayList ();
                        origin = new ArrayList ();
                }
-#endif
 
                public static bool IsGranted (IPermission perm)
                {
@@ -113,25 +126,25 @@ namespace System.Security {
                        // - Not affected by overrides (like Assert, Deny and PermitOnly)
                        // - calls IsSubsetOf even for non CAS permissions
                        //   (i.e. it does call Demand so any code there won't be executed)
+                       // with 2.0 identity permission are unrestrictable
                        return IsGranted (Assembly.GetCallingAssembly (), perm);
                }
 
+               // note: in 2.0 *all* permissions (including identity permissions) support unrestricted
                internal static bool IsGranted (Assembly a, IPermission perm)
                {
-                       CodeAccessPermission grant = null;
-
-                       if (a.GrantedPermissionSet != null) {
-                               grant = (CodeAccessPermission) a.GrantedPermissionSet.GetPermission (perm.GetType ());
-                               if (grant == null) {
-                                       if (!a.GrantedPermissionSet.IsUnrestricted () || !(perm is IUnrestrictedPermission)) {
-                                               return perm.IsSubsetOf (null);
-                                       }
-                               } else if (!perm.IsSubsetOf (grant)) {
+                       PermissionSet granted = a.GrantedPermissionSet;
+                       if ((granted != null) && !granted.IsUnrestricted ()) {
+                               CodeAccessPermission grant = (CodeAccessPermission) granted.GetPermission (perm.GetType ());
+                               if (!perm.IsSubsetOf (grant)) {
                                        return false;
                                }
                        }
 
-                       if (a.DeniedPermissionSet != null) {
+                       PermissionSet denied = a.DeniedPermissionSet;
+                       if ((denied != null) && !denied.IsEmpty ()) {
+                               if (denied.IsUnrestricted ())
+                                       return false;
                                CodeAccessPermission refuse = (CodeAccessPermission) a.DeniedPermissionSet.GetPermission (perm.GetType ());
                                if ((refuse != null) && perm.IsSubsetOf (refuse))
                                        return false;
@@ -139,16 +152,16 @@ namespace System.Security {
                        return true;
                }
 
-               internal static bool IsGranted (Assembly a, PermissionSet ps, bool noncas)
+               internal static IPermission CheckPermissionSet (Assembly a, PermissionSet ps, bool noncas)
                {
                        if (ps.IsEmpty ())
-                               return true;
+                               return null;
 
                        foreach (IPermission p in ps) {
                                // note: this may contains non CAS permissions
                                if ((!noncas) && (p is CodeAccessPermission)) {
-                                       if (!SecurityManager.IsGranted (a, p))
-                                               return false;
+                                       if (!IsGranted (a, p))
+                                               return p;
                                } else {
                                        // but non-CAS will throw on failure...
                                        try {
@@ -156,14 +169,52 @@ namespace System.Security {
                                        }
                                        catch (SecurityException) {
                                                // ... so we catch
-                                               return false;
+                                               return p;
                                        }
                                }
                        }
-                       return true;
+                       return null;
+               }
+
+               internal static IPermission CheckPermissionSet (AppDomain ad, PermissionSet ps)
+               {
+                       if ((ps == null) || ps.IsEmpty ())
+                               return null;
+
+                       PermissionSet granted = ad.GrantedPermissionSet;
+                       if (granted == null)
+                               return null;
+                       if (granted.IsUnrestricted ())
+                               return null;
+                       if (ps.IsUnrestricted ())
+                               return new SecurityPermission (SecurityPermissionFlag.NoFlags);
+
+                       foreach (IPermission p in ps) {
+                               if (p is CodeAccessPermission) {
+                                       CodeAccessPermission grant = (CodeAccessPermission) granted.GetPermission (p.GetType ());
+                                       if (grant == null) {
+                                               if (!granted.IsUnrestricted () || !(p is IUnrestrictedPermission)) {
+                                                       if (!p.IsSubsetOf (null))
+                                                               return p;
+                                               }
+                                       } else if (!p.IsSubsetOf (grant)) {
+                                               return p;
+                                       }
+                               } else {
+                                       // but non-CAS will throw on failure...
+                                       try {
+                                               p.Demand ();
+                                       }
+                                       catch (SecurityException) {
+                                               // ... so we catch
+                                               return p;
+                                       }
+                               }
+                       }
+                       return null;
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static PolicyLevel LoadPolicyLevelFromFile (string path, PolicyLevelType type)
                {
                        if (path == null)
@@ -180,7 +231,7 @@ namespace System.Security {
                        return pl;
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static PolicyLevel LoadPolicyLevelFromString (string str, PolicyLevelType type)
                {
                        if (null == str)
@@ -197,7 +248,7 @@ namespace System.Security {
                        return pl;
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static IEnumerator PolicyHierarchy ()
                {
                        return Hierarchy;
@@ -220,11 +271,11 @@ namespace System.Security {
                        }
 
                        ResolveIdentityPermissions (ps, evidence);
+
                        return ps;
                }
 
-#if NET_2_0
-               [MonoTODO ("more tests are needed")]
+               [MonoTODO ("(2.0) more tests are needed")]
                public static PermissionSet ResolvePolicy (Evidence[] evidences)
                {
                        if ((evidences == null) || (evidences.Length == 0) ||
@@ -260,11 +311,9 @@ namespace System.Security {
                        ResolveIdentityPermissions (ps, evidence);
                        return ps;
                }
-#endif
 
                static private SecurityPermission _execution = new SecurityPermission (SecurityPermissionFlag.Execution);
 
-               [MonoTODO()]
                public static PermissionSet ResolvePolicy (Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, out PermissionSet denied)
                {
                        PermissionSet resolved = ResolvePolicy (evidence);
@@ -273,17 +322,26 @@ namespace System.Security {
                                throw new PolicyException (Locale.GetText (
                                        "Policy doesn't grant the minimal permissions required to execute the assembly."));
                        }
-                       // do we have the right to execute ?
+
+                       // do we check for execution rights ?
                        if (CheckExecutionRights) {
-                               // unless we have "Full Trust"...
-                               if (!resolved.IsUnrestricted ()) {
-                                       // ... we need to find a SecurityPermission
-                                       IPermission security = resolved.GetPermission (typeof (SecurityPermission));
-                                       if (!_execution.IsSubsetOf (security)) {
-                                               throw new PolicyException (Locale.GetText (
-                                                       "Policy doesn't grant the right to execute to the assembly."));
+                               bool execute = false;
+                               // an empty permissionset doesn't include Execution
+                               if (resolved != null) {
+                                       // unless we have "Full Trust"...
+                                       if (resolved.IsUnrestricted ()) {
+                                               execute = true;
+                                       } else {
+                                               // ... we need to find a SecurityPermission
+                                               IPermission security = resolved.GetPermission (typeof (SecurityPermission));
+                                               execute = _execution.IsSubsetOf (security);
                                        }
                                }
+
+                               if (!execute) {
+                                       throw new PolicyException (Locale.GetText (
+                                               "Policy doesn't grant the right to execute the assembly."));
+                               }
                        }
 
                        denied = denyPset;
@@ -306,7 +364,7 @@ namespace System.Security {
                        return al.GetEnumerator ();
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static void SavePolicy () 
                {
                        IEnumerator e = Hierarchy;
@@ -316,7 +374,7 @@ namespace System.Security {
                        }
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static void SavePolicyLevel (PolicyLevel level) 
                {
                        // Yes this will throw a NullReferenceException, just like MS (see FDBK13121)
@@ -327,12 +385,9 @@ namespace System.Security {
 
                private static IEnumerator Hierarchy {
                        get {
-                               // double-lock pattern
-                               if (_hierarchy == null) {
-                                       lock (_lockObject) {
-                                               if (_hierarchy == null)
-                                                       InitializePolicyHierarchy ();
-                                       }
+                               lock (_lockObject) {
+                                       if (_hierarchy == null)
+                                               InitializePolicyHierarchy ();
                                }
                                return _hierarchy.GetEnumerator ();
                        }
@@ -344,17 +399,25 @@ namespace System.Security {
                        // note: use InternalGetFolderPath to avoid recursive policy initialization
                        string userPolicyPath = Path.Combine (Environment.InternalGetFolderPath (Environment.SpecialFolder.ApplicationData), "mono");
 
-                       ArrayList al = new ArrayList ();
-                       al.Add (new PolicyLevel ("Enterprise", PolicyLevelType.Enterprise,
-                               Path.Combine (machinePolicyPath, "enterprisesec.config")));
+                       PolicyLevel enterprise = new PolicyLevel ("Enterprise", PolicyLevelType.Enterprise);
+                       _level = enterprise;
+                       enterprise.LoadFromFile (Path.Combine (machinePolicyPath, "enterprisesec.config"));
 
-                       al.Add (new PolicyLevel ("Machine", PolicyLevelType.Machine,
-                               Path.Combine (machinePolicyPath, "security.config")));
+                       PolicyLevel machine = new PolicyLevel ("Machine", PolicyLevelType.Machine);
+                       _level = machine;
+                       machine.LoadFromFile (Path.Combine (machinePolicyPath, "security.config"));
 
-                       al.Add (new PolicyLevel ("User", PolicyLevelType.User,
-                               Path.Combine (userPolicyPath, "security.config")));
+                       PolicyLevel user = new PolicyLevel ("User", PolicyLevelType.User);
+                       _level = user;
+                       user.LoadFromFile (Path.Combine (userPolicyPath, "security.config"));
+
+                       ArrayList al = new ArrayList ();
+                       al.Add (enterprise);
+                       al.Add (machine);
+                       al.Add (user);
 
                        _hierarchy = ArrayList.Synchronized (al);
+                       _level = null;
                }
 
                internal static bool ResolvePolicyLevel (ref PermissionSet ps, PolicyLevel pl, Evidence evidence)
@@ -378,9 +441,12 @@ namespace System.Security {
                        return false;
                }
 
-               // TODO: this changes in 2.0 as identity permissions can now be unrestricted
                internal static void ResolveIdentityPermissions (PermissionSet ps, Evidence evidence)
                {
+                       // in 2.0 identity permissions can now be unrestricted
+                       if (ps.IsUnrestricted ())
+                               return;
+
                        // Only host evidence are used for policy resolution
                        IEnumerator ee = evidence.GetHostEnumerator ();
                        while (ee.MoveNext ()) {
@@ -392,21 +458,22 @@ namespace System.Security {
                        }
                }
 
+               internal static PolicyLevel ResolvingPolicyLevel {
+                       get { return _level; }
+                       set { _level = value; }
+               }
+
                internal static PermissionSet Decode (IntPtr permissions, int length)
                {
                        // Permission sets from the runtime (declarative security) can be cached
                        // for performance as they can never change (i.e. they are read-only).
+                       PermissionSet ps = null;
 
-                       if (_declsecCache == null) {
-                               lock (_lockObject) {
-                                       if (_declsecCache == null) {
-                                               _declsecCache = new Hashtable ();
-                                       }
+                       lock (_lockObject) {
+                               if (_declsecCache == null) {
+                                       _declsecCache = new Hashtable ();
                                }
-                       }
 
-                       PermissionSet ps = null;
-                       lock (_lockObject) {
                                object key = (object) (int) permissions;
                                ps = (PermissionSet) _declsecCache [key];
                                if (ps == null) {
@@ -439,62 +506,12 @@ namespace System.Security {
                                throw new SecurityException (Locale.GetText ("Unknown metadata format."));
                        }
                }
-#if NET_2_0
-               internal static PermissionSetCollection DecodeCollection (IntPtr permissions, int length)
-               {
-                       // Permission sets from the runtime (declarative security) can be cached
-                       // for performance as they can never change (i.e. they are read-only).
-
-                       if (_declsecCache == null) {
-                               lock (_lockObject) {
-                                       if (_declsecCache == null) {
-                                               _declsecCache = new Hashtable ();
-                                       }
-                               }
-                       }
-
-                       PermissionSetCollection psc = null;
-                       lock (_lockObject) {
-                               object key = (object) (int) permissions;
-                               psc = (PermissionSetCollection) _declsecCache [key];
-                               if (psc == null) {
-                                       // create permissionset and add it to the cache
-                                       byte[] data = new byte [length];
-                                       Marshal.Copy (permissions, data, 0, length);
-                                       psc = DecodeCollection (data);
-                                       _declsecCache.Add (key, psc);
-                               }
-                       }
-                       return psc;
-               }
-
-               internal static PermissionSetCollection DecodeCollection (byte[] encodedPermissions)
-               {
-                       if ((encodedPermissions == null) || (encodedPermissions.Length < 1))
-                               throw new SecurityException ("Invalid metadata format.");
-
-                       switch (encodedPermissions [0]) {
-                       case 60:
-                               // Fx 1.0/1.1 declarative security permissions metadata is in Unicode-encoded XML
-                               throw new SecurityException (Locale.GetText ("1.0 metadata format doesn't support collections."));
-                       case 0x2E:
-                               // Fx 2.0 are encoded "somewhat, but not enough, like" custom attributes
-                               // note: we still support the older format!
-                               return PermissionSetCollection.CreateFromBinaryFormat (encodedPermissions);
-                       default:
-                               throw new SecurityException (Locale.GetText ("Unknown metadata format."));
-                       }
-               }
-#endif
 
                private static IPermission UnmanagedCode {
                        get {
-                               // double-lock pattern
-                               if (_unmanagedCode == null) {
-                                       lock (_lockObject) {
-                                               if (_unmanagedCode == null)
-                                                       _unmanagedCode = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode);
-                                       }
+                               lock (_lockObject) {
+                                       if (_unmanagedCode == null)
+                                               _unmanagedCode = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode);
                                }
                                return _unmanagedCode;
                        }
@@ -537,17 +554,6 @@ namespace System.Security {
                        // a single stack walk (not up to 4).
                        if (ps != null)
                                ps.Demand ();
-#if NET_2_0
-                       // Process LinkDemandChoice (2.0)
-                       if (klass.choice.size > 0) {
-                               PermissionSetCollection psc = DecodeCollection (klass.choice.blob, klass.choice.size);
-                               psc.DemandChoice ();
-                       }
-                       if (method.choice.size > 0) {
-                               PermissionSetCollection psc = DecodeCollection (method.choice.blob, method.choice.size);
-                               psc.DemandChoice ();
-                       }
-#endif
                }
 
                internal unsafe static bool ReflectedLinkDemandQuery (MethodBase mb)
@@ -561,13 +567,6 @@ namespace System.Security {
                        return LinkDemand (mb.ReflectedType.Assembly, &klass, &method);
                }
 
-               // internal - get called at JIT time
-
-               private static void DemandUnmanaged ()
-               {
-                       UnmanagedCode.Demand ();
-               }
-
                private unsafe static bool LinkDemand (Assembly a, RuntimeDeclSecurityActions *klass, RuntimeDeclSecurityActions *method)
                {
                        try {
@@ -575,48 +574,21 @@ namespace System.Security {
                                bool result = true;
                                if (klass->cas.size > 0) {
                                        ps = Decode (klass->cas.blob, klass->cas.size);
-                                       result = SecurityManager.IsGranted (a, ps, false);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, false) == null);
                                }
                                if (result && (klass->noncas.size > 0)) {
                                        ps = Decode (klass->noncas.blob, klass->noncas.size);
-                                       result = SecurityManager.IsGranted (a, ps, true);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, true) == null);
                                }
 
                                if (result && (method->cas.size > 0)) {
                                        ps = Decode (method->cas.blob, method->cas.size);
-                                       result = SecurityManager.IsGranted (a, ps, false);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, false) == null);
                                }
                                if (result && (method->noncas.size > 0)) {
                                        ps = Decode (method->noncas.blob, method->noncas.size);
-                                       result = SecurityManager.IsGranted (a, ps, true);
-                               }
-#if NET_2_0
-                               // success if one of the permission is granted
-                               if (result && (klass->choice.size > 0)) {
-                                       PermissionSetCollection psc = DecodeCollection (klass->choice.blob, klass->choice.size);
-                                       if (psc.Count > 0) {
-                                               result = false;
-                                               foreach (PermissionSet pset in psc) {
-                                                       if (SecurityManager.IsGranted (a, pset, false)) {
-                                                               result = true;
-                                                               break;
-                                                       }
-                                               }
-                                       }
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, true) == null);
                                }
-                               if (result && (method->choice.size > 0)) {
-                                       PermissionSetCollection psc = DecodeCollection (method->choice.blob, method->choice.size);
-                                       if (psc.Count > 0) {
-                                               result = false;
-                                               foreach (PermissionSet pset in psc) {
-                                                       if (SecurityManager.IsGranted (a, pset, false)) {
-                                                               result = true;
-                                                               break;
-                                                       }
-                                               }
-                                       }
-                               }
-#endif
                                return result;
                        }
                        catch (SecurityException) {
@@ -624,32 +596,35 @@ namespace System.Security {
                        }
                }
 
+#pragma warning disable 169
                private static bool LinkDemandFullTrust (Assembly a)
                {
-                       // double-lock pattern
-                       if (_fullTrust == null) {
-                               lock (_lockObject) {
-                                       if (_fullTrust == null)
-                                               _fullTrust = new NamedPermissionSet ("FullTrust");
-                               }
-                       }
+                       // FullTrust is immutable (and means Unrestricted) 
+                       // so we can skip the subset operations and jump to IsUnrestricted.
+                       PermissionSet granted = a.GrantedPermissionSet;
+                       if ((granted != null) && !granted.IsUnrestricted ())
+                               return false;
 
-                       try {
-                               return SecurityManager.IsGranted (a, _fullTrust, false);
-                       }
-                       catch (SecurityException) {
+                       PermissionSet denied = a.DeniedPermissionSet;
+                       if ((denied != null) && !denied.IsEmpty ())
                                return false;
-                       }
+
+                       return true;
                }
 
                private static bool LinkDemandUnmanaged (Assembly a)
                {
+                       // note: we know that UnmanagedCode (SecurityPermission) implements IUnrestrictedPermission
                        return IsGranted (a, UnmanagedCode);
                }
 
                // we try to provide as much details as possible to help debugging
-               private static void LinkDemandSecurityException (int securityViolation, Assembly a, MethodInfo method)
+               private static void LinkDemandSecurityException (int securityViolation, IntPtr methodHandle)
                {
+                       RuntimeMethodHandle runtimeHandle = new RuntimeMethodHandle (methodHandle);
+                       MethodInfo method = (MethodInfo)(MethodBase.GetMethodFromHandle (runtimeHandle));
+                       Assembly a = method.DeclaringType.Assembly;
+
                        string message = null;
                        AssemblyName an = null;
                        PermissionSet granted = null;
@@ -658,7 +633,7 @@ namespace System.Security {
                        IPermission failed = null;
 
                        if (a != null) {
-                               an = a.GetName ();
+                               an = a.UnprotectedGetName ();
                                granted = a.GrantedPermissionSet;
                                refused = a.DeniedPermissionSet;
                        }
@@ -669,7 +644,7 @@ namespace System.Security {
                                break;
                        case 2: // MONO_JIT_LINKDEMAND_APTC
                                message = Locale.GetText ("Partially trusted callers aren't allowed to call into this assembly.");
-                               demanded = (object) _fullTrust;
+                               demanded = (object) DefaultPolicies.FullTrust; // immutable
                                break;
                        case 4: // MONO_JIT_LINKDEMAND_ECMA
                                message = Locale.GetText ("Calling internal calls is restricted to ECMA signed assemblies.");
@@ -695,7 +670,7 @@ namespace System.Security {
                        PermissionSet refused = null;
 
                        if (a != null) {
-                               an = a.GetName ();
+                               an = a.UnprotectedGetName ();
                                granted = a.GrantedPermissionSet;
                                refused = a.DeniedPermissionSet;
                        }
@@ -712,7 +687,14 @@ namespace System.Security {
                                break;
                        }
 
-                       throw new SecurityException (message, an, granted, refused, method, SecurityAction.LinkDemand, null, null, null);
+                       throw new SecurityException (message, an, granted, refused, method, SecurityAction.InheritanceDemand, null, null, null);
+               }
+
+               // called by the runtime when CoreCLR is enabled
+
+               private static void ThrowException (Exception ex)
+               {
+                       throw ex;
                }
 
                // internal - get called by the class loader
@@ -720,34 +702,27 @@ namespace System.Security {
                // Called when
                // - class inheritance
                // - method overrides
-               private unsafe static bool InheritanceDemand (Assembly a, RuntimeDeclSecurityActions *actions)
+               private unsafe static bool InheritanceDemand (AppDomain ad, Assembly a, RuntimeDeclSecurityActions *actions)
                {
                        try {
                                PermissionSet ps = null;
                                bool result = true;
                                if (actions->cas.size > 0) {
                                        ps = Decode (actions->cas.blob, actions->cas.size);
-                                       result = SecurityManager.IsGranted (a, ps, false);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, false) == null);
+                                       if (result) {
+                                               // also check appdomain
+                                               result = (SecurityManager.CheckPermissionSet (ad, ps) == null);
+                                       }
                                }
                                if (actions->noncas.size > 0) {
                                        ps = Decode (actions->noncas.blob, actions->noncas.size);
-                                       result = SecurityManager.IsGranted (a, ps, true);
-                               }
-#if NET_2_0
-                               // success if one of the permission is granted
-                               if (result && (actions->choice.size > 0)) {
-                                       PermissionSetCollection psc = DecodeCollection (actions->choice.blob, actions->choice.size);
-                                       if (psc.Count > 0) {
-                                               result = false;
-                                               foreach (PermissionSet pset in psc) {
-                                                       if (SecurityManager.IsGranted (a, pset, false)) {
-                                                               result = true;
-                                                               break;
-                                                       }
-                                               }
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, true) == null);
+                                       if (result) {
+                                               // also check appdomain
+                                               result = (SecurityManager.CheckPermissionSet (ad, ps) == null);
                                        }
                                }
-#endif
                                return result;
                        }
                        catch (SecurityException) {
@@ -755,6 +730,12 @@ namespace System.Security {
                        }
                }
 
+               // internal - get called at JIT time
+
+               private static void DemandUnmanaged ()
+               {
+                       UnmanagedCode.Demand ();
+               }
 
                // internal - get called by JIT generated code
 
@@ -766,12 +747,11 @@ namespace System.Security {
 
                private static void InternalDemandChoice (IntPtr permissions, int length)
                {
-#if NET_2_0
-                       PermissionSetCollection psc = DecodeCollection (permissions, length);
-                       psc.DemandChoice ();
-#else
-                       throw new SecurityException ("SecurityAction.DemandChoice is only possible in 2.0");
-#endif
+                       throw new SecurityException ("SecurityAction.DemandChoice was removed from 2.0");
                }
+#pragma warning restore 169            
        }
 }
+
+#endif
+