AW: AW: AW: Key generation with GPGME and GnuPG hangs at gpgme_op_genkey

Sandra Schreiner sasc0041 at stud.hs-kl.de
Wed Jan 27 13:49:51 CET 2016


> If your problem is merely lack of entropy on a VM, then I'd recommend installing haveged, available in Jessie. It 
> broadens the sources used by the kernel for the entropy pool.

Yes, that was the problem. I installed haveged and it worked. But it seems
that the key generation in my C++ application will not work, if a custom passphrase 
callback is set. The key generation code is unchanged, but with the callback I get an 
GPG_ERR_GENERAL error. The init and callback code is as follows:

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

    gpgme_error_t 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;
    } 
    //callback for user passphrase
    gpgme_set_passphrase_cb(mContext, passphrase_cb, NULL);

And the callback (similar to gpgme++):
-----------------------------
static gpgme_error_t passphrase_cb(void *hook, const char *uid_hint, const char *passphrase_info,
                                   int prev_was_bad, int fd){

    std::string passphraseString;
    std::cout<< "Enter your password:";
    std::cin >> passphraseString;

    if(passphraseString.empty()){
        return GPG_ERR_CANCELED;
    }

    size_t passphraseLength = passphraseString.size();
    char* passphrase = new char[passphraseLength + 1];
    std::copy(passphraseString.begin(), passphraseString.end(), passphrase);

    size_t written = 0;
    do {
        size_t nowWritten = gpgme_io_write(fd, passphrase + written, passphraseLength - written);
        if(nowWritten < 0) {
            break;
        }
        written += nowWritten;
    } while(written < passphraseLength);
    free(passphrase);
    gpgme_io_write(fd, "\n", 1);
    return 0;
}
--------------------------





More information about the Gnupg-users mailing list