Skip to content

Commit 4facc96

Browse files
committed
avoid the rest of Ruby.getGlobalRuntime usages - only worked in 1 runtime envs
- also do not catch Errors - we retrieve the ruby ENV gently - addCertificateDirectory(lookupData, null, ...) ends up in NPE - thus use default cert dir not sponsored by Lookout inc.
1 parent aa51d9b commit 4facc96

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ private RubyArray matchedCiphers(final ThreadContext context) {
417417
final Ruby runtime = context.runtime;
418418
try {
419419
final String[] supported = getSupportedCipherSuites(this.protocol);
420-
@SuppressWarnings("unchecked")
421420
final Collection<CipherStrings.Def> cipherDefs =
422421
CipherStrings.matchingCiphers(this.ciphers, supported, false);
423422

src/main/java/org/jruby/ext/openssl/x509store/Lookup.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package org.jruby.ext.openssl.x509store;
2929

3030
import static org.jruby.ext.openssl.x509store.X509Utils.CRYPTO_LOCK_X509_STORE;
31+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_CERT_DIR;
3132
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_ASN1;
3233
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_DEFAULT;
3334
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_PEM;
@@ -78,8 +79,10 @@ public class Lookup {
7879

7980
boolean init = false;
8081
boolean skip = false;
82+
8183
final LookupMethod method;
82-
final Ruby runtime;
84+
private final Ruby runtime;
85+
8386
Object methodData;
8487
Store store;
8588

@@ -296,29 +299,29 @@ public int loadDefaultJavaCACertsFile() throws Exception {
296299
private InputStream wrapJRubyNormalizedInputStream(String file) throws IOException {
297300
try {
298301
FileResource resource = JRubyFile.createResource(runtime, file);
299-
if(!resource.exists()) {
302+
if ( ! resource.exists() ) {
300303
throw new FileNotFoundException(file + " (No such file or directory)");
301304
}
302-
if(resource.isDirectory()) {
305+
if ( resource.isDirectory() ) {
303306
throw new IOException(file + " is a directory");
304307
}
305308
InputStream is = resource.openInputStream();
306-
if (is instanceof BufferedInputStream) {
307-
return is;
308-
}
309-
else {
310-
return new BufferedInputStream(is);
311-
}
309+
return ( is instanceof BufferedInputStream ) ? is : new BufferedInputStream(is);
312310
}
313-
catch(NoSuchMethodError e){
311+
catch (NoSuchMethodError e) { // JRubyFile.createResource (JRuby < 1.7.13)
314312
File f = new File(file);
315-
if(!f.isAbsolute()) {
313+
if ( ! f.isAbsolute() ) {
316314
f = new File(runtime.getCurrentDirectory(), file);
317315
}
318316
return new BufferedInputStream(new FileInputStream(f));
319317
}
320318
}
321319

320+
private String envEntry(final String key) {
321+
RubyHash env = (RubyHash) runtime.getObject().getConstant("ENV");
322+
return (String) env.get( runtime.newString(key) );
323+
}
324+
322325
/**
323326
* c: X509_LOOKUP_free
324327
*/
@@ -426,10 +429,10 @@ public int call(final Lookup ctx, final Integer cmd, final String argp, final Nu
426429
case X509_L_FILE_LOAD:
427430
if (arglInt == X509_FILETYPE_DEFAULT) {
428431
try {
429-
RubyHash env = (RubyHash)Ruby.getGlobalRuntime().getObject().getConstant("ENV");
430-
file = (String)env.get(Ruby.getGlobalRuntime().newString(getDefaultCertificateFileEnvironment()));
431-
} catch (Error error) {
432+
file = ctx.envEntry( getDefaultCertificateFileEnvironment() );
432433
}
434+
catch (RuntimeException e) { }
435+
433436
if (file != null) {
434437
ok = ctx.loadCertificateOrCRLFile(file, X509_FILETYPE_PEM) != 0 ? 1 : 0;
435438
} else {
@@ -499,30 +502,29 @@ public int call(final Lookup ctx, final Integer cmd, String argp, Number argl, S
499502
if ( argl.intValue() == X509_FILETYPE_DEFAULT ) {
500503
String certDir = null;
501504
try {
502-
certDir = getDefaultCertificateDirectory();
505+
certDir = getDefaultCertificateDirectory(ctx);
503506
}
504507
catch (RuntimeException e) { }
505508

506509
if ( certDir != null ) {
507510
ret = addCertificateDirectory(lookupData, certDir, X509_FILETYPE_PEM);
508511
} else {
509-
ret = addCertificateDirectory(lookupData, getDefaultCertificateDirectory(), X509_FILETYPE_PEM);
512+
ret = addCertificateDirectory(lookupData, X509_CERT_DIR, X509_FILETYPE_PEM);
510513
}
511514
if ( ret == 0 ) {
512515
X509Error.addError(X509_R_LOADING_CERT_DIR);
513516
}
514517
}
515518
else {
516-
ret = addCertificateDirectory(lookupData,argp, argl.intValue());
519+
ret = addCertificateDirectory(lookupData, argp, argl.intValue());
517520
}
518521
break;
519522
}
520523
return ret;
521524
}
522525

523-
private static String getDefaultCertificateDirectory() {
524-
final RubyHash env = Ruby.getGlobalRuntime().getENV();
525-
return (String) env.get( getDefaultCertificateDirectoryEnvironment() );
526+
private static String getDefaultCertificateDirectory(final Lookup ctx) {
527+
return ctx.envEntry( getDefaultCertificateDirectoryEnvironment() );
526528
}
527529

528530
/**

0 commit comments

Comments
 (0)