소스 검색

SPNEGO: Minor code refactoring

Tal Aloni 7 년 전
부모
커밋
eb92e824ac

+ 1 - 1
SMBLibrary/Authentication/GSSAPI/GSSProvider.cs

@@ -48,7 +48,7 @@ namespace SMBLibrary.Authentication.GSSAPI
             {
                 token.MechanismTypeList.Add(mechanism.Identifier);
             }
-            return SimpleProtectedNegotiationToken.GetTokenBytes(token);
+            return token.GetBytes(true);
         }
 
         public virtual NTStatus AcceptSecurityContext(ref GSSContext context, byte[] inputToken, out byte[] outputToken)

+ 26 - 28
SMBLibrary/Authentication/GSSAPI/SPNEGO/SimpleProtectedNegotiationToken.cs

@@ -19,6 +19,32 @@ namespace SMBLibrary.Authentication.GSSAPI
 
         public abstract byte[] GetBytes();
 
+        /// <param name="includeHeader">Prepend the generic GSSAPI header. Required for negTokenInit, optional for negTokenResp.</param>
+        public byte[] GetBytes(bool includeHeader)
+        {
+            byte[] tokenBytes = this.GetBytes();
+            if (includeHeader)
+            {
+                int objectIdentifierFieldSize = DerEncodingHelper.GetLengthFieldSize(SPNEGOIdentifier.Length);
+                int tokenLength = 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length + tokenBytes.Length;
+                int tokenLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(tokenLength);
+                int headerLength = 1 + tokenLengthFieldSize + 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length;
+                byte[] buffer = new byte[headerLength + tokenBytes.Length];
+                int offset = 0;
+                ByteWriter.WriteByte(buffer, ref offset, ApplicationTag);
+                DerEncodingHelper.WriteLength(buffer, ref offset, tokenLength);
+                ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ObjectIdentifier);
+                DerEncodingHelper.WriteLength(buffer, ref offset, SPNEGOIdentifier.Length);
+                ByteWriter.WriteBytes(buffer, ref offset, SPNEGOIdentifier);
+                ByteWriter.WriteBytes(buffer, ref offset, tokenBytes);
+                return buffer;
+            }
+            else
+            {
+                return tokenBytes;
+            }
+        }
+
         /// <summary>
         /// https://tools.ietf.org/html/rfc2743
         /// </summary>
@@ -60,33 +86,5 @@ namespace SMBLibrary.Authentication.GSSAPI
             }
             return null;
         }
-
-        /// <summary>
-        /// Will append the generic GSSAPI header.
-        /// </summary>
-        public static byte[] GetTokenBytes(SimpleProtectedNegotiationToken token)
-        {
-            if (token is SimpleProtectedNegotiationTokenInit)
-            {
-                byte[] tokenBytes = token.GetBytes();
-                int objectIdentifierFieldSize = DerEncodingHelper.GetLengthFieldSize(SPNEGOIdentifier.Length);
-                int tokenLength = 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length + tokenBytes.Length;
-                int tokenLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(tokenLength);
-                int headerLength = 1 + tokenLengthFieldSize + 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length;
-                byte[] buffer = new byte[headerLength + tokenBytes.Length];
-                int offset = 0;
-                ByteWriter.WriteByte(buffer, ref offset, ApplicationTag);
-                DerEncodingHelper.WriteLength(buffer, ref offset, tokenLength);
-                ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ObjectIdentifier);
-                DerEncodingHelper.WriteLength(buffer, ref offset, SPNEGOIdentifier.Length);
-                ByteWriter.WriteBytes(buffer, ref offset, SPNEGOIdentifier);
-                ByteWriter.WriteBytes(buffer, ref offset, tokenBytes);
-                return buffer;
-            }
-            else
-            {
-                return token.GetBytes();
-            }
-        }
     }
 }

+ 1 - 1
SMBLibrary/Client/Helpers/NTLMAuthenticationHelper.cs

@@ -67,7 +67,7 @@ namespace SMBLibrary.Client
                 outputToken.MechanismTypeList = new List<byte[]>();
                 outputToken.MechanismTypeList.Add(GSSProvider.NTLMSSPIdentifier);
                 outputToken.MechanismToken = negotiateMessage.GetBytes();
-                return SimpleProtectedNegotiationToken.GetTokenBytes(outputToken);
+                return outputToken.GetBytes(true);
             }
             else
             {