NTLM: remove random pad from v1 plaintext
[siplcs.git] / src / core / tests.c
blob16692ee18ec1ccee55c9298db8dadbf2fb41831e
1 /**
2 * @file tests.c
4 * pidgin-sipe
6 * Copyright (C) 2008 Novell, Inc.
8 * Implemented with reference to the follow documentation:
9 * - http://davenport.sourceforge.net/ntlm.html
10 * - MS-NLMP: http://msdn.microsoft.com/en-us/library/cc207842.aspx
11 * - MS-SIP : http://msdn.microsoft.com/en-us/library/cc246115.aspx
13 * Build and run with (adjust as needed to your build platform!)
15 * $ gcc -I /usr/include/libpurple \
16 * -I /usr/include/dbus-1.0 -I /usr/lib/dbus-1.0/include \
17 * -I /usr/include/glib-2.0 -I /usr/lib/glib-2.0/include \
18 * -o tests tests.c sipe-sign.c sipmsg.c sip-sec.c uuid.c -lpurple
19 * ./tests
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <glib.h>
37 #include <stdlib.h>
39 #include "sipe-sign.h"
40 #include "sip-sec-ntlm.c"
42 #include "dbus-server.h"
44 #include "uuid.h"
46 static int successes = 0;
47 static int failures = 0;
49 void assert_equal(const char * expected, const guchar * got, int len, gboolean stringify)
51 const gchar * res = (gchar *) got;
52 gchar to_str[len*2];
54 if (stringify) {
55 int i, j;
56 for (i = 0, j = 0; i < len; i++, j+=2) {
57 g_sprintf(&to_str[j], "%02X", (got[i]&0xff));
59 len *= 2;
60 res = to_str;
63 printf("expected: %s\n", expected);
64 printf("received: %s\n", res);
66 if (strncmp(expected, res, len) == 0) {
67 successes++;
68 printf("PASSED\n");
69 } else {
70 failures++;
71 printf("FAILED\n");
75 int main()
77 printf ("Starting Tests\n");
79 // Initialization that Pidgin would normally do
80 g_type_init();
81 purple_signals_init();
82 purple_util_init();
83 purple_debug_init();
84 purple_dbus_init();
85 purple_ciphers_init();
86 purple_debug_set_enabled(TRUE);
88 /* These tests are from the MS-SIPE document */
90 const char * password = "Password";
91 const char * user = "User";
92 const char * domain = "Domain";
93 const guchar client_challenge [] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
94 /* server challenge */
95 const guchar nonce [] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
96 /* 16 bytes */
97 const guchar exported_session_key[] = {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
99 guint32 flags = 0
100 | NTLMSSP_NEGOTIATE_KEY_EXCH
101 | NTLMSSP_NEGOTIATE_56
102 | NTLMSSP_NEGOTIATE_128
103 | NTLMSSP_NEGOTIATE_VERSION
104 | NTLMSSP_TARGET_TYPE_SERVER
105 | NTLMSSP_NEGOTIATE_ALWAYS_SIGN
106 | NTLMSSP_NEGOTIATE_NTLM
107 | NTLMSSP_NEGOTIATE_SEAL
108 | NTLMSSP_NEGOTIATE_SIGN
109 | NTLMSSP_NEGOTIATE_OEM
110 | NTLMSSP_NEGOTIATE_UNICODE;
112 printf ("\nTesting MD4()\n");
113 guchar md4 [16];
114 MD4 ((const unsigned char *)"message digest", 14, md4);
115 assert_equal("D9130A8164549FE818874806E1C7014B", md4, 16, TRUE);
117 printf ("\nTesting MD5()\n");
118 guchar md5 [16];
119 MD5 ((const unsigned char *)"message digest", 14, md5);
120 assert_equal("F96B697D7CB7938D525A2F31AAF161D0", md5, 16, TRUE);
122 printf ("\nTesting HMAC_MD5()\n");
123 guchar hmac_md5 [16];
124 HMAC_MD5 ((const unsigned char *)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16, (const unsigned char *)"Hi There", 8, hmac_md5);
125 assert_equal("9294727A3638BB1C13F48EF8158BFC9D", hmac_md5, 16, TRUE);
127 printf ("\nTesting LMOWFv1()\n");
128 guchar response_key_lm [16];
129 LMOWFv1 (password, user, domain, response_key_lm);
130 assert_equal("E52CAC67419A9A224A3B108F3FA6CB6D", response_key_lm, 16, TRUE);
132 printf ("\nTesting LM Response Generation\n");
133 guchar lm_challenge_response [24];
134 DESL (response_key_lm, nonce, lm_challenge_response);
135 assert_equal("98DEF7B87F88AA5DAFE2DF779688A172DEF11C7D5CCDEF13", lm_challenge_response, 24, TRUE);
137 printf ("\n\nTesting NTOWFv1()\n");
138 guchar response_key_nt [16];
139 NTOWFv1 (password, user, domain, response_key_nt);
140 assert_equal("A4F49C406510BDCAB6824EE7C30FD852", response_key_nt, 16, TRUE);
142 printf ("\nTesting NT Response Generation\n");
143 guchar nt_challenge_response [24];
144 DESL (response_key_nt, nonce, nt_challenge_response);
145 assert_equal("67C43011F30298A2AD35ECE64F16331C44BDBED927841F94", nt_challenge_response, 24, TRUE);
147 printf ("\n\nTesting Session Base Key and Key Exchange Generation\n");
148 guchar session_base_key [16];
149 MD4(response_key_nt, 16, session_base_key);
150 guchar key_exchange_key [16];
151 KXKEY(flags, session_base_key, lm_challenge_response, nonce, key_exchange_key);
152 assert_equal("D87262B0CDE4B1CB7499BECCCDF10784", session_base_key, 16, TRUE);
153 assert_equal("D87262B0CDE4B1CB7499BECCCDF10784", key_exchange_key, 16, TRUE);
155 printf ("\n\nTesting Encrypted Session Key Generation\n");
156 guchar encrypted_random_session_key [16];
157 RC4K (key_exchange_key, 16, exported_session_key, 16, encrypted_random_session_key);
158 assert_equal("518822B1B3F350C8958682ECBB3E3CB7", encrypted_random_session_key, 16, TRUE);
160 printf ("\n\nTesting CRC32\n");
161 const guchar text [] = {0x50, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x74, 0x00, 0x65, 0x00, 0x78, 0x00, 0x74, 0x00}; //P·l·a·i·n·t·e·x·t·
162 //guchar text [] = {0x56, 0xfe, 0x04, 0xd8, 0x61, 0xf9, 0x31, 0x9a, 0xf0, 0xd7, 0x23, 0x8a, 0x2e, 0x3b, 0x4d, 0x45, 0x7f, 0xb8};
163 gint32 crc = CRC32((char*)text, 18);
164 assert_equal("7D84AA93", (guchar *)&crc, 4, TRUE);
166 printf ("\n\nTesting Encryption\n");
167 guchar client_seal_key [16];
168 //SEALKEY (flags, exported_session_key, TRUE, client_seal_key);
169 guchar buff [18 + 12];
170 memcpy(buff, text, 18);
171 guchar text_enc [18 + 12];
172 guint32 *ptr = (guint32 *)(buff + 18);
173 ptr[0] = 0; // random pad
174 ptr[1] = crc;
175 ptr[2] = 0; // zero
176 RC4K (exported_session_key, 16, buff, 18 + 12, text_enc);
177 //The point is to not reinitialize rc4 cypher
178 // 0 crc 0 (zero)
179 assert_equal("56FE04D861F9319AF0D7238A2E3B4D457FB8" "45C844E5" "09DCD1DF" "2E459D36", text_enc, 18 + 12, TRUE);
181 printf ("\n\nTesting MAC\n");
182 // won't work in the case with sealing because RC4 is re-initialized inside.
183 //gchar *mac = MAC (flags, (gchar*)text, 18, (guchar*)exported_session_key, 16, 0,16, 0x00000000, 0);
184 ptr = (guint32 *)(text_enc + 18);
185 guint32 mac2 [4];
186 mac2 [0] = 1; // version
187 mac2 [1] = ptr [0];
188 mac2 [2] = ptr [1];
189 mac2 [3] = ptr [2] ^ ((guint32)0); // ^ seq
190 assert_equal("0100000045C844E509DCD1DF2E459D36", (guchar*)mac2, 16, TRUE);
192 ////// EXTENDED_SESSIONSECURITY ///////
193 //guint32 flags = NEGOTIATE_FLAGS | NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY;
194 flags = 0
195 | NTLMSSP_NEGOTIATE_56
196 | NTLMSSP_NEGOTIATE_VERSION
197 | NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
198 | NTLMSSP_TARGET_TYPE_SERVER
199 | NTLMSSP_NEGOTIATE_ALWAYS_SIGN
200 | NTLMSSP_NEGOTIATE_NTLM
201 | NTLMSSP_NEGOTIATE_SEAL
202 | NTLMSSP_NEGOTIATE_SIGN
203 | NTLMSSP_NEGOTIATE_OEM
204 | NTLMSSP_NEGOTIATE_UNICODE;
206 printf ("\n\n(Extended session security) Testing LM Response Generation\n");
207 memcpy(lm_challenge_response, client_challenge, 8);
208 Z (lm_challenge_response+8, 16);
209 assert_equal("AAAAAAAAAAAAAAAA00000000000000000000000000000000", lm_challenge_response, 24, TRUE);
211 printf ("\n\n(Extended session seurity) Testing Key Exchange\n");
212 KXKEY(flags, session_base_key, lm_challenge_response, nonce, key_exchange_key);
213 assert_equal("EB93429A8BD952F8B89C55B87F475EDC", key_exchange_key, 16, TRUE);
215 printf ("\n\n(Extended session security) Testing NT Response Generation\n");
216 unsigned char prehash [16];
217 unsigned char hash [16];
218 memcpy(prehash, nonce, 8);
219 memcpy(prehash + 8, client_challenge, 8);
220 MD5 (prehash, 16, hash);
221 DESL (response_key_nt, hash, nt_challenge_response);
222 assert_equal("7537F803AE367128CA458204BDE7CAF81E97ED2683267232", nt_challenge_response, 24, TRUE);
224 printf ("\n\n(Extended session security) SIGNKEY\n");
225 guchar client_sign_key [16];
226 SIGNKEY (key_exchange_key, TRUE, client_sign_key);
227 assert_equal("60E799BE5C72FC92922AE8EBE961FB8D", client_sign_key, 16, TRUE);
229 printf ("\n\n(Extended session security) SEALKEY\n");
230 SEALKEY (flags, key_exchange_key, TRUE, client_seal_key);
231 assert_equal("04DD7F014D8504D265A25CC86A3A7C06", client_seal_key, 16, TRUE);
233 printf ("\n\n(Extended session security) Testing Encryption\n");
234 RC4K (client_seal_key, 16, text, 18, text_enc);
235 assert_equal("A02372F6530273F3AA1EB90190CE5200C99D", text_enc, 18, TRUE);
237 printf ("\n\n(Extended session security) Testing MAC\n");
238 gchar *mac = MAC (flags, (gchar*)text,18, client_sign_key,16, client_seal_key,16, 0, 0);
239 assert_equal("01000000FF2AEB52F681793A00000000", (guchar*)mac, 32, FALSE);
240 g_free(mac);
242 ////// NTLMv2 ///////
243 flags = 0
244 | NTLMSSP_NEGOTIATE_KEY_EXCH
245 | NTLMSSP_NEGOTIATE_56
246 | NTLMSSP_NEGOTIATE_128
247 | NTLMSSP_NEGOTIATE_VERSION
248 | NTLMSSP_NEGOTIATE_TARGET_INFO
249 | NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
250 | NTLMSSP_TARGET_TYPE_SERVER
251 | NTLMSSP_NEGOTIATE_ALWAYS_SIGN
252 | NTLMSSP_NEGOTIATE_NTLM
253 | NTLMSSP_NEGOTIATE_SEAL
254 | NTLMSSP_NEGOTIATE_SIGN
255 | NTLMSSP_NEGOTIATE_OEM
256 | NTLMSSP_NEGOTIATE_UNICODE;
258 printf ("\n\nTesting NTOWFv2()\n");
259 NTOWFv2 (password, user, domain, response_key_nt);
260 assert_equal("0C868A403BFD7A93A3001EF22EF02E3F", response_key_nt, 16, TRUE);
262 printf ("\n\nTesting (NTLMv2) SIGNKEY\n");
263 SIGNKEY (exported_session_key, TRUE, client_sign_key);
264 assert_equal("4788DC861B4782F35D43FD98FE1A2D39", client_sign_key, 16, TRUE);
266 printf ("\n\nTesting (NTLMv2) SEALKEY\n");
267 SEALKEY (flags, exported_session_key, TRUE, client_seal_key);
268 assert_equal("59F600973CC4960A25480A7C196E4C58", client_seal_key, 16, TRUE);
270 printf ("\n\nTesting (NTLMv2) Encryption\n");
271 RC4K (client_seal_key, 16, text, 18, text_enc);
272 assert_equal("54E50165BF1936DC996020C1811B0F06FB5F", text_enc, 18, TRUE);
274 // printf ("\n\nTesting (NTLMv2) Encryption\n");
275 //const guchar text2 [] = {0x50, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x74, 0x00, 0x65, 0x00, 0x78, 0x00, 0x74, 0x00
276 // , 0x70, 0x35, 0x28, 0x51, 0xf2, 0x56, 0x43, 0x09}; //P·l·a·i·n·t·e·x·t·
277 //guchar text_enc2 [18+8];
278 // RC4K (client_seal_key, 16, text2, 18+8, text_enc2);
279 // assert_equal("54E50165BF1936DC996020C1811B0F06FB5F", text_enc2, 18+8, TRUE);
281 printf ("\n\nTesting (NTLMv2) MAC (without RC4, as we don't keep its handle yet)\n");
282 mac = MAC (flags & ~NTLMSSP_NEGOTIATE_KEY_EXCH, (gchar*)text,18, client_sign_key,16, client_seal_key,16, 0, 0);
283 assert_equal("0100000070352851F256430900000000", (guchar*)mac, 32, FALSE);
284 g_free(mac);
287 /* End tests from the MS-SIPE document */
289 // Test from http://davenport.sourceforge.net/ntlm.html#ntlm1Signing
290 const gchar *text_j = "jCIFS";
291 printf ("\n\n(davenport) Testing Signature Algorithm\n");
292 guchar sk [] = {0x01, 0x02, 0x03, 0x04, 0x05, 0xe5, 0x38, 0xb0};
293 assert_equal (
294 "0100000078010900397420FE0E5A0F89",
295 (guchar *) MAC(NEGOTIATE_FLAGS, text_j, strlen(text_j), sk, 8, 0,16, 0x00090178, 0),
296 32, FALSE
299 // Tests from http://davenport.sourceforge.net/ntlm.html#ntlm2Signing
300 printf ("\n\n(davenport) SIGNKEY\n");
301 const guchar master_key [] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00};
302 SIGNKEY (master_key, TRUE, client_sign_key);
303 assert_equal("F7F97A82EC390F9C903DAC4F6ACEB132", client_sign_key, 16, TRUE);
305 printf ("\n\n(davenport) Testing MAC - no Key Exchange flag\n");
306 mac = MAC (flags & ~NTLMSSP_NEGOTIATE_KEY_EXCH, text_j, strlen(text_j), client_sign_key, 16, 0,16, 0, 0);
307 assert_equal("010000000A003602317A759A00000000", (guchar*)mac, 32, FALSE);
308 g_free(mac);
310 // Verify signature of SIPE message received from OCS 2007 after authenticating with pidgin-sipe
311 printf ("\n\nTesting MS-SIPE Example Message Signing\n");
312 char * msg1 = "<NTLM><0878F41B><1><SIP Communications Service><ocs1.ocs.provo.novell.com><8592g5DCBa1694i5887m0D0Bt2247b3F38xAE9Fx><3><REGISTER><sip:gabriel@ocs.provo.novell.com><2947328781><B816D65C2300A32CFA6D371F2AF537FD><900><200>";
313 guchar exported_session_key2 [] = { 0x5F, 0x02, 0x91, 0x53, 0xBC, 0x02, 0x50, 0x58, 0x96, 0x95, 0x48, 0x61, 0x5E, 0x70, 0x99, 0xBA };
314 assert_equal (
315 "0100000000000000BF2E52667DDF6DED",
316 (guchar *) MAC(NEGOTIATE_FLAGS, msg1, strlen(msg1), exported_session_key2, 16, 0,16, 0, 100),
317 32, FALSE
320 // Verify parsing of message and signature verification
321 printf ("\n\nTesting MS-SIPE Example Message Parsing, Signing, and Verification\n");
322 char * msg2 = "SIP/2.0 200 OK\r\nms-keep-alive: UAS; tcp=no; hop-hop=yes; end-end=no; timeout=300\r\nAuthentication-Info: NTLM rspauth=\"0100000000000000BF2E52667DDF6DED\", srand=\"0878F41B\", snum=\"1\", opaque=\"4452DFB0\", qop=\"auth\", targetname=\"ocs1.ocs.provo.novell.com\", realm=\"SIP Communications Service\"\r\nFrom: \"Gabriel Burt\"<sip:gabriel@ocs.provo.novell.com>;tag=2947328781;epid=1234567890\r\nTo: <sip:gabriel@ocs.provo.novell.com>;tag=B816D65C2300A32CFA6D371F2AF537FD\r\nCall-ID: 8592g5DCBa1694i5887m0D0Bt2247b3F38xAE9Fx\r\nCSeq: 3 REGISTER\r\nVia: SIP/2.0/TLS 164.99.194.49:10409;branch=z9hG4bKE0E37DBAF252C3255BAD;received=164.99.195.20;ms-received-port=10409;ms-received-cid=1E00\r\nContact: <sip:164.99.195.20:10409;transport=tls;ms-received-cid=1E00>;expires=900\r\nExpires: 900\r\nAllow-Events: vnd-microsoft-provisioning,vnd-microsoft-roaming-contacts,vnd-microsoft-roaming-ACL,presence,presence.wpending,vnd-microsoft-roaming-self,vnd-microsoft-provisioning-v2\r\nSupported: adhoclist\r\nServer: RTC/3.0\r\nSupported: com.microsoft.msrtc.presence\r\nContent-Length: 0\r\n\r\n";
323 struct sipmsg * msg = sipmsg_parse_msg(msg2);
324 struct sipmsg_breakdown msgbd;
325 msgbd.msg = msg;
326 sipmsg_breakdown_parse(&msgbd, "SIP Communications Service", "ocs1.ocs.provo.novell.com");
327 gchar * msg_str = sipmsg_breakdown_get_string(&msgbd);
328 gchar * sig = purple_ntlm_sipe_signature_make (NEGOTIATE_FLAGS, msg_str, 0, exported_session_key2);
329 sipmsg_breakdown_free(&msgbd);
330 assert_equal ("0100000000000000BF2E52667DDF6DED", (guchar *) sig, 32, FALSE);
331 printf("purple_ntlm_verify_signature result = %i\n", purple_ntlm_verify_signature (sig, "0100000000000000BF2E52667DDF6DED"));
334 /* begin tests from MS-SIPRE */
336 const char *testEpid = "01010101";
337 const char *expectedUUID = "4b1682a8-f968-5701-83fc-7c6741dc6697";
338 gchar *calcUUID = generateUUIDfromEPID(testEpid);
340 printf("\n\nTesting MS-SIPRE UUID derivation\n");
342 assert_equal(expectedUUID, (guchar *) calcUUID, strlen(expectedUUID), FALSE);
343 g_free(calcUUID);
345 guchar addr[6];
346 gchar nmac[6];
348 int i,j;
349 for (i = 0,j=0; i < 6; i++,j+=2) {
350 g_sprintf(&nmac[j], "%02X", addr[i]);
353 printf("Mac: %s\n", g_strdup(nmac));
355 /* end tests from MS-SIPRE */
357 printf ("\nFinished With Tests; %d successs %d failures\n", successes, failures);
359 return(0);
363 Local Variables:
364 mode: c
365 c-file-style: "bsd"
366 indent-tabs-mode: t
367 tab-width: 8
368 End: