gpgme secret key problem

harry_b at mm.st harry_b at mm.st
Thu Jul 6 11:54:16 CEST 2006


Hello,

I am struggling with a problem finding whether a secret key is available or 
not. The reason for this is, that I want to display a warning to the user, 
if he encrypts data without one of his own keys. Obviously, he wouldn't be 
able to read the data himself if he does so.

My function to look for the secret key is this:

int gpgIsSecretKey(char* keyname)
  {
    gpgme_ctx_t         context;
    gpgme_key_t         key;
    gpgme_error_t       error;
    int                 secret = 0;

    /* get a new context */
    error = gpgme_new(&context);
    if (error)
      {
        gpgme_release(context);
        gpgError(error);
        return 0;
      }

    /* start cycling through the list of keys */
    error = gpgme_op_keylist_start(context, keyname, LIST_SECRET);
    if (error)
      {
        gpgme_release(context);
        gpgError(error);
        return 0;
      }

    while (!(error = gpgme_op_keylist_next(context, &key)))
      {   /* take the first usable key we find */
        /* TODO: only choose usable secret keys */
        if (key -> can_encrypt &&
            key -> secret &&
            !key -> disabled &&
            !key -> expired &&
            !key -> invalid &&
            !key -> revoked)
          {   /* we just use keys we can encrypt for */
            secret = 1;
          }

        gpgme_key_unref(key);

        if (secret)
          { break; }
      }
    gpgme_release(context);

    return secret;
  }


I can't see what I do wrong but obviously this does not always work as 
intended. Some users complain that I display the warning even when they use 
their own key. With my own key everything works fine.

Any ideas what I miss?

TIA,
   Harry.

-- 

1024D/40F14012 18F3 736A 4080 303C E61E  2E72 7E05 1F6E 40F1 4012

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GIT/S dx s: a C++ ULS++++$ P+++ L+++$ !E W++ N+ o? K? !w !O !M
V PS+ PE Y? PGP+++ t+ 5-- X+ R+ !tv b++ DI++ D+ G e* h r++ y++
------END GEEK CODE BLOCK------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : /pipermail/attachments/20060706/6c125c58/attachment-0001.pgp


More information about the Gnupg-devel mailing list