@@ -88,19 +88,15 @@ public SecBuffer(int bufferSize)
8888 }
8989
9090 public SecBuffer ( byte [ ] secBufferBytes )
91+ : this ( secBufferBytes . Length )
9192 {
92- cbBuffer = secBufferBytes . Length ;
93- bufferType = ( int ) SecBufferType . SECBUFFER_TOKEN ;
94- pvBuffer = Marshal . AllocHGlobal ( cbBuffer ) ;
9593 Marshal . Copy ( secBufferBytes , 0 , pvBuffer , cbBuffer ) ;
9694 }
9795
9896 public SecBuffer ( byte [ ] secBufferBytes , SecBufferType bufferType )
97+ : this ( secBufferBytes )
9998 {
100- cbBuffer = secBufferBytes . Length ;
10199 this . bufferType = ( int ) bufferType ;
102- pvBuffer = Marshal . AllocHGlobal ( cbBuffer ) ;
103- Marshal . Copy ( secBufferBytes , 0 , pvBuffer , cbBuffer ) ;
104100 }
105101
106102 public void Dispose ( )
@@ -129,7 +125,7 @@ private struct SecBufferDesc : IDisposable
129125 {
130126 public int ulVersion ;
131127 public int cBuffers ;
132- public IntPtr pBuffers ; //Point to SecBuffer
128+ public IntPtr pBuffers ;
133129
134130 public SecBufferDesc ( int bufferSize )
135131 {
@@ -272,9 +268,9 @@ out SecInteger ptsExpiry //PTimeStamp
272268
273269 #region Private members
274270
275- private SecHandle _clientCredentials = new SecHandle ( ) ;
276- private SecHandle _clientContext = new SecHandle ( ) ;
277- private bool _disposed = false ;
271+ private SecHandle _clientCredentials ;
272+ private SecHandle _clientContext ;
273+ private bool _disposed ;
278274
279275 private string _securPackage ;
280276 private string _remotePrincipal ;
@@ -309,10 +305,12 @@ public SspiHelper(string securityPackage, string remotePrincipal)
309305 {
310306 _securPackage = securityPackage ;
311307 _remotePrincipal = remotePrincipal ;
308+ _clientCredentials = new SecHandle ( ) ;
312309 SecInteger expiry = new SecInteger ( ) ;
313- if ( AcquireCredentialsHandle ( null , securityPackage , SECPKG_CRED_OUTBOUND ,
314- IntPtr . Zero , IntPtr . Zero , 0 , IntPtr . Zero ,
315- out _clientCredentials , out expiry ) != SEC_E_OK )
310+ int resCode = AcquireCredentialsHandle ( null , securityPackage , SECPKG_CRED_OUTBOUND ,
311+ IntPtr . Zero , IntPtr . Zero , 0 , IntPtr . Zero ,
312+ out _clientCredentials , out expiry ) ;
313+ if ( resCode != SEC_E_OK )
316314 throw new Exception ( $ "{ nameof ( AcquireCredentialsHandle ) } failed") ;
317315 }
318316
@@ -326,27 +324,27 @@ public SspiHelper(string securityPackage, string remotePrincipal)
326324 /// <returns>Client authentication data to be sent to server</returns>
327325 public byte [ ] InitializeClientSecurity ( )
328326 {
329- if ( _disposed )
330- throw new ObjectDisposedException ( nameof ( SspiHelper ) ) ;
327+ EnsureDisposed ( ) ;
331328 CloseClientContext ( ) ;
332- SecInteger expiry = new SecInteger ( 0 ) ;
329+ _clientContext = new SecHandle ( ) ;
330+ SecInteger expiry = new SecInteger ( ) ;
333331 uint contextAttributes ;
334332 SecBufferDesc clientTokenBuf = new SecBufferDesc ( MAX_TOKEN_SIZE ) ;
335333 try
336334 {
337335 int resCode = InitializeSecurityContext (
338336 ref _clientCredentials ,
339337 IntPtr . Zero ,
340- _remotePrincipal , // null string pszTargetName,
338+ _remotePrincipal ,
341339 STANDARD_CONTEXT_ATTRIBUTES ,
342- 0 , //int Reserved1,
343- SECURITY_NATIVE_DREP , //int TargetDataRep
344- IntPtr . Zero , //Always zero first time around...
345- 0 , //int Reserved2,
346- out _clientContext , //pHandle CtxtHandle = SecHandle
347- ref clientTokenBuf , //ref SecBufferDesc pOutput, //PSecBufferDesc
348- out contextAttributes , //ref int pfContextAttr,
349- out expiry ) ; //ref IntPtr ptsExpiry ); //PTimeStamp
340+ 0 ,
341+ SECURITY_NATIVE_DREP ,
342+ IntPtr . Zero ,
343+ 0 ,
344+ out _clientContext ,
345+ ref clientTokenBuf ,
346+ out contextAttributes ,
347+ out expiry ) ;
350348 if ( resCode != SEC_E_OK && resCode != SEC_I_CONTINUE_NEEDED )
351349 throw new Exception ( $ "{ nameof ( InitializeSecurityContext ) } failed") ;
352350 return clientTokenBuf . GetSecBufferBytes ( ) ;
@@ -366,8 +364,7 @@ public byte[] InitializeClientSecurity()
366364 /// <returns>Client authentication data to be sent to server</returns>
367365 public byte [ ] GetClientSecurity ( byte [ ] serverToken )
368366 {
369- if ( _disposed )
370- throw new ObjectDisposedException ( nameof ( SspiHelper ) ) ;
367+ EnsureDisposed ( ) ;
371368 if ( _clientContext . IsInvalid )
372369 throw new InvalidOperationException ( $ "{ nameof ( InitializeClientSecurity ) } not called") ;
373370 SecInteger expiry = new SecInteger ( ) ;
@@ -381,16 +378,16 @@ public byte[] GetClientSecurity(byte[] serverToken)
381378 int resCode = InitializeSecurityContext (
382379 ref _clientCredentials ,
383380 ref _clientContext ,
384- _remotePrincipal , // null string pszTargetName,
381+ _remotePrincipal ,
385382 STANDARD_CONTEXT_ATTRIBUTES ,
386- 0 , //int Reserved1,
387- SECURITY_NATIVE_DREP , //int TargetDataRep
388- ref serverTokenBuf , // server token must be ref because it is struct
389- 0 , //int Reserved2,
390- out _clientContext , //pHandle CtxtHandle = SecHandle
391- ref clientTokenBuf , //ref SecBufferDesc pOutput, //PSecBufferDesc
392- out contextAttributes , //ref int pfContextAttr,
393- out expiry ) ; //ref IntPtr ptsExpiry ); //PTimeStamp
383+ 0 ,
384+ SECURITY_NATIVE_DREP ,
385+ ref serverTokenBuf ,
386+ 0 ,
387+ out _clientContext ,
388+ ref clientTokenBuf ,
389+ out contextAttributes ,
390+ out expiry ) ;
394391 if ( resCode != SEC_E_OK && resCode != SEC_I_CONTINUE_NEEDED )
395392 throw new Exception ( $ "{ nameof ( InitializeSecurityContext ) } failed") ;
396393 return clientTokenBuf . GetSecBufferBytes ( ) ;
@@ -431,37 +428,30 @@ public void Dispose()
431428
432429 private void Dispose ( bool disposing )
433430 {
434- lock ( this )
431+ if ( ! _disposed )
435432 {
436- if ( ! _disposed )
437- {
438- CloseClientContext ( ) ;
439- CloseClientCredentials ( ) ;
440-
441- if ( disposing )
442- { }
443-
444- _disposed = true ;
445- }
433+ _disposed = true ;
434+ CloseClientContext ( ) ;
435+ CloseClientCredentials ( ) ;
446436 }
447437 }
448438
449439 private void CloseClientContext ( )
450440 {
451441 if ( ! _clientContext . IsInvalid )
452- {
453442 DeleteSecurityContext ( ref _clientContext ) ;
454- _clientContext = new SecHandle ( ) ;
455- }
456443 }
457444
458445 private void CloseClientCredentials ( )
459446 {
460447 if ( ! _clientCredentials . IsInvalid )
461- {
462448 FreeCredentialsHandle ( ref _clientCredentials ) ;
463- _clientCredentials = new SecHandle ( ) ;
464- }
449+ }
450+
451+ private void EnsureDisposed ( )
452+ {
453+ if ( _disposed )
454+ throw new ObjectDisposedException ( nameof ( SspiHelper ) ) ;
465455 }
466456
467457 #endregion
0 commit comments