Android安全加密專題文章索引
以上學(xué)習(xí)所有內(nèi)容,對(duì)稱加密、非對(duì)稱加密、消息摘要、數(shù)字簽名等知識(shí)都是為了理解數(shù)字證書工作原理而作為一個(gè)預(yù)備知識(shí)。數(shù)字證書是密碼學(xué)里的終極武器,是人類幾千年歷史總結(jié)的智慧的結(jié)晶,只有在明白了數(shù)字證書工作原理后,才能理解Https 協(xié)議的安全通訊機(jī)制。最終才能在SSL 開發(fā)過程中得心應(yīng)手。
另外,對(duì)稱加密和消息摘要這兩個(gè)知識(shí)點(diǎn)是可以單獨(dú)拿來使用的。
知識(shí)點(diǎn)串聯(lián):
數(shù)字證書使用到了以上學(xué)習(xí)的所有知識(shí)
通過以上內(nèi)容的學(xué)習(xí),我們要能掌握以下知識(shí)點(diǎn):
概述
SSL(Secure Sockets Layer 安全套接層),為網(wǎng)景公司(Netscape)所研發(fā),用以保障在Internet 上數(shù)據(jù)傳輸之安全,利用數(shù)據(jù)加密(Encryption)技術(shù),可確保數(shù)據(jù)在網(wǎng)絡(luò)上之傳輸過程中不會(huì)被截取及竊聽。一般通用之規(guī)格為40 bit 之安全標(biāo)準(zhǔn),美國則已推出128 bit 之更高安全標(biāo)準(zhǔn),但限制出境。只要3.0 版本以上之I.E.或Netscape 瀏覽器即可支持SSL。
TLS(Transport Layer Security 傳輸層安全),用于在兩個(gè)通信應(yīng)用程序之間提供保密性和數(shù)據(jù)完整性。TLS 是SSL 的標(biāo)準(zhǔn)化后的產(chǎn)物,有1.0 ,1.1 ,1.2 三個(gè)版本,默認(rèn)使用1.0。TLS1.0 和SSL3.0 幾乎沒
有區(qū)別,事實(shí)上我們現(xiàn)在用的都是TLS,但因?yàn)闅v史上習(xí)慣了SSL 這個(gè)稱呼。
SSL 通信簡單圖示:

SSL 通信詳細(xì)圖示:

當(dāng)請(qǐng)求使用自簽名證書的網(wǎng)站數(shù)據(jù)時(shí),例如請(qǐng)求12306 的客運(yùn)服務(wù)頁面:https://kyfw.12306.cn/otn/,則會(huì)報(bào)下面的錯(cuò)誤,原因是客戶端的根認(rèn)證機(jī)構(gòu)不能識(shí)別該證書錯(cuò)誤信息:unable to find valid certification path to requested target
解決方案1
一個(gè)證書可不可信,是由TrustManager 決定的,所以我們只需要自定義一個(gè)什么都不做的TrustManager即可,服務(wù)器出示的所有證書都不做校驗(yàn),一律放行。
public static void main(String[] args) throws Exception {//協(xié)議傳輸層安全TLS(transport layer secure)SSLContext sslContext = SSLContext.getInstance("TLS");//創(chuàng)建信任管理器(TrustManager 負(fù)責(zé)校驗(yàn)證書是否可信)TrustManager[] tm = new TrustManager[]{new EmptyX509TrustManager()};//使用自定義的信任管理器初始化SSL 上下文對(duì)象sslContext.init(null, tm, null);//設(shè)置全局的SSLSocketFactory 工廠(對(duì)所有ssl 鏈接都產(chǎn)生影響)HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); //URL url = new URL("https://www.baidu.com"); URL url = new URL("https://kyfw.12306.cn/otn/"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); InputStream in = conn.getInputStream(); System.out.println(Util.inputstream2String(in)); } /** * 自定義一個(gè)什么都不做的信任管理器,所有證書都不做校驗(yàn),一律放行 */ private static class EmptyX509TrustManager implements X509TrustManager{ @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; }} 解決方案2
12306 服務(wù)器出示的證書是中鐵集團(tuán)SRCA 給他頒發(fā)的,所以SRCA 的證書是能夠識(shí)別12306 的證書的,所以只需要把SRCA 證書導(dǎo)入系統(tǒng)的KeyStore 里,之后交給TrustManagerFactory 進(jìn)行初始化,則可把SRCA 添加至根證書認(rèn)證機(jī)構(gòu),之后校驗(yàn)的時(shí)候,SRCA 對(duì)12306 證書校驗(yàn)時(shí)就能通過認(rèn)證。
這種解決方案有兩種使用方式:一是直接使用SRCA.cer 文件,二是使用改文件的RFC 格式數(shù)據(jù),將其寫在代碼里。
//12306 證書的RFC 格式(注意要記得手動(dòng)添加兩個(gè)換行符) private static final String CERT_12306_RFC = "-----BEGIN CERTIFICATE-----/n"+"MIICmjCCAgOgAwIBAgIIbyZr5/jKH6QwDQYJKoZIhvcNAQEFBQAwRzELMAkGA1UEBhMCQ04xKTAn"+"BgNVBAoTIFNpbm9yYWlsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRTUkNBMB4X"+"DTA5MDUyNTA2NTYwMFoXDTI5MDUyMDA2NTYwMFowRzELMAkGA1UEBhMCQ04xKTAnBgNVBAoTIFNp"+"bm9yYWlsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRTUkNBMIGfMA0GCSqGSIb3"+"DQEBAQUAA4GNADCBiQKBgQDMpbNeb34p0GvLkZ6t72/OOba4mX2K/eZRWFfnuk8e5jKDH+9BgCb2"+"9bSotqPqTbxXWPxIOz8EjyUO3bfR5pQ8ovNTOlks2rS5BdMhoi4sUjCKi5ELiqtyww/XgY5iFqv6"+"D4Pw9QvOUcdRVSbPWo1DwMmH75It6pk/rARIFHEjWwIDAQABo4GOMIGLMB8GA1UdIwQYMBaAFHle"+"tne34lKDQ+3HUYhMY4UsAENYMAwGA1UdEwQFMAMBAf8wLgYDVR0fBCcwJTAjoCGgH4YdaHR0cDov"+"LzE5Mi4xNjguOS4xNDkvY3JsMS5jcmwwCwYDVR0PBAQDAgH+MB0GA1UdDgQWBBR5XrZ3t+JSg0Pt"+"x1GITGOFLABDWDANBgkqhkiG9w0BAQUFAAOBgQDGrAm2U/of1LbOnG2bnnQtgcVaBXiVJF8LKPaV"+"23XQ96HU8xfgSZMJS6U00WHAI7zp0q208RSUft9wDq9ee///VOhzR6Tebg9QfyPSohkBrhXQenvQ"+ "og555S+C3eJAAVeNCTeMS3N/M5hzBRJAoffn3qoYdAO1Q8bTguOi+2849A=="+ "-----END CERTIFICATE-----/n"; public static void main(String[] args) throws Exception { // 使用傳輸層安全協(xié)議TLS(transport layer secure) SSLContext sslContext = SSLContext.getInstance("TLS"); //使用SRCA.cer 文件的形式//FileInputStream certInputStream = new FileInputStream(new File("srca.cer"));//也可以通過RFC 字符串的形式使用證書ByteArrayInputStream certInputStream = newByteArrayInputStream(CERT_12306_RFC.getBytes());// 初始化keyStore,用來導(dǎo)入證書KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());//參數(shù)null 表示使用系統(tǒng)默認(rèn)keystore,也可使用其他keystore(需事先將srca.cer 證書導(dǎo)入keystore 里)keyStore.load(null);//通過流創(chuàng)建一個(gè)證書Certificate certificate = CertificateFactory.getInstance("X.509").generateCertificate(certInputStream);// 把srca.cer 這個(gè)證書導(dǎo)入到KeyStore 里,別名叫做srcakeyStore.setCertificateEntry("srca", certificate);// 設(shè)置使用keyStore 去進(jìn)行證書校驗(yàn)TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init(keyStore);//用我們?cè)O(shè)定好的TrustManager 去做ssl 通信協(xié)議校驗(yàn),即證書校驗(yàn)sslContext.init(null, trustManagerFactory.getTrustManagers(), null);HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());URL url = new URL("https://kyfw.12306.cn/otn/");HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();InputStream in = conn.getInputStream();System.out.println(Util.inputstream2String(in));} Android 里的https 請(qǐng)求:
把scra.cer 文件考到assets 或raw 目錄下,或者直接使用證書的RFC 格式,接下來的做法和java工程代碼一樣
//ByteArrayInputStream in = new ByteArrayInputStream("rfc".getBytes());CertificateFactory cf = CertificateFactory.getInstance("X.509");InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));Certificate ca;try { ca = cf.generateCertificate(caInput); System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());} finally { caInput.close();}String keyStoreType = KeyStore.getDefaultType();KeyStore keyStore = KeyStore.getInstance(keyStoreType);keyStore.load(null, null);keyStore.setCertificateEntry("ca", ca);String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);tmf.init(keyStore);SSLContext context = SSLContext.getInstance("TLS");context.init(null, tmf.getTrustManagers(), null);URL url = new URL("https://certs.cac.washington.edu/CAtest/");HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();urlConnection.setSSLSocketFactory(context.getSocketFactory());InputStream in = urlConnection.getInputStream();copyInputStreamToOutputStream(in, System.out); 雙向證書驗(yàn)證
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());keyStore.load(null);SSLContext sslContext = SSLContext.getInstance("TLS");TrustManagerFactory trustManagerFactory = TrustManagerFactory. getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init(keyStore);//初始化keystoreKeyStore clientKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());clientKeyStore.load(getAssets().open("client.bks"), "123456".toCharArray());KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());keyManagerFactory.init(clientKeyStore, "123456".toCharArray());sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom()); Nogotofail
網(wǎng)絡(luò)流量安全測試工具,Google的開源項(xiàng)目:https://github.com/google/nogotofail
新聞熱點(diǎn)
疑難解答
圖片精選