PartialTrustHelpers.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / System.Runtime.DurableInstancing / System / Runtime / PartialTrustHelpers.cs / 1305376 / PartialTrustHelpers.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.Runtime 
{
    using System.Security; 
    using System.Security.Permissions; 
    using System.Runtime.CompilerServices;
    using System.Reflection; 

    static class PartialTrustHelpers
    {
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical]
        static Type aptca; 
 
        internal static bool ShouldFlowSecurityContext
        { 
            [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
            [SecurityCritical]
            get
            { 
                if (AppDomain.CurrentDomain.IsHomogenous)
                { 
                    return false; 
                }
 
                return SecurityManager.CurrentThreadRequiresSecurityContextCapture();
            }
        }
 
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
        [SecurityCritical] 
        internal static bool IsInFullTrust() 
        {
            if (AppDomain.CurrentDomain.IsHomogenous) 
            {
                return AppDomain.CurrentDomain.IsFullyTrusted;
            }
            else 
            {
                if (!SecurityManager.CurrentThreadRequiresSecurityContextCapture()) 
                { 
                    return true;
                } 

                try
                {
                    DemandForFullTrust(); 
                    return true;
                } 
                catch (SecurityException) 
                {
                    return false; 
                }
            }
        }
 
        [Fx.Tag.SecurityNote(Critical = "Captures security context with identity flow suppressed, " +
            "this requires satisfying a LinkDemand for infrastructure.")] 
        [SecurityCritical] 
        internal static SecurityContext CaptureSecurityContextNoIdentityFlow()
        { 
            // capture the security context but never flow windows identity
            if (SecurityContext.IsWindowsIdentityFlowSuppressed())
            {
                return SecurityContext.Capture(); 
            }
            else 
            { 
                using (SecurityContext.SuppressFlowWindowsIdentity())
                { 
                    return SecurityContext.Capture();
                }
            }
        } 

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical] 
        internal static bool IsTypeAptca(Type type)
        { 
            Assembly assembly = type.Assembly;
            return IsAssemblyAptca(assembly) || !IsAssemblySigned(assembly);
        }
 
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
        [SecurityCritical] 
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] 
        [MethodImpl(MethodImplOptions.NoInlining)]
        static void DemandForFullTrust() 
        {
        }

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical]
        static bool IsAssemblyAptca(Assembly assembly) 
        { 
            if (aptca == null)
            { 
                aptca = typeof(AllowPartiallyTrustedCallersAttribute);
            }
            return assembly.GetCustomAttributes(aptca, false).Length > 0;
        } 

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical] 
        [FileIOPermission(SecurityAction.Assert, Unrestricted = true)]
        static bool IsAssemblySigned(Assembly assembly) 
        {
            byte[] publicKeyToken = assembly.GetName().GetPublicKeyToken();
            return publicKeyToken != null & publicKeyToken.Length > 0;
        } 

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical] 
        internal static bool CheckAppDomainPermissions(PermissionSet permissions)
        { 
            return AppDomain.CurrentDomain.IsHomogenous &&
                   permissions.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);
        }
 
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
        [SecurityCritical] 
        internal static bool HasEtwPermissions() 
        {
            //Currently unrestricted permissions are required to create Etw provider. 
            PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted);
            return CheckAppDomainPermissions(permissions);
        }
 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.Runtime 
{
    using System.Security; 
    using System.Security.Permissions; 
    using System.Runtime.CompilerServices;
    using System.Reflection; 

    static class PartialTrustHelpers
    {
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical]
        static Type aptca; 
 
        internal static bool ShouldFlowSecurityContext
        { 
            [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
            [SecurityCritical]
            get
            { 
                if (AppDomain.CurrentDomain.IsHomogenous)
                { 
                    return false; 
                }
 
                return SecurityManager.CurrentThreadRequiresSecurityContextCapture();
            }
        }
 
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
        [SecurityCritical] 
        internal static bool IsInFullTrust() 
        {
            if (AppDomain.CurrentDomain.IsHomogenous) 
            {
                return AppDomain.CurrentDomain.IsFullyTrusted;
            }
            else 
            {
                if (!SecurityManager.CurrentThreadRequiresSecurityContextCapture()) 
                { 
                    return true;
                } 

                try
                {
                    DemandForFullTrust(); 
                    return true;
                } 
                catch (SecurityException) 
                {
                    return false; 
                }
            }
        }
 
        [Fx.Tag.SecurityNote(Critical = "Captures security context with identity flow suppressed, " +
            "this requires satisfying a LinkDemand for infrastructure.")] 
        [SecurityCritical] 
        internal static SecurityContext CaptureSecurityContextNoIdentityFlow()
        { 
            // capture the security context but never flow windows identity
            if (SecurityContext.IsWindowsIdentityFlowSuppressed())
            {
                return SecurityContext.Capture(); 
            }
            else 
            { 
                using (SecurityContext.SuppressFlowWindowsIdentity())
                { 
                    return SecurityContext.Capture();
                }
            }
        } 

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical] 
        internal static bool IsTypeAptca(Type type)
        { 
            Assembly assembly = type.Assembly;
            return IsAssemblyAptca(assembly) || !IsAssemblySigned(assembly);
        }
 
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
        [SecurityCritical] 
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] 
        [MethodImpl(MethodImplOptions.NoInlining)]
        static void DemandForFullTrust() 
        {
        }

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical]
        static bool IsAssemblyAptca(Assembly assembly) 
        { 
            if (aptca == null)
            { 
                aptca = typeof(AllowPartiallyTrustedCallersAttribute);
            }
            return assembly.GetCustomAttributes(aptca, false).Length > 0;
        } 

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical] 
        [FileIOPermission(SecurityAction.Assert, Unrestricted = true)]
        static bool IsAssemblySigned(Assembly assembly) 
        {
            byte[] publicKeyToken = assembly.GetName().GetPublicKeyToken();
            return publicKeyToken != null & publicKeyToken.Length > 0;
        } 

        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")] 
        [SecurityCritical] 
        internal static bool CheckAppDomainPermissions(PermissionSet permissions)
        { 
            return AppDomain.CurrentDomain.IsHomogenous &&
                   permissions.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);
        }
 
        [Fx.Tag.SecurityNote(Critical = "used in a security-sensitive decision")]
        [SecurityCritical] 
        internal static bool HasEtwPermissions() 
        {
            //Currently unrestricted permissions are required to create Etw provider. 
            PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted);
            return CheckAppDomainPermissions(permissions);
        }
 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK