Skip to content

使用 openssl 进行 tls 证书认证,支持自动格式化数据#139

Open
Soybean01 wants to merge 2 commits into
tzfun:masterfrom
Soybean01:feature_auto_format
Open

使用 openssl 进行 tls 证书认证,支持自动格式化数据#139
Soybean01 wants to merge 2 commits into
tzfun:masterfrom
Soybean01:feature_auto_format

Conversation

@Soybean01

Copy link
Copy Markdown

作者你好!非常感谢这个项目,etcd 的可视化操作让我工作效率大大提高~
但是我在使用的过程中遇到了如下两个问题:

  1. 我们 etcd 使用证书加密,但是我发现当前项目使用 rust tls 进行加密认证,无法连接到我们的 etcd,我修改成了 openssl 之后就可以了;
  2. 我们很多数据在查看的时候需要自己手动点击一下格式化,这样每次会多一步操作,因此设置了自动格式化

@tzfun

tzfun commented Mar 18, 2026

Copy link
Copy Markdown
Owner

感谢你的提交,自动格式化功能可以合并,对于openssl的引入我需要先确认一下 rust-tls 无法连接的原因,你可以提供一下认证密钥生成的算法吗?

openssl之前我有尝试过,但出于以下几个原因没有开放:

  1. 打包环境依赖openssl库,不同平台的打包需要部署openssl,当时觉得有点麻烦,如果本次引入,打包环境以及docker配置也需要相应调整
  2. openssl开发版本不好控制
  3. openssl能做的 rusttls也都能做(除了一些被标记为不安全的算法密钥),且rusttls体积更小

鉴于以上原因我还是希望能找到rust tls在你的环境无法连接的原因,如果这实在不能解决再考虑引入openssl吧。我看你的代码中加了一个 filter_pem 函数,对 EC PRIVATE KEY 做了处理,是因为这种key不能被rust tls正确读取吗?

@Soybean01

Copy link
Copy Markdown
Author

大佬你好,我们公司这边的 etcd 使用的是 X.509 v1 版本的证书,rustls 不支持该版本,我给切换成了 openssl 之后可以运行了,兼容性更强一些~

@tzfun tzfun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

经查阅资料, X.509 v1 版本的证书确实无法被 rust-tls 使用,从安全角度官方更推荐 v3 版本,但从兼容性角度来讲,Etcd工具理应兼容老的密钥和证书,所以我决定还是引入openssl吧。

除了你的PR之外我还需要做兼容性测试和多环境打包等一系列调整,所以正式发布openssl版本可能还需要一段时间。

我review了你的代码,其中问题麻烦处理一下~

tls_option =
tls_option.identity(Identity::from_pem(identity.cert, identity.key));
let cert = filter_pem(&identity.cert, "CERTIFICATE");
let key = filter_pem(&identity.key, "PRIVATE KEY");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么需要手动过滤掉 PEM 中的标识块?底层工具可以自动处理这些标识,并且去掉了key的标识信息可能会导致无法读取对应的算法,例如:

ssl工具根据标识块信息得知密钥算法为RSA,需要以 PKCS#1 格式解析其中内容

-----BEGIN RSA PRIVATE KEY-----
xxx
-----END RSA PRIVATE KEY-----

同理,你在filter_pem 函数中兼容的 EC PRIVATE KEY 块中EC也可能是有用信息,另外还有 Ed25519 等。

建议在交生成 identity 之前不要手动处理 PEM 文件内容,这可能存在兼容性问题

openssl_config = openssl_config
.client_cert_pem_and_key(identity.cert.as_slice(), identity.key.as_slice());
let cert = filter_pem(&identity.cert, "CERTIFICATE");
let key = filter_pem(&identity.key, "PRIVATE KEY");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也和前面 rust-tls 处理一样,PEM文件交由下层工具处理,除非下层无法正确处理PEM文件

Comment on lines +1246 to +1258
let mut in_block = false;
for line in content.lines() {
if line.contains(&begin_tag) {
in_block = true;
}
if in_block {
filtered.push_str(line);
filtered.push('\n');
}
if line.contains(&end_tag) {
in_block = false;
}
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里存在一个可能的漏洞,当PEM中存在多个密钥段时,可能会读取到错误的密钥内容,例如这样的文件:

-----BEGIN PRIVATE KEY-----
xxx
-----END PRIVATE KEY-----

-----BEGIN PUBLIC KEY-----
xxx
-----END PUBLIC KEY-----

-----BEGIN CERTIFICATE-----
xxx
-----END CERTIFICATE-----

-----BEGIN PRIVATE KEY-----
xxx
-----END PRIVATE KEY-----

当然如果此函数可以移除,这个问题可以忽略

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants