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"
39 static gss_eap_attr_create_provider gssEapAttrFactories[ATTR_TYPE_MAX];
40 static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX];
43 gss_eap_attr_ctx::registerProvider(unsigned int type,
45 gss_eap_attr_create_provider factory)
47 assert(type < ATTR_TYPE_MAX);
49 assert(gssEapAttrFactories[type] == NULL);
51 gssEapAttrFactories[type] = factory;
53 gssEapAttrPrefixes[type].value = (void *)prefix;
54 gssEapAttrPrefixes[type].length = strlen(prefix);
56 gssEapAttrPrefixes[type].value = NULL;
57 gssEapAttrPrefixes[type].length = 0;
62 gss_eap_attr_ctx::unregisterProvider(unsigned int type)
64 assert(type < ATTR_TYPE_MAX);
66 gssEapAttrFactories[type] = NULL;
67 gssEapAttrPrefixes[type].value = NULL;
68 gssEapAttrPrefixes[type].length = 0;
71 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
73 for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
74 gss_eap_attr_provider *provider;
76 provider = (gssEapAttrFactories[i])();
78 m_providers[i] = provider;
83 gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
87 for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_LOCAL; i++) {
88 if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
92 return ATTR_TYPE_LOCAL;
96 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
98 if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
99 return GSS_C_NO_BUFFER;
101 return &gssEapAttrPrefixes[type];
105 gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
107 for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
108 gss_eap_attr_provider *provider;
110 provider = m_providers[i];
111 if (provider != NULL) {
112 if (!provider->initFromExistingContext(this, manager->m_providers[i]))
121 gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
122 const gss_ctx_id_t ctx)
124 for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
125 gss_eap_attr_provider *provider;
127 provider = m_providers[i];
128 if (provider != NULL) {
129 if (!provider->initFromGssContext(this, cred, ctx))
137 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
139 for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++)
140 delete m_providers[i];
143 gss_eap_attr_provider *
144 gss_eap_attr_ctx::getProvider(unsigned int type) const
146 return m_providers[type];
149 gss_eap_attr_provider *
150 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
154 type = attributePrefixToType(prefix);
156 return m_providers[type];
160 gss_eap_attr_ctx::setAttribute(int complete,
161 const gss_buffer_t attr,
162 const gss_buffer_t value)
164 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
166 gss_eap_attr_provider *provider;
168 decomposeAttributeName(attr, &type, &suffix);
170 provider = m_providers[type];
171 if (provider != NULL) {
172 provider->setAttribute(complete,
173 (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
180 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
182 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
184 gss_eap_attr_provider *provider;
186 decomposeAttributeName(attr, &type, &suffix);
188 provider = m_providers[type];
189 if (provider != NULL) {
190 provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
195 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
200 for (i = 0; i < ATTR_TYPE_MAX; i++) {
201 gss_eap_attr_provider *provider;
203 provider = m_providers[i];
204 if (provider == NULL)
207 ret = provider->getAttributeTypes(cb, data);
215 struct eap_gss_get_attr_types_args {
217 gss_buffer_set_t attrs;
221 addAttribute(const gss_eap_attr_provider *provider,
222 const gss_buffer_t attribute,
225 eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
226 gss_buffer_t prefix = GSS_C_NO_BUFFER;
227 gss_buffer_desc qualified;
228 OM_uint32 major, minor;
230 if (args->type != ATTR_TYPE_LOCAL) {
231 gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
232 major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
233 gss_release_buffer(&minor, &qualified);
235 major = gss_add_buffer_set_member(&minor, prefix, &args->attrs);
238 return GSS_ERROR(major) ? false : true;
242 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
244 eap_gss_get_attr_types_args args;
245 OM_uint32 major, minor;
249 major = gss_create_empty_buffer_set(&minor, attrs);
250 if (GSS_ERROR(major)) {
251 throw new std::bad_alloc;
257 for (i = 0; i < ATTR_TYPE_MAX; i++) {
258 gss_eap_attr_provider *provider;
262 provider = m_providers[i];
263 if (provider == NULL)
266 ret = provider->getAttributeTypes(addAttribute, (void *)&args);
272 gss_release_buffer_set(&minor, attrs);
279 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
283 gss_buffer_t display_value,
286 gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
288 gss_eap_attr_provider *provider;
291 decomposeAttributeName(attr, &type, &suffix);
293 provider = m_providers[type];
294 if (provider == NULL) {
299 ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
300 authenticated, complete,
301 value, display_value, more);
307 gss_eap_attr_ctx::mapToAny(int authenticated,
308 gss_buffer_t type_id) const
311 gss_eap_attr_provider *provider;
313 type = attributePrefixToType(type_id);
315 provider = m_providers[type];
317 return provider->mapToAny(authenticated, type_id);
321 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
322 gss_any_t input) const
325 gss_eap_attr_provider *provider;
327 type = attributePrefixToType(type_id);
329 provider = m_providers[type];
331 provider->releaseAnyNameMapping(type_id, input);
334 gss_eap_attr_provider *
335 gss_eap_attr_ctx::getPrimaryProvider(void) const
337 return m_providers[ATTR_TYPE_RADIUS];
341 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
343 getPrimaryProvider()->exportToBuffer(buffer);
347 gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
351 gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
353 assert(primaryProvider != NULL);
355 ret = primaryProvider->initFromBuffer(this, buffer);
359 for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
360 gss_eap_attr_provider *provider = m_providers[i];
362 if (provider == primaryProvider)
365 ret = provider->initFromGssContext(this,
380 mapException(OM_uint32 *minor, std::exception &e)
383 return GSS_S_FAILURE;
387 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
394 for (i = 0; i < attribute->length; i++) {
395 if (((char *)attribute->value)[i] == ' ') {
396 p = (char *)attribute->value + i + 1;
401 prefix->value = attribute->value;
404 if (p != NULL && *p != '\0') {
405 suffix->length = attribute->length - 1 - prefix->length;
409 suffix->value = NULL;
414 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
415 const gss_buffer_t suffix)
419 if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
422 str.append((const char *)prefix->value, prefix->length);
424 if (suffix != GSS_C_NO_BUFFER) {
426 str.append((const char *)suffix->value, suffix->length);
433 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
434 const gss_buffer_t suffix)
436 const gss_buffer_t prefix = attributeTypeToPrefix(type);
438 return composeAttributeName(prefix, suffix);
442 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
443 const gss_buffer_t suffix,
444 gss_buffer_t attribute)
446 std::string str = composeAttributeName(prefix, suffix);
448 if (str.length() != 0) {
449 return duplicateBuffer(str, attribute);
451 attribute->length = 0;
452 attribute->value = NULL;
457 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
461 gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
463 decomposeAttributeName(attribute, &prefix, suffix);
464 *type = attributePrefixToType(&prefix);
468 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
469 const gss_buffer_t suffix,
470 gss_buffer_t attribute)
472 gss_buffer_t prefix = attributeTypeToPrefix(type);
474 return composeAttributeName(prefix, suffix, attribute);
478 gssEapInquireName(OM_uint32 *minor,
482 gss_buffer_set_t *attrs)
484 if (name->attrCtx == NULL)
485 return GSS_S_UNAVAILABLE;
488 if (!name->attrCtx->getAttributeTypes(attrs))
489 return GSS_S_UNAVAILABLE;
490 } catch (std::exception &e) {
491 return mapException(minor, e);
494 return GSS_S_COMPLETE;
498 gssEapGetNameAttribute(OM_uint32 *minor,
504 gss_buffer_t display_value,
515 if (display_value != NULL) {
516 display_value->length = 0;
517 display_value->value = NULL;
520 if (name->attrCtx == NULL)
521 return GSS_S_UNAVAILABLE;
524 if (!name->attrCtx->getAttribute(attr, authenticated, complete,
525 value, display_value, more))
526 return GSS_S_UNAVAILABLE;
527 } catch (std::exception &e) {
528 return mapException(minor, e);
531 return GSS_S_COMPLETE;
535 gssEapDeleteNameAttribute(OM_uint32 *minor,
539 if (name->attrCtx == NULL)
540 return GSS_S_UNAVAILABLE;
543 name->attrCtx->deleteAttribute(attr);
544 } catch (std::exception &ex) {
545 return mapException(minor, ex);
548 return GSS_S_COMPLETE;
552 gssEapSetNameAttribute(OM_uint32 *minor,
558 if (name->attrCtx == NULL)
559 return GSS_S_UNAVAILABLE;
562 name->attrCtx->setAttribute(complete, attr, value);
563 } catch (std::exception &ex) {
564 return mapException(minor, ex);
567 return GSS_S_COMPLETE;
571 gssEapExportAttrContext(OM_uint32 *minor,
575 if (name->attrCtx == NULL) {
577 buffer->value = NULL;
579 return GSS_S_COMPLETE;
583 name->attrCtx->exportToBuffer(buffer);
584 if (buffer->length == 0)
585 return GSS_S_FAILURE;
586 } catch (std::exception &e) {
587 return mapException(minor, e);
590 return GSS_S_COMPLETE;
594 gssEapImportAttrContext(OM_uint32 *minor,
598 gss_eap_attr_ctx *ctx = NULL;
600 assert(name->attrCtx == NULL);
602 if (buffer->length != 0) {
604 ctx = new gss_eap_attr_ctx();
606 if (!ctx->initFromBuffer(buffer)) {
608 return GSS_S_DEFECTIVE_TOKEN;
611 } catch (std::exception &e) {
613 return mapException(minor, e);
617 return GSS_S_COMPLETE;
621 gssEapDuplicateAttrContext(OM_uint32 *minor,
625 gss_eap_attr_ctx *ctx = NULL;
627 assert(out->attrCtx == NULL);
630 if (in->attrCtx != NULL) {
631 ctx = new gss_eap_attr_ctx();
632 if (!ctx->initFromExistingContext(in->attrCtx)) {
634 return GSS_S_FAILURE;
638 } catch (std::exception &e) {
640 return mapException(minor, e);
643 return GSS_S_COMPLETE;
647 gssEapMapNameToAny(OM_uint32 *minor,
650 gss_buffer_t type_id,
654 *output = name->attrCtx->mapToAny(authenticated, type_id);
655 } catch (std::exception &e) {
656 return mapException(minor, e);
659 return GSS_S_COMPLETE;
663 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
665 gss_buffer_t type_id,
668 if (name->attrCtx == NULL)
669 return GSS_S_UNAVAILABLE;
673 name->attrCtx->releaseAnyNameMapping(type_id, *input);
675 } catch (std::exception &e) {
676 return mapException(minor, e);
679 return GSS_S_COMPLETE;
683 gssEapReleaseAttrContext(OM_uint32 *minor,
686 if (name->attrCtx != NULL)
687 delete name->attrCtx;
689 return GSS_S_COMPLETE;
693 gssEapAttrProvidersInit(OM_uint32 *minor)
696 if (gss_eap_radius_attr_provider::init() &&
697 gss_eap_saml_assertion_provider::init() &&
698 gss_eap_saml_attr_provider::init() &&
699 gss_eap_shib_attr_provider::init())
700 return GSS_S_COMPLETE;
701 } catch (std::exception &e) {
702 return mapException(minor, e);
705 return GSS_S_FAILURE;
709 gssEapAttrProvidersFinalize(OM_uint32 *minor)
712 gss_eap_shib_attr_provider::finalize();
713 gss_eap_saml_attr_provider::finalize();
714 gss_eap_saml_assertion_provider::finalize();
715 gss_eap_radius_attr_provider::finalize();
716 } catch (std::exception &e) {
717 return mapException(minor, e);
720 return GSS_S_COMPLETE;
723 struct gss_eap_attr_ctx *
724 gssEapCreateAttrContext(gss_cred_id_t gssCred,
727 gss_eap_attr_ctx *ctx;
729 assert(gssCtx != GSS_C_NO_CONTEXT);
731 ctx = new gss_eap_attr_ctx();
732 if (!ctx->initFromGssContext(gssCred, gssCtx)) {