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 getPrimaryProvider()->exportToBuffer(buffer);
451 * Return soonest expiry time of providers
454 gss_eap_attr_ctx::getExpiryTime(void) const
457 time_t expiryTime = 0;
459 for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
460 gss_eap_attr_provider *provider = m_providers[i];
461 time_t providerExpiryTime;
463 if (provider == NULL)
466 providerExpiryTime = provider->getExpiryTime();
467 if (providerExpiryTime == 0)
470 if (expiryTime == 0 || providerExpiryTime < expiryTime)
471 expiryTime = providerExpiryTime;
478 * Map C++ exception to GSS status
481 mapException(OM_uint32 *minor, std::exception &e)
483 OM_uint32 major = GSS_S_FAILURE;
485 /* XXX TODO implement other mappings */
486 if (typeid(e) == typeid(std::bad_alloc))
492 /* rethrow for now for debugging */
500 * Decompose attribute name into prefix and suffix
503 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
510 for (i = 0; i < attribute->length; i++) {
511 if (((char *)attribute->value)[i] == ' ') {
512 p = (char *)attribute->value + i + 1;
517 prefix->value = attribute->value;
520 if (p != NULL && *p != '\0') {
521 suffix->length = attribute->length - 1 - prefix->length;
525 suffix->value = NULL;
530 * Decompose attribute name into type and suffix
533 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
537 gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
539 decomposeAttributeName(attribute, &prefix, suffix);
540 *type = attributePrefixToType(&prefix);
544 * Compose attribute name from prefix, suffix; returns C++ string
547 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
548 const gss_buffer_t suffix)
552 if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
555 str.append((const char *)prefix->value, prefix->length);
557 if (suffix != GSS_C_NO_BUFFER) {
559 str.append((const char *)suffix->value, suffix->length);
566 * Compose attribute name from type, suffix; returns C++ string
569 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
570 const gss_buffer_t suffix)
572 const gss_buffer_t prefix = attributeTypeToPrefix(type);
574 return composeAttributeName(prefix, suffix);
578 * Compose attribute name from prefix, suffix; returns GSS buffer
581 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
582 const gss_buffer_t suffix,
583 gss_buffer_t attribute)
585 std::string str = composeAttributeName(prefix, suffix);
587 if (str.length() != 0) {
588 return duplicateBuffer(str, attribute);
590 attribute->length = 0;
591 attribute->value = NULL;
596 * Compose attribute name from type, suffix; returns GSS buffer
599 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
600 const gss_buffer_t suffix,
601 gss_buffer_t attribute)
603 gss_buffer_t prefix = attributeTypeToPrefix(type);
605 return composeAttributeName(prefix, suffix, attribute);
612 gssEapInquireName(OM_uint32 *minor,
616 gss_buffer_set_t *attrs)
618 if (name->attrCtx == NULL)
619 return GSS_S_UNAVAILABLE;
622 if (!name->attrCtx->getAttributeTypes(attrs))
623 return GSS_S_UNAVAILABLE;
624 } catch (std::exception &e) {
625 return mapException(minor, e);
628 return GSS_S_COMPLETE;
632 gssEapGetNameAttribute(OM_uint32 *minor,
638 gss_buffer_t display_value,
649 if (display_value != NULL) {
650 display_value->length = 0;
651 display_value->value = NULL;
654 if (name->attrCtx == NULL)
655 return GSS_S_UNAVAILABLE;
658 if (!name->attrCtx->getAttribute(attr, authenticated, complete,
659 value, display_value, more))
660 return GSS_S_UNAVAILABLE;
661 } catch (std::exception &e) {
662 return mapException(minor, e);
665 return GSS_S_COMPLETE;
669 gssEapDeleteNameAttribute(OM_uint32 *minor,
673 if (name->attrCtx == NULL)
674 return GSS_S_UNAVAILABLE;
677 name->attrCtx->deleteAttribute(attr);
678 } catch (std::exception &ex) {
679 return mapException(minor, ex);
682 return GSS_S_COMPLETE;
686 gssEapSetNameAttribute(OM_uint32 *minor,
692 if (name->attrCtx == NULL)
693 return GSS_S_UNAVAILABLE;
696 name->attrCtx->setAttribute(complete, attr, value);
697 } catch (std::exception &ex) {
698 return mapException(minor, ex);
701 return GSS_S_COMPLETE;
705 gssEapExportAttrContext(OM_uint32 *minor,
709 if (name->attrCtx == NULL) {
711 buffer->value = NULL;
713 return GSS_S_COMPLETE;
717 name->attrCtx->exportToBuffer(buffer);
718 } catch (std::exception &e) {
719 return mapException(minor, e);
722 return GSS_S_COMPLETE;
726 gssEapImportAttrContext(OM_uint32 *minor,
730 gss_eap_attr_ctx *ctx = NULL;
732 assert(name->attrCtx == NULL);
734 if (buffer->length != 0) {
736 ctx = new gss_eap_attr_ctx();
738 if (!ctx->initFromBuffer(buffer)) {
740 return GSS_S_DEFECTIVE_TOKEN;
743 } catch (std::exception &e) {
745 return mapException(minor, e);
749 return GSS_S_COMPLETE;
753 gssEapDuplicateAttrContext(OM_uint32 *minor,
757 gss_eap_attr_ctx *ctx = NULL;
759 assert(out->attrCtx == NULL);
762 if (in->attrCtx != NULL) {
763 ctx = new gss_eap_attr_ctx();
764 if (!ctx->initFromExistingContext(in->attrCtx)) {
766 return GSS_S_FAILURE;
770 } catch (std::exception &e) {
772 return mapException(minor, e);
775 return GSS_S_COMPLETE;
779 gssEapMapNameToAny(OM_uint32 *minor,
782 gss_buffer_t type_id,
785 if (name->attrCtx == NULL)
786 return GSS_S_UNAVAILABLE;
789 *output = name->attrCtx->mapToAny(authenticated, type_id);
790 } catch (std::exception &e) {
791 return mapException(minor, e);
794 return GSS_S_COMPLETE;
798 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
800 gss_buffer_t type_id,
803 if (name->attrCtx == NULL)
804 return GSS_S_UNAVAILABLE;
808 name->attrCtx->releaseAnyNameMapping(type_id, *input);
810 } catch (std::exception &e) {
811 return mapException(minor, e);
814 return GSS_S_COMPLETE;
818 gssEapReleaseAttrContext(OM_uint32 *minor,
821 if (name->attrCtx != NULL)
822 delete name->attrCtx;
824 return GSS_S_COMPLETE;
828 * Public accessor for initialisng a context from a GSS context. Also
829 * sets expiry time on GSS context as a side-effect.
831 struct gss_eap_attr_ctx *
832 gssEapCreateAttrContext(gss_cred_id_t gssCred,
835 gss_eap_attr_ctx *ctx;
837 assert(gssCtx != GSS_C_NO_CONTEXT);
839 ctx = new gss_eap_attr_ctx();
840 if (!ctx->initFromGssContext(gssCred, gssCtx)) {
845 gssCtx->expiryTime = ctx->getExpiryTime();