Skip to content

Commit 1d2d056

Browse files
committed
handle SecurityException from File().exists() on paths initialization
1 parent ea6815f commit 1d2d056

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

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

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,45 +294,59 @@ else if ( keyUsage != null && ! keyUsage[5] ) { // KU_KEY_CERT_SIGN
294294

295295
static {
296296
// 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-
297+
// and falling back to trust store from java to be on the save side
298+
299299
// TODO usability in limited environments should be tested/reviewed
300300
final String JAVA_HOME = SafePropertyAccessor.getProperty("java.home", "");
301301

302302
// if the default files/dirs exist we use them. with this a switch
303303
// from MRI to JRuby produces the same results. otherwise we use the
304304
// certs from JAVA_HOME.
305-
final String MAYBE_CERT_FILE;
306305
final String LINUX_CERT_AREA = "/etc/ssl";
307306
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";
307+
308+
String certArea, certDir, privateDir;
309+
String maybeCertFile;
310+
String maybePkiCertFile = "/etc/pki/tls/certs/ca-bundle.crt";
311+
try {
312+
if (new File(LINUX_CERT_AREA).exists()) {
313+
certArea = LINUX_CERT_AREA;
314+
certDir = certArea + "/certs";
315+
privateDir = certArea + "/private";
316+
maybeCertFile = certDir + "/cert.pem";
317+
}
318+
else if (new File(MACOS_CERT_AREA).exists()) {
319+
certArea = MACOS_CERT_AREA;
320+
certDir = certArea + "/certs";
321+
privateDir = certArea + "/private";
322+
maybeCertFile = certArea + "/cert.pem";
323+
}
324+
else {
325+
certArea = JAVA_HOME + "/lib/security";
326+
certDir = certArea;
327+
privateDir = certArea;
328+
maybeCertFile = maybePkiCertFile;
329+
}
320330
}
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;
331+
catch (SecurityException e) {
332+
maybeCertFile = null; maybePkiCertFile = null;
333+
privateDir = certDir = certArea = JAVA_HOME + "/lib/security";
326334
}
327-
if (new File(MAYBE_PKI_CERT_FILE).exists()) {
328-
X509_CERT_FILE = MAYBE_PKI_CERT_FILE;
335+
336+
X509_CERT_AREA = certArea;
337+
X509_CERT_DIR = certDir;
338+
X509_PRIVATE_DIR = privateDir;
339+
340+
if (maybePkiCertFile != null && new File(maybePkiCertFile).exists()) {
341+
X509_CERT_FILE = maybePkiCertFile;
329342
}
330-
else if (new File(MAYBE_CERT_FILE).exists()) {
331-
X509_CERT_FILE = MAYBE_CERT_FILE;
343+
else if (maybeCertFile != null && new File(maybeCertFile).exists()) {
344+
X509_CERT_FILE = maybeCertFile;
332345
}
333346
else {
334347
X509_CERT_FILE = JAVA_HOME + "/lib/security/cacerts";
335348
}
349+
336350
// keep it with some meaninful content as it is a public constant
337351
OPENSSLDIR = X509_CERT_AREA;
338352
}

0 commit comments

Comments
 (0)