budger
budger

Reputation: 3

Export gpg Public Key in c++ using gpgme

I need to export public key via gpgme in c++. I have a problem with gpgme_get_key call where it returns a [NO DATA] error or and [END OF FILE] error.

I attempted gpgme_op_keylist_start and the call return [NO DATA] with fingerprint, uid and name as the pattern.

 gpgme_ctx_t ctx;
 gpgme_error_t err;
 gpgme_data_t  out;
 gpgme_key_t key[4] = {NULL,NULL,NULL,NULL};
  gpgme_key_t ekey[2] = {NULL,NULL};

 init_gpgme (GPGME_PROTOCOL_OpenPGP);

   err = gpgme_new (&ctx);
   fail_if_err (err);
gpgme_set_armor (ctx,1);

/* Might want to comment gpgme_ctx_set_engine_info() below. */
    err = gpgme_ctx_set_engine_info (ctx,GPGME_PROTOCOL_OpenPGP,
      "/usr/local/bin/gpg","/root/budger/.gnupg");
    fail_if_err (err);

  /* Generate test keys. */
      err = gpgme_get_key (ctx,"203EBE6AD4860CDB33A66BE8809C3C1548861E13",&key[0],1);
  fail_if_err (err);


   err = gpgme_data_new (&out);
    fail_if_err (err);

    /* Export key[2] only. */
    ekey[0] = key[0];
      err = gpgme_op_export_keys (ctx,ekey,0,out);
     fail_if_err (err);

    fflush (NULL);
    fputs ("Begin Result:\n", stdout);
  print_data (out);
     fputs ("End Result.\n", stdout);
     gpgme_data_release (out);


 gpgme_release (ctx);
 return 0;

budger@thaw:~/gpgme/ref$ ./export export.cpp:60: GPGME: End of file

gpgme_op_keylist_start (ctx, NULL, 0);
while (!(err = gpgme_op_keylist_next (ctx, &key[0]))){

   gpgme_op_export_keys(ctx, key, 0, out);
   print_data(out);

}

budger@thaw:~/gpgme/ref$ ./export_new Segmentation fault (core dumped)

Upvotes: 0

Views: 427

Answers (1)

budger
budger

Reputation: 3

I had to use gpgme_op_export() to search the keyring instead of acquiring the key in order to use gpgme_op_export_keys() which renders a seg fault.

  gpgme_key_t key[1]={NULL};
  gpgme_data_t out;
  gpgme_data_new(&out);

  gpgme_set_armor (ctx,1);
  gpgme_op_export(ctx, "[email protected]", 0, out);
  print_data(out);

Upvotes: 0

Related Questions