public class SSLCertificateSocketFactory
extends SSLSocketFactory
| java.lang.Object | |||
| javax.net.SocketFactory | |||
| javax.net.ssl.SSLSocketFactory | |||
| android.net.SSLCertificateSocketFactory | |||
带有几个额外功能的SSLSocketFactory实现:
SSLSessionCache createSocket() and
connect(SocketAddress, int), after which you must verify the identity of the server you are connected to.
大多数SSLSocketFactory实现不验证服务器的身份,允许中间人攻击。 此实现会检查服务器的证书主机名,但仅限于指定主机名的createSocket变体。 当使用使用InetAddress或返回未连接套接字的方法时,您必须自己验证服务器的身份以确保安全连接。
验证服务器身份的一种方法是使用 getDefaultHostnameVerifier()获取 HostnameVerifier来验证证书主机名。
在开发设备上,“setprop socket.relaxsslcheck yes”会绕过所有SSL证书和主机名检查以进行测试。 该设置需要root访问权限。
Public constructors |
|
|---|---|
SSLCertificateSocketFactory(int handshakeTimeoutMillis) 此构造函数在API级别8中已弃用。请改用 |
|
公共方法(Public methods) |
|
|---|---|
Socket |
createSocket(String host, int port) 创建套接字并将其连接到指定远程端口上的指定远程主机。 此方法在连接后验证对等方的证书主机名(除非使用 |
Socket |
createSocket(Socket k, String host, int port, boolean close) 返回一个套接字,该套接字在连接到指定主机的现有套接字上分层,位于给定端口上。 此方法在连接后验证对等方的证书主机名(除非使用 |
Socket |
createSocket() 创建一个未连接到任何远程主机的新套接字。 |
Socket |
createSocket(InetAddress addr, int port, InetAddress localAddr, int localPort) 创建套接字并将其连接到指定远程端口上的指定远程地址。 警告:此方法不执行主机名验证。 |
Socket |
createSocket(InetAddress addr, int port) 创建一个套接字并将其连接到指定地址的指定端口号。 警告:此方法不执行主机名验证。 |
Socket |
createSocket(String host, int port, InetAddress localAddr, int localPort) 创建套接字并将其连接到指定远程端口上的指定远程主机。 此方法在连接后验证对等方的证书主机名(除非使用 |
static SocketFactory |
getDefault(int handshakeTimeoutMillis) 使用可选的握手超时返回新的套接字工厂实例。 |
static SSLSocketFactory |
getDefault(int handshakeTimeoutMillis, SSLSessionCache cache) 使用可选的握手超时和SSL会话高速缓存返回新的套接字工厂实例。 |
String[] |
getDefaultCipherSuites() 返回默认启用的密码套件列表。 |
static SSLSocketFactory |
getInsecure(int handshakeTimeoutMillis, SSLSessionCache cache) 使用可选的握手超时和SSL会话高速缓存,返回所有SSL安全性检查被禁用的套接字工厂的新实例。 |
byte[] |
getNpnSelectedProtocol(Socket socket) 返回由客户端和服务器选择的 Next Protocol Negotiation (NPN)协议;如果没有协议协商,则返回null。 |
String[] |
getSupportedCipherSuites() 返回可用于SSL连接的密码套件的名称。 |
void |
setHostname(Socket socket, String hostName) 在给定套接字上打开 Server Name Indication (SNI) 。 |
void |
setKeyManagers(KeyManager[] keyManagers) 设置 |
void |
setNpnProtocols(byte[][] npnProtocols) 设置该对等体感兴趣的 Next Protocol Negotiation (NPN)协议。 |
void |
setTrustManagers(TrustManager[] trustManager) 设置 |
void |
setUseSessionTickets(Socket socket, boolean useSessionTickets) 在给定套接字上启用 session ticket支持。 |
继承方法(Inherited methods) |
|
|---|---|
javax.net.ssl.SSLSocketFactory
|
|
javax.net.SocketFactory
|
|
java.lang.Object
|
|
SSLCertificateSocketFactory (int handshakeTimeoutMillis)
此构造函数在API级别8中已被弃用。
改为使用getDefault(int) 。
| 参数(Parameters) | |
|---|---|
handshakeTimeoutMillis |
int
|
Socket createSocket (String host, int port)
创建套接字并将其连接到指定远程端口上的指定远程主机。 该套接字使用为该工厂建立的套接字选项进行配置。
如果有安全管理器,则使用主机地址和port作为参数调用其checkConnect方法。 这可能会导致SecurityException。
此方法在连接后验证对等方的证书主机名(除非使用 getInsecure(int, SSLSessionCache)创建)。
| 参数(Parameters) | |
|---|---|
host |
String: the server host name with which to connect, or null for the loopback address. |
port |
int: the server port |
| 返回(Returns) | |
|---|---|
Socket |
the Socket |
| 抛出异常(Throws) | |
|---|---|
IOException |
|
Socket createSocket (Socket k, String host, int port, boolean close)
返回一个套接字,该套接字在连接到指定主机的现有套接字上分层,位于给定端口上。 当通过代理隧道化SSL或在现有套接字上协商使用SSL时,可以使用此构造函数。 主机和端口是指逻辑对等端目标。 该套接字使用为该工厂建立的套接字选项进行配置。
此方法在连接后验证对等方的证书主机名(除非使用 getInsecure(int, SSLSessionCache)创建)。
| 参数(Parameters) | |
|---|---|
k |
Socket: the existing socket |
host |
String: the server host |
port |
int: the server port |
close |
boolean: close the underlying socket when this socket is closed |
| 返回(Returns) | |
|---|---|
Socket |
a socket connected to the specified host and port |
| 抛出异常(Throws) | |
|---|---|
IOException |
|
Socket createSocket ()
创建一个未连接到任何远程主机的新套接字。 您必须使用connect(SocketAddress)来连接插座。
警告:此方法不执行主机名验证。 连接套接字后,您必须验证服务器的身份以避免中间人攻击。
| 返回(Returns) | |
|---|---|
Socket |
the unconnected socket |
| 抛出异常(Throws) | |
|---|---|
IOException |
|
Socket createSocket (InetAddress addr, int port, InetAddress localAddr, int localPort)
创建套接字并将其连接到指定远程端口上的指定远程地址。 套接字也将被绑定到本地地址和端口。 套接字使用为此工厂建立的套接字选项进行配置。
如果有安全管理器,则使用主机地址和port作为参数调用其checkConnect方法。 这可能会导致SecurityException。
警告:此方法不执行主机名验证。 连接套接字后,您必须验证服务器的身份以避免中间人攻击。
| 参数(Parameters) | |
|---|---|
addr |
InetAddress: the server network address |
port |
int: the server port |
localAddr |
InetAddress: the client network address |
localPort |
int: the client port |
| 返回(Returns) | |
|---|---|
Socket |
the Socket |
| 抛出异常(Throws) | |
|---|---|
IOException |
|
Socket createSocket (InetAddress addr, int port)
创建一个套接字并将其连接到指定地址的指定端口号。 该套接字使用为该工厂建立的套接字选项进行配置。
如果有安全管理器,则使用主机地址和port作为参数调用其checkConnect方法。 这可能会导致SecurityException。
警告:此方法不执行主机名验证。 连接套接字后,您必须验证服务器的身份以避免中间人攻击。
| 参数(Parameters) | |
|---|---|
addr |
InetAddress: the server host |
port |
int: the server port |
| 返回(Returns) | |
|---|---|
Socket |
the Socket |
| 抛出异常(Throws) | |
|---|---|
IOException |
|
Socket createSocket (String host, int port, InetAddress localAddr, int localPort)
创建套接字并将其连接到指定远程端口上的指定远程主机。 套接字也将被绑定到本地地址和提供的端口。 该套接字使用为该工厂建立的套接字选项进行配置。
如果有安全管理器,则使用主机地址和port作为其参数调用其checkConnect方法。 这可能会导致SecurityException。
此方法在连接后验证对等方的证书主机名(除非使用 getInsecure(int, SSLSessionCache)创建)。
| 参数(Parameters) | |
|---|---|
host |
String: the server host name with which to connect, or null for the loopback address. |
port |
int: the server port |
localAddr |
InetAddress: the local address the socket is bound to |
localPort |
int: the local port the socket is bound to |
| 返回(Returns) | |
|---|---|
Socket |
the Socket |
| 抛出异常(Throws) | |
|---|---|
IOException |
|
SocketFactory getDefault (int handshakeTimeoutMillis)
使用可选的握手超时返回新的套接字工厂实例。
| 参数(Parameters) | |
|---|---|
handshakeTimeoutMillis |
int: to use for SSL connection handshake, or 0 for none. The socket timeout is reset to 0 after the handshake. |
| 返回(Returns) | |
|---|---|
SocketFactory |
a new SSLSocketFactory with the specified parameters |
SSLSocketFactory getDefault (int handshakeTimeoutMillis, SSLSessionCache cache)
使用可选的握手超时和SSL会话高速缓存返回新的套接字工厂实例。
| 参数(Parameters) | |
|---|---|
handshakeTimeoutMillis |
int: to use for SSL connection handshake, or 0 for none. The socket timeout is reset to 0 after the handshake. |
cache |
SSLSessionCache: The SSLSessionCache to use, or null for no cache. |
| 返回(Returns) | |
|---|---|
SSLSocketFactory |
a new SSLSocketFactory with the specified parameters |
String[] getDefaultCipherSuites ()
返回默认启用的密码套件列表。 除非启用不同的列表,否则SSL连接握手将使用其中一个密码套件。 这些默认值的最低服务质量需要机密性保护和服务器身份验证(即没有匿名密码套件)。
| 返回(Returns) | |
|---|---|
String[] |
array of the cipher suites enabled by default |
SSLSocketFactory getInsecure (int handshakeTimeoutMillis, SSLSessionCache cache)
使用可选的握手超时和SSL会话高速缓存,返回所有SSL安全性检查被禁用的套接字工厂的新实例。
警告:使用这个工厂创建的套接字很容易受到中间人攻击!
| 参数(Parameters) | |
|---|---|
handshakeTimeoutMillis |
int: to use for SSL connection handshake, or 0 for none. The socket timeout is reset to 0 after the handshake. |
cache |
SSLSessionCache: The SSLSessionCache to use, or null for no cache. |
| 返回(Returns) | |
|---|---|
SSLSocketFactory |
an insecure SSLSocketFactory with the specified parameters |
byte[] getNpnSelectedProtocol (Socket socket)
返回由客户端和服务器选择的 Next Protocol Negotiation (NPN)协议;如果没有协议协商,则返回null。
| 参数(Parameters) | |
|---|---|
socket |
Socket: a socket created by this factory. |
| 返回(Returns) | |
|---|---|
byte[] |
|
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the socket was not created by this factory. |
String[] getSupportedCipherSuites ()
返回可用于SSL连接的密码套件的名称。 通常,默认情况下,只有这些列表中的一部分会实际启用,因为此列表可能包含不符合服务质量要求的密码套件。 这种密码套件在专门的应用程序中很有用。
| 返回(Returns) | |
|---|---|
String[] |
an array of cipher suite names |
void setHostname (Socket socket, String hostName)
在给定套接字上打开 Server Name Indication (SNI) 。
| 参数(Parameters) | |
|---|---|
socket |
Socket: a socket created by this factory. |
hostName |
String: the desired SNI hostname, null to disable. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the socket was not created by this factory. |
void setKeyManagers (KeyManager[] keyManagers)
设置 KeyManager用于由该工厂进行的连接。
| 参数(Parameters) | |
|---|---|
keyManagers |
KeyManager
|
void setNpnProtocols (byte[][] npnProtocols)
设置该对等体感兴趣的 Next Protocol Negotiation (NPN)协议。
对于服务器来说,这是按照优先顺序按照支持进行广告的协议序列。 该列表以未加密方式发送给所有支持NPN的客户端。
对于客户端,这是一个支持的协议列表,用于匹配服务器列表。 如果客户端和服务器都不支持协议,则会选择客户端列表中的第一个协议。 客户协议的顺序无关紧要。
| 参数(Parameters) | |
|---|---|
npnProtocols |
byte: a non-empty list of protocol byte arrays. All arrays must be non-empty and of length less than 256. |
void setTrustManagers (TrustManager[] trustManager)
设置 TrustManager用于由该工厂进行的连接。
| 参数(Parameters) | |
|---|---|
trustManager |
TrustManager
|
void setUseSessionTickets (Socket socket, boolean useSessionTickets)
在给定套接字上启用 session ticket支持。
| 参数(Parameters) | |
|---|---|
socket |
Socket: a socket created by this factory |
useSessionTickets |
boolean: true to enable session ticket support on this socket. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the socket was not created by this factory. |