2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 #include "gssapiP_eap.h"
40 static gss_eap_attr_create_provider gssEapAttrFactories[ATTR_TYPE_MAX + 1];
41 static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX + 1];
44 * Register a provider for a particular type and prefix
47 gss_eap_attr_ctx::registerProvider(unsigned int type,
49 gss_eap_attr_create_provider factory)
51 assert(type <= ATTR_TYPE_MAX);
53 assert(gssEapAttrFactories[type] == NULL);
55 gssEapAttrFactories[type] = factory;
57 gssEapAttrPrefixes[type].value = (void *)prefix;
58 gssEapAttrPrefixes[type].length = strlen(prefix);
60 gssEapAttrPrefixes[type].value = NULL;
61 gssEapAttrPrefixes[type].length = 0;
66 * Unregister a provider
69 gss_eap_attr_ctx::unregisterProvider(unsigned int type)
71 assert(type <= ATTR_TYPE_MAX);
73 gssEapAttrFactories[type] = NULL;
74 gssEapAttrPrefixes[type].value = NULL;
75 gssEapAttrPrefixes[type].length = 0;
79 * Create an attribute context, that manages instances of providers
81 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
83 for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
84 gss_eap_attr_provider *provider;
86 if (gssEapAttrFactories[i] != NULL) {
87 provider = (gssEapAttrFactories[i])();
92 m_providers[i] = provider;
97 * Convert an attribute prefix to a type
100 gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
104 for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
105 if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
109 return ATTR_TYPE_LOCAL;
113 * Convert a type to an attribute prefix
116 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
118 if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_MAX)
119 return GSS_C_NO_BUFFER;
121 return &gssEapAttrPrefixes[type];
125 * Initialize a context from an existing context.
128 gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
132 for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
133 gss_eap_attr_provider *provider = m_providers[i];
135 if (provider == NULL)
138 ret = provider->initFromExistingContext(this,
139 manager->m_providers[i]);
142 m_providers[i] = NULL;
151 * Initialize a context from a GSS credential and context.
154 gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
155 const gss_ctx_id_t ctx)
159 for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
160 gss_eap_attr_provider *provider = m_providers[i];
162 if (provider == NULL)
165 ret = provider->initFromGssContext(this, cred, ctx);
168 m_providers[i] = NULL;
177 * Initialize a context from an exported context or name token
180 gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
183 gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
185 ret = primaryProvider->initFromBuffer(this, buffer);
189 for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
190 gss_eap_attr_provider *provider = m_providers[i];
192 if (provider == primaryProvider)
195 ret = provider->initFromGssContext(this,
200 m_providers[i] = NULL;
208 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
210 for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++)
211 delete m_providers[i];
215 * Locate provider for a given type
217 gss_eap_attr_provider *
218 gss_eap_attr_ctx::getProvider(unsigned int type) const
220 assert(type >= ATTR_TYPE_MIN && type <= ATTR_TYPE_MAX);
221 return m_providers[type];
225 * Locate provider for a given prefix
227 gss_eap_attr_provider *
228 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
232 type = attributePrefixToType(prefix);
234 return m_providers[type];
238 * Get primary provider. Only the primary provider is serialised when
239 * gss_export_sec_context() or gss_export_name_composite() is called.
241 gss_eap_attr_provider *
242 gss_eap_attr_ctx::getPrimaryProvider(void) const
244 return m_providers[ATTR_TYPE_MIN];
251 gss_eap_attr_ctx::setAttribute(int complete,
252 const gss_buffer_t attr,
253 const gss_buffer_t value)
255 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
257 gss_eap_attr_provider *provider;
259 decomposeAttributeName(attr, &type, &suffix);
261 provider = m_providers[type];
262 if (provider != NULL) {
263 provider->setAttribute(complete,
264 (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
267 /* XXX TODO throw exception */
272 * Delete an attrbiute
275 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
277 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
279 gss_eap_attr_provider *provider;
281 decomposeAttributeName(attr, &type, &suffix);
283 provider = m_providers[type];
284 if (provider != NULL)
285 provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
289 * Enumerate attribute types with callback
292 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
297 for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
298 gss_eap_attr_provider *provider = m_providers[i];
300 if (provider == NULL)
303 ret = provider->getAttributeTypes(cb, data);
311 struct eap_gss_get_attr_types_args {
313 gss_buffer_set_t attrs;
317 addAttribute(const gss_eap_attr_provider *provider,
318 const gss_buffer_t attribute,
321 eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
322 gss_buffer_desc qualified;
323 OM_uint32 major, minor;
325 if (args->type != ATTR_TYPE_LOCAL) {
326 gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
327 major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
328 gss_release_buffer(&minor, &qualified);
330 major = gss_add_buffer_set_member(&minor, attribute, &args->attrs);
333 return GSS_ERROR(major) == false;
337 * Enumerate attribute types, output is buffer set
340 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
342 eap_gss_get_attr_types_args args;
343 OM_uint32 major, minor;
347 major = gss_create_empty_buffer_set(&minor, attrs);
348 if (GSS_ERROR(major)) {
349 throw new std::bad_alloc;
355 for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
356 gss_eap_attr_provider *provider = m_providers[i];
360 if (provider == NULL)
363 ret = provider->getAttributeTypes(addAttribute, (void *)&args);
369 gss_release_buffer_set(&minor, attrs);
375 * Get attribute with given name
378 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
382 gss_buffer_t display_value,
385 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
387 gss_eap_attr_provider *provider;
390 decomposeAttributeName(attr, &type, &suffix);
392 provider = m_providers[type];
393 if (provider == NULL)
396 ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
397 authenticated, complete,
398 value, display_value, more);
404 * Map attribute context to C++ object
407 gss_eap_attr_ctx::mapToAny(int authenticated,
408 gss_buffer_t type_id) const
411 gss_eap_attr_provider *provider;
412 gss_buffer_desc suffix;
414 decomposeAttributeName(type_id, &type, &suffix);
416 provider = m_providers[type];
417 if (provider == NULL)
418 return (gss_any_t)NULL;
420 return provider->mapToAny(authenticated, &suffix);
424 * Release mapped context
427 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
428 gss_any_t input) const
431 gss_eap_attr_provider *provider;
432 gss_buffer_desc suffix;
434 decomposeAttributeName(type_id, &type, &suffix);
436 provider = m_providers[type];
437 if (provider != NULL)
438 provider->releaseAnyNameMapping(&suffix, input);
442 * Export attribute context to buffer
445 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
447 const gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
449 primaryProvider->exportToBuffer(buffer);
453 * Return soonest expiry time of providers
456 gss_eap_attr_ctx::getExpiryTime(void) const
459 time_t expiryTime = 0;
461 for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
462 gss_eap_attr_provider *provider = m_providers[i];
463 time_t providerExpiryTime;
465 if (provider == NULL)
468 providerExpiryTime = provider->getExpiryTime();
469 if (providerExpiryTime == 0)
472 if (expiryTime == 0 || providerExpiryTime < expiryTime)
473 expiryTime = providerExpiryTime;
480 * Map C++ exception to GSS status
483 mapException(OM_uint32 *minor, std::exception &e)
485 OM_uint32 major = GSS_S_FAILURE;
487 /* XXX TODO implement other mappings */
488 if (typeid(e) == typeid(std::bad_alloc))
494 /* rethrow for now for debugging */
502 * Decompose attribute name into prefix and suffix
505 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
512 for (i = 0; i < attribute->length; i++) {
513 if (((char *)attribute->value)[i] == ' ') {
514 p = (char *)attribute->value + i + 1;
519 prefix->value = attribute->value;
522 if (p != NULL && *p != '\0') {
523 suffix->length = attribute->length - 1 - prefix->length;
527 suffix->value = NULL;
532 * Decompose attribute name into type and suffix
535 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
539 gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
541 decomposeAttributeName(attribute, &prefix, suffix);
542 *type = attributePrefixToType(&prefix);
546 * Compose attribute name from prefix, suffix; returns C++ string
549 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
550 const gss_buffer_t suffix)
554 if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
557 str.append((const char *)prefix->value, prefix->length);
559 if (suffix != GSS_C_NO_BUFFER) {
561 str.append((const char *)suffix->value, suffix->length);
568 * Compose attribute name from type, suffix; returns C++ string
571 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
572 const gss_buffer_t suffix)
574 const gss_buffer_t prefix = attributeTypeToPrefix(type);
576 return composeAttributeName(prefix, suffix);
580 * Compose attribute name from prefix, suffix; returns GSS buffer
583 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
584 const gss_buffer_t suffix,
585 gss_buffer_t attribute)
587 std::string str = composeAttributeName(prefix, suffix);
589 if (str.length() != 0) {
590 return duplicateBuffer(str, attribute);
592 attribute->length = 0;
593 attribute->value = NULL;
598 * Compose attribute name from type, suffix; returns GSS buffer
601 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
602 const gss_buffer_t suffix,
603 gss_buffer_t attribute)
605 gss_buffer_t prefix = attributeTypeToPrefix(type);
607 return composeAttributeName(prefix, suffix, attribute);
614 gssEapInquireName(OM_uint32 *minor,
618 gss_buffer_set_t *attrs)
620 if (name->attrCtx == NULL)
621 return GSS_S_UNAVAILABLE;
624 if (!name->attrCtx->getAttributeTypes(attrs))
625 return GSS_S_UNAVAILABLE;
626 } catch (std::exception &e) {
627 return mapException(minor, e);
630 return GSS_S_COMPLETE;
634 gssEapGetNameAttribute(OM_uint32 *minor,
640 gss_buffer_t display_value,
651 if (display_value != NULL) {
652 display_value->length = 0;
653 display_value->value = NULL;
656 if (name->attrCtx == NULL)
657 return GSS_S_UNAVAILABLE;
660 if (!name->attrCtx->getAttribute(attr, authenticated, complete,
661 value, display_value, more))
662 return GSS_S_UNAVAILABLE;
663 } catch (std::exception &e) {
664 return mapException(minor, e);
667 return GSS_S_COMPLETE;
671 gssEapDeleteNameAttribute(OM_uint32 *minor,
675 if (name->attrCtx == NULL)
676 return GSS_S_UNAVAILABLE;
679 name->attrCtx->deleteAttribute(attr);
680 } catch (std::exception &ex) {
681 return mapException(minor, ex);
684 return GSS_S_COMPLETE;
688 gssEapSetNameAttribute(OM_uint32 *minor,
694 if (name->attrCtx == NULL)
695 return GSS_S_UNAVAILABLE;
698 name->attrCtx->setAttribute(complete, attr, value);
699 } catch (std::exception &ex) {
700 return mapException(minor, ex);
703 return GSS_S_COMPLETE;
707 gssEapExportAttrContext(OM_uint32 *minor,
711 if (name->attrCtx == NULL) {
713 buffer->value = NULL;
715 return GSS_S_COMPLETE;
719 name->attrCtx->exportToBuffer(buffer);
720 } catch (std::exception &e) {
721 return mapException(minor, e);
724 return GSS_S_COMPLETE;
728 gssEapImportAttrContext(OM_uint32 *minor,
732 gss_eap_attr_ctx *ctx = NULL;
734 assert(name->attrCtx == NULL);
736 if (buffer->length != 0) {
738 ctx = new gss_eap_attr_ctx();
740 if (!ctx->initFromBuffer(buffer)) {
742 return GSS_S_DEFECTIVE_TOKEN;
745 } catch (std::exception &e) {
747 return mapException(minor, e);
751 return GSS_S_COMPLETE;
755 gssEapDuplicateAttrContext(OM_uint32 *minor,
759 gss_eap_attr_ctx *ctx = NULL;
761 assert(out->attrCtx == NULL);
764 if (in->attrCtx != NULL) {
765 ctx = new gss_eap_attr_ctx();
766 if (!ctx->initFromExistingContext(in->attrCtx)) {
768 return GSS_S_FAILURE;
772 } catch (std::exception &e) {
774 return mapException(minor, e);
777 return GSS_S_COMPLETE;
781 gssEapMapNameToAny(OM_uint32 *minor,
784 gss_buffer_t type_id,
787 if (name->attrCtx == NULL)
788 return GSS_S_UNAVAILABLE;
791 *output = name->attrCtx->mapToAny(authenticated, type_id);
792 } catch (std::exception &e) {
793 return mapException(minor, e);
796 return GSS_S_COMPLETE;
800 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
802 gss_buffer_t type_id,
805 if (name->attrCtx == NULL)
806 return GSS_S_UNAVAILABLE;
810 name->attrCtx->releaseAnyNameMapping(type_id, *input);
812 } catch (std::exception &e) {
813 return mapException(minor, e);
816 return GSS_S_COMPLETE;
820 gssEapReleaseAttrContext(OM_uint32 *minor,
823 if (name->attrCtx != NULL)
824 delete name->attrCtx;
826 return GSS_S_COMPLETE;
830 * Public accessor for initialisng a context from a GSS context. Also
831 * sets expiry time on GSS context as a side-effect.
833 struct gss_eap_attr_ctx *
834 gssEapCreateAttrContext(gss_cred_id_t gssCred,
837 gss_eap_attr_ctx *ctx;
839 assert(gssCtx != GSS_C_NO_CONTEXT);
841 ctx = new gss_eap_attr_ctx();
842 if (!ctx->initFromGssContext(gssCred, gssCtx)) {
847 gssCtx->expiryTime = ctx->getExpiryTime();