dead code removal
[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 /*
52  * gss_eap_saml_assertion_provider is for retrieving the underlying
53  * assertion.
54  */
55 bool
56 gss_eap_saml_assertion_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
57                                                          const gss_eap_attr_provider *ctx)
58 {
59     /* Then we may be creating from an existing attribute context */
60     const gss_eap_saml_assertion_provider *saml;
61
62     assert(m_assertion == NULL);
63
64     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
65         return false;
66
67     saml = static_cast<const gss_eap_saml_assertion_provider *>(ctx);
68     setAssertion(saml->getAssertion());
69
70     return true;
71 }
72
73 bool
74 gss_eap_saml_assertion_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
75                                                     const gss_cred_id_t gssCred,
76                                                     const gss_ctx_id_t gssCtx)
77 {
78     const gss_eap_radius_attr_provider *radius;
79     gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
80     int authenticated, complete, more = -1;
81     OM_uint32 minor;
82
83     assert(m_assertion == NULL);
84
85     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
86         return false;
87
88     radius = static_cast<const gss_eap_radius_attr_provider *>
89         (m_manager->getProvider(ATTR_TYPE_RADIUS));
90     if (radius != NULL &&
91         radius->getAttribute(512 /* XXX */, &authenticated, &complete,
92                              &value, NULL, &more)) {
93         m_assertion = parseAssertion(&value);
94         gss_release_buffer(&minor, &value);
95     } else {
96         m_assertion = NULL;
97     }
98
99     return true;
100 }
101
102 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
103 {
104     delete m_assertion;
105 }
106
107 void
108 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion)
109 {
110
111     delete m_assertion;
112
113     if (assertion != NULL)
114         m_assertion = dynamic_cast<saml2::Assertion*>(assertion->clone());
115     else
116         m_assertion = NULL;
117 }
118
119 saml2::Assertion *
120 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
121 {
122     string str((char *)buffer->value, buffer->length);
123     istringstream istream(str);
124     DOMDocument *doc;
125     const XMLObjectBuilder *b;
126
127     doc = XMLToolingConfig::getConfig().getParser().parse(istream);
128     b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
129
130     return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
131 }
132
133 bool
134 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
135                                                    void *data) const
136 {
137     /* just add the prefix */
138     return addAttribute(this, GSS_C_NO_BUFFER, data);
139 }
140
141 void
142 gss_eap_saml_assertion_provider::setAttribute(int complete,
143                                               const gss_buffer_t attr,
144                                               const gss_buffer_t value)
145 {
146     if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
147         saml2::Assertion *assertion = parseAssertion(value);
148
149         delete m_assertion;
150         m_assertion = assertion;
151     }
152 }
153
154 void
155 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
156 {
157     delete m_assertion;
158     m_assertion = NULL;
159 }
160
161 bool
162 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
163                                               int *authenticated,
164                                               int *complete,
165                                               gss_buffer_t value,
166                                               gss_buffer_t display_value,
167                                               int *more) const
168 {
169     string str;
170
171     if (attr != GSS_C_NO_BUFFER || attr->length != 0)
172         return false;
173
174     if (m_assertion == NULL)
175         return false;
176
177     if (*more != -1)
178         return false;
179
180     *authenticated = true;
181     *complete = false;
182
183     XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
184
185     duplicateBuffer(str, value);
186     *more = 0;
187
188     return true;
189 }
190
191 gss_any_t
192 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
193                                           gss_buffer_t type_id) const
194 {
195     return (gss_any_t)m_assertion;
196 }
197
198 void
199 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
200                                                        gss_any_t input) const
201 {
202     delete ((saml2::Assertion *)input);
203 }
204
205 void
206 gss_eap_saml_assertion_provider::exportToBuffer(gss_buffer_t buffer) const
207 {
208     ostringstream sink;
209     string str;
210
211     buffer->length = 0;
212     buffer->value = NULL;
213
214     if (m_assertion == NULL)
215         return;
216
217     sink << *m_assertion;
218     str = sink.str();
219
220     duplicateBuffer(str, buffer);
221 }
222
223 bool
224 gss_eap_saml_assertion_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
225                                                 const gss_buffer_t buffer)
226 {
227     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
228         return false;
229
230     if (buffer->length == 0)
231         return true;
232
233     assert(m_assertion == NULL);
234
235     m_assertion = parseAssertion(buffer);
236     if (m_assertion == NULL)
237         return false;
238
239     return true;
240 }
241
242 bool
243 gss_eap_saml_assertion_provider::init(void)
244 {
245     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION,
246                                        "urn:ietf:params:gss-eap:saml-aaa-assertion",
247                                        gss_eap_saml_assertion_provider::createAttrContext);
248     return true;
249 }
250
251 void
252 gss_eap_saml_assertion_provider::finalize(void)
253 {
254     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML_ASSERTION);
255 }
256
257 gss_eap_attr_provider *
258 gss_eap_saml_assertion_provider::createAttrContext(void)
259 {
260     return new gss_eap_saml_assertion_provider;
261 }
262
263 /*
264  * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
265  */
266 const saml2::Assertion *
267 gss_eap_saml_attr_provider::getAssertion(void) const
268 {
269     const gss_eap_saml_assertion_provider *saml;
270     
271     saml = static_cast<const gss_eap_saml_assertion_provider *>
272         (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
273     if (saml != NULL)
274         return saml->getAssertion();
275
276     return NULL;
277 }
278
279 gss_eap_saml_attr_provider::~gss_eap_saml_attr_provider(void)
280 {
281     /* Nothing to do, we're just a wrapper around the assertion provider. */
282 }
283
284 bool
285 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
286                                               void *data) const
287 {
288     const saml2::Assertion *assertion = getAssertion();
289     bool ret = true;
290
291     if (assertion == NULL)
292         return true;
293
294     const vector<saml2::Attribute*>& attrs2 =
295         const_cast<const saml2::AttributeStatement*>(assertion->getAttributeStatements().front())->getAttributes();
296     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
297         a != attrs2.end();
298         ++a)
299     {
300         const XMLCh *attributeName = (*a)->getName();
301         const XMLCh *attributeNameFormat = (*a)->getNameFormat();
302         XMLCh *qualifiedName;
303         XMLCh space[2] = { ' ', 0 };
304         gss_buffer_desc utf8;
305
306         qualifiedName = new XMLCh[XMLString::stringLen(attributeName) + 1 +
307                                   XMLString::stringLen(attributeNameFormat) + 1];
308         XMLString::copyString(qualifiedName, attributeName);
309         XMLString::catString(qualifiedName, space);
310         XMLString::catString(qualifiedName, attributeNameFormat);
311
312         utf8.value = (void *)toUTF8(qualifiedName);
313         utf8.length = strlen((char *)utf8.value);
314
315         ret = addAttribute(this, &utf8, data);
316
317         delete qualifiedName;
318         delete qualifiedName;
319
320         if (!ret)
321             break;
322     }
323
324     return ret;
325 }
326
327 void
328 gss_eap_saml_attr_provider::setAttribute(int complete,
329                                          const gss_buffer_t attr,
330                                          const gss_buffer_t value)
331 {
332 }
333
334 void
335 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
336 {
337 }
338
339 static BaseRefVectorOf<XMLCh> *
340 decomposeAttributeName(const gss_buffer_t attr)
341 {
342     XMLCh *qualifiedAttr = new XMLCh[attr->length + 1];
343     XMLString::transcode((const char *)attr->value, qualifiedAttr, attr->length);
344
345     BaseRefVectorOf<XMLCh> *components = XMLString::tokenizeString(qualifiedAttr);
346
347     delete qualifiedAttr;
348
349     return components;
350 }
351
352 const saml2::Attribute *
353 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr) const
354 {
355     /* Check we have an assertion */
356     const saml2::Assertion *assertion = getAssertion();
357     if (assertion == NULL ||
358         assertion->getAttributeStatements().size() == 0)
359         return NULL;
360
361     /* Check the attribute name consists of name format | whsp | name */
362     BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
363     if (components == NULL || components->size() != 2) {
364         delete components;
365         return NULL;
366     }
367
368     /* For each attribute statement, look for an attribute match */
369     const vector <saml2::AttributeStatement *>&statements =
370         assertion->getAttributeStatements();
371     const saml2::Attribute *ret = NULL;
372
373     for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
374         s != statements.end();
375         ++s) {
376         const vector<saml2::Attribute*>& attrs =
377             const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
378
379         for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
380             if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
381                 XMLString::equals((*a)->getName(), components->elementAt(1))) {
382                 ret = *a;
383                 break;
384             }
385         }
386
387         if (ret != NULL)
388             break;
389     }
390
391     delete components;
392
393     return ret;
394 }
395
396 bool
397 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
398                                          int *authenticated,
399                                          int *complete,
400                                          gss_buffer_t value,
401                                          gss_buffer_t display_value,
402                                          int *more) const
403 {
404     const saml2::Attribute *a;
405     const saml2::AttributeValue *av;
406     int nvalues, i = *more;
407
408     *more = 0;
409
410     a = getAttribute(attr);
411     if (a == NULL)
412         return false;
413
414     nvalues = a->getAttributeValues().size();
415
416     if (i == -1)
417         i = 0;
418     else if (i >= nvalues)
419         return false;
420     av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
421 );
422     if (av == NULL)
423         return false;
424
425     *authenticated = TRUE;
426     *complete = FALSE;
427
428     value->value = toUTF8(av->getTextContent(), true);
429     value->length = strlen((char *)value->value);
430
431     if (display_value != NULL)
432         duplicateBuffer(*value, display_value);
433
434     if (nvalues > ++i)
435         *more = i;
436
437     return true;
438 }
439
440 gss_any_t
441 gss_eap_saml_attr_provider::mapToAny(int authenticated,
442                                      gss_buffer_t type_id) const
443 {
444     return (gss_any_t)NULL;
445 }
446
447 void
448 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
449                                                   gss_any_t input) const
450 {
451 }
452
453 void
454 gss_eap_saml_attr_provider::exportToBuffer(gss_buffer_t buffer) const
455 {
456     buffer->length = 0;
457     buffer->value = NULL;
458 }
459
460 bool
461 gss_eap_saml_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
462                                            const gss_buffer_t buffer)
463 {
464     return gss_eap_attr_provider::initFromBuffer(ctx, buffer);
465 }
466
467 bool
468 gss_eap_saml_attr_provider::init(void)
469 {
470     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML,
471                                        "urn:ietf:params:gss-eap:saml-attr",
472                                        gss_eap_saml_attr_provider::createAttrContext);
473     return true;
474 }
475
476 void
477 gss_eap_saml_attr_provider::finalize(void)
478 {
479     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML);
480 }
481
482 gss_eap_attr_provider *
483 gss_eap_saml_attr_provider::createAttrContext(void)
484 {
485     return new gss_eap_saml_attr_provider;
486 }