x509
符合PKI ITU-T X509標(biāo)準(zhǔn)德谅,基本的證書格式,只包含公鑰萨螺。x509證書由用戶公共密鑰和用戶標(biāo)識(shí)符組成。此外還包括版本號(hào)愧驱、證書序列號(hào)慰技、CA標(biāo)識(shí)符、簽名算法標(biāo)識(shí)组砚、簽發(fā)者名稱吻商、證書有效期等信息。
SSL加密是Netscape公司所提出的安全保bai密協(xié)議糟红,在瀏覽器和duWeb服務(wù)器之間構(gòu)造安zhi全通道來進(jìn)行數(shù)據(jù)傳輸艾帐,SSL運(yùn)行在TCP/IP層之上、應(yīng)dao用層之下盆偿,為應(yīng)用程序提供加密數(shù)據(jù)通道柒爸,它采用了RC4、MD5以及RSA等加密算法事扭,使用40 位的密鑰捎稚,適用于商業(yè)信息的加密。
TLS是安全傳輸層協(xié)議求橄。安全傳輸層協(xié)議(TLS)用于在兩個(gè)通信應(yīng)用程序之間提供保密性和數(shù)據(jù)完整性今野。該協(xié)議由兩層組成: TLS 記錄協(xié)議(TLS Record)和 TLS 握手協(xié)議(TLS Handshake)。較低的層為 TLS 記錄協(xié)議罐农,位于某個(gè)可靠的傳輸協(xié)議上面条霜。
SSL加密并不保護(hù)數(shù)據(jù)中心本身,而是確保了SSL加密設(shè)備的數(shù)據(jù)中心安全涵亏,可以監(jiān)控企業(yè)中來往于數(shù)據(jù)中心的最終用戶流量宰睡。
從某個(gè)角度來看,數(shù)據(jù)中心管理員可以放心將加密裝置放在某個(gè)地方溯乒,需要使用時(shí)再進(jìn)行應(yīng)用夹厌,數(shù)據(jù)中心應(yīng)該會(huì)有更合理的方法來應(yīng)對(duì)利用SSL的惡意攻擊,需要找到SSL加密應(yīng)用的最佳實(shí)踐裆悄。
TLS協(xié)議是可選的汇恤,必須配置客戶端和服務(wù)器才能使用。主要有兩種方式實(shí)現(xiàn)這一目標(biāo):一個(gè)是使用統(tǒng)一的TLS協(xié)議通信端口(例如:用于HTTPS的端口443)事镣。另一個(gè)是客戶端請(qǐng)求服務(wù)器連接到TLS時(shí)使用特定的協(xié)議機(jī)制(例如:郵件、新聞協(xié)議和STARTTLS)孩等。
一旦客戶端和服務(wù)器都同意使用TLS協(xié)議,他們通過使用一個(gè)握手過程協(xié)商出一個(gè)有狀態(tài)的連接以傳輸數(shù)據(jù)采够。通過握手肄方,客戶端和服務(wù)器協(xié)商各種參數(shù)用于創(chuàng)建安全連接。
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.
此方法遍歷已注冊(cè)安全提供者的列表蹬癌,從最喜歡的提供者開始权她。返回一個(gè)新的SSLContext對(duì)象,該對(duì)象封裝了支持指定協(xié)議的第一個(gè)提供程序的SSLContextSpi實(shí)現(xiàn)逝薪。
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.)
初始化此上下文隅要。前兩個(gè)參數(shù)中的任何一個(gè)都可以為null,在這種情況下董济,將搜索*已安裝的安全提供程序以尋找適當(dāng)工廠的最高優(yōu)先級(jí)實(shí)現(xiàn)步清。同樣,安全隨機(jī)參數(shù)可以為null虏肾,在這種情況下廓啊,將使用默認(rèn)實(shí)現(xiàn)。
<P> 僅使用數(shù)組中特定密鑰和/或信任管理器*實(shí)現(xiàn)類型的第一個(gè)實(shí)例封豪。 (例如谴轮,僅使用數(shù)組中的第一個(gè)javax.net.ssl.X509KeyManager。)
SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory());
做了個(gè)代理
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()
*/
翻譯:
設(shè)置啟用用于此連接的協(xié)議版本撑毛。 * <P> *必須由* <code> getSupportedProtocols()</ code>列出該協(xié)議受支持书聚。 *成功調(diào)用此方法后,僅允許使用<code> protocols </ code>參數(shù)中列出的協(xié)議藻雌。
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 中調(diào)用
@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證書數(shù)組雌续。
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
該方法檢查服務(wù)器的證書,若不信任該證書同樣拋出異常胯杭。通過自己實(shí)現(xiàn)該方法驯杜,可以使之信任我們指定的任何證書。
// 在實(shí)現(xiàn)該方法時(shí)做个,也可以簡(jiǎn)單的不做任何處理鸽心,即一個(gè)空的函數(shù)體,由于不會(huì)拋出異常居暖,它就會(huì)信任任何證書顽频。
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
該方法檢查客戶端的證書,若不信任該證書則拋出異常太闺。由于我們不需要對(duì)客戶端進(jìn)行認(rèn)證糯景,
// 因此我們只需要執(zhí)行默認(rèn)的信任管理器的這個(gè)方法。JSSE中,默認(rèn)的信任管理器類為TrustManager蟀淮。
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
//設(shè)置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();