简体   繁体   中英

Error while generating key with GPGME

I'm trying to generate a new key with GPGME but unfortunately I can't make the following code work:

 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;
                }
            }
        }

The condition error == GPG_ERR_NO_ERROR is not true. Indeed none of the error checking conditions is. My debugger just tells me that the value of error is 117440567.The initialisation looks like this:

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;    

I don't get any errors here. I'm perfectly able to create keys by using GnuPG command line. So what does this strange error while creating a key with GPGME mean?

UDAPTE:

Thanks to Auges answer I now can tell it is a GPG_ERR_INV_VALUE meaning the param string is malformed. But ... why? Do I have to separate the values by something more than a blank?

UPDATE

I changed the quotes of internal

GnupgKeyParms format=\"internal\"

But now I'm getting a GPG_ERR_GENERAL .

UPDATE:

The string now looks like:

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>";

But the application hangs at gpgme_op_genkey .

If you take at look at the documentation you find that gpgme_errot_t cannot be copared against an error code:

... 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.

Still the value of error should be zero. With that I can't help you

You could try using gpgme_err_code to find out which error occurred.

The program stopped while generating the key because too less entropy was available on the system (it is a debian VM). For others that might stumble across this question: You can see how much entropy is available in debian (and other linux distributions) by looking at /proc/sys/kernel/random/entropy_avail. I installed haveged (available in package manager) and re-run the code. Everything worked fine. Many thanks to the mailing list of GnuPG , which helped me figure out this solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM