Contributed by Andrew Bartlett:
[heimdal.git] / lib / gssapi / krb5 / accept_sec_context.c
blob8d998ed0988f3ec3b5d033ccb761f0b006dcb913
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "gsskrb5_locl.h"
36 HEIMDAL_MUTEX gssapi_keytab_mutex = HEIMDAL_MUTEX_INITIALIZER;
37 krb5_keytab _gsskrb5_keytab;
39 OM_uint32
40 _gsskrb5_register_acceptor_identity (const char *identity)
42 krb5_context context;
43 krb5_error_code ret;
45 ret = _gsskrb5_init(&context);
46 if(ret)
47 return GSS_S_FAILURE;
49 HEIMDAL_MUTEX_lock(&gssapi_keytab_mutex);
51 if(_gsskrb5_keytab != NULL) {
52 krb5_kt_close(context, _gsskrb5_keytab);
53 _gsskrb5_keytab = NULL;
55 if (identity == NULL) {
56 ret = krb5_kt_default(context, &_gsskrb5_keytab);
57 } else {
58 char *p;
60 asprintf(&p, "FILE:%s", identity);
61 if(p == NULL) {
62 HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
63 return GSS_S_FAILURE;
65 ret = krb5_kt_resolve(context, p, &_gsskrb5_keytab);
66 free(p);
68 HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
69 if(ret)
70 return GSS_S_FAILURE;
71 return GSS_S_COMPLETE;
74 void
75 _gsskrb5i_is_cfx(krb5_context context, gsskrb5_ctx ctx, int acceptor)
77 krb5_error_code ret;
78 krb5_keyblock *key;
80 if (acceptor) {
81 if (ctx->auth_context->local_subkey)
82 key = ctx->auth_context->local_subkey;
83 else
84 key = ctx->auth_context->remote_subkey;
85 } else {
86 if (ctx->auth_context->remote_subkey)
87 key = ctx->auth_context->remote_subkey;
88 else
89 key = ctx->auth_context->local_subkey;
91 if (key == NULL)
92 key = ctx->auth_context->keyblock;
94 if (key == NULL)
95 return;
97 switch (key->keytype) {
98 case ETYPE_DES_CBC_CRC:
99 case ETYPE_DES_CBC_MD4:
100 case ETYPE_DES_CBC_MD5:
101 case ETYPE_DES3_CBC_MD5:
102 case ETYPE_DES3_CBC_SHA1:
103 case ETYPE_ARCFOUR_HMAC_MD5:
104 case ETYPE_ARCFOUR_HMAC_MD5_56:
105 break;
106 default :
107 ctx->more_flags |= IS_CFX;
109 if ((acceptor && ctx->auth_context->local_subkey) ||
110 (!acceptor && ctx->auth_context->remote_subkey))
111 ctx->more_flags |= ACCEPTOR_SUBKEY;
112 break;
114 if (ctx->crypto)
115 krb5_crypto_destroy(context, ctx->crypto);
116 ret = krb5_crypto_init(context, key, 0, &ctx->crypto);
120 static OM_uint32
121 gsskrb5_accept_delegated_token
122 (OM_uint32 * minor_status,
123 gsskrb5_ctx ctx,
124 krb5_context context,
125 gss_cred_id_t * delegated_cred_handle
128 krb5_ccache ccache = NULL;
129 krb5_error_code kret;
130 int32_t ac_flags, ret = GSS_S_COMPLETE;
132 *minor_status = 0;
134 /* XXX Create a new delegated_cred_handle? */
135 if (delegated_cred_handle == NULL) {
136 kret = krb5_cc_default (context, &ccache);
137 } else {
138 *delegated_cred_handle = NULL;
139 kret = krb5_cc_new_unique (context, krb5_cc_type_memory,
140 NULL, &ccache);
142 if (kret) {
143 ctx->flags &= ~GSS_C_DELEG_FLAG;
144 goto out;
147 kret = krb5_cc_initialize(context, ccache, ctx->source);
148 if (kret) {
149 ctx->flags &= ~GSS_C_DELEG_FLAG;
150 goto out;
153 krb5_auth_con_removeflags(context,
154 ctx->auth_context,
155 KRB5_AUTH_CONTEXT_DO_TIME,
156 &ac_flags);
157 kret = krb5_rd_cred2(context,
158 ctx->auth_context,
159 ccache,
160 &ctx->fwd_data);
161 krb5_auth_con_setflags(context,
162 ctx->auth_context,
163 ac_flags);
164 if (kret) {
165 ctx->flags &= ~GSS_C_DELEG_FLAG;
166 ret = GSS_S_FAILURE;
167 *minor_status = kret;
168 goto out;
171 if (delegated_cred_handle) {
172 gsskrb5_cred handle;
174 ret = _gsskrb5_import_cred(minor_status,
175 ccache,
176 NULL,
177 NULL,
178 delegated_cred_handle);
179 if (ret != GSS_S_COMPLETE)
180 goto out;
182 handle = (gsskrb5_cred) *delegated_cred_handle;
184 handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
185 krb5_cc_close(context, ccache);
186 ccache = NULL;
189 out:
190 if (ccache) {
191 /* Don't destroy the default cred cache */
192 if (delegated_cred_handle == NULL)
193 krb5_cc_close(context, ccache);
194 else
195 krb5_cc_destroy(context, ccache);
197 return ret;
200 static OM_uint32
201 gsskrb5_acceptor_ready(OM_uint32 * minor_status,
202 gsskrb5_ctx ctx,
203 krb5_context context,
204 gss_cred_id_t *delegated_cred_handle)
206 OM_uint32 ret;
207 int32_t seq_number;
208 int is_cfx = 0;
210 krb5_auth_getremoteseqnumber (context,
211 ctx->auth_context,
212 &seq_number);
214 _gsskrb5i_is_cfx(context, ctx, 1);
215 is_cfx = (ctx->more_flags & IS_CFX);
217 ret = _gssapi_msg_order_create(minor_status,
218 &ctx->order,
219 _gssapi_msg_order_f(ctx->flags),
220 seq_number, 0, is_cfx);
221 if (ret)
222 return ret;
225 * If requested, set local sequence num to remote sequence if this
226 * isn't a mutual authentication context
228 if (!(ctx->flags & GSS_C_MUTUAL_FLAG) && _gssapi_msg_order_f(ctx->flags)) {
229 krb5_auth_con_setlocalseqnumber(context,
230 ctx->auth_context,
231 seq_number);
235 * We should handle the delegation ticket, in case it's there
237 if (ctx->fwd_data.length > 0 && (ctx->flags & GSS_C_DELEG_FLAG)) {
238 ret = gsskrb5_accept_delegated_token(minor_status,
239 ctx,
240 context,
241 delegated_cred_handle);
242 if (ret)
243 return ret;
244 } else {
245 /* Well, looks like it wasn't there after all */
246 ctx->flags &= ~GSS_C_DELEG_FLAG;
249 ctx->state = ACCEPTOR_READY;
250 ctx->more_flags |= OPEN;
252 return GSS_S_COMPLETE;
255 static OM_uint32
256 send_error_token(OM_uint32 *minor_status,
257 krb5_context context,
258 krb5_error_code kret,
259 krb5_principal server,
260 krb5_data *indata,
261 gss_buffer_t output_token)
263 krb5_principal ap_req_server = NULL;
264 krb5_error_code ret;
265 krb5_data outbuf;
267 /* build server from request if the acceptor had not selected one */
268 if (server == NULL) {
269 AP_REQ ap_req;
271 ret = krb5_decode_ap_req(context, indata, &ap_req);
272 if (ret) {
273 *minor_status = ret;
274 return GSS_S_FAILURE;
276 ret = _krb5_principalname2krb5_principal(context,
277 &ap_req_server,
278 ap_req.ticket.sname,
279 ap_req.ticket.realm);
280 free_AP_REQ(&ap_req);
281 if (ret) {
282 *minor_status = ret;
283 return GSS_S_FAILURE;
285 server = ap_req_server;
288 ret = krb5_mk_error(context, kret, NULL, NULL, NULL,
289 server, NULL, NULL, &outbuf);
290 if (ap_req_server)
291 krb5_free_principal(context, ap_req_server);
292 if (ret) {
293 *minor_status = ret;
294 return GSS_S_FAILURE;
297 ret = _gsskrb5_encapsulate(minor_status,
298 &outbuf,
299 output_token,
300 "\x03\x00",
301 GSS_KRB5_MECHANISM);
302 krb5_data_free (&outbuf);
303 if (ret)
304 return ret;
306 *minor_status = 0;
307 return GSS_S_CONTINUE_NEEDED;
311 static OM_uint32
312 gsskrb5_acceptor_start(OM_uint32 * minor_status,
313 gsskrb5_ctx ctx,
314 krb5_context context,
315 const gss_cred_id_t acceptor_cred_handle,
316 const gss_buffer_t input_token_buffer,
317 const gss_channel_bindings_t input_chan_bindings,
318 gss_name_t * src_name,
319 gss_OID * mech_type,
320 gss_buffer_t output_token,
321 OM_uint32 * ret_flags,
322 OM_uint32 * time_rec,
323 gss_cred_id_t * delegated_cred_handle)
325 krb5_error_code kret;
326 OM_uint32 ret = GSS_S_COMPLETE;
327 krb5_data indata;
328 krb5_flags ap_options;
329 krb5_keytab keytab = NULL;
330 int is_cfx = 0;
331 const gsskrb5_cred acceptor_cred = (gsskrb5_cred)acceptor_cred_handle;
334 * We may, or may not, have an escapsulation.
336 ret = _gsskrb5_decapsulate (minor_status,
337 input_token_buffer,
338 &indata,
339 "\x01\x00",
340 GSS_KRB5_MECHANISM);
342 if (ret) {
343 /* Assume that there is no OID wrapping. */
344 indata.length = input_token_buffer->length;
345 indata.data = input_token_buffer->value;
349 * We need to get our keytab
351 if (acceptor_cred == NULL) {
352 if (_gsskrb5_keytab != NULL)
353 keytab = _gsskrb5_keytab;
354 } else if (acceptor_cred->keytab != NULL) {
355 keytab = acceptor_cred->keytab;
359 * We need to check the ticket and create the AP-REP packet
363 krb5_rd_req_in_ctx in = NULL;
364 krb5_rd_req_out_ctx out = NULL;
365 krb5_principal server = NULL;
367 if (acceptor_cred)
368 server = acceptor_cred->principal;
370 kret = krb5_rd_req_in_ctx_alloc(context, &in);
371 if (kret == 0)
372 kret = krb5_rd_req_in_set_keytab(context, in, keytab);
373 if (kret) {
374 if (in)
375 krb5_rd_req_in_ctx_free(context, in);
376 *minor_status = kret;
377 return GSS_S_FAILURE;
380 kret = krb5_rd_req_ctx(context,
381 &ctx->auth_context,
382 &indata,
383 server,
384 in, &out);
385 krb5_rd_req_in_ctx_free(context, in);
386 if (kret == KRB5KRB_AP_ERR_SKEW || kret == KRB5KRB_AP_ERR_TKT_NYV) {
388 * No reply in non-MUTUAL mode, but we don't know that its
389 * non-MUTUAL mode yet, thats inside the 8003 checksum, so
390 * lets only send the error token on clock skew, that
391 * limit when send error token for non-MUTUAL.
393 return send_error_token(minor_status, context, kret,
394 server, &indata, output_token);
395 } else if (kret) {
396 *minor_status = kret;
397 return GSS_S_FAILURE;
401 * we need to remember some data on the context_handle.
403 kret = krb5_rd_req_out_get_ap_req_options(context, out,
404 &ap_options);
405 if (kret == 0)
406 kret = krb5_rd_req_out_get_ticket(context, out,
407 &ctx->ticket);
408 if (kret == 0)
409 kret = krb5_rd_req_out_get_keyblock(context, out,
410 &ctx->service_keyblock);
411 ctx->lifetime = ctx->ticket->ticket.endtime;
413 krb5_rd_req_out_ctx_free(context, out);
414 if (kret) {
415 ret = GSS_S_FAILURE;
416 *minor_status = kret;
417 return ret;
423 * We need to copy the principal names to the context and the
424 * calling layer.
426 kret = krb5_copy_principal(context,
427 ctx->ticket->client,
428 &ctx->source);
429 if (kret) {
430 ret = GSS_S_FAILURE;
431 *minor_status = kret;
434 kret = krb5_copy_principal(context,
435 ctx->ticket->server,
436 &ctx->target);
437 if (kret) {
438 ret = GSS_S_FAILURE;
439 *minor_status = kret;
440 return ret;
444 * We need to setup some compat stuff, this assumes that
445 * context_handle->target is already set.
447 ret = _gss_DES3_get_mic_compat(minor_status, ctx, context);
448 if (ret)
449 return ret;
451 if (src_name != NULL) {
452 kret = krb5_copy_principal (context,
453 ctx->ticket->client,
454 (gsskrb5_name*)src_name);
455 if (kret) {
456 ret = GSS_S_FAILURE;
457 *minor_status = kret;
458 return ret;
463 * We need to get the flags out of the 8003 checksum.
466 krb5_authenticator authenticator;
468 kret = krb5_auth_con_getauthenticator(context,
469 ctx->auth_context,
470 &authenticator);
471 if(kret) {
472 ret = GSS_S_FAILURE;
473 *minor_status = kret;
474 return ret;
477 if (authenticator->cksum->cksumtype == CKSUMTYPE_GSSAPI) {
478 ret = _gsskrb5_verify_8003_checksum(minor_status,
479 input_chan_bindings,
480 authenticator->cksum,
481 &ctx->flags,
482 &ctx->fwd_data);
484 krb5_free_authenticator(context, &authenticator);
485 if (ret) {
486 return ret;
488 } else {
489 krb5_crypto crypto;
491 kret = krb5_crypto_init(context,
492 ctx->auth_context->keyblock,
493 0, &crypto);
494 if(kret) {
495 krb5_free_authenticator(context, &authenticator);
497 ret = GSS_S_FAILURE;
498 *minor_status = kret;
499 return ret;
503 * Windows accepts Samba3's use of a kerberos, rather than
504 * GSSAPI checksum here
507 kret = krb5_verify_checksum(context,
508 crypto, KRB5_KU_AP_REQ_AUTH_CKSUM, NULL, 0,
509 authenticator->cksum);
510 krb5_free_authenticator(context, &authenticator);
511 krb5_crypto_destroy(context, crypto);
513 if(kret) {
514 ret = GSS_S_BAD_SIG;
515 *minor_status = kret;
516 return ret;
520 * Samba style get some flags (but not DCE-STYLE), use
521 * ap_options to guess the mutual flag.
523 ctx->flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
524 if (ap_options & AP_OPTS_MUTUAL_REQUIRED)
525 ctx->flags |= GSS_C_MUTUAL_FLAG;
529 if(ctx->flags & GSS_C_MUTUAL_FLAG) {
530 krb5_data outbuf;
531 int use_subkey = 0;
533 _gsskrb5i_is_cfx(context, ctx, 1);
534 is_cfx = (ctx->more_flags & IS_CFX);
536 if (is_cfx || (ap_options & AP_OPTS_USE_SUBKEY)) {
537 use_subkey = 1;
538 } else {
539 krb5_keyblock *rkey;
542 * If there is a initiator subkey, copy that to acceptor
543 * subkey to match Windows behavior
545 kret = krb5_auth_con_getremotesubkey(context,
546 ctx->auth_context,
547 &rkey);
548 if (kret == 0) {
549 kret = krb5_auth_con_setlocalsubkey(context,
550 ctx->auth_context,
551 rkey);
552 if (kret == 0)
553 use_subkey = 1;
554 krb5_free_keyblock(context, rkey);
557 if (use_subkey) {
558 ctx->more_flags |= ACCEPTOR_SUBKEY;
559 krb5_auth_con_addflags(context, ctx->auth_context,
560 KRB5_AUTH_CONTEXT_USE_SUBKEY,
561 NULL);
564 kret = krb5_mk_rep(context,
565 ctx->auth_context,
566 &outbuf);
567 if (kret) {
568 *minor_status = kret;
569 return GSS_S_FAILURE;
572 if (IS_DCE_STYLE(ctx)) {
573 output_token->length = outbuf.length;
574 output_token->value = outbuf.data;
575 } else {
576 ret = _gsskrb5_encapsulate(minor_status,
577 &outbuf,
578 output_token,
579 "\x02\x00",
580 GSS_KRB5_MECHANISM);
581 krb5_data_free (&outbuf);
582 if (ret)
583 return ret;
587 ctx->flags |= GSS_C_TRANS_FLAG;
589 /* Remember the flags */
591 ctx->lifetime = ctx->ticket->ticket.endtime;
592 ctx->more_flags |= OPEN;
594 if (mech_type)
595 *mech_type = GSS_KRB5_MECHANISM;
597 if (time_rec) {
598 ret = _gsskrb5_lifetime_left(minor_status,
599 context,
600 ctx->lifetime,
601 time_rec);
602 if (ret) {
603 return ret;
608 * When GSS_C_DCE_STYLE is in use, we need ask for a AP-REP from
609 * the client.
611 if (IS_DCE_STYLE(ctx)) {
613 * Return flags to caller, but we haven't processed
614 * delgations yet
616 if (ret_flags)
617 *ret_flags = (ctx->flags & ~GSS_C_DELEG_FLAG);
619 ctx->state = ACCEPTOR_WAIT_FOR_DCESTYLE;
620 return GSS_S_CONTINUE_NEEDED;
623 ret = gsskrb5_acceptor_ready(minor_status, ctx, context,
624 delegated_cred_handle);
626 if (ret_flags)
627 *ret_flags = ctx->flags;
629 return ret;
632 static OM_uint32
633 acceptor_wait_for_dcestyle(OM_uint32 * minor_status,
634 gsskrb5_ctx ctx,
635 krb5_context context,
636 const gss_cred_id_t acceptor_cred_handle,
637 const gss_buffer_t input_token_buffer,
638 const gss_channel_bindings_t input_chan_bindings,
639 gss_name_t * src_name,
640 gss_OID * mech_type,
641 gss_buffer_t output_token,
642 OM_uint32 * ret_flags,
643 OM_uint32 * time_rec,
644 gss_cred_id_t * delegated_cred_handle)
646 OM_uint32 ret;
647 krb5_error_code kret;
648 krb5_data inbuf;
649 int32_t r_seq_number, l_seq_number;
652 * We know it's GSS_C_DCE_STYLE so we don't need to decapsulate the AP_REP
655 inbuf.length = input_token_buffer->length;
656 inbuf.data = input_token_buffer->value;
659 * We need to remeber the old remote seq_number, then check if the
660 * client has replied with our local seq_number, and then reset
661 * the remote seq_number to the old value
664 kret = krb5_auth_con_getlocalseqnumber(context,
665 ctx->auth_context,
666 &l_seq_number);
667 if (kret) {
668 *minor_status = kret;
669 return GSS_S_FAILURE;
672 kret = krb5_auth_getremoteseqnumber(context,
673 ctx->auth_context,
674 &r_seq_number);
675 if (kret) {
676 *minor_status = kret;
677 return GSS_S_FAILURE;
680 kret = krb5_auth_con_setremoteseqnumber(context,
681 ctx->auth_context,
682 l_seq_number);
683 if (kret) {
684 *minor_status = kret;
685 return GSS_S_FAILURE;
690 * We need to verify the AP_REP, but we need to flag that this is
691 * DCE_STYLE, so don't check the timestamps this time, but put the
692 * flag DO_TIME back afterward.
695 krb5_ap_rep_enc_part *repl;
696 int32_t auth_flags;
698 krb5_auth_con_removeflags(context,
699 ctx->auth_context,
700 KRB5_AUTH_CONTEXT_DO_TIME,
701 &auth_flags);
703 kret = krb5_rd_rep(context, ctx->auth_context, &inbuf, &repl);
704 if (kret) {
705 *minor_status = kret;
706 return GSS_S_FAILURE;
708 krb5_free_ap_rep_enc_part(context, repl);
709 krb5_auth_con_setflags(context, ctx->auth_context, auth_flags);
712 /* We need to check the liftime */
714 OM_uint32 lifetime_rec;
716 ret = _gsskrb5_lifetime_left(minor_status,
717 context,
718 ctx->lifetime,
719 &lifetime_rec);
720 if (ret) {
721 return ret;
723 if (lifetime_rec == 0) {
724 return GSS_S_CONTEXT_EXPIRED;
727 if (time_rec) *time_rec = lifetime_rec;
730 /* We need to give the caller the flags which are in use */
731 if (ret_flags) *ret_flags = ctx->flags;
733 if (src_name) {
734 kret = krb5_copy_principal(context,
735 ctx->source,
736 (gsskrb5_name*)src_name);
737 if (kret) {
738 *minor_status = kret;
739 return GSS_S_FAILURE;
744 * After the krb5_rd_rep() the remote and local seq_number should
745 * be the same, because the client just replies the seq_number
746 * from our AP-REP in its AP-REP, but then the client uses the
747 * seq_number from its AP-REQ for GSS_wrap()
750 int32_t tmp_r_seq_number, tmp_l_seq_number;
752 kret = krb5_auth_getremoteseqnumber(context,
753 ctx->auth_context,
754 &tmp_r_seq_number);
755 if (kret) {
756 *minor_status = kret;
757 return GSS_S_FAILURE;
760 kret = krb5_auth_con_getlocalseqnumber(context,
761 ctx->auth_context,
762 &tmp_l_seq_number);
763 if (kret) {
765 *minor_status = kret;
766 return GSS_S_FAILURE;
770 * Here we check if the client has responsed with our local seq_number,
772 if (tmp_r_seq_number != tmp_l_seq_number) {
773 return GSS_S_UNSEQ_TOKEN;
778 * We need to reset the remote seq_number, because the client will use,
779 * the old one for the GSS_wrap() calls
782 kret = krb5_auth_con_setremoteseqnumber(context,
783 ctx->auth_context,
784 r_seq_number);
785 if (kret) {
786 *minor_status = kret;
787 return GSS_S_FAILURE;
791 return gsskrb5_acceptor_ready(minor_status, ctx, context,
792 delegated_cred_handle);
796 OM_uint32
797 _gsskrb5_accept_sec_context(OM_uint32 * minor_status,
798 gss_ctx_id_t * context_handle,
799 const gss_cred_id_t acceptor_cred_handle,
800 const gss_buffer_t input_token_buffer,
801 const gss_channel_bindings_t input_chan_bindings,
802 gss_name_t * src_name,
803 gss_OID * mech_type,
804 gss_buffer_t output_token,
805 OM_uint32 * ret_flags,
806 OM_uint32 * time_rec,
807 gss_cred_id_t * delegated_cred_handle)
809 krb5_context context;
810 OM_uint32 ret;
811 gsskrb5_ctx ctx;
813 GSSAPI_KRB5_INIT(&context);
815 output_token->length = 0;
816 output_token->value = NULL;
818 if (src_name != NULL)
819 *src_name = NULL;
820 if (mech_type)
821 *mech_type = GSS_KRB5_MECHANISM;
823 if (*context_handle == GSS_C_NO_CONTEXT) {
824 ret = _gsskrb5_create_ctx(minor_status,
825 context_handle,
826 context,
827 input_chan_bindings,
828 ACCEPTOR_START);
829 if (ret)
830 return ret;
833 ctx = (gsskrb5_ctx)*context_handle;
837 * TODO: check the channel_bindings
838 * (above just sets them to krb5 layer)
841 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
843 switch (ctx->state) {
844 case ACCEPTOR_START:
845 ret = gsskrb5_acceptor_start(minor_status,
846 ctx,
847 context,
848 acceptor_cred_handle,
849 input_token_buffer,
850 input_chan_bindings,
851 src_name,
852 mech_type,
853 output_token,
854 ret_flags,
855 time_rec,
856 delegated_cred_handle);
857 break;
858 case ACCEPTOR_WAIT_FOR_DCESTYLE:
859 ret = acceptor_wait_for_dcestyle(minor_status,
860 ctx,
861 context,
862 acceptor_cred_handle,
863 input_token_buffer,
864 input_chan_bindings,
865 src_name,
866 mech_type,
867 output_token,
868 ret_flags,
869 time_rec,
870 delegated_cred_handle);
871 break;
872 case ACCEPTOR_READY:
874 * If we get there, the caller have called
875 * gss_accept_sec_context() one time too many.
877 ret = GSS_S_BAD_STATUS;
878 break;
879 default:
880 /* TODO: is this correct here? --metze */
881 ret = GSS_S_BAD_STATUS;
882 break;
885 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
887 if (GSS_ERROR(ret)) {
888 OM_uint32 min2;
889 _gsskrb5_delete_sec_context(&min2, context_handle, GSS_C_NO_BUFFER);
892 return ret;