Debian Codegraph @

lib/gssapi/krb5/accept_sec_context.c

raw
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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 "gsskrb5_locl.h"
35
36 HEIMDAL_MUTEX gssapi_keytab_mutex = HEIMDAL_MUTEX_INITIALIZER;
37 krb5_keytab _gsskrb5_keytab;
38
39 static krb5_error_code
40 validate_keytab(krb5_context context, const char *name, krb5_keytab *id)
41 {
42 krb5_error_code ret;
43
44 ret = krb5_kt_resolve(context, name, id);
45 if (ret)
46 return ret;
47
48 ret = krb5_kt_have_content(context, *id);
49 if (ret) {
50 krb5_kt_close(context, *id);
51 *id = NULL;
52 }
53
54 return ret;
55 }
56
57 OM_uint32
58 _gsskrb5_register_acceptor_identity(OM_uint32 *min_stat, const char *identity)
59 {
60 krb5_context context;
61 krb5_error_code ret;
62
63 *min_stat = 0;
64
65 ret = _gsskrb5_init(&context);
66 if(ret)
67 return GSS_S_FAILURE;
68
69 HEIMDAL_MUTEX_lock(&gssapi_keytab_mutex);
70
71 if(_gsskrb5_keytab != NULL) {
72 krb5_kt_close(context, _gsskrb5_keytab);
73 _gsskrb5_keytab = NULL;
74 }
75 if (identity == NULL) {
76 ret = krb5_kt_default(context, &_gsskrb5_keytab);
77 } else {
78 /*
79 * First check if we can the keytab as is and if it has content...
80 */
81 ret = validate_keytab(context, identity, &_gsskrb5_keytab);
82 /*
83 * if it doesn't, lets prepend FILE: and try again
84 */
85 if (ret) {
86 char *p = NULL;
87 ret = asprintf(&p, "FILE:%s", identity);
88 if(ret < 0 || p == NULL) {
89 HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
90 return GSS_S_FAILURE;
91 }
92 ret = validate_keytab(context, p, &_gsskrb5_keytab);
93 free(p);
94 }
95 }
96 HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
97 if(ret) {
98 *min_stat = ret;
99 return GSS_S_FAILURE;
100 }
101 return GSS_S_COMPLETE;
102 }
103
104 void
105 _gsskrb5i_is_cfx(krb5_context context, gsskrb5_ctx ctx, int acceptor)
106 {
107 krb5_keyblock *key;
108
109 if (acceptor) {
110 if (ctx->auth_context->local_subkey)
111 key = ctx->auth_context->local_subkey;
112 else
113 key = ctx->auth_context->remote_subkey;
114 } else {
115 if (ctx->auth_context->remote_subkey)
116 key = ctx->auth_context->remote_subkey;
117 else
118 key = ctx->auth_context->local_subkey;
119 }
120 if (key == NULL)
121 key = ctx->auth_context->keyblock;
122
123 if (key == NULL)
124 return;
125
126 switch (key->keytype) {
127 case ETYPE_DES_CBC_CRC:
128 case ETYPE_DES_CBC_MD4:
129 case ETYPE_DES_CBC_MD5:
130 case ETYPE_DES3_CBC_MD5:
131 case ETYPE_OLD_DES3_CBC_SHA1:
132 case ETYPE_DES3_CBC_SHA1:
133 case ETYPE_ARCFOUR_HMAC_MD5:
134 case ETYPE_ARCFOUR_HMAC_MD5_56:
135 break;
136 default :
137 ctx->more_flags |= IS_CFX;
138
139 if ((acceptor && ctx->auth_context->local_subkey) ||
140 (!acceptor && ctx->auth_context->remote_subkey))
141 ctx->more_flags |= ACCEPTOR_SUBKEY;
142 break;
143 }
144 if (ctx->crypto)
145 krb5_crypto_destroy(context, ctx->crypto);
146 /* XXX We really shouldn't ignore this; will come back to this */
147 (void) krb5_crypto_init(context, key, 0, &ctx->crypto);
148 }
149
150
151 static OM_uint32
152 gsskrb5_accept_delegated_token
153 (OM_uint32 * minor_status,
154 gsskrb5_ctx ctx,
155 krb5_context context,
156 gss_cred_id_t * delegated_cred_handle
157 )
158 {
159 krb5_ccache ccache = NULL;
160 krb5_error_code kret;
161 int32_t ac_flags, ret = GSS_S_COMPLETE;
162
163 *minor_status = 0;
164
165 /* XXX Create a new delegated_cred_handle? */
166 if (delegated_cred_handle == NULL) {
167 ret = GSS_S_COMPLETE;
168 goto out;
169 }
170
171 *delegated_cred_handle = NULL;
172 kret = krb5_cc_new_unique (context, krb5_cc_type_memory,
173 NULL, &ccache);
174 if (kret) {
175 ctx->flags &= ~GSS_C_DELEG_FLAG;
176 goto out;
177 }
178
179 kret = krb5_cc_initialize(context, ccache, ctx->source);
180 if (kret) {
181 ctx->flags &= ~GSS_C_DELEG_FLAG;
182 goto out;
183 }
184
185 krb5_auth_con_removeflags(context,
186 ctx->auth_context,
187 KRB5_AUTH_CONTEXT_DO_TIME,
188 &ac_flags);
189 kret = krb5_rd_cred2(context,
190 ctx->auth_context,
191 ccache,
192 &ctx->fwd_data);
193 krb5_auth_con_setflags(context,
194 ctx->auth_context,
195 ac_flags);
196 if (kret) {
197 ctx->flags &= ~GSS_C_DELEG_FLAG;
198 ret = GSS_S_FAILURE;
199 *minor_status = kret;
200 goto out;
201 }
202
203 if (delegated_cred_handle) {
204 gsskrb5_cred handle;
205
206 ret = _gsskrb5_krb5_import_cred(minor_status,
207 ccache,
208 NULL,
209 NULL,
210 delegated_cred_handle);
211 if (ret != GSS_S_COMPLETE)
212 goto out;
213
214 handle = (gsskrb5_cred) *delegated_cred_handle;
215
216 handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
217 krb5_cc_close(context, ccache);
218 ccache = NULL;
219 }
220
221 out:
222 if (ccache) {
223 /* Don't destroy the default cred cache */
224 if (delegated_cred_handle == NULL)
225 krb5_cc_close(context, ccache);
226 else
227 krb5_cc_destroy(context, ccache);
228 }
229 return ret;
230 }
231
232 static OM_uint32
233 gsskrb5_acceptor_ready(OM_uint32 * minor_status,
234 gsskrb5_ctx ctx,
235 krb5_context context,
236 gss_cred_id_t *delegated_cred_handle)
237 {
238 OM_uint32 ret;
239 int32_t seq_number;
240 int is_cfx = 0;
241
242 krb5_auth_con_getremoteseqnumber (context,
243 ctx->auth_context,
244 &seq_number);
245
246 _gsskrb5i_is_cfx(context, ctx, 1);
247 is_cfx = (ctx->more_flags & IS_CFX);
248
249 ret = _gssapi_msg_order_create(minor_status,
250 &ctx->order,
251 _gssapi_msg_order_f(ctx->flags),
252 seq_number, 0, is_cfx);
253 if (ret)
254 return ret;
255
256 /*
257 * If requested, set local sequence num to remote sequence if this
258 * isn't a mutual authentication context
259 */
260 if (!(ctx->flags & GSS_C_MUTUAL_FLAG) && _gssapi_msg_order_f(ctx->flags)) {
261 krb5_auth_con_setlocalseqnumber(context,
262 ctx->auth_context,
263 seq_number);
264 }
265
266 /*
267 * We should handle the delegation ticket, in case it's there
268 */
269 if (ctx->fwd_data.length > 0 && (ctx->flags & GSS_C_DELEG_FLAG)) {
270 ret = gsskrb5_accept_delegated_token(minor_status,
271 ctx,
272 context,
273 delegated_cred_handle);
274 if (ret != GSS_S_COMPLETE)
275 return ret;
276 } else {
277 /* Well, looks like it wasn't there after all */
278 ctx->flags &= ~GSS_C_DELEG_FLAG;
279 }
280
281 ctx->state = ACCEPTOR_READY;
282 ctx->more_flags |= OPEN;
283
284 return GSS_S_COMPLETE;
285 }
286
287 static OM_uint32
288 send_error_token(OM_uint32 *minor_status,
289 krb5_context context,
290 krb5_error_code kret,
291 krb5_principal server,
292 krb5_data *indata,
293 gss_buffer_t output_token)
294 {
295 krb5_principal ap_req_server = NULL;
296 krb5_error_code ret;
297 krb5_data outbuf;
298 /* this e_data value encodes KERB_AP_ERR_TYPE_SKEW_RECOVERY which
299 tells windows to try again with the corrected timestamp. See
300 [MS-KILE] 2.2.1 KERB-ERROR-DATA */
301 krb5_data e_data = { 7, rk_UNCONST("\x30\x05\xa1\x03\x02\x01\x02") };
302
303 /* build server from request if the acceptor had not selected one */
304 if (server == NULL) {
305 AP_REQ ap_req;
306
307 ret = krb5_decode_ap_req(context, indata, &ap_req);
308 if (ret) {
309 *minor_status = ret;
310 return GSS_S_FAILURE;
311 }
312 ret = _krb5_principalname2krb5_principal(context,
313 &ap_req_server,
314 ap_req.ticket.sname,
315 ap_req.ticket.realm);
316 free_AP_REQ(&ap_req);
317 if (ret) {
318 *minor_status = ret;
319 return GSS_S_FAILURE;
320 }
321 server = ap_req_server;
322 }
323
324 ret = krb5_mk_error(context, kret, NULL, &e_data, NULL,
325 server, NULL, NULL, &outbuf);
326 if (ap_req_server)
327 krb5_free_principal(context, ap_req_server);
328 if (ret) {
329 *minor_status = ret;
330 return GSS_S_FAILURE;
331 }
332
333 ret = _gsskrb5_encapsulate(minor_status,
334 &outbuf,
335 output_token,
336 "\x03\x00",
337 GSS_KRB5_MECHANISM);
338 krb5_data_free (&outbuf);
339 if (ret)
340 return ret;
341
342 *minor_status = 0;
343 return GSS_S_CONTINUE_NEEDED;
344 }
345
346
347 static OM_uint32
348 gsskrb5_acceptor_start(OM_uint32 * minor_status,
349 gsskrb5_ctx ctx,
350 krb5_context context,
351 gss_const_cred_id_t acceptor_cred_handle,
352 const gss_buffer_t input_token_buffer,
353 const gss_channel_bindings_t input_chan_bindings,
354 gss_name_t * src_name,
355 gss_OID * mech_type,
356 gss_buffer_t output_token,
357 OM_uint32 * ret_flags,
358 OM_uint32 * time_rec,
359 gss_cred_id_t * delegated_cred_handle)
360 {
361 krb5_error_code kret;
362 OM_uint32 ret = GSS_S_COMPLETE;
363 krb5_data indata;
364 krb5_flags ap_options;
365 krb5_keytab keytab = NULL;
366 int is_cfx = 0;
367 int close_kt = 0;
368 const gsskrb5_cred acceptor_cred = (gsskrb5_cred)acceptor_cred_handle;
369
370 /*
371 * We may, or may not, have an escapsulation.
372 */
373 ret = _gsskrb5_decapsulate (minor_status,
374 input_token_buffer,
375 &indata,
376 "\x01\x00",
377 GSS_KRB5_MECHANISM);
378
379 if (ret) {
380 /* Assume that there is no OID wrapping. */
381 indata.length = input_token_buffer->length;
382 indata.data = input_token_buffer->value;
383 }
384
385 /*
386 * We need to get our keytab
387 */
388 if (acceptor_cred == NULL) {
389 HEIMDAL_MUTEX_lock(&gssapi_keytab_mutex);
390 if (_gsskrb5_keytab != NULL) {
391 char *name = NULL;
392 kret = krb5_kt_get_full_name(context, _gsskrb5_keytab, &name);
393 if (kret == 0) {
394 kret = krb5_kt_resolve(context, name, &keytab);
395 krb5_xfree(name);
396 }
397 if (kret == 0)
398 close_kt = 1;
399 else
400 keytab = NULL;
401 }
402 HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
403 } else if (acceptor_cred->keytab != NULL) {
404 keytab = acceptor_cred->keytab;
405 }
406
407 /*
408 * We need to check the ticket and create the AP-REP packet
409 */
410
411 {
412 krb5_rd_req_in_ctx in = NULL;
413 krb5_rd_req_out_ctx out = NULL;
414 krb5_principal server = NULL;
415
416 if (acceptor_cred)
417 server = acceptor_cred->principal;
418
419 kret = krb5_rd_req_in_ctx_alloc(context, &in);
420 if (kret == 0)
421 kret = krb5_rd_req_in_set_keytab(context, in, keytab);
422 if (kret) {
423 if (in)
424 krb5_rd_req_in_ctx_free(context, in);
425 if (close_kt)
426 krb5_kt_close(context, keytab);
427 *minor_status = kret;
428 return GSS_S_FAILURE;
429 }
430
431 kret = krb5_rd_req_ctx(context,
432 &ctx->auth_context,
433 &indata,
434 server,
435 in, &out);
436 krb5_rd_req_in_ctx_free(context, in);
437 if (close_kt)
438 krb5_kt_close(context, keytab);
439 if (kret == KRB5KRB_AP_ERR_SKEW || kret == KRB5KRB_AP_ERR_TKT_NYV) {
440 /*
441 * No reply in non-MUTUAL mode, but we don't know that its
442 * non-MUTUAL mode yet, thats inside the 8003 checksum, so
443 * lets only send the error token on clock skew, that
444 * limit when send error token for non-MUTUAL.
445 */
446 free_Authenticator(ctx->auth_context->authenticator);
447 return send_error_token(minor_status, context, kret,
448 server, &indata, output_token);
449 } else if (kret) {
450 *minor_status = kret;
451 return GSS_S_FAILURE;
452 }
453
454 /*
455 * we need to remember some data on the context_handle.
456 */
457 kret = krb5_rd_req_out_get_ap_req_options(context, out,
458 &ap_options);
459 if (kret == 0)
460 kret = krb5_rd_req_out_get_ticket(context, out,
461 &ctx->ticket);
462 if (kret == 0)
463 kret = krb5_rd_req_out_get_keyblock(context, out,
464 &ctx->service_keyblock);
465 ctx->endtime = ctx->ticket->ticket.endtime;
466
467 krb5_rd_req_out_ctx_free(context, out);
468 if (kret) {
469 ret = GSS_S_FAILURE;
470 *minor_status = kret;
471 return ret;
472 }
473 }
474
475
476 /*
477 * We need to copy the principal names to the context and the
478 * calling layer.
479 */
480 kret = krb5_copy_principal(context,
481 ctx->ticket->client,
482 &ctx->source);
483 if (kret) {
484 ret = GSS_S_FAILURE;
485 *minor_status = kret;
486 return ret;
487 }
488
489 kret = krb5_copy_principal(context,
490 ctx->ticket->server,
491 &ctx->target);
492 if (kret) {
493 ret = GSS_S_FAILURE;
494 *minor_status = kret;
495 return ret;
496 }
497
498 /*
499 * We need to setup some compat stuff, this assumes that
500 * context_handle->target is already set.
501 */
502 ret = _gss_DES3_get_mic_compat(minor_status, ctx, context);
503 if (ret)
504 return ret;
505
506 if (src_name != NULL) {
507 kret = krb5_copy_principal (context,
508 ctx->ticket->client,
509 (gsskrb5_name*)src_name);
510 if (kret) {
511 ret = GSS_S_FAILURE;
512 *minor_status = kret;
513 return ret;
514 }
515 }
516
517 /*
518 * We need to get the flags out of the 8003 checksum.
519 */
520
521 {
522 krb5_authenticator authenticator;
523
524 kret = krb5_auth_con_getauthenticator(context,
525 ctx->auth_context,
526 &authenticator);
527 if(kret) {
528 ret = GSS_S_FAILURE;
529 *minor_status = kret;
530 return ret;
531 }
532
533 if (authenticator->cksum != NULL
534 && authenticator->cksum->cksumtype == CKSUMTYPE_GSSAPI) {
535 ret = _gsskrb5_verify_8003_checksum(minor_status,
536 input_chan_bindings,
537 authenticator->cksum,
538 &ctx->flags,
539 &ctx->fwd_data);
540
541 if (ret) {
542 krb5_free_authenticator(context, &authenticator);
543 return ret;
544 }
545 } else {
546 if (authenticator->cksum != NULL) {
547 krb5_crypto crypto;
548
549 kret = krb5_crypto_init(context,
550 ctx->auth_context->keyblock,
551 0, &crypto);
552 if (kret) {
553 krb5_free_authenticator(context, &authenticator);
554 ret = GSS_S_FAILURE;
555 *minor_status = kret;
556 return ret;
557 }
558
559 /*
560 * Windows accepts Samba3's use of a kerberos, rather than
561 * GSSAPI checksum here
562 */
563
564 kret = krb5_verify_checksum(context,
565 crypto, KRB5_KU_AP_REQ_AUTH_CKSUM, NULL, 0,
566 authenticator->cksum);
567 krb5_crypto_destroy(context, crypto);
568
569 if (kret) {
570 krb5_free_authenticator(context, &authenticator);
571 ret = GSS_S_BAD_SIG;
572 *minor_status = kret;
573 return ret;
574 }
575 }
576
577 /*
578 * If there is no checksum or a kerberos checksum (which Windows
579 * and Samba accept), we use the ap_options to guess the mutual
580 * flag.
581 */
582
583 ctx->flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
584 if (ap_options & AP_OPTS_MUTUAL_REQUIRED)
585 ctx->flags |= GSS_C_MUTUAL_FLAG;
586 }
587 krb5_free_authenticator(context, &authenticator);
588 }
589
590 if(ctx->flags & GSS_C_MUTUAL_FLAG) {
591 krb5_data outbuf;
592 int use_subkey = 0;
593
594 _gsskrb5i_is_cfx(context, ctx, 1);
595 is_cfx = (ctx->more_flags & IS_CFX);
596
597 if (is_cfx || (ap_options & AP_OPTS_USE_SUBKEY)) {
598 use_subkey = 1;
599 } else {
600 krb5_keyblock *rkey;
601
602 /*
603 * If there is a initiator subkey, copy that to acceptor
604 * subkey to match Windows behavior
605 */
606 kret = krb5_auth_con_getremotesubkey(context,
607 ctx->auth_context,
608 &rkey);
609 if (kret == 0) {
610 kret = krb5_auth_con_setlocalsubkey(context,
611 ctx->auth_context,
612 rkey);
613 if (kret == 0)
614 use_subkey = 1;
615 }
616 krb5_free_keyblock(context, rkey);
617 }
618 if (use_subkey) {
619 ctx->more_flags |= ACCEPTOR_SUBKEY;
620 krb5_auth_con_addflags(context, ctx->auth_context,
621 KRB5_AUTH_CONTEXT_USE_SUBKEY,
622 NULL);
623 }
624
625 kret = krb5_mk_rep(context,
626 ctx->auth_context,
627 &outbuf);
628 if (kret) {
629 *minor_status = kret;
630 return GSS_S_FAILURE;
631 }
632
633 if (IS_DCE_STYLE(ctx)) {
634 output_token->length = outbuf.length;
635 output_token->value = outbuf.data;
636 } else {
637 ret = _gsskrb5_encapsulate(minor_status,
638 &outbuf,
639 output_token,
640 "\x02\x00",
641 GSS_KRB5_MECHANISM);
642 krb5_data_free (&outbuf);
643 if (ret)
644 return ret;
645 }
646 }
647
648 ctx->flags |= GSS_C_TRANS_FLAG;
649
650 /* Remember the flags */
651
652 ctx->endtime = ctx->ticket->ticket.endtime;
653 ctx->more_flags |= OPEN;
654
655 if (mech_type)
656 *mech_type = GSS_KRB5_MECHANISM;
657
658 if (time_rec) {
659 ret = _gsskrb5_lifetime_left(minor_status,
660 context,
661 ctx->endtime,
662 time_rec);
663 if (ret) {
664 return ret;
665 }
666 }
667
668 /*
669 * When GSS_C_DCE_STYLE is in use, we need ask for a AP-REP from
670 * the client.
671 */
672 if (IS_DCE_STYLE(ctx)) {
673 /*
674 * Return flags to caller, but we haven't processed
675 * delgations yet
676 */
677 if (ret_flags)
678 *ret_flags = (ctx->flags & ~GSS_C_DELEG_FLAG);
679
680 ctx->state = ACCEPTOR_WAIT_FOR_DCESTYLE;
681 return GSS_S_CONTINUE_NEEDED;
682 }
683
684 ret = gsskrb5_acceptor_ready(minor_status, ctx, context,
685 delegated_cred_handle);
686
687 if (ret_flags)
688 *ret_flags = ctx->flags;
689
690 return ret;
691 }
692
693 static OM_uint32
694 acceptor_wait_for_dcestyle(OM_uint32 * minor_status,
695 gsskrb5_ctx ctx,
696 krb5_context context,
697 gss_const_cred_id_t acceptor_cred_handle,
698 const gss_buffer_t input_token_buffer,
699 const gss_channel_bindings_t input_chan_bindings,
700 gss_name_t * src_name,
701 gss_OID * mech_type,
702 gss_buffer_t output_token,
703 OM_uint32 * ret_flags,
704 OM_uint32 * time_rec,
705 gss_cred_id_t * delegated_cred_handle)
706 {
707 OM_uint32 ret;
708 krb5_error_code kret;
709 krb5_data inbuf;
710 int32_t r_seq_number, l_seq_number;
711
712 /*
713 * We know it's GSS_C_DCE_STYLE so we don't need to decapsulate the AP_REP
714 */
715
716 inbuf.length = input_token_buffer->length;
717 inbuf.data = input_token_buffer->value;
718
719 /*
720 * We need to remeber the old remote seq_number, then check if the
721 * client has replied with our local seq_number, and then reset
722 * the remote seq_number to the old value
723 */
724 {
725 kret = krb5_auth_con_getlocalseqnumber(context,
726 ctx->auth_context,
727 &l_seq_number);
728 if (kret) {
729 *minor_status = kret;
730 return GSS_S_FAILURE;
731 }
732
733 kret = krb5_auth_con_getremoteseqnumber(context,
734 ctx->auth_context,
735 &r_seq_number);
736 if (kret) {
737 *minor_status = kret;
738 return GSS_S_FAILURE;
739 }
740
741 kret = krb5_auth_con_setremoteseqnumber(context,
742 ctx->auth_context,
743 l_seq_number);
744 if (kret) {
745 *minor_status = kret;
746 return GSS_S_FAILURE;
747 }
748 }
749
750 /*
751 * We need to verify the AP_REP, but we need to flag that this is
752 * DCE_STYLE, so don't check the timestamps this time, but put the
753 * flag DO_TIME back afterward.
754 */
755 {
756 krb5_ap_rep_enc_part *repl;
757 int32_t auth_flags;
758
759 krb5_auth_con_removeflags(context,
760 ctx->auth_context,
761 KRB5_AUTH_CONTEXT_DO_TIME,
762 &auth_flags);
763
764 kret = krb5_rd_rep(context, ctx->auth_context, &inbuf, &repl);
765 if (kret) {
766 *minor_status = kret;
767 return GSS_S_FAILURE;
768 }
769 krb5_free_ap_rep_enc_part(context, repl);
770 krb5_auth_con_setflags(context, ctx->auth_context, auth_flags);
771 }
772
773 /* We need to check the liftime */
774 {
775 OM_uint32 lifetime_rec;
776
777 ret = _gsskrb5_lifetime_left(minor_status,
778 context,
779 ctx->endtime,
780 &lifetime_rec);
781 if (ret) {
782 return ret;
783 }
784 if (lifetime_rec == 0) {
785 return GSS_S_CONTEXT_EXPIRED;
786 }
787
788 if (time_rec) *time_rec = lifetime_rec;
789 }
790
791 /* We need to give the caller the flags which are in use */
792 if (ret_flags) *ret_flags = ctx->flags;
793
794 if (src_name) {
795 kret = krb5_copy_principal(context,
796 ctx->source,
797 (gsskrb5_name*)src_name);
798 if (kret) {
799 *minor_status = kret;
800 return GSS_S_FAILURE;
801 }
802 }
803
804 /*
805 * After the krb5_rd_rep() the remote and local seq_number should
806 * be the same, because the client just replies the seq_number
807 * from our AP-REP in its AP-REP, but then the client uses the
808 * seq_number from its AP-REQ for GSS_wrap()
809 */
810 {
811 int32_t tmp_r_seq_number, tmp_l_seq_number;
812
813 kret = krb5_auth_con_getremoteseqnumber(context,
814 ctx->auth_context,
815 &tmp_r_seq_number);
816 if (kret) {
817 *minor_status = kret;
818 return GSS_S_FAILURE;
819 }
820
821 kret = krb5_auth_con_getlocalseqnumber(context,
822 ctx->auth_context,
823 &tmp_l_seq_number);
824 if (kret) {
825
826 *minor_status = kret;
827 return GSS_S_FAILURE;
828 }
829
830 /*
831 * Here we check if the client has responsed with our local seq_number,
832 */
833 if (tmp_r_seq_number != tmp_l_seq_number) {
834 return GSS_S_UNSEQ_TOKEN;
835 }
836 }
837
838 /*
839 * We need to reset the remote seq_number, because the client will use,
840 * the old one for the GSS_wrap() calls
841 */
842 {
843 kret = krb5_auth_con_setremoteseqnumber(context,
844 ctx->auth_context,
845 r_seq_number);
846 if (kret) {
847 *minor_status = kret;
848 return GSS_S_FAILURE;
849 }
850 }
851
852 return gsskrb5_acceptor_ready(minor_status, ctx, context,
853 delegated_cred_handle);
854 }
855
856
857 OM_uint32 GSSAPI_CALLCONV
858 _gsskrb5_accept_sec_context(OM_uint32 * minor_status,
859 gss_ctx_id_t * context_handle,
860 gss_const_cred_id_t acceptor_cred_handle,
861 const gss_buffer_t input_token_buffer,
862 const gss_channel_bindings_t input_chan_bindings,
863 gss_name_t * src_name,
864 gss_OID * mech_type,
865 gss_buffer_t output_token,
866 OM_uint32 * ret_flags,
867 OM_uint32 * time_rec,
868 gss_cred_id_t * delegated_cred_handle)
869 {
870 krb5_context context;
871 OM_uint32 ret;
872 gsskrb5_ctx ctx;
873
874 GSSAPI_KRB5_INIT(&context);
875
876 output_token->length = 0;
877 output_token->value = NULL;
878
879 if (src_name != NULL)
880 *src_name = NULL;
881 if (mech_type)
882 *mech_type = GSS_KRB5_MECHANISM;
883
884 if (*context_handle == GSS_C_NO_CONTEXT) {
885 ret = _gsskrb5_create_ctx(minor_status,
886 context_handle,
887 context,
888 input_chan_bindings,
889 ACCEPTOR_START);
890 if (ret)
891 return ret;
892 }
893
894 ctx = (gsskrb5_ctx)*context_handle;
895
896
897 /*
898 * TODO: check the channel_bindings
899 * (above just sets them to krb5 layer)
900 */
901
902 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
903
904 switch (ctx->state) {
905 case ACCEPTOR_START:
906 ret = gsskrb5_acceptor_start(minor_status,
907 ctx,
908 context,
909 acceptor_cred_handle,
910 input_token_buffer,
911 input_chan_bindings,
912 src_name,
913 mech_type,
914 output_token,
915 ret_flags,
916 time_rec,
917 delegated_cred_handle);
918 break;
919 case ACCEPTOR_WAIT_FOR_DCESTYLE:
920 ret = acceptor_wait_for_dcestyle(minor_status,
921 ctx,
922 context,
923 acceptor_cred_handle,
924 input_token_buffer,
925 input_chan_bindings,
926 src_name,
927 mech_type,
928 output_token,
929 ret_flags,
930 time_rec,
931 delegated_cred_handle);
932 break;
933 case ACCEPTOR_READY:
934 /*
935 * If we get there, the caller have called
936 * gss_accept_sec_context() one time too many.
937 */
938 ret = GSS_S_BAD_STATUS;
939 break;
940 default:
941 /* TODO: is this correct here? --metze */
942 ret = GSS_S_BAD_STATUS;
943 break;
944 }
945
946 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
947
948 if (GSS_ERROR(ret)) {
949 OM_uint32 min2;
950 _gsskrb5_delete_sec_context(&min2, context_handle, GSS_C_NO_BUFFER);
951 }
952
953 return ret;
954 }