diff --git a/Contexts/Context.cs b/Contexts/Context.cs index 0df256c..1741838 100644 --- a/Contexts/Context.cs +++ b/Contexts/Context.cs @@ -101,37 +101,12 @@ namespace NSspi using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) ) { - bool gotRef = false; - - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - 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(); - } - } + status = ContextNativeMethods.SafeEncryptMessage( + this.ContextHandle, + 0, + adapter, + 0 + ); } if( status != SecurityStatus.OK ) @@ -245,7 +220,7 @@ namespace NSspi status = ContextNativeMethods.SafeDecryptMessage( this.ContextHandle, 0, - adapter.Handle, + adapter, 0 ); } diff --git a/Contexts/ContextNativeMethods.cs b/Contexts/ContextNativeMethods.cs index 5ff0b1e..91f1225 100644 --- a/Contexts/ContextNativeMethods.cs +++ b/Contexts/ContextNativeMethods.cs @@ -143,10 +143,52 @@ namespace NSspi [DllImport( "Secur32.dll", EntryPoint = "FreeContextBuffer", CharSet = CharSet.Unicode )] 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( SafeContextHandle handle, int qualityOfProtection, - IntPtr bufferDescriptor, + SecureBufferAdapter bufferAdapter, int sequenceNumber ) { SecurityStatus status = SecurityStatus.InvalidHandle; @@ -173,7 +215,7 @@ namespace NSspi { status = ContextNativeMethods.DecryptMessage( ref handle.rawHandle, - bufferDescriptor, + bufferAdapter.Handle, 0, 0 );