2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Library configuration
24 #include "exceptions.h"
25 #include "SAMLArtifact.h"
26 #include "SAMLConfig.h"
27 #include "saml1/core/Assertions.h"
28 #include "saml1/core/Protocols.h"
29 #include "saml2/core/Protocols.h"
30 #include "saml2/metadata/Metadata.h"
31 #include "saml2/metadata/MetadataProvider.h"
32 #include "security/TrustEngine.h"
33 #include "util/SAMLConstants.h"
35 #include <xmltooling/XMLToolingConfig.h>
36 #include <xmltooling/signature/Signature.h>
37 #include <xmltooling/util/NDC.h>
39 #include <log4cpp/Category.hh>
40 #include <xsec/enc/XSECCryptoException.hpp>
41 #include <xsec/enc/XSECCryptoProvider.hpp>
42 #include <xsec/utils/XSECPlatformUtils.hpp>
45 using namespace opensaml;
46 using namespace xmlsignature;
47 using namespace xmltooling;
48 using namespace log4cpp;
51 DECL_EXCEPTION_FACTORY(ArtifactException,opensaml);
52 DECL_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
55 SAMLInternalConfig g_config;
58 SAMLConfig& SAMLConfig::getConfig()
63 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
68 bool SAMLInternalConfig::init()
71 xmltooling::NDC ndc("init");
73 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
74 log.debug("library initialization started");
76 XMLToolingConfig::getConfig().init();
77 log.debug("XMLTooling library initialized");
79 REGISTER_EXCEPTION_FACTORY(ArtifactException,opensaml);
80 REGISTER_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
82 registerSAMLArtifacts();
83 saml1::registerAssertionClasses();
84 saml1p::registerProtocolClasses();
85 saml2::registerAssertionClasses();
86 saml2p::registerProtocolClasses();
87 saml2md::registerMetadataClasses();
88 saml2md::registerMetadataProviders();
89 saml2md::registerMetadataFilters();
90 registerTrustEngines();
92 log.info("library initialization complete");
96 void SAMLInternalConfig::term()
99 xmltooling::NDC ndc("term");
102 saml1::AssertionSchemaValidators.destroyValidators();
103 saml1p::ProtocolSchemaValidators.destroyValidators();
104 saml2::AssertionSchemaValidators.destroyValidators();
105 saml2md::MetadataSchemaValidators.destroyValidators();
107 SAMLArtifactManager.deregisterFactories();
108 MetadataFilterManager.deregisterFactories();
109 MetadataProviderManager.deregisterFactories();
110 TrustEngineManager.deregisterFactories();
112 XMLToolingConfig::getConfig().term();
113 Category::getInstance(SAML_LOGCAT".SAMLConfig").info("library shutdown complete");
116 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
119 if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
120 throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
122 catch (XSECCryptoException& e) {
123 throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
127 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
130 auto_ptr<unsigned char> hold(new unsigned char[len]);
131 generateRandomBytes(hold.get(),len);
132 for (unsigned int i=0; i<len; i++)
133 buf+=(hold.get())[i];
136 XMLCh* SAMLInternalConfig::generateIdentifier()
138 unsigned char key[17];
139 generateRandomBytes(key,16);
142 sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
143 key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
144 key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
146 return XMLString::transcode(hexform);
149 string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
151 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
153 auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
155 auto_ptr<char> dup(strdup(s));
156 unsigned char buf[21];
157 hasher->hash(reinterpret_cast<unsigned char*>(dup.get()),strlen(dup.get()));
158 if (hasher->finish(buf,20)==20) {
161 for (unsigned int i=0; i<20; i++) {
162 ret+=(DIGITS[((unsigned char)(0xF0 & buf[i])) >> 4 ]);
163 ret+=(DIGITS[0x0F & buf[i]]);
167 for (unsigned int i=0; i<20; i++) {
174 throw XMLSecurityException("Unable to generate SHA-1 hash.");