Better error reporting through com_err
[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 <typeinfo>
36 #include <string>
37 #include <exception>
38 #include <new>
39
40 /* lazy initialisation */
41 static GSSEAP_THREAD_ONCE gssEapAttrProvidersInitOnce = GSSEAP_ONCE_INITIALIZER;
42 static OM_uint32 gssEapAttrProvidersInitStatus = GSS_S_UNAVAILABLE;
43
44 static void
45 gssEapAttrProvidersInitInternal(void)
46 {
47     OM_uint32 major, minor;
48
49     assert(gssEapAttrProvidersInitStatus == GSS_S_UNAVAILABLE);
50
51     major = gssEapRadiusAttrProviderInit(&minor);
52     if (major == GSS_S_COMPLETE)
53         major = gssEapSamlAttrProvidersInit(&minor);
54     if (major == GSS_S_COMPLETE)
55         major = gssEapLocalAttrProviderInit(&minor);
56
57 #ifdef GSSEAP_DEBUG
58     assert(major == GSS_S_COMPLETE);
59 #endif
60
61     gssEapAttrProvidersInitStatus = major;
62 }
63
64 static OM_uint32
65 gssEapAttrProvidersInit(OM_uint32 *minor)
66 {
67     GSSEAP_ONCE(&gssEapAttrProvidersInitOnce, gssEapAttrProvidersInitInternal);
68
69     if (GSS_ERROR(gssEapAttrProvidersInitStatus))
70         *minor = GSSEAP_NO_ATTR_PROVIDERS;
71
72     return gssEapAttrProvidersInitStatus;
73 }
74
75 OM_uint32
76 gssEapAttrProvidersFinalize(OM_uint32 *minor)
77 {
78     OM_uint32 major = GSS_S_COMPLETE;
79
80     if (gssEapAttrProvidersInitStatus == GSS_S_COMPLETE) {
81         major = gssEapLocalAttrProviderFinalize(minor);
82         if (major == GSS_S_COMPLETE)
83             major = gssEapSamlAttrProvidersFinalize(minor);
84         if (major == GSS_S_COMPLETE)
85             major = gssEapRadiusAttrProviderFinalize(minor);
86
87         gssEapAttrProvidersInitStatus = GSS_S_UNAVAILABLE;
88     }
89
90     return major;
91 }
92
93 static gss_eap_attr_create_provider gssEapAttrFactories[ATTR_TYPE_MAX + 1];
94 static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX + 1];
95
96 /*
97  * Register a provider for a particular type and prefix
98  */
99 void
100 gss_eap_attr_ctx::registerProvider(unsigned int type,
101                                    const char *prefix,
102                                    gss_eap_attr_create_provider factory)
103 {
104     assert(type <= ATTR_TYPE_MAX);
105
106     assert(gssEapAttrFactories[type] == NULL);
107
108     gssEapAttrFactories[type] = factory;
109     if (prefix != NULL) {
110         gssEapAttrPrefixes[type].value = (void *)prefix;
111         gssEapAttrPrefixes[type].length = strlen(prefix);
112     } else {
113         gssEapAttrPrefixes[type].value = NULL;
114         gssEapAttrPrefixes[type].length = 0;
115     }
116 }
117
118 /*
119  * Unregister a provider
120  */
121 void
122 gss_eap_attr_ctx::unregisterProvider(unsigned int type)
123 {
124     assert(type <= ATTR_TYPE_MAX);
125
126     gssEapAttrFactories[type] = NULL;
127     gssEapAttrPrefixes[type].value = NULL;
128     gssEapAttrPrefixes[type].length = 0;
129 }
130
131 /*
132  * Create an attribute context, that manages instances of providers
133  */
134 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
135 {
136     m_flags = 0;
137
138     for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
139         gss_eap_attr_provider *provider;
140
141         if (gssEapAttrFactories[i] != NULL) {
142             provider = (gssEapAttrFactories[i])();
143         } else {
144             provider = NULL;
145         }
146
147         m_providers[i] = provider;
148     }
149 }
150
151 /*
152  * Convert an attribute prefix to a type
153  */
154 unsigned int
155 gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
156 {
157     unsigned int i;
158
159     for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
160         if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
161             return i;
162     }
163
164     return ATTR_TYPE_LOCAL;
165 }
166
167 /*
168  * Convert a type to an attribute prefix
169  */
170 const gss_buffer_t
171 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
172 {
173     if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_MAX)
174         return GSS_C_NO_BUFFER;
175
176     return &gssEapAttrPrefixes[type];
177 }
178
179 bool
180 gss_eap_attr_ctx::providerEnabled(unsigned int type) const
181 {
182     if (type == ATTR_TYPE_LOCAL &&
183         (m_flags & ATTR_FLAG_DISABLE_LOCAL))
184         return false;
185
186     if (m_providers[type] == NULL)
187         return false;
188
189     return true;
190 }
191
192 void
193 gss_eap_attr_ctx::releaseProvider(unsigned int type)
194 {
195     delete m_providers[type];
196     m_providers[type] = NULL;
197 }
198
199 /*
200  * Initialize a context from an existing context.
201  */
202 bool
203 gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
204 {
205     bool ret = true;
206
207     m_flags = manager->m_flags;
208
209     for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
210         gss_eap_attr_provider *provider;
211
212         if (!providerEnabled(i)) {
213             releaseProvider(i);
214             continue;
215         }
216
217         provider = m_providers[i];
218
219         ret = provider->initFromExistingContext(this,
220                                                 manager->m_providers[i]);
221         if (ret == false) {
222             releaseProvider(i);
223             break;
224         }
225     }
226
227     return ret;
228 }
229
230 /*
231  * Initialize a context from a GSS credential and context.
232  */
233 bool
234 gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
235                                      const gss_ctx_id_t ctx)
236 {
237     bool ret = true;
238
239     if (cred != GSS_C_NO_CREDENTIAL &&
240         (cred->flags & GSS_EAP_DISABLE_LOCAL_ATTRS_FLAG)) {
241         m_flags |= ATTR_FLAG_DISABLE_LOCAL;
242     }
243
244     for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
245         gss_eap_attr_provider *provider;
246
247         if (!providerEnabled(i)) {
248             releaseProvider(i);
249             continue;
250         }
251
252         provider = m_providers[i];
253
254         ret = provider->initFromGssContext(this, cred, ctx);
255         if (ret == false) {
256             releaseProvider(i);
257             break;
258         }
259     }
260
261     return ret;
262 }
263
264 /*
265  * Initialize a context from an exported context or name token
266  */
267 bool
268 gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
269 {
270     bool ret;
271     gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
272     gss_buffer_desc primaryBuf;
273
274     if (buffer->length < 4)
275         return false;
276
277     m_flags = load_uint32_be(buffer->value);
278
279     primaryBuf.length = buffer->length - 4;
280     primaryBuf.value = (char *)buffer->value + 4;
281
282     ret = primaryProvider->initFromBuffer(this, &primaryBuf);
283     if (ret == false)
284         return ret;
285
286     for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
287         gss_eap_attr_provider *provider;
288
289         if (!providerEnabled(i)) {
290             releaseProvider(i);
291             continue;
292         }
293
294         provider = m_providers[i];
295         if (provider == primaryProvider)
296             continue;
297
298         ret = provider->initFromGssContext(this,
299                                            GSS_C_NO_CREDENTIAL,
300                                            GSS_C_NO_CONTEXT);
301         if (ret == false) {
302             releaseProvider(i);
303             break;
304         }
305     }
306
307     return ret;
308 }
309
310 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
311 {
312     for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++)
313         delete m_providers[i];
314 }
315
316 /*
317  * Locate provider for a given type
318  */
319 gss_eap_attr_provider *
320 gss_eap_attr_ctx::getProvider(unsigned int type) const
321 {
322     assert(type >= ATTR_TYPE_MIN && type <= ATTR_TYPE_MAX);
323     return m_providers[type];
324 }
325
326 /*
327  * Locate provider for a given prefix
328  */
329 gss_eap_attr_provider *
330 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
331 {
332     unsigned int type;
333
334     type = attributePrefixToType(prefix);
335
336     return m_providers[type];
337 }
338
339 /*
340  * Get primary provider. Only the primary provider is serialised when
341  * gss_export_sec_context() or gss_export_name_composite() is called.
342  */
343 gss_eap_attr_provider *
344 gss_eap_attr_ctx::getPrimaryProvider(void) const
345 {
346     return m_providers[ATTR_TYPE_MIN];
347 }
348
349 /*
350  * Set an attribute
351  */
352 void
353 gss_eap_attr_ctx::setAttribute(int complete,
354                                const gss_buffer_t attr,
355                                const gss_buffer_t value)
356 {
357     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
358     unsigned int type;
359     gss_eap_attr_provider *provider;
360
361     decomposeAttributeName(attr, &type, &suffix);
362
363     provider = m_providers[type];
364     if (provider != NULL) {
365         provider->setAttribute(complete,
366                                (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
367                                value);
368     } else {
369         /* XXX TODO throw exception */
370     }
371 }
372
373 /*
374  * Delete an attrbiute
375  */
376 void
377 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
378 {
379     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
380     unsigned int type;
381     gss_eap_attr_provider *provider;
382
383     decomposeAttributeName(attr, &type, &suffix);
384
385     provider = m_providers[type];
386     if (provider != NULL)
387         provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
388 }
389
390 /*
391  * Enumerate attribute types with callback
392  */
393 bool
394 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
395 {
396     bool ret = false;
397     size_t i;
398
399     for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
400         gss_eap_attr_provider *provider = m_providers[i];
401
402         if (provider == NULL)
403             continue;
404
405         ret = provider->getAttributeTypes(cb, data);
406         if (ret == false)
407             break;
408     }
409
410     return ret;
411 }
412
413 struct eap_gss_get_attr_types_args {
414     unsigned int type;
415     gss_buffer_set_t attrs;
416 };
417
418 static bool
419 addAttribute(const gss_eap_attr_provider *provider,
420              const gss_buffer_t attribute,
421              void *data)
422 {
423     eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
424     gss_buffer_desc qualified;
425     OM_uint32 major, minor;
426
427     if (args->type != ATTR_TYPE_LOCAL) {
428         gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
429         major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
430         gss_release_buffer(&minor, &qualified);
431     } else {
432         major = gss_add_buffer_set_member(&minor, attribute, &args->attrs);
433     }
434
435     return GSS_ERROR(major) == false;
436 }
437
438 /*
439  * Enumerate attribute types, output is buffer set
440  */
441 bool
442 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
443 {
444     eap_gss_get_attr_types_args args;
445     OM_uint32 major, minor;
446     bool ret = false;
447     unsigned int i;
448
449     major = gss_create_empty_buffer_set(&minor, attrs);
450     if (GSS_ERROR(major)) {
451         throw new std::bad_alloc;
452         return false;
453     }
454
455     args.attrs = *attrs;
456
457     for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
458         gss_eap_attr_provider *provider = m_providers[i];
459
460         args.type = i;
461
462         if (provider == NULL)
463             continue;
464
465         ret = provider->getAttributeTypes(addAttribute, (void *)&args);
466         if (ret == false)
467             break;
468     }
469
470     if (ret == false)
471         gss_release_buffer_set(&minor, attrs);
472
473     return ret;
474 }
475
476 /*
477  * Get attribute with given name
478  */
479 bool
480 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
481                                int *authenticated,
482                                int *complete,
483                                gss_buffer_t value,
484                                gss_buffer_t display_value,
485                                int *more) const
486 {
487     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
488     unsigned int type;
489     gss_eap_attr_provider *provider;
490     bool ret;
491
492     decomposeAttributeName(attr, &type, &suffix);
493
494     provider = m_providers[type];
495     if (provider == NULL)
496         return false;
497
498     ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
499                                  authenticated, complete,
500                                  value, display_value, more);
501
502     return ret;
503 }
504
505 /*
506  * Map attribute context to C++ object
507  */
508 gss_any_t
509 gss_eap_attr_ctx::mapToAny(int authenticated,
510                            gss_buffer_t type_id) const
511 {
512     unsigned int type;
513     gss_eap_attr_provider *provider;
514     gss_buffer_desc suffix;
515
516     decomposeAttributeName(type_id, &type, &suffix);
517
518     provider = m_providers[type];
519     if (provider == NULL)
520         return (gss_any_t)NULL;
521
522     return provider->mapToAny(authenticated, &suffix);
523 }
524
525 /*
526  * Release mapped context
527  */
528 void
529 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
530                                         gss_any_t input) const
531 {
532     unsigned int type;
533     gss_eap_attr_provider *provider;
534     gss_buffer_desc suffix;
535
536     decomposeAttributeName(type_id, &type, &suffix);
537
538     provider = m_providers[type];
539     if (provider != NULL)
540         provider->releaseAnyNameMapping(&suffix, input);
541 }
542
543 /*
544  * Export attribute context to buffer
545  */
546 void
547 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
548 {
549     const gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
550     gss_buffer_desc tmp;
551     unsigned char *p;
552     OM_uint32 tmpMinor;
553
554     primaryProvider->exportToBuffer(&tmp);
555
556     buffer->length = 4 + tmp.length;
557     buffer->value = GSSEAP_MALLOC(buffer->length);
558     if (buffer->value == NULL)
559         throw new std::bad_alloc;
560
561     p = (unsigned char *)buffer->value;
562     store_uint32_be(m_flags, p);
563     memcpy(p + 4, tmp.value, tmp.length);
564
565     gss_release_buffer(&tmpMinor, &tmp);
566 }
567
568 /*
569  * Return soonest expiry time of providers
570  */
571 time_t
572 gss_eap_attr_ctx::getExpiryTime(void) const
573 {
574     unsigned int i;
575     time_t expiryTime = 0;
576
577     for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
578         gss_eap_attr_provider *provider = m_providers[i];
579         time_t providerExpiryTime;
580
581         if (provider == NULL)
582             continue;
583
584         providerExpiryTime = provider->getExpiryTime();
585         if (providerExpiryTime == 0)
586             continue;
587
588         if (expiryTime == 0 || providerExpiryTime < expiryTime)
589             expiryTime = providerExpiryTime;
590     }
591
592     return expiryTime;
593 }
594
595 /*
596  * Map C++ exception to GSS status
597  */
598 static OM_uint32
599 mapException(OM_uint32 *minor, std::exception &e)
600 {
601     OM_uint32 major = GSS_S_FAILURE;
602
603     /* XXX TODO implement other mappings */
604     if (typeid(e) == typeid(std::bad_alloc))
605         *minor = ENOMEM;
606     else
607         *minor = 0;
608
609 #ifdef GSSEAP_DEBUG
610     /* rethrow for now for debugging */
611     throw e;
612 #endif
613
614     return major;
615 }
616
617 /*
618  * Decompose attribute name into prefix and suffix
619  */
620 void
621 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
622                                          gss_buffer_t prefix,
623                                          gss_buffer_t suffix)
624 {
625     char *p = NULL;
626     size_t i;
627
628     for (i = 0; i < attribute->length; i++) {
629         if (((char *)attribute->value)[i] == ' ') {
630             p = (char *)attribute->value + i + 1;
631             break;
632         }
633     }
634
635     prefix->value = attribute->value;
636     prefix->length = i;
637
638     if (p != NULL && *p != '\0')  {
639         suffix->length = attribute->length - 1 - prefix->length;
640         suffix->value = p;
641     } else {
642         suffix->length = 0;
643         suffix->value = NULL;
644     }
645 }
646
647 /*
648  * Decompose attribute name into type and suffix
649  */
650 void
651 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
652                                          unsigned int *type,
653                                          gss_buffer_t suffix)
654 {
655     gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
656
657     decomposeAttributeName(attribute, &prefix, suffix);
658     *type = attributePrefixToType(&prefix);
659 }
660
661 /*
662  * Compose attribute name from prefix, suffix; returns C++ string
663  */
664 std::string
665 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
666                                        const gss_buffer_t suffix)
667 {
668     std::string str;
669
670     if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
671         return str;
672
673     str.append((const char *)prefix->value, prefix->length);
674
675     if (suffix != GSS_C_NO_BUFFER) {
676         str.append(" ");
677         str.append((const char *)suffix->value, suffix->length);
678     }
679
680     return str;
681 }
682
683 /*
684  * Compose attribute name from type, suffix; returns C++ string
685  */
686 std::string
687 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
688                                        const gss_buffer_t suffix)
689 {
690     const gss_buffer_t prefix = attributeTypeToPrefix(type);
691
692     return composeAttributeName(prefix, suffix);
693 }
694
695 /*
696  * Compose attribute name from prefix, suffix; returns GSS buffer
697  */
698 void
699 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
700                                        const gss_buffer_t suffix,
701                                        gss_buffer_t attribute)
702 {
703     std::string str = composeAttributeName(prefix, suffix);
704
705     if (str.length() != 0) {
706         return duplicateBuffer(str, attribute);
707     } else {
708         attribute->length = 0;
709         attribute->value = NULL;
710     }
711 }
712
713 /*
714  * Compose attribute name from type, suffix; returns GSS buffer
715  */
716 void
717 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
718                                        const gss_buffer_t suffix,
719                                        gss_buffer_t attribute)
720 {
721     gss_buffer_t prefix = attributeTypeToPrefix(type);
722
723     return composeAttributeName(prefix, suffix, attribute);
724 }
725
726 /*
727  * C wrappers
728  */
729 OM_uint32
730 gssEapInquireName(OM_uint32 *minor,
731                   gss_name_t name,
732                   int *name_is_MN,
733                   gss_OID *MN_mech,
734                   gss_buffer_set_t *attrs)
735 {
736     if (name->attrCtx == NULL) {
737         *minor = GSSEAP_NO_ATTR_CONTEXT;
738         return GSS_S_UNAVAILABLE;
739     }
740
741     if (GSS_ERROR(gssEapAttrProvidersInit(minor))) {
742         return GSS_S_UNAVAILABLE;
743     }
744
745     try {
746         if (!name->attrCtx->getAttributeTypes(attrs)) {
747             *minor = GSSEAP_NO_ATTR_CONTEXT;
748             return GSS_S_UNAVAILABLE;
749         }
750     } catch (std::exception &e) {
751         return mapException(minor, e);
752     }
753
754     return GSS_S_COMPLETE;
755 }
756
757 OM_uint32
758 gssEapGetNameAttribute(OM_uint32 *minor,
759                        gss_name_t name,
760                        gss_buffer_t attr,
761                        int *authenticated,
762                        int *complete,
763                        gss_buffer_t value,
764                        gss_buffer_t display_value,
765                        int *more)
766 {
767     *authenticated = 0;
768     *complete = 0;
769
770     if (value != NULL) {
771         value->length = 0;
772         value->value = NULL;
773     }
774
775     if (display_value != NULL) {
776         display_value->length = 0;
777         display_value->value = NULL;
778     }
779
780     if (name->attrCtx == NULL) {
781         *minor = GSSEAP_NO_ATTR_CONTEXT;
782         return GSS_S_UNAVAILABLE;
783     }
784
785     if (GSS_ERROR(gssEapAttrProvidersInit(minor))) {
786         return GSS_S_UNAVAILABLE;
787     }
788
789     try {
790         if (!name->attrCtx->getAttribute(attr, authenticated, complete,
791                                          value, display_value, more)) {
792             *minor = GSSEAP_NO_SUCH_ATTR;
793             gssEapSaveStatusInfo(*minor, "Unknown naming attribute %.*s",
794                                  (int)attr->length, (char *)attr->value);
795             return GSS_S_UNAVAILABLE;
796         }
797     } catch (std::exception &e) {
798         return mapException(minor, e);
799     }
800
801     return GSS_S_COMPLETE;
802 }
803
804 OM_uint32
805 gssEapDeleteNameAttribute(OM_uint32 *minor,
806                           gss_name_t name,
807                           gss_buffer_t attr)
808 {
809     if (name->attrCtx == NULL) {
810         *minor = GSSEAP_NO_ATTR_CONTEXT;
811         return GSS_S_UNAVAILABLE;
812     }
813
814     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
815         return GSS_S_UNAVAILABLE;
816
817     try {
818         name->attrCtx->deleteAttribute(attr);
819     } catch (std::exception &ex) {
820         return mapException(minor, ex);
821     }
822
823     return GSS_S_COMPLETE;
824 }
825
826 OM_uint32
827 gssEapSetNameAttribute(OM_uint32 *minor,
828                        gss_name_t name,
829                        int complete,
830                        gss_buffer_t attr,
831                        gss_buffer_t value)
832 {
833     if (name->attrCtx == NULL) {
834         *minor = GSSEAP_NO_ATTR_CONTEXT;
835         return GSS_S_UNAVAILABLE;
836     }
837
838     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
839         return GSS_S_UNAVAILABLE;
840
841     try {
842         name->attrCtx->setAttribute(complete, attr, value);
843     } catch (std::exception &ex) {
844         return mapException(minor, ex);
845     }
846
847     return GSS_S_COMPLETE;
848 }
849
850 OM_uint32
851 gssEapExportAttrContext(OM_uint32 *minor,
852                         gss_name_t name,
853                         gss_buffer_t buffer)
854 {
855     if (name->attrCtx == NULL) {
856         buffer->length = 0;
857         buffer->value = NULL;
858
859         return GSS_S_COMPLETE;
860     }
861
862     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
863         return GSS_S_UNAVAILABLE;
864
865     try {
866         name->attrCtx->exportToBuffer(buffer);
867     } catch (std::exception &e) {
868         return mapException(minor, e);
869     }
870
871     return GSS_S_COMPLETE;
872 }
873
874 OM_uint32
875 gssEapImportAttrContext(OM_uint32 *minor,
876                         gss_buffer_t buffer,
877                         gss_name_t name)
878 {
879     gss_eap_attr_ctx *ctx = NULL;
880
881     assert(name->attrCtx == NULL);
882
883     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
884         return GSS_S_UNAVAILABLE;
885
886     if (buffer->length != 0) {
887         try {
888             ctx = new gss_eap_attr_ctx();
889
890             if (!ctx->initFromBuffer(buffer)) {
891                 delete ctx;
892                 *minor = GSSEAP_BAD_ATTR_TOKEN;
893                 return GSS_S_DEFECTIVE_TOKEN;
894             }
895             name->attrCtx = ctx;
896         } catch (std::exception &e) {
897             delete ctx;
898             return mapException(minor, e);
899         }
900     }
901
902     return GSS_S_COMPLETE;
903 }
904
905 OM_uint32
906 gssEapDuplicateAttrContext(OM_uint32 *minor,
907                            gss_name_t in,
908                            gss_name_t out)
909 {
910     gss_eap_attr_ctx *ctx = NULL;
911
912     assert(out->attrCtx == NULL);
913
914     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
915         return GSS_S_UNAVAILABLE;
916
917     try {
918         if (in->attrCtx != NULL) {
919             ctx = new gss_eap_attr_ctx();
920             if (!ctx->initFromExistingContext(in->attrCtx)) {
921                 delete ctx;
922                 *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
923                 return GSS_S_FAILURE;
924             }
925             out->attrCtx = ctx;
926         }
927     } catch (std::exception &e) {
928         delete ctx;
929         return mapException(minor, e);
930     }
931
932     return GSS_S_COMPLETE;
933 }
934
935 OM_uint32
936 gssEapMapNameToAny(OM_uint32 *minor,
937                    gss_name_t name,
938                    int authenticated,
939                    gss_buffer_t type_id,
940                    gss_any_t *output)
941 {
942     if (name->attrCtx == NULL) {
943         *minor = GSSEAP_NO_ATTR_CONTEXT;
944         return GSS_S_UNAVAILABLE;
945     }
946
947     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
948         return GSS_S_UNAVAILABLE;
949
950     try {
951         *output = name->attrCtx->mapToAny(authenticated, type_id);
952     } catch (std::exception &e) {
953         return mapException(minor, e);
954     }
955
956     return GSS_S_COMPLETE;
957 }
958
959 OM_uint32
960 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
961                             gss_name_t name,
962                             gss_buffer_t type_id,
963                             gss_any_t *input)
964 {
965     if (name->attrCtx == NULL) {
966         *minor = GSSEAP_NO_ATTR_CONTEXT;
967         return GSS_S_UNAVAILABLE;
968     }
969
970     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
971         return GSS_S_UNAVAILABLE;
972
973     try {
974         if (*input != NULL)
975             name->attrCtx->releaseAnyNameMapping(type_id, *input);
976         *input = NULL;
977     } catch (std::exception &e) {
978         return mapException(minor, e);
979     }
980
981     return GSS_S_COMPLETE;
982 }
983
984 OM_uint32
985 gssEapReleaseAttrContext(OM_uint32 *minor,
986                          gss_name_t name)
987 {
988     if (name->attrCtx != NULL)
989         delete name->attrCtx;
990
991     return GSS_S_COMPLETE;
992 }
993
994 /*
995  * Public accessor for initialisng a context from a GSS context. Also
996  * sets expiry time on GSS context as a side-effect.
997  */
998 struct gss_eap_attr_ctx *
999 gssEapCreateAttrContext(gss_cred_id_t gssCred,
1000                         gss_ctx_id_t gssCtx)
1001 {
1002     gss_eap_attr_ctx *ctx;
1003     OM_uint32 tmpMinor;
1004
1005     assert(gssCtx != GSS_C_NO_CONTEXT);
1006
1007     if (GSS_ERROR(gssEapAttrProvidersInit(&tmpMinor)))
1008         return NULL;
1009
1010     ctx = new gss_eap_attr_ctx();
1011     if (!ctx->initFromGssContext(gssCred, gssCtx)) {
1012         delete ctx;
1013         return NULL;
1014     }
1015
1016     gssCtx->expiryTime = ctx->getExpiryTime();
1017
1018     return ctx;
1019 }