iOS 破坏了发送到服务器的 TLS 数据

标签 ios websocket ssl

我在 iOS 上使用 SocketRocket 或 Unitt 网络套接字库执行安全网络套接字时遇到了同样的问题。我可以控制 Java 网络套接字服务器 - Netty - 并且可以详细观察服务器端 SSL/TLS 数据。我使用端口 6970 作为套接字。

在使用 TLS 握手正确打开客户端 wss 套接字后,我在第一次客户端数据写入时收到服务器错误,即网络套接字握手。客户端写202字节,服务端收到272字节,消息认证码在服务端失败。我在服务器跟踪中看到我的客户端数据,但它有 8 个字节的垃圾预挂。

首先,我在服务器上尝试了 SocketRocket 库,结果相同 - 良好握手后出现错误 MAC。我的 Android 版 Unitt 客户端运行良好。我的 JavaScript 安全网络套接字代码也运行良好。

在 iOS/XCode 上,我可以调试到 Unitt/AsyncSocket/doSendBytes 中的 CFWriteStreamWrite(...) - 它报告确实写入了 202 个客户端数据字节。但是我不知道如何进一步调试。在某个地方,iOS TLS 框架在发送过程中破坏了我的客户端数据——我想。

最佳答案

通过过滤掉某些与 iOS 不兼容的椭圆曲线密码套件,我能够让我的 iOS 安全网络套接字与我的 Java Netty 服务器一起运行。这是我的代码...

/** the enabled SSL cipher suites */
private static String[] enabledCipherSuites;
/** the enabled SSL cipher suites lock */
private static final Object ENABLED_CIPHER_SUITES_LOCK = new Object();
/** the iOS incompatible cipher suites */
private static final List<String> iOSIncompatibleCipherSuites = new ArrayList<>();

/** Configures the SSL engine for client or for the server. Arranges the enabled ciphers to favor the
* most secure over the less secure, and omits the least secure ciphers.  Requires that the SSL server
* authenticate the client.
*
* @param sslEngine the SSL engine
* @param useClientMode the indicator whether the SSL engine is operating in client mode
* @param needClientAuth the indicator whether the server authenticates the client's SSL certificate
*/
public static synchronized void configureSSLEngine(
         final SSLEngine sslEngine,
         final boolean useClientMode,
         final boolean needClientAuth) {
  //Preconditions
  assert sslEngine != null : "sslEngine must not be null";

  if (useClientMode) {
    LOGGER.info("configuring SSL engine for the client side of the connection");
    sslEngine.setUseClientMode(true);
    sslEngine.setNeedClientAuth(false);
  } else {
    if (needClientAuth) {
      LOGGER.info("configuring SSL engine for the server side of the connection with required client authorization");
    } else {
      LOGGER.info("configuring SSL engine for the server side of the connection without required client authorization");
    }
    sslEngine.setUseClientMode(false);
    sslEngine.setNeedClientAuth(needClientAuth);
 }
  synchronized (ENABLED_CIPHER_SUITES_LOCK) {
    if (enabledCipherSuites == null) {
      iOSIncompatibleCipherSuites.add("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384");
      iOSIncompatibleCipherSuites.add("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256");
      iOSIncompatibleCipherSuites.add("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA");
      iOSIncompatibleCipherSuites.add("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");

      // TLS_ECDHE_RSA_WITH_RC4_128_SHA is the negotiated cipher suite for iOS

      // select and arrange the highest security cipher suites and cache the result
      final String[] supportedCipherSuites = sslEngine.getSupportedCipherSuites();
      final List<String> enabledCipherSuitesList = new ArrayList<>(supportedCipherSuites.length);
      // The first pass selects 256 bit ciphers available with the Java Cryptography Extension (JCE)
      // Unlimited Strength Jurisdiction Policy Files, downloaded and installed from http://java.sun.com/javase/downloads/index.jsp .
      for (final String supportedCipherSuite : supportedCipherSuites) {
        if (supportedCipherSuite.contains("_256_") && !supportedCipherSuite.contains("_anon_")) {
          enabledCipherSuitesList.add(supportedCipherSuite);
        }
      }
      // The second pass selects 128 bit ciphers that use SHA hashing - its more secure than MD5 but slower.
      for (final String supportedCipherSuite : supportedCipherSuites) {
        if (supportedCipherSuite.contains("_128_") && !supportedCipherSuite.endsWith("_MD5") && !supportedCipherSuite.contains("_anon_")) {
          enabledCipherSuitesList.add(supportedCipherSuite);
        }
      }
      // The third pass selects 128 bit ciphers that use MD5 hashing.
      for (final String supportedCipherSuite : supportedCipherSuites) {
        if (supportedCipherSuite.contains("_128_") && supportedCipherSuite.endsWith("_MD5") && !supportedCipherSuite.contains("_anon_")) {
          enabledCipherSuitesList.add(supportedCipherSuite);
       }
      }
      // The fourth pass removes the iOS incompatible cipher suites
      enabledCipherSuitesList.removeAll(iOSIncompatibleCipherSuites);

      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("enabledCipherSuites: " + enabledCipherSuitesList);
      }
      final int enabledCipherSuitesList_size = enabledCipherSuitesList.size();
      enabledCipherSuites = new String[enabledCipherSuitesList_size];
      for (int i = 0; i < enabledCipherSuitesList_size; i++) {
        enabledCipherSuites[i] = enabledCipherSuitesList.get(i);
      }
    }
    sslEngine.setEnabledCipherSuites(enabledCipherSuites);
  }
}

关于iOS 破坏了发送到服务器的 TLS 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11271125/

相关文章:

ios - 适用于 IOS 的 Unity IAP — 未找到应用程序收据

bash - 如何使用 netcat/socat/telnet 手动连接到 websocket?

c - 使用 websocket 的 GWAN 双向通信

IOS 9 Ipad kCFStreamErrorDomainSSL, -9802,如何解决这个问题?

ssl - SSL 反向代理站点上的 Liferay Web 表单重新验证问题

ios - 如何在iOS应用中重置所有Facebook权限?

ios - 多对多关系中的 CoreData 排序

ios - 如何为 UIPageViewController 设置背景颜色?

javascript - 捕获WebSocket异常: "Error during WebSocket handshake: Unexpected response code: 503"

html - 奇怪的 SSL 问题