2 * Copyright 2001-2007 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.
18 * XMLToolingConfig.cpp
20 * Library configuration
24 #include "exceptions.h"
25 #include "XMLToolingConfig.h"
26 #include "encryption/Encryption.h"
27 #include "encryption/Encrypter.h"
28 #include "impl/UnknownElement.h"
29 #include "security/TrustEngine.h"
30 #include "security/OpenSSLCryptoX509CRL.h"
31 #include "security/CredentialResolver.h"
32 #include "security/KeyInfoResolver.h"
33 #include "signature/Signature.h"
34 #include "soap/SOAP.h"
35 #include "soap/SOAPTransport.h"
37 #include "util/ReplayCache.h"
38 #include "util/StorageService.h"
39 #include "util/TemplateEngine.h"
40 #include "util/URLEncoder.h"
41 #include "util/XMLConstants.h"
42 #include "validation/ValidatorSuite.h"
49 #include <log4cpp/Category.hh>
50 #include <log4cpp/PropertyConfigurator.hh>
51 #include <log4cpp/OstreamAppender.hh>
52 #include <xercesc/util/PlatformUtils.hpp>
53 #ifndef XMLTOOLING_NO_XMLSEC
54 # include <curl/curl.h>
55 # include <openssl/err.h>
56 # include <xsec/framework/XSECProvider.hpp>
59 using namespace soap11;
60 using namespace xmlencryption;
61 using namespace xmlsignature;
62 using namespace xmltooling;
63 using namespace log4cpp;
66 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLParserException,xmltooling);
67 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
68 DECL_XMLTOOLING_EXCEPTION_FACTORY(MarshallingException,xmltooling);
69 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
70 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
71 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
72 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
73 DECL_XMLTOOLING_EXCEPTION_FACTORY(ValidationException,xmltooling);
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(IOException,xmltooling);
76 #ifndef XMLTOOLING_NO_XMLSEC
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(SignatureException,xmlsignature);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption);
82 namespace xmltooling {
83 static XMLToolingInternalConfig g_config;
84 #ifndef XMLTOOLING_NO_XMLSEC
85 static vector<Mutex*> g_openssl_locks;
87 extern "C" void openssl_locking_callback(int mode,int n,const char *file,int line)
89 if (mode & CRYPTO_LOCK)
90 g_openssl_locks[n]->lock();
92 g_openssl_locks[n]->unlock();
96 extern "C" unsigned long openssl_thread_id(void)
98 return (unsigned long)(pthread_self());
104 XMLToolingConfig& XMLToolingConfig::getConfig()
109 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
114 bool XMLToolingInternalConfig::log_config(const char* config)
117 if (!config || !*config)
118 config=getenv("XMLTOOLING_LOG_CONFIG");
119 if (!config || !*config)
123 Category& root = Category::getRoot();
124 if (!strcmp(config,"DEBUG")) {
125 root.setPriority(Priority::DEBUG);
128 else if (!strcmp(config,"INFO")) {
129 root.setPriority(Priority::INFO);
132 else if (!strcmp(config,"NOTICE")) {
133 root.setPriority(Priority::NOTICE);
136 else if (!strcmp(config,"WARN")) {
137 root.setPriority(Priority::WARN);
140 else if (!strcmp(config,"ERROR")) {
141 root.setPriority(Priority::ERROR);
144 else if (!strcmp(config,"CRIT")) {
145 root.setPriority(Priority::CRIT);
148 else if (!strcmp(config,"ALERT")) {
149 root.setPriority(Priority::ALERT);
152 else if (!strcmp(config,"EMERG")) {
153 root.setPriority(Priority::EMERG);
156 else if (!strcmp(config,"FATAL")) {
157 root.setPriority(Priority::FATAL);
161 root.setAppender(new OstreamAppender("default",&cerr));
163 PropertyConfigurator::configure(config);
165 catch (const ConfigureFailure& e) {
166 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
173 void XMLToolingConfig::setReplayCache(ReplayCache* replayCache)
175 delete m_replayCache;
176 m_replayCache = replayCache;
179 void XMLToolingConfig::setTemplateEngine(TemplateEngine* templateEngine)
181 delete m_templateEngine;
182 m_templateEngine = templateEngine;
185 void XMLToolingConfig::setURLEncoder(URLEncoder* urlEncoder)
188 m_urlEncoder = urlEncoder;
191 bool XMLToolingInternalConfig::init()
194 xmltooling::NDC ndc("init");
196 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
198 log.debug("library initialization started");
200 #ifndef XMLTOOLING_NO_XMLSEC
201 if (curl_global_init(CURL_GLOBAL_ALL)) {
202 log.fatal("failed to initialize libcurl, OpenSSL, or Winsock");
205 log.debug("libcurl %s initialization complete", LIBCURL_VERSION);
208 XMLPlatformUtils::Initialize();
209 log.debug("Xerces initialization complete");
211 #ifndef XMLTOOLING_NO_XMLSEC
212 XSECPlatformUtils::Initialise();
213 m_xsecProvider=new XSECProvider();
214 log.debug("XMLSec initialization complete");
217 m_parserPool=new ParserPool();
218 m_validatingPool=new ParserPool(true,true);
219 m_lock=XMLPlatformUtils::makeMutex();
221 // Load catalogs from path.
222 if (!catalog_path.empty()) {
223 char* catpath=strdup(catalog_path.c_str());
226 while (start && *start) {
227 sep=strchr(start,PATH_SEPARATOR_CHAR);
230 auto_ptr_XMLCh temp(start);
231 m_validatingPool->loadCatalog(temp.get());
232 start = sep ? sep + 1 : NULL;
237 // default registrations
238 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
240 registerKeyInfoClasses();
241 registerEncryptionClasses();
242 registerSOAPClasses();
244 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLParserException,xmltooling);
245 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
246 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MarshallingException,xmltooling);
247 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
248 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
249 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
250 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ValidationException,xmltooling);
251 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(IOException,xmltooling);
253 #ifndef XMLTOOLING_NO_XMLSEC
254 XMLObjectBuilder::registerBuilder(QName(xmlconstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
255 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
256 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SignatureException,xmlsignature);
257 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption);
258 registerKeyInfoResolvers();
259 registerCredentialResolvers();
260 registerTrustEngines();
261 registerXMLAlgorithms();
262 registerSOAPTransports();
263 initSOAPTransports();
265 registerStorageServices();
267 m_urlEncoder = new URLEncoder();
268 #ifndef XMLTOOLING_NO_XMLSEC
269 m_keyInfoResolver = KeyInfoResolverManager.newPlugin(INLINE_KEYINFO_RESOLVER,NULL);
272 // Register xml:id as an ID attribute.
273 static const XMLCh xmlid[] = UNICODE_LITERAL_2(i,d);
274 AttributeExtensibleXMLObject::registerIDAttribute(QName(xmlconstants::XML_NS, xmlid));
276 catch (const xercesc::XMLException&) {
277 log.fatal("caught exception while initializing Xerces");
278 #ifndef XMLTOOLING_NO_XMLSEC
279 curl_global_cleanup();
284 #ifndef XMLTOOLING_NO_XMLSEC
285 // Set up OpenSSL locking.
286 for (int i=0; i<CRYPTO_num_locks(); i++)
287 g_openssl_locks.push_back(Mutex::create());
288 CRYPTO_set_locking_callback(openssl_locking_callback);
290 CRYPTO_set_id_callback(openssl_thread_id);
294 log.info("library initialization complete");
298 void XMLToolingInternalConfig::term()
300 #ifndef XMLTOOLING_NO_XMLSEC
301 CRYPTO_set_locking_callback(NULL);
302 for_each(g_openssl_locks.begin(), g_openssl_locks.end(), xmltooling::cleanup<Mutex>());
303 g_openssl_locks.clear();
306 SchemaValidators.destroyValidators();
307 XMLObjectBuilder::destroyBuilders();
308 XMLToolingException::deregisterFactories();
309 AttributeExtensibleXMLObject::deregisterIDAttributes();
311 StorageServiceManager.deregisterFactories();
313 #ifndef XMLTOOLING_NO_XMLSEC
314 termSOAPTransports();
315 SOAPTransportManager.deregisterFactories();
316 TrustEngineManager.deregisterFactories();
317 CredentialResolverManager.deregisterFactories();
318 KeyInfoResolverManager.deregisterFactories();
319 m_algorithmMap.clear();
321 delete m_keyInfoResolver;
322 m_keyInfoResolver = NULL;
325 delete m_replayCache;
326 m_replayCache = NULL;
328 delete m_templateEngine;
329 m_templateEngine = NULL;
334 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
336 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
339 FreeLibrary(static_cast<HMODULE>(*i));
340 #elif defined(HAVE_DLFCN_H)
341 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
346 # error "Don't know about dynamic loading on this platform!"
349 m_libhandles.clear();
353 delete m_validatingPool;
354 m_validatingPool=NULL;
356 #ifndef XMLTOOLING_NO_XMLSEC
357 delete m_xsecProvider;
359 XSECPlatformUtils::Terminate();
362 XMLPlatformUtils::closeMutex(m_lock);
364 XMLPlatformUtils::Terminate();
366 #ifndef XMLTOOLING_NO_XMLSEC
367 curl_global_cleanup();
370 xmltooling::NDC ndc("term");
372 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
375 Lockable* XMLToolingInternalConfig::lock()
377 xercesc::XMLPlatformUtils::lockMutex(m_lock);
381 void XMLToolingInternalConfig::unlock()
383 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
386 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
389 xmltooling::NDC ndc("LoadLibrary");
391 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
392 log.info("loading extension: %s", path);
398 char* fixed=const_cast<char*>(path);
399 if (strchr(fixed,'/')) {
402 while (p=strchr(p,'/'))
406 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
408 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
410 handle=LoadLibraryEx(fixed,NULL,0);
412 throw runtime_error(string("unable to load extension library: ") + fixed);
413 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
415 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
416 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
417 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
422 catch(runtime_error& e) {
432 #elif defined(HAVE_DLFCN_H)
433 void* handle=dlopen(path,RTLD_LAZY);
435 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
436 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
440 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
441 (dlerror() ? dlerror() : "unknown error")
446 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
448 catch(runtime_error& e) {
455 # error "Don't know about dynamic loading on this platform!"
457 m_libhandles.push_back(handle);
458 log.info("loaded extension: %s", path);
462 #ifndef XMLTOOLING_NO_XMLSEC
463 void xmltooling::log_openssl()
469 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
471 Category& log=Category::getInstance("OpenSSL");
472 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
473 if (data && (flags & ERR_TXT_STRING))
474 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
475 code=ERR_get_error_line_data(&file,&line,&data,&flags);
479 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
481 return new OpenSSLCryptoX509CRL();
484 void XMLToolingInternalConfig::registerXMLAlgorithms()
486 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_MD5, "RSA", 0);
487 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA1, "RSA", 0);
488 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA224, "RSA", 0);
489 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA256, "RSA", 0);
490 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA384, "RSA", 0);
491 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA512, "RSA", 0);
493 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_1_5, "RSA", 0);
494 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_OAEP_MGFP1, "RSA", 0);
496 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIDSA_SHA1, "DSA", 0);
498 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA1, "HMAC", 0);
499 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA224, "HMAC", 0);
500 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA256, "HMAC", 0);
501 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA384, "HMAC", 0);
502 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA512, "HMAC", 0);
504 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURI3DES_CBC, "DESede", 192);
505 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_3DES, "DESede", 192);
507 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES128_CBC, "AES", 128);
508 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES128, "AES", 128);
510 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES192_CBC, "AES", 192);
511 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES192, "AES", 192);
513 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES256_CBC, "AES", 256);
514 registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES256, "AES", 256);