【问题标题】:Multi-threaded SSL / TLS version switching多线程 SSL/TLS 版本切换
【发布时间】:2018-06-14 03:08:25
【问题描述】:

我们有 asp.net mvc 使用的 Wcf 服务(核心服务)。该核心服务与其他第三方服务集成以实现一些用例。所有这些第三方服务端点都通过https 公开,但在支持的 SSL/TLS 版本(1.0、1.1、1.2)方面有所不同。

我们对此没有太多控制权,只是在连接任何给定的第三方服务之前粘贴特定的(受支持的)SSL/TLS 版本。即,每当核心服务想要使用任何第三方服务时,它将ServicePointManager.SecurityProtocol 设置为支持的 SSL/TLS 版本(第三方要求我们使用 x.x 或更高版本进行连接)。

    // brief expression of logic which switch TLS version 1.2
    var currentSecurityProtocol = ServicePointManager.SecurityProtocol; // get the current security protocol.
    try
    {

        ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        //now get/post data to/from third party service
    }
    catch (Exception ex)
    {
       //report it
    }
    finally
    {
        ServicePointManager.SecurityProtocol = currentSecurityProtocol;
        ServicePointManager.ServerCertificateValidationCallback = null;
    }

问题:

  1. 如何重新实现上述TLS版本切换逻辑才能工作 在多线程环境中无缝连接?

我设法编写了通过锁定ServicePointManager.SecurityProtocol 属性来同步访问的逻辑。但是这种尝试会影响其他连接客户端的性能(当他们使用任何其他第三方产品/服务时)

【问题讨论】:

  • 多线程不会影响 TLS 的工作方式。 ServicePointManager.SecurityProtocol 是一个标志,带有您愿意接受的 TLS 版本。服务器和客户端将协商并选择双方都可接受的最高版本。
  • 顺便说一句,您应该至少需要 TLS11。 1.0 不被认为是安全的。您应该考虑要求 TLS12,除非对方有重大理由仅在短时间内降级。航空公司、银行、谷歌等已经需要 TLS1.2

标签: c# asp.net .net asp.net-mvc wcf


【解决方案1】:

您可以指定多个版本的 Tls,这样您就不需要在运行时更改它。下面的代码表示它支持TLS1.0TLS1.1TLS1.2

如果客户端支持TLS1.2或以下,则使用TLS1.2创建连接

如果客户端支持TLS1.1或以下,则使用最高可用的安全协议TLS1.1

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

【讨论】:

  • 提前感谢您的回答。但我不清楚这将如何在多线程场景中工作,因为 ServicePointManager.SecurityProtocol 是一个静态(应用程序域/范围内的全局)变量。你能进一步解释一下这种方法对我有什么帮助吗?
  • @Nair 多线程与使用的协议版本无关。 ServicePointManager.SecurityProtocol 是允许的最低 TLS 版本。客户端和服务器都协商版本从最高开始
  • 假设我有 2 个服务 A 和 B,其中 A 支持的最大 TLS 版本为 1.0,B 为 1.2。现在,如果我的核心服务并行调用服务 A 和 B,其中 A 先被调度(执行)然后 B。在这种情况下,很可能会出现数据竞争问题(版本字段是静态的)。因此,A 的 TLS 版本现在可能是 1.2,在我们将数据上传/发布到第三方服务器时将不支持。
  • 通过示例代码,将使用 1.0 和 1.2 创建两个连接
  • @RawitasKrungkaew,已接受答案。我对切换和版本协商有一个错误的想法,因此有点混乱。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-04-03
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 2014-12-12
  • 2014-12-12
  • 2017-10-22
相关资源
最近更新 更多