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
34 * SAML attribute provider implementation.
37 #include "gssapiP_eap.h"
41 #include <xercesc/util/XMLUniDefs.hpp>
42 #include <xmltooling/unicode.h>
43 #include <xmltooling/XMLToolingConfig.h>
44 #include <xmltooling/util/XMLHelper.h>
45 #include <xmltooling/util/ParserPool.h>
46 #include <xmltooling/util/DateTime.h>
48 #include <saml/exceptions.h>
49 #include <saml/saml1/core/Assertions.h>
50 #include <saml/saml2/core/Assertions.h>
51 #include <saml/saml2/metadata/Metadata.h>
52 #include <saml/saml2/metadata/MetadataProvider.h>
54 using namespace xmltooling;
55 using namespace opensaml::saml2md;
56 using namespace opensaml;
57 using namespace xercesc;
61 * gss_eap_saml_assertion_provider is for retrieving the underlying
64 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(void)
67 m_authenticated = false;
70 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
76 gss_eap_saml_assertion_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
77 const gss_eap_attr_provider *ctx)
79 /* Then we may be creating from an existing attribute context */
80 const gss_eap_saml_assertion_provider *saml;
82 assert(m_assertion == NULL);
84 if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx))
87 saml = static_cast<const gss_eap_saml_assertion_provider *>(ctx);
88 setAssertion(saml->getAssertion(), saml->authenticated());
94 gss_eap_saml_assertion_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
95 const gss_cred_id_t gssCred,
96 const gss_ctx_id_t gssCtx)
98 const gss_eap_radius_attr_provider *radius;
99 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
100 int authenticated, complete;
103 assert(m_assertion == NULL);
105 if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
109 * XXX TODO we need to support draft-howlett-radius-saml-attr-00
111 radius = static_cast<const gss_eap_radius_attr_provider *>
112 (m_manager->getProvider(ATTR_TYPE_RADIUS));
113 if (radius != NULL &&
114 radius->getFragmentedAttribute(PW_SAML_AAA_ASSERTION,
116 &authenticated, &complete, &value)) {
117 setAssertion(&value, authenticated);
118 gss_release_buffer(&minor, &value);
127 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion,
133 if (assertion != NULL) {
135 m_assertion = (saml2::Assertion *)((void *)assertion->clone());
137 m_assertion = dynamic_cast<saml2::Assertion *>(assertion->clone());
139 m_authenticated = authenticated;
142 m_authenticated = false;
147 gss_eap_saml_assertion_provider::setAssertion(const gss_buffer_t buffer,
152 m_assertion = parseAssertion(buffer);
153 m_authenticated = (m_assertion != NULL && authenticated);
157 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
159 string str((char *)buffer->value, buffer->length);
160 istringstream istream(str);
162 const XMLObjectBuilder *b;
165 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
169 b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
172 return (saml2::Assertion *)((void *)b->buildFromDocument(doc));
174 return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
176 } catch (exception &e) {
182 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
187 /* just add the prefix */
188 if (m_assertion != NULL)
189 ret = addAttribute(m_manager, this, GSS_C_NO_BUFFER, data);
197 gss_eap_saml_assertion_provider::setAttribute(int complete GSSEAP_UNUSED,
198 const gss_buffer_t attr,
199 const gss_buffer_t value)
201 if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
210 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value GSSEAP_UNUSED)
214 m_authenticated = false;
220 gss_eap_saml_assertion_provider::getExpiryTime(void) const
222 saml2::Conditions *conditions;
223 time_t expiryTime = 0;
225 if (m_assertion == NULL)
228 conditions = m_assertion->getConditions();
230 if (conditions != NULL && conditions->getNotOnOrAfter() != NULL)
231 expiryTime = conditions->getNotOnOrAfter()->getEpoch();
237 gss_eap_saml_assertion_provider::mapException(OM_uint32 *minor,
238 std::exception &e) const
240 if (typeid(e) == typeid(SecurityPolicyException))
241 *minor = GSSEAP_SAML_SEC_POLICY_FAILURE;
242 else if (typeid(e) == typeid(BindingException))
243 *minor = GSSEAP_SAML_BINDING_FAILURE;
244 else if (typeid(e) == typeid(ProfileException))
245 *minor = GSSEAP_SAML_PROFILE_FAILURE;
246 else if (typeid(e) == typeid(FatalProfileException))
247 *minor = GSSEAP_SAML_FATAL_PROFILE_FAILURE;
248 else if (typeid(e) == typeid(RetryableProfileException))
249 *minor = GSSEAP_SAML_RETRY_PROFILE_FAILURE;
250 else if (typeid(e) == typeid(MetadataException))
251 *minor = GSSEAP_SAML_METADATA_FAILURE;
253 return GSS_S_CONTINUE_NEEDED;
255 gssEapSaveStatusInfo(*minor, "%s", e.what());
257 return GSS_S_FAILURE;
261 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
265 gss_buffer_t display_value GSSEAP_UNUSED,
270 if (attr != GSS_C_NO_BUFFER && attr->length != 0)
273 if (m_assertion == NULL)
279 if (authenticated != NULL)
280 *authenticated = m_authenticated;
281 if (complete != NULL)
284 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
287 duplicateBuffer(str, value);
288 if (display_value != NULL)
289 duplicateBuffer(str, display_value);
297 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
298 gss_buffer_t type_id GSSEAP_UNUSED) const
300 if (authenticated && !m_authenticated)
301 return (gss_any_t)NULL;
303 return (gss_any_t)m_assertion;
307 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
308 gss_any_t input) const
310 delete ((saml2::Assertion *)input);
314 gss_eap_saml_assertion_provider::prefix(void) const
316 return "urn:ietf:params:gss-eap:saml-aaa-assertion";
320 gss_eap_saml_assertion_provider::init(void)
322 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION, createAttrContext);
327 gss_eap_saml_assertion_provider::finalize(void)
329 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML_ASSERTION);
332 gss_eap_attr_provider *
333 gss_eap_saml_assertion_provider::createAttrContext(void)
335 return new gss_eap_saml_assertion_provider;
339 gss_eap_saml_assertion_provider::initAssertion(void)
342 m_assertion = saml2::AssertionBuilder::buildAssertion();
343 m_authenticated = false;
349 * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
352 gss_eap_saml_attr_provider::getAssertion(int *authenticated,
353 saml2::Assertion **pAssertion,
354 bool createIfAbsent) const
356 gss_eap_saml_assertion_provider *saml;
358 if (authenticated != NULL)
359 *authenticated = false;
360 if (pAssertion != NULL)
363 saml = static_cast<const gss_eap_saml_assertion_provider *>
364 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
368 if (authenticated != NULL)
369 *authenticated = saml->authenticated();
370 if (pAssertion != NULL)
371 *pAssertion = saml->getAssertion();
373 if (saml->getAssertion() == NULL) {
374 if (createIfAbsent) {
375 if (authenticated != NULL)
376 *authenticated = false;
377 if (pAssertion != NULL)
378 *pAssertion = saml->initAssertion();
387 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
390 saml2::Assertion *assertion;
393 if (!getAssertion(&authenticated, &assertion))
397 * Note: the first prefix is added by the attribute provider manager
399 * From draft-hartman-gss-eap-naming-00:
401 * Each attribute carried in the assertion SHOULD also be a GSS name
402 * attribute. The name of this attribute has three parts, all separated
403 * by an ASCII space character. The first part is
404 * urn:ietf:params:gss-eap:saml-attr. The second part is the URI for
405 * the SAML attribute name format. The final part is the name of the
406 * SAML attribute. If the mechanism performs an additional attribute
407 * query, the retrieved attributes SHOULD be GSS-API name attributes
408 * using the same name syntax.
410 /* For each attribute statement, look for an attribute match */
411 const vector <saml2::AttributeStatement *> &statements =
412 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
414 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
415 s != statements.end();
417 const vector<saml2::Attribute*> &attrs =
418 const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
420 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
421 const XMLCh *attributeName, *attributeNameFormat;
422 XMLCh space[2] = { ' ', 0 };
423 gss_buffer_desc utf8;
425 attributeName = (*a)->getName();
426 attributeNameFormat = (*a)->getNameFormat();
427 if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
428 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
430 XMLCh qualifiedName[XMLString::stringLen(attributeNameFormat) + 1 +
431 XMLString::stringLen(attributeName) + 1];
432 XMLString::copyString(qualifiedName, attributeNameFormat);
433 XMLString::catString(qualifiedName, space);
434 XMLString::catString(qualifiedName, attributeName);
436 utf8.value = (void *)toUTF8(qualifiedName);
437 utf8.length = strlen((char *)utf8.value);
439 if (!addAttribute(m_manager, this, &utf8, data))
447 static BaseRefVectorOf<XMLCh> *
448 decomposeAttributeName(const gss_buffer_t attr)
450 BaseRefVectorOf<XMLCh> *components;
451 string str((const char *)attr->value, attr->length);
452 XMLCh qualifiedAttr[str.length() + 1];
454 XMLString::transcode(str.c_str(), qualifiedAttr, str.length());
456 components = XMLString::tokenizeString(qualifiedAttr);
458 if (components->size() != 2) {
467 gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
468 const gss_buffer_t attr,
469 const gss_buffer_t value)
471 saml2::Assertion *assertion;
472 saml2::Attribute *attribute;
473 saml2::AttributeValue *attributeValue;
474 saml2::AttributeStatement *attributeStatement;
476 if (!getAssertion(NULL, &assertion, true))
479 if (assertion->getAttributeStatements().size() != 0) {
480 attributeStatement = assertion->getAttributeStatements().front();
482 attributeStatement = saml2::AttributeStatementBuilder::buildAttributeStatement();
483 assertion->getAttributeStatements().push_back(attributeStatement);
486 /* Check the attribute name consists of name format | whsp | name */
487 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
488 if (components == NULL)
491 attribute = saml2::AttributeBuilder::buildAttribute();
492 attribute->setNameFormat(components->elementAt(0));
493 attribute->setName(components->elementAt(1));
495 XMLCh *xmlValue = new XMLCh[value->length + 1];
496 XMLString::transcode((const char *)value->value, xmlValue, attr->length);
498 attributeValue = saml2::AttributeValueBuilder::buildAttributeValue();
499 attributeValue->setTextContent(xmlValue);
501 attribute->getAttributeValues().push_back(attributeValue);
503 assert(attributeStatement != NULL);
504 attributeStatement->getAttributes().push_back(attribute);
513 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t attr)
515 saml2::Assertion *assertion;
518 if (!getAssertion(NULL, &assertion) ||
519 assertion->getAttributeStatements().size() == 0)
522 /* Check the attribute name consists of name format | whsp | name */
523 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
524 if (components == NULL)
527 /* For each attribute statement, look for an attribute match */
528 const vector<saml2::AttributeStatement *> &statements =
529 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
531 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
532 s != statements.end();
534 const vector<saml2::Attribute *> &attrs =
535 const_cast<const saml2::AttributeStatement *>(*s)->getAttributes();
536 ssize_t index = -1, i = 0;
538 /* There's got to be an easier way to do this */
539 for (vector<saml2::Attribute *>::const_iterator a = attrs.begin();
542 if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
543 XMLString::equals((*a)->getName(), components->elementAt(1))) {
550 (*s)->getAttributes().erase((*s)->getAttributes().begin() + index);
561 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
564 const saml2::Attribute **pAttribute) const
566 saml2::Assertion *assertion;
568 if (authenticated != NULL)
569 *authenticated = false;
570 if (complete != NULL)
574 if (!getAssertion(authenticated, &assertion) ||
575 assertion->getAttributeStatements().size() == 0)
578 /* Check the attribute name consists of name format | whsp | name */
579 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
580 if (components == NULL)
583 /* For each attribute statement, look for an attribute match */
584 const vector <saml2::AttributeStatement *> &statements =
585 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
586 const saml2::Attribute *ret = NULL;
588 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
589 s != statements.end();
591 const vector<saml2::Attribute *> &attrs =
592 const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
594 for (vector<saml2::Attribute *>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
595 const XMLCh *attributeName, *attributeNameFormat;
597 attributeName = (*a)->getName();
598 attributeNameFormat = (*a)->getNameFormat();
599 if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
600 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
602 if (XMLString::equals(attributeNameFormat, components->elementAt(0)) &&
603 XMLString::equals(attributeName, components->elementAt(1))) {
617 return (ret != NULL);
621 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
625 gss_buffer_t display_value,
628 const saml2::Attribute *a;
629 const saml2::AttributeValue *av;
630 int nvalues, i = *more;
634 if (!getAttribute(attr, authenticated, complete, &a))
637 nvalues = a->getAttributeValues().size();
641 else if (i >= nvalues)
644 av = (const saml2::AttributeValue *)((void *)(a->getAttributeValues().at(i)));
646 av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i));
650 value->value = toUTF8(av->getTextContent(), true);
651 value->length = strlen((char *)value->value);
653 if (display_value != NULL) {
654 display_value->value = toUTF8(av->getTextContent(), true);
655 display_value->length = strlen((char *)value->value);
666 gss_eap_saml_attr_provider::mapToAny(int authenticated GSSEAP_UNUSED,
667 gss_buffer_t type_id GSSEAP_UNUSED) const
669 return (gss_any_t)NULL;
673 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
674 gss_any_t input GSSEAP_UNUSED) const
679 gss_eap_saml_attr_provider::prefix(void) const
681 return "urn:ietf:params:gss-eap:saml-attr";
685 gss_eap_saml_attr_provider::init(void)
687 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML, createAttrContext);
692 gss_eap_saml_attr_provider::finalize(void)
694 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML);
697 gss_eap_attr_provider *
698 gss_eap_saml_attr_provider::createAttrContext(void)
700 return new gss_eap_saml_attr_provider;
704 gssEapSamlAttrProvidersInit(OM_uint32 *minor)
706 if (!gss_eap_saml_assertion_provider::init() ||
707 !gss_eap_saml_attr_provider::init()) {
708 *minor = GSSEAP_SAML_INIT_FAILURE;
709 return GSS_S_FAILURE;
712 return GSS_S_COMPLETE;
716 gssEapSamlAttrProvidersFinalize(OM_uint32 *minor)
718 gss_eap_saml_attr_provider::finalize();
719 gss_eap_saml_assertion_provider::finalize();
722 return GSS_S_COMPLETE;