001: package net.sourceforge.jtds.test;
002:
003: import net.sourceforge.jtds.jdbc.NtlmAuth;
004:
005: import java.util.Arrays;
006:
007: /**
008: * Unit test for NTLM challenge/response calculation
009: * Written by mdb.
010: */
011: public class NtlmAuthTest extends TestBase {
012: public NtlmAuthTest(String name) {
013: super (name);
014: }
015:
016: public static byte[] hexToBytes(String hex) {
017: byte[] rtn = new byte[hex.length() / 2];
018: for (int i = 0; i < rtn.length; i++) {
019: rtn[i] = (byte) Integer.parseInt(hex.substring(i * 2,
020: (i + 1) * 2), 16);
021: }
022: return rtn;
023: }
024:
025: /**
026: * Tests the NT challenge/response against a known-good value. This was captured
027: * from a successful login to one of my (mdb's) test computers.
028: */
029: public void testChallengeResponse() throws Exception {
030: final String password = "bark";
031: byte[] challenge = new byte[] { (byte) 0xd9, (byte) 0x90,
032: (byte) 0xed, (byte) 0xaf, (byte) 0x94, (byte) 0x17,
033: (byte) 0x36, (byte) 0xaf };
034:
035: byte[] ntResp = NtlmAuth.answerNtChallenge(password, challenge);
036: byte[] lmResp = NtlmAuth.answerLmChallenge(password, challenge);
037:
038: byte[] ntExpected = new byte[] { (byte) 0x8e, (byte) 0x75,
039: (byte) 0x8e, (byte) 0x79, (byte) 0xe2, (byte) 0xa1,
040: (byte) 0x45, (byte) 0x75, (byte) 0xb4, (byte) 0x21,
041: (byte) 0x55, (byte) 0x9b, (byte) 0x12, (byte) 0x29,
042: (byte) 0xd3, (byte) 0x5a, (byte) 0x23, (byte) 0x8b,
043: (byte) 0x7d, (byte) 0xa8, (byte) 0x3a, (byte) 0x50,
044: (byte) 0xc6, (byte) 0xa7 };
045:
046: byte[] lmExpected = new byte[] { (byte) 0xe6, (byte) 0x19,
047: (byte) 0x92, (byte) 0xcd, (byte) 0x84, (byte) 0xf7,
048: (byte) 0xb8, (byte) 0x49, (byte) 0xaf, (byte) 0x75,
049: (byte) 0xf9, (byte) 0x37, (byte) 0xd4, (byte) 0x0b,
050: (byte) 0xe6, (byte) 0x81, (byte) 0xc4, (byte) 0x0c,
051: (byte) 0x7c, (byte) 0x3f, (byte) 0x3e, (byte) 0xc6,
052: (byte) 0x8b, (byte) 0x7f };
053:
054: assertTrue(Arrays.equals(ntResp, ntExpected));
055: assertTrue(Arrays.equals(lmResp, lmExpected));
056: }
057:
058: //--------------------------------------------------------------------------
059: // these tests came from the web page:
060: // http://davenport.sourceforge.net/ntlm.html
061: //--------------------------------------------------------------------------
062:
063: public void testLMv2() throws Exception {
064: byte[] answer = NtlmAuth.answerLmv2Challenge("DOMAIN", "user",
065: "SecREt01", hexToBytes("0123456789abcdef"),
066: hexToBytes("ffffff0011223344"));
067:
068: byte[] expected = hexToBytes("d6e6152ea25d03b7c6ba6629c2d6aaf0ffffff0011223344");
069:
070: assertTrue(Arrays.equals(answer, expected));
071: }
072:
073: public void testNTLMv2() throws Exception {
074: byte[] answer = NtlmAuth
075: .answerNtlmv2Challenge(
076: "DOMAIN",
077: "user",
078: "SecREt01",
079: hexToBytes("0123456789abcdef"), //nonce
080: //target info:
081: hexToBytes("02000c0044004f004d00410049004e0001000c005300450052"
082: + "005600450052000400140064006f006d00610069006e002e00"
083: + "63006f006d00030022007300650072007600650072002e0064"
084: + "006f006d00610069006e002e0063006f006d0000000000"),
085: hexToBytes("ffffff0011223344"),//client nonce
086: 1055844000000L); //timestamp
087:
088: byte[] expected = hexToBytes("cbabbca713eb795d04c97abc01ee4983"
089: + "01010000000000000090d336b734c301"
090: + "ffffff00112233440000000002000c00"
091: + "44004f004d00410049004e0001000c00"
092: + "53004500520056004500520004001400"
093: + "64006f006d00610069006e002e006300"
094: + "6f006d00030022007300650072007600"
095: + "650072002e0064006f006d0061006900"
096: + "6e002e0063006f006d00000000000000" + "0000");
097:
098: assertTrue(Arrays.equals(answer, expected));
099: }
100:
101: public void testTimestampConversion() throws Exception {
102: long time = 1055844000000L;
103: byte[] ts = NtlmAuth.createTimestamp(time);
104:
105: byte[] expected = hexToBytes("0090d336b734c301");
106: assertTrue(Arrays.equals(ts, expected));
107: }
108:
109: //--------------------------------------------------------------------------
110: // these came from tests with real data:
111: //--------------------------------------------------------------------------
112:
113: public void testLMv2CapturedData() throws Exception {
114: byte[] answer = NtlmAuth.answerLmv2Challenge("MDB-PADRE",
115: "dog", "bark", hexToBytes("73f35b0fe01a5a31"),
116: hexToBytes("2c66391a0a1b7881"));
117:
118: byte[] expected = hexToBytes("4dc364696984b6e07df1a659313f277a2c66391a0a1b7881");
119:
120: assertTrue(Arrays.equals(answer, expected));
121: }
122:
123: public void testNTLMv2CapturedData() throws Exception {
124: byte[] targetInfo = hexToBytes("02000c004200450041004500"
125: + "4e004700010014004d00440042002d00"
126: + "42005200450057004500520004002200"
127: + "62006500610065006e0067002e006d00"
128: + "6600650065006e0067002e006f007200"
129: + "6700030038006d00640062002d006200"
130: + "720065007700650072002e0062006500"
131: + "610065006e0067002e006d0066006500"
132: + "65006e0067002e006f00720067000500"
133: + "14006d006600650065006e0067002e00"
134: + "6f007200670000000000");
135:
136: byte[] answer = NtlmAuth.answerNtlmv2Challenge("MDB-PADRE",
137: "dog", "bark", hexToBytes("73f35b0fe01a5a31"),
138: targetInfo, hexToBytes("2c66391a0a1b7881"),
139: hexToBytes("06198e3a444dc601"));
140:
141: byte[] expected = hexToBytes("5416e7ef86091320"
142: + "5b652f7b3002fc7f0101000000000000"
143: + "06198e3a444dc6012c66391a0a1b7881"
144: + "0000000002000c004200450041004500"
145: + "4e004700010014004d00440042002d00"
146: + "42005200450057004500520004002200"
147: + "62006500610065006e0067002e006d00"
148: + "6600650065006e0067002e006f007200"
149: + "6700030038006d00640062002d006200"
150: + "720065007700650072002e0062006500"
151: + "610065006e0067002e006d0066006500"
152: + "65006e0067002e006f00720067000500"
153: + "14006d006600650065006e0067002e00"
154: + "6f00720067000000000000000000");
155:
156: //debug...
157: /*
158: //debug
159: public static void dump(byte[] bytes, String fileName)
160: {
161: try
162: {
163: FileOutputStream out = new FileOutputStream(fileName);
164: out.write( bytes );
165: out.close();
166: }
167: catch(Exception e)
168: {
169: //don't worry about it
170: }
171: }
172:
173: dump(expected, "/home/brinkley/tmp/ntlm2-expected" );
174: dump(answer, "/home/brinkley/tmp/ntlm2-answer" );
175: */
176:
177: assertTrue(Arrays.equals(answer, expected));
178: }
179:
180: }
|