@@ -41,78 +41,70 @@ public final class BouncyCastleUtil { |
41 | 41 | private static final String BC_JSSE_SSLENGINE = "org.bouncycastle.jsse.BCSSLEngine"; |
42 | 42 | private static final String BC_JSSE_ALPN_SELECTOR = "org.bouncycastle.jsse.BCApplicationProtocolSelector"; |
43 | 43 |
|
44 | | - private static volatile Throwable unavailabilityCauseBcProv; |
45 | | - private static volatile Throwable unavailabilityCauseBcPkix; |
46 | | - private static volatile Throwable unavailabilityCauseBcTls; |
47 | | - private static volatile Provider bcProviderJce; |
48 | | - private static volatile Provider bcProviderJsse; |
49 | | - private static volatile Class<? extends SSLEngine> bcSSLEngineClass; |
50 | | - private static volatile boolean attemptedLoading; |
51 | | - |
52 | 44 | /** |
53 | 45 | * Indicate whether the BouncyCastle Java Crypto Extensions provider is available. |
54 | 46 | */ |
55 | 47 | public static boolean isBcProvAvailable() { |
56 | | - ensureLoaded(); |
57 | | - return unavailabilityCauseBcProv == null; |
| 48 | + BcProv.ensureLoaded(); |
| 49 | + return BcProv.unavailabilityCauseBcProv == null; |
58 | 50 | } |
59 | 51 |
|
60 | 52 | /** |
61 | 53 | * Indicate whether the BouncyCastle Public-Key Infrastructure utilities are available. |
62 | 54 | */ |
63 | 55 | public static boolean isBcPkixAvailable() { |
64 | | - ensureLoaded(); |
65 | | - return unavailabilityCauseBcPkix == null; |
| 56 | + BcPkix.ensureLoaded(); |
| 57 | + return BcPkix.unavailabilityCauseBcPkix == null; |
66 | 58 | } |
67 | 59 |
|
68 | 60 | /** |
69 | 61 | * Indicate whether the BouncyCastle Java Secure Socket Extensions provider is available. |
70 | 62 | */ |
71 | 63 | public static boolean isBcTlsAvailable() { |
72 | | - ensureLoaded(); |
73 | | - return unavailabilityCauseBcTls == null; |
| 64 | + BcTls.ensureLoaded(); |
| 65 | + return BcTls.unavailabilityCauseBcTls == null; |
74 | 66 | } |
75 | 67 |
|
76 | 68 | /** |
77 | 69 | * @return the cause if unavailable. {@code null} if available. |
78 | 70 | */ |
79 | 71 | public static Throwable unavailabilityCauseBcProv() { |
80 | | - ensureLoaded(); |
81 | | - return unavailabilityCauseBcProv; |
| 72 | + BcProv.ensureLoaded(); |
| 73 | + return BcProv.unavailabilityCauseBcProv; |
82 | 74 | } |
83 | 75 |
|
84 | 76 | /** |
85 | 77 | * @return the cause if unavailable. {@code null} if available. |
86 | 78 | */ |
87 | 79 | public static Throwable unavailabilityCauseBcPkix() { |
88 | | - ensureLoaded(); |
89 | | - return unavailabilityCauseBcPkix; |
| 80 | + BcPkix.ensureLoaded(); |
| 81 | + return BcPkix.unavailabilityCauseBcPkix; |
90 | 82 | } |
91 | 83 |
|
92 | 84 | /** |
93 | 85 | * @return the cause if unavailable. {@code null} if available. |
94 | 86 | */ |
95 | 87 | public static Throwable unavailabilityCauseBcTls() { |
96 | | - ensureLoaded(); |
97 | | - return unavailabilityCauseBcTls; |
| 88 | + BcTls.ensureLoaded(); |
| 89 | + return BcTls.unavailabilityCauseBcTls; |
98 | 90 | } |
99 | 91 |
|
100 | 92 | /** |
101 | 93 | * Indicates whether the given SSLEngine is implemented by BouncyCastle. |
102 | 94 | */ |
103 | 95 | public static boolean isBcJsseInUse(SSLEngine engine) { |
104 | | - ensureLoaded(); |
105 | | - Class<? extends SSLEngine> bcEngineClass = bcSSLEngineClass; |
| 96 | + BcTls.ensureLoaded(); |
| 97 | + Class<? extends SSLEngine> bcEngineClass = BcTls.bcSSLEngineClass; |
106 | 98 | return bcEngineClass != null && bcEngineClass.isInstance(engine); |
107 | 99 | } |
108 | 100 |
|
109 | 101 | /** |
110 | 102 | * Get the BouncyCastle Java Crypto Extensions provider, or throw an exception if it is unavailable. |
111 | 103 | */ |
112 | 104 | public static Provider getBcProviderJce() { |
113 | | - ensureLoaded(); |
114 | | - Throwable cause = unavailabilityCauseBcProv; |
115 | | - Provider provider = bcProviderJce; |
| 105 | + BcProv.ensureLoaded(); |
| 106 | + Throwable cause = BcProv.unavailabilityCauseBcProv; |
| 107 | + Provider provider = BcProv.bcProviderJce; |
116 | 108 | if (cause != null || provider == null) { |
117 | 109 | throw new IllegalStateException(cause); |
118 | 110 | } |
@@ -123,9 +115,9 @@ public static Provider getBcProviderJce() { |
123 | 115 | * Get the BouncyCastle Java Secure Socket Extensions provider, or throw an exception if it is unavailable. |
124 | 116 | */ |
125 | 117 | public static Provider getBcProviderJsse() { |
126 | | - ensureLoaded(); |
127 | | - Throwable cause = unavailabilityCauseBcTls; |
128 | | - Provider provider = bcProviderJsse; |
| 118 | + BcTls.ensureLoaded(); |
| 119 | + Throwable cause = BcTls.unavailabilityCauseBcTls; |
| 120 | + Provider provider = BcTls.bcProviderJsse; |
129 | 121 | if (cause != null || provider == null) { |
130 | 122 | throw new IllegalStateException(cause); |
131 | 123 | } |
@@ -139,97 +131,157 @@ public static Provider getBcProviderJsse() { |
139 | 131 | * @return engine class. |
140 | 132 | */ |
141 | 133 | public static Class<? extends SSLEngine> getBcSSLEngineClass() { |
142 | | - ensureLoaded(); |
143 | | - return bcSSLEngineClass; |
| 134 | + BcTls.ensureLoaded(); |
| 135 | + return BcTls.bcSSLEngineClass; |
144 | 136 | } |
145 | 137 |
|
146 | 138 | /** |
147 | 139 | * Reset the loaded providers. Useful for testing, to redo the loading under different conditions. |
148 | 140 | */ |
149 | 141 | static void reset() { |
150 | | - attemptedLoading = false; |
151 | | - unavailabilityCauseBcProv = null; |
152 | | - unavailabilityCauseBcPkix = null; |
153 | | - unavailabilityCauseBcTls = null; |
154 | | - bcProviderJce = null; |
155 | | - bcProviderJsse = null; |
156 | | - bcSSLEngineClass = null; |
157 | | - } |
158 | | - |
159 | | - private static void ensureLoaded() { |
160 | | - if (!attemptedLoading) { |
161 | | - tryLoading(); |
162 | | - } |
| 142 | + BcProv.attemptedLoading = false; |
| 143 | + BcPkix.attemptedLoading = false; |
| 144 | + BcTls.attemptedLoading = false; |
| 145 | + BcProv.unavailabilityCauseBcProv = null; |
| 146 | + BcPkix.unavailabilityCauseBcPkix = null; |
| 147 | + BcTls.unavailabilityCauseBcTls = null; |
| 148 | + BcProv.bcProviderJce = null; |
| 149 | + BcTls.bcProviderJsse = null; |
| 150 | + BcTls.bcSSLEngineClass = null; |
163 | 151 | } |
164 | 152 |
|
165 | | - @SuppressWarnings("unchecked") |
166 | | - private static void tryLoading() { |
167 | | - AccessController.doPrivileged((PrivilegedAction<?>) () -> { |
168 | | - try { |
169 | | - // Check for bcprov-jdk18on or bc-fips: |
170 | | - Provider provider = Security.getProvider(BC_PROVIDER_NAME); |
171 | | - if (provider == null) { |
172 | | - provider = Security.getProvider(BC_FIPS_PROVIDER_NAME); |
173 | | - } |
174 | | - if (provider == null) { |
175 | | - ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader(); |
176 | | - Class<Provider> bcProviderClass; |
| 153 | + private static final class BcProv { |
| 154 | + static volatile Throwable unavailabilityCauseBcProv; |
| 155 | + static volatile Provider bcProviderJce; |
| 156 | + static volatile boolean attemptedLoading; |
| 157 | + |
| 158 | + @SuppressWarnings("unchecked") |
| 159 | + private static void ensureLoaded() { |
| 160 | + if (!attemptedLoading) { |
| 161 | + AccessController.doPrivileged((PrivilegedAction<?>) () -> { |
177 | 162 | try { |
178 | | - bcProviderClass = (Class<Provider>) Class.forName(BC_PROVIDER, true, classLoader); |
179 | | - } catch (ClassNotFoundException e) { |
180 | | - try { |
181 | | - bcProviderClass = (Class<Provider>) Class.forName(BC_FIPS_PROVIDER, true, classLoader); |
182 | | - } catch (ClassNotFoundException ex) { |
183 | | - ThrowableUtil.addSuppressed(e, ex); |
184 | | - throw e; |
| 163 | + // Check for bcprov-jdk18on or bc-fips: |
| 164 | + Provider provider = Security.getProvider(BC_PROVIDER_NAME); |
| 165 | + if (provider == null) { |
| 166 | + provider = Security.getProvider(BC_FIPS_PROVIDER_NAME); |
185 | 167 | } |
| 168 | + if (provider == null) { |
| 169 | + ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader(); |
| 170 | + Class<Provider> bcProviderClass; |
| 171 | + try { |
| 172 | + bcProviderClass = (Class<Provider>) Class.forName( |
| 173 | + BC_PROVIDER, true, classLoader); |
| 174 | + } catch (ClassNotFoundException e) { |
| 175 | + try { |
| 176 | + bcProviderClass = (Class<Provider>) Class.forName( |
| 177 | + BC_FIPS_PROVIDER, true, classLoader); |
| 178 | + } catch (ClassNotFoundException ex) { |
| 179 | + ThrowableUtil.addSuppressed(e, ex); |
| 180 | + throw e; |
| 181 | + } |
| 182 | + } |
| 183 | + provider = bcProviderClass.getConstructor().newInstance(); |
| 184 | + } |
| 185 | + bcProviderJce = provider; |
| 186 | + logger.debug("Bouncy Castle provider available"); |
| 187 | + } catch (Throwable e) { |
| 188 | + String msg = "Bouncy Castle provider (bcprov-jdk18on or bc-fips) was not loaded"; |
| 189 | + if (includeLoadingErrorStackTrace(e)) { |
| 190 | + logger.debug(msg, e); |
| 191 | + } else { |
| 192 | + logger.debug(msg); |
| 193 | + } |
| 194 | + unavailabilityCauseBcProv = e; |
186 | 195 | } |
187 | | - provider = bcProviderClass.getConstructor().newInstance(); |
188 | | - } |
189 | | - bcProviderJce = provider; |
190 | | - logger.debug("Bouncy Castle provider available"); |
191 | | - } catch (Throwable e) { |
192 | | - logger.debug("Cannot load Bouncy Castle provider", e); |
193 | | - unavailabilityCauseBcProv = e; |
| 196 | + return null; |
| 197 | + }); |
| 198 | + attemptedLoading = true; |
194 | 199 | } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private static final class BcPkix { |
| 204 | + static volatile Throwable unavailabilityCauseBcPkix; |
| 205 | + static volatile boolean attemptedLoading; |
195 | 206 |
|
196 | | - try { |
197 | | - // Check for bcpkix-jdk18on: |
198 | | - ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader(); |
199 | | - Provider provider = bcProviderJce; |
200 | | - if (provider != null) { |
201 | | - // Use provider class loader in case it was loaded by the system loader. |
202 | | - classLoader = provider.getClass().getClassLoader(); |
203 | | - } |
204 | | - Class.forName(BC_PEMPARSER, true, classLoader); |
205 | | - logger.debug("Bouncy Castle PKIX available"); |
206 | | - } catch (Throwable e) { |
207 | | - logger.debug("Cannot load Bouncy Castle PKIX", e); |
208 | | - unavailabilityCauseBcPkix = e; |
| 207 | + private static void ensureLoaded() { |
| 208 | + if (!attemptedLoading) { |
| 209 | + BcProv.ensureLoaded(); // For BcProv.bcProviderJce |
| 210 | + AccessController.doPrivileged((PrivilegedAction<?>) () -> { |
| 211 | + try { |
| 212 | + // Check for bcpkix-jdk18on: |
| 213 | + ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader(); |
| 214 | + Provider provider = BcProv.bcProviderJce; |
| 215 | + if (provider != null) { |
| 216 | + // Use provider class loader in case it was loaded by the system loader. |
| 217 | + classLoader = provider.getClass().getClassLoader(); |
| 218 | + } |
| 219 | + Class.forName(BC_PEMPARSER, true, classLoader); |
| 220 | + logger.debug("Bouncy Castle PKIX available"); |
| 221 | + } catch (Throwable e) { |
| 222 | + String msg = "Bouncy Castle PKIX (bcpkix-jdk18on) was not loaded"; |
| 223 | + if (includeLoadingErrorStackTrace(e)) { |
| 224 | + logger.debug(msg, e); |
| 225 | + } else { |
| 226 | + logger.debug(msg); |
| 227 | + } |
| 228 | + unavailabilityCauseBcPkix = e; |
| 229 | + } |
| 230 | + return null; |
| 231 | + }); |
| 232 | + attemptedLoading = true; |
209 | 233 | } |
| 234 | + } |
| 235 | + } |
210 | 236 |
|
211 | | - try { |
212 | | - // Check for bctls-jdk18on: |
213 | | - ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader(); |
214 | | - Provider provider = Security.getProvider(BC_JSSE_PROVIDER_NAME); |
215 | | - if (provider != null) { |
216 | | - // Use provider class loader in case it was loaded by the system loader. |
217 | | - classLoader = provider.getClass().getClassLoader(); |
218 | | - } else { |
219 | | - Class<?> providerClass = Class.forName(BC_JSSE_PROVIDER, true, classLoader); |
220 | | - provider = (Provider) providerClass.getConstructor().newInstance(); |
221 | | - } |
222 | | - bcSSLEngineClass = (Class<? extends SSLEngine>) Class.forName(BC_JSSE_SSLENGINE, true, classLoader); |
223 | | - Class.forName(BC_JSSE_ALPN_SELECTOR, true, classLoader); |
224 | | - bcProviderJsse = provider; |
225 | | - logger.debug("Bouncy Castle JSSE available"); |
226 | | - } catch (Throwable e) { |
227 | | - logger.debug("Cannot load Bouncy Castle TLS", e); |
228 | | - unavailabilityCauseBcTls = e; |
| 237 | + private static final class BcTls { |
| 238 | + static volatile Throwable unavailabilityCauseBcTls; |
| 239 | + static volatile Provider bcProviderJsse; |
| 240 | + static volatile Class<? extends SSLEngine> bcSSLEngineClass; |
| 241 | + static volatile boolean attemptedLoading; |
| 242 | + |
| 243 | + @SuppressWarnings("unchecked") |
| 244 | + private static void ensureLoaded() { |
| 245 | + if (!attemptedLoading) { |
| 246 | + AccessController.doPrivileged((PrivilegedAction<?>) () -> { |
| 247 | + try { |
| 248 | + // Check for bctls-jdk18on: |
| 249 | + ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader(); |
| 250 | + Provider provider = Security.getProvider(BC_JSSE_PROVIDER_NAME); |
| 251 | + if (provider != null) { |
| 252 | + // Use provider class loader in case it was loaded by the system loader. |
| 253 | + classLoader = provider.getClass().getClassLoader(); |
| 254 | + } else { |
| 255 | + Class<?> providerClass = Class.forName(BC_JSSE_PROVIDER, true, classLoader); |
| 256 | + provider = (Provider) providerClass.getConstructor().newInstance(); |
| 257 | + } |
| 258 | + bcSSLEngineClass = (Class<? extends SSLEngine>) Class.forName( |
| 259 | + BC_JSSE_SSLENGINE, true, classLoader); |
| 260 | + Class.forName(BC_JSSE_ALPN_SELECTOR, true, classLoader); |
| 261 | + bcProviderJsse = provider; |
| 262 | + logger.debug("Bouncy Castle JSSE available"); |
| 263 | + } catch (Throwable e) { |
| 264 | + String msg = "Bouncy Castle TLS (bctls-jdk18on) was not loaded"; |
| 265 | + if (includeLoadingErrorStackTrace(e)) { |
| 266 | + logger.debug(msg, e); |
| 267 | + } else { |
| 268 | + logger.debug(msg); |
| 269 | + } |
| 270 | + unavailabilityCauseBcTls = e; |
| 271 | + } |
| 272 | + return null; |
| 273 | + }); |
| 274 | + attemptedLoading = true; |
229 | 275 | } |
230 | | - attemptedLoading = true; |
231 | | - return null; |
232 | | - }); |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + private static boolean includeLoadingErrorStackTrace(Throwable e) { |
| 280 | + // Only include the stack trace if we have TRACE logging enabled, |
| 281 | + // or if the exception is something other than ClassNotFoundException. |
| 282 | + // The ClassNotFoundException is what we would expect to see if the |
| 283 | + // BC JAR files are just not on the classpath. |
| 284 | + return logger.isTraceEnabled() || !(e instanceof ClassNotFoundException); |
233 | 285 | } |
234 | 286 |
|
235 | 287 | private BouncyCastleUtil() { |
|