Implemented CERs for encryption and decryption.

This commit is contained in:
antiduh
2014-06-24 22:51:38 +00:00
parent bf1312cb81
commit 8837f3e95c
2 changed files with 69 additions and 18 deletions

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace NSspi namespace NSspi
{ {
@@ -89,7 +90,7 @@ namespace NSspi
SecureBuffer paddingBuffer; SecureBuffer paddingBuffer;
SecureBufferAdapter adapter; SecureBufferAdapter adapter;
SecurityStatus status; SecurityStatus status = SecurityStatus.InvalidHandle;
byte[] result; byte[] result;
trailerBuffer = new SecureBuffer( new byte[sizes.SecurityTrailer], BufferType.Token ); trailerBuffer = new SecureBuffer( new byte[sizes.SecurityTrailer], BufferType.Token );
@@ -100,13 +101,37 @@ namespace NSspi
using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) ) using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) )
{ {
// TODO SAFE_CER bool gotRef = false;
status = ContextNativeMethods.EncryptMessage(
ref this.ContextHandle.rawHandle, RuntimeHelpers.PrepareConstrainedRegions();
0, try
adapter.Handle, {
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 )
@@ -152,8 +177,8 @@ namespace NSspi
SecureBuffer dataBuffer; SecureBuffer dataBuffer;
SecureBuffer paddingBuffer; SecureBuffer paddingBuffer;
SecureBufferAdapter adapter; SecureBufferAdapter adapter;
SecurityStatus status; SecurityStatus status = SecurityStatus.InvalidHandle;
byte[] result = null; byte[] result = null;
int remaining; int remaining;
int position; int position;
@@ -217,13 +242,37 @@ namespace NSspi
using( adapter = new SecureBufferAdapter( new [] { trailerBuffer, dataBuffer, paddingBuffer } ) ) using( adapter = new SecureBufferAdapter( new [] { trailerBuffer, dataBuffer, paddingBuffer } ) )
{ {
// TODO SAFE_CER bool gotRef = false;
status = ContextNativeMethods.DecryptMessage(
ref this.ContextHandle.rawHandle, RuntimeHelpers.PrepareConstrainedRegions();
adapter.Handle, try
0, {
0 this.ContextHandle.DangerousAddRef( ref gotRef );
); }
catch( Exception )
{
if( gotRef )
{
this.ContextHandle.DangerousRelease();
gotRef = false;
}
throw;
}
finally
{
if( gotRef )
{
status = ContextNativeMethods.DecryptMessage(
ref this.ContextHandle.rawHandle,
adapter.Handle,
0,
0
);
this.ContextHandle.DangerousRelease();
}
}
} }
if( status != SecurityStatus.OK ) if( status != SecurityStatus.OK )

View File

@@ -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;
@@ -105,6 +106,7 @@ namespace NSspi
public static extern SecurityStatus DeleteSecurityContext( ref RawSspiHandle contextHandle ); public static extern SecurityStatus DeleteSecurityContext( ref RawSspiHandle contextHandle );
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail )]
[DllImport( "Secur32.dll", EntryPoint = "EncryptMessage", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "EncryptMessage", CharSet = CharSet.Unicode )]
public static extern SecurityStatus EncryptMessage( public static extern SecurityStatus EncryptMessage(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,
@@ -113,7 +115,7 @@ namespace NSspi
int sequenceNumber int sequenceNumber
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )]
[DllImport( "Secur32.dll", EntryPoint = "DecryptMessage", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "DecryptMessage", CharSet = CharSet.Unicode )]
public static extern SecurityStatus DecryptMessage( public static extern SecurityStatus DecryptMessage(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,