예제 #1
0
        /// <summary>
        /// Converts a SecurityElement (or tree of elements) to a permission instance.
        /// </summary>
        /// <param name="elem">See <see cref="SecurityElement"/>.</param>
        public override void FromXml(SecurityElement elem)
        {
            // Check for an unrestricted instance.
            string attrVal = elem.Attribute("Unrestricted");

            if (attrVal != null)
            {
                if (String.Compare(attrVal, "true", true, CultureInfo.InvariantCulture) == 0)
                {
                    this._permFlag = CryptographicPermissionFlags.Encrypt | CryptographicPermissionFlags.Decrypt | CryptographicPermissionFlags.Sign;
                }
                return;
            }

            //Turn off the permission and store flags.
            this._permFlag &= ~(CryptographicPermissionFlags.Encrypt | CryptographicPermissionFlags.Decrypt | CryptographicPermissionFlags.Sign);

            attrVal = elem.Attribute("Flags");

            if (attrVal != null)
            {
                if (attrVal.Trim().Length > 0)
                {
                    this._permFlag = (CryptographicPermissionFlags)Enum.Parse(typeof(CryptographicPermissionFlags), attrVal);
                }
            }
        }
        /// <summary>
        /// This method creates	a permission object that can then be serialized and persisted with the specified
        /// <see cref="SecurityAction"/> enumeration in an assembly’s metadata.
        /// </summary>
        /// <returns>See <see cref="IPermission"/>.</returns>
        public override IPermission CreatePermission()
        {
            // The runtime automatically provides a property to indicate
            // whether or not an unrestricted instance is required.
            if (Unrestricted || (_encrypt && _decrypt))
            {
                return(new CryptographicPermission(PermissionState.Unrestricted));
            }

            // Copy the state from the attribute to the permission object
            CryptographicPermissionFlags perm = 0x0;

            if (_encrypt)
            {
                perm |= CryptographicPermissionFlags.Encrypt;
            }
            if (_decrypt)
            {
                perm |= CryptographicPermissionFlags.Decrypt;
            }
            if (_sign)
            {
                perm |= CryptographicPermissionFlags.Sign;
            }

            // Return the final permission.
            return(new CryptographicPermission(perm));
        }
예제 #3
0
 /// <summary>
 /// It is convention for permission types to provide a constructor
 /// that accepts the <see cref="PermissionState"/> enumeration.
 /// </summary>
 /// <param name="state">See <see cref="PermissionState"/>.</param>
 public CryptographicPermission(PermissionState state) : base()
 {
     if (state == PermissionState.Unrestricted)
     {
         _permFlag = CryptographicPermissionFlags.Encrypt | CryptographicPermissionFlags.Decrypt | CryptographicPermissionFlags.Sign;
     }
     else if (state == PermissionState.None)
     {
         _permFlag &= ~(CryptographicPermissionFlags.Encrypt | CryptographicPermissionFlags.Decrypt | CryptographicPermissionFlags.Sign);
     }
     else
     {
         throw new ArgumentException(Resource.ResourceManager[Resource.MessageKey.InvalidPermissionState, state]);
     }
 }
예제 #4
0
        /// <summary>
        /// Implement <see cref="IPermission.Union"/>. This returns a permission object that is the result
        /// of the set union between the current permission and the supplied permission.
        /// </summary>
        /// <param name="target">See <see cref="IPermission"/>.</param>
        /// <returns></returns>
        public override IPermission Union(IPermission target)
        {
            if (target == null)
            {
                return(Copy());
            }

            if (!target.GetType().Equals(this.GetType()))
            {
                throw new ArgumentException(Resource.ResourceManager[Resource.MessageKey.CryptographicPermissionArgumentException]);
            }

            // Cast the target to an CryptographicPermission.
            CryptographicPermission      targetPerm = (CryptographicPermission)target;
            CryptographicPermissionFlags unionPerm  = this._permFlag | targetPerm._permFlag;

            return(new CryptographicPermission(unionPerm));
        }
예제 #5
0
        /// <summary>
        /// Implement <see cref="IPermission.Intersect"/>. This returns a permission object that is the
        /// result of the set intersection between the current permission and the supplied permission.
        /// </summary>
        /// <param name="target">See <see cref="IPermission"/>.</param>
        /// <returns></returns>
        public override IPermission Intersect(IPermission target)
        {
            // An input of null indicates a permission with no state.
            // There can be no common state, so the method returns null.
            if (target == null)
            {
                return(null);
            }

            if (!target.GetType().Equals(this.GetType()))
            {
                throw new ArgumentException(Resource.ResourceManager[Resource.MessageKey.CryptographicPermissionArgumentException]);
            }

            // Cast target to an CryptographicPermission.
            CryptographicPermission      targetPerm    = (CryptographicPermission)target;
            CryptographicPermissionFlags intersectPerm = this._permFlag & targetPerm._permFlag;

            return(new CryptographicPermission(intersectPerm));
        }
예제 #6
0
 /// <summary>
 /// Base constructor.
 /// </summary>
 public CryptographicPermission() : base()
 {
     _permFlag &= ~CryptographicPermissionFlags.Encrypt | CryptographicPermissionFlags.Decrypt | CryptographicPermissionFlags.Sign;
 }
예제 #7
0
 /// <summary>
 /// This constructor allows you to specify the permission level based on
 /// the <see cref="CryptographicPermissionFlags"/> flag.
 /// </summary>
 /// <param name="permflag">See <see cref="CryptographicPermissionFlags"/>.</param>
 public CryptographicPermission(CryptographicPermissionFlags permflag)
 {
     _permFlag = permflag;
 }