more cleanup/revert
[mech_eap.orig] / util_attr.cpp
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 #include <string>
36 #include <exception>
37 #include <new>
38
39 static gss_eap_attr_create_factory
40 gss_eap_attr_factories[ATTR_TYPE_MAX];
41
42 void
43 gss_eap_attr_ctx::registerProvider(unsigned int type,
44                                    gss_eap_attr_create_factory factory)
45 {
46     gss_eap_attr_factories[type] = factory;
47 }
48
49 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
50 {
51     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
52         gss_eap_attr_provider *provider;
53
54         provider = (gss_eap_attr_factories[i])();
55
56         m_providers[i] = provider;
57     }
58 }
59
60 bool
61 gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager,
62                                           const gss_eap_attr_provider *provider)
63 {
64     if (!gss_eap_attr_provider::initFromExistingContext(this, provider))
65         return false;
66
67     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
68         gss_eap_attr_provider *provider;
69
70         provider = m_providers[i];
71         if (provider != NULL) {
72             if (!provider->initFromExistingContext(this, provider))
73                 return false;
74         }
75     }
76
77     return true;
78 }
79
80 bool
81 gss_eap_attr_ctx::initFromGssContext(const gss_eap_attr_ctx *manager,
82                                      const gss_cred_id_t cred,
83                                      const gss_ctx_id_t ctx)
84 {
85     if (!gss_eap_attr_provider::initFromGssContext(this, cred, ctx))
86         return false;
87
88     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
89         gss_eap_attr_provider *provider;
90
91         provider = m_providers[i];
92         if (provider != NULL) {
93             if (!provider->initFromGssContext(this, cred, ctx))
94                 return false;
95         }
96     }
97
98     return true;
99 }
100
101 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
102 {
103     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++)
104         delete m_providers[i];
105 }
106
107 bool
108 gss_eap_attr_ctx::init(void)
109 {
110     return gss_eap_radius_attr_provider::init() &&
111            gss_eap_saml_assertion_provider::init() &&
112            gss_eap_saml_attr_provider::init() &&
113            gss_eap_shib_attr_provider::init();
114 }
115
116 void
117 gss_eap_attr_ctx::finalize(void)
118 {
119     gss_eap_shib_attr_provider::finalize();
120     gss_eap_saml_attr_provider::finalize();
121     gss_eap_saml_assertion_provider::finalize();
122     gss_eap_radius_attr_provider::finalize();
123 }
124
125 gss_eap_attr_provider *
126 gss_eap_attr_ctx::getProvider(unsigned int type) const
127 {
128     return m_providers[type];
129 }
130
131 gss_eap_attr_provider *
132 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
133 {
134     unsigned int type;
135
136     type = attributePrefixToType(prefix);
137
138     return m_providers[type];
139 }
140
141 void
142 gss_eap_attr_ctx::setAttribute(int complete,
143                                const gss_buffer_t attr,
144                                const gss_buffer_t value)
145 {
146     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
147     unsigned int type;
148     gss_eap_attr_provider *provider;
149
150     decomposeAttributeName(attr, &type, &suffix);
151
152     provider = m_providers[type];
153     if (provider != NULL) {
154         provider->setAttribute(complete,
155                                (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
156                                value);
157                                
158     }
159 }
160
161 void
162 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
163 {
164     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
165     unsigned int type;
166     gss_eap_attr_provider *provider;
167
168     decomposeAttributeName(attr, &type, &suffix);
169
170     provider = m_providers[type];
171     if (provider != NULL) {
172         provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
173     }
174 }
175
176 bool
177 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
178 {
179     bool ret = false;
180     size_t i;
181
182     for (i = 0; i < ATTR_TYPE_MAX; i++) {
183         gss_eap_attr_provider *provider;
184
185         provider = m_providers[i];
186         if (provider == NULL)
187             continue;
188
189         ret = provider->getAttributeTypes(cb, data);
190         if (ret == false)
191             break;
192     }
193
194     return ret;
195 }
196
197 struct eap_gss_get_attr_types_args {
198     unsigned int type;
199     gss_buffer_set_t attrs;
200 };
201
202 static bool
203 addAttribute(const gss_eap_attr_provider *provider,
204              const gss_buffer_t attribute,
205              void *data)
206 {
207     eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
208     gss_buffer_t prefix = GSS_C_NO_BUFFER;
209     gss_buffer_desc qualified;
210     OM_uint32 major, minor;
211
212     if (args->type != ATTR_TYPE_LOCAL) {
213         gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
214         major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
215         gss_release_buffer(&minor, &qualified);
216     } else {
217         major = gss_add_buffer_set_member(&minor, prefix, &args->attrs);
218     }
219
220     return GSS_ERROR(major) ? false : true;
221 }
222
223 bool
224 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
225 {
226     eap_gss_get_attr_types_args args;
227     OM_uint32 major, minor;
228     bool ret = false;
229     unsigned int i;
230
231     major = gss_create_empty_buffer_set(&minor, attrs);
232     if (GSS_ERROR(major)) {
233         throw new std::bad_alloc;
234         return false;
235     }
236
237     args.attrs = *attrs;
238
239     for (i = 0; i < ATTR_TYPE_MAX; i++) {
240         gss_eap_attr_provider *provider;
241
242         args.type = i;
243
244         provider = m_providers[i];
245         if (provider == NULL)
246             continue;
247
248         ret = provider->getAttributeTypes(addAttribute, (void *)&args);
249         if (ret == false)
250             break;
251     }
252
253     if (ret == false) {
254         gss_release_buffer_set(&minor, attrs);
255     }
256
257     return ret;
258 }
259
260 bool
261 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
262                                int *authenticated,
263                                int *complete,
264                                gss_buffer_t value,
265                                gss_buffer_t display_value,
266                                int *more) const
267 {
268     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
269     unsigned int type;
270     gss_eap_attr_provider *provider;
271     bool ret;
272
273     decomposeAttributeName(attr, &type, &suffix);
274
275     provider = m_providers[type];
276     if (provider == NULL) {
277         *more = 0;
278         return false;
279     }
280
281     ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
282                                  authenticated, complete,
283                                  value, display_value, more);
284
285     return ret;
286 }
287
288 gss_any_t
289 gss_eap_attr_ctx::mapToAny(int authenticated,
290                            gss_buffer_t type_id) const
291 {
292     return NULL;
293 }
294
295 void
296 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
297                                         gss_any_t input) const
298 {
299 }
300
301 void
302 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
303 {
304     m_providers[ATTR_TYPE_RADIUS]->exportToBuffer(buffer);
305 }
306
307 bool
308 gss_eap_attr_ctx::initFromBuffer(const gss_eap_attr_ctx *manager,
309                                  const gss_buffer_t buffer)
310 {
311     unsigned int i;
312     bool ret;
313
314     ret = m_providers[ATTR_TYPE_RADIUS]->initFromBuffer(this, buffer);
315     if (!ret)
316         return false;
317
318     for (i = ATTR_TYPE_RADIUS + 1; i < ATTR_TYPE_MAX; i++) {
319         gss_eap_attr_provider *provider = m_providers[i];
320
321         ret = provider->initFromGssContext(this,
322                                            GSS_C_NO_CREDENTIAL,
323                                            GSS_C_NO_CONTEXT);
324         if (!ret)
325             break;
326     }
327
328     return ret;
329 }
330
331
332 /*
333  * C wrappers
334  */
335
336 static OM_uint32
337 mapException(OM_uint32 *minor, std::exception &e)
338 {
339     *minor = 0;
340     return GSS_S_FAILURE;
341 }
342
343 static gss_buffer_desc attributePrefixes[] = {
344     {
345         /* ATTR_TYPE_RADIUS_AVP */
346         sizeof("urn:ietf:params:gss-eap:radius-avp"),
347         (void *)"urn:ietf:params:gss-eap:radius-avp",
348     },
349     {
350         /* ATTR_TYPE_SAML_AAA_ASSERTION */
351         sizeof("urn:ietf:params:gss-eap:saml-aaa-assertion"),
352         (void *)"urn:ietf:params:gss-eap:saml-aaa-assertion"
353     },
354     {
355         /* ATTR_TYPE_SAML_ATTR */
356         sizeof("urn:ietf:params:gss-eap:saml-attr"),
357         (void *)"urn:ietf:params:gss-eap:saml-attr"
358     },
359 };
360
361 unsigned int
362 gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
363 {
364     unsigned int i;
365
366     for (i = ATTR_TYPE_MIN;
367          i < sizeof(attributePrefixes) / sizeof(attributePrefixes[0]);
368          i++)
369     {
370         if (bufferEqual(&attributePrefixes[i], prefix))
371             return i;
372     }
373
374     return ATTR_TYPE_LOCAL;
375 }
376
377 const gss_buffer_t
378 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
379 {
380     if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
381         return GSS_C_NO_BUFFER;
382
383     return &attributePrefixes[type];
384 }
385
386 void
387 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
388                                          gss_buffer_t prefix,
389                                          gss_buffer_t suffix)
390 {
391     char *p = NULL;
392     size_t i;
393
394     for (i = 0; i < attribute->length; i++) {
395         if (((char *)attribute->value)[i] == ' ') {
396             p = (char *)attribute->value + i + 1;
397             break;
398         }
399     }
400
401     prefix->value = attribute->value;
402     prefix->length = i;
403
404     if (p != NULL && *p != '\0')  {
405         suffix->length = attribute->length - 1 - prefix->length;
406         suffix->value = p;
407     } else {
408         suffix->length = 0;
409         suffix->value = NULL;
410     }
411 }
412
413 std::string
414 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
415                                        const gss_buffer_t suffix)
416 {
417     std::string str;
418
419     if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
420         return str;
421
422     str.append((const char *)prefix->value, prefix->length);
423
424     if (suffix != GSS_C_NO_BUFFER) {
425         str.append(" ");
426         str.append((const char *)suffix->value, suffix->length);
427     }
428
429     return str;
430 }
431
432 std::string
433 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
434                                        const gss_buffer_t suffix)
435 {
436     const gss_buffer_t prefix = attributeTypeToPrefix(type);
437
438     return composeAttributeName(prefix, suffix);
439 }
440
441 void
442 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
443                                        const gss_buffer_t suffix,
444                                        gss_buffer_t attribute)
445 {
446     std::string str = composeAttributeName(prefix, suffix);
447
448     if (str.length() != 0) {
449         return duplicateBuffer(str, attribute);
450     } else {
451         attribute->length = 0;
452         attribute->value = NULL;
453     }
454 }
455
456 void
457 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
458                                          unsigned int *type,
459                                          gss_buffer_t suffix)
460 {
461     gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
462
463     decomposeAttributeName(attribute, &prefix, suffix);
464     *type = attributePrefixToType(&prefix);
465 }
466
467 void
468 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
469                                        const gss_buffer_t suffix,
470                                        gss_buffer_t attribute)
471 {
472     gss_buffer_t prefix = attributeTypeToPrefix(type);
473
474     return composeAttributeName(prefix, suffix, attribute);
475 }
476
477 OM_uint32
478 gssEapInquireName(OM_uint32 *minor,
479                   gss_name_t name,
480                   int *name_is_MN,
481                   gss_OID *MN_mech,
482                   gss_buffer_set_t *attrs)
483 {
484     if (name->attrCtx == NULL)
485         return GSS_S_UNAVAILABLE;
486
487     try {
488         if (!name->attrCtx->getAttributeTypes(attrs))
489             return GSS_S_UNAVAILABLE;
490     } catch (std::exception &e) {
491         return mapException(minor, e);
492     }
493
494     return GSS_S_COMPLETE;
495 }
496
497 OM_uint32
498 gssEapGetNameAttribute(OM_uint32 *minor,
499                        gss_name_t name,
500                        gss_buffer_t attr,
501                        int *authenticated,
502                        int *complete,
503                        gss_buffer_t value,
504                        gss_buffer_t display_value,
505                        int *more)
506 {
507     *authenticated = 0;
508     *complete = 0;
509
510     value->length = 0;
511     value->value = NULL;
512
513     if (display_value != NULL) {
514         display_value->length = 0;
515         display_value->value = NULL;
516     }
517
518     *more = -1;
519
520     if (name->attrCtx == NULL)
521         return GSS_S_UNAVAILABLE;
522
523     try {
524         if (!name->attrCtx->getAttribute(attr, authenticated, complete,
525                                          value, display_value, more))
526             return GSS_S_UNAVAILABLE;
527     } catch (std::exception &e) {
528         return mapException(minor, e);
529     }
530
531     return GSS_S_COMPLETE;
532 }
533
534 OM_uint32
535 gssEapDeleteNameAttribute(OM_uint32 *minor,
536                           gss_name_t name,
537                           gss_buffer_t attr)
538 {
539     if (name->attrCtx == NULL)
540         return GSS_S_UNAVAILABLE;
541
542     try {
543         name->attrCtx->deleteAttribute(attr);
544     } catch (std::exception &ex) {
545         return mapException(minor, ex);
546     }
547
548     return GSS_S_COMPLETE;
549 }
550
551 OM_uint32
552 gssEapSetNameAttribute(OM_uint32 *minor,
553                        gss_name_t name,
554                        int complete,
555                        gss_buffer_t attr,
556                        gss_buffer_t value)
557 {
558     if (name->attrCtx == NULL)
559         return GSS_S_UNAVAILABLE;
560
561     try {
562         name->attrCtx->setAttribute(complete, attr, value);
563     } catch (std::exception &ex) {
564         return mapException(minor, ex);
565     }
566
567     return GSS_S_COMPLETE;
568 }
569
570 OM_uint32
571 gssEapExportAttrContext(OM_uint32 *minor,
572                         gss_name_t name,
573                         gss_buffer_t buffer)
574 {
575     if (name->attrCtx == NULL) {
576         buffer->length = 0;
577         buffer->value = NULL;
578
579         return GSS_S_COMPLETE;
580     };
581
582     try {
583         name->attrCtx->exportToBuffer(buffer);
584     } catch (std::exception &e) {
585         return mapException(minor, e);
586     }
587
588     return GSS_S_COMPLETE;
589 }
590
591 OM_uint32
592 gssEapImportAttrContext(OM_uint32 *minor,
593                         gss_buffer_t buffer,
594                         gss_name_t name)
595 {
596     gss_eap_attr_ctx *ctx = NULL;
597
598     assert(name->attrCtx == NULL);
599
600     if (buffer->length != 0) {
601         try {
602             ctx = new gss_eap_attr_ctx;
603
604             if (!ctx->initFromBuffer(NULL, buffer)) {
605                 delete ctx;
606                 return GSS_S_DEFECTIVE_TOKEN;
607             }
608             name->attrCtx = ctx;
609         } catch (std::exception &e) {
610             delete ctx;
611             return mapException(minor, e);
612         }
613     }
614
615     return GSS_S_COMPLETE;
616 }
617
618 OM_uint32
619 gssEapDuplicateAttrContext(OM_uint32 *minor,
620                            gss_name_t in,
621                            gss_name_t out)
622 {
623     gss_eap_attr_ctx *ctx = NULL;
624
625     assert(out->attrCtx == NULL);
626
627     try {
628         if (in->attrCtx != NULL) {
629             if (!ctx->initFromExistingContext(NULL, in->attrCtx)) {
630                 delete ctx;
631                 return GSS_S_FAILURE;
632             }
633             out->attrCtx = ctx;
634         }
635     } catch (std::exception &e) {
636         delete ctx;
637         return mapException(minor, e);
638     }
639
640     return GSS_S_COMPLETE;
641 }
642
643 OM_uint32
644 gssEapMapNameToAny(OM_uint32 *minor,
645                    gss_name_t name,
646                    int authenticated,
647                    gss_buffer_t type_id,
648                    gss_any_t *output)
649 {
650     try {
651         *output = name->attrCtx->mapToAny(authenticated, type_id);
652     } catch (std::exception &e) {
653         return mapException(minor, e);
654     }
655
656     return GSS_S_COMPLETE;
657 }
658
659 OM_uint32
660 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
661                             gss_name_t name,
662                             gss_buffer_t type_id,
663                             gss_any_t *input)
664 {
665     if (name->attrCtx == NULL)
666         return GSS_S_UNAVAILABLE;
667
668     try {
669         if (*input != NULL)
670             name->attrCtx->releaseAnyNameMapping(type_id, *input);
671         *input = NULL;
672     } catch (std::exception &e) {
673         return mapException(minor, e);
674     }
675
676     return GSS_S_COMPLETE;
677 }
678
679 OM_uint32
680 gssEapReleaseAttrContext(OM_uint32 *minor,
681                          gss_name_t name)
682 {
683     if (name->attrCtx != NULL)
684         delete name->attrCtx;
685
686     return GSS_S_COMPLETE;
687 }
688
689 OM_uint32
690 gssEapAttrProvidersInit(OM_uint32 *minor)
691 {
692     try {
693         gss_eap_attr_ctx::init();
694     } catch (std::exception &e) {
695         return mapException(minor, e);
696     }
697
698     return GSS_S_COMPLETE;
699 }
700
701 OM_uint32
702 gssEapAttrProvidersFinalize(OM_uint32 *minor)
703 {
704     try {
705         gss_eap_attr_ctx::finalize();
706     } catch (std::exception &e) {
707         return mapException(minor, e);
708     }
709
710     return GSS_S_COMPLETE;
711 }
712
713 struct gss_eap_attr_ctx *
714 gssEapCreateAttrContext(gss_cred_id_t gssCred,
715                         gss_ctx_id_t gssCtx)
716 {
717     gss_eap_attr_ctx *ctx;
718
719     ctx = new gss_eap_attr_ctx;
720     if (!ctx->initFromGssContext(NULL, gssCred, gssCtx)) {
721         delete ctx;
722         return NULL;
723     }
724
725     return ctx;
726 }