Add internal copy of the Xerces net accessor for libcurl, to get SSL support.
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLToolingConfig.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * XMLToolingConfig.cpp
19  * 
20  * Library configuration 
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "XMLToolingConfig.h"
26 #include "encryption/Encrypter.h"
27 #include "impl/UnknownElement.h"
28 #include "security/TrustEngine.h"
29 #include "security/OpenSSLCryptoX509CRL.h"
30 #include "security/CredentialResolver.h"
31 #include "security/KeyInfoResolver.h"
32 #include "signature/Signature.h"
33 #include "soap/SOAP.h"
34 #include "soap/SOAPTransport.h"
35 #include "util/CurlNetAccessor.h"
36 #include "util/NDC.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"
43
44 #ifdef HAVE_DLFCN_H
45 # include <dlfcn.h>
46 #endif
47
48 #include <stdexcept>
49 #include <curl/curl.h>
50 #include <log4cpp/Category.hh>
51 #include <log4cpp/PropertyConfigurator.hh>
52 #include <log4cpp/OstreamAppender.hh>
53 #include <xercesc/util/PlatformUtils.hpp>
54 #ifndef XMLTOOLING_NO_XMLSEC
55     #include <xsec/framework/XSECProvider.hpp>
56     #include <openssl/err.h>
57 #endif
58
59 using namespace soap11;
60 using namespace xmlencryption;
61 using namespace xmlsignature;
62 using namespace xmltooling;
63 using namespace log4cpp;
64 using namespace std;
65
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);
75
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);
80 #endif
81
82 namespace xmltooling {
83     static XMLToolingInternalConfig g_config;
84     static vector<Mutex*> g_openssl_locks;
85
86     extern "C" void openssl_locking_callback(int mode,int n,const char *file,int line)
87     {
88         if (mode & CRYPTO_LOCK)
89             g_openssl_locks[n]->lock();
90         else
91             g_openssl_locks[n]->unlock();
92     }
93     
94     #ifndef WIN32
95     extern "C" unsigned long openssl_thread_id(void)
96     {
97         return (unsigned long)(pthread_self());
98     }
99     #endif
100 }
101
102 XMLToolingConfig& XMLToolingConfig::getConfig()
103 {
104     return g_config;
105 }
106
107 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
108 {
109     return g_config;
110 }
111
112 bool XMLToolingInternalConfig::log_config(const char* config)
113 {
114     try {
115         if (!config || !*config)
116             config=getenv("XMLTOOLING_LOG_CONFIG");
117         if (!config || !*config)
118             config="WARN";
119         
120         bool level=false;
121         Category& root = Category::getRoot();
122         if (!strcmp(config,"DEBUG")) {
123             root.setPriority(Priority::DEBUG);
124             level=true;
125         }
126         else if (!strcmp(config,"INFO")) {
127             root.setPriority(Priority::INFO);
128             level=true;
129         }
130         else if (!strcmp(config,"NOTICE")) {
131             root.setPriority(Priority::NOTICE);
132             level=true;
133         }
134         else if (!strcmp(config,"WARN")) {
135             root.setPriority(Priority::WARN);
136             level=true;
137         }
138         else if (!strcmp(config,"ERROR")) {
139             root.setPriority(Priority::ERROR);
140             level=true;
141         }
142         else if (!strcmp(config,"CRIT")) {
143             root.setPriority(Priority::CRIT);
144             level=true;
145         }
146         else if (!strcmp(config,"ALERT")) {
147             root.setPriority(Priority::ALERT);
148             level=true;
149         }
150         else if (!strcmp(config,"EMERG")) {
151             root.setPriority(Priority::EMERG);
152             level=true;
153         }
154         else if (!strcmp(config,"FATAL")) {
155             root.setPriority(Priority::FATAL);
156             level=true;
157         }
158         if (level)
159             root.setAppender(new OstreamAppender("default",&cerr));
160         else
161             PropertyConfigurator::configure(config);
162     }
163     catch (const ConfigureFailure& e) {
164         Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
165         return false;
166     }
167     
168     return true;
169 }
170
171 void XMLToolingConfig::setReplayCache(ReplayCache* replayCache)
172 {
173     delete m_replayCache;
174     m_replayCache = replayCache;
175 }
176
177 void XMLToolingConfig::setTemplateEngine(TemplateEngine* templateEngine)
178 {
179     delete m_templateEngine;
180     m_templateEngine = templateEngine;
181 }
182
183 void XMLToolingConfig::setURLEncoder(URLEncoder* urlEncoder)
184 {
185     delete m_urlEncoder;
186     m_urlEncoder = urlEncoder;
187 }
188
189 bool XMLToolingInternalConfig::init()
190 {
191 #ifdef _DEBUG
192     xmltooling::NDC ndc("init");
193 #endif
194     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
195     try {
196         log.debug("library initialization started");
197
198         if (curl_global_init(CURL_GLOBAL_ALL)) {
199             log.fatal("failed to initialize libcurl, OpenSSL, or Winsock");
200             return false;
201         }
202         log.debug("libcurl %s initialization complete", LIBCURL_VERSION);
203
204         XMLPlatformUtils::Initialize();
205         auto_ptr<XMLNetAccessor> curler(new CurlNetAccessor());
206         delete XMLPlatformUtils::fgNetAccessor;
207         XMLPlatformUtils::fgNetAccessor = curler.release();
208         log.debug("Xerces initialization complete");
209
210 #ifndef XMLTOOLING_NO_XMLSEC
211         XSECPlatformUtils::Initialise();
212         m_xsecProvider=new XSECProvider();
213         log.debug("XMLSec initialization complete");
214 #endif
215
216         m_parserPool=new ParserPool();
217         m_validatingPool=new ParserPool(true,true);
218         m_lock=XMLPlatformUtils::makeMutex();
219         
220         // Load catalogs from path.
221         if (!catalog_path.empty()) {
222             char* catpath=strdup(catalog_path.c_str());
223             char* sep=NULL;
224             char* start=catpath;
225             while (start && *start) {
226                 sep=strchr(start,PATH_SEPARATOR_CHAR);
227                 if (sep)
228                     *sep=0;
229                 auto_ptr_XMLCh temp(start);
230                 m_validatingPool->loadCatalog(temp.get());
231                 start = sep ? sep + 1 : NULL;
232             }
233             free(catpath);
234         }
235
236         // default registrations
237         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
238
239         registerKeyInfoClasses();
240         registerEncryptionClasses();
241         registerSOAPClasses();
242
243         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLParserException,xmltooling);
244         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
245         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MarshallingException,xmltooling);
246         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
247         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
248         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
249         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ValidationException,xmltooling);
250         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(IOException,xmltooling);
251         
252 #ifndef XMLTOOLING_NO_XMLSEC
253         XMLObjectBuilder::registerBuilder(QName(xmlconstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
254         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
255         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SignatureException,xmlsignature);
256         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption);
257         registerKeyInfoResolvers();
258         registerCredentialResolvers();
259         registerTrustEngines();
260         registerXMLAlgorithms();
261 #endif
262         registerSOAPTransports();
263         initSOAPTransports();
264         registerStorageServices();
265
266         m_urlEncoder = new URLEncoder();
267         m_keyInfoResolver = KeyInfoResolverManager.newPlugin(INLINE_KEYINFO_RESOLVER,NULL);
268         
269         // Register xml:id as an ID attribute.        
270         static const XMLCh xmlid[] = UNICODE_LITERAL_2(i,d);
271         AttributeExtensibleXMLObject::registerIDAttribute(QName(xmlconstants::XML_NS, xmlid)); 
272     }
273     catch (const xercesc::XMLException&) {
274         log.fatal("caught exception while initializing Xerces");
275         curl_global_cleanup();
276         return false;
277     }
278
279     // Set up OpenSSL locking.
280     for (int i=0; i<CRYPTO_num_locks(); i++)
281         g_openssl_locks.push_back(Mutex::create());
282     CRYPTO_set_locking_callback(openssl_locking_callback);
283 #ifndef WIN32
284     CRYPTO_set_id_callback(openssl_thread_id);
285 #endif
286
287     log.info("library initialization complete");
288     return true;
289 }
290
291 void XMLToolingInternalConfig::term()
292 {
293     CRYPTO_set_locking_callback(NULL);
294     for_each(g_openssl_locks.begin(), g_openssl_locks.end(), xmltooling::cleanup<Mutex>());
295     g_openssl_locks.clear();
296
297     SchemaValidators.destroyValidators();
298     XMLObjectBuilder::destroyBuilders();
299     XMLToolingException::deregisterFactories();
300     AttributeExtensibleXMLObject::deregisterIDAttributes();
301
302     StorageServiceManager.deregisterFactories();
303     termSOAPTransports();
304     SOAPTransportManager.deregisterFactories();
305 #ifndef XMLTOOLING_NO_XMLSEC
306     TrustEngineManager.deregisterFactories();
307     CredentialResolverManager.deregisterFactories();
308     KeyInfoResolverManager.deregisterFactories();
309     m_algorithmMap.clear();
310 #endif
311
312     delete m_keyInfoResolver;
313     m_keyInfoResolver = NULL;
314
315     delete m_replayCache;
316     m_replayCache = NULL;
317     
318     delete m_templateEngine;
319     m_templateEngine = NULL;
320
321     delete m_urlEncoder;
322     m_urlEncoder = NULL;
323
324     for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
325 #if defined(WIN32)
326         FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
327         if (fn)
328             fn();
329         FreeLibrary(static_cast<HMODULE>(*i));
330 #elif defined(HAVE_DLFCN_H)
331         void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
332         if (fn)
333             fn();
334         dlclose(*i);
335 #else
336 # error "Don't know about dynamic loading on this platform!"
337 #endif
338     }
339     m_libhandles.clear();
340     
341     delete m_parserPool;
342     m_parserPool=NULL;
343     delete m_validatingPool;
344     m_validatingPool=NULL;
345
346 #ifndef XMLTOOLING_NO_XMLSEC
347     delete m_xsecProvider;
348     m_xsecProvider=NULL;
349     XSECPlatformUtils::Terminate();
350 #endif
351
352     XMLPlatformUtils::closeMutex(m_lock);
353     m_lock=NULL;
354     XMLPlatformUtils::Terminate();
355
356     curl_global_cleanup();
357     
358  #ifdef _DEBUG
359     xmltooling::NDC ndc("term");
360 #endif
361    Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
362 }
363
364 Lockable* XMLToolingInternalConfig::lock()
365 {
366     xercesc::XMLPlatformUtils::lockMutex(m_lock);
367     return this;
368 }
369
370 void XMLToolingInternalConfig::unlock()
371 {
372     xercesc::XMLPlatformUtils::unlockMutex(m_lock);
373 }
374
375 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
376 {
377 #ifdef _DEBUG
378     xmltooling::NDC ndc("LoadLibrary");
379 #endif
380     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
381     log.info("loading extension: %s", path);
382
383     Locker locker(this);
384
385 #if defined(WIN32)
386     HMODULE handle=NULL;
387     char* fixed=const_cast<char*>(path);
388     if (strchr(fixed,'/')) {
389         fixed=strdup(path);
390         char* p=fixed;
391         while (p=strchr(p,'/'))
392             *p='\\';
393     }
394
395     UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
396     try {
397         handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
398         if (!handle)
399              handle=LoadLibraryEx(fixed,NULL,0);
400         if (!handle)
401             throw runtime_error(string("unable to load extension library: ") + fixed);
402         FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
403         if (!fn)
404             throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
405         if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
406             throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
407         if (fixed!=path)
408             free(fixed);
409         SetErrorMode(em);
410     }
411     catch(runtime_error& e) {
412         log.error(e.what());
413         if (handle)
414             FreeLibrary(handle);
415         SetErrorMode(em);
416         if (fixed!=path)
417             free(fixed);
418         return false;
419     }
420
421 #elif defined(HAVE_DLFCN_H)
422     void* handle=dlopen(path,RTLD_LAZY);
423     if (!handle)
424         throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
425     int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
426     if (!fn) {
427         dlclose(handle);
428         throw runtime_error(
429             string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
430                 (dlerror() ? dlerror() : "unknown error")
431             );
432     }
433     try {
434         if (fn(context)!=0)
435             throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
436     }
437     catch(runtime_error& e) {
438         log.error(e.what());
439         if (handle)
440             dlclose(handle);
441         return false;
442     }
443 #else
444 # error "Don't know about dynamic loading on this platform!"
445 #endif
446     m_libhandles.push_back(handle);
447     log.info("loaded extension: %s", path);
448     return true;
449 }
450
451 #ifndef XMLTOOLING_NO_XMLSEC
452 void xmltooling::log_openssl()
453 {
454     const char* file;
455     const char* data;
456     int flags,line;
457
458     unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
459     while (code) {
460         Category& log=Category::getInstance("OpenSSL");
461         log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
462         if (data && (flags & ERR_TXT_STRING))
463             log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
464         code=ERR_get_error_line_data(&file,&line,&data,&flags);
465     }
466 }
467
468 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
469 {
470     return new OpenSSLCryptoX509CRL();
471 }
472
473 void XMLToolingInternalConfig::registerXMLAlgorithms()
474 {
475     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_MD5, "RSA", 0);
476     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA1, "RSA", 0);
477     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA224, "RSA", 0);
478     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA256, "RSA", 0);
479     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA384, "RSA", 0);
480     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA512, "RSA", 0);
481
482     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_1_5, "RSA", 0);
483     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_OAEP_MGFP1, "RSA", 0);
484
485     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIDSA_SHA1, "DSA", 0);
486
487     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA1, "HMAC", 0);
488     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA224, "HMAC", 0);
489     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA256, "HMAC", 0);
490     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA384, "HMAC", 0);
491     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA512, "HMAC", 0);
492
493     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURI3DES_CBC, "DESede", 192);
494     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_3DES, "DESede", 192);
495
496     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES128_CBC, "AES", 128);
497     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES128, "AES", 128);
498
499     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES192_CBC, "AES", 192);
500     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES192, "AES", 192);
501
502     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES256_CBC, "AES", 256);
503     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES256, "AES", 256);
504 }
505 #endif