OpenSSL 3.0.2 报 dh key too small 的问题

问题复现

运行命令 curl 访问一个 https 网站,可能会出现 "dh key too small" 的问题。

复制代码
> curl -v --insecure  https://some_web_site
*   Trying 175.21.4.7:443...
* Connected to some_web_site (175.21.4.7) port 443 (#0)
* ALPN: offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.0 (IN), TLS handshake, Certificate (11):
* TLSv1.0 (IN), TLS handshake, Server key exchange (12):
* TLSv1.0 (OUT), TLS alert, handshake failure (552):
* OpenSSL/3.0.15: error:0A00018A:SSL routines::dh key too small
* Closing connection 0
curl: (35) OpenSSL/3.0.15: error:0A00018A:SSL routines::dh key too small

原因是服务器比较老,SSL 协议中使用了较短的 dh key。 而客户端的 openssl 版本较新,默认的安全级别高,当握手时,发现对方的服务器使用了 短的 dh key,就拒绝进一步连接,认为该服务器是不安全的。

然而,现存有大量这样的服务器。由于无法更改服务器的配置,因此不得不降低客户端openssl的安全策略。

通常的解决办法( openSSL 3.0 以下)

这个情况实际上已经出现很多年了,大部分的解决方案是修改 openssl 的配置文件 openssl.cnf

将 openssl_conf 修改如下

复制代码
openssl_conf = default_conf

并在 文件最后添加下面

复制代码
[default_conf]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT@SECLEVEL=1

更详细的解释可参考
https://askubuntu.com/questions/1233186/ubuntu-20-04-how-to-set-lower-ssl-security-level

新的解决办法( openSSL 3.0 及以上)

然而,对于 OpenSSL 3.0 以上的系统,例如 Debian 12 自带的是 openSSL 3.0.15, 则老的解决办法就不灵了。需要用另外一种方式。与老的方法差别体现在 【system_default_sect】段的定义

复制代码
[system_default_sect]
CipherString = 'DEFAULT:!DH'
复制代码
核心要义是 CipherString = 'DEFAULT:!DH'

问题解决后的效果

复制代码
$ curl -v --insecure  https://some_web_site
*   Trying 175.21.4.7:443...
* Connected to some_web_site (175.21.4.7) port 443 (#0)
* ALPN: offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.0 (IN), TLS handshake, Certificate (11):
* TLSv1.0 (IN), TLS handshake, Server finished (14):
* TLSv1.0 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.0 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.0 (OUT), TLS handshake, Finished (20):
* TLSv1.0 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1 / AES256-SHA
* ALPN: server did not agree on a protocol. Uses default.
* Server certificate:
*  subject: CN=some_web_site
*  start date: Jul 27 09:38:07 2022 GMT
*  expire date: Jul 24 09:38:07 2032 GMT
*  issuer: CN=some_web_site
*  SSL certificate verify result: self-signed certificate (18), continuing anyway.
* using HTTP/1.x
> GET / HTTP/1.1
> Host: some_web_site
> User-Agent: curl/7.88.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Thu, 20 Mar 2025 15:15:20 GMT
< Server: Apache
< Last-Modified: Mon, 25 Jul 2011 04:02:56 GMT
< ETag: "18b-4a8dce3dc9c00"
< Accept-Ranges: bytes
< Content-Length: 395
< Content-Type: text/html
< 
<!DOCTYPE html
  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="0;URL=svn/" http-equiv="refresh"/>
<title>VisualSVN Server</title>
</head>
<body>
<h1>Welcome to VisualSVN Server!</h1>
<p><a href="/svn/">Repositories</a></p>
</body>
</html>
* Connection #0 to host some_web_site left intact

受影响的范围

这个问题影响到通过 https:// 对外提供服务的各种老的应用。例如: 通过 https 的方式对外服务的 svn 或者 gitLab 服务。 如果服务器上的 openssl 安全设置配置较低,则会出现这个问题。但是,svn, git 等客户端的报错不一定会出现 "dh key too small" 的字样。可能是笼统的提示 SSL communication 错误。此时,通过 curl 命令,能够比较清晰的显示这个错误内容,如本文最前面所示。

关于这个问题的详细解释请参考:
[S3 Connection Error `dh key too small` in Ubuntu 20.04 (1058) · Snippets · GitLab](https://git.rwth-aachen.de/-/snippets/1058 "S3 Connection Error `dh key too small` in Ubuntu 20.04 (1058) · Snippets · GitLab ")

相关推荐
南枝异客34 分钟前
CentOS 7 网络连接问题
linux·运维·centos
牛奶咖啡131 小时前
实现Linux的ssh免密登录实操保姆级教程
linux·ssh·生成ssh密钥对的三种方法·添加公钥到需ssh免登录服务器·测试ssh免登录的服务器·生产环境linux的优化策略
zhangrelay2 小时前
操作系统全解析:Windows、macOS与Linux的深度对比与选择指南(AI)
linux·笔记·学习
阿方索4 小时前
Linux 正则表达式
linux·运维
金色熊族4 小时前
ubuntu20.04编译qt源码5.15.3
linux·c++·qt
焚膏油以继晷,恒兀兀以穷年4 小时前
git 基础命令学习
git·版本管理工具
一只游鱼4 小时前
大文件推送到git仓库
git·git lfs
zhaotiannuo_19985 小时前
【Linux CentOS 7 版本更换yum源】
linux
aitav06 小时前
⚡ WSL2 搭建 s5p6818 Linux 嵌入式开发平台(part 1):环境准备与架构设计
linux·嵌入式·wsl·wsl2
一袋米扛几楼987 小时前
【软件安全】fgets / strncpy / gets(不安全) / snprintf的对比
linux·服务器·安全