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