Updated to release 1.7.1
[siplcs.git] / src / tests.c
blob7212913b9be304ca13b61ee4cb6acbf6bba768ac
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();
87 /* These tests are from the MS-SIPE document */
89 char * password = "Password";
90 char * user = "User";
91 char * domain = "Domain";
92 guchar nonce [] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
93 guchar exported_session_key[] = {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
95 printf ("\nTesting LMOWFv1()\n");
96 guchar response_key_lm [16];
97 LMOWFv1 (password, user, domain, response_key_lm);
98 assert_equal("E52CAC67419A9A224A3B108F3FA6CB6D", response_key_lm, 16, TRUE);
100 printf ("\nTesting LM Response Generation\n");
101 guchar lm_challenge_response [24];
102 DESL (response_key_lm, nonce, lm_challenge_response);
103 assert_equal("98DEF7B87F88AA5DAFE2DF779688A172DEF11C7D5CCDEF13", lm_challenge_response, 24, TRUE);
105 printf ("\n\nTesting NTOWFv1()\n");
106 guchar response_key_nt [16];
107 NTOWFv1 (password, user, domain, response_key_nt);
108 assert_equal("A4F49C406510BDCAB6824EE7C30FD852", response_key_nt, 16, TRUE);
110 printf ("\nTesting NT Response Generation\n");
111 guchar nt_challenge_response [24];
112 DESL (response_key_nt, nonce, nt_challenge_response);
113 assert_equal("67C43011F30298A2AD35ECE64F16331C44BDBED927841F94", nt_challenge_response, 24, TRUE);
115 printf ("\n\nTesting Session Base Key and Key Exchange Generation\n");
116 guchar session_base_key [16];
117 MD4(response_key_nt, 16, session_base_key);
118 guchar key_exchange_key [16];
119 KXKEY(session_base_key, lm_challenge_response, key_exchange_key);
120 assert_equal("D87262B0CDE4B1CB7499BECCCDF10784", session_base_key, 16, TRUE);
121 assert_equal("D87262B0CDE4B1CB7499BECCCDF10784", key_exchange_key, 16, TRUE);
123 printf ("\n\nTesting Encrypted Session Key Generation\n");
124 guchar encrypted_random_session_key [16];
125 RC4K (key_exchange_key, exported_session_key, encrypted_random_session_key);
126 assert_equal("518822B1B3F350C8958682ECBB3E3CB7", encrypted_random_session_key, 16, TRUE);
128 /* End tests from the MS-SIPE document */
130 // Test from http://davenport.sourceforge.net/ntlm.html#ntlm1Signing
131 printf ("\n\nTesting Signature Algorithm\n");
132 guchar sk [] = {0x01, 0x02, 0x03, 0x04, 0x05, 0xe5, 0x38, 0xb0};
133 assert_equal (
134 "0100000078010900397420FE0E5A0F89",
135 (guchar *) purple_ntlm_gen_signature ("jCIFS", sk, 0x00090178, 0, 8),
136 32, FALSE
139 // Verify signature of SIPE message received from OCS 2007 after authenticating with pidgin-sipe
140 printf ("\n\nTesting MS-SIPE Example Message Signing\n");
141 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>";
142 guchar exported_session_key2 [] = { 0x5F, 0x02, 0x91, 0x53, 0xBC, 0x02, 0x50, 0x58, 0x96, 0x95, 0x48, 0x61, 0x5E, 0x70, 0x99, 0xBA };
143 assert_equal (
144 "0100000000000000BF2E52667DDF6DED",
145 (guchar *) purple_ntlm_gen_signature(msg1, exported_session_key2, 0, 100, 16),
146 32, FALSE
149 // Verify parsing of message and signature verification
150 printf ("\n\nTesting MS-SIPE Example Message Parsing, Signing, and Verification\n");
151 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";
152 struct sipmsg * msg = sipmsg_parse_msg(msg2);
153 struct sipmsg_breakdown msgbd;
154 msgbd.msg = msg;
155 sipmsg_breakdown_parse(&msgbd, "SIP Communications Service", "ocs1.ocs.provo.novell.com");
156 gchar * msg_str = sipmsg_breakdown_get_string(&msgbd);
157 gchar * sig = purple_ntlm_sipe_signature_make (msg_str, exported_session_key2);
158 sipmsg_breakdown_free(&msgbd);
159 assert_equal ("0100000000000000BF2E52667DDF6DED", (guchar *) sig, 32, FALSE);
160 printf("purple_ntlm_verify_signature result = %i\n", purple_ntlm_verify_signature (sig, "0100000000000000BF2E52667DDF6DED"));
163 /* begin tests from MS-SIPRE */
165 const char *testEpid = "01010101";
166 const char *expectedUUID = "4b1682a8-f968-5701-83fc-7c6741dc6697";
167 gchar *calcUUID = generateUUIDfromEPID(testEpid);
169 printf("\n\nTesting MS-SIPRE uuid derivation\n");
171 assert_equal(expectedUUID, (guchar *) calcUUID, strlen(expectedUUID), FALSE);
172 g_free(calcUUID);
174 guchar addr[6];
175 gchar nmac[6];
177 int i,j;
178 for (i = 0,j=0; i < 6; i++,j+=2) {
179 g_sprintf(&nmac[j], "%02X", addr[i]);
182 printf("Mac: %s\n", g_strdup(nmac));
184 /* end tests from MS-SIPRE */
186 printf ("\nFinished With Tests; %d successs %d failures\n", successes, failures);
190 Local Variables:
191 mode: c
192 c-file-style: "bsd"
193 indent-tabs-mode: t
194 tab-width: 8
195 End: