Skip to content

Commit a38ec5c

Browse files
committed
disable DHE (by default) on Java <= 7 ... on Java 8 we (still) force 1024/2048
... resolves jruby/jruby#2872 (also related to #45)
1 parent fec9cf7 commit a38ec5c

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ static void warn(final ThreadContext context, final IRubyObject msg) {
225225
if ( warn ) context.runtime.getModule("OpenSSL").callMethod(context, "warn", msg);
226226
}
227227

228+
private static String javaVersion(final String def) {
229+
return SafePropertyAccessor.getProperty("java.version", def);
230+
}
231+
232+
static boolean javaVersion7(final boolean atLeast) {
233+
final int gt = "1.7".compareTo( javaVersion("0.0").substring(0, 3) );
234+
return atLeast ? gt <= 0 : gt == 0;
235+
}
236+
237+
static boolean javaVersion8(final boolean atLeast) {
238+
final int gt = "1.8".compareTo( javaVersion("0.0").substring(0, 3) );
239+
return atLeast ? gt <= 0 : gt == 0;
240+
}
241+
228242
//
229243

230244
static IRubyObject to_der_if_possible(final ThreadContext context, IRubyObject obj) {

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,31 @@ public class SSL {
6868
static { configureJSSE(); }
6969

7070
private static void configureJSSE() {
71-
final String ephemeralDHKeySize = "jdk.tls.ephemeralDHKeySize";
72-
try {
73-
if ( System.getProperty(ephemeralDHKeySize) == null ) {
74-
// The key size is the same as the authentication certificate,
75-
// but must be between 1024 bits and 2048 bits, inclusively.
76-
// However, the SunJCE provider only supports 2048-bit DH keys larger
77-
// than 1024 bits. Consequently, you may use the values 1024 or 2048 only.
78-
System.setProperty(ephemeralDHKeySize, "matched"); // only affects Java 8
71+
if ( OpenSSL.javaVersion8(true) ) { // >= 1.8
72+
final String ephemeralDHKeySize = "jdk.tls.ephemeralDHKeySize";
73+
try {
74+
if ( System.getProperty(ephemeralDHKeySize) == null ) {
75+
// The key size is the same as the authentication certificate,
76+
// but must be between 1024 bits and 2048 bits, inclusively.
77+
// However, the SunJCE provider only supports 2048-bit DH keys larger
78+
// than 1024 bits. Consequently, you may use the values 1024 or 2048 only.
79+
System.setProperty(ephemeralDHKeySize, "matched"); // only on Java 8
80+
}
81+
}
82+
catch (SecurityException ex) {
83+
OpenSSL.debug("setting " + ephemeralDHKeySize + " failed: " + ex);
7984
}
8085
}
81-
catch (SecurityException ex) {
82-
OpenSSL.debug("setting " + ephemeralDHKeySize + " failed: " + ex);
86+
else { // on JDK 7 DHE is weak - disable completely (unless user-set)
87+
final String disabledAlgorithms = "jdk.tls.disabledAlgorithms";
88+
try {
89+
if ( System.getProperty(disabledAlgorithms) == null ) {
90+
System.setProperty(disabledAlgorithms, "SSLv3, DHE");
91+
}
92+
}
93+
catch (SecurityException se) {
94+
OpenSSL.debug("setting " + disabledAlgorithms + " failed: " + se);
95+
}
8396
}
8497
}
8598

0 commit comments

Comments
 (0)