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