使用Git,是否有一种方法告诉它接受自签名证书?

我使用https服务器托管git服务器,但目前证书是自签名的。

当我第一次尝试在那里创建回购时:

git push origin master -f

我得到了错误:

error: Cannot access URL     
https://the server/git.aspx/PocketReferences/, return code 22

fatal: git-http-push failed

当前回答

I'm not a huge fan of the [EDIT: original versions of the] existing answers, because disabling security checks should be a last resort, not the first solution offered. Even though you cannot trust self-signed certificates on first receipt without some additional method of verification, using the certificate for subsequent git operations at least makes life a lot harder for attacks which only occur after you have downloaded the certificate. In other words, if the certificate you downloaded is genuine, then you're good from that point onwards. In contrast, if you simply disable verification then you are wide open to any kind of man-in-the-middle attack at any point.

举一个具体的例子:著名的repo.or.cz存储库提供了一个自签名证书。我可以下载这个文件,把它放在/etc/ssl/certs这样的地方,然后做:

# Initial clone
GIT_SSL_CAINFO=/etc/ssl/certs/rorcz_root_cert.pem \
    git clone https://repo.or.cz/org-mode.git

# Ensure all future interactions with origin remote also work
cd org-mode
git config http.sslCAInfo /etc/ssl/certs/rorcz_root_cert.pem

注意,在这里使用本地git配置(即不使用——global)意味着这个自签名证书只对这个特定的存储库受信任,这很好。它也比使用GIT_SSL_CAPATH更好,因为它消除了git通过不同的证书颁发机构进行验证的风险,这种风险可能会受到损害。

其他回答

I'm not a huge fan of the [EDIT: original versions of the] existing answers, because disabling security checks should be a last resort, not the first solution offered. Even though you cannot trust self-signed certificates on first receipt without some additional method of verification, using the certificate for subsequent git operations at least makes life a lot harder for attacks which only occur after you have downloaded the certificate. In other words, if the certificate you downloaded is genuine, then you're good from that point onwards. In contrast, if you simply disable verification then you are wide open to any kind of man-in-the-middle attack at any point.

举一个具体的例子:著名的repo.or.cz存储库提供了一个自签名证书。我可以下载这个文件,把它放在/etc/ssl/certs这样的地方,然后做:

# Initial clone
GIT_SSL_CAINFO=/etc/ssl/certs/rorcz_root_cert.pem \
    git clone https://repo.or.cz/org-mode.git

# Ensure all future interactions with origin remote also work
cd org-mode
git config http.sslCAInfo /etc/ssl/certs/rorcz_root_cert.pem

注意,在这里使用本地git配置(即不使用——global)意味着这个自签名证书只对这个特定的存储库受信任,这很好。它也比使用GIT_SSL_CAPATH更好,因为它消除了git通过不同的证书颁发机构进行验证的风险,这种风险可能会受到损害。

我用的是windows电脑,这篇文章对我很有帮助。基本上我打开了ca-bundle。CRT在记事本和添加链证书在它(所有)。这个问题通常发生在公司网络中,我们在系统和git回购之间有中间人。我们需要导出证书链中除了base 64格式的leaf cert之外的所有证书,并将它们全部添加到ca-bundle中。然后为修改后的CRT文件配置git。

您可以像这样添加自签名证书,然后再按…

git config --global http.sslCAInfo "C:\Program Files\Git\usr\ssl\cert.pem"

永久地接受特定的证书

http。sslCAPath或http.sslCAInfo。Adam Spiers的回答给出了一些很好的例子。这是这个问题最可靠的解决办法。

禁用单个git命令的TLS/SSL验证

尝试将-c与正确的配置变量传递给git,或使用Flow的答案:

git -c http.sslVerify=false clone https://example.com/path/to/git

禁用所有存储库的SSL验证

可以全局禁用ssl验证。强烈建议不要这样做,但为了完整起见,这里提到了:

git config --global http.sslVerify false # Do NOT do this!

git中有相当多的SSL配置选项。从git配置的手册页:

http.sslVerify
    Whether to verify the SSL certificate when fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_NO_VERIFY environment variable.

http.sslCAInfo
    File containing the certificates to verify the peer with when fetching or pushing
    over HTTPS. Can be overridden by the GIT_SSL_CAINFO environment variable.

http.sslCAPath
    Path containing files with the CA certificates to verify the peer with when
    fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_CAPATH environment variable.

其他一些有用的SSL配置选项:

http.sslCert
    File containing the SSL certificate when fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_CERT environment variable.

http.sslKey
    File containing the SSL private key when fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_KEY environment variable.

http.sslCertPasswordProtected
    Enable git's password prompt for the SSL certificate. Otherwise OpenSSL will
    prompt the user, possibly many times, if the certificate or private key is encrypted.
    Can be overridden by the GIT_SSL_CERT_PASSWORD_PROTECTED environment variable.

我的回答可能晚了,但对我有用。它可能会帮助某些人。

我尝试了上面提到的步骤,但没有解决问题。

试试这个git配置——global http。sslVerify假