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
314 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
315 gss_any_t input) const
320 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
322 m_providers[ATTR_TYPE_RADIUS]->exportToBuffer(buffer);
326 gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
331 ret = m_providers[ATTR_TYPE_RADIUS]->initFromBuffer(this, buffer);
335 for (i = ATTR_TYPE_RADIUS + 1; i < ATTR_TYPE_MAX; i++) {
336 gss_eap_attr_provider *provider = m_providers[i];
338 ret = provider->initFromGssContext(this,
353 mapException(OM_uint32 *minor, std::exception &e)
356 return GSS_S_FAILURE;
360 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
367 for (i = 0; i < attribute->length; i++) {
368 if (((char *)attribute->value)[i] == ' ') {
369 p = (char *)attribute->value + i + 1;
374 prefix->value = attribute->value;
377 if (p != NULL && *p != '\0') {
378 suffix->length = attribute->length - 1 - prefix->length;
382 suffix->value = NULL;
387 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
388 const gss_buffer_t suffix)
392 if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
395 str.append((const char *)prefix->value, prefix->length);
397 if (suffix != GSS_C_NO_BUFFER) {
399 str.append((const char *)suffix->value, suffix->length);
406 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
407 const gss_buffer_t suffix)
409 const gss_buffer_t prefix = attributeTypeToPrefix(type);
411 return composeAttributeName(prefix, suffix);
415 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
416 const gss_buffer_t suffix,
417 gss_buffer_t attribute)
419 std::string str = composeAttributeName(prefix, suffix);
421 if (str.length() != 0) {
422 return duplicateBuffer(str, attribute);
424 attribute->length = 0;
425 attribute->value = NULL;
430 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
434 gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
436 decomposeAttributeName(attribute, &prefix, suffix);
437 *type = attributePrefixToType(&prefix);
441 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
442 const gss_buffer_t suffix,
443 gss_buffer_t attribute)
445 gss_buffer_t prefix = attributeTypeToPrefix(type);
447 return composeAttributeName(prefix, suffix, attribute);
451 gssEapInquireName(OM_uint32 *minor,
455 gss_buffer_set_t *attrs)
457 if (name->attrCtx == NULL)
458 return GSS_S_UNAVAILABLE;
461 if (!name->attrCtx->getAttributeTypes(attrs))
462 return GSS_S_UNAVAILABLE;
463 } catch (std::exception &e) {
464 return mapException(minor, e);
467 return GSS_S_COMPLETE;
471 gssEapGetNameAttribute(OM_uint32 *minor,
477 gss_buffer_t display_value,
486 if (display_value != NULL) {
487 display_value->length = 0;
488 display_value->value = NULL;
493 if (name->attrCtx == NULL)
494 return GSS_S_UNAVAILABLE;
497 if (!name->attrCtx->getAttribute(attr, authenticated, complete,
498 value, display_value, more))
499 return GSS_S_UNAVAILABLE;
500 } catch (std::exception &e) {
501 return mapException(minor, e);
504 return GSS_S_COMPLETE;
508 gssEapDeleteNameAttribute(OM_uint32 *minor,
512 if (name->attrCtx == NULL)
513 return GSS_S_UNAVAILABLE;
516 name->attrCtx->deleteAttribute(attr);
517 } catch (std::exception &ex) {
518 return mapException(minor, ex);
521 return GSS_S_COMPLETE;
525 gssEapSetNameAttribute(OM_uint32 *minor,
531 if (name->attrCtx == NULL)
532 return GSS_S_UNAVAILABLE;
535 name->attrCtx->setAttribute(complete, attr, value);
536 } catch (std::exception &ex) {
537 return mapException(minor, ex);
540 return GSS_S_COMPLETE;
544 gssEapExportAttrContext(OM_uint32 *minor,
548 if (name->attrCtx == NULL) {
550 buffer->value = NULL;
552 return GSS_S_COMPLETE;
556 name->attrCtx->exportToBuffer(buffer);
557 if (buffer->length == 0)
558 return GSS_S_FAILURE;
559 } catch (std::exception &e) {
560 return mapException(minor, e);
563 return GSS_S_COMPLETE;
567 gssEapImportAttrContext(OM_uint32 *minor,
571 gss_eap_attr_ctx *ctx = NULL;
573 assert(name->attrCtx == NULL);
575 if (buffer->length != 0) {
577 ctx = new gss_eap_attr_ctx();
579 if (!ctx->initFromBuffer(buffer)) {
581 return GSS_S_DEFECTIVE_TOKEN;
584 } catch (std::exception &e) {
586 return mapException(minor, e);
590 return GSS_S_COMPLETE;
594 gssEapDuplicateAttrContext(OM_uint32 *minor,
598 gss_eap_attr_ctx *ctx = NULL;
600 assert(out->attrCtx == NULL);
603 if (in->attrCtx != NULL) {
604 ctx = new gss_eap_attr_ctx();
605 if (!ctx->initFromExistingContext(in->attrCtx)) {
607 return GSS_S_FAILURE;
611 } catch (std::exception &e) {
613 return mapException(minor, e);
616 return GSS_S_COMPLETE;
620 gssEapMapNameToAny(OM_uint32 *minor,
623 gss_buffer_t type_id,
627 *output = name->attrCtx->mapToAny(authenticated, type_id);
628 } catch (std::exception &e) {
629 return mapException(minor, e);
632 return GSS_S_COMPLETE;
636 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
638 gss_buffer_t type_id,
641 if (name->attrCtx == NULL)
642 return GSS_S_UNAVAILABLE;
646 name->attrCtx->releaseAnyNameMapping(type_id, *input);
648 } catch (std::exception &e) {
649 return mapException(minor, e);
652 return GSS_S_COMPLETE;
656 gssEapReleaseAttrContext(OM_uint32 *minor,
659 if (name->attrCtx != NULL)
660 delete name->attrCtx;
662 return GSS_S_COMPLETE;
666 gssEapAttrProvidersInit(OM_uint32 *minor)
669 if (gss_eap_radius_attr_provider::init() &&
670 gss_eap_saml_assertion_provider::init() &&
671 gss_eap_saml_attr_provider::init() &&
672 gss_eap_shib_attr_provider::init())
673 return GSS_S_COMPLETE;
674 } catch (std::exception &e) {
675 return mapException(minor, e);
678 return GSS_S_FAILURE;
682 gssEapAttrProvidersFinalize(OM_uint32 *minor)
685 gss_eap_shib_attr_provider::finalize();
686 gss_eap_saml_attr_provider::finalize();
687 gss_eap_saml_assertion_provider::finalize();
688 gss_eap_radius_attr_provider::finalize();
689 } catch (std::exception &e) {
690 return mapException(minor, e);
693 return GSS_S_COMPLETE;
696 struct gss_eap_attr_ctx *
697 gssEapCreateAttrContext(gss_cred_id_t gssCred,
700 gss_eap_attr_ctx *ctx;
702 assert(gssCtx != GSS_C_NO_CONTEXT);
704 ctx = new gss_eap_attr_ctx();
705 if (!ctx->initFromGssContext(gssCred, gssCtx)) {