3 * Copyright 2001-2007 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/SecurityPolicyRule.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/MetadataFilter.h"
37 #include "saml2/metadata/MetadataProvider.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>
49 using namespace opensaml;
50 using namespace xmlsignature;
51 using namespace xmltooling;
52 using namespace log4cpp;
55 // Expose entry points when used as an extension library
57 extern "C" int SAML_API xmltooling_extension_init(void*)
59 if (SAMLConfig::getConfig().init(false))
64 extern "C" void SAML_API xmltooling_extension_term()
66 SAMLConfig::getConfig().term(false);
69 DECL_XMLTOOLING_EXCEPTION_FACTORY(ArtifactException,opensaml);
70 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
71 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
72 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
73 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
79 SAMLInternalConfig g_config;
82 SAMLConfig& SAMLConfig::getConfig()
87 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
92 void SAMLConfig::setArtifactMap(ArtifactMap* artifactMap)
95 m_artifactMap = artifactMap;
98 bool SAMLInternalConfig::init(bool initXMLTooling)
101 xmltooling::NDC ndc("init");
103 Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
104 log.debug("library initialization started");
106 if (initXMLTooling) {
107 XMLToolingConfig::getConfig().init();
108 log.debug("XMLTooling library initialized");
111 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ArtifactException,opensaml);
112 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
113 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
114 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
115 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
116 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
117 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
118 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
120 saml1::registerAssertionClasses();
121 saml1p::registerProtocolClasses();
122 saml2::registerAssertionClasses();
123 saml2p::registerProtocolClasses();
124 saml2md::registerMetadataClasses();
125 saml2md::registerMetadataProviders();
126 saml2md::registerMetadataFilters();
127 registerSAMLArtifacts();
128 registerMessageEncoders();
129 registerMessageDecoders();
130 registerSecurityPolicyRules();
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 SecurityPolicyRuleManager.deregisterFactories();
146 SAMLArtifactManager.deregisterFactories();
147 MetadataFilterManager.deregisterFactories();
148 MetadataProviderManager.deregisterFactories();
150 delete m_artifactMap;
151 m_artifactMap = NULL;
153 if (termXMLTooling) {
154 XMLToolingConfig::getConfig().term();
155 log.debug("XMLTooling library shut down");
157 log.info("library shutdown complete");
160 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
163 if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
164 throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
166 catch (XSECCryptoException& e) {
167 throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
171 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
174 auto_ptr<unsigned char> hold(new unsigned char[len]);
175 generateRandomBytes(hold.get(),len);
176 for (unsigned int i=0; i<len; i++)
177 buf+=(hold.get())[i];
180 XMLCh* SAMLInternalConfig::generateIdentifier()
182 unsigned char key[17];
183 generateRandomBytes(key,16);
186 sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
187 key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
188 key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
190 return XMLString::transcode(hexform);
193 string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
195 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
197 auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
199 unsigned char buf[21];
200 hasher->hash(reinterpret_cast<unsigned char*>(const_cast<char*>(s)),strlen(s));
201 if (hasher->finish(buf,20)==20) {
204 for (unsigned int i=0; i<20; i++) {
205 ret+=(DIGITS[((unsigned char)(0xF0 & buf[i])) >> 4 ]);
206 ret+=(DIGITS[0x0F & buf[i]]);
210 for (unsigned int i=0; i<20; i++) {
217 throw XMLSecurityException("Unable to generate SHA-1 hash.");
220 using namespace saml2p;
221 using namespace saml2md;
223 void opensaml::annotateException(XMLToolingException* e, const EntityDescriptor* entity, const Status* status, bool rethrow)
225 const RoleDescriptor* role = NULL;
227 const list<XMLObject*>& roles=entity->getOrderedChildren();
228 for (list<XMLObject*>::const_iterator child=roles.begin(); !role && child!=roles.end(); ++child) {
229 role=dynamic_cast<RoleDescriptor*>(*child);
230 if (role && !role->isValid())
234 annotateException(e, role, status, rethrow);
237 void opensaml::annotateException(XMLToolingException* e, const RoleDescriptor* role, const Status* status, bool rethrow)
240 auto_ptr_char id(dynamic_cast<EntityDescriptor*>(role->getParent())->getEntityID());
241 e->addProperty("entityID",id.get());
243 const vector<ContactPerson*>& contacts=role->getContactPersons();
244 for (vector<ContactPerson*>::const_iterator c=contacts.begin(); c!=contacts.end(); ++c) {
245 const XMLCh* ctype=(*c)->getContactType();
246 if (ctype && (XMLString::equals(ctype,ContactPerson::CONTACT_SUPPORT)
247 || XMLString::equals(ctype,ContactPerson::CONTACT_TECHNICAL))) {
248 GivenName* fname=(*c)->getGivenName();
249 SurName* lname=(*c)->getSurName();
250 auto_ptr_char first(fname ? fname->getName() : NULL);
251 auto_ptr_char last(lname ? lname->getName() : NULL);
252 if (first.get() && last.get()) {
253 string contact=string(first.get()) + ' ' + last.get();
254 e->addProperty("contactName",contact.c_str());
256 else if (first.get())
257 e->addProperty("contactName",first.get());
259 e->addProperty("contactName",last.get());
260 const vector<EmailAddress*>& emails=const_cast<const ContactPerson*>(*c)->getEmailAddresss();
261 if (!emails.empty()) {
262 auto_ptr_char email(emails.front()->getAddress());
264 e->addProperty("contactEmail",email.get());
270 auto_ptr_char eurl(role->getErrorURL());
272 e->addProperty("errorURL",eurl.get());
277 auto_ptr_char sc(status->getStatusCode() ? status->getStatusCode()->getValue() : NULL);
278 if (sc.get() && *sc.get())
279 e->addProperty("statusCode", sc.get());
280 if (status->getStatusCode()->getStatusCode()) {
281 auto_ptr_char sc2(status->getStatusCode()->getStatusCode()->getValue());
282 if (sc2.get() && *sc.get())
283 e->addProperty("statusCode2", sc2.get());
285 if (status->getStatusMessage()) {
286 auto_ptr_char msg(status->getStatusMessage()->getMessage());
287 if (msg.get() && *msg.get())
288 e->addProperty("statusMessage", msg.get());