Refactor attribute context so it's not a subclass of provider
[mech_eap.orig] / 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     return NULL;
311 }
312
313 void
314 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
315                                         gss_any_t input) const
316 {
317 }
318
319 void
320 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
321 {
322     m_providers[ATTR_TYPE_RADIUS]->exportToBuffer(buffer);
323 }
324
325 bool
326 gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
327 {
328     unsigned int i;
329     bool ret;
330
331     ret = m_providers[ATTR_TYPE_RADIUS]->initFromBuffer(this, buffer);
332     if (!ret)
333         return false;
334
335     for (i = ATTR_TYPE_RADIUS + 1; i < ATTR_TYPE_MAX; i++) {
336         gss_eap_attr_provider *provider = m_providers[i];
337
338         ret = provider->initFromGssContext(this,
339                                            GSS_C_NO_CREDENTIAL,
340                                            GSS_C_NO_CONTEXT);
341         if (!ret)
342             break;
343     }
344
345     return ret;
346 }
347
348 /*
349  * C wrappers
350  */
351
352 static OM_uint32
353 mapException(OM_uint32 *minor, std::exception &e)
354 {
355     *minor = 0;
356     return GSS_S_FAILURE;
357 }
358
359 void
360 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
361                                          gss_buffer_t prefix,
362                                          gss_buffer_t suffix)
363 {
364     char *p = NULL;
365     size_t i;
366
367     for (i = 0; i < attribute->length; i++) {
368         if (((char *)attribute->value)[i] == ' ') {
369             p = (char *)attribute->value + i + 1;
370             break;
371         }
372     }
373
374     prefix->value = attribute->value;
375     prefix->length = i;
376
377     if (p != NULL && *p != '\0')  {
378         suffix->length = attribute->length - 1 - prefix->length;
379         suffix->value = p;
380     } else {
381         suffix->length = 0;
382         suffix->value = NULL;
383     }
384 }
385
386 std::string
387 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
388                                        const gss_buffer_t suffix)
389 {
390     std::string str;
391
392     if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
393         return str;
394
395     str.append((const char *)prefix->value, prefix->length);
396
397     if (suffix != GSS_C_NO_BUFFER) {
398         str.append(" ");
399         str.append((const char *)suffix->value, suffix->length);
400     }
401
402     return str;
403 }
404
405 std::string
406 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
407                                        const gss_buffer_t suffix)
408 {
409     const gss_buffer_t prefix = attributeTypeToPrefix(type);
410
411     return composeAttributeName(prefix, suffix);
412 }
413
414 void
415 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
416                                        const gss_buffer_t suffix,
417                                        gss_buffer_t attribute)
418 {
419     std::string str = composeAttributeName(prefix, suffix);
420
421     if (str.length() != 0) {
422         return duplicateBuffer(str, attribute);
423     } else {
424         attribute->length = 0;
425         attribute->value = NULL;
426     }
427 }
428
429 void
430 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
431                                          unsigned int *type,
432                                          gss_buffer_t suffix)
433 {
434     gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
435
436     decomposeAttributeName(attribute, &prefix, suffix);
437     *type = attributePrefixToType(&prefix);
438 }
439
440 void
441 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
442                                        const gss_buffer_t suffix,
443                                        gss_buffer_t attribute)
444 {
445     gss_buffer_t prefix = attributeTypeToPrefix(type);
446
447     return composeAttributeName(prefix, suffix, attribute);
448 }
449
450 OM_uint32
451 gssEapInquireName(OM_uint32 *minor,
452                   gss_name_t name,
453                   int *name_is_MN,
454                   gss_OID *MN_mech,
455                   gss_buffer_set_t *attrs)
456 {
457     if (name->attrCtx == NULL)
458         return GSS_S_UNAVAILABLE;
459
460     try {
461         if (!name->attrCtx->getAttributeTypes(attrs))
462             return GSS_S_UNAVAILABLE;
463     } catch (std::exception &e) {
464         return mapException(minor, e);
465     }
466
467     return GSS_S_COMPLETE;
468 }
469
470 OM_uint32
471 gssEapGetNameAttribute(OM_uint32 *minor,
472                        gss_name_t name,
473                        gss_buffer_t attr,
474                        int *authenticated,
475                        int *complete,
476                        gss_buffer_t value,
477                        gss_buffer_t display_value,
478                        int *more)
479 {
480     *authenticated = 0;
481     *complete = 0;
482
483     value->length = 0;
484     value->value = NULL;
485
486     if (display_value != NULL) {
487         display_value->length = 0;
488         display_value->value = NULL;
489     }
490
491     *more = -1;
492
493     if (name->attrCtx == NULL)
494         return GSS_S_UNAVAILABLE;
495
496     try {
497         if (!name->attrCtx->getAttribute(attr, authenticated, complete,
498                                          value, display_value, more))
499             return GSS_S_UNAVAILABLE;
500     } catch (std::exception &e) {
501         return mapException(minor, e);
502     }
503
504     return GSS_S_COMPLETE;
505 }
506
507 OM_uint32
508 gssEapDeleteNameAttribute(OM_uint32 *minor,
509                           gss_name_t name,
510                           gss_buffer_t attr)
511 {
512     if (name->attrCtx == NULL)
513         return GSS_S_UNAVAILABLE;
514
515     try {
516         name->attrCtx->deleteAttribute(attr);
517     } catch (std::exception &ex) {
518         return mapException(minor, ex);
519     }
520
521     return GSS_S_COMPLETE;
522 }
523
524 OM_uint32
525 gssEapSetNameAttribute(OM_uint32 *minor,
526                        gss_name_t name,
527                        int complete,
528                        gss_buffer_t attr,
529                        gss_buffer_t value)
530 {
531     if (name->attrCtx == NULL)
532         return GSS_S_UNAVAILABLE;
533
534     try {
535         name->attrCtx->setAttribute(complete, attr, value);
536     } catch (std::exception &ex) {
537         return mapException(minor, ex);
538     }
539
540     return GSS_S_COMPLETE;
541 }
542
543 OM_uint32
544 gssEapExportAttrContext(OM_uint32 *minor,
545                         gss_name_t name,
546                         gss_buffer_t buffer)
547 {
548     if (name->attrCtx == NULL) {
549         buffer->length = 0;
550         buffer->value = NULL;
551
552         return GSS_S_COMPLETE;
553     }
554
555     try {
556         name->attrCtx->exportToBuffer(buffer);
557         if (buffer->length == 0)
558             return GSS_S_FAILURE;
559     } catch (std::exception &e) {
560         return mapException(minor, e);
561     }
562
563     return GSS_S_COMPLETE;
564 }
565
566 OM_uint32
567 gssEapImportAttrContext(OM_uint32 *minor,
568                         gss_buffer_t buffer,
569                         gss_name_t name)
570 {
571     gss_eap_attr_ctx *ctx = NULL;
572
573     assert(name->attrCtx == NULL);
574
575     if (buffer->length != 0) {
576         try {
577             ctx = new gss_eap_attr_ctx();
578
579             if (!ctx->initFromBuffer(buffer)) {
580                 delete ctx;
581                 return GSS_S_DEFECTIVE_TOKEN;
582             }
583             name->attrCtx = ctx;
584         } catch (std::exception &e) {
585             delete ctx;
586             return mapException(minor, e);
587         }
588     }
589
590     return GSS_S_COMPLETE;
591 }
592
593 OM_uint32
594 gssEapDuplicateAttrContext(OM_uint32 *minor,
595                            gss_name_t in,
596                            gss_name_t out)
597 {
598     gss_eap_attr_ctx *ctx = NULL;
599
600     assert(out->attrCtx == NULL);
601
602     try {
603         if (in->attrCtx != NULL) {
604             ctx = new gss_eap_attr_ctx();
605             if (!ctx->initFromExistingContext(in->attrCtx)) {
606                 delete ctx;
607                 return GSS_S_FAILURE;
608             }
609             out->attrCtx = ctx;
610         }
611     } catch (std::exception &e) {
612         delete ctx;
613         return mapException(minor, e);
614     }
615
616     return GSS_S_COMPLETE;
617 }
618
619 OM_uint32
620 gssEapMapNameToAny(OM_uint32 *minor,
621                    gss_name_t name,
622                    int authenticated,
623                    gss_buffer_t type_id,
624                    gss_any_t *output)
625 {
626     try {
627         *output = name->attrCtx->mapToAny(authenticated, type_id);
628     } catch (std::exception &e) {
629         return mapException(minor, e);
630     }
631
632     return GSS_S_COMPLETE;
633 }
634
635 OM_uint32
636 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
637                             gss_name_t name,
638                             gss_buffer_t type_id,
639                             gss_any_t *input)
640 {
641     if (name->attrCtx == NULL)
642         return GSS_S_UNAVAILABLE;
643
644     try {
645         if (*input != NULL)
646             name->attrCtx->releaseAnyNameMapping(type_id, *input);
647         *input = NULL;
648     } catch (std::exception &e) {
649         return mapException(minor, e);
650     }
651
652     return GSS_S_COMPLETE;
653 }
654
655 OM_uint32
656 gssEapReleaseAttrContext(OM_uint32 *minor,
657                          gss_name_t name)
658 {
659     if (name->attrCtx != NULL)
660         delete name->attrCtx;
661
662     return GSS_S_COMPLETE;
663 }
664
665 OM_uint32
666 gssEapAttrProvidersInit(OM_uint32 *minor)
667 {
668     try {
669         if (gss_eap_radius_attr_provider::init()    &&
670             gss_eap_saml_assertion_provider::init() &&
671             gss_eap_saml_attr_provider::init()      &&
672             gss_eap_shib_attr_provider::init())
673             return GSS_S_COMPLETE;
674     } catch (std::exception &e) {
675         return mapException(minor, e);
676     }
677
678     return GSS_S_FAILURE;
679 }
680
681 OM_uint32
682 gssEapAttrProvidersFinalize(OM_uint32 *minor)
683 {
684     try {
685         gss_eap_shib_attr_provider::finalize();
686         gss_eap_saml_attr_provider::finalize();
687         gss_eap_saml_assertion_provider::finalize();
688         gss_eap_radius_attr_provider::finalize();
689     } catch (std::exception &e) {
690         return mapException(minor, e);
691     }
692
693     return GSS_S_COMPLETE;
694 }
695
696 struct gss_eap_attr_ctx *
697 gssEapCreateAttrContext(gss_cred_id_t gssCred,
698                         gss_ctx_id_t gssCtx)
699 {
700     gss_eap_attr_ctx *ctx;
701
702     assert(gssCtx != GSS_C_NO_CONTEXT);
703
704     ctx = new gss_eap_attr_ctx();
705     if (!ctx->initFromGssContext(gssCred, gssCtx)) {
706         delete ctx;
707         return NULL;
708     }
709
710     return ctx;
711 }