check syntax before decoding base64 encoded SAML attributes
[moonshot.git] / mech_eap / util_saml.cpp
1 /*
2  * Copyright (c) 2011, 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 /*
34  * SAML attribute provider implementation.
35  */
36
37 #include "gssapiP_eap.h"
38
39 #include <sstream>
40
41 #include <xercesc/util/XMLUniDefs.hpp>
42 #include <xmltooling/unicode.h>
43 #include <xmltooling/XMLToolingConfig.h>
44 #include <xmltooling/util/XMLHelper.h>
45 #include <xmltooling/util/ParserPool.h>
46 #include <xmltooling/util/DateTime.h>
47
48 #include <saml/exceptions.h>
49 #include <saml/saml1/core/Assertions.h>
50 #include <saml/saml2/core/Assertions.h>
51 #include <saml/saml2/metadata/Metadata.h>
52 #include <saml/saml2/metadata/MetadataProvider.h>
53
54 using namespace xmltooling;
55 using namespace opensaml::saml2md;
56 using namespace opensaml;
57 using namespace xercesc;
58 using namespace std;
59
60 static const XMLCh
61 base64Binary[] = {'b','a','s','e','6','4','B','i','n','a','r','y',0};
62
63 /*
64  * gss_eap_saml_assertion_provider is for retrieving the underlying
65  * assertion.
66  */
67 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(void)
68 {
69     m_assertion = NULL;
70     m_authenticated = false;
71 }
72
73 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
74 {
75     delete m_assertion;
76 }
77
78 bool
79 gss_eap_saml_assertion_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
80                                                          const gss_eap_attr_provider *ctx)
81 {
82     /* Then we may be creating from an existing attribute context */
83     const gss_eap_saml_assertion_provider *saml;
84
85     assert(m_assertion == NULL);
86
87     if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx))
88         return false;
89
90     saml = static_cast<const gss_eap_saml_assertion_provider *>(ctx);
91     setAssertion(saml->getAssertion(), saml->authenticated());
92
93     return true;
94 }
95
96 bool
97 gss_eap_saml_assertion_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
98                                                     const gss_cred_id_t gssCred,
99                                                     const gss_ctx_id_t gssCtx)
100 {
101     const gss_eap_radius_attr_provider *radius;
102     gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
103     int authenticated, complete;
104     OM_uint32 minor;
105
106     assert(m_assertion == NULL);
107
108     if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
109         return false;
110
111     /*
112      * XXX TODO we need to support draft-howlett-radius-saml-attr-00
113      */
114     radius = static_cast<const gss_eap_radius_attr_provider *>
115         (m_manager->getProvider(ATTR_TYPE_RADIUS));
116     if (radius != NULL &&
117         radius->getFragmentedAttribute(PW_SAML_AAA_ASSERTION,
118                                        VENDORPEC_UKERNA,
119                                        &authenticated, &complete, &value)) {
120         setAssertion(&value, authenticated);
121         gss_release_buffer(&minor, &value);
122     } else {
123         m_assertion = NULL;
124     }
125
126     return true;
127 }
128
129 void
130 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion,
131                                               bool authenticated)
132 {
133
134     delete m_assertion;
135
136     if (assertion != NULL) {
137 #ifdef __APPLE__
138         m_assertion = (saml2::Assertion *)((void *)assertion->clone());
139 #else
140         m_assertion = dynamic_cast<saml2::Assertion *>(assertion->clone());
141 #endif
142         m_authenticated = authenticated;
143     } else {
144         m_assertion = NULL;
145         m_authenticated = false;
146     }
147 }
148
149 void
150 gss_eap_saml_assertion_provider::setAssertion(const gss_buffer_t buffer,
151                                               bool authenticated)
152 {
153     delete m_assertion;
154
155     m_assertion = parseAssertion(buffer);
156     m_authenticated = (m_assertion != NULL && authenticated);
157 }
158
159 saml2::Assertion *
160 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
161 {
162     string str((char *)buffer->value, buffer->length);
163     istringstream istream(str);
164     DOMDocument *doc;
165     const XMLObjectBuilder *b;
166
167     try {
168         doc = XMLToolingConfig::getConfig().getParser().parse(istream);
169         if (doc == NULL)
170             return NULL;
171
172         b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
173
174 #ifdef __APPLE__
175         return (saml2::Assertion *)((void *)b->buildFromDocument(doc));
176 #else
177         return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
178 #endif
179     } catch (exception &e) {
180         return NULL;
181     }
182 }
183
184 bool
185 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
186                                                    void *data) const
187 {
188     bool ret;
189
190     /* just add the prefix */
191     if (m_assertion != NULL)
192         ret = addAttribute(m_manager, this, GSS_C_NO_BUFFER, data);
193     else
194         ret = true;
195
196     return ret;
197 }
198
199 bool
200 gss_eap_saml_assertion_provider::setAttribute(int complete GSSEAP_UNUSED,
201                                               const gss_buffer_t attr,
202                                               const gss_buffer_t value)
203 {
204     if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
205         setAssertion(value);
206         return true;
207     }
208
209     return false;
210 }
211
212 bool
213 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value GSSEAP_UNUSED)
214 {
215     delete m_assertion;
216     m_assertion = NULL;
217     m_authenticated = false;
218
219     return true;
220 }
221
222 time_t
223 gss_eap_saml_assertion_provider::getExpiryTime(void) const
224 {
225     saml2::Conditions *conditions;
226     time_t expiryTime = 0;
227
228     if (m_assertion == NULL)
229         return 0;
230
231     conditions = m_assertion->getConditions();
232
233     if (conditions != NULL && conditions->getNotOnOrAfter() != NULL)
234         expiryTime = conditions->getNotOnOrAfter()->getEpoch();
235
236     return expiryTime;
237 }
238
239 OM_uint32
240 gss_eap_saml_assertion_provider::mapException(OM_uint32 *minor,
241                                               std::exception &e) const
242 {
243     if (typeid(e) == typeid(SecurityPolicyException))
244         *minor = GSSEAP_SAML_SEC_POLICY_FAILURE;
245     else if (typeid(e) == typeid(BindingException))
246         *minor = GSSEAP_SAML_BINDING_FAILURE;
247     else if (typeid(e) == typeid(ProfileException))
248         *minor = GSSEAP_SAML_PROFILE_FAILURE;
249     else if (typeid(e) == typeid(FatalProfileException))
250         *minor = GSSEAP_SAML_FATAL_PROFILE_FAILURE;
251     else if (typeid(e) == typeid(RetryableProfileException))
252         *minor = GSSEAP_SAML_RETRY_PROFILE_FAILURE;
253     else if (typeid(e) == typeid(MetadataException))
254         *minor = GSSEAP_SAML_METADATA_FAILURE;
255     else
256         return GSS_S_CONTINUE_NEEDED;
257
258     gssEapSaveStatusInfo(*minor, "%s", e.what());
259
260     return GSS_S_FAILURE;
261 }
262
263 bool
264 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
265                                               int *authenticated,
266                                               int *complete,
267                                               gss_buffer_t value,
268                                               gss_buffer_t display_value GSSEAP_UNUSED,
269                                               int *more) const
270 {
271     string str;
272
273     if (attr != GSS_C_NO_BUFFER && attr->length != 0)
274         return false;
275
276     if (m_assertion == NULL)
277         return false;
278
279     if (*more != -1)
280         return false;
281
282     if (authenticated != NULL)
283         *authenticated = m_authenticated;
284     if (complete != NULL)
285         *complete = true;
286
287     XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
288
289     if (value != NULL)
290         duplicateBuffer(str, value);
291     if (display_value != NULL)
292         duplicateBuffer(str, display_value);
293
294     *more = 0;
295
296     return true;
297 }
298
299 gss_any_t
300 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
301                                           gss_buffer_t type_id GSSEAP_UNUSED) const
302 {
303     if (authenticated && !m_authenticated)
304         return (gss_any_t)NULL;
305
306     return (gss_any_t)m_assertion;
307 }
308
309 void
310 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
311                                                        gss_any_t input) const
312 {
313     delete ((saml2::Assertion *)input);
314 }
315
316 const char *
317 gss_eap_saml_assertion_provider::prefix(void) const
318 {
319     return "urn:ietf:params:gss-eap:saml-aaa-assertion";
320 }
321
322 bool
323 gss_eap_saml_assertion_provider::init(void)
324 {
325     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION, createAttrContext);
326     return true;
327 }
328
329 void
330 gss_eap_saml_assertion_provider::finalize(void)
331 {
332     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML_ASSERTION);
333 }
334
335 gss_eap_attr_provider *
336 gss_eap_saml_assertion_provider::createAttrContext(void)
337 {
338     return new gss_eap_saml_assertion_provider;
339 }
340
341 saml2::Assertion *
342 gss_eap_saml_assertion_provider::initAssertion(void)
343 {
344     delete m_assertion;
345     m_assertion = saml2::AssertionBuilder::buildAssertion();
346     m_authenticated = false;
347
348     return m_assertion;
349 }
350
351 /*
352  * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
353  */
354 bool
355 gss_eap_saml_attr_provider::getAssertion(int *authenticated,
356                                          saml2::Assertion **pAssertion,
357                                          bool createIfAbsent) const
358 {
359     gss_eap_saml_assertion_provider *saml;
360
361     if (authenticated != NULL)
362         *authenticated = false;
363     if (pAssertion != NULL)
364         *pAssertion = NULL;
365
366     saml = static_cast<const gss_eap_saml_assertion_provider *>
367         (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
368     if (saml == NULL)
369         return false;
370
371     if (authenticated != NULL)
372         *authenticated = saml->authenticated();
373     if (pAssertion != NULL)
374         *pAssertion = saml->getAssertion();
375
376     if (saml->getAssertion() == NULL) {
377         if (createIfAbsent) {
378             if (authenticated != NULL)
379                 *authenticated = false;
380             if (pAssertion != NULL)
381                 *pAssertion = saml->initAssertion();
382         } else
383             return false;
384     }
385
386     return true;
387 }
388
389 bool
390 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
391                                               void *data) const
392 {
393     saml2::Assertion *assertion;
394     int authenticated;
395
396     if (!getAssertion(&authenticated, &assertion))
397         return true;
398
399     /*
400      * Note: the first prefix is added by the attribute provider manager
401      *
402      * From draft-hartman-gss-eap-naming-00:
403      *
404      *   Each attribute carried in the assertion SHOULD also be a GSS name
405      *   attribute.  The name of this attribute has three parts, all separated
406      *   by an ASCII space character.  The first part is
407      *   urn:ietf:params:gss-eap:saml-attr.  The second part is the URI for
408      *   the SAML attribute name format.  The final part is the name of the
409      *   SAML attribute.  If the mechanism performs an additional attribute
410      *   query, the retrieved attributes SHOULD be GSS-API name attributes
411      *   using the same name syntax.
412      */
413     /* For each attribute statement, look for an attribute match */
414     const vector <saml2::AttributeStatement *> &statements =
415         const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
416
417     for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
418         s != statements.end();
419         ++s) {
420         const vector<saml2::Attribute*> &attrs =
421             const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
422
423         for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
424             const XMLCh *attributeName, *attributeNameFormat;
425             XMLCh space[2] = { ' ', 0 };
426             gss_buffer_desc utf8;
427
428             attributeName = (*a)->getName();
429             attributeNameFormat = (*a)->getNameFormat();
430             if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
431                 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
432
433             XMLCh qualifiedName[XMLString::stringLen(attributeNameFormat) + 1 +
434                                 XMLString::stringLen(attributeName) + 1];
435             XMLString::copyString(qualifiedName, attributeNameFormat);
436             XMLString::catString(qualifiedName, space);
437             XMLString::catString(qualifiedName, attributeName);
438
439             utf8.value = (void *)toUTF8(qualifiedName);
440             utf8.length = strlen((char *)utf8.value);
441
442             if (!addAttribute(m_manager, this, &utf8, data))
443                 return false;
444         }
445     }
446
447     return true;
448 }
449
450 static BaseRefVectorOf<XMLCh> *
451 decomposeAttributeName(const gss_buffer_t attr)
452 {
453     BaseRefVectorOf<XMLCh> *components;
454     string str((const char *)attr->value, attr->length);
455     auto_ptr_XMLCh qualifiedAttr(str.c_str());
456
457     components = XMLString::tokenizeString(qualifiedAttr.get());
458
459     if (components->size() != 2) {
460         delete components;
461         components = NULL;
462     }
463
464     return components;
465 }
466
467 static bool
468 isNotPrintableP(const gss_buffer_t value)
469 {
470     size_t i;
471     char *p = (char *)value->value;
472
473     if (isgraph(p[0]) &&
474         isgraph(p[value->length - 1]))
475     {
476         for (i = 0; p[i]; i++) {
477             if (!isascii(p[i]) || !isprint(p[i]))
478                 return true;
479         }
480         return false;
481     }
482
483     return true;
484 }
485
486 bool
487 gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
488                                          const gss_buffer_t attr,
489                                          const gss_buffer_t value)
490 {
491     saml2::Assertion *assertion;
492     saml2::Attribute *attribute;
493     saml2::AttributeValue *attributeValue;
494     saml2::AttributeStatement *attributeStatement;
495
496     if (!getAssertion(NULL, &assertion, true))
497         return false;
498
499     if (assertion->getAttributeStatements().size() != 0) {
500         attributeStatement = assertion->getAttributeStatements().front();
501     } else {
502         attributeStatement = saml2::AttributeStatementBuilder::buildAttributeStatement();
503         assertion->getAttributeStatements().push_back(attributeStatement);
504     }
505
506     /* Check the attribute name consists of name format | whsp | name */
507     BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
508     if (components == NULL)
509         return false;
510
511     attribute = saml2::AttributeBuilder::buildAttribute();
512     attribute->setNameFormat(components->elementAt(0));
513     attribute->setName(components->elementAt(1));
514
515     attributeValue = saml2::AttributeValueBuilder::buildAttributeValue();
516     if (isNotPrintableP(value)) {
517         /* XXX FIXME where is setSchemaType()? */
518         xmltooling::QName base64SchemaType(xmlconstants::XSD_NS,
519                                            base64Binary,
520                                            xmlconstants::XSD_PREFIX);
521         char *b64;
522
523         if (base64Encode(value->value, value->length, &b64) < 0)
524             return false;
525
526         auto_ptr_XMLCh unistr(b64);
527         attributeValue->setTextContent(unistr.get());
528     } else {
529         auto_ptr_XMLCh unistr((char *)value->value);
530         attributeValue->setTextContent(unistr.get());
531     }
532
533     attribute->getAttributeValues().push_back(attributeValue);
534
535     assert(attributeStatement != NULL);
536     attributeStatement->getAttributes().push_back(attribute);
537
538     delete components;
539
540     return true;
541 }
542
543 bool
544 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t attr)
545 {
546     saml2::Assertion *assertion;
547     bool ret = false;
548
549     if (!getAssertion(NULL, &assertion) ||
550         assertion->getAttributeStatements().size() == 0)
551         return false;
552
553     /* Check the attribute name consists of name format | whsp | name */
554     BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
555     if (components == NULL)
556         return false;
557
558     /* For each attribute statement, look for an attribute match */
559     const vector<saml2::AttributeStatement *> &statements =
560         const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
561
562     for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
563         s != statements.end();
564         ++s) {
565         const vector<saml2::Attribute *> &attrs =
566             const_cast<const saml2::AttributeStatement *>(*s)->getAttributes();
567         ssize_t index = -1, i = 0;
568
569         /* There's got to be an easier way to do this */
570         for (vector<saml2::Attribute *>::const_iterator a = attrs.begin();
571              a != attrs.end();
572              ++a) {
573             if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
574                 XMLString::equals((*a)->getName(), components->elementAt(1))) {
575                 index = i;
576                 break;
577             }
578             ++i;
579         }
580         if (index != -1) {
581             (*s)->getAttributes().erase((*s)->getAttributes().begin() + index);
582             ret = true;
583         }
584     }
585
586     delete components;
587
588     return ret;
589 }
590
591 bool
592 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
593                                          int *authenticated,
594                                          int *complete,
595                                          const saml2::Attribute **pAttribute) const
596 {
597     saml2::Assertion *assertion;
598
599     if (authenticated != NULL)
600         *authenticated = false;
601     if (complete != NULL)
602         *complete = true;
603     *pAttribute = NULL;
604
605     if (!getAssertion(authenticated, &assertion) ||
606         assertion->getAttributeStatements().size() == 0)
607         return false;
608
609     /* Check the attribute name consists of name format | whsp | name */
610     BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
611     if (components == NULL)
612         return false;
613
614     /* For each attribute statement, look for an attribute match */
615     const vector <saml2::AttributeStatement *> &statements =
616         const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
617     const saml2::Attribute *ret = NULL;
618
619     for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
620         s != statements.end();
621         ++s) {
622         const vector<saml2::Attribute *> &attrs =
623             const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
624
625         for (vector<saml2::Attribute *>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
626             const XMLCh *attributeName, *attributeNameFormat;
627
628             attributeName = (*a)->getName();
629             attributeNameFormat = (*a)->getNameFormat();
630             if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
631                 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
632
633             if (XMLString::equals(attributeNameFormat, components->elementAt(0)) &&
634                 XMLString::equals(attributeName, components->elementAt(1))) {
635                 ret = *a;
636                 break;
637             }
638         }
639
640         if (ret != NULL)
641             break;
642     }
643
644     delete components;
645
646     *pAttribute = ret;
647
648     return (ret != NULL);
649 }
650
651 static bool
652 isBase64EncodedAttributeValueP(const saml2::AttributeValue *av)
653 {
654     const xmltooling::QName *type = av->getSchemaType();
655
656     if (type == NULL)
657         return false;
658
659     if (!type->hasNamespaceURI() ||
660         !XMLString::equals(type->getNamespaceURI(), xmlconstants::XSD_NS))
661         return false;
662
663     if (!type->hasPrefix() ||
664         !XMLString::equals(type->getPrefix(), xmlconstants::XSD_PREFIX))
665         return false;
666
667     if (!type->hasLocalPart() ||
668         !XMLString::equals(type->getLocalPart(), base64Binary))
669         return false;
670
671     return true;
672 }
673
674 bool
675 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
676                                          int *authenticated,
677                                          int *complete,
678                                          gss_buffer_t value,
679                                          gss_buffer_t display_value,
680                                          int *more) const
681 {
682     const saml2::Attribute *a;
683     const saml2::AttributeValue *av;
684     int nvalues, i = *more;
685
686     *more = 0;
687
688     if (!getAttribute(attr, authenticated, complete, &a))
689         return false;
690
691     nvalues = a->getAttributeValues().size();
692
693     if (i == -1)
694         i = 0;
695     if (i >= nvalues)
696         return false;
697 #ifdef __APPLE__
698     av = (const saml2::AttributeValue *)((void *)(a->getAttributeValues().at(i)));
699 #else
700     av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i));
701 #endif
702     if (av != NULL) {
703         bool base64Encoded = isBase64EncodedAttributeValueP(av);
704
705         if (value != NULL) {
706             char *stringValue = toUTF8(av->getTextContent(), true);
707             size_t stringValueLen = strlen(stringValue);
708
709             if (base64Encoded) {
710                 ssize_t octetLen;
711
712                 value->value = GSSEAP_MALLOC(stringValueLen);
713                 if (value->value == NULL) {
714                     GSSEAP_FREE(stringValue);
715                     throw new std::bad_alloc;
716                 }
717
718                 octetLen = base64Decode(stringValue, value->value);
719                 if (octetLen < 0) {
720                     GSSEAP_FREE(value->value);
721                     GSSEAP_FREE(stringValue);
722                     value->value = NULL;
723                     return false;
724                 }
725                 value->length = octetLen;
726                 GSSEAP_FREE(stringValue);
727             } else {
728                 value->value = stringValue;
729                 value->length = stringValueLen;
730             }
731         }
732         if (display_value != NULL && base64Encoded == false) {
733             display_value->value = toUTF8(av->getTextContent(), true);
734             display_value->length = strlen((char *)value->value);
735         }
736     }
737
738     if (nvalues > ++i)
739         *more = i;
740
741     return true;
742 }
743
744 gss_any_t
745 gss_eap_saml_attr_provider::mapToAny(int authenticated GSSEAP_UNUSED,
746                                      gss_buffer_t type_id GSSEAP_UNUSED) const
747 {
748     return (gss_any_t)NULL;
749 }
750
751 void
752 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
753                                                   gss_any_t input GSSEAP_UNUSED) const
754 {
755 }
756
757 const char *
758 gss_eap_saml_attr_provider::prefix(void) const
759 {
760     return "urn:ietf:params:gss-eap:saml-attr";
761 }
762
763 bool
764 gss_eap_saml_attr_provider::init(void)
765 {
766     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML, createAttrContext);
767     return true;
768 }
769
770 void
771 gss_eap_saml_attr_provider::finalize(void)
772 {
773     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML);
774 }
775
776 gss_eap_attr_provider *
777 gss_eap_saml_attr_provider::createAttrContext(void)
778 {
779     return new gss_eap_saml_attr_provider;
780 }
781
782 OM_uint32
783 gssEapSamlAttrProvidersInit(OM_uint32 *minor)
784 {
785     if (!gss_eap_saml_assertion_provider::init() ||
786         !gss_eap_saml_attr_provider::init()) {
787         *minor = GSSEAP_SAML_INIT_FAILURE;
788         return GSS_S_FAILURE;
789     }
790
791     return GSS_S_COMPLETE;
792 }
793
794 OM_uint32
795 gssEapSamlAttrProvidersFinalize(OM_uint32 *minor)
796 {
797     gss_eap_saml_attr_provider::finalize();
798     gss_eap_saml_assertion_provider::finalize();
799
800     *minor = 0;
801     return GSS_S_COMPLETE;
802 }