SAML TrustEngine wrappers, ExplicitKeyTrustEngine plugin.
[shibboleth/cpp-opensaml.git] / saml / SAMLConfig.cpp
index efd3e1c..e154646 100644 (file)
  * Library configuration 
  */
 
-#define SAML_DECLARE_VALIDATORS
-
 #include "internal.h"
 #include "exceptions.h"
+#include "SAMLArtifact.h"
 #include "SAMLConfig.h"
 #include "saml1/core/Assertions.h"
 #include "saml1/core/Protocols.h"
-#include "saml2/core/Assertions.h"
+#include "saml2/core/Protocols.h"
+#include "saml2/metadata/Metadata.h"
+#include "saml2/metadata/MetadataProvider.h"
+#include "security/TrustEngine.h"
 #include "util/SAMLConstants.h"
 
 #include <xmltooling/XMLToolingConfig.h>
 
 #include <log4cpp/Category.hh>
 #include <xsec/enc/XSECCryptoException.hpp>
+#include <xsec/enc/XSECCryptoProvider.hpp>
 #include <xsec/utils/XSECPlatformUtils.hpp>
 
+
 using namespace opensaml;
 using namespace xmlsignature;
 using namespace xmltooling;
 using namespace log4cpp;
 using namespace std;
 
-//DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
+DECL_EXCEPTION_FACTORY(ArtifactException,opensaml);
+DECL_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
 
 namespace opensaml {
    SAMLInternalConfig g_config;
@@ -71,9 +76,18 @@ bool SAMLInternalConfig::init()
     XMLToolingConfig::getConfig().init();
     log.debug("XMLTooling library initialized");
 
+    REGISTER_EXCEPTION_FACTORY(ArtifactException,opensaml);
+    REGISTER_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
+
+    registerSAMLArtifacts();
     saml1::registerAssertionClasses();
     saml1p::registerProtocolClasses();
     saml2::registerAssertionClasses();
+    saml2p::registerProtocolClasses();
+    saml2md::registerMetadataClasses();
+    saml2md::registerMetadataProviders();
+    saml2md::registerMetadataFilters();
+    registerTrustEngines();
 
     log.info("library initialization complete");
     return true;
@@ -84,6 +98,17 @@ void SAMLInternalConfig::term()
 #ifdef _DEBUG
     xmltooling::NDC ndc("term");
 #endif
+
+    saml1::AssertionSchemaValidators.destroyValidators();
+    saml1p::ProtocolSchemaValidators.destroyValidators();
+    saml2::AssertionSchemaValidators.destroyValidators();
+    saml2md::MetadataSchemaValidators.destroyValidators();
+    
+    SAMLArtifactManager.deregisterFactories();
+    MetadataFilterManager.deregisterFactories();
+    MetadataProviderManager.deregisterFactories();
+    TrustEngineManager.deregisterFactories();
+
     XMLToolingConfig::getConfig().term();
     Category::getInstance(SAML_LOGCAT".SAMLConfig").info("library shutdown complete");
 }
@@ -120,3 +145,31 @@ XMLCh* SAMLInternalConfig::generateIdentifier()
     hexform[33]=0;
     return XMLString::transcode(hexform);
 }
+
+string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
+{
+    static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+    auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
+    if (hasher.get()) {
+        auto_ptr<char> dup(strdup(s));
+        unsigned char buf[21];
+        hasher->hash(reinterpret_cast<unsigned char*>(dup.get()),strlen(dup.get()));
+        if (hasher->finish(buf,20)==20) {
+            string ret;
+            if (toHex) {
+                for (unsigned int i=0; i<20; i++) {
+                    ret+=(DIGITS[((unsigned char)(0xF0 & buf[i])) >> 4 ]);
+                    ret+=(DIGITS[0x0F & buf[i]]);
+                }
+            }
+            else {
+                for (unsigned int i=0; i<20; i++) {
+                    ret+=buf[i];
+                }
+            }
+            return ret;
+        }
+    }
+    throw XMLSecurityException("Unable to generate SHA-1 hash.");
+}