1/* 2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * Portions Copyright (c) 2004 PADL Software Pty Ltd. 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 the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include "spnego_locl.h" 35 36#define GKIS(name) \ 37static \ 38OM_uint32 name(OM_uint32 *, gss_cred_id_t, gssspnego_ctx, \ 39 gss_name_t, const gss_OID, \ 40 OM_uint32, OM_uint32, const gss_channel_bindings_t, \ 41 const gss_buffer_t, gss_buffer_t, \ 42 OM_uint32 *, OM_uint32 *) 43 44GKIS(spnego_initial); 45GKIS(spnego_reply); 46GKIS(wait_server_mic); 47GKIS(step_completed); 48 49 50 51/* 52 * Is target_name an sane target for `mech´. 53 */ 54 55struct mech_selection { 56 OM_uint32 req_flags; 57 gss_name_t target_name; 58 OM_uint32 time_req; 59 gss_channel_bindings_t input_chan_bindings; 60 /* out */ 61 gss_OID preferred_mech_type; 62 gss_OID negotiated_mech_type; 63 gss_buffer_desc optimistic_token; 64 OM_uint32 optimistic_flags; 65 gss_ctx_id_t gssctx; 66 int complete; 67}; 68 69 70static OM_uint32 71initiator_approved(void *userptr, 72 gss_name_t target_name, 73 const gss_cred_id_t cred, 74 gss_OID mech) 75{ 76 OM_uint32 min_stat, maj_stat; 77 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT; 78 gss_buffer_desc out; 79 struct mech_selection *sel = userptr; 80 gss_OID negotiated_mech_type = GSS_C_NO_OID; 81 OM_uint32 flags = 0; 82 83 maj_stat = gss_init_sec_context(&min_stat, 84 cred, 85 &ctx, 86 sel->target_name, 87 mech, 88 sel->req_flags, 89 sel->time_req, 90 sel->input_chan_bindings, 91 GSS_C_NO_BUFFER, 92 &negotiated_mech_type, 93 &out, 94 &flags, 95 NULL); 96 if (GSS_ERROR(maj_stat)) { 97 gss_mg_collect_error(mech, GSS_S_BAD_MECH, min_stat); 98 return GSS_S_BAD_MECH; 99 } 100 if (sel->preferred_mech_type == NULL) { 101 sel->preferred_mech_type = mech; 102 sel->negotiated_mech_type = negotiated_mech_type; 103 sel->optimistic_token = out; 104 sel->optimistic_flags = flags; 105 sel->gssctx = ctx; 106 if (maj_stat == GSS_S_COMPLETE) 107 sel->complete = 1; 108 } else { 109 gss_release_buffer(&min_stat, &out); 110 gss_delete_sec_context(&min_stat, &ctx, NULL); 111 } 112 113 return GSS_S_COMPLETE; 114} 115 116/* 117 * Send a reply. Note that we only need to send a reply if we 118 * need to send a MIC or a mechanism token. Otherwise, we can 119 * return an empty buffer. 120 * 121 * The return value of this will be returned to the API, so it 122 * must return GSS_S_CONTINUE_NEEDED if a token was generated. 123 */ 124static OM_uint32 125make_reply(OM_uint32 *minor_status, 126 gssspnego_ctx ctx, 127 gss_buffer_t mech_token, 128 gss_buffer_t output_token) 129{ 130 NegotiationToken nt; 131 gss_buffer_desc mic_buf; 132 OM_uint32 ret; 133 size_t size; 134 NegResultEnum result; 135 136 memset(&nt, 0, sizeof(nt)); 137 138 nt.element = choice_NegotiationToken_negTokenResp; 139 nt.u.negTokenResp.negResult = NULL; 140 nt.u.negTokenResp.supportedMech = NULL; 141 142 output_token->length = 0; 143 output_token->value = NULL; 144 145 /* figure out our status */ 146 147 if (ctx->flags.open) { 148 if (ctx->flags.verified_mic == 1 || ctx->flags.require_mic == 0) 149 result = accept_completed; 150 else 151 result = accept_incomplete; 152 } else { 153 result = accept_incomplete; 154 } 155 156 if (mech_token->length == 0) { 157 nt.u.negTokenResp.responseToken = NULL; 158 } else { 159 ALLOC(nt.u.negTokenResp.responseToken, 1); 160 if (nt.u.negTokenResp.responseToken == NULL) { 161 free_NegotiationToken(&nt); 162 *minor_status = ENOMEM; 163 return GSS_S_FAILURE; 164 } 165 nt.u.negTokenResp.responseToken->length = mech_token->length; 166 nt.u.negTokenResp.responseToken->data = mech_token->value; 167 mech_token->length = 0; 168 mech_token->value = NULL; 169 } 170 171 /* 172 * XXX should limit when we send the MIC ? 173 */ 174 175 if (ctx->flags.open && ctx->flags.sent_mic == 0) { 176 177 ctx->flags.sent_mic = 1; 178 179 ret = gss_get_mic(minor_status, 180 ctx->negotiated_ctx_id, 181 0, 182 &ctx->NegTokenInit_mech_types, 183 &mic_buf); 184 if (ret == GSS_S_COMPLETE) { 185 ALLOC(nt.u.negTokenResp.mechListMIC, 1); 186 if (nt.u.negTokenResp.mechListMIC == NULL) { 187 gss_release_buffer(minor_status, &mic_buf); 188 free_NegotiationToken(&nt); 189 *minor_status = ENOMEM; 190 return GSS_S_FAILURE; 191 } 192 193 nt.u.negTokenResp.mechListMIC->length = mic_buf.length; 194 nt.u.negTokenResp.mechListMIC->data = mic_buf.value; 195 /* mic_buf free()d with nt */ 196 } else if (ret == GSS_S_UNAVAILABLE) { 197 /* lets hope that its ok to not send te mechListMIC for broken mechs */ 198 nt.u.negTokenResp.mechListMIC = NULL; 199 ctx->flags.require_mic = 0; 200 } else { 201 free_NegotiationToken(&nt); 202 *minor_status = ENOMEM; 203 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 204 ret, *minor_status, 205 "SPNEGO failed to sign MIC"); 206 } 207 } else { 208 nt.u.negTokenResp.mechListMIC = NULL; 209 } 210 211 /* 212 * If this is NTLM we have negotiated, don't include the negResult 213 * since Samba 3.0.28a (Mac OS X SnowLeopard) can't handle them. 214 * 215 * But since we need accept_completed for modern windows that uses 216 * MIC, we need send this for NTLM, only do that when we are a 217 * session that is not accept_completed. 218 */ 219 220 if (gss_oid_equal(ctx->negotiated_mech_type, GSS_NTLM_MECHANISM) == 0) { 221 222 ALLOC(nt.u.negTokenResp.negResult, 1); 223 if (nt.u.negTokenResp.negResult == NULL) { 224 free_NegotiationToken(&nt); 225 *minor_status = ENOMEM; 226 return GSS_S_FAILURE; 227 } 228 *nt.u.negTokenResp.negResult = result; 229 } 230 231 ASN1_MALLOC_ENCODE(NegotiationToken, 232 output_token->value, output_token->length, 233 &nt, &size, ret); 234 free_NegotiationToken(&nt); 235 if (ret) { 236 *minor_status = ret; 237 return GSS_S_FAILURE; 238 } 239 240 if (result != accept_completed) 241 return GSS_S_CONTINUE_NEEDED; 242 243 _gss_spnego_fixup_ntlm(ctx); 244 245 return GSS_S_COMPLETE; 246} 247 248static OM_uint32 249spnego_initial(OM_uint32 * minor_status, 250 gss_cred_id_t cred, 251 gssspnego_ctx ctx, 252 const gss_name_t target_name, 253 const gss_OID mech_type, 254 OM_uint32 req_flags, 255 OM_uint32 time_req, 256 const gss_channel_bindings_t input_chan_bindings, 257 const gss_buffer_t input_token, 258 gss_buffer_t output_token, 259 OM_uint32 * ret_flags, 260 OM_uint32 * time_rec) 261{ 262 NegotiationToken nt; 263 int ret; 264 OM_uint32 sub, minor; 265 gss_buffer_desc mech_token; 266 size_t size = 0; 267 gss_buffer_desc data; 268 spnego_name name = (spnego_name)target_name; 269 struct mech_selection sel; 270 271 *minor_status = 0; 272 273 memset (&nt, 0, sizeof(nt)); 274 275 if (target_name == GSS_C_NO_NAME) 276 return GSS_S_BAD_NAME; 277 278 ctx->flags.local = 1; 279 280 sub = gss_import_name(&minor, &name->value, &name->type, &ctx->target_name); 281 if (GSS_ERROR(sub)) { 282 *minor_status = minor; 283 return sub; 284 } 285 286 nt.element = choice_NegotiationToken_negTokenInit; 287 288 memset(&sel, 0, sizeof(sel)); 289 290 sel.target_name = ctx->target_name; 291 sel.preferred_mech_type = GSS_C_NO_OID; 292 sel.req_flags = req_flags; 293 sel.time_req = time_req; 294 sel.input_chan_bindings = (gss_channel_bindings_t)input_chan_bindings; 295 296 sub = _gss_spnego_indicate_mechtypelist(&minor, 297 ctx->target_name, 298 initiator_approved, 299 &sel, 300 0, 301 cred, 302 &nt.u.negTokenInit.mechTypes, 303 &ctx->preferred_mech_type); 304 if (GSS_ERROR(sub)) { 305 *minor_status = minor; 306 return sub; 307 } 308 309 nt.u.negTokenInit.reqFlags = NULL; 310 311 if (sel.gssctx == NULL) { 312 free_NegotiationToken(&nt); 313 *minor_status = 0; 314 return gss_mg_set_error_string(GSS_C_NO_OID, GSS_S_NO_CONTEXT, 0, 315 "SPNEGO could not find a prefered mechanism"); 316 } 317 318 /* optimistic token from selection context */ 319 mech_token = sel.optimistic_token; 320 ctx->mech_flags = sel.optimistic_flags; 321 ctx->negotiated_mech_type = sel.negotiated_mech_type; 322 ctx->negotiated_ctx_id = sel.gssctx; 323 ctx->flags.maybe_open = sel.complete; 324 325 if (mech_token.length != 0) { 326 ALLOC(nt.u.negTokenInit.mechToken, 1); 327 if (nt.u.negTokenInit.mechToken == NULL) { 328 free_NegotiationToken(&nt); 329 gss_release_buffer(&minor, &mech_token); 330 *minor_status = ENOMEM; 331 return GSS_S_FAILURE; 332 } 333 nt.u.negTokenInit.mechToken->length = mech_token.length; 334 nt.u.negTokenInit.mechToken->data = malloc(mech_token.length); 335 if (nt.u.negTokenInit.mechToken->data == NULL && mech_token.length != 0) { 336 free_NegotiationToken(&nt); 337 gss_release_buffer(&minor, &mech_token); 338 *minor_status = ENOMEM; 339 return GSS_S_FAILURE; 340 } 341 memcpy(nt.u.negTokenInit.mechToken->data, mech_token.value, mech_token.length); 342 gss_release_buffer(&minor, &mech_token); 343 } else 344 nt.u.negTokenInit.mechToken = NULL; 345 346 nt.u.negTokenInit.mechListMIC = NULL; 347 348 { 349 MechTypeList mt; 350 351 mt.len = nt.u.negTokenInit.mechTypes.len; 352 mt.val = nt.u.negTokenInit.mechTypes.val; 353 354 ASN1_MALLOC_ENCODE(MechTypeList, 355 ctx->NegTokenInit_mech_types.value, 356 ctx->NegTokenInit_mech_types.length, 357 &mt, &size, ret); 358 if (ret) { 359 *minor_status = ret; 360 free_NegotiationToken(&nt); 361 return GSS_S_FAILURE; 362 } 363 //XXX heim_assert(ctx->NegTokenInit_mech_types.length == size, "asn1 internal error"); 364 } 365 366 ASN1_MALLOC_ENCODE(NegotiationToken, data.value, data.length, &nt, &size, ret); 367 free_NegotiationToken(&nt); 368 if (ret) { 369 return GSS_S_FAILURE; 370 } 371 if (data.length != size) 372 abort(); 373 374 sub = gss_encapsulate_token(&data, 375 GSS_SPNEGO_MECHANISM, 376 output_token); 377 free (data.value); 378 379 if (sub) { 380 return sub; 381 } 382 383 if (ret_flags) 384 *ret_flags = ctx->mech_flags; 385 if (time_rec) 386 *time_rec = ctx->mech_time_rec; 387 388 ctx->initiator_state = spnego_reply; 389 390 return GSS_S_CONTINUE_NEEDED; 391} 392 393/* 394 * 395 */ 396 397static OM_uint32 398spnego_reply(OM_uint32 * minor_status, 399 const gss_cred_id_t cred, 400 gssspnego_ctx ctx, 401 const gss_name_t target_name, 402 const gss_OID mech_type, 403 OM_uint32 req_flags, 404 OM_uint32 time_req, 405 const gss_channel_bindings_t input_chan_bindings, 406 const gss_buffer_t input_token, 407 gss_buffer_t output_token, 408 OM_uint32 * ret_flags, 409 OM_uint32 * time_rec) 410{ 411 OM_uint32 ret, minor; 412 NegotiationToken resp; 413 gss_buffer_desc mech_output_token; 414 415 *minor_status = 0; 416 417 output_token->length = 0; 418 output_token->value = NULL; 419 420 mech_output_token.length = 0; 421 mech_output_token.value = NULL; 422 423 ret = decode_NegotiationToken(input_token->value, input_token->length, 424 &resp, NULL); 425 if (ret) 426 return ret; 427 428 if (resp.element != choice_NegotiationToken_negTokenResp) { 429 free_NegotiationToken(&resp); 430 *minor_status = 0; 431 return GSS_S_BAD_MECH; 432 } 433 434 if (resp.u.negTokenResp.negResult == NULL 435 || *resp.u.negTokenResp.negResult == reject) 436 { 437 free_NegotiationToken(&resp); 438 return GSS_S_BAD_MECH; 439 } 440 441 /* 442 * Pick up the mechanism that the acceptor selected, only pick up 443 * the first selection. 444 */ 445 446 if (ctx->selected_mech_type == NULL && resp.u.negTokenResp.supportedMech) { 447 gss_OID_desc oid; 448 size_t len; 449 450 ctx->flags.seen_supported_mech = 1; 451 452 oid.length = (OM_uint32)der_length_oid(resp.u.negTokenResp.supportedMech); 453 oid.elements = malloc(oid.length); 454 if (oid.elements == NULL) { 455 free_NegotiationToken(&resp); 456 return GSS_S_BAD_MECH; 457 } 458 ret = der_put_oid(((uint8_t *)oid.elements) + oid.length - 1, 459 oid.length, 460 resp.u.negTokenResp.supportedMech, 461 &len); 462 if (ret || len != oid.length) { 463 free(oid.elements); 464 free_NegotiationToken(&resp); 465 return GSS_S_BAD_MECH; 466 } 467 468 if (gss_oid_equal(GSS_SPNEGO_MECHANISM, &oid)) { 469 free(oid.elements); 470 free_NegotiationToken(&resp); 471 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 472 GSS_S_BAD_MECH, (*minor_status = EINVAL), 473 "SPNEGO acceptor picked SPNEGO??"); 474 } 475 476 /* check if the acceptor took our optimistic token */ 477 if (gss_oid_equal(ctx->preferred_mech_type, &oid)) { 478 ctx->selected_mech_type = ctx->preferred_mech_type; 479 } else if (gss_oid_equal(ctx->preferred_mech_type, GSS_KRB5_MECHANISM) && 480 gss_oid_equal(&oid, &_gss_spnego_mskrb_mechanism_oid_desc)) { 481 /* mis-encoded asn1 type from msft servers */ 482 ctx->selected_mech_type = ctx->preferred_mech_type; 483 } else { 484 /* nope, lets start over */ 485 gss_delete_sec_context(&minor, &ctx->negotiated_ctx_id, 486 GSS_C_NO_BUFFER); 487 ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT; 488 489 ctx->selected_mech_type = _gss_mg_support_mechanism(&oid); 490 491 /* XXX check that server pick a mechanism we proposed */ 492 if (ctx->selected_mech_type == GSS_C_NO_OID) { 493 free(oid.elements); 494 free_NegotiationToken(&resp); 495 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 496 GSS_S_BAD_MECH, (*minor_status = EINVAL), 497 "SPNEGO acceptor send supportedMech we don't support"); 498 } 499 } 500 501 free(oid.elements); 502 503 } else if (ctx->selected_mech_type == NULL) { 504 free_NegotiationToken(&resp); 505 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 506 GSS_S_BAD_MECH, (*minor_status = EINVAL), 507 "SPNEGO acceptor didn't send supportedMech"); 508 } 509 510 /* if a token (of non zero length), or no context, pass to underlaying mech */ 511 if ((resp.u.negTokenResp.responseToken != NULL && resp.u.negTokenResp.responseToken->length) || 512 ctx->negotiated_ctx_id == GSS_C_NO_CONTEXT) { 513 gss_buffer_desc mech_input_token; 514 515 if (resp.u.negTokenResp.responseToken) { 516 mech_input_token.length = resp.u.negTokenResp.responseToken->length; 517 mech_input_token.value = resp.u.negTokenResp.responseToken->data; 518 } else { 519 mech_input_token.length = 0; 520 mech_input_token.value = NULL; 521 } 522 523 /* Fall through as if the negotiated mechanism 524 was requested explicitly */ 525 ret = gss_init_sec_context(&minor, 526 cred, 527 &ctx->negotiated_ctx_id, 528 ctx->target_name, 529 ctx->selected_mech_type, 530 req_flags, 531 time_req, 532 input_chan_bindings, 533 &mech_input_token, 534 &ctx->negotiated_mech_type, 535 &mech_output_token, 536 &ctx->mech_flags, 537 &ctx->mech_time_rec); 538 if (GSS_ERROR(ret)) { 539 free_NegotiationToken(&resp); 540 gss_mg_collect_error(ctx->selected_mech_type, ret, minor); 541 *minor_status = minor; 542 return ret; 543 } 544 if (ret == GSS_S_COMPLETE) { 545 ctx->flags.open = 1; 546 } 547 } else if (*resp.u.negTokenResp.negResult == accept_completed) { 548 if (ctx->flags.maybe_open) 549 ctx->flags.open = 1; 550 551 if (!ctx->flags.open) { 552 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 553 GSS_S_BAD_MECH, (*minor_status = EINVAL), 554 "SPNEGO acceptor send acceptor compete, " 555 "but we are not complete yet"); 556 } 557 } 558 559 if (*resp.u.negTokenResp.negResult == request_mic) { 560 ctx->flags.peer_require_mic = 1; 561 } 562 563 if (ctx->flags.open && ctx->flags.verified_mic == 0) { 564 565 /* 566 * Verify the mechListMIC if CFX was used and a non-preferred 567 * mechanism was selected. 568 */ 569 ctx->flags.require_mic = _gss_spnego_require_mechlist_mic(ctx); 570 571 /* 572 * If the peer sent mechListMIC, require it to verify ... 573 */ 574 if (resp.u.negTokenResp.mechListMIC) { 575 heim_octet_string *m = resp.u.negTokenResp.mechListMIC; 576 577 ctx->flags.require_mic = 1; 578 579 /* ...unless its a windows 2000 server that sends the 580 * responseToken inside the mechListMIC too. We only 581 * accept this condition if would have been safe to omit 582 * anyway. */ 583 584 if (ctx->flags.safe_omit 585 && resp.u.negTokenResp.responseToken 586 && der_heim_octet_string_cmp(m, resp.u.negTokenResp.responseToken) == 0) 587 { 588 ctx->flags.require_mic = 0; 589 } 590 } 591 592 } else { 593 ctx->flags.require_mic = 0; 594 } 595 596 /* 597 * If we are supposed to check mic and have it, force checking now. 598 */ 599 600 if (ctx->flags.require_mic && resp.u.negTokenResp.mechListMIC) { 601 602 ret = _gss_spnego_verify_mechtypes_mic(minor_status, ctx, 603 resp.u.negTokenResp.mechListMIC); 604 if (ret) { 605 free_NegotiationToken(&resp); 606 return ret; 607 } 608 } 609 610 /* 611 * Now that underlaying mech is open (conncted), we can figure out 612 * what nexd step to go to. 613 */ 614 615 if (ctx->flags.open) { 616 617 if (*resp.u.negTokenResp.negResult == accept_completed && ctx->flags.safe_omit) { 618 ctx->initiator_state = step_completed; 619 ret = GSS_S_COMPLETE; 620 } else if (ctx->flags.require_mic != 0 && ctx->flags.verified_mic == 0) { 621 ctx->initiator_state = wait_server_mic; 622 ret = GSS_S_CONTINUE_NEEDED; 623 } else { 624 ctx->initiator_state = step_completed; 625 ret = GSS_S_COMPLETE; 626 } 627 } 628 629 if (*resp.u.negTokenResp.negResult != accept_completed || 630 ctx->initiator_state != step_completed || 631 mech_output_token.length) 632 { 633 OM_uint32 ret2; 634 ret2 = make_reply(minor_status, ctx, 635 &mech_output_token, 636 output_token); 637 if (ret2) 638 ret = ret2; 639 } 640 641 free_NegotiationToken(&resp); 642 643 gss_release_buffer(&minor, &mech_output_token); 644 645 if (ret_flags) 646 *ret_flags = ctx->mech_flags; 647 if (time_rec) 648 *time_rec = ctx->mech_time_rec; 649 650 return ret; 651} 652 653static OM_uint32 654wait_server_mic(OM_uint32 * minor_status, 655 const gss_cred_id_t cred, 656 gssspnego_ctx ctx, 657 const gss_name_t target_name, 658 const gss_OID mech_type, 659 OM_uint32 req_flags, 660 OM_uint32 time_req, 661 const gss_channel_bindings_t input_chan_bindings, 662 const gss_buffer_t input_token, 663 gss_buffer_t output_token, 664 OM_uint32 * ret_flags, 665 OM_uint32 * time_rec) 666{ 667 OM_uint32 major_status; 668 NegotiationToken resp; 669 int ret; 670 671 ret = decode_NegotiationToken(input_token->value, input_token->length, &resp, NULL); 672 if (ret) 673 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 674 GSS_S_BAD_MECH, ret, 675 "Failed to decode NegotiationToken"); 676 677 if (resp.element != choice_NegotiationToken_negTokenResp 678 || resp.u.negTokenResp.negResult == NULL 679 || *resp.u.negTokenResp.negResult != accept_completed) 680 { 681 free_NegotiationToken(&resp); 682 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 683 GSS_S_BAD_MECH, (*minor_status = EINVAL), 684 "NegToken not accept_completed"); 685 } 686 687 if (resp.u.negTokenResp.mechListMIC) { 688 major_status = _gss_spnego_verify_mechtypes_mic(minor_status, ctx, 689 resp.u.negTokenResp.mechListMIC); 690 } else if (ctx->flags.safe_omit == 0) { 691 free_NegotiationToken(&resp); 692 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 693 GSS_S_BAD_MECH, (*minor_status = EINVAL), 694 "Waiting for MIC, but its missing in server request"); 695 } else { 696 major_status = GSS_S_COMPLETE; 697 } 698 699 free_NegotiationToken(&resp); 700 if (major_status != GSS_S_COMPLETE) 701 return major_status; 702 703 ctx->flags.verified_mic = 1; 704 ctx->initiator_state = step_completed; 705 706 if (ret_flags) 707 *ret_flags = ctx->mech_flags; 708 if (time_rec) 709 *time_rec = ctx->mech_time_rec; 710 711 *minor_status = 0; 712 return GSS_S_COMPLETE; 713} 714 715static OM_uint32 716step_completed(OM_uint32 * minor_status, 717 gss_cred_id_t cred, 718 gssspnego_ctx ctx, 719 gss_name_t name, 720 const gss_OID mech_type, 721 OM_uint32 req_flags, 722 OM_uint32 time_req, 723 const gss_channel_bindings_t input_chan_bindings, 724 const gss_buffer_t input_token, 725 gss_buffer_t output_token, 726 OM_uint32 * ret_flags, 727 OM_uint32 * time_rec) 728{ 729 return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM, 730 GSS_S_BAD_STATUS, (*minor_status = EINVAL), 731 "SPNEGO called got ISC call one too many"); 732} 733 734 735OM_uint32 736_gss_spnego_init_sec_context(OM_uint32 * minor_status, 737 const gss_cred_id_t initiator_cred_handle, 738 gss_ctx_id_t * context_handle, 739 const gss_name_t target_name, 740 const gss_OID mech_type, 741 OM_uint32 req_flags, 742 OM_uint32 time_req, 743 const gss_channel_bindings_t input_chan_bindings, 744 const gss_buffer_t input_token, 745 gss_OID * actual_mech_type, 746 gss_buffer_t output_token, 747 OM_uint32 * ret_flags, 748 OM_uint32 * time_rec) 749{ 750 gssspnego_ctx ctx; 751 OM_uint32 ret; 752 753 if (*context_handle == GSS_C_NO_CONTEXT) { 754 ret = _gss_spnego_alloc_sec_context(minor_status, context_handle); 755 if (GSS_ERROR(ret)) 756 return ret; 757 758 ctx = (gssspnego_ctx)*context_handle; 759 760 ctx->initiator_state = spnego_initial; 761 } else { 762 ctx = (gssspnego_ctx)*context_handle; 763 } 764 765 766 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); 767 768 do { 769 ret = ctx->initiator_state(minor_status, initiator_cred_handle, ctx, target_name, 770 mech_type, req_flags, time_req, input_chan_bindings, input_token, 771 output_token, ret_flags, time_rec); 772 773 } while (ret == GSS_S_COMPLETE && 774 ctx->initiator_state != step_completed && 775 output_token->length == 0); 776 777 /* destroy context in case of error */ 778 if (GSS_ERROR(ret)) { 779 OM_uint32 junk; 780 _gss_spnego_internal_delete_sec_context(&junk, context_handle, GSS_C_NO_BUFFER); 781 } else { 782 783 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); 784 785 if (actual_mech_type) 786 *actual_mech_type = ctx->negotiated_mech_type; 787 } 788 789 return ret; 790} 791 792