Daway.Huang(黄伟) 2024-02-28
1、问题描述
在执行"git clone"命令克隆网站上远程仓库时报告服务器证书验证失败,没有CRLfile。
(1)使用https方式clone 时
$ git clone https://github.com/brektrou/rtl8821CU.git
正克隆到 'rtl8821CU'...
fatal: 无法访问 'https://github.com/brektrou/rtl8821CU.git':server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
(2)使用ssh方式clone时
$ git clone git@gitee.com:daway/rtl8821CU.git
正克隆到 'rtl8821CU'...
kex_exchange_identification: read: Connection reset by peer
fatal: 无法读取远程仓库。
请确认您有正确的访问权限并且仓库存在。
2、原因分析
根本原因是计算机不信任对远程服务器(Gitlab/Gitee/Github)上使用的证书进行签名的证书颁发机构。这并不意味着证书可疑,它可以是自签名的,也可以由不在系统的CA列表中的机构/公司签名。
curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs).
#curl默认情况下使用“捆绑包”执行SSL证书验证,证书颁发机构(CA)公共密钥(CA证书)。
If the default bundle file isn't adequate, you can specify an alternate file using the --cacert option.
#如果默认捆绑文件不足,您可以指定备用文件使用--cacert选项。
If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might not match the domain name in the URL).
#如果此HTTPS服务器使用由表示为捆绑软件,证书验证可能由于证书有问题(证书可能已过期,或者名称可能与网址中的域名不匹配)。
If you'd like to turn off curl's verification of the certificate, use the -k (or --insecure) option.
#如果您要关闭curl对证书的验证,请使用 -k(或--insecure)选项。
3、解决方案
告诉计算机(系统)信任这个网站的证书(颁发机构),该方案能解决托管仓库网站证书的问题,且继续保证系统安全,相对复杂。
下面引自Pete Clark的话:
“如果在ubuntu机器上运行,则可能是Web服务器的CA证书不在/etc/ssl/certs/ca-certificates.crt文件中。我遇到了一个托管在Web服务器上的git服务器,该服务器具有由www.incommon.org签名的SSL证书。您可以将中间证书添加到ca-certificates文件中,如下所示:”
$ wget http://cert.incommon.org/InCommonServerCA.crt
$ openssl x509 -inform DER -in InCommonServerCA.crt -out incommon.pem
$ cat /etc/ssl/certs/ca-certificates.crt incommon.pem > ca-certs2.crt
$ sudo cp /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt.bak
$ sudo cp ca-certs2.crt /etc/ssl/certs/ca-certificates.crt
4、最终效果
通过执行以上解决方案中的5条命令,最终完美解决了本次证书信任的问题。git clone执行效果详见如下内容。
(1)使用https方式clone 正常
$ git clone https://github.com/brektrou/rtl8821CU.git
正克隆到 'rtl8821CU'...
remote: Enumerating objects: 1743, done.
remote: Total 1743 (delta 0), reused 0 (delta 0), pack-reused 1743
接收对象中: 100% (1743/1743), 7.73 MiB | 3.45 MiB/s, 完成.
处理 delta 中: 100% (717/717), 完成.
(2)使用ssh方式clone正常
$ git clone git@gitee.com:daway/rtl8821CU.git
正克隆到 'rtl8821CU'...
Warning: Permanently added the ECDSA host key for IP address '180.76.198.77' to the list of known hosts.
remote: Enumerating objects: 1743, done.
remote: Total 1743 (delta 0), reused 0 (delta 0), pack-reused 1743
接收对象中: 100% (1743/1743), 7.73 MiB | 4.43 MiB/s, 完成.
处理 delta 中: 100% (717/717), 完成.
5、后记
在前面执行“wget http://cert.incommon.org/InCommonServerCA.crt”命令时又出现了问题:
$ wget http://cert.incommon.org/InCommonServerCA.crt
ates.crt.bak
sudo cp ca-certs2.crt /etc/ssl/certs/ca-certificates.crt
--2024-02-28 16:50:04-- http://cert.incommon.org/InCommonServerCA.crt
正在解析主机 cert.incommon.org (cert.incommon.org)... 172.64.149.23, 104.18.38.233, 2606:4700:4400::6812:26e9, ...
正在连接 cert.incommon.org (cert.incommon.org)|172.64.149.23|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Moved Temporarily
位置:http://172.169.x.x/auth_entry.php?authmeth=3&......
[跟随至新的 URL]
--2024-02-28 16:50:06-- http://172.169.x.x/auth_entry.php?authmeth=3&......
正在连接 172.169.x.x:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:未指定 [text/html]
正在保存至: “InCommonServerCA.crt”
InCommonServerCA.crt [ ] 6.10K --.-KB/s 用时 0s
2024-02-28 16:50:06 (730 MB/s) - “InCommonServerCA.crt” 已保存 [6245]
本来应该下载的是 http://cert.incommon.org/InCommonServerCA.crt这个文件,但由于公司内网需要通过认证授权才能连接外网,导致转到内网认证网站上了,所保存的“InCommonServerCA.crt”文件是一个html格式文件,而不是所需要的证书文件(二进制),使得执行证书转换命令“$ openssl x509 -inform DER -in InCommonServerCA.crt -out incommon.pem”时也报错了,而导致证书信任未生效,问题没能得到解决。证书转换命令报错如下:
$ openssl x509 -inform DER -in InCommonServerCA.crt -out incommon.pem
unable to load certificate
139623146378560:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1130:
139623146378560:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:290:Type=X509
$ cat /etc/ssl/certs/ca-certificates.crt incommon.pem > ca-certs2.crt
cat: incommon.pem: 没有那个文件或目录
在通过认证授权后,再执行以上命令问题得到解决,前面的证书问题也得到彻底解决。
$ wget http://cert.incommon.org/InCommonServerCA.crt
--2024-02-28 17:19:26-- http://cert.incommon.org/InCommonServerCA.crt
正在解析主机 cert.incommon.org (cert.incommon.org)... 172.64.149.23, 104.18.38.233, 2606:4700:4400::ac40:9517, ...
正在连接 cert.incommon.org (cert.incommon.org)|172.64.149.23|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1223 (1.2K) [application/pkix-cert]
正在保存至: “InCommonServerCA.crt.1”
InCommonServerCA.crt.1 100% [===================================================================>] 1.19K --.-KB/s 用时 0s
2024-02-28 17:19:28 (228 MB/s) - 已保存 “InCommonServerCA.crt.1” [1223/1223])
$ openssl x509 -inform DER -in InCommonServerCA.crt -out incommon.pem
$ ls
incommon.pem InCommonServerCA.crt
$ cat /etc/ssl/certs/ca-certificates.crt incommon.pem > ca-certs2.crt
以上命令执行后都没有报错,正常执行完成。