Convert from NULL macro to nullptr, remove unused zlib code.
[shibboleth/cpp-opensaml.git] / saml / signature / ContentReference.cpp
index b224fcb..eb74162 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2007 Internet2
+ *  Copyright 2001-2010 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
 /**
  * ContentReference.cpp
  * 
- * SAML-specific signature reference profile 
+ * SAML-specific signature reference profile.
  */
  
 #include "internal.h"
 #include <xmltooling/signature/Signature.h>
 #include <xercesc/util/XMLUniDefs.hpp>
 #include <xsec/dsig/DSIGReference.hpp>
+#include <xsec/dsig/DSIGSignature.hpp>
 #include <xsec/dsig/DSIGTransformC14n.hpp>
 
 using namespace opensaml;
+using namespace xmltooling;
 using namespace std;
 
-class _addprefix : public binary_function<DSIGTransformC14n*,string,void> {
-public:
-    void operator()(DSIGTransformC14n* t, const string& s) const {
-        if (s.empty())
-            t->addInclusiveNamespace("#default");
-        else 
-            t->addInclusiveNamespace(s.c_str());
-    }
-};
+ContentReference::ContentReference(const SignableObject& signableObject)
+    : m_signableObject(signableObject), m_digest(nullptr), m_c14n(nullptr)
+{
+}
+
+ContentReference::~ContentReference()
+{
+}
 
 void ContentReference::createReferences(DSIGSignature* sig)
 {
-    DSIGReference* ref=NULL;
+    DSIGReference* ref = nullptr;
     const XMLCh* id=m_signableObject.getXMLID();
     if (!id || !*id)
-        ref=sig->createReference(&chNull);  // whole doc reference
+        ref=sig->createReference(&chNull, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1);  // whole doc reference
     else {
         XMLCh* buf=new XMLCh[XMLString::stringLen(id) + 2];
         buf[0]=chPound;
         buf[1]=chNull;
         XMLString::catString(buf,id);
         try {
-            ref=sig->createReference(buf);
+            ref=sig->createReference(buf, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1);
             delete[] buf;
         }
         catch(...) {
@@ -62,7 +63,41 @@ void ContentReference::createReferences(DSIGSignature* sig)
             throw;
         }
     }
+    
     ref->appendEnvelopedSignatureTransform();
-    DSIGTransformC14n* c14n=ref->appendCanonicalizationTransform(CANON_C14NE_NOC);
-    for_each(m_prefixes.begin(), m_prefixes.end(), bind1st(_addprefix(),c14n));
+    DSIGTransformC14n* c14n=ref->appendCanonicalizationTransform(m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC);
+
+    if (!m_c14n || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_NOC || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_COM) {
+        // Compute inclusive prefix set.
+        set<xstring> prefix_set;
+        XMLHelper::getNonVisiblyUsedPrefixes(m_signableObject, prefix_set);
+        prefix_set.insert(m_prefixes.begin(), m_prefixes.end());
+
+        // Build up the string of prefixes.
+        xstring prefixes;
+        static const XMLCh _default[] = { chPound, chLatin_d, chLatin_e, chLatin_f, chLatin_a, chLatin_u, chLatin_l, chLatin_t, chNull };
+        for (set<xstring>::const_iterator p = prefix_set.begin(); p != prefix_set.end(); ++p) {
+            prefixes += (p->empty() ? _default : p->c_str());
+            prefixes += chSpace;
+        }
+        if (!prefixes.empty()) {
+            prefixes.erase(prefixes.begin() + prefixes.size() - 1);
+            c14n->setInclusiveNamespaces(XMLString::replicate(prefixes.c_str()));
+        }
+    }
+}
+
+void ContentReference::addInclusivePrefix(const XMLCh* prefix)
+{
+    m_prefixes.insert(prefix ? prefix : &chNull);
+}
+
+void ContentReference::setDigestAlgorithm(const XMLCh* digest)
+{
+    m_digest = digest;
+}
+
+void ContentReference::setCanonicalizationMethod(const XMLCh* c14n)
+{
+    m_c14n = c14n;
 }