| 1 |
/* |
| 2 |
* Copyright (c) 2003 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 |
OM_uint32 GSSAPI_CALLCONV _gsskrb5_add_cred ( |
| 37 |
OM_uint32 *minor_status, |
| 38 |
gss_const_cred_id_t input_cred_handle, |
| 39 |
gss_const_name_t desired_name, |
| 40 |
const gss_OID desired_mech, |
| 41 |
gss_cred_usage_t cred_usage, |
| 42 |
OM_uint32 initiator_time_req, |
| 43 |
OM_uint32 acceptor_time_req, |
| 44 |
gss_cred_id_t *output_cred_handle, |
| 45 |
gss_OID_set *actual_mechs, |
| 46 |
OM_uint32 *initiator_time_rec, |
| 47 |
OM_uint32 *acceptor_time_rec) |
| 48 |
{ |
| 49 |
krb5_context context; |
| 50 |
OM_uint32 major, lifetime; |
| 51 |
gsskrb5_cred cred, handle; |
| 52 |
krb5_const_principal dname; |
| 53 |
|
| 54 |
handle = NULL; |
| 55 |
cred = (gsskrb5_cred)input_cred_handle; |
| 56 |
dname = (krb5_const_principal)desired_name; |
| 57 |
|
| 58 |
if (cred == NULL && output_cred_handle == NULL) { |
| 59 |
*minor_status = EINVAL; |
| 60 |
return GSS_S_CALL_INACCESSIBLE_WRITE; |
| 61 |
} |
| 62 |
|
| 63 |
GSSAPI_KRB5_INIT (&context); |
| 64 |
|
| 65 |
if (desired_mech != GSS_C_NO_OID && |
| 66 |
gss_oid_equal(desired_mech, GSS_KRB5_MECHANISM) == 0) { |
| 67 |
*minor_status = 0; |
| 68 |
return GSS_S_BAD_MECH; |
| 69 |
} |
| 70 |
|
| 71 |
if (cred == NULL) { |
| 72 |
/* |
| 73 |
* Acquire a credential; output_cred_handle can't be NULL, see above. |
| 74 |
*/ |
| 75 |
heim_assert(output_cred_handle != NULL, |
| 76 |
"internal error in _gsskrb5_add_cred()"); |
| 77 |
|
| 78 |
major = _gsskrb5_acquire_cred(minor_status, desired_name, |
| 79 |
min(initiator_time_req, |
| 80 |
acceptor_time_req), |
| 81 |
GSS_C_NO_OID_SET, |
| 82 |
cred_usage, |
| 83 |
output_cred_handle, |
| 84 |
actual_mechs, &lifetime); |
| 85 |
if (major != GSS_S_COMPLETE) |
| 86 |
goto failure; |
| 87 |
|
| 88 |
} else { |
| 89 |
/* |
| 90 |
* Check that we're done or copy input to output if |
| 91 |
* output_cred_handle != NULL. |
| 92 |
*/ |
| 93 |
|
| 94 |
HEIMDAL_MUTEX_lock(&cred->cred_id_mutex); |
| 95 |
|
| 96 |
/* Check if requested output usage is compatible with output usage */ |
| 97 |
if (cred->usage != cred_usage && cred->usage != GSS_C_BOTH) { |
| 98 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 99 |
*minor_status = GSS_KRB5_S_G_BAD_USAGE; |
| 100 |
return(GSS_S_FAILURE); |
| 101 |
} |
| 102 |
|
| 103 |
/* Check that we have the same name */ |
| 104 |
if (dname != NULL && |
| 105 |
krb5_principal_compare(context, dname, |
| 106 |
cred->principal) != FALSE) { |
| 107 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 108 |
*minor_status = 0; |
| 109 |
return GSS_S_BAD_NAME; |
| 110 |
} |
| 111 |
|
| 112 |
if (output_cred_handle == NULL) { |
| 113 |
/* |
| 114 |
* This case is basically useless as we implement a single |
| 115 |
* mechanism here, so we can't add elements to the |
| 116 |
* input_cred_handle. |
| 117 |
*/ |
| 118 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 119 |
*minor_status = 0; |
| 120 |
return GSS_S_COMPLETE; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
* Copy input to output -- this works as if we were a |
| 125 |
* GSS_Duplicate_cred() for one mechanism element. |
| 126 |
*/ |
| 127 |
|
| 128 |
handle = calloc(1, sizeof(*handle)); |
| 129 |
if (handle == NULL) { |
| 130 |
if (cred != NULL) |
| 131 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 132 |
*minor_status = ENOMEM; |
| 133 |
return (GSS_S_FAILURE); |
| 134 |
} |
| 135 |
|
| 136 |
handle->usage = cred_usage; |
| 137 |
handle->endtime = cred->endtime; |
| 138 |
handle->principal = NULL; |
| 139 |
handle->keytab = NULL; |
| 140 |
handle->ccache = NULL; |
| 141 |
handle->mechanisms = NULL; |
| 142 |
HEIMDAL_MUTEX_init(&handle->cred_id_mutex); |
| 143 |
|
| 144 |
major = GSS_S_FAILURE; |
| 145 |
|
| 146 |
*minor_status = krb5_copy_principal(context, cred->principal, |
| 147 |
&handle->principal); |
| 148 |
if (*minor_status) { |
| 149 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 150 |
free(handle); |
| 151 |
return GSS_S_FAILURE; |
| 152 |
} |
| 153 |
|
| 154 |
if (cred->keytab) { |
| 155 |
char *name = NULL; |
| 156 |
|
| 157 |
*minor_status = krb5_kt_get_full_name(context, cred->keytab, |
| 158 |
&name); |
| 159 |
if (*minor_status) |
| 160 |
goto failure; |
| 161 |
|
| 162 |
*minor_status = krb5_kt_resolve(context, name, &handle->keytab); |
| 163 |
krb5_xfree(name); |
| 164 |
if (*minor_status) |
| 165 |
goto failure; |
| 166 |
} |
| 167 |
|
| 168 |
if (cred->ccache) { |
| 169 |
const char *type, *name; |
| 170 |
char *type_name = NULL; |
| 171 |
|
| 172 |
type = krb5_cc_get_type(context, cred->ccache); |
| 173 |
if (type == NULL){ |
| 174 |
*minor_status = ENOMEM; |
| 175 |
goto failure; |
| 176 |
} |
| 177 |
|
| 178 |
if (strcmp(type, "MEMORY") == 0) { |
| 179 |
*minor_status = krb5_cc_new_unique(context, type, |
| 180 |
NULL, &handle->ccache); |
| 181 |
if (*minor_status) |
| 182 |
goto failure; |
| 183 |
|
| 184 |
*minor_status = krb5_cc_copy_cache(context, cred->ccache, |
| 185 |
handle->ccache); |
| 186 |
if (*minor_status) |
| 187 |
goto failure; |
| 188 |
|
| 189 |
} else { |
| 190 |
name = krb5_cc_get_name(context, cred->ccache); |
| 191 |
if (name == NULL) { |
| 192 |
*minor_status = ENOMEM; |
| 193 |
goto failure; |
| 194 |
} |
| 195 |
|
| 196 |
if (asprintf(&type_name, "%s:%s", type, name) == -1 || |
| 197 |
type_name == NULL) { |
| 198 |
*minor_status = ENOMEM; |
| 199 |
goto failure; |
| 200 |
} |
| 201 |
|
| 202 |
*minor_status = krb5_cc_resolve(context, type_name, |
| 203 |
&handle->ccache); |
| 204 |
free(type_name); |
| 205 |
if (*minor_status) |
| 206 |
goto failure; |
| 207 |
} |
| 208 |
} |
| 209 |
major = gss_create_empty_oid_set(minor_status, &handle->mechanisms); |
| 210 |
if (major != GSS_S_COMPLETE) |
| 211 |
goto failure; |
| 212 |
|
| 213 |
major = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM, |
| 214 |
&handle->mechanisms); |
| 215 |
if (major != GSS_S_COMPLETE) |
| 216 |
goto failure; |
| 217 |
|
| 218 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 219 |
|
| 220 |
major = _gsskrb5_inquire_cred(minor_status, (gss_cred_id_t)cred, |
| 221 |
NULL, &lifetime, NULL, actual_mechs); |
| 222 |
if (major != GSS_S_COMPLETE) |
| 223 |
goto failure; |
| 224 |
|
| 225 |
*output_cred_handle = (gss_cred_id_t)handle; |
| 226 |
} |
| 227 |
|
| 228 |
if (initiator_time_rec) |
| 229 |
*initiator_time_rec = lifetime; |
| 230 |
if (acceptor_time_rec) |
| 231 |
*acceptor_time_rec = lifetime; |
| 232 |
|
| 233 |
*minor_status = 0; |
| 234 |
return major; |
| 235 |
|
| 236 |
failure: |
| 237 |
if (handle) { |
| 238 |
if (handle->principal) |
| 239 |
krb5_free_principal(context, handle->principal); |
| 240 |
if (handle->keytab) |
| 241 |
krb5_kt_close(context, handle->keytab); |
| 242 |
if (handle->ccache) |
| 243 |
krb5_cc_destroy(context, handle->ccache); |
| 244 |
if (handle->mechanisms) |
| 245 |
gss_release_oid_set(NULL, &handle->mechanisms); |
| 246 |
free(handle); |
| 247 |
} |
| 248 |
if (cred && output_cred_handle) |
| 249 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 250 |
return major; |
| 251 |
} |