Debian Codegraph @

lib/gssapi/krb5/init_sec_context.c

raw
1 /*
2 * Copyright (c) 1997 - 2008 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 /*
37 * copy the addresses from `input_chan_bindings' (if any) to
38 * the auth context `ac'
39 */
40
41 static OM_uint32
42 set_addresses (krb5_context context,
43 krb5_auth_context ac,
44 const gss_channel_bindings_t input_chan_bindings)
45 {
46 /* Port numbers are expected to be in application_data.value,
47 * initator's port first */
48
49 krb5_address initiator_addr, acceptor_addr;
50 krb5_error_code kret;
51
52 if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS
53 || input_chan_bindings->application_data.length !=
54 2 * sizeof(ac->local_port))
55 return 0;
56
57 memset(&initiator_addr, 0, sizeof(initiator_addr));
58 memset(&acceptor_addr, 0, sizeof(acceptor_addr));
59
60 ac->local_port =
61 *(int16_t *) input_chan_bindings->application_data.value;
62
63 ac->remote_port =
64 *((int16_t *) input_chan_bindings->application_data.value + 1);
65
66 kret = _gsskrb5i_address_to_krb5addr(context,
67 input_chan_bindings->acceptor_addrtype,
68 &input_chan_bindings->acceptor_address,
69 ac->remote_port,
70 &acceptor_addr);
71 if (kret)
72 return kret;
73
74 kret = _gsskrb5i_address_to_krb5addr(context,
75 input_chan_bindings->initiator_addrtype,
76 &input_chan_bindings->initiator_address,
77 ac->local_port,
78 &initiator_addr);
79 if (kret) {
80 krb5_free_address (context, &acceptor_addr);
81 return kret;
82 }
83
84 kret = krb5_auth_con_setaddrs(context,
85 ac,
86 &initiator_addr, /* local address */
87 &acceptor_addr); /* remote address */
88
89 krb5_free_address (context, &initiator_addr);
90 krb5_free_address (context, &acceptor_addr);
91
92 #if 0
93 free(input_chan_bindings->application_data.value);
94 input_chan_bindings->application_data.value = NULL;
95 input_chan_bindings->application_data.length = 0;
96 #endif
97
98 return kret;
99 }
100
101 OM_uint32
102 _gsskrb5_create_ctx(
103 OM_uint32 * minor_status,
104 gss_ctx_id_t * context_handle,
105 krb5_context context,
106 const gss_channel_bindings_t input_chan_bindings,
107 enum gss_ctx_id_t_state state)
108 {
109 krb5_error_code kret;
110 gsskrb5_ctx ctx;
111
112 *context_handle = NULL;
113
114 ctx = malloc(sizeof(*ctx));
115 if (ctx == NULL) {
116 *minor_status = ENOMEM;
117 return GSS_S_FAILURE;
118 }
119 ctx->auth_context = NULL;
120 ctx->deleg_auth_context = NULL;
121 ctx->source = NULL;
122 ctx->target = NULL;
123 ctx->kcred = NULL;
124 ctx->ccache = NULL;
125 ctx->state = state;
126 ctx->flags = 0;
127 ctx->more_flags = 0;
128 ctx->service_keyblock = NULL;
129 ctx->ticket = NULL;
130 krb5_data_zero(&ctx->fwd_data);
131 ctx->endtime = 0;
132 ctx->order = NULL;
133 ctx->crypto = NULL;
134 HEIMDAL_MUTEX_init(&ctx->ctx_id_mutex);
135
136 kret = krb5_auth_con_init (context, &ctx->auth_context);
137 if (kret) {
138 *minor_status = kret;
139 HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
140 free(ctx);
141 return GSS_S_FAILURE;
142 }
143
144 kret = krb5_auth_con_init (context, &ctx->deleg_auth_context);
145 if (kret) {
146 *minor_status = kret;
147 krb5_auth_con_free(context, ctx->auth_context);
148 HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
149 free(ctx);
150 return GSS_S_FAILURE;
151 }
152
153 kret = set_addresses(context, ctx->auth_context, input_chan_bindings);
154 if (kret) {
155 *minor_status = kret;
156
157 krb5_auth_con_free(context, ctx->auth_context);
158 krb5_auth_con_free(context, ctx->deleg_auth_context);
159
160 HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
161 free(ctx);
162 return GSS_S_BAD_BINDINGS;
163 }
164
165 kret = set_addresses(context, ctx->deleg_auth_context, input_chan_bindings);
166 if (kret) {
167 *minor_status = kret;
168
169 krb5_auth_con_free(context, ctx->auth_context);
170 krb5_auth_con_free(context, ctx->deleg_auth_context);
171
172 HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
173 free(ctx);
174 return GSS_S_BAD_BINDINGS;
175 }
176
177 /*
178 * We need a sequence number
179 */
180
181 krb5_auth_con_addflags(context,
182 ctx->auth_context,
183 KRB5_AUTH_CONTEXT_DO_SEQUENCE |
184 KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED,
185 NULL);
186
187 /*
188 * We need a sequence number
189 */
190
191 krb5_auth_con_addflags(context,
192 ctx->deleg_auth_context,
193 KRB5_AUTH_CONTEXT_DO_SEQUENCE |
194 KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED,
195 NULL);
196
197 *context_handle = (gss_ctx_id_t)ctx;
198
199 return GSS_S_COMPLETE;
200 }
201
202
203 static OM_uint32
204 gsskrb5_get_creds(
205 OM_uint32 * minor_status,
206 krb5_context context,
207 krb5_ccache ccache,
208 gsskrb5_ctx ctx,
209 gss_const_name_t target_name,
210 OM_uint32 time_req,
211 OM_uint32 * time_rec)
212 {
213 OM_uint32 ret;
214 krb5_error_code kret;
215 krb5_creds this_cred;
216 OM_uint32 lifetime_rec;
217
218 if (ctx->target) {
219 krb5_free_principal(context, ctx->target);
220 ctx->target = NULL;
221 }
222 if (ctx->kcred) {
223 krb5_free_creds(context, ctx->kcred);
224 ctx->kcred = NULL;
225 }
226
227 ret = _gsskrb5_canon_name(minor_status, context, target_name,
228 &ctx->target);
229 if (ret)
230 return ret;
231
232 memset(&this_cred, 0, sizeof(this_cred));
233 this_cred.client = ctx->source;
234 this_cred.server = ctx->target;
235
236 if (time_req && time_req != GSS_C_INDEFINITE) {
237 krb5_timestamp ts;
238
239 krb5_timeofday (context, &ts);
240 this_cred.times.endtime = ts + time_req;
241 } else {
242 this_cred.times.endtime = 0;
243 }
244
245 this_cred.session.keytype = KEYTYPE_NULL;
246
247 kret = krb5_get_credentials(context,
248 0,
249 ccache,
250 &this_cred,
251 &ctx->kcred);
252 if (kret) {
253 *minor_status = kret;
254 return GSS_S_FAILURE;
255 }
256
257 ctx->endtime = ctx->kcred->times.endtime;
258
259 ret = _gsskrb5_lifetime_left(minor_status, context,
260 ctx->endtime, &lifetime_rec);
261 if (ret) return ret;
262
263 if (lifetime_rec == 0) {
264 *minor_status = 0;
265 return GSS_S_CONTEXT_EXPIRED;
266 }
267
268 if (time_rec) *time_rec = lifetime_rec;
269
270 return GSS_S_COMPLETE;
271 }
272
273 static OM_uint32
274 gsskrb5_initiator_ready(
275 OM_uint32 * minor_status,
276 gsskrb5_ctx ctx,
277 krb5_context context)
278 {
279 OM_uint32 ret;
280 int32_t seq_number;
281 int is_cfx = 0;
282 OM_uint32 flags = ctx->flags;
283
284 krb5_free_creds(context, ctx->kcred);
285 ctx->kcred = NULL;
286
287 if (ctx->more_flags & CLOSE_CCACHE)
288 krb5_cc_close(context, ctx->ccache);
289 ctx->ccache = NULL;
290
291 krb5_auth_con_getremoteseqnumber (context, ctx->auth_context, &seq_number);
292
293 _gsskrb5i_is_cfx(context, ctx, 0);
294 is_cfx = (ctx->more_flags & IS_CFX);
295
296 ret = _gssapi_msg_order_create(minor_status,
297 &ctx->order,
298 _gssapi_msg_order_f(flags),
299 seq_number, 0, is_cfx);
300 if (ret) return ret;
301
302 ctx->state = INITIATOR_READY;
303 ctx->more_flags |= OPEN;
304
305 return GSS_S_COMPLETE;
306 }
307
308 /*
309 * handle delegated creds in init-sec-context
310 */
311
312 static void
313 do_delegation (krb5_context context,
314 krb5_auth_context ac,
315 krb5_ccache ccache,
316 krb5_creds *cred,
317 krb5_const_principal name,
318 krb5_data *fwd_data,
319 uint32_t flagmask,
320 uint32_t *flags)
321 {
322 krb5_creds creds;
323 KDCOptions fwd_flags;
324 krb5_error_code kret;
325
326 memset (&creds, 0, sizeof(creds));
327 krb5_data_zero (fwd_data);
328
329 kret = krb5_cc_get_principal(context, ccache, &creds.client);
330 if (kret)
331 goto out;
332
333 kret = krb5_make_principal(context,
334 &creds.server,
335 creds.client->realm,
336 KRB5_TGS_NAME,
337 creds.client->realm,
338 NULL);
339 if (kret)
340 goto out;
341
342 creds.times.endtime = 0;
343
344 memset(&fwd_flags, 0, sizeof(fwd_flags));
345 fwd_flags.forwarded = 1;
346 fwd_flags.forwardable = 1;
347
348 if (name->name.name_string.len < 2)
349 goto out;
350
351 kret = krb5_get_forwarded_creds(context,
352 ac,
353 ccache,
354 KDCOptions2int(fwd_flags),
355 name->name.name_string.val[1],
356 &creds,
357 fwd_data);
358
359 out:
360 if (kret)
361 *flags &= ~flagmask;
362 else
363 *flags |= flagmask;
364
365 if (creds.client)
366 krb5_free_principal(context, creds.client);
367 if (creds.server)
368 krb5_free_principal(context, creds.server);
369 }
370
371 /*
372 * first stage of init-sec-context
373 */
374
375 static OM_uint32
376 init_auth
377 (OM_uint32 * minor_status,
378 gsskrb5_cred cred,
379 gsskrb5_ctx ctx,
380 krb5_context context,
381 gss_const_name_t name,
382 const gss_OID mech_type,
383 OM_uint32 req_flags,
384 OM_uint32 time_req,
385 const gss_buffer_t input_token,
386 gss_OID * actual_mech_type,
387 gss_buffer_t output_token,
388 OM_uint32 * ret_flags,
389 OM_uint32 * time_rec
390 )
391 {
392 OM_uint32 ret = GSS_S_FAILURE;
393 krb5_error_code kret;
394 krb5_data fwd_data;
395 OM_uint32 lifetime_rec;
396
397 krb5_data_zero(&fwd_data);
398
399 *minor_status = 0;
400
401 if (actual_mech_type)
402 *actual_mech_type = GSS_KRB5_MECHANISM;
403
404 if (cred == NULL) {
405 kret = krb5_cc_default (context, &ctx->ccache);
406 if (kret) {
407 *minor_status = kret;
408 ret = GSS_S_FAILURE;
409 goto failure;
410 }
411 ctx->more_flags |= CLOSE_CCACHE;
412 } else
413 ctx->ccache = cred->ccache;
414
415 kret = krb5_cc_get_principal (context, ctx->ccache, &ctx->source);
416 if (kret) {
417 *minor_status = kret;
418 ret = GSS_S_FAILURE;
419 goto failure;
420 }
421
422 /*
423 * This is hideous glue for (NFS) clients that wants to limit the
424 * available enctypes to what it can support (encryption in
425 * kernel).
426 */
427 if (cred && cred->enctypes)
428 krb5_set_default_in_tkt_etypes(context, cred->enctypes);
429
430 ret = gsskrb5_get_creds(minor_status, context, ctx->ccache,
431 ctx, name, time_req, time_rec);
432 if (ret)
433 goto failure;
434
435 ctx->endtime = ctx->kcred->times.endtime;
436
437 ret = _gss_DES3_get_mic_compat(minor_status, ctx, context);
438 if (ret)
439 goto failure;
440
441 ret = _gsskrb5_lifetime_left(minor_status,
442 context,
443 ctx->endtime,
444 &lifetime_rec);
445 if (ret)
446 goto failure;
447
448 if (lifetime_rec == 0) {
449 *minor_status = 0;
450 ret = GSS_S_CONTEXT_EXPIRED;
451 goto failure;
452 }
453
454 krb5_auth_con_setkey(context,
455 ctx->auth_context,
456 &ctx->kcred->session);
457
458 kret = krb5_auth_con_generatelocalsubkey(context,
459 ctx->auth_context,
460 &ctx->kcred->session);
461 if(kret) {
462 *minor_status = kret;
463 ret = GSS_S_FAILURE;
464 goto failure;
465 }
466
467 return GSS_S_COMPLETE;
468
469 failure:
470 if (ctx->ccache && (ctx->more_flags & CLOSE_CCACHE))
471 krb5_cc_close(context, ctx->ccache);
472 ctx->ccache = NULL;
473
474 return ret;
475
476 }
477
478 static OM_uint32
479 init_auth_restart
480 (OM_uint32 * minor_status,
481 gsskrb5_cred cred,
482 gsskrb5_ctx ctx,
483 krb5_context context,
484 OM_uint32 req_flags,
485 const gss_channel_bindings_t input_chan_bindings,
486 const gss_buffer_t input_token,
487 gss_OID * actual_mech_type,
488 gss_buffer_t output_token,
489 OM_uint32 * ret_flags,
490 OM_uint32 * time_rec
491 )
492 {
493 OM_uint32 ret = GSS_S_FAILURE;
494 krb5_error_code kret;
495 krb5_flags ap_options;
496 krb5_data outbuf;
497 uint32_t flags;
498 krb5_data authenticator;
499 Checksum cksum;
500 krb5_enctype enctype;
501 krb5_data fwd_data, timedata;
502 int32_t offset = 0, oldoffset = 0;
503 uint32_t flagmask;
504
505 krb5_data_zero(&outbuf);
506 krb5_data_zero(&fwd_data);
507
508 *minor_status = 0;
509
510 /*
511 * If the credential doesn't have ok-as-delegate, check if there
512 * is a realm setting and use that.
513 */
514 if (!ctx->kcred->flags.b.ok_as_delegate) {
515 krb5_data data;
516
517 ret = krb5_cc_get_config(context, ctx->ccache, NULL,
518 "realm-config", &data);
519 if (ret == 0) {
520 /* XXX 1 is use ok-as-delegate */
521 if (data.length < 1 || ((((unsigned char *)data.data)[0]) & 1) == 0)
522 req_flags &= ~(GSS_C_DELEG_FLAG|GSS_C_DELEG_POLICY_FLAG);
523 krb5_data_free(&data);
524 }
525 }
526
527 flagmask = 0;
528
529 /* if we used GSS_C_DELEG_POLICY_FLAG, trust KDC */
530 if ((req_flags & GSS_C_DELEG_POLICY_FLAG)
531 && ctx->kcred->flags.b.ok_as_delegate)
532 flagmask |= GSS_C_DELEG_FLAG | GSS_C_DELEG_POLICY_FLAG;
533 /* if there still is a GSS_C_DELEG_FLAG, use that */
534 if (req_flags & GSS_C_DELEG_FLAG)
535 flagmask |= GSS_C_DELEG_FLAG;
536
537
538 flags = 0;
539 ap_options = 0;
540 if (flagmask & GSS_C_DELEG_FLAG) {
541 do_delegation (context,
542 ctx->deleg_auth_context,
543 ctx->ccache, ctx->kcred, ctx->target,
544 &fwd_data, flagmask, &flags);
545 }
546
547 if (req_flags & GSS_C_MUTUAL_FLAG) {
548 flags |= GSS_C_MUTUAL_FLAG;
549 ap_options |= AP_OPTS_MUTUAL_REQUIRED;
550 }
551
552 if (req_flags & GSS_C_REPLAY_FLAG)
553 flags |= GSS_C_REPLAY_FLAG;
554 if (req_flags & GSS_C_SEQUENCE_FLAG)
555 flags |= GSS_C_SEQUENCE_FLAG;
556 #if 0
557 if (req_flags & GSS_C_ANON_FLAG)
558 ; /* XXX */
559 #endif
560 if (req_flags & GSS_C_DCE_STYLE) {
561 /* GSS_C_DCE_STYLE implies GSS_C_MUTUAL_FLAG */
562 flags |= GSS_C_DCE_STYLE | GSS_C_MUTUAL_FLAG;
563 ap_options |= AP_OPTS_MUTUAL_REQUIRED;
564 }
565 if (req_flags & GSS_C_IDENTIFY_FLAG)
566 flags |= GSS_C_IDENTIFY_FLAG;
567 if (req_flags & GSS_C_EXTENDED_ERROR_FLAG)
568 flags |= GSS_C_EXTENDED_ERROR_FLAG;
569
570 if (req_flags & GSS_C_CONF_FLAG) {
571 flags |= GSS_C_CONF_FLAG;
572 }
573 if (req_flags & GSS_C_INTEG_FLAG) {
574 flags |= GSS_C_INTEG_FLAG;
575 }
576 if (cred == NULL || !(cred->cred_flags & GSS_CF_NO_CI_FLAGS)) {
577 flags |= GSS_C_CONF_FLAG;
578 flags |= GSS_C_INTEG_FLAG;
579 }
580 flags |= GSS_C_TRANS_FLAG;
581
582 if (ret_flags)
583 *ret_flags = flags;
584 ctx->flags = flags;
585 ctx->more_flags |= LOCAL;
586
587 ret = _gsskrb5_create_8003_checksum (minor_status,
588 input_chan_bindings,
589 flags,
590 &fwd_data,
591 &cksum);
592 krb5_data_free (&fwd_data);
593 if (ret)
594 goto failure;
595
596 enctype = ctx->auth_context->keyblock->keytype;
597
598 ret = krb5_cc_get_config(context, ctx->ccache, ctx->target,
599 "time-offset", &timedata);
600 if (ret == 0) {
601 if (timedata.length == 4) {
602 const u_char *p = timedata.data;
603 if (p[0] < 128) {
604 offset = (p[0] <<24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
605 } else {
606 /*
607 * (p[0] << 24), if p[0] > 127 -> offset is negative, but *p is
608 * positive, so this is overflow -- overflow we want, but UBSAN
609 * flags it.
610 *
611 * NOTE: We assume the platform is a twos-complement platform.
612 */
613 offset = INT32_MIN;
614 offset |= ((p[0] & 0x7f) <<24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
615 }
616 }
617 krb5_data_free(&timedata);
618 }
619
620 if (offset) {
621 krb5_get_kdc_sec_offset (context, &oldoffset, NULL);
622 krb5_set_kdc_sec_offset (context, offset, -1);
623 }
624
625 kret = _krb5_build_authenticator(context,
626 ctx->auth_context,
627 enctype,
628 ctx->kcred,
629 &cksum,
630 &authenticator,
631 KRB5_KU_AP_REQ_AUTH);
632
633 if (kret) {
634 if (offset)
635 krb5_set_kdc_sec_offset (context, oldoffset, -1);
636 *minor_status = kret;
637 ret = GSS_S_FAILURE;
638 goto failure;
639 }
640
641 kret = krb5_build_ap_req (context,
642 enctype,
643 ctx->kcred,
644 ap_options,
645 authenticator,
646 &outbuf);
647 if (offset)
648 krb5_set_kdc_sec_offset (context, oldoffset, -1);
649 if (kret) {
650 *minor_status = kret;
651 ret = GSS_S_FAILURE;
652 goto failure;
653 }
654
655 if (flags & GSS_C_DCE_STYLE) {
656 output_token->value = outbuf.data;
657 output_token->length = outbuf.length;
658 } else {
659 ret = _gsskrb5_encapsulate (minor_status, &outbuf, output_token,
660 (u_char *)(intptr_t)"\x01\x00",
661 GSS_KRB5_MECHANISM);
662 krb5_data_free (&outbuf);
663 if (ret)
664 goto failure;
665 }
666
667 free_Checksum(&cksum);
668
669 if (flags & GSS_C_MUTUAL_FLAG) {
670 ctx->state = INITIATOR_WAIT_FOR_MUTAL;
671 return GSS_S_CONTINUE_NEEDED;
672 }
673
674 return gsskrb5_initiator_ready(minor_status, ctx, context);
675 failure:
676 if (ctx->ccache && (ctx->more_flags & CLOSE_CCACHE))
677 krb5_cc_close(context, ctx->ccache);
678 ctx->ccache = NULL;
679
680 return ret;
681 }
682
683 static krb5_error_code
684 handle_error_packet(krb5_context context,
685 gsskrb5_ctx ctx,
686 krb5_data indata)
687 {
688 krb5_error_code kret;
689 KRB_ERROR error;
690
691 kret = krb5_rd_error(context, &indata, &error);
692 if (kret == 0) {
693 kret = krb5_error_from_rd_error(context, &error, NULL);
694
695 /* save the time skrew for this host */
696 if (kret == KRB5KRB_AP_ERR_SKEW) {
697 krb5_data timedata;
698 unsigned char p[4];
699 int32_t t = error.stime - time(NULL);
700
701 p[0] = (t >> 24) & 0xFF;
702 p[1] = (t >> 16) & 0xFF;
703 p[2] = (t >> 8) & 0xFF;
704 p[3] = (t >> 0) & 0xFF;
705
706 timedata.data = p;
707 timedata.length = sizeof(p);
708
709 krb5_cc_set_config(context, ctx->ccache, ctx->target,
710 "time-offset", &timedata);
711
712 if ((ctx->more_flags & RETRIED) == 0)
713 ctx->state = INITIATOR_RESTART;
714 ctx->more_flags |= RETRIED;
715 }
716 free_KRB_ERROR (&error);
717 }
718 return kret;
719 }
720
721
722 static OM_uint32
723 repl_mutual
724 (OM_uint32 * minor_status,
725 gsskrb5_ctx ctx,
726 krb5_context context,
727 const gss_OID mech_type,
728 OM_uint32 req_flags,
729 OM_uint32 time_req,
730 const gss_channel_bindings_t input_chan_bindings,
731 const gss_buffer_t input_token,
732 gss_OID * actual_mech_type,
733 gss_buffer_t output_token,
734 OM_uint32 * ret_flags,
735 OM_uint32 * time_rec
736 )
737 {
738 OM_uint32 ret;
739 krb5_error_code kret;
740 krb5_data indata;
741 krb5_ap_rep_enc_part *repl;
742
743 output_token->length = 0;
744 output_token->value = NULL;
745
746 if (actual_mech_type)
747 *actual_mech_type = GSS_KRB5_MECHANISM;
748
749 if (IS_DCE_STYLE(ctx)) {
750 /* There is no OID wrapping. */
751 indata.length = input_token->length;
752 indata.data = input_token->value;
753 kret = krb5_rd_rep(context,
754 ctx->auth_context,
755 &indata,
756 &repl);
757 if (kret) {
758 ret = _gsskrb5_decapsulate(minor_status,
759 input_token,
760 &indata,
761 "\x03\x00",
762 GSS_KRB5_MECHANISM);
763 if (ret == GSS_S_COMPLETE) {
764 *minor_status = handle_error_packet(context, ctx, indata);
765 } else {
766 *minor_status = kret;
767 }
768 return GSS_S_FAILURE;
769 }
770 } else {
771 ret = _gsskrb5_decapsulate (minor_status,
772 input_token,
773 &indata,
774 "\x02\x00",
775 GSS_KRB5_MECHANISM);
776 if (ret == GSS_S_DEFECTIVE_TOKEN) {
777 /* check if there is an error token sent instead */
778 ret = _gsskrb5_decapsulate (minor_status,
779 input_token,
780 &indata,
781 "\x03\x00",
782 GSS_KRB5_MECHANISM);
783 if (ret == GSS_S_COMPLETE) {
784 *minor_status = handle_error_packet(context, ctx, indata);
785 return GSS_S_FAILURE;
786 }
787 }
788 kret = krb5_rd_rep (context,
789 ctx->auth_context,
790 &indata,
791 &repl);
792 if (kret) {
793 *minor_status = kret;
794 return GSS_S_FAILURE;
795 }
796 }
797
798 krb5_free_ap_rep_enc_part (context,
799 repl);
800
801 *minor_status = 0;
802 if (time_rec)
803 _gsskrb5_lifetime_left(minor_status,
804 context,
805 ctx->endtime,
806 time_rec);
807 if (ret_flags)
808 *ret_flags = ctx->flags;
809
810 if (req_flags & GSS_C_DCE_STYLE) {
811 int32_t local_seq, remote_seq;
812 krb5_data outbuf;
813
814 /*
815 * So DCE_STYLE is strange. The client echos the seq number
816 * that the server used in the server's mk_rep in its own
817 * mk_rep(). After when done, it resets to it's own seq number
818 * for the gss_wrap calls.
819 */
820
821 krb5_auth_con_getremoteseqnumber(context, ctx->auth_context, &remote_seq);
822 krb5_auth_con_getlocalseqnumber(context, ctx->auth_context, &local_seq);
823 krb5_auth_con_setlocalseqnumber(context, ctx->auth_context, remote_seq);
824
825 kret = krb5_mk_rep(context, ctx->auth_context, &outbuf);
826 if (kret) {
827 *minor_status = kret;
828 return GSS_S_FAILURE;
829 }
830
831 /* reset local seq number */
832 krb5_auth_con_setlocalseqnumber(context, ctx->auth_context, local_seq);
833
834 output_token->length = outbuf.length;
835 output_token->value = outbuf.data;
836 }
837
838 return gsskrb5_initiator_ready(minor_status, ctx, context);
839 }
840
841 /*
842 * gss_init_sec_context
843 */
844
845 OM_uint32 GSSAPI_CALLCONV _gsskrb5_init_sec_context
846 (OM_uint32 * minor_status,
847 gss_const_cred_id_t cred_handle,
848 gss_ctx_id_t * context_handle,
849 gss_const_name_t target_name,
850 const gss_OID mech_type,
851 OM_uint32 req_flags,
852 OM_uint32 time_req,
853 const gss_channel_bindings_t input_chan_bindings,
854 const gss_buffer_t input_token,
855 gss_OID * actual_mech_type,
856 gss_buffer_t output_token,
857 OM_uint32 * ret_flags,
858 OM_uint32 * time_rec
859 )
860 {
861 krb5_context context;
862 gsskrb5_cred cred = (gsskrb5_cred)cred_handle;
863 gsskrb5_ctx ctx;
864 OM_uint32 ret;
865
866 GSSAPI_KRB5_INIT (&context);
867
868 output_token->length = 0;
869 output_token->value = NULL;
870
871 if (context_handle == NULL) {
872 *minor_status = 0;
873 return GSS_S_FAILURE | GSS_S_CALL_BAD_STRUCTURE;
874 }
875
876 if (ret_flags)
877 *ret_flags = 0;
878 if (time_rec)
879 *time_rec = 0;
880
881 if (target_name == GSS_C_NO_NAME) {
882 if (actual_mech_type)
883 *actual_mech_type = GSS_C_NO_OID;
884 *minor_status = 0;
885 return GSS_S_BAD_NAME;
886 }
887
888 if (mech_type != GSS_C_NO_OID &&
889 !gss_oid_equal(mech_type, GSS_KRB5_MECHANISM))
890 return GSS_S_BAD_MECH;
891
892 if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
893 OM_uint32 ret1;
894
895 if (*context_handle != GSS_C_NO_CONTEXT) {
896 *minor_status = 0;
897 return GSS_S_FAILURE | GSS_S_CALL_BAD_STRUCTURE;
898 }
899
900 ret1 = _gsskrb5_create_ctx(minor_status,
901 context_handle,
902 context,
903 input_chan_bindings,
904 INITIATOR_START);
905 if (ret1)
906 return ret1;
907 }
908
909 if (*context_handle == GSS_C_NO_CONTEXT) {
910 *minor_status = 0;
911 return GSS_S_FAILURE | GSS_S_CALL_BAD_STRUCTURE;
912 }
913
914 ctx = (gsskrb5_ctx) *context_handle;
915
916 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
917
918 again:
919 switch (ctx->state) {
920 case INITIATOR_START:
921 ret = init_auth(minor_status,
922 cred,
923 ctx,
924 context,
925 target_name,
926 mech_type,
927 req_flags,
928 time_req,
929 input_token,
930 actual_mech_type,
931 output_token,
932 ret_flags,
933 time_rec);
934 if (ret != GSS_S_COMPLETE)
935 break;
936 /* FALLTHROUGH */
937 case INITIATOR_RESTART:
938 ret = init_auth_restart(minor_status,
939 cred,
940 ctx,
941 context,
942 req_flags,
943 input_chan_bindings,
944 input_token,
945 actual_mech_type,
946 output_token,
947 ret_flags,
948 time_rec);
949 break;
950 case INITIATOR_WAIT_FOR_MUTAL:
951 ret = repl_mutual(minor_status,
952 ctx,
953 context,
954 mech_type,
955 req_flags,
956 time_req,
957 input_chan_bindings,
958 input_token,
959 actual_mech_type,
960 output_token,
961 ret_flags,
962 time_rec);
963 if (ctx->state == INITIATOR_RESTART)
964 goto again;
965 break;
966 case INITIATOR_READY:
967 /*
968 * If we get there, the caller have called
969 * gss_init_sec_context() one time too many.
970 */
971 _gsskrb5_set_status(EINVAL, "init_sec_context "
972 "called one time too many");
973 *minor_status = EINVAL;
974 ret = GSS_S_BAD_STATUS;
975 break;
976 default:
977 _gsskrb5_set_status(EINVAL, "init_sec_context "
978 "invalid state %d for client",
979 (int)ctx->state);
980 *minor_status = EINVAL;
981 ret = GSS_S_BAD_STATUS;
982 break;
983 }
984 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
985
986 /* destroy context in case of error */
987 if (GSS_ERROR(ret)) {
988 OM_uint32 min2;
989 _gsskrb5_delete_sec_context(&min2, context_handle, GSS_C_NO_BUFFER);
990 }
991
992 return ret;
993
994 }