【问题标题】:Using gpg --search-keys in --batch mode在 --batch 模式下使用 gpg --search-keys
【发布时间】:2014-07-18 19:34:13
【问题描述】:

我正在开发一个应用程序,该应用程序最终将绘制一组预定义的电子邮件地址之间的 gpg 签名连接。我需要它以编程方式从密钥服务器收集公钥。我有一个工作模型,它将使用 gpg 的 --search-keys 选项。但是,当使用 --batch 标志运行时,我收到错误“gpg:抱歉,我们处于批处理模式 - 无法获取输入”。当我在没有 --batch 标志的情况下运行时,gpg 需要输入。

我希望 gpg 有一些我错过的标志。或者,可以与密钥服务器交互的库(最好是 python)。

【问题讨论】:

    标签: python batch-file gnupg pgp


    【解决方案1】:

    使用

    gpg --batch --keyserver hkp://pool.sks-keyservers.net --search-keys ...
    

    并解析输出以获取密钥 ID。

    之后

    gpg --batch --keyserver hkp://pool.sks-keyservers.net  --recv-keys key-id key-id ..
    

    应该有效

    【讨论】:

      【解决方案2】:

      当您导入非常大部分信任网络时,GnuPG 的性能无论如何都不是很好,尤其是在导入阶段。

      我会去设置一个本地密钥服务器,只需将所有密钥转储在那里(2014 年的下载大小不到 10GB)并直接查询您自己的本地密钥服务器。

      Hockeypuck 相当容易设置,尤其是查询,因为它将数据存储在 PostgreSQL 数据库中。

      【讨论】:

      • 在我的特殊情况下,我不会处理超过 100 个键。但是,运行本地密钥服务器并以这种形式处理数据可能会有所帮助。我会试一试。谢谢!
      【解决方案3】:

      使用 --recv-keys 无需提示即可获取密钥。

      【讨论】:

      • --recv-keys 需要密钥 ID。我正在尝试通过电子邮件地址查找密钥 ID。我应该注意,如果 --recv-keys 会接收电子邮件、执行搜索并导入任何结果键,那将是我正在寻找的确切选项。
      【解决方案4】:

      在 hkps 服务器的情况下,以下操作将起作用:

      gpg --keyserver hkps://***HKPSDOMAIN*** --recv-keys \
      $(curl -s "https://***HKPSDOMAIN***/?op=index&options=mr&search=***SEARCHSTRING***"\
      |grep pub|awk -F ":" '{print $2}')
      

      【讨论】:

        【解决方案5】:

        我们可以将gpg --search-keys 命令的std 和err 输出存储到specifying 2>&1 的变量中,然后对这些变量进行处理。例如,获取公钥 id 或 *.amazon.com 电子邮件地址:

        pubkeyids=$(gpg --batch --keyserver hkp://keyserver.ubuntu.com --search-keys amazon.com 2>&1 | grep -Po '\d+\s*bit\s*\S+\s*key\s*[^,]+' | cut -d' ' -f5)
        

        正则表达式在regex101.com 上有完整的解释。我们可以通过它们的 ID 自动搜索密钥,并通过解析该输出使用 bash 将它们添加到密钥环中。作为说明,我创建了the following GitHub gist 来托管下面的代码。

        示例地址列表example.csv:

        First Name Last Name Email Address
        Hi Bye hi@bye.com
        Yes No yes@no.com
        Why Not why@not.com

        然后我们可以将 csv 路径传递给 bash 脚本,该脚本将在 csv 中添加属于电子邮件地址的所有键:

        $ getPubKeysFromCSV.sh ~/example.csv
        

        这是上述想法的一个实现,getPubKeysFromCSV.sh:

        # CSV of email address
        csv=$1
        
        # Get headers from CSV
        headers=$(head -1 $csv)
        
        # Find the column number of the email address
        emailCol=$(echo $headers | tr ',' '\n' | grep -n "Email Address" | cut -d':' -f1)
        
        # Content of the CSV at emailCol column, skipping the first line
        emailAddrs=$(tail -n +2 $csv | cut -d',' -f$emailCol)
        gpgListPatrn='(?<entropy>\d+)\s*bit\s*(?<algo>\S+)\s*key\s*(?<pubkeyid>[^,]+)'
        # Loop through the array and get the public keys
        for email in "${emailAddrs[@]}"
        do
            # Get the public key ids for the email address by matching the regex gpgListPatt
            pubkeyids=$(gpg --batch --keyserver hkp://keyserver.ubuntu.com --search-keys $email 2>&1 | grep -Po $gpgListPatrn | cut -d' ' -f5)
            # For each public key id, get the public key
            for pubkeyid in $pubkeyids
            do
                # Add the public key to the local keyring
                recvr=$(gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys $pubkeyids 2>&1)
                # Check exit code to see if the key was added
                if [ $? -eq 0 ]; then
                    # If the public key is added, do some extra work with it
                    # [do stuff] 
                fi
            done
        done
        

        如果我们愿意,我们可以通过在成功添加公钥后验证循环体中的文件签名来使getPubKeysFromCSV.sh 更复杂。除了 CSV 路径,我们将签名路径和文件路径分别作为参数二和三传递:

        $ getPubKeysFromCSV.sh ~/example.csv ./example.file.sig ./example.file
        

        这里是更新后的脚本差异diff:

        --- original.sh
        +++ updated.sh
        @@ -1,6 +1,12 @@
         # CSV of email address
         csv=$1
         
        +# signature file
        +sig=$2
        +
        +# file to verify
        +file=$3
        +
         # Get headers from CSV
         headers=$(head -1 $csv)
         
        @@ -22,5 +28,17 @@
                 recvr=$(gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys $pubkeyids 2>&1)
                # Check exit code to see if the key was added
        +       if [ $? -eq 0 ]; then
        +           verify=$(gpg --batch --verify $sig $file 2>&1)
        +           # If the signature is verified, announce it was verified
        +           # else, print error not verified and exit
        +           if [[ $verify =~ "^gpg: Good signature from" ]]; then 
        +               echo "$file was verified by $email using $pubkeyid" 
        +           else 
        +               printf '%s\n' "$file was unable to be verified" >&2
        +               exit 1
        +           fi
        +       fi
             done
         done
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-19
          • 1970-01-01
          • 2018-02-27
          • 1970-01-01
          • 2016-06-09
          • 2018-12-27
          相关资源
          最近更新 更多