insert missing call to gss_eap_attr_ctx constructor
[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         if (buffer->length == 0)
568             return GSS_S_FAILURE;
569     } catch (std::exception &e) {
570         return mapException(minor, e);
571     }
572
573     return GSS_S_COMPLETE;
574 }
575
576 OM_uint32
577 gssEapImportAttrContext(OM_uint32 *minor,
578                         gss_buffer_t buffer,
579                         gss_name_t name)
580 {
581     gss_eap_attr_ctx *ctx = NULL;
582
583     assert(name->attrCtx == NULL);
584
585     if (buffer->length != 0) {
586         try {
587             ctx = new gss_eap_attr_ctx();
588
589             if (!ctx->initFromBuffer(NULL, buffer)) {
590                 delete ctx;
591                 return GSS_S_DEFECTIVE_TOKEN;
592             }
593             name->attrCtx = ctx;
594         } catch (std::exception &e) {
595             delete ctx;
596             return mapException(minor, e);
597         }
598     }
599
600     return GSS_S_COMPLETE;
601 }
602
603 OM_uint32
604 gssEapDuplicateAttrContext(OM_uint32 *minor,
605                            gss_name_t in,
606                            gss_name_t out)
607 {
608     gss_eap_attr_ctx *ctx = NULL;
609
610     assert(out->attrCtx == NULL);
611
612     try {
613         if (in->attrCtx != NULL) {
614             ctx = new gss_eap_attr_ctx();
615             if (!ctx->initFromExistingContext(NULL, in->attrCtx)) {
616                 delete ctx;
617                 return GSS_S_FAILURE;
618             }
619             out->attrCtx = ctx;
620         }
621     } catch (std::exception &e) {
622         delete ctx;
623         return mapException(minor, e);
624     }
625
626     return GSS_S_COMPLETE;
627 }
628
629 OM_uint32
630 gssEapMapNameToAny(OM_uint32 *minor,
631                    gss_name_t name,
632                    int authenticated,
633                    gss_buffer_t type_id,
634                    gss_any_t *output)
635 {
636     try {
637         *output = name->attrCtx->mapToAny(authenticated, type_id);
638     } catch (std::exception &e) {
639         return mapException(minor, e);
640     }
641
642     return GSS_S_COMPLETE;
643 }
644
645 OM_uint32
646 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
647                             gss_name_t name,
648                             gss_buffer_t type_id,
649                             gss_any_t *input)
650 {
651     if (name->attrCtx == NULL)
652         return GSS_S_UNAVAILABLE;
653
654     try {
655         if (*input != NULL)
656             name->attrCtx->releaseAnyNameMapping(type_id, *input);
657         *input = NULL;
658     } catch (std::exception &e) {
659         return mapException(minor, e);
660     }
661
662     return GSS_S_COMPLETE;
663 }
664
665 OM_uint32
666 gssEapReleaseAttrContext(OM_uint32 *minor,
667                          gss_name_t name)
668 {
669     if (name->attrCtx != NULL)
670         delete name->attrCtx;
671
672     return GSS_S_COMPLETE;
673 }
674
675 OM_uint32
676 gssEapAttrProvidersInit(OM_uint32 *minor)
677 {
678     try {
679         if (gss_eap_radius_attr_provider::init()    &&
680             gss_eap_saml_assertion_provider::init() &&
681             gss_eap_saml_attr_provider::init()      &&
682             gss_eap_shib_attr_provider::init())
683             return GSS_S_COMPLETE;
684     } catch (std::exception &e) {
685         return mapException(minor, e);
686     }
687
688     return GSS_S_FAILURE;
689 }
690
691 OM_uint32
692 gssEapAttrProvidersFinalize(OM_uint32 *minor)
693 {
694     try {
695         gss_eap_shib_attr_provider::finalize();
696         gss_eap_saml_attr_provider::finalize();
697         gss_eap_saml_assertion_provider::finalize();
698         gss_eap_radius_attr_provider::finalize();
699     } catch (std::exception &e) {
700         return mapException(minor, e);
701     }
702
703     return GSS_S_COMPLETE;
704 }
705
706 struct gss_eap_attr_ctx *
707 gssEapCreateAttrContext(gss_cred_id_t gssCred,
708                         gss_ctx_id_t gssCtx)
709 {
710     gss_eap_attr_ctx *ctx;
711
712     ctx = new gss_eap_attr_ctx();
713     if (!ctx->initFromGssContext(NULL, gssCred, gssCtx)) {
714         delete ctx;
715         return NULL;
716     }
717
718     return ctx;
719 }