2 * Copyright (c) 2011, 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 * Copyright 2001-2009 Internet2
35 * Licensed under the Apache License, Version 2.0 (the "License");
36 * you may not use this file except in compliance with the License.
37 * You may obtain a copy of the License at
39 * http://www.apache.org/licenses/LICENSE-2.0
41 * Unless required by applicable law or agreed to in writing, software
42 * distributed under the License is distributed on an "AS IS" BASIS,
43 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44 * See the License for the specific language governing permissions and
45 * limitations under the License.
49 * Local attribute provider implementation.
52 #include "gssapiP_eap.h"
54 #include <xmltooling/XMLObject.h>
56 #include <xmltooling/XMLToolingConfig.h>
57 #include <xmltooling/util/ParserPool.h>
60 #include <saml/saml2/core/Assertions.h>
62 #include <shibsp/exceptions.h>
63 #include <shibsp/attribute/SimpleAttribute.h>
64 #include <shibsp/attribute/BinaryAttribute.h>
65 #include <shibsp/attribute/ScopedAttribute.h>
66 #include <shibresolver/resolver.h>
71 using namespace shibsp;
72 using namespace shibresolver;
73 using namespace xmltooling;
76 using namespace opensaml::saml2md;
77 using namespace opensaml;
79 using namespace xercesc;
82 // Use unique_ptr where available; auto_ptr otherwise.
83 #if __cplusplus > 199711L
84 #define UNIQUE_OR_AUTO_PTR unique_ptr
86 #define UNIQUE_OR_AUTO_PTR auto_ptr
90 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
92 m_initialized = false;
93 m_authenticated = false;
96 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
98 for_each(m_attributes.begin(),
100 xmltooling::cleanup<Attribute>())
105 gss_eap_shib_attr_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
106 const gss_eap_attr_provider *ctx)
108 const gss_eap_shib_attr_provider *shib;
110 if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx)) {
114 m_authenticated = false;
116 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
118 m_attributes = duplicateAttributes(shib->getAttributes());
119 m_authenticated = shib->authenticated();
122 m_initialized = true;
128 gss_eap_shib_attr_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
129 const gss_cred_id_t gssCred,
130 const gss_ctx_id_t gssCtx)
132 if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
135 UNIQUE_OR_AUTO_PTR<ShibbolethResolver> resolver(ShibbolethResolver::create());
138 * For now, leave ApplicationID defaulted.
139 * Later on, we could allow this via config option to the mechanism
140 * or rely on an SPRequest interface to pass in a URI identifying the
144 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
145 if (gssCred != GSS_C_NO_CREDENTIAL &&
146 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
147 resolver->setApplicationID((const char *)nameBuf.value);
148 gss_release_buffer(&minor, &nameBuf);
152 gss_buffer_desc mechName = GSS_C_EMPTY_BUFFER;
153 OM_uint32 major, minor;
155 major = gssEapExportNameInternal(&minor, gssCtx->initiatorName, &mechName,
156 EXPORT_NAME_FLAG_OID |
157 EXPORT_NAME_FLAG_COMPOSITE);
158 if (major == GSS_S_COMPLETE) {
159 resolver->addToken(&mechName);
160 gss_release_buffer(&minor, &mechName);
164 const gss_eap_saml_assertion_provider *saml;
165 saml = static_cast<const gss_eap_saml_assertion_provider *>
166 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
167 if (saml != NULL && saml->getAssertion() != NULL) {
168 resolver->addToken(saml->getAssertion());
171 /* If no OpenSAML, parse the XML assertion explicitly */
172 const gss_eap_radius_attr_provider *radius;
173 int authenticated, complete;
174 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
175 gss_eap_attrid attrid(VENDORPEC_UKERNA, PW_SAML_AAA_ASSERTION);
177 radius = static_cast<const gss_eap_radius_attr_provider *>
178 (m_manager->getProvider(ATTR_TYPE_RADIUS));
179 if (radius != NULL &&
180 radius->getFragmentedAttribute(attrid, &authenticated, &complete, &value)) {
181 string str((char *)value.value, value.length);
182 istringstream istream(str);
183 DOMDocument *doc = XMLToolingConfig::getConfig().getParser().parse(istream);
184 const XMLObjectBuilder *b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
185 resolver->addToken(b->buildFromDocument(doc));
186 gss_release_buffer(&minor, &value);
188 #endif /* HAVE_OPENSAML */
192 m_attributes = resolver->getResolvedAttributes();
193 resolver->getResolvedAttributes().clear();
194 } catch (exception &e) {
198 m_authenticated = true;
199 m_initialized = true;
205 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
209 GSSEAP_ASSERT(m_initialized);
211 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
212 a != m_attributes.end();
215 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
216 s != (*a)->getAliases().end();
218 if (attr->length == (*s).length() &&
219 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
229 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
230 const gss_buffer_t attr,
231 const gss_buffer_t value)
233 string attrStr((char *)attr->value, attr->length);
234 vector <string> ids(1, attrStr);
235 BinaryAttribute *a = new BinaryAttribute(ids);
237 GSSEAP_ASSERT(m_initialized);
239 if (value->length != 0) {
240 string valueStr((char *)value->value, value->length);
242 a->getValues().push_back(valueStr);
245 m_attributes.push_back(a);
246 m_authenticated = false;
252 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
256 GSSEAP_ASSERT(m_initialized);
258 i = getAttributeIndex(attr);
260 m_attributes.erase(m_attributes.begin() + i);
262 m_authenticated = false;
268 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
271 GSSEAP_ASSERT(m_initialized);
273 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
274 a != m_attributes.end();
277 gss_buffer_desc attribute;
279 attribute.value = (void *)((*a)->getId());
280 attribute.length = strlen((char *)attribute.value);
282 if (!addAttribute(m_manager, this, &attribute, data))
290 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
292 const Attribute *ret = NULL;
294 GSSEAP_ASSERT(m_initialized);
296 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
297 a != m_attributes.end();
300 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
301 s != (*a)->getAliases().end();
303 if (attr->length == (*s).length() &&
304 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
317 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
321 gss_buffer_t display_value,
324 const Attribute *shibAttr = NULL;
325 const BinaryAttribute *binaryAttr;
326 gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
327 gss_buffer_desc displayValueBuf = GSS_C_EMPTY_BUFFER;
328 int nvalues, i = *more;
330 GSSEAP_ASSERT(m_initialized);
334 shibAttr = getAttribute(attr);
335 if (shibAttr == NULL)
338 nvalues = shibAttr->valueCount();
345 binaryAttr = dynamic_cast<const BinaryAttribute *>(shibAttr);
346 if (binaryAttr != NULL) {
347 std::string str = binaryAttr->getValues()[*more];
349 valueBuf.value = (void *)str.data();
350 valueBuf.length = str.size();
352 std::string str = shibAttr->getSerializedValues()[*more];
354 valueBuf.value = (void *)str.c_str();
355 valueBuf.length = str.length();
357 const SimpleAttribute *simpleAttr =
358 dynamic_cast<const SimpleAttribute *>(shibAttr);
359 const ScopedAttribute *scopedAttr =
360 dynamic_cast<const ScopedAttribute *>(shibAttr);
361 if (simpleAttr != NULL || scopedAttr != NULL)
362 displayValueBuf = valueBuf;
365 if (authenticated != NULL)
366 *authenticated = m_authenticated;
367 if (complete != NULL)
370 duplicateBuffer(valueBuf, value);
371 if (display_value != NULL)
372 duplicateBuffer(displayValueBuf, display_value);
380 gss_eap_shib_attr_provider::mapToAny(int authenticated,
381 gss_buffer_t type_id GSSEAP_UNUSED) const
385 GSSEAP_ASSERT(m_initialized);
387 if (authenticated && !m_authenticated)
388 return (gss_any_t)NULL;
390 vector <Attribute *>v = duplicateAttributes(m_attributes);
392 output = (gss_any_t)new vector <Attribute *>(v);
398 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
399 gss_any_t input) const
401 GSSEAP_ASSERT(m_initialized);
403 vector <Attribute *> *v = ((vector <Attribute *> *)input);
408 gss_eap_shib_attr_provider::prefix(void) const
414 gss_eap_shib_attr_provider::name(void) const
420 gss_eap_shib_attr_provider::jsonRepresentation(void) const
424 if (m_initialized == false)
425 return obj; /* don't export incomplete context */
427 JSONObject jattrs = JSONObject::array();
429 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
430 a != m_attributes.end(); ++a) {
431 DDF attr = (*a)->marshall();
432 JSONObject jattr = JSONObject::ddf(attr);
433 jattrs.append(jattr);
436 obj.set("attributes", jattrs);
438 obj.set("authenticated", m_authenticated);
444 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
447 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
450 GSSEAP_ASSERT(m_authenticated == false);
451 GSSEAP_ASSERT(m_attributes.size() == 0);
453 JSONObject jattrs = obj["attributes"];
454 size_t nelems = jattrs.size();
456 for (size_t i = 0; i < nelems; i++) {
457 JSONObject jattr = jattrs.get(i);
459 DDF attr = jattr.ddf();
460 Attribute *attribute = Attribute::unmarshall(attr);
461 m_attributes.push_back(attribute);
464 m_authenticated = obj["authenticated"].integer();
465 m_initialized = true;
471 gss_eap_shib_attr_provider::init(void)
476 ret = ShibbolethResolver::init();
477 } catch (exception &e) {
481 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
487 gss_eap_shib_attr_provider::finalize(void)
489 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
490 ShibbolethResolver::term();
494 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
495 std::exception &e) const
497 if (typeid(e) == typeid(AttributeException))
498 *minor = GSSEAP_SHIB_ATTR_FAILURE;
499 else if (typeid(e) == typeid(AttributeExtractionException))
500 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
501 else if (typeid(e) == typeid(AttributeFilteringException))
502 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
503 else if (typeid(e) == typeid(AttributeResolutionException))
504 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
505 else if (typeid(e) == typeid(ConfigurationException))
506 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
507 else if (typeid(e) == typeid(ListenerException))
508 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
510 return GSS_S_CONTINUE_NEEDED;
512 gssEapSaveStatusInfo(*minor, "%s", e.what());
514 return GSS_S_FAILURE;
517 gss_eap_attr_provider *
518 gss_eap_shib_attr_provider::createAttrContext(void)
520 return new gss_eap_shib_attr_provider;
524 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
526 DDF obj = src->marshall();
527 Attribute *attribute = Attribute::unmarshall(obj);
534 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
536 vector <Attribute *> dst;
538 for (vector<Attribute *>::const_iterator a = src.begin();
541 dst.push_back(duplicateAttribute(*a));
547 gssEapLocalAttrProviderInit(OM_uint32 *minor)
549 if (!gss_eap_shib_attr_provider::init()) {
550 *minor = GSSEAP_SHIB_INIT_FAILURE;
551 return GSS_S_FAILURE;
553 return GSS_S_COMPLETE;
557 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
559 gss_eap_shib_attr_provider::finalize();
562 return GSS_S_COMPLETE;