Debian Codegraph @

lib/gssapi/test_context.c

raw
1 /*
2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include "krb5/gsskrb5_locl.h"
35 #include <err.h>
36 #include <getarg.h>
37 #include <gssapi.h>
38 #include <gssapi_krb5.h>
39 #include <gssapi_spnego.h>
40 #include <gssapi_ntlm.h>
41 #include "test_common.h"
42
43 static char *type_string;
44 static char *mech_string;
45 static char *mechs_string;
46 static char *ret_mech_string;
47 static char *client_name;
48 static char *client_password;
49 static int dns_canon_flag = -1;
50 static int mutual_auth_flag = 0;
51 static int dce_style_flag = 0;
52 static int wrapunwrap_flag = 0;
53 static int iov_flag = 0;
54 static int aead_flag = 0;
55 static int getverifymic_flag = 0;
56 static int deleg_flag = 0;
57 static int policy_deleg_flag = 0;
58 static int server_no_deleg_flag = 0;
59 static int ei_flag = 0;
60 static char *gsskrb5_acceptor_identity = NULL;
61 static char *session_enctype_string = NULL;
62 static int client_time_offset = 0;
63 static int server_time_offset = 0;
64 static int max_loops = 0;
65 static char *limit_enctype_string = NULL;
66 static int version_flag = 0;
67 static int verbose_flag = 0;
68 static int help_flag = 0;
69
70 static krb5_context context;
71 static krb5_enctype limit_enctype = 0;
72
73 static struct {
74 const char *name;
75 gss_OID oid;
76 } o2n[] = {
77 { "krb5", NULL /* GSS_KRB5_MECHANISM */ },
78 { "spnego", NULL /* GSS_SPNEGO_MECHANISM */ },
79 { "ntlm", NULL /* GSS_NTLM_MECHANISM */ },
80 { "sasl-digest-md5", NULL /* GSS_SASL_DIGEST_MD5_MECHANISM */ }
81 };
82
83 static void
84 init_o2n(void)
85 {
86 o2n[0].oid = GSS_KRB5_MECHANISM;
87 o2n[1].oid = GSS_SPNEGO_MECHANISM;
88 o2n[2].oid = GSS_NTLM_MECHANISM;
89 o2n[3].oid = GSS_SASL_DIGEST_MD5_MECHANISM;
90 }
91
92 static gss_OID
93 string_to_oid(const char *name)
94 {
95 size_t i;
96 for (i = 0; i < sizeof(o2n)/sizeof(o2n[0]); i++)
97 if (strcasecmp(name, o2n[i].name) == 0)
98 return o2n[i].oid;
99 errx(1, "name '%s' not unknown", name);
100 }
101
102 static void
103 string_to_oids(gss_OID_set *oidsetp, gss_OID_set oidset,
104 gss_OID_desc *oidarray, size_t oidarray_len,
105 char *names)
106 {
107 char *name;
108 char *s;
109
110 if (names[0] == '\0') {
111 *oidsetp = GSS_C_NO_OID_SET;
112 return;
113 }
114
115 oidset->elements = &oidarray[0];
116 if (strcasecmp(names, "all") == 0) {
117 if (sizeof(o2n)/sizeof(o2n[0]) > oidarray_len)
118 errx(1, "internal error: oidarray must be enlarged");
119 for (oidset->count = 0; oidset->count < oidarray_len; oidset->count++)
120 oidset->elements[oidset->count] = *o2n[oidset->count].oid;
121 } else {
122 for (oidset->count = 0, name = strtok_r(names, ", ", &s);
123 name != NULL;
124 oidset->count++, name = strtok_r(NULL, ", ", &s)) {
125 if (oidset->count >= oidarray_len)
126 errx(1, "too many mech names given");
127 oidset->elements[oidset->count] = *string_to_oid(name);
128 }
129 oidset->count = oidset->count;
130 }
131 *oidsetp = oidset;
132 }
133
134 static const char *
135 oid_to_string(const gss_OID oid)
136 {
137 size_t i;
138 for (i = 0; i < sizeof(o2n)/sizeof(o2n[0]); i++)
139 if (gss_oid_equal(oid, o2n[i].oid))
140 return o2n[i].name;
141 return "unknown oid";
142 }
143
144 static void
145 loop(gss_OID mechoid,
146 gss_OID nameoid, const char *target,
147 gss_cred_id_t init_cred,
148 gss_ctx_id_t *sctx, gss_ctx_id_t *cctx,
149 gss_OID *actual_mech,
150 gss_cred_id_t *deleg_cred)
151 {
152 int server_done = 0, client_done = 0;
153 int num_loops = 0;
154 OM_uint32 maj_stat, min_stat;
155 gss_name_t gss_target_name;
156 gss_buffer_desc input_token, output_token;
157 OM_uint32 flags = 0, ret_cflags, ret_sflags;
158 gss_OID actual_mech_client;
159 gss_OID actual_mech_server;
160
161 *actual_mech = GSS_C_NO_OID;
162
163 flags |= GSS_C_INTEG_FLAG;
164 flags |= GSS_C_CONF_FLAG;
165
166 if (mutual_auth_flag)
167 flags |= GSS_C_MUTUAL_FLAG;
168 if (dce_style_flag)
169 flags |= GSS_C_DCE_STYLE;
170 if (deleg_flag)
171 flags |= GSS_C_DELEG_FLAG;
172 if (policy_deleg_flag)
173 flags |= GSS_C_DELEG_POLICY_FLAG;
174
175 input_token.value = rk_UNCONST(target);
176 input_token.length = strlen(target);
177
178 maj_stat = gss_import_name(&min_stat,
179 &input_token,
180 nameoid,
181 &gss_target_name);
182 if (GSS_ERROR(maj_stat))
183 err(1, "import name creds failed with: %d", maj_stat);
184
185 input_token.length = 0;
186 input_token.value = NULL;
187
188 while (!server_done || !client_done) {
189 num_loops++;
190
191 gsskrb5_set_time_offset(client_time_offset);
192
193 maj_stat = gss_init_sec_context(&min_stat,
194 init_cred,
195 cctx,
196 gss_target_name,
197 mechoid,
198 flags,
199 0,
200 NULL,
201 &input_token,
202 &actual_mech_client,
203 &output_token,
204 &ret_cflags,
205 NULL);
206 if (GSS_ERROR(maj_stat))
207 errx(1, "init_sec_context: %s",
208 gssapi_err(maj_stat, min_stat, mechoid));
209 if (maj_stat & GSS_S_CONTINUE_NEEDED)
210 ;
211 else
212 client_done = 1;
213
214 gsskrb5_get_time_offset(&client_time_offset);
215
216 if (client_done && server_done)
217 break;
218
219 if (input_token.length != 0)
220 gss_release_buffer(&min_stat, &input_token);
221
222 gsskrb5_set_time_offset(server_time_offset);
223
224 maj_stat = gss_accept_sec_context(&min_stat,
225 sctx,
226 GSS_C_NO_CREDENTIAL,
227 &output_token,
228 GSS_C_NO_CHANNEL_BINDINGS,
229 NULL,
230 &actual_mech_server,
231 &input_token,
232 &ret_sflags,
233 NULL,
234 deleg_cred);
235 if (GSS_ERROR(maj_stat))
236 errx(1, "accept_sec_context: %s",
237 gssapi_err(maj_stat, min_stat, actual_mech_server));
238
239 gsskrb5_get_time_offset(&server_time_offset);
240
241 if (output_token.length != 0)
242 gss_release_buffer(&min_stat, &output_token);
243
244 if (maj_stat & GSS_S_CONTINUE_NEEDED)
245 ;
246 else
247 server_done = 1;
248 }
249 if (output_token.length != 0)
250 gss_release_buffer(&min_stat, &output_token);
251 if (input_token.length != 0)
252 gss_release_buffer(&min_stat, &input_token);
253 gss_release_name(&min_stat, &gss_target_name);
254
255 if (deleg_flag || policy_deleg_flag) {
256 if (server_no_deleg_flag) {
257 if (*deleg_cred != GSS_C_NO_CREDENTIAL)
258 errx(1, "got delegated cred but didn't expect one");
259 } else if (*deleg_cred == GSS_C_NO_CREDENTIAL)
260 errx(1, "asked for delegarated cred but did get one");
261 } else if (*deleg_cred != GSS_C_NO_CREDENTIAL)
262 errx(1, "got deleg_cred cred but didn't ask");
263
264 if (gss_oid_equal(actual_mech_server, actual_mech_client) == 0)
265 errx(1, "mech mismatch");
266 *actual_mech = actual_mech_server;
267
268 if (max_loops && num_loops > max_loops)
269 errx(1, "num loops %d was lager then max loops %d",
270 num_loops, max_loops);
271
272 if (verbose_flag) {
273 printf("server time offset: %d\n", server_time_offset);
274 printf("client time offset: %d\n", client_time_offset);
275 printf("num loops %d\n", num_loops);
276 }
277 }
278
279 static void
280 wrapunwrap(gss_ctx_id_t cctx, gss_ctx_id_t sctx, int flags, gss_OID mechoid)
281 {
282 gss_buffer_desc input_token, output_token, output_token2;
283 OM_uint32 min_stat, maj_stat;
284 gss_qop_t qop_state;
285 int conf_state;
286
287 input_token.value = "foo";
288 input_token.length = 3;
289
290 maj_stat = gss_wrap(&min_stat, cctx, flags, 0, &input_token,
291 &conf_state, &output_token);
292 if (maj_stat != GSS_S_COMPLETE)
293 errx(1, "gss_wrap failed: %s",
294 gssapi_err(maj_stat, min_stat, mechoid));
295
296 maj_stat = gss_unwrap(&min_stat, sctx, &output_token,
297 &output_token2, &conf_state, &qop_state);
298 if (maj_stat != GSS_S_COMPLETE)
299 errx(1, "gss_unwrap failed: %s",
300 gssapi_err(maj_stat, min_stat, mechoid));
301
302 gss_release_buffer(&min_stat, &output_token);
303 gss_release_buffer(&min_stat, &output_token2);
304
305 #if 0 /* doesn't work for NTLM yet */
306 if (!!conf_state != !!flags)
307 errx(1, "conf_state mismatch");
308 #endif
309 }
310
311 #define USE_CONF 1
312 #define USE_HEADER_ONLY 2
313 #define USE_SIGN_ONLY 4
314 #define FORCE_IOV 8
315
316 static void
317 wrapunwrap_iov(gss_ctx_id_t cctx, gss_ctx_id_t sctx, int flags, gss_OID mechoid)
318 {
319 krb5_data token, header, trailer;
320 OM_uint32 min_stat, maj_stat;
321 gss_qop_t qop_state;
322 int conf_state, conf_state2;
323 gss_iov_buffer_desc iov[6];
324 unsigned char *p;
325 int iov_len;
326 char header_data[9] = "ABCheader";
327 char trailer_data[10] = "trailerXYZ";
328
329 char token_data[16] = "0123456789abcdef";
330
331 memset(&iov, 0, sizeof(iov));
332
333 if (flags & USE_SIGN_ONLY) {
334 header.data = header_data;
335 header.length = 9;
336 trailer.data = trailer_data;
337 trailer.length = 10;
338 } else {
339 header.data = NULL;
340 header.length = 0;
341 trailer.data = NULL;
342 trailer.length = 0;
343 }
344
345 token.data = token_data;
346 token.length = 16;
347
348 iov_len = sizeof(iov)/sizeof(iov[0]);
349
350 memset(iov, 0, sizeof(iov));
351
352 iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
353
354 if (header.length != 0) {
355 iov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
356 iov[1].buffer.length = header.length;
357 iov[1].buffer.value = header.data;
358 } else {
359 iov[1].type = GSS_IOV_BUFFER_TYPE_EMPTY;
360 iov[1].buffer.length = 0;
361 iov[1].buffer.value = NULL;
362 }
363 iov[2].type = GSS_IOV_BUFFER_TYPE_DATA;
364 iov[2].buffer.length = token.length;
365 iov[2].buffer.value = token.data;
366 if (trailer.length != 0) {
367 iov[3].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
368 iov[3].buffer.length = trailer.length;
369 iov[3].buffer.value = trailer.data;
370 } else {
371 iov[3].type = GSS_IOV_BUFFER_TYPE_EMPTY;
372 iov[3].buffer.length = 0;
373 iov[3].buffer.value = NULL;
374 }
375 if (dce_style_flag) {
376 iov[4].type = GSS_IOV_BUFFER_TYPE_EMPTY;
377 } else {
378 iov[4].type = GSS_IOV_BUFFER_TYPE_PADDING | GSS_IOV_BUFFER_FLAG_ALLOCATE;
379 }
380 iov[4].buffer.length = 0;
381 iov[4].buffer.value = 0;
382 if (dce_style_flag) {
383 iov[5].type = GSS_IOV_BUFFER_TYPE_EMPTY;
384 } else if (flags & USE_HEADER_ONLY) {
385 iov[5].type = GSS_IOV_BUFFER_TYPE_EMPTY;
386 } else {
387 iov[5].type = GSS_IOV_BUFFER_TYPE_TRAILER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
388 }
389 iov[5].buffer.length = 0;
390 iov[5].buffer.value = 0;
391
392 maj_stat = gss_wrap_iov(&min_stat, cctx, dce_style_flag || flags & USE_CONF, 0, &conf_state,
393 iov, iov_len);
394 if (maj_stat != GSS_S_COMPLETE)
395 errx(1, "gss_wrap_iov failed");
396
397 token.length =
398 iov[0].buffer.length +
399 iov[1].buffer.length +
400 iov[2].buffer.length +
401 iov[3].buffer.length +
402 iov[4].buffer.length +
403 iov[5].buffer.length;
404 token.data = emalloc(token.length);
405
406 p = token.data;
407
408 if (iov[0].buffer.length)
409 memcpy(p, iov[0].buffer.value, iov[0].buffer.length);
410 p += iov[0].buffer.length;
411
412 if (iov[1].buffer.length)
413 memcpy(p, iov[1].buffer.value, iov[1].buffer.length);
414 p += iov[1].buffer.length;
415
416 if (iov[2].buffer.length)
417 memcpy(p, iov[2].buffer.value, iov[2].buffer.length);
418 p += iov[2].buffer.length;
419
420 if (iov[3].buffer.length)
421 memcpy(p, iov[3].buffer.value, iov[3].buffer.length);
422 p += iov[3].buffer.length;
423
424 if (iov[4].buffer.length)
425 memcpy(p, iov[4].buffer.value, iov[4].buffer.length);
426 p += iov[4].buffer.length;
427
428 if (iov[5].buffer.length)
429 memcpy(p, iov[5].buffer.value, iov[5].buffer.length);
430 p += iov[5].buffer.length;
431
432 assert(p - ((unsigned char *)token.data) == token.length);
433
434 if ((flags & (USE_SIGN_ONLY|FORCE_IOV)) == 0) {
435 gss_buffer_desc input, output;
436
437 input.value = token.data;
438 input.length = token.length;
439
440 maj_stat = gss_unwrap(&min_stat, sctx, &input,
441 &output, &conf_state2, &qop_state);
442
443 if (maj_stat != GSS_S_COMPLETE)
444 errx(1, "gss_unwrap from gss_wrap_iov failed: %s",
445 gssapi_err(maj_stat, min_stat, mechoid));
446
447 gss_release_buffer(&min_stat, &output);
448 } else {
449 maj_stat = gss_unwrap_iov(&min_stat, sctx, &conf_state2, &qop_state,
450 iov, iov_len);
451
452 if (maj_stat != GSS_S_COMPLETE)
453 errx(1, "gss_unwrap_iov failed: %x %s", flags,
454 gssapi_err(maj_stat, min_stat, mechoid));
455
456 }
457 if (conf_state2 != conf_state)
458 errx(1, "conf state wrong for iov: %x", flags);
459
460 gss_release_iov_buffer(&min_stat, iov, iov_len);
461
462 free(token.data);
463 }
464
465 static void
466 wrapunwrap_aead(gss_ctx_id_t cctx, gss_ctx_id_t sctx, int flags, gss_OID mechoid)
467 {
468 gss_buffer_desc token, assoc, message = GSS_C_EMPTY_BUFFER;
469 gss_buffer_desc output;
470 OM_uint32 min_stat, maj_stat;
471 gss_qop_t qop_state;
472 int conf_state, conf_state2;
473 char assoc_data[9] = "ABCheader";
474 char token_data[16] = "0123456789abcdef";
475
476 if (flags & USE_SIGN_ONLY) {
477 assoc.value = assoc_data;
478 assoc.length = 9;
479 } else {
480 assoc.value = NULL;
481 assoc.length = 0;
482 }
483
484 token.value = token_data;
485 token.length = 16;
486
487 maj_stat = gss_wrap_aead(&min_stat, cctx, dce_style_flag || flags & USE_CONF,
488 GSS_C_QOP_DEFAULT, &assoc, &token,
489 &conf_state, &message);
490 if (maj_stat != GSS_S_COMPLETE)
491 errx(1, "gss_wrap_aead failed");
492
493 if ((flags & (USE_SIGN_ONLY|FORCE_IOV)) == 0) {
494 maj_stat = gss_unwrap(&min_stat, sctx, &message,
495 &output, &conf_state2, &qop_state);
496
497 if (maj_stat != GSS_S_COMPLETE)
498 errx(1, "gss_unwrap from gss_wrap_aead failed: %s",
499 gssapi_err(maj_stat, min_stat, mechoid));
500 } else {
501 maj_stat = gss_unwrap_aead(&min_stat, sctx, &message, &assoc,
502 &output, &conf_state2, &qop_state);
503 if (maj_stat != GSS_S_COMPLETE)
504 errx(1, "gss_unwrap_aead failed: %x %s", flags,
505 gssapi_err(maj_stat, min_stat, mechoid));
506 }
507
508 if (output.length != token.length)
509 errx(1, "plaintext length wrong for aead");
510 else if (memcmp(output.value, token.value, token.length) != 0)
511 errx(1, "plaintext wrong for aead");
512 if (conf_state2 != conf_state)
513 errx(1, "conf state wrong for aead: %x", flags);
514
515 gss_release_buffer(&min_stat, &message);
516 gss_release_buffer(&min_stat, &output);
517 }
518
519 static void
520 getverifymic(gss_ctx_id_t cctx, gss_ctx_id_t sctx, gss_OID mechoid)
521 {
522 gss_buffer_desc input_token, output_token;
523 OM_uint32 min_stat, maj_stat;
524 gss_qop_t qop_state;
525
526 input_token.value = "bar";
527 input_token.length = 3;
528
529 maj_stat = gss_get_mic(&min_stat, cctx, 0, &input_token,
530 &output_token);
531 if (maj_stat != GSS_S_COMPLETE)
532 errx(1, "gss_get_mic failed: %s",
533 gssapi_err(maj_stat, min_stat, mechoid));
534
535 maj_stat = gss_verify_mic(&min_stat, sctx, &input_token,
536 &output_token, &qop_state);
537 if (maj_stat != GSS_S_COMPLETE)
538 errx(1, "gss_verify_mic failed: %s",
539 gssapi_err(maj_stat, min_stat, mechoid));
540
541 gss_release_buffer(&min_stat, &output_token);
542 }
543
544 static void
545 empty_release(void)
546 {
547 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
548 gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
549 gss_name_t name = GSS_C_NO_NAME;
550 gss_OID_set oidset = GSS_C_NO_OID_SET;
551 OM_uint32 junk;
552
553 gss_delete_sec_context(&junk, &ctx, NULL);
554 gss_release_cred(&junk, &cred);
555 gss_release_name(&junk, &name);
556 gss_release_oid_set(&junk, &oidset);
557 }
558
559 /*
560 *
561 */
562
563 static struct getargs args[] = {
564 {"name-type",0, arg_string, &type_string, "type of name", NULL },
565 {"mech-type",0, arg_string, &mech_string, "mech type (name)", NULL },
566 {"mech-types",0, arg_string, &mechs_string, "mech types (names)", NULL },
567 {"ret-mech-type",0, arg_string, &ret_mech_string,
568 "type of return mech", NULL },
569 {"dns-canonicalize",0,arg_negative_flag, &dns_canon_flag,
570 "use dns to canonicalize", NULL },
571 {"mutual-auth",0, arg_flag, &mutual_auth_flag,"mutual auth", NULL },
572 {"client-name", 0, arg_string, &client_name, "client name", NULL },
573 {"client-password", 0, arg_string, &client_password, "client password", NULL },
574 {"limit-enctype",0, arg_string, &limit_enctype_string, "enctype", NULL },
575 {"dce-style",0, arg_flag, &dce_style_flag, "dce-style", NULL },
576 {"wrapunwrap",0, arg_flag, &wrapunwrap_flag, "wrap/unwrap", NULL },
577 {"iov", 0, arg_flag, &iov_flag, "wrap/unwrap iov", NULL },
578 {"aead", 0, arg_flag, &aead_flag, "wrap/unwrap aead", NULL },
579 {"getverifymic",0, arg_flag, &getverifymic_flag,
580 "get and verify mic", NULL },
581 {"delegate",0, arg_flag, &deleg_flag, "delegate credential", NULL },
582 {"policy-delegate",0, arg_flag, &policy_deleg_flag, "policy delegate credential", NULL },
583 {"server-no-delegate",0, arg_flag, &server_no_deleg_flag,
584 "server should get a credential", NULL },
585 {"export-import-cred",0, arg_flag, &ei_flag, "test export/import cred", NULL },
586 {"gsskrb5-acceptor-identity", 0, arg_string, &gsskrb5_acceptor_identity, "keytab", NULL },
587 {"session-enctype", 0, arg_string, &session_enctype_string, "enctype", NULL },
588 {"client-time-offset", 0, arg_integer, &client_time_offset, "time", NULL },
589 {"server-time-offset", 0, arg_integer, &server_time_offset, "time", NULL },
590 {"max-loops", 0, arg_integer, &max_loops, "time", NULL },
591 {"version", 0, arg_flag, &version_flag, "print version", NULL },
592 {"verbose", 'v', arg_flag, &verbose_flag, "verbose", NULL },
593 {"help", 0, arg_flag, &help_flag, NULL, NULL }
594 };
595
596 static void
597 usage (int ret)
598 {
599 arg_printusage (args, sizeof(args)/sizeof(*args),
600 NULL, "service@host");
601 exit (ret);
602 }
603
604 int
605 main(int argc, char **argv)
606 {
607 int optidx = 0;
608 OM_uint32 min_stat, maj_stat;
609 gss_ctx_id_t cctx, sctx;
610 void *ctx;
611 gss_OID nameoid, mechoid, actual_mech, actual_mech2;
612 gss_cred_id_t client_cred = GSS_C_NO_CREDENTIAL, deleg_cred = GSS_C_NO_CREDENTIAL;
613 gss_name_t cname = GSS_C_NO_NAME;
614 gss_buffer_desc credential_data = GSS_C_EMPTY_BUFFER;
615 gss_OID_desc oids[4];
616 gss_OID_set_desc mechoid_descs;
617 gss_OID_set mechoids = GSS_C_NO_OID_SET;
618
619 setprogname(argv[0]);
620
621 init_o2n();
622
623 if (krb5_init_context(&context))
624 errx(1, "krb5_init_context");
625
626 cctx = sctx = GSS_C_NO_CONTEXT;
627
628 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
629 usage(1);
630
631 if (help_flag)
632 usage (0);
633
634 if(version_flag){
635 print_version(NULL);
636 exit(0);
637 }
638
639 argc -= optidx;
640 argv += optidx;
641
642 if (argc != 1)
643 usage(1);
644
645 if (dns_canon_flag != -1)
646 gsskrb5_set_dns_canonicalize(dns_canon_flag);
647
648 if (type_string == NULL)
649 nameoid = GSS_C_NT_HOSTBASED_SERVICE;
650 else if (strcmp(type_string, "hostbased-service") == 0)
651 nameoid = GSS_C_NT_HOSTBASED_SERVICE;
652 else if (strcmp(type_string, "krb5-principal-name") == 0)
653 nameoid = GSS_KRB5_NT_PRINCIPAL_NAME;
654 else
655 errx(1, "%s not supported", type_string);
656
657 if (mech_string == NULL)
658 mechoid = GSS_KRB5_MECHANISM;
659 else
660 mechoid = string_to_oid(mech_string);
661
662 if (mechs_string == NULL) {
663 /*
664 * We ought to be able to use the OID set of the one mechanism
665 * OID given. But there's some breakage that conspires to make
666 * that fail though it should succeed:
667 *
668 * - the NTLM gss_acquire_cred() refuses to work with
669 * desired_name == GSS_C_NO_NAME
670 * - gss_acquire_cred() with desired_mechs == GSS_C_NO_OID_SET
671 * does work here because we happen to have Kerberos
672 * credentials in check-ntlm, and the subsequent
673 * gss_init_sec_context() call finds no cred element for NTLM
674 * but plows on anyways, surprisingly enough, and then the
675 * NTLM gss_init_sec_context() just works.
676 *
677 * In summary, there's some breakage in gss_init_sec_context()
678 * and some breakage in NTLM that conspires against us here.
679 *
680 * We work around this in check-ntlm and check-spnego by adding
681 * --client-name=user1@${R} to the invocations of this test
682 * program that require it.
683 */
684 oids[0] = *mechoid;
685 mechoid_descs.elements = &oids[0];
686 mechoid_descs.count = 1;
687 mechoids = &mechoid_descs;
688 } else {
689 string_to_oids(&mechoids, &mechoid_descs,
690 oids, sizeof(oids)/sizeof(oids[0]), mechs_string);
691 }
692
693 if (gsskrb5_acceptor_identity) {
694 maj_stat = gsskrb5_register_acceptor_identity(gsskrb5_acceptor_identity);
695 if (maj_stat)
696 errx(1, "gsskrb5_acceptor_identity: %s",
697 gssapi_err(maj_stat, 0, GSS_C_NO_OID));
698 }
699
700 if (client_password) {
701 credential_data.value = client_password;
702 credential_data.length = strlen(client_password);
703 }
704
705 if (client_name) {
706 gss_buffer_desc cn;
707
708 cn.value = client_name;
709 cn.length = strlen(client_name);
710
711 maj_stat = gss_import_name(&min_stat, &cn, GSS_C_NT_USER_NAME, &cname);
712 if (maj_stat)
713 errx(1, "gss_import_name: %s",
714 gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));
715 }
716
717 if (client_password) {
718 maj_stat = gss_acquire_cred_with_password(&min_stat,
719 cname,
720 &credential_data,
721 GSS_C_INDEFINITE,
722 mechoids,
723 GSS_C_INITIATE,
724 &client_cred,
725 NULL,
726 NULL);
727 if (GSS_ERROR(maj_stat)) {
728 if (mechoids != GSS_C_NO_OID_SET && mechoids->count == 1)
729 mechoid = &mechoids->elements[0];
730 else
731 mechoid = GSS_C_NO_OID;
732 errx(1, "gss_acquire_cred_with_password: %s",
733 gssapi_err(maj_stat, min_stat, mechoid));
734 }
735 } else {
736 maj_stat = gss_acquire_cred(&min_stat,
737 cname,
738 GSS_C_INDEFINITE,
739 mechoids,
740 GSS_C_INITIATE,
741 &client_cred,
742 NULL,
743 NULL);
744 if (GSS_ERROR(maj_stat))
745 errx(1, "gss_acquire_cred: %s",
746 gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));
747 }
748
749 if (limit_enctype_string) {
750 krb5_error_code ret;
751
752 ret = krb5_string_to_enctype(context,
753 limit_enctype_string,
754 &limit_enctype);
755 if (ret)
756 krb5_err(context, 1, ret, "krb5_string_to_enctype");
757 }
758
759
760 if (limit_enctype) {
761 if (client_cred == NULL)
762 errx(1, "client_cred missing");
763
764 maj_stat = gss_krb5_set_allowable_enctypes(&min_stat, client_cred,
765 1, &limit_enctype);
766 if (maj_stat)
767 errx(1, "gss_krb5_set_allowable_enctypes: %s",
768 gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));
769 }
770
771 loop(mechoid, nameoid, argv[0], client_cred,
772 &sctx, &cctx, &actual_mech, &deleg_cred);
773
774 if (verbose_flag)
775 printf("resulting mech: %s\n", oid_to_string(actual_mech));
776
777 if (ret_mech_string) {
778 gss_OID retoid;
779
780 retoid = string_to_oid(ret_mech_string);
781
782 if (gss_oid_equal(retoid, actual_mech) == 0)
783 errx(1, "actual_mech mech is not the expected type %s",
784 ret_mech_string);
785 }
786
787 /* XXX should be actual_mech */
788 if (gss_oid_equal(mechoid, GSS_KRB5_MECHANISM)) {
789 time_t sc_time;
790 gss_buffer_desc authz_data;
791 gss_buffer_desc in, out1, out2;
792 krb5_keyblock *keyblock, *keyblock2;
793 krb5_timestamp now;
794 krb5_error_code ret;
795
796 ret = krb5_timeofday(context, &now);
797 if (ret)
798 errx(1, "krb5_timeofday failed");
799
800 /* client */
801 maj_stat = gss_krb5_export_lucid_sec_context(&min_stat,
802 &cctx,
803 1, /* version */
804 &ctx);
805 if (maj_stat != GSS_S_COMPLETE)
806 errx(1, "gss_krb5_export_lucid_sec_context failed: %s",
807 gssapi_err(maj_stat, min_stat, actual_mech));
808
809
810 maj_stat = gss_krb5_free_lucid_sec_context(&maj_stat, ctx);
811 if (maj_stat != GSS_S_COMPLETE)
812 errx(1, "gss_krb5_free_lucid_sec_context failed: %s",
813 gssapi_err(maj_stat, min_stat, actual_mech));
814
815 /* server */
816 maj_stat = gss_krb5_export_lucid_sec_context(&min_stat,
817 &sctx,
818 1, /* version */
819 &ctx);
820 if (maj_stat != GSS_S_COMPLETE)
821 errx(1, "gss_krb5_export_lucid_sec_context failed: %s",
822 gssapi_err(maj_stat, min_stat, actual_mech));
823 maj_stat = gss_krb5_free_lucid_sec_context(&min_stat, ctx);
824 if (maj_stat != GSS_S_COMPLETE)
825 errx(1, "gss_krb5_free_lucid_sec_context failed: %s",
826 gssapi_err(maj_stat, min_stat, actual_mech));
827
828 maj_stat = gsskrb5_extract_authtime_from_sec_context(&min_stat,
829 sctx,
830 &sc_time);
831 if (maj_stat != GSS_S_COMPLETE)
832 errx(1, "gsskrb5_extract_authtime_from_sec_context failed: %s",
833 gssapi_err(maj_stat, min_stat, actual_mech));
834
835 if (sc_time > now)
836 errx(1, "gsskrb5_extract_authtime_from_sec_context failed: "
837 "time authtime is before now: %ld %ld",
838 (long)sc_time, (long)now);
839
840 maj_stat = gsskrb5_extract_service_keyblock(&min_stat,
841 sctx,
842 &keyblock);
843 if (maj_stat != GSS_S_COMPLETE)
844 errx(1, "gsskrb5_export_service_keyblock failed: %s",
845 gssapi_err(maj_stat, min_stat, actual_mech));
846
847 krb5_free_keyblock(context, keyblock);
848
849 maj_stat = gsskrb5_get_subkey(&min_stat,
850 sctx,
851 &keyblock);
852 if (maj_stat != GSS_S_COMPLETE
853 && (!(maj_stat == GSS_S_FAILURE && min_stat == GSS_KRB5_S_KG_NO_SUBKEY)))
854 errx(1, "gsskrb5_get_subkey server failed: %s",
855 gssapi_err(maj_stat, min_stat, actual_mech));
856
857 if (maj_stat != GSS_S_COMPLETE)
858 keyblock = NULL;
859 else if (limit_enctype && keyblock->keytype != limit_enctype)
860 errx(1, "gsskrb5_get_subkey wrong enctype");
861
862 maj_stat = gsskrb5_get_subkey(&min_stat,
863 cctx,
864 &keyblock2);
865 if (maj_stat != GSS_S_COMPLETE
866 && (!(maj_stat == GSS_S_FAILURE && min_stat == GSS_KRB5_S_KG_NO_SUBKEY)))
867 errx(1, "gsskrb5_get_subkey client failed: %s",
868 gssapi_err(maj_stat, min_stat, actual_mech));
869
870 if (maj_stat != GSS_S_COMPLETE)
871 keyblock2 = NULL;
872 else if (limit_enctype && keyblock->keytype != limit_enctype)
873 errx(1, "gsskrb5_get_subkey wrong enctype");
874
875 if (keyblock || keyblock2) {
876 if (keyblock == NULL)
877 errx(1, "server missing token keyblock");
878 if (keyblock2 == NULL)
879 errx(1, "client missing token keyblock");
880
881 if (keyblock->keytype != keyblock2->keytype)
882 errx(1, "enctype mismatch");
883 if (keyblock->keyvalue.length != keyblock2->keyvalue.length)
884 errx(1, "key length mismatch");
885 if (memcmp(keyblock->keyvalue.data, keyblock2->keyvalue.data,
886 keyblock2->keyvalue.length) != 0)
887 errx(1, "key data mismatch");
888 }
889
890 if (session_enctype_string) {
891 krb5_enctype enctype;
892
893 ret = krb5_string_to_enctype(context,
894 session_enctype_string,
895 &enctype);
896
897 if (ret)
898 krb5_err(context, 1, ret, "krb5_string_to_enctype");
899
900 if (enctype != keyblock->keytype)
901 errx(1, "keytype is not the expected %d != %d",
902 (int)enctype, (int)keyblock2->keytype);
903 }
904
905 if (keyblock)
906 krb5_free_keyblock(context, keyblock);
907 if (keyblock2)
908 krb5_free_keyblock(context, keyblock2);
909
910 maj_stat = gsskrb5_get_initiator_subkey(&min_stat,
911 sctx,
912 &keyblock);
913 if (maj_stat != GSS_S_COMPLETE
914 && (!(maj_stat == GSS_S_FAILURE && min_stat == GSS_KRB5_S_KG_NO_SUBKEY)))
915 errx(1, "gsskrb5_get_initiator_subkey failed: %s",
916 gssapi_err(maj_stat, min_stat, actual_mech));
917
918 if (maj_stat == GSS_S_COMPLETE) {
919
920 if (limit_enctype && keyblock->keytype != limit_enctype)
921 errx(1, "gsskrb5_get_initiator_subkey wrong enctype");
922 krb5_free_keyblock(context, keyblock);
923 }
924
925 maj_stat = gsskrb5_extract_authz_data_from_sec_context(&min_stat,
926 sctx,
927 128,
928 &authz_data);
929 if (maj_stat == GSS_S_COMPLETE)
930 gss_release_buffer(&min_stat, &authz_data);
931
932
933 memset(&out1, 0, sizeof(out1));
934 memset(&out2, 0, sizeof(out2));
935
936 in.value = "foo";
937 in.length = 3;
938
939 gss_pseudo_random(&min_stat, sctx, GSS_C_PRF_KEY_FULL, &in,
940 100, &out1);
941 gss_pseudo_random(&min_stat, cctx, GSS_C_PRF_KEY_FULL, &in,
942 100, &out2);
943
944 if (out1.length != out2.length)
945 errx(1, "prf len mismatch");
946 if (out1.length && memcmp(out1.value, out2.value, out1.length) != 0)
947 errx(1, "prf data mismatch");
948
949 gss_release_buffer(&min_stat, &out1);
950
951 gss_pseudo_random(&min_stat, sctx, GSS_C_PRF_KEY_FULL, &in,
952 100, &out1);
953
954 if (out1.length != out2.length)
955 errx(1, "prf len mismatch");
956 if (out1.length && memcmp(out1.value, out2.value, out1.length) != 0)
957 errx(1, "prf data mismatch");
958
959 gss_release_buffer(&min_stat, &out1);
960 gss_release_buffer(&min_stat, &out2);
961
962 in.value = "bar";
963 in.length = 3;
964
965 gss_pseudo_random(&min_stat, sctx, GSS_C_PRF_KEY_PARTIAL, &in,
966 100, &out1);
967 gss_pseudo_random(&min_stat, cctx, GSS_C_PRF_KEY_PARTIAL, &in,
968 100, &out2);
969
970 if (out1.length != out2.length)
971 errx(1, "prf len mismatch");
972 if (memcmp(out1.value, out2.value, out1.length) != 0)
973 errx(1, "prf data mismatch");
974
975 gss_release_buffer(&min_stat, &out1);
976 gss_release_buffer(&min_stat, &out2);
977
978 wrapunwrap_flag = 1;
979 getverifymic_flag = 1;
980 }
981
982 if (wrapunwrap_flag) {
983 wrapunwrap(cctx, sctx, 0, actual_mech);
984 wrapunwrap(cctx, sctx, 1, actual_mech);
985 wrapunwrap(sctx, cctx, 0, actual_mech);
986 wrapunwrap(sctx, cctx, 1, actual_mech);
987 }
988
989 if (iov_flag) {
990 wrapunwrap_iov(cctx, sctx, 0, actual_mech);
991 wrapunwrap_iov(cctx, sctx, USE_HEADER_ONLY|FORCE_IOV, actual_mech);
992 wrapunwrap_iov(cctx, sctx, USE_HEADER_ONLY, actual_mech);
993 wrapunwrap_iov(cctx, sctx, USE_CONF, actual_mech);
994 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_HEADER_ONLY, actual_mech);
995
996 wrapunwrap_iov(cctx, sctx, FORCE_IOV, actual_mech);
997 wrapunwrap_iov(cctx, sctx, USE_CONF|FORCE_IOV, actual_mech);
998 wrapunwrap_iov(cctx, sctx, USE_HEADER_ONLY|FORCE_IOV, actual_mech);
999 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_HEADER_ONLY|FORCE_IOV, actual_mech);
1000
1001 wrapunwrap_iov(cctx, sctx, USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1002 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1003 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_HEADER_ONLY|USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1004
1005 /* works */
1006 wrapunwrap_iov(cctx, sctx, 0, actual_mech);
1007 wrapunwrap_iov(cctx, sctx, FORCE_IOV, actual_mech);
1008
1009 wrapunwrap_iov(cctx, sctx, USE_CONF, actual_mech);
1010 wrapunwrap_iov(cctx, sctx, USE_CONF|FORCE_IOV, actual_mech);
1011
1012 wrapunwrap_iov(cctx, sctx, USE_SIGN_ONLY, actual_mech);
1013 wrapunwrap_iov(cctx, sctx, USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1014
1015 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_SIGN_ONLY, actual_mech);
1016 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1017
1018 wrapunwrap_iov(cctx, sctx, USE_HEADER_ONLY, actual_mech);
1019 wrapunwrap_iov(cctx, sctx, USE_HEADER_ONLY|FORCE_IOV, actual_mech);
1020
1021 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_HEADER_ONLY, actual_mech);
1022 wrapunwrap_iov(cctx, sctx, USE_CONF|USE_HEADER_ONLY|FORCE_IOV, actual_mech);
1023 }
1024
1025 if (aead_flag) {
1026 wrapunwrap_aead(cctx, sctx, 0, actual_mech);
1027 wrapunwrap_aead(cctx, sctx, USE_CONF, actual_mech);
1028
1029 wrapunwrap_aead(cctx, sctx, FORCE_IOV, actual_mech);
1030 wrapunwrap_aead(cctx, sctx, USE_CONF|FORCE_IOV, actual_mech);
1031
1032 wrapunwrap_aead(cctx, sctx, USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1033 wrapunwrap_aead(cctx, sctx, USE_CONF|USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1034
1035 wrapunwrap_aead(cctx, sctx, 0, actual_mech);
1036 wrapunwrap_aead(cctx, sctx, FORCE_IOV, actual_mech);
1037
1038 wrapunwrap_aead(cctx, sctx, USE_CONF, actual_mech);
1039 wrapunwrap_aead(cctx, sctx, USE_CONF|FORCE_IOV, actual_mech);
1040
1041 wrapunwrap_aead(cctx, sctx, USE_SIGN_ONLY, actual_mech);
1042 wrapunwrap_aead(cctx, sctx, USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1043
1044 wrapunwrap_aead(cctx, sctx, USE_CONF|USE_SIGN_ONLY, actual_mech);
1045 wrapunwrap_aead(cctx, sctx, USE_CONF|USE_SIGN_ONLY|FORCE_IOV, actual_mech);
1046 }
1047
1048 if (getverifymic_flag) {
1049 getverifymic(cctx, sctx, actual_mech);
1050 getverifymic(cctx, sctx, actual_mech);
1051 getverifymic(sctx, cctx, actual_mech);
1052 getverifymic(sctx, cctx, actual_mech);
1053 }
1054
1055
1056 gss_delete_sec_context(&min_stat, &cctx, NULL);
1057 gss_delete_sec_context(&min_stat, &sctx, NULL);
1058
1059 if (deleg_cred != GSS_C_NO_CREDENTIAL) {
1060 gss_cred_id_t cred2 = GSS_C_NO_CREDENTIAL;
1061 gss_buffer_desc cb;
1062
1063 if (verbose_flag)
1064 printf("checking actual mech (%s) on delegated cred\n",
1065 oid_to_string(actual_mech));
1066 loop(actual_mech, nameoid, argv[0], deleg_cred, &sctx, &cctx, &actual_mech2, &cred2);
1067
1068 gss_delete_sec_context(&min_stat, &cctx, NULL);
1069 gss_delete_sec_context(&min_stat, &sctx, NULL);
1070
1071 gss_release_cred(&min_stat, &cred2);
1072
1073 #if 0
1074 /*
1075 * XXX We can't do this. Delegated credentials only work with
1076 * the actual_mech. We could gss_store_cred the delegated
1077 * credentials *then* gss_add/acquire_cred() with SPNEGO, then
1078 * we could try loop() with those credentials.
1079 */
1080 /* try again using SPNEGO */
1081 if (verbose_flag)
1082 printf("checking spnego on delegated cred\n");
1083 loop(GSS_SPNEGO_MECHANISM, nameoid, argv[0], deleg_cred, &sctx, &cctx,
1084 &actual_mech2, &cred2);
1085
1086 gss_delete_sec_context(&min_stat, &cctx, NULL);
1087 gss_delete_sec_context(&min_stat, &sctx, NULL);
1088
1089 gss_release_cred(&min_stat, &cred2);
1090 #endif
1091
1092 /* check export/import */
1093 if (ei_flag) {
1094
1095 maj_stat = gss_export_cred(&min_stat, deleg_cred, &cb);
1096 if (maj_stat != GSS_S_COMPLETE)
1097 errx(1, "export failed: %s",
1098 gssapi_err(maj_stat, min_stat, NULL));
1099
1100 maj_stat = gss_import_cred(&min_stat, &cb, &cred2);
1101 if (maj_stat != GSS_S_COMPLETE)
1102 errx(1, "import failed: %s",
1103 gssapi_err(maj_stat, min_stat, NULL));
1104
1105 gss_release_buffer(&min_stat, &cb);
1106 gss_release_cred(&min_stat, &deleg_cred);
1107
1108 if (verbose_flag)
1109 printf("checking actual mech (%s) on export/imported cred\n",
1110 oid_to_string(actual_mech));
1111 loop(actual_mech, nameoid, argv[0], cred2, &sctx, &cctx,
1112 &actual_mech2, &deleg_cred);
1113
1114 gss_release_cred(&min_stat, &deleg_cred);
1115
1116 gss_delete_sec_context(&min_stat, &cctx, NULL);
1117 gss_delete_sec_context(&min_stat, &sctx, NULL);
1118
1119 #if 0
1120 /* XXX See above */
1121 /* try again using SPNEGO */
1122 if (verbose_flag)
1123 printf("checking SPNEGO on export/imported cred\n");
1124 loop(GSS_SPNEGO_MECHANISM, nameoid, argv[0], cred2, &sctx, &cctx,
1125 &actual_mech2, &deleg_cred);
1126
1127 gss_release_cred(&min_stat, &deleg_cred);
1128
1129 gss_delete_sec_context(&min_stat, &cctx, NULL);
1130 gss_delete_sec_context(&min_stat, &sctx, NULL);
1131 #endif
1132
1133 gss_release_cred(&min_stat, &cred2);
1134
1135 } else {
1136 gss_release_cred(&min_stat, &deleg_cred);
1137 }
1138
1139 }
1140
1141 empty_release();
1142
1143 krb5_free_context(context);
1144
1145 return 0;
1146 }