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