f1655ccdd7fb621b138a0615a112cff8df0ed539
[mech_eap.orig] / 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 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     if (name->krbPrincipal->length == 1) {
136         name->flags |= NAME_FLAG_NAI;
137     } else {
138         name->flags |= NAME_FLAG_SERVICE;
139     }
140
141     *minor = 0;
142     return GSS_S_COMPLETE;
143 }
144
145 static OM_uint32
146 importServiceName(OM_uint32 *minor,
147                   const gss_buffer_t nameBuffer,
148                   gss_name_t *pName)
149 {
150     OM_uint32 major, tmpMinor;
151     krb5_context krbContext;
152     krb5_principal krbPrinc;
153     char *service, *host;
154
155     GSSEAP_KRB_INIT(&krbContext);
156
157     major = bufferToString(minor, nameBuffer, &service);
158     if (GSS_ERROR(major))
159         return major;
160
161     host = strchr(service, '@');
162     if (host != NULL) {
163         *host = '\0';
164         host++;
165     }    
166
167     /* XXX this is probably NOT what we want to be doing */
168     *minor = krb5_sname_to_principal(krbContext, host, service,
169                                      KRB5_NT_SRV_HST, &krbPrinc);
170     if (*minor != 0) {
171         GSSEAP_FREE(service);
172         return GSS_S_FAILURE;
173     }
174
175     major = krbPrincipalToName(minor, &krbPrinc, pName);
176     if (GSS_ERROR(major)) {
177         krb5_free_principal(krbContext, krbPrinc);
178     }
179
180     GSSEAP_FREE(service);
181     return major;
182 }
183
184 static OM_uint32
185 importUserName(OM_uint32 *minor,
186                const gss_buffer_t nameBuffer,
187                gss_name_t *pName)
188 {
189     OM_uint32 major, tmpMinor;
190     krb5_context krbContext;
191     krb5_principal krbPrinc;
192     char *nameString;
193
194     GSSEAP_KRB_INIT(&krbContext);
195
196     major = bufferToString(minor, nameBuffer, &nameString);
197     if (GSS_ERROR(major))
198         return major;
199
200     *minor = krb5_parse_name(krbContext, nameString, &krbPrinc);
201     if (*minor != 0) {
202         GSSEAP_FREE(nameString);
203         return GSS_S_FAILURE;
204     }
205
206     major = krbPrincipalToName(minor, &krbPrinc, pName);
207     if (GSS_ERROR(major)) {
208         krb5_free_principal(krbContext, krbPrinc);
209     }
210
211     GSSEAP_FREE(nameString);
212     return major;
213 }
214
215 static OM_uint32
216 importExportedName(OM_uint32 *minor,
217                    const gss_buffer_t nameBuffer,
218                    gss_name_t *pName)
219 {
220     OM_uint32 major, tmpMinor;
221     krb5_context krbContext;
222     unsigned char *p;
223     int composite = 0;
224     size_t len, remain;
225     gss_buffer_desc buf;
226     enum gss_eap_token_type tok_type;
227
228     GSSEAP_KRB_INIT(&krbContext);
229
230     p = (unsigned char *)nameBuffer->value;
231     remain = nameBuffer->length;
232
233     if (remain < 6 + GSS_EAP_MECHANISM->length + 4)
234         return GSS_S_BAD_NAME;
235
236     /* TOK_ID */
237     tok_type = load_uint16_be(p);
238     if (tok_type != TOK_TYPE_EXPORT_NAME &&
239         tok_type != TOK_TYPE_EXPORT_NAME_COMPOSITE)
240         return GSS_S_BAD_NAME;
241     p += 2;
242     remain -= 2;
243
244     /* MECH_OID_LEN */
245     len = load_uint16_be(p);
246     if (len != 2 + GSS_EAP_MECHANISM->length)
247         return GSS_S_BAD_NAME;
248     p += 2;
249     remain -= 2;
250
251     /* MECH_OID */
252     if (p[0] != 0x06)
253         return GSS_S_BAD_NAME;
254     if (p[1] != GSS_EAP_MECHANISM->length)
255         return GSS_S_BAD_MECH;
256     if (memcmp(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length))
257         return GSS_S_BAD_MECH;
258     p += 2 + GSS_EAP_MECHANISM->length;
259     remain -= 2 + GSS_EAP_MECHANISM->length;
260
261     /* NAME_LEN */
262     len = load_uint32_be(p);
263     p += 4;
264
265     if (remain < len)
266         return GSS_S_BAD_NAME;
267
268     /* NAME */
269     buf.length = len;
270     buf.value = p;
271
272     p += len;
273     remain -= len;
274
275     if (composite == 0 && remain != 0)
276         return GSS_S_BAD_NAME;
277
278     major = importUserName(minor, &buf, pName);
279     if (GSS_ERROR(major))
280         return major;
281
282     /* XXX TODO composite handling */
283
284     return GSS_S_COMPLETE;
285 }
286
287 OM_uint32 gssEapImportName(OM_uint32 *minor,
288                            const gss_buffer_t nameBuffer,
289                            gss_OID nameType,
290                            gss_name_t *name)
291 {
292     OM_uint32 major, tmpMinor;
293
294     *name = GSS_C_NO_NAME;
295
296     if (nameType == GSS_C_NULL_OID ||
297         oidEqual(nameType, GSS_C_NT_USER_NAME) ||
298         oidEqual(nameType, GSS_EAP_NT_PRINCIPAL_NAME))
299         major = importUserName(minor, nameBuffer, name);
300     else if (oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE) ||
301                oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE_X))
302         major = importServiceName(minor, nameBuffer, name);
303     else if (oidEqual(nameType, GSS_C_NT_EXPORT_NAME))
304         major = importExportedName(minor, nameBuffer, name);
305     else
306         major = GSS_S_BAD_NAMETYPE;
307
308     if (GSS_ERROR(major))
309         gssEapReleaseName(&tmpMinor, name);
310
311     return major;
312 }
313
314 OM_uint32 gssEapExportName(OM_uint32 *minor,
315                            const gss_name_t name,
316                            gss_buffer_t exportedName,
317                            int composite)
318 {
319     OM_uint32 major = GSS_S_FAILURE, tmpMinor;
320     krb5_context krbContext;
321     char *krbName = NULL;
322     size_t krbNameLen;
323     unsigned char *p;
324
325     exportedName->length = 0;
326     exportedName->value = NULL;
327
328     GSSEAP_KRB_INIT(&krbContext);
329     GSSEAP_MUTEX_LOCK(&name->mutex);
330
331     /*
332      * Don't export a composite name if we don't have any attributes.
333      */
334     if (composite && !NAME_HAS_ATTRIBUTES(name))
335         composite = 0;
336
337     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
338     if (*minor != 0)
339         goto cleanup;
340     krbNameLen = strlen(krbName);
341
342     exportedName->length = 6 + GSS_EAP_MECHANISM->length + 4 + krbNameLen;
343     if (composite) {
344         /* TODO: export SAML/AVP, this is pending specification */
345         GSSEAP_NOT_IMPLEMENTED;
346     }
347
348     exportedName->value = GSSEAP_MALLOC(exportedName->length);
349     if (exportedName->value == NULL) {
350         *minor = ENOMEM;
351         goto cleanup;
352     }
353
354     /* TOK | MECH_OID_LEN */
355     p = (unsigned char *)exportedName->value;
356     store_uint16_be(composite
357                         ? TOK_TYPE_EXPORT_NAME_COMPOSITE
358                         : TOK_TYPE_EXPORT_NAME,
359                     p);
360     p += 2;
361     store_uint16_be(GSS_EAP_MECHANISM->length + 2, p);
362     p += 2;
363
364     /* MECH_OID */
365     *p++ = 0x06;
366     *p++ = GSS_EAP_MECHANISM->length & 0xff;
367     memcpy(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length);
368     p += GSS_EAP_MECHANISM->length;
369
370     /* NAME_LEN */
371     store_uint32_be(krbNameLen, p);
372     p += 4;
373
374     /* NAME */
375     memcpy(p, krbName, krbNameLen);
376     p += krbNameLen;
377
378     *minor = 0;
379     major = GSS_S_COMPLETE;
380
381 cleanup:
382     GSSEAP_MUTEX_UNLOCK(&name->mutex);
383     if (GSS_ERROR(major))
384         gss_release_buffer(&tmpMinor, exportedName);
385     krb5_free_unparsed_name(krbContext, krbName);
386
387     return major;
388 }
389
390 static gss_buffer_desc attributePrefixes[] = {
391     {
392         /* ATTR_TYPE_NONE */
393         0,
394         NULL,
395     },
396     {
397         /* ATTR_TYPE_SAML_AAA_ASSERTION */
398         sizeof("urn:ietf:params:gss-eap:saml-aaa-assertion"),
399         "urn:ietf:params:gss-eap:saml-aaa-assertion"
400     },
401     {
402         /* ATTR_TYPE_SAML_ATTR */
403         sizeof("urn:ietf:params:gss-eap:saml-attr"),
404         "urn:ietf:params:gss-eap:saml-attr"
405     },
406     {
407         /* ATTR_TYPE_RADIUS_AVP */
408         sizeof("urn:ietf:params:gss-eap:radius-avp"),
409         "urn:ietf:params:gss-eap:radius-avp",
410     }
411 };
412
413 enum gss_eap_attribute_type
414 gssEapAttributePrefixToType(const gss_buffer_t prefix)
415 {
416     enum gss_eap_attribute_type i;
417
418     for (i = ATTR_TYPE_SAML_AAA_ASSERTION;
419          i < sizeof(attributePrefixes) / sizeof(attributePrefixes[0]);
420          i++)
421     {
422         gss_buffer_t p = &attributePrefixes[i];
423
424         if (p->length == prefix->length &&
425             memcmp(p->value, prefix->value, prefix->length) == 0) {
426             return i;
427         }
428     }
429
430     return ATTR_TYPE_NONE;
431 }
432
433 gss_buffer_t
434 gssEapAttributeTypeToPrefix(enum gss_eap_attribute_type type)
435 {
436     if (type <= ATTR_TYPE_NONE ||
437         type > ATTR_TYPE_RADIUS_AVP)
438         return GSS_C_NO_BUFFER;
439
440     return &attributePrefixes[type];
441 }
442
443 OM_uint32
444 decomposeAttributeName(OM_uint32 *minor,
445                        const gss_buffer_t attribute,
446                        gss_buffer_t prefix,
447                        gss_buffer_t suffix)
448 {
449     char *p = NULL;
450     int i;
451
452     for (i = 0; i < attribute->length; i++) {
453         if (((char *)attribute->value)[i] == ' ') {
454             p = (char *)attribute->value + i + 1;
455             break;
456         }
457     }
458
459     prefix->value = attribute->value;
460     prefix->length = i;
461
462     if (p != NULL && *p != '\0')  {
463         suffix->length = attribute->length - 1 - prefix->length;
464         suffix->value = p;
465     } else {
466         suffix->length = 0;
467         suffix->value = NULL;
468     }
469
470     *minor = 0;
471     return GSS_S_COMPLETE;
472 }
473
474 OM_uint32
475 composeAttributeName(OM_uint32 *minor,
476                        const gss_buffer_t prefix,
477                        const gss_buffer_t suffix,
478                        gss_buffer_t attribute)
479 {
480     size_t len = 0;
481     char *p;
482
483     attribute->length = 0;
484     attribute->value = NULL;
485
486     if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
487         return GSS_S_COMPLETE;
488
489     len = prefix->length;
490     if (suffix != NULL) {
491         len += 1 + suffix->length;
492     }
493
494     p = attribute->value = GSSEAP_MALLOC(len + 1);
495     if (attribute->value == NULL) {
496         *minor = ENOMEM;
497         return GSS_S_FAILURE;
498     }
499     attribute->length = len;
500
501     memcpy(p, prefix->value, prefix->length);
502     if (suffix != NULL) {
503         p[prefix->length] = ' ';
504         memcpy(p + prefix->length + 1, suffix->value, suffix->length);
505     }
506
507     p[attribute->length] = '\0';
508
509     *minor = 0;
510     return GSS_S_COMPLETE;
511 }