Пример #1
0
        /// <summary>
        /// Parses encrypted buffer in format MESSAGE_LENGTH|ENCRYPTED_MESSAGE|MESSAGE_TRAILER
        /// </summary>
        private static void ParseEncryptedBuffer(byte[] encBuffer, out byte[] encrypted, out byte[] trailer)
        {
            GCHandle gcEncBuffer = GCHandle.Alloc(encBuffer, GCHandleType.Pinned);

            try
            {
                IntPtr sizePtr      = gcEncBuffer.AddrOfPinnedObject();
                int    encryptedLen = Marshal.ReadInt32(sizePtr);

                int trailerLen = encBuffer.Length - 4 - encryptedLen;
                if (trailerLen < 0)
                {
                    throw new ArgumentOutOfRangeException("encBuffer.Length");
                }

                IntPtr encryptedPtr = IntPtrHelper.Add(sizePtr, 4);
                encrypted = new byte[encryptedLen];
                Marshal.Copy(encryptedPtr, encrypted, 0, encryptedLen);

                IntPtr trailerPtr = IntPtrHelper.Add(encryptedPtr, encryptedLen);
                trailer = new byte[trailerLen];
                Marshal.Copy(trailerPtr, trailer, 0, trailerLen);
            }
            finally
            {
                gcEncBuffer.Free();
            }
        }
Пример #2
0
        /// <summary>
        /// Returns buffer data
        /// </summary>
        public byte[] GetBuffer(int index)
        {
            // check object state
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            // parameters validation
            if ((index < 0) || (index >= BuffersCount))
            {
                throw new ArgumentOutOfRangeException("count");
            }

            // get buffer descriptor
            IntPtr secBuffer = IntPtrHelper.Add(pvBuffers, index * SSPINative.SecBuffer.Size);

            // return buffer
            int size = MarshalHelper.ReadInt32(secBuffer, typeof(SSPINative.SecBuffer), "BufferSize");

            if (size > 0)
            {
                return(MarshalHelper.ReadBytes(secBuffer, typeof(SSPINative.SecBuffer), "pvBuffer", size));
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
        //
        // The overlapped function called (either by the thread pool or the socket)
        // when IO completes. (only called on Win9x)
        //
        private void OverlappedCallback(object stateObject, bool Signaled)
        {
#if DEBUG
            // GlobalLog.SetThreadSource(ThreadKinds.Worker);  Because of change 1077887, need logic to determine thread type here.
            using (GlobalLog.SetThreadKind(ThreadKinds.System)) {
#endif
            BaseOverlappedAsyncResult asyncResult = (BaseOverlappedAsyncResult)stateObject;

            GlobalLog.Assert(!asyncResult.InternalPeekCompleted, "AcceptOverlappedAsyncResult#{0}::OverlappedCallback()|asyncResult.IsCompleted", ValidationHelper.HashString(asyncResult));
            //
            // the IO completed asynchronously, see if there was a failure the Internal
            // field in the Overlapped structure will be non zero. to optimize the non
            // error case, we look at it without calling WSAGetOverlappedResult().
            //
            uint errorCode = (uint)Marshal.ReadInt32(IntPtrHelper.Add(asyncResult.m_UnmanagedBlob.DangerousGetHandle(),
                                                                      Win32.OverlappedInternalOffset));
            uint numBytes = errorCode != 0 ? unchecked ((uint)-1) : (uint)Marshal.ReadInt32(IntPtrHelper.Add(asyncResult.m_UnmanagedBlob.DangerousGetHandle(),
                                                                                                             Win32.OverlappedInternalHighOffset));
            //
            // this will release the unmanaged pin handles and unmanaged overlapped ptr
            //
            asyncResult.ErrorCode = (int)errorCode;
            object returnObject = asyncResult.PostCompletion((int)numBytes);
            asyncResult.ReleaseUnmanagedStructures();
            asyncResult.InvokeCallback(returnObject);
#if DEBUG
        }
#endif
        }
Пример #4
0
        /// <summary>
        /// Creates encrypted buffer in format MESSAGE_LENGTH|ENCRYPTED_MESSAGE|MESSAGE_TRAILER
        /// </summary>
        private static byte[] CreateEncryptedBuffer(byte[] encrypted, byte[] trailer)
        {
            int encryptedLen = encrypted.Length;
            int trailerLen   = trailer.Length;

            byte[]   encBuffer   = new byte[4 + encryptedLen + trailerLen];
            GCHandle gcEncBuffer = GCHandle.Alloc(encBuffer, GCHandleType.Pinned);

            try
            {
                IntPtr sizePtr = gcEncBuffer.AddrOfPinnedObject();
                Marshal.WriteInt32(sizePtr, encryptedLen);

                IntPtr encryptedPtr = IntPtrHelper.Add(sizePtr, 4);
                Marshal.Copy(encrypted, 0, encryptedPtr, encryptedLen);

                IntPtr trailerPtr = IntPtrHelper.Add(encryptedPtr, encryptedLen);
                Marshal.Copy(trailer, 0, trailerPtr, trailerLen);

                return(encBuffer);
            }
            finally
            {
                gcEncBuffer.Free();
            }
        }
        private void OverlappedCallback(object stateObject, bool Signaled)
        {
            BaseOverlappedAsyncResult result = (BaseOverlappedAsyncResult)stateObject;
            uint num  = (uint)Marshal.ReadInt32(IntPtrHelper.Add(result.m_UnmanagedBlob.DangerousGetHandle(), 0));
            uint num2 = (num != 0) ? uint.MaxValue : ((uint)Marshal.ReadInt32(IntPtrHelper.Add(result.m_UnmanagedBlob.DangerousGetHandle(), Win32.OverlappedInternalHighOffset)));

            result.ErrorCode = (int)num;
            object obj2 = result.PostCompletion((int)num2);

            result.ReleaseUnmanagedStructures();
            result.InvokeCallback(obj2);
        }
        /// <summary>
        /// Writes ACE to native buffer
        /// </summary>
        internal void UnsafeWrite(IntPtr pAce)
        {
            // write ACE properties
            MarshalHelper.WriteByte(pAce, typeof(SecurityNative.ACE), "AceType", (byte)_aceType);
            MarshalHelper.WriteByte(pAce, typeof(SecurityNative.ACE), "AceFlags", (byte)_aceFlags);
            MarshalHelper.WriteInt16(pAce, typeof(SecurityNative.ACE), "AceSize", (short)_size);
            MarshalHelper.WriteInt32(pAce, typeof(SecurityNative.ACE), "AccessMask", (int)_accessMask);

            // write SID
            IntPtr pSid = IntPtrHelper.Add(pAce, Marshal.OffsetOf(typeof(SecurityNative.ACE), "SidStart"));

            Win32.CopyMemory(pSid, _trustee.Handle, (uint)_trustee.Size);
        }
Пример #7
0
 internal PingReply(Icmp6EchoReply reply, IntPtr dataPtr, int sendSize)
 {
     this.address  = new IPAddress(reply.Address.Address, (long)reply.Address.ScopeID);
     this.ipStatus = (IPStatus)reply.Status;
     if (this.ipStatus == IPStatus.Success)
     {
         this.rtt    = reply.RoundTripTime;
         this.buffer = new byte[sendSize];
         Marshal.Copy(IntPtrHelper.Add(dataPtr, 0x24), this.buffer, 0, sendSize);
     }
     else
     {
         this.buffer = new byte[0];
     }
 }
Пример #8
0
        /// <summary>
        /// Initializes buffer with data
        /// </summary>
        public void SetBuffer(int index, int type, byte[] buffer)
        {
            // check object state
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            // parameters validation
            if ((index < 0) || (index >= BuffersCount))
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (buffer.Length == 0)
            {
                throw new ArgumentOutOfRangeException("buffer.Length");
            }

            // allocate buffer
            int    size       = buffer.Length;
            IntPtr bufferData = Win32.LocalAlloc(Win32.LMEM_ZEROINIT, (uint)size);

            if (bufferData == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            try
            {
                // copy buffer data
                Marshal.Copy(buffer, 0, bufferData, size);

                // create buffer descriptor
                IntPtr secBuffer = IntPtrHelper.Add(pvBuffers, index * SSPINative.SecBuffer.Size);
                MarshalHelper.WriteInt32(secBuffer, typeof(SSPINative.SecBuffer), "BufferSize", size);
                MarshalHelper.WriteInt32(secBuffer, typeof(SSPINative.SecBuffer), "BufferType", type);
                MarshalHelper.WriteIntPtr(secBuffer, typeof(SSPINative.SecBuffer), "pvBuffer", bufferData);
            }
            catch
            {
                Win32.LocalFree(bufferData);
                throw;
            }
        }
Пример #9
0
        internal void FreeBuffers()
        {
            IntPtr secBuffer = pvBuffers;

            for (int i = 0; i < BuffersCount; ++i)
            {
                IntPtr bufferData = MarshalHelper.ReadIntPtr(secBuffer, typeof(SSPINative.SecBuffer), "pvBuffer");
                if (bufferData != IntPtr.Zero)
                {
                    Win32.LocalFree(bufferData);
                }

                secBuffer = IntPtrHelper.Add(pvBuffers, SSPINative.SecBuffer.Size);
            }

            Win32.RtlZeroMemory(pvBuffers, (uint)(SSPINative.SecBuffer.Size * BuffersCount));
        }
Пример #10
0
        // the main constructor for the icmpsendecho apis
        internal PingReply(Icmp6EchoReply reply, IntPtr dataPtr, int sendSize)
        {
            address  = new IPAddress(reply.Address.Address, reply.Address.ScopeID);
            ipStatus = (IPStatus)reply.Status; //the icmpsendecho ip status codes

            //only copy the data if we succeed w/ the ping operation
            if (ipStatus == IPStatus.Success)
            {
                rtt    = (long)reply.RoundTripTime;
                buffer = new byte[sendSize];
                Marshal.Copy(IntPtrHelper.Add(dataPtr, 36), buffer, 0, sendSize);
                //options = new PingOptions (reply.options);
            }
            else
            {
                buffer = new byte[0];
            }
        }
Пример #11
0
        internal AccessControlEntry(IntPtr pAce)
        {
            // parameters validation
            if (pAce == IntPtr.Zero)
            {
                throw new ArgumentNullException("pAce");
            }

            // read ACE properties
            _aceType    = (AccessControlEntryType)MarshalHelper.ReadByte(pAce, typeof(SecurityNative.ACE), "AceType");
            _aceFlags   = (AccessControlEntryFlags)MarshalHelper.ReadByte(pAce, typeof(SecurityNative.ACE), "AceFlags");
            _accessMask = (AccessMask)MarshalHelper.ReadInt32(pAce, typeof(SecurityNative.ACE), "AccessMask");

            // read SID
            IntPtr pSid = IntPtrHelper.Add(pAce, Marshal.OffsetOf(typeof(SecurityNative.ACE), "SidStart"));

            Trustee = new SecurityIdentifier(pSid, true);
        }
Пример #12
0
        internal PingReply(Interop.IpHlpApi.Icmp6EchoReply reply, IntPtr dataPtr, int sendSize)
        {
            _address = new IPAddress(reply.Address.Address, reply.Address.ScopeID);
            // The icmpsendecho IP status codes.
            _ipStatus = (IPStatus)reply.Status;

            // Only copy the data if we succeed w/ the ping operation.
            if (_ipStatus == IPStatus.Success)
            {
                _rtt    = (long)reply.RoundTripTime;
                _buffer = new byte[sendSize];
                Marshal.Copy(IntPtrHelper.Add(dataPtr, 36), _buffer, 0, sendSize);
            }
            else
            {
                _buffer = Array.Empty <byte>();
            }
        }
        //
        // The overlapped function called (either by the thread pool or the socket)
        // when IO completes. (only called on Win9x)
        //
        internal void OverlappedCallback(object stateObject, bool Signaled)
        {
            OverlappedAsyncResult asyncResult = (OverlappedAsyncResult)stateObject;

            GlobalLog.Assert(!asyncResult.IsCompleted, "OverlappedAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::OverlappedCallback() asyncResult.IsCompleted", "");
            //
            // the IO completed asynchronously, see if there was a failure the Internal
            // field in the Overlapped structure will be non zero. to optimize the non
            // error case, we look at it without calling WSAGetOverlappedResult().
            //
            uint errorCode = (uint)Marshal.ReadInt32(IntPtrHelper.Add(asyncResult.m_UnmanagedBlob, Win32.OverlappedInternalOffset));
            uint numBytes  = errorCode != 0 ? unchecked ((uint)-1) : (uint)Marshal.ReadInt32(IntPtrHelper.Add(asyncResult.m_UnmanagedBlob, Win32.OverlappedInternalHighOffset));

            //
            // this will release the unmanaged pin handles and unmanaged overlapped ptr
            //
            asyncResult.ReleaseUnmanagedStructures();
            asyncResult.ErrorCode = (int)errorCode;
            asyncResult.InvokeCallback(false, (int)numBytes);
        }
Пример #14
0
        public void IntPtrAddTest()
        {
            int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
            unsafe
            {
                fixed(int *parr = arr)
                {
                    IntPtr ptr = new IntPtr(parr);
                    // Get the size of an array element.
                    int size  = sizeof(int);
                    int index = 0;

                    for (int ctr = 0; ctr < arr.Length; ctr++)
                    {
                        IntPtr newPtr = IntPtrHelper.Add(ptr, ctr * size);
                        Assert.AreEqual(arr[index], Marshal.ReadInt32(newPtr));
                        index++;
                    }
                }
            }
        }
Пример #15
0
        public void IntPtrSubtractTest()
        {
            int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
            unsafe
            {
                fixed(int *parr = arr)
                {
                    // Get the size of an array element.
                    const int size  = sizeof(int);
                    var       ptr   = IntPtrHelper.Add(new IntPtr(parr), size * (arr.Length - 1));
                    var       index = arr.Length - 1;

                    for (var ctr = 0; ctr < arr.Length; ctr++)
                    {
                        var newPtr = IntPtrHelper.Subtract(ptr, ctr * size);
                        Assert.AreEqual(arr[index], Marshal.ReadInt32(newPtr));
                        index--;
                    }
                }
            }
        }
Пример #16
0
        private static PingReply CreatePingReplyFromIcmp6EchoReply(Interop.IpHlpApi.Icmp6EchoReply reply, IntPtr dataPtr, int sendSize)
        {
            IPAddress address  = new IPAddress(reply.Address.Address, reply.Address.ScopeID);
            IPStatus  ipStatus = (IPStatus)reply.Status;            // The icmpsendecho IP status codes.

            long rtt;

            byte[] buffer;

            rtt = reply.RoundTripTime;
            if (ipStatus == IPStatus.Success)
            {
                // Only copy the data if we succeed w/ the ping operation.
                buffer = new byte[sendSize];
                Marshal.Copy(IntPtrHelper.Add(dataPtr, 36), buffer, 0, sendSize);
            }
            else
            {
                buffer = Array.Empty <byte>();
            }

            return(new PingReply(address, default(PingOptions), ipStatus, rtt, buffer));
        }
Пример #17
0
        public static int HackedGetOverlappedResult(IntPtr pOverlapped, out int totalBytes)
        {
            //
            // read IO asynchronous status from Overlapped structure
            //
            int status = Marshal.ReadInt32(IntPtrHelper.Add(pOverlapped, Overlapped.InternalLowOffset));

            if (status == 0)
            {
                //
                // the Async IO call completed
                //
                totalBytes = Marshal.ReadInt32(IntPtrHelper.Add(pOverlapped, Overlapped.InternalHighOffset));
            }
            else
            {
                //
                // the Async IO call failed or still pending
                //
                totalBytes = -1;
            }

            return(status);
        }