Cleanup
[moonshot.git] / mech_eap / util_name.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  * Portions Copyright 2009 by the Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  */
55
56 #include "gssapiP_eap.h"
57
58 static const gss_OID_desc gssEapNtPrincipalName = {
59     /* 1.3.6.1.4.1.5322.21.2.1  */
60     12, "\x06\x0A\x2B\x06\x01\x04\x01\xA9\x4A\x15\x02\x01"
61 };
62
63 gss_OID GSS_EAP_NT_PRINCIPAL_NAME = &gssEapNtPrincipalName;
64
65 OM_uint32
66 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName)
67 {
68     OM_uint32 tmpMinor;
69     gss_name_t name;
70
71     assert(*pName == GSS_C_NO_NAME);
72
73     name = (gss_name_t)GSSEAP_CALLOC(1, sizeof(*name));
74     if (name == NULL) {
75         *minor = ENOMEM;
76         return GSS_S_FAILURE;
77     }
78
79     if (GSSEAP_MUTEX_INIT(&name->mutex) != 0) {
80         *minor = errno;
81         gssEapReleaseName(&tmpMinor, &name);
82         return GSS_S_FAILURE;
83     }
84
85     *pName = name;
86
87     return GSS_S_COMPLETE;
88 }
89
90 OM_uint32
91 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName)
92 {
93     gss_name_t name;
94     krb5_context krbContext = NULL;
95     OM_uint32 tmpMinor;
96
97     if (pName == NULL) {
98         return GSS_S_COMPLETE;
99     }
100
101     name = *pName;
102     if (name == GSS_C_NO_NAME) {
103         return GSS_S_COMPLETE;
104     }
105
106     GSSEAP_KRB_INIT(&krbContext);
107     krb5_free_principal(krbContext, name->krbPrincipal);
108
109     radiusFreeAVPs(&tmpMinor, name->avps);
110     samlFreeAssertion(&tmpMinor, name->assertion);
111
112     GSSEAP_MUTEX_DESTROY(&name->mutex);
113     GSSEAP_FREE(name);
114     *pName = NULL;
115
116     *minor = 0;
117     return GSS_S_COMPLETE;
118 }
119
120 static OM_uint32
121 krbPrincipalToName(OM_uint32 *minor,
122                    krb5_principal *principal,
123                    gss_name_t *pName)
124 {
125     OM_uint32 major;
126     gss_name_t name;
127
128     major = gssEapAllocName(minor, &name);
129     if (GSS_ERROR(major))
130         return major;
131
132     name->krbPrincipal = *principal;
133     *principal = NULL;
134
135     *minor = 0;
136     return GSS_S_COMPLETE;
137 }
138
139 static OM_uint32
140 importServiceName(OM_uint32 *minor,
141                   const gss_buffer_t nameBuffer,
142                   gss_name_t *pName)
143 {
144     OM_uint32 major, tmpMinor;
145     krb5_context krbContext;
146     krb5_principal krbPrinc;
147     char *service, *host;
148
149     GSSEAP_KRB_INIT(&krbContext);
150
151     major = bufferToString(minor, nameBuffer, &service);
152     if (GSS_ERROR(major))
153         return major;
154
155     host = strchr(service, '@');
156     if (host != NULL) {
157         *host = '\0';
158         host++;
159     }    
160
161     /* XXX this is probably NOT what we want to be doing */
162     *minor = krb5_sname_to_principal(krbContext, host, service,
163                                      KRB5_NT_SRV_HST, &krbPrinc);
164     if (*minor != 0) {
165         GSSEAP_FREE(service);
166         return GSS_S_FAILURE;
167     }
168
169     major = krbPrincipalToName(minor, &krbPrinc, pName);
170     if (GSS_ERROR(major)) {
171         krb5_free_principal(krbContext, krbPrinc);
172     }
173
174     GSSEAP_FREE(service);
175     return major;
176 }
177
178 static OM_uint32
179 importUserName(OM_uint32 *minor,
180                const gss_buffer_t nameBuffer,
181                gss_name_t *pName)
182 {
183     OM_uint32 major, tmpMinor;
184     krb5_context krbContext;
185     krb5_principal krbPrinc;
186     char *nameString;
187
188     GSSEAP_KRB_INIT(&krbContext);
189
190     major = bufferToString(minor, nameBuffer, &nameString);
191     if (GSS_ERROR(major))
192         return major;
193
194     *minor = krb5_parse_name(krbContext, nameString, &krbPrinc);
195     if (*minor != 0) {
196         GSSEAP_FREE(nameString);
197         return GSS_S_FAILURE;
198     }
199
200     major = krbPrincipalToName(minor, &krbPrinc, pName);
201     if (GSS_ERROR(major)) {
202         krb5_free_principal(krbContext, krbPrinc);
203     }
204
205     GSSEAP_FREE(nameString);
206     return major;
207 }
208
209 static OM_uint32
210 importExportedName(OM_uint32 *minor,
211                    const gss_buffer_t nameBuffer,
212                    gss_name_t *pName)
213 {
214     OM_uint32 major, tmpMinor;
215     krb5_context krbContext;
216     unsigned char *p;
217     int composite = 0;
218     size_t len, remain;
219     gss_buffer_desc buf;
220     enum gss_eap_token_type tok_type;
221
222     GSSEAP_KRB_INIT(&krbContext);
223
224     p = (unsigned char *)nameBuffer->value;
225     remain = nameBuffer->length;
226
227     if (remain < 6 + GSS_EAP_MECHANISM->length + 4)
228         return GSS_S_BAD_NAME;
229
230     /* TOK_ID */
231     tok_type = load_uint16_be(p);
232     if (tok_type != TOK_TYPE_EXPORT_NAME &&
233         tok_type != TOK_TYPE_EXPORT_NAME_COMPOSITE)
234         return GSS_S_BAD_NAME;
235     p += 2;
236     remain -= 2;
237
238     /* MECH_OID_LEN */
239     len = load_uint16_be(p);
240     if (len != 2 + GSS_EAP_MECHANISM->length)
241         return GSS_S_BAD_NAME;
242     p += 2;
243     remain -= 2;
244
245     /* MECH_OID */
246     if (p[0] != 0x06)
247         return GSS_S_BAD_NAME;
248     if (p[1] != GSS_EAP_MECHANISM->length)
249         return GSS_S_BAD_MECH;
250     if (memcmp(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length))
251         return GSS_S_BAD_MECH;
252     p += 2 + GSS_EAP_MECHANISM->length;
253     remain -= 2 + GSS_EAP_MECHANISM->length;
254
255     /* NAME_LEN */
256     len = load_uint32_be(p);
257     p += 4;
258
259     if (remain < len)
260         return GSS_S_BAD_NAME;
261
262     /* NAME */
263     buf.length = len;
264     buf.value = p;
265
266     p += len;
267     remain -= len;
268
269     if (composite == 0 && remain != 0)
270         return GSS_S_BAD_NAME;
271
272     major = importUserName(minor, &buf, pName);
273     if (GSS_ERROR(major))
274         return major;
275
276     /* XXX TODO composite handling */
277
278     return GSS_S_COMPLETE;
279 }
280
281 OM_uint32 gssEapImportName(OM_uint32 *minor,
282                            const gss_buffer_t nameBuffer,
283                            gss_OID nameType,
284                            gss_name_t *name)
285 {
286     OM_uint32 major, tmpMinor;
287
288     *name = GSS_C_NO_NAME;
289
290     if (nameType == GSS_C_NULL_OID ||
291         oidEqual(nameType, GSS_C_NT_USER_NAME) ||
292         oidEqual(nameType, GSS_EAP_NT_PRINCIPAL_NAME))
293         major = importUserName(minor, nameBuffer, name);
294     else if (oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE) ||
295                oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE_X))
296         major = importServiceName(minor, nameBuffer, name);
297     else if (oidEqual(nameType, GSS_C_NT_EXPORT_NAME))
298         major = importExportedName(minor, nameBuffer, name);
299     else
300         major = GSS_S_BAD_NAMETYPE;
301
302     if (GSS_ERROR(major))
303         gssEapReleaseName(&tmpMinor, name);
304
305     return major;
306 }
307
308 OM_uint32 gssEapExportName(OM_uint32 *minor,
309                            const gss_name_t name,
310                            gss_buffer_t exportedName,
311                            int composite)
312 {
313     OM_uint32 major = GSS_S_FAILURE, tmpMinor;
314     krb5_context krbContext;
315     char *krbName = NULL;
316     size_t krbNameLen;
317     unsigned char *p;
318
319     exportedName->length = 0;
320     exportedName->value = NULL;
321
322     GSSEAP_KRB_INIT(&krbContext);
323     GSSEAP_MUTEX_LOCK(&name->mutex);
324
325     /*
326      * Don't export a composite name if we don't have any attributes.
327      */
328     if (composite &&
329         (name->flags & (NAME_FLAG_SAML | NAME_FLAG_RADIUS)) == 0) {
330         composite = 0;
331     }
332
333     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
334     if (*minor != 0)
335         goto cleanup;
336     krbNameLen = strlen(krbName);
337
338     exportedName->length = 6 + GSS_EAP_MECHANISM->length + 4 + krbNameLen;
339     if (composite) {
340         /* TODO: export SAML/AVP, this is pending specification */
341         GSSEAP_NOT_IMPLEMENTED;
342     }
343
344     exportedName->value = GSSEAP_MALLOC(exportedName->length);
345     if (exportedName->value == NULL) {
346         *minor = ENOMEM;
347         goto cleanup;
348     }
349
350     /* TOK | MECH_OID_LEN */
351     p = (unsigned char *)exportedName->value;
352     store_uint16_be(composite
353                         ? TOK_TYPE_EXPORT_NAME_COMPOSITE
354                         : TOK_TYPE_EXPORT_NAME,
355                     p);
356     p += 2;
357     store_uint16_be(GSS_EAP_MECHANISM->length + 2, p);
358     p += 2;
359
360     /* MECH_OID */
361     *p++ = 0x06;
362     *p++ = GSS_EAP_MECHANISM->length & 0xff;
363     memcpy(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length);
364     p += GSS_EAP_MECHANISM->length;
365
366     /* NAME_LEN */
367     store_uint32_be(krbNameLen, p);
368     p += 4;
369
370     /* NAME */
371     memcpy(p, krbName, krbNameLen);
372     p += krbNameLen;
373
374     *minor = 0;
375     major = GSS_S_COMPLETE;
376
377 cleanup:
378     GSSEAP_MUTEX_UNLOCK(&name->mutex);
379     if (GSS_ERROR(major))
380         gss_release_buffer(&tmpMinor, exportedName);
381     krb5_free_unparsed_name(krbContext, krbName);
382
383     return major;
384 }