From 4224193f6f77173123ed6ece88bc45ecc2f62610 Mon Sep 17 00:00:00 2001 From: antiduh Date: Fri, 27 Jun 2014 01:47:43 +0000 Subject: [PATCH] CERs around the handle usage in MakeSignature and VerifySignature --- Contexts/Context.cs | 75 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/Contexts/Context.cs b/Contexts/Context.cs index 0822580..5d2a0ca 100644 --- a/Contexts/Context.cs +++ b/Contexts/Context.cs @@ -279,13 +279,37 @@ namespace NSspi.Contexts using ( adapter = new SecureBufferAdapter( new[] { dataBuffer, signatureBuffer } ) ) { - // TODO CER - status = ContextNativeMethods.MakeSignature( - ref this.ContextHandle.rawHandle, - 0, - adapter.Handle, - 0 - ); + 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(); + } + } } if ( status != SecurityStatus.OK ) @@ -360,12 +384,37 @@ namespace NSspi.Contexts using ( adapter = new SecureBufferAdapter( new[] { dataBuffer, signatureBuffer } ) ) { - status = ContextNativeMethods.VerifySignature( - ref this.ContextHandle.rawHandle, - adapter.Handle, - 0, - 0 - ); + 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(); + } + } } if ( status == SecurityStatus.OK )