1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 /*
21 * XSEC
22 *
23 * XSECPlatformUtils:= To support the platform we run in
24 *
25 * Author(s): Berin Lautenbach
26 *
27 * $Id: XSECPlatformUtils.cpp 1819851 2018-01-02 15:40:04Z scantor $
28 *
29 */
30
31 // XSEC
32
33 #include <xsec/utils/XSECPlatformUtils.hpp>
34 #include <xsec/framework/XSECError.hpp>
35 #include <xsec/dsig/DSIGConstants.hpp>
36 #include <xsec/dsig/DSIGSignature.hpp>
37 #include <xsec/xkms/XKMSConstants.hpp>
38 #include <xsec/framework/XSECAlgorithmMapper.hpp>
39 #include <xsec/transformers/TXFMOutputFile.hpp>
40
41 #include "../xenc/impl/XENCCipherImpl.hpp"
42
43 XERCES_CPP_NAMESPACE_USE
44
45 #if defined (XSEC_HAVE_OPENSSL)
46 # include <xsec/enc/OpenSSL/OpenSSLCryptoProvider.hpp>
47 #endif
48
49 #if defined (XSEC_HAVE_WINCAPI)
50 # include <xsec/enc/WinCAPI/WinCAPICryptoProvider.hpp>
51 #endif
52
53 #if defined (XSEC_HAVE_NSS)
54 # include <xsec/enc/NSS/NSSCryptoProvider.hpp>
55 #endif
56
57 // Static data used by all of XSEC
58 int XSECPlatformUtils::initCount = 0;
59 XSECCryptoProvider * XSECPlatformUtils::g_cryptoProvider = NULL;
60
61 // Have a const copy for external usage
62 const XSECAlgorithmMapper * XSECPlatformUtils::g_algorithmMapper = NULL;
63
64 XSECAlgorithmMapper * internalMapper = NULL;
65
66 XSECPlatformUtils::TransformFactory* XSECPlatformUtils::g_loggingSink = NULL;
67
68 // Determine default crypto provider
69
70 #if defined (XSEC_HAVE_OPENSSL)
71 # define XSEC_DEFAULT_PROVIDER OpenSSLCryptoProvider()
72 #else
73 # if defined (XSEC_HAVE_WINCAPI)
74 # define XSEC_DEFAULT_PROVIDER WinCAPICryptoProvider()
75 # else
76 # if defined (XSEC_HAVE_NSS)
77 # define XSEC_DEFAULT_PROVIDER NSSCryptoProvider()
78 # endif
79 # endif
80 #endif
81
TXFMOutputFileFactory(DOMDocument * doc)82 TXFMBase* TXFMOutputFileFactory(DOMDocument* doc) {
83
84 TXFMOutputFile* sink = new TXFMOutputFile(doc);
85 if (sink)
86 sink->setFile(getenv("XSEC_DEBUG_FILE"));
87 return sink;
88
89 }
90
Initialise(XSECCryptoProvider * p)91 void XSECPlatformUtils::Initialise(XSECCryptoProvider * p) {
92
93 if (++initCount > 1)
94 return;
95
96 if (p != NULL)
97 g_cryptoProvider = p;
98 else
99 #if defined XSEC_DEFAULT_PROVIDER
100 XSECnew(g_cryptoProvider, XSEC_DEFAULT_PROVIDER);
101 #else
102 throw XSECException(XSECException::CryptoProviderError,
103 "XSECPlatformUtils::Initialise() called with NULL provider, but no default defined");
104 #endif
105
106 // Set up necessary constants
107 DSIGConstants::create();
108 #ifdef XSEC_XKMS_ENABLED
109 XKMSConstants::create();
110 #endif
111
112 // Initialise the safeBuffer system
113 safeBuffer::init();
114
115 // Initialise Algorithm Mapper
116 XSECnew(internalMapper, XSECAlgorithmMapper);
117 g_algorithmMapper = internalMapper;
118
119 // Initialise the XENCCipherImpl class
120 XENCCipherImpl::Initialise();
121
122 // Initialise the DSIGSignature class
123 DSIGSignature::Initialise();
124
125 const char* sink = getenv("XSEC_DEBUG_FILE");
126 if (sink && *sink)
127 g_loggingSink = TXFMOutputFileFactory;
128 };
129
SetCryptoProvider(XSECCryptoProvider * p)130 void XSECPlatformUtils::SetCryptoProvider(XSECCryptoProvider * p) {
131
132 if (g_cryptoProvider != NULL)
133 delete g_cryptoProvider;
134
135 g_cryptoProvider = p;
136
137 }
138
SetReferenceLoggingSink(TransformFactory * factory)139 void XSECPlatformUtils::SetReferenceLoggingSink(TransformFactory* factory) {
140
141 g_loggingSink = factory;
142
143 }
144
GetReferenceLoggingSink(DOMDocument * doc)145 TXFMBase* XSECPlatformUtils::GetReferenceLoggingSink(DOMDocument* doc) {
146
147 return (g_loggingSink ? g_loggingSink(doc) : NULL);
148 }
149
Terminate(void)150 void XSECPlatformUtils::Terminate(void) {
151
152 if (--initCount > 0)
153 return;
154
155 // Clean out the algorithm mapper
156 delete internalMapper;
157
158 if (g_cryptoProvider != NULL)
159 delete g_cryptoProvider;
160
161 DSIGConstants::destroy();
162 #ifdef XSEC_XKMS_ENABLED
163 XKMSConstants::destroy();
164 #endif
165
166 }
167
registerAlgorithmHandler(const XMLCh * uri,const XSECAlgorithmHandler & handler)168 void XSECPlatformUtils::registerAlgorithmHandler(
169 const XMLCh * uri,
170 const XSECAlgorithmHandler & handler) {
171
172 internalMapper->registerHandler(uri, handler);
173
174 }
175
whitelistAlgorithm(const XMLCh * uri)176 void XSECPlatformUtils::whitelistAlgorithm(const XMLCh* uri) {
177
178 internalMapper->whitelistAlgorithm(uri);
179
180 }
181
blacklistAlgorithm(const XMLCh * uri)182 void XSECPlatformUtils::blacklistAlgorithm(const XMLCh* uri) {
183
184 internalMapper->blacklistAlgorithm(uri);
185
186 }
187