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