Browse Source

GSS improvements, set context to null after a successfull call to DeleteSecurityContext

Tal Aloni 8 years atrás
parent
commit
d742a22756

+ 9 - 3
SMBLibrary/Authentication/GSSAPI/GSSProvider.cs

@@ -112,17 +112,23 @@ namespace SMBLibrary.Authentication.GSSAPI
             return mechanism.GetContextAttribute(context, attributeName);
         }
 
-        public void DeleteSecurityContext(ref object context)
+        public bool DeleteSecurityContext(ref object context)
         {
+            bool result = false;
             if (context != null)
             {
                 IGSSMechanism mechanism;
                 if (m_contextToMechanism.TryGetValue(context, out mechanism))
                 {
-                    mechanism.DeleteSecurityContext(ref context);
-                    m_contextToMechanism.Remove(context);
+                    object contextReference = context;
+                    result = mechanism.DeleteSecurityContext(ref context);
+                    if (result)
+                    {
+                        m_contextToMechanism.Remove(contextReference);
+                    }
                 }
             }
+            return result;
         }
 
         /// <summary>

+ 2 - 2
SMBLibrary/Authentication/GSSAPI/IGSSMechanism.cs

@@ -18,12 +18,12 @@ namespace SMBLibrary.Authentication.GSSAPI
 
         /// <summary>
         /// Equivalent to GSS_Delete_sec_context
-        /// Obtains information about a given security context (even an incomplete one)
         /// </summary>
-        void DeleteSecurityContext(ref object context);
+        bool DeleteSecurityContext(ref object context);
 
         /// <summary>
         /// Equivalent to GSS_Inquire_context
+        /// Obtains information about a given security context (even an incomplete one)
         /// </summary>
         object GetContextAttribute(object context, GSSAttributeName attributeName);
 

+ 3 - 1
SMBLibrary/Authentication/NTLM/IndependentNTLMAuthenticationProvider.cs

@@ -223,8 +223,10 @@ namespace SMBLibrary.Authentication.NTLM
             }
         }
 
-        public override void DeleteSecurityContext(ref object context)
+        public override bool DeleteSecurityContext(ref object context)
         {
+            context = null;
+            return true;
         }
 
         public override object GetContextAttribute(object context, GSSAttributeName attributeName)

+ 1 - 1
SMBLibrary/Authentication/NTLM/NTLMAuthenticationProviderBase.cs

@@ -46,7 +46,7 @@ namespace SMBLibrary.Authentication.NTLM
 
         public abstract NTStatus Authenticate(object context, AuthenticateMessage authenticateMessage);
 
-        public abstract void DeleteSecurityContext(ref object context);
+        public abstract bool DeleteSecurityContext(ref object context);
 
         public abstract object GetContextAttribute(object context, GSSAttributeName attributeName);
 

+ 9 - 3
SMBLibrary/Win32/IntegratedNTLMAuthenticationProvider.cs

@@ -125,16 +125,22 @@ namespace SMBLibrary.Win32.Security
             }
         }
 
-        public override void DeleteSecurityContext(ref object context)
+        public override bool DeleteSecurityContext(ref object context)
         {
             AuthContext authContext = context as AuthContext;
             if (authContext == null)
             {
-                return;
+                return false;
             }
 
             SecHandle handle = ((AuthContext)context).ServerContext;
-            SSPIHelper.DeleteSecurityContext(ref handle);
+            uint result = SSPIHelper.DeleteSecurityContext(ref handle);
+            bool success = (result == 0); // SEC_E_OK
+            if (success)
+            {
+                context = null;
+            }
+            return success;
         }
 
         public override object GetContextAttribute(object context, GSSAttributeName attributeName)