x509
符合PKI ITU-T X509标准,基本的证书格式,只包含公钥。x509证书由用户公共密钥和用户标识符组成。此外还包括版本号、证书序列号、CA标识符、签名算法标识、签发者名称、证书有效期等信息。
SSL加密是Netscape公司所提出的安全保bai密协议,在浏览器和duWeb服务器之间构造安zhi全通道来进行数据传输,SSL运行在TCP/IP层之上、应dao用层之下,为应用程序提供加密数据通道,它采用了RC4、MD5以及RSA等加密算法,使用40 位的密钥,适用于商业信息的加密。
相关图
TLS是安全传输层协议。安全传输层协议(TLS)用于在两个通信应用程序之间提供保密性和数据完整性。该协议由两层组成: TLS 记录协议(TLS Record)和 TLS 握手协议(TLS Handshake)。较低的层为 TLS 记录协议,位于某个可靠的传输协议上面。
SSL加密并不保护数据中心本身,而是确保了SSL加密设备的数据中心安全,可以监控企业中来往于数据中心的最终用户流量。
从某个角度来看,数据中心管理员可以放心将加密装置放在某个地方,需要使用时再进行应用,数据中心应该会有更合理的方法来应对利用SSL的恶意攻击,需要找到SSL加密应用的最佳实践。
TLS协议是可选的,必须配置客户端和服务器才能使用。主要有两种方式实现这一目标:一个是使用统一的TLS协议通信端口(例如:用于HTTPS的端口443)。另一个是客户端请求服务器连接到TLS时使用特定的协议机制(例如:邮件、新闻协议和STARTTLS)。
一旦客户端和服务器都同意使用TLS协议,他们通过使用一个握手过程协商出一个有状态的连接以传输数据。通过握手,客户端和服务器协商各种参数用于创建安全连接。
SSLContext sslContext =SSLContext.getInstance("TLS");
主要注释:
This method traverses the list of registered security Providers,
starting with the most preferred Provider.
A new SSLContext object encapsulating the
SSLContextSpi implementation from the first
Provider that supports the specified protocol is returned.
此方法遍历已注册安全提供者的列表,从最喜欢的提供者开始。返回一个新的SSLContext对象,该对象封装了支持指定协议的第一个提供程序的SSLContextSpi实现。
sslContext.init(null, null, null);
主要注释:
Initializes this context. Either of the first two parameters
may be null in which case the installed security providers will
be searched for the highest priority implementation of the
appropriate factory. Likewise, the secure random parameter may
be null in which case the default implementation will be used.
<P>
Only the first instance of a particular key and/or trust manager
implementation type in the array is used. (For example, only
the first javax.net.ssl.X509KeyManager in the array will be used.)
初始化此上下文。前两个参数中的任何一个都可以为null,在这种情况下,将搜索*已安装的安全提供程序以寻找适当工厂的最高优先级实现。同样,安全随机参数可以为null,在这种情况下,将使用默认实现。
<P> 仅使用数组中特定密钥和/或信任管理器*实现类型的第一个实例。 (例如,仅使用数组中的第一个javax.net.ssl.X509KeyManager。)
SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory());
做了个代理
public class Tls12SocketFactory extends SSLSocketFactory {
private static final String[] TLS_SUPPORT_VERSION = {"TLSv1.1", "TLSv1.2"};
final SSLSocketFactory delegate;
public Tls12SocketFactory(SSLSocketFactory delegate) {
this.delegate = delegate;
}
...
}
/**
* Sets the protocol versions enabled for use on this connection.
* <P>
* The protocols must have been listed by
* <code>getSupportedProtocols()</code> as being supported.
* Following a successful call to this method, only protocols listed
* in the <code>protocols</code> parameter are enabled for use.
*
* @param protocols Names of all the protocols to enable.
* @throws IllegalArgumentException when one or more of
* the protocols named by the parameter is not supported or
* when the protocols parameter is null.
* @see #getEnabledProtocols()
*/
翻译:
设置启用用于此连接的协议版本。 * <P> *必须由* <code> getSupportedProtocols()</ code>列出该协议受支持。 *成功调用此方法后,仅允许使用<code> protocols </ code>参数中列出的协议。
public abstract void setEnabledProtocols(String protocols[]);
使用:
private static final String[] TLS_SUPPORT_VERSION = {"TLSv1.1", "TLSv1.2"};
private Socket patch(Socket s) {
if (s instanceof SSLSocket) {
((SSLSocket) s).setEnabledProtocols(TLS_SUPPORT_VERSION);
}
return s;
}
eg: 在 createSocket 中调用
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return patch(delegate.createSocket(host, port, localHost, localPort));
}
public static class UnSafeTrustManager implements X509TrustManager
{
返回受信任的X509证书数组。
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
该方法检查服务器的证书,若不信任该证书同样抛出异常。通过自己实现该方法,可以使之信任我们指定的任何证书。
// 在实现该方法时,也可以简单的不做任何处理,即一个空的函数体,由于不会抛出异常,它就会信任任何证书。
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
该方法检查客户端的证书,若不信任该证书则抛出异常。由于我们不需要对客户端进行认证,
// 因此我们只需要执行默认的信任管理器的这个方法。JSSE中,默认的信任管理器类为TrustManager。
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
//设置HTTPS 证书
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(BuildConfig.INTERNET_REQUEST_TIME, TimeUnit.MILLISECONDS)
.connectTimeout(BuildConfig.INTERNET_REQUEST_TIME, TimeUnit.MILLISECONDS)
.addInterceptor(headerInterceptor)
.addInterceptor(logging)
.sslSocketFactory(socketFactory, new UnSafeTrustManager())
.build();
网友评论