3 * Copyright 2001-2006 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
25 #include "exceptions.h"
26 #include "SAMLConfig.h"
27 #include "binding/ArtifactMap.h"
28 #include "binding/MessageDecoder.h"
29 #include "binding/MessageEncoder.h"
30 #include "binding/SAMLArtifact.h"
31 #include "binding/URLEncoder.h"
32 #include "saml1/core/Assertions.h"
33 #include "saml1/core/Protocols.h"
34 #include "saml2/core/Protocols.h"
35 #include "saml2/metadata/Metadata.h"
36 #include "saml2/metadata/MetadataProvider.h"
37 #include "security/TrustEngine.h"
38 #include "util/SAMLConstants.h"
40 #include <xmltooling/XMLToolingConfig.h>
41 #include <xmltooling/signature/Signature.h>
42 #include <xmltooling/util/NDC.h>
44 #include <log4cpp/Category.hh>
45 #include <xsec/enc/XSECCryptoException.hpp>
46 #include <xsec/enc/XSECCryptoProvider.hpp>
47 #include <xsec/utils/XSECPlatformUtils.hpp>
48 #include <openssl/err.h>
50 using namespace opensaml;
51 using namespace xmlsignature;
52 using namespace xmltooling;
53 using namespace log4cpp;
56 // Expose entry points when used as an extension library
58 extern "C" int SAML_API xmltooling_extension_init(void*)
60 if (SAMLConfig::getConfig().init(false))
65 extern "C" void SAML_API xmltooling_extension_term()
67 SAMLConfig::getConfig().term(false);
70 DECL_EXCEPTION_FACTORY(ArtifactException,opensaml);
71 DECL_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
72 DECL_EXCEPTION_FACTORY(BindingException,opensaml);
75 SAMLInternalConfig g_config;
78 SAMLConfig& SAMLConfig::getConfig()
83 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
88 void SAMLConfig::setArtifactMap(ArtifactMap* artifactMap)
91 m_artifactMap = artifactMap;
94 void SAMLConfig::setURLEncoder(URLEncoder* urlEncoder)
97 m_urlEncoder = urlEncoder;
100 bool SAMLInternalConfig::init(bool initXMLTooling)
103 xmltooling::NDC ndc("init");
105 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
106 log.debug("library initialization started");
108 if (initXMLTooling) {
109 XMLToolingConfig::getConfig().init();
110 log.debug("XMLTooling library initialized");
113 REGISTER_EXCEPTION_FACTORY(ArtifactException,opensaml);
114 REGISTER_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
115 REGISTER_EXCEPTION_FACTORY(BindingException,opensaml);
117 saml1::registerAssertionClasses();
118 saml1p::registerProtocolClasses();
119 saml2::registerAssertionClasses();
120 saml2p::registerProtocolClasses();
121 saml2md::registerMetadataClasses();
122 saml2md::registerMetadataProviders();
123 saml2md::registerMetadataFilters();
124 registerSAMLArtifacts();
125 registerTrustEngines();
126 registerMessageEncoders();
127 registerMessageDecoders();
128 registerSecurityPolicyRules();
130 m_urlEncoder = new URLEncoder();
132 log.info("library initialization complete");
136 void SAMLInternalConfig::term(bool termXMLTooling)
139 xmltooling::NDC ndc("term");
141 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
143 MessageDecoderManager.deregisterFactories();
144 MessageEncoderManager.deregisterFactories();
145 TrustEngineManager.deregisterFactories();
146 SecurityPolicyRuleManager.deregisterFactories();
147 SAMLArtifactManager.deregisterFactories();
148 MetadataFilterManager.deregisterFactories();
149 MetadataProviderManager.deregisterFactories();
151 delete m_artifactMap;
152 m_artifactMap = NULL;
156 if (termXMLTooling) {
157 XMLToolingConfig::getConfig().term();
158 log.debug("XMLTooling library shut down");
160 log.info("library shutdown complete");
163 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
166 if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
167 throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
169 catch (XSECCryptoException& e) {
170 throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
174 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
177 auto_ptr<unsigned char> hold(new unsigned char[len]);
178 generateRandomBytes(hold.get(),len);
179 for (unsigned int i=0; i<len; i++)
180 buf+=(hold.get())[i];
183 XMLCh* SAMLInternalConfig::generateIdentifier()
185 unsigned char key[17];
186 generateRandomBytes(key,16);
189 sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
190 key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
191 key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
193 return XMLString::transcode(hexform);
196 string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
198 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
200 auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
202 auto_ptr<char> dup(strdup(s));
203 unsigned char buf[21];
204 hasher->hash(reinterpret_cast<unsigned char*>(dup.get()),strlen(dup.get()));
205 if (hasher->finish(buf,20)==20) {
208 for (unsigned int i=0; i<20; i++) {
209 ret+=(DIGITS[((unsigned char)(0xF0 & buf[i])) >> 4 ]);
210 ret+=(DIGITS[0x0F & buf[i]]);
214 for (unsigned int i=0; i<20; i++) {
221 throw XMLSecurityException("Unable to generate SHA-1 hash.");
224 void opensaml::log_openssl()
230 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
232 Category& log=Category::getInstance("OpenSSL");
233 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
234 if (data && (flags & ERR_TXT_STRING))
235 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
236 code=ERR_get_error_line_data(&file,&line,&data,&flags);
240 using namespace saml2md;
242 void opensaml::annotateException(XMLToolingException* e, const EntityDescriptor* entity, bool rethrow)
245 auto_ptr_char id(entity->getEntityID());
246 e->addProperty("entityID",id.get());
247 const list<XMLObject*>& roles=entity->getOrderedChildren();
248 for (list<XMLObject*>::const_iterator child=roles.begin(); child!=roles.end(); ++child) {
249 const RoleDescriptor* role=dynamic_cast<RoleDescriptor*>(*child);
250 if (role && role->isValid()) {
251 const vector<ContactPerson*>& contacts=role->getContactPersons();
252 for (vector<ContactPerson*>::const_iterator c=contacts.begin(); c!=contacts.end(); ++c) {
253 const XMLCh* ctype=(*c)->getContactType();
254 if (ctype && (XMLString::equals(ctype,ContactPerson::CONTACT_SUPPORT)
255 || XMLString::equals(ctype,ContactPerson::CONTACT_TECHNICAL))) {
256 GivenName* fname=(*c)->getGivenName();
257 SurName* lname=(*c)->getSurName();
258 auto_ptr_char first(fname ? fname->getName() : NULL);
259 auto_ptr_char last(lname ? lname->getName() : NULL);
260 if (first.get() && last.get()) {
261 string contact=string(first.get()) + ' ' + last.get();
262 e->addProperty("contactName",contact.c_str());
264 else if (first.get())
265 e->addProperty("contactName",first.get());
267 e->addProperty("contactName",last.get());
268 const vector<EmailAddress*>& emails=const_cast<const ContactPerson*>(*c)->getEmailAddresss();
269 if (!emails.empty()) {
270 auto_ptr_char email(emails.front()->getAddress());
272 e->addProperty("contactEmail",email.get());
277 if (e->getProperty("contactName") || e->getProperty("contactEmail")) {
278 auto_ptr_char eurl(role->getErrorURL());
280 e->addProperty("errorURL",eurl.get());
292 void opensaml::annotateException(XMLToolingException* e, const RoleDescriptor* role, bool rethrow)
295 auto_ptr_char id(dynamic_cast<EntityDescriptor*>(role->getParent())->getEntityID());
296 e->addProperty("entityID",id.get());
298 const vector<ContactPerson*>& contacts=role->getContactPersons();
299 for (vector<ContactPerson*>::const_iterator c=contacts.begin(); c!=contacts.end(); ++c) {
300 const XMLCh* ctype=(*c)->getContactType();
301 if (ctype && (XMLString::equals(ctype,ContactPerson::CONTACT_SUPPORT)
302 || XMLString::equals(ctype,ContactPerson::CONTACT_TECHNICAL))) {
303 GivenName* fname=(*c)->getGivenName();
304 SurName* lname=(*c)->getSurName();
305 auto_ptr_char first(fname ? fname->getName() : NULL);
306 auto_ptr_char last(lname ? lname->getName() : NULL);
307 if (first.get() && last.get()) {
308 string contact=string(first.get()) + ' ' + last.get();
309 e->addProperty("contactName",contact.c_str());
311 else if (first.get())
312 e->addProperty("contactName",first.get());
314 e->addProperty("contactName",last.get());
315 const vector<EmailAddress*>& emails=const_cast<const ContactPerson*>(*c)->getEmailAddresss();
316 if (!emails.empty()) {
317 auto_ptr_char email(emails.front()->getAddress());
319 e->addProperty("contactEmail",email.get());
325 auto_ptr_char eurl(role->getErrorURL());
327 e->addProperty("errorURL",eurl.get());