使用 GPGME 生成密钥时出错

Error while generating key with GPGME

我正在尝试使用 GPGME 生成新密钥,但不幸的是我无法使以下代码工作:

 std::string def = "<GnupgKeyParms format='internal'>"
                    " Key-Type: default "
                    " Subkey-Type: default"
                    " Name-Real: Joe Tester"
                    " Name-Comment: with stupid passphrase"
                    " Name-Email: joe2@foo.bar"
                    " Expire-Date: 0"
                    " Passphrase: abc"
                    " </GnupgKeyParms>";

            gpgme_error_t error = gpgme_op_genkey(mContext, def.c_str(), NULL, NULL);

            if(GPG_ERR_INV_VALUE  == error){
                std::cout << "Value error";
            }
            if(GPG_ERR_NOT_SUPPORTED == error){
                std::cout << "Not supported error";
            }
            if(GPG_ERR_GENERAL  ==  error){
                std::cout << "general error";
            }
            if(error == GPG_ERR_NO_ERROR){
                gpgme_genkey_result_t res = gpgme_op_genkey_result(mContext);
                if(res->primary && res->sub){
                    result = true;
                }
            }
        }

条件error == GPG_ERR_NO_ERROR不成立。确实none的错误检查条件是。我的调试器只是告诉我 error 的值是 117440567.The 初始化看起来像这样:

gpgme_engine_info_t info;
    gpgme_error_t  error;
    const char * CONFIG_DIR = "/";

    // Initializes gpgme
    gpgme_check_version(NULL);

    // Initialize the locale environment.
    setlocale(LC_ALL, "");
    gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
#ifdef LC_MESSAGES
    gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
#endif
    error = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL,
                                  CONFIG_DIR);
    if(error)
        return false;
    error = gpgme_new(&mContext);
    if(error)
        return false;
    // Check OpenPGP
    error = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
    if(error)
        return false;
    // load engine info
    error = gpgme_get_engine_info(&info);
    if(error)
        return false;
    while(info && info->protocol != gpgme_get_protocol(mContext)) {
        info = info->next;
    }     

    if(error)
        return false;    

我在这里没有收到任何错误。我完全能够使用 GnuPG 命令行创建密钥。那么在使用 GPGME 创建密钥时出现的这个奇怪错误是什么意思呢?

UDAPTE:

感谢 Auges 的回答,我现在可以说这是一个 GPG_ERR_INV_VALUE,这意味着参数字符串格式错误。但为什么?我必须用空格以外的东西分隔值吗?

更新

我更改了内部引号

GnupgKeyParms format=\"internal\"

但现在我得到了 GPG_ERR_GENERAL

更新:

字符串现在看起来像:

std::string def = "<GnupgKeyParms format=\"internal\"> \n"
                    " Key-Type: default \n"
                    " Subkey-Type: default \n"
                    " Name-Real: Joe Tester3 \n"
                    " Name-Comment: with stupid passphrase \n"
                    " Name-Email: joe3@foo.bar \n"
                    " Expire-Date: 0 \n"
                    " Passphrase: abc \n"
                    " </GnupgKeyParms>";

但应用程序挂起在 gpgme_op_genkey

如果您查看 documentation,您会发现 gpgme_errot_t 无法与错误代码进行比较:

... the error value can not be directly compared against an error code, but the accessor functions described below must be used. However, it is guaranteed that only 0 is used to indicate success (GPG_ERR_NO_ERROR), and that in this case all other parts of the error value are set to 0, too.

error 的值仍然应该为零。那我帮不了你了

您可以尝试使用 gpgme_err_code 找出发生的错误。

程序在生成密钥时停止,因为系统上可用的熵太少(它是 debian VM)。对于可能偶然发现这个问题的其他人:您可以通过查看 /proc/sys/kernel/random/entropy_avail 来了解 debian(和其他 linux 发行版)中有多少熵可用。我安装了 haveged(在包管理器中可用)和 re-run 代码。一切正常。非常感谢 mailing list of GnuPG,它帮助我找到了这个解决方案。