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