コード例 #1
0
ファイル: WindowsAPI.cs プロジェクト: welly87/csharp-driver
        public SecBufferDesc(MultipleSecBufferHelper[] secBufferBytesArray)
        {
            if (secBufferBytesArray == null || secBufferBytesArray.Length == 0)
            {
                throw new ArgumentException("secBufferBytesArray cannot be null or 0 length");
            }

            ulVersion = (int)SecBufferType.SECBUFFER_VERSION;
            cBuffers  = secBufferBytesArray.Length;

            //Allocate memory for SecBuffer Array....
            pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SecBuffer)) * cBuffers);

            for (int Index = 0; Index < secBufferBytesArray.Length; Index++)
            {
                //Super hack: Now allocate memory for the individual SecBuffers
                //and just copy the bit values to the SecBuffer array!!!
                SecBuffer ThisSecBuffer = secBufferBytesArray[Index].Buffer == null ? new SecBuffer() :
                                          new SecBuffer(secBufferBytesArray[Index].Buffer, secBufferBytesArray[Index].BufferType);

                //We will write out bits in the following order:
                //int cbBuffer;
                //int BufferType;
                //pvBuffer;
                //Note that we won't be releasing the memory allocated by ThisSecBuffer until we
                //are disposed...
                int CurrentOffset = Index * Marshal.SizeOf(typeof(SecBuffer));
                Marshal.WriteInt32(pBuffers, CurrentOffset, ThisSecBuffer.cbBuffer);
                Marshal.WriteInt32(pBuffers, CurrentOffset + Marshal.SizeOf(ThisSecBuffer.cbBuffer), ThisSecBuffer.BufferType);
                Marshal.WriteIntPtr(pBuffers, CurrentOffset + Marshal.SizeOf(ThisSecBuffer.cbBuffer) + Marshal.SizeOf(ThisSecBuffer.BufferType), ThisSecBuffer.pvBuffer);
            }
        }
コード例 #2
0
ファイル: WindowsAPI.cs プロジェクト: welly87/csharp-driver
        public void Dispose()
        {
            if (pBuffers != IntPtr.Zero)
            {
                if (cBuffers == 1)
                {
                    SecBuffer ThisSecBuffer = (SecBuffer)Marshal.PtrToStructure(pBuffers, typeof(SecBuffer));
                    ThisSecBuffer.Dispose();
                }
                else
                {
                    for (int Index = 0; Index < cBuffers; Index++)
                    {
                        //The bits were written out the following order:
                        //int cbBuffer;
                        //int BufferType;
                        //pvBuffer;
                        //What we need to do here is to grab a hold of the pvBuffer allocate by the individual
                        //SecBuffer and release it...
                        int    CurrentOffset     = Index * Marshal.SizeOf(typeof(SecBuffer));
                        IntPtr SecBufferpvBuffer = Marshal.ReadIntPtr(pBuffers, CurrentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int)));
                        Marshal.FreeHGlobal(SecBufferpvBuffer);
                    }
                }

                Marshal.FreeHGlobal(pBuffers);
                pBuffers = IntPtr.Zero;
            }
        }
コード例 #3
0
        internal static ExportedSecurityContext ExportContext(SecHandle context, SecPkgContextExportFlags export_flags, string package, bool client)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            SecBuffer buffer = new SecBuffer()
            {
                BufferType = SecurityBufferType.Empty
            };

            try
            {
                SecurityNativeMethods.ExportSecurityContext(context, export_flags,
                                                            buffer, out SafeKernelObjectHandle token).CheckResult();
                return(new ExportedSecurityContext(package, buffer.ToArray(), !token.IsInvalid ? NtToken.FromHandle(token) : null, client));
            }
            finally
            {
                if (buffer.pvBuffer != IntPtr.Zero)
                {
                    SecurityNativeMethods.FreeContextBuffer(buffer.pvBuffer);
                }
            }
        }
コード例 #4
0
        public int HandleType2(byte[] ntlmBytes)
        {
            SecBuffer secBuffer = secServerBufferDesc.GetSecBuffer();

            byte[] newNtlmBytes = secBuffer.GetBytes();

            if (ntlmBytes.Length >= newNtlmBytes.Length)
            {
                for (int idx = 0; idx < ntlmBytes.Length; ++idx)
                {
                    if (idx < newNtlmBytes.Length)
                    {
                        ntlmBytes[idx] = newNtlmBytes[idx];
                    }
                    else
                    {
                        ntlmBytes[idx] = 0;
                    }
                }
            }
            else
            {
                Console.WriteLine("NTLM Type2 cannot be replaced.  New buffer too big");
            }

            return(0);
        }
コード例 #5
0
        private bool GenServerContext(AuthenticationToken token)
        {
            bool new_context = _new_context;

            _new_context = false;
            using (DisposableList list = new DisposableList()) {
                SecBuffer     out_sec_buffer  = list.AddResource(new SecBuffer(SecBufferType.Token, 64 * 1024));
                SecBufferDesc out_buffer_desc = list.AddResource(new SecBufferDesc(out_sec_buffer));

                List <SecBuffer> buffers = new List <SecBuffer>();
                buffers.Add(list.AddResource(new SecBuffer(SecBufferType.Token, token.ToArray())));
                if (_channel_binding != null)
                {
                    buffers.Add(list.AddResource(SecBuffer.CreateForChannelBinding(_channel_binding)));
                }
                SecBufferDesc in_buffer_desc = list.AddResource(new SecBufferDesc(buffers.ToArray()));

                LargeInteger  expiry = new LargeInteger();
                SecStatusCode result = SecurityNativeMethods.AcceptSecurityContext(_creds.CredHandle, new_context ? null : _context,
                                                                                   in_buffer_desc, _req_flags, _data_rep, _context, out_buffer_desc, out AcceptContextRetFlags context_attr, expiry).CheckResult();
                Flags  = context_attr;
                Expiry = expiry.QuadPart;
                if (result == SecStatusCode.CompleteNeeded || result == SecStatusCode.CompleteAndContinue)
                {
                    SecurityNativeMethods.CompleteAuthToken(_context, out_buffer_desc).CheckResult();
                }

                Token = AuthenticationToken.Parse(_creds.PackageName, _token_count++, false, out_buffer_desc.ToArray()[0].ToArray());
                return(!(result == SecStatusCode.ContinueNeeded || result == SecStatusCode.CompleteAndContinue));
            }
        }
 internal override void FromBuffer(SecBuffer buffer)
 {
     if (_type.HasFlagSet(SecurityBufferType.ReadOnly | SecurityBufferType.ReadOnlyWithChecksum))
     {
         return;
     }
     _type = buffer.BufferType;
 }
コード例 #7
0
ファイル: potato.cs プロジェクト: zu1kbackup/Canvas
 public void InitClientBuffer(ref SecBuffer buffer, ref SecBufferDesc buffer_desc)
 {
     buffer.BufferType       = (int)Secur32.SECBUFFER_TOKEN;
     buffer_desc.Version     = (int)Secur32.SECBUFFER_VERSION;
     buffer_desc.BufferCount = 1;
     buffer_desc.BuffersPtr  = Marshal.AllocHGlobal(Marshal.SizeOf(buffer));
     Marshal.StructureToPtr(buffer, buffer_desc.BuffersPtr, false);
 }
 internal override void FromBuffer(SecBuffer buffer)
 {
     if (Type.HasFlagSet(SecurityBufferType.ReadOnly | SecurityBufferType.ReadOnlyWithChecksum))
     {
         return;
     }
     _array = new ArraySegment <byte>(buffer.ToArray());
 }
コード例 #9
0
        public byte[] GetSecBufferByteArray()
        {
            byte[] Buffer = null;

            if (pBuffers == IntPtr.Zero)
            {
                throw new InvalidOperationException("Object has already been disposed!!!");
            }

            if (cBuffers == 1)
            {
                SecBuffer ThisSecBuffer = (SecBuffer)Marshal.PtrToStructure(pBuffers, typeof(SecBuffer));

                if (ThisSecBuffer.cbBuffer > 0)
                {
                    Buffer = new byte[ThisSecBuffer.cbBuffer];
                    Marshal.Copy(ThisSecBuffer.pvBuffer, Buffer, 0, ThisSecBuffer.cbBuffer);
                }
            }
            else
            {
                int BytesToAllocate = 0;

                for (int Index = 0; Index < cBuffers; Index++)
                {
                    //The bits were written out the following order:
                    //int cbBuffer;
                    //int BufferType;
                    //pvBuffer;
                    //What we need to do here calculate the total number of bytes we need to copy...
                    int CurrentOffset = Index * Marshal.SizeOf(typeof(SecBuffer));
                    BytesToAllocate += Marshal.ReadInt32(pBuffers, CurrentOffset);
                }

                Buffer = new byte[BytesToAllocate];

                for (int Index = 0, BufferIndex = 0; Index < cBuffers; Index++)
                {
                    //The bits were written out the following order:
                    //int cbBuffer;
                    //int BufferType;
                    //pvBuffer;
                    //Now iterate over the individual buffers and put them together into a
                    //byte array...
                    int CurrentOffset = Index * Marshal.SizeOf(typeof(SecBuffer));
                    int BytesToCopy   = Marshal.ReadInt32(pBuffers, CurrentOffset);
                    if (BytesToCopy == 0)
                    {
                        break;
                    }
                    IntPtr SecBufferpvBuffer = Marshal.ReadIntPtr(pBuffers, CurrentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int)));
                    Marshal.Copy(SecBufferpvBuffer, Buffer, BufferIndex, BytesToCopy);
                    BufferIndex += BytesToCopy;
                }
            }

            return(Buffer);
        }
コード例 #10
0
            /// <summary>
            /// Creates a SecBufferDesc struct with a single empty SecBuffer.
            /// </summary>
            /// <param name="bufferSize">Size of the SecBuffer</param>
            public SecBufferDesc(int bufferSize)
            {
                ulVersion = SECBUFFER_VERSION;
                cBuffers  = 1;
                SecBuffer ThisSecBuffer = new SecBuffer(bufferSize);

                pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(ThisSecBuffer));
                Marshal.StructureToPtr(ThisSecBuffer, pBuffers, false);
            }
コード例 #11
0
 internal static ExportedSecurityContext ExportContext(SecHandle context, SecPkgContextExportFlags export_flags, string package)
 {
     using (SecBuffer buffer = new SecBuffer(SecurityBufferType.Empty, 64 * 1024))
     {
         SecurityNativeMethods.ExportSecurityContext(context, SecPkgContextExportFlags.None,
                                                     buffer, out SafeKernelObjectHandle token).CheckResult();
         return(new ExportedSecurityContext(package, buffer.ToArray(), NtToken.FromHandle(token)));
     }
 }
コード例 #12
0
            public SecBufferDesc(byte[] secBufferBytes)
            {
                ulVersion = (int)SecBufferType.SECBUFFER_VERSION;
                cBuffers  = 1;
                SecBuffer secBuffer = new SecBuffer(secBufferBytes);

                pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(secBuffer));
                Marshal.StructureToPtr(secBuffer, pBuffers, false);
            }
コード例 #13
0
            public byte[] GetSecBufferBytes()
            {
                if (pBuffers == IntPtr.Zero)
                {
                    throw new ObjectDisposedException("SecBufferDesc");
                }
                SecBuffer secBuffer = (SecBuffer)Marshal.PtrToStructure(pBuffers, typeof(SecBuffer));

                return(secBuffer.GetBytes());
            }
コード例 #14
0
 public void Dispose()
 {
     if (pBuffers != IntPtr.Zero)
     {
         SecBuffer secBuffer = Marshal2.PtrToStructure <SecBuffer>(pBuffers);
         secBuffer.Dispose();
         Marshal.FreeHGlobal(pBuffers);
         pBuffers = IntPtr.Zero;
     }
 }
コード例 #15
0
ファイル: NativeMethods.cs プロジェクト: topotam/Kerberos.NET
            private SecBufferDesc(SecBuffer secBuffer)
            {
                ulVersion = SecBufferType.SECBUFFER_VERSION;

                cBuffers = 1;

                pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(secBuffer));

                Marshal.StructureToPtr(secBuffer, pBuffers, false);
            }
コード例 #16
0
            public byte[] GetSecBufferBytes()
            {
                if (pBuffers == IntPtr.Zero)
                {
                    throw new ObjectDisposedException(nameof(SecBufferDesc));
                }
                SecBuffer secBuffer = Marshal2.PtrToStructure <SecBuffer>(pBuffers);

                return(secBuffer.GetBytes());
            }
コード例 #17
0
 public void Dispose()
 {
     if (pBuffers != IntPtr.Zero)
     {
         SecBuffer secBuffer = (SecBuffer)Marshal.PtrToStructure(pBuffers, typeof(SecBuffer));
         secBuffer.Dispose();
         Marshal.FreeHGlobal(pBuffers);
         pBuffers = IntPtr.Zero;
     }
 }
コード例 #18
0
        internal override SecBuffer ToBuffer(DisposableList list)
        {
            SEC_CHANNEL_BINDINGS sec_channel_bind = new SEC_CHANNEL_BINDINGS();

            sec_channel_bind.cbApplicationDataLength = _channel_binding_token.Length;
            sec_channel_bind.dwApplicationDataOffset = Marshal.SizeOf(typeof(SEC_CHANNEL_BINDINGS));
            using (var binding = new SafeStructureInOutBuffer <SEC_CHANNEL_BINDINGS>(sec_channel_bind, _channel_binding_token.Length, true))
            {
                binding.Data.WriteBytes(_channel_binding_token);
                return(SecBuffer.Create(SecurityBufferType.ChannelBindings | SecurityBufferType.ReadOnly, binding.ToArray(), list));
            }
        }
コード例 #19
0
 public void ExImportSecurityContextTest()
 {
     using (var hCred = AcqCredHandle())
         using (var hCtx = GetSecContext(hCred, out _))
         {
             var secBuf = new SecBuffer(SecBufferType.SECBUFFER_EMPTY);
             Assert.That(ExportSecurityContext(hCtx, SECPKG_CONTEXT_EXPORT.SECPKG_CONTEXT_EXPORT_RESET_NEW, ref secBuf, out var hTok), Is.EqualTo((HRESULT)HRESULT.SEC_E_UNSUPPORTED_FUNCTION));
             Assert.That(secBuf.pvBuffer, Is.EqualTo(IntPtr.Zero));
             var hBuf = new SafeContextBuffer(secBuf.pvBuffer);
             Assert.That(ImportSecurityContext(MICROSOFT_KERBEROS_NAME, ref secBuf, hTok, out var hNewCtx), Is.EqualTo((HRESULT)HRESULT.SEC_E_INVALID_TOKEN));
         }
 }
コード例 #20
0
 internal override void FromBuffer(SecBuffer buffer)
 {
     if (buffer.pvBuffer == IntPtr.Zero)
     {
         _array = new byte[0];
         return;
     }
     _array = new byte[buffer.cbBuffer];
     Marshal.Copy(buffer.pvBuffer, _array, 0, _array.Length);
     SecurityNativeMethods.FreeContextBuffer(buffer.pvBuffer);
     buffer.pvBuffer = IntPtr.Zero;
     _type           = buffer.BufferType;
 }
コード例 #21
0
            /// <summary>
            /// Marshals all the SecBuffers into a byte array.
            /// </summary>
            /// <returns>Byte array representation of all the SecBuffers contained in this SecBufferDesc</returns>
            public byte[] GetSecBufferByteArray()
            {
                byte[] buffer = null;

                if (pBuffers == IntPtr.Zero)
                {
                    throw new ObjectDisposedException("pBuffers");
                }

                if (cBuffers == 1)
                {
                    SecBuffer ThisSecBuffer = (SecBuffer)Marshal.PtrToStructure(pBuffers, typeof(SecBuffer));

                    if (ThisSecBuffer.cbBuffer > 0)
                    {
                        buffer = new byte[ThisSecBuffer.cbBuffer];
                        Marshal.Copy(ThisSecBuffer.pvBuffer, buffer, 0, ThisSecBuffer.cbBuffer);
                    }
                }
                else
                {
                    int bytesToAllocate = 0;

                    // The bits were written out the following order:
                    //  int cbBuffer;
                    //  int BufferType;
                    //  pvBuffer;

                    // calculate the total size of the buffer
                    for (int Index = 0; Index < cBuffers; Index++)
                    {
                        int currentOffset = Index * Marshal.SizeOf(typeof(SecBuffer));
                        bytesToAllocate += Marshal.ReadInt32(pBuffers, currentOffset);
                    }

                    buffer = new byte[bytesToAllocate];

                    // copy to the destination buffer
                    for (int Index = 0, bufferIndex = 0; Index < cBuffers; Index++)
                    {
                        int    currentOffset     = Index * Marshal.SizeOf(typeof(SecBuffer));
                        int    bytesToCopy       = Marshal.ReadInt32(pBuffers, currentOffset);
                        IntPtr secBufferpvBuffer = Marshal.ReadIntPtr(pBuffers, currentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int)));
                        Marshal.Copy(secBufferpvBuffer, buffer, bufferIndex, bytesToCopy);
                        bufferIndex += bytesToCopy;
                    }
                }

                return(buffer);
            }
        private bool GenClientContext(AuthenticationToken token)
        {
            using (DisposableList list = new DisposableList())
            {
                SecStatusCode result = 0;

                SecBuffer     out_sec_buffer  = list.AddResource(new SecBuffer(SecBufferType.Token, 64 * 1024));
                SecBufferDesc out_buffer_desc = list.AddResource(new SecBufferDesc(out_sec_buffer));

                InitializeContextRetFlags flags;
                LargeInteger expiry = new LargeInteger();
                if (token != null)
                {
                    List <SecBuffer> buffers = new List <SecBuffer>();
                    buffers.Add(list.AddResource(new SecBuffer(SecBufferType.Token, token.ToArray())));
                    if (_channel_binding != null)
                    {
                        buffers.Add(list.AddResource(SecBuffer.CreateForChannelBinding(_channel_binding)));
                    }
                    SecBufferDesc in_buffer_desc = list.AddResource(new SecBufferDesc(buffers.ToArray()));
                    result = SecurityNativeMethods.InitializeSecurityContext(_creds.CredHandle, _context, _target, _req_attributes, 0,
                                                                             _data_rep, in_buffer_desc, 0, _context, out_buffer_desc, out flags, expiry).CheckResult();
                    Flags = flags;
                }
                else
                {
                    SecBufferDesc    in_buffer_desc = null;
                    List <SecBuffer> buffers        = new List <SecBuffer>();
                    if (_channel_binding != null)
                    {
                        buffers.Add(list.AddResource(SecBuffer.CreateForChannelBinding(_channel_binding)));
                        in_buffer_desc = list.AddResource(new SecBufferDesc(buffers.ToArray()));
                    }

                    result = SecurityNativeMethods.InitializeSecurityContext(_creds.CredHandle, null, _target,
                                                                             _req_attributes, 0, _data_rep, in_buffer_desc, 0, _context,
                                                                             out_buffer_desc, out flags, expiry).CheckResult();
                }

                Expiry = expiry.QuadPart;
                Flags  = flags;
                if (result == SecStatusCode.CompleteNeeded || result == SecStatusCode.CompleteAndContinue)
                {
                    SecurityNativeMethods.CompleteAuthToken(_context, out_buffer_desc).CheckResult();
                }

                Token = AuthenticationToken.Parse(_creds.PackageName, _token_count++, true, out_buffer_desc.ToArray()[0].ToArray());
                return(!(result == SecStatusCode.ContinueNeeded || result == SecStatusCode.CompleteAndContinue));
            }
        }
コード例 #23
0
        public IntPtr pBuffers; //Point to SecBuffer

        public SecBufferDesc(int bufferSize)
        {
            ulVersion = (int)SecBufferType.SECBUFFER_VERSION;
            if (bufferSize < 0)
            {
                cBuffers = 0;
                pBuffers = IntPtr.Zero;
            }
            else
            {
                cBuffers = 1;
                SecBuffer ThisSecBuffer = new SecBuffer(bufferSize);
                pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(ThisSecBuffer));
                Marshal.StructureToPtr(ThisSecBuffer, pBuffers, false);
            }
        }
コード例 #24
0
ファイル: NativeMethods.cs プロジェクト: topotam/Kerberos.NET
            private void ForEachBuffer(Action <SecBuffer> onBuffer)
            {
                for (int Index = 0; Index < cBuffers; Index++)
                {
                    int CurrentOffset = Index * Marshal.SizeOf(typeof(SecBuffer));

                    SecBuffer thisSecBuffer = (SecBuffer)Marshal.PtrToStructure(
                        IntPtr.Add(
                            pBuffers,
                            CurrentOffset
                            ),
                        typeof(SecBuffer)
                        );

                    onBuffer(thisSecBuffer);
                }
            }
コード例 #25
0
ファイル: potato.cs プロジェクト: zu1kbackup/Canvas
    public NTLMExecutor()
    {
        client_buffer = new SecBuffer();
        client_desc   = new SecBufferDesc();

        server_buffer = new SecBuffer();
        server_desc   = new SecBufferDesc();

        cred_handle   = new SECURITY_HANDLE(0);
        ctxt_handle   = new SECURITY_HANDLE(0);
        p_cred_handle = Marshal.AllocHGlobal(Marshal.SizeOf(cred_handle));
        Marshal.StructureToPtr(cred_handle, p_cred_handle, false);
        p_ctxt_handle = Marshal.AllocHGlobal(Marshal.SizeOf(ctxt_handle));
        Marshal.StructureToPtr(ctxt_handle, p_ctxt_handle, false);

        p_elevated_handle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
    }
コード例 #26
0
            /// <summary>
            /// Dispose the SecBufferDesc and all the SecBuffers it contains.
            /// </summary>
            public void Dispose()
            {
                if (pBuffers != IntPtr.Zero)
                {
                    if (cBuffers == 1)
                    {
                        SecBuffer ThisSecBuffer = (SecBuffer)Marshal.PtrToStructure(pBuffers, typeof(SecBuffer));
                        ThisSecBuffer.Dispose();
                    }
                    else
                    {
                        for (int Index = 0; Index < cBuffers; Index++)
                        {
                            int    currentOffset     = Index * Marshal.SizeOf(typeof(SecBuffer));
                            IntPtr secBufferpvBuffer = Marshal.ReadIntPtr(pBuffers, currentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int)));
                            Marshal.FreeHGlobal(secBufferpvBuffer);
                        }
                    }

                    Marshal.FreeHGlobal(pBuffers);
                    pBuffers = IntPtr.Zero;
                }
            }
コード例 #27
0
ファイル: Secur32.cs プロジェクト: Napadelice/waffle
 /// <summary>
 /// Constructs a SecBufferDesc with a single SecBuffer initialized from the
 /// byte array secBufferBytes.
 /// </summary>
 /// <param name="secBufferBytes">The SecBuffer to add to this SecBufferDesc</param>
 public SecBufferDesc(byte[] secBufferBytes)
 {
     ulVersion = SECBUFFER_VERSION;
     cBuffers = 1;
     SecBuffer ThisSecBuffer = new SecBuffer(secBufferBytes);
     pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(ThisSecBuffer));
     Marshal.StructureToPtr(ThisSecBuffer, pBuffers, false);
 }
コード例 #28
0
 internal abstract void FromBuffer(SecBuffer buffer);
コード例 #29
0
ファイル: WindowsAPI.cs プロジェクト: rasmus-s/csharp-driver
        public IntPtr pBuffers; //Point to SecBuffer

        #region Constructors

        public SecBufferDesc(int bufferSize)
        {
            ulVersion = (int)SecBufferType.SECBUFFER_VERSION;
            if (bufferSize < 0)
            {
                cBuffers = 0;
                pBuffers = IntPtr.Zero;
            }
            else
            {
                cBuffers = 1;
                SecBuffer ThisSecBuffer = new SecBuffer(bufferSize);
                pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(ThisSecBuffer));
                Marshal.StructureToPtr(ThisSecBuffer, pBuffers, false);
            }
        }
コード例 #30
0
			public IntPtr pBuffers; //Point to SecBuffer

			public SecBufferDesc(int bufferSize)
			{
				ulVersion = (int)SecBufferType.SECBUFFER_VERSION;
				cBuffers = 1;
				SecBuffer secBuffer = new SecBuffer(bufferSize);
				pBuffers = Marshal.AllocHGlobal(Marshal.SizeOf(secBuffer));
				Marshal.StructureToPtr(secBuffer, pBuffers, false);
			}
コード例 #31
0
ファイル: SSPIHandler.cs プロジェクト: ArsenShnurkov/npgsql
        internal byte[] Continue([CanBeNull] byte[] authData)
        {
            if (authData == null && _isSSPICtxSet)
                throw new InvalidOperationException("The authData parameter con only be null at the first call to continue!");

            var outBuffer = new SecBuffer
            {
                pvBuffer = IntPtr.Zero,
                BufferType = SecbufferToken,
                cbBuffer = 0
            };

            var outbuf = new SecBufferDesc
            {
                cBuffers = 1,
                ulVersion = SecbufferVersion,
                pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(outBuffer))
            };

            try
            {
                int status;
                SecHandle newContext;
                SecHandle expire;
                int contextAttr;

                Marshal.StructureToPtr(outBuffer, outbuf.pBuffer, false);

                if (_isSSPICtxSet)
                {
                    Contract.Assert(authData != null);
                    var inbuf = new SecBufferDesc { pBuffer = IntPtr.Zero };
                    var inBuffer = new SecBuffer { pvBuffer = Marshal.AllocHGlobal(authData.Length) };
                    try
                    {
                        Marshal.Copy(authData, 0, inBuffer.pvBuffer, authData.Length);
                        inBuffer.cbBuffer = authData.Length;
                        inBuffer.BufferType = SecbufferToken;
                        inbuf.ulVersion = SecbufferVersion;
                        inbuf.cBuffers = 1;
                        inbuf.pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(inBuffer));
                        Marshal.StructureToPtr(inBuffer, inbuf.pBuffer, false);
                        status = InitializeSecurityContext(
                            ref _sspiCred,
                            ref _sspiCtx,
                            _sspiTarget,
                            IscReqAllocateMemory,
                            0,
                            SecurityNetworkDrep,
                            ref inbuf,
                            0,
                            out newContext,
                            out outbuf,
                            out contextAttr,
                            out expire
                        );
                    }
                    finally
                    {
                        if (inBuffer.pvBuffer != IntPtr.Zero)
                            Marshal.FreeHGlobal(inBuffer.pvBuffer);
                        if (inbuf.pBuffer != IntPtr.Zero)
                            Marshal.FreeHGlobal(inbuf.pBuffer);
                    }
                }
                else
                {
                    status = InitializeSecurityContext(
                        ref _sspiCred,
                        IntPtr.Zero,
                        _sspiTarget,
                        IscReqAllocateMemory,
                        0,
                        SecurityNetworkDrep,
                        IntPtr.Zero,
                        0,
                        out newContext,
                        out outbuf,
                        out contextAttr,
                        out expire
                    );
                }

                if (status != SecEOk && status != SecIContinueNeeded)
                {
                    // This will automaticcaly fill in the message of the last Win32 error
                    throw new Win32Exception();
                }
                if (!_isSSPICtxSet)
                {
                    _sspiCtx.dwUpper = newContext.dwUpper;
                    _sspiCtx.dwLower = newContext.dwLower;
                    _isSSPICtxSet = true;
                }

                if (outbuf.cBuffers > 0)
                {
                    if (outbuf.cBuffers != 1)
                        throw new InvalidOperationException("SSPI returned invalid number of output buffers");
                    // attention: OutBuffer is still our initially created struct but outbuf.pBuffer doesn't point to
                    // it but to the copy of it we created on the unmanaged heap and passed to InitializeSecurityContext()
                    // we have to marshal it back to see the content change
#if NET45
                    outBuffer = (SecBuffer)Marshal.PtrToStructure(outbuf.pBuffer, typeof(SecBuffer));
#else
                    outBuffer = Marshal.PtrToStructure<SecBuffer>(outbuf.pBuffer);
#endif
                    if (outBuffer.cbBuffer > 0)
                    {
                        // we need the buffer with a terminating 0 so we
                        // make it one byte bigger
                        var buffer = new byte[outBuffer.cbBuffer];
                        Marshal.Copy(outBuffer.pvBuffer, buffer, 0, buffer.Length);
                        // The SSPI authentication data must be sent as password message

                        return buffer;
                        //stream.WriteByte((byte)'p');
                        //PGUtil.WriteInt32(stream, buffer.Length + 5);
                        //stream.Write(buffer, 0, buffer.Length);
                        //stream.Flush();
                    }
                }
                return PGUtil.EmptyBuffer;
            }
            finally
            {
                if (outBuffer.pvBuffer != IntPtr.Zero)
                    FreeContextBuffer(outBuffer.pvBuffer);
                if (outbuf.pBuffer != IntPtr.Zero)
                    Marshal.FreeHGlobal(outbuf.pBuffer);
            }
        }
 internal override void FromBuffer(SecBuffer buffer)
 {
 }
コード例 #33
0
 internal override SecBuffer ToBuffer(DisposableList list)
 {
     return(SecBuffer.Create(_type, _size, list));
 }
コード例 #34
0
 internal override void FromBuffer(SecBuffer buffer)
 {
     _array = buffer.ToArray();
     _size  = _array.Length;
     _type  = buffer.BufferType;
 }