Example #1
0
        /// <summary>
        /// Implement <see cref="IPermission.Copy"/>. This creates an identical copy of the current
        /// permission instance and returns it to the caller.
        /// </summary>
        /// <returns></returns>
        public override IPermission Copy()
        {
            CryptographicPermission permission = new CryptographicPermission(PermissionState.None);

            permission._permFlag = this._permFlag;

            return(permission);
        }
Example #2
0
        /// <summary>
        /// Implement the IPermission.IsSubsetOf. This method returns a bool to indicate
        /// whether or not the current permission is a subset of the supplied permission. To be a subset, every item of state in the current permission must also be in the target permission.
        /// </summary>
        /// <param name="target">See <see cref="IPermission"/>.</param>
        /// <returns></returns>
        public override bool IsSubsetOf(IPermission target)
        {
            // An input of null indicates a permission with no state.
            // The permission can only be a subset if it's in a similar empty state.
            bool canEncrypt, canDecrypt, canSign;
            bool canTargetEncrypt, canTargetDecrypt, canTargetSignature;

            canEncrypt = (this._permFlag & CryptographicPermissionFlags.Encrypt).Equals(CryptographicPermissionFlags.Encrypt);
            canDecrypt = (this._permFlag & CryptographicPermissionFlags.Decrypt).Equals(CryptographicPermissionFlags.Decrypt);
            canSign    = (this._permFlag & CryptographicPermissionFlags.Sign).Equals(CryptographicPermissionFlags.Sign);

            if (target == null)
            {
                if (canEncrypt == false && canDecrypt == false && canSign == false)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

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

            // Cast the target to an EncryptionPermission.
            CryptographicPermission targetPerm = (CryptographicPermission)target;

            canTargetEncrypt   = (targetPerm._permFlag & CryptographicPermissionFlags.Encrypt).Equals(CryptographicPermissionFlags.Encrypt);
            canTargetDecrypt   = (targetPerm._permFlag & CryptographicPermissionFlags.Decrypt).Equals(CryptographicPermissionFlags.Decrypt);
            canTargetSignature = (targetPerm._permFlag & CryptographicPermissionFlags.Sign).Equals(CryptographicPermissionFlags.Sign);

            // Every value set (true) in this permission must be in the target.
            // The following code checks to see if the current permission is a subset
            // of the target. If the current permission has something that the target
            // does not have, it cannot be a subset.

            if (canEncrypt == true && canTargetEncrypt == false)
            {
                return(false);
            }
            if (canDecrypt == true && canTargetDecrypt == false)
            {
                return(false);
            }
            if (canSign == true && canTargetSignature == false)
            {
                return(false);
            }

            return(true);
        }
Example #3
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));
        }
Example #4
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));
        }