Skip to content

Commit b1bac76

Browse files
committed
be less fatal on Java 9 -> will still require a lot of refactoring
... won't attempt reflective SPIs when accessibility checks fail!
1 parent 7e872b7 commit b1bac76

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/main/java/org/jruby/ext/openssl/OpenSSL.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ static boolean javaVersion8(final boolean atLeast) {
268268
return atLeast ? gt <= 0 : gt == 0;
269269
}
270270

271+
static boolean javaVersion9(final boolean atLeast) {
272+
final int gt = "9".compareTo( javaVersion("0").substring(0, 1) );
273+
return atLeast ? gt <= 0 : gt == 0;
274+
}
275+
271276
private static String javaName(final String def) {
272277
// Sun Java 6 or Oracle Java 7/8
273278
// "Java HotSpot(TM) Server VM" or "Java HotSpot(TM) 64-Bit Server VM"

src/main/java/org/jruby/ext/openssl/SecurityHelper.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,38 @@ public static Provider getSecurityProvider() {
139139
return securityProvider;
140140
}
141141

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+
142172
public static synchronized void setSecurityProvider(final Provider provider) {
173+
if ( provider != null ) OpenSSL.debug("using provider: " + provider);
143174
securityProvider = provider;
144175
}
145176

@@ -165,7 +196,7 @@ static boolean isProviderAvailable(final String name) {
165196
return Security.getProvider(name) != null;
166197
}
167198

168-
static boolean isProviderRegistered() {
199+
public static boolean isProviderRegistered() {
169200
if ( securityProvider == null ) return false;
170201
return Security.getProvider(securityProvider.getName()) != null;
171202
}
@@ -190,7 +221,7 @@ private static void doRegisterProvider() {
190221
public static CertificateFactory getCertificateFactory(final String type)
191222
throws CertificateException {
192223
try {
193-
final Provider provider = getSecurityProvider();
224+
final Provider provider = getSecurityProviderIfAccessible();
194225
if ( provider != null ) return getCertificateFactory(type, provider);
195226
}
196227
catch (CertificateException e) { debugStackTrace(e); }
@@ -227,7 +258,7 @@ static CertificateFactory getCertificateFactory(final String type, final Provide
227258
public static KeyFactory getKeyFactory(final String algorithm)
228259
throws NoSuchAlgorithmException {
229260
try {
230-
final Provider provider = getSecurityProvider();
261+
final Provider provider = getSecurityProviderIfAccessible();
231262
if ( provider != null ) return getKeyFactory(algorithm, provider);
232263
}
233264
catch (NoSuchAlgorithmException e) { }
@@ -250,7 +281,7 @@ static KeyFactory getKeyFactory(final String algorithm, final Provider provider)
250281
public static KeyPairGenerator getKeyPairGenerator(final String algorithm)
251282
throws NoSuchAlgorithmException {
252283
try {
253-
final Provider provider = getSecurityProvider();
284+
final Provider provider = getSecurityProviderIfAccessible();
254285
if ( provider != null ) return getKeyPairGenerator(algorithm, provider);
255286
}
256287
catch (NoSuchAlgorithmException e) { }
@@ -290,7 +321,7 @@ static KeyPairGenerator getKeyPairGenerator(final String algorithm, final Provid
290321
public static KeyStore getKeyStore(final String type)
291322
throws KeyStoreException {
292323
try {
293-
final Provider provider = getSecurityProvider();
324+
final Provider provider = getSecurityProviderIfAccessible();
294325
if ( provider != null ) return getKeyStore(type, provider);
295326
}
296327
catch (KeyStoreException e) { }
@@ -307,7 +338,7 @@ static KeyStore getKeyStore(final String type, final Provider provider)
307338
*/
308339
public static MessageDigest getMessageDigest(final String algorithm) throws NoSuchAlgorithmException {
309340
try {
310-
final Provider provider = getSecurityProvider();
341+
final Provider provider = getSecurityProviderIfAccessible();
311342
if ( provider != null ) return getMessageDigest(algorithm, provider);
312343
}
313344
catch (NoSuchAlgorithmException e) { }
@@ -341,7 +372,7 @@ static MessageDigest getMessageDigest(final String algorithm, final Provider pro
341372

342373
public static SecureRandom getSecureRandom() {
343374
try {
344-
final Provider provider = getSecurityProvider();
375+
final Provider provider = getSecurityProviderIfAccessible();
345376
if ( provider != null ) {
346377
final String algorithm = getSecureRandomAlgorithm(provider);
347378
if ( algorithm != null ) {
@@ -473,7 +504,7 @@ private static Cipher getCipherInternal(String transformation, final Provider pr
473504
*/
474505
public static Signature getSignature(final String algorithm) throws NoSuchAlgorithmException {
475506
try {
476-
final Provider provider = getSecurityProvider();
507+
final Provider provider = getSecurityProviderIfAccessible();
477508
if ( provider != null ) return getSignature(algorithm, provider);
478509
}
479510
catch (NoSuchAlgorithmException e) { }
@@ -509,7 +540,7 @@ static Signature getSignature(final String algorithm, final Provider provider)
509540
*/
510541
public static Mac getMac(final String algorithm) throws NoSuchAlgorithmException {
511542
Mac mac = null;
512-
final Provider provider = getSecurityProvider();
543+
final Provider provider = getSecurityProviderIfAccessible();
513544
if ( provider != null ) {
514545
mac = getMac(algorithm, provider, true);
515546
}
@@ -540,7 +571,7 @@ private static Mac getMac(final String algorithm, final Provider provider, boole
540571
*/
541572
public static KeyGenerator getKeyGenerator(final String algorithm) throws NoSuchAlgorithmException {
542573
try {
543-
final Provider provider = getSecurityProvider();
574+
final Provider provider = getSecurityProviderIfAccessible();
544575
if ( provider != null ) return getKeyGenerator(algorithm, provider);
545576
}
546577
catch (NoSuchAlgorithmException e) { }
@@ -564,7 +595,7 @@ static KeyGenerator getKeyGenerator(final String algorithm, final Provider provi
564595
*/
565596
public static KeyAgreement getKeyAgreement(final String algorithm) throws NoSuchAlgorithmException {
566597
try {
567-
final Provider provider = getSecurityProvider();
598+
final Provider provider = getSecurityProviderIfAccessible();
568599
if ( provider != null ) return getKeyAgreement(algorithm, provider);
569600
}
570601
catch (NoSuchAlgorithmException e) { }
@@ -588,7 +619,7 @@ static KeyAgreement getKeyAgreement(final String algorithm, final Provider provi
588619
*/
589620
public static SecretKeyFactory getSecretKeyFactory(final String algorithm) throws NoSuchAlgorithmException {
590621
try {
591-
final Provider provider = getSecurityProvider();
622+
final Provider provider = getSecurityProviderIfAccessible();
592623
if ( provider != null ) return getSecretKeyFactory(algorithm, provider);
593624
}
594625
catch (NoSuchAlgorithmException e) { }
@@ -613,7 +644,7 @@ public static SSLContext getSSLContext(final String protocol)
613644
throws NoSuchAlgorithmException {
614645
try {
615646
if ( providerSSLContext ) {
616-
final Provider provider = getSecurityProvider();
647+
final Provider provider = getSecurityProviderIfAccessible();
617648
if ( provider != null ) {
618649
return getSSLContext(protocol, provider);
619650
}

0 commit comments

Comments
 (0)