使用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

永久地接受特定的证书

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_SSL_NO_VERIFY设置为true:

GIT_SSL_NO_VERIFY=true git clone https://example.com/path/to/git

或者配置Git不验证命令行上的连接:

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

请注意,如果您不验证SSL/TLS证书,那么您很容易受到MitM攻击。


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通过不同的证书颁发机构进行验证的风险,这种风险可能会受到损害。


我经常遇到这个问题,所以写了一个脚本从服务器下载自签名证书并将其安装到~/。然后更新git-config以指向这些证书。它存储在全局配置中,因此每个远程只需要运行一次。

https://github.com/iwonbigbro/tools/blob/master/bin/git-remote-install-cert.sh


Git自签名证书配置

博士tl;

永远不要禁用所有SSL验证! 这造成了一种糟糕的安全文化。不要成为那样的人。

您需要的配置键是:

http。sslverify -始终为真。见上注。

这些用于配置您信任的主机证书

http.sslCAPath http.sslCAInfo

它们用于配置您的证书以响应SSL挑战。

http.sslCert http.sslCertPasswordProtected

选择性地将上述设置应用于特定主机。

http。< url >。*

自签名证书颁发机构的全局.gitconfig

为了我自己和我的同事,本文介绍了如何在不禁用sslVerify的情况下使自签名证书工作。编辑你的.gitconfig,使用gitconfig——global -e添加以下内容:

# Specify the scheme and host as a 'context' that only these settings apply
# Must use Git v1.8.5+ for these contexts to work
[credential "https://your.domain.com"]
  username = user.name

  # Uncomment the credential helper that applies to your platform
  # Windows
  # helper = manager

  # OSX
  # helper = osxkeychain

  # Linux (in-memory credential helper)
  # helper = cache

  # Linux (permanent storage credential helper)
  # https://askubuntu.com/a/776335/491772

# Specify the scheme and host as a 'context' that only these settings apply 
# Must use Git v1.8.5+ for these contexts to work
[http "https://your.domain.com"]
  ##################################
  # Self Signed Server Certificate #
  ##################################

  # MUST be PEM format
  # Some situations require both the CAPath AND CAInfo 
  sslCAInfo = /path/to/selfCA/self-signed-certificate.crt
  sslCAPath = /path/to/selfCA/
  sslVerify = true

  ###########################################
  # Private Key and Certificate information #
  ###########################################

  # Must be PEM format and include BEGIN CERTIFICATE / END CERTIFICATE, 
  # not just the BEGIN PRIVATE KEY / END PRIVATE KEY for Git to recognise it.
  sslCert = /path/to/privatekey/myprivatecert.pem

  # Even if your PEM file is password protected, set this to false.
  # Setting this to true always asks for a password even if you don't have one.
  # When you do have a password, even with this set to false it will prompt anyhow. 
  sslCertPasswordProtected = 0

引用:

Git凭证 Git凭据存储 使用Gnome Keyring作为凭证存储 Git配置http.<url>。* Git v1.8.5支持

在克隆git时指定配置

如果你需要在每次回购的基础上应用它,文档告诉你只需要在你的回购目录中运行git config——local。当你还没有在本地克隆回购时,这是没有用的,不是吗?

你可以做全局->本地hokey-pokey通过设置你的全局配置,然后复制这些设置到你的本地回购配置一旦克隆…

或者,你可以在git克隆时指定配置命令,这些命令在目标repo克隆后应用到目标repo。

# Declare variables to make clone command less verbose     
OUR_CA_PATH=/path/to/selfCA/
OUR_CA_FILE=$OUR_CA_PATH/self-signed-certificate.crt
MY_PEM_FILE=/path/to/privatekey/myprivatecert.pem
SELF_SIGN_CONFIG="-c http.sslCAPath=$OUR_CA_PATH -c http.sslCAInfo=$OUR_CA_FILE -c http.sslVerify=1 -c http.sslCert=$MY_PEM_FILE -c http.sslCertPasswordProtected=0"

# With this environment variable defined it makes subsequent clones easier if you need to pull down multiple repos.
git clone $SELF_SIGN_CONFIG https://mygit.server.com/projects/myproject.git myproject/

一个衬套

编辑:请参阅VonC的回答,其中指出了关于从2.14.x/2.15到这一行的特定git版本的绝对路径和相对路径的警告

git clone -c http.sslCAPath="/path/to/selfCA" -c http.sslCAInfo="/path/to/selfCA/self-signed-certificate.crt" -c http.sslVerify=1 -c http.sslCert="/path/to/privatekey/myprivatecert.pem" -c http.sslCertPasswordProtected=0 https://mygit.server.com/projects/myproject.git myproject/

CentOS无法加载客户端密钥

如果你在CentOS上尝试这个,你的。pem文件会给你

unable to load client key: "-8178 (SEC_ERROR_BAD_KEY)"

然后你会想要这个StackOverflow关于curl如何使用NSS而不是Open SSL的答案。

你会想要从source重建curl:

git clone http://github.com/curl/curl.git curl/
cd curl/
# Need these for ./buildconf
yum install autoconf automake libtool m4 nroff perl -y
#Need these for ./configure
yum install openssl-devel openldap-devel libssh2-devel -y

./buildconf
su # Switch to super user to install into /usr/bin/curl
./configure --with-openssl --with-ldap --with-libssh2 --prefix=/usr/
make
make install

重新启动计算机,因为libcurl仍然作为共享库存在于内存中

Python, pip和conda

相关:如何将自定义CA根证书添加到Windows中pip使用的CA存储?


在使用sslKey或sslCert使用一行程序时要小心,如Josh Peak的回答所示:

git clone -c http.sslCAPath="/path/to/selfCA" \
  -c http.sslCAInfo="/path/to/selfCA/self-signed-certificate.crt" \
  -c http.sslVerify=1 \
  -c http.sslCert="/path/to/privatekey/myprivatecert.pem" \
  -c http.sslCertPasswordProtected=0 \
https://mygit.server.com/projects/myproject.git myproject

只有Git 2.14.x/2.15(2015年Q3)能够正确地解释~username/mykey这样的路径(同时它仍然可以解释/path/to/privatekey这样的绝对路径)。

参见Junio C Hamano (gitster)提交的8d15496(2017年7月20日)。 资助人:查尔斯·贝利(hashpling)。 (由Junio C Hamano合并- gitster -在commit 17b1e1d, 2017年8月11日)

http.c: http.sslcert and http.sslkey are both pathnames Back when the modern http_options() codepath was created to parse various http.* options at 29508e1 ("Isolate shared HTTP request functionality", 2005-11-18, Git 0.99.9k), and then later was corrected for interation between the multiple configuration files in 7059cd9 ("http_init(): Fix config file parsing", 2009-03-09, Git 1.6.3-rc0), we parsed configuration variables like http.sslkey, http.sslcert as plain vanilla strings, because git_config_pathname() that understands "~[username]/" prefix did not exist. Later, we converted some of them (namely, http.sslCAPath and http.sslCAInfo) to use the function, and added variables like http.cookeyFile http.pinnedpubkey to use the function from the beginning. Because of that, these variables all understand "~[username]/" prefix. Make the remaining two variables, http.sslcert and http.sslkey, also aware of the convention, as they are both clearly pathnames to files.


在.gitconfig文件中,您可以添加以下给定值,以使自签名证书可接受

sslCAInfo = /home/XXXX/abc.crt


在Windows上使用64位版本的Git,只需将自签名CA证书添加到这些文件中:

C:\Program Files\Git\mingw64\ssl\certs\ ca-bundle.crt C:\Program Files\Git\mingw64\ssl\certs\ ca-bundle.trust.crt

如果它只是一个服务器自签名证书,请将其添加到

C:\Program Files\Git\mingw64\ssl\cert.pem


以下答案摘自迈克尔·考夫曼(Michael Kauffman)撰写的文章。

使用带有公司SSL证书的Git for Windows

问题:

如果你有一个公司SSL证书,想要从控制台或VSCode克隆你的repo,你会得到以下错误:

致命:无法访问“https://myserver/tfs/DefaultCollection/_git/Proj/”:SSL证书问题:无法获得本地颁发者证书

解决方案:

将根自签名证书导出到文件中。您可以在浏览器中完成此操作。 定位“ca-bundle”。crt”文件(当前版本C:\Program Files\Git\usr\ssl\certs,但在过去已更改)。将文件复制到您的用户配置文件。用文本编辑器(如VSCode)打开它,并将导出的证书的内容添加到文件的末尾。

现在我们必须配置git来使用新文件:

Git配置——global http。sslCAInfo C: /用户/ < yourname > / ca-bundle.crt

这将把以下条目添加到用户配置文件根目录下的.gitconfig文件中。

(http) sslCAInfo = C:/Users/<yourname>/ bat .crt


检查防病毒和防火墙设置。

从一天到另一天,git不再工作了。根据上面的描述,我发现卡巴斯基在中间放置了一个自签名的反病毒个人根证书。按照上面的说明,我没有设法让Git接受该证书。我放弃了。对我来说有用的是禁用扫描加密连接的功能。

卡巴斯基开放 设置>附加>网络>不扫描加密连接

在此之后,git再次启用sslVerify。

请注意。这对我来说仍然不满意,因为我想让我的反病毒功能激活。在高级设置中,卡巴斯基显示了一个不能使用该功能的网站列表。Github不在其中。我会在卡巴斯基论坛上查的。似乎有一些话题,例如。 https://forum.kaspersky.com/index.php?/topic/395220-kis-interfering-with-git/&tab=comments#comment-2801211


我是这样做的:

git init
git config --global http.sslVerify false
git clone https://myurl/myrepo.git

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

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

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


在Windows上,这对我来说很管用:

将自签名证书的内容添加到ca-bundle文件的末尾。包括-----BEGIN CERTIFICATE-----和-----END CERTIFICATE-----行

ca-bundle文件的位置通常是C:\Program Files\Git\mingw64\ssl\certs

然后,将ca-bundle文件的路径添加到全局git配置中。下面的命令可以做到这一点:git config——global http。sslCAInfo "C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt"

注意:路径依赖于ca-bundle文件的本地路径!


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


设置http不是一个好的做法。sslVerify假。 相反,我们可以使用SSL证书。

因此,构建代理将使用https与SSL证书和PAT进行身份验证。

复制cer文件的内容,包括- begin -和-end——。

Git bash build agent => Git配置-global http。sslcainfo " C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt " 转到该文件并添加.cer内容。

因此,构建代理可以访问SSL证书


它为我工作,只需运行以下命令

git config --global http.sslVerify false

它将打开一个git凭据窗口,提供您的凭据。第一次只有它问


关于http。sslCAPath选项:如果在包含证书文件的目录上执行了OpenSSL c_rehash命令,git将只检测指定目录路径下的证书文件。c_rehash命令将为每个证书创建符号链接,其中链接的名称为哈希值。例如:

$ cd /path/to/ssl/cert/directory

$ ls -al

  total 16
  drwxr-xr-x  3 user  staff    96 Oct 20 13:47 .
  drwxr-xr-x  4 user  staff   128 Oct 20 13:46 ..
  -rw-r--r--  1 user  staff  4832 Oct 20 13:47 google.pem

$ /usr/local/opt/openssl@1.1/bin/c_rehash ./

  Doing ./

$ ls -al

  total 16
  drwxr-xr-x  4 user  staff   128 Oct 20 13:58 .
  drwxr-xr-x  4 user  staff   128 Oct 20 13:46 ..
  lrwxr-xr-x  1 user  staff    10 Oct 20 13:58 f6dbf7a7.0 -> google.pem
  -rw-r--r--  1 user  staff  4832 Oct 20 13:47 google.pem

注意,c_rehash命令创建了以下符号链接:f6dbf7a7.0 -> google.pem。

您还可以用下面的命令代替c_rehash实用程序,不过请注意,下面的命令只处理*。Pem文件,而c_rehash实用程序将处理.pem, .crt, .cer或.crl文件:

for file in *.pem; do ln -s $file `openssl x509 -hash -noout -in $file`.0; done

如果您现在配置http。将sslCAPath导入到包含上述符号链接的目录,git将获取证书文件:

# contents of /etc/gitconfig
[http]
        sslCAPath = /path/to/ssl/cert/directory/

也可以配置http。sslCAPath使用一个环境变量:

export GIT_SSL_CAPATH=/path/to/ssl/cert/directory/

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

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