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