No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / ntlm / test_ntlm.c
blobff94c88cbcb096103c34cef79d7dcf331c2b4c1f
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "config.h"
36 #include <stdio.h>
37 #include <err.h>
38 #include <roken.h>
39 #include <getarg.h>
41 __RCSID("$Heimdal: test_ntlm.c 22377 2007-12-28 18:38:53Z lha $"
42 "$NetBSD$");
44 #include <krb5.h>
45 #include <heimntlm.h>
47 static int
48 test_parse(void)
50 const char *user = "foo",
51 *domain = "mydomain",
52 *password = "digestpassword",
53 *target = "DOMAIN";
54 struct ntlm_type1 type1;
55 struct ntlm_type2 type2;
56 struct ntlm_type3 type3;
57 struct ntlm_buf data;
58 krb5_error_code ret;
59 int flags;
61 memset(&type1, 0, sizeof(type1));
63 type1.flags = NTLM_NEG_UNICODE|NTLM_NEG_TARGET|NTLM_NEG_NTLM;
64 type1.domain = rk_UNCONST(domain);
65 type1.hostname = NULL;
66 type1.os[0] = 0;
67 type1.os[1] = 0;
69 ret = heim_ntlm_encode_type1(&type1, &data);
70 if (ret)
71 errx(1, "heim_ntlm_encode_type1");
73 memset(&type1, 0, sizeof(type1));
75 ret = heim_ntlm_decode_type1(&data, &type1);
76 free(data.data);
77 if (ret)
78 errx(1, "heim_ntlm_encode_type1");
80 heim_ntlm_free_type1(&type1);
86 memset(&type2, 0, sizeof(type2));
88 flags = NTLM_NEG_UNICODE | NTLM_NEG_NTLM | NTLM_TARGET_DOMAIN;
89 type2.flags = flags;
91 memset(type2.challange, 0x7f, sizeof(type2.challange));
92 type2.targetname = rk_UNCONST(target);
93 type2.targetinfo.data = NULL;
94 type2.targetinfo.length = 0;
96 ret = heim_ntlm_encode_type2(&type2, &data);
97 if (ret)
98 errx(1, "heim_ntlm_encode_type2");
100 memset(&type2, 0, sizeof(type2));
102 ret = heim_ntlm_decode_type2(&data, &type2);
103 free(data.data);
104 if (ret)
105 errx(1, "heim_ntlm_decode_type2");
107 heim_ntlm_free_type2(&type2);
113 memset(&type3, 0, sizeof(type3));
115 type3.flags = flags;
116 type3.username = rk_UNCONST(user);
117 type3.targetname = rk_UNCONST(target);
118 type3.ws = rk_UNCONST("workstation");
121 struct ntlm_buf key;
122 heim_ntlm_nt_key(password, &key);
124 heim_ntlm_calculate_ntlm1(key.data, key.length,
125 type2.challange,
126 &type3.ntlm);
127 free(key.data);
130 ret = heim_ntlm_encode_type3(&type3, &data);
131 if (ret)
132 errx(1, "heim_ntlm_encode_type3");
134 free(type3.ntlm.data);
136 memset(&type3, 0, sizeof(type3));
138 ret = heim_ntlm_decode_type3(&data, 1, &type3);
139 free(data.data);
140 if (ret)
141 errx(1, "heim_ntlm_decode_type3");
143 if (strcmp("workstation", type3.ws) != 0)
144 errx(1, "type3 ws wrong");
146 if (strcmp(target, type3.targetname) != 0)
147 errx(1, "type3 targetname wrong");
149 if (strcmp(user, type3.username) != 0)
150 errx(1, "type3 username wrong");
153 heim_ntlm_free_type3(&type3);
156 * NTLMv2
159 memset(&type2, 0, sizeof(type2));
161 flags = NTLM_NEG_UNICODE | NTLM_NEG_NTLM | NTLM_TARGET_DOMAIN;
162 type2.flags = flags;
164 memset(type2.challange, 0x7f, sizeof(type2.challange));
165 type2.targetname = rk_UNCONST(target);
166 type2.targetinfo.data = "\x00\x00";
167 type2.targetinfo.length = 2;
169 ret = heim_ntlm_encode_type2(&type2, &data);
170 if (ret)
171 errx(1, "heim_ntlm_encode_type2");
173 memset(&type2, 0, sizeof(type2));
175 ret = heim_ntlm_decode_type2(&data, &type2);
176 free(data.data);
177 if (ret)
178 errx(1, "heim_ntlm_decode_type2");
180 heim_ntlm_free_type2(&type2);
182 return 0;
185 static int
186 test_keys(void)
188 const char
189 *username = "test",
190 *password = "test1234",
191 *target = "TESTNT";
192 const unsigned char
193 serverchallange[8] = "\x67\x7f\x1c\x55\x7a\x5e\xe9\x6c";
194 struct ntlm_buf infotarget, infotarget2, answer, key;
195 unsigned char ntlmv2[16], ntlmv2_1[16];
196 int ret;
198 infotarget.length = 70;
199 infotarget.data =
200 "\x02\x00\x0c\x00\x54\x00\x45\x00\x53\x00\x54\x00\x4e\x00\x54\x00"
201 "\x01\x00\x0c\x00\x4d\x00\x45\x00\x4d\x00\x42\x00\x45\x00\x52\x00"
202 "\x03\x00\x1e\x00\x6d\x00\x65\x00\x6d\x00\x62\x00\x65\x00\x72\x00"
203 "\x2e\x00\x74\x00\x65\x00\x73\x00\x74\x00\x2e\x00\x63\x00\x6f"
204 "\x00\x6d\x00"
205 "\x00\x00\x00\x00";
207 answer.length = 0;
208 answer.data = NULL;
210 heim_ntlm_nt_key(password, &key);
212 ret = heim_ntlm_calculate_ntlm2(key.data,
213 key.length,
214 username,
215 target,
216 serverchallange,
217 &infotarget,
218 ntlmv2,
219 &answer);
220 if (ret)
221 errx(1, "heim_ntlm_calculate_ntlm2");
223 ret = heim_ntlm_verify_ntlm2(key.data,
224 key.length,
225 username,
226 target,
228 serverchallange,
229 &answer,
230 &infotarget2,
231 ntlmv2_1);
232 if (ret)
233 errx(1, "heim_ntlm_verify_ntlm2");
235 if (memcmp(ntlmv2, ntlmv2_1, sizeof(ntlmv2)) != 0)
236 errx(1, "ntlm master key not same");
238 if (infotarget.length > infotarget2.length)
239 errx(1, "infotarget length");
241 if (memcmp(infotarget.data, infotarget2.data, infotarget.length) != 0)
242 errx(1, "infotarget not the same");
244 free(key.data);
245 free(answer.data);
246 free(infotarget2.data);
248 return 0;
251 static int
252 test_ntlm2_session_resp(void)
254 int ret;
255 struct ntlm_buf lm, ntlm;
257 const unsigned char lm_resp[24] =
258 "\xff\xff\xff\x00\x11\x22\x33\x44"
259 "\x00\x00\x00\x00\x00\x00\x00\x00"
260 "\x00\x00\x00\x00\x00\x00\x00\x00";
261 const unsigned char ntlm2_sess_resp[24] =
262 "\x10\xd5\x50\x83\x2d\x12\xb2\xcc"
263 "\xb7\x9d\x5a\xd1\xf4\xee\xd3\xdf"
264 "\x82\xac\xa4\xc3\x68\x1d\xd4\x55";
266 const unsigned char client_nonce[8] =
267 "\xff\xff\xff\x00\x11\x22\x33\x44";
268 const unsigned char server_challange[8] =
269 "\x01\x23\x45\x67\x89\xab\xcd\xef";
271 const unsigned char ntlm_hash[16] =
272 "\xcd\x06\xca\x7c\x7e\x10\xc9\x9b"
273 "\x1d\x33\xb7\x48\x5a\x2e\xd8\x08";
275 ret = heim_ntlm_calculate_ntlm2_sess(client_nonce,
276 server_challange,
277 ntlm_hash,
278 &lm,
279 &ntlm);
280 if (ret)
281 errx(1, "heim_ntlm_calculate_ntlm2_sess_resp");
283 if (lm.length != 24 || memcmp(lm.data, lm_resp, 24) != 0)
284 errx(1, "lm_resp wrong");
285 if (ntlm.length != 24 || memcmp(ntlm.data, ntlm2_sess_resp, 24) != 0)
286 errx(1, "ntlm2_sess_resp wrong");
288 free(lm.data);
289 free(ntlm.data);
292 return 0;
295 static int version_flag = 0;
296 static int help_flag = 0;
298 static struct getargs args[] = {
299 {"version", 0, arg_flag, &version_flag, "print version", NULL },
300 {"help", 0, arg_flag, &help_flag, NULL, NULL }
303 static void
304 usage (int ret)
306 arg_printusage (args, sizeof(args)/sizeof(*args),
307 NULL, "");
308 exit (ret);
312 main(int argc, char **argv)
314 int ret = 0, optind = 0;
316 setprogname(argv[0]);
318 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
319 usage(1);
321 if (help_flag)
322 usage (0);
324 if(version_flag){
325 print_version(NULL);
326 exit(0);
329 argc -= optind;
330 argv += optind;
332 printf("test_parse\n");
333 ret += test_parse();
334 printf("test_keys\n");
335 ret += test_keys();
336 printf("test_ntlm2_session_resp\n");
337 ret += test_ntlm2_session_resp();
339 return 0;