Added plugin unregistration, add plugin aliases.
[shibboleth/cpp-opensaml.git] / saml / SAMLConfig.cpp
1 /*
2  *  Copyright 2001-2006 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  * SAMLConfig.cpp
19  * 
20  * Library configuration 
21  */
22
23 #define SAML_DECLARE_VALIDATORS
24
25 #include "internal.h"
26 #include "exceptions.h"
27 #include "SAMLConfig.h"
28 #include "saml1/core/Assertions.h"
29 #include "saml1/core/Protocols.h"
30 #include "saml2/core/Protocols.h"
31 #include "saml2/metadata/Metadata.h"
32 #include "saml2/metadata/MetadataProvider.h"
33 #include "util/SAMLConstants.h"
34
35 #include <xmltooling/XMLToolingConfig.h>
36 #include <xmltooling/signature/Signature.h>
37 #include <xmltooling/util/NDC.h>
38
39 #include <log4cpp/Category.hh>
40 #include <xsec/enc/XSECCryptoException.hpp>
41 #include <xsec/utils/XSECPlatformUtils.hpp>
42
43 using namespace opensaml;
44 using namespace xmlsignature;
45 using namespace xmltooling;
46 using namespace log4cpp;
47 using namespace std;
48
49 //DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
50
51 namespace opensaml {
52    SAMLInternalConfig g_config;
53 }
54
55 SAMLConfig& SAMLConfig::getConfig()
56 {
57     return g_config;
58 }
59
60 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
61 {
62     return g_config;
63 }
64
65 bool SAMLInternalConfig::init()
66 {
67 #ifdef _DEBUG
68     xmltooling::NDC ndc("init");
69 #endif
70     Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
71     log.debug("library initialization started");
72
73     XMLToolingConfig::getConfig().init();
74     log.debug("XMLTooling library initialized");
75
76     saml1::registerAssertionClasses();
77     saml1p::registerProtocolClasses();
78     saml2::registerAssertionClasses();
79     saml2p::registerProtocolClasses();
80     saml2md::registerMetadataClasses();
81     saml2md::registerMetadataProviders();
82     saml2md::registerMetadataFilters();
83
84     log.info("library initialization complete");
85     return true;
86 }
87
88 void SAMLInternalConfig::term()
89 {
90 #ifdef _DEBUG
91     xmltooling::NDC ndc("term");
92 #endif
93
94     saml1::AssertionSchemaValidators.destroyValidators();
95     saml1p::ProtocolSchemaValidators.destroyValidators();
96     saml2::AssertionSchemaValidators.destroyValidators();
97     saml2md::MetadataSchemaValidators.destroyValidators();
98     
99     MetadataFilterManager.deregisterFactories();
100     MetadataProviderManager.deregisterFactories();
101
102     XMLToolingConfig::getConfig().term();
103     Category::getInstance(SAML_LOGCAT".SAMLConfig").info("library shutdown complete");
104 }
105
106 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
107 {
108     try {
109         if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
110             throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
111     }
112     catch (XSECCryptoException& e) {
113         throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
114     }
115 }
116
117 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
118 {
119     buf.erase();
120     auto_ptr<unsigned char> hold(new unsigned char[len]);
121     generateRandomBytes(hold.get(),len);
122     for (unsigned int i=0; i<len; i++)
123         buf+=(hold.get())[i];
124 }
125
126 XMLCh* SAMLInternalConfig::generateIdentifier()
127 {
128     unsigned char key[17];
129     generateRandomBytes(key,16);
130     
131     char hexform[34];
132     sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
133             key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
134             key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
135     hexform[33]=0;
136     return XMLString::transcode(hexform);
137 }