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"
37 #include <xercesc/util/XMLUniDefs.hpp>
38 #include <xmltooling/XMLToolingConfig.h>
39 #include <xmltooling/util/XMLHelper.h>
41 #include <saml/saml1/core/Assertions.h>
42 #include <saml/saml2/core/Assertions.h>
43 #include <saml/saml2/metadata/Metadata.h>
45 using namespace xmltooling;
46 using namespace opensaml::saml2md;
47 using namespace opensaml;
48 using namespace xercesc;
52 * gss_eap_saml_assertion_provider is for retrieving the underlying
55 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(void)
58 m_authenticated = false;
61 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
67 gss_eap_saml_assertion_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
68 const gss_eap_attr_provider *ctx)
70 /* Then we may be creating from an existing attribute context */
71 const gss_eap_saml_assertion_provider *saml;
73 assert(m_assertion == NULL);
75 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
78 saml = static_cast<const gss_eap_saml_assertion_provider *>(ctx);
79 setAssertion(saml->getAssertion(), saml->authenticated());
85 gss_eap_saml_assertion_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
86 const gss_cred_id_t gssCred,
87 const gss_ctx_id_t gssCtx)
89 const gss_eap_radius_attr_provider *radius;
90 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
91 int authenticated, complete, more = -1;
94 assert(m_assertion == NULL);
96 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
99 radius = static_cast<const gss_eap_radius_attr_provider *>
100 (m_manager->getProvider(ATTR_TYPE_RADIUS));
101 if (radius != NULL &&
102 radius->getAttribute(PW_SAML_ASSERTION, &authenticated, &complete,
103 &value, NULL, &more)) {
104 setAssertion(&value, authenticated);
105 gss_release_buffer(&minor, &value);
114 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion,
120 if (assertion != NULL) {
121 m_assertion = dynamic_cast<saml2::Assertion *>(assertion->clone());
122 m_authenticated = authenticated;
125 m_authenticated = false;
130 gss_eap_saml_assertion_provider::setAssertion(const gss_buffer_t buffer,
135 m_assertion = parseAssertion(buffer);
136 m_authenticated = (m_assertion != NULL && authenticated);
140 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
142 string str((char *)buffer->value, buffer->length);
143 istringstream istream(str);
145 const XMLObjectBuilder *b;
147 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
148 b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
150 return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
154 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
159 /* just add the prefix */
160 if (m_assertion != NULL)
161 ret = addAttribute(this, GSS_C_NO_BUFFER, data);
169 gss_eap_saml_assertion_provider::setAttribute(int complete,
170 const gss_buffer_t attr,
171 const gss_buffer_t value)
173 if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
179 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
183 m_authenticated = false;
187 gss_eap_saml_assertion_provider::getExpiryTime(void) const
189 saml2::Conditions *conditions;
190 time_t expiryTime = 0;
192 if (m_assertion == NULL)
195 conditions = m_assertion->getConditions();
197 if (conditions != NULL && conditions->getNotOnOrAfter() != NULL)
198 expiryTime = conditions->getNotOnOrAfter()->getEpoch();
204 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
208 gss_buffer_t display_value,
213 if (attr != GSS_C_NO_BUFFER || attr->length != 0)
216 if (m_assertion == NULL)
222 if (authenticated != NULL)
223 *authenticated = m_authenticated;
224 if (complete != NULL)
227 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
229 duplicateBuffer(str, value);
236 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
237 gss_buffer_t type_id) const
239 return (gss_any_t)m_assertion;
243 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
244 gss_any_t input) const
246 delete ((saml2::Assertion *)input);
250 gss_eap_saml_assertion_provider::exportToBuffer(gss_buffer_t buffer) const
256 buffer->value = NULL;
258 if (m_assertion == NULL)
261 sink << *m_assertion;
264 duplicateBuffer(str, buffer);
268 gss_eap_saml_assertion_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
269 const gss_buffer_t buffer)
271 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
274 if (buffer->length == 0)
277 assert(m_assertion == NULL);
279 setAssertion(buffer);
280 /* TODO XXX how to propagate authenticated flag? */
286 gss_eap_saml_assertion_provider::init(void)
288 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION,
289 "urn:ietf:params:gss-eap:saml-aaa-assertion",
290 gss_eap_saml_assertion_provider::createAttrContext);
295 gss_eap_saml_assertion_provider::finalize(void)
297 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML_ASSERTION);
300 gss_eap_attr_provider *
301 gss_eap_saml_assertion_provider::createAttrContext(void)
303 return new gss_eap_saml_assertion_provider;
307 * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
310 gss_eap_saml_attr_provider::getAssertion(int *authenticated,
311 const saml2::Assertion **pAssertion) const
313 const gss_eap_saml_assertion_provider *saml;
315 if (authenticated != NULL)
316 *authenticated = false;
317 if (pAssertion != NULL)
320 saml = static_cast<const gss_eap_saml_assertion_provider *>
321 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
325 if (authenticated != NULL)
326 *authenticated = saml->authenticated();
327 if (pAssertion != NULL)
328 *pAssertion = saml->getAssertion();
330 return (saml->getAssertion() != NULL);
334 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
337 const saml2::Assertion *assertion;
341 if (!getAssertion(&authenticated, &assertion))
345 * Note: the first prefix is added by the attribute provider manager
347 * From draft-hartman-gss-eap-naming-00:
349 * Each attribute carried in the assertion SHOULD also be a GSS name
350 * attribute. The name of this attribute has three parts, all separated
351 * by an ASCII space character. The first part is
352 * urn:ietf:params:gss-eap:saml-attr. The second part is the URI for
353 * the SAML attribute name format. The final part is the name of the
354 * SAML attribute. If the mechanism performs an additional attribute
355 * query, the retrieved attributes SHOULD be GSS-API name attributes
356 * using the same name syntax.
358 const vector<saml2::Attribute*>& attrs2 =
359 const_cast<const saml2::AttributeStatement*>(assertion->getAttributeStatements().front())->getAttributes();
360 for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
364 const XMLCh *attributeName = (*a)->getName();
365 const XMLCh *attributeNameFormat = (*a)->getNameFormat();
366 XMLCh *qualifiedName;
367 XMLCh space[2] = { ' ', 0 };
368 gss_buffer_desc utf8;
370 qualifiedName = new XMLCh[XMLString::stringLen(attributeName) + 1 +
371 XMLString::stringLen(attributeNameFormat) + 1];
372 XMLString::copyString(qualifiedName, attributeName);
373 XMLString::catString(qualifiedName, space);
374 XMLString::catString(qualifiedName, attributeNameFormat);
376 utf8.value = (void *)toUTF8(qualifiedName);
377 utf8.length = strlen((char *)utf8.value);
379 ret = addAttribute(this, &utf8, data);
381 delete qualifiedName;
391 gss_eap_saml_attr_provider::setAttribute(int complete,
392 const gss_buffer_t attr,
393 const gss_buffer_t value)
398 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
402 static BaseRefVectorOf<XMLCh> *
403 decomposeAttributeName(const gss_buffer_t attr)
405 XMLCh *qualifiedAttr = new XMLCh[attr->length + 1];
406 XMLString::transcode((const char *)attr->value, qualifiedAttr, attr->length);
408 BaseRefVectorOf<XMLCh> *components = XMLString::tokenizeString(qualifiedAttr);
410 delete qualifiedAttr;
416 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
419 const saml2::Attribute **pAttribute) const
421 const saml2::Assertion *assertion;
423 if (authenticated != NULL)
424 *authenticated = false;
425 if (complete != NULL)
429 if (!getAssertion(authenticated, &assertion) ||
430 assertion->getAttributeStatements().size() == 0)
433 /* Check the attribute name consists of name format | whsp | name */
434 BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
435 if (components == NULL || components->size() != 2) {
440 /* For each attribute statement, look for an attribute match */
441 const vector <saml2::AttributeStatement *>&statements =
442 assertion->getAttributeStatements();
443 const saml2::Attribute *ret = NULL;
445 for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
446 s != statements.end();
448 const vector<saml2::Attribute*>& attrs =
449 const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
451 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
452 if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
453 XMLString::equals((*a)->getName(), components->elementAt(1))) {
467 return (ret != NULL);
471 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
475 gss_buffer_t display_value,
478 const saml2::Attribute *a;
479 const saml2::AttributeValue *av;
480 int nvalues, i = *more;
484 if (!getAttribute(attr, authenticated, complete, &a))
487 nvalues = a->getAttributeValues().size();
491 else if (i >= nvalues)
493 av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
497 value->value = toUTF8(av->getTextContent(), true);
498 value->length = strlen((char *)value->value);
500 if (display_value != NULL) {
501 display_value->value = toUTF8(av->getTextContent(), true);
502 display_value->length = strlen((char *)value->value);
513 gss_eap_saml_attr_provider::mapToAny(int authenticated,
514 gss_buffer_t type_id) const
516 return (gss_any_t)NULL;
520 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
521 gss_any_t input) const
526 gss_eap_saml_attr_provider::exportToBuffer(gss_buffer_t buffer) const
529 buffer->value = NULL;
533 gss_eap_saml_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
534 const gss_buffer_t buffer)
536 return gss_eap_attr_provider::initFromBuffer(ctx, buffer);
540 gss_eap_saml_attr_provider::init(void)
542 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML,
543 "urn:ietf:params:gss-eap:saml-attr",
544 gss_eap_saml_attr_provider::createAttrContext);
549 gss_eap_saml_attr_provider::finalize(void)
551 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML);
554 gss_eap_attr_provider *
555 gss_eap_saml_attr_provider::createAttrContext(void)
557 return new gss_eap_saml_attr_provider;