static bool TryProcessPermissionSetAttribute (SecurityDeclaration declaration, out PermissionSet set)
		{
			set = null;

			if (!declaration.HasSecurityAttributes && declaration.SecurityAttributes.Count != 1)
				return false;

			var security_attribute = declaration.SecurityAttributes [0];
			if (!security_attribute.AttributeType.IsTypeOf ("System.Security.Permissions", "PermissionSetAttribute"))
				return false;

			var attribute = new SSP.PermissionSetAttribute ((SSP.SecurityAction) declaration.Action);

			var named_argument = security_attribute.Properties [0];
			string value = (string) named_argument.Argument.Value;
			switch (named_argument.Name) {
			case "XML":
				attribute.XML = value;
				break;
			case "Name":
				attribute.Name = value;
				break;
			default:
				throw new NotImplementedException (named_argument.Name);
			}

			set = attribute.CreatePermissionSet ();
			return true;
		}
		static PermissionSet CreatePermissionSet (SecurityDeclaration declaration)
		{
			var set = new PermissionSet (SSP.PermissionState.None);

			foreach (var attribute in declaration.SecurityAttributes) {
				var permission = CreatePermission (declaration, attribute);
				set.AddPermission (permission);
			}

			return set;
		}
        public void CreateSimpleSecurityDeclaration()
        {
            const string typeNamespace = "System.WitchCraft";
            const string typeName = "MagicalWand";

            // set up temp assembly.
            var assembly = Utilities.CreateTempNetAssembly();
            var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
            var typeTable = tableStream.GetTable<TypeDefinition>();
            var declarationTable = tableStream.GetTable<SecurityDeclaration>();
            var importer = new ReferenceImporter(tableStream);

            // create temp type.
            var type = new TypeDefinition(typeNamespace, typeName);
            type.MetadataRow.Column5 = 1; // FieldList
            type.MetadataRow.Column6 = 2; // MethodList.
            typeTable.Add(type);

            // create attribute.
            var securityAttribute = new SecurityAttributeSignature()
            {
                TypeName = typeof(TypeDescriptorPermissionAttribute).AssemblyQualifiedName,
            };

            // create permission set.
            var permissionSet = new PermissionSetSignature();
            permissionSet.Attributes.Add(securityAttribute);

            // create declaration.
            var declaration = new SecurityDeclaration(SecurityAction.Assert, permissionSet);
            type.SecurityDeclarations.Add(declaration);
            declarationTable.Add(declaration);

            assembly = Utilities.RebuildNetAssembly(assembly);
            tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
            typeTable = tableStream.GetTable<TypeDefinition>();
            type = typeTable.First(x => x.IsTypeOf(typeNamespace, typeName));

            Assert.IsTrue(type.SecurityDeclarations.Count > 0);
            var newDeclaration = type.SecurityDeclarations[0];

            Assert.AreEqual(declaration.Action, newDeclaration.Action);
            Assert.AreEqual(declaration.PermissionSet.Attributes.Count, newDeclaration.PermissionSet.Attributes.Count);

            for (int i = 0; i < declaration.PermissionSet.Attributes.Count; i++)
            {
                var attribute = declaration.PermissionSet.Attributes[i];
                var newAttribute = newDeclaration.PermissionSet.Attributes[i];

                Assert.AreEqual(attribute.TypeName, newAttribute.TypeName);

            }
        }
		static IPermission CreatePermission (SecurityDeclaration declaration, SecurityAttribute attribute)
		{
			var attribute_type = Type.GetType (attribute.AttributeType.FullName);
			if (attribute_type == null)
				throw new ArgumentException ("attribute");

			var security_attribute = CreateSecurityAttribute (attribute_type, declaration);
			if (security_attribute == null)
				throw new InvalidOperationException ();

			CompleteSecurityAttribute (security_attribute, attribute);

			return security_attribute.CreatePermission ();
		}
        public static SecurityDeclaration ToSecurityDeclaration(this PermissionSet self, SecurityAction action, ModuleDefinition module)
        {
            if (self == null)
                throw new ArgumentNullException ("self");
            if (module == null)
                throw new ArgumentNullException ("module");

            var declaration = new SecurityDeclaration (action);

            var attribute = new SecurityAttribute (
                module.TypeSystem.LookupType ("System.Security.Permissions", "PermissionSetAttribute"));

            attribute.Properties.Add (
                new CustomAttributeNamedArgument (
                    "XML",
                    new CustomAttributeArgument (
                        module.TypeSystem.String, self.ToXml ().ToString ())));

            declaration.SecurityAttributes.Add (attribute);

            return declaration;
        }
		static SSP.SecurityAttribute CreateSecurityAttribute (Type attribute_type, SecurityDeclaration declaration)
		{
			SSP.SecurityAttribute security_attribute;
			try {
				security_attribute = (SSP.SecurityAttribute) Activator.CreateInstance (
					attribute_type, new object [] { (SSP.SecurityAction) declaration.Action });
			} catch (MissingMethodException) {
				security_attribute = (SSP.SecurityAttribute) Activator.CreateInstance (attribute_type, new object [0]);
			}

			return security_attribute;
		}
        public static int CompareToSecurityAttribute(this SecurityAttribute first, SecurityAttribute second, SecurityDeclaration firstDeclaration, SecurityDeclaration secondDeclaration)
        {
            if (first == second)
            {
                return 0;
            }

            string firstActionAsString = firstDeclaration.Action.ToString();
            string secondActionAsString = secondDeclaration.Action.ToString();

            if (firstActionAsString != secondActionAsString)
            {
                return firstActionAsString.CompareTo(secondActionAsString);
            }

            #region Compare Properties

            int maxProperties = Math.Max(first.Properties.Count, second.Properties.Count);

            for (int i = 0; i < maxProperties; i++)
            {

                if (i >= first.Properties.Count)
                {
                    return 1;
                }
                if (i >= second.Properties.Count)
                {
                    return -1;
                }

                CustomAttributeNamedArgument firstProperty = first.Properties[i];
                CustomAttributeNamedArgument secondProperty = second.Properties[i];

                if (firstProperty.Name == secondProperty.Name)
                {
                    string firstValue = firstProperty.Argument.Value.ToString();
                    string secondValue = secondProperty.Argument.Value.ToString();
                    if (firstValue != secondValue)
                    {
                        return firstValue.CompareTo(secondValue);
                    }
                }
                else
                {
                    return firstProperty.Name.CompareTo(secondProperty.Name);
                }
            }

            #endregion

            #region Compare Fiels

            int maxFields = Math.Max(first.Fields.Count, second.Fields.Count);

            for (int i = 0; i < maxFields; i++)
            {

                if (i >= first.Fields.Count)
                {
                    return 1;
                }
                if (i >= second.Fields.Count)
                {
                    return -1;
                }

                CustomAttributeNamedArgument firstField = first.Fields[i];
                CustomAttributeNamedArgument secondField = second.Fields[i];

                if (firstField.Name == secondField.Name)
                {
                    string firstValue = firstField.Argument.Value.ToString();
                    string secondValue = secondField.Argument.Value.ToString();
                    if (firstValue != secondValue)
                    {
                        return firstValue.CompareTo(secondValue);
                    }
                }
                else
                {
                    return firstField.Name.CompareTo(secondField.Name);
                }
            }

            #endregion

            return 0;

        }
        static bool TryProcessPermissionSetAttribute(SecurityDeclaration declaration, out PermissionSet set)
        {
            set = null;

            if (!declaration.HasSecurityAttributes && declaration.SecurityAttributes.Count != 1)
                return false;

            var security_attribute = declaration.SecurityAttributes [0];
            if (!security_attribute.AttributeType.IsTypeOf ("System.Security.Permissions", "PermissionSetAttribute"))
                return false;

            var named_argument = security_attribute.Properties [0];
            if (named_argument.Name != "XML")
                throw new NotSupportedException ();

            var attribute = new SSP.PermissionSetAttribute ((SSP.SecurityAction) declaration.Action);
            attribute.XML = (string) named_argument.Argument.Value;

            set = attribute.CreatePermissionSet ();
            return true;
        }
Exemple #9
0
        internal static TypeDefinition Clone(TypeDefinition type, ImportContext context)
        {
            TypeDefinition nt = new TypeDefinition(
                type.Name,
                type.Namespace,
                type.Attributes);

            TypeReference contextType = context.GenericContext.Type;

            context.GenericContext.Type = nt;

            GenericParameter.CloneInto(type, nt, context);

            if (type.BaseType != null)
            {
                nt.BaseType = context.Import(type.BaseType);
            }

            if (type.HasLayoutInfo)
            {
                nt.ClassSize   = type.ClassSize;
                nt.PackingSize = type.PackingSize;
            }

            if (type.HasFields)
            {
                foreach (FieldDefinition field in type.Fields)
                {
                    nt.Fields.Add(FieldDefinition.Clone(field, context));
                }
            }
            if (type.HasConstructors)
            {
                foreach (MethodDefinition ctor in type.Constructors)
                {
                    nt.Constructors.Add(MethodDefinition.Clone(ctor, context));
                }
            }
            if (type.HasMethods)
            {
                foreach (MethodDefinition meth in type.Methods)
                {
                    nt.Methods.Add(MethodDefinition.Clone(meth, context));
                }
            }
            if (type.HasEvents)
            {
                foreach (EventDefinition evt in type.Events)
                {
                    nt.Events.Add(EventDefinition.Clone(evt, context));
                }
            }
            if (type.HasProperties)
            {
                foreach (PropertyDefinition prop in type.Properties)
                {
                    nt.Properties.Add(PropertyDefinition.Clone(prop, context));
                }
            }
            if (type.HasInterfaces)
            {
                foreach (TypeReference intf in type.Interfaces)
                {
                    nt.Interfaces.Add(context.Import(intf));
                }
            }
            if (type.HasNestedTypes)
            {
                foreach (TypeDefinition nested in type.NestedTypes)
                {
                    nt.NestedTypes.Add(Clone(nested, context));
                }
            }
            if (type.HasCustomAttributes)
            {
                foreach (CustomAttribute ca in type.CustomAttributes)
                {
                    nt.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
                }
            }
            if (type.HasSecurityDeclarations)
            {
                foreach (SecurityDeclaration dec in type.SecurityDeclarations)
                {
                    nt.SecurityDeclarations.Add(SecurityDeclaration.Clone(dec));
                }
            }

            context.GenericContext.Type = contextType;

            return(nt);
        }
 /// <summary>
 /// Gets the dependencies of the given item.
 /// </summary>
 /// <param name="item">Item to get dependencies of.</param>
 /// <returns>Dependencies of <paramref name="item"/>.</returns>
 protected override IEnumerable <object> InvokeForItem(SecurityDeclaration item)
 {
     return(new object[0]);
 }
Exemple #11
0
 public void VisitSecurityDeclaration(SecurityDeclaration secDecl)
 {
 }
Exemple #12
0
 private bool WriteSecurityAttribute(ModuleDefinition module, bool isAssemblyDeclaration, SecurityAttribute attribute, SecurityDeclaration securityDeclaration, out bool wroteArgument, bool skipTheNewLine = false, bool isReturnValueAttribute = false)
 {
     this.genericWriter.WriteToken(this.get_OpeningBracket());
     if (!isAssemblyDeclaration)
     {
         if (isReturnValueAttribute)
         {
             this.WriteReturnValueAttributeKeyword();
         }
     }
     else
     {
         this.genericWriter.WriteKeyword(this.genericWriter.get_KeyWordWriter().get_Assembly());
         this.genericWriter.Write(":");
         this.genericWriter.WriteSpace();
     }
     if (attribute.get_AttributeType().get_Name().EndsWith("Attribute"))
     {
         stackVariable19 = attribute.get_AttributeType().get_Name().Remove(attribute.get_AttributeType().get_Name().LastIndexOf("Attribute"));
     }
     else
     {
         stackVariable19 = attribute.get_AttributeType().get_Name();
     }
     V_0 = stackVariable19;
     this.genericWriter.WriteNamespaceIfTypeInCollision(attribute.get_AttributeType());
     this.genericWriter.WriteReference(V_0, attribute.get_AttributeType());
     this.genericWriter.WriteToken("(");
     V_1 = securityDeclaration.GetSecurityActionTypeReference(module);
     if (V_1.get_IsDefinition())
     {
         stackVariable38 = V_1 as TypeDefinition;
     }
     else
     {
         stackVariable38 = V_1.Resolve();
     }
     V_2 = stackVariable38;
     if (V_2 != null && V_2.get_IsEnum())
     {
         V_3 = EnumValueToFieldCombinationMatcher.GetEnumFieldDefinitionByValue(V_2.get_Fields(), (Int32)securityDeclaration.get_Action(), V_2.get_CustomAttributes());
         if (V_3.get_Count() == 0)
         {
             this.WriteSecurityAttributeAction(securityDeclaration.get_Action());
         }
         else
         {
             V_4 = 0;
             while (V_4 < V_3.get_Count())
             {
                 this.genericWriter.WriteReferenceAndNamespaceIfInCollision(V_3.get_Item(V_4).get_DeclaringType());
                 this.genericWriter.WriteToken(".");
                 this.genericWriter.WriteEnumValueField(V_3.get_Item(V_4));
                 if (V_4 + 1 < V_3.get_Count())
                 {
                     this.genericWriter.WriteSpace();
                     this.genericWriter.WriteBitwiseOr();
                     this.genericWriter.WriteSpace();
                 }
                 V_4 = V_4 + 1;
             }
         }
     }
     wroteArgument = true;
     if (attribute.get_HasFields() || attribute.get_HasProperties())
     {
         V_5 = attribute.get_AttributeType().Resolve();
         if (attribute.get_HasProperties())
         {
             wroteArgument = this.WriteAttributeNamedArgs(V_5, attribute.get_Properties(), false, wroteArgument);
         }
         if (attribute.get_HasFields())
         {
             dummyVar0 = this.WriteAttributeNamedArgs(V_5, attribute.get_Fields(), true, wroteArgument);
         }
     }
     this.genericWriter.WriteToken(")");
     this.genericWriter.WriteToken(this.get_ClosingBracket());
     if (!skipTheNewLine)
     {
         this.genericWriter.WriteLine();
     }
     return(wroteArgument);
 }