Create a similar SafeDecryptMessage that handles the CER crud.

Change the Safe*Message methods to order the parameters the same (even though the Win32 API doesn't..) and to take in the SecureBufferAdapter instead of its handle.
This commit is contained in:
antiduh
2014-06-25 01:08:29 +00:00
parent 2b52e1d84f
commit 26888a8b7b
2 changed files with 51 additions and 34 deletions

View File

@@ -101,37 +101,12 @@ namespace NSspi
using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) ) using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) )
{ {
bool gotRef = false; status = ContextNativeMethods.SafeEncryptMessage(
this.ContextHandle,
RuntimeHelpers.PrepareConstrainedRegions(); 0,
try adapter,
{ 0
this.ContextHandle.DangerousAddRef( ref gotRef ); );
}
catch( Exception )
{
if( gotRef )
{
this.ContextHandle.DangerousRelease();
gotRef = false;
}
throw;
}
finally
{
if( gotRef )
{
status = ContextNativeMethods.EncryptMessage(
ref this.ContextHandle.rawHandle,
0,
adapter.Handle,
0
);
this.ContextHandle.DangerousRelease();
}
}
} }
if( status != SecurityStatus.OK ) if( status != SecurityStatus.OK )
@@ -245,7 +220,7 @@ namespace NSspi
status = ContextNativeMethods.SafeDecryptMessage( status = ContextNativeMethods.SafeDecryptMessage(
this.ContextHandle, this.ContextHandle,
0, 0,
adapter.Handle, adapter,
0 0
); );
} }

View File

@@ -143,10 +143,52 @@ namespace NSspi
[DllImport( "Secur32.dll", EntryPoint = "FreeContextBuffer", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "FreeContextBuffer", CharSet = CharSet.Unicode )]
public static extern SecurityStatus FreeContextBuffer( IntPtr handle ); public static extern SecurityStatus FreeContextBuffer( IntPtr handle );
public static SecurityStatus SafeEncryptMessage(
SafeContextHandle handle,
int qualityOfProtection,
SecureBufferAdapter bufferAdapter,
int sequenceNumber )
{
SecurityStatus status = SecurityStatus.InternalError;
bool gotRef = false;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
handle.DangerousAddRef( ref gotRef );
}
catch ( Exception )
{
if ( gotRef )
{
handle.DangerousRelease();
gotRef = false;
}
throw;
}
finally
{
if ( gotRef )
{
status = ContextNativeMethods.EncryptMessage(
ref handle.rawHandle,
0,
bufferAdapter.Handle,
0
);
handle.DangerousRelease();
}
}
return status;
}
public static SecurityStatus SafeDecryptMessage( public static SecurityStatus SafeDecryptMessage(
SafeContextHandle handle, SafeContextHandle handle,
int qualityOfProtection, int qualityOfProtection,
IntPtr bufferDescriptor, SecureBufferAdapter bufferAdapter,
int sequenceNumber ) int sequenceNumber )
{ {
SecurityStatus status = SecurityStatus.InvalidHandle; SecurityStatus status = SecurityStatus.InvalidHandle;
@@ -173,7 +215,7 @@ namespace NSspi
{ {
status = ContextNativeMethods.DecryptMessage( status = ContextNativeMethods.DecryptMessage(
ref handle.rawHandle, ref handle.rawHandle,
bufferDescriptor, bufferAdapter.Handle,
0, 0,
0 0
); );