static void gpa_file_import_operation_done_cb (GpaContext *context, gpg_error_t err, GpaFileImportOperation *op) { if (err) { gpa_gpgme_update_import_results (&op->counters, 1, 1, NULL); } else { gpgme_import_result_t res; res = gpgme_op_import_result (GPA_OPERATION (op)->context->ctx); gpa_gpgme_update_import_results (&op->counters, 1, 0, res); } if (gpg_err_code (err) != GPG_ERR_CANCELED) { /* Go to the next file in the list and import it. */ GPA_FILE_OPERATION (op)->current = (g_list_next (GPA_FILE_OPERATION (op)->current)); gpa_file_import_operation_next (op); } }
void GpgME::ImportResult::init( gpgme_ctx_t ctx ) { if ( !ctx ) { return; } gpgme_import_result_t res = gpgme_op_import_result( ctx ); if ( !res ) { return; } d.reset( new Private( *res ) ); }
gboolean fu_keyring_add_public_key (FuKeyring *keyring, const gchar *filename, GError **error) { FuKeyringPrivate *priv = GET_PRIVATE (keyring); gpgme_error_t rc; gpgme_import_result_t result; gpgme_import_status_t s; g_auto(gpgme_data_t) data = NULL; g_return_val_if_fail (FU_IS_KEYRING (keyring), FALSE); g_return_val_if_fail (filename != NULL, FALSE); /* import public key */ g_debug ("Adding public key %s", filename); rc = gpgme_data_new_from_file (&data, filename, 1); if (rc != GPG_ERR_NO_ERROR) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "failed to load %s: %s", filename, gpgme_strerror (rc)); return FALSE; } rc = gpgme_op_import (priv->ctx, data); if (rc != GPG_ERR_NO_ERROR) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "failed to import %s: %s", filename, gpgme_strerror (rc)); return FALSE; } /* print what keys were imported */ result = gpgme_op_import_result (priv->ctx); for (s = result->imports; s != NULL; s = s->next) { g_debug ("importing key %s [%u] %s", s->fpr, s->status, gpgme_strerror (s->result)); } /* make sure keys were really imported */ if (result->imported == 0 && result->unchanged == 0) { g_debug("imported: %d, unchanged: %d, not_imported: %d", result->imported, result->unchanged, result->not_imported); g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "key import failed %s", filename); return FALSE; } return TRUE; }
/* Deprecated interface. */ gpgme_error_t gpgme_op_import_ext (gpgme_ctx_t ctx, gpgme_data_t keydata, int *nr) { gpgme_error_t err = gpgme_op_import (ctx, keydata); if (!err && nr) { gpgme_import_result_t result = gpgme_op_import_result (ctx); *nr = result->considered; } return err; }
int main (int argc, char **argv) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in; gpgme_import_result_t result; char *cert_1 = make_filename ("cert_dfn_pca01.der"); char *cert_2 = make_filename ("cert_dfn_pca15.der"); init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); err = gpgme_data_new_from_file (&in, cert_1, 1); free (cert_1); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD", 1, 1); gpgme_data_release (in); err = gpgme_data_new_from_file (&in, cert_2, 1); free (cert_2); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "2C8F3C356AB761CB3674835B792CDA52937F9285", 1, 2); gpgme_data_release (in); gpgme_release (ctx); return 0; }
int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in; gpgme_import_result_t result; char *pubkey_1_asc = make_filename ("pubkey-1.asc"); char *seckey_1_asc = make_filename ("seckey-1.asc"); init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_data_new_from_file (&in, pubkey_1_asc, 1); free (pubkey_1_asc); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 0); gpgme_data_release (in); err = gpgme_data_new_from_file (&in, seckey_1_asc, 1); free (seckey_1_asc); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 1); gpgme_data_release (in); gpgme_release (ctx); return 0; }
SEXP R_gpg_import(SEXP pubkey) { gpgme_data_t KEY; bail(gpgme_data_new_from_mem(&KEY, (const char*) RAW(pubkey), LENGTH(pubkey), 0), "creating key buffer"); bail(gpgme_op_import(ctx, KEY), "importing pubkey"); gpgme_import_result_t result = gpgme_op_import_result(ctx); SEXP out = PROTECT(allocVector(INTSXP, 5)); INTEGER(out)[0] = result->considered; INTEGER(out)[1] = result->imported; INTEGER(out)[2] = result->secret_imported; INTEGER(out)[3] = result->new_signatures; INTEGER(out)[4] = result->new_revocations; UNPROTECT(1); return out; }
/** * Import a key into the local keyring. * @param handle the context handle * @param key the key to import, likely retrieved from #key_search * @return 0 on success, -1 on error */ static int key_import(alpm_handle_t *handle, alpm_pgpkey_t *key) { gpgme_error_t gpg_err; gpgme_ctx_t ctx; gpgme_key_t keys[2]; gpgme_import_result_t result; int ret = -1; if(_alpm_access(handle, handle->gpgdir, "pubring.gpg", W_OK)) { /* no chance of import succeeding if pubring isn't writable */ _alpm_log(handle, ALPM_LOG_ERROR, _("keyring is not writable\n")); return -1; } memset(&ctx, 0, sizeof(ctx)); gpg_err = gpgme_new(&ctx); CHECK_ERR(); _alpm_log(handle, ALPM_LOG_DEBUG, "importing key\n"); keys[0] = key->data; keys[1] = NULL; gpg_err = gpgme_op_import_keys(ctx, keys); CHECK_ERR(); result = gpgme_op_import_result(ctx); CHECK_ERR(); /* we know we tried to import exactly one key, so check for this */ if(result->considered != 1 || !result->imports) { _alpm_log(handle, ALPM_LOG_DEBUG, "could not import key, 0 results\n"); ret = -1; } else if(result->imports->result != GPG_ERR_NO_ERROR) { _alpm_log(handle, ALPM_LOG_DEBUG, "gpg error: %s\n", gpgme_strerror(gpg_err)); ret = -1; } else { ret = 0; } gpg_error: gpgme_release(ctx); return ret; }
/* ------------------ * import ascii armored key * ------------------ */ int import_key(char* armored_key) { gpgme_error_t error; gpgme_ctx_t ctx; gpgme_data_t keydata; gpgme_import_result_t result; // connect to gpgme gpgme_check_version (NULL); error = gpgme_new(&ctx); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_new failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); return FALSE; } purple_debug_info(PLUGIN_ID,"try to import key: %s\n",armored_key); // create data containers gpgme_data_new_from_mem (&keydata, armored_key,strlen(armored_key),1); // import key, ascii armored gpgme_set_armor(ctx,1); error = gpgme_op_import (ctx, keydata); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_op_import: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); gpgme_release (ctx); return FALSE; } result = gpgme_op_import_result (ctx); purple_debug_info(PLUGIN_ID,"considered keys: %d; imported keys: %d; not imported keys: %d\n",result->considered,result->imported,result->not_imported); // release memory for data containers gpgme_data_release(keydata); // close gpgme connection gpgme_release (ctx); return TRUE; }
void import ( gpgme_ctx_t * context, telem_gpg_opts * options, int num_keys, char ** key_path ) { gpgme_error_t err; int url_mode = 0; int nul_mode = 0; gpgme_import_result_t impres; gpgme_data_t data; for (; num_keys; --num_keys, key_path++) { printf ("reading file `%s'\n", *key_path); err = gpgme_data_new_from_file(&data, *key_path, 1); fail_if_err(err); if (url_mode) gpgme_data_set_encoding( data, (nul_mode? GPGME_DATA_ENCODING_URL0 : GPGME_DATA_ENCODING_URL) ); err = gpgme_op_import(*context, data); fail_if_err(err); impres = gpgme_op_import_result(*context); if (!impres) { fprintf(stderr, "No import result returned\n"); exit(1); } print_import_result(impres); gpgme_data_release(data); } }
int main(void) { gpgme_ctx_t ctx; gpgme_error_t err; int setup_res = setup(&ctx); if(setup_res) return setup_res; /* Change mode to extern */ gpgme_keylist_mode_t mode = gpgme_get_keylist_mode(ctx); mode &= ~GPGME_KEYLIST_MODE_LOCAL; mode |= GPGME_KEYLIST_MODE_EXTERN; mode |= GPGME_KEYLIST_MODE_SIGS | GPGME_KEYLIST_MODE_SIG_NOTATIONS; err = gpgme_set_keylist_mode(ctx, mode); if (err != GPG_ERR_NO_ERROR) return 6; /* List all keys */ /* (context, pattern, secret_only) */ err = gpgme_op_keylist_start(ctx, "07E7871B", 0); if (err != GPG_ERR_NO_ERROR) return 7; gpgme_key_t key; size_t MAX_KEYS = 100; gpgme_key_t keyarray[MAX_KEYS+1]; // need a space for the NULL term size_t i = 0; while ((err = gpgme_op_keylist_next(ctx, &key)) == GPG_ERR_NO_ERROR) { printf("Got a key\n"); printf ("%s:", key->subkeys->keyid); if (key->uids && key->uids->name) printf (" %s", key->uids->name); if (key->uids && key->uids->email) printf (" <%s>", key->uids->email); if (key->uids && key->uids->signatures) { printf("First signature: %s\n", key->uids->signatures->keyid); } putchar ('\n'); if (i < MAX_KEYS) { keyarray[i++] = key; } else { printf("Too many keys, skipping a key\n"); } //gpgme_key_release (key); } if (gpg_err_code(err) == GPG_ERR_EOF) { printf("%xu\n", err); printf("End of keylist.\n"); } else { print_gpg_error(err); return 8; } // https://bugs.gnupg.org/gnupg/issue1670 printf("Attempting to import %lu keys\n", i); err = gpgme_op_import_keys(ctx, keyarray); if (err != GPG_ERR_NO_ERROR) return 9; gpgme_import_result_t impres = gpgme_op_import_result(ctx); if (!impres) return 10; print_import_result(impres); /* free keys */ for (i = 0; keyarray[i]; i++) gpgme_key_release(key); /* free context */ gpgme_release(ctx); return 0; }