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