Top Description Inners Fields Constructors Methods
com.sun.mail.util

public Class MailSSLSocketFactory

extends SSLSocketFactory
Class Inheritance
Imports
java.io.*, java.net.*, java.security.*, java.security.cert.*, java.util.*, javax.net.ssl.*

An SSL socket factory that makes it easier to specify trust. This socket factory can be configured to trust all hosts 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.

Authors
Stephan Sann, Bill Shannon
Since
JavaMail 1.4.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private SSLSocketFactory
adapteeFactory

Holds a SSLSocketFactory to pass all API-method-calls to

private KeyManager[]
keyManagers

Holds the KeyManager array to use

private SecureRandom
secureRandom

Holds the SecureRandom to use

private SSLContext
sslcontext

Holds a SSLContext to get SSLSocketFactories from

private boolean
trustAllHosts

Should all hosts be trusted?

private String[]
trustedHosts

String-array of trusted hosts

private TrustManager[]
trustManagers

Holds the TrustManager array to use

Constructor Summary

AccessConstructor and Description
public
MailSSLSocketFactory()

Initializes a new MailSSLSocketFactory.

public
MailSSLSocketFactory(String
The protocol to use
protocol
)

Initializes a new MailSSLSocketFactory with a given protocol.

Method Summary

Modifier and TypeMethod and Description
public synchronized Socket
createSocket(Socket
the existing socket
socket
,
String
the server host
s
,
int
the server port
i
,
boolean
close the underlying socket when this socket is closed
flag
)

Implements abstract javax.net.ssl.SSLSocketFactory.createSocket.

Returns a socket layered over an existing socket connected to the named host, at the given port.
public synchronized Socket
createSocket()

Overrides javax.net.SocketFactory.createSocket.

Creates an unconnected socket.
public synchronized Socket
createSocket(InetAddress
the server network address
inetaddress
,
int
the server port
i
,
InetAddress
the client network address
inetaddress1
,
int
the client port
j
)

Implements abstract javax.net.SocketFactory.createSocket.

Creates a socket and connect it to the specified remote address on the specified remote port.
public synchronized Socket
createSocket(InetAddress
the server host
inetaddress
,
int
the server port
i
)

Implements abstract javax.net.SocketFactory.createSocket.

Creates a socket and connects it to the specified port number at the specified address.
public synchronized Socket
createSocket(String
the server host name with which to connect, or null for the loopback address.
s
,
int
the server port
i
,
InetAddress
the local address the socket is bound to
inetaddress
,
int
the local port the socket is bound to
j
)

Implements abstract javax.net.SocketFactory.createSocket.

Creates a socket and connects it to the specified remote host on the specified remote port.
public synchronized Socket
createSocket(String
the server host name with which to connect, or null for the loopback address.
s
,
int
the server port
i
)

Implements abstract javax.net.SocketFactory.createSocket.

Creates a socket and connects it to the specified remote host at the specified remote port.
public synchronized String[]
getDefaultCipherSuites()

Implements abstract javax.net.ssl.SSLSocketFactory.getDefaultCipherSuites.

Returns the list of cipher suites which are enabled by default.
public synchronized KeyManager[]

Returns:

the keyManagers
getKeyManagers
()

public synchronized SecureRandom

Returns:

the secureRandom
getSecureRandom
()

public synchronized String[]
getSupportedCipherSuites()

Implements abstract javax.net.ssl.SSLSocketFactory.getSupportedCipherSuites.

Returns the names of the cipher suites which could be enabled for use on an SSL connection.
public synchronized String[]

Returns:

the trusted hosts
getTrustedHosts
()

public synchronized TrustManager[]

Returns:

the trustManagers
getTrustManagers
()

public synchronized boolean

Returns:

true if "trustAllHosts" is set to true OR the server is contained in the "trustedHosts" array;
isServerTrusted
(String
name of the server we connected to
server
,
SSLSocket
SSLSocket connected to the server
sslSocket
)

After a successful conection to the server, this method is called to ensure that the server should be trusted.

public synchronized boolean

Returns:

true if all hosts should be trusted
isTrustAllHosts
()

private synchronized void
newAdapteeFactory()

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

public synchronized void
setKeyManagers(KeyManager[]
the keyManagers to set
keyManagers
)

public synchronized void
setSecureRandom(SecureRandom
the secureRandom to set
secureRandom
)

public synchronized void
setTrustAllHosts(boolean
should all hosts be trusted?
trustAllHosts
)

public synchronized void
setTrustedHosts(String[]
the hosts to trust
trustedHosts
)

public synchronized void
setTrustManagers(TrustManager[]
the trustManagers to set
trustManagers
)

Inherited from javax.net.ssl.SSLSocketFactory:
createSocketgetDefault

Field Detail

adapteeFactoryback to summary
private SSLSocketFactory adapteeFactory

Holds a SSLSocketFactory to pass all API-method-calls to

keyManagersback to summary
private KeyManager[] keyManagers

Holds the KeyManager array to use

secureRandomback to summary
private SecureRandom secureRandom

Holds the SecureRandom to use

sslcontextback to summary
private SSLContext sslcontext

Holds a SSLContext to get SSLSocketFactories from

trustAllHostsback to summary
private boolean trustAllHosts

Should all hosts be trusted?

trustedHostsback to summary
private String[] trustedHosts

String-array of trusted hosts

trustManagersback to summary
private TrustManager[] trustManagers

Holds the TrustManager array to use

Constructor Detail

MailSSLSocketFactoryback to summary
public MailSSLSocketFactory() throws GeneralSecurityException

Initializes a new MailSSLSocketFactory.

Exceptions
GeneralSecurityException:
for security errors
MailSSLSocketFactoryback to summary
public MailSSLSocketFactory(String protocol) throws GeneralSecurityException

Initializes a new MailSSLSocketFactory with a given protocol. Normally the protocol will be specified as "TLS".

Parameters
protocol:String

The protocol to use

Exceptions
GeneralSecurityException:
for security errors
NoSuchAlgorithmException:
if given protocol is not supported

Method Detail

createSocketback to summary
public synchronized Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException

Implements abstract javax.net.ssl.SSLSocketFactory.createSocket.

Doc from javax.net.ssl.SSLSocketFactory.createSocket.

Returns a socket layered over an existing socket connected to the named host, at the given port. This constructor can be used when tunneling SSL through a proxy or when negotiating the use of SSL over an existing socket. The host and port refer to the logical peer destination. This socket is configured using the socket options established for this factory.

Parameters
socket:Socket

the existing socket

s:String

the server host

i:int

the server port

flag:boolean

close the underlying socket when this socket is closed

Returns:Socket

a socket connected to the specified host and port

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs when creating the socket
createSocketback to summary
public synchronized Socket createSocket() throws IOException

Overrides javax.net.SocketFactory.createSocket.

Doc from javax.net.SocketFactory.createSocket.

Creates an unconnected socket.

Returns:Socket

the unconnected socket

Annotations
@Override
Exceptions
IOException:
if the socket cannot be created
createSocketback to summary
public synchronized Socket createSocket(InetAddress inetaddress, int i, InetAddress inetaddress1, int j) throws IOException

Implements abstract javax.net.SocketFactory.createSocket.

Doc from javax.net.SocketFactory.createSocket.

Creates a socket and connect it to the specified remote address on the specified remote port. The socket will also be bound to the local address and port supplied. The socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters
inetaddress:InetAddress

the server network address

i:int

the server port

inetaddress1:InetAddress

the client network address

j:int

the client port

Returns:Socket

the Socket

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs when creating the socket
createSocketback to summary
public synchronized Socket createSocket(InetAddress inetaddress, int i) throws IOException

Implements abstract javax.net.SocketFactory.createSocket.

Doc from javax.net.SocketFactory.createSocket.

Creates a socket and connects it to the specified port number at the specified address. This socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters
inetaddress:InetAddress

the server host

i:int

the server port

Returns:Socket

the Socket

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs when creating the socket
createSocketback to summary
public synchronized Socket createSocket(String s, int i, InetAddress inetaddress, int j) throws IOException, UnknownHostException

Implements abstract javax.net.SocketFactory.createSocket.

Doc from javax.net.SocketFactory.createSocket.

Creates a socket and connects it to the specified remote host on the specified remote port. The socket will also be bound to the local address and port supplied. This socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters
s:String

the server host name with which to connect, or null for the loopback address.

i:int

the server port

inetaddress:InetAddress

the local address the socket is bound to

j:int

the local port the socket is bound to

Returns:Socket

the Socket

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs when creating the socket
UnknownHostException:
if the host is not known
createSocketback to summary
public synchronized Socket createSocket(String s, int i) throws IOException, UnknownHostException

Implements abstract javax.net.SocketFactory.createSocket.

Doc from javax.net.SocketFactory.createSocket.

Creates a socket and connects it to the specified remote host at the specified remote port. This socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters
s:String

the server host name with which to connect, or null for the loopback address.

i:int

the server port

Returns:Socket

the Socket

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs when creating the socket
UnknownHostException:
if the host is not known
getDefaultCipherSuitesback to summary
public synchronized String[] getDefaultCipherSuites()

Implements abstract javax.net.ssl.SSLSocketFactory.getDefaultCipherSuites.

Doc from javax.net.ssl.SSLSocketFactory.getDefaultCipherSuites.

Returns the list of cipher suites which are enabled by default. Unless a different list is enabled, handshaking on an SSL connection will use one of these cipher suites. The minimum quality of service for these defaults requires confidentiality protection and server authentication (that is, no anonymous cipher suites).

The returned array includes cipher suites from the list of standard cipher suite names in the JSSE Cipher Suite Names section of the Java Security Standard Algorithm Names Specification, and may also include other cipher suites that the provider supports.

Returns:String[]

array of the cipher suites enabled by default

Annotations
@Override
getKeyManagersback to summary
public synchronized KeyManager[] getKeyManagers()
Returns:KeyManager[]

the keyManagers

getSecureRandomback to summary
public synchronized SecureRandom getSecureRandom()
Returns:SecureRandom

the secureRandom

getSupportedCipherSuitesback to summary
public synchronized String[] getSupportedCipherSuites()

Implements abstract javax.net.ssl.SSLSocketFactory.getSupportedCipherSuites.

Doc from javax.net.ssl.SSLSocketFactory.getSupportedCipherSuites.

Returns the names of the cipher suites which could be enabled for use on an SSL connection. Normally, only a subset of these will actually be enabled by default, since this list may include cipher suites which do not meet quality of service requirements for those defaults. Such cipher suites are useful in specialized applications.

The returned array includes cipher suites from the list of standard cipher suite names in the JSSE Cipher Suite Names section of the Java Security Standard Algorithm Names Specification, and may also include other cipher suites that the provider supports.

Returns:String[]

an array of cipher suite names

Annotations
@Override
getTrustedHostsback to summary
public synchronized String[] getTrustedHosts()
Returns:String[]

the trusted hosts

getTrustManagersback to summary
public synchronized TrustManager[] getTrustManagers()
Returns:TrustManager[]

the trustManagers

isServerTrustedback to summary
public synchronized boolean isServerTrusted(String server, SSLSocket sslSocket)

After a successful conection to the server, this method is called to ensure that the server should be trusted.

Parameters
server:String

name of the server we connected to

sslSocket:SSLSocket

SSLSocket connected to the server

Returns:boolean

true if "trustAllHosts" is set to true OR the server is contained in the "trustedHosts" array;

isTrustAllHostsback to summary
public synchronized boolean isTrustAllHosts()
Returns:boolean

true if all hosts should be trusted

newAdapteeFactoryback to summary
private synchronized void newAdapteeFactory() throws KeyManagementException

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

Exceptions
KeyManagementException:
for key manager errors
setKeyManagersback to summary
public synchronized void setKeyManagers(KeyManager[] keyManagers) throws GeneralSecurityException
Parameters
keyManagers:KeyManager[]

the keyManagers to set

Exceptions
GeneralSecurityException:
for security errors
setSecureRandomback to summary
public synchronized void setSecureRandom(SecureRandom secureRandom) throws GeneralSecurityException
Parameters
secureRandom:SecureRandom

the secureRandom to set

Exceptions
GeneralSecurityException:
for security errors
setTrustAllHostsback to summary
public synchronized void setTrustAllHosts(boolean trustAllHosts)
Parameters
trustAllHosts:boolean

should all hosts be trusted?

setTrustedHostsback to summary
public synchronized void setTrustedHosts(String[] trustedHosts)
Parameters
trustedHosts:String[]

the hosts to trust

setTrustManagersback to summary
public synchronized void setTrustManagers(TrustManager[] trustManagers) throws GeneralSecurityException
Parameters
trustManagers:TrustManager[]

the trustManagers to set

Exceptions
GeneralSecurityException:
for security errors
com.sun.mail.util back to summary

private Class MailSSLSocketFactory.MailTrustManager

extends Object
implements X509TrustManager
Class Inheritance
All Implemented Interfaces
javax.net.ssl.X509TrustManager, javax.net.ssl.TrustManager

A default Trustmanager.
Author
Stephan Sann

Field Summary

Modifier and TypeField and Description
private X509TrustManager
adapteeTrustManager

A TrustManager to pass method calls to

Constructor Summary

AccessConstructor and Description
private
MailTrustManager()

Initializes a new TrustManager instance.

Method Summary

Modifier and TypeMethod and Description
public void
checkClientTrusted(X509Certificate[]
the peer certificate chain
certs
,
String
the authentication type based on the client certificate
authType
)

Implements javax.net.ssl.X509TrustManager.checkClientTrusted.

Given the partial or complete certificate chain provided by the peer, build a certificate path to a trusted root and return if it can be validated and is trusted for client SSL authentication based on the authentication type.
public void
checkServerTrusted(X509Certificate[]
the peer certificate chain
certs
,
String
the key exchange algorithm used
authType
)

Implements javax.net.ssl.X509TrustManager.checkServerTrusted.

Given the partial or complete certificate chain provided by the peer, build a certificate path to a trusted root and return if it can be validated and is trusted for server SSL authentication based on the authentication type.
public X509Certificate[]
getAcceptedIssuers()

Implements javax.net.ssl.X509TrustManager.getAcceptedIssuers.

Return an array of certificate authority certificates which are trusted for authenticating peers.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

adapteeTrustManagerback to summary
private X509TrustManager adapteeTrustManager

A TrustManager to pass method calls to

Constructor Detail

MailTrustManagerback to summary
private MailTrustManager() throws GeneralSecurityException

Initializes a new TrustManager instance.

Method Detail

checkClientTrustedback to summary
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException

Implements javax.net.ssl.X509TrustManager.checkClientTrusted.

Doc from javax.net.ssl.X509TrustManager.checkClientTrusted.

Given the partial or complete certificate chain provided by the peer, build a certificate path to a trusted root and return if it can be validated and is trusted for client SSL authentication based on the authentication type.

The authentication type is determined by the actual certificate used. For instance, if RSAPublicKey is used, the authType should be "RSA". Checking is case-sensitive.

Parameters
certs:X509Certificate[]

the peer certificate chain

authType:String

the authentication type based on the client certificate

Annotations
@Override
Exceptions
CertificateException:
if the certificate chain is not trusted by this TrustManager.
checkServerTrustedback to summary
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException

Implements javax.net.ssl.X509TrustManager.checkServerTrusted.

Doc from javax.net.ssl.X509TrustManager.checkServerTrusted.

Given the partial or complete certificate chain provided by the peer, build a certificate path to a trusted root and return if it can be validated and is trusted for server SSL authentication based on the authentication type.

The authentication type is the key exchange algorithm portion of the cipher suites represented as a String, such as "RSA", "DHE_DSS".

Note

for some exportable cipher suites, the key exchange algorithm is determined at run time during the handshake. For instance, for TLS_RSA_EXPORT_WITH_RC4_40_MD5, the authType should be RSA_EXPORT when an ephemeral RSA key is used for the key exchange, and RSA when the key from the server certificate is used. Checking is case-sensitive.

Parameters
certs:X509Certificate[]

the peer certificate chain

authType:String

the key exchange algorithm used

Annotations
@Override
Exceptions
CertificateException:
if the certificate chain is not trusted by this TrustManager.
getAcceptedIssuersback to summary
public X509Certificate[] getAcceptedIssuers()

Implements javax.net.ssl.X509TrustManager.getAcceptedIssuers.

Doc from javax.net.ssl.X509TrustManager.getAcceptedIssuers.

Return an array of certificate authority certificates which are trusted for authenticating peers.

Returns:X509Certificate[]

a non-null (possibly empty) array of acceptable CA issuer certificates.

Annotations
@Override