2 // System.Security.PermissionSet.cs
5 // Nick Drochak(ndrochak@gol.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Diagnostics;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Runtime.Serialization.Formatters.Binary;
39 using System.Security.Permissions;
40 using System.Security.Policy;
42 using System.Threading;
44 namespace System.Security {
47 // Microsoft public key - i.e. only MS signed assembly can inherit from PermissionSet (1.x) or (2.0) FullTrust assemblies
48 [StrongNameIdentityPermission (SecurityAction.InheritanceDemand, PublicKey="002400000480000094000000060200000024000052534131000400000100010007D1FA57C4AED9F0A32E84AA0FAEFD0DE9E8FD6AEC8F87FB03766C834C99921EB23BE79AD9D5DCC1DD9AD236132102900B723CF980957FC4E177108FC607774F29E8320E92EA05ECE4E821C0A5EFE8F1645C4C0C93C1AB99285D622CAA652C1DFAD63D745D6F2DE5F17E5EAF0FC4963D261C8A12436518206DC093344D5AD293")]
50 [MonoTODO ("CAS support is experimental (and unsupported).")]
51 public class PermissionSet: ISecurityEncodable, ICollection, IEnumerable, IStackWalk, IDeserializationCallback {
53 private const string tagName = "PermissionSet";
54 private const int version = 1;
55 private static object[] psUnrestricted = new object [1] { PermissionState.Unrestricted };
57 private PermissionState state;
58 private ArrayList list;
59 private PolicyLevel _policyLevel;
60 private bool _declsec;
61 private bool _readOnly;
62 private bool[] _ignored; // for asserts and non-CAS permissions
66 // for PolicyLevel (to avoid validation duplication)
67 internal PermissionSet ()
69 list = new ArrayList ();
72 public PermissionSet (PermissionState state) : this ()
74 this.state = CodeAccessPermission.CheckPermissionState (state, true);
77 public PermissionSet (PermissionSet permSet) : this ()
79 // LAMESPEC: This would be handled by the compiler. No way permSet is not a PermissionSet.
80 //if (!(permSet is PermissionSet))
81 // throw new System.ArgumentException(); // permSet is not an instance of System.Security.PermissionSet.
82 if (permSet != null) {
83 state = permSet.state;
84 foreach (IPermission p in permSet.list)
89 internal PermissionSet (string xml)
92 state = PermissionState.None;
94 SecurityElement se = SecurityElement.FromString (xml);
99 // Light version for creating a (non unrestricted) PermissionSet with
100 // a single permission. This allows to relax most validations.
101 internal PermissionSet (IPermission perm)
105 // note: we do not copy IPermission like AddPermission
112 public IPermission AddPermission (IPermission perm)
114 if ((perm == null) || _readOnly)
117 // we don't add to an unrestricted permission set unless...
118 if (state == PermissionState.Unrestricted) {
119 // identity permissions can be unrestricted under 2.x
121 // we return the union of the permission with unrestricted
122 // which results in a permission of the same type initialized
123 // with PermissionState.Unrestricted
124 return (IPermission) Activator.CreateInstance (perm.GetType (), psUnrestricted);
128 // we can't add two permissions of the same type in a set
129 // so we remove an existing one, union with it and add it back
130 IPermission existing = RemovePermission (perm.GetType ());
131 if (existing != null) {
132 perm = perm.Union (existing);
135 // note: Add doesn't copy
140 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
141 [SecurityPermission (SecurityAction.Demand, Assertion = true)]
142 public void Assert ()
144 int count = this.Count;
146 // we (current frame) must have the permission to assert it to others
147 // otherwise we don't assert (but we don't throw an exception)
148 foreach (IPermission p in list) {
149 // note: we ignore non-CAS permissions
150 if (p is IStackWalk) {
151 if (!SecurityManager.IsGranted (p)) {
158 // note: we must ignore the stack modifiers for the non-CAS permissions
159 if (SecurityManager.SecurityEnabled && (count > 0))
160 throw new NotSupportedException ("Currently only declarative Assert are supported.");
163 internal void Clear ()
168 public virtual PermissionSet Copy ()
170 return new PermissionSet (this);
173 public virtual void CopyTo (Array array, int index)
176 throw new ArgumentNullException ("array");
178 if (list.Count > 0) {
179 if (array.Rank > 1) {
180 throw new ArgumentException (Locale.GetText (
181 "Array has more than one dimension"));
183 if (index < 0 || index >= array.Length) {
184 throw new IndexOutOfRangeException ("index");
187 list.CopyTo (array, index);
191 public void Demand ()
193 // Note: SecurityEnabled only applies to CAS permissions
194 // so we're not checking for it (yet)
199 if ((_ignored == null) || (_ignored.Length != n)) {
200 _ignored = new bool [n];
203 bool call_cas_only = this.IsUnrestricted ();
204 // non CAS permissions (e.g. PrincipalPermission) do not requires a stack walk
205 for (int i = 0; i < n; i++) {
206 IPermission p = (IPermission) list [i];
207 Type t = p.GetType ();
208 if (t.IsSubclassOf (typeof (CodeAccessPermission))) {
209 _ignored [i] = false;
210 call_cas_only = true;
217 // don't start the stack walk if
218 // - the permission set only contains non CAS permissions; or
219 // - security isn't enabled (applis only to CAS!)
220 if (call_cas_only && SecurityManager.SecurityEnabled)
221 CasOnlyDemand (_declsec ? 5 : 3);
224 // The number of frames to skip depends on who's calling
225 // - CodeAccessPermission.Demand (imperative)
226 // - PermissionSet.Demand (imperative)
227 // - SecurityManager.InternalDemand (declarative)
228 internal void CasOnlyDemand (int skip)
231 Assembly current = null;
232 AppDomain domain = null;
235 if (_ignored == null) {
236 // special case when directly called from CodeAccessPermission.Demand
237 _ignored = new bool [list.Count];
241 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
244 if (!SecurityManager.SecurityEnabled)
247 foreach (IPermission p in list) {
248 // note: we ignore non-CAS permissions
249 if (p is IStackWalk) {
250 throw new NotSupportedException ("Currently only declarative Deny are supported.");
255 public virtual void FromXml (SecurityElement et)
258 throw new ArgumentNullException ("et");
259 if (et.Tag != tagName) {
260 string msg = String.Format ("Invalid tag {0} expected {1}", et.Tag, tagName);
261 throw new ArgumentException (msg, "et");
266 if (CodeAccessPermission.IsUnrestricted (et)) {
267 state = PermissionState.Unrestricted;
268 // no need to continue for an unrestricted permission
269 // because identity permissions now "supports" unrestricted
272 state = PermissionState.None;
275 if (et.Children != null) {
276 foreach (SecurityElement se in et.Children) {
277 string className = se.Attribute ("class");
278 if (className == null) {
279 throw new ArgumentException (Locale.GetText (
280 "No permission class is specified."));
282 if (Resolver != null) {
283 // policy class names do not have to be fully qualified
284 className = Resolver.ResolveClassName (className);
287 list.Add (PermissionBuilder.Create (className, se));
292 public IEnumerator GetEnumerator ()
294 return list.GetEnumerator ();
297 public bool IsSubsetOf (PermissionSet target)
299 // if target is empty we must be empty too
300 if ((target == null) || (target.IsEmpty ()))
301 return this.IsEmpty ();
303 // all permissions support unrestricted in 2.0
304 if (target.IsUnrestricted ())
306 if (this.IsUnrestricted ())
309 if (this.IsUnrestricted () && ((target == null) || !target.IsUnrestricted ()))
312 // if each of our permission is (a) present and (b) a subset of target
313 foreach (IPermission p in list) {
314 // non CAS permissions must be evaluated for unrestricted
315 Type t = p.GetType ();
316 IPermission i = null;
317 if (target.IsUnrestricted () && (p is CodeAccessPermission) && (p is IUnrestrictedPermission)) {
318 i = (IPermission) Activator.CreateInstance (t, psUnrestricted);
320 i = target.GetPermission (t);
323 if (!p.IsSubsetOf (i))
324 return false; // not a subset (condition b)
329 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
330 public void PermitOnly ()
332 if (!SecurityManager.SecurityEnabled)
335 foreach (IPermission p in list) {
336 // note: we ignore non-CAS permissions
337 if (p is IStackWalk) {
338 throw new NotSupportedException ("Currently only declarative Deny are supported.");
343 public bool ContainsNonCodeAccessPermissions ()
345 if (list.Count > 0) {
346 foreach (IPermission p in list) {
347 if (! p.GetType ().IsSubclassOf (typeof (CodeAccessPermission)))
354 // FIXME little documentation in Fx 2.0 beta 1
355 public static byte[] ConvertPermissionSet (string inFormat, byte[] inData, string outFormat)
357 if (inFormat == null)
358 throw new ArgumentNullException ("inFormat");
359 if (outFormat == null)
360 throw new ArgumentNullException ("outFormat");
364 if (inFormat == outFormat)
367 PermissionSet ps = null;
369 if (inFormat == "BINARY") {
370 if (outFormat.StartsWith ("XML")) {
371 using (MemoryStream ms = new MemoryStream (inData)) {
372 BinaryFormatter formatter = new BinaryFormatter ();
373 ps = (PermissionSet) formatter.Deserialize (ms);
376 string xml = ps.ToString ();
380 return Encoding.ASCII.GetBytes (xml);
382 return Encoding.Unicode.GetBytes (xml);
386 else if (inFormat.StartsWith ("XML")) {
387 if (outFormat == "BINARY") {
392 xml = Encoding.ASCII.GetString (inData);
395 xml = Encoding.Unicode.GetString (inData);
399 ps = new PermissionSet (PermissionState.None);
400 ps.FromXml (SecurityElement.FromString (xml));
402 MemoryStream ms = new MemoryStream ();
403 BinaryFormatter formatter = new BinaryFormatter ();
404 formatter.Serialize (ms, ps);
406 return ms.ToArray ();
409 else if (outFormat.StartsWith ("XML")) {
410 string msg = String.Format (Locale.GetText ("Can't convert from {0} to {1}"), inFormat, outFormat);
411 throw new XmlSyntaxException (msg);
415 // unknown inFormat, returns null
418 // unknown outFormat, throw
419 throw new SerializationException (String.Format (Locale.GetText ("Unknown output format {0}."), outFormat));
422 public IPermission GetPermission (Type permClass)
424 if ((permClass == null) || (list.Count == 0))
427 foreach (object o in list) {
428 if ((o != null) && o.GetType ().Equals (permClass))
429 return (IPermission) o;
431 // it's normal to return null for unrestricted sets
435 public PermissionSet Intersect (PermissionSet other)
437 // no intersection possible
438 if ((other == null) || (other.IsEmpty ()) || (this.IsEmpty ()))
441 PermissionState state = PermissionState.None;
442 if (this.IsUnrestricted () && other.IsUnrestricted ())
443 state = PermissionState.Unrestricted;
445 PermissionSet interSet = null;
446 // much simpler with 2.0
447 if (state == PermissionState.Unrestricted) {
448 interSet = new PermissionSet (state);
449 } else if (this.IsUnrestricted ()) {
450 interSet = other.Copy ();
451 } else if (other.IsUnrestricted ()) {
452 interSet = this.Copy ();
454 interSet = new PermissionSet (state);
455 InternalIntersect (interSet, this, other, false);
460 internal void InternalIntersect (PermissionSet intersect, PermissionSet a, PermissionSet b, bool unrestricted)
462 foreach (IPermission p in b.list) {
463 // for every type in both list
464 IPermission i = a.GetPermission (p.GetType ());
466 // add intersection for this type
467 intersect.AddPermission (p.Intersect (i));
469 // unrestricted is possible for indentity permissions
470 else if (unrestricted) {
471 intersect.AddPermission (p);
477 public bool IsEmpty ()
479 // note: Unrestricted isn't empty
480 if (state == PermissionState.Unrestricted)
482 if ((list == null) || (list.Count == 0))
484 // the set may include some empty permissions
485 foreach (IPermission p in list) {
486 // empty == fully restricted == IsSubsetOf(null) == true
487 if (!p.IsSubsetOf (null))
493 public bool IsUnrestricted ()
495 return (state == PermissionState.Unrestricted);
498 public IPermission RemovePermission (Type permClass)
500 if ((permClass == null) || _readOnly)
503 foreach (object o in list) {
504 if (o.GetType ().Equals (permClass)) {
506 return (IPermission) o;
512 public IPermission SetPermission (IPermission perm)
514 if ((perm == null) || _readOnly)
516 IUnrestrictedPermission u = (perm as IUnrestrictedPermission);
518 state = PermissionState.None;
520 state = u.IsUnrestricted () ? state : PermissionState.None;
522 RemovePermission (perm.GetType ());
527 public override string ToString ()
529 return ToXml ().ToString ();
532 public virtual SecurityElement ToXml ()
534 SecurityElement se = new SecurityElement (tagName);
535 se.AddAttribute ("class", GetType ().FullName);
536 se.AddAttribute ("version", version.ToString ());
537 if (state == PermissionState.Unrestricted)
538 se.AddAttribute ("Unrestricted", "true");
540 // required for permissions that do not implement IUnrestrictedPermission
541 foreach (IPermission p in list) {
542 se.AddChild (p.ToXml ());
547 public PermissionSet Union (PermissionSet other)
552 PermissionSet copy = null;
553 if (this.IsUnrestricted () || other.IsUnrestricted ()) {
554 // there are no child elements in unrestricted permission sets
555 return new PermissionSet (PermissionState.Unrestricted);
558 // PermissionState.None -> copy all permissions
559 foreach (IPermission p in other.list) {
560 copy.AddPermission (p);
566 public virtual int Count {
567 get { return list.Count; }
570 public virtual bool IsSynchronized {
571 get { return list.IsSynchronized; }
574 public virtual bool IsReadOnly {
575 // always false (as documented) but the PermissionSet can be read-only
576 // e.g. in a PolicyStatement
577 get { return false; }
580 public virtual object SyncRoot {
584 internal bool DeclarativeSecurity {
585 get { return _declsec; }
586 set { _declsec = value; }
589 [MonoTODO ("may not be required")]
590 void IDeserializationCallback.OnDeserialization (object sender)
595 public override bool Equals (object obj)
599 PermissionSet ps = (obj as PermissionSet);
602 if (state != ps.state)
604 if (list.Count != ps.Count)
607 for (int i=0; i < list.Count; i++) {
609 for (int j=0; i < ps.list.Count; j++) {
610 if (list [i].Equals (ps.list [j])) {
622 public override int GetHashCode ()
624 return (list.Count == 0) ? (int) state : base.GetHashCode ();
627 // FIXME what's it doing here? There's probably a reason this was added here.
628 static public void RevertAssert ()
630 CodeAccessPermission.RevertAssert ();
635 internal PolicyLevel Resolver {
636 get { return _policyLevel; }
637 set { _policyLevel = value; }
640 internal void SetReadOnly (bool value)
645 private bool AllIgnored ()
647 if (_ignored == null)
648 throw new NotSupportedException ("bad bad bad");
650 for (int i=0; i < _ignored.Length; i++) {
654 // everything is ignored (i.e. non-CAS permission or asserted permission).
658 // 2.0 metadata format
660 internal static PermissionSet CreateFromBinaryFormat (byte[] data)
662 if ((data == null) || (data [0] != 0x2E) || (data.Length < 2)) {
663 string msg = Locale.GetText ("Invalid data in 2.0 metadata format.");
664 throw new SecurityException (msg);
668 int numattr = ReadEncodedInt (data, ref pos);
669 PermissionSet ps = new PermissionSet (PermissionState.None);
670 for (int i = 0; i < numattr; i++) {
671 IPermission p = ProcessAttribute (data, ref pos);
673 string msg = Locale.GetText ("Unsupported data found in 2.0 metadata format.");
674 throw new SecurityException (msg);
676 ps.AddPermission (p);
681 internal static int ReadEncodedInt (byte[] data, ref int position)
684 if ((data [position] & 0x80) == 0) {
685 len = data [position];
687 } else if ((data [position] & 0x40) == 0) {
688 len = ((data [position] & 0x3f) << 8 | data [position + 1]);
691 len = (((data [position] & 0x1f) << 24) | (data [position + 1] << 16) |
692 (data [position + 2] << 8) | (data [position + 3]));
698 static object[] action = new object [1] { (SecurityAction) 0 };
700 // TODO: add support for arrays and enums (2.0)
701 internal static IPermission ProcessAttribute (byte[] data, ref int position)
703 int clen = ReadEncodedInt (data, ref position);
704 string cnam = Encoding.UTF8.GetString (data, position, clen);
707 Type secattr = Type.GetType (cnam);
708 SecurityAttribute sa = (Activator.CreateInstance (secattr, action) as SecurityAttribute);
712 /*int optionalParametersLength =*/ ReadEncodedInt (data, ref position);
713 int numberOfParameters = ReadEncodedInt (data, ref position);
714 for (int j=0; j < numberOfParameters; j++) {
715 bool property = false;
716 switch (data [position++]) {
717 case 0x53: // field (technically possible and working)
720 case 0x54: // property (common case)
728 byte type = data [position++];
731 type = data [position++];
734 int plen = ReadEncodedInt (data, ref position);
735 string pnam = Encoding.UTF8.GetString (data, position, plen);
740 arrayLength = BitConverter.ToInt32 (data, position);
745 object[] arrayIndex = null;
746 for (int i = 0; i < arrayLength; i++) {
748 // TODO - setup index (2.0)
751 // sadly type values doesn't match ther TypeCode enum :(
753 case 0x02: // MONO_TYPE_BOOLEAN
754 obj = (object) Convert.ToBoolean (data [position++]);
756 case 0x03: // MONO_TYPE_CHAR
757 obj = (object) Convert.ToChar (data [position]);
760 case 0x04: // MONO_TYPE_I1
761 obj = (object) Convert.ToSByte (data [position++]);
763 case 0x05: // MONO_TYPE_U1
764 obj = (object) Convert.ToByte (data [position++]);
766 case 0x06: // MONO_TYPE_I2
767 obj = (object) Convert.ToInt16 (data [position]);
770 case 0x07: // MONO_TYPE_U2
771 obj = (object) Convert.ToUInt16 (data [position]);
774 case 0x08: // MONO_TYPE_I4
775 obj = (object) Convert.ToInt32 (data [position]);
778 case 0x09: // MONO_TYPE_U4
779 obj = (object) Convert.ToUInt32 (data [position]);
782 case 0x0A: // MONO_TYPE_I8
783 obj = (object) Convert.ToInt64 (data [position]);
786 case 0x0B: // MONO_TYPE_U8
787 obj = (object) Convert.ToUInt64 (data [position]);
790 case 0x0C: // MONO_TYPE_R4
791 obj = (object) Convert.ToSingle (data [position]);
794 case 0x0D: // MONO_TYPE_R8
795 obj = (object) Convert.ToDouble (data [position]);
798 case 0x0E: // MONO_TYPE_STRING
800 if (data [position] != 0xFF) {
801 int slen = ReadEncodedInt (data, ref position);
802 s = Encoding.UTF8.GetString (data, position, slen);
809 case 0x50: // special for TYPE
810 int tlen = ReadEncodedInt (data, ref position);
811 obj = (object) Type.GetType (Encoding.UTF8.GetString (data, position, tlen));
815 return null; // unsupported
819 PropertyInfo pi = secattr.GetProperty (pnam);
820 pi.SetValue (sa, obj, arrayIndex);
822 FieldInfo fi = secattr.GetField (pnam);
823 fi.SetValue (sa, obj);
827 return sa.CreatePermission ();