3 * Copyright 2001-2006 Internet2
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * Library configuration
25 #include "exceptions.h"
26 #include "SAMLConfig.h"
27 #include "binding/ArtifactMap.h"
28 #include "binding/MessageEncoder.h"
29 #include "binding/SAMLArtifact.h"
30 #include "binding/URLEncoder.h"
31 #include "saml1/core/Assertions.h"
32 #include "saml1/core/Protocols.h"
33 #include "saml2/core/Protocols.h"
34 #include "saml2/metadata/Metadata.h"
35 #include "saml2/metadata/MetadataProvider.h"
36 #include "security/TrustEngine.h"
37 #include "util/SAMLConstants.h"
39 #include <xmltooling/XMLToolingConfig.h>
40 #include <xmltooling/signature/Signature.h>
41 #include <xmltooling/util/NDC.h>
43 #include <log4cpp/Category.hh>
44 #include <xsec/enc/XSECCryptoException.hpp>
45 #include <xsec/enc/XSECCryptoProvider.hpp>
46 #include <xsec/utils/XSECPlatformUtils.hpp>
47 #include <openssl/err.h>
49 using namespace opensaml;
50 using namespace xmlsignature;
51 using namespace xmltooling;
52 using namespace log4cpp;
55 // Expose entry points when used as an extension library
57 extern "C" int SAML_API xmltooling_extension_init(void*)
59 if (SAMLConfig::getConfig().init(false))
64 extern "C" void SAML_API xmltooling_extension_term()
66 SAMLConfig::getConfig().term(false);
69 DECL_EXCEPTION_FACTORY(ArtifactException,opensaml);
70 DECL_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
71 DECL_EXCEPTION_FACTORY(BindingException,opensaml);
74 SAMLInternalConfig g_config;
77 SAMLConfig& SAMLConfig::getConfig()
82 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
87 void SAMLConfig::setArtifactMap(ArtifactMap* artifactMap)
90 m_artifactMap = artifactMap;
93 void SAMLConfig::setURLEncoder(URLEncoder* urlEncoder)
96 m_urlEncoder = urlEncoder;
99 bool SAMLInternalConfig::init(bool initXMLTooling)
102 xmltooling::NDC ndc("init");
104 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
105 log.debug("library initialization started");
107 if (initXMLTooling) {
108 XMLToolingConfig::getConfig().init();
109 log.debug("XMLTooling library initialized");
112 REGISTER_EXCEPTION_FACTORY(ArtifactException,opensaml);
113 REGISTER_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
114 REGISTER_EXCEPTION_FACTORY(BindingException,opensaml);
116 registerMessageEncoders();
117 registerSAMLArtifacts();
118 saml1::registerAssertionClasses();
119 saml1p::registerProtocolClasses();
120 saml2::registerAssertionClasses();
121 saml2p::registerProtocolClasses();
122 saml2md::registerMetadataClasses();
123 saml2md::registerMetadataProviders();
124 saml2md::registerMetadataFilters();
125 registerTrustEngines();
127 m_urlEncoder = new URLEncoder();
129 log.info("library initialization complete");
133 void SAMLInternalConfig::term(bool termXMLTooling)
136 xmltooling::NDC ndc("term");
138 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
140 saml1::AssertionSchemaValidators.destroyValidators();
141 saml1p::ProtocolSchemaValidators.destroyValidators();
142 saml2::AssertionSchemaValidators.destroyValidators();
143 saml2md::MetadataSchemaValidators.destroyValidators();
145 TrustEngineManager.deregisterFactories();
146 MetadataFilterManager.deregisterFactories();
147 MetadataProviderManager.deregisterFactories();
148 SAMLArtifactManager.deregisterFactories();
149 MessageEncoderManager.deregisterFactories();
151 delete m_artifactMap;
152 m_artifactMap = NULL;
156 if (termXMLTooling) {
157 XMLToolingConfig::getConfig().term();
158 log.debug("XMLTooling library shut down");
160 log.info("library shutdown complete");
163 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
166 if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
167 throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
169 catch (XSECCryptoException& e) {
170 throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
174 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
177 auto_ptr<unsigned char> hold(new unsigned char[len]);
178 generateRandomBytes(hold.get(),len);
179 for (unsigned int i=0; i<len; i++)
180 buf+=(hold.get())[i];
183 XMLCh* SAMLInternalConfig::generateIdentifier()
185 unsigned char key[17];
186 generateRandomBytes(key,16);
189 sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
190 key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
191 key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
193 return XMLString::transcode(hexform);
196 string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
198 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
200 auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
202 auto_ptr<char> dup(strdup(s));
203 unsigned char buf[21];
204 hasher->hash(reinterpret_cast<unsigned char*>(dup.get()),strlen(dup.get()));
205 if (hasher->finish(buf,20)==20) {
208 for (unsigned int i=0; i<20; i++) {
209 ret+=(DIGITS[((unsigned char)(0xF0 & buf[i])) >> 4 ]);
210 ret+=(DIGITS[0x0F & buf[i]]);
214 for (unsigned int i=0; i<20; i++) {
221 throw XMLSecurityException("Unable to generate SHA-1 hash.");
224 void opensaml::log_openssl()
230 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
232 Category& log=Category::getInstance("OpenSSL");
233 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
234 if (data && (flags & ERR_TXT_STRING))
235 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
236 code=ERR_get_error_line_data(&file,&line,&data,&flags);