@@ -139,7 +139,38 @@ public static Provider getSecurityProvider() {
139
139
return securityProvider ;
140
140
}
141
141
142
+ static final boolean SPI_ACCESSIBLE ;
143
+
144
+ static {
145
+ boolean canSetAccessible = true ;
146
+ if ( OpenSSL .javaVersion9 (true ) ) {
147
+ final Provider provider = getSecurityProvider ();
148
+ if ( provider != null ) {
149
+ try {
150
+ // NOTE: some getXxx pieces might still work
151
+ // where SPI are returned directly + there's a public <init> e.g. MessageDigest(...)
152
+ getCertificateFactory ("X.509" , provider ); // !!! disables EVERYTHING :(
153
+ }
154
+ catch (CertificateException ex ) {
155
+ debugStackTrace (ex );
156
+ canSetAccessible = false ;
157
+ }
158
+ catch (RuntimeException ex ) {
159
+ debugStackTrace (ex );
160
+ // java.lang.reflect.InaccessibleObjectException (extends RuntimeException)
161
+ canSetAccessible = false ;
162
+ }
163
+ }
164
+ }
165
+ SPI_ACCESSIBLE = canSetAccessible ;
166
+ }
167
+
168
+ static Provider getSecurityProviderIfAccessible () {
169
+ return SPI_ACCESSIBLE ? getSecurityProvider () : null ;
170
+ }
171
+
142
172
public static synchronized void setSecurityProvider (final Provider provider ) {
173
+ if ( provider != null ) OpenSSL .debug ("using provider: " + provider );
143
174
securityProvider = provider ;
144
175
}
145
176
@@ -165,7 +196,7 @@ static boolean isProviderAvailable(final String name) {
165
196
return Security .getProvider (name ) != null ;
166
197
}
167
198
168
- static boolean isProviderRegistered () {
199
+ public static boolean isProviderRegistered () {
169
200
if ( securityProvider == null ) return false ;
170
201
return Security .getProvider (securityProvider .getName ()) != null ;
171
202
}
@@ -190,7 +221,7 @@ private static void doRegisterProvider() {
190
221
public static CertificateFactory getCertificateFactory (final String type )
191
222
throws CertificateException {
192
223
try {
193
- final Provider provider = getSecurityProvider ();
224
+ final Provider provider = getSecurityProviderIfAccessible ();
194
225
if ( provider != null ) return getCertificateFactory (type , provider );
195
226
}
196
227
catch (CertificateException e ) { debugStackTrace (e ); }
@@ -227,7 +258,7 @@ static CertificateFactory getCertificateFactory(final String type, final Provide
227
258
public static KeyFactory getKeyFactory (final String algorithm )
228
259
throws NoSuchAlgorithmException {
229
260
try {
230
- final Provider provider = getSecurityProvider ();
261
+ final Provider provider = getSecurityProviderIfAccessible ();
231
262
if ( provider != null ) return getKeyFactory (algorithm , provider );
232
263
}
233
264
catch (NoSuchAlgorithmException e ) { }
@@ -250,7 +281,7 @@ static KeyFactory getKeyFactory(final String algorithm, final Provider provider)
250
281
public static KeyPairGenerator getKeyPairGenerator (final String algorithm )
251
282
throws NoSuchAlgorithmException {
252
283
try {
253
- final Provider provider = getSecurityProvider ();
284
+ final Provider provider = getSecurityProviderIfAccessible ();
254
285
if ( provider != null ) return getKeyPairGenerator (algorithm , provider );
255
286
}
256
287
catch (NoSuchAlgorithmException e ) { }
@@ -290,7 +321,7 @@ static KeyPairGenerator getKeyPairGenerator(final String algorithm, final Provid
290
321
public static KeyStore getKeyStore (final String type )
291
322
throws KeyStoreException {
292
323
try {
293
- final Provider provider = getSecurityProvider ();
324
+ final Provider provider = getSecurityProviderIfAccessible ();
294
325
if ( provider != null ) return getKeyStore (type , provider );
295
326
}
296
327
catch (KeyStoreException e ) { }
@@ -307,7 +338,7 @@ static KeyStore getKeyStore(final String type, final Provider provider)
307
338
*/
308
339
public static MessageDigest getMessageDigest (final String algorithm ) throws NoSuchAlgorithmException {
309
340
try {
310
- final Provider provider = getSecurityProvider ();
341
+ final Provider provider = getSecurityProviderIfAccessible ();
311
342
if ( provider != null ) return getMessageDigest (algorithm , provider );
312
343
}
313
344
catch (NoSuchAlgorithmException e ) { }
@@ -341,7 +372,7 @@ static MessageDigest getMessageDigest(final String algorithm, final Provider pro
341
372
342
373
public static SecureRandom getSecureRandom () {
343
374
try {
344
- final Provider provider = getSecurityProvider ();
375
+ final Provider provider = getSecurityProviderIfAccessible ();
345
376
if ( provider != null ) {
346
377
final String algorithm = getSecureRandomAlgorithm (provider );
347
378
if ( algorithm != null ) {
@@ -473,7 +504,7 @@ private static Cipher getCipherInternal(String transformation, final Provider pr
473
504
*/
474
505
public static Signature getSignature (final String algorithm ) throws NoSuchAlgorithmException {
475
506
try {
476
- final Provider provider = getSecurityProvider ();
507
+ final Provider provider = getSecurityProviderIfAccessible ();
477
508
if ( provider != null ) return getSignature (algorithm , provider );
478
509
}
479
510
catch (NoSuchAlgorithmException e ) { }
@@ -509,7 +540,7 @@ static Signature getSignature(final String algorithm, final Provider provider)
509
540
*/
510
541
public static Mac getMac (final String algorithm ) throws NoSuchAlgorithmException {
511
542
Mac mac = null ;
512
- final Provider provider = getSecurityProvider ();
543
+ final Provider provider = getSecurityProviderIfAccessible ();
513
544
if ( provider != null ) {
514
545
mac = getMac (algorithm , provider , true );
515
546
}
@@ -540,7 +571,7 @@ private static Mac getMac(final String algorithm, final Provider provider, boole
540
571
*/
541
572
public static KeyGenerator getKeyGenerator (final String algorithm ) throws NoSuchAlgorithmException {
542
573
try {
543
- final Provider provider = getSecurityProvider ();
574
+ final Provider provider = getSecurityProviderIfAccessible ();
544
575
if ( provider != null ) return getKeyGenerator (algorithm , provider );
545
576
}
546
577
catch (NoSuchAlgorithmException e ) { }
@@ -564,7 +595,7 @@ static KeyGenerator getKeyGenerator(final String algorithm, final Provider provi
564
595
*/
565
596
public static KeyAgreement getKeyAgreement (final String algorithm ) throws NoSuchAlgorithmException {
566
597
try {
567
- final Provider provider = getSecurityProvider ();
598
+ final Provider provider = getSecurityProviderIfAccessible ();
568
599
if ( provider != null ) return getKeyAgreement (algorithm , provider );
569
600
}
570
601
catch (NoSuchAlgorithmException e ) { }
@@ -588,7 +619,7 @@ static KeyAgreement getKeyAgreement(final String algorithm, final Provider provi
588
619
*/
589
620
public static SecretKeyFactory getSecretKeyFactory (final String algorithm ) throws NoSuchAlgorithmException {
590
621
try {
591
- final Provider provider = getSecurityProvider ();
622
+ final Provider provider = getSecurityProviderIfAccessible ();
592
623
if ( provider != null ) return getSecretKeyFactory (algorithm , provider );
593
624
}
594
625
catch (NoSuchAlgorithmException e ) { }
@@ -613,7 +644,7 @@ public static SSLContext getSSLContext(final String protocol)
613
644
throws NoSuchAlgorithmException {
614
645
try {
615
646
if ( providerSSLContext ) {
616
- final Provider provider = getSecurityProvider ();
647
+ final Provider provider = getSecurityProviderIfAccessible ();
617
648
if ( provider != null ) {
618
649
return getSSLContext (protocol , provider );
619
650
}
0 commit comments