Hello,

I am running Ubuntu 20.04.4 LTS, I installed libgpgme11 from Ubuntu with 
version 1.13.1-7ubuntu2 and I installed gpg2 with gpg 2.2.19, libgcrypt 1.8.5.

My problem is this, I try to use keys on my computer in a C program and sign 
something with it. Currently my program can't find any keys. I can see 2 of my 
keys in terminal with gpg2 --list-keys. One key is 1024 bits, no passphrase, 
type 4(RSA) key, the other is the same but type 1 + subkey. I attached my test 
c program, which basically just configured everything according to manual and 
try to read out the keys. I also tried to generate key with my test program but 
it returned GPG_ERR_NOT_SUPPORTED.


I setup everything as default, so my home folder is /home/wyu/.gnupg, gpg2 
locates in /usr/bin/gpg2 and so on. Maybe someone could tell me what have I 
missed?

Thanks a lot and best regards,
William
/* Ubuntu: to compile use "gcc -o tstgpgme tstgpgme.c `gpgme-config --libs`" so the compiler knows where the library is */
#include <gpgme.h>
#include <locale.h>
/*this file name should be the path to and name of the executable crypto engine, in linux NULL means default. used in gpgme_set_engine_info().*/
#define FILE_NAME "NULL" 
/*location of the keyring, NULL means default. used in gpgme_set_engine_info().*/
#define HOME_DIR "NULL"
/*set this to "NULL" to return ALL keys, uid is user name*/
#define KEY_UID "NULL"


void init_gpgme(void)
{
    //init locale environment.
    setlocale(LC_ALL, "");
    fprintf(stdout, "gpgme library version: %s\n", gpgme_check_version(NULL));
    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_set_global_flag("gpg-name",NULL); //change for windows later
}

void conf(gpgme_ctx_t *ctx)
{
    gpgme_error_t err;
    gpgme_keylist_mode_t mode;
    gpgme_key_t r_key;

    err = gpgme_set_protocol(*ctx, GPGME_PROTOCOL_OpenPGP);
    if(err != GPG_ERR_NO_ERROR)
    {
        fprintf(stderr, "protocal selection failed!\n");
    }

    err = gpgme_ctx_set_engine_info(*ctx, GPGME_PROTOCOL_OpenPGP, FILE_NAME, HOME_DIR);
    if(err != GPG_ERR_NO_ERROR)
    {
        fprintf(stderr, "engine setting failed!\n");
    }

    //enable text mode for old standard
    gpgme_set_textmode(*ctx, 1);

    mode = gpgme_get_keylist_mode(*ctx);
    //mode |= GPGME_KEYLIST_MODE_LOCAL;
    mode |= GPGME_KEYLIST_MODE_SIGS;
    err = gpgme_set_keylist_mode(*ctx, mode);
    if(err != GPG_ERR_NO_ERROR)
    {
        fprintf(stderr, "keylist mode setting failed!\n");
    }

    fprintf(stdout, "engine configuration complete!\n");
    /*
    fprintf(stdout, "default home dir = %s\n", gpgme_get_dirinfo("homedir"));
    fprintf(stdout, "system configuration dir = %s\n", gpgme_get_dirinfo("sysconfdir"));
    fprintf(stdout, "gpg program files home dir = %s\n", gpgme_get_dirinfo("bindir"));
    fprintf(stdout, "gpg helper program files dir = %s\n", gpgme_get_dirinfo("libexecdir"));
    fprintf(stdout, "gpg shared data = %s\n", gpgme_get_dirinfo("datadir"));
    */
}

void get_key(gpgme_ctx_t ctx)
{
    gpgme_key_t key;
    gpgme_error_t err;

    //return keys with specified UID, 1 means only return secret keys, 0 otherwise
    err = gpgme_op_keylist_start(ctx, KEY_UID, 1);
    if(err != GPG_ERR_NO_ERROR)
    {
        fprintf(stderr, "keylist start failed!\n");
    }

    while(err != GPG_ERR_EOF)
    {
        err = gpgme_op_keylist_next(ctx, &key);
        if (key == NULL)
        {
            fprintf(stderr, "read keylist failed!\n");
            break;
        }

        fprintf (stdout, "subkey id: %s: ", key->subkeys->keyid);
        if (key->uids && key->uids->name)
            printf ("key id: %s ", key->uids->name);
        if (key->uids && key->uids->email)
            printf (" <%s>", key->uids->email);
        putchar ('\n');
        gpgme_key_unref (key);

    }
    if (gpg_err_code (err) != GPG_ERR_EOF)
    {
        fprintf (stderr, "can not list keys: %s\n", gpgme_strerror (err));
    }
    else
        fprintf(stdout, "key listing complete!\n");
}

int main(void)
{
    gpgme_error_t err;
    gpgme_ctx_t ctx;

    init_gpgme();

    //create context
    err = gpgme_new(&ctx);
    if(err != GPG_ERR_NO_ERROR)
    {
        fprintf(stderr, "context creation failed!\n");
        return -1;
    }

    conf(&ctx);

    get_key(ctx);

    /*test section. create key and check local*/
    err = gpgme_op_createkey(ctx, "w...@work.com", "default", 0, 0, NULL, GPGME_CREATE_NOPASSWD);
    if (err != 0)
        fprintf(stderr, "key gen failed!\n");

    err = gpgme_op_createkey_start(ctx, "w...@work.com", "default", 0, 0, NULL, GPGME_CREATE_NOPASSWD);
    if (err != 0)
        fprintf(stderr, "key gen failed!\n");
    
    gpgme_wait(ctx, &err, 0);



    //destroy context
    gpgme_release(ctx);
    return 0;
}
_______________________________________________
Gnupg-users mailing list
Gnupg-users@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-users

Reply via email to