I can now successfully call InitializeSecurityContext and get a status of ContinueNeeded.

This commit is contained in:
antiduh
2014-06-21 16:32:34 +00:00
parent cc0235262c
commit c64765fbdf
11 changed files with 210 additions and 36 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@@ -8,7 +9,7 @@ namespace NSspi.Contexts
{
public class ClientContext : Context
{
public ClientContext( ClientCredential cred, string serverPrinc, ContextReqAttrib attribs )
public ClientContext( ClientCredential cred, string serverPrinc, ContextAttrib attribs )
: base( cred )
{
long credHandle = base.Credential.CredentialHandle;
@@ -17,26 +18,58 @@ namespace NSspi.Contexts
long newContextHandle = 0;
long expiry = 0;
int newContextAttribs = 0;
ContextAttrib newContextAttribs = 0;
SecurityStatus status;
SecureBufferDesc descriptor;
SecureBuffer secureBuffer;
byte[] tokenBuffer = new byte[100000];
GCHandle tokenBufferHandle;
GCHandle bufferArrayHandle;
GCHandle descriptorHandle;
SecureBuffer[] bufferArray;
tokenBufferHandle = GCHandle.Alloc( tokenBuffer, GCHandleType.Pinned );
secureBuffer = new SecureBuffer();
secureBuffer.Type = BufferType.Token;
secureBuffer.Count = tokenBuffer.Length;
secureBuffer.Buffer = tokenBufferHandle.AddrOfPinnedObject();
bufferArray = new SecureBuffer[1];
bufferArray[0] = secureBuffer;
bufferArrayHandle = GCHandle.Alloc( bufferArray, GCHandleType.Pinned );
status = NativeMethods.InitializeSecurityContext_Client(
descriptor = new SecureBufferDesc();
descriptor.Version = SecureBufferDesc.ApiVersion;
descriptor.NumBuffers = bufferArray.Length;
descriptor.Buffers = bufferArrayHandle.AddrOfPinnedObject();
descriptorHandle = GCHandle.Alloc( descriptor, GCHandleType.Pinned );
status = NativeMethods.InitializeSecurityContext_Client1(
ref credHandle,
ref prevContextHandle,
IntPtr.Zero,
serverPrinc,
attribs,
0,
0,
0,
SecureBufferDataRep.Network,
IntPtr.Zero,
0,
ref newContextHandle,
IntPtr.Zero,
descriptorHandle.AddrOfPinnedObject(),
ref newContextAttribs,
ref expiry
);
descriptorHandle.Free();
bufferArrayHandle.Free();
tokenBufferHandle.Free();
secureBuffer = bufferArray[0];
Console.Out.WriteLine( status );
base.ContextHandle = newContextHandle;
}
}

View File

@@ -43,11 +43,6 @@ namespace NSspi.Contexts
/// </summary>
ReplayDetect = 0x00000004,
// The context must be allowed to detect out-of-order
// delivery of packets later through the message support
// functions. Use of this flag implies all of the
// conditions specified by the Integrity flag.
/// <summary>
/// Detect messages received out of sequence when using the message support functionality.
/// This flag implies all of the conditions specified by the Integrity flag - out-of-order sequence
@@ -126,6 +121,9 @@ namespace NSspi.Contexts
/// </summary>
AcceptIntegrity = 0x00020000,
InitIdentify = 0x00020000,
AcceptIdentify = 0x00080000,
/// <summary>
/// An Schannel provider connection is instructed to not authenticate the server automatically.
/// </summary>

20
Contexts/SecureBuffer.cs Normal file
View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace NSspi.Contexts
{
[StructLayout( LayoutKind.Sequential )]
public unsafe struct SecureBuffer
{
public int Count;
public BufferType Type;
// A pointer to a byte[]
public IntPtr Buffer;
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NSspi.Contexts
{
public enum SecureBufferDataRep : int
{
/*
#define SECURITY_NATIVE_DREP 0x00000010
#define SECURITY_NETWORK_DREP 0x00000000
*/
Nativee = 0x10,
Network = 0x00
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace NSspi.Contexts
{
[StructLayout( LayoutKind.Sequential)]
public unsafe struct SecureBufferDesc
{
public int Version;
public int NumBuffers;
// A pointer to a SecureBuffer[]
public IntPtr Buffers;
public const int ApiVersion = 0;
}
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NSspi.Contexts
{
public enum BufferType : int
{
Empty = 0x00,
Data = 0x01,
Token = 0x02,
Parameters = 0x03,
Missing = 0x04,
Extra = 0x05,
Trailer = 0x06,
Header = 0x07,
Padding = 0x09,
Stream = 0x0A,
ChannelBindings = 0x0E,
TargetHost = 0x10,
ReadOnlyFlag = unchecked( (int)0x80000000 ),
ReadOnlyWithChecksum = 0x10000000
}
}