Skip to content

Commit aa51d9b

Browse files
committed
use JRubyFile to get input-stream to file-resource fixes #11
this is just a mix of the old code, the new code and what the ChannelDescriptor of jruby-1.7.x is doing. but works only for jruby-1.7.14 or newer with a fallback to new code. Sponsored by Lookout Inc.
1 parent c6479d0 commit aa51d9b

File tree

8 files changed

+92
-42
lines changed

8 files changed

+92
-42
lines changed

Mavenfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ end
7878

7979
# NOTE: unfortunately we can not use 1.6.8 to generate invokers ...
8080
# although we'd like to compile against 1.6 to make sure all is well
81-
jar 'org.jruby:jruby-core', '1.7.10', :scope => :provided # 1.6.8
81+
jar 'org.jruby:jruby-core', '1.7.16', :scope => :provided # 1.6.8
8282
jar 'junit:junit', '4.11', :scope => :test
8383

8484
jruby_plugin! :gem do

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<dependency>
7474
<groupId>org.jruby</groupId>
7575
<artifactId>jruby-core</artifactId>
76-
<version>1.7.10</version>
76+
<version>1.7.16</version>
7777
<scope>provided</scope>
7878
</dependency>
7979
<dependency>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public IRubyObject setup(final ThreadContext context) {
336336
String caPath = getCaPath();
337337
if (caFile != null || caPath != null) {
338338
try {
339-
if (internalContext.store.loadLocations(caFile, caPath) == 0) {
339+
if (internalContext.store.loadLocations(runtime, caFile, caPath) == 0) {
340340
runtime.getWarnings().warn(ID.MISCELLANEOUS, "can't set verify locations");
341341
}
342342
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
***** END LICENSE BLOCK *****/
2828
package org.jruby.ext.openssl;
2929

30+
import static org.jruby.ext.openssl.OpenSSL.debugStackTrace;
31+
import static org.jruby.ext.openssl.OpenSSL.warn;
32+
import static org.jruby.ext.openssl.X509._X509;
33+
3034
import org.jruby.Ruby;
3135
import org.jruby.RubyClass;
3236
import org.jruby.RubyFixnum;
@@ -35,22 +39,17 @@
3539
import org.jruby.RubyObject;
3640
import org.jruby.anno.JRubyMethod;
3741
import org.jruby.exceptions.RaiseException;
38-
import org.jruby.ext.openssl.x509store.X509AuxCertificate;
3942
import org.jruby.ext.openssl.x509store.Store;
4043
import org.jruby.ext.openssl.x509store.StoreContext;
44+
import org.jruby.ext.openssl.x509store.X509AuxCertificate;
45+
import org.jruby.ext.openssl.x509store.X509Error;
4146
import org.jruby.ext.openssl.x509store.X509Utils;
4247
import org.jruby.runtime.Arity;
4348
import org.jruby.runtime.Block;
4449
import org.jruby.runtime.ObjectAllocator;
4550
import org.jruby.runtime.ThreadContext;
46-
import org.jruby.runtime.builtin.IRubyObject;
4751
import org.jruby.runtime.Visibility;
48-
49-
import static org.jruby.ext.openssl.OpenSSL.debugStackTrace;
50-
import static org.jruby.ext.openssl.OpenSSL.isDebug;
51-
import static org.jruby.ext.openssl.OpenSSL.warn;
52-
import static org.jruby.ext.openssl.X509._X509;
53-
import org.jruby.ext.openssl.x509store.X509Error;
52+
import org.jruby.runtime.builtin.IRubyObject;
5453

5554
/**
5655
* @author <a href="mailto:[email protected]">Ola Bini</a>
@@ -164,11 +163,11 @@ public IRubyObject add_path(final ThreadContext context, final IRubyObject arg)
164163
@JRubyMethod
165164
public IRubyObject add_file(final IRubyObject arg) {
166165
String file = arg.toString();
166+
final Ruby runtime = getRuntime();
167167
try {
168-
store.loadLocations(file, null);
168+
store.loadLocations(runtime, file, null);
169169
}
170170
catch (Exception e) {
171-
final Ruby runtime = getRuntime();
172171
debugStackTrace(runtime, e);
173172
throw newStoreError(runtime, "loading file failed: ", e);
174173
}
@@ -179,7 +178,7 @@ public IRubyObject add_file(final IRubyObject arg) {
179178
public IRubyObject set_default_paths(final ThreadContext context) {
180179
final Ruby runtime = context.runtime;
181180
try {
182-
store.setDefaultPaths();
181+
store.setDefaultPaths(runtime);
183182
}
184183
catch (Exception e) {
185184
debugStackTrace(runtime, e);

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

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,47 @@
2727
***** END LICENSE BLOCK *****/
2828
package org.jruby.ext.openssl.x509store;
2929

30+
import static org.jruby.ext.openssl.x509store.X509Utils.CRYPTO_LOCK_X509_STORE;
31+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_ASN1;
32+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_DEFAULT;
33+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_PEM;
34+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_LU_CRL;
35+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_LU_FAIL;
36+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_LU_X509;
37+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_L_ADD_DIR;
38+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_L_FILE_LOAD;
39+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_R_BAD_X509_FILETYPE;
40+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_R_INVALID_DIRECTORY;
41+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_R_LOADING_CERT_DIR;
42+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_R_LOADING_DEFAULTS;
43+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_R_WRONG_LOOKUP_TYPE;
44+
import static org.jruby.ext.openssl.x509store.X509Utils.getDefaultCertificateDirectoryEnvironment;
45+
import static org.jruby.ext.openssl.x509store.X509Utils.getDefaultCertificateFileEnvironment;
46+
3047
import java.io.BufferedInputStream;
48+
import java.io.BufferedReader;
3149
import java.io.File;
50+
import java.io.FileInputStream;
51+
import java.io.FileNotFoundException;
3252
import java.io.IOException;
33-
import java.io.Reader;
3453
import java.io.InputStream;
35-
import java.io.BufferedReader;
36-
import java.io.FileInputStream;
3754
import java.io.InputStreamReader;
38-
55+
import java.io.Reader;
3956
import java.math.BigInteger;
40-
4157
import java.security.KeyStore;
58+
import java.security.cert.CRL;
4259
import java.security.cert.PKIXParameters;
4360
import java.security.cert.TrustAnchor;
4461
import java.security.cert.X509Certificate;
45-
import java.security.cert.CRL;
46-
4762
import java.util.ArrayList;
4863
import java.util.Collection;
4964
import java.util.Iterator;
5065

5166
import org.jruby.Ruby;
5267
import org.jruby.RubyHash;
53-
5468
import org.jruby.ext.openssl.SecurityHelper;
55-
56-
import static org.jruby.ext.openssl.x509store.X509Utils.*;
69+
import org.jruby.util.FileResource;
70+
import org.jruby.util.JRubyFile;
5771

5872
/**
5973
* X509_LOOKUP
@@ -65,17 +79,19 @@ public class Lookup {
6579
boolean init = false;
6680
boolean skip = false;
6781
final LookupMethod method;
82+
final Ruby runtime;
6883
Object methodData;
6984
Store store;
7085

7186
/**
7287
* c: X509_LOOKUP_new
7388
*/
74-
public Lookup(LookupMethod method) {
89+
public Lookup(Ruby runtime, LookupMethod method) {
7590
if ( method == null ) {
7691
throw new IllegalArgumentException("null method");
7792
}
7893
this.method = method;
94+
this.runtime = runtime;
7995

8096
final LookupMethod.NewItemFunction newItem = method.newItem;
8197
if ( newItem != null && newItem != Function1.EMPTY ) {
@@ -278,7 +294,29 @@ public int loadDefaultJavaCACertsFile() throws Exception {
278294
}
279295

280296
private InputStream wrapJRubyNormalizedInputStream(String file) throws IOException {
281-
return new BufferedInputStream(new FileInputStream(file));
297+
try {
298+
FileResource resource = JRubyFile.createResource(runtime, file);
299+
if(!resource.exists()) {
300+
throw new FileNotFoundException(file + " (No such file or directory)");
301+
}
302+
if(resource.isDirectory()) {
303+
throw new IOException(file + " is a directory");
304+
}
305+
InputStream is = resource.openInputStream();
306+
if (is instanceof BufferedInputStream) {
307+
return is;
308+
}
309+
else {
310+
return new BufferedInputStream(is);
311+
}
312+
}
313+
catch(NoSuchMethodError e){
314+
File f = new File(file);
315+
if(!f.isAbsolute()) {
316+
f = new File(runtime.getCurrentDirectory(), file);
317+
}
318+
return new BufferedInputStream(new FileInputStream(f));
319+
}
282320
}
283321

284322
/**

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@
2727
***** END LICENSE BLOCK *****/
2828
package org.jruby.ext.openssl.x509store;
2929

30+
import static org.jruby.ext.openssl.x509store.X509Utils.CRYPTO_LOCK_X509_STORE;
31+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_DEFAULT;
32+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_FILETYPE_PEM;
33+
import static org.jruby.ext.openssl.x509store.X509Utils.X509_R_CERT_ALREADY_IN_HASH_TABLE;
34+
3035
import java.io.FileNotFoundException;
3136
import java.security.cert.X509Certificate;
3237
import java.util.ArrayList;
3338
import java.util.List;
39+
3440
import javax.net.ssl.X509TrustManager;
3541

36-
import static org.jruby.ext.openssl.x509store.X509Utils.*;
42+
import org.jruby.Ruby;
3743

3844
/**
3945
* c: X509_STORE
@@ -244,11 +250,11 @@ public int setParam(VerifyParameter pm) {
244250
/**
245251
* c: X509_STORE_add_lookup
246252
*/
247-
public Lookup addLookup(final LookupMethod method) throws Exception {
253+
public Lookup addLookup(Ruby runtime, final LookupMethod method) throws Exception {
248254
for ( Lookup lookup : certificateMethods ) {
249255
if ( lookup.equals(method) ) return lookup;
250256
}
251-
Lookup lookup = new Lookup(method);
257+
Lookup lookup = new Lookup(runtime, method);
252258
lookup.store = this;
253259
certificateMethods.add(lookup);
254260
return lookup;
@@ -300,9 +306,9 @@ public int addCRL(final java.security.cert.CRL crl) {
300306
/**
301307
* c: X509_STORE_load_locations
302308
*/
303-
public int loadLocations(String file, String path) throws Exception {
309+
public int loadLocations(Ruby runtime, String file, String path) throws Exception {
304310
if ( file != null ) {
305-
final Lookup lookup = addLookup( Lookup.fileLookup() );
311+
final Lookup lookup = addLookup( runtime, Lookup.fileLookup() );
306312
if ( lookup == null ) {
307313
return 0;
308314
}
@@ -312,7 +318,7 @@ public int loadLocations(String file, String path) throws Exception {
312318
}
313319

314320
if ( path != null ) {
315-
final Lookup lookup = addLookup( Lookup.hashDirLookup() );
321+
final Lookup lookup = addLookup( runtime, Lookup.hashDirLookup() );
316322
if ( lookup == null ) {
317323
return 0;
318324
}
@@ -328,9 +334,9 @@ public int loadLocations(String file, String path) throws Exception {
328334
/**
329335
* c: X509_STORE_set_default_paths
330336
*/
331-
public int setDefaultPaths() throws Exception {
337+
public int setDefaultPaths(Ruby runtime) throws Exception {
332338

333-
Lookup lookup = addLookup(Lookup.fileLookup());
339+
Lookup lookup = addLookup(runtime, Lookup.fileLookup());
334340
//if ( lookup == null ) return 0;
335341

336342
try {
@@ -340,7 +346,7 @@ public int setDefaultPaths() throws Exception {
340346
// set_default_paths ignores FileNotFound
341347
}
342348

343-
lookup = addLookup(Lookup.hashDirLookup());
349+
lookup = addLookup(runtime, Lookup.hashDirLookup());
344350
//if ( lookup == null ) return 0;
345351

346352
try {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@
3232
import java.security.cert.X509CRL;
3333
import java.security.cert.X509Certificate;
3434
import java.security.cert.X509Extension;
35-
35+
import java.util.ArrayList;
3636
import java.util.Calendar;
3737
import java.util.Collection;
3838
import java.util.Date;
39-
import java.util.ArrayList;
39+
import java.util.HashSet;
4040
import java.util.List;
4141
import java.util.Set;
42-
import java.util.HashSet;
4342

4443
import org.bouncycastle.asn1.ASN1InputStream;
4544
import org.bouncycastle.asn1.ASN1Integer;
4645
import org.bouncycastle.asn1.ASN1Sequence;
46+
import org.jruby.Ruby;
4747
import org.jruby.ext.openssl.SecurityHelper;
4848

4949
/**
@@ -448,7 +448,7 @@ private void resetSettingsToWithoutStore() {
448448
/**
449449
* c: SSL_CTX_load_verify_locations
450450
*/
451-
public int loadVerifyLocations(String CAfile, String CApath) {
451+
public int loadVerifyLocations(Ruby runtime, String CAfile, String CApath) {
452452
boolean reset = false;
453453
try {
454454
if ( store == null ) {
@@ -483,7 +483,7 @@ public int loadVerifyLocations(String CAfile, String CApath) {
483483
}
484484
}
485485

486-
final int ret = store.loadLocations(CAfile, CApath);
486+
final int ret = store.loadLocations(runtime, CAfile, CApath);
487487
if ( ret == 0 && reset ) resetSettingsToWithoutStore();
488488

489489
return ret;

src/test/java/org/jruby/ext/openssl/SecurityHelperTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11

22
package org.jruby.ext.openssl;
33

4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.Assert.assertNull;
7+
import static org.junit.Assert.assertSame;
8+
import static org.junit.Assert.fail;
9+
410
import java.security.KeyStoreException;
511
import java.security.NoSuchAlgorithmException;
612
import java.security.Provider;
713
import java.security.cert.CertificateException;
814

9-
import org.junit.*;
10-
import static org.junit.Assert.*;
15+
import org.junit.After;
16+
import org.junit.Before;
17+
import org.junit.Test;
1118

1219
/**
1320
* @author kares

0 commit comments

Comments
 (0)