cleanup
[moonshot.git] / mech_eap / 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_provider gssEapAttrFactories[ATTR_TYPE_MAX];
40 static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX];
41
42 void
43 gss_eap_attr_ctx::registerProvider(unsigned int type,
44                                    const char *prefix,
45                                    gss_eap_attr_create_provider factory)
46 {
47     assert(type < ATTR_TYPE_MAX);
48
49     assert(gssEapAttrFactories[type] == NULL);
50
51     gssEapAttrFactories[type] = factory;
52     if (prefix != NULL) {
53         gssEapAttrPrefixes[type].value = (void *)prefix;
54         gssEapAttrPrefixes[type].length = strlen(prefix);
55     } else {
56         gssEapAttrPrefixes[type].value = NULL;
57         gssEapAttrPrefixes[type].length = 0;
58     }
59 }
60
61 void
62 gss_eap_attr_ctx::unregisterProvider(unsigned int type)
63 {
64     assert(type < ATTR_TYPE_MAX);
65
66     gssEapAttrFactories[type] = NULL;
67     gssEapAttrPrefixes[type].value = NULL;
68     gssEapAttrPrefixes[type].length = 0;
69 }
70
71 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
72 {
73     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
74         gss_eap_attr_provider *provider;
75
76         provider = (gssEapAttrFactories[i])();
77
78         m_providers[i] = provider;
79     }
80 }
81
82 unsigned int
83 gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
84 {
85     unsigned int i;
86
87     for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_LOCAL; i++) {
88         if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
89             return i;
90     }
91
92     return ATTR_TYPE_LOCAL;
93 }
94
95 const gss_buffer_t
96 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
97 {
98     if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
99         return GSS_C_NO_BUFFER;
100
101     return &gssEapAttrPrefixes[type];
102 }
103
104 /*
105  * Initialize a context from an existing context. All non-NULL providers
106  * in the existing context are duplicated, otherwise FALSE is returned.
107  */
108 bool
109 gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
110 {
111     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
112         gss_eap_attr_provider *provider;
113
114         if (manager->m_providers[i] == NULL)
115             continue;
116
117         provider = m_providers[i];
118         if (!provider->initFromExistingContext(this, manager->m_providers[i]))
119             return false;
120     }
121
122     return true;
123 }
124
125 /*
126  * Initialize a context from a GSS credential and context.
127  */
128 bool
129 gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
130                                      const gss_ctx_id_t ctx)
131 {
132     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
133         gss_eap_attr_provider *provider = m_providers[i];
134
135         if (!provider->initFromGssContext(this, cred, ctx))
136             return false;
137     }
138
139     return true;
140 }
141
142 /*
143  * Initialize a context from an exported context or name token
144  */
145 bool
146 gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
147 {
148     unsigned int i;
149     gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
150
151     assert(primaryProvider != NULL);
152
153     if (!primaryProvider->initFromBuffer(this, buffer))
154         return false;
155
156     for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
157         gss_eap_attr_provider *provider = m_providers[i];
158
159         if (provider == primaryProvider)
160             continue;
161
162         if (!provider->initFromGssContext(this,
163                                           GSS_C_NO_CREDENTIAL,
164                                           GSS_C_NO_CONTEXT))
165             return false;
166     }
167
168     return true;
169 }
170
171 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
172 {
173     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++)
174         delete m_providers[i];
175 }
176
177 gss_eap_attr_provider *
178 gss_eap_attr_ctx::getProvider(unsigned int type) const
179 {
180     return m_providers[type];
181 }
182
183 gss_eap_attr_provider *
184 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
185 {
186     unsigned int type;
187
188     type = attributePrefixToType(prefix);
189
190     return m_providers[type];
191 }
192
193 void
194 gss_eap_attr_ctx::setAttribute(int complete,
195                                const gss_buffer_t attr,
196                                const gss_buffer_t value)
197 {
198     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
199     unsigned int type;
200     gss_eap_attr_provider *provider;
201
202     decomposeAttributeName(attr, &type, &suffix);
203
204     provider = m_providers[type];
205     if (provider != NULL) {
206         provider->setAttribute(complete,
207                                (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
208                                value);
209                                
210     }
211 }
212
213 void
214 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
215 {
216     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
217     unsigned int type;
218     gss_eap_attr_provider *provider;
219
220     decomposeAttributeName(attr, &type, &suffix);
221
222     provider = m_providers[type];
223     if (provider != NULL) {
224         provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
225     }
226 }
227
228 bool
229 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
230 {
231     bool ret = false;
232     size_t i;
233
234     for (i = 0; i < ATTR_TYPE_MAX; i++) {
235         gss_eap_attr_provider *provider;
236
237         provider = m_providers[i];
238         if (provider == NULL)
239             continue;
240
241         ret = provider->getAttributeTypes(cb, data);
242         if (ret == false)
243             break;
244     }
245
246     return ret;
247 }
248
249 struct eap_gss_get_attr_types_args {
250     unsigned int type;
251     gss_buffer_set_t attrs;
252 };
253
254 static bool
255 addAttribute(const gss_eap_attr_provider *provider,
256              const gss_buffer_t attribute,
257              void *data)
258 {
259     eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
260     gss_buffer_t prefix = GSS_C_NO_BUFFER;
261     gss_buffer_desc qualified;
262     OM_uint32 major, minor;
263
264     if (args->type != ATTR_TYPE_LOCAL) {
265         gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
266         major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
267         gss_release_buffer(&minor, &qualified);
268     } else {
269         major = gss_add_buffer_set_member(&minor, prefix, &args->attrs);
270     }
271
272     return GSS_ERROR(major) ? false : true;
273 }
274
275 bool
276 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
277 {
278     eap_gss_get_attr_types_args args;
279     OM_uint32 major, minor;
280     bool ret = false;
281     unsigned int i;
282
283     major = gss_create_empty_buffer_set(&minor, attrs);
284     if (GSS_ERROR(major)) {
285         throw new std::bad_alloc;
286         return false;
287     }
288
289     args.attrs = *attrs;
290
291     for (i = 0; i < ATTR_TYPE_MAX; i++) {
292         gss_eap_attr_provider *provider;
293
294         args.type = i;
295
296         provider = m_providers[i];
297         if (provider == NULL)
298             continue;
299
300         ret = provider->getAttributeTypes(addAttribute, (void *)&args);
301         if (ret == false)
302             break;
303     }
304
305     if (ret == false) {
306         gss_release_buffer_set(&minor, attrs);
307     }
308
309     return ret;
310 }
311
312 bool
313 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
314                                int *authenticated,
315                                int *complete,
316                                gss_buffer_t value,
317                                gss_buffer_t display_value,
318                                int *more) const
319 {
320     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
321     unsigned int type;
322     gss_eap_attr_provider *provider;
323     bool ret;
324
325     decomposeAttributeName(attr, &type, &suffix);
326
327     provider = m_providers[type];
328     if (provider == NULL) {
329         *more = 0;
330         return false;
331     }
332
333     ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
334                                  authenticated, complete,
335                                  value, display_value, more);
336
337     return ret;
338 }
339
340 gss_any_t
341 gss_eap_attr_ctx::mapToAny(int authenticated,
342                            gss_buffer_t type_id) const
343 {
344     unsigned int type;
345     gss_eap_attr_provider *provider;
346
347     type = attributePrefixToType(type_id);
348
349     provider = m_providers[type];
350
351     return provider->mapToAny(authenticated, type_id);
352 }
353
354 void
355 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
356                                         gss_any_t input) const
357 {
358     unsigned int type;
359     gss_eap_attr_provider *provider;
360
361     type = attributePrefixToType(type_id);
362
363     provider = m_providers[type];
364
365     provider->releaseAnyNameMapping(type_id, input);
366 }
367
368 gss_eap_attr_provider *
369 gss_eap_attr_ctx::getPrimaryProvider(void) const
370 {
371     return m_providers[ATTR_TYPE_RADIUS];
372 }
373
374 void
375 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
376 {
377     getPrimaryProvider()->exportToBuffer(buffer);
378 }
379
380 time_t
381 gss_eap_attr_ctx::getExpiryTime(void) const
382 {
383     unsigned int i;
384     time_t expiryTime = 0;
385
386     for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
387         time_t providerExpiryTime = m_providers[i]->getExpiryTime();
388
389         if (providerExpiryTime == 0)
390             continue;
391
392         if (expiryTime == 0 || providerExpiryTime < expiryTime)
393             expiryTime = providerExpiryTime;
394     }
395
396     return expiryTime;
397 }
398
399 /*
400  * C wrappers
401  */
402
403 static OM_uint32
404 mapException(OM_uint32 *minor, std::exception &e)
405 {
406     *minor = 0;
407     return GSS_S_FAILURE;
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         if (buffer->length == 0)
609             return GSS_S_FAILURE;
610     } catch (std::exception &e) {
611         return mapException(minor, e);
612     }
613
614     return GSS_S_COMPLETE;
615 }
616
617 OM_uint32
618 gssEapImportAttrContext(OM_uint32 *minor,
619                         gss_buffer_t buffer,
620                         gss_name_t name)
621 {
622     gss_eap_attr_ctx *ctx = NULL;
623
624     assert(name->attrCtx == NULL);
625
626     if (buffer->length != 0) {
627         try {
628             ctx = new gss_eap_attr_ctx();
629
630             if (!ctx->initFromBuffer(buffer)) {
631                 delete ctx;
632                 return GSS_S_DEFECTIVE_TOKEN;
633             }
634             name->attrCtx = ctx;
635         } catch (std::exception &e) {
636             delete ctx;
637             return mapException(minor, e);
638         }
639     }
640
641     return GSS_S_COMPLETE;
642 }
643
644 OM_uint32
645 gssEapDuplicateAttrContext(OM_uint32 *minor,
646                            gss_name_t in,
647                            gss_name_t out)
648 {
649     gss_eap_attr_ctx *ctx = NULL;
650
651     assert(out->attrCtx == NULL);
652
653     try {
654         if (in->attrCtx != NULL) {
655             ctx = new gss_eap_attr_ctx();
656             if (!ctx->initFromExistingContext(in->attrCtx)) {
657                 delete ctx;
658                 return GSS_S_FAILURE;
659             }
660             out->attrCtx = ctx;
661         }
662     } catch (std::exception &e) {
663         delete ctx;
664         return mapException(minor, e);
665     }
666
667     return GSS_S_COMPLETE;
668 }
669
670 OM_uint32
671 gssEapMapNameToAny(OM_uint32 *minor,
672                    gss_name_t name,
673                    int authenticated,
674                    gss_buffer_t type_id,
675                    gss_any_t *output)
676 {
677     try {
678         *output = name->attrCtx->mapToAny(authenticated, type_id);
679     } catch (std::exception &e) {
680         return mapException(minor, e);
681     }
682
683     return GSS_S_COMPLETE;
684 }
685
686 OM_uint32
687 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
688                             gss_name_t name,
689                             gss_buffer_t type_id,
690                             gss_any_t *input)
691 {
692     if (name->attrCtx == NULL)
693         return GSS_S_UNAVAILABLE;
694
695     try {
696         if (*input != NULL)
697             name->attrCtx->releaseAnyNameMapping(type_id, *input);
698         *input = NULL;
699     } catch (std::exception &e) {
700         return mapException(minor, e);
701     }
702
703     return GSS_S_COMPLETE;
704 }
705
706 OM_uint32
707 gssEapReleaseAttrContext(OM_uint32 *minor,
708                          gss_name_t name)
709 {
710     if (name->attrCtx != NULL)
711         delete name->attrCtx;
712
713     return GSS_S_COMPLETE;
714 }
715
716 OM_uint32
717 gssEapAttrProvidersInit(OM_uint32 *minor)
718 {
719     try {
720         if (gss_eap_radius_attr_provider::init()    &&
721             gss_eap_saml_assertion_provider::init() &&
722             gss_eap_saml_attr_provider::init()      &&
723             gss_eap_shib_attr_provider::init())
724             return GSS_S_COMPLETE;
725     } catch (std::exception &e) {
726         return mapException(minor, e);
727     }
728
729     return GSS_S_FAILURE;
730 }
731
732 OM_uint32
733 gssEapAttrProvidersFinalize(OM_uint32 *minor)
734 {
735     try {
736         gss_eap_shib_attr_provider::finalize();
737         gss_eap_saml_attr_provider::finalize();
738         gss_eap_saml_assertion_provider::finalize();
739         gss_eap_radius_attr_provider::finalize();
740     } catch (std::exception &e) {
741         return mapException(minor, e);
742     }
743
744     return GSS_S_COMPLETE;
745 }
746
747 struct gss_eap_attr_ctx *
748 gssEapCreateAttrContext(gss_cred_id_t gssCred,
749                         gss_ctx_id_t gssCtx)
750 {
751     gss_eap_attr_ctx *ctx;
752
753     assert(gssCtx != GSS_C_NO_CONTEXT);
754
755     ctx = new gss_eap_attr_ctx();
756     if (!ctx->initFromGssContext(gssCred, gssCtx)) {
757         delete ctx;
758         return NULL;
759     }
760
761     gssCtx->expiryTime = ctx->getExpiryTime();
762
763     return ctx;
764 }