标签:
Android软件通常使用WIFI网络与服务器进行通信。WiFi并非总是可靠的,例如,开放式网络或弱加密网络中,接入者可以监听网络流量;攻击者可能 自己设置WIFI网络钓鱼。此外,在获得root权限后,还可以在Android系统中监听网络数据。
1 public static HttpClient getWapHttpClient() { 2 3 try { 4 5 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 6 7 trustStore.load(null, null); 8 9 SSLSocketFactory sf = new MySSLSocketFactory(trustStore); 10 11 sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 12 13 //此处信任手机中的所有证书,包括用户安装的第三方证书 14 15 HttpParams params = new BasicHttpParams(); 16 17 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 18 19 HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 20 21 SchemeRegistry registry = new SchemeRegistry(); 22 23 registry.register(new Scheme(“http”, PlainSocketFactory.getSocketFactory(), 80)); 24 25 registry.register(new Scheme(“https”, sf, 443)); 26 27 ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 28 29 return new DefaultHttpClient(ccm, params); 30 31 } catch (Exception e) { 32 33 return new DefaultHttpClient(); 34 35 } 36 37 }
而在客户端中覆盖google默认的证书检查机制(X509TrustManager),并且在代码中无任何校验SSL证书有效性相关代码:
1 public class MySSLSocketFactory extends SSLSocketFactory { 2 3 SSLContext sslContext = SSLContext.getInstance(“TLS”); 4 5 public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { 6 7 super(truststore); 8 9 TrustManager tm = new X509TrustManager() { 10 11 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 12 13 } 14 15 //客户端并未对SSL证书的有效性进行校验,并且使用了自定义方法的方式覆盖android自带的校验方法 16 17 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 18 19 } 20 21 public X509Certificate[] getAcceptedIssuers() { 22 23 return null; 24 25 } 26 27 }; 28 29 sslContext.init(null, new TrustManager[] { tm }, null); 30 31 } 32 }
1 public final class PubKeyManager implements X509TrustManager{ 2 private static String PUB_KEY = "30820122300d06092a864886f70d0101" + "0105000382010f003082010a0282010100b35ea8adaf4cb6db86068a836f3c85" +"5a545b1f0cc8afb19e38213bac4d55c3f2f19df6dee82ead67f70a990131b6bc" + "ac1a9116acc883862f00593199df19ce027c8eaaae8e3121f7f329219464e657" +"2cbf66e8e229eac2992dd795c4f23df0fe72b6ceef457eba0b9029619e0395b8" + "609851849dd6214589a2ceba4f7a7dcceb7ab2a6b60c27c69317bd7ab2135f50" +"c6317e5dbfb9d1e55936e4109b7b911450c746fe0d5d07165b6b23ada7700b00" + "33238c858ad179a82459c4718019c111b4ef7be53e5972e06ca68a112406da38" + "cf60d2f4fda4d1cd52f1da9fd6104d91a34455cd7b328b02525320a35253147b" + "e0b7a5bc860966dc84f10d723ce7eed5430203010001"; 3 4 //锁定证书公钥在apk中 5 6 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException 7 8 { 9 10 if (chain == null) { 11 12 throw new IllegalArgumentException("checkServerTrusted: X509Certificate array is null"); 13 14 } 15 16 if (!(chain.length > 0)) { 17 18 throw new IllegalArgumentException("checkServerTrusted: X509Certificate is empty"); 19 20 } 21 22 if (!(null != authType && authType.equalsIgnoreCase("RSA"))) { 23 24 throw new CertificateException("checkServerTrusted: AuthType is not RSA"); 25 26 } 27 28 // Perform customary SSL/TLS checks 29 30 try { 31 32 TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); 33 34 tmf.init((KeyStore) null); 35 36 for (TrustManager trustManager : tmf.getTrustManagers()) { 37 38 ((X509TrustManager) trustManager).checkServerTrusted(chain, authType); 39 40 } 41 42 } catch (Exception e) { 43 44 throw new CertificateException(e); 45 46 } 47 48 // Hack ahead: BigInteger and toString(). We know a DER encoded Public Key begins 49 50 // with 0×30 (ASN.1 SEQUENCE and CONSTRUCTED), so there is no leading 0×00 to drop. 51 52 RSAPublicKey pubkey = (RSAPublicKey) chain[0].getPublicKey(); 53 54 String encoded = new BigInteger(1 /* positive */, pubkey.getEncoded()).toString(16); 55 56 // Pin it! 57 58 final boolean expected = PUB_KEY.equalsIgnoreCase(encoded); 59 60 if (!expected) { 61 62 throw new CertificateException("checkServerTrusted: Expected public key: " + PUB_KEY + ", got public key:" + encoded); 63 64 } 65 66 } 67 68 }
标签:
原文地址:http://www.cnblogs.com/chengzhengfu/p/4581683.html