avoid NPE with jdk 7u4ea8 changed behaviour of SslContext init

This commit is contained in:
Greg Wilkins 2012-01-23 16:58:29 +11:00
parent ae5ddb2930
commit 067e3f17cb
7 changed files with 34 additions and 98 deletions

View File

@ -188,20 +188,6 @@ public class HttpServerTestFixture
}
}
// Create a trust manager that does not validate certificate chains
public final static TrustManager[] __trustAllCerts = new TrustManager[] {
new X509TrustManager(){
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
public final static HostnameVerifier __hostnameverifier = new HostnameVerifier()
{

View File

@ -20,7 +20,8 @@ package org.eclipse.jetty.server.ssl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import java.io.BufferedReader;
import java.io.IOException;
@ -37,8 +38,6 @@ import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
@ -81,25 +80,6 @@ public class SSLEngineTest
private static final int BODY_SIZE=300;
private static final TrustManager[] s_dummyTrustManagers=new TrustManager[]
{
new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
}
};
private static Server server;
private static SslSelectChannelConnector connector;
@ -134,7 +114,7 @@ public class SSLEngineTest
public void testBigResponse() throws Exception
{
SSLContext ctx=SSLContext.getInstance("TLS");
ctx.init(null,s_dummyTrustManagers,new java.security.SecureRandom());
ctx.init(null,SslContextFactory.TRUST_ALL_CERTS,new java.security.SecureRandom());
int port=connector.getLocalPort();
@ -152,7 +132,7 @@ public class SSLEngineTest
String response = IO.toString(client.getInputStream());
assertTrue(response.length()>102400);
assertThat(response.length(),greaterThan(102400));
}
@Test
@ -164,7 +144,7 @@ public class SSLEngineTest
Socket[] client=new Socket[numConns];
SSLContext ctx=SSLContext.getInstance("SSLv3");
ctx.init(null,s_dummyTrustManagers,new java.security.SecureRandom());
ctx.init(null,SslContextFactory.TRUST_ALL_CERTS,new java.security.SecureRandom());
int port=connector.getLocalPort();
@ -231,7 +211,7 @@ public class SSLEngineTest
server.start();
SSLContext context = SSLContext.getInstance("SSL");
context.init(null,s_dummyTrustManagers,new java.security.SecureRandom());
context.init(null,SslContextFactory.TRUST_ALL_CERTS,new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
URL url = new URL("https://localhost:"+connector.getLocalPort()+"/test");

View File

@ -73,7 +73,7 @@ public class SelectChannelServerSslTest extends HttpServerTestBase
{
HttpsURLConnection.setDefaultHostnameVerifier(__hostnameverifier);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, __trustAllCerts, new java.security.SecureRandom());
sc.init(null, SslContextFactory.TRUST_ALL_CERTS, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
catch(Exception e)

View File

@ -37,22 +37,6 @@ public class SslRenegotiateTest
{
private static final Logger LOG = Log.getLogger(SslRenegotiateTest.class);
private static final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType )
{
}
public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType )
{
}
} };
private ByteBuffer _outAppB;
private ByteBuffer _outPacketB;
private ByteBuffer _inAppB;
@ -110,7 +94,7 @@ public class SslRenegotiateTest
_socket.configureBlocking(true);
SSLContext context=SSLContext.getInstance("SSL");
context.init( null, trustAllCerts, new java.security.SecureRandom() );
context.init( null, SslContextFactory.TRUST_ALL_CERTS, new java.security.SecureRandom() );
_engine = context.createSSLEngine();
_engine.setUseClientMode(true);

View File

@ -37,7 +37,7 @@ public class StdErrLog extends AbstractLogger
{
private static final String EOL = System.getProperty("line.separator");
private static DateCache _dateCache;
private static Properties __props = Log.__props;
private static final Properties __props = new Properties();
private final static boolean __source = Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.SOURCE",
Log.__props.getProperty("org.eclipse.jetty.util.log.stderr.SOURCE","false")));
@ -45,6 +45,8 @@ public class StdErrLog extends AbstractLogger
static
{
__props.putAll(Log.__props);
String deprecatedProperties[] =
{ "DEBUG", "org.eclipse.jetty.util.log.DEBUG", "org.eclipse.jetty.util.log.stderr.DEBUG" };
@ -97,7 +99,8 @@ public class StdErrLog extends AbstractLogger
public StdErrLog(String name, Properties props)
{
__props = props;
if (props!=null)
__props.putAll(props);
this._name = name == null?"":name;
this._abbrevname = condensePackageString(this._name);
this._level = getLoggingLevel(props,this._name);
@ -603,7 +606,8 @@ public class StdErrLog extends AbstractLogger
public static void setProperties(Properties props)
{
__props = props;
__props.clear();
__props.putAll(props);
}
public void ignore(Throwable ignored)

View File

@ -71,6 +71,22 @@ import org.eclipse.jetty.util.security.Password;
*/
public class SslContextFactory extends AbstractLifeCycle
{
public final static TrustManager[] TRUST_ALL_CERTS = new X509TrustManager[]{new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return new java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
}};
private static final Logger LOG = Log.getLogger(SslContextFactory.class);
public static final String DEFAULT_KEYMANAGERFACTORY_ALGORITHM =
@ -229,22 +245,7 @@ public class SslContextFactory extends AbstractLifeCycle
{
LOG.debug("No keystore or trust store configured. ACCEPTING UNTRUSTED CERTIFICATES!!!!!");
// Create a trust manager that does not validate certificate chains
TrustManager trustAllCerts = new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
};
trust_managers = new TrustManager[] { trustAllCerts };
trust_managers = TRUST_ALL_CERTS;
}
SecureRandom secureRandom = (_secureRandomAlgorithm == null)?null:SecureRandom.getInstance(_secureRandomAlgorithm);

View File

@ -32,6 +32,7 @@ import javax.net.ssl.X509TrustManager;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
/**
* An HTTPS Socket Impl
@ -45,26 +46,6 @@ public class HttpsSocketImpl implements HttpSocket
public HttpsSocketImpl() throws Exception
{
// Create loose SSL context.
// Create a trust manager that does not validate certificate
// chains
TrustManager[] trustAllCerts = new TrustManager[]
{ new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
} };
@SuppressWarnings("unused")
HostnameVerifier hostnameVerifier = new HostnameVerifier()
{
@ -80,7 +61,7 @@ public class HttpsSocketImpl implements HttpSocket
{
// TODO real trust manager
this.sslContext = SSLContext.getInstance("TLS");
sslContext.init(null,trustAllCerts,new java.security.SecureRandom());
sslContext.init(null,SslContextFactory.TRUST_ALL_CERTS,new java.security.SecureRandom());
}
catch (Exception e)
{