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