cleanup
[mech_eap.git] / util_saml.cpp
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 #include "gssapiP_eap.h"
34
35 #include <sstream>
36
37 #include <xercesc/util/XMLUniDefs.hpp>
38 #include <xmltooling/XMLToolingConfig.h> 
39 #include <xmltooling/util/XMLHelper.h>
40
41 #include <saml/saml1/core/Assertions.h> 
42 #include <saml/saml2/core/Assertions.h>
43 #include <saml/saml2/metadata/Metadata.h>
44
45 using namespace xmltooling;
46 using namespace opensaml::saml2md;
47 using namespace opensaml;
48 using namespace xercesc;
49 using namespace std;
50
51 class auto_ptr_gss_buffer {
52     MAKE_NONCOPYABLE(auto_ptr_gss_buffer);
53     public:
54         auto_ptr_gss_buffer() : m_buf(NULL) {
55         }
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);
59         }
60         ~auto_ptr_gss_buffer() {
61             xercesc::XMLString::release(&m_buf);
62         }
63         const XMLCh* get() const {
64             return m_buf;
65         }
66         XMLCh* release() {
67             XMLCh *temp = m_buf; m_buf = NULL; return temp;
68         }
69     private:
70         XMLCh *m_buf;
71 };
72
73 /*
74  * gss_eap_saml_assertion_provider is for retrieving the underlying
75  * assertion.
76  */
77 bool
78 gss_eap_saml_assertion_provider::initFromExistingContext(const gss_eap_attr_ctx *source,
79                                                          const gss_eap_attr_provider *ctx)
80 {
81     /* Then we may be creating from an existing attribute context */
82     const gss_eap_saml_assertion_provider *saml;
83
84     if (!gss_eap_attr_provider::initFromExistingContext(source, ctx))
85         return false;
86
87     saml = dynamic_cast<const gss_eap_saml_assertion_provider *>(ctx);
88     setAssertion(saml->getAssertion());
89 }
90
91 bool
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)
95 {
96     const gss_eap_radius_attr_provider *radius;
97     gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
98     int authenticated, complete, more = -1;
99     OM_uint32 minor;
100
101     if (!gss_eap_attr_provider::initFromGssContext(source, gssCred, gssCtx))
102         return false;
103
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);
111     }
112 }
113
114 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
115 {
116     delete m_assertion;
117 }
118
119 void
120 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion)
121 {
122
123     delete m_assertion;
124     m_assertion = dynamic_cast<saml2::Assertion*>(assertion->clone());
125 }
126
127 saml2::Assertion *
128 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
129 {
130     string str((char *)buffer->value, buffer->length);
131     istringstream istream(str);
132     DOMDocument *doc;
133     const XMLObjectBuilder *b;
134
135     doc = XMLToolingConfig::getConfig().getParser().parse(istream);
136     b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
137
138     return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
139 }
140
141 bool
142 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
143 {
144     return addAttribute(this, GSS_C_NO_BUFFER, data);
145 }
146
147 void
148 gss_eap_saml_assertion_provider::setAttribute(int complete,
149                                               const gss_buffer_t attr,
150                                               const gss_buffer_t value)
151 {
152     saml2::Assertion *assertion = parseAssertion(value);
153
154     m_assertion = assertion;
155 }
156
157 void
158 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
159 {
160     delete m_assertion;
161     m_assertion = NULL;
162 }
163
164 bool
165 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
166                                               int *authenticated,
167                                               int *complete,
168                                               gss_buffer_t value,
169                                               gss_buffer_t display_value,
170                                               int *more) const
171 {
172     string str;
173
174     if (attr->length != 0 || m_assertion == NULL)
175         return false;
176
177     if (*more == -1)
178         *more = 0;
179
180     if (*more == 0) {
181         *authenticated = true;
182         *complete = false;
183
184         XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
185
186         duplicateBuffer(str, value);
187     }
188
189     return true;
190 }
191
192 gss_any_t
193 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
194                                           gss_buffer_t type_id) const
195 {
196     return (gss_any_t)m_assertion;
197 }
198
199 void
200 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
201                                                        gss_any_t input) const
202 {
203     delete ((saml2::Assertion *)input);
204 }
205
206 void
207 gss_eap_saml_assertion_provider::marshall(gss_buffer_t buffer) const
208 {
209     ostringstream sink;
210     string str;
211
212     buffer->length = 0;
213     buffer->value = NULL;
214
215     if (m_assertion == NULL)
216         return;
217
218     sink << *m_assertion;
219     str = sink.str();
220
221     duplicateBuffer(str, buffer);
222 }
223
224 bool
225 gss_eap_saml_assertion_provider::unmarshall(const gss_eap_attr_ctx *ctx,
226                                             const gss_buffer_t buffer)
227 {
228     assert(m_assertion == NULL);
229
230     m_assertion = parseAssertion(buffer);
231
232     return (m_assertion != NULL);
233 }
234
235 bool
236 gss_eap_saml_assertion_provider::init(void)
237 {
238     return true;
239 }
240
241 void
242 gss_eap_saml_assertion_provider::finalize(void)
243 {
244 }
245
246 gss_eap_attr_provider *
247 gss_eap_saml_assertion_provider::createAttrContext(void)
248 {
249     return new gss_eap_saml_assertion_provider;
250 }
251
252 /*
253  * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
254  */
255 const saml2::Assertion *
256 gss_eap_saml_attr_provider::getAssertion(void) const
257 {
258     const gss_eap_saml_assertion_provider *saml;
259     
260     saml = dynamic_cast<const gss_eap_saml_assertion_provider *>
261         (m_source->getProvider(ATTR_TYPE_SAML_ASSERTION));
262     if (saml != NULL)
263         return saml->getAssertion();
264
265     return NULL;
266 }
267
268 gss_eap_saml_attr_provider::~gss_eap_saml_attr_provider(void)
269 {
270     /* Nothing to do, we're just a wrapper around the assertion provider. */
271 }
272
273 bool
274 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
275                                               void *data) const
276 {
277     const saml2::Assertion *assertion = getAssertion();
278
279     if (assertion == NULL)
280         return true;
281
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();
285         a != attrs2.end();
286         ++a)
287     {
288         gss_buffer_desc attribute;
289
290         attribute.value = (void *)toUTF8((*a)->getName(), true);
291         attribute.length = strlen((char *)attribute.value);
292
293         if (!addAttribute(this, &attribute, data))
294             return false;
295
296         delete (char *)attribute.value;
297     }
298
299     return true;
300 }
301
302 void
303 gss_eap_saml_attr_provider::setAttribute(int complete,
304                                          const gss_buffer_t attr,
305                                          const gss_buffer_t value)
306 {
307 }
308
309 void
310 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
311 {
312 }
313
314 const saml2::Attribute *
315 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr) const
316 {
317     const saml2::Assertion *assertion = getAssertion();
318     saml2::AttributeStatement *statement;
319
320     if (assertion == NULL)
321         return NULL;
322
323     if (assertion->getAttributeStatements().size() == 0)
324         return NULL;
325
326     statement = assertion->getAttributeStatements().front();
327
328     auto_ptr_gss_buffer attrname(attr);
329
330     const vector<saml2::Attribute*>& attrs2 =
331         const_cast<const saml2::AttributeStatement*>(statement)->getAttributes();
332
333     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
334         a != attrs2.end();
335         ++a) {
336         if (XMLString::equals((*a)->getName(), attrname.get()))
337             return *a;
338     }
339
340     return NULL;
341 }
342
343 bool
344 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
345                                          int *authenticated,
346                                          int *complete,
347                                          gss_buffer_t value,
348                                          gss_buffer_t display_value,
349                                          int *more) const
350 {
351     const saml2::Attribute *a;
352     const saml2::AttributeValue *av;
353     int nvalues, i = *more;
354
355     *more = 0;
356
357     a = getAttribute(attr);
358     if (a == NULL)
359         return false;
360
361     nvalues = a->getAttributeValues().size();
362
363     if (i == -1)
364         i = 0;
365     else if (i >= nvalues)
366         return false;
367     av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
368 );
369     if (av == NULL)
370         return false;
371
372     *authenticated = TRUE;
373     *complete = FALSE;
374
375     value->value = toUTF8(av->getTextContent(), true);
376     value->length = strlen((char *)value->value);
377
378     if (nvalues > ++i)
379         *more = i;
380
381     return true;
382 }
383
384 gss_any_t
385 gss_eap_saml_attr_provider::mapToAny(int authenticated,
386                                           gss_buffer_t type_id) const
387 {
388     return (gss_any_t)0;
389 }
390
391 void
392 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
393                                                   gss_any_t input) const
394 {
395 }
396
397 void
398 gss_eap_saml_attr_provider::marshall(gss_buffer_t buffer) const
399 {
400 }
401
402 bool
403 gss_eap_saml_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
404                                        const gss_buffer_t buffer)
405 {
406     return false;
407 }
408
409 bool
410 gss_eap_saml_attr_provider::init(void)
411 {
412     return true;
413 }
414
415 void
416 gss_eap_saml_attr_provider::finalize(void)
417 {
418 }
419
420 gss_eap_attr_provider *
421 gss_eap_saml_attr_provider::createAttrContext(void)
422 {
423     return new gss_eap_saml_attr_provider;
424 }