Home | History | Annotate | Line # | Download | only in mech
      1 /*	$NetBSD: gss_add_cred.c,v 1.2 2017/01/28 21:31:46 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005 Doug Rabson
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  *	$FreeBSD: src/lib/libgssapi/gss_add_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
     29  */
     30 
     31 #include "mech_locl.h"
     32 
     33 struct _gss_mechanism_cred *
     34 _gss_copy_cred(struct _gss_mechanism_cred *mc)
     35 {
     36 	struct _gss_mechanism_cred *new_mc;
     37 	gssapi_mech_interface m = mc->gmc_mech;
     38 	OM_uint32 major_status, minor_status;
     39 	gss_name_t name;
     40 	gss_cred_id_t cred;
     41 	OM_uint32 initiator_lifetime, acceptor_lifetime;
     42 	gss_cred_usage_t cred_usage;
     43 
     44 	major_status = m->gm_inquire_cred_by_mech(&minor_status,
     45 	    mc->gmc_cred, mc->gmc_mech_oid,
     46 	    &name, &initiator_lifetime, &acceptor_lifetime, &cred_usage);
     47 	if (major_status) {
     48 		_gss_mg_error(m, major_status, minor_status);
     49 		return (0);
     50 	}
     51 
     52 	major_status = m->gm_add_cred(&minor_status,
     53 	    GSS_C_NO_CREDENTIAL, name, mc->gmc_mech_oid,
     54 	    cred_usage, initiator_lifetime, acceptor_lifetime,
     55 	    &cred, 0, 0, 0);
     56 	m->gm_release_name(&minor_status, &name);
     57 
     58 	if (major_status) {
     59 		_gss_mg_error(m, major_status, minor_status);
     60 		return (0);
     61 	}
     62 
     63 	new_mc = malloc(sizeof(struct _gss_mechanism_cred));
     64 	if (!new_mc) {
     65 		m->gm_release_cred(&minor_status, &cred);
     66 		return (0);
     67 	}
     68 	new_mc->gmc_mech = m;
     69 	new_mc->gmc_mech_oid = &m->gm_mech_oid;
     70 	new_mc->gmc_cred = cred;
     71 
     72 	return (new_mc);
     73 }
     74 
     75 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
     76 gss_add_cred(OM_uint32 *minor_status,
     77     gss_const_cred_id_t input_cred_handle,
     78     gss_const_name_t desired_name,
     79     const gss_OID desired_mech,
     80     gss_cred_usage_t cred_usage,
     81     OM_uint32 initiator_time_req,
     82     OM_uint32 acceptor_time_req,
     83     gss_cred_id_t *output_cred_handle,
     84     gss_OID_set *actual_mechs,
     85     OM_uint32 *initiator_time_rec,
     86     OM_uint32 *acceptor_time_rec)
     87 {
     88 	OM_uint32 major_status;
     89 	gssapi_mech_interface m;
     90 	struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
     91 	struct _gss_cred *new_cred;
     92 	gss_cred_id_t release_cred;
     93 	struct _gss_mechanism_cred *mc, *target_mc, *copy_mc;
     94 	struct _gss_mechanism_name *mn;
     95 	OM_uint32 junk;
     96 
     97 	*minor_status = 0;
     98 	*output_cred_handle = GSS_C_NO_CREDENTIAL;
     99 	if (initiator_time_rec)
    100 	    *initiator_time_rec = 0;
    101 	if (acceptor_time_rec)
    102 	    *acceptor_time_rec = 0;
    103 	if (actual_mechs)
    104 	    *actual_mechs = GSS_C_NO_OID_SET;
    105 
    106 	new_cred = malloc(sizeof(struct _gss_cred));
    107 	if (!new_cred) {
    108 		*minor_status = ENOMEM;
    109 		return (GSS_S_FAILURE);
    110 	}
    111 	HEIM_SLIST_INIT(&new_cred->gc_mc);
    112 
    113 	/*
    114 	 * We go through all the mc attached to the input_cred_handle
    115 	 * and check the mechanism. If it matches, we call
    116 	 * gss_add_cred for that mechanism, otherwise we copy the mc
    117 	 * to new_cred.
    118 	 */
    119 	target_mc = 0;
    120 	if (cred) {
    121 		HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
    122 			if (gss_oid_equal(mc->gmc_mech_oid, desired_mech)) {
    123 				target_mc = mc;
    124 			}
    125 			copy_mc = _gss_copy_cred(mc);
    126 			if (!copy_mc) {
    127 				release_cred = (gss_cred_id_t)new_cred;
    128 				gss_release_cred(&junk, &release_cred);
    129 				*minor_status = ENOMEM;
    130 				return (GSS_S_FAILURE);
    131 			}
    132 			HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
    133 		}
    134 	}
    135 
    136 	/*
    137 	 * Figure out a suitable mn, if any.
    138 	 */
    139 	if (desired_name) {
    140 		major_status = _gss_find_mn(minor_status,
    141 					    (struct _gss_name *) desired_name,
    142 					    desired_mech,
    143 					    &mn);
    144 		if (major_status != GSS_S_COMPLETE) {
    145 			free(new_cred);
    146 			return major_status;
    147 		}
    148 	} else {
    149 		mn = 0;
    150 	}
    151 
    152 	m = __gss_get_mechanism(desired_mech);
    153 
    154 	mc = malloc(sizeof(struct _gss_mechanism_cred));
    155 	if (!mc) {
    156 		release_cred = (gss_cred_id_t)new_cred;
    157 		gss_release_cred(&junk, &release_cred);
    158 		*minor_status = ENOMEM;
    159 		return (GSS_S_FAILURE);
    160 	}
    161 	mc->gmc_mech = m;
    162 	mc->gmc_mech_oid = &m->gm_mech_oid;
    163 
    164 	major_status = m->gm_add_cred(minor_status,
    165 	    target_mc ? target_mc->gmc_cred : GSS_C_NO_CREDENTIAL,
    166 	    desired_name ? mn->gmn_name : GSS_C_NO_NAME,
    167 	    desired_mech,
    168 	    cred_usage,
    169 	    initiator_time_req,
    170 	    acceptor_time_req,
    171 	    &mc->gmc_cred,
    172 	    actual_mechs,
    173 	    initiator_time_rec,
    174 	    acceptor_time_rec);
    175 
    176 	if (major_status) {
    177 		_gss_mg_error(m, major_status, *minor_status);
    178 		release_cred = (gss_cred_id_t)new_cred;
    179 		gss_release_cred(&junk, &release_cred);
    180 		free(mc);
    181 		return (major_status);
    182 	}
    183 	HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
    184 	*output_cred_handle = (gss_cred_id_t) new_cred;
    185 
    186 	return (GSS_S_COMPLETE);
    187 }
    188 
    189