移动应用安全开发指南(Android)--数据传输

概述

移动应用很多时候并非孤立存在,在多数场景下存在前、后台以及第三方服务之间进行数据交互,因此,在网络中传输敏感数据在所难免,如果不使用正确安全的传输方式,有可能存在敏感信息泄漏的风险。

安全准则

A.      使用SSL协议(或基于SSL的协议)传输敏感数据。

B.      验证服务器证书的有效性,防止中间人攻击,当证书有效性验证失败时应强行断开连接,而不是提示用户。

C.      尽可能地减少使用短信发送敏感信息。

D.      增强项:应用程序可以对敏感数据进行一次独立加密后再通过SSL进行传输。

详细描述

A.      可以使用“证书锁定”(certificate pinning)的方式验证证书的有效性,即在代码中精确的验证当前服务器是否持有某张指定的证书。X509TrustManager接口是实现证书锁定一种方法,它通过在SSL回调函数中读取服务器证书密钥并和程序预埋的证书密钥进行对比,如果两者不一致则强行断开链接(参考附录2)。

备注

A.      任何申请了READ_SMS permission的应用都可以读取短信内容,故应尽量减少使用短信发送敏感信息。

B.      应用自身进行一次独立加密的好处是,当出现SSL协议相关漏洞(比如影响巨大的openssl heart bleed漏洞)时能够有效减少损失。

C.      “证书锁定”的缺点是:由于证书是预埋在应用程序内的,是当服务器更新证书时,应用程序也要同步更新。

注:如果IE显示格式不正确,请使用chrome浏览器

附录2:

使用“证书锁定”验证证书有效性方案:

/* 使用https需要定义实现X509TrustManager接口的类,并重写里面的方法,这些方法会在建立SSL链接的过程中被自动调用 */

public final class PubKeyManager implements X509TrustManager

{

         /* 此处存放服务器证书密钥 */

  private static String PUB_KEY =

                       "30820122300d06092a864886f70d01010105000382010f"

                            + "003082010a0282010100973a0569971991dc9446f309ec0af7646377dca80eb1"

                            + "e1357f5fb5c69d046d03d23f6cf743d155e7b44d834cf71d6500a8b1e38110b5"

                            + "35ad07212a50e1f3ab497acfde74e065018d64136d14d63d04604124aacd74ea"

                            + "037a5f8d6894aadd58f7774655761c9fba22426935794f6740fa89b6f7e902b8"

                            + "e02c0b842116272701c9edaef62c977b0df488847c2e0a75028865be34c98903"

                            + "be11a2cf4495acec5c958ddf64619808e641bac64ab432e4638e969f4214d7bc"

                            + "88db81feaef4d5d329f93f2e79535b2d00c01145823b664ca7ab8db9de858d29"

                            + "161c6cce86decb6a4f66cf86afb79e182a5b5a2bb6d8795af749f7af356aef2e"

                            + "64b6ae785ff88d456f970203010001";

 

         /*  https协商中获取的服务器证书链(chain)将自动传入该方法 */

  public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException

  {

    if (chain == null) {

      throw new IllegalArgumentException("checkServerTrusted: X509Certificate array is null");

    }

 

    if (!(chain.length > 0)) {

      throw new IllegalArgumentException("checkServerTrusted: X509Certificate is empty");

}

 

         /* authType为建立SSL链接时使用的非对称加密算法,RSA为业界主流算法 */

    if (!(null != authType && authType.equalsIgnoreCase("RSA"))) {

      throw new CertificateException("checkServerTrusted: AuthType is not RSA");

    }

 

    /* 执行SSL/TLS常规性检查 */

    try {

      TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");

      tmf.init((KeyStore) null);

     

      for (TrustManager trustManager : tmf.getTrustManagers()) {

        ((X509TrustManager) trustManager).checkServerTrusted(chain, authType);

      }

    } catch (Exception e) {

      throw new CertificateException(e);

}

 

/* 将编码后的密钥转换成16进制形式的大整数 */

    RSAPublicKey pubkey = (RSAPublicKey) chain[0].getPublicKey();

    String encoded = new BigInteger(1, pubkey.getEncoded()).toString(16);

 

    /* 比较预埋证书密钥和服务器证书密钥是否一致 */

    final boolean expected = PUB_KEY.equalsIgnoreCase(encoded);

    if (!expected) {

      throw new CertificateException("checkServerTrusted: Expected public key: "

                + PUB_KEY + ", got public key:" + encoded);

      }

    }

  }

}