Ejemplo n.º 1
0
        public void ClientSecurityCallbackCalled()
        {
            Guid iid = Guid.NewGuid();
            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_ip_tcp, "18080", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo clientInfo, byte[] arg)
                {
                    return arg;
                };

                var endpoingBinding = new EndpointBindingInfo(RpcProtseq.ncacn_ip_tcp, "127.0.0.1", "18080");
                bool securityCalled = false;
                using (var client = new ExplicitBytesClient (iid, endpoingBinding)) {

                    var authCallback = new FunctionPtr<RPC_C_SECURITY_CALLBACK>(x=>{
                        securityCalled = true;
                        IntPtr securityContext = IntPtr.Zero;
                        var getSecurityStatus = NativeMethods.I_RpcBindingInqSecurityContext(x, out securityContext);
                        Assert.AreEqual (RPC_STATUS.RPC_S_OK, getSecurityStatus);
                        Assert.AreNotEqual(IntPtr.Zero,securityContext);
                    });
                    client.AuthenticateAs (null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    var setStatus = NativeMethods.RpcBindingSetOption (client.Handle, (uint)RpcBindingOptions.RPC_C_OPT_SECURITY_CALLBACK, authCallback.Handle);
                    Assert.AreEqual (RPC_STATUS.RPC_S_OK, setStatus);
                    client.Execute (new byte[0]);

                }
                Assert.IsTrue (securityCalled);

            }
        }
Ejemplo n.º 2
0
        public void ClientSecurityCallbackCalled()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_ip_tcp, "18080", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo clientInfo, byte[] arg)
                {
                    return(arg);
                };


                var  endpoingBinding = new EndpointBindingInfo(RpcProtseq.ncacn_ip_tcp, "127.0.0.1", "18080");
                bool securityCalled  = false;
                using (var client = new ExplicitBytesClient(iid, endpoingBinding)) {
                    var authCallback = new FunctionPtr <RPC_C_SECURITY_CALLBACK>(x => {
                        securityCalled         = true;
                        IntPtr securityContext = IntPtr.Zero;
                        var getSecurityStatus  = NativeMethods.I_RpcBindingInqSecurityContext(x, out securityContext);
                        Assert.AreEqual(RPC_STATUS.RPC_S_OK, getSecurityStatus);
                        Assert.AreNotEqual(IntPtr.Zero, securityContext);
                    });
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    var setStatus = NativeMethods.RpcBindingSetOption(client.Handle, (uint)RpcBindingOptions.RPC_C_OPT_SECURITY_CALLBACK, authCallback.Handle);
                    Assert.AreEqual(RPC_STATUS.RPC_S_OK, setStatus);
                    client.Execute(new byte[0]);
                }
                Assert.IsTrue(securityCalled);
            }
        }
Ejemplo n.º 3
0
        public void TestFunctionPtr()
        {
            // Note: All delegates are the same; this test will need to be changed if they ever change.
            FunctionPtr <NativeCalculator.AddFunction> functionPtr = new FunctionPtr <NativeCalculator.AddFunction>((ulong)_nativeCalculator.VTable);

            for (int x = 1; x < 100; x++)
            {
                for (int y = 100; y > 0; y--)
                {
                    int add      = x + y;
                    int subtract = x - y;
                    int multiply = x * y;
                    int divide   = x / y;

                    Assert.Equal(add, functionPtr[(int)NativeCalculator.VTableFunctions.Add](x, y));
                    Assert.Equal(subtract, functionPtr[(int)NativeCalculator.VTableFunctions.Subtract](x, y));
                    Assert.Equal(multiply, functionPtr[(int)NativeCalculator.VTableFunctions.Multiply](x, y));
                    Assert.Equal(divide, functionPtr[(int)NativeCalculator.VTableFunctions.Divide](x, y));
                }
            }
        }
Ejemplo n.º 4
0
        private static void InvokeRpcAsync(RpcHandle handle, Guid iid, byte[] input, APICallback callback)
        {
            Log.Verbose("InvokeRpcAsync on {0}, sending {1} bytes", handle.Handle, input.Length);
            Ptr <MIDL_STUB_DESC> pStub;

            if (!handle.GetPtr(out pStub))
            {
                pStub = handle.CreatePtr(new MIDL_STUB_DESC(
                                             handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false));
            }

            int    cbOutput  = 0;
            var    pcbOutput = new Ptr <int>(cbOutput);
            IntPtr result    = IntPtr.Zero;
            IntPtr output    = IntPtr.Zero;
            var    pOutput   = new Ptr <IntPtr>(output);

            using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input))
            {
                AsyncCallback cbRountine = (IntPtr pAsync, IntPtr pContext, RpcAsyncEvent _event) =>
                {
                    RPC_ASYNC_STATE async      = (RPC_ASYNC_STATE)Marshal.PtrToStructure(pAsync, typeof(RPC_ASYNC_STATE));
                    var             myCallback = (APICallback)Marshal.GetDelegateForFunctionPointer(async.UserInfo, typeof(APICallback));

                    RpcError hr = RpcApi.RpcAsyncCompleteCall(pAsync, ref result);
                    if (RpcError.RPC_S_OK == hr)
                    {
                        if (RpcError.RPC_S_OK == (RpcError)result.ToInt32())
                        {
                            ExecuteAsyncResponse response;
                            try
                            {
                                cbOutput = pcbOutput.Data;
                                byte[] _output = new byte[cbOutput];
                                Marshal.Copy(pOutput.Data, _output, 0, cbOutput);

                                response.result   = RpcError.RPC_S_OK;
                                response.response = _output;
                            }
                            catch (Exception e)
                            {
                                response.result   = RpcError.RPC_E_FAIL;
                                response.response = null;
                            }
                            finally
                            {
                                RpcApi.Free(pOutput.Data);
                            }

                            myCallback(response);
                        }
                        else
                        {
                            ExecuteAsyncResponse response;
                            response.result   = (RpcError)result.ToInt32();
                            response.response = null;
                            myCallback(response);
                        }
                    }
                    else
                    {
                        ExecuteAsyncResponse response;
                        response.result   = hr;
                        response.response = null;
                        myCallback(response);
                    }
                };

                var pCallbackRoutine = new FunctionPtr <AsyncCallback>(cbRountine);

                RPC_ASYNC_STATE asyncState   = new RPC_ASYNC_STATE();
                var             _pAsyncState = new Ptr <RPC_ASYNC_STATE>(asyncState);
                RpcApi.RpcAsyncInitializeHandle(_pAsyncState.Handle, Marshal.SizeOf(typeof(RPC_ASYNC_STATE)));
                asyncState.Event                 = _pAsyncState.Data.Event;
                asyncState.Flags                 = _pAsyncState.Data.Flags;
                asyncState.Lock                  = _pAsyncState.Data.Lock;
                asyncState.RuntimeInfo           = _pAsyncState.Data.RuntimeInfo;
                asyncState.Signature             = _pAsyncState.Data.Signature;
                asyncState.Size                  = _pAsyncState.Data.Size;
                asyncState.StubInfo              = _pAsyncState.Data.StubInfo;
                asyncState.NotificationType      = RpcNotificationTypes.RpcNotificationTypeCallback;
                asyncState.u.NotificationRoutine = pCallbackRoutine.Handle;

                var ptrCB = Marshal.GetFunctionPointerForDelegate(callback);
                asyncState.UserInfo = ptrCB;

                var pAsyncState = new Ptr <RPC_ASYNC_STATE>(asyncState);

                if (RpcApi.Is64BitProcess)
                {
                    try
                    {
                        result = RpcApi.NdrAsyncClientCallx64(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pAsyncState.Handle, handle.Handle,
                                                              input.Length, pInputBuffer.Handle, out cbOutput, out output);
                    }
                    catch (SEHException ex)
                    {
                        RpcException.Assert(ex.ErrorCode);
                        throw;
                    }
                }
                else
                {
                    using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[7]))
                    {
                        pStack32.Data[0] = pAsyncState.Handle.ToInt32();
                        pStack32.Data[1] = handle.Handle.ToInt32();
                        pStack32.Data[2] = input.Length;
                        pStack32.Data[3] = pInputBuffer.Handle.ToInt32();
                        pStack32.Data[4] = pcbOutput.Handle.ToInt32();
                        pStack32.Data[5] = pOutput.Handle.ToInt32();
                        pStack32.Data[6] = 0; //reserved

                        try
                        {
                            result = RpcApi.NdrAsyncClientCallx86(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pStack32.Handle);
                        }
                        catch (SEHException ex)
                        {
                            Log.Verbose("exception on {0}", ex);
                            RpcException.Assert(ex.ErrorCode);
                            throw;
                        }
                    }
                }

                GC.KeepAlive(pInputBuffer);
            }
            RpcException.Assert(result.ToInt32());
            Log.Verbose("InvokeRpc.InvokeRpc response on {0}", handle.Handle);
        }
Ejemplo n.º 5
0
        private static void InvokeRpcAsync(RpcHandle handle, Guid iid, byte[] input, APICallback callback)
        {
            Log.Verbose("InvokeRpcAsync on {0}, sending {1} bytes", handle.Handle, input.Length);
              Ptr<MIDL_STUB_DESC> pStub;
              if (!handle.GetPtr(out pStub))
              {
            pStub = handle.CreatePtr(new MIDL_STUB_DESC(
              handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false));
              }

              int cbOutput = 0;
              var pcbOutput = new Ptr<int>(cbOutput);
              IntPtr result = IntPtr.Zero;
              IntPtr output = IntPtr.Zero;
              var pOutput = new Ptr<IntPtr>(output);

              using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input))
              {
            AsyncCallback cbRountine = (IntPtr pAsync, IntPtr pContext, RpcAsyncEvent _event) =>
            {
              RPC_ASYNC_STATE async = (RPC_ASYNC_STATE)Marshal.PtrToStructure(pAsync, typeof(RPC_ASYNC_STATE));
              var myCallback = (APICallback)Marshal.GetDelegateForFunctionPointer(async.UserInfo, typeof(APICallback));

              RpcError hr = RpcApi.RpcAsyncCompleteCall(pAsync, ref result);
              if (RpcError.RPC_S_OK == hr)
              {
            if (RpcError.RPC_S_OK == (RpcError) result.ToInt32())
            {
              ExecuteAsyncResponse response;
              try
              {
                cbOutput = pcbOutput.Data;
                byte[] _output = new byte[cbOutput];
                Marshal.Copy(pOutput.Data, _output, 0, cbOutput);

                response.result = RpcError.RPC_S_OK;
                response.response = _output;
              }
              catch (Exception e)
              {
                response.result = RpcError.RPC_E_FAIL;
                response.response = null;
              }
              finally
              {
                RpcApi.Free(pOutput.Data);
              }

              myCallback(response);
            }
            else
            {
              ExecuteAsyncResponse response;
              response.result = (RpcError)result.ToInt32();
              response.response = null;
              myCallback(response);
            }
              }
              else
              {
            ExecuteAsyncResponse response;
            response.result = hr;
            response.response = null;
            myCallback(response);
              }
            };

            var pCallbackRoutine = new FunctionPtr<AsyncCallback>(cbRountine);

            RPC_ASYNC_STATE asyncState = new RPC_ASYNC_STATE();
            var _pAsyncState = new Ptr<RPC_ASYNC_STATE>(asyncState);
            RpcApi.RpcAsyncInitializeHandle(_pAsyncState.Handle, Marshal.SizeOf(typeof(RPC_ASYNC_STATE)));
            asyncState.Event = _pAsyncState.Data.Event;
            asyncState.Flags = _pAsyncState.Data.Flags;
            asyncState.Lock = _pAsyncState.Data.Lock;
            asyncState.RuntimeInfo = _pAsyncState.Data.RuntimeInfo;
            asyncState.Signature = _pAsyncState.Data.Signature;
            asyncState.Size = _pAsyncState.Data.Size;
            asyncState.StubInfo = _pAsyncState.Data.StubInfo;
            asyncState.NotificationType = RpcNotificationTypes.RpcNotificationTypeCallback;
            asyncState.u.NotificationRoutine = pCallbackRoutine.Handle;

            var ptrCB = Marshal.GetFunctionPointerForDelegate(callback);
            asyncState.UserInfo = ptrCB;

            var pAsyncState = new Ptr<RPC_ASYNC_STATE>(asyncState);

            if (RpcApi.Is64BitProcess)
            {
              try
              {
            result = RpcApi.NdrAsyncClientCallx64(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pAsyncState.Handle, handle.Handle,
              input.Length, pInputBuffer.Handle, out cbOutput, out output);
              }
              catch (SEHException ex)
              {
            RpcException.Assert(ex.ErrorCode);
            throw;
              }
            }
            else
            {
              using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[7]))
              {
            pStack32.Data[0] = pAsyncState.Handle.ToInt32();
            pStack32.Data[1] = handle.Handle.ToInt32();
            pStack32.Data[2] = input.Length;
            pStack32.Data[3] = pInputBuffer.Handle.ToInt32();
            pStack32.Data[4] = pcbOutput.Handle.ToInt32();
            pStack32.Data[5] = pOutput.Handle.ToInt32();
            pStack32.Data[6] = 0; //reserved

            try
            {
              result = RpcApi.NdrAsyncClientCallx86(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pStack32.Handle);
            }
            catch (SEHException ex)
            {
              Log.Verbose("exception on {0}", ex);
              RpcException.Assert(ex.ErrorCode);
              throw;
            }
              }
            }

            GC.KeepAlive(pInputBuffer);
              }
              RpcException.Assert(result.ToInt32());
              Log.Verbose("InvokeRpc.InvokeRpc response on {0}", handle.Handle);
        }