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