Openssl-无法设置使用libssl(C++)时要使用的特定TLS版本



我想在C++中使用openssl lib 1.1.1k时,只使用特定的TLS版本连接到服务器。以下是我尝试实现以下目标的客户端代码片段:

m_sslContext = SSL_CTX_new(TLSv1_2_client_method());
if(!m_sslContext) {
return -1;
}
int ret = SSL_CTX_set_min_proto_version(m_sslContext, TLS1_2_VERSION);
LOG(INFO, "[SSL] Setting min proto version- ", ret);
ret = SSL_CTX_set_max_proto_version(m_sslContext, TLS1_2_VERSION);
LOG(INFO, "[SSL] Setting max proto version- ", ret);
LOG(INFO, "[SSL] Using SSL VERSION - ", SSL_CTX_get_min_proto_version(m_sslContext));
LOG(INFO, "[SSL] Using SSL VERSION - ", SSL_CTX_get_max_proto_version(m_sslContext));

这个片段的输出如下:

20220829-13:21:40.279623591 INFO: [SSL] Setting min proto version- 1
20220829-13:21:40.279624064 INFO: [SSL] Setting max proto version- 1
20220829-13:21:40.279624191 INFO: [SSL] Using SSL VERSION - 0
20220829-13:21:40.279624544 INFO: [SSL] Using SSL VERSION - 0

不确定这里出了什么问题。建立的连接使用TLS1.3:

* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256

它确实支持TLS1.2,因为openssl命令:

openssl s_client -tls1_2 -cipher AES128-SHA -connect host:ip

openssl-lib是在支持TLS1.2的情况下编译的。有人能帮我弄清楚这里发生了什么吗?提前谢谢。

编辑:更多信息。我尝试使用-SSL_get_version()打印正在使用的SSL版本,它显示-

[SSL] Using SSL VERSION - TLSv1.2

但最终连接仍然使用TLS1.3。这里的另一点是,我正在进行HTTP1.1/2连接。

m_sslContext = SSL_CTX_new(TLSv1_2_client_method());

TLSv1_2_client_method是不推荐使用的,使用适当的编译标志也会这样说(文档中也这样说(:

x.c:9:5: warning: ‘TLSv1_2_client_method’ is deprecated [-Wdeprecated-declarations]
9 |     m_sslContext = SSL_CTX_new(TLSv1_2_client_method());

当使用TLS_client_method时,编译警告消失,代码执行预期操作:

[SSL] Setting min proto version- 1
[SSL] Setting max proto version- 1
[SSL] Using SSL VERSION - 771
[SSL] Using SSL VERSION - 771

但为什么TLSv1_2_client_method失败了呢?查看源代码,最终得到ssl/statem/statem_lib.c中的ssl_set_version_bound,其中包含前导注释:

* ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
* protocols, provided the initial (D)TLS method is version-flexible.  This
* function sanity-checks the proposed value and makes sure the method is
* version-flexible, then sets the limit if all is well.

TLSv1_2_client_method的版本显然不灵活,因此该功能在这里没有任何作用。但为什么它会获得成功呢?评论中还解释道:

* We ignore attempts to set bounds on version-inflexible methods,
* returning success.

最新更新