package testssl;
/**
* Copyright (C) 2013 Alexander Szczuczko
*
* This file may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* A X509TrustManager that trusts everyone. Use with caution, this exposes you to MITM attacks.
*/
public class TrustAllTrustManager
implements X509TrustManager
{
private static SSLSocketFactory trustAllSocketFactory;
@Override
public X509Certificate[] getAcceptedIssuers()
{
return new X509Certificate[0];
}
@Override
public void checkClientTrusted( X509Certificate[] cert, String authType )
throws CertificateException
{
}
@Override
public void checkServerTrusted( X509Certificate[] cert, String authType )
throws CertificateException
{
}
/**
* Get a SSLSocketFactory using this X509TrustManager.
* @return
*/
public static synchronized SSLSocketFactory getSSLSocketFactory()
{
if ( trustAllSocketFactory == null )
{
try
{
KeyManager[] km = new KeyManager[0];
TrustManager[] tm = new TrustManager[] { new TrustAllTrustManager() };
SSLContext context = SSLContext.getInstance( "SSL" );
context.init( km, tm, new SecureRandom() );
trustAllSocketFactory = (SSLSocketFactory) context.getSocketFactory();
}
catch ( KeyManagementException | NoSuchAlgorithmException e )
{
System.out.println("Error creating custom SSLSocketFactory");
e.printStackTrace(System.out);
}
}
return trustAllSocketFactory;
}
}