2 * Copyright (c) 2011, JANET(UK)
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 JANET(UK) 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND 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 THE COPYRIGHT HOLDER 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
34 * Utility routines for credential handles.
37 #include "gssapiP_eap.h"
40 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred)
45 *pCred = GSS_C_NO_CREDENTIAL;
47 cred = (gss_cred_id_t)GSSEAP_CALLOC(1, sizeof(*cred));
53 if (GSSEAP_MUTEX_INIT(&cred->mutex) != 0) {
55 gssEapReleaseCred(&tmpMinor, &cred);
62 return GSS_S_COMPLETE;
66 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred)
69 gss_cred_id_t cred = *pCred;
70 krb5_context krbContext = NULL;
72 if (cred == GSS_C_NO_CREDENTIAL) {
73 return GSS_S_COMPLETE;
76 GSSEAP_KRB_INIT(&krbContext);
78 gssEapReleaseName(&tmpMinor, &cred->name);
80 if (cred->password.value != NULL) {
81 memset(cred->password.value, 0, cred->password.length);
82 GSSEAP_FREE(cred->password.value);
85 if (cred->radiusConfigFile != NULL)
86 GSSEAP_FREE(cred->radiusConfigFile);
87 if (cred->radiusConfigStanza != NULL)
88 GSSEAP_FREE(cred->radiusConfigStanza);
90 #ifdef GSSEAP_ENABLE_REAUTH
91 if (cred->krbCredCache != NULL) {
92 if (cred->flags & CRED_FLAG_DEFAULT_CCACHE)
93 krb5_cc_close(krbContext, cred->krbCredCache);
95 krb5_cc_destroy(krbContext, cred->krbCredCache);
97 if (cred->krbCred != GSS_C_NO_CREDENTIAL)
98 gssReleaseCred(&tmpMinor, &cred->krbCred);
101 GSSEAP_MUTEX_DESTROY(&cred->mutex);
102 memset(cred, 0, sizeof(*cred));
107 return GSS_S_COMPLETE;
111 gssEapAcquireCred(OM_uint32 *minor,
112 const gss_name_t desiredName,
113 const gss_buffer_t password,
115 const gss_OID_set desiredMechs,
117 gss_cred_id_t *pCred,
118 gss_OID_set *pActualMechs,
121 OM_uint32 major, tmpMinor;
124 /* XXX TODO validate with changed set_cred_option API */
125 *pCred = GSS_C_NO_CREDENTIAL;
127 major = gssEapAllocCred(minor, &cred);
128 if (GSS_ERROR(major))
133 cred->flags |= CRED_FLAG_INITIATE | CRED_FLAG_ACCEPT;
136 cred->flags |= CRED_FLAG_INITIATE;
139 cred->flags |= CRED_FLAG_ACCEPT;
142 major = GSS_S_FAILURE;
143 *minor = GSSEAP_BAD_USAGE;
148 if (desiredName != GSS_C_NO_NAME) {
149 GSSEAP_MUTEX_LOCK(&desiredName->mutex);
151 major = gssEapDuplicateName(minor, desiredName, &cred->name);
152 if (GSS_ERROR(major)) {
153 GSSEAP_MUTEX_UNLOCK(&desiredName->mutex);
157 GSSEAP_MUTEX_UNLOCK(&desiredName->mutex);
159 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
160 gss_OID nameType = GSS_C_NO_OID;
162 if (cred->flags & CRED_FLAG_ACCEPT) {
163 char serviceName[5 + MAXHOSTNAMELEN] = "host@";
165 /* default host-based service is host@localhost */
166 if (gethostname(&serviceName[5], MAXHOSTNAMELEN) != 0) {
167 major = GSS_S_FAILURE;
168 *minor = GSSEAP_NO_HOSTNAME;
172 nameBuf.value = serviceName;
173 nameBuf.length = strlen((char *)nameBuf.value);
175 nameType = GSS_C_NT_HOSTBASED_SERVICE;
176 } else if (cred->flags & CRED_FLAG_INITIATE) {
177 nameBuf.value = getlogin(); /* XXX */
178 nameBuf.length = strlen((char *)nameBuf.value);
180 nameType = GSS_C_NT_USER_NAME;
183 if (nameBuf.length != 0) {
184 major = gssEapImportName(minor, &nameBuf, nameType, &cred->name);
185 if (GSS_ERROR(major))
189 cred->flags |= CRED_FLAG_DEFAULT_IDENTITY;
192 if (password != GSS_C_NO_BUFFER) {
193 major = duplicateBuffer(minor, password, &cred->password);
194 if (GSS_ERROR(major))
197 cred->flags |= CRED_FLAG_PASSWORD;
198 } else if (cred->flags & CRED_FLAG_INITIATE) {
200 * OK, here we need to ask the supplicant if we have creds or it
201 * will acquire them, so GS2 can know whether to prompt for a
205 && !gssEapCanReauthP(cred, GSS_C_NO_NAME, timeReq)
207 major = GSS_S_CRED_UNAVAIL;
208 *minor = GSSEAP_MISSING_PASSWORD;
212 major = gssEapValidateMechs(minor, desiredMechs);
213 if (GSS_ERROR(major))
216 major = duplicateOidSet(minor, desiredMechs, &cred->mechanisms);
217 if (GSS_ERROR(major))
220 if (pActualMechs != NULL) {
221 major = duplicateOidSet(minor, cred->mechanisms, pActualMechs);
222 if (GSS_ERROR(major))
227 *timeRec = GSS_C_INDEFINITE;
231 major = GSS_S_COMPLETE;
235 if (GSS_ERROR(major))
236 gssEapReleaseCred(&tmpMinor, &cred);
242 * Return TRUE if cred available for mechanism. Caller need no acquire
243 * lock because mechanisms list is immutable.
246 gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech)
251 assert(mech != GSS_C_NO_OID);
253 if (cred == GSS_C_NO_CREDENTIAL || cred->mechanisms == GSS_C_NO_OID_SET)
256 gss_test_oid_set_member(&minor, mech, cred->mechanisms, &present);