|
28 | 28 | package org.jruby.ext.openssl.x509store;
|
29 | 29 |
|
30 | 30 | import static org.jruby.ext.openssl.x509store.X509Utils.CRYPTO_LOCK_X509_STORE;
|
| 31 | +import static org.jruby.ext.openssl.x509store.X509Utils.X509_CERT_DIR; |
31 | 32 | import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_ASN1;
|
32 | 33 | import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_DEFAULT;
|
33 | 34 | import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_PEM;
|
@@ -78,8 +79,10 @@ public class Lookup {
|
78 | 79 |
|
79 | 80 | boolean init = false;
|
80 | 81 | boolean skip = false;
|
| 82 | + |
81 | 83 | final LookupMethod method;
|
82 |
| - final Ruby runtime; |
| 84 | + private final Ruby runtime; |
| 85 | + |
83 | 86 | Object methodData;
|
84 | 87 | Store store;
|
85 | 88 |
|
@@ -296,29 +299,29 @@ public int loadDefaultJavaCACertsFile() throws Exception {
|
296 | 299 | private InputStream wrapJRubyNormalizedInputStream(String file) throws IOException {
|
297 | 300 | try {
|
298 | 301 | FileResource resource = JRubyFile.createResource(runtime, file);
|
299 |
| - if(!resource.exists()) { |
| 302 | + if ( ! resource.exists() ) { |
300 | 303 | throw new FileNotFoundException(file + " (No such file or directory)");
|
301 | 304 | }
|
302 |
| - if(resource.isDirectory()) { |
| 305 | + if ( resource.isDirectory() ) { |
303 | 306 | throw new IOException(file + " is a directory");
|
304 | 307 | }
|
305 | 308 | 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); |
312 | 310 | }
|
313 |
| - catch(NoSuchMethodError e){ |
| 311 | + catch (NoSuchMethodError e) { // JRubyFile.createResource (JRuby < 1.7.13) |
314 | 312 | File f = new File(file);
|
315 |
| - if(!f.isAbsolute()) { |
| 313 | + if ( ! f.isAbsolute() ) { |
316 | 314 | f = new File(runtime.getCurrentDirectory(), file);
|
317 | 315 | }
|
318 | 316 | return new BufferedInputStream(new FileInputStream(f));
|
319 | 317 | }
|
320 | 318 | }
|
321 | 319 |
|
| 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 | + |
322 | 325 | /**
|
323 | 326 | * c: X509_LOOKUP_free
|
324 | 327 | */
|
@@ -426,10 +429,10 @@ public int call(final Lookup ctx, final Integer cmd, final String argp, final Nu
|
426 | 429 | case X509_L_FILE_LOAD:
|
427 | 430 | if (arglInt == X509_FILETYPE_DEFAULT) {
|
428 | 431 | 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() ); |
432 | 433 | }
|
| 434 | + catch (RuntimeException e) { } |
| 435 | + |
433 | 436 | if (file != null) {
|
434 | 437 | ok = ctx.loadCertificateOrCRLFile(file, X509_FILETYPE_PEM) != 0 ? 1 : 0;
|
435 | 438 | } else {
|
@@ -499,30 +502,29 @@ public int call(final Lookup ctx, final Integer cmd, String argp, Number argl, S
|
499 | 502 | if ( argl.intValue() == X509_FILETYPE_DEFAULT ) {
|
500 | 503 | String certDir = null;
|
501 | 504 | try {
|
502 |
| - certDir = getDefaultCertificateDirectory(); |
| 505 | + certDir = getDefaultCertificateDirectory(ctx); |
503 | 506 | }
|
504 | 507 | catch (RuntimeException e) { }
|
505 | 508 |
|
506 | 509 | if ( certDir != null ) {
|
507 | 510 | ret = addCertificateDirectory(lookupData, certDir, X509_FILETYPE_PEM);
|
508 | 511 | } else {
|
509 |
| - ret = addCertificateDirectory(lookupData, getDefaultCertificateDirectory(), X509_FILETYPE_PEM); |
| 512 | + ret = addCertificateDirectory(lookupData, X509_CERT_DIR, X509_FILETYPE_PEM); |
510 | 513 | }
|
511 | 514 | if ( ret == 0 ) {
|
512 | 515 | X509Error.addError(X509_R_LOADING_CERT_DIR);
|
513 | 516 | }
|
514 | 517 | }
|
515 | 518 | else {
|
516 |
| - ret = addCertificateDirectory(lookupData,argp, argl.intValue()); |
| 519 | + ret = addCertificateDirectory(lookupData, argp, argl.intValue()); |
517 | 520 | }
|
518 | 521 | break;
|
519 | 522 | }
|
520 | 523 | return ret;
|
521 | 524 | }
|
522 | 525 |
|
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() ); |
526 | 528 | }
|
527 | 529 |
|
528 | 530 | /**
|
|
0 commit comments