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