【问题标题】:OkHttp client certificate without intermediaries无中介的 OkHttp 客户端证书
【发布时间】:2021-02-10 17:19:21
【问题描述】:

我正在尝试使用okhttp 对服务器进行身份验证。

curl 会这样:

curl \
--cert certificate.cer \
--key private-key.pkcs8 \
"https://some-url"

不幸的是,okhttp-tls 似乎总是期望除了持有的证书之外还有一系列证书,这不是我所拥有的。

heldCertificate 需要证书链,除了 clientCertificate,与 reame 中提供的示例相反:

HandshakeCertificates clientCertificates = new HandshakeCertificates.Builder()
    .addTrustedCertificate(rootCertificate.certificate())
    .heldCertificate(clientCertificate) // <--------------------
    .build();

如何将okhttp 与单个证书和我的私钥一起使用?

【问题讨论】:

    标签: okhttp mutual-authentication


    【解决方案1】:

    heldCertificates 期望可能存在于客户端证书(匹配私钥)和直到但不包括服务器已知信任的根 CA 之间存在的中间链。因此,如果不需要其他证书,则可以省略。

    https://square.github.io/okhttp/4.x/okhttp-tls/okhttp3.tls/-handshake-certificates/-builder/held-certificate/

    配置认证时使用的证书链。这 第一个证书是持有的证书,其他证书是 包含在握手中,因此对等方可以建立一条可信路径 受信任的根证书。

    链应包括所有中间证书,但不包括 需要我们希望远程知道的根证书 同行。对等方已经拥有该证书,因此传输它是 没必要。

    以下是一些使用自签名证书的示例(如果已知服务器已信任该证书),以及根据所连接的主机切换所使用的密钥的示例。您需要根据您的确切设置来调整这些,但它们应该为您提供测试的起点。

    https://github.com/square/okhttp/pull/6470/files

    【讨论】:

    • 感谢您的回答。但是,在查看您的示例后,我遇到了同样的问题:HandshakeCertificates.BuilderheldCertificate 方法不接受单个参数。当省略中间证书时,我得到“没有匹配的方法heldCertificate 为类 okhttp3.tls.HandshakeCertificates$Builder 找到 1 个参数”。
    • 您可以尝试使用空数组吗?它应该是一个可变参数
    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2020-11-23
    • 2011-09-28
    相关资源
    最近更新 更多