automatically decode base64 encoded SAML values
[moonshot.git] / mech_eap / util_saml.cpp
index d02fa73..9658bf0 100644 (file)
@@ -449,11 +449,9 @@ decomposeAttributeName(const gss_buffer_t attr)
 {
     BaseRefVectorOf<XMLCh> *components;
     string str((const char *)attr->value, attr->length);
-    XMLCh qualifiedAttr[str.length() + 1];
+    auto_ptr_XMLCh qualifiedAttr(str.c_str());
 
-    XMLString::transcode(str.c_str(), qualifiedAttr, str.length());
-
-    components = XMLString::tokenizeString(qualifiedAttr);
+    components = XMLString::tokenizeString(qualifiedAttr.get());
 
     if (components->size() != 2) {
         delete components;
@@ -463,6 +461,25 @@ decomposeAttributeName(const gss_buffer_t attr)
     return components;
 }
 
+static bool
+isNotPrintable(const gss_buffer_t value)
+{
+    size_t i;
+    char *p = (char *)value->value;
+
+    if (isgraph(p[0]) &&
+        isgraph(p[value->length - 1]))
+    {
+        for (i = 0; p[i]; i++) {
+            if (!isascii(p[i]) || !isprint(p[i]))
+                return true;
+        }
+        return false;
+    }
+
+    return true;
+}
+
 bool
 gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
                                          const gss_buffer_t attr,
@@ -492,11 +509,19 @@ gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
     attribute->setNameFormat(components->elementAt(0));
     attribute->setName(components->elementAt(1));
 
-    XMLCh *xmlValue = new XMLCh[value->length + 1];
-    XMLString::transcode((const char *)value->value, xmlValue, attr->length);
-
     attributeValue = saml2::AttributeValueBuilder::buildAttributeValue();
-    attributeValue->setTextContent(xmlValue);
+    if (isNotPrintable(value)) {
+        char *b64;
+
+        if (base64Encode(value->value, value->length, &b64))
+            return false;
+
+        auto_ptr_XMLCh unistr(b64);
+        attributeValue->setTextContent(unistr.get());
+    } else {
+        auto_ptr_XMLCh unistr((char *)value->value);
+        attributeValue->setTextContent(unistr.get());
+    }
 
     attribute->getAttributeValues().push_back(attributeValue);
 
@@ -504,7 +529,6 @@ gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
     attributeStatement->getAttributes().push_back(attribute);
 
     delete components;
-    delete xmlValue;
 
     return true;
 }
@@ -638,7 +662,7 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
 
     if (i == -1)
         i = 0;
-    else if (i >= nvalues)
+    if (i >= nvalues)
         return false;
 #ifdef __APPLE__
     av = (const saml2::AttributeValue *)((void *)(a->getAttributeValues().at(i)));
@@ -647,8 +671,27 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
 #endif
     if (av != NULL) {
         if (value != NULL) {
-            value->value = toUTF8(av->getTextContent(), true);
-            value->length = strlen((char *)value->value);
+            char *stringValue = toUTF8(av->getTextContent(), true);
+            size_t stringValueLen = strlen(stringValue);
+
+            if (base64Valid(stringValue)) {
+                ssize_t binaryLen;
+
+                value->value = GSSEAP_MALLOC(stringValueLen);
+                if (value->value == NULL)
+                    throw new std::bad_alloc;
+
+                binaryLen = base64Decode(stringValue, value->value);
+                if (binaryLen < 0) {
+                    GSSEAP_FREE(value->value);
+                    value->value = NULL;
+                    return false;
+                }
+                value->length = binaryLen;
+            } else {
+                value->value = stringValue;
+                value->length = stringValueLen;
+            }
         }
         if (display_value != NULL) {
             display_value->value = toUTF8(av->getTextContent(), true);