6f4bf768c40e86ef095767036881b2462ade9228
[shibboleth/cpp-opensaml.git] / saml / SAMLConfig.cpp
1
2 /*
3  *  Copyright 2001-2006 Internet2
4  * 
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /**
19  * SAMLConfig.cpp
20  * 
21  * Library configuration 
22  */
23
24 #include "internal.h"
25 #include "exceptions.h"
26 #include "SAMLArtifact.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 "saml2/metadata/MetadataProvider.h"
33 #include "security/TrustEngine.h"
34 #include "util/SAMLConstants.h"
35
36 #include <xmltooling/XMLToolingConfig.h>
37 #include <xmltooling/signature/Signature.h>
38 #include <xmltooling/util/NDC.h>
39
40 #include <log4cpp/Category.hh>
41 #include <xsec/enc/XSECCryptoException.hpp>
42 #include <xsec/enc/XSECCryptoProvider.hpp>
43 #include <xsec/utils/XSECPlatformUtils.hpp>
44 #include <openssl/err.h>
45
46 using namespace opensaml;
47 using namespace xmlsignature;
48 using namespace xmltooling;
49 using namespace log4cpp;
50 using namespace std;
51
52 DECL_EXCEPTION_FACTORY(ArtifactException,opensaml);
53 DECL_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
54
55 namespace opensaml {
56    SAMLInternalConfig g_config;
57 }
58
59 SAMLConfig& SAMLConfig::getConfig()
60 {
61     return g_config;
62 }
63
64 SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
65 {
66     return g_config;
67 }
68
69 bool SAMLInternalConfig::init()
70 {
71 #ifdef _DEBUG
72     xmltooling::NDC ndc("init");
73 #endif
74     Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
75     log.debug("library initialization started");
76
77     XMLToolingConfig::getConfig().init();
78     log.debug("XMLTooling library initialized");
79
80     REGISTER_EXCEPTION_FACTORY(ArtifactException,opensaml);
81     REGISTER_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
82
83     registerSAMLArtifacts();
84     saml1::registerAssertionClasses();
85     saml1p::registerProtocolClasses();
86     saml2::registerAssertionClasses();
87     saml2p::registerProtocolClasses();
88     saml2md::registerMetadataClasses();
89     saml2md::registerMetadataProviders();
90     saml2md::registerMetadataFilters();
91     registerTrustEngines();
92
93     log.info("library initialization complete");
94     return true;
95 }
96
97 void SAMLInternalConfig::term()
98 {
99 #ifdef _DEBUG
100     xmltooling::NDC ndc("term");
101 #endif
102
103     saml1::AssertionSchemaValidators.destroyValidators();
104     saml1p::ProtocolSchemaValidators.destroyValidators();
105     saml2::AssertionSchemaValidators.destroyValidators();
106     saml2md::MetadataSchemaValidators.destroyValidators();
107     
108     SAMLArtifactManager.deregisterFactories();
109     MetadataFilterManager.deregisterFactories();
110     MetadataProviderManager.deregisterFactories();
111     TrustEngineManager.deregisterFactories();
112
113     XMLToolingConfig::getConfig().term();
114     Category::getInstance(SAML_LOGCAT".SAMLConfig").info("library shutdown complete");
115 }
116
117 void SAMLInternalConfig::generateRandomBytes(void* buf, unsigned int len)
118 {
119     try {
120         if (XSECPlatformUtils::g_cryptoProvider->getRandom(reinterpret_cast<unsigned char*>(buf),len)<len)
121             throw XMLSecurityException("Unable to generate random data; was PRNG seeded?");
122     }
123     catch (XSECCryptoException& e) {
124         throw XMLSecurityException("Unable to generate random data: $1",params(1,e.getMsg()));
125     }
126 }
127
128 void SAMLInternalConfig::generateRandomBytes(std::string& buf, unsigned int len)
129 {
130     buf.erase();
131     auto_ptr<unsigned char> hold(new unsigned char[len]);
132     generateRandomBytes(hold.get(),len);
133     for (unsigned int i=0; i<len; i++)
134         buf+=(hold.get())[i];
135 }
136
137 XMLCh* SAMLInternalConfig::generateIdentifier()
138 {
139     unsigned char key[17];
140     generateRandomBytes(key,16);
141     
142     char hexform[34];
143     sprintf(hexform,"_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
144             key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],
145             key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
146     hexform[33]=0;
147     return XMLString::transcode(hexform);
148 }
149
150 string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
151 {
152     static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
153
154     auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
155     if (hasher.get()) {
156         auto_ptr<char> dup(strdup(s));
157         unsigned char buf[21];
158         hasher->hash(reinterpret_cast<unsigned char*>(dup.get()),strlen(dup.get()));
159         if (hasher->finish(buf,20)==20) {
160             string ret;
161             if (toHex) {
162                 for (unsigned int i=0; i<20; i++) {
163                     ret+=(DIGITS[((unsigned char)(0xF0 & buf[i])) >> 4 ]);
164                     ret+=(DIGITS[0x0F & buf[i]]);
165                 }
166             }
167             else {
168                 for (unsigned int i=0; i<20; i++) {
169                     ret+=buf[i];
170                 }
171             }
172             return ret;
173         }
174     }
175     throw XMLSecurityException("Unable to generate SHA-1 hash.");
176 }
177
178 void opensaml::log_openssl()
179 {
180     const char* file;
181     const char* data;
182     int flags,line;
183
184     unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
185     while (code) {
186         Category& log=Category::getInstance("OpenSSL");
187         log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
188         if (data && (flags & ERR_TXT_STRING))
189             log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
190         code=ERR_get_error_line_data(&file,&line,&data,&flags);
191     }
192 }