695964a5c3c996b1a90219d70d608ecdba79fcb9
[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 #include "internal.h"
24 #include "exceptions.h"
25 #include "SAMLArtifact.h"
26 #include "SAMLConfig.h"
27 #include "saml1/core/Assertions.h"
28 #include "saml1/core/Protocols.h"
29 #include "saml2/core/Protocols.h"
30 #include "saml2/metadata/Metadata.h"
31 #include "saml2/metadata/MetadataProvider.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/enc/XSECCryptoProvider.hpp>
41 #include <xsec/utils/XSECPlatformUtils.hpp>
42
43
44 using namespace opensaml;
45 using namespace xmlsignature;
46 using namespace xmltooling;
47 using namespace log4cpp;
48 using namespace std;
49
50 DECL_EXCEPTION_FACTORY(ArtifactException,opensaml);
51 DECL_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
52
53 namespace opensaml {
54    SAMLInternalConfig g_config;
55 }
56
57 SAMLConfig& SAMLConfig::getConfig()
58 {
59     return g_config;
60 }
61
62 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
63 {
64     return g_config;
65 }
66
67 bool SAMLInternalConfig::init()
68 {
69 #ifdef _DEBUG
70     xmltooling::NDC ndc("init");
71 #endif
72     Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
73     log.debug("library initialization started");
74
75     XMLToolingConfig::getConfig().init();
76     log.debug("XMLTooling library initialized");
77
78     REGISTER_EXCEPTION_FACTORY(ArtifactException,opensaml);
79     REGISTER_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
80
81     registerSAMLArtifacts();
82     saml1::registerAssertionClasses();
83     saml1p::registerProtocolClasses();
84     saml2::registerAssertionClasses();
85     saml2p::registerProtocolClasses();
86     saml2md::registerMetadataClasses();
87     saml2md::registerMetadataProviders();
88     saml2md::registerMetadataFilters();
89
90     log.info("library initialization complete");
91     return true;
92 }
93
94 void SAMLInternalConfig::term()
95 {
96 #ifdef _DEBUG
97     xmltooling::NDC ndc("term");
98 #endif
99
100     saml1::AssertionSchemaValidators.destroyValidators();
101     saml1p::ProtocolSchemaValidators.destroyValidators();
102     saml2::AssertionSchemaValidators.destroyValidators();
103     saml2md::MetadataSchemaValidators.destroyValidators();
104     
105     SAMLArtifactManager.deregisterFactories();
106     MetadataFilterManager.deregisterFactories();
107     MetadataProviderManager.deregisterFactories();
108
109     XMLToolingConfig::getConfig().term();
110     Category::getInstance(SAML_LOGCAT".SAMLConfig").info("library shutdown complete");
111 }
112
113 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
114 {
115     try {
116         if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
117             throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
118     }
119     catch (XSECCryptoException& e) {
120         throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
121     }
122 }
123
124 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
125 {
126     buf.erase();
127     auto_ptr<unsigned char> hold(new unsigned char[len]);
128     generateRandomBytes(hold.get(),len);
129     for (unsigned int i=0; i<len; i++)
130         buf+=(hold.get())[i];
131 }
132
133 XMLCh* SAMLInternalConfig::generateIdentifier()
134 {
135     unsigned char key[17];
136     generateRandomBytes(key,16);
137     
138     char hexform[34];
139     sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
140             key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
141             key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
142     hexform[33]=0;
143     return XMLString::transcode(hexform);
144 }
145
146 string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
147 {
148     static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
149
150     auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
151     if (hasher.get()) {
152         auto_ptr<char> dup(strdup(s));
153         unsigned char buf[21];
154         hasher->hash(reinterpret_cast<unsigned char*>(dup.get()),strlen(dup.get()));
155         if (hasher->finish(buf,20)==20) {
156             string ret;
157             if (toHex) {
158                 for (unsigned int i=0; i<20; i++) {
159                     ret+=(DIGITS[((unsigned char)(0xF0 & buf[i])) >> 4 ]);
160                     ret+=(DIGITS[0x0F & buf[i]]);
161                 }
162             }
163             else {
164                 for (unsigned int i=0; i<20; i++) {
165                     ret+=buf[i];
166                 }
167             }
168             return ret;
169         }
170     }
171     throw XMLSecurityException("Unable to generate SHA-1 hash.");
172 }