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