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/SAMLConfig.h>
50 #include <saml/saml1/core/Assertions.h>
51 #include <saml/saml2/core/Assertions.h>
52 #include <saml/saml2/metadata/Metadata.h>
53 #include <saml/saml2/metadata/MetadataProvider.h>
55 using namespace xmltooling;
56 using namespace opensaml::saml2md;
57 using namespace opensaml;
58 using namespace xercesc;
62 base64Binary[] = {'b','a','s','e','6','4','B','i','n','a','r','y',0};
65 * gss_eap_saml_assertion_provider is for retrieving the underlying
68 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(void)
71 m_authenticated = false;
74 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
80 gss_eap_saml_assertion_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
81 const gss_eap_attr_provider *ctx)
83 /* Then we may be creating from an existing attribute context */
84 const gss_eap_saml_assertion_provider *saml;
86 assert(m_assertion == NULL);
88 if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx))
91 saml = static_cast<const gss_eap_saml_assertion_provider *>(ctx);
92 setAssertion(saml->getAssertion(), saml->authenticated());
98 gss_eap_saml_assertion_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
99 const gss_cred_id_t gssCred,
100 const gss_ctx_id_t gssCtx)
102 const gss_eap_radius_attr_provider *radius;
103 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
104 int authenticated, complete;
107 assert(m_assertion == NULL);
109 if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
113 * XXX TODO we need to support draft-howlett-radius-saml-attr-00
115 radius = static_cast<const gss_eap_radius_attr_provider *>
116 (m_manager->getProvider(ATTR_TYPE_RADIUS));
117 if (radius != NULL &&
118 radius->getFragmentedAttribute(PW_SAML_AAA_ASSERTION,
120 &authenticated, &complete, &value)) {
121 setAssertion(&value, authenticated);
122 gss_release_buffer(&minor, &value);
131 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion,
137 if (assertion != NULL) {
139 m_assertion = (saml2::Assertion *)((void *)assertion->clone());
141 m_assertion = dynamic_cast<saml2::Assertion *>(assertion->clone());
143 m_authenticated = authenticated;
146 m_authenticated = false;
151 gss_eap_saml_assertion_provider::setAssertion(const gss_buffer_t buffer,
156 m_assertion = parseAssertion(buffer);
157 m_authenticated = (m_assertion != NULL && authenticated);
161 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
163 string str((char *)buffer->value, buffer->length);
164 istringstream istream(str);
166 const XMLObjectBuilder *b;
169 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
173 b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
176 return (saml2::Assertion *)((void *)b->buildFromDocument(doc));
178 return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
180 } catch (exception &e) {
186 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
191 /* just add the prefix */
192 if (m_assertion != NULL)
193 ret = addAttribute(m_manager, this, GSS_C_NO_BUFFER, data);
201 gss_eap_saml_assertion_provider::setAttribute(int complete GSSEAP_UNUSED,
202 const gss_buffer_t attr,
203 const gss_buffer_t value)
205 if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
214 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value GSSEAP_UNUSED)
218 m_authenticated = false;
224 gss_eap_saml_assertion_provider::getExpiryTime(void) const
226 saml2::Conditions *conditions;
227 time_t expiryTime = 0;
229 if (m_assertion == NULL)
232 conditions = m_assertion->getConditions();
234 if (conditions != NULL && conditions->getNotOnOrAfter() != NULL)
235 expiryTime = conditions->getNotOnOrAfter()->getEpoch();
241 gss_eap_saml_assertion_provider::mapException(OM_uint32 *minor,
242 std::exception &e) const
244 if (typeid(e) == typeid(SecurityPolicyException))
245 *minor = GSSEAP_SAML_SEC_POLICY_FAILURE;
246 else if (typeid(e) == typeid(BindingException))
247 *minor = GSSEAP_SAML_BINDING_FAILURE;
248 else if (typeid(e) == typeid(ProfileException))
249 *minor = GSSEAP_SAML_PROFILE_FAILURE;
250 else if (typeid(e) == typeid(FatalProfileException))
251 *minor = GSSEAP_SAML_FATAL_PROFILE_FAILURE;
252 else if (typeid(e) == typeid(RetryableProfileException))
253 *minor = GSSEAP_SAML_RETRY_PROFILE_FAILURE;
254 else if (typeid(e) == typeid(MetadataException))
255 *minor = GSSEAP_SAML_METADATA_FAILURE;
257 return GSS_S_CONTINUE_NEEDED;
259 gssEapSaveStatusInfo(*minor, "%s", e.what());
261 return GSS_S_FAILURE;
265 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
269 gss_buffer_t display_value GSSEAP_UNUSED,
274 if (attr != GSS_C_NO_BUFFER && attr->length != 0)
277 if (m_assertion == NULL)
283 if (authenticated != NULL)
284 *authenticated = m_authenticated;
285 if (complete != NULL)
288 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
291 duplicateBuffer(str, value);
292 if (display_value != NULL)
293 duplicateBuffer(str, display_value);
301 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
302 gss_buffer_t type_id GSSEAP_UNUSED) const
304 if (authenticated && !m_authenticated)
305 return (gss_any_t)NULL;
307 return (gss_any_t)m_assertion;
311 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
312 gss_any_t input) const
314 delete ((saml2::Assertion *)input);
318 gss_eap_saml_assertion_provider::prefix(void) const
320 return "urn:ietf:params:gss-eap:saml-aaa-assertion";
324 gss_eap_saml_assertion_provider::init(void)
329 ret = SAMLConfig::getConfig().init();
330 } catch (exception &e) {
334 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION, createAttrContext);
340 gss_eap_saml_assertion_provider::finalize(void)
342 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML_ASSERTION);
345 gss_eap_attr_provider *
346 gss_eap_saml_assertion_provider::createAttrContext(void)
348 return new gss_eap_saml_assertion_provider;
352 gss_eap_saml_assertion_provider::initAssertion(void)
355 m_assertion = saml2::AssertionBuilder::buildAssertion();
356 m_authenticated = false;
362 * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
365 gss_eap_saml_attr_provider::getAssertion(int *authenticated,
366 saml2::Assertion **pAssertion,
367 bool createIfAbsent) const
369 gss_eap_saml_assertion_provider *saml;
371 if (authenticated != NULL)
372 *authenticated = false;
373 if (pAssertion != NULL)
376 saml = static_cast<gss_eap_saml_assertion_provider *>
377 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
381 if (authenticated != NULL)
382 *authenticated = saml->authenticated();
383 if (pAssertion != NULL)
384 *pAssertion = saml->getAssertion();
386 if (saml->getAssertion() == NULL) {
387 if (createIfAbsent) {
388 if (authenticated != NULL)
389 *authenticated = false;
390 if (pAssertion != NULL)
391 *pAssertion = saml->initAssertion();
400 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
403 saml2::Assertion *assertion;
406 if (!getAssertion(&authenticated, &assertion))
410 * Note: the first prefix is added by the attribute provider manager
412 * From draft-hartman-gss-eap-naming-00:
414 * Each attribute carried in the assertion SHOULD also be a GSS name
415 * attribute. The name of this attribute has three parts, all separated
416 * by an ASCII space character. The first part is
417 * urn:ietf:params:gss-eap:saml-attr. The second part is the URI for
418 * the SAML attribute name format. The final part is the name of the
419 * SAML attribute. If the mechanism performs an additional attribute
420 * query, the retrieved attributes SHOULD be GSS-API name attributes
421 * using the same name syntax.
423 /* For each attribute statement, look for an attribute match */
424 const vector <saml2::AttributeStatement *> &statements =
425 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
427 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
428 s != statements.end();
430 const vector<saml2::Attribute*> &attrs =
431 const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
433 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
434 const XMLCh *attributeName, *attributeNameFormat;
435 XMLCh space[2] = { ' ', 0 };
436 gss_buffer_desc utf8;
438 attributeName = (*a)->getName();
439 attributeNameFormat = (*a)->getNameFormat();
440 if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
441 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
443 XMLCh qualifiedName[XMLString::stringLen(attributeNameFormat) + 1 +
444 XMLString::stringLen(attributeName) + 1];
445 XMLString::copyString(qualifiedName, attributeNameFormat);
446 XMLString::catString(qualifiedName, space);
447 XMLString::catString(qualifiedName, attributeName);
449 utf8.value = (void *)toUTF8(qualifiedName);
450 utf8.length = strlen((char *)utf8.value);
452 if (!addAttribute(m_manager, this, &utf8, data))
460 static BaseRefVectorOf<XMLCh> *
461 decomposeAttributeName(const gss_buffer_t attr)
463 BaseRefVectorOf<XMLCh> *components;
464 string str((const char *)attr->value, attr->length);
465 auto_ptr_XMLCh qualifiedAttr(str.c_str());
467 components = XMLString::tokenizeString(qualifiedAttr.get());
469 if (components->size() != 2) {
478 gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
479 const gss_buffer_t attr,
480 const gss_buffer_t value)
482 saml2::Assertion *assertion;
483 saml2::Attribute *attribute;
484 saml2::AttributeValue *attributeValue;
485 saml2::AttributeStatement *attributeStatement;
487 if (!getAssertion(NULL, &assertion, true))
490 if (assertion->getAttributeStatements().size() != 0) {
491 attributeStatement = assertion->getAttributeStatements().front();
493 attributeStatement = saml2::AttributeStatementBuilder::buildAttributeStatement();
494 assertion->getAttributeStatements().push_back(attributeStatement);
497 /* Check the attribute name consists of name format | whsp | name */
498 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
499 if (components == NULL)
502 attribute = saml2::AttributeBuilder::buildAttribute();
503 attribute->setNameFormat(components->elementAt(0));
504 attribute->setName(components->elementAt(1));
506 attributeValue = saml2::AttributeValueBuilder::buildAttributeValue();
507 auto_ptr_XMLCh unistr((char *)value->value, value->length);
508 attributeValue->setTextContent(unistr.get());
510 attribute->getAttributeValues().push_back(attributeValue);
512 assert(attributeStatement != NULL);
513 attributeStatement->getAttributes().push_back(attribute);
521 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t attr)
523 saml2::Assertion *assertion;
526 if (!getAssertion(NULL, &assertion) ||
527 assertion->getAttributeStatements().size() == 0)
530 /* Check the attribute name consists of name format | whsp | name */
531 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
532 if (components == NULL)
535 /* For each attribute statement, look for an attribute match */
536 const vector<saml2::AttributeStatement *> &statements =
537 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
539 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
540 s != statements.end();
542 const vector<saml2::Attribute *> &attrs =
543 const_cast<const saml2::AttributeStatement *>(*s)->getAttributes();
544 ssize_t index = -1, i = 0;
546 /* There's got to be an easier way to do this */
547 for (vector<saml2::Attribute *>::const_iterator a = attrs.begin();
550 if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
551 XMLString::equals((*a)->getName(), components->elementAt(1))) {
558 (*s)->getAttributes().erase((*s)->getAttributes().begin() + index);
569 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
572 const saml2::Attribute **pAttribute) const
574 saml2::Assertion *assertion;
576 if (authenticated != NULL)
577 *authenticated = false;
578 if (complete != NULL)
582 if (!getAssertion(authenticated, &assertion) ||
583 assertion->getAttributeStatements().size() == 0)
586 /* Check the attribute name consists of name format | whsp | name */
587 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
588 if (components == NULL)
591 /* For each attribute statement, look for an attribute match */
592 const vector <saml2::AttributeStatement *> &statements =
593 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
594 const saml2::Attribute *ret = NULL;
596 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
597 s != statements.end();
599 const vector<saml2::Attribute *> &attrs =
600 const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
602 for (vector<saml2::Attribute *>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
603 const XMLCh *attributeName, *attributeNameFormat;
605 attributeName = (*a)->getName();
606 attributeNameFormat = (*a)->getNameFormat();
607 if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
608 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
610 if (XMLString::equals(attributeNameFormat, components->elementAt(0)) &&
611 XMLString::equals(attributeName, components->elementAt(1))) {
625 return (ret != NULL);
629 isBase64EncodedAttributeValueP(const saml2::AttributeValue *av)
631 const xmltooling::QName *type = av->getSchemaType();
636 if (!type->hasNamespaceURI() ||
637 !XMLString::equals(type->getNamespaceURI(), xmlconstants::XSD_NS))
640 if (!type->hasLocalPart() ||
641 !XMLString::equals(type->getLocalPart(), base64Binary))
648 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
652 gss_buffer_t display_value,
655 const saml2::Attribute *a;
656 const saml2::AttributeValue *av;
657 int nvalues, i = *more;
661 if (!getAttribute(attr, authenticated, complete, &a))
664 nvalues = a->getAttributeValues().size();
671 av = (const saml2::AttributeValue *)((void *)(a->getAttributeValues().at(i)));
673 av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i));
676 bool base64Encoded = isBase64EncodedAttributeValueP(av);
679 char *stringValue = toUTF8(av->getTextContent(), true);
680 size_t stringValueLen = strlen(stringValue);
685 value->value = GSSEAP_MALLOC(stringValueLen);
686 if (value->value == NULL) {
687 GSSEAP_FREE(stringValue);
688 throw new std::bad_alloc;
691 octetLen = base64Decode(stringValue, value->value);
693 GSSEAP_FREE(value->value);
694 GSSEAP_FREE(stringValue);
698 value->length = octetLen;
699 GSSEAP_FREE(stringValue);
701 value->value = stringValue;
702 value->length = stringValueLen;
705 if (display_value != NULL && base64Encoded == false) {
706 display_value->value = toUTF8(av->getTextContent(), true);
707 display_value->length = strlen((char *)value->value);
718 gss_eap_saml_attr_provider::mapToAny(int authenticated GSSEAP_UNUSED,
719 gss_buffer_t type_id GSSEAP_UNUSED) const
721 return (gss_any_t)NULL;
725 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
726 gss_any_t input GSSEAP_UNUSED) const
731 gss_eap_saml_attr_provider::prefix(void) const
733 return "urn:ietf:params:gss-eap:saml-attr";
737 gss_eap_saml_attr_provider::init(void)
739 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML, createAttrContext);
744 gss_eap_saml_attr_provider::finalize(void)
746 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML);
749 gss_eap_attr_provider *
750 gss_eap_saml_attr_provider::createAttrContext(void)
752 return new gss_eap_saml_attr_provider;
756 gssEapSamlAttrProvidersInit(OM_uint32 *minor)
758 if (!gss_eap_saml_assertion_provider::init() ||
759 !gss_eap_saml_attr_provider::init()) {
760 *minor = GSSEAP_SAML_INIT_FAILURE;
761 return GSS_S_FAILURE;
764 return GSS_S_COMPLETE;
768 gssEapSamlAttrProvidersFinalize(OM_uint32 *minor)
770 gss_eap_saml_attr_provider::finalize();
771 gss_eap_saml_assertion_provider::finalize();
774 return GSS_S_COMPLETE;