2 * Copyright (c) 2004, PADL Software Pty Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * 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.
16 * 3. Neither the name of PADL Software nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "krb5/gsskrb5_locl.h"
35 RCSID("$Id: set_cred_option.c 20325 2007-04-12 16:49:17Z lha $");
37 static gss_OID_desc gss_krb5_import_cred_x_oid_desc =
38 {9, (void *)"\x2b\x06\x01\x04\x01\xa9\x4a\x13\x04"}; /* XXX */
40 gss_OID GSS_KRB5_IMPORT_CRED_X = &gss_krb5_import_cred_x_oid_desc;
43 import_cred(OM_uint32 *minor_status,
45 gss_cred_id_t *cred_handle,
46 const gss_buffer_t value)
50 krb5_principal keytab_principal = NULL;
51 krb5_keytab keytab = NULL;
52 krb5_storage *sp = NULL;
53 krb5_ccache id = NULL;
56 if (cred_handle == NULL || *cred_handle != GSS_C_NO_CREDENTIAL) {
61 sp = krb5_storage_from_mem(value->value, value->length);
67 /* credential cache name */
68 ret = krb5_ret_string(sp, &str);
71 major_stat = GSS_S_FAILURE;
75 ret = krb5_cc_resolve(context, str, &id);
78 major_stat = GSS_S_FAILURE;
85 /* keytab principal name */
86 ret = krb5_ret_string(sp, &str);
87 if (ret == 0 && str[0])
88 ret = krb5_parse_name(context, str, &keytab_principal);
91 major_stat = GSS_S_FAILURE;
97 /* keytab principal */
98 ret = krb5_ret_string(sp, &str);
101 major_stat = GSS_S_FAILURE;
105 ret = krb5_kt_resolve(context, str, &keytab);
108 major_stat = GSS_S_FAILURE;
115 major_stat = _gsskrb5_import_cred(minor_status, id, keytab_principal,
116 keytab, cred_handle);
119 krb5_cc_close(context, id);
120 if (keytab_principal)
121 krb5_free_principal(context, keytab_principal);
123 krb5_kt_close(context, keytab);
127 krb5_storage_free(sp);
134 allowed_enctypes(OM_uint32 *minor_status,
135 krb5_context context,
136 gss_cred_id_t *cred_handle,
137 const gss_buffer_t value)
139 OM_uint32 major_stat;
142 krb5_enctype *enctypes = NULL;
143 krb5_storage *sp = NULL;
146 if (cred_handle == NULL || *cred_handle == GSS_C_NO_CREDENTIAL) {
148 return GSS_S_FAILURE;
151 cred = (gsskrb5_cred)*cred_handle;
153 if ((value->length % 4) != 0) {
155 major_stat = GSS_S_FAILURE;
159 len = value->length / 4;
160 enctypes = malloc((len + 1) * 4);
161 if (enctypes == NULL) {
162 *minor_status = ENOMEM;
163 major_stat = GSS_S_FAILURE;
167 sp = krb5_storage_from_mem(value->value, value->length);
169 *minor_status = ENOMEM;
170 major_stat = GSS_S_FAILURE;
174 for (i = 0; i < len; i++) {
177 ret = krb5_ret_uint32(sp, &e);
180 major_stat = GSS_S_FAILURE;
188 free(cred->enctypes);
189 cred->enctypes = enctypes;
191 krb5_storage_free(sp);
193 return GSS_S_COMPLETE;
197 krb5_storage_free(sp);
206 _gsskrb5_set_cred_option
207 (OM_uint32 *minor_status,
208 gss_cred_id_t *cred_handle,
209 const gss_OID desired_object,
210 const gss_buffer_t value)
212 krb5_context context;
214 GSSAPI_KRB5_INIT (&context);
216 if (value == GSS_C_NO_BUFFER) {
217 *minor_status = EINVAL;
218 return GSS_S_FAILURE;
221 if (gss_oid_equal(desired_object, GSS_KRB5_IMPORT_CRED_X))
222 return import_cred(minor_status, context, cred_handle, value);
224 if (gss_oid_equal(desired_object, GSS_KRB5_SET_ALLOWABLE_ENCTYPES_X))
225 return allowed_enctypes(minor_status, context, cred_handle, value);
227 *minor_status = EINVAL;
228 return GSS_S_FAILURE;