/// <summary> /// Prevents a default instance of the <see cref="NtlmGenerator"/> abstract base class. /// </summary> /// <param name="message">The message body to parse as an array of bytes.</param> /// <param name="expectedType">The type of the message being generated.</param> protected NtlmGenerator(byte[] message, NtlmMessageType expectedType) { messageContents = message; // Look for NTLM message if (messageContents.Length < MessageSignature.Length) { throw new NtlmAuthorizationGenerationException("NTLM message decoding error - packet too short"); } int i = 0; while (i < MessageSignature.Length) { if (messageContents[i] != MessageSignature[i]) { throw new NtlmAuthorizationGenerationException("NTLM message expected - instead got unrecognized bytes"); } i++; } // Check to be sure the correct type is being used. NtlmMessageType type = (NtlmMessageType)ReadUInt(MessageSignature.Length); if (type != expectedType) { string errorMessage = string.Format("NTLM type {0} message expected - instead got type {1}", expectedType.ToString(), type.ToString()); throw new NtlmAuthorizationGenerationException(errorMessage); } currentOutputPosition = messageContents.Length; }
/// <summary> /// Initializes the response to contain the message data. /// </summary> /// <param name="maxlength">The maximum length of the message data.</param> /// <param name="messageType">The <see cref="NtlmMessageType"/> of the message.</param> protected void InitializeMessage(int maxlength, NtlmMessageType messageType) { messageContents = new byte[maxlength]; currentOutputPosition = 0; AddBytes(MessageSignature); AddUInt(Convert.ToUInt32(messageType)); }
private protected NtlmAuthenticationToken( byte[] data, NtlmMessageType message_type, NtlmNegotiateFlags flags) : base(data) { MessageType = message_type; Flags = flags; }
/// <summary> /// Try and parse data into an NTLM authentication token. /// </summary> /// <param name="data">The data to parse.</param> /// <param name="token">The NTLM authentication token.</param> /// <param name="client">True if this is a token from a client.</param> /// <param name="token_count">The token count number.</param> /// <returns>True if parsed successfully.</returns> internal static bool TryParse(byte[] data, int token_count, bool client, out NtlmAuthenticationToken token) { token = null; if (data.Length < 12) { return(false); } if (BinaryEncoding.Instance.GetString(data, 0, 8) != NTLM_MAGIC) { return(false); } MemoryStream stm = new MemoryStream(data); BinaryReader reader = new BinaryReader(stm); stm.Position = 8; NtlmMessageType type = (NtlmMessageType)reader.ReadInt32(); switch (type) { case NtlmMessageType.Negotiate: return(NtlmNegotiateAuthenticationToken.TryParse(data, reader, out token)); case NtlmMessageType.Challenge: return(NtlmChallengeAuthenticationToken.TryParse(data, reader, out token)); case NtlmMessageType.Authenticate: return(NtlmAuthenticateAuthenticationToken.TryParse(data, reader, out token)); default: return(false); } }
private static bool IsNtlmMessage(HeyHttpRequest request, NtlmMessageType expectedMessageType) { if (!IsNtlmAuthentication(request)) { return(false); } string message = request.Authorization.Substring("NTLM ".Length); byte[] buffer = Convert.FromBase64String(message); // Validate signature. string signature = Encoding.ASCII.GetString(buffer, 0, 8); if (signature != "NTLMSSP\0") { return(false); } // Is it negotiation message? uint messageType = BitConverter.ToUInt32(buffer, 8); if (messageType != (uint)expectedMessageType) { return(false); } if (expectedMessageType == NtlmMessageType.NegotiatieMessage) { // Domain name. bool hasDomainName = (buffer[14] & 0x10) == 1; uint domainNameLength = BitConverter.ToUInt16(buffer, 16); uint domainNameMaxLength = BitConverter.ToUInt16(buffer, 18); uint domainNameOffset = BitConverter.ToUInt32(buffer, 20); } return(true); }
/// <summary> /// Prevents a default instance of the <see cref="NtlmGenerator"/> abstract base class. /// </summary> /// <param name="messageBody">The message body to parse as a base64-encoded string.</param> /// <param name="expectedType">The type of the message being generated.</param> protected NtlmGenerator(string messageBody, NtlmMessageType expectedType) : this(Convert.FromBase64String(messageBody), expectedType) { }