/*******************************************************************************
 * Waffle (https://github.com/dblock/waffle)
 * 
 * Copyright (c) 2010 Application Security, Inc.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Application Security, Inc.
 *******************************************************************************/
package waffle.util;

/**
 * Rudimentary NTLM message utility.
 * 
 * @author ari.suutari[at]syncrontech[dot]com
 */
public abstract class SPNegoMessage {

	// SPNEGO OID, for details see http://msdn.microsoft.com/en-us/library/ms995330.aspx
	private static final byte[]	spnegoOid	= {0x06, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02};

	public static boolean isSPNegoMessage(byte[] message) {
		if (message == null || message.length < 2) {
			return false;
		}

		return isNegTokenInit(message) || isNegTokenArg(message);
	}

	public static boolean isNegTokenInit(byte[] message)
	{
		// First byte should always be 0x60 (Application Constructed Object)
		if (message[0] != 0x60) {
			return false;
		}

		// Next byte(s) contain token length, figure out
		// how many bytes are used for length data
		int lenBytes = 1;
		if ((message[1] & 0x80) != 0) {
			lenBytes = 1 + (message[1] & 0x7f);
		}

		if (message.length < spnegoOid.length + 1 + lenBytes) {
			return false;
		}

		// Now check for SPNEGO OID, which should start just after length data.
		for (int i = 0; i < spnegoOid.length; i++) {
			if (spnegoOid[i] != message[i + 1 + lenBytes]) {
				return false;
			}
		}

		return true;
	}

	public static boolean isNegTokenArg(byte[] message)
	{
		// Check if this is NegTokenArg packet, it's id is 0xa1
		if ((message[0] & 0xff) != 0xa1)
			return false;

		int lenBytes;
		int len;

		// Get lenght of message for additional check.
		if ((message[1] & 0x80) == 0)
			len = message[1];
		else {

			lenBytes = message[1] & 0x7f;
			len = 0;
			int i = 2;
			while (lenBytes > 0) {

				len = len << 8;
				len |= (message[i] & 0xff);
				--lenBytes;
			}
		}

		return (len + 2 == message.length);
	}
}

@@ -722,35 +722,165 @@
 %09// 
-SPNEGO OID, for details see
+Check if this message is SPNEGO auhentication token. There%0A%09// are two token types, NegTokenInit and NegTokenArg. %0A%09// For details and specification, see%0A%09//
  htt
@@ -936,22 +936,21 @@
 x%0A%09p
-rivate
+ublic
  static 
 fina
@@ -949,137 +949,190 @@
 tic 
-final byte%5B%5D%09spnegoOid%09= %7B0x06, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02%7D;%0A%0A%09public static boolean isSPNegoMessage(byte%5B%5D
+boolean isSPNegoMessage(byte%5B%5D message) %7B%0A%09%09%0A%09%09// Message should always contains at least some kind of%0A%09%09// id byte and length. If it is too short, it%0A%09%09// cannot be a SPNEGO
  message
 ) %7B%0A
@@ -1127,19 +1127,17 @@
  message
-) %7B
+.
 %0A%09%09if (m
@@ -1204,68 +1204,349 @@
 %0A%0A%09%09
-return isNegTokenInit(message) %7C%7C isNegTokenArg(message);%0A%09%7D
+// Message is SPNEGO message if it is either NegTokenInit or NegTokenArg.%0A%09%09return isNegTokenInit(message) %7C%7C isNegTokenArg(message);%0A%09%7D%0A%0A%09// Check for NegTokenInit. It has always a special oid (%22spnegoOid%22),%0A%09// which makes it rather easy to detect.%0A%09private static final byte%5B%5D%09spnegoOid%09= %7B0x06, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02%7D;
 %0A%0A%09p
@@ -2215,16 +2215,269 @@
 ue;%0A%09%7D%0A%0A
+%09// Check for NegTokenArg. It doesn't have oid similar to NegTokenInit.%0A%09// Instead id has one-byte id (0xa1). Obviously this is not%0A%09// a great way to detect the message, so we check encoded%0A%09// message length against number of received message bytes.%0A
 %09public 

Document detection of different tokens better.