1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.common.crypto;
21
22 import java.lang.reflect.Field;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
25 import java.security.PrivilegedExceptionAction;
26 import java.security.Provider;
27 import java.security.Security;
28
29 import org.apache.wss4j.common.util.FIPSUtils;
30 import org.apache.wss4j.common.util.Loader;
31 import org.apache.xml.security.utils.I18n;
32 import org.apache.xml.security.utils.XMLUtils;
33
34
35
36
37
38 public final class WSProviderConfig {
39
40 private static final org.slf4j.Logger LOG =
41 org.slf4j.LoggerFactory.getLogger(WSProviderConfig.class);
42
43
44
45
46
47
48
49
50 private static boolean addJceProviders = true;
51
52
53
54
55
56
57 private static boolean staticallyInitialized;
58
59 private static boolean santuarioProviderAdded;
60 private static boolean bcProviderAdded;
61 private static boolean tlProviderAdded;
62
63 private WSProviderConfig() {
64
65 }
66
67 public static synchronized void init() {
68 if (!staticallyInitialized) {
69 if (addJceProviders) {
70 initializeResourceBundles();
71 setXmlSecIgnoreLineBreak();
72 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
73 public Boolean run() {
74 addXMLDSigRIInternal();
75 return true;
76 }
77 });
78
79 santuarioProviderAdded = true;
80 bcProviderAdded = false;
81 tlProviderAdded = false;
82 }
83 if (FIPSUtils.isFIPSEnabled()) {
84
85
86
87 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
88 public Boolean run() {
89 addJceProvider("BCFIPS", "org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider");
90 return true;
91 }
92 });
93 }
94 staticallyInitialized = true;
95 }
96 }
97
98 public static synchronized void init(boolean addXMLDSigRIInternalProv, boolean addBCProv, boolean addTLProv) {
99 if (!staticallyInitialized) {
100 initializeResourceBundles();
101 setXmlSecIgnoreLineBreak();
102 santuarioProviderAdded = addXMLDSigRIInternalProv;
103 if (addXMLDSigRIInternalProv) {
104 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
105 public Boolean run() {
106 addXMLDSigRIInternal();
107 return true;
108 }
109 });
110 }
111
112 bcProviderAdded = addBCProv;
113 if (addBCProv) {
114 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
115 public Boolean run() {
116 addJceProvider("BC", "org.bouncycastle.jce.provider.BouncyCastleProvider");
117 return true;
118 }
119 });
120 }
121 if (FIPSUtils.isFIPSEnabled()) {
122
123
124
125
126
127 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
128 public Boolean run() {
129 addJceProvider("BCFIPS", "org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider");
130 return true;
131 }
132 });
133
134 }
135
136 tlProviderAdded = addTLProv;
137 if (addTLProv) {
138 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
139 public Boolean run() {
140 ThreadLocalSecurityProvider.install();
141 return true;
142 }
143 });
144 }
145 staticallyInitialized = true;
146 }
147 }
148
149 public static synchronized void cleanUp() {
150 if (staticallyInitialized) {
151 if (santuarioProviderAdded) {
152 Security.removeProvider("ApacheXMLDSig");
153 santuarioProviderAdded = false;
154 }
155 if (bcProviderAdded) {
156 Security.removeProvider("BC");
157 bcProviderAdded = false;
158 }
159 if (tlProviderAdded) {
160 Security.removeProvider("TLSP");
161 tlProviderAdded = false;
162 }
163
164 staticallyInitialized = false;
165 }
166 }
167
168
169
170
171
172
173
174
175 public static void setAddJceProviders(boolean value) {
176 addJceProviders = value;
177 }
178
179 public static void setXmlSecIgnoreLineBreak() {
180
181 boolean wasSet = false;
182 try {
183
184 wasSet = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
185 public Boolean run() {
186 String lineBreakPropName = "org.apache.xml.security.ignoreLineBreaks";
187 if (System.getProperty(lineBreakPropName) == null) {
188 System.setProperty(lineBreakPropName, "true");
189 return false;
190 }
191 return true;
192 }
193 });
194 } catch (Throwable t) {
195
196 }
197 org.apache.xml.security.Init.init();
198 if (!wasSet) {
199 try {
200 AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
201 public Boolean run() throws Exception {
202 Field f = XMLUtils.class.getDeclaredField("ignoreLineBreaks");
203 f.setAccessible(true);
204 f.set(null, Boolean.TRUE);
205 return false;
206 }
207 });
208 } catch (Throwable t) {
209
210 }
211 }
212 }
213
214 private static void addXMLDSigRIInternal() {
215 Security.removeProvider("ApacheXMLDSig");
216 addJceProvider("ApacheXMLDSig", SantuarioUtil.getSantuarioProvider());
217 }
218
219 private static void initializeResourceBundles() {
220 I18n.init(new WSS4JResourceBundle());
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 public static String addJceProvider(String name, String className) {
237 Provider currentProvider = Security.getProvider(name);
238 if (currentProvider == null) {
239 try {
240 Class<? extends Provider> clazz = Loader.loadClass(className, false, Provider.class);
241 Provider provider = clazz.getDeclaredConstructor().newInstance();
242 return addJceProvider(name, provider);
243 } catch (Throwable t) {
244 if (LOG.isDebugEnabled()) {
245 LOG.debug("The provider " + name + " could not be added: " + t.getMessage(), t);
246 }
247 return null;
248 }
249 }
250 return currentProvider.getName();
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264
265 public static String addJceProvider(String name, Provider provider) {
266 Provider currentProvider = Security.getProvider(name);
267 if (currentProvider == null) {
268 try {
269 int ret = Security.addProvider(provider);
270 if (LOG.isDebugEnabled()) {
271 LOG.debug(
272 "The provider " + provider.getName() + " - "
273 + provider.getVersionStr() + " was added at position: " + ret
274 );
275 }
276 return provider.getName();
277 } catch (Throwable t) {
278 if (LOG.isDebugEnabled()) {
279 LOG.debug("The provider " + name + " could not be added: " + t.getMessage(), t);
280 }
281 return null;
282 }
283 }
284 return currentProvider.getName();
285 }
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 public static String appendJceProvider(String name, String className) {
303 Provider currentProvider = Security.getProvider(name);
304 if (currentProvider == null) {
305 try {
306 Class<? extends Provider> clazz = Loader.loadClass(className, false, Provider.class);
307 Provider provider = clazz.getDeclaredConstructor().newInstance();
308
309 int ret = Security.addProvider(provider);
310 LOG.debug(
311 "The provider {} was added at position: {}",
312 provider.getName(), ret
313 );
314 return provider.getName();
315 } catch (Throwable t) {
316 if (LOG.isDebugEnabled()) {
317 LOG.debug("The provider " + name + " could not be added: " + t.getMessage(), t);
318 }
319 return null;
320 }
321 }
322 return currentProvider.getName();
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 public static String appendJceProvider(String name, Provider provider) {
339 Provider currentProvider = Security.getProvider(name);
340 if (currentProvider == null) {
341 try {
342 int ret = Security.addProvider(provider);
343 LOG.debug(
344 "The provider {} was added at position: {}",
345 provider.getName(), ret
346 );
347 return provider.getName();
348 } catch (Throwable t) {
349 if (LOG.isDebugEnabled()) {
350 LOG.debug("The provider " + name + " could not be added: " + t.getMessage(), t);
351 }
352 return null;
353 }
354 }
355 return currentProvider.getName();
356 }
357
358 }