【问题标题】:cURL a local GO server using a self signed certificate使用自签名证书 cURL 本地 GO 服务器
【发布时间】:2020-05-04 14:04:13
【问题描述】:

所以我有一个简单的 GO 服务器在端口 8080 上运行,使用的是我使用以下命令创建的自签名证书:

openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out local.crt -keyout local.key

创建它时,我将字段设置为以下值:
如您所见,我跳过了除fully qualified host name 之外的所有内容,我将其设置为go-auth

我使用 local.keylocal.crt 文件成功启动了我的 go 服务器。

我尝试过 cURLing 如下:

➜  certs git:(master) ✗ curl --proxy-cacert local.crt https://go-auth/
curl: (6) Could not resolve host: go-auth

➜  certs git:(master) ✗ curl --proxy-cacert local.crt https://localhost:8080/
curl: (60) SSL certificate problem: self signed 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.

之后我尝试从正在运行的服务器获取证书并将其保存到 cacert.pem 文件并再次尝试:

➜  certs git:(master) ✗ echo quit | openssl s_client -showcerts -servername go-auth -connect localhost:8080 > cacert.pem
depth=0 CN = go-auth
verify error:num=18:self signed certificate
verify return:1
depth=0 CN = go-auth
verify return:1
DONE

➜  certs git:(master) ✗ curl --cacert cacert.pem https://go-auth/
curl: (6) Could not resolve host: go-auth

➜  certs git:(master) ✗ curl --proxy-cacert cacert.pem https://go-auth/
curl: (6) Could not resolve host: go-auth

此时我不知道,我试图按照这个问题的答案:Use self signed certificate with cURL? 但没有得到想要的结果。

【问题讨论】:

  • “无法解析主机”是 DNS 错误,而不是与证书相关的错误。您需要首先确保您的 go-auth 名称在您的系统上正确解析,例如使用 /etc/hosts 或等效名称。但这与编程无关。
  • 试试这个curl --cacert cacert.pem https://localhost:8080/--proxy-cacert 用于HTTPS 代理
  • 尝试使用标志 --insecure 禁用 curl 的证书验证。您可以查看github.com/FiloSottile/mkcert 中的mkcert 工具来生成本地CA 和本地开发证书。
  • 根据你拥有的卷发,你在本地主机上运行它。您的主机名不是go-auth。即使你使用正确的证书与curl,它也会失败,因为主机名不是go-auth,所以你必须使用localhost对其进行curl,使用curl -k来禁用证书验证。

标签: ssl curl tls1.2


【解决方案1】:

您可以使用-k 参数来跳过证书验证。

您的命令必须类似于以下命令:

curl -vk https://localhost:8080/
  • -v启用一些调试信息
  • -k 禁用证书验证

如果要启用证书验证,有两种方法:

  • 将证书添加并信任到您当前的 CA 列表
    通过这种方式,您将“接受”您的自签名证书作为有效证书,并且您将能够使用任何类型的 HTTP 客户端(Java、Go、cURL 等)调用服务(显然是从您的机器上) )。

  • 使用cURL 命令的--cacert 参数 指定与证书相关的路径,以便对服务进行身份验证。

【讨论】:

  • 好的,这次我让它工作了,但是有没有办法避免使用 --insecure 来攻击我的本地服务器?
猜你喜欢
  • 2015-03-04
  • 2023-04-07
  • 2011-08-15
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 2015-02-21
相关资源
最近更新 更多