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