1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
/*
* Copyright 2001-2005 Internet2
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* SAMLConfig.cpp - SAML runtime configuration
Scott Cantor
2/20/02
$History:$
*/
#include "internal.h"
#ifdef HAVE_DLFCN_H
# include <dlfcn.h>
#endif
#include <curl/curl.h>
#include <log4cpp/Category.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <xercesc/util/PlatformUtils.hpp>
#include <xsec/framework/XSECProvider.hpp>
#include <fstream>
using namespace saml;
using namespace log4cpp;
using namespace std;
SAML_EXCEPTION_FACTORY(MalformedException);
SAML_EXCEPTION_FACTORY(UnsupportedExtensionException);
SAML_EXCEPTION_FACTORY(InvalidCryptoException);
SAML_EXCEPTION_FACTORY(TrustException);
SAML_EXCEPTION_FACTORY(BindingException);
SAML_EXCEPTION_FACTORY(SOAPException);
SAML_EXCEPTION_FACTORY(HTTPException);
SAML_EXCEPTION_FACTORY(ContentTypeException);
SAML_EXCEPTION_FACTORY(UnknownAssertionException);
SAML_EXCEPTION_FACTORY(ProfileException);
SAML_EXCEPTION_FACTORY(FatalProfileException);
SAML_EXCEPTION_FACTORY(RetryableProfileException);
SAML_EXCEPTION_FACTORY(UnsupportedProfileException);
SAML_EXCEPTION_FACTORY(ExpiredAssertionException);
SAML_EXCEPTION_FACTORY(InvalidAssertionException);
SAML_EXCEPTION_FACTORY(ReplayedAssertionException);
extern "C" SAMLQuery* SAMLAttributeQueryFactory(DOMElement* e)
{
return new SAMLAttributeQuery(e);
}
extern "C" SAMLStatement* SAMLAttributeStatementFactory(DOMElement* e)
{
return new SAMLAttributeStatement(e);
}
extern "C" SAMLQuery* SAMLAuthenticationQueryFactory(DOMElement* e)
{
return new SAMLAuthenticationQuery(e);
}
extern "C" SAMLStatement* SAMLAuthenticationStatementFactory(DOMElement* e)
{
return new SAMLAuthenticationStatement(e);
}
extern "C" SAMLQuery* SAMLAuthorizationDecisionQueryFactory(DOMElement *e)
{
return new SAMLAuthorizationDecisionQuery(e);
}
extern "C" SAMLStatement* SAMLAuthorizationDecisionStatementFactory(DOMElement *e)
{
return new SAMLAuthorizationDecisionStatement(e);
}
extern "C" SAMLCondition* SAMLAudienceConditionFactory(DOMElement* e)
{
return new SAMLAudienceRestrictionCondition(e);
}
void PlugManager::regFactory(const char* type, Factory* factory)
{
if (type && factory)
m_map[type]=factory;
}
void PlugManager::regFactory(const char* type, XMLChFactory* factory)
{
if (type && factory)
m_XMLCh_map[type]=factory;
}
IPlugIn* PlugManager::newPlugin(const char* type, const DOMElement* e)
{
FactoryMap::const_iterator i=m_map.find(type);
if (i==m_map.end())
throw UnsupportedExtensionException(string("unable to build plugin of type '") + type + "'");
return i->second(e);
}
IPlugIn* PlugManager::newPlugin(const char* type, const XMLCh* qualifier, const DOMElement* e)
{
XMLChFactoryMap::const_iterator i=m_XMLCh_map.find(type);
if (i==m_XMLCh_map.end())
throw UnsupportedExtensionException(string("unable to build plugin of type '") + type + "'");
return i->second(qualifier, e);
}
void PlugManager::unregFactory(const char* type)
{
if (type) {
m_map.erase(type);
m_XMLCh_map.erase(type);
}
}
PlugManager::Factory MemoryReplayCacheFactory;
PlugManager::Factory BrowserProfileFactory;
PlugManager::XMLChFactory SOAPBindingFactory;
extern "C" {
SAMLArtifactFactory SAMLArtifactType0001Factory;
SAMLArtifactFactory SAMLArtifactType0002Factory;
}
namespace {
SAMLInternalConfig g_config;
}
SAMLConfig& SAMLConfig::getConfig()
{
return g_config;
}
static const char defaultAlgConfig[] =
"<AlgorithmConfig xmlns=\"http://www.opensaml.org\">"
"<Signature uri=\"http://www.w3.org/2000/09/xmldsig#dsa-sha1\" num=\"1\"/>"
"<Signature uri=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\" num=\"2\"/>"
"<Signature uri=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" num=\"3\"/>"
"<Digest uri=\"http://www.w3.org/2000/09/xmldsig#sha1\" num=\"1\"/>"
"<Digest uri=\"http://www.w3.org/2001/04/xmldsig-more#md5\" num=\"2\"/>"
#if (XSEC_VERSION_MAJOR > 1) || (((XSEC_VERSION_MAJOR == 1)) && (XSEC_VERSION_MEDIUM >= 2))
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#hmac-sha224\" num=\"2\"/>"
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256\" num=\"2\"/>"
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#hmac-sha384\" num=\"2\"/>"
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#hmac-sha512\" num=\"2\"/>"
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha224\" num=\"3\"/>"
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256\" num=\"3\"/>"
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha384\" num=\"3\"/>"
"<Signature uri=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512\" num=\"3\"/>"
"<Signature uri=\"http://www.w3.org/2000/09/xmldsig#rsa-md5\" num=\"3\"/>"
"<Digest uri=\"http://www.w3.org/2000/09/xmldsig-more#sha224\" num=\"3\"/>"
"<Digest uri=\"http://www.w3.org/2001/04/xmlenc#sha256\" num=\"4\"/>"
"<Digest uri=\"http://www.w3.org/2000/09/xmldsig-more#sha384\" num=\"5\"/>"
"<Digest uri=\"http://www.w3.org/2001/04/xmlenc#sha512\" num=\"6\"/>"
#endif
"</AlgorithmConfig>";
bool SAMLInternalConfig::init()
{
try {
if (!log_config.empty())
PropertyConfigurator::configure(log_config);
m_log=&(Category::getInstance(SAML_LOGCAT".SAMLInternalConfig"));
m_log->debug("library initialization started");
if (curl_global_init(CURL_GLOBAL_ALL))
{
m_log->fatal("init: failed to initialize libcurl, SSL, or Winsock");
return false;
}
m_log->debug("libcurl %s initialization complete",LIBCURL_VERSION);
XMLPlatformUtils::Initialize();
m_log->debug("Xerces initialization complete");
soap_pool_init();
XSECPlatformUtils::Initialise();
m_xsec=new XSECProvider();
m_log->debug("XSEC initialization complete");
if (schema_dir[schema_dir.length()-1]!='/')
schema_dir+='/';
wide_schema_dir=XMLString::transcode(schema_dir.c_str());
if (inclusive_namespace_prefixes.empty())
inclusive_namespace_prefixes="#default saml samlp ds xsd xsi code kind rw typens";
wide_inclusive_namespace_prefixes=XMLString::transcode(inclusive_namespace_prefixes.c_str());
m_pool=new XML::ParserPool();
m_pool->registerSchema(XML::SAML_NS,XML::SAML11_SCHEMA_ID);
m_pool->registerSchema(XML::SAMLP_NS,XML::SAMLP11_SCHEMA_ID);
m_pool->registerSchema(XML::SOAP11ENV_NS,XML::SOAP11ENV_SCHEMA_ID);
m_pool->registerSchema(XML::XMLSIG_NS,XML::XMLSIG_SCHEMA_ID);
m_pool->registerSchema(XML::XML_NS,XML::XML_SCHEMA_ID);
m_compat_pool=new XML::ParserPool();
m_compat_pool->registerSchema(XML::SAML_NS,XML::SAML_SCHEMA_ID);
m_compat_pool->registerSchema(XML::SAMLP_NS,XML::SAMLP_SCHEMA_ID);
m_compat_pool->registerSchema(XML::SOAP11ENV_NS,XML::SOAP11ENV_SCHEMA_ID);
m_compat_pool->registerSchema(XML::XMLSIG_NS,XML::XMLSIG_SCHEMA_ID);
m_compat_pool->registerSchema(XML::XML_NS,XML::XML_SCHEMA_ID);
m_log->debug("SAML schema registration complete");
m_lock=XMLPlatformUtils::makeMutex();
// Register built-in SAML type factories.
saml::QName q1(XML::SAMLP_NS,L(AttributeQueryType));
saml::QName q2(XML::SAMLP_NS,L(AttributeQuery));
SAMLQuery::regFactory(q1,&SAMLAttributeQueryFactory);
SAMLQuery::regFactory(q2,&SAMLAttributeQueryFactory);
saml::QName s1(XML::SAML_NS,L(AttributeStatementType));
saml::QName s2(XML::SAML_NS,L(AttributeStatement));
SAMLStatement::regFactory(s1,&SAMLAttributeStatementFactory);
SAMLStatement::regFactory(s2,&SAMLAttributeStatementFactory);
saml::QName q3(XML::SAMLP_NS,L(AuthenticationQueryType));
saml::QName q4(XML::SAMLP_NS,L(AuthenticationQuery));
SAMLQuery::regFactory(q3,&SAMLAuthenticationQueryFactory);
SAMLQuery::regFactory(q4,&SAMLAuthenticationQueryFactory);
saml::QName s3(XML::SAML_NS,L(AuthenticationStatementType));
saml::QName s4(XML::SAML_NS,L(AuthenticationStatement));
SAMLStatement::regFactory(s3,&SAMLAuthenticationStatementFactory);
SAMLStatement::regFactory(s4,&SAMLAuthenticationStatementFactory);
QName q5(XML::SAMLP_NS,L(AuthorizationDecisionQueryType));
QName q6(XML::SAMLP_NS,L(AuthorizationDecisionQuery));
SAMLQuery::regFactory(q5,&SAMLAuthorizationDecisionQueryFactory);
SAMLQuery::regFactory(q6,&SAMLAuthorizationDecisionQueryFactory);
QName s5(XML::SAML_NS,L(AuthorizationDecisionStatementType));
QName s6(XML::SAML_NS,L(AuthorizationDecisionStatement));
SAMLStatement::regFactory(s5,&SAMLAuthorizationDecisionStatementFactory);
SAMLStatement::regFactory(s6,&SAMLAuthorizationDecisionStatementFactory);
saml::QName c1(XML::SAML_NS,L(AudienceRestrictionConditionType));
saml::QName c2(XML::SAML_NS,L(AudienceRestrictionCondition));
SAMLCondition::regFactory(c1,&SAMLAudienceConditionFactory);
SAMLCondition::regFactory(c2,&SAMLAudienceConditionFactory);
REGISTER_EXCEPTION_FACTORY(MalformedException);
REGISTER_EXCEPTION_FACTORY(UnsupportedExtensionException);
REGISTER_EXCEPTION_FACTORY(InvalidCryptoException);
REGISTER_EXCEPTION_FACTORY(TrustException);
REGISTER_EXCEPTION_FACTORY(BindingException);
REGISTER_EXCEPTION_FACTORY(SOAPException);
REGISTER_EXCEPTION_FACTORY(HTTPException);
REGISTER_EXCEPTION_FACTORY(ContentTypeException);
REGISTER_EXCEPTION_FACTORY(UnknownAssertionException);
REGISTER_EXCEPTION_FACTORY(ProfileException);
REGISTER_EXCEPTION_FACTORY(FatalProfileException);
REGISTER_EXCEPTION_FACTORY(RetryableProfileException);
REGISTER_EXCEPTION_FACTORY(UnsupportedProfileException);
REGISTER_EXCEPTION_FACTORY(ExpiredAssertionException);
REGISTER_EXCEPTION_FACTORY(InvalidAssertionException);
REGISTER_EXCEPTION_FACTORY(ReplayedAssertionException);
m_plugMgr.regFactory(DEFAULT_REPLAYCACHE_PROVIDER,&MemoryReplayCacheFactory);
m_plugMgr.regFactory(DEFAULT_BROWSERPROFILE_PROVIDER,&BrowserProfileFactory);
setDefaultBindingProvider(SAMLBinding::SOAP,DEFAULT_SOAPBINDING_PROVIDER);
m_plugMgr.regFactory(DEFAULT_SOAPBINDING_PROVIDER,&SOAPBindingFactory);
string typecode;
typecode+=(char)0x0;
typecode+=(char)0x1;
SAMLArtifact::regFactory(typecode,&SAMLArtifactType0001Factory);
typecode[1]=(char)0x2;
SAMLArtifact::regFactory(typecode,&SAMLArtifactType0002Factory);
m_log->debug("SAML type factory registration complete");
// Load XMLSig algorithm mappings.
DOMDocument* algConfigDoc=NULL;
DOMBuilder* parser=m_pool->get(false);
if (alg_config.empty()) {
m_log->debug("loading default XMLSig algorithm configuration");
istringstream in(defaultAlgConfig);
XML::StreamInputSource src(in);
Wrapper4InputSource dsrc(&src,false);
try {
algConfigDoc=parser->parse(dsrc);
}
catch (...) {
m_log->error("caught exception while parsing built-in XMLSig algorithm configuration");
parser->release();
}
}
else {
m_log->debug("loading external XMLSig algorithm configuration from file (%s)", alg_config.c_str());
ifstream in(alg_config.c_str());
XML::StreamInputSource src(in);
Wrapper4InputSource dsrc(&src,false);
try {
algConfigDoc=parser->parse(dsrc);
}
catch (...) {
m_log->error("caught exception while parsing XMLSig algorithm configuration in file (%s)", alg_config.c_str());
parser->release();
}
}
if (algConfigDoc) {
static const XMLCh uri[]={chLatin_u, chLatin_r, chLatin_i, chNull};
static const XMLCh num[]={chLatin_n, chLatin_u, chLatin_m, chNull};
DOMNodeList* nlist=algConfigDoc->getDocumentElement()->getElementsByTagNameNS(XML::OPENSAML_NS,L(Signature));
unsigned int algs;
for (algs=0; nlist && algs < nlist->getLength(); algs++) {
auto_ptr_char u(static_cast<DOMElement*>(nlist->item(algs))->getAttributeNS(NULL,uri));
auto_ptr_char n(static_cast<DOMElement*>(nlist->item(algs))->getAttributeNS(NULL,num));
m_log->debug("loaded Signature mapping (%s) -> (%s)", u.get(), n.get());
m_sigAlgFromURI.insert(pair<string,signatureMethod>(u.get(),static_cast<signatureMethod>(atoi(n.get()))));
}
nlist=algConfigDoc->getDocumentElement()->getElementsByTagNameNS(XML::OPENSAML_NS,L(Digest));
for (algs=0; nlist && algs < nlist->getLength(); algs++) {
auto_ptr_char u(static_cast<DOMElement*>(nlist->item(algs))->getAttributeNS(NULL,uri));
auto_ptr_char n(static_cast<DOMElement*>(nlist->item(algs))->getAttributeNS(NULL,num));
m_log->debug("loaded Digest mapping (%s) -> (%s)", u.get(), n.get());
m_digestAlgFromURI.insert(pair<string,hashMethod>(u.get(),static_cast<hashMethod>(atoi(n.get()))));
}
algConfigDoc->release();
}
}
catch(const ConfigureFailure& e) {
cerr << "SAMLConfig::init() caught exception while initializing log4cpp: " << e.what() << endl;
return false;
}
catch (const XMLException&) {
m_log->fatal("caught exception while initializing Xerces");
curl_global_cleanup();
return false;
}
m_log->info("library initialization complete");
return true;
}
void SAMLInternalConfig::term()
{
m_plugMgr.unregFactory("org.opensaml.provider.MemoryReplayCache");
for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++)
{
#if defined(WIN32)
FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"saml_extension_term");
if (fn)
fn();
FreeLibrary(static_cast<HMODULE>(*i));
#elif defined(HAVE_DLFCN_H)
void (*fn)()=(void (*)())dlsym(*i,"saml_extension_term");
if (fn)
fn();
dlclose(*i);
#else
# error "Don't know about dynamic loading on this platform!"
#endif
}
m_libhandles.clear();
delete m_xsec; m_xsec=NULL;
XSECPlatformUtils::Terminate();
XMLPlatformUtils::closeMutex(m_lock);
delete m_pool; m_pool=NULL;
delete m_compat_pool; m_compat_pool=NULL;
if (wide_schema_dir) {
XMLString::release(&wide_schema_dir);
wide_schema_dir=NULL;
}
if (wide_inclusive_namespace_prefixes) {
XMLString::release(&wide_inclusive_namespace_prefixes);
wide_inclusive_namespace_prefixes=NULL;
}
soap_pool_term();
XMLPlatformUtils::Terminate();
curl_global_cleanup();
m_log->info("library shutdown complete");
m_log=NULL;
}
void SAMLInternalConfig::saml_lock() const
{
XMLPlatformUtils::lockMutex(m_lock);
}
void SAMLInternalConfig::saml_unlock() const
{
XMLPlatformUtils::unlockMutex(m_lock);
}
void SAMLInternalConfig::saml_register_extension(const char* path, void* context) const
{
#ifdef _DEBUG
saml::NDC ndc("saml_register_extension");
#endif
m_log->info("loading extension: %s",path);
#if defined(WIN32)
HMODULE handle=NULL;
char* fixed=const_cast<char*>(path);
if (strchr(fixed,'/'))
{
fixed=strdup(path);
char* p=fixed;
while (p=strchr(p,'/'))
*p='\\';
}
UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
try
{
handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
if (!handle)
handle=LoadLibraryEx(fixed,NULL,0);
if (!handle)
throw SAMLException(string("SAMLConfig::saml_register_extension() unable to load extension library: ") + fixed);
FARPROC fn=GetProcAddress(handle,"saml_extension_init");
if (!fn)
throw SAMLException(string("SAMLConfig::saml_register_extension() unable to locate saml_extension_init entry point: ") + fixed);
if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
throw SAMLException(string("SAMLConfig::saml_register_extension() detected error in saml_extension_init: ") + fixed);
if (fixed!=path)
free(fixed);
SetErrorMode(em);
}
catch(...)
{
if (handle)
FreeLibrary(handle);
SetErrorMode(em);
if (fixed!=path)
free(fixed);
throw;
}
#elif defined(HAVE_DLFCN_H)
void* handle=dlopen(path,RTLD_LAZY);
if (!handle)
throw SAMLException(string("SAMLConfig::saml_register_extension unable to load extension library '") + path + "': " + dlerror());
int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"saml_extension_init"));
if (!fn)
{
dlclose(handle);
throw SAMLException(string("SAMLConfig::saml_register_extension unable to locate saml_extension_init entry point in '") + path + "': " + (dlerror() ? dlerror() : "unknown error"));
}
try
{
if (fn(context)!=0)
throw SAMLException(string("SAMLConfig::saml_register_extension() detected error in saml_extension_init in ") + path);
}
catch(...)
{
if (handle)
dlclose(handle);
throw;
}
#else
# error "Don't know about dynamic loading on this platform!"
#endif
m_libhandles.push_back(handle);
m_log->info("loaded extension: %s",path);
}
const char* SAMLInternalConfig::getDefaultBindingProvider(const XMLCh* binding) const
{
#ifdef HAVE_GOOD_STL
map<xstring,string>::const_iterator i=m_bindingMap.find(binding);
#else
auto_ptr_char temp(binding);
map<string,string>::const_iterator i=m_bindingMap.find(temp.get());
#endif
return (i==m_bindingMap.end()) ? NULL : i->second.c_str();
}
void SAMLInternalConfig::setDefaultBindingProvider(const XMLCh* binding, const char* type)
{
#ifdef HAVE_GOOD_STL
m_bindingMap[binding]=type;
#else
auto_ptr_char temp(binding);
m_bindingMap[temp.get()]=type;
#endif
}
|