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