3 * Copyright 2001-2010 Internet2
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * Library configuration.
26 #if defined(XMLTOOLING_LOG4SHIB)
27 # ifndef OPENSAML_LOG4SHIB
28 # error "Logging library mismatch (XMLTooling is using log4shib)."
30 #elif defined(XMLTOOLING_LOG4CPP)
31 # ifndef OPENSAML_LOG4CPP
32 # error "Logging library mismatch (XMLTooling is using log4cpp)."
35 # error "No supported logging library."
38 #include "exceptions.h"
39 #include "SAMLConfig.h"
40 #include "binding/ArtifactMap.h"
41 #include "binding/MessageDecoder.h"
42 #include "binding/MessageEncoder.h"
43 #include "binding/SAMLArtifact.h"
44 #include "binding/SecurityPolicyRule.h"
45 #include "saml1/core/Assertions.h"
46 #include "saml1/core/Protocols.h"
47 #include "saml2/core/Protocols.h"
48 #include "saml2/metadata/Metadata.h"
49 #include "saml2/metadata/MetadataFilter.h"
50 #include "saml2/metadata/MetadataProvider.h"
51 #include "util/SAMLConstants.h"
53 #include <xmltooling/logging.h>
54 #include <xmltooling/XMLToolingConfig.h>
55 #include <xmltooling/security/SecurityHelper.h>
56 #include <xmltooling/signature/Signature.h>
57 #include <xmltooling/util/NDC.h>
58 #include <xmltooling/util/PathResolver.h>
60 #include <xsec/enc/XSECCryptoException.hpp>
61 #include <xsec/enc/XSECCryptoProvider.hpp>
62 #include <xsec/utils/XSECPlatformUtils.hpp>
64 using namespace opensaml;
65 using namespace xmlsignature;
66 using namespace xmltooling::logging;
67 using namespace xmltooling;
70 // Expose entry points when used as an extension library
72 extern "C" int SAML_API xmltooling_extension_init(void*)
74 if (SAMLConfig::getConfig().init(false))
79 extern "C" void SAML_API xmltooling_extension_term()
81 SAMLConfig::getConfig().term(false);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(ArtifactException,opensaml);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
89 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
90 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
91 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
94 SAMLInternalConfig g_config;
97 SAMLConfig& SAMLConfig::getConfig()
102 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
107 SAMLConfig::SAMLConfig() : m_artifactMap(nullptr)
111 SAMLConfig::~SAMLConfig()
115 ArtifactMap* SAMLConfig::getArtifactMap() const
117 return m_artifactMap;
120 void SAMLConfig::setArtifactMap(ArtifactMap* artifactMap)
122 delete m_artifactMap;
123 m_artifactMap = artifactMap;
126 bool SAMLInternalConfig::init(bool initXMLTooling)
129 xmltooling::NDC ndc("init");
131 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
132 log.debug("library initialization started");
135 XMLToolingConfig::getConfig().init();
136 XMLToolingConfig::getConfig().getPathResolver()->setDefaultPackageName("opensaml");
138 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ArtifactException,opensaml);
139 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
140 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
141 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
142 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
143 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
144 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
145 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
147 saml1::registerAssertionClasses();
148 saml1p::registerProtocolClasses();
149 saml2::registerAssertionClasses();
150 saml2p::registerProtocolClasses();
151 saml2md::registerMetadataClasses();
152 saml2md::registerMetadataProviders();
153 saml2md::registerMetadataFilters();
154 registerSAMLArtifacts();
155 registerMessageEncoders();
156 registerMessageDecoders();
157 registerSecurityPolicyRules();
159 log.info("%s library initialization complete", PACKAGE_STRING);
163 void SAMLInternalConfig::term(bool termXMLTooling)
166 xmltooling::NDC ndc("term");
168 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
170 MessageDecoderManager.deregisterFactories();
171 MessageEncoderManager.deregisterFactories();
172 SecurityPolicyRuleManager.deregisterFactories();
173 SAMLArtifactManager.deregisterFactories();
174 MetadataFilterManager.deregisterFactories();
175 MetadataProviderManager.deregisterFactories();
177 delete m_artifactMap;
178 m_artifactMap = nullptr;
181 XMLToolingConfig::getConfig().term();
183 log.info("%s library shutdown complete", PACKAGE_STRING);
186 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
189 if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
190 throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
192 catch (XSECCryptoException& e) {
193 throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
197 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
200 auto_ptr<unsigned char> hold(new unsigned char[len]);
201 generateRandomBytes(hold.get(),len);
202 for (unsigned int i=0; i<len; i++)
203 buf+=(hold.get())[i];
206 XMLCh* SAMLInternalConfig::generateIdentifier()
208 unsigned char key[17];
209 generateRandomBytes(key,16);
212 sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
213 key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
214 key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
216 return XMLString::transcode(hexform);
219 string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
221 return SecurityHelper::doHash("SHA1", s, strlen(s), toHex);
224 SignableObject::SignableObject()
228 SignableObject::~SignableObject()
232 RootObject::RootObject()
236 RootObject::~RootObject()
240 Assertion::Assertion()
244 Assertion::~Assertion()
248 using namespace saml2p;
249 using namespace saml2md;
251 void opensaml::annotateException(XMLToolingException* e, const EntityDescriptor* entity, const Status* status, bool rethrow)
253 const RoleDescriptor* role = nullptr;
255 const list<XMLObject*>& roles=entity->getOrderedChildren();
256 for (list<XMLObject*>::const_iterator child=roles.begin(); !role && child!=roles.end(); ++child) {
257 role=dynamic_cast<RoleDescriptor*>(*child);
258 if (role && !role->isValid())
262 annotateException(e, role, status, rethrow);
265 void opensaml::annotateException(XMLToolingException* e, const RoleDescriptor* role, const Status* status, bool rethrow)
268 auto_ptr_char id(dynamic_cast<EntityDescriptor*>(role->getParent())->getEntityID());
269 e->addProperty("entityID",id.get());
271 const vector<ContactPerson*>& contacts=role->getContactPersons();
272 for (vector<ContactPerson*>::const_iterator c=contacts.begin(); c!=contacts.end(); ++c) {
273 const XMLCh* ctype=(*c)->getContactType();
274 if (ctype && (XMLString::equals(ctype,ContactPerson::CONTACT_SUPPORT)
275 || XMLString::equals(ctype,ContactPerson::CONTACT_TECHNICAL))) {
276 GivenName* fname=(*c)->getGivenName();
277 SurName* lname=(*c)->getSurName();
278 auto_ptr_char first(fname ? fname->getName() : nullptr);
279 auto_ptr_char last(lname ? lname->getName() : nullptr);
280 if (first.get() && last.get()) {
281 string contact=string(first.get()) + ' ' + last.get();
282 e->addProperty("contactName",contact.c_str());
284 else if (first.get())
285 e->addProperty("contactName",first.get());
287 e->addProperty("contactName",last.get());
288 const vector<EmailAddress*>& emails=const_cast<const ContactPerson*>(*c)->getEmailAddresss();
289 if (!emails.empty()) {
290 auto_ptr_char email(emails.front()->getAddress());
292 e->addProperty("contactEmail",email.get());
298 auto_ptr_char eurl(role->getErrorURL());
300 e->addProperty("errorURL",eurl.get());
305 auto_ptr_char sc(status->getStatusCode() ? status->getStatusCode()->getValue() : nullptr);
306 if (sc.get() && *sc.get())
307 e->addProperty("statusCode", sc.get());
308 if (status->getStatusCode()->getStatusCode()) {
309 auto_ptr_char sc2(status->getStatusCode()->getStatusCode()->getValue());
310 if (sc2.get() && *sc.get())
311 e->addProperty("statusCode2", sc2.get());
313 if (status->getStatusMessage()) {
314 auto_ptr_char msg(status->getStatusMessage()->getMessage());
315 if (msg.get() && *msg.get())
316 e->addProperty("statusMessage", msg.get());