如何制作汽车信任gpg公钥?

问题描述 投票:20回答:13

我正在尝试添加我的GPG公钥作为我们的设备安装过程的一部分。它的目的是加密任何重要文件,如管理员,然后使用管理门户将其拉入本地,然后使用私钥解密它们。计划是将公钥导出到文件中,并使用gpg --import命令进行设备安装过程以导入它。但我意识到,在进行任何加密之前,需要对密钥进行信任/签名。如何在安装时无需任何人为干预即可信任此密钥?顺便说一下,我们的设备操作系统是ubuntu vm,我们使用kickstart进行自动化。

感谢所有人的帮助。

encryption ubuntu-12.04 gnupg appliance
13个回答
14
投票

你的问题实际上是“如果密钥不受信任,我如何在没有gpg的情况下加密密钥?”

一个答案是你可以签署密钥。

gpg --edit-key YOUR_RECIPIENT
sign
yes
save

另一个是你可以告诉gpg继续并相信。

gpg --encrypt --recipient YOUR_RECIPIENT --trust-model always YOUR_FILE

0
投票
echo "$( gpg --list-keys --fingerprint | grep "your-key-name-here" -B 1 | head -1 | tr -d '[:space:]'|awk 'BEGIN { FS = "=" } ; { print $2 }' ):6:" | gpg --import-ownertrust;

这个单线是从this gist拉出来的

只需将“your-key-name-here”替换为您的密钥名称即可。


0
投票

有一种方法可以使用--edit-key自动生成密钥,但无需进入交互式shell(因此可以在脚本中自动执行)。以下是Windows的示例:

(echo trust &echo 5 &echo y &echo quit) | gpg --command-fd 0 --edit-key [email protected]

0
投票

基于Unix:

echo -e "5\ny\n" |  gpg --homedir . --command-fd 0 --expert --edit-key [email protected] trust;

欲了解更多信息,请阅读this post。如果您要创建多个密钥,则会详细说明。


-1
投票

请参阅我最近写的this article。两种可能的解决方

  • 壳魔术
  • 期望

11
投票

巧合的是我和OP有类似的情况 - 我正在尝试使用公钥/私钥来为不同的嵌入式设备签名和加密固件。由于还没有答案显示如何为您已导入的密钥添加信任,这是我的答案。

在测试机器上创建和测试密钥后,我将它们导出为ascii:

$ gpg --export -a <hex_key_id> > public_key.asc
$ gpg --export-secret-keys -a <hex_key_id> > private_key.asc

然后安全复制并将它们导入构建服务器:

$ gpg --import public_key.asc
$ gpg --import private_key.asc

Important: add trust

现在编辑密钥以添加最终信任:

$ gpg --edit-key <[email protected]>

gpg>提示符下,键入trust,然后键入5以获得最终信任,然后y确认,然后quit

现在用测试文件测试它:

$ gpg --sign --encrypt --yes --batch --status-fd 1 --recipient "recipient" --output testfile.gpg testfile.txt

报道

...
[GNUPG:] END_ENCRYPTION

在没有增加信任的情况下,我得到了各种错误(不限于以下内容):

gpg: There is no assurance this key belongs to the named user
gpg: testfile.bin: sign+encrypt failed: Unusable public key

7
投票

trusted-key 0x0123456789ABCDEF添加到你的~/.gnupg/gpg.conf替换keyid。这相当于最终信任此密钥,这意味着它所做的认证将被接受为有效。只需将此密钥标记为有效而不信任它就更难,并且需要签名或将信任模型切换为直接。如果您确定只导入有效密钥,则可以通过添加trust-model always将所有密钥标记为有效。在后一种情况下,请确保禁用自动密钥检索(默认情况下不启用)。


4
投票

这对我有用:

尝试加密文件会响应:

gpg -e --yes -r <uid> <filename>

It is NOT certain that the key belongs to the person named
in the user ID.  If you *really* know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N)

That causes my shell script to fail.

所以我:

$gpg --edit-key <uid>

gpg> trust

Please decide how far you trust this user to correctly verify other 
users' keys (by looking at passports, checking fingerprints from 
different sources, etc.)

  1 = I don't know or won't say
  2 = I do NOT trust
  3 = I trust marginally
  4 = I trust fully
  5 = I trust ultimately
  m = back to the main menu

Your decision? 5
Do you really want to set this key to ultimate trust? (y/N) y

Please note that the shown key validity is not necessarily correct
unless you restart the program.

gpg> quit

现在加密工作正常。


4
投票

通过使用--trust-model选项,可以更方便地告诉GPG信任其所有键:

    gpg -a --encrypt -r <recipient key name> --trust-model always

从手册页:

  --trust-model pgp|classic|direct|always|auto

    Set what trust model GnuPG should follow. The models are:

      always Skip  key  validation  and assume that used 
             keys are always fully trusted. You generally 
             won't use this unless you are using some 
             external validation scheme. This option also 
             suppresses the "[uncertain]" tag printed 
             with signature checks when there is no evidence 
             that the user ID is bound to the key.  Note that 
             this trust model still does  not  allow  the use 
             of expired, revoked, or disabled keys.

3
投票

基于@ tersmitten的文章和一些试验和错误,我最终使用以下命令行来信任给定密钥环中的所有密钥而无需用户交互。我将它用于StackEschange Blackboxhiera-eyaml-gpg使用的密钥:

# The "-E" makes this work with both GNU sed and OS X sed
gpg --list-keys --fingerprint --with-colons |
  sed -E -n -e 's/^fpr:::::::::([0-9A-F]+):$/\1:6:/p' |
  gpg --import-ownertrust

就个人而言,我更喜欢将结果存储在trustdb文件中的解决方案,而不是依赖于共享Git仓库之外的用户环境。


2
投票

这是我为GnuPG密钥管理自动化找到的一个技巧,暗示heredoc + --command-fd 0就像魔术一样。下面是其中一个脚本的简略版本,这些脚本是为了帮助GnuPG实现自动化而编写的。

#!/usr/bin/env bash
## First argument should be a file path or key id
Var_gnupg_import_key="${1}"
## Second argument should be an integer
Var_gnupg_import_key_trust="${2:-1}"
## Point to preferred default key server
Var_gnupg_key_server="${3:-hkp://keys.gnupg.net}"
Func_import_gnupg_key_edit_trust(){
    _gnupg_import_key="${1:-${Var_gnupg_import_key}}"
    gpg --no-tty --command-fd 0 --edit-key ${_gnupg_import_key} <<EOF
trust
${Var_gnupg_import_key_trust}
quit
EOF
}
Func_import_gnupg_key(){
    _gnupg_import_key="${1:-${Var_gnupg_import_key}}"
    if [ -f "${_gnupg_import_key}" ]; then
        echo "# ${0##*/} reports: importing key file [${_gnupg_import_key}]"
        gpg --no-tty --command-fd 0 --import ${_gnupg_import_key} <<EOF
trust
${Var_gnupg_import_key_trust}
quit
EOF
    else
        _grep_string='not found on keyserver'
        gpg --dry-run --batch --search-keys ${_gnupg_import_key} --keyserver ${Var_gnupg_key_server} | grep -qE "${_grep_string}"
        _exit_status=$?
        if [ "${_exit_status}" != "0" ]; then
            _key_fingerprint="$(gpg --no-tty --batch --dry-run --search-keys ${_gnupg_import_key} | awk '/key /{print $5}' | tail -n1)"
            _key_fingerprint="${_key_fingerprint//,/}"
            if [ "${#_key_fingerprint}" != "0" ]; then
                echo "# ${0##*/} reports: importing key [${_key_fingerprint}] from keyserver [${Var_gnupg_key_server}]"
                gpg --keyserver ${Var_gnupg_key_server} --recv-keys ${_key_fingerprint}
                Func_import_gnupg_key_edit_trust "${_gnupg_import_key}"
            else
                echo "# ${0##*/} reports: error no public key [${_gnupg_import_key}] as file or on key server [${Var_gnupg_key_server}]"
            fi
        else
            echo "# ${0##*/} reports: error no public key [${_gnupg_import_key}] as file or on key server [${Var_gnupg_key_server}]"
        fi
    fi
}
if [ "${#Var_gnupg_import_key}" != "0" ]; then
    Func_import_gnupg_key "${Var_gnupg_import_key}"
else
    echo "# ${0##*/} needs a key to import."
    exit 1
fi

使用script_name.sh 'path/to/key' '1'script_name.sh 'key-id' '1'运行以导入密钥并分配信任值1或使用script_name.sh 'path/to/key' '1' 'hkp://preferred.key.server'编辑所有值

加密现在应该没有投诉,但即使它确实如下,--always-trust选项应该允许加密,即使有投诉。

gpg --no-tty --batch --always-trust -e some_file -r some_recipient -o some_file.gpg

如果你希望看到这个在行动,那么检查Travis-CI构建日志以及如何在同一操作中使用帮助程序脚本GnuPG_Gen_Key.sh来生成和导入密钥...这个帮助程序脚本的第二版将更加清晰和可修改但它是一个很好的起点。


1
投票

我想,我想办法做到这一点。我使用'gpg --import-ownertrust'将我的信任数据库导出到一个文本文件中,然后从中删除了我的所有密钥,除了我需要推送的公钥。然后将我的公钥和编辑后的所有者信任文件导入到服务器上。这似乎工作。现在我在Kickstart文件中实现这些步骤时遇到了麻烦:-(


0
投票

使用powershell,这里是如何信任[email protected](改编自@tersmitten博客文章):

(gpg --fingerprint [email protected] | out-string)  -match 'fingerprint = (.+)'
$fingerprint = $Matches[1] -replace '\s'
"${fingerprint}:6:" | gpg --import-ownertrust

注意:使用cinst gpg4win-vanilla

© www.soinside.com 2019 - 2024. All rights reserved.