FileDocCategorySizeDatePackage
MailSSLSocketFactory.javaAPI DocJavaMail 1.4.311346Tue Nov 17 10:38:12 GMT 2009com.sun.mail.util

MailSSLSocketFactory

public class MailSSLSocketFactory extends SSLSocketFactory
An SSL socket factory that makes it easier to specify trust. This socket factory can be configured to trust all hosts or or trust a specific set of hosts, in which case the server's certificate isn't verified. Alternatively, a custom TrustManager can be supplied. An instance of this factory can be set as the value of the mail.<protocol>.ssl.socketFactory property.
since
JavaMail 1.4.2
author
Stephan Sann
author
Bill Shannon

Fields Summary
private boolean
trustAllHosts
Should all hosts be trusted?
private String[]
trustedHosts
String-array of trusted hosts
private SSLContext
sslcontext
Holds a SSLContext to get SSLSocketFactories from
private KeyManager[]
keyManagers
Holds the KeyManager array to use
private TrustManager[]
trustManagers
Holds the TrustManager array to use
private SecureRandom
secureRandom
Holds the SecureRandom to use
private SSLSocketFactory
adapteeFactory
Holds a SSLSocketFactory to pass all API-method-calls to
Constructors Summary
public MailSSLSocketFactory()
Initializes a new MailSSLSocketFactory.

throws
GeneralSecurityException


                 
        
	this("TLS");
    
public MailSSLSocketFactory(String protocol)
Initializes a new MailSSLSocketFactory with a given protocol. Normally the protocol will be specified as "TLS".

param
protocol The protocol to use
throws
NoSuchAlgorithmException if given protocol is not supported


	// By default we do NOT trust all hosts.
	trustAllHosts = false;

	// Get an instance of an SSLContext.
	sslcontext = SSLContext.getInstance(protocol);

	// Default properties to init the SSLContext
	keyManagers = null;
	trustManagers = new TrustManager[] { new MailTrustManager() };
	secureRandom = null;

	// Assemble a default SSLSocketFactory to delegate all API-calls to.
	newAdapteeFactory();
    
Methods Summary
public synchronized java.net.SocketcreateSocket(java.net.Socket socket, java.lang.String s, int i, boolean flag)

	return adapteeFactory.createSocket(socket, s, i, flag);
    
public synchronized java.net.SocketcreateSocket()

	return adapteeFactory.createSocket();
    
public synchronized java.net.SocketcreateSocket(java.net.InetAddress inetaddress, int i, java.net.InetAddress inetaddress1, int j)

	return adapteeFactory.createSocket(inetaddress, i, inetaddress1, j);
    
public synchronized java.net.SocketcreateSocket(java.net.InetAddress inetaddress, int i)

	return adapteeFactory.createSocket(inetaddress, i);
    
public synchronized java.net.SocketcreateSocket(java.lang.String s, int i, java.net.InetAddress inetaddress, int j)

	return adapteeFactory.createSocket(s, i, inetaddress, j);
    
public synchronized java.net.SocketcreateSocket(java.lang.String s, int i)

	return adapteeFactory.createSocket(s, i);
    
public synchronized java.lang.String[]getDefaultCipherSuites()

	return adapteeFactory.getDefaultCipherSuites();
    
public synchronized javax.net.ssl.KeyManager[]getKeyManagers()

return
the keyManagers

	return (KeyManager[])keyManagers.clone();
    
public synchronized java.security.SecureRandomgetSecureRandom()

return
the secureRandom

	return secureRandom;
    
public synchronized java.lang.String[]getSupportedCipherSuites()

	return adapteeFactory.getSupportedCipherSuites();
    
public synchronized javax.net.ssl.TrustManager[]getTrustManagers()

return
the trustManagers

	return trustManagers;
    
public synchronized java.lang.String[]getTrustedHosts()

return
the trusted hosts

	return (String[])trustedHosts.clone();
    
public synchronized booleanisServerTrusted(java.lang.String server, javax.net.ssl.SSLSocket sslSocket)
After a successful conection to the server, this method is called to ensure that the server should be trusted.

param
server name of the server we connected to
param
sslSocket SSLSocket connected to the server
return
true if "trustAllHosts" is set to true OR the server is contained in the "trustedHosts" array;


	//System.out.println("DEBUG: isServerTrusted host " + server);

	// If "trustAllHosts" is set to true, we return true
	if (trustAllHosts)
	    return true;

	// If the socket host is contained in the "trustedHosts" array,
	// we return true
	if (trustedHosts != null)
	    return Arrays.asList(trustedHosts).contains(server); // ignore case?

	// If we get here, trust of the server was verified by the trust manager
	return true;
    
public synchronized booleanisTrustAllHosts()

return
true if all hosts should be trusted

	return trustAllHosts;
    
private synchronized voidnewAdapteeFactory()
Gets an SSLSocketFactory based on the given (or default) KeyManager array, TrustManager array and SecureRandom and sets it to the instance var adapteeFactory.

	sslcontext.init(keyManagers, trustManagers, secureRandom);

	// Get SocketFactory and save it in our instance var
	adapteeFactory = (SSLSocketFactory)sslcontext.getSocketFactory();
    
public synchronized voidsetKeyManagers(javax.net.ssl.KeyManager[] keyManagers)

param
keyManagers the keyManagers to set

	this.keyManagers = (KeyManager[])keyManagers.clone();
	newAdapteeFactory();
    
public synchronized voidsetSecureRandom(java.security.SecureRandom secureRandom)

param
secureRandom the secureRandom to set

	this.secureRandom = secureRandom;
	newAdapteeFactory();
    
public synchronized voidsetTrustAllHosts(boolean trustAllHosts)

param
trustAllHosts should all hosts be trusted?

	this.trustAllHosts = trustAllHosts;
    
public synchronized voidsetTrustManagers(javax.net.ssl.TrustManager[] trustManagers)

param
trustManagers the trustManagers to set

	this.trustManagers = trustManagers;
	newAdapteeFactory();
    
public synchronized voidsetTrustedHosts(java.lang.String[] trustedHosts)

param
trustedHosts the hosts to trust

	this.trustedHosts = (String[])trustedHosts.clone();