summaryrefslogtreecommitdiff
path: root/source4/heimdal/lib/gssapi/spnego/accept_sec_context.c
blob: 8a885a3e2f71d8bf8db493a5d2162b61453360c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden). 
 * Portions Copyright (c) 2004 PADL Software Pty Ltd.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 *
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 *
 * 3. Neither the name of the Institute nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE. 
 */

#include "spnego/spnego_locl.h"

RCSID("$Id: accept_sec_context.c,v 1.6 2006/10/07 22:26:57 lha Exp $");

OM_uint32
_gss_spnego_encode_response(OM_uint32 *minor_status,
			    const NegTokenResp *resp,
			    gss_buffer_t data,
			    u_char **ret_buf)
{
    OM_uint32 ret;
    u_char *buf;
    size_t buf_size, buf_len;

    buf_size = 1024;
    buf = malloc(buf_size);
    if (buf == NULL) {
	*minor_status = ENOMEM;
	return GSS_S_FAILURE;
    }

    do {
	ret = encode_NegTokenResp(buf + buf_size - 1,
				  buf_size,
				  resp, &buf_len);
	if (ret == 0) {
	    size_t tmp;

	    ret = der_put_length_and_tag(buf + buf_size - buf_len - 1,
					 buf_size - buf_len,
					 buf_len,
					 ASN1_C_CONTEXT,
					 CONS,
					 1,
					 &tmp);
	    if (ret == 0)
		buf_len += tmp;
	}
	if (ret) {
	    if (ret == ASN1_OVERFLOW) {
		u_char *tmp;

		buf_size *= 2;
		tmp = realloc (buf, buf_size);
		if (tmp == NULL) {
		    *minor_status = ENOMEM;
		    free(buf);
		    return GSS_S_FAILURE;
		}
		buf = tmp;
	    } else {
		*minor_status = ret;
		free(buf);
		return GSS_S_FAILURE;
	    }
	}
    } while (ret == ASN1_OVERFLOW);

    data->value  = buf + buf_size - buf_len;
    data->length = buf_len;
    *ret_buf     = buf;

    return GSS_S_COMPLETE;
}

static OM_uint32
send_reject (OM_uint32 *minor_status,
	     gss_buffer_t output_token)
{
    NegTokenResp resp;
    gss_buffer_desc data;
    u_char *buf;
    OM_uint32 ret;

    ALLOC(resp.negResult, 1);
    if (resp.negResult == NULL) {
	*minor_status = ENOMEM;
	return GSS_S_FAILURE;
    }
    *(resp.negResult)  = reject;
    resp.supportedMech = NULL;
    resp.responseToken = NULL;
    resp.mechListMIC   = NULL;
    
    ret = _gss_spnego_encode_response (minor_status, &resp, &data, &buf);
    free_NegTokenResp(&resp);
    if (ret != GSS_S_COMPLETE)
	return ret;

    output_token->value = malloc(data.length);
    if (output_token->value == NULL) {
	*minor_status = ENOMEM;
	ret = GSS_S_FAILURE;
    } else {
	output_token->length = data.length;
	memcpy(output_token->value, data.value, output_token->length);
    }
    free(buf);
    if (ret != GSS_S_COMPLETE)
	return ret;
    return GSS_S_BAD_MECH;
}

OM_uint32
_gss_spnego_indicate_mechtypelist (OM_uint32 *minor_status,
				   int includeMSCompatOID,
				   const gssspnego_cred cred_handle,
				   MechTypeList *mechtypelist,
				   gss_OID *preferred_mech)
{
    OM_uint32 ret;
    gss_OID_set supported_mechs = GSS_C_NO_OID_SET;
    int i, count;

    if (cred_handle != NULL) {
	ret = gss_inquire_cred(minor_status,
			       cred_handle->negotiated_cred_id,
			       NULL,
			       NULL,
			       NULL,
			       &supported_mechs);
    } else {
	ret = gss_indicate_mechs(minor_status, &supported_mechs);
    }

    if (ret != GSS_S_COMPLETE) {
	return ret;
    }

    if (supported_mechs->count == 0) {
	*minor_status = ENOENT;
	gss_release_oid_set(minor_status, &supported_mechs);
	return GSS_S_FAILURE;
    }

    count = supported_mechs->count;
    if (includeMSCompatOID)
	count++;

    mechtypelist->len = 0;
    mechtypelist->val = calloc(count, sizeof(MechType));
    if (mechtypelist->val == NULL) {
	*minor_status = ENOMEM;
	gss_release_oid_set(minor_status, &supported_mechs);
	return GSS_S_FAILURE;
    }

    for (i = 0; i < supported_mechs->count; i++) {
	ret = _gss_spnego_add_mech_type(&supported_mechs->elements[i],
					includeMSCompatOID,
					mechtypelist);
	if (ret != 0) {
	    *minor_status = ENOMEM;
	    ret = GSS_S_FAILURE;
	    break;
	}
    }

    if (ret == GSS_S_COMPLETE && preferred_mech != NULL) {
	ret = gss_duplicate_oid(minor_status,
				&supported_mechs->elements[0],
				preferred_mech);
    }

    if (ret != GSS_S_COMPLETE) {
	free_MechTypeList(mechtypelist);
	mechtypelist->len = 0;
	mechtypelist->val = NULL;
    }
    gss_release_oid_set(minor_status, &supported_mechs);

    return ret;
}

static OM_uint32
send_supported_mechs (OM_uint32 *minor_status,
		      gss_buffer_t output_token)
{
    NegTokenInit ni;
    char hostname[MAXHOSTNAMELEN], *p;
    gss_buffer_desc name_buf;
    gss_OID name_type;
    gss_name_t target_princ;
    gss_name_t canon_princ;
    OM_uint32 ret, minor;
    u_char *buf;
    size_t buf_size, buf_len;
    gss_buffer_desc data;

    memset(&ni, 0, sizeof(ni));

    ni.reqFlags = NULL;
    ni.mechToken = NULL;
    ni.negHints = NULL;
    ni.mechListMIC = NULL;

    ret = _gss_spnego_indicate_mechtypelist(minor_status, 1,
					    NULL,
					    &ni.mechTypes, NULL);
    if (ret != GSS_S_COMPLETE) {
	return ret;
    }

    memset(&target_princ, 0, sizeof(target_princ));
    if (gethostname(hostname, sizeof(hostname) - 1) != 0) {
	*minor_status = errno;
	free_NegTokenInit(&ni);
	return GSS_S_FAILURE;
    }

    /* Send the constructed SAM name for this host */
    for (p = hostname; *p != '\0' && *p != '.'; p++) {
	*p = toupper((unsigned char)*p);
    }
    *p++ = '$';
    *p = '\0';

    name_buf.length = strlen(hostname);
    name_buf.value = hostname;

    ret = gss_import_name(minor_status, &name_buf,
			  GSS_C_NO_OID,
			  &target_princ);
    if (ret != GSS_S_COMPLETE) {
	return ret;
    }

    name_buf.length = 0;
    name_buf.value = NULL;

    /* Canonicalize the name using the preferred mechanism */
    ret = gss_canonicalize_name(minor_status,
				target_princ,
				GSS_C_NO_OID,
				&canon_princ);
    if (ret != GSS_S_COMPLETE) {
	gss_release_name(&minor, &target_princ);
	return ret;
    }

    ret = gss_display_name(minor_status, canon_princ,
			   &name_buf, &name_type);
    if (ret != GSS_S_COMPLETE) {
	gss_release_name(&minor, &canon_princ);
	gss_release_name(&minor, &target_princ);
	return ret;
    }

    gss_release_name(&minor, &canon_princ);
    gss_release_name(&minor, &target_princ);

    ALLOC(ni.negHints, 1);
    if (ni.negHints == NULL) {
	*minor_status = ENOMEM;
	gss_release_buffer(&minor, &name_buf);
	free_NegTokenInit(&ni);
	return GSS_S_FAILURE;
    }

    ALLOC(ni.negHints->hintName, 1);
    if (ni.negHints->hintName == NULL) {
	*minor_status = ENOMEM;
	gss_release_buffer(&minor, &name_buf);
	free_NegTokenInit(&ni);
	return GSS_S_FAILURE;
    }

    *(ni.negHints->hintName) = name_buf.value;
    name_buf.value = NULL;
    ni.negHints->hintAddress = NULL;

    buf_size = 1024;
    buf = malloc(buf_size);
    if (buf == NULL) {
	free_NegTokenInit(&ni);
	*minor_status = ENOMEM;
	return GSS_S_FAILURE;
    }

    do {
	ret = encode_NegTokenInit(buf + buf_size - 1,
				  buf_size,
				  &ni, &buf_len);
	if (ret == 0) {
	    size_t tmp;

	    ret = der_put_length_and_tag(buf + buf_size - buf_len - 1,
					 buf_size - buf_len,
					 buf_len,
					 ASN1_C_CONTEXT,
					 CONS,
					 0,
					 &tmp);
	    if (ret == 0)
		buf_len += tmp;
	}
	if (ret) {
	    if (ret == ASN1_OVERFLOW) {
		u_char *tmp;

		buf_size *= 2;
		tmp = realloc (buf, buf_size);
		if (tmp == NULL) {
		    *minor_status = ENOMEM;
		    free(buf);
		    free_NegTokenInit(&ni);
		    return GSS_S_FAILURE;
		}
		buf = tmp;
	    } else {
		*minor_status = ret;
		free(buf);
		free_NegTokenInit(&ni);
		return GSS_S_FAILURE;
	    }
	}
    } while (ret == ASN1_OVERFLOW);

    data.value  = buf + buf_size - buf_len;
    data.length = buf_len;

    ret = gss_encapsulate_token(&data,
				GSS_SPNEGO_MECHANISM,
				output_token);
    free (buf);
    free_NegTokenInit (&ni);

    if (ret != GSS_S_COMPLETE)
	return ret;

    *minor_status = 0;

    return GSS_S_CONTINUE_NEEDED;
}

static OM_uint32
send_accept (OM_uint32 *minor_status,
	     gssspnego_ctx context_handle,
	     gss_buffer_t mech_token,
	     int initial_response,
	     gss_buffer_t mech_buf,
	     gss_buffer_t output_token)
{
    NegTokenResp resp;
    gss_buffer_desc data;
    u_char *buf;
    OM_uint32 ret;
    gss_buffer_desc mech_mic_buf;

    memset(&resp, 0, sizeof(resp));

    ALLOC(resp.negResult, 1);
    if (resp.negResult == NULL) {
	*minor_status = ENOMEM;
	return GSS_S_FAILURE;
    }

    if (context_handle->open) {
	if (mech_token != GSS_C_NO_BUFFER
	    && mech_token->length != 0
	    && mech_buf != GSS_C_NO_BUFFER)
	    *(resp.negResult) = accept_incomplete;
	else
	    *(resp.negResult) = accept_completed;
    } else {
	if (initial_response && context_handle->require_mic)
	    *(resp.negResult) = request_mic;
	else
	    *(resp.negResult) = accept_incomplete;
    }

    if (initial_response) {
	ALLOC(resp.supportedMech, 1);
	if (resp.supportedMech == NULL) {
	    free_NegTokenResp(&resp);
	    *minor_status = ENOMEM;
	    return GSS_S_FAILURE;
	}

	ret = der_get_oid(context_handle->preferred_mech_type->elements,
			  context_handle->preferred_mech_type->length,
			  resp.supportedMech,
			  NULL);
	if (ret) {
	    free_NegTokenResp(&resp);
	    *minor_status = ENOMEM;
	    return GSS_S_FAILURE;
	}
    } else {
	resp.supportedMech = NULL;
    }

    if (mech_token != GSS_C_NO_BUFFER && mech_token->length != 0) {
	ALLOC(resp.responseToken, 1);
	if (resp.responseToken == NULL) {
	    free_NegTokenResp(&resp);
	    *minor_status = ENOMEM;
	    return GSS_S_FAILURE;
	}
	resp.responseToken->length = mech_token->length;
	resp.responseToken->data   = mech_token->value;
	mech_token->length = 0;
	mech_token->value  = NULL;
    } else {
	resp.responseToken = NULL;
    }

    if (mech_buf != GSS_C_NO_BUFFER) {
	ALLOC(resp.mechListMIC, 1);
	if (resp.mechListMIC == NULL) {
	    free_NegTokenResp(&resp);
	    *minor_status = ENOMEM;
	    return GSS_S_FAILURE;
	}

	ret = gss_get_mic(minor_status,
			  context_handle->negotiated_ctx_id,
			  0,
			  mech_buf,
			  &mech_mic_buf);
	if (ret != GSS_S_COMPLETE) {
	    free_NegTokenResp(&resp);
	    return ret;
	}

	resp.mechListMIC->length = mech_mic_buf.length;
	resp.mechListMIC->data   = mech_mic_buf.value;
    } else
	resp.mechListMIC = NULL;
 
    ret = _gss_spnego_encode_response (minor_status, &resp, &data, &buf);
    if (ret != GSS_S_COMPLETE) {
	free_NegTokenResp(&resp);
	return ret;
    }

    /*
     * The response should not be encapsulated, because
     * it is a SubsequentContextToken (note though RFC 1964
     * specifies encapsulation for all _Kerberos_ tokens).
     */
    output_token->value = malloc(data.length);
    if (output_token->value == NULL) {
	*minor_status = ENOMEM;
	ret = GSS_S_FAILURE;
    } else {
	output_token->length = data.length;
	memcpy(output_token->value, data.value, output_token->length);
    }
    free(buf);
    if (ret != GSS_S_COMPLETE) {
	free_NegTokenResp(&resp);
	return ret;
    }

    ret = (*(resp.negResult) == accept_completed) ? GSS_S_COMPLETE :
						    GSS_S_CONTINUE_NEEDED;
    free_NegTokenResp(&resp);
    return ret;
}


static OM_uint32
verify_mechlist_mic
	   (OM_uint32 *minor_status,
	    gssspnego_ctx context_handle,
	    gss_buffer_t mech_buf,
	    heim_octet_string *mechListMIC
	   )
{
    OM_uint32 ret;
    gss_buffer_desc mic_buf;

    if (context_handle->verified_mic) {
	/* This doesn't make sense, we've already verified it? */
	*minor_status = 0;
	return GSS_S_DUPLICATE_TOKEN;
    }

    if (mechListMIC == NULL) {
	*minor_status = 0;
	return GSS_S_DEFECTIVE_TOKEN;
    }

    mic_buf.length = mechListMIC->length;
    mic_buf.value  = mechListMIC->data;

    ret = gss_verify_mic(minor_status,
			 context_handle->negotiated_ctx_id,
			 mech_buf,
			 &mic_buf,
			 NULL);

    if (ret != GSS_S_COMPLETE)
	ret = GSS_S_DEFECTIVE_TOKEN;

    return ret;
}

OM_uint32
_gss_spnego_accept_sec_context
	   (OM_uint32 * minor_status,
	    gss_ctx_id_t * context_handle,
	    const gss_cred_id_t acceptor_cred_handle,
	    const gss_buffer_t input_token_buffer,
	    const gss_channel_bindings_t input_chan_bindings,
	    gss_name_t * src_name,
	    gss_OID * mech_type,
	    gss_buffer_t output_token,
	    OM_uint32 * ret_flags,
	    OM_uint32 * time_rec,
	    gss_cred_id_t *delegated_cred_handle
	   )
{
    OM_uint32 ret, ret2, minor;
    NegTokenInit ni;
    NegTokenResp na;
    size_t ni_len, na_len;
    int i;
    gss_buffer_desc data;
    size_t len, taglen;
    int initialToken;
    unsigned int negResult = accept_incomplete;
    gss_buffer_t mech_input_token = GSS_C_NO_BUFFER;
    gss_buffer_t mech_output_token = GSS_C_NO_BUFFER;
    gss_buffer_desc mech_buf;
    gss_OID preferred_mech_type = GSS_C_NO_OID;
    gssspnego_ctx ctx;
    gssspnego_cred acceptor_cred = (gssspnego_cred)acceptor_cred_handle;

    *minor_status = 0;

    output_token->length = 0;
    output_token->value  = NULL;

    if (src_name != NULL)
	*src_name = GSS_C_NO_NAME;

    if (mech_type != NULL)
	*mech_type = GSS_C_NO_OID;

    if (ret_flags != NULL)
	*ret_flags = 0;

    if (time_rec != NULL)
	*time_rec = 0;

    if (delegated_cred_handle != NULL)
	*delegated_cred_handle = GSS_C_NO_CREDENTIAL;

    mech_buf.value = NULL;

    if (*context_handle == GSS_C_NO_CONTEXT) {
	ret = _gss_spnego_alloc_sec_context(minor_status,
					    context_handle);
	if (ret != GSS_S_COMPLETE)
	    return ret;

	if (input_token_buffer->length == 0) {
	    return send_supported_mechs (minor_status,
					 output_token);
	}
    }

    ctx = (gssspnego_ctx)*context_handle;

    /*
     * The GSS-API encapsulation is only present on the initial
     * context token (negTokenInit).
     */
    ret = gss_decapsulate_token (input_token_buffer,
				 GSS_SPNEGO_MECHANISM,
				 &data);
    initialToken = (ret == GSS_S_COMPLETE);

    if (!initialToken) {
	data.value  = input_token_buffer->value;
	data.length = input_token_buffer->length;
    }

    ret = der_match_tag_and_length(data.value, data.length,
				   ASN1_C_CONTEXT, CONS,
				   initialToken ? 0 : 1,
				   &len, &taglen);
    if (ret) {
	*minor_status = ret;
	return GSS_S_FAILURE;
    }

    if (len > data.length - taglen) {
	*minor_status = ASN1_OVERRUN;
	return GSS_S_FAILURE;
    }

    if (initialToken) {
	ret = decode_NegTokenInit((const unsigned char *)data.value + taglen, 
				  len, &ni, &ni_len);
    } else {
	ret = decode_NegTokenResp((const unsigned char *)data.value + taglen, 
				  len, &na, &na_len);
    }
    if (ret) {
	*minor_status = ret;
	return GSS_S_DEFECTIVE_TOKEN;
    }

    if (!initialToken && na.negResult != NULL) {
	negResult = *(na.negResult);
    }

    if (negResult == reject || negResult == request_mic) {
	/* request_mic should only be sent by acceptor */
	free_NegTokenResp(&na);
	return GSS_S_DEFECTIVE_TOKEN;
    }

    if (initialToken) {
	for (i = 0; i < ni.mechTypes.len; ++i) {
	    /* Call glue layer to find first mech we support */
	    ret = _gss_spnego_select_mech(minor_status, &ni.mechTypes.val[i],
					  &preferred_mech_type);
	    if (ret == 0)
		break;
	}
	if (preferred_mech_type == GSS_C_NO_OID) {
	    free_NegTokenInit(&ni);
	    return GSS_S_BAD_MECH;
	}
    }

    HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);

    if (initialToken) {
	ctx->preferred_mech_type = preferred_mech_type;
	ctx->initiator_mech_types.len = ni.mechTypes.len;
	ctx->initiator_mech_types.val = ni.mechTypes.val;
	ni.mechTypes.len = 0;
	ni.mechTypes.val = NULL;
    }

    {
	gss_buffer_desc ibuf, obuf;
	int require_mic, verify_mic, get_mic;
	int require_response;
	heim_octet_string *mic;

	if (initialToken) {
	    if (ni.mechToken != NULL) {
		ibuf.length = ni.mechToken->length;
		ibuf.value = ni.mechToken->data;
		mech_input_token = &ibuf;
	    }
	} else {
	    if (na.responseToken != NULL) {
		ibuf.length = na.responseToken->length;
		ibuf.value = na.responseToken->data;
		mech_input_token = &ibuf;
	    }
	}

	if (mech_input_token != GSS_C_NO_BUFFER) {
	    gss_cred_id_t mech_cred;
	    gss_cred_id_t mech_delegated_cred;
	    gss_cred_id_t *mech_delegated_cred_p;

	    if (acceptor_cred != NULL)
		mech_cred = acceptor_cred->negotiated_cred_id;
	    else
		mech_cred = GSS_C_NO_CREDENTIAL;

	    if (delegated_cred_handle != NULL) {
		mech_delegated_cred = GSS_C_NO_CREDENTIAL;
		mech_delegated_cred_p = &mech_delegated_cred;
	    } else {
		mech_delegated_cred_p = NULL;
	    }

	    if (ctx->mech_src_name != GSS_C_NO_NAME)
		gss_release_name(&minor, &ctx->mech_src_name);

	    if (ctx->delegated_cred_id != GSS_C_NO_CREDENTIAL)
		_gss_spnego_release_cred(&minor, &ctx->delegated_cred_id);

	    ret = gss_accept_sec_context(&minor,
					 &ctx->negotiated_ctx_id,
					 mech_cred,
					 mech_input_token,
					 input_chan_bindings,
					 &ctx->mech_src_name,
					 &ctx->negotiated_mech_type,
					 &obuf,
					 &ctx->mech_flags,
					 &ctx->mech_time_rec,
					 mech_delegated_cred_p);
	    if (ret == GSS_S_COMPLETE || ret == GSS_S_CONTINUE_NEEDED) {
		if (mech_delegated_cred_p != NULL &&
		    mech_delegated_cred != GSS_C_NO_CREDENTIAL) {
		    ret2 = _gss_spnego_alloc_cred(minor_status,
						  mech_delegated_cred,
						  &ctx->delegated_cred_id);
		    if (ret2 != GSS_S_COMPLETE)
			ret = ret2;
		}
		mech_output_token = &obuf;
	    }
	    if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
		if (initialToken)
		    free_NegTokenInit(&ni);
		else
		    free_NegTokenResp(&na);
		send_reject (minor_status, output_token);
		HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
		return ret;
	    }
	    if (ret == GSS_S_COMPLETE)
		ctx->open = 1;
	} else
	    ret = GSS_S_COMPLETE;

	ret2 = _gss_spnego_require_mechlist_mic(minor_status, 
						ctx,
						&require_mic);
	if (ret2)
	    goto out;

	ctx->require_mic = require_mic;

	mic = initialToken ? ni.mechListMIC : na.mechListMIC;
	if (mic != NULL)
	    require_mic = 1;

	if (ctx->open && require_mic) {
	    if (mech_input_token == GSS_C_NO_BUFFER) { /* Even/One */
		verify_mic = 1;
		get_mic = 0;
	    } else if (mech_output_token != GSS_C_NO_BUFFER &&
		       mech_output_token->length == 0) { /* Odd */
		get_mic = verify_mic = 1;
	    } else { /* Even/One */
		verify_mic = 0;
		get_mic = 1;
	    }

	    if (verify_mic || get_mic) {
		int eret;
		size_t buf_len;

    		ASN1_MALLOC_ENCODE(MechTypeList, 
				   mech_buf.value, mech_buf.length,
				   &ctx->initiator_mech_types, &buf_len, eret);
		if (eret) {
		    ret2 = GSS_S_FAILURE;
		    *minor_status = eret;
		    goto out;
		}
		if (mech_buf.length != buf_len)
		    abort();
	    }

	    if (verify_mic) {
		ret2 = verify_mechlist_mic(minor_status, ctx, &mech_buf, mic);
		if (ret2) {
		    if (get_mic)
			send_reject (minor_status, output_token);
		    goto out;
		}

		ctx->verified_mic = 1;
	    }
	} else
	    verify_mic = get_mic = 0;

	if (ctx->mech_flags & GSS_C_DCE_STYLE)
	    require_response = (negResult != accept_completed);
	else
	    require_response = 0;

	/*
	 * Check whether we need to send a result: there should be only
	 * one accept_completed response sent in the entire negotiation
	 */
	if ((mech_output_token != GSS_C_NO_BUFFER &&
	     mech_output_token->length != 0)
	    || require_response
	    || get_mic) {
	    ret2 = send_accept (minor_status,
				ctx,
				mech_output_token,
				initialToken,
				get_mic ? &mech_buf : NULL,
				output_token);
	    if (ret2)
		goto out;
	}

     out:
	if (ret2 != GSS_S_COMPLETE)
	    ret = ret2;
	if (mech_output_token != NULL)
	    gss_release_buffer(&minor, mech_output_token);
	if (mech_buf.value != NULL)
	    free(mech_buf.value);
	if (initialToken)
	    free_NegTokenInit(&ni);
	else
	    free_NegTokenResp(&na);
    }

    if (ret == GSS_S_COMPLETE) {
	if (src_name != NULL) {
	    ret2 = gss_duplicate_name(minor_status,
				      ctx->mech_src_name,
				      src_name);
	    if (ret2 != GSS_S_COMPLETE)
		ret = ret2;
	}
        if (delegated_cred_handle != NULL) {
	    *delegated_cred_handle = ctx->delegated_cred_id;
	    ctx->delegated_cred_id = GSS_C_NO_CREDENTIAL;
	}
    }

    if (mech_type != NULL)
	*mech_type = ctx->negotiated_mech_type;
    if (ret_flags != NULL)
	*ret_flags = ctx->mech_flags;
    if (time_rec != NULL)
	*time_rec = ctx->mech_time_rec;

    if (ret == GSS_S_COMPLETE || ret == GSS_S_CONTINUE_NEEDED) {
	HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
 	return ret;
    }

    _gss_spnego_internal_delete_sec_context(&minor, context_handle,
				   GSS_C_NO_BUFFER);

    return ret;
}