cleanup
[mech_eap.git] / 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         /* providers should fail in constructor */
81         assert(m_providers[i] != NULL);
82     }
83 }
84
85 unsigned int
86 gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
87 {
88     unsigned int i;
89
90     for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_LOCAL; i++) {
91         if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
92             return i;
93     }
94
95     return ATTR_TYPE_LOCAL;
96 }
97
98 const gss_buffer_t
99 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
100 {
101     if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
102         return GSS_C_NO_BUFFER;
103
104     return &gssEapAttrPrefixes[type];
105 }
106
107 /*
108  * Initialize a context from an existing context. All non-NULL providers
109  * in the existing context are duplicated, otherwise FALSE is returned.
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 = 0; 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 void
199 gss_eap_attr_ctx::setAttribute(int complete,
200                                const gss_buffer_t attr,
201                                const gss_buffer_t value)
202 {
203     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
204     unsigned int type;
205     gss_eap_attr_provider *provider;
206
207     decomposeAttributeName(attr, &type, &suffix);
208
209     provider = m_providers[type];
210     if (provider != NULL) {
211         provider->setAttribute(complete,
212                                (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
213                                value);
214                                
215     }
216 }
217
218 void
219 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
220 {
221     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
222     unsigned int type;
223     gss_eap_attr_provider *provider;
224
225     decomposeAttributeName(attr, &type, &suffix);
226
227     provider = m_providers[type];
228     if (provider != NULL) {
229         provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
230     }
231 }
232
233 bool
234 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
235 {
236     bool ret = false;
237     size_t i;
238
239     for (i = 0; i < ATTR_TYPE_MAX; i++) {
240         ret = m_providers[i]->getAttributeTypes(cb, data);
241         if (ret == false)
242             break;
243     }
244
245     return ret;
246 }
247
248 struct eap_gss_get_attr_types_args {
249     unsigned int type;
250     gss_buffer_set_t attrs;
251 };
252
253 static bool
254 addAttribute(const gss_eap_attr_provider *provider,
255              const gss_buffer_t attribute,
256              void *data)
257 {
258     eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
259     gss_buffer_t prefix = GSS_C_NO_BUFFER;
260     gss_buffer_desc qualified;
261     OM_uint32 major, minor;
262
263     if (args->type != ATTR_TYPE_LOCAL) {
264         gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
265         major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
266         gss_release_buffer(&minor, &qualified);
267     } else {
268         major = gss_add_buffer_set_member(&minor, prefix, &args->attrs);
269     }
270
271     return GSS_ERROR(major) ? false : true;
272 }
273
274 bool
275 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
276 {
277     eap_gss_get_attr_types_args args;
278     OM_uint32 major, minor;
279     bool ret = false;
280     unsigned int i;
281
282     major = gss_create_empty_buffer_set(&minor, attrs);
283     if (GSS_ERROR(major)) {
284         throw new std::bad_alloc;
285         return false;
286     }
287
288     args.attrs = *attrs;
289
290     for (i = 0; i < ATTR_TYPE_MAX; i++) {
291         args.type = i;
292
293         ret = m_providers[i]->getAttributeTypes(addAttribute, (void *)&args);
294         if (ret == false)
295             break;
296     }
297
298     if (ret == false)
299         gss_release_buffer_set(&minor, attrs);
300
301     return ret;
302 }
303
304 bool
305 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
306                                int *authenticated,
307                                int *complete,
308                                gss_buffer_t value,
309                                gss_buffer_t display_value,
310                                int *more) const
311 {
312     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
313     unsigned int type;
314     gss_eap_attr_provider *provider;
315     bool ret;
316
317     decomposeAttributeName(attr, &type, &suffix);
318
319     provider = m_providers[type];
320
321     ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
322                                  authenticated, complete,
323                                  value, display_value, more);
324
325     return ret;
326 }
327
328 gss_any_t
329 gss_eap_attr_ctx::mapToAny(int authenticated,
330                            gss_buffer_t type_id) const
331 {
332     unsigned int type;
333     gss_eap_attr_provider *provider;
334
335     type = attributePrefixToType(type_id);
336
337     provider = m_providers[type];
338
339     return provider->mapToAny(authenticated, type_id);
340 }
341
342 void
343 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
344                                         gss_any_t input) const
345 {
346     unsigned int type;
347     gss_eap_attr_provider *provider;
348
349     type = attributePrefixToType(type_id);
350
351     provider = m_providers[type];
352
353     provider->releaseAnyNameMapping(type_id, input);
354 }
355
356 gss_eap_attr_provider *
357 gss_eap_attr_ctx::getPrimaryProvider(void) const
358 {
359     return m_providers[ATTR_TYPE_RADIUS];
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     *minor = 0;
395     return GSS_S_FAILURE;
396 }
397
398 void
399 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
400                                          gss_buffer_t prefix,
401                                          gss_buffer_t suffix)
402 {
403     char *p = NULL;
404     size_t i;
405
406     for (i = 0; i < attribute->length; i++) {
407         if (((char *)attribute->value)[i] == ' ') {
408             p = (char *)attribute->value + i + 1;
409             break;
410         }
411     }
412
413     prefix->value = attribute->value;
414     prefix->length = i;
415
416     if (p != NULL && *p != '\0')  {
417         suffix->length = attribute->length - 1 - prefix->length;
418         suffix->value = p;
419     } else {
420         suffix->length = 0;
421         suffix->value = NULL;
422     }
423 }
424
425 std::string
426 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
427                                        const gss_buffer_t suffix)
428 {
429     std::string str;
430
431     if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
432         return str;
433
434     str.append((const char *)prefix->value, prefix->length);
435
436     if (suffix != GSS_C_NO_BUFFER) {
437         str.append(" ");
438         str.append((const char *)suffix->value, suffix->length);
439     }
440
441     return str;
442 }
443
444 std::string
445 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
446                                        const gss_buffer_t suffix)
447 {
448     const gss_buffer_t prefix = attributeTypeToPrefix(type);
449
450     return composeAttributeName(prefix, suffix);
451 }
452
453 void
454 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
455                                        const gss_buffer_t suffix,
456                                        gss_buffer_t attribute)
457 {
458     std::string str = composeAttributeName(prefix, suffix);
459
460     if (str.length() != 0) {
461         return duplicateBuffer(str, attribute);
462     } else {
463         attribute->length = 0;
464         attribute->value = NULL;
465     }
466 }
467
468 void
469 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
470                                          unsigned int *type,
471                                          gss_buffer_t suffix)
472 {
473     gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
474
475     decomposeAttributeName(attribute, &prefix, suffix);
476     *type = attributePrefixToType(&prefix);
477 }
478
479 void
480 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
481                                        const gss_buffer_t suffix,
482                                        gss_buffer_t attribute)
483 {
484     gss_buffer_t prefix = attributeTypeToPrefix(type);
485
486     return composeAttributeName(prefix, suffix, attribute);
487 }
488
489 OM_uint32
490 gssEapInquireName(OM_uint32 *minor,
491                   gss_name_t name,
492                   int *name_is_MN,
493                   gss_OID *MN_mech,
494                   gss_buffer_set_t *attrs)
495 {
496     if (name->attrCtx == NULL)
497         return GSS_S_UNAVAILABLE;
498
499     try {
500         if (!name->attrCtx->getAttributeTypes(attrs))
501             return GSS_S_UNAVAILABLE;
502     } catch (std::exception &e) {
503         return mapException(minor, e);
504     }
505
506     return GSS_S_COMPLETE;
507 }
508
509 OM_uint32
510 gssEapGetNameAttribute(OM_uint32 *minor,
511                        gss_name_t name,
512                        gss_buffer_t attr,
513                        int *authenticated,
514                        int *complete,
515                        gss_buffer_t value,
516                        gss_buffer_t display_value,
517                        int *more)
518 {
519     *authenticated = 0;
520     *complete = 0;
521
522     if (value != NULL) {
523         value->length = 0;
524         value->value = NULL;
525     }
526
527     if (display_value != NULL) {
528         display_value->length = 0;
529         display_value->value = NULL;
530     }
531
532     if (name->attrCtx == NULL)
533         return GSS_S_UNAVAILABLE;
534
535     try {
536         if (!name->attrCtx->getAttribute(attr, authenticated, complete,
537                                          value, display_value, more))
538             return GSS_S_UNAVAILABLE;
539     } catch (std::exception &e) {
540         return mapException(minor, e);
541     }
542
543     return GSS_S_COMPLETE;
544 }
545
546 OM_uint32
547 gssEapDeleteNameAttribute(OM_uint32 *minor,
548                           gss_name_t name,
549                           gss_buffer_t attr)
550 {
551     if (name->attrCtx == NULL)
552         return GSS_S_UNAVAILABLE;
553
554     try {
555         name->attrCtx->deleteAttribute(attr);
556     } catch (std::exception &ex) {
557         return mapException(minor, ex);
558     }
559
560     return GSS_S_COMPLETE;
561 }
562
563 OM_uint32
564 gssEapSetNameAttribute(OM_uint32 *minor,
565                        gss_name_t name,
566                        int complete,
567                        gss_buffer_t attr,
568                        gss_buffer_t value)
569 {
570     if (name->attrCtx == NULL)
571         return GSS_S_UNAVAILABLE;
572
573     try {
574         name->attrCtx->setAttribute(complete, attr, value);
575     } catch (std::exception &ex) {
576         return mapException(minor, ex);
577     }
578
579     return GSS_S_COMPLETE;
580 }
581
582 OM_uint32
583 gssEapExportAttrContext(OM_uint32 *minor,
584                         gss_name_t name,
585                         gss_buffer_t buffer)
586 {
587     if (name->attrCtx == NULL) {
588         buffer->length = 0;
589         buffer->value = NULL;
590
591         return GSS_S_COMPLETE;
592     }
593
594     try {
595         name->attrCtx->exportToBuffer(buffer);
596         if (buffer->length == 0)
597             return GSS_S_FAILURE;
598     } catch (std::exception &e) {
599         return mapException(minor, e);
600     }
601
602     return GSS_S_COMPLETE;
603 }
604
605 OM_uint32
606 gssEapImportAttrContext(OM_uint32 *minor,
607                         gss_buffer_t buffer,
608                         gss_name_t name)
609 {
610     gss_eap_attr_ctx *ctx = NULL;
611
612     assert(name->attrCtx == NULL);
613
614     if (buffer->length != 0) {
615         try {
616             ctx = new gss_eap_attr_ctx();
617
618             if (!ctx->initFromBuffer(buffer)) {
619                 delete ctx;
620                 return GSS_S_DEFECTIVE_TOKEN;
621             }
622             name->attrCtx = ctx;
623         } catch (std::exception &e) {
624             delete ctx;
625             return mapException(minor, e);
626         }
627     }
628
629     return GSS_S_COMPLETE;
630 }
631
632 OM_uint32
633 gssEapDuplicateAttrContext(OM_uint32 *minor,
634                            gss_name_t in,
635                            gss_name_t out)
636 {
637     gss_eap_attr_ctx *ctx = NULL;
638
639     assert(out->attrCtx == NULL);
640
641     try {
642         if (in->attrCtx != NULL) {
643             ctx = new gss_eap_attr_ctx();
644             if (!ctx->initFromExistingContext(in->attrCtx)) {
645                 delete ctx;
646                 return GSS_S_FAILURE;
647             }
648             out->attrCtx = ctx;
649         }
650     } catch (std::exception &e) {
651         delete ctx;
652         return mapException(minor, e);
653     }
654
655     return GSS_S_COMPLETE;
656 }
657
658 OM_uint32
659 gssEapMapNameToAny(OM_uint32 *minor,
660                    gss_name_t name,
661                    int authenticated,
662                    gss_buffer_t type_id,
663                    gss_any_t *output)
664 {
665     try {
666         *output = name->attrCtx->mapToAny(authenticated, type_id);
667     } catch (std::exception &e) {
668         return mapException(minor, e);
669     }
670
671     return GSS_S_COMPLETE;
672 }
673
674 OM_uint32
675 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
676                             gss_name_t name,
677                             gss_buffer_t type_id,
678                             gss_any_t *input)
679 {
680     if (name->attrCtx == NULL)
681         return GSS_S_UNAVAILABLE;
682
683     try {
684         if (*input != NULL)
685             name->attrCtx->releaseAnyNameMapping(type_id, *input);
686         *input = NULL;
687     } catch (std::exception &e) {
688         return mapException(minor, e);
689     }
690
691     return GSS_S_COMPLETE;
692 }
693
694 OM_uint32
695 gssEapReleaseAttrContext(OM_uint32 *minor,
696                          gss_name_t name)
697 {
698     if (name->attrCtx != NULL)
699         delete name->attrCtx;
700
701     return GSS_S_COMPLETE;
702 }
703
704 OM_uint32
705 gssEapAttrProvidersInit(OM_uint32 *minor)
706 {
707     try {
708         if (gss_eap_radius_attr_provider::init()    &&
709             gss_eap_saml_assertion_provider::init() &&
710             gss_eap_saml_attr_provider::init()      &&
711             gss_eap_shib_attr_provider::init())
712             return GSS_S_COMPLETE;
713     } catch (std::exception &e) {
714         return mapException(minor, e);
715     }
716
717     return GSS_S_FAILURE;
718 }
719
720 OM_uint32
721 gssEapAttrProvidersFinalize(OM_uint32 *minor)
722 {
723     try {
724         gss_eap_shib_attr_provider::finalize();
725         gss_eap_saml_attr_provider::finalize();
726         gss_eap_saml_assertion_provider::finalize();
727         gss_eap_radius_attr_provider::finalize();
728     } catch (std::exception &e) {
729         return mapException(minor, e);
730     }
731
732     return GSS_S_COMPLETE;
733 }
734
735 struct gss_eap_attr_ctx *
736 gssEapCreateAttrContext(gss_cred_id_t gssCred,
737                         gss_ctx_id_t gssCtx)
738 {
739     gss_eap_attr_ctx *ctx;
740
741     assert(gssCtx != GSS_C_NO_CONTEXT);
742
743     ctx = new gss_eap_attr_ctx();
744     if (!ctx->initFromGssContext(gssCred, gssCtx)) {
745         delete ctx;
746         return NULL;
747     }
748
749     gssCtx->expiryTime = ctx->getExpiryTime();
750
751     return ctx;
752 }