示例#1
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public override Object Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
        {
            INVOCATION_FLAGS invocationFlags = InvocationFlags;

            // get the declaring TypeHandle early for consistent exceptions in IntrospectionOnly context
            RuntimeTypeHandle declaringTypeHandle = m_declaringType.TypeHandle;

            if ((invocationFlags & (INVOCATION_FLAGS.INVOCATION_FLAGS_NO_INVOKE | INVOCATION_FLAGS.INVOCATION_FLAGS_CONTAINS_STACK_POINTERS | INVOCATION_FLAGS.INVOCATION_FLAGS_NO_CTOR_INVOKE)) != 0)
            {
                ThrowNoInvokeException();
            }

#if FEATURE_APPX
            if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0)
            {
                StackCrawlMark  stackMark = StackCrawlMark.LookForMyCaller;
                RuntimeAssembly caller    = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
                if (caller != null && !caller.IsSafeForReflection())
                {
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext", FullName));
                }
            }
#endif

            if ((invocationFlags & (INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD | INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY | INVOCATION_FLAGS.INVOCATION_FLAGS_IS_DELEGATE_CTOR)) != 0)
            {
#if !FEATURE_CORECLR
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD) != 0)
                {
                    CodeAccessPermission.Demand(PermissionType.ReflectionMemberAccess);
                }
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY) != 0)
#endif // !FEATURE_CORECLR
                RuntimeMethodHandle.PerformSecurityCheck(null, this, m_declaringType, (uint)(m_invocationFlags | INVOCATION_FLAGS.INVOCATION_FLAGS_CONSTRUCTOR_INVOKE));
#if !FEATURE_CORECLR
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_IS_DELEGATE_CTOR) != 0)
                {
                    new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
                }
#endif // !FEATURE_CORECLR
            }

            // get the signature
            Signature sig = Signature;

            int formalCount = sig.Arguments.Length;
            int actualCount = (parameters != null) ? parameters.Length : 0;
            if (formalCount != actualCount)
            {
                throw new TargetParameterCountException(Environment.GetResourceString("Arg_ParmCnt"));
            }

            // We don't need to explicitly invoke the class constructor here,
            // JIT/NGen will insert the call to .cctor in the instance ctor.

            // if we are here we passed all the previous checks. Time to look at the arguments
            if (actualCount > 0)
            {
                Object[] arguments = CheckArguments(parameters, binder, invokeAttr, culture, sig);
                Object   retValue  = RuntimeMethodHandle.InvokeMethod(null, arguments, sig, true);
                // copy out. This should be made only if ByRef are present.
                for (int index = 0; index < arguments.Length; index++)
                {
                    parameters[index] = arguments[index];
                }
                return(retValue);
            }
            return(RuntimeMethodHandle.InvokeMethod(null, null, sig, true));
        }
示例#2
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public override Object Invoke(
            Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
        {
            INVOCATION_FLAGS invocationFlags = InvocationFlags;

            if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NO_INVOKE) != 0)
            {
                ThrowNoInvokeException();
            }

            // check basic method consistency. This call will throw if there are problems in the target/method relationship
            CheckConsistency(obj);

#if FEATURE_APPX
            if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0)
            {
                StackCrawlMark  stackMark = StackCrawlMark.LookForMyCaller;
                RuntimeAssembly caller    = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
                if (caller != null && !caller.IsSafeForReflection())
                {
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext", FullName));
                }
            }
#endif

            if (obj != null)
            {
#if FEATURE_CORECLR
                // For unverifiable code, we require the caller to be critical.
                // Adding the INVOCATION_FLAGS_NEED_SECURITY flag makes that check happen
                invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY;
#else // FEATURE_CORECLR
                new SecurityPermission(SecurityPermissionFlag.SkipVerification).Demand();
#endif // FEATURE_CORECLR
            }

#if !FEATURE_CORECLR
            if ((invocationFlags & (INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD | INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY)) != 0)
            {
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD) != 0)
                {
                    CodeAccessPermission.Demand(PermissionType.ReflectionMemberAccess);
                }
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY) != 0)
                {
                    RuntimeMethodHandle.PerformSecurityCheck(obj, this, m_declaringType, (uint)m_invocationFlags);
                }
            }
#endif // !FEATURE_CORECLR

            Signature sig = Signature;

            // get the signature
            int formalCount = sig.Arguments.Length;
            int actualCount = (parameters != null) ? parameters.Length : 0;
            if (formalCount != actualCount)
            {
                throw new TargetParameterCountException(Environment.GetResourceString("Arg_ParmCnt"));
            }

            // if we are here we passed all the previous checks. Time to look at the arguments
            if (actualCount > 0)
            {
                Object[] arguments = CheckArguments(parameters, binder, invokeAttr, culture, sig);
                Object   retValue  = RuntimeMethodHandle.InvokeMethod(obj, arguments, sig, false);
                // copy out. This should be made only if ByRef are present.
                for (int index = 0; index < arguments.Length; index++)
                {
                    parameters[index] = arguments[index];
                }
                return(retValue);
            }
            return(RuntimeMethodHandle.InvokeMethod(obj, null, sig, false));
        }
示例#3
0
        internal void Serialize(object graph, Header[] inHeaders, __BinaryWriter serWriter, bool fCheck)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph", Environment.GetResourceString("ArgumentNull_Graph"));
            }
            if (serWriter == null)
            {
                throw new ArgumentNullException("serWriter", Environment.GetResourceString("ArgumentNull_WithParamName", new object[] { "serWriter" }));
            }
            if (fCheck)
            {
                CodeAccessPermission.Demand(PermissionType.SecuritySerialization);
            }
            this.serWriter = serWriter;
            this.headers   = inHeaders;
            serWriter.WriteBegin();
            long headerId          = 0L;
            bool flag2             = false;
            bool flag3             = false;
            IMethodCallMessage mcm = graph as IMethodCallMessage;

            if (mcm != null)
            {
                flag2 = true;
                graph = this.WriteMethodCall(mcm);
            }
            else
            {
                IMethodReturnMessage mrm = graph as IMethodReturnMessage;
                if (mrm != null)
                {
                    flag3 = true;
                    graph = this.WriteMethodReturn(mrm);
                }
            }
            if (graph == null)
            {
                this.WriteSerializedStreamHeader(this.topId, headerId);
                if (flag2)
                {
                    serWriter.WriteMethodCall();
                }
                else if (flag3)
                {
                    serWriter.WriteMethodReturn();
                }
                serWriter.WriteSerializationHeaderEnd();
                serWriter.WriteEnd();
            }
            else
            {
                object obj2;
                long   num2;
                bool   flag;
                this.m_idGenerator        = new ObjectIDGenerator();
                this.m_objectQueue        = new Queue();
                this.m_formatterConverter = new FormatterConverter();
                this.serObjectInfoInit    = new SerObjectInfoInit();
                this.topId = this.InternalGetId(graph, false, null, out flag);
                if (this.headers != null)
                {
                    headerId = this.InternalGetId(this.headers, false, null, out flag);
                }
                else
                {
                    headerId = -1L;
                }
                this.WriteSerializedStreamHeader(this.topId, headerId);
                if (flag2)
                {
                    serWriter.WriteMethodCall();
                }
                else if (flag3)
                {
                    serWriter.WriteMethodReturn();
                }
                if ((this.headers != null) && (this.headers.Length > 0))
                {
                    this.m_objectQueue.Enqueue(this.headers);
                }
                if (graph != null)
                {
                    this.m_objectQueue.Enqueue(graph);
                }
                while ((obj2 = this.GetNext(out num2)) != null)
                {
                    WriteObjectInfo objectInfo = null;
                    if (obj2 is WriteObjectInfo)
                    {
                        objectInfo = (WriteObjectInfo)obj2;
                    }
                    else
                    {
                        objectInfo         = WriteObjectInfo.Serialize(obj2, this.m_surrogates, this.m_context, this.serObjectInfoInit, this.m_formatterConverter, this, this.m_binder);
                        objectInfo.assemId = this.GetAssemblyId(objectInfo);
                    }
                    objectInfo.objectId = num2;
                    NameInfo memberNameInfo = this.TypeToNameInfo(objectInfo);
                    this.Write(objectInfo, memberNameInfo, memberNameInfo);
                    this.PutNameInfo(memberNameInfo);
                    objectInfo.ObjectEnd();
                }
                serWriter.WriteSerializationHeaderEnd();
                serWriter.WriteEnd();
                this.m_objectManager.RaiseOnSerializedEvent();
            }
        }
示例#4
0
 /// <summary>
 /// Forces a System.Security.SecurityException at run time if all callers higher
 /// in the call stack have not been granted the permission specified by the current
 /// instance.
 /// </summary>
 /// <exception cref="System.Security.SecurityException">A caller higher in the call
 /// stack does not have the permission specified by the current instance</exception>
 public void Demand()
 {
     _permission.Demand();
 }
示例#5
0
 /// <summary>
 /// Make a Demand of the given permission
 /// </summary>
 /// <param name="p"></param>
 internal static void Demand(CodeAccessPermission p)
 {
     p.Demand();
 }
示例#6
0
        internal void Serialize(object graph, Header[] inHeaders, __BinaryWriter serWriter, bool fCheck)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph", Environment.GetResourceString("ArgumentNull_Graph"));
            }
            if (serWriter == null)
            {
                throw new ArgumentNullException("serWriter", Environment.GetResourceString("ArgumentNull_WithParamName", (object)"serWriter"));
            }
            if (fCheck)
            {
                CodeAccessPermission.Demand(PermissionType.SecuritySerialization);
            }
            this.serWriter = serWriter;
            this.headers   = inHeaders;
            serWriter.WriteBegin();
            long headerId          = 0;
            bool flag1             = false;
            bool flag2             = false;
            IMethodCallMessage mcm = graph as IMethodCallMessage;

            if (mcm != null)
            {
                flag1 = true;
                graph = (object)this.WriteMethodCall(mcm);
            }
            else
            {
                IMethodReturnMessage mrm = graph as IMethodReturnMessage;
                if (mrm != null)
                {
                    flag2 = true;
                    graph = (object)this.WriteMethodReturn(mrm);
                }
            }
            if (graph == null)
            {
                this.WriteSerializedStreamHeader(this.topId, headerId);
                if (flag1)
                {
                    serWriter.WriteMethodCall();
                }
                else if (flag2)
                {
                    serWriter.WriteMethodReturn();
                }
                serWriter.WriteSerializationHeaderEnd();
                serWriter.WriteEnd();
            }
            else
            {
                this.m_idGenerator        = new ObjectIDGenerator();
                this.m_objectQueue        = new Queue();
                this.m_formatterConverter = (IFormatterConverter) new FormatterConverter();
                this.serObjectInfoInit    = new SerObjectInfoInit();
                bool isNew;
                this.topId = this.InternalGetId(graph, false, (Type)null, out isNew);
                this.WriteSerializedStreamHeader(this.topId, this.headers == null ? -1L : this.InternalGetId((object)this.headers, false, (Type)null, out isNew));
                if (flag1)
                {
                    serWriter.WriteMethodCall();
                }
                else if (flag2)
                {
                    serWriter.WriteMethodReturn();
                }
                if (this.headers != null && this.headers.Length != 0)
                {
                    this.m_objectQueue.Enqueue((object)this.headers);
                }
                if (graph != null)
                {
                    this.m_objectQueue.Enqueue(graph);
                }
                object next;
                long   objID;
                while ((next = this.GetNext(out objID)) != null)
                {
                    WriteObjectInfo objectInfo1;
                    if (next is WriteObjectInfo)
                    {
                        objectInfo1 = (WriteObjectInfo)next;
                    }
                    else
                    {
                        objectInfo1         = WriteObjectInfo.Serialize(next, this.m_surrogates, this.m_context, this.serObjectInfoInit, this.m_formatterConverter, this, this.m_binder);
                        objectInfo1.assemId = this.GetAssemblyId(objectInfo1);
                    }
                    objectInfo1.objectId = objID;
                    NameInfo        nameInfo1   = this.TypeToNameInfo(objectInfo1);
                    WriteObjectInfo objectInfo2 = objectInfo1;
                    NameInfo        nameInfo2   = nameInfo1;
                    this.Write(objectInfo2, nameInfo2, nameInfo2);
                    this.PutNameInfo(nameInfo1);
                    objectInfo1.ObjectEnd();
                }
                serWriter.WriteSerializationHeaderEnd();
                serWriter.WriteEnd();
                this.m_objectManager.RaiseOnSerializedEvent();
            }
        }
        internal RSACryptoServiceProvider(int dwKeySize, CspParameters parameters, bool useDefaultKeySize)
        {
            int hr;

            //
            //  Save the CSP Parameters
            //

            if (parameters == null)
            {
                _parameters = new CspParameters(1, null, null, m_UseMachineKeyStore);
            }
            else
            {
                // Check the parameter options: specifying either a key container name or UseDefaultKeyContainer flag
                // requires unmanaged code permission
                if (((parameters.Flags & CspProviderFlags.UseDefaultKeyContainer) != 0) ||
                    ((parameters.KeyContainerName != null) && (parameters.KeyContainerName.Length > 0)))
                {
                    _UCpermission.Demand();
                    // If we specified a key container name for this key, then mark it persisted
                    if ((parameters.KeyContainerName != null) && (parameters.KeyContainerName.Length > 0))
                    {
                        // CAPI doesn't accept Container Names longer than 260 characters
                        if (parameters.KeyContainerName.Length > 260)
                        {
                            throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyContainerName"));
                        }

                        _persistKeyInCSP = true;
                    }
                }
                _parameters = parameters;
            }


            //
            //  If no key spec has been specified, then set it to be
            //      AT_KEYEXCHANGE,  if a CALG_* has been specified, then
            //      map that to AT_* value
            if (_parameters.KeyNumber == -1)
            {
                _parameters.KeyNumber = AT_KEYEXCHANGE;
            }
            else if (_parameters.KeyNumber == CALG_RSA_KEYX)
            {
                _parameters.KeyNumber = AT_KEYEXCHANGE;
            }
            else if (_parameters.KeyNumber == CALG_RSA_SIGN)
            {
                _parameters.KeyNumber = AT_SIGNATURE;
            }

            // See if we have the Enhanced RSA provider on this machine
            _hasEnhancedProvider = HasEnhancedProvider();

            // Now determine legal key sizes.  If AT_SIGNATURE, then 384 -- 16386.  Otherwise, depends on
            // whether the strong provider is present.

            if (_parameters.KeyNumber == AT_SIGNATURE)
            {
                LegalKeySizesValue = new KeySizes[1] {
                    new KeySizes(384, 16384, 8)
                };
            }
            else if (_hasEnhancedProvider)
            {
                // it is, we have the strong provider
                LegalKeySizesValue = new KeySizes[1] {
                    new KeySizes(384, 16384, 8)
                };
            }
            else
            {
                // nope, all we have is the base provider
                LegalKeySizesValue = new KeySizes[1] {
                    new KeySizes(384, 512, 8)
                };
                // tone down the default key size
                _defaultKeySize = 512;
            }
            // Set the key size; this will throw an exception if dwKeySize is invalid.
            // Don't check if dwKeySize == 0, since that's the "default size", however
            // *our* default should be 1024 if the CSP can handle it.  So, if the
            // key size was unspecified in a constructor to us, it'll be -1 here and
            // change it to the default size.  If the user really put in a 0 give him back
            // the default for the CSP whatever it is.
            if (useDefaultKeySize)
            {
                dwKeySize = _defaultKeySize;
            }
            if (dwKeySize != 0)
            {
                KeySize = dwKeySize;
            }
            _dwKeySize = dwKeySize;

            //
            //  Create the CSP container for this set of keys
            //
            _hCSP = IntPtr.Zero;
            _hKey = IntPtr.Zero;
            hr    = _CreateCSP(_parameters, ref _hCSP);
            if (hr != 0)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CouldNotAcquire"));
            }
            if (_hCSP == IntPtr.Zero)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CouldNotAcquire"));
            }

            // If the key already exists, use it, else generate a new one
            hr = _GetUserKey(_hCSP, _parameters.KeyNumber, ref _hKey);
            if (hr != 0)
            {
                _hKey = _GenerateKey(_hCSP, _parameters.KeyNumber, dwKeySize << 16);
                if (_hKey == IntPtr.Zero)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CreateKey"));
                }
                // We just gen'd a new key pair, so we have both halves.
                _containerContents = KeyContainerContents.PublicAndExportablePrivate;
            }
            else
            {
                // If the key already exists, make sure to persist it
                _persistKeyInCSP = true;
                // we have both halves, but we don't know if it's exportable or not
                _containerContents = KeyContainerContents.Unknown;
            }

            _CSPHandleProtector = new __CSPHandleProtector(_hCSP, _persistKeyInCSP, _parameters);
            _KeyHandleProtector = new __KeyHandleProtector(_hKey);
        }