首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何信任cURL命令行中的自签名证书?

如何信任cURL命令行中的自签名证书?
EN

Unix & Linux用户
提问于 2018-06-21 23:20:02
回答 7查看 191.5K关注 0票数 76

我使用这个Makefile为foo.localhost创建了一个使用让我们加密推荐的自签名证书:

代码语言:javascript
运行
复制
include ../.env

configuration = csr.cnf
certificate = self-signed.crt
key = self-signed.key

.PHONY: all
all: $(certificate)

$(certificate): $(configuration)
    openssl req -x509 -out $@ -keyout $(key) -newkey rsa:2048 -nodes -sha256 -subj '/CN=$(HOSTNAME)' -extensions EXT -config $(configuration)

$(configuration):
    printf "[dn]\nCN=$(HOSTNAME)\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:$(HOSTNAME)\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth" > $@

.PHONY: clean
clean:
    $(RM) $(configuration)

然后,我将其分配给一个web服务器。我已经验证服务器返回相关证书:

代码语言:javascript
运行
复制
$ openssl s_client -showcerts -connect foo.localhost:8443 < /dev/null
CONNECTED(00000003)
depth=0 CN = foo.localhost
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = foo.localhost
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:/CN=foo.localhost
   i:/CN=foo.localhost
-----BEGIN CERTIFICATE-----
[…]
-----END CERTIFICATE-----
---
Server certificate
subject=/CN=foo.localhost
issuer=/CN=foo.localhost
---
No client certificate CA names sent
Peer signing digest: SHA512
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 1330 bytes and written 269 bytes
Verification error: unable to verify the first certificate
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES128-GCM-SHA256
    Session-ID: […]
    Session-ID-ctx: 
    Master-Key: […]
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    TLS session ticket:
    […]

    Start Time: 1529622990
    Timeout   : 7200 (sec)
    Verify return code: 21 (unable to verify the first certificate)
    Extended master secret: no
---
DONE

如何使cURL信任它而不修改/etc中的任何内容?--cacert不工作,大概是因为没有CA:

代码语言:javascript
运行
复制
$ curl --cacert tls/foo.localhost.crt 'https://foo.localhost:8443/'
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

目标是在开发过程中启用HTTPS:

  • 如果没有大量的工作在所有开发环境中启用DNS验证,我就不可能有一个完全类似于生产的证书。因此,我必须使用自签署的证书。
  • 显然,我仍然希望使我的开发环境尽可能类似于生产,所以我不能简单地忽略任何和所有的证书问题。在这种情况下,curl -k就像catch (Exception e) {}一样--一点也不像浏览器与web服务器对话。

换句话说,在运行curl [something] https://project.local/api/foo时,我希望确信

  1. 如果正确地将TLS配置为具有自签名证书的except,则命令将成功,并且
  2. 如果我的TLS配置<#>except存在自签名证书的问题,那么命令将失败。

使用HTTP或--insecure不能满足第二个条件。

EN

回答 7

Unix & Linux用户

回答已采纳

发布于 2020-04-24 20:12:26

我有这个问题,完全相同的问题和错误消息,但是我使用GNUTLS的certtool来生成我的cert,而不是openssl。

我的问题是,我没有使我自己签署证书为CA。它只被配置为充当web服务器证书。这就是我想要做的,我不打算用它作为一个CA来签署其他证书。

但是,当您想要将证书作为其他证书的颁发者添加到信任链中时,该证书必须是CA,否则将被openssl拒绝!

certtool -i < mycert.crt中,人们需要看到以下内容:

代码语言:javascript
运行
复制
    Extensions:
            Basic Constraints (critical):
                    Certificate Authority (CA): TRUE

尝试将-addext basicConstraints=critical,CA:TRUE,pathlen:1添加到openssl命令中,或以同样的效果修改cnf文件。

或者,使用certtool,这对于一次性证书生成来说要容易得多:

代码语言:javascript
运行
复制
certtool -p --outfile localhost.key
certtool -s --load-privkey localhost.key --outfile localhost.crt

然后回答提供证书的CN等提示。当被问到是否为证书颁发机构时,请说是!

票数 10
EN

Unix & Linux用户

发布于 2018-10-16 23:55:31

试试-k

代码语言:javascript
运行
复制
curl -k https://yourhost/

它应该“接受”自签署的证书。

票数 64
EN

Unix & Linux用户

发布于 2018-09-11 19:19:32

遵循以下步骤可以解决您的问题:

  1. 下载并保存自签名证书:echo quit | openssl s_client -showcerts -servername "${API_HOST}" -connect "${API_HOST}":443 > cacert.pem
  2. 告诉curl客户端:curl --cacert cacert.pem --location --silent https://${API_HOST}

此外,还可以使用wget和忽略使用:wget --no-check-certificate https://${API_HOST}的证书。

票数 17
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/451207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档