some work on fast reauth
[mech_eap.git] / util_cred.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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  * 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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 #include "gssapiP_eap.h"
34
35 OM_uint32
36 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred)
37 {
38     OM_uint32 tmpMinor;
39     gss_cred_id_t cred;
40
41     *pCred = GSS_C_NO_CREDENTIAL;
42
43     cred = (gss_cred_id_t)GSSEAP_CALLOC(1, sizeof(*cred));
44     if (cred == NULL) {
45         *minor = ENOMEM;
46         return GSS_S_FAILURE;
47     }
48
49     if (GSSEAP_MUTEX_INIT(&cred->mutex) != 0) {
50         *minor = errno;
51         gssEapReleaseCred(&tmpMinor, &cred);
52         return GSS_S_FAILURE;
53     }
54
55     *pCred = cred;
56
57     *minor = 0;
58     return GSS_S_COMPLETE;
59 }
60
61 OM_uint32
62 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred)
63 {
64     OM_uint32 tmpMinor;
65     gss_cred_id_t cred = *pCred;
66     krb5_context krbContext = NULL;
67
68     if (cred == GSS_C_NO_CREDENTIAL) {
69         return GSS_S_COMPLETE;
70     }
71
72     GSSEAP_KRB_INIT(&krbContext);
73
74     gssEapReleaseName(&tmpMinor, &cred->name);
75
76     if (cred->password.value != NULL) {
77         memset(cred->password.value, 0, cred->password.length);
78         GSSEAP_FREE(cred->password.value);
79     }
80
81     if (cred->radiusConfigFile != NULL)
82         GSSEAP_FREE(cred->radiusConfigFile);
83
84     if (cred->krbCredCache != NULL)
85         krb5_cc_destroy(krbContext, cred->krbCredCache);
86
87     GSSEAP_MUTEX_DESTROY(&cred->mutex);
88     memset(cred, 0, sizeof(*cred));
89     GSSEAP_FREE(cred);
90     *pCred = NULL;
91
92     *minor = 0;
93     return GSS_S_COMPLETE;
94 }
95
96 OM_uint32
97 gssEapAcquireCred(OM_uint32 *minor,
98                   const gss_name_t desiredName,
99                   const gss_buffer_t password,
100                   OM_uint32 timeReq,
101                   const gss_OID_set desiredMechs,
102                   int credUsage,
103                   gss_cred_id_t *pCred,
104                   gss_OID_set *pActualMechs,
105                   OM_uint32 *timeRec)
106 {
107     OM_uint32 major, tmpMinor;
108     gss_cred_id_t cred;
109
110     /* XXX TODO validate with changed set_cred_option API */
111     *pCred = GSS_C_NO_CREDENTIAL;
112
113     major = gssEapAllocCred(minor, &cred);
114     if (GSS_ERROR(major))
115         goto cleanup;
116
117     switch (credUsage) {
118     case GSS_C_BOTH:
119         cred->flags |= CRED_FLAG_INITIATE | CRED_FLAG_ACCEPT;
120         break;
121     case GSS_C_INITIATE:
122         cred->flags |= CRED_FLAG_INITIATE;
123         break;
124     case GSS_C_ACCEPT:
125         cred->flags |= CRED_FLAG_ACCEPT;
126         break;
127     default:
128         major = GSS_S_FAILURE;
129         goto cleanup;
130         break;
131     }
132
133     if (desiredName != GSS_C_NO_NAME) {
134         major = gssEapDuplicateName(minor, desiredName, &cred->name);
135         if (GSS_ERROR(major))
136             goto cleanup;
137     } else {
138         if (cred->flags & CRED_FLAG_INITIATE) {
139             gss_buffer_desc buf;
140
141             buf.value = getlogin(); /* XXX */
142             buf.length = strlen((char *)buf.value);
143
144             major = gssEapImportName(minor, &buf,
145                                      GSS_C_NT_USER_NAME, &cred->name);
146             if (GSS_ERROR(major))
147                 goto cleanup;
148         }
149
150         cred->flags |= CRED_FLAG_DEFAULT_IDENTITY;
151     }
152
153     if (password != GSS_C_NO_BUFFER) {
154         major = duplicateBuffer(minor, password, &cred->password);
155         if (GSS_ERROR(major))
156             goto cleanup;
157
158         cred->flags |= CRED_FLAG_PASSWORD;
159     }
160
161     major = gssEapValidateMechs(minor, desiredMechs);
162     if (GSS_ERROR(major))
163         goto cleanup;
164
165     major = duplicateOidSet(minor, desiredMechs, &cred->mechanisms);
166     if (GSS_ERROR(major))
167         goto cleanup;
168
169     if (pActualMechs != NULL) {
170         major = duplicateOidSet(minor, cred->mechanisms, pActualMechs);
171         if (GSS_ERROR(major))
172             goto cleanup;
173     }
174
175     if (timeRec != NULL)
176         *timeRec = GSS_C_INDEFINITE;
177
178     *pCred = cred;
179     major = GSS_S_COMPLETE;
180
181 cleanup:
182     if (GSS_ERROR(major))
183         gssEapReleaseCred(&tmpMinor, &cred);
184
185     return major;
186 }
187
188 int
189 gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech)
190 {
191     OM_uint32 minor;
192     int present = 0;
193
194     assert(mech != GSS_C_NO_OID);
195
196     if (cred == GSS_C_NO_CREDENTIAL || cred->mechanisms == GSS_C_NO_OID_SET)
197         return TRUE;
198
199     gss_test_oid_set_member(&minor, mech, cred->mechanisms, &present);
200
201     return present;
202 }