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