Skip to content

Commit d717926

Browse files
committed
Merge pull request #61 from jruby/paths
keep trusted certificate stores in line with MRI if possible
2 parents a7d7aff + dfd8429 commit d717926

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

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

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

3030

31+
import java.io.File;
3132
import java.io.IOException;
3233
import java.math.BigInteger;
3334
import java.util.Arrays;
@@ -292,13 +293,48 @@ else if ( keyUsage != null && ! keyUsage[5] ) { // KU_KEY_CERT_SIGN
292293
public static final String X509_PRIVATE_DIR;
293294

294295
static {
295-
OPENSSLDIR = "/usr/local/openssl"; // NOTE: blindly follow?!
296+
// roughly following the ideas from https://www.happyassassin.net/2015/01/12/a-note-about-ssltls-trusted-certificate-stores-and-platforms/
297+
// and falling back to trust store from java to be on the save side
298+
296299
// TODO usability in limited environments should be tested/reviewed
297300
final String JAVA_HOME = SafePropertyAccessor.getProperty("java.home", "");
298-
X509_CERT_AREA = JAVA_HOME + "/lib/security";
299-
X509_CERT_DIR = X509_CERT_AREA;
300-
X509_CERT_FILE = X509_CERT_DIR + "/cacerts";
301-
X509_PRIVATE_DIR = "/usr/lib/ssl/private"; // NOTE: blindly follow?!
301+
302+
// if the default files/dirs exist we use them. with this a switch
303+
// from MRI to JRuby produces the same results. otherwise we use the
304+
// certs from JAVA_HOME.
305+
final String MAYBE_CERT_FILE;
306+
final String LINUX_CERT_AREA = "/etc/ssl";
307+
final String MACOS_CERT_AREA = "/System/Library/OpenSSL";
308+
final String MAYBE_PKI_CERT_FILE = "/etc/pki/tls/certs/ca-bundle.crt";
309+
if (new File(LINUX_CERT_AREA).exists()) {
310+
X509_CERT_AREA = LINUX_CERT_AREA;
311+
X509_CERT_DIR = X509_CERT_AREA + "/certs";
312+
X509_PRIVATE_DIR = X509_CERT_AREA + "/private";
313+
MAYBE_CERT_FILE = X509_CERT_DIR + "/cert.pem";
314+
}
315+
else if (new File(MACOS_CERT_AREA).exists()) {
316+
X509_CERT_AREA = MACOS_CERT_AREA;
317+
X509_CERT_DIR = X509_CERT_AREA + "/certs";
318+
X509_PRIVATE_DIR = X509_CERT_AREA + "/private";
319+
MAYBE_CERT_FILE = X509_CERT_AREA + "/cert.pem";
320+
}
321+
else {
322+
X509_CERT_AREA = JAVA_HOME + "/lib/security";
323+
X509_CERT_DIR = X509_CERT_AREA;
324+
X509_PRIVATE_DIR = X509_CERT_AREA;
325+
MAYBE_CERT_FILE = MAYBE_PKI_CERT_FILE;
326+
}
327+
if (new File(MAYBE_PKI_CERT_FILE).exists()) {
328+
X509_CERT_FILE = MAYBE_PKI_CERT_FILE;
329+
}
330+
else if (new File(MAYBE_CERT_FILE).exists()) {
331+
X509_CERT_FILE = MAYBE_CERT_FILE;
332+
}
333+
else {
334+
X509_CERT_FILE = JAVA_HOME + "/lib/security/cacerts";
335+
}
336+
// keep it with some meaninful content as it is a public constant
337+
OPENSSLDIR = X509_CERT_AREA;
302338
}
303339

304340
public static final String X509_CERT_DIR_EVP = "SSL_CERT_DIR";

0 commit comments

Comments
 (0)