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