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;
51 class auto_ptr_gss_buffer {
52 MAKE_NONCOPYABLE(auto_ptr_gss_buffer);
54 auto_ptr_gss_buffer() : m_buf(NULL) {
56 auto_ptr_gss_buffer(const gss_buffer_t src) {
57 m_buf = new XMLCh[src->length + 1];
58 XMLString::transcode((const char *)src->value, m_buf, src->length);
60 ~auto_ptr_gss_buffer() {
61 xercesc::XMLString::release(&m_buf);
63 const XMLCh* get() const {
67 XMLCh *temp = m_buf; m_buf = NULL; return temp;
74 * gss_eap_saml_assertion_source is for retrieving the underlying
78 gss_eap_saml_assertion_source::initFromExistingContext(const gss_eap_attr_ctx *source,
79 const gss_eap_attr_source *ctx)
81 /* Then we may be creating from an existing attribute context */
82 const gss_eap_saml_assertion_source *saml;
84 assert(m_assertion == NULL);
86 if (!gss_eap_attr_source::initFromExistingContext(source, ctx))
89 saml = dynamic_cast<const gss_eap_saml_assertion_source *>(ctx);
90 setAssertion(saml->getAssertion());
96 gss_eap_saml_assertion_source::initFromGssContext(const gss_eap_attr_ctx *source,
97 const gss_cred_id_t gssCred,
98 const gss_ctx_id_t gssCtx)
100 const gss_eap_radius_attr_source *radius;
101 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
102 int authenticated, complete, more = -1;
105 assert(m_assertion == NULL);
107 if (!gss_eap_attr_source::initFromGssContext(source, gssCred, gssCtx))
110 radius = dynamic_cast<const gss_eap_radius_attr_source *>
111 (m_manager->getProvider(ATTR_TYPE_RADIUS));
112 if (radius != NULL &&
113 radius->getAttribute(512 /* XXX */, &authenticated, &complete,
114 &value, NULL, &more)) {
115 m_assertion = parseAssertion(&value);
116 gss_release_buffer(&minor, &value);
124 gss_eap_saml_assertion_source::~gss_eap_saml_assertion_source(void)
130 gss_eap_saml_assertion_source::setAssertion(const saml2::Assertion *assertion)
135 if (assertion != NULL)
136 m_assertion = dynamic_cast<saml2::Assertion*>(assertion->clone());
142 gss_eap_saml_assertion_source::parseAssertion(const gss_buffer_t buffer)
144 string str((char *)buffer->value, buffer->length);
145 istringstream istream(str);
147 const XMLObjectBuilder *b;
149 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
150 b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
152 return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
156 gss_eap_saml_assertion_source::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
159 /* just add the prefix */
160 return addAttribute(this, GSS_C_NO_BUFFER, data);
164 gss_eap_saml_assertion_source::setAttribute(int complete,
165 const gss_buffer_t attr,
166 const gss_buffer_t value)
168 if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
169 saml2::Assertion *assertion = parseAssertion(value);
172 m_assertion = assertion;
177 gss_eap_saml_assertion_source::deleteAttribute(const gss_buffer_t value)
184 gss_eap_saml_assertion_source::getAttribute(const gss_buffer_t attr,
188 gss_buffer_t display_value,
193 if (attr != GSS_C_NO_BUFFER || attr->length != 0)
196 if (m_assertion == NULL)
202 *authenticated = true;
205 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
207 duplicateBuffer(str, value);
214 gss_eap_saml_assertion_source::mapToAny(int authenticated,
215 gss_buffer_t type_id) const
217 return (gss_any_t)m_assertion;
221 gss_eap_saml_assertion_source::releaseAnyNameMapping(gss_buffer_t type_id,
222 gss_any_t input) const
224 delete ((saml2::Assertion *)input);
228 gss_eap_saml_assertion_source::exportToBuffer(gss_buffer_t buffer) const
234 buffer->value = NULL;
236 if (m_assertion == NULL)
239 sink << *m_assertion;
242 duplicateBuffer(str, buffer);
246 gss_eap_saml_assertion_source::initFromBuffer(const gss_eap_attr_ctx *ctx,
247 const gss_buffer_t buffer)
249 if (!gss_eap_attr_source::initFromBuffer(ctx, buffer))
252 assert(m_assertion == NULL);
254 if (buffer->length == 0)
257 m_assertion = parseAssertion(buffer);
258 return (m_assertion != NULL);
262 gss_eap_saml_assertion_source::init(void)
268 gss_eap_saml_assertion_source::finalize(void)
272 gss_eap_attr_source *
273 gss_eap_saml_assertion_source::createAttrContext(void)
275 return new gss_eap_saml_assertion_source;
279 * gss_eap_saml_attr_source is for retrieving the underlying attributes.
281 const saml2::Assertion *
282 gss_eap_saml_attr_source::getAssertion(void) const
284 const gss_eap_saml_assertion_source *saml;
286 saml = dynamic_cast<const gss_eap_saml_assertion_source *>
287 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
289 return saml->getAssertion();
294 gss_eap_saml_attr_source::~gss_eap_saml_attr_source(void)
296 /* Nothing to do, we're just a wrapper around the assertion provider. */
300 gss_eap_saml_attr_source::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
303 const saml2::Assertion *assertion = getAssertion();
305 if (assertion == NULL)
308 const vector<saml2::Attribute*>& attrs2 =
309 const_cast<const saml2::AttributeStatement*>(assertion->getAttributeStatements().front())->getAttributes();
310 for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
314 gss_buffer_desc attribute;
316 attribute.value = (void *)toUTF8((*a)->getName(), true);
317 attribute.length = strlen((char *)attribute.value);
319 if (!addAttribute(this, &attribute, data))
322 delete (char *)attribute.value;
329 gss_eap_saml_attr_source::setAttribute(int complete,
330 const gss_buffer_t attr,
331 const gss_buffer_t value)
336 gss_eap_saml_attr_source::deleteAttribute(const gss_buffer_t value)
340 const saml2::Attribute *
341 gss_eap_saml_attr_source::getAttribute(const gss_buffer_t attr) const
343 const saml2::Assertion *assertion = getAssertion();
344 saml2::AttributeStatement *statement;
346 if (assertion == NULL)
349 if (assertion->getAttributeStatements().size() == 0)
352 statement = assertion->getAttributeStatements().front();
354 auto_ptr_gss_buffer attrname(attr);
356 const vector<saml2::Attribute*>& attrs2 =
357 const_cast<const saml2::AttributeStatement*>(statement)->getAttributes();
359 for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
362 if (XMLString::equals((*a)->getName(), attrname.get()))
370 gss_eap_saml_attr_source::getAttribute(const gss_buffer_t attr,
374 gss_buffer_t display_value,
377 const saml2::Attribute *a;
378 const saml2::AttributeValue *av;
379 int nvalues, i = *more;
383 a = getAttribute(attr);
387 nvalues = a->getAttributeValues().size();
391 else if (i >= nvalues)
393 av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
398 *authenticated = TRUE;
401 value->value = toUTF8(av->getTextContent(), true);
402 value->length = strlen((char *)value->value);
411 gss_eap_saml_attr_source::mapToAny(int authenticated,
412 gss_buffer_t type_id) const
414 return (gss_any_t)NULL;
418 gss_eap_saml_attr_source::releaseAnyNameMapping(gss_buffer_t type_id,
419 gss_any_t input) const
424 gss_eap_saml_attr_source::exportToBuffer(gss_buffer_t buffer) const
427 buffer->value = NULL;
431 gss_eap_saml_attr_source::initFromBuffer(const gss_eap_attr_ctx *ctx,
432 const gss_buffer_t buffer)
438 gss_eap_saml_attr_source::init(void)
444 gss_eap_saml_attr_source::finalize(void)
448 gss_eap_attr_source *
449 gss_eap_saml_attr_source::createAttrContext(void)
451 return new gss_eap_saml_attr_source;