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