1*ebfedea0SLionel Sambuc /* $NetBSD: test_ntlm.c,v 1.1.1.1 2011/04/13 18:14:44 elric Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc * All rights reserved.
7*ebfedea0SLionel Sambuc *
8*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc * are met:
11*ebfedea0SLionel Sambuc *
12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc *
15*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc *
19*ebfedea0SLionel Sambuc * 3. Neither the name of KTH nor the names of its contributors may be
20*ebfedea0SLionel Sambuc * used to endorse or promote products derived from this software without
21*ebfedea0SLionel Sambuc * specific prior written permission.
22*ebfedea0SLionel Sambuc *
23*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24*ebfedea0SLionel Sambuc * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26*ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27*ebfedea0SLionel Sambuc * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*ebfedea0SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*ebfedea0SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30*ebfedea0SLionel Sambuc * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31*ebfedea0SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32*ebfedea0SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33*ebfedea0SLionel Sambuc * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*ebfedea0SLionel Sambuc */
35*ebfedea0SLionel Sambuc
36*ebfedea0SLionel Sambuc #include "config.h"
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel Sambuc #include <krb5/roken.h>
39*ebfedea0SLionel Sambuc #include <stdio.h>
40*ebfedea0SLionel Sambuc #include <gssapi/gssapi.h>
41*ebfedea0SLionel Sambuc #include <err.h>
42*ebfedea0SLionel Sambuc #include <krb5/getarg.h>
43*ebfedea0SLionel Sambuc #include "test_common.h"
44*ebfedea0SLionel Sambuc
45*ebfedea0SLionel Sambuc #include <krb5/krb5.h>
46*ebfedea0SLionel Sambuc #include <krb5/heimntlm.h>
47*ebfedea0SLionel Sambuc
48*ebfedea0SLionel Sambuc static int
test_libntlm_v1(int flags)49*ebfedea0SLionel Sambuc test_libntlm_v1(int flags)
50*ebfedea0SLionel Sambuc {
51*ebfedea0SLionel Sambuc const char *user = "foo",
52*ebfedea0SLionel Sambuc *domain = "mydomain",
53*ebfedea0SLionel Sambuc *password = "digestpassword";
54*ebfedea0SLionel Sambuc OM_uint32 maj_stat, min_stat;
55*ebfedea0SLionel Sambuc gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
56*ebfedea0SLionel Sambuc gss_buffer_desc input, output;
57*ebfedea0SLionel Sambuc struct ntlm_type1 type1;
58*ebfedea0SLionel Sambuc struct ntlm_type2 type2;
59*ebfedea0SLionel Sambuc struct ntlm_type3 type3;
60*ebfedea0SLionel Sambuc struct ntlm_buf data;
61*ebfedea0SLionel Sambuc krb5_error_code ret;
62*ebfedea0SLionel Sambuc gss_name_t src_name = GSS_C_NO_NAME;
63*ebfedea0SLionel Sambuc
64*ebfedea0SLionel Sambuc memset(&type1, 0, sizeof(type1));
65*ebfedea0SLionel Sambuc memset(&type2, 0, sizeof(type2));
66*ebfedea0SLionel Sambuc memset(&type3, 0, sizeof(type3));
67*ebfedea0SLionel Sambuc
68*ebfedea0SLionel Sambuc type1.flags = NTLM_NEG_UNICODE|NTLM_NEG_TARGET|NTLM_NEG_NTLM|flags;
69*ebfedea0SLionel Sambuc type1.domain = strdup(domain);
70*ebfedea0SLionel Sambuc type1.hostname = NULL;
71*ebfedea0SLionel Sambuc type1.os[0] = 0;
72*ebfedea0SLionel Sambuc type1.os[1] = 0;
73*ebfedea0SLionel Sambuc
74*ebfedea0SLionel Sambuc ret = heim_ntlm_encode_type1(&type1, &data);
75*ebfedea0SLionel Sambuc if (ret)
76*ebfedea0SLionel Sambuc errx(1, "heim_ntlm_encode_type1");
77*ebfedea0SLionel Sambuc
78*ebfedea0SLionel Sambuc input.value = data.data;
79*ebfedea0SLionel Sambuc input.length = data.length;
80*ebfedea0SLionel Sambuc
81*ebfedea0SLionel Sambuc output.length = 0;
82*ebfedea0SLionel Sambuc output.value = NULL;
83*ebfedea0SLionel Sambuc
84*ebfedea0SLionel Sambuc maj_stat = gss_accept_sec_context(&min_stat,
85*ebfedea0SLionel Sambuc &ctx,
86*ebfedea0SLionel Sambuc GSS_C_NO_CREDENTIAL,
87*ebfedea0SLionel Sambuc &input,
88*ebfedea0SLionel Sambuc GSS_C_NO_CHANNEL_BINDINGS,
89*ebfedea0SLionel Sambuc NULL,
90*ebfedea0SLionel Sambuc NULL,
91*ebfedea0SLionel Sambuc &output,
92*ebfedea0SLionel Sambuc NULL,
93*ebfedea0SLionel Sambuc NULL,
94*ebfedea0SLionel Sambuc NULL);
95*ebfedea0SLionel Sambuc free(data.data);
96*ebfedea0SLionel Sambuc if (GSS_ERROR(maj_stat))
97*ebfedea0SLionel Sambuc errx(1, "accept_sec_context v1: %s",
98*ebfedea0SLionel Sambuc gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));
99*ebfedea0SLionel Sambuc
100*ebfedea0SLionel Sambuc if (output.length == 0)
101*ebfedea0SLionel Sambuc errx(1, "output.length == 0");
102*ebfedea0SLionel Sambuc
103*ebfedea0SLionel Sambuc data.data = output.value;
104*ebfedea0SLionel Sambuc data.length = output.length;
105*ebfedea0SLionel Sambuc
106*ebfedea0SLionel Sambuc ret = heim_ntlm_decode_type2(&data, &type2);
107*ebfedea0SLionel Sambuc if (ret)
108*ebfedea0SLionel Sambuc errx(1, "heim_ntlm_decode_type2");
109*ebfedea0SLionel Sambuc
110*ebfedea0SLionel Sambuc gss_release_buffer(&min_stat, &output);
111*ebfedea0SLionel Sambuc
112*ebfedea0SLionel Sambuc type3.flags = type2.flags;
113*ebfedea0SLionel Sambuc type3.username = rk_UNCONST(user);
114*ebfedea0SLionel Sambuc type3.targetname = type2.targetname;
115*ebfedea0SLionel Sambuc type3.ws = rk_UNCONST("workstation");
116*ebfedea0SLionel Sambuc
117*ebfedea0SLionel Sambuc {
118*ebfedea0SLionel Sambuc struct ntlm_buf key;
119*ebfedea0SLionel Sambuc
120*ebfedea0SLionel Sambuc heim_ntlm_nt_key(password, &key);
121*ebfedea0SLionel Sambuc
122*ebfedea0SLionel Sambuc heim_ntlm_calculate_ntlm1(key.data, key.length,
123*ebfedea0SLionel Sambuc type2.challenge,
124*ebfedea0SLionel Sambuc &type3.ntlm);
125*ebfedea0SLionel Sambuc
126*ebfedea0SLionel Sambuc if (flags & NTLM_NEG_KEYEX) {
127*ebfedea0SLionel Sambuc struct ntlm_buf sessionkey;
128*ebfedea0SLionel Sambuc heim_ntlm_build_ntlm1_master(key.data, key.length,
129*ebfedea0SLionel Sambuc &sessionkey,
130*ebfedea0SLionel Sambuc &type3.sessionkey);
131*ebfedea0SLionel Sambuc free(sessionkey.data);
132*ebfedea0SLionel Sambuc }
133*ebfedea0SLionel Sambuc free(key.data);
134*ebfedea0SLionel Sambuc }
135*ebfedea0SLionel Sambuc
136*ebfedea0SLionel Sambuc ret = heim_ntlm_encode_type3(&type3, &data);
137*ebfedea0SLionel Sambuc if (ret)
138*ebfedea0SLionel Sambuc errx(1, "heim_ntlm_encode_type3");
139*ebfedea0SLionel Sambuc
140*ebfedea0SLionel Sambuc input.length = data.length;
141*ebfedea0SLionel Sambuc input.value = data.data;
142*ebfedea0SLionel Sambuc
143*ebfedea0SLionel Sambuc maj_stat = gss_accept_sec_context(&min_stat,
144*ebfedea0SLionel Sambuc &ctx,
145*ebfedea0SLionel Sambuc GSS_C_NO_CREDENTIAL,
146*ebfedea0SLionel Sambuc &input,
147*ebfedea0SLionel Sambuc GSS_C_NO_CHANNEL_BINDINGS,
148*ebfedea0SLionel Sambuc &src_name,
149*ebfedea0SLionel Sambuc NULL,
150*ebfedea0SLionel Sambuc &output,
151*ebfedea0SLionel Sambuc NULL,
152*ebfedea0SLionel Sambuc NULL,
153*ebfedea0SLionel Sambuc NULL);
154*ebfedea0SLionel Sambuc free(input.value);
155*ebfedea0SLionel Sambuc if (maj_stat != GSS_S_COMPLETE)
156*ebfedea0SLionel Sambuc errx(1, "accept_sec_context v1 2 %s",
157*ebfedea0SLionel Sambuc gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));
158*ebfedea0SLionel Sambuc
159*ebfedea0SLionel Sambuc gss_release_buffer(&min_stat, &output);
160*ebfedea0SLionel Sambuc gss_delete_sec_context(&min_stat, &ctx, NULL);
161*ebfedea0SLionel Sambuc
162*ebfedea0SLionel Sambuc if (src_name == GSS_C_NO_NAME)
163*ebfedea0SLionel Sambuc errx(1, "no source name!");
164*ebfedea0SLionel Sambuc
165*ebfedea0SLionel Sambuc gss_display_name(&min_stat, src_name, &output, NULL);
166*ebfedea0SLionel Sambuc
167*ebfedea0SLionel Sambuc printf("src_name: %.*s\n", (int)output.length, (char*)output.value);
168*ebfedea0SLionel Sambuc
169*ebfedea0SLionel Sambuc gss_release_name(&min_stat, &src_name);
170*ebfedea0SLionel Sambuc gss_release_buffer(&min_stat, &output);
171*ebfedea0SLionel Sambuc
172*ebfedea0SLionel Sambuc return 0;
173*ebfedea0SLionel Sambuc }
174*ebfedea0SLionel Sambuc
175*ebfedea0SLionel Sambuc static int
test_libntlm_v2(int flags)176*ebfedea0SLionel Sambuc test_libntlm_v2(int flags)
177*ebfedea0SLionel Sambuc {
178*ebfedea0SLionel Sambuc const char *user = "foo",
179*ebfedea0SLionel Sambuc *domain = "mydomain",
180*ebfedea0SLionel Sambuc *password = "digestpassword";
181*ebfedea0SLionel Sambuc OM_uint32 maj_stat, min_stat;
182*ebfedea0SLionel Sambuc gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
183*ebfedea0SLionel Sambuc gss_buffer_desc input, output;
184*ebfedea0SLionel Sambuc struct ntlm_type1 type1;
185*ebfedea0SLionel Sambuc struct ntlm_type2 type2;
186*ebfedea0SLionel Sambuc struct ntlm_type3 type3;
187*ebfedea0SLionel Sambuc struct ntlm_buf data;
188*ebfedea0SLionel Sambuc krb5_error_code ret;
189*ebfedea0SLionel Sambuc
190*ebfedea0SLionel Sambuc memset(&type1, 0, sizeof(type1));
191*ebfedea0SLionel Sambuc memset(&type2, 0, sizeof(type2));
192*ebfedea0SLionel Sambuc memset(&type3, 0, sizeof(type3));
193*ebfedea0SLionel Sambuc
194*ebfedea0SLionel Sambuc type1.flags = NTLM_NEG_UNICODE|NTLM_NEG_NTLM|flags;
195*ebfedea0SLionel Sambuc type1.domain = strdup(domain);
196*ebfedea0SLionel Sambuc type1.hostname = NULL;
197*ebfedea0SLionel Sambuc type1.os[0] = 0;
198*ebfedea0SLionel Sambuc type1.os[1] = 0;
199*ebfedea0SLionel Sambuc
200*ebfedea0SLionel Sambuc ret = heim_ntlm_encode_type1(&type1, &data);
201*ebfedea0SLionel Sambuc if (ret)
202*ebfedea0SLionel Sambuc errx(1, "heim_ntlm_encode_type1");
203*ebfedea0SLionel Sambuc
204*ebfedea0SLionel Sambuc input.value = data.data;
205*ebfedea0SLionel Sambuc input.length = data.length;
206*ebfedea0SLionel Sambuc
207*ebfedea0SLionel Sambuc output.length = 0;
208*ebfedea0SLionel Sambuc output.value = NULL;
209*ebfedea0SLionel Sambuc
210*ebfedea0SLionel Sambuc maj_stat = gss_accept_sec_context(&min_stat,
211*ebfedea0SLionel Sambuc &ctx,
212*ebfedea0SLionel Sambuc GSS_C_NO_CREDENTIAL,
213*ebfedea0SLionel Sambuc &input,
214*ebfedea0SLionel Sambuc GSS_C_NO_CHANNEL_BINDINGS,
215*ebfedea0SLionel Sambuc NULL,
216*ebfedea0SLionel Sambuc NULL,
217*ebfedea0SLionel Sambuc &output,
218*ebfedea0SLionel Sambuc NULL,
219*ebfedea0SLionel Sambuc NULL,
220*ebfedea0SLionel Sambuc NULL);
221*ebfedea0SLionel Sambuc free(data.data);
222*ebfedea0SLionel Sambuc if (GSS_ERROR(maj_stat))
223*ebfedea0SLionel Sambuc errx(1, "accept_sec_context v2 %s",
224*ebfedea0SLionel Sambuc gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));
225*ebfedea0SLionel Sambuc
226*ebfedea0SLionel Sambuc if (output.length == 0)
227*ebfedea0SLionel Sambuc errx(1, "output.length == 0");
228*ebfedea0SLionel Sambuc
229*ebfedea0SLionel Sambuc data.data = output.value;
230*ebfedea0SLionel Sambuc data.length = output.length;
231*ebfedea0SLionel Sambuc
232*ebfedea0SLionel Sambuc ret = heim_ntlm_decode_type2(&data, &type2);
233*ebfedea0SLionel Sambuc if (ret)
234*ebfedea0SLionel Sambuc errx(1, "heim_ntlm_decode_type2");
235*ebfedea0SLionel Sambuc
236*ebfedea0SLionel Sambuc type3.flags = type2.flags;
237*ebfedea0SLionel Sambuc type3.username = rk_UNCONST(user);
238*ebfedea0SLionel Sambuc type3.targetname = type2.targetname;
239*ebfedea0SLionel Sambuc type3.ws = rk_UNCONST("workstation");
240*ebfedea0SLionel Sambuc
241*ebfedea0SLionel Sambuc {
242*ebfedea0SLionel Sambuc struct ntlm_buf key;
243*ebfedea0SLionel Sambuc unsigned char ntlmv2[16];
244*ebfedea0SLionel Sambuc
245*ebfedea0SLionel Sambuc heim_ntlm_nt_key(password, &key);
246*ebfedea0SLionel Sambuc
247*ebfedea0SLionel Sambuc heim_ntlm_calculate_ntlm2(key.data, key.length,
248*ebfedea0SLionel Sambuc user,
249*ebfedea0SLionel Sambuc type2.targetname,
250*ebfedea0SLionel Sambuc type2.challenge,
251*ebfedea0SLionel Sambuc &type2.targetinfo,
252*ebfedea0SLionel Sambuc ntlmv2,
253*ebfedea0SLionel Sambuc &type3.ntlm);
254*ebfedea0SLionel Sambuc free(key.data);
255*ebfedea0SLionel Sambuc
256*ebfedea0SLionel Sambuc if (flags & NTLM_NEG_KEYEX) {
257*ebfedea0SLionel Sambuc struct ntlm_buf sessionkey;
258*ebfedea0SLionel Sambuc heim_ntlm_build_ntlm1_master(ntlmv2, sizeof(ntlmv2),
259*ebfedea0SLionel Sambuc &sessionkey,
260*ebfedea0SLionel Sambuc &type3.sessionkey);
261*ebfedea0SLionel Sambuc free(sessionkey.data);
262*ebfedea0SLionel Sambuc }
263*ebfedea0SLionel Sambuc }
264*ebfedea0SLionel Sambuc
265*ebfedea0SLionel Sambuc ret = heim_ntlm_encode_type3(&type3, &data);
266*ebfedea0SLionel Sambuc if (ret)
267*ebfedea0SLionel Sambuc errx(1, "heim_ntlm_encode_type3");
268*ebfedea0SLionel Sambuc
269*ebfedea0SLionel Sambuc input.length = data.length;
270*ebfedea0SLionel Sambuc input.value = data.data;
271*ebfedea0SLionel Sambuc
272*ebfedea0SLionel Sambuc maj_stat = gss_accept_sec_context(&min_stat,
273*ebfedea0SLionel Sambuc &ctx,
274*ebfedea0SLionel Sambuc GSS_C_NO_CREDENTIAL,
275*ebfedea0SLionel Sambuc &input,
276*ebfedea0SLionel Sambuc GSS_C_NO_CHANNEL_BINDINGS,
277*ebfedea0SLionel Sambuc NULL,
278*ebfedea0SLionel Sambuc NULL,
279*ebfedea0SLionel Sambuc &output,
280*ebfedea0SLionel Sambuc NULL,
281*ebfedea0SLionel Sambuc NULL,
282*ebfedea0SLionel Sambuc NULL);
283*ebfedea0SLionel Sambuc free(input.value);
284*ebfedea0SLionel Sambuc if (maj_stat != GSS_S_COMPLETE)
285*ebfedea0SLionel Sambuc errx(1, "accept_sec_context v2 2 %s",
286*ebfedea0SLionel Sambuc gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));
287*ebfedea0SLionel Sambuc
288*ebfedea0SLionel Sambuc gss_delete_sec_context(&min_stat, &ctx, NULL);
289*ebfedea0SLionel Sambuc
290*ebfedea0SLionel Sambuc return 0;
291*ebfedea0SLionel Sambuc }
292*ebfedea0SLionel Sambuc
293*ebfedea0SLionel Sambuc
294*ebfedea0SLionel Sambuc
295*ebfedea0SLionel Sambuc static int version_flag = 0;
296*ebfedea0SLionel Sambuc static int help_flag = 0;
297*ebfedea0SLionel Sambuc
298*ebfedea0SLionel Sambuc static struct getargs args[] = {
299*ebfedea0SLionel Sambuc {"version", 0, arg_flag, &version_flag, "print version", NULL },
300*ebfedea0SLionel Sambuc {"help", 0, arg_flag, &help_flag, NULL, NULL }
301*ebfedea0SLionel Sambuc };
302*ebfedea0SLionel Sambuc
303*ebfedea0SLionel Sambuc static void
usage(int ret)304*ebfedea0SLionel Sambuc usage (int ret)
305*ebfedea0SLionel Sambuc {
306*ebfedea0SLionel Sambuc arg_printusage (args, sizeof(args)/sizeof(*args),
307*ebfedea0SLionel Sambuc NULL, "");
308*ebfedea0SLionel Sambuc exit (ret);
309*ebfedea0SLionel Sambuc }
310*ebfedea0SLionel Sambuc
311*ebfedea0SLionel Sambuc int
main(int argc,char ** argv)312*ebfedea0SLionel Sambuc main(int argc, char **argv)
313*ebfedea0SLionel Sambuc {
314*ebfedea0SLionel Sambuc int ret = 0, optind = 0;
315*ebfedea0SLionel Sambuc
316*ebfedea0SLionel Sambuc setprogname(argv[0]);
317*ebfedea0SLionel Sambuc
318*ebfedea0SLionel Sambuc if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
319*ebfedea0SLionel Sambuc usage(1);
320*ebfedea0SLionel Sambuc
321*ebfedea0SLionel Sambuc if (help_flag)
322*ebfedea0SLionel Sambuc usage (0);
323*ebfedea0SLionel Sambuc
324*ebfedea0SLionel Sambuc if(version_flag){
325*ebfedea0SLionel Sambuc print_version(NULL);
326*ebfedea0SLionel Sambuc exit(0);
327*ebfedea0SLionel Sambuc }
328*ebfedea0SLionel Sambuc
329*ebfedea0SLionel Sambuc argc -= optind;
330*ebfedea0SLionel Sambuc argv += optind;
331*ebfedea0SLionel Sambuc
332*ebfedea0SLionel Sambuc ret += test_libntlm_v1(0);
333*ebfedea0SLionel Sambuc ret += test_libntlm_v1(NTLM_NEG_KEYEX);
334*ebfedea0SLionel Sambuc
335*ebfedea0SLionel Sambuc ret += test_libntlm_v2(0);
336*ebfedea0SLionel Sambuc ret += test_libntlm_v2(NTLM_NEG_KEYEX);
337*ebfedea0SLionel Sambuc
338*ebfedea0SLionel Sambuc return 0;
339*ebfedea0SLionel Sambuc }
340