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;
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 allow it
443     * to be sent in packet.
444     */
445
446    if (resp.u.negTokenResp.supportedMech || ctx->flags.seen_supported_mech == 0) {
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_S_BAD_MECH;
472	}
473
474	/* check if the acceptor took our optimistic token */
475	if (!gss_oid_equal(ctx->preferred_mech_type, &oid) &&
476	    (!gss_oid_equal(ctx->preferred_mech_type, GSS_KRB5_MECHANISM) ||
477	     !gss_oid_equal(&oid, &_gss_spnego_mskrb_mechanism_oid_desc)))
478	{
479	    /* nope, lets start over */
480	    free(oid.elements);
481	    gss_delete_sec_context(&minor, &ctx->negotiated_ctx_id,
482				   GSS_C_NO_BUFFER);
483	    ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;
484	} else {
485	    gss_duplicate_oid(minor_status, &oid, &ctx->selected_mech_type);
486	    free(oid.elements);
487	}
488
489    } else if (!ctx->flags.seen_supported_mech) {
490	free_NegotiationToken(&resp);
491	return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM,
492				       GSS_S_BAD_MECH, (*minor_status = EINVAL),
493				       "SPNEGO acceptor didn't send supportedMech");
494    }
495
496    /* if a token (of non zero length), or no context, pass to underlaying mech */
497    if ((resp.u.negTokenResp.responseToken != NULL && resp.u.negTokenResp.responseToken->length) ||
498	ctx->negotiated_ctx_id == GSS_C_NO_CONTEXT) {
499	gss_buffer_desc mech_input_token;
500
501	if (resp.u.negTokenResp.responseToken) {
502	    mech_input_token.length = resp.u.negTokenResp.responseToken->length;
503	    mech_input_token.value  = resp.u.negTokenResp.responseToken->data;
504	} else {
505	    mech_input_token.length = 0;
506	    mech_input_token.value = NULL;
507	}
508
509	/* Fall through as if the negotiated mechanism
510	   was requested explicitly */
511	ret = gss_init_sec_context(&minor,
512				   cred,
513				   &ctx->negotiated_ctx_id,
514				   ctx->target_name,
515				   ctx->selected_mech_type,
516				   req_flags,
517				   time_req,
518				   input_chan_bindings,
519				   &mech_input_token,
520				   &ctx->negotiated_mech_type,
521				   &mech_output_token,
522				   &ctx->mech_flags,
523				   &ctx->mech_time_rec);
524	if (GSS_ERROR(ret)) {
525	    free_NegotiationToken(&resp);
526	    gss_mg_collect_error(ctx->selected_mech_type, ret, minor);
527	    *minor_status = minor;
528	    return ret;
529	}
530	if (ret == GSS_S_COMPLETE) {
531	    ctx->flags.open = 1;
532	}
533    } else if (*resp.u.negTokenResp.negResult == accept_completed) {
534	if (ctx->flags.maybe_open)
535	    ctx->flags.open = 1;
536    }
537
538    if (*resp.u.negTokenResp.negResult == request_mic) {
539	ctx->flags.peer_require_mic = 1;
540    }
541
542    if (ctx->flags.open && ctx->flags.verified_mic == 0) {
543
544	/*
545	 * Verify the mechListMIC if CFX was used and a non-preferred
546	 * mechanism was selected.
547	 */
548	ctx->flags.require_mic = _gss_spnego_require_mechlist_mic(ctx);
549
550	/*
551	 * If the peer sent mechListMIC, require it to verify ...
552	 */
553	if (resp.u.negTokenResp.mechListMIC) {
554	    heim_octet_string *m = resp.u.negTokenResp.mechListMIC;
555
556	    ctx->flags.require_mic = 1;
557
558	    /* ...unless its a windows 2000 server that sends the
559	     * responseToken inside the mechListMIC too. We only
560	     * accept this condition if would have been safe to omit
561	     * anyway. */
562
563	    if (ctx->flags.safe_omit
564		&& resp.u.negTokenResp.responseToken
565		&& der_heim_octet_string_cmp(m, resp.u.negTokenResp.responseToken) == 0)
566	    {
567		ctx->flags.require_mic = 0;
568	    }
569	}
570
571    } else {
572	ctx->flags.require_mic = 0;
573    }
574
575    /*
576     * If we are supposed to check mic and have it, force checking now.
577     */
578
579    if (ctx->flags.require_mic && resp.u.negTokenResp.mechListMIC) {
580
581	ret = _gss_spnego_verify_mechtypes_mic(minor_status, ctx,
582					       resp.u.negTokenResp.mechListMIC);
583	if (ret) {
584	    free_NegotiationToken(&resp);
585	    return ret;
586	}
587    }
588
589    /*
590     * Now that underlaying mech is open (conncted), we can figure out
591     * what nexd step to go to.
592     */
593
594    if (ctx->flags.open) {
595
596	if (*resp.u.negTokenResp.negResult == accept_completed && ctx->flags.safe_omit) {
597	    ctx->initiator_state = step_completed;
598	    ret = GSS_S_COMPLETE;
599	} else if (ctx->flags.require_mic != 0 && ctx->flags.verified_mic == 0) {
600	    ctx->initiator_state = wait_server_mic;
601	    ret = GSS_S_CONTINUE_NEEDED;
602	} else {
603	    ctx->initiator_state = step_completed;
604	    ret = GSS_S_COMPLETE;
605	}
606    }
607
608    if (*resp.u.negTokenResp.negResult != accept_completed ||
609	ctx->initiator_state != step_completed ||
610	mech_output_token.length)
611    {
612	OM_uint32 ret2;
613	ret2 = make_reply(minor_status, ctx,
614			  &mech_output_token,
615			  output_token);
616	if (ret2)
617	    ret = ret2;
618    }
619
620    free_NegotiationToken(&resp);
621
622    gss_release_buffer(&minor, &mech_output_token);
623
624    if (ret_flags)
625	*ret_flags = ctx->mech_flags;
626    if (time_rec)
627	*time_rec = ctx->mech_time_rec;
628
629    return ret;
630}
631
632static OM_uint32
633wait_server_mic(OM_uint32 * minor_status,
634		const gss_cred_id_t cred,
635		gssspnego_ctx ctx,
636		const gss_name_t target_name,
637		const gss_OID mech_type,
638		OM_uint32 req_flags,
639		OM_uint32 time_req,
640		const gss_channel_bindings_t input_chan_bindings,
641		const gss_buffer_t input_token,
642		gss_buffer_t output_token,
643		OM_uint32 * ret_flags,
644		OM_uint32 * time_rec)
645{
646    OM_uint32 major_status;
647    NegotiationToken resp;
648    int ret;
649
650    ret = decode_NegotiationToken(input_token->value, input_token->length, &resp, NULL);
651    if (ret)
652	return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM,
653				       GSS_S_BAD_MECH, ret,
654				       "Failed to decode NegotiationToken");
655
656    if (resp.element != choice_NegotiationToken_negTokenResp
657	|| resp.u.negTokenResp.negResult == NULL
658	|| *resp.u.negTokenResp.negResult != accept_completed)
659    {
660	free_NegotiationToken(&resp);
661	return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM,
662				       GSS_S_BAD_MECH, (*minor_status = EINVAL),
663				       "NegToken not accept_completed");
664    }
665
666    if (resp.u.negTokenResp.mechListMIC) {
667	major_status = _gss_spnego_verify_mechtypes_mic(minor_status, ctx,
668							resp.u.negTokenResp.mechListMIC);
669    } else if (ctx->flags.safe_omit == 0) {
670	free_NegotiationToken(&resp);
671	return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM,
672				       GSS_S_BAD_MECH, (*minor_status = EINVAL),
673				       "Waiting for MIC, but its missing in server request");
674    } else {
675	major_status = GSS_S_COMPLETE;
676    }
677
678    free_NegotiationToken(&resp);
679    if (major_status != GSS_S_COMPLETE)
680	return major_status;
681
682    ctx->flags.verified_mic = 1;
683    ctx->initiator_state = step_completed;
684
685    if (ret_flags)
686	*ret_flags = ctx->mech_flags;
687    if (time_rec)
688	*time_rec = ctx->mech_time_rec;
689
690    *minor_status = 0;
691    return GSS_S_COMPLETE;
692}
693
694static OM_uint32
695step_completed(OM_uint32 * minor_status,
696	       gss_cred_id_t cred,
697	       gssspnego_ctx ctx,
698	       gss_name_t name,
699	       const gss_OID mech_type,
700	       OM_uint32 req_flags,
701	       OM_uint32 time_req,
702	       const gss_channel_bindings_t input_chan_bindings,
703	       const gss_buffer_t input_token,
704	       gss_buffer_t output_token,
705	       OM_uint32 * ret_flags,
706	       OM_uint32 * time_rec)
707{
708    return gss_mg_set_error_string(GSS_SPNEGO_MECHANISM,
709				   GSS_S_BAD_STATUS, (*minor_status = EINVAL),
710				   "SPNEGO called got ISC call one too many");
711}
712
713
714OM_uint32
715_gss_spnego_init_sec_context(OM_uint32 * minor_status,
716			     const gss_cred_id_t initiator_cred_handle,
717			     gss_ctx_id_t * context_handle,
718			     const gss_name_t target_name,
719			     const gss_OID mech_type,
720			     OM_uint32 req_flags,
721			     OM_uint32 time_req,
722			     const gss_channel_bindings_t input_chan_bindings,
723			     const gss_buffer_t input_token,
724			     gss_OID * actual_mech_type,
725			     gss_buffer_t output_token,
726			     OM_uint32 * ret_flags,
727			     OM_uint32 * time_rec)
728{
729    gssspnego_ctx ctx;
730    OM_uint32 ret;
731
732    if (*context_handle == GSS_C_NO_CONTEXT) {
733	ret = _gss_spnego_alloc_sec_context(minor_status, context_handle);
734	if (GSS_ERROR(ret))
735	    return ret;
736
737	ctx = (gssspnego_ctx)*context_handle;
738
739	ctx->initiator_state = spnego_initial;
740    } else {
741	ctx = (gssspnego_ctx)*context_handle;
742    }
743
744
745    HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
746
747    do {
748	ret = ctx->initiator_state(minor_status, initiator_cred_handle, ctx, target_name,
749				   mech_type, req_flags, time_req, input_chan_bindings, input_token,
750				   output_token, ret_flags, time_rec);
751
752    } while (ret == GSS_S_COMPLETE &&
753	     ctx->initiator_state != step_completed &&
754	     output_token->length == 0);
755
756    /* destroy context in case of error */
757    if (GSS_ERROR(ret)) {
758	OM_uint32 junk;
759	_gss_spnego_internal_delete_sec_context(&junk, context_handle, GSS_C_NO_BUFFER);
760    } else {
761
762	HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
763
764	if (actual_mech_type)
765	    *actual_mech_type = ctx->negotiated_mech_type;
766    }
767
768    return ret;
769}
770
771