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 base64Binary[] = {'b','a','s','e','6','4','B','i','n','a','r','y',0};
64 * gss_eap_saml_assertion_provider is for retrieving the underlying
67 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(void)
70 m_authenticated = false;
73 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
79 gss_eap_saml_assertion_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
80 const gss_eap_attr_provider *ctx)
82 /* Then we may be creating from an existing attribute context */
83 const gss_eap_saml_assertion_provider *saml;
85 assert(m_assertion == NULL);
87 if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx))
90 saml = static_cast<const gss_eap_saml_assertion_provider *>(ctx);
91 setAssertion(saml->getAssertion(), saml->authenticated());
97 gss_eap_saml_assertion_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
98 const gss_cred_id_t gssCred,
99 const gss_ctx_id_t gssCtx)
101 const gss_eap_radius_attr_provider *radius;
102 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
103 int authenticated, complete;
106 assert(m_assertion == NULL);
108 if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
112 * XXX TODO we need to support draft-howlett-radius-saml-attr-00
114 radius = static_cast<const gss_eap_radius_attr_provider *>
115 (m_manager->getProvider(ATTR_TYPE_RADIUS));
116 if (radius != NULL &&
117 radius->getFragmentedAttribute(PW_SAML_AAA_ASSERTION,
119 &authenticated, &complete, &value)) {
120 setAssertion(&value, authenticated);
121 gss_release_buffer(&minor, &value);
130 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion,
136 if (assertion != NULL) {
138 m_assertion = (saml2::Assertion *)((void *)assertion->clone());
140 m_assertion = dynamic_cast<saml2::Assertion *>(assertion->clone());
142 m_authenticated = authenticated;
145 m_authenticated = false;
150 gss_eap_saml_assertion_provider::setAssertion(const gss_buffer_t buffer,
155 m_assertion = parseAssertion(buffer);
156 m_authenticated = (m_assertion != NULL && authenticated);
160 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
162 string str((char *)buffer->value, buffer->length);
163 istringstream istream(str);
165 const XMLObjectBuilder *b;
168 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
172 b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
175 return (saml2::Assertion *)((void *)b->buildFromDocument(doc));
177 return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
179 } catch (exception &e) {
185 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
190 /* just add the prefix */
191 if (m_assertion != NULL)
192 ret = addAttribute(m_manager, this, GSS_C_NO_BUFFER, data);
200 gss_eap_saml_assertion_provider::setAttribute(int complete GSSEAP_UNUSED,
201 const gss_buffer_t attr,
202 const gss_buffer_t value)
204 if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
213 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value GSSEAP_UNUSED)
217 m_authenticated = false;
223 gss_eap_saml_assertion_provider::getExpiryTime(void) const
225 saml2::Conditions *conditions;
226 time_t expiryTime = 0;
228 if (m_assertion == NULL)
231 conditions = m_assertion->getConditions();
233 if (conditions != NULL && conditions->getNotOnOrAfter() != NULL)
234 expiryTime = conditions->getNotOnOrAfter()->getEpoch();
240 gss_eap_saml_assertion_provider::mapException(OM_uint32 *minor,
241 std::exception &e) const
243 if (typeid(e) == typeid(SecurityPolicyException))
244 *minor = GSSEAP_SAML_SEC_POLICY_FAILURE;
245 else if (typeid(e) == typeid(BindingException))
246 *minor = GSSEAP_SAML_BINDING_FAILURE;
247 else if (typeid(e) == typeid(ProfileException))
248 *minor = GSSEAP_SAML_PROFILE_FAILURE;
249 else if (typeid(e) == typeid(FatalProfileException))
250 *minor = GSSEAP_SAML_FATAL_PROFILE_FAILURE;
251 else if (typeid(e) == typeid(RetryableProfileException))
252 *minor = GSSEAP_SAML_RETRY_PROFILE_FAILURE;
253 else if (typeid(e) == typeid(MetadataException))
254 *minor = GSSEAP_SAML_METADATA_FAILURE;
256 return GSS_S_CONTINUE_NEEDED;
258 gssEapSaveStatusInfo(*minor, "%s", e.what());
260 return GSS_S_FAILURE;
264 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
268 gss_buffer_t display_value GSSEAP_UNUSED,
273 if (attr != GSS_C_NO_BUFFER && attr->length != 0)
276 if (m_assertion == NULL)
282 if (authenticated != NULL)
283 *authenticated = m_authenticated;
284 if (complete != NULL)
287 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
290 duplicateBuffer(str, value);
291 if (display_value != NULL)
292 duplicateBuffer(str, display_value);
300 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
301 gss_buffer_t type_id GSSEAP_UNUSED) const
303 if (authenticated && !m_authenticated)
304 return (gss_any_t)NULL;
306 return (gss_any_t)m_assertion;
310 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
311 gss_any_t input) const
313 delete ((saml2::Assertion *)input);
317 gss_eap_saml_assertion_provider::prefix(void) const
319 return "urn:ietf:params:gss-eap:saml-aaa-assertion";
323 gss_eap_saml_assertion_provider::init(void)
325 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION, createAttrContext);
330 gss_eap_saml_assertion_provider::finalize(void)
332 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML_ASSERTION);
335 gss_eap_attr_provider *
336 gss_eap_saml_assertion_provider::createAttrContext(void)
338 return new gss_eap_saml_assertion_provider;
342 gss_eap_saml_assertion_provider::initAssertion(void)
345 m_assertion = saml2::AssertionBuilder::buildAssertion();
346 m_authenticated = false;
352 * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
355 gss_eap_saml_attr_provider::getAssertion(int *authenticated,
356 saml2::Assertion **pAssertion,
357 bool createIfAbsent) const
359 gss_eap_saml_assertion_provider *saml;
361 if (authenticated != NULL)
362 *authenticated = false;
363 if (pAssertion != NULL)
366 saml = static_cast<const gss_eap_saml_assertion_provider *>
367 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
371 if (authenticated != NULL)
372 *authenticated = saml->authenticated();
373 if (pAssertion != NULL)
374 *pAssertion = saml->getAssertion();
376 if (saml->getAssertion() == NULL) {
377 if (createIfAbsent) {
378 if (authenticated != NULL)
379 *authenticated = false;
380 if (pAssertion != NULL)
381 *pAssertion = saml->initAssertion();
390 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
393 saml2::Assertion *assertion;
396 if (!getAssertion(&authenticated, &assertion))
400 * Note: the first prefix is added by the attribute provider manager
402 * From draft-hartman-gss-eap-naming-00:
404 * Each attribute carried in the assertion SHOULD also be a GSS name
405 * attribute. The name of this attribute has three parts, all separated
406 * by an ASCII space character. The first part is
407 * urn:ietf:params:gss-eap:saml-attr. The second part is the URI for
408 * the SAML attribute name format. The final part is the name of the
409 * SAML attribute. If the mechanism performs an additional attribute
410 * query, the retrieved attributes SHOULD be GSS-API name attributes
411 * using the same name syntax.
413 /* For each attribute statement, look for an attribute match */
414 const vector <saml2::AttributeStatement *> &statements =
415 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
417 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
418 s != statements.end();
420 const vector<saml2::Attribute*> &attrs =
421 const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
423 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
424 const XMLCh *attributeName, *attributeNameFormat;
425 XMLCh space[2] = { ' ', 0 };
426 gss_buffer_desc utf8;
428 attributeName = (*a)->getName();
429 attributeNameFormat = (*a)->getNameFormat();
430 if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
431 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
433 XMLCh qualifiedName[XMLString::stringLen(attributeNameFormat) + 1 +
434 XMLString::stringLen(attributeName) + 1];
435 XMLString::copyString(qualifiedName, attributeNameFormat);
436 XMLString::catString(qualifiedName, space);
437 XMLString::catString(qualifiedName, attributeName);
439 utf8.value = (void *)toUTF8(qualifiedName);
440 utf8.length = strlen((char *)utf8.value);
442 if (!addAttribute(m_manager, this, &utf8, data))
450 static BaseRefVectorOf<XMLCh> *
451 decomposeAttributeName(const gss_buffer_t attr)
453 BaseRefVectorOf<XMLCh> *components;
454 string str((const char *)attr->value, attr->length);
455 auto_ptr_XMLCh qualifiedAttr(str.c_str());
457 components = XMLString::tokenizeString(qualifiedAttr.get());
459 if (components->size() != 2) {
468 gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
469 const gss_buffer_t attr,
470 const gss_buffer_t value)
472 saml2::Assertion *assertion;
473 saml2::Attribute *attribute;
474 saml2::AttributeValue *attributeValue;
475 saml2::AttributeStatement *attributeStatement;
477 if (!getAssertion(NULL, &assertion, true))
480 if (assertion->getAttributeStatements().size() != 0) {
481 attributeStatement = assertion->getAttributeStatements().front();
483 attributeStatement = saml2::AttributeStatementBuilder::buildAttributeStatement();
484 assertion->getAttributeStatements().push_back(attributeStatement);
487 /* Check the attribute name consists of name format | whsp | name */
488 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
489 if (components == NULL)
492 attribute = saml2::AttributeBuilder::buildAttribute();
493 attribute->setNameFormat(components->elementAt(0));
494 attribute->setName(components->elementAt(1));
496 attributeValue = saml2::AttributeValueBuilder::buildAttributeValue();
497 auto_ptr_XMLCh unistr((char *)value->value, value->length);
498 attributeValue->setTextContent(unistr.get());
500 attribute->getAttributeValues().push_back(attributeValue);
502 assert(attributeStatement != NULL);
503 attributeStatement->getAttributes().push_back(attribute);
511 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t attr)
513 saml2::Assertion *assertion;
516 if (!getAssertion(NULL, &assertion) ||
517 assertion->getAttributeStatements().size() == 0)
520 /* Check the attribute name consists of name format | whsp | name */
521 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
522 if (components == NULL)
525 /* For each attribute statement, look for an attribute match */
526 const vector<saml2::AttributeStatement *> &statements =
527 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
529 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
530 s != statements.end();
532 const vector<saml2::Attribute *> &attrs =
533 const_cast<const saml2::AttributeStatement *>(*s)->getAttributes();
534 ssize_t index = -1, i = 0;
536 /* There's got to be an easier way to do this */
537 for (vector<saml2::Attribute *>::const_iterator a = attrs.begin();
540 if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
541 XMLString::equals((*a)->getName(), components->elementAt(1))) {
548 (*s)->getAttributes().erase((*s)->getAttributes().begin() + index);
559 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
562 const saml2::Attribute **pAttribute) const
564 saml2::Assertion *assertion;
566 if (authenticated != NULL)
567 *authenticated = false;
568 if (complete != NULL)
572 if (!getAssertion(authenticated, &assertion) ||
573 assertion->getAttributeStatements().size() == 0)
576 /* Check the attribute name consists of name format | whsp | name */
577 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
578 if (components == NULL)
581 /* For each attribute statement, look for an attribute match */
582 const vector <saml2::AttributeStatement *> &statements =
583 const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
584 const saml2::Attribute *ret = NULL;
586 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
587 s != statements.end();
589 const vector<saml2::Attribute *> &attrs =
590 const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
592 for (vector<saml2::Attribute *>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
593 const XMLCh *attributeName, *attributeNameFormat;
595 attributeName = (*a)->getName();
596 attributeNameFormat = (*a)->getNameFormat();
597 if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
598 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
600 if (XMLString::equals(attributeNameFormat, components->elementAt(0)) &&
601 XMLString::equals(attributeName, components->elementAt(1))) {
615 return (ret != NULL);
619 isBase64EncodedAttributeValueP(const saml2::AttributeValue *av)
621 const xmltooling::QName *type = av->getSchemaType();
626 if (!type->hasNamespaceURI() ||
627 !XMLString::equals(type->getNamespaceURI(), xmlconstants::XSD_NS))
630 if (!type->hasLocalPart() ||
631 !XMLString::equals(type->getLocalPart(), base64Binary))
638 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
642 gss_buffer_t display_value,
645 const saml2::Attribute *a;
646 const saml2::AttributeValue *av;
647 int nvalues, i = *more;
651 if (!getAttribute(attr, authenticated, complete, &a))
654 nvalues = a->getAttributeValues().size();
661 av = (const saml2::AttributeValue *)((void *)(a->getAttributeValues().at(i)));
663 av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i));
666 bool base64Encoded = isBase64EncodedAttributeValueP(av);
669 char *stringValue = toUTF8(av->getTextContent(), true);
670 size_t stringValueLen = strlen(stringValue);
675 value->value = GSSEAP_MALLOC(stringValueLen);
676 if (value->value == NULL) {
677 GSSEAP_FREE(stringValue);
678 throw new std::bad_alloc;
681 octetLen = base64Decode(stringValue, value->value);
683 GSSEAP_FREE(value->value);
684 GSSEAP_FREE(stringValue);
688 value->length = octetLen;
689 GSSEAP_FREE(stringValue);
691 value->value = stringValue;
692 value->length = stringValueLen;
695 if (display_value != NULL && base64Encoded == false) {
696 display_value->value = toUTF8(av->getTextContent(), true);
697 display_value->length = strlen((char *)value->value);
708 gss_eap_saml_attr_provider::mapToAny(int authenticated GSSEAP_UNUSED,
709 gss_buffer_t type_id GSSEAP_UNUSED) const
711 return (gss_any_t)NULL;
715 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
716 gss_any_t input GSSEAP_UNUSED) const
721 gss_eap_saml_attr_provider::prefix(void) const
723 return "urn:ietf:params:gss-eap:saml-attr";
727 gss_eap_saml_attr_provider::init(void)
729 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML, createAttrContext);
734 gss_eap_saml_attr_provider::finalize(void)
736 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML);
739 gss_eap_attr_provider *
740 gss_eap_saml_attr_provider::createAttrContext(void)
742 return new gss_eap_saml_attr_provider;
746 gssEapSamlAttrProvidersInit(OM_uint32 *minor)
748 if (!gss_eap_saml_assertion_provider::init() ||
749 !gss_eap_saml_attr_provider::init()) {
750 *minor = GSSEAP_SAML_INIT_FAILURE;
751 return GSS_S_FAILURE;
754 return GSS_S_COMPLETE;
758 gssEapSamlAttrProvidersFinalize(OM_uint32 *minor)
760 gss_eap_saml_attr_provider::finalize();
761 gss_eap_saml_assertion_provider::finalize();
764 return GSS_S_COMPLETE;