如何在Windows上安装和使用gpg-agent?

23

我想开始给 Git 提交签名。我使用 keybase.io 设置了 GPG 密钥,并在本地机器和 Git 服务器上同步了它。现在,我正在尝试解决每次提交时都要输入密钥密码的问题。

PS> git commit -m "testing" --allow-empty

You need a passphrase to unlock the secret key for
user: "keybase.io/anthonymastrean <anthonymastrean@keybase.io>"
2048-bit RSA key, ID AD9184C0, created 2015-04-14 (main key ID 293FEB8B)

Enter passphrase:
据我所理解,我需要安装类似于gpg-agent的东西。我使用的是Windows 10 Pro 1803,因此我在看Gpg4win(由GitHub和其他人推荐)。我通过Chocolatey安装了它,所以我拥有完整的默认安装程序。

但是,我无法弄清楚如何启动gpg-agent来缓存我的密码。每次提交时都会提示我输入密码。

gpg-agent说它正在运行

PS> gpg-agent
gpg-agent[4644]: gpg-agent running and available

我有这个gpg-connect-agent东西,但是我不知道该怎么做。

PS> gpg-connect-agent.exe
> help
# NOP
# CANCEL
# OPTION
# BYE
# AUTH
# RESET
# END
# HELP
# GETEVENTCOUNTER
# ISTRUSTED <hexstring_with_fingerprint>
# HAVEKEY <hexstrings_with_keygrips>
# KEYINFO [--[ssh-]list] [--data] [--ssh-fpr] [--with-ssh] <keygrip>
# SIGKEY <hexstring_with_keygrip>
# SETKEY
# SETKEYDESC plus_percent_escaped_string
# SETHASH (--hash=<name>)|(<algonumber>) <hexstring>
# PKSIGN [<options>] [<cache_nonce>]
# PKDECRYPT [<options>]
# GENKEY [--no-protection] [--preset] [--inq-passwd]
# READKEY <hexstring_with_keygrip>
# GET_PASSPHRASE [--data] [--check] [--no-ask] [--repeat[=N]]
# PRESET_PASSPHRASE [--inquire] <string_or_keygrip> <timeout> [<hexstring>]
# CLEAR_PASSPHRASE [--mode=normal] <cache_id>
# GET_CONFIRMATION <description>
# LISTTRUSTED
# MARKTRUSTED <hexstring_with_fingerprint> <flag> <display_name>
# LEARN [--send] [--sendinfo] [--force]
# PASSWD [--cache-nonce=<c>] [--passwd-nonce=<s>] [--preset]
# INPUT
# OUTPUT
# SCD <commands to pass to the scdaemon>
# KEYWRAP_KEY [--clear] <mode>
# IMPORT_KEY [--unattended] [--force] [<cache_nonce>]
# EXPORT_KEY [--cache-nonce=<nonce>] [--openpgp] <hexstring_with_keygrip>
# DELETE_KEY [--force|--stub-only] <hexstring_with_keygrip>
# GETVAL <key>
# PUTVAL <key> [<percent_escaped_value>]
# UPDATESTARTUPTTY
# KILLAGENT
# RELOADAGENT
# GETINFO <what>
# KEYTOCARD [--force] <hexstring_with_keygrip> <serialno> <id> <timestamp>
OK
我看到手册上讲了如何在Bash会话中启动gpg-agent,但我不确定如何将其翻译成Windows并使其适用于cmd.exe和PowerShell。

https://linux.die.net/man/1/gpg-agent

是的,这里讨论了生成密钥和设置签名,但没有涉及如何在Windows上配置gpg-agent以缓存密码! - Anthony Mastrean
哦,你说得对。我记得那篇文章里有提到过。我会尝试找出那篇文章的。 - Gary Ewan Park
3个回答

31
  1. 您可以使用gpgconf --launch gpg-agent在Windows上让gpg-agent在后台运行。

  2. 为了让gpg-agent在我登录时自动运行,我在任务计划程序中添加一个任务:

gpg-agent-autostart

  1. 要扩展密码短语的过期时间,请将这些行添加到gpg-agent.conf中:

  2. default-cache-ttl 34560000
    
    max-cache-ttl 34560000
    

我试图将数字设置为999999999,但完全没有起作用。

您可以使用此命令找到gpg-agent.conf的位置:

$ gpgconf.exe --list-dirs

sysconfdir:C%3a\ProgramData\GNU\etc\gnupg
bindir:C%3a\Program Files (x86)\GnuPG\bin
libexecdir:C%3a\Program Files (x86)\GnuPG\bin
libdir:C%3a\Program Files (x86)\GnuPG\lib\gnupg
datadir:C%3a\Program Files (x86)\GnuPG\share\gnupg
localedir:C%3a\Program Files (x86)\GnuPG\share\locale
socketdir:C%3a\Users\Jerry\AppData\Roaming\gnupg
dirmngr-socket:C%3a\Users\Jerry\AppData\Roaming\gnupg\S.dirmngr
agent-ssh-socket:C%3a\Users\Jerry\AppData\Roaming\gnupg\S.gpg-agent.ssh
agent-extra-socket:C%3a\Users\Jerry\AppData\Roaming\gnupg\S.gpg-agent.extra
agent-browser-socket:C%3a\Users\Jerry\AppData\Roaming\gnupg\S.gpg-agent.browser
agent-socket:C%3a\Users\Jerry\AppData\Roaming\gnupg\S.gpg-agent
homedir:C%3a\Users\Jerry\AppData\Roaming\gnupg

gpg-agent.conf位于主目录中


第一步有所帮助,但我需要更多的帮助。也许这是gpg4win的变化,但是使用管理员Power Shell窗口,这个过程最终对我有用。 - James

20

我需要告知git我已经安装了gpg程序,该程序本身知道它应该使用的gpg-agent。

PS> git config --global gpg.program $(Resolve-Path (Get-Command gpg | Select-Object -Expand Source) | Select-Object -Expand Path)

设置完这个配置后,Gpg4win 的 "PIN 输入" 对话框会弹出!

enter image description here

也许我在自动启动 gpg-agent 或理解会话生命周期方面漏掉了什么,但我会回来提供更多细节。


我是唯一一个最不理解这个答案的人吗?问题是从Windows框连接到Linux远程。给定上面路径的git程序是远程还是本地框?本地git可执行文件何时以及如何被调用?难道远程gpg可执行文件不必连接到本地密钥环吗? - Meteorhead
1
@Meteorhead,我认为问题是“Windows上的gpg-agent仅缓存10分钟的密码短语”,而Anthony想强制它记住GPG密钥的密码短语更长一段时间,并且最好永久记住,以避免每次提交输入密码短语。这真的很烦人(我今天遇到了这个问题 =) - hotenov

8
除了 Tao Zhu 的回答之外,您还可以在随附 Gpg4win 的 GUI 密钥管理器 Kleopatra 中指定口令短语(PIN)的过期时间,无需配置文件:
在安装 Gpg4win 时进行检查(俄文安装程序):enter image description here 在“开始”菜单中找到并运行 Kleopatra。转到菜单:“设置”-“配置 Kleopatra...”enter image description here 切换到GnuPG 系统选项卡,然后是私钥选项卡。更改两个设置 Expire cached PINs after N secondsSet maximum PIN cache lifetime to N seconds 的默认值为您自己的值。我这样设置:enter image description here 较大的值使您可以避免在一天内每次 git 提交时频繁和烦人地输入 GPG 密钥的口令短语(直到下一次 Windows 重启或过期指定时间)。 不幸的是,我还没有找到在会话(重新启动)之间保留口令短语的方法。

这是我找到的最清晰的答案,解决了“如何让Git在提交之间记住我的GPG密码”的问题。虽然还在寻找更好的方法,但这比我目前的设置要好得多。我正在寻找一个以Windows为中心的解决方案,尽可能地利用Windows提供的开箱即用配置。这个解决方案已经接近标准(配置文件也可以,但这个对我来说更容易记忆)。 - d3r3kk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接