New attribute provider SPI
[mech_eap.orig] / 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 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(const gss_eap_attr_ctx *ctx,
78                                                                  gss_cred_id_t gssCred,
79                                                                  gss_ctx_id_t gssCtx)
80     : gss_eap_attr_provider(ctx, gssCred, gssCtx)
81 {
82     if (gssCtx == GSS_C_NO_CONTEXT) {
83         /* Then we are creating from an existing attribute context */
84         gss_eap_saml_assertion_provider *saml;
85
86         saml = dynamic_cast<gss_eap_saml_assertion_provider *>
87             (ctx->getProvider(ATTR_TYPE_SAML_ASSERTION));
88         if (saml != NULL)
89             setAssertion(saml->getAssertion());
90     } else { 
91         gss_eap_radius_attr_provider *radius;
92         gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
93         int authenticated, complete, more = -1;
94         OM_uint32 minor;
95
96         radius = dynamic_cast<gss_eap_radius_attr_provider *>
97             (ctx->getProvider(ATTR_TYPE_RADIUS));
98         if (radius != NULL &&
99             radius->getAttribute(512, &authenticated, &complete,
100                                  &value, NULL, &more)) {
101             m_assertion = parseAssertion(&value);
102             gss_release_buffer(&minor, &value);
103         }
104     }
105 }
106
107 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
108 {
109     delete m_assertion;
110 }
111
112 void
113 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion)
114 {
115
116     delete m_assertion;
117     m_assertion = dynamic_cast<saml2::Assertion*>(assertion->clone());
118 }
119
120 saml2::Assertion *
121 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
122 {
123     string str((char *)buffer->value, buffer->length);
124     istringstream istream(str);
125     DOMDocument *doc;
126     const XMLObjectBuilder *b;
127
128     doc = XMLToolingConfig::getConfig().getParser().parse(istream);
129     b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
130
131     return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
132 }
133
134 bool
135 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
136 {
137     addAttribute(this, GSS_C_NO_BUFFER, data);
138 }
139
140 void
141 gss_eap_saml_assertion_provider::setAttribute(int complete,
142                                               const gss_buffer_t attr,
143                                               const gss_buffer_t value)
144 {
145     saml2::Assertion *assertion = parseAssertion(value);
146
147     m_assertion = assertion;
148 }
149
150 void
151 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
152 {
153     delete m_assertion;
154     m_assertion = NULL;
155 }
156
157 bool
158 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
159                                               int *authenticated,
160                                               int *complete,
161                                               gss_buffer_t value,
162                                               gss_buffer_t display_value,
163                                               int *more) const
164 {
165     string str;
166
167     if (attr->length != 0 || m_assertion == NULL)
168         return false;
169
170     if (*more == -1)
171         *more = 0;
172
173     if (*more == 0) {
174         *authenticated = true;
175         *complete = false;
176
177         XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
178
179         duplicateBuffer(str, value);
180     }
181
182     return true;
183 }
184
185 gss_any_t
186 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
187                                           gss_buffer_t type_id) const
188 {
189     return (gss_any_t)m_assertion;
190 }
191
192 void
193 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
194                                                        gss_any_t input) const
195 {
196     delete ((saml2::Assertion *)input);
197 }
198
199 void
200 gss_eap_saml_assertion_provider::marshall(gss_buffer_t buffer) const
201 {
202     ostringstream sink;
203     string str;
204
205     buffer->length = 0;
206     buffer->value = NULL;
207
208     if (m_assertion == NULL)
209         return;
210
211     sink << *m_assertion;
212     str = sink.str();
213
214     duplicateBuffer(str, buffer);
215 }
216
217 gss_eap_attr_provider *
218 gss_eap_saml_assertion_provider::unmarshall(const gss_eap_attr_ctx *ctx,
219                                             const gss_buffer_t buffer)
220 {
221    return new gss_eap_saml_assertion_provider(ctx, buffer);
222 }
223
224 bool
225 gss_eap_saml_assertion_provider::init(void)
226 {
227 }
228
229 void
230 gss_eap_saml_assertion_provider::finalize(void)
231 {
232 }
233
234 gss_eap_attr_provider *
235 gss_eap_saml_assertion_provider::createAttrContext(const gss_eap_attr_ctx *ctx,
236                                                    gss_cred_id_t gssCred,
237                                                    gss_ctx_id_t gssCtx)
238 {
239     return new gss_eap_saml_assertion_provider(ctx, gssCred, gssCtx);
240 }
241
242 /*
243  * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
244  */
245 const saml2::Assertion *
246 gss_eap_saml_attr_provider::getAssertion(void) const
247 {
248     gss_eap_saml_assertion_provider *saml;
249     
250     saml = dynamic_cast<gss_eap_saml_assertion_provider *>(m_source->getProvider(ATTR_TYPE_SAML_ASSERTION));
251     assert(saml != NULL);
252
253     return saml->getAssertion();
254 }
255
256 gss_eap_saml_attr_provider::gss_eap_saml_attr_provider(const gss_eap_attr_ctx *ctx,
257                                                        gss_cred_id_t gssCred,
258                                                        gss_ctx_id_t gssCtx)
259     : gss_eap_attr_provider(ctx, gssCred, gssCtx)
260 {
261     /* Nothing to do, we're just a wrapper around the assertion provider. */
262 }
263
264 gss_eap_saml_attr_provider::~gss_eap_saml_attr_provider(void)
265 {
266     /* Nothing to do, we're just a wrapper around the assertion provider. */
267 }
268
269 bool
270 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
271                                               void *data) const
272 {
273     const saml2::Assertion *assertion = getAssertion();
274
275     if (assertion == NULL)
276         return true;
277
278     const vector<saml2::Attribute*>& attrs2 =
279         const_cast<const saml2::AttributeStatement*>(assertion->getAttributeStatements().front())->getAttributes();
280     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
281         a != attrs2.end();
282         ++a)
283     {
284         gss_buffer_desc attribute;
285
286         attribute.value = (void *)toUTF8((*a)->getName(), true);
287         attribute.length = strlen((char *)attribute.value);
288
289         if (!addAttribute(this, &attribute, data))
290             return false;
291
292         delete (char *)attribute.value;
293     }
294
295     return true;
296 }
297
298 void
299 gss_eap_saml_attr_provider::setAttribute(int complete,
300                                          const gss_buffer_t attr,
301                                          const gss_buffer_t value)
302 {
303 }
304
305 void
306 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
307 {
308 }
309
310 const saml2::Attribute *
311 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr) const
312 {
313     const saml2::Assertion *assertion = getAssertion();
314     saml2::AttributeStatement *statement;
315
316     if (assertion == NULL)
317         return NULL;
318
319     if (assertion->getAttributeStatements().size() == 0)
320         return NULL;
321
322     statement = assertion->getAttributeStatements().front();
323
324     auto_ptr_gss_buffer attrname(attr);
325
326     const vector<saml2::Attribute*>& attrs2 =
327         const_cast<const saml2::AttributeStatement*>(statement)->getAttributes();
328
329     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
330         a != attrs2.end();
331         ++a) {
332         if (XMLString::equals((*a)->getName(), attrname.get()))
333             return *a;
334     }
335
336     return NULL;
337 }
338
339 bool
340 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
341                                          int *authenticated,
342                                          int *complete,
343                                          gss_buffer_t value,
344                                          gss_buffer_t display_value,
345                                          int *more) const
346 {
347     const saml2::Attribute *a;
348     const saml2::AttributeValue *av;
349     int nvalues, i = *more;
350
351     *more = 0;
352
353     a = getAttribute(attr);
354     if (a == NULL)
355         return false;
356
357     nvalues = a->getAttributeValues().size();
358
359     if (i == -1)
360         i = 0;
361     else if (i >= nvalues)
362         return false;
363     av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
364 );
365     if (av == NULL)
366         return false;
367
368     *authenticated = TRUE;
369     *complete = FALSE;
370
371     value->value = toUTF8(av->getTextContent(), true);
372     value->length = strlen((char *)value->value);
373
374     if (nvalues > ++i)
375         *more = i;
376
377     return true;
378 }
379
380 gss_any_t
381 gss_eap_saml_attr_provider::mapToAny(int authenticated,
382                                           gss_buffer_t type_id) const
383 {
384     return (gss_any_t)0;
385 }
386
387 void
388 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
389                                                   gss_any_t input) const
390 {
391 }
392
393 void
394 gss_eap_saml_attr_provider::marshall(gss_buffer_t buffer) const
395 {
396 }
397
398 gss_eap_attr_provider *
399 gss_eap_saml_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
400                                        const gss_buffer_t buffer)
401 {
402     return new gss_eap_saml_attr_provider(ctx);
403 }
404
405 bool
406 gss_eap_saml_attr_provider::init(void)
407 {
408     return true;
409 }
410
411 void
412 gss_eap_saml_attr_provider::finalize(void)
413 {
414 }
415
416 gss_eap_attr_provider *
417 gss_eap_saml_attr_provider::createAttrContext(const gss_eap_attr_ctx *ctx,
418                                               gss_cred_id_t gssCred,
419                                               gss_ctx_id_t gssCtx)
420 {
421     return new gss_eap_saml_attr_provider(ctx, gssCred, gssCtx);
422 }