Changed how internal structures are created for handling SecureBuffers.
This commit is contained in:
@@ -21,54 +21,30 @@ namespace NSspi.Contexts
|
|||||||
ContextAttrib newContextAttribs = 0;
|
ContextAttrib newContextAttribs = 0;
|
||||||
|
|
||||||
SecurityStatus status;
|
SecurityStatus status;
|
||||||
SecureBufferDesc descriptor;
|
SecureBuffer tokenBuffer = new SecureBuffer( new byte[12288], BufferType.Token );
|
||||||
SecureBuffer secureBuffer;
|
SecureBufferAdapter list = new SecureBufferAdapter( tokenBuffer );
|
||||||
byte[] tokenBuffer = new byte[100000];
|
|
||||||
GCHandle tokenBufferHandle;
|
|
||||||
GCHandle bufferArrayHandle;
|
|
||||||
GCHandle descriptorHandle;
|
|
||||||
SecureBuffer[] bufferArray;
|
|
||||||
|
|
||||||
tokenBufferHandle = GCHandle.Alloc( tokenBuffer, GCHandleType.Pinned );
|
using ( list )
|
||||||
|
{
|
||||||
secureBuffer = new SecureBuffer();
|
status = NativeMethods.InitializeSecurityContext_Client1(
|
||||||
secureBuffer.Type = BufferType.Token;
|
ref credHandle,
|
||||||
secureBuffer.Count = tokenBuffer.Length;
|
IntPtr.Zero,
|
||||||
secureBuffer.Buffer = tokenBufferHandle.AddrOfPinnedObject();
|
serverPrinc,
|
||||||
|
attribs,
|
||||||
bufferArray = new SecureBuffer[1];
|
0,
|
||||||
bufferArray[0] = secureBuffer;
|
SecureBufferDataRep.Network,
|
||||||
bufferArrayHandle = GCHandle.Alloc( bufferArray, GCHandleType.Pinned );
|
IntPtr.Zero,
|
||||||
|
0,
|
||||||
|
ref newContextHandle,
|
||||||
|
list.Handle,
|
||||||
|
ref newContextAttribs,
|
||||||
|
ref expiry
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
descriptor = new SecureBufferDesc();
|
Console.Out.WriteLine( "Call status: " + status );
|
||||||
descriptor.Version = SecureBufferDesc.ApiVersion;
|
Console.Out.WriteLine( "Buffer length: " + tokenBuffer.Length );
|
||||||
descriptor.NumBuffers = bufferArray.Length;
|
Console.Out.WriteLine( "First bytes: " + tokenBuffer.Buffer[0] );
|
||||||
descriptor.Buffers = bufferArrayHandle.AddrOfPinnedObject();
|
|
||||||
|
|
||||||
descriptorHandle = GCHandle.Alloc( descriptor, GCHandleType.Pinned );
|
|
||||||
|
|
||||||
status = NativeMethods.InitializeSecurityContext_Client1(
|
|
||||||
ref credHandle,
|
|
||||||
IntPtr.Zero,
|
|
||||||
serverPrinc,
|
|
||||||
attribs,
|
|
||||||
0,
|
|
||||||
SecureBufferDataRep.Network,
|
|
||||||
IntPtr.Zero,
|
|
||||||
0,
|
|
||||||
ref newContextHandle,
|
|
||||||
descriptorHandle.AddrOfPinnedObject(),
|
|
||||||
ref newContextAttribs,
|
|
||||||
ref expiry
|
|
||||||
);
|
|
||||||
|
|
||||||
descriptorHandle.Free();
|
|
||||||
bufferArrayHandle.Free();
|
|
||||||
tokenBufferHandle.Free();
|
|
||||||
|
|
||||||
secureBuffer = bufferArray[0];
|
|
||||||
|
|
||||||
Console.Out.WriteLine( status );
|
|
||||||
base.ContextHandle = newContextHandle;
|
base.ContextHandle = newContextHandle;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,7 @@
|
|||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="SecureBuffer\SecureBuffer.cs" />
|
<Compile Include="SecureBuffer\SecureBuffer.cs" />
|
||||||
|
<Compile Include="SecureBuffer\SecureBufferAdapter.cs" />
|
||||||
<Compile Include="SecureBuffer\SecureBufferDataRep.cs" />
|
<Compile Include="SecureBuffer\SecureBufferDataRep.cs" />
|
||||||
<Compile Include="SecureBuffer\SecureBufferDesc.cs" />
|
<Compile Include="SecureBuffer\SecureBufferDesc.cs" />
|
||||||
<Compile Include="SecureBuffer\SecureBufferType.cs" />
|
<Compile Include="SecureBuffer\SecureBufferType.cs" />
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
|||||||
namespace NSspi.Contexts
|
namespace NSspi.Contexts
|
||||||
{
|
{
|
||||||
[StructLayout( LayoutKind.Sequential )]
|
[StructLayout( LayoutKind.Sequential )]
|
||||||
public unsafe struct SecureBuffer
|
public unsafe struct SecureBufferInternal
|
||||||
{
|
{
|
||||||
public int Count;
|
public int Count;
|
||||||
|
|
||||||
@@ -17,4 +17,20 @@ namespace NSspi.Contexts
|
|||||||
// A pointer to a byte[]
|
// A pointer to a byte[]
|
||||||
public IntPtr Buffer;
|
public IntPtr Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class SecureBuffer
|
||||||
|
{
|
||||||
|
public SecureBuffer( byte[] buffer, BufferType type )
|
||||||
|
{
|
||||||
|
this.Buffer = buffer;
|
||||||
|
this.Type = type;
|
||||||
|
this.Length = this.Buffer.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BufferType Type { get; set; }
|
||||||
|
|
||||||
|
public byte[] Buffer { get; set; }
|
||||||
|
|
||||||
|
public int Length { get; internal set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
108
SecureBuffer/SecureBufferAdapter.cs
Normal file
108
SecureBuffer/SecureBufferAdapter.cs
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace NSspi.Contexts
|
||||||
|
{
|
||||||
|
public class SecureBufferAdapter : IDisposable
|
||||||
|
{
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
private IList<SecureBuffer> buffers;
|
||||||
|
|
||||||
|
private GCHandle descriptorHandle;
|
||||||
|
|
||||||
|
private GCHandle[] bufferHandles;
|
||||||
|
|
||||||
|
private SecureBufferDescInternal descriptor;
|
||||||
|
private SecureBufferInternal[] bufferCarrier;
|
||||||
|
private GCHandle bufferCarrierHandle;
|
||||||
|
|
||||||
|
public SecureBufferAdapter( SecureBuffer buffer )
|
||||||
|
: this( new[] { buffer } )
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//[ReliabilityContract( Consistency.MayCorruptAppDomain, Cer.None)]
|
||||||
|
public SecureBufferAdapter( IList<SecureBuffer> buffers )
|
||||||
|
{
|
||||||
|
this.buffers = buffers;
|
||||||
|
|
||||||
|
this.disposed = false;
|
||||||
|
|
||||||
|
this.bufferHandles = new GCHandle[this.buffers.Count];
|
||||||
|
this.bufferCarrier = new SecureBufferInternal[this.buffers.Count];
|
||||||
|
|
||||||
|
for ( int i = 0; i < this.buffers.Count; i++ )
|
||||||
|
{
|
||||||
|
this.bufferHandles[i] = GCHandle.Alloc( this.buffers[i].Buffer, GCHandleType.Pinned );
|
||||||
|
|
||||||
|
this.bufferCarrier[i] = new SecureBufferInternal();
|
||||||
|
this.bufferCarrier[i].Type = this.buffers[i].Type;
|
||||||
|
this.bufferCarrier[i].Count = this.buffers[i].Buffer.Length;
|
||||||
|
this.bufferCarrier[i].Buffer = bufferHandles[i].AddrOfPinnedObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.bufferCarrierHandle = GCHandle.Alloc( bufferCarrier, GCHandleType.Pinned );
|
||||||
|
|
||||||
|
this.descriptor = new SecureBufferDescInternal();
|
||||||
|
this.descriptor.Version = SecureBufferDescInternal.ApiVersion;
|
||||||
|
this.descriptor.NumBuffers = this.buffers.Count;
|
||||||
|
this.descriptor.Buffers = bufferCarrierHandle.AddrOfPinnedObject();
|
||||||
|
|
||||||
|
this.descriptorHandle = GCHandle.Alloc( descriptor, GCHandleType.Pinned );
|
||||||
|
}
|
||||||
|
|
||||||
|
~SecureBufferAdapter()
|
||||||
|
{
|
||||||
|
Dispose( false );
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntPtr Handle
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if ( this.disposed )
|
||||||
|
{
|
||||||
|
throw new ObjectDisposedException( "Cannot use SecureBufferListHandle after it has been disposed" );
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.descriptorHandle.AddrOfPinnedObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
this.Dispose( true );
|
||||||
|
GC.SuppressFinalize( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose( bool disposing )
|
||||||
|
{
|
||||||
|
if ( this.disposed == true ) { return; }
|
||||||
|
|
||||||
|
if ( disposing )
|
||||||
|
{
|
||||||
|
for ( int i = 0; i < this.buffers.Count; i++ )
|
||||||
|
{
|
||||||
|
this.buffers[i].Length = this.bufferCarrier[i].Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( int i = 0; i < this.bufferHandles.Length; i++ )
|
||||||
|
{
|
||||||
|
this.bufferHandles[i].Free();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.bufferCarrierHandle.Free();
|
||||||
|
this.descriptorHandle.Free();
|
||||||
|
|
||||||
|
this.disposed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.ConstrainedExecution;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -8,7 +9,7 @@ using System.Threading.Tasks;
|
|||||||
namespace NSspi.Contexts
|
namespace NSspi.Contexts
|
||||||
{
|
{
|
||||||
[StructLayout( LayoutKind.Sequential)]
|
[StructLayout( LayoutKind.Sequential)]
|
||||||
public unsafe struct SecureBufferDesc
|
public struct SecureBufferDescInternal
|
||||||
{
|
{
|
||||||
public int Version;
|
public int Version;
|
||||||
public int NumBuffers;
|
public int NumBuffers;
|
||||||
@@ -18,6 +19,4 @@ namespace NSspi.Contexts
|
|||||||
|
|
||||||
public const int ApiVersion = 0;
|
public const int ApiVersion = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user