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_provider is for retrieving the underlying
78 gss_eap_saml_assertion_provider::initFromExistingContext(const gss_eap_attr_ctx *source,
79 const gss_eap_attr_provider *ctx)
81 /* Then we may be creating from an existing attribute context */
82 const gss_eap_saml_assertion_provider *saml;
84 if (!gss_eap_attr_provider::initFromExistingContext(source, ctx))
87 saml = dynamic_cast<const gss_eap_saml_assertion_provider *>(ctx);
88 setAssertion(saml->getAssertion());
92 gss_eap_saml_assertion_provider::initFromGssContext(const gss_eap_attr_ctx *source,
93 const gss_cred_id_t gssCred,
94 const gss_ctx_id_t gssCtx)
96 const gss_eap_radius_attr_provider *radius;
97 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
98 int authenticated, complete, more = -1;
101 if (!gss_eap_attr_provider::initFromGssContext(source, gssCred, gssCtx))
104 radius = dynamic_cast<const gss_eap_radius_attr_provider *>
105 (source->getProvider(ATTR_TYPE_RADIUS));
106 if (radius != NULL &&
107 radius->getAttribute(512, &authenticated, &complete,
108 &value, NULL, &more)) {
109 m_assertion = parseAssertion(&value);
110 gss_release_buffer(&minor, &value);
114 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
120 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion)
124 m_assertion = dynamic_cast<saml2::Assertion*>(assertion->clone());
128 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
130 string str((char *)buffer->value, buffer->length);
131 istringstream istream(str);
133 const XMLObjectBuilder *b;
135 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
136 b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
138 return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
142 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
144 return addAttribute(this, GSS_C_NO_BUFFER, data);
148 gss_eap_saml_assertion_provider::setAttribute(int complete,
149 const gss_buffer_t attr,
150 const gss_buffer_t value)
152 saml2::Assertion *assertion = parseAssertion(value);
154 m_assertion = assertion;
158 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
165 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
169 gss_buffer_t display_value,
174 if (attr->length != 0 || m_assertion == NULL)
181 *authenticated = true;
184 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
186 duplicateBuffer(str, value);
193 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
194 gss_buffer_t type_id) const
196 return (gss_any_t)m_assertion;
200 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
201 gss_any_t input) const
203 delete ((saml2::Assertion *)input);
207 gss_eap_saml_assertion_provider::marshall(gss_buffer_t buffer) const
213 buffer->value = NULL;
215 if (m_assertion == NULL)
218 sink << *m_assertion;
221 duplicateBuffer(str, buffer);
225 gss_eap_saml_assertion_provider::unmarshall(const gss_eap_attr_ctx *ctx,
226 const gss_buffer_t buffer)
228 assert(m_assertion == NULL);
230 m_assertion = parseAssertion(buffer);
232 return (m_assertion != NULL);
236 gss_eap_saml_assertion_provider::init(void)
242 gss_eap_saml_assertion_provider::finalize(void)
246 gss_eap_attr_provider *
247 gss_eap_saml_assertion_provider::createAttrContext(void)
249 return new gss_eap_saml_assertion_provider;
253 * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
255 const saml2::Assertion *
256 gss_eap_saml_attr_provider::getAssertion(void) const
258 const gss_eap_saml_assertion_provider *saml;
260 saml = dynamic_cast<const gss_eap_saml_assertion_provider *>
261 (m_source->getProvider(ATTR_TYPE_SAML_ASSERTION));
263 return saml->getAssertion();
268 gss_eap_saml_attr_provider::~gss_eap_saml_attr_provider(void)
270 /* Nothing to do, we're just a wrapper around the assertion provider. */
274 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
277 const saml2::Assertion *assertion = getAssertion();
279 if (assertion == NULL)
282 const vector<saml2::Attribute*>& attrs2 =
283 const_cast<const saml2::AttributeStatement*>(assertion->getAttributeStatements().front())->getAttributes();
284 for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
288 gss_buffer_desc attribute;
290 attribute.value = (void *)toUTF8((*a)->getName(), true);
291 attribute.length = strlen((char *)attribute.value);
293 if (!addAttribute(this, &attribute, data))
296 delete (char *)attribute.value;
303 gss_eap_saml_attr_provider::setAttribute(int complete,
304 const gss_buffer_t attr,
305 const gss_buffer_t value)
310 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
314 const saml2::Attribute *
315 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr) const
317 const saml2::Assertion *assertion = getAssertion();
318 saml2::AttributeStatement *statement;
320 if (assertion == NULL)
323 if (assertion->getAttributeStatements().size() == 0)
326 statement = assertion->getAttributeStatements().front();
328 auto_ptr_gss_buffer attrname(attr);
330 const vector<saml2::Attribute*>& attrs2 =
331 const_cast<const saml2::AttributeStatement*>(statement)->getAttributes();
333 for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
336 if (XMLString::equals((*a)->getName(), attrname.get()))
344 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
348 gss_buffer_t display_value,
351 const saml2::Attribute *a;
352 const saml2::AttributeValue *av;
353 int nvalues, i = *more;
357 a = getAttribute(attr);
361 nvalues = a->getAttributeValues().size();
365 else if (i >= nvalues)
367 av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
372 *authenticated = TRUE;
375 value->value = toUTF8(av->getTextContent(), true);
376 value->length = strlen((char *)value->value);
385 gss_eap_saml_attr_provider::mapToAny(int authenticated,
386 gss_buffer_t type_id) const
392 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
393 gss_any_t input) const
398 gss_eap_saml_attr_provider::marshall(gss_buffer_t buffer) const
403 gss_eap_saml_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
404 const gss_buffer_t buffer)
410 gss_eap_saml_attr_provider::init(void)
416 gss_eap_saml_attr_provider::finalize(void)
420 gss_eap_attr_provider *
421 gss_eap_saml_attr_provider::createAttrContext(void)
423 return new gss_eap_saml_attr_provider;