2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "gsskrb5_locl.h"
39 * copy the addresses from `input_chan_bindings' (if any) to
40 * the auth context `ac'
44 set_addresses (krb5_context context,
46 const gss_channel_bindings_t input_chan_bindings)
48 /* Port numbers are expected to be in application_data.value,
49 * initator's port first */
51 krb5_address initiator_addr, acceptor_addr;
54 if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS
55 || input_chan_bindings->application_data.length !=
56 2 * sizeof(ac->local_port))
59 memset(&initiator_addr, 0, sizeof(initiator_addr));
60 memset(&acceptor_addr, 0, sizeof(acceptor_addr));
63 *(int16_t *) input_chan_bindings->application_data.value;
66 *((int16_t *) input_chan_bindings->application_data.value + 1);
68 kret = _gsskrb5i_address_to_krb5addr(context,
69 input_chan_bindings->acceptor_addrtype,
70 &input_chan_bindings->acceptor_address,
76 kret = _gsskrb5i_address_to_krb5addr(context,
77 input_chan_bindings->initiator_addrtype,
78 &input_chan_bindings->initiator_address,
82 krb5_free_address (context, &acceptor_addr);
86 kret = krb5_auth_con_setaddrs(context,
88 &initiator_addr, /* local address */
89 &acceptor_addr); /* remote address */
91 krb5_free_address (context, &initiator_addr);
92 krb5_free_address (context, &acceptor_addr);
95 free(input_chan_bindings->application_data.value);
96 input_chan_bindings->application_data.value = NULL;
97 input_chan_bindings->application_data.length = 0;
105 OM_uint32 * minor_status,
106 gss_ctx_id_t * context_handle,
107 krb5_context context,
108 const gss_channel_bindings_t input_chan_bindings,
109 enum gss_ctx_id_t_state state)
111 krb5_error_code kret;
114 *context_handle = NULL;
116 ctx = malloc(sizeof(*ctx));
118 *minor_status = ENOMEM;
119 return GSS_S_FAILURE;
121 ctx->auth_context = NULL;
129 ctx->service_keyblock = NULL;
131 krb5_data_zero(&ctx->fwd_data);
132 ctx->lifetime = GSS_C_INDEFINITE;
135 HEIMDAL_MUTEX_init(&ctx->ctx_id_mutex);
137 kret = krb5_auth_con_init (context, &ctx->auth_context);
139 *minor_status = kret;
140 HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
141 return GSS_S_FAILURE;
144 kret = set_addresses(context, ctx->auth_context, input_chan_bindings);
146 *minor_status = kret;
148 HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
150 krb5_auth_con_free(context, ctx->auth_context);
152 return GSS_S_BAD_BINDINGS;
156 * We need a sequence number
159 krb5_auth_con_addflags(context,
161 KRB5_AUTH_CONTEXT_DO_SEQUENCE |
162 KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED,
165 *context_handle = (gss_ctx_id_t)ctx;
167 return GSS_S_COMPLETE;
173 OM_uint32 * minor_status,
174 krb5_context context,
177 const gss_name_t target_name,
180 OM_uint32 * time_rec,
184 krb5_error_code kret;
185 krb5_creds this_cred;
186 OM_uint32 lifetime_rec;
191 krb5_free_principal(context, ctx->target);
195 ret = _gsskrb5_canon_name(minor_status, context, use_dns,
196 target_name, &ctx->target);
200 memset(&this_cred, 0, sizeof(this_cred));
201 this_cred.client = ctx->source;
202 this_cred.server = ctx->target;
204 if (time_req && time_req != GSS_C_INDEFINITE) {
207 krb5_timeofday (context, &ts);
208 this_cred.times.endtime = ts + time_req;
210 this_cred.times.endtime = 0;
213 this_cred.session.keytype = KEYTYPE_NULL;
215 kret = krb5_get_credentials(context,
221 *minor_status = kret;
222 return GSS_S_FAILURE;
225 ctx->lifetime = (*cred)->times.endtime;
227 ret = _gsskrb5_lifetime_left(minor_status, context,
228 ctx->lifetime, &lifetime_rec);
231 if (lifetime_rec == 0) {
233 return GSS_S_CONTEXT_EXPIRED;
236 if (time_rec) *time_rec = lifetime_rec;
238 return GSS_S_COMPLETE;
242 gsskrb5_initiator_ready(
243 OM_uint32 * minor_status,
245 krb5_context context)
250 OM_uint32 flags = ctx->flags;
252 krb5_free_creds(context, ctx->kcred);
255 if (ctx->more_flags & CLOSE_CCACHE)
256 krb5_cc_close(context, ctx->ccache);
259 krb5_auth_getremoteseqnumber (context, ctx->auth_context, &seq_number);
261 _gsskrb5i_is_cfx(context, ctx, 0);
262 is_cfx = (ctx->more_flags & IS_CFX);
264 ret = _gssapi_msg_order_create(minor_status,
266 _gssapi_msg_order_f(flags),
267 seq_number, 0, is_cfx);
270 ctx->state = INITIATOR_READY;
271 ctx->more_flags |= OPEN;
273 return GSS_S_COMPLETE;
277 * handle delegated creds in init-sec-context
281 do_delegation (krb5_context context,
282 krb5_auth_context ac,
285 krb5_const_principal name,
291 KDCOptions fwd_flags;
292 krb5_error_code kret;
294 memset (&creds, 0, sizeof(creds));
295 krb5_data_zero (fwd_data);
297 kret = krb5_cc_get_principal(context, ccache, &creds.client);
301 kret = krb5_build_principal(context,
303 strlen(creds.client->realm),
311 creds.times.endtime = 0;
313 memset(&fwd_flags, 0, sizeof(fwd_flags));
314 fwd_flags.forwarded = 1;
315 fwd_flags.forwardable = 1;
317 if ( /*target_name->name.name_type != KRB5_NT_SRV_HST ||*/
318 name->name.name_string.len < 2)
321 kret = krb5_get_forwarded_creds(context,
324 KDCOptions2int(fwd_flags),
325 name->name.name_string.val[1],
336 krb5_free_principal(context, creds.client);
338 krb5_free_principal(context, creds.server);
342 * first stage of init-sec-context
347 (OM_uint32 * minor_status,
350 krb5_context context,
352 const gss_OID mech_type,
355 const gss_buffer_t input_token,
356 gss_OID * actual_mech_type,
357 gss_buffer_t output_token,
358 OM_uint32 * ret_flags,
362 OM_uint32 ret = GSS_S_FAILURE;
363 krb5_error_code kret;
366 OM_uint32 lifetime_rec;
369 krb5_data_zero(&outbuf);
370 krb5_data_zero(&fwd_data);
374 if (actual_mech_type)
375 *actual_mech_type = GSS_KRB5_MECHANISM;
378 kret = krb5_cc_default (context, &ctx->ccache);
380 *minor_status = kret;
384 ctx->more_flags |= CLOSE_CCACHE;
386 ctx->ccache = cred->ccache;
388 kret = krb5_cc_get_principal (context, ctx->ccache, &ctx->source);
390 *minor_status = kret;
395 ret = _gss_DES3_get_mic_compat(minor_status, ctx, context);
401 * This is hideous glue for (NFS) clients that wants to limit the
402 * available enctypes to what it can support (encryption in
403 * kernel). If there is no enctypes selected for this credential,
404 * reset it to the default set of enctypes.
407 krb5_enctype *enctypes = NULL;
409 if (cred && cred->enctypes)
410 enctypes = cred->enctypes;
411 krb5_set_default_in_tkt_etypes(context, enctypes);
414 /* canon name if needed for client + target realm */
415 kret = krb5_cc_get_config(context, ctx->ccache, NULL,
416 "realm-config", &outbuf);
418 /* XXX 2 is no server canon */
419 if (outbuf.length < 1 || ((((unsigned char *)outbuf.data)[0]) & 2))
421 krb5_data_free(&outbuf);
425 * First we try w/o dns, hope that the KDC have register alias
426 * (and referrals if cross realm) for this principal. If that
427 * fails and if we are allowed to using this realm try again with
428 * DNS canonicalizion.
430 ret = gsskrb5_get_creds(minor_status, context, ctx->ccache,
431 ctx, name, 0, time_req,
432 time_rec, &ctx->kcred);
433 if (ret && allow_dns)
434 ret = gsskrb5_get_creds(minor_status, context, ctx->ccache,
435 ctx, name, 1, time_req,
436 time_rec, &ctx->kcred);
440 ctx->lifetime = ctx->kcred->times.endtime;
442 ret = _gsskrb5_lifetime_left(minor_status,
449 if (lifetime_rec == 0) {
451 ret = GSS_S_CONTEXT_EXPIRED;
455 krb5_auth_con_setkey(context,
457 &ctx->kcred->session);
459 kret = krb5_auth_con_generatelocalsubkey(context,
461 &ctx->kcred->session);
463 *minor_status = kret;
468 return GSS_S_COMPLETE;
471 if (ctx->ccache && (ctx->more_flags & CLOSE_CCACHE))
472 krb5_cc_close(context, ctx->ccache);
481 (OM_uint32 * minor_status,
484 krb5_context context,
486 const gss_channel_bindings_t input_chan_bindings,
487 const gss_buffer_t input_token,
488 gss_OID * actual_mech_type,
489 gss_buffer_t output_token,
490 OM_uint32 * ret_flags,
494 OM_uint32 ret = GSS_S_FAILURE;
495 krb5_error_code kret;
496 krb5_flags ap_options;
499 krb5_data authenticator;
501 krb5_enctype enctype;
502 krb5_data fwd_data, timedata;
503 int32_t offset = 0, oldoffset;
506 krb5_data_zero(&outbuf);
507 krb5_data_zero(&fwd_data);
512 * If the credential doesn't have ok-as-delegate, check if there
513 * is a realm setting and use that.
515 if (!ctx->kcred->flags.b.ok_as_delegate) {
518 ret = krb5_cc_get_config(context, ctx->ccache, NULL,
519 "realm-config", &data);
521 /* XXX 1 is use ok-as-delegate */
522 if (data.length < 1 || ((((unsigned char *)data.data)[0]) & 1) == 0)
523 req_flags &= ~(GSS_C_DELEG_FLAG|GSS_C_DELEG_POLICY_FLAG);
524 krb5_data_free(&data);
530 /* if we used GSS_C_DELEG_POLICY_FLAG, trust KDC */
531 if ((req_flags & GSS_C_DELEG_POLICY_FLAG)
532 && ctx->kcred->flags.b.ok_as_delegate)
533 flagmask |= GSS_C_DELEG_FLAG | GSS_C_DELEG_POLICY_FLAG;
534 /* if there still is a GSS_C_DELEG_FLAG, use that */
535 if (req_flags & GSS_C_DELEG_FLAG)
536 flagmask |= GSS_C_DELEG_FLAG;
541 if (flagmask & GSS_C_DELEG_FLAG) {
542 do_delegation (context,
544 ctx->ccache, ctx->kcred, ctx->target,
545 &fwd_data, flagmask, &flags);
548 if (req_flags & GSS_C_MUTUAL_FLAG) {
549 flags |= GSS_C_MUTUAL_FLAG;
550 ap_options |= AP_OPTS_MUTUAL_REQUIRED;
553 if (req_flags & GSS_C_REPLAY_FLAG)
554 flags |= GSS_C_REPLAY_FLAG;
555 if (req_flags & GSS_C_SEQUENCE_FLAG)
556 flags |= GSS_C_SEQUENCE_FLAG;
558 if (req_flags & GSS_C_ANON_FLAG)
561 if (req_flags & GSS_C_DCE_STYLE) {
562 /* GSS_C_DCE_STYLE implies GSS_C_MUTUAL_FLAG */
563 flags |= GSS_C_DCE_STYLE | GSS_C_MUTUAL_FLAG;
564 ap_options |= AP_OPTS_MUTUAL_REQUIRED;
566 if (req_flags & GSS_C_IDENTIFY_FLAG)
567 flags |= GSS_C_IDENTIFY_FLAG;
568 if (req_flags & GSS_C_EXTENDED_ERROR_FLAG)
569 flags |= GSS_C_EXTENDED_ERROR_FLAG;
571 if (req_flags & GSS_C_CONF_FLAG) {
572 flags |= GSS_C_CONF_FLAG;
574 if (req_flags & GSS_C_INTEG_FLAG) {
575 flags |= GSS_C_INTEG_FLAG;
577 if (cred == NULL || !(cred->cred_flags & GSS_CF_NO_CI_FLAGS)) {
578 flags |= GSS_C_CONF_FLAG;
579 flags |= GSS_C_INTEG_FLAG;
581 flags |= GSS_C_TRANS_FLAG;
586 ctx->more_flags |= LOCAL;
588 ret = _gsskrb5_create_8003_checksum (minor_status,
593 krb5_data_free (&fwd_data);
597 enctype = ctx->auth_context->keyblock->keytype;
599 ret = krb5_cc_get_config(context, ctx->ccache, ctx->target,
600 "time-offset", &timedata);
602 if (timedata.length == 4) {
603 const u_char *p = timedata.data;
604 offset = (p[0] <<24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
606 krb5_data_free(&timedata);
610 krb5_get_kdc_sec_offset (context, &oldoffset, NULL);
611 krb5_set_kdc_sec_offset (context, offset, -1);
614 kret = krb5_build_authenticator (context,
621 KRB5_KU_AP_REQ_AUTH);
625 krb5_set_kdc_sec_offset (context, oldoffset, -1);
626 *minor_status = kret;
631 kret = krb5_build_ap_req (context,
638 krb5_set_kdc_sec_offset (context, oldoffset, -1);
640 *minor_status = kret;
645 if (flags & GSS_C_DCE_STYLE) {
646 output_token->value = outbuf.data;
647 output_token->length = outbuf.length;
649 ret = _gsskrb5_encapsulate (minor_status, &outbuf, output_token,
650 (u_char *)"\x01\x00", GSS_KRB5_MECHANISM);
651 krb5_data_free (&outbuf);
656 free_Checksum(&cksum);
658 if (flags & GSS_C_MUTUAL_FLAG) {
659 ctx->state = INITIATOR_WAIT_FOR_MUTAL;
660 return GSS_S_CONTINUE_NEEDED;
663 return gsskrb5_initiator_ready(minor_status, ctx, context);
665 if (ctx->ccache && (ctx->more_flags & CLOSE_CCACHE))
666 krb5_cc_close(context, ctx->ccache);
675 (OM_uint32 * minor_status,
677 krb5_context context,
678 const gss_OID mech_type,
681 const gss_channel_bindings_t input_chan_bindings,
682 const gss_buffer_t input_token,
683 gss_OID * actual_mech_type,
684 gss_buffer_t output_token,
685 OM_uint32 * ret_flags,
690 krb5_error_code kret;
692 krb5_ap_rep_enc_part *repl;
694 output_token->length = 0;
695 output_token->value = NULL;
697 if (actual_mech_type)
698 *actual_mech_type = GSS_KRB5_MECHANISM;
700 if (ctx->flags & GSS_C_DCE_STYLE) {
701 /* There is no OID wrapping. */
702 indata.length = input_token->length;
703 indata.data = input_token->value;
705 ret = _gsskrb5_decapsulate (minor_status,
710 if (ret == GSS_S_DEFECTIVE_TOKEN) {
711 /* check if there is an error token sent instead */
712 ret = _gsskrb5_decapsulate (minor_status,
717 if (ret == GSS_S_COMPLETE) {
720 kret = krb5_rd_error(context, &indata, &error);
722 kret = krb5_error_from_rd_error(context, &error, NULL);
724 /* save the time skrew for this host */
725 if (kret == KRB5KRB_AP_ERR_SKEW) {
728 int32_t t = error.stime - time(NULL);
730 p[0] = (t >> 24) & 0xFF;
731 p[1] = (t >> 16) & 0xFF;
732 p[2] = (t >> 8) & 0xFF;
733 p[3] = (t >> 0) & 0xFF;
736 timedata.length = sizeof(p);
738 krb5_cc_set_config(context, ctx->ccache, ctx->target,
739 "time-offset", &timedata);
741 if ((ctx->more_flags & RETRIED) == 0)
742 ctx->state = INITIATOR_RESTART;
743 ctx->more_flags |= RETRIED;
745 free_KRB_ERROR (&error);
747 *minor_status = kret;
748 return GSS_S_FAILURE;
754 kret = krb5_rd_rep (context,
759 *minor_status = kret;
760 return GSS_S_FAILURE;
762 krb5_free_ap_rep_enc_part (context,
767 ret = _gsskrb5_lifetime_left(minor_status,
772 ret = GSS_S_COMPLETE;
775 *ret_flags = ctx->flags;
777 if (req_flags & GSS_C_DCE_STYLE) {
778 int32_t local_seq, remote_seq;
782 * So DCE_STYLE is strange. The client echos the seq number
783 * that the server used in the server's mk_rep in its own
784 * mk_rep(). After when done, it resets to it's own seq number
785 * for the gss_wrap calls.
788 krb5_auth_getremoteseqnumber(context, ctx->auth_context, &remote_seq);
789 krb5_auth_con_getlocalseqnumber(context, ctx->auth_context, &local_seq);
790 krb5_auth_con_setlocalseqnumber(context, ctx->auth_context, remote_seq);
792 kret = krb5_mk_rep(context, ctx->auth_context, &outbuf);
794 *minor_status = kret;
795 return GSS_S_FAILURE;
798 /* reset local seq number */
799 krb5_auth_con_setlocalseqnumber(context, ctx->auth_context, local_seq);
801 output_token->length = outbuf.length;
802 output_token->value = outbuf.data;
805 return gsskrb5_initiator_ready(minor_status, ctx, context);
809 * gss_init_sec_context
812 OM_uint32 _gsskrb5_init_sec_context
813 (OM_uint32 * minor_status,
814 const gss_cred_id_t cred_handle,
815 gss_ctx_id_t * context_handle,
816 const gss_name_t target_name,
817 const gss_OID mech_type,
820 const gss_channel_bindings_t input_chan_bindings,
821 const gss_buffer_t input_token,
822 gss_OID * actual_mech_type,
823 gss_buffer_t output_token,
824 OM_uint32 * ret_flags,
828 krb5_context context;
829 gsskrb5_cred cred = (gsskrb5_cred)cred_handle;
833 GSSAPI_KRB5_INIT (&context);
835 output_token->length = 0;
836 output_token->value = NULL;
838 if (context_handle == NULL) {
840 return GSS_S_FAILURE | GSS_S_CALL_BAD_STRUCTURE;
848 if (target_name == GSS_C_NO_NAME) {
849 if (actual_mech_type)
850 *actual_mech_type = GSS_C_NO_OID;
852 return GSS_S_BAD_NAME;
855 if (mech_type != GSS_C_NO_OID &&
856 !gss_oid_equal(mech_type, GSS_KRB5_MECHANISM))
857 return GSS_S_BAD_MECH;
859 if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
862 if (*context_handle != GSS_C_NO_CONTEXT) {
864 return GSS_S_FAILURE | GSS_S_CALL_BAD_STRUCTURE;
867 ret = _gsskrb5_create_ctx(minor_status,
876 if (*context_handle == GSS_C_NO_CONTEXT) {
878 return GSS_S_FAILURE | GSS_S_CALL_BAD_STRUCTURE;
881 ctx = (gsskrb5_ctx) *context_handle;
883 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
886 switch (ctx->state) {
887 case INITIATOR_START:
888 ret = init_auth(minor_status,
901 if (ret != GSS_S_COMPLETE)
904 case INITIATOR_RESTART:
905 ret = init_auth_restart(minor_status,
917 case INITIATOR_WAIT_FOR_MUTAL:
918 ret = repl_mutual(minor_status,
930 if (ctx->state == INITIATOR_RESTART)
933 case INITIATOR_READY:
935 * If we get there, the caller have called
936 * gss_init_sec_context() one time too many.
938 _gsskrb5_set_status(EINVAL, "init_sec_context "
939 "called one time too many");
940 *minor_status = EINVAL;
941 ret = GSS_S_BAD_STATUS;
944 _gsskrb5_set_status(EINVAL, "init_sec_context "
945 "invalid state %d for client",
947 *minor_status = EINVAL;
948 ret = GSS_S_BAD_STATUS;
951 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
953 /* destroy context in case of error */
954 if (GSS_ERROR(ret)) {
956 _gsskrb5_delete_sec_context(&min2, context_handle, GSS_C_NO_BUFFER);