0

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

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

首先,我在服务器上尝试了具有相同结果的 SocketRocket 库——良好握手后的 MAC 错误。我的 Unitt 客户端的 android 版本运行良好。我的 JavaScript 安全 Web 套接字代码也很好用。

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

4

1 回答 1

0

通过过滤掉某些与 iOS 不兼容的椭圆曲线密码套件,我能够让我的 iOS 安全 Web 套接字与我的 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);
  }
}
于 2012-06-30T21:06:39.363 回答