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