diff --git a/Contexts/Context.cs b/Contexts/Context.cs index 5d2a0ca..37ebbf3 100644 --- a/Contexts/Context.cs +++ b/Contexts/Context.cs @@ -279,37 +279,12 @@ namespace NSspi.Contexts using ( adapter = new SecureBufferAdapter( new[] { dataBuffer, signatureBuffer } ) ) { - 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.MakeSignature( - ref this.ContextHandle.rawHandle, - 0, - adapter.Handle, - 0 - ); - - this.ContextHandle.DangerousRelease(); - } - } + status = ContextNativeMethods.SafeMakeSignature( + this.ContextHandle, + 0, + adapter, + 0 + ); } if ( status != SecurityStatus.OK ) @@ -384,37 +359,12 @@ namespace NSspi.Contexts using ( adapter = new SecureBufferAdapter( new[] { dataBuffer, signatureBuffer } ) ) { - 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.VerifySignature( - ref this.ContextHandle.rawHandle, - adapter.Handle, - 0, - 0 - ); - - this.ContextHandle.DangerousRelease(); - } - } + status = ContextNativeMethods.SafeVerifySignature( + this.ContextHandle, + 0, + adapter, + 0 + ); } if ( status == SecurityStatus.OK ) diff --git a/Contexts/ContextNativeMethods.cs b/Contexts/ContextNativeMethods.cs index 57ce8c2..462a07f 100644 --- a/Contexts/ContextNativeMethods.cs +++ b/Contexts/ContextNativeMethods.cs @@ -257,5 +257,89 @@ namespace NSspi.Contexts return status; } + + internal static SecurityStatus SafeMakeSignature( + SafeContextHandle handle, + int qualityOfProtection, + SecureBufferAdapter adapter, + int sequenceNumber ) + { + bool gotRef = false; + SecurityStatus status = SecurityStatus.InternalError; + + RuntimeHelpers.PrepareConstrainedRegions(); + try + { + handle.DangerousAddRef( ref gotRef ); + } + catch ( Exception ) + { + if ( gotRef ) + { + handle.DangerousRelease(); + gotRef = false; + } + + throw; + } + finally + { + if ( gotRef ) + { + status = ContextNativeMethods.MakeSignature( + ref handle.rawHandle, + 0, + adapter.Handle, + 0 + ); + + handle.DangerousRelease(); + } + } + + return status; + } + + internal static SecurityStatus SafeVerifySignature( + SafeContextHandle handle, + int qualityOfProtection, + SecureBufferAdapter adapter, + int sequenceNumber ) + { + bool gotRef = false; + SecurityStatus status = SecurityStatus.InternalError; + + RuntimeHelpers.PrepareConstrainedRegions(); + try + { + handle.DangerousAddRef( ref gotRef ); + } + catch ( Exception ) + { + if ( gotRef ) + { + handle.DangerousRelease(); + gotRef = false; + } + + throw; + } + finally + { + if ( gotRef ) + { + status = ContextNativeMethods.VerifySignature( + ref handle.rawHandle, + adapter.Handle, + 0, + 0 + ); + + handle.DangerousRelease(); + } + } + + return status; + } } }