check syntax before decoding base64 encoded SAML attributes
[moonshot.git] / mech_eap / util_saml.cpp
index b4ab0ea..c37d8fd 100644 (file)
@@ -57,6 +57,9 @@ using namespace opensaml;
 using namespace xercesc;
 using namespace std;
 
+static const XMLCh
+base64Binary[] = {'b','a','s','e','6','4','B','i','n','a','r','y',0};
+
 /*
  * gss_eap_saml_assertion_provider is for retrieving the underlying
  * assertion.
@@ -283,7 +286,11 @@ gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
 
     XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
 
-    duplicateBuffer(str, value);
+    if (value != NULL)
+        duplicateBuffer(str, value);
+    if (display_value != NULL)
+        duplicateBuffer(str, display_value);
+
     *more = 0;
 
     return true;
@@ -415,18 +422,16 @@ gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAtt
 
         for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
             const XMLCh *attributeName, *attributeNameFormat;
-            XMLCh *qualifiedName;
             XMLCh space[2] = { ' ', 0 };
             gss_buffer_desc utf8;
-            bool ret;
 
             attributeName = (*a)->getName();
             attributeNameFormat = (*a)->getNameFormat();
             if (attributeNameFormat == NULL || attributeNameFormat[0] == '\0')
                 attributeNameFormat = saml2::Attribute::UNSPECIFIED;
 
-            qualifiedName = new XMLCh[XMLString::stringLen(attributeNameFormat) + 1 +
-                                      XMLString::stringLen(attributeName) + 1];
+            XMLCh qualifiedName[XMLString::stringLen(attributeNameFormat) + 1 +
+                                XMLString::stringLen(attributeName) + 1];
             XMLString::copyString(qualifiedName, attributeNameFormat);
             XMLString::catString(qualifiedName, space);
             XMLString::catString(qualifiedName, attributeName);
@@ -434,12 +439,8 @@ gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAtt
             utf8.value = (void *)toUTF8(qualifiedName);
             utf8.length = strlen((char *)utf8.value);
 
-            ret = addAttribute(m_manager, this, &utf8, data);
-
-            delete qualifiedName;
-
-            if (!ret)
-                return ret;
+            if (!addAttribute(m_manager, this, &utf8, data))
+                return false;
         }
     }
 
@@ -449,12 +450,11 @@ gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAtt
 static BaseRefVectorOf<XMLCh> *
 decomposeAttributeName(const gss_buffer_t attr)
 {
-    XMLCh *qualifiedAttr = new XMLCh[attr->length + 1];
-    XMLString::transcode((const char *)attr->value, qualifiedAttr, attr->length);
-
-    BaseRefVectorOf<XMLCh> *components = XMLString::tokenizeString(qualifiedAttr);
+    BaseRefVectorOf<XMLCh> *components;
+    string str((const char *)attr->value, attr->length);
+    auto_ptr_XMLCh qualifiedAttr(str.c_str());
 
-    delete qualifiedAttr;
+    components = XMLString::tokenizeString(qualifiedAttr.get());
 
     if (components->size() != 2) {
         delete components;
@@ -464,6 +464,25 @@ decomposeAttributeName(const gss_buffer_t attr)
     return components;
 }
 
+static bool
+isNotPrintableP(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,
@@ -493,11 +512,23 @@ 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 (isNotPrintableP(value)) {
+        /* XXX FIXME where is setSchemaType()? */
+        xmltooling::QName base64SchemaType(xmlconstants::XSD_NS,
+                                           base64Binary,
+                                           xmlconstants::XSD_PREFIX);
+        char *b64;
+
+        if (base64Encode(value->value, value->length, &b64) < 0)
+            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);
 
@@ -505,7 +536,6 @@ gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
     attributeStatement->getAttributes().push_back(attribute);
 
     delete components;
-    delete xmlValue;
 
     return true;
 }
@@ -618,6 +648,29 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
     return (ret != NULL);
 }
 
+static bool
+isBase64EncodedAttributeValueP(const saml2::AttributeValue *av)
+{
+    const xmltooling::QName *type = av->getSchemaType();
+
+    if (type == NULL)
+        return false;
+
+    if (!type->hasNamespaceURI() ||
+        !XMLString::equals(type->getNamespaceURI(), xmlconstants::XSD_NS))
+        return false;
+
+    if (!type->hasPrefix() ||
+        !XMLString::equals(type->getPrefix(), xmlconstants::XSD_PREFIX))
+        return false;
+
+    if (!type->hasLocalPart() ||
+        !XMLString::equals(type->getLocalPart(), base64Binary))
+        return false;
+
+    return true;
+}
+
 bool
 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
                                          int *authenticated,
@@ -639,7 +692,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,11 +700,36 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
     av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i));
 #endif
     if (av != NULL) {
+        bool base64Encoded = isBase64EncodedAttributeValueP(av);
+
         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 (base64Encoded) {
+                ssize_t octetLen;
+
+                value->value = GSSEAP_MALLOC(stringValueLen);
+                if (value->value == NULL) {
+                    GSSEAP_FREE(stringValue);
+                    throw new std::bad_alloc;
+                }
+
+                octetLen = base64Decode(stringValue, value->value);
+                if (octetLen < 0) {
+                    GSSEAP_FREE(value->value);
+                    GSSEAP_FREE(stringValue);
+                    value->value = NULL;
+                    return false;
+                }
+                value->length = octetLen;
+                GSSEAP_FREE(stringValue);
+            } else {
+                value->value = stringValue;
+                value->length = stringValueLen;
+            }
         }
-        if (display_value != NULL) {
+        if (display_value != NULL && base64Encoded == false) {
             display_value->value = toUTF8(av->getTextContent(), true);
             display_value->length = strlen((char *)value->value);
         }