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];
41 static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX];
44 gss_eap_attr_ctx::registerProvider(unsigned int type,
46 gss_eap_attr_create_provider factory)
48 assert(type < ATTR_TYPE_MAX);
50 assert(gssEapAttrFactories[type] == NULL);
52 gssEapAttrFactories[type] = factory;
54 gssEapAttrPrefixes[type].value = (void *)prefix;
55 gssEapAttrPrefixes[type].length = strlen(prefix);
57 gssEapAttrPrefixes[type].value = NULL;
58 gssEapAttrPrefixes[type].length = 0;
63 gss_eap_attr_ctx::unregisterProvider(unsigned int type)
65 assert(type < ATTR_TYPE_MAX);
67 gssEapAttrFactories[type] = NULL;
68 gssEapAttrPrefixes[type].value = NULL;
69 gssEapAttrPrefixes[type].length = 0;
72 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
74 for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
75 gss_eap_attr_provider *provider;
77 provider = (gssEapAttrFactories[i])();
79 m_providers[i] = provider;
81 /* providers should fail in constructor */
82 assert(m_providers[i] != NULL);
87 gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
91 for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_LOCAL; i++) {
92 if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
96 return ATTR_TYPE_LOCAL;
100 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
102 if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
103 return GSS_C_NO_BUFFER;
105 return &gssEapAttrPrefixes[type];
109 * Initialize a context from an existing context.
112 gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
116 for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
117 gss_eap_attr_provider *provider = m_providers[i];
119 ret = provider->initFromExistingContext(this, manager->m_providers[i]);
128 * Initialize a context from a GSS credential and context.
131 gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
132 const gss_ctx_id_t ctx)
136 for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
137 gss_eap_attr_provider *provider = m_providers[i];
139 ret = provider->initFromGssContext(this, cred, ctx);
148 * Initialize a context from an exported context or name token
151 gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
154 gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
156 ret = primaryProvider->initFromBuffer(this, buffer);
160 for (unsigned int i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
161 gss_eap_attr_provider *provider = m_providers[i];
163 if (provider == primaryProvider)
166 ret = provider->initFromGssContext(this,
176 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
178 for (unsigned int i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++)
179 delete m_providers[i];
182 gss_eap_attr_provider *
183 gss_eap_attr_ctx::getProvider(unsigned int type) const
185 return m_providers[type];
188 gss_eap_attr_provider *
189 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
193 type = attributePrefixToType(prefix);
195 return m_providers[type];
198 gss_eap_attr_provider *
199 gss_eap_attr_ctx::getPrimaryProvider(void) const
201 return m_providers[ATTR_TYPE_RADIUS];
205 gss_eap_attr_ctx::setAttribute(int complete,
206 const gss_buffer_t attr,
207 const gss_buffer_t value)
209 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
211 gss_eap_attr_provider *provider;
213 decomposeAttributeName(attr, &type, &suffix);
215 provider = m_providers[type];
216 if (provider != NULL) {
217 provider->setAttribute(complete,
218 (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
225 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
227 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
229 gss_eap_attr_provider *provider;
231 decomposeAttributeName(attr, &type, &suffix);
233 provider = m_providers[type];
234 if (provider != NULL) {
235 provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
240 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
245 for (i = 0; i < ATTR_TYPE_MAX; i++) {
246 ret = m_providers[i]->getAttributeTypes(cb, data);
254 struct eap_gss_get_attr_types_args {
256 gss_buffer_set_t attrs;
260 addAttribute(const gss_eap_attr_provider *provider,
261 const gss_buffer_t attribute,
264 eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
265 gss_buffer_desc qualified;
266 OM_uint32 major, minor;
268 if (args->type != ATTR_TYPE_LOCAL) {
269 gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
270 major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
271 gss_release_buffer(&minor, &qualified);
273 major = gss_add_buffer_set_member(&minor, attribute, &args->attrs);
276 return GSS_ERROR(major) == false;
280 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
282 eap_gss_get_attr_types_args args;
283 OM_uint32 major, minor;
287 major = gss_create_empty_buffer_set(&minor, attrs);
288 if (GSS_ERROR(major)) {
289 throw new std::bad_alloc;
295 for (i = 0; i < ATTR_TYPE_MAX; i++) {
298 ret = m_providers[i]->getAttributeTypes(addAttribute, (void *)&args);
304 gss_release_buffer_set(&minor, attrs);
310 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
314 gss_buffer_t display_value,
317 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
319 gss_eap_attr_provider *provider;
322 decomposeAttributeName(attr, &type, &suffix);
324 provider = m_providers[type];
326 ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
327 authenticated, complete,
328 value, display_value, more);
334 gss_eap_attr_ctx::mapToAny(int authenticated,
335 gss_buffer_t type_id) const
338 gss_eap_attr_provider *provider;
340 type = attributePrefixToType(type_id);
342 provider = m_providers[type];
344 return provider->mapToAny(authenticated, type_id);
348 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
349 gss_any_t input) const
352 gss_eap_attr_provider *provider;
354 type = attributePrefixToType(type_id);
356 provider = m_providers[type];
358 provider->releaseAnyNameMapping(type_id, input);
362 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
364 getPrimaryProvider()->exportToBuffer(buffer);
368 gss_eap_attr_ctx::getExpiryTime(void) const
371 time_t expiryTime = 0;
373 for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
374 time_t providerExpiryTime = m_providers[i]->getExpiryTime();
376 if (providerExpiryTime == 0)
379 if (expiryTime == 0 || providerExpiryTime < expiryTime)
380 expiryTime = providerExpiryTime;
391 mapException(OM_uint32 *minor, std::exception &e)
393 OM_uint32 major = GSS_S_FAILURE;
395 /* XXX TODO implement other mappings */
396 if (typeid(e) == typeid(std::bad_alloc))
402 /* rethrow for now for debugging */
410 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
417 for (i = 0; i < attribute->length; i++) {
418 if (((char *)attribute->value)[i] == ' ') {
419 p = (char *)attribute->value + i + 1;
424 prefix->value = attribute->value;
427 if (p != NULL && *p != '\0') {
428 suffix->length = attribute->length - 1 - prefix->length;
432 suffix->value = NULL;
437 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
438 const gss_buffer_t suffix)
442 if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
445 str.append((const char *)prefix->value, prefix->length);
447 if (suffix != GSS_C_NO_BUFFER) {
449 str.append((const char *)suffix->value, suffix->length);
456 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
457 const gss_buffer_t suffix)
459 const gss_buffer_t prefix = attributeTypeToPrefix(type);
461 return composeAttributeName(prefix, suffix);
465 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
466 const gss_buffer_t suffix,
467 gss_buffer_t attribute)
469 std::string str = composeAttributeName(prefix, suffix);
471 if (str.length() != 0) {
472 return duplicateBuffer(str, attribute);
474 attribute->length = 0;
475 attribute->value = NULL;
480 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
484 gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
486 decomposeAttributeName(attribute, &prefix, suffix);
487 *type = attributePrefixToType(&prefix);
491 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
492 const gss_buffer_t suffix,
493 gss_buffer_t attribute)
495 gss_buffer_t prefix = attributeTypeToPrefix(type);
497 return composeAttributeName(prefix, suffix, attribute);
501 gssEapInquireName(OM_uint32 *minor,
505 gss_buffer_set_t *attrs)
507 if (name->attrCtx == NULL)
508 return GSS_S_UNAVAILABLE;
511 if (!name->attrCtx->getAttributeTypes(attrs))
512 return GSS_S_UNAVAILABLE;
513 } catch (std::exception &e) {
514 return mapException(minor, e);
517 return GSS_S_COMPLETE;
521 gssEapGetNameAttribute(OM_uint32 *minor,
527 gss_buffer_t display_value,
538 if (display_value != NULL) {
539 display_value->length = 0;
540 display_value->value = NULL;
543 if (name->attrCtx == NULL)
544 return GSS_S_UNAVAILABLE;
547 if (!name->attrCtx->getAttribute(attr, authenticated, complete,
548 value, display_value, more))
549 return GSS_S_UNAVAILABLE;
550 } catch (std::exception &e) {
551 return mapException(minor, e);
554 return GSS_S_COMPLETE;
558 gssEapDeleteNameAttribute(OM_uint32 *minor,
562 if (name->attrCtx == NULL)
563 return GSS_S_UNAVAILABLE;
566 name->attrCtx->deleteAttribute(attr);
567 } catch (std::exception &ex) {
568 return mapException(minor, ex);
571 return GSS_S_COMPLETE;
575 gssEapSetNameAttribute(OM_uint32 *minor,
581 if (name->attrCtx == NULL)
582 return GSS_S_UNAVAILABLE;
585 name->attrCtx->setAttribute(complete, attr, value);
586 } catch (std::exception &ex) {
587 return mapException(minor, ex);
590 return GSS_S_COMPLETE;
594 gssEapExportAttrContext(OM_uint32 *minor,
598 if (name->attrCtx == NULL) {
600 buffer->value = NULL;
602 return GSS_S_COMPLETE;
606 name->attrCtx->exportToBuffer(buffer);
607 } catch (std::exception &e) {
608 return mapException(minor, e);
611 return GSS_S_COMPLETE;
615 gssEapImportAttrContext(OM_uint32 *minor,
619 gss_eap_attr_ctx *ctx = NULL;
621 assert(name->attrCtx == NULL);
623 if (buffer->length != 0) {
625 ctx = new gss_eap_attr_ctx();
627 if (!ctx->initFromBuffer(buffer)) {
629 return GSS_S_DEFECTIVE_TOKEN;
632 } catch (std::exception &e) {
634 return mapException(minor, e);
638 return GSS_S_COMPLETE;
642 gssEapDuplicateAttrContext(OM_uint32 *minor,
646 gss_eap_attr_ctx *ctx = NULL;
648 assert(out->attrCtx == NULL);
651 if (in->attrCtx != NULL) {
652 ctx = new gss_eap_attr_ctx();
653 if (!ctx->initFromExistingContext(in->attrCtx)) {
655 return GSS_S_FAILURE;
659 } catch (std::exception &e) {
661 return mapException(minor, e);
664 return GSS_S_COMPLETE;
668 gssEapMapNameToAny(OM_uint32 *minor,
671 gss_buffer_t type_id,
674 if (name->attrCtx == NULL)
675 return GSS_S_UNAVAILABLE;
678 *output = name->attrCtx->mapToAny(authenticated, type_id);
679 } catch (std::exception &e) {
680 return mapException(minor, e);
683 return GSS_S_COMPLETE;
687 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
689 gss_buffer_t type_id,
692 if (name->attrCtx == NULL)
693 return GSS_S_UNAVAILABLE;
697 name->attrCtx->releaseAnyNameMapping(type_id, *input);
699 } catch (std::exception &e) {
700 return mapException(minor, e);
703 return GSS_S_COMPLETE;
707 gssEapReleaseAttrContext(OM_uint32 *minor,
710 if (name->attrCtx != NULL)
711 delete name->attrCtx;
713 return GSS_S_COMPLETE;
717 gssEapAttrProvidersInit(OM_uint32 *minor)
720 if (gss_eap_radius_attr_provider::init() &&
721 gss_eap_saml_assertion_provider::init() &&
722 gss_eap_saml_attr_provider::init() &&
723 gss_eap_shib_attr_provider::init())
724 return GSS_S_COMPLETE;
725 } catch (std::exception &e) {
726 return mapException(minor, e);
729 return GSS_S_FAILURE;
733 gssEapAttrProvidersFinalize(OM_uint32 *minor)
736 gss_eap_shib_attr_provider::finalize();
737 gss_eap_saml_attr_provider::finalize();
738 gss_eap_saml_assertion_provider::finalize();
739 gss_eap_radius_attr_provider::finalize();
740 } catch (std::exception &e) {
741 return mapException(minor, e);
744 return GSS_S_COMPLETE;
747 struct gss_eap_attr_ctx *
748 gssEapCreateAttrContext(gss_cred_id_t gssCred,
751 gss_eap_attr_ctx *ctx;
753 assert(gssCtx != GSS_C_NO_CONTEXT);
755 ctx = new gss_eap_attr_ctx();
756 if (!ctx->initFromGssContext(gssCred, gssCtx)) {
761 gssCtx->expiryTime = ctx->getExpiryTime();